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 = ALLOC_OBJ(ME_UndoItem);
61 case diUndoPotentialEndTransaction:
62 /* only should be added for manually typed chars, not undos or redos */
63 assert(editor->nUndoMode == umAddToUndo);
64 /* intentional fall-through to next case */
65 case diUndoEndTransaction:
67 case diUndoSetParagraphFormat:
69 pItem->member.para = pdi->member.para;
70 pItem->member.para.pFmt = ALLOC_OBJ(PARAFORMAT2);
71 *pItem->member.para.pFmt = *pdi->member.para.pFmt;
75 pItem->member.run = pdi->member.run;
76 pItem->member.run.strText = ME_StrDup(pItem->member.run.strText);
77 ME_AddRefStyle(pItem->member.run.style);
78 if (pdi->member.run.ole_obj)
80 pItem->member.run.ole_obj = ALLOC_OBJ(*pItem->member.run.ole_obj);
81 ME_CopyReObject(pItem->member.run.ole_obj, pdi->member.run.ole_obj);
83 else pItem->member.run.ole_obj = NULL;
85 case diUndoSetCharFormat:
88 case diUndoJoinParagraphs:
90 case diUndoSplitParagraph:
92 ME_DisplayItem *prev_para = pdi->member.para.prev_para;
93 assert(pdi->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
94 pItem->member.para.pFmt = ALLOC_OBJ(PARAFORMAT2);
95 pItem->member.para.pFmt->cbSize = sizeof(PARAFORMAT2);
96 pItem->member.para.pFmt->dwMask = 0;
97 *pItem->member.para.pFmt = *pdi->member.para.pFmt;
98 pItem->member.para.border = pdi->member.para.border;
99 pItem->member.para.nFlags = prev_para->member.para.nFlags & ~MEPF_CELL;
100 pItem->member.para.pCell = NULL;
104 assert(0 == "AddUndoItem, unsupported item type");
109 if (editor->nUndoMode == umAddToUndo || editor->nUndoMode == umAddBackToUndo)
111 if (editor->pUndoStack
112 && editor->pUndoStack->type == diUndoPotentialEndTransaction)
114 editor->pUndoStack->type = diUndoEndTransaction;
116 if (editor->nUndoMode == umAddToUndo)
117 TRACE("Pushing id=%s to undo stack, deleting redo stack\n", ME_GetDITypeName(type));
119 TRACE("Pushing id=%s to undo stack\n", ME_GetDITypeName(type));
121 pItem->next = editor->pUndoStack;
122 if (type == diUndoEndTransaction || type == diUndoPotentialEndTransaction)
123 editor->nUndoStackSize++;
124 if (editor->pUndoStack)
125 editor->pUndoStack->prev = pItem;
127 editor->pUndoStackBottom = pItem;
128 editor->pUndoStack = pItem;
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;
139 ME_DisplayItem *pp = p->next;
140 ME_DestroyDisplayItem(p);
143 editor->nUndoStackSize--;
145 /* any new operation (not redo) clears the redo stack */
146 if (editor->nUndoMode == umAddToUndo) {
147 ME_DisplayItem *p = editor->pRedoStack;
150 ME_DisplayItem *pp = p->next;
151 ME_DestroyDisplayItem(p);
154 editor->pRedoStack = NULL;
157 else if (editor->nUndoMode == umAddToRedo)
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;
167 return (ME_UndoItem *)pItem;
172 * Commits preceding changes into a transaction that can be undone together.
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.
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.
180 * This can also be used to conclude a coalescing transaction (used for grouping
183 void ME_CommitUndo(ME_TextEditor *editor) {
184 if (editor->nUndoMode == umIgnore)
187 assert(editor->nUndoMode == umAddToUndo);
189 /* no transactions, no need to commit */
190 if (!editor->pUndoStack)
193 /* no need to commit empty transactions */
194 if (editor->pUndoStack->type == diUndoEndTransaction)
197 if (editor->pUndoStack->type == diUndoPotentialEndTransaction)
199 /* Previous transaction was as a result of characters typed,
200 * so the end of this transaction is confirmed. */
201 editor->pUndoStack->type = diUndoEndTransaction;
205 ME_AddUndoItem(editor, diUndoEndTransaction, NULL);
206 ME_SendSelChange(editor);
210 * Groups supsequent changes with previous ones for an undo if coalescing.
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
217 * This allows multiple consecutively typed characters to be grouped together
218 * to be undone by a single undo operation.
220 void ME_ContinueCoalescingTransaction(ME_TextEditor *editor)
224 if (editor->nUndoMode == umIgnore)
227 assert(editor->nUndoMode == umAddToUndo);
229 p = editor->pUndoStack;
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);
241 * Commits preceding changes into a undo transaction that can be expanded.
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.
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.
254 void ME_CommitCoalescingUndo(ME_TextEditor *editor)
256 if (editor->nUndoMode == umIgnore)
259 assert(editor->nUndoMode == umAddToUndo);
261 /* no transactions, no need to commit */
262 if (!editor->pUndoStack)
265 /* no need to commit empty transactions */
266 if (editor->pUndoStack->type == diUndoEndTransaction)
268 if (editor->pUndoStack->type == diUndoPotentialEndTransaction)
271 ME_AddUndoItem(editor, diUndoPotentialEndTransaction, NULL);
272 ME_SendSelChange(editor);
275 static void ME_PlayUndoItem(ME_TextEditor *editor, ME_DisplayItem *pItem)
277 ME_UndoItem *pUItem = (ME_UndoItem *)pItem;
279 if (editor->nUndoMode == umIgnore)
281 TRACE("Playing undo/redo item, id=%s\n", ME_GetDITypeName(pItem->type));
285 case diUndoPotentialEndTransaction:
286 case diUndoEndTransaction:
288 case diUndoSetParagraphFormat:
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 para->member.para.border = pItem->member.para.border;
299 case diUndoSetCharFormat:
301 ME_Cursor start, end;
302 ME_CursorFromCharOfs(editor, pUItem->nStart, &start);
304 ME_MoveCursorChars(editor, &end, pUItem->nLen);
305 ME_SetCharFormat(editor, &start, &end, &pItem->member.ustyle->fmt);
308 case diUndoInsertRun:
311 ME_CursorFromCharOfs(editor, pItem->member.run.nCharOfs, &tmp);
312 ME_InsertRunAtCursor(editor, &tmp, pItem->member.run.style,
313 pItem->member.run.strText->szData,
314 pItem->member.run.strText->nLen,
315 pItem->member.run.nFlags);
318 case diUndoDeleteRun:
321 ME_CursorFromCharOfs(editor, pUItem->nStart, &tmp);
322 ME_InternalDeleteText(editor, &tmp, pUItem->nLen, TRUE);
325 case diUndoJoinParagraphs:
328 ME_CursorFromCharOfs(editor, pUItem->nStart, &tmp);
329 /* the only thing that's needed is paragraph offset, so no need to split runs */
330 ME_JoinParagraphs(editor, tmp.pPara, TRUE);
333 case diUndoSplitParagraph:
336 ME_DisplayItem *this_para, *new_para;
338 int paraFlags = pItem->member.para.nFlags & (MEPF_ROWSTART|MEPF_CELL|MEPF_ROWEND);
339 ME_CursorFromCharOfs(editor, pUItem->nStart, &tmp);
341 tmp.pRun = ME_SplitRunSimple(editor, tmp.pRun, tmp.nOffset);
342 assert(pUItem->eol_str);
343 this_para = tmp.pPara;
344 bFixRowStart = this_para->member.para.nFlags & MEPF_ROWSTART;
347 /* Re-insert the paragraph before the table, making sure the nFlag value
349 this_para->member.para.nFlags &= ~MEPF_ROWSTART;
351 new_para = ME_SplitParagraph(editor, tmp.pRun, tmp.pRun->member.run.style,
352 pUItem->eol_str, paraFlags);
354 new_para->member.para.nFlags |= MEPF_ROWSTART;
355 assert(pItem->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
356 *new_para->member.para.pFmt = *pItem->member.para.pFmt;
357 new_para->member.para.border = pItem->member.para.border;
358 if (pItem->member.para.pCell)
360 ME_DisplayItem *pItemCell, *pCell;
361 pItemCell = pItem->member.para.pCell;
362 pCell = new_para->member.para.pCell;
363 pCell->member.cell.nRightBoundary = pItemCell->member.cell.nRightBoundary;
364 pCell->member.cell.border = pItemCell->member.cell.border;
369 assert(0 == "PlayUndoItem, unexpected type");
373 BOOL ME_Undo(ME_TextEditor *editor) {
375 ME_UndoMode nMode = editor->nUndoMode;
377 if (editor->nUndoMode == umIgnore)
379 assert(nMode == umAddToUndo || nMode == umIgnore);
381 /* no undo items ? */
382 if (!editor->pUndoStack)
385 /* watch out for uncommitted transactions ! */
386 assert(editor->pUndoStack->type == diUndoEndTransaction
387 || editor->pUndoStack->type == diUndoPotentialEndTransaction);
389 editor->nUndoMode = umAddToRedo;
390 p = editor->pUndoStack->next;
391 ME_DestroyDisplayItem(editor->pUndoStack);
392 editor->pUndoStack = p;
395 ME_PlayUndoItem(editor, p);
396 editor->pUndoStack = p->next;
397 ME_DestroyDisplayItem(p);
398 p = editor->pUndoStack;
399 } while(p && p->type != diUndoEndTransaction);
402 ME_MoveCursorFromTableRowStartParagraph(editor);
403 ME_AddUndoItem(editor, diUndoEndTransaction, NULL);
404 ME_CheckTablesForCorruption(editor);
405 editor->nUndoStackSize--;
406 editor->nUndoMode = nMode;
407 ME_UpdateRepaint(editor);
411 BOOL ME_Redo(ME_TextEditor *editor) {
413 ME_UndoMode nMode = editor->nUndoMode;
415 assert(nMode == umAddToUndo || nMode == umIgnore);
417 if (editor->nUndoMode == umIgnore)
419 /* no redo items ? */
420 if (!editor->pRedoStack)
423 /* watch out for uncommitted transactions ! */
424 assert(editor->pRedoStack->type == diUndoEndTransaction);
426 editor->nUndoMode = umAddBackToUndo;
427 p = editor->pRedoStack->next;
428 ME_DestroyDisplayItem(editor->pRedoStack);
429 editor->pRedoStack = p;
432 ME_PlayUndoItem(editor, p);
433 editor->pRedoStack = p->next;
434 ME_DestroyDisplayItem(p);
435 p = editor->pRedoStack;
436 } while(p && p->type != diUndoEndTransaction);
439 ME_MoveCursorFromTableRowStartParagraph(editor);
440 ME_AddUndoItem(editor, diUndoEndTransaction, NULL);
441 ME_CheckTablesForCorruption(editor);
442 editor->nUndoMode = nMode;
443 ME_UpdateRepaint(editor);