windowscodecs: Add wrapper functions for IWICPalette methods.
[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 static BOOL ME_DITypesEqual(ME_DIType type, ME_DIType nTypeOrClass)
46 {
47   switch (nTypeOrClass)
48   {
49     case diRunOrParagraph:
50       return type == diRun || type == diParagraph;
51     case diRunOrStartRow:
52       return type == diRun || type == diStartRow;
53     case diParagraphOrEnd:
54       return type == diTextEnd || type == diParagraph;
55     case diStartRowOrParagraph:
56       return type == diStartRow || type == diParagraph;
57     case diStartRowOrParagraphOrEnd:
58       return type == diStartRow || type == diParagraph || type == diTextEnd;
59     case diRunOrParagraphOrEnd:
60       return type == diRun || type == diParagraph || type == diTextEnd;
61     default:
62       return type == nTypeOrClass;
63   }
64 }
65
66 /* Modifies run pointer to point to the next run, and modify the
67  * paragraph pointer if moving into the next paragraph.
68  *
69  * Returns TRUE if next run is found, otherwise returns FALSE. */
70 BOOL ME_NextRun(ME_DisplayItem **para, ME_DisplayItem **run)
71 {
72   ME_DisplayItem *p = (*run)->next;
73   while (p->type != diTextEnd)
74   {
75     if (p->type == diParagraph) {
76       *para = p;
77     } else if (p->type == diRun) {
78       *run = p;
79       return TRUE;
80     }
81     p = p->next;
82   }
83   return FALSE;
84 }
85
86 /* Modifies run pointer to point to the previous run, and modify the
87  * paragraph pointer if moving into the previous paragraph.
88  *
89  * Returns TRUE if previous run is found, otherwise returns FALSE. */
90 BOOL ME_PrevRun(ME_DisplayItem **para, ME_DisplayItem **run)
91 {
92   ME_DisplayItem *p = (*run)->prev;
93   while (p->type != diTextStart)
94   {
95     if (p->type == diParagraph) {
96       if (p->member.para.prev_para->type == diParagraph)
97         *para = p->member.para.prev_para;
98     } else if (p->type == diRun) {
99       *run = p;
100       return TRUE;
101     }
102     p = p->prev;
103   }
104   return FALSE;
105 }
106
107 ME_DisplayItem *ME_FindItemBack(ME_DisplayItem *di, ME_DIType nTypeOrClass)
108 {
109   if (!di)
110     return NULL;
111   di = di->prev;
112   while(di!=NULL) {
113     if (ME_DITypesEqual(di->type, nTypeOrClass))
114       return di;
115     di = di->prev;
116   }
117   return NULL;
118 }
119
120 ME_DisplayItem *ME_FindItemBackOrHere(ME_DisplayItem *di, ME_DIType nTypeOrClass)
121 {
122   while(di!=NULL) {
123     if (ME_DITypesEqual(di->type, nTypeOrClass))
124       return di;
125     di = di->prev;
126   }
127   return NULL;
128 }
129
130 ME_DisplayItem *ME_FindItemFwd(ME_DisplayItem *di, ME_DIType nTypeOrClass)
131 {
132   if (!di) return NULL;
133   di = di->next;
134   while(di!=NULL) {
135     if (ME_DITypesEqual(di->type, nTypeOrClass))
136       return di;
137     di = di->next;
138   }
139   return NULL;
140 }
141
142 void ME_DestroyDisplayItem(ME_DisplayItem *item) {
143 /*  TRACE("type=%s\n", ME_GetDITypeName(item->type)); */
144   if (item->type==diParagraph || item->type == diUndoSetParagraphFormat) {
145     FREE_OBJ(item->member.para.pFmt);
146   }
147   if (item->type==diRun || item->type == diUndoInsertRun) {
148     if (item->member.run.ole_obj) ME_DeleteReObject(item->member.run.ole_obj);
149     ME_ReleaseStyle(item->member.run.style);
150     ME_DestroyString(item->member.run.strText);
151   }
152   if (item->type==diUndoSetCharFormat) {
153     ME_ReleaseStyle(item->member.ustyle);
154   }
155   if (item->type==diUndoSplitParagraph) {
156      FREE_OBJ(item->member.para.pFmt);
157      FREE_OBJ(item->member.para.pCell);
158   }
159   FREE_OBJ(item);
160 }
161
162 ME_DisplayItem *ME_MakeDI(ME_DIType type) {
163   ME_DisplayItem *item = ALLOC_OBJ(ME_DisplayItem);
164   ZeroMemory(item, sizeof(ME_DisplayItem));
165   item->type = type;
166   item->prev = item->next = NULL;
167   if (type == diParagraph || type == diUndoSplitParagraph) {
168     item->member.para.pFmt = ALLOC_OBJ(PARAFORMAT2);
169     ME_SetDefaultParaFormat(item->member.para.pFmt);
170     item->member.para.nFlags = MEPF_REWRAP;
171   }
172     
173   return item;
174 }
175
176 const char *ME_GetDITypeName(ME_DIType type)
177 {
178   switch(type)
179   {
180     case diParagraph: return "diParagraph";
181     case diRun: return "diRun";
182     case diCell: return "diCell";
183     case diTextStart: return "diTextStart";
184     case diTextEnd: return "diTextEnd";
185     case diStartRow: return "diStartRow";
186     case diUndoEndTransaction: return "diUndoEndTransaction";
187     case diUndoPotentialEndTransaction: return "diUndoPotentialEndTransaction";
188     case diUndoSetParagraphFormat: return "diUndoSetParagraphFormat";
189     case diUndoSetCharFormat: return "diUndoSetCharFormat";
190     case diUndoInsertRun: return "diUndoInsertRun";
191     case diUndoDeleteRun: return "diUndoDeleteRun";
192     case diUndoJoinParagraphs: return "diJoinParagraphs";
193     case diUndoSplitParagraph: return "diSplitParagraph";
194     default: return "?";
195   }
196 }
197
198 void ME_DumpDocument(ME_TextBuffer *buffer)
199 {
200   /* FIXME this is useless, */
201   ME_DisplayItem *pItem = buffer->pFirst;
202   TRACE("DOCUMENT DUMP START\n");
203   while(pItem) {
204     switch(pItem->type)
205     {
206       case diTextStart:
207         TRACE("Start\n");
208         break;
209       case diCell:
210         TRACE("Cell(level=%d%s)\n", pItem->member.cell.nNestingLevel,
211               !pItem->member.cell.next_cell ? ", END" :
212                 (!pItem->member.cell.prev_cell ? ", START" :""));
213         break;
214       case diParagraph:
215         TRACE("Paragraph(ofs=%d)\n", pItem->member.para.nCharOfs);
216         if (pItem->member.para.nFlags & MEPF_ROWSTART)
217           TRACE(" - (Table Row Start)\n");
218         if (pItem->member.para.nFlags & MEPF_ROWEND)
219           TRACE(" - (Table Row End)\n");
220         break;
221       case diStartRow:
222         TRACE(" - StartRow\n");
223         break;
224       case diRun:
225         TRACE(" - Run(\"%s\", %d, flags=%x)\n", debugstr_w(pItem->member.run.strText->szData),
226           pItem->member.run.nCharOfs, pItem->member.run.nFlags);
227         break;
228       case diTextEnd:
229         TRACE("End(ofs=%d)\n", pItem->member.para.nCharOfs);
230         break;
231       default:
232         break;
233     }
234     pItem = pItem->next;
235   }
236   TRACE("DOCUMENT DUMP END\n");
237 }