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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 = NULL;
38 ME_DestroyDisplayItem(p);
41 p = editor->pRedoStack;
42 editor->pRedoStack = NULL;
45 ME_DestroyDisplayItem(p);
50 ME_UndoItem *ME_AddUndoItem(ME_TextEditor *editor, ME_DIType type, ME_DisplayItem *pdi) {
51 if (editor->nUndoMode == umIgnore)
55 ME_DisplayItem *pItem = (ME_DisplayItem *)ALLOC_OBJ(ME_UndoItem);
58 case diUndoEndTransaction:
60 case diUndoSetParagraphFormat:
62 CopyMemory(&pItem->member.para, &pdi->member.para, sizeof(ME_Paragraph));
63 pItem->member.para.pFmt = ALLOC_OBJ(PARAFORMAT2);
64 CopyMemory(pItem->member.para.pFmt, pdi->member.para.pFmt, sizeof(PARAFORMAT2));
68 CopyMemory(&pItem->member.run, &pdi->member.run, sizeof(ME_Run));
69 pItem->member.run.strText = ME_StrDup(pItem->member.run.strText);
70 ME_AddRefStyle(pItem->member.run.style);
72 case diUndoSetCharFormat:
73 case diUndoSetDefaultCharFormat:
76 case diUndoJoinParagraphs:
78 case diUndoSplitParagraph:
79 pItem->member.para.pFmt = ALLOC_OBJ(PARAFORMAT2);
80 pItem->member.para.pFmt->cbSize = sizeof(PARAFORMAT2);
81 pItem->member.para.pFmt->dwMask = 0;
85 assert(0 == "AddUndoItem, unsupported item type");
90 if (editor->nUndoMode == umAddToUndo || editor->nUndoMode == umAddBackToUndo)
92 if (editor->nUndoMode == umAddToUndo)
93 TRACE("Pushing id=%s to undo stack, deleting redo stack\n", ME_GetDITypeName(type));
95 TRACE("Pushing id=%s to undo stack\n", ME_GetDITypeName(type));
96 pItem->next = editor->pUndoStack;
97 if (editor->pUndoStack)
98 editor->pUndoStack->prev = pItem;
99 editor->pUndoStack = pItem;
100 /* any new operation (not redo) clears the redo stack */
101 if (editor->nUndoMode == umAddToUndo) {
102 ME_DisplayItem *p = editor->pRedoStack;
105 ME_DisplayItem *pp = p->next;
106 ME_DestroyDisplayItem(p);
109 editor->pRedoStack = NULL;
112 else if (editor->nUndoMode == umAddToRedo)
114 TRACE("Pushing id=%s to redo stack\n", ME_GetDITypeName(type));
115 pItem->next = editor->pRedoStack;
116 if (editor->pRedoStack)
117 editor->pRedoStack->prev = pItem;
118 editor->pRedoStack = pItem;
122 return (ME_UndoItem *)pItem;
126 void ME_CommitUndo(ME_TextEditor *editor) {
128 if (editor->nUndoMode == umIgnore)
131 assert(editor->nUndoMode == umAddToUndo);
133 /* no transactions, no need to commit */
134 if (!editor->pUndoStack)
137 /* no need to commit empty transactions */
138 if (editor->pUndoStack->type == diUndoEndTransaction)
141 ME_AddUndoItem(editor, diUndoEndTransaction, NULL);
142 ME_SendSelChange(editor);
143 editor->nModifyStep++;
146 void ME_PlayUndoItem(ME_TextEditor *editor, ME_DisplayItem *pItem)
148 ME_UndoItem *pUItem = (ME_UndoItem *)pItem;
150 if (editor->nUndoMode == umIgnore)
152 TRACE("Playing undo/redo item, id=%s\n", ME_GetDITypeName(pItem->type));
156 case diUndoEndTransaction:
158 case diUndoSetParagraphFormat:
161 ME_CursorFromCharOfs(editor, pItem->member.para.nCharOfs, &tmp);
162 ME_SetParaFormat(editor, ME_FindItemBack(tmp.pRun, diParagraph), pItem->member.para.pFmt);
165 case diUndoSetCharFormat:
167 ME_SetCharFormat(editor, pUItem->nStart, pUItem->nLen, &pItem->member.ustyle->fmt);
170 case diUndoSetDefaultCharFormat:
172 ME_SetDefaultCharFormat(editor, &pItem->member.ustyle->fmt);
175 case diUndoInsertRun:
177 ME_InsertRun(editor, pItem->member.run.nCharOfs, pItem);
180 case diUndoDeleteRun:
182 ME_InternalDeleteText(editor, pUItem->nStart, pUItem->nLen);
185 case diUndoJoinParagraphs:
188 ME_CursorFromCharOfs(editor, pUItem->nStart, &tmp);
189 /* the only thing that's needed is paragraph offset, so no need to split runs */
190 ME_JoinParagraphs(editor, ME_GetParagraph(tmp.pRun));
193 case diUndoSplitParagraph:
196 ME_DisplayItem *new_para;
197 ME_CursorFromCharOfs(editor, pUItem->nStart, &tmp);
199 tmp.pRun = ME_SplitRunSimple(editor, tmp.pRun, tmp.nOffset);
200 new_para = ME_SplitParagraph(editor, tmp.pRun, tmp.pRun->member.run.style);
201 assert(pItem->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
202 CopyMemory(new_para->member.para.pFmt, pItem->member.para.pFmt, sizeof(PARAFORMAT2));
206 assert(0 == "PlayUndoItem, unexpected type");
210 void ME_Undo(ME_TextEditor *editor) {
212 ME_UndoMode nMode = editor->nUndoMode;
214 if (editor->nUndoMode == umIgnore)
216 assert(nMode == umAddToUndo || nMode == umIgnore);
218 /* no undo items ? */
219 if (!editor->pUndoStack)
222 /* watch out for uncommited transactions ! */
223 assert(editor->pUndoStack->type == diUndoEndTransaction);
225 editor->nUndoMode = umAddToRedo;
226 p = editor->pUndoStack->next;
227 ME_DestroyDisplayItem(editor->pUndoStack);
229 ME_DisplayItem *pp = p;
230 ME_PlayUndoItem(editor, p);
232 ME_DestroyDisplayItem(pp);
233 } while(p && p->type != diUndoEndTransaction);
234 ME_AddUndoItem(editor, diUndoEndTransaction, NULL);
235 editor->pUndoStack = p;
238 editor->nUndoMode = nMode;
239 editor->nModifyStep--;
240 ME_UpdateRepaint(editor);
243 void ME_Redo(ME_TextEditor *editor) {
245 ME_UndoMode nMode = editor->nUndoMode;
247 assert(nMode == umAddToUndo || nMode == umIgnore);
249 if (editor->nUndoMode == umIgnore)
251 /* no redo items ? */
252 if (!editor->pRedoStack)
255 /* watch out for uncommited transactions ! */
256 assert(editor->pRedoStack->type == diUndoEndTransaction);
258 editor->nUndoMode = umAddBackToUndo;
259 p = editor->pRedoStack->next;
260 ME_DestroyDisplayItem(editor->pRedoStack);
262 ME_DisplayItem *pp = p;
263 ME_PlayUndoItem(editor, p);
265 ME_DestroyDisplayItem(pp);
266 } while(p && p->type != diUndoEndTransaction);
267 ME_AddUndoItem(editor, diUndoEndTransaction, NULL);
268 editor->pRedoStack = p;
271 editor->nUndoMode = nMode;
272 editor->nModifyStep++;
273 ME_UpdateRepaint(editor);