cabinet: Add initial tests for FDI.
[wine] / dlls / riched20 / list.c
1 /*
2  * RichEdit - Basic operations on double linked lists.
3  *
4  * Copyright 2004 by Krzysztof Foltman
5  *
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.
10  *
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.
15  *
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
19  */
20
21
22 #include "editor.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(richedit_lists);
25
26 void ME_InsertBefore(ME_DisplayItem *diWhere, ME_DisplayItem *diWhat)
27 {
28   diWhat->next = diWhere;
29   diWhat->prev = diWhere->prev;
30
31   diWhere->prev->next = diWhat;
32   diWhat->next->prev = diWhat;
33 }
34
35 void ME_Remove(ME_DisplayItem *diWhere)
36 {
37   ME_DisplayItem *diNext = diWhere->next;
38   ME_DisplayItem *diPrev = diWhere->prev;
39   assert(diNext);
40   assert(diPrev);
41   diPrev->next = diNext;
42   diNext->prev = diPrev;
43 }
44
45 ME_DisplayItem *ME_FindItemBack(ME_DisplayItem *di, ME_DIType nTypeOrClass)
46 {
47   if (!di)
48     return NULL;
49   di = di->prev;
50   while(di!=NULL) {
51     if (ME_DITypesEqual(di->type, nTypeOrClass))
52       return di;
53     di = di->prev;
54   }
55   return NULL;
56 }
57
58 ME_DisplayItem *ME_FindItemBackOrHere(ME_DisplayItem *di, ME_DIType nTypeOrClass)
59 {
60   while(di!=NULL) {
61     if (ME_DITypesEqual(di->type, nTypeOrClass))
62       return di;
63     di = di->prev;
64   }
65   return NULL;
66 }
67
68 ME_DisplayItem *ME_FindItemFwd(ME_DisplayItem *di, ME_DIType nTypeOrClass)
69 {
70   if (!di) return NULL;
71   di = di->next;
72   while(di!=NULL) {
73     if (ME_DITypesEqual(di->type, nTypeOrClass))
74       return di;
75     di = di->next;
76   }
77   return NULL;
78 }
79
80 ME_DisplayItem *ME_FindItemFwdOrHere(ME_DisplayItem *di, ME_DIType nTypeOrClass)
81 {
82   while(di!=NULL) {
83     if (ME_DITypesEqual(di->type, nTypeOrClass))
84       return di;
85     di = di->next;
86   }
87   return NULL;
88 }
89
90 BOOL ME_DITypesEqual(ME_DIType type, ME_DIType nTypeOrClass)
91 {
92   if (type==nTypeOrClass)
93     return TRUE;
94   if (nTypeOrClass==diRunOrParagraph && (type==diRun || type==diParagraph))
95     return TRUE;
96   if (nTypeOrClass==diRunOrStartRow && (type==diRun || type==diStartRow))
97     return TRUE;
98   if (nTypeOrClass==diParagraphOrEnd && (type==diTextEnd || type==diParagraph))
99     return TRUE;
100   if (nTypeOrClass==diStartRowOrParagraph && (type==diStartRow || type==diParagraph))
101     return TRUE;
102   if (nTypeOrClass==diStartRowOrParagraphOrEnd 
103     && (type==diStartRow || type==diParagraph || type==diTextEnd))
104     return TRUE;
105   if (nTypeOrClass==diRunOrParagraphOrEnd 
106     && (type==diRun || type==diParagraph || type==diTextEnd))
107     return TRUE;
108   return FALSE;
109 }
110
111 void ME_DestroyDisplayItem(ME_DisplayItem *item) {
112 /*  TRACE("type=%s\n", ME_GetDITypeName(item->type)); */
113   if (item->type==diParagraph || item->type == diUndoSetParagraphFormat) {
114     FREE_OBJ(item->member.para.pFmt);
115     ME_DestroyTableCellList(item);
116   }
117   if (item->type==diRun || item->type == diUndoInsertRun) {
118     ME_ReleaseStyle(item->member.run.style);
119     ME_DestroyString(item->member.run.strText);
120   }
121   if (item->type==diUndoSetCharFormat || item->type==diUndoSetDefaultCharFormat) {
122     ME_ReleaseStyle(item->member.ustyle);
123   }
124   if (item->type==diUndoSplitParagraph)
125      FREE_OBJ(item->member.para.pFmt);
126   FREE_OBJ(item);
127 }
128
129 void
130 ME_DestroyTableCellList(ME_DisplayItem *item)
131 {
132   if (item->member.para.pCells)
133   {
134     ME_TableCell *pCell = item->member.para.pCells;
135     ME_TableCell *pNext;
136
137     while (pCell) {
138       pNext = pCell->next;
139       FREE_OBJ(pCell);
140       pCell = pNext;
141     }
142     item->member.para.pCells = NULL;
143   }
144 }
145
146 ME_DisplayItem *ME_MakeDI(ME_DIType type) {
147   ME_DisplayItem *item = ALLOC_OBJ(ME_DisplayItem);
148   ZeroMemory(item, sizeof(ME_DisplayItem));
149   item->type = type;
150   item->prev = item->next = NULL;
151   if (type == diParagraph || type == diUndoSplitParagraph) {
152     item->member.para.pFmt = ALLOC_OBJ(PARAFORMAT2);
153     item->member.para.pFmt->cbSize = sizeof(PARAFORMAT2);
154     item->member.para.pFmt->dwMask = 0;
155     item->member.para.nFlags = MEPF_REWRAP;
156   }
157     
158   return item;
159 }
160
161 const char *ME_GetDITypeName(ME_DIType type)
162 {
163   switch(type)
164   {
165     case diParagraph: return "diParagraph";
166     case diRun: return "diRun";
167     case diTextStart: return "diTextStart";
168     case diTextEnd: return "diTextEnd";
169     case diStartRow: return "diStartRow";
170     case diUndoEndTransaction: return "diUndoEndTransaction";
171     case diUndoSetParagraphFormat: return "diUndoSetParagraphFormat";
172     case diUndoSetCharFormat: return "diUndoSetCharFormat";
173     case diUndoInsertRun: return "diUndoInsertRun";
174     case diUndoDeleteRun: return "diUndoDeleteRun";
175     case diUndoJoinParagraphs: return "diJoinParagraphs";
176     case diUndoSplitParagraph: return "diSplitParagraph";
177     case diUndoSetDefaultCharFormat: return "diUndoSetDefaultCharFormat";
178     default: return "?";
179   }
180 }
181
182 void ME_DumpDocument(ME_TextBuffer *buffer)
183 {
184   /* FIXME this is useless, */
185   ME_DisplayItem *pItem = buffer->pFirst;
186   TRACE("DOCUMENT DUMP START\n");
187   while(pItem) {
188     switch(pItem->type)
189     {
190       case diTextStart:
191         TRACE("Start\n");
192         break;
193       case diParagraph:
194         TRACE("Paragraph(ofs=%d)\n", pItem->member.para.nCharOfs);
195         break;
196       case diStartRow:
197         TRACE(" - StartRow\n");
198         break;
199       case diRun:
200         TRACE(" - Run(\"%s\", %d)\n", debugstr_w(pItem->member.run.strText->szData), 
201           pItem->member.run.nCharOfs);
202         break;
203       case diTextEnd:
204         TRACE("End(ofs=%d)\n", pItem->member.para.nCharOfs);
205         break;
206       default:
207         break;
208     }
209     pItem = pItem->next;
210   }
211   TRACE("DOCUMENT DUMP END\n");
212 }