riched20: Use helper function rather than goto to return found position.
[wine] / dlls / riched20 / undo.c
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 = (ME_DisplayItem *)ALLOC_OBJ(ME_UndoItem);
59     ((ME_UndoItem *)pItem)->nCR = ((ME_UndoItem *)pItem)->nLF = -1;
60     switch(type)
61     {
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:
67       break;
68     case diUndoSetParagraphFormat:
69       assert(pdi);
70       pItem->member.para = pdi->member.para;
71       pItem->member.para.pFmt = ALLOC_OBJ(PARAFORMAT2);
72       *pItem->member.para.pFmt = *pdi->member.para.pFmt;
73       break;
74     case diUndoInsertRun:
75       assert(pdi);
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)
80       {
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);
83       }
84       else pItem->member.run.ole_obj = NULL;
85       break;
86     case diUndoSetCharFormat:
87       break;
88     case diUndoDeleteRun:
89     case diUndoJoinParagraphs:
90       break;
91     case diUndoSplitParagraph:
92     {
93       ME_DisplayItem *prev_para = pdi->member.para.prev_para;
94       assert(pdi->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
95       pItem->member.para.pFmt = ALLOC_OBJ(PARAFORMAT2);
96       pItem->member.para.pFmt->cbSize = sizeof(PARAFORMAT2);
97       pItem->member.para.pFmt->dwMask = 0;
98       *pItem->member.para.pFmt = *pdi->member.para.pFmt;
99       pItem->member.para.nFlags = prev_para->member.para.nFlags & ~MEPF_CELL;
100       pItem->member.para.pCell = NULL;
101       break;
102     }
103     default:
104       assert(0 == "AddUndoItem, unsupported item type");
105       return NULL;
106     }
107     pItem->type = type;
108     pItem->prev = NULL;
109     if (editor->nUndoMode == umAddToUndo || editor->nUndoMode == umAddBackToUndo)
110     {
111       if (editor->pUndoStack
112           && editor->pUndoStack->type == diUndoPotentialEndTransaction)
113       {
114           editor->pUndoStack->type = diUndoEndTransaction;
115       }
116       if (editor->nUndoMode == umAddToUndo)
117         TRACE("Pushing id=%s to undo stack, deleting redo stack\n", ME_GetDITypeName(type));
118       else
119         TRACE("Pushing id=%s to undo stack\n", ME_GetDITypeName(type));
120
121       pItem->next = editor->pUndoStack;
122       if (type == diUndoEndTransaction || type == diUndoPotentialEndTransaction)
123         editor->nUndoStackSize++;
124       if (editor->pUndoStack)
125         editor->pUndoStack->prev = pItem;
126       else
127         editor->pUndoStackBottom = pItem;
128       editor->pUndoStack = pItem;
129       
130       if (editor->nUndoStackSize > editor->nUndoLimit)
131       { /* remove oldest undo from stack */
132         ME_DisplayItem *p = editor->pUndoStackBottom;
133         while (p->type !=diUndoEndTransaction)
134           p = p->prev; /*find new stack bottom */
135         editor->pUndoStackBottom = p->prev;
136           editor->pUndoStackBottom->next = NULL;
137         do
138         {
139           ME_DisplayItem *pp = p->next;
140           ME_DestroyDisplayItem(p);
141           p = pp;
142         } while (p);
143         editor->nUndoStackSize--;
144       }
145       /* any new operation (not redo) clears the redo stack */
146       if (editor->nUndoMode == umAddToUndo) {
147         ME_DisplayItem *p = editor->pRedoStack;
148         while(p)
149         {
150           ME_DisplayItem *pp = p->next;
151           ME_DestroyDisplayItem(p);
152           p = pp;
153         }
154         editor->pRedoStack = NULL;
155       }
156     }
157     else if (editor->nUndoMode == umAddToRedo)
158     {
159       TRACE("Pushing id=%s to redo stack\n", ME_GetDITypeName(type));
160       pItem->next = editor->pRedoStack;
161       if (editor->pRedoStack)
162         editor->pRedoStack->prev = pItem;
163       editor->pRedoStack = pItem;
164     }
165     else
166       assert(0);
167     return (ME_UndoItem *)pItem;
168   }
169 }
170
171 /**
172  * Commits preceding changes into a transaction that can be undone together.
173  *
174  * This should be called after all the changes occur associated with an event
175  * so that the group of changes can be undone atomically as a transaction.
176  *
177  * This will have no effect the undo mode is set to ignore changes, or if no
178  * changes preceded calling this function before the last time it was called.
179  *
180  * This can also be used to conclude a coalescing transaction (used for grouping
181  * typed characters).
182  */
183 void ME_CommitUndo(ME_TextEditor *editor) {
184   if (editor->nUndoMode == umIgnore)
185     return;
186   
187   assert(editor->nUndoMode == umAddToUndo);
188   
189   /* no transactions, no need to commit */
190   if (!editor->pUndoStack)
191     return;
192
193   /* no need to commit empty transactions */
194   if (editor->pUndoStack->type == diUndoEndTransaction)
195     return;
196     
197   if (editor->pUndoStack->type == diUndoPotentialEndTransaction)
198   {
199       /* Previous transaction was as a result of characters typed,
200        * so the end of this transaction is confirmed. */
201       editor->pUndoStack->type = diUndoEndTransaction;
202       return;
203   }
204
205   ME_AddUndoItem(editor, diUndoEndTransaction, NULL);
206   ME_SendSelChange(editor);
207 }
208
209 /**
210  * Groups supsequent changes with previous ones for an undo if coalescing.
211  *
212  * Has no effect if the previous changes were followed by a ME_CommitUndo. This
213  * function will only have an affect if the previous changes were followed by
214  * a call to ME_CommitCoalescingUndo, which allows the transaction to be
215  * continued.
216  *
217  * This allows multiple consecutively typed characters to be grouped together
218  * to be undone by a single undo operation.
219  */
220 void ME_ContinueCoalescingTransaction(ME_TextEditor *editor)
221 {
222   ME_DisplayItem* p;
223
224   if (editor->nUndoMode == umIgnore)
225     return;
226
227   assert(editor->nUndoMode == umAddToUndo);
228
229   p = editor->pUndoStack;
230
231   if (p && p->type == diUndoPotentialEndTransaction) {
232     assert(p->next); /* EndTransactions shouldn't be at bottom of undo stack */
233     editor->pUndoStack = p->next;
234     editor->pUndoStack->prev = NULL;
235     editor->nUndoStackSize--;
236     ME_DestroyDisplayItem(p);
237   }
238 }
239
240 /**
241  * Commits preceding changes into a undo transaction that can be expanded.
242  *
243  * This function allows the transaction to be reopened with
244  * ME_ContinueCoalescingTransaction in order to continue the transaction.  If an
245  * undo item is added to the undo stack as a result of a change without the
246  * transaction being reopened, then the transaction will be ended, and the
247  * changes will become a part of the next transaction.
248  *
249  * This is used to allow typed characters to be grouped together since each
250  * typed character results in a single event, and each event adding undo items
251  * must be committed.  Using this function as opposed to ME_CommitUndo allows
252  * multiple events to be grouped, and undone together.
253  */
254 void ME_CommitCoalescingUndo(ME_TextEditor *editor)
255 {
256   if (editor->nUndoMode == umIgnore)
257     return;
258
259   assert(editor->nUndoMode == umAddToUndo);
260
261   /* no transactions, no need to commit */
262   if (!editor->pUndoStack)
263     return;
264
265   /* no need to commit empty transactions */
266   if (editor->pUndoStack->type == diUndoEndTransaction)
267     return;
268   if (editor->pUndoStack->type == diUndoPotentialEndTransaction)
269     return;
270
271   ME_AddUndoItem(editor, diUndoPotentialEndTransaction, NULL);
272   ME_SendSelChange(editor);
273 }
274
275 static void ME_PlayUndoItem(ME_TextEditor *editor, ME_DisplayItem *pItem)
276 {
277   ME_UndoItem *pUItem = (ME_UndoItem *)pItem;
278
279   if (editor->nUndoMode == umIgnore)
280     return;
281   TRACE("Playing undo/redo item, id=%s\n", ME_GetDITypeName(pItem->type));
282
283   switch(pItem->type)
284   {
285   case diUndoPotentialEndTransaction:
286   case diUndoEndTransaction:
287     assert(0);
288   case diUndoSetParagraphFormat:
289   {
290     ME_Cursor tmp;
291     ME_DisplayItem *para;
292     ME_CursorFromCharOfs(editor, pItem->member.para.nCharOfs, &tmp);
293     para = ME_FindItemBack(tmp.pRun, diParagraph);
294     ME_AddUndoItem(editor, diUndoSetParagraphFormat, para);
295     *para->member.para.pFmt = *pItem->member.para.pFmt;
296     break;
297   }
298   case diUndoSetCharFormat:
299   {
300     ME_SetCharFormat(editor, pUItem->nStart, pUItem->nLen, &pItem->member.ustyle->fmt);
301     break;
302   }
303   case diUndoInsertRun:
304   {
305     ME_InsertRun(editor, pItem->member.run.nCharOfs, pItem);
306     break;
307   }
308   case diUndoDeleteRun:
309   {
310     ME_InternalDeleteText(editor, pUItem->nStart, pUItem->nLen, TRUE);
311     break;
312   }
313   case diUndoJoinParagraphs:
314   {
315     ME_Cursor tmp;
316     ME_CursorFromCharOfs(editor, pUItem->nStart, &tmp);
317     /* the only thing that's needed is paragraph offset, so no need to split runs */
318     ME_JoinParagraphs(editor, ME_GetParagraph(tmp.pRun), TRUE);
319     break;
320   }
321   case diUndoSplitParagraph:
322   {
323     ME_Cursor tmp;
324     ME_DisplayItem *this_para, *new_para;
325     BOOL bFixRowStart;
326     int paraFlags = pItem->member.para.nFlags & (MEPF_ROWSTART|MEPF_CELL|MEPF_ROWEND);
327     ME_CursorFromCharOfs(editor, pUItem->nStart, &tmp);
328     if (tmp.nOffset)
329       tmp.pRun = ME_SplitRunSimple(editor, tmp.pRun, tmp.nOffset);
330     assert(pUItem->nCR >= 0);
331     assert(pUItem->nLF >= 0);
332     this_para = ME_GetParagraph(tmp.pRun);
333     bFixRowStart = this_para->member.para.nFlags & MEPF_ROWSTART;
334     if (bFixRowStart)
335     {
336       /* Re-insert the paragraph before the table, making sure the nFlag value
337        * is correct. */
338       this_para->member.para.nFlags &= ~MEPF_ROWSTART;
339     }
340     new_para = ME_SplitParagraph(editor, tmp.pRun, tmp.pRun->member.run.style,
341                                  pUItem->nCR, pUItem->nLF, paraFlags);
342     if (bFixRowStart)
343       new_para->member.para.nFlags |= MEPF_ROWSTART;
344     assert(pItem->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
345     *new_para->member.para.pFmt = *pItem->member.para.pFmt;
346     if (pItem->member.para.pCell)
347     {
348       ME_DisplayItem *pItemCell, *pCell;
349       pItemCell = pItem->member.para.pCell;
350       pCell = new_para->member.para.pCell;
351       pCell->member.cell.nRightBoundary = pItemCell->member.cell.nRightBoundary;
352     }
353     break;
354   }
355   default:
356     assert(0 == "PlayUndoItem, unexpected type");
357   }
358 }
359
360 BOOL ME_Undo(ME_TextEditor *editor) {
361   ME_DisplayItem *p;
362   ME_UndoMode nMode = editor->nUndoMode;
363   
364   if (editor->nUndoMode == umIgnore)
365     return FALSE;
366   assert(nMode == umAddToUndo || nMode == umIgnore);
367   
368   /* no undo items ? */
369   if (!editor->pUndoStack)
370     return FALSE;
371     
372   /* watch out for uncommitted transactions ! */
373   assert(editor->pUndoStack->type == diUndoEndTransaction
374         || editor->pUndoStack->type == diUndoPotentialEndTransaction);
375   
376   editor->nUndoMode = umAddToRedo;
377   p = editor->pUndoStack->next;
378   ME_DestroyDisplayItem(editor->pUndoStack);
379   editor->pUndoStack = p;
380   do {
381     p->prev = NULL;
382     ME_PlayUndoItem(editor, p);
383     editor->pUndoStack = p->next;
384     ME_DestroyDisplayItem(p);
385     p = editor->pUndoStack;
386   } while(p && p->type != diUndoEndTransaction);
387   if (p)
388     p->prev = NULL;
389   ME_AddUndoItem(editor, diUndoEndTransaction, NULL);
390   ME_CheckTablesForCorruption(editor);
391   editor->nUndoStackSize--;
392   editor->nUndoMode = nMode;
393   ME_UpdateRepaint(editor);
394   return TRUE;
395 }
396
397 BOOL ME_Redo(ME_TextEditor *editor) {
398   ME_DisplayItem *p;
399   ME_UndoMode nMode = editor->nUndoMode;
400   
401   assert(nMode == umAddToUndo || nMode == umIgnore);
402   
403   if (editor->nUndoMode == umIgnore)
404     return FALSE;
405   /* no redo items ? */
406   if (!editor->pRedoStack)
407     return FALSE;
408     
409   /* watch out for uncommitted transactions ! */
410   assert(editor->pRedoStack->type == diUndoEndTransaction);
411   
412   editor->nUndoMode = umAddBackToUndo;
413   p = editor->pRedoStack->next;
414   ME_DestroyDisplayItem(editor->pRedoStack);
415   editor->pRedoStack = p;
416   do {
417     p->prev = NULL;
418     ME_PlayUndoItem(editor, p);
419     editor->pRedoStack = p->next;
420     ME_DestroyDisplayItem(p);
421     p = editor->pRedoStack;
422   } while(p && p->type != diUndoEndTransaction);
423   if (p)
424     p->prev = NULL;
425   ME_AddUndoItem(editor, diUndoEndTransaction, NULL);
426   ME_CheckTablesForCorruption(editor);
427   editor->nUndoMode = nMode;
428   ME_UpdateRepaint(editor);
429   return TRUE;
430 }