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
, ME_DisplayItem
*pdi
) {
52 if (editor
->nUndoMode
== umIgnore
)
54 else if (editor
->nUndoLimit
== 0)
58 ME_DisplayItem
*pItem
= (ME_DisplayItem
*)ALLOC_OBJ(ME_UndoItem
);
61 case diUndoEndTransaction
:
63 case diUndoSetParagraphFormat
:
65 CopyMemory(&pItem
->member
.para
, &pdi
->member
.para
, sizeof(ME_Paragraph
));
66 pItem
->member
.para
.pFmt
= ALLOC_OBJ(PARAFORMAT2
);
67 CopyMemory(pItem
->member
.para
.pFmt
, pdi
->member
.para
.pFmt
, sizeof(PARAFORMAT2
));
71 CopyMemory(&pItem
->member
.run
, &pdi
->member
.run
, sizeof(ME_Run
));
72 pItem
->member
.run
.strText
= ME_StrDup(pItem
->member
.run
.strText
);
73 ME_AddRefStyle(pItem
->member
.run
.style
);
75 case diUndoSetCharFormat
:
76 case diUndoSetDefaultCharFormat
:
79 case diUndoJoinParagraphs
:
81 case diUndoSplitParagraph
:
82 pItem
->member
.para
.pFmt
= ALLOC_OBJ(PARAFORMAT2
);
83 pItem
->member
.para
.pFmt
->cbSize
= sizeof(PARAFORMAT2
);
84 pItem
->member
.para
.pFmt
->dwMask
= 0;
88 assert(0 == "AddUndoItem, unsupported item type");
93 if (editor
->nUndoMode
== umAddToUndo
|| editor
->nUndoMode
== umAddBackToUndo
)
95 if (editor
->nUndoMode
== umAddToUndo
)
96 TRACE("Pushing id=%s to undo stack, deleting redo stack\n", ME_GetDITypeName(type
));
98 TRACE("Pushing id=%s to undo stack\n", ME_GetDITypeName(type
));
100 pItem
->next
= editor
->pUndoStack
;
101 if (type
== diUndoEndTransaction
)
102 editor
->nUndoStackSize
++;
103 if (editor
->pUndoStack
)
104 editor
->pUndoStack
->prev
= pItem
;
106 editor
->pUndoStackBottom
= pItem
;
107 editor
->pUndoStack
= pItem
;
109 if (editor
->nUndoStackSize
> editor
->nUndoLimit
)
110 { /* remove oldest undo from stack */
111 ME_DisplayItem
*p
= editor
->pUndoStackBottom
;
112 while (p
->type
!=diUndoEndTransaction
)
113 p
= p
->prev
; /*find new stack bottom */
114 editor
->pUndoStackBottom
= p
->prev
;
115 editor
->pUndoStackBottom
->next
= NULL
;
118 ME_DisplayItem
*pp
= p
->next
;
119 ME_DestroyDisplayItem(p
);
122 editor
->nUndoStackSize
--;
124 /* any new operation (not redo) clears the redo stack */
125 if (editor
->nUndoMode
== umAddToUndo
) {
126 ME_DisplayItem
*p
= editor
->pRedoStack
;
129 ME_DisplayItem
*pp
= p
->next
;
130 ME_DestroyDisplayItem(p
);
133 editor
->pRedoStack
= NULL
;
136 else if (editor
->nUndoMode
== umAddToRedo
)
138 TRACE("Pushing id=%s to redo stack\n", ME_GetDITypeName(type
));
139 pItem
->next
= editor
->pRedoStack
;
140 if (editor
->pRedoStack
)
141 editor
->pRedoStack
->prev
= pItem
;
142 editor
->pRedoStack
= pItem
;
146 return (ME_UndoItem
*)pItem
;
150 void ME_CommitUndo(ME_TextEditor
*editor
) {
151 if (editor
->nUndoMode
== umIgnore
)
154 assert(editor
->nUndoMode
== umAddToUndo
);
156 /* no transactions, no need to commit */
157 if (!editor
->pUndoStack
)
160 /* no need to commit empty transactions */
161 if (editor
->pUndoStack
->type
== diUndoEndTransaction
)
164 ME_AddUndoItem(editor
, diUndoEndTransaction
, NULL
);
165 ME_SendSelChange(editor
);
166 editor
->nModifyStep
++;
169 void ME_PlayUndoItem(ME_TextEditor
*editor
, ME_DisplayItem
*pItem
)
171 ME_UndoItem
*pUItem
= (ME_UndoItem
*)pItem
;
173 if (editor
->nUndoMode
== umIgnore
)
175 TRACE("Playing undo/redo item, id=%s\n", ME_GetDITypeName(pItem
->type
));
179 case diUndoEndTransaction
:
181 case diUndoSetParagraphFormat
:
184 ME_CursorFromCharOfs(editor
, pItem
->member
.para
.nCharOfs
, &tmp
);
185 ME_SetParaFormat(editor
, ME_FindItemBack(tmp
.pRun
, diParagraph
), pItem
->member
.para
.pFmt
);
188 case diUndoSetCharFormat
:
190 ME_SetCharFormat(editor
, pUItem
->nStart
, pUItem
->nLen
, &pItem
->member
.ustyle
->fmt
);
193 case diUndoSetDefaultCharFormat
:
195 ME_SetDefaultCharFormat(editor
, &pItem
->member
.ustyle
->fmt
);
198 case diUndoInsertRun
:
200 ME_InsertRun(editor
, pItem
->member
.run
.nCharOfs
, pItem
);
203 case diUndoDeleteRun
:
205 ME_InternalDeleteText(editor
, pUItem
->nStart
, pUItem
->nLen
);
208 case diUndoJoinParagraphs
:
211 ME_CursorFromCharOfs(editor
, pUItem
->nStart
, &tmp
);
212 /* the only thing that's needed is paragraph offset, so no need to split runs */
213 ME_JoinParagraphs(editor
, ME_GetParagraph(tmp
.pRun
));
216 case diUndoSplitParagraph
:
219 ME_DisplayItem
*new_para
;
220 ME_CursorFromCharOfs(editor
, pUItem
->nStart
, &tmp
);
222 tmp
.pRun
= ME_SplitRunSimple(editor
, tmp
.pRun
, tmp
.nOffset
);
223 new_para
= ME_SplitParagraph(editor
, tmp
.pRun
, tmp
.pRun
->member
.run
.style
);
224 assert(pItem
->member
.para
.pFmt
->cbSize
== sizeof(PARAFORMAT2
));
225 CopyMemory(new_para
->member
.para
.pFmt
, pItem
->member
.para
.pFmt
, sizeof(PARAFORMAT2
));
229 assert(0 == "PlayUndoItem, unexpected type");
233 void ME_Undo(ME_TextEditor
*editor
) {
235 ME_UndoMode nMode
= editor
->nUndoMode
;
237 if (editor
->nUndoMode
== umIgnore
)
239 assert(nMode
== umAddToUndo
|| nMode
== umIgnore
);
241 /* no undo items ? */
242 if (!editor
->pUndoStack
)
245 /* watch out for uncommited transactions ! */
246 assert(editor
->pUndoStack
->type
== diUndoEndTransaction
);
248 editor
->nUndoMode
= umAddToRedo
;
249 p
= editor
->pUndoStack
->next
;
250 ME_DestroyDisplayItem(editor
->pUndoStack
);
252 ME_DisplayItem
*pp
= p
;
253 ME_PlayUndoItem(editor
, p
);
255 ME_DestroyDisplayItem(pp
);
256 } while(p
&& p
->type
!= diUndoEndTransaction
);
257 ME_AddUndoItem(editor
, diUndoEndTransaction
, NULL
);
258 editor
->pUndoStack
= p
;
259 editor
->nUndoStackSize
--;
262 editor
->nUndoMode
= nMode
;
263 editor
->nModifyStep
--;
264 ME_UpdateRepaint(editor
);
267 void ME_Redo(ME_TextEditor
*editor
) {
269 ME_UndoMode nMode
= editor
->nUndoMode
;
271 assert(nMode
== umAddToUndo
|| nMode
== umIgnore
);
273 if (editor
->nUndoMode
== umIgnore
)
275 /* no redo items ? */
276 if (!editor
->pRedoStack
)
279 /* watch out for uncommited transactions ! */
280 assert(editor
->pRedoStack
->type
== diUndoEndTransaction
);
282 editor
->nUndoMode
= umAddBackToUndo
;
283 p
= editor
->pRedoStack
->next
;
284 ME_DestroyDisplayItem(editor
->pRedoStack
);
286 ME_DisplayItem
*pp
= p
;
287 ME_PlayUndoItem(editor
, p
);
289 ME_DestroyDisplayItem(pp
);
290 } while(p
&& p
->type
!= diUndoEndTransaction
);
291 ME_AddUndoItem(editor
, diUndoEndTransaction
, NULL
);
292 editor
->pRedoStack
= p
;
295 editor
->nUndoMode
= nMode
;
296 editor
->nModifyStep
++;
297 ME_UpdateRepaint(editor
);