1 /*
2 * RichEdit - functions dealing with editor object
3 *
4 * Copyright 2004 by Krzysztof Foltman
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "editor.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
24
25 void ME_EmptyUndoStack(ME_TextEditor *editor)
26 {
27 ME_DisplayItem *p, *pNext;
28
29 if (editor->nUndoMode == umIgnore)
30 return;
31
32 TRACE("Emptying undo stack\n");
33
34 p = editor->pUndoStack;
35 editor->pUndoStack = editor->pUndoStackBottom = NULL;
36 editor->nUndoStackSize = 0;
37 while(p) {
38 pNext = p->next;
39 ME_DestroyDisplayItem(p);
40 p = pNext;
41 }
42 p = editor->pRedoStack;
43 editor->pRedoStack = NULL;
44 while(p) {
45 pNext = p->next;
46 ME_DestroyDisplayItem(p);
47 p = pNext;
48 }
49 }
50
51 ME_UndoItem *ME_AddUndoItem(ME_TextEditor *editor, ME_DIType type, const ME_DisplayItem *pdi) {
52 if (editor->nUndoMode == umIgnore)
53 return NULL;
54 else if (editor->nUndoLimit == 0)
55 return NULL;
56 else
57 {
58 ME_DisplayItem *pItem;
59 if (editor->pUndoStack
60 && editor->pUndoStack->type == diUndoPotentialEndTransaction)
61 {
62 editor->pUndoStack->type = diUndoEndTransaction;
63 }
64 pItem = (ME_DisplayItem *)ALLOC_OBJ(ME_UndoItem);
65 ((ME_UndoItem *)pItem)->nCR = ((ME_UndoItem *)pItem)->nLF = -1;
66 switch(type)
67 {
68 case diUndoPotentialEndTransaction:
69 /* only should be added for manually typed chars, not undos or redos */
70 assert(editor->nUndoMode == umAddToUndo);
71 /* intentional fall-through to next case */
72 case diUndoEndTransaction:
73 break;
74 case diUndoSetParagraphFormat:
75 assert(pdi);
76 pItem->member.para = pdi->member.para;
77 pItem->member.para.pFmt = ALLOC_OBJ(PARAFORMAT2);
78 *pItem->member.para.pFmt = *pdi->member.para.pFmt;
79 break;
80 case diUndoInsertRun:
81 assert(pdi);
82 pItem->member.run = pdi->member.run;
83 pItem->member.run.strText = ME_StrDup(pItem->member.run.strText);
84 ME_AddRefStyle(pItem->member.run.style);
85 if (pdi->member.run.ole_obj)
86 {
87 pItem->member.run.ole_obj = ALLOC_OBJ(*pItem->member.run.ole_obj);
88 ME_CopyReObject(pItem->member.run.ole_obj, pdi->member.run.ole_obj);
89 }
90 else pItem->member.run.ole_obj = NULL;
91 break;
92 case diUndoSetCharFormat:
93 case diUndoSetDefaultCharFormat:
94 break;
95 case diUndoDeleteRun:
96 case diUndoJoinParagraphs:
97 break;
98 case diUndoSplitParagraph:
99 pItem->member.para.pFmt = ALLOC_OBJ(PARAFORMAT2);
100 pItem->member.para.pFmt->cbSize = sizeof(PARAFORMAT2);
101 pItem->member.para.pFmt->dwMask = 0;
102
103 break;
104 default:
105 assert(0 == "AddUndoItem, unsupported item type");
106 return NULL;
107 }
108 pItem->type = type;
109 pItem->prev = NULL;
110 if (editor->nUndoMode == umAddToUndo || editor->nUndoMode == umAddBackToUndo)
111 {
112 if (editor->nUndoMode == umAddToUndo)
113 TRACE("Pushing id=%s to undo stack, deleting redo stack\n", ME_GetDITypeName(type));
114 else
115 TRACE("Pushing id=%s to undo stack\n", ME_GetDITypeName(type));
116
117 pItem->next = editor->pUndoStack;
118 if (type == diUndoEndTransaction || type == diUndoPotentialEndTransaction)
119 editor->nUndoStackSize++;
120 if (editor->pUndoStack)
121 editor->pUndoStack->prev = pItem;
122 else
123 editor->pUndoStackBottom = pItem;
124 editor->pUndoStack = pItem;
125
126 if (editor->nUndoStackSize > editor->nUndoLimit)
127 { /* remove oldest undo from stack */
128 ME_DisplayItem *p = editor->pUndoStackBottom;
129 while (p->type !=diUndoEndTransaction)
130 p = p->prev; /*find new stack bottom */
131 editor->pUndoStackBottom = p->prev;
132 editor->pUndoStackBottom->next = NULL;
133 do
134 {
135 ME_DisplayItem *pp = p->next;
136 ME_DestroyDisplayItem(p);
137 p = pp;
138 } while (p);
139 editor->nUndoStackSize--;
140 }
141 /* any new operation (not redo) clears the redo stack */
142 if (editor->nUndoMode == umAddToUndo) {
143 ME_DisplayItem *p = editor->pRedoStack;
144 while(p)
145 {
146 ME_DisplayItem *pp = p->next;
147 ME_DestroyDisplayItem(p);
148 p = pp;
149 }
150 editor->pRedoStack = NULL;
151 }
152 }
153 else if (editor->nUndoMode == umAddToRedo)
154 {
155 TRACE("Pushing id=%s to redo stack\n", ME_GetDITypeName(type));
156 pItem->next = editor->pRedoStack;
157 if (editor->pRedoStack)
158 editor->pRedoStack->prev = pItem;
159 editor->pRedoStack = pItem;
160 }
161 else
162 assert(0);
163 return (ME_UndoItem *)pItem;
164 }
165 }
166
167 /**
168 * Commits preceding changes into a transaction that can be undone together.
169 *
170 * This should be called after all the changes occur associated with an event
171 * so that the group of changes can be undone atomically as a transaction.
172 *
173 * This will have no effect the undo mode is set to ignore changes, or if no
174 * changes preceded calling this function before the last time it was called.
175 *
176 * This can also be used to conclude a coalescing transaction (used for grouping
177 * typed characters).
178 */
179 void ME_CommitUndo(ME_TextEditor *editor) {
180 if (editor->nUndoMode == umIgnore)
181 return;
182
183 assert(editor->nUndoMode == umAddToUndo);
184
185 /* no transactions, no need to commit */
186 if (!editor->pUndoStack)
187 return;
188
189 /* no need to commit empty transactions */
190 if (editor->pUndoStack->type == diUndoEndTransaction)
191 return;
192
193 if (editor->pUndoStack->type == diUndoPotentialEndTransaction)
194 {
195 /* Previous transaction was as a result of characters typed,
196 * so the end of this transaction is confirmed. */
197 editor->pUndoStack->type = diUndoEndTransaction;
198 return;
199 }
200
201 ME_AddUndoItem(editor, diUndoEndTransaction, NULL);
202 ME_SendSelChange(editor);
203 }
204
205 /**
206 * Groups supsequent changes with previous ones for an undo if coalescing.
207 *
208 * Has no effect if the previous changes were followed by a ME_CommitUndo. This
209 * function will only have an affect if the previous changes were followed by
210 * a call to ME_CommitCoalescingUndo, which allows the transaction to be
211 * continued.
212 *
213 * This allows multiple consecutively typed characters to be grouped together
214 * to be undone by a single undo operation.
215 */
216 void ME_ContinueCoalescingTransaction(ME_TextEditor *editor)
217 {
218 ME_DisplayItem* p;
219
220 if (editor->nUndoMode == umIgnore)
221 return;
222
223 assert(editor->nUndoMode == umAddToUndo);
224
225 p = editor->pUndoStack;
226
227 if (p && p->type == diUndoPotentialEndTransaction) {
228 assert(p->next); /* EndTransactions shouldn't be at bottom of undo stack */
229 editor->pUndoStack = p->next;
230 editor->pUndoStack->prev = NULL;
231 editor->nUndoStackSize--;
232 ME_DestroyDisplayItem(p);
233 }
234 }
235
236 /**
237 * Commits preceding changes into a undo transaction that can be expanded.
238 *
239 * This function allows the transaction to be reopened with
240 * ME_ContinueCoalescingTransaction in order to continue the transaction. If an
241 * undo item is added to the undo stack as a result of a change without the
242 * transaction being reopened, then the transaction will be ended, and the
243 * changes will become a part of the next transaction.
244 *
245 * This is used to allow typed characters to be grouped together since each
246 * typed character results in a single event, and each event adding undo items
247 * must be committed. Using this function as opposed to ME_CommitUndo allows
248 * multiple events to be grouped, and undone together.
249 */
250 void ME_CommitCoalescingUndo(ME_TextEditor *editor)
251 {
252 if (editor->nUndoMode == umIgnore)
253 return;
254
255 assert(editor->nUndoMode == umAddToUndo);
256
257 /* no transactions, no need to commit */
258 if (!editor->pUndoStack)
259 return;
260
261 /* no need to commit empty transactions */
262 if (editor->pUndoStack->type == diUndoEndTransaction)
263 return;
264 if (editor->pUndoStack->type == diUndoPotentialEndTransaction)
265 return;
266
267 ME_AddUndoItem(editor, diUndoPotentialEndTransaction, NULL);
268 ME_SendSelChange(editor);
269 }
270
271 static void ME_PlayUndoItem(ME_TextEditor *editor, ME_DisplayItem *pItem)
272 {
273 ME_UndoItem *pUItem = (ME_UndoItem *)pItem;
274
275 if (editor->nUndoMode == umIgnore)
276 return;
277 TRACE("Playing undo/redo item, id=%s\n", ME_GetDITypeName(pItem->type));
278
279 switch(pItem->type)
280 {
281 case diUndoPotentialEndTransaction:
282 case diUndoEndTransaction:
283 assert(0);
284 case diUndoSetParagraphFormat:
285 {
286 ME_Cursor tmp;
287 ME_CursorFromCharOfs(editor, pItem->member.para.nCharOfs, &tmp);
288 ME_SetParaFormat(editor, ME_FindItemBack(tmp.pRun, diParagraph), pItem->member.para.pFmt);
289 break;
290 }
291 case diUndoSetCharFormat:
292 {
293 ME_SetCharFormat(editor, pUItem->nStart, pUItem->nLen, &pItem->member.ustyle->fmt);
294 break;
295 }
296 case diUndoSetDefaultCharFormat:
297 {
298 ME_SetDefaultCharFormat(editor, &pItem->member.ustyle->fmt);
299 break;
300 }
301 case diUndoInsertRun:
302 {
303 ME_InsertRun(editor, pItem->member.run.nCharOfs, pItem);
304 break;
305 }
306 case diUndoDeleteRun:
307 {
308 ME_InternalDeleteText(editor, pUItem->nStart, pUItem->nLen);
309 break;
310 }
311 case diUndoJoinParagraphs:
312 {
313 ME_Cursor tmp;
314 ME_CursorFromCharOfs(editor, pUItem->nStart, &tmp);
315 /* the only thing that's needed is paragraph offset, so no need to split runs */
316 ME_JoinParagraphs(editor, ME_GetParagraph(tmp.pRun));
317 break;
318 }
319 case diUndoSplitParagraph:
320 {
321 ME_Cursor tmp;
322 ME_DisplayItem *new_para;
323 ME_CursorFromCharOfs(editor, pUItem->nStart, &tmp);
324 if (tmp.nOffset)
325 tmp.pRun = ME_SplitRunSimple(editor, tmp.pRun, tmp.nOffset);
326 assert(pUItem->nCR >= 0);
327 assert(pUItem->nLF >= 0);
328 new_para = ME_SplitParagraph(editor, tmp.pRun, tmp.pRun->member.run.style,
329 pUItem->nCR, pUItem->nLF);
330 assert(pItem->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
331 *new_para->member.para.pFmt = *pItem->member.para.pFmt;
332 break;
333 }
334 default:
335 assert(0 == "PlayUndoItem, unexpected type");
336 }
337 }
338
339 BOOL ME_Undo(ME_TextEditor *editor) {
340 ME_DisplayItem *p;
341 ME_UndoMode nMode = editor->nUndoMode;
342
343 if (editor->nUndoMode == umIgnore)
344 return FALSE;
345 assert(nMode == umAddToUndo || nMode == umIgnore);
346
347 /* no undo items ? */
348 if (!editor->pUndoStack)
349 return FALSE;
350
351 /* watch out for uncommitted transactions ! */
352 assert(editor->pUndoStack->type == diUndoEndTransaction
353 || editor->pUndoStack->type == diUndoPotentialEndTransaction);
354
355 editor->nUndoMode = umAddToRedo;
356 p = editor->pUndoStack->next;
357 ME_DestroyDisplayItem(editor->pUndoStack);
358 do {
359 ME_DisplayItem *pp = p;
360 ME_PlayUndoItem(editor, p);
361 p = p->next;
362 ME_DestroyDisplayItem(pp);
363 } while(p && p->type != diUndoEndTransaction);
364 ME_AddUndoItem(editor, diUndoEndTransaction, NULL);
365 editor->pUndoStack = p;
366 editor->nUndoStackSize--;
367 if (p)
368 p->prev = NULL;
369 editor->nUndoMode = nMode;
370 ME_UpdateRepaint(editor);
371 return TRUE;
372 }
373
374 BOOL ME_Redo(ME_TextEditor *editor) {
375 ME_DisplayItem *p;
376 ME_UndoMode nMode = editor->nUndoMode;
377
378 assert(nMode == umAddToUndo || nMode == umIgnore);
379
380 if (editor->nUndoMode == umIgnore)
381 return FALSE;
382 /* no redo items ? */
383 if (!editor->pRedoStack)
384 return FALSE;
385
386 /* watch out for uncommitted transactions ! */
387 assert(editor->pRedoStack->type == diUndoEndTransaction);
388
389 editor->nUndoMode = umAddBackToUndo;
390 p = editor->pRedoStack->next;
391 ME_DestroyDisplayItem(editor->pRedoStack);
392 do {
393 ME_DisplayItem *pp = p;
394 ME_PlayUndoItem(editor, p);
395 p = p->next;
396 ME_DestroyDisplayItem(pp);
397 } while(p && p->type != diUndoEndTransaction);
398 ME_AddUndoItem(editor, diUndoEndTransaction, NULL);
399 editor->pRedoStack = p;
400 if (p)
401 p->prev = NULL;
402 editor->nUndoMode = nMode;
403 ME_UpdateRepaint(editor);
404 return TRUE;
405 }
406
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.