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 diUndoEndTransaction
:
64 case diUndoSetParagraphFormat
:
66 pItem
->member
.para
= pdi
->member
.para
;
67 pItem
->member
.para
.pFmt
= ALLOC_OBJ(PARAFORMAT2
);
68 *pItem
->member
.para
.pFmt
= *pdi
->member
.para
.pFmt
;
72 pItem
->member
.run
= pdi
->member
.run
;
73 pItem
->member
.run
.strText
= ME_StrDup(pItem
->member
.run
.strText
);
74 ME_AddRefStyle(pItem
->member
.run
.style
);
75 if (pdi
->member
.run
.ole_obj
)
77 pItem
->member
.run
.ole_obj
= ALLOC_OBJ(*pItem
->member
.run
.ole_obj
);
78 ME_CopyReObject(pItem
->member
.run
.ole_obj
, pdi
->member
.run
.ole_obj
);
80 else pItem
->member
.run
.ole_obj
= NULL
;
82 case diUndoSetCharFormat
:
83 case diUndoSetDefaultCharFormat
:
86 case diUndoJoinParagraphs
:
88 case diUndoSplitParagraph
:
89 pItem
->member
.para
.pFmt
= ALLOC_OBJ(PARAFORMAT2
);
90 pItem
->member
.para
.pFmt
->cbSize
= sizeof(PARAFORMAT2
);
91 pItem
->member
.para
.pFmt
->dwMask
= 0;
95 assert(0 == "AddUndoItem, unsupported item type");
100 if (editor
->nUndoMode
== umAddToUndo
|| editor
->nUndoMode
== umAddBackToUndo
)
102 if (editor
->nUndoMode
== umAddToUndo
)
103 TRACE("Pushing id=%s to undo stack, deleting redo stack\n", ME_GetDITypeName(type
));
105 TRACE("Pushing id=%s to undo stack\n", ME_GetDITypeName(type
));
107 pItem
->next
= editor
->pUndoStack
;
108 if (type
== diUndoEndTransaction
)
109 editor
->nUndoStackSize
++;
110 if (editor
->pUndoStack
)
111 editor
->pUndoStack
->prev
= pItem
;
113 editor
->pUndoStackBottom
= pItem
;
114 editor
->pUndoStack
= pItem
;
116 if (editor
->nUndoStackSize
> editor
->nUndoLimit
)
117 { /* remove oldest undo from stack */
118 ME_DisplayItem
*p
= editor
->pUndoStackBottom
;
119 while (p
->type
!=diUndoEndTransaction
)
120 p
= p
->prev
; /*find new stack bottom */
121 editor
->pUndoStackBottom
= p
->prev
;
122 editor
->pUndoStackBottom
->next
= NULL
;
125 ME_DisplayItem
*pp
= p
->next
;
126 ME_DestroyDisplayItem(p
);
129 editor
->nUndoStackSize
--;
131 /* any new operation (not redo) clears the redo stack */
132 if (editor
->nUndoMode
== umAddToUndo
) {
133 ME_DisplayItem
*p
= editor
->pRedoStack
;
136 ME_DisplayItem
*pp
= p
->next
;
137 ME_DestroyDisplayItem(p
);
140 editor
->pRedoStack
= NULL
;
143 else if (editor
->nUndoMode
== umAddToRedo
)
145 TRACE("Pushing id=%s to redo stack\n", ME_GetDITypeName(type
));
146 pItem
->next
= editor
->pRedoStack
;
147 if (editor
->pRedoStack
)
148 editor
->pRedoStack
->prev
= pItem
;
149 editor
->pRedoStack
= pItem
;
153 return (ME_UndoItem
*)pItem
;
157 void ME_CommitUndo(ME_TextEditor
*editor
) {
158 if (editor
->nUndoMode
== umIgnore
)
161 assert(editor
->nUndoMode
== umAddToUndo
);
163 /* no transactions, no need to commit */
164 if (!editor
->pUndoStack
)
167 /* no need to commit empty transactions */
168 if (editor
->pUndoStack
->type
== diUndoEndTransaction
)
171 ME_AddUndoItem(editor
, diUndoEndTransaction
, NULL
);
172 ME_SendSelChange(editor
);
175 static void ME_PlayUndoItem(ME_TextEditor
*editor
, ME_DisplayItem
*pItem
)
177 ME_UndoItem
*pUItem
= (ME_UndoItem
*)pItem
;
179 if (editor
->nUndoMode
== umIgnore
)
181 TRACE("Playing undo/redo item, id=%s\n", ME_GetDITypeName(pItem
->type
));
185 case diUndoEndTransaction
:
187 case diUndoSetParagraphFormat
:
190 ME_CursorFromCharOfs(editor
, pItem
->member
.para
.nCharOfs
, &tmp
);
191 ME_SetParaFormat(editor
, ME_FindItemBack(tmp
.pRun
, diParagraph
), pItem
->member
.para
.pFmt
);
194 case diUndoSetCharFormat
:
196 ME_SetCharFormat(editor
, pUItem
->nStart
, pUItem
->nLen
, &pItem
->member
.ustyle
->fmt
);
199 case diUndoSetDefaultCharFormat
:
201 ME_SetDefaultCharFormat(editor
, &pItem
->member
.ustyle
->fmt
);
204 case diUndoInsertRun
:
206 ME_InsertRun(editor
, pItem
->member
.run
.nCharOfs
, pItem
);
209 case diUndoDeleteRun
:
211 ME_InternalDeleteText(editor
, pUItem
->nStart
, pUItem
->nLen
);
214 case diUndoJoinParagraphs
:
217 ME_CursorFromCharOfs(editor
, pUItem
->nStart
, &tmp
);
218 /* the only thing that's needed is paragraph offset, so no need to split runs */
219 ME_JoinParagraphs(editor
, ME_GetParagraph(tmp
.pRun
));
222 case diUndoSplitParagraph
:
225 ME_DisplayItem
*new_para
;
226 ME_CursorFromCharOfs(editor
, pUItem
->nStart
, &tmp
);
228 tmp
.pRun
= ME_SplitRunSimple(editor
, tmp
.pRun
, tmp
.nOffset
);
229 assert(pUItem
->nCR
>= 0);
230 assert(pUItem
->nLF
>= 0);
231 new_para
= ME_SplitParagraph(editor
, tmp
.pRun
, tmp
.pRun
->member
.run
.style
,
232 pUItem
->nCR
, pUItem
->nLF
);
233 assert(pItem
->member
.para
.pFmt
->cbSize
== sizeof(PARAFORMAT2
));
234 *new_para
->member
.para
.pFmt
= *pItem
->member
.para
.pFmt
;
238 assert(0 == "PlayUndoItem, unexpected type");
242 void ME_Undo(ME_TextEditor
*editor
) {
244 ME_UndoMode nMode
= editor
->nUndoMode
;
246 if (editor
->nUndoMode
== umIgnore
)
248 assert(nMode
== umAddToUndo
|| nMode
== umIgnore
);
250 /* no undo items ? */
251 if (!editor
->pUndoStack
)
254 /* watch out for uncommitted transactions ! */
255 assert(editor
->pUndoStack
->type
== diUndoEndTransaction
);
257 editor
->nUndoMode
= umAddToRedo
;
258 p
= editor
->pUndoStack
->next
;
259 ME_DestroyDisplayItem(editor
->pUndoStack
);
261 ME_DisplayItem
*pp
= p
;
262 ME_PlayUndoItem(editor
, p
);
264 ME_DestroyDisplayItem(pp
);
265 } while(p
&& p
->type
!= diUndoEndTransaction
);
266 ME_AddUndoItem(editor
, diUndoEndTransaction
, NULL
);
267 editor
->pUndoStack
= p
;
268 editor
->nUndoStackSize
--;
271 editor
->nUndoMode
= nMode
;
272 ME_UpdateRepaint(editor
);
275 void ME_Redo(ME_TextEditor
*editor
) {
277 ME_UndoMode nMode
= editor
->nUndoMode
;
279 assert(nMode
== umAddToUndo
|| nMode
== umIgnore
);
281 if (editor
->nUndoMode
== umIgnore
)
283 /* no redo items ? */
284 if (!editor
->pRedoStack
)
287 /* watch out for uncommitted transactions ! */
288 assert(editor
->pRedoStack
->type
== diUndoEndTransaction
);
290 editor
->nUndoMode
= umAddBackToUndo
;
291 p
= editor
->pRedoStack
->next
;
292 ME_DestroyDisplayItem(editor
->pRedoStack
);
294 ME_DisplayItem
*pp
= p
;
295 ME_PlayUndoItem(editor
, p
);
297 ME_DestroyDisplayItem(pp
);
298 } while(p
&& p
->type
!= diUndoEndTransaction
);
299 ME_AddUndoItem(editor
, diUndoEndTransaction
, NULL
);
300 editor
->pRedoStack
= p
;
303 editor
->nUndoMode
= nMode
;
304 ME_UpdateRepaint(editor
);