2 * RichEdit - functions dealing with editor object
4 * Copyright 2004 by Krzysztof Foltman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
25 void ME_EmptyUndoStack(ME_TextEditor *editor)
27 ME_DisplayItem *p, *pNext;
29 if (editor->nUndoMode == umIgnore)
32 TRACE("Emptying undo stack\n");
34 p = editor->pUndoStack;
35 editor->pUndoStack = editor->pUndoStackBottom = NULL;
36 editor->nUndoStackSize = 0;
39 ME_DestroyDisplayItem(p);
42 p = editor->pRedoStack;
43 editor->pRedoStack = NULL;
46 ME_DestroyDisplayItem(p);
51 ME_UndoItem *ME_AddUndoItem(ME_TextEditor *editor, ME_DIType type, const ME_DisplayItem *pdi) {
52 if (editor->nUndoMode == umIgnore)
54 else if (editor->nUndoLimit == 0)
58 ME_DisplayItem *pItem = (ME_DisplayItem *)ALLOC_OBJ(ME_UndoItem);
59 ((ME_UndoItem *)pItem)->nCR = ((ME_UndoItem *)pItem)->nLF = -1;
62 case diUndoPotentialEndTransaction:
63 /* only should be added for manually typed chars, not undos or redos */
64 assert(editor->nUndoMode == umAddToUndo);
65 /* intentional fall-through to next case */
66 case diUndoEndTransaction:
68 case diUndoSetParagraphFormat:
70 pItem->member.para = pdi->member.para;
71 pItem->member.para.pFmt = ALLOC_OBJ(PARAFORMAT2);
72 *pItem->member.para.pFmt = *pdi->member.para.pFmt;
76 pItem->member.run = pdi->member.run;
77 pItem->member.run.strText = ME_StrDup(pItem->member.run.strText);
78 ME_AddRefStyle(pItem->member.run.style);
79 if (pdi->member.run.ole_obj)
81 pItem->member.run.ole_obj = ALLOC_OBJ(*pItem->member.run.ole_obj);
82 ME_CopyReObject(pItem->member.run.ole_obj, pdi->member.run.ole_obj);
84 else pItem->member.run.ole_obj = NULL;
86 case diUndoSetCharFormat:
87 case diUndoSetDefaultCharFormat:
90 case diUndoJoinParagraphs:
92 case diUndoSplitParagraph:
93 pItem->member.para.pFmt = ALLOC_OBJ(PARAFORMAT2);
94 pItem->member.para.pFmt->cbSize = sizeof(PARAFORMAT2);
95 pItem->member.para.pFmt->dwMask = 0;
99 assert(0 == "AddUndoItem, unsupported item type");
104 if (editor->nUndoMode == umAddToUndo || editor->nUndoMode == umAddBackToUndo)
106 if (editor->pUndoStack
107 && editor->pUndoStack->type == diUndoPotentialEndTransaction)
109 editor->pUndoStack->type = diUndoEndTransaction;
111 if (editor->nUndoMode == umAddToUndo)
112 TRACE("Pushing id=%s to undo stack, deleting redo stack\n", ME_GetDITypeName(type));
114 TRACE("Pushing id=%s to undo stack\n", ME_GetDITypeName(type));
116 pItem->next = editor->pUndoStack;
117 if (type == diUndoEndTransaction || type == diUndoPotentialEndTransaction)
118 editor->nUndoStackSize++;
119 if (editor->pUndoStack)
120 editor->pUndoStack->prev = pItem;
122 editor->pUndoStackBottom = pItem;
123 editor->pUndoStack = pItem;
125 if (editor->nUndoStackSize > editor->nUndoLimit)
126 { /* remove oldest undo from stack */
127 ME_DisplayItem *p = editor->pUndoStackBottom;
128 while (p->type !=diUndoEndTransaction)
129 p = p->prev; /*find new stack bottom */
130 editor->pUndoStackBottom = p->prev;
131 editor->pUndoStackBottom->next = NULL;
134 ME_DisplayItem *pp = p->next;
135 ME_DestroyDisplayItem(p);
138 editor->nUndoStackSize--;
140 /* any new operation (not redo) clears the redo stack */
141 if (editor->nUndoMode == umAddToUndo) {
142 ME_DisplayItem *p = editor->pRedoStack;
145 ME_DisplayItem *pp = p->next;
146 ME_DestroyDisplayItem(p);
149 editor->pRedoStack = NULL;
152 else if (editor->nUndoMode == umAddToRedo)
154 TRACE("Pushing id=%s to redo stack\n", ME_GetDITypeName(type));
155 pItem->next = editor->pRedoStack;
156 if (editor->pRedoStack)
157 editor->pRedoStack->prev = pItem;
158 editor->pRedoStack = pItem;
162 return (ME_UndoItem *)pItem;
167 * Commits preceding changes into a transaction that can be undone together.
169 * This should be called after all the changes occur associated with an event
170 * so that the group of changes can be undone atomically as a transaction.
172 * This will have no effect the undo mode is set to ignore changes, or if no
173 * changes preceded calling this function before the last time it was called.
175 * This can also be used to conclude a coalescing transaction (used for grouping
178 void ME_CommitUndo(ME_TextEditor *editor) {
179 if (editor->nUndoMode == umIgnore)
182 assert(editor->nUndoMode == umAddToUndo);
184 /* no transactions, no need to commit */
185 if (!editor->pUndoStack)
188 /* no need to commit empty transactions */
189 if (editor->pUndoStack->type == diUndoEndTransaction)
192 if (editor->pUndoStack->type == diUndoPotentialEndTransaction)
194 /* Previous transaction was as a result of characters typed,
195 * so the end of this transaction is confirmed. */
196 editor->pUndoStack->type = diUndoEndTransaction;
200 ME_AddUndoItem(editor, diUndoEndTransaction, NULL);
201 ME_SendSelChange(editor);
205 * Groups supsequent changes with previous ones for an undo if coalescing.
207 * Has no effect if the previous changes were followed by a ME_CommitUndo. This
208 * function will only have an affect if the previous changes were followed by
209 * a call to ME_CommitCoalescingUndo, which allows the transaction to be
212 * This allows multiple consecutively typed characters to be grouped together
213 * to be undone by a single undo operation.
215 void ME_ContinueCoalescingTransaction(ME_TextEditor *editor)
219 if (editor->nUndoMode == umIgnore)
222 assert(editor->nUndoMode == umAddToUndo);
224 p = editor->pUndoStack;
226 if (p && p->type == diUndoPotentialEndTransaction) {
227 assert(p->next); /* EndTransactions shouldn't be at bottom of undo stack */
228 editor->pUndoStack = p->next;
229 editor->pUndoStack->prev = NULL;
230 editor->nUndoStackSize--;
231 ME_DestroyDisplayItem(p);
236 * Commits preceding changes into a undo transaction that can be expanded.
238 * This function allows the transaction to be reopened with
239 * ME_ContinueCoalescingTransaction in order to continue the transaction. If an
240 * undo item is added to the undo stack as a result of a change without the
241 * transaction being reopened, then the transaction will be ended, and the
242 * changes will become a part of the next transaction.
244 * This is used to allow typed characters to be grouped together since each
245 * typed character results in a single event, and each event adding undo items
246 * must be committed. Using this function as opposed to ME_CommitUndo allows
247 * multiple events to be grouped, and undone together.
249 void ME_CommitCoalescingUndo(ME_TextEditor *editor)
251 if (editor->nUndoMode == umIgnore)
254 assert(editor->nUndoMode == umAddToUndo);
256 /* no transactions, no need to commit */
257 if (!editor->pUndoStack)
260 /* no need to commit empty transactions */
261 if (editor->pUndoStack->type == diUndoEndTransaction)
263 if (editor->pUndoStack->type == diUndoPotentialEndTransaction)
266 ME_AddUndoItem(editor, diUndoPotentialEndTransaction, NULL);
267 ME_SendSelChange(editor);
270 static void ME_PlayUndoItem(ME_TextEditor *editor, ME_DisplayItem *pItem)
272 ME_UndoItem *pUItem = (ME_UndoItem *)pItem;
274 if (editor->nUndoMode == umIgnore)
276 TRACE("Playing undo/redo item, id=%s\n", ME_GetDITypeName(pItem->type));
280 case diUndoPotentialEndTransaction:
281 case diUndoEndTransaction:
283 case diUndoSetParagraphFormat:
286 ME_CursorFromCharOfs(editor, pItem->member.para.nCharOfs, &tmp);
287 ME_SetParaFormat(editor, ME_FindItemBack(tmp.pRun, diParagraph), pItem->member.para.pFmt);
290 case diUndoSetCharFormat:
292 ME_SetCharFormat(editor, pUItem->nStart, pUItem->nLen, &pItem->member.ustyle->fmt);
295 case diUndoSetDefaultCharFormat:
297 ME_SetDefaultCharFormat(editor, &pItem->member.ustyle->fmt);
300 case diUndoInsertRun:
302 ME_InsertRun(editor, pItem->member.run.nCharOfs, pItem);
305 case diUndoDeleteRun:
307 ME_InternalDeleteText(editor, pUItem->nStart, pUItem->nLen);
310 case diUndoJoinParagraphs:
313 ME_CursorFromCharOfs(editor, pUItem->nStart, &tmp);
314 /* the only thing that's needed is paragraph offset, so no need to split runs */
315 ME_JoinParagraphs(editor, ME_GetParagraph(tmp.pRun));
318 case diUndoSplitParagraph:
321 ME_DisplayItem *new_para;
322 ME_CursorFromCharOfs(editor, pUItem->nStart, &tmp);
324 tmp.pRun = ME_SplitRunSimple(editor, tmp.pRun, tmp.nOffset);
325 assert(pUItem->nCR >= 0);
326 assert(pUItem->nLF >= 0);
327 new_para = ME_SplitParagraph(editor, tmp.pRun, tmp.pRun->member.run.style,
328 pUItem->nCR, pUItem->nLF);
329 assert(pItem->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
330 *new_para->member.para.pFmt = *pItem->member.para.pFmt;
334 assert(0 == "PlayUndoItem, unexpected type");
338 BOOL ME_Undo(ME_TextEditor *editor) {
340 ME_UndoMode nMode = editor->nUndoMode;
342 if (editor->nUndoMode == umIgnore)
344 assert(nMode == umAddToUndo || nMode == umIgnore);
346 /* no undo items ? */
347 if (!editor->pUndoStack)
350 /* watch out for uncommitted transactions ! */
351 assert(editor->pUndoStack->type == diUndoEndTransaction
352 || editor->pUndoStack->type == diUndoPotentialEndTransaction);
354 editor->nUndoMode = umAddToRedo;
355 p = editor->pUndoStack->next;
356 ME_DestroyDisplayItem(editor->pUndoStack);
357 editor->pUndoStack = p;
360 ME_PlayUndoItem(editor, p);
361 editor->pUndoStack = p->next;
362 ME_DestroyDisplayItem(p);
363 p = editor->pUndoStack;
364 } while(p && p->type != diUndoEndTransaction);
367 ME_AddUndoItem(editor, diUndoEndTransaction, NULL);
368 editor->nUndoStackSize--;
369 editor->nUndoMode = nMode;
370 ME_UpdateRepaint(editor);
374 BOOL ME_Redo(ME_TextEditor *editor) {
376 ME_UndoMode nMode = editor->nUndoMode;
378 assert(nMode == umAddToUndo || nMode == umIgnore);
380 if (editor->nUndoMode == umIgnore)
382 /* no redo items ? */
383 if (!editor->pRedoStack)
386 /* watch out for uncommitted transactions ! */
387 assert(editor->pRedoStack->type == diUndoEndTransaction);
389 editor->nUndoMode = umAddBackToUndo;
390 p = editor->pRedoStack->next;
391 ME_DestroyDisplayItem(editor->pRedoStack);
392 editor->pRedoStack = p;
395 ME_PlayUndoItem(editor, p);
396 editor->pRedoStack = p->next;
397 ME_DestroyDisplayItem(p);
398 p = editor->pRedoStack;
399 } while(p && p->type != diUndoEndTransaction);
402 ME_AddUndoItem(editor, diUndoEndTransaction, NULL);
403 editor->nUndoMode = nMode;
404 ME_UpdateRepaint(editor);