richedit: Initially disable scrollbars for ES_DISABLENOSCROLL.
[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   if (type==nTypeOrClass)
48     return TRUE;
49   if (nTypeOrClass==diRunOrParagraph && (type==diRun || type==diParagraph))
50     return TRUE;
51   if (nTypeOrClass==diRunOrStartRow && (type==diRun || type==diStartRow))
52     return TRUE;
53   if (nTypeOrClass==diParagraphOrEnd && (type==diTextEnd || type==diParagraph))
54     return TRUE;
55   if (nTypeOrClass==diStartRowOrParagraph && (type==diStartRow || type==diParagraph))
56     return TRUE;
57   if (nTypeOrClass==diStartRowOrParagraphOrEnd
58     && (type==diStartRow || type==diParagraph || type==diTextEnd))
59     return TRUE;
60   if (nTypeOrClass==diRunOrParagraphOrEnd
61     && (type==diRun || type==diParagraph || type==diTextEnd))
62     return TRUE;
63   return FALSE;
64 }
65
66 ME_DisplayItem *ME_FindItemBack(ME_DisplayItem *di, ME_DIType nTypeOrClass)
67 {
68   if (!di)
69     return NULL;
70   di = di->prev;
71   while(di!=NULL) {
72     if (ME_DITypesEqual(di->type, nTypeOrClass))
73       return di;
74     di = di->prev;
75   }
76   return NULL;
77 }
78
79 ME_DisplayItem *ME_FindItemBackOrHere(ME_DisplayItem *di, ME_DIType nTypeOrClass)
80 {
81   while(di!=NULL) {
82     if (ME_DITypesEqual(di->type, nTypeOrClass))
83       return di;
84     di = di->prev;
85   }
86   return NULL;
87 }
88
89 ME_DisplayItem *ME_FindItemFwd(ME_DisplayItem *di, ME_DIType nTypeOrClass)
90 {
91   if (!di) return NULL;
92   di = di->next;
93   while(di!=NULL) {
94     if (ME_DITypesEqual(di->type, nTypeOrClass))
95       return di;
96     di = di->next;
97   }
98   return NULL;
99 }
100
101 void ME_DestroyDisplayItem(ME_DisplayItem *item) {
102 /*  TRACE("type=%s\n", ME_GetDITypeName(item->type)); */
103   if (item->type==diParagraph || item->type == diUndoSetParagraphFormat) {
104     FREE_OBJ(item->member.para.pFmt);
105   }
106   if (item->type==diRun || item->type == diUndoInsertRun) {
107     if (item->member.run.ole_obj) ME_DeleteReObject(item->member.run.ole_obj);
108     ME_ReleaseStyle(item->member.run.style);
109     ME_DestroyString(item->member.run.strText);
110   }
111   if (item->type==diUndoSetCharFormat) {
112     ME_ReleaseStyle(item->member.ustyle);
113   }
114   if (item->type==diUndoSplitParagraph) {
115      FREE_OBJ(item->member.para.pFmt);
116      FREE_OBJ(item->member.para.pCell);
117   }
118   FREE_OBJ(item);
119 }
120
121 ME_DisplayItem *ME_MakeDI(ME_DIType type) {
122   ME_DisplayItem *item = ALLOC_OBJ(ME_DisplayItem);
123   ZeroMemory(item, sizeof(ME_DisplayItem));
124   item->type = type;
125   item->prev = item->next = NULL;
126   if (type == diParagraph || type == diUndoSplitParagraph) {
127     item->member.para.pFmt = ALLOC_OBJ(PARAFORMAT2);
128     ME_SetDefaultParaFormat(item->member.para.pFmt);
129     item->member.para.nFlags = MEPF_REWRAP;
130   }
131     
132   return item;
133 }
134
135 const char *ME_GetDITypeName(ME_DIType type)
136 {
137   switch(type)
138   {
139     case diParagraph: return "diParagraph";
140     case diRun: return "diRun";
141     case diCell: return "diCell";
142     case diTextStart: return "diTextStart";
143     case diTextEnd: return "diTextEnd";
144     case diStartRow: return "diStartRow";
145     case diUndoEndTransaction: return "diUndoEndTransaction";
146     case diUndoPotentialEndTransaction: return "diUndoPotentialEndTransaction";
147     case diUndoSetParagraphFormat: return "diUndoSetParagraphFormat";
148     case diUndoSetCharFormat: return "diUndoSetCharFormat";
149     case diUndoInsertRun: return "diUndoInsertRun";
150     case diUndoDeleteRun: return "diUndoDeleteRun";
151     case diUndoJoinParagraphs: return "diJoinParagraphs";
152     case diUndoSplitParagraph: return "diSplitParagraph";
153     default: return "?";
154   }
155 }
156
157 void ME_DumpDocument(ME_TextBuffer *buffer)
158 {
159   /* FIXME this is useless, */
160   ME_DisplayItem *pItem = buffer->pFirst;
161   TRACE("DOCUMENT DUMP START\n");
162   while(pItem) {
163     switch(pItem->type)
164     {
165       case diTextStart:
166         TRACE("Start\n");
167         break;
168       case diCell:
169         TRACE("Cell(level=%d%s)\n", pItem->member.cell.nNestingLevel,
170               !pItem->member.cell.next_cell ? ", END" :
171                 (!pItem->member.cell.prev_cell ? ", START" :""));
172         break;
173       case diParagraph:
174         TRACE("Paragraph(ofs=%d)\n", pItem->member.para.nCharOfs);
175         if (pItem->member.para.nFlags & MEPF_ROWSTART)
176           TRACE(" - (Table Row Start)\n");
177         if (pItem->member.para.nFlags & MEPF_ROWEND)
178           TRACE(" - (Table Row End)\n");
179         break;
180       case diStartRow:
181         TRACE(" - StartRow\n");
182         break;
183       case diRun:
184         TRACE(" - Run(\"%s\", %d, flags=%x)\n", debugstr_w(pItem->member.run.strText->szData),
185           pItem->member.run.nCharOfs, pItem->member.run.nFlags);
186         break;
187       case diTextEnd:
188         TRACE("End(ofs=%d)\n", pItem->member.para.nCharOfs);
189         break;
190       default:
191         break;
192     }
193     pItem = pItem->next;
194   }
195   TRACE("DOCUMENT DUMP END\n");
196 }