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
:
89 case diUndoJoinParagraphs
:
91 case diUndoSplitParagraph
:
92 pItem
->member
.para
.pFmt
= ALLOC_OBJ(PARAFORMAT2
);
93 pItem
->member
.para
.pFmt
->cbSize
= sizeof(PARAFORMAT2
);
94 pItem
->member
.para
.pFmt
->dwMask
= 0;
98 assert(0 == "AddUndoItem, unsupported item type");
103 if (editor
->nUndoMode
== umAddToUndo
|| editor
->nUndoMode
== umAddBackToUndo
)
105 if (editor
->pUndoStack
106 && editor
->pUndoStack
->type
== diUndoPotentialEndTransaction
)
108 editor
->pUndoStack
->type
= diUndoEndTransaction
;
110 if (editor
->nUndoMode
== umAddToUndo
)
111 TRACE("Pushing id=%s to undo stack, deleting redo stack\n", ME_GetDITypeName(type
));
113 TRACE("Pushing id=%s to undo stack\n", ME_GetDITypeName(type
));
115 pItem
->next
= editor
->pUndoStack
;
116 if (type
== diUndoEndTransaction
|| type
== diUndoPotentialEndTransaction
)
117 editor
->nUndoStackSize
++;
118 if (editor
->pUndoStack
)
119 editor
->pUndoStack
->prev
= pItem
;
121 editor
->pUndoStackBottom
= pItem
;
122 editor
->pUndoStack
= pItem
;
124 if (editor
->nUndoStackSize
> editor
->nUndoLimit
)
125 { /* remove oldest undo from stack */
126 ME_DisplayItem
*p
= editor
->pUndoStackBottom
;
127 while (p
->type
!=diUndoEndTransaction
)
128 p
= p
->prev
; /*find new stack bottom */
129 editor
->pUndoStackBottom
= p
->prev
;
130 editor
->pUndoStackBottom
->next
= NULL
;
133 ME_DisplayItem
*pp
= p
->next
;
134 ME_DestroyDisplayItem(p
);
137 editor
->nUndoStackSize
--;
139 /* any new operation (not redo) clears the redo stack */
140 if (editor
->nUndoMode
== umAddToUndo
) {
141 ME_DisplayItem
*p
= editor
->pRedoStack
;
144 ME_DisplayItem
*pp
= p
->next
;
145 ME_DestroyDisplayItem(p
);
148 editor
->pRedoStack
= NULL
;
151 else if (editor
->nUndoMode
== umAddToRedo
)
153 TRACE("Pushing id=%s to redo stack\n", ME_GetDITypeName(type
));
154 pItem
->next
= editor
->pRedoStack
;
155 if (editor
->pRedoStack
)
156 editor
->pRedoStack
->prev
= pItem
;
157 editor
->pRedoStack
= pItem
;
161 return (ME_UndoItem
*)pItem
;
166 * Commits preceding changes into a transaction that can be undone together.
168 * This should be called after all the changes occur associated with an event
169 * so that the group of changes can be undone atomically as a transaction.
171 * This will have no effect the undo mode is set to ignore changes, or if no
172 * changes preceded calling this function before the last time it was called.
174 * This can also be used to conclude a coalescing transaction (used for grouping
177 void ME_CommitUndo(ME_TextEditor
*editor
) {
178 if (editor
->nUndoMode
== umIgnore
)
181 assert(editor
->nUndoMode
== umAddToUndo
);
183 /* no transactions, no need to commit */
184 if (!editor
->pUndoStack
)
187 /* no need to commit empty transactions */
188 if (editor
->pUndoStack
->type
== diUndoEndTransaction
)
191 if (editor
->pUndoStack
->type
== diUndoPotentialEndTransaction
)
193 /* Previous transaction was as a result of characters typed,
194 * so the end of this transaction is confirmed. */
195 editor
->pUndoStack
->type
= diUndoEndTransaction
;
199 ME_AddUndoItem(editor
, diUndoEndTransaction
, NULL
);
200 ME_SendSelChange(editor
);
204 * Groups supsequent changes with previous ones for an undo if coalescing.
206 * Has no effect if the previous changes were followed by a ME_CommitUndo. This
207 * function will only have an affect if the previous changes were followed by
208 * a call to ME_CommitCoalescingUndo, which allows the transaction to be
211 * This allows multiple consecutively typed characters to be grouped together
212 * to be undone by a single undo operation.
214 void ME_ContinueCoalescingTransaction(ME_TextEditor
*editor
)
218 if (editor
->nUndoMode
== umIgnore
)
221 assert(editor
->nUndoMode
== umAddToUndo
);
223 p
= editor
->pUndoStack
;
225 if (p
&& p
->type
== diUndoPotentialEndTransaction
) {
226 assert(p
->next
); /* EndTransactions shouldn't be at bottom of undo stack */
227 editor
->pUndoStack
= p
->next
;
228 editor
->pUndoStack
->prev
= NULL
;
229 editor
->nUndoStackSize
--;
230 ME_DestroyDisplayItem(p
);
235 * Commits preceding changes into a undo transaction that can be expanded.
237 * This function allows the transaction to be reopened with
238 * ME_ContinueCoalescingTransaction in order to continue the transaction. If an
239 * undo item is added to the undo stack as a result of a change without the
240 * transaction being reopened, then the transaction will be ended, and the
241 * changes will become a part of the next transaction.
243 * This is used to allow typed characters to be grouped together since each
244 * typed character results in a single event, and each event adding undo items
245 * must be committed. Using this function as opposed to ME_CommitUndo allows
246 * multiple events to be grouped, and undone together.
248 void ME_CommitCoalescingUndo(ME_TextEditor
*editor
)
250 if (editor
->nUndoMode
== umIgnore
)
253 assert(editor
->nUndoMode
== umAddToUndo
);
255 /* no transactions, no need to commit */
256 if (!editor
->pUndoStack
)
259 /* no need to commit empty transactions */
260 if (editor
->pUndoStack
->type
== diUndoEndTransaction
)
262 if (editor
->pUndoStack
->type
== diUndoPotentialEndTransaction
)
265 ME_AddUndoItem(editor
, diUndoPotentialEndTransaction
, NULL
);
266 ME_SendSelChange(editor
);
269 static void ME_PlayUndoItem(ME_TextEditor
*editor
, ME_DisplayItem
*pItem
)
271 ME_UndoItem
*pUItem
= (ME_UndoItem
*)pItem
;
273 if (editor
->nUndoMode
== umIgnore
)
275 TRACE("Playing undo/redo item, id=%s\n", ME_GetDITypeName(pItem
->type
));
279 case diUndoPotentialEndTransaction
:
280 case diUndoEndTransaction
:
282 case diUndoSetParagraphFormat
:
285 ME_CursorFromCharOfs(editor
, pItem
->member
.para
.nCharOfs
, &tmp
);
286 ME_SetParaFormat(editor
, ME_FindItemBack(tmp
.pRun
, diParagraph
), pItem
->member
.para
.pFmt
);
289 case diUndoSetCharFormat
:
291 ME_SetCharFormat(editor
, pUItem
->nStart
, pUItem
->nLen
, &pItem
->member
.ustyle
->fmt
);
294 case diUndoInsertRun
:
296 ME_InsertRun(editor
, pItem
->member
.run
.nCharOfs
, pItem
);
299 case diUndoDeleteRun
:
301 ME_InternalDeleteText(editor
, pUItem
->nStart
, pUItem
->nLen
);
304 case diUndoJoinParagraphs
:
307 ME_CursorFromCharOfs(editor
, pUItem
->nStart
, &tmp
);
308 /* the only thing that's needed is paragraph offset, so no need to split runs */
309 ME_JoinParagraphs(editor
, ME_GetParagraph(tmp
.pRun
));
312 case diUndoSplitParagraph
:
315 ME_DisplayItem
*new_para
;
316 ME_CursorFromCharOfs(editor
, pUItem
->nStart
, &tmp
);
318 tmp
.pRun
= ME_SplitRunSimple(editor
, tmp
.pRun
, tmp
.nOffset
);
319 assert(pUItem
->nCR
>= 0);
320 assert(pUItem
->nLF
>= 0);
321 new_para
= ME_SplitParagraph(editor
, tmp
.pRun
, tmp
.pRun
->member
.run
.style
,
322 pUItem
->nCR
, pUItem
->nLF
);
323 assert(pItem
->member
.para
.pFmt
->cbSize
== sizeof(PARAFORMAT2
));
324 *new_para
->member
.para
.pFmt
= *pItem
->member
.para
.pFmt
;
328 assert(0 == "PlayUndoItem, unexpected type");
332 BOOL
ME_Undo(ME_TextEditor
*editor
) {
334 ME_UndoMode nMode
= editor
->nUndoMode
;
336 if (editor
->nUndoMode
== umIgnore
)
338 assert(nMode
== umAddToUndo
|| nMode
== umIgnore
);
340 /* no undo items ? */
341 if (!editor
->pUndoStack
)
344 /* watch out for uncommitted transactions ! */
345 assert(editor
->pUndoStack
->type
== diUndoEndTransaction
346 || editor
->pUndoStack
->type
== diUndoPotentialEndTransaction
);
348 editor
->nUndoMode
= umAddToRedo
;
349 p
= editor
->pUndoStack
->next
;
350 ME_DestroyDisplayItem(editor
->pUndoStack
);
351 editor
->pUndoStack
= p
;
354 ME_PlayUndoItem(editor
, p
);
355 editor
->pUndoStack
= p
->next
;
356 ME_DestroyDisplayItem(p
);
357 p
= editor
->pUndoStack
;
358 } while(p
&& p
->type
!= diUndoEndTransaction
);
361 ME_AddUndoItem(editor
, diUndoEndTransaction
, NULL
);
362 editor
->nUndoStackSize
--;
363 editor
->nUndoMode
= nMode
;
364 ME_UpdateRepaint(editor
);
368 BOOL
ME_Redo(ME_TextEditor
*editor
) {
370 ME_UndoMode nMode
= editor
->nUndoMode
;
372 assert(nMode
== umAddToUndo
|| nMode
== umIgnore
);
374 if (editor
->nUndoMode
== umIgnore
)
376 /* no redo items ? */
377 if (!editor
->pRedoStack
)
380 /* watch out for uncommitted transactions ! */
381 assert(editor
->pRedoStack
->type
== diUndoEndTransaction
);
383 editor
->nUndoMode
= umAddBackToUndo
;
384 p
= editor
->pRedoStack
->next
;
385 ME_DestroyDisplayItem(editor
->pRedoStack
);
386 editor
->pRedoStack
= p
;
389 ME_PlayUndoItem(editor
, p
);
390 editor
->pRedoStack
= p
->next
;
391 ME_DestroyDisplayItem(p
);
392 p
= editor
->pRedoStack
;
393 } while(p
&& p
->type
!= diUndoEndTransaction
);
396 ME_AddUndoItem(editor
, diUndoEndTransaction
, NULL
);
397 editor
->nUndoMode
= nMode
;
398 ME_UpdateRepaint(editor
);