Rbuild changes for include structure change.
[reactos.git] / reactos / dll / win32 / riched20 / para.c
1 /*
2 * RichEdit - functions working on paragraphs of text (diParagraph).
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include "editor.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
24
25 static WCHAR wszParagraphSign[] = {0xB6, 0};
26
27 void ME_MakeFirstParagraph(HDC hDC, ME_TextBuffer *text)
28 {
29 PARAFORMAT2 fmt;
30 CHARFORMAT2W cf;
31 LOGFONTW lf;
32 HFONT hf;
33 ME_DisplayItem *para = ME_MakeDI(diParagraph);
34 ME_DisplayItem *run;
35 ME_Style *style;
36
37 hf = (HFONT)GetStockObject(SYSTEM_FONT);
38 assert(hf);
39 GetObjectW(hf, sizeof(LOGFONTW), &lf);
40 ZeroMemory(&cf, sizeof(cf));
41 cf.cbSize = sizeof(cf);
42 cf.dwMask = CFM_BACKCOLOR|CFM_COLOR|CFM_FACE|CFM_SIZE|CFM_CHARSET;
43 cf.dwMask |= CFM_ALLCAPS|CFM_BOLD|CFM_DISABLED|CFM_EMBOSS|CFM_HIDDEN;
44 cf.dwMask |= CFM_IMPRINT|CFM_ITALIC|CFM_LINK|CFM_OUTLINE|CFM_PROTECTED;
45 cf.dwMask |= CFM_REVISED|CFM_SHADOW|CFM_SMALLCAPS|CFM_STRIKEOUT;
46 cf.dwMask |= CFM_SUBSCRIPT|CFM_UNDERLINE;
47
48 cf.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
49 lstrcpyW(cf.szFaceName, lf.lfFaceName);
50 cf.yHeight=lf.lfHeight*1440/GetDeviceCaps(hDC, LOGPIXELSY);
51 if (lf.lfWeight>=700) /* FIXME correct weight ? */
52 cf.dwEffects |= CFE_BOLD;
53 cf.wWeight = lf.lfWeight;
54 if (lf.lfItalic) cf.dwEffects |= CFE_ITALIC;
55 if (lf.lfUnderline) cf.dwEffects |= CFE_UNDERLINE;
56 if (lf.lfStrikeOut) cf.dwEffects |= CFE_STRIKEOUT;
57
58 ZeroMemory(&fmt, sizeof(fmt));
59 fmt.cbSize = sizeof(fmt);
60 fmt.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_STARTINDENT | PFM_RIGHTINDENT | PFM_TABSTOPS;
61
62 CopyMemory(para->member.para.pFmt, &fmt, sizeof(PARAFORMAT2));
63
64 style = ME_MakeStyle(&cf);
65 text->pDefaultStyle = style;
66
67 run = ME_MakeRun(style, ME_MakeString(wszParagraphSign), MERF_ENDPARA);
68 run->member.run.nCharOfs = 0;
69
70 ME_InsertBefore(text->pLast, para);
71 ME_InsertBefore(text->pLast, run);
72 para->member.para.prev_para = text->pFirst;
73 para->member.para.next_para = text->pLast;
74 text->pFirst->member.para.next_para = para;
75 text->pLast->member.para.prev_para = para;
76
77 text->pLast->member.para.nCharOfs = 1;
78 }
79
80 void ME_MarkAllForWrapping(ME_TextEditor *editor)
81 {
82 ME_MarkForWrapping(editor, editor->pBuffer->pFirst->member.para.next_para, editor->pBuffer->pLast);
83 }
84
85 void ME_MarkForWrapping(ME_TextEditor *editor, ME_DisplayItem *first, ME_DisplayItem *last)
86 {
87 while(first != last)
88 {
89 first->member.para.nFlags |= MEPF_REWRAP;
90 first = first->member.para.next_para;
91 }
92 }
93
94 /* split paragraph at the beginning of the run */
95 ME_DisplayItem *ME_SplitParagraph(ME_TextEditor *editor, ME_DisplayItem *run, ME_Style *style)
96 {
97 ME_DisplayItem *next_para = NULL;
98 ME_DisplayItem *run_para = NULL;
99 ME_DisplayItem *new_para = ME_MakeDI(diParagraph);
100 ME_DisplayItem *end_run = ME_MakeRun(style,ME_MakeString(wszParagraphSign), MERF_ENDPARA);
101 ME_UndoItem *undo = NULL;
102 int ofs;
103 ME_DisplayItem *pp;
104 int end_len = (editor->bEmulateVersion10 ? 2 : 1);
105
106 assert(run->type == diRun);
107
108 run_para = ME_GetParagraph(run);
109 assert(run_para->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
110
111 ofs = end_run->member.run.nCharOfs = run->member.run.nCharOfs;
112 next_para = run_para->member.para.next_para;
113 assert(next_para == ME_FindItemFwd(run_para, diParagraphOrEnd));
114
115 undo = ME_AddUndoItem(editor, diUndoJoinParagraphs, NULL);
116 if (undo)
117 undo->nStart = run_para->member.para.nCharOfs + ofs;
118
119 /* the new paragraph will have a different starting offset, so let's update its runs */
120 pp = run;
121 while(pp->type == diRun) {
122 pp->member.run.nCharOfs -= ofs;
123 pp = ME_FindItemFwd(pp, diRunOrParagraphOrEnd);
124 }
125 new_para->member.para.nCharOfs = ME_GetParagraph(run)->member.para.nCharOfs+ofs;
126 new_para->member.para.nCharOfs += end_len;
127
128 new_para->member.para.nFlags = MEPF_REWRAP; /* FIXME copy flags (if applicable) */
129 /* FIXME initialize format style and call ME_SetParaFormat blah blah */
130 CopyMemory(new_para->member.para.pFmt, run_para->member.para.pFmt, sizeof(PARAFORMAT2));
131
132 /* FIXME remove this as soon as nLeftMargin etc are replaced with proper fields of PARAFORMAT2 */
133 new_para->member.para.nLeftMargin = run_para->member.para.nLeftMargin;
134 new_para->member.para.nRightMargin = run_para->member.para.nRightMargin;
135 new_para->member.para.nFirstMargin = run_para->member.para.nFirstMargin;
136
137 /* insert paragraph into paragraph double linked list */
138 new_para->member.para.prev_para = run_para;
139 new_para->member.para.next_para = next_para;
140 run_para->member.para.next_para = new_para;
141 next_para->member.para.prev_para = new_para;
142
143 /* insert end run of the old paragraph, and new paragraph, into DI double linked list */
144 ME_InsertBefore(run, new_para);
145 ME_InsertBefore(new_para, end_run);
146
147 /* force rewrap of the */
148 run_para->member.para.prev_para->member.para.nFlags |= MEPF_REWRAP;
149 new_para->member.para.prev_para->member.para.nFlags |= MEPF_REWRAP;
150
151 /* we've added the end run, so we need to modify nCharOfs in the next paragraphs */
152 ME_PropagateCharOffset(next_para, end_len);
153 editor->nParagraphs++;
154
155 return new_para;
156 }
157
158 /* join tp with tp->member.para.next_para, keeping tp's style; this
159 * is consistent with the original */
160 ME_DisplayItem *ME_JoinParagraphs(ME_TextEditor *editor, ME_DisplayItem *tp)
161 {
162 ME_DisplayItem *pNext, *pFirstRunInNext, *pRun, *pTmp;
163 int i, shift;
164 ME_UndoItem *undo = NULL;
165 int end_len = (editor->bEmulateVersion10 ? 2 : 1);
166
167 assert(tp->type == diParagraph);
168 assert(tp->member.para.next_para);
169 assert(tp->member.para.next_para->type == diParagraph);
170
171 pNext = tp->member.para.next_para;
172
173 {
174 /* null char format operation to store the original char format for the ENDPARA run */
175 CHARFORMAT2W fmt;
176 ME_InitCharFormat2W(&fmt);
177 ME_SetCharFormat(editor, pNext->member.para.nCharOfs - end_len, end_len, &fmt);
178 }
179 undo = ME_AddUndoItem(editor, diUndoSplitParagraph, NULL);
180 if (undo)
181 {
182 undo->nStart = pNext->member.para.nCharOfs - end_len;
183 assert(pNext->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
184 CopyMemory(undo->di.member.para.pFmt, pNext->member.para.pFmt, sizeof(PARAFORMAT2));
185 }
186
187 shift = pNext->member.para.nCharOfs - tp->member.para.nCharOfs - end_len;
188
189 pRun = ME_FindItemBack(pNext, diRunOrParagraph);
190 pFirstRunInNext = ME_FindItemFwd(pNext, diRunOrParagraph);
191
192 assert(pRun);
193 assert(pRun->type == diRun);
194 assert(pRun->member.run.nFlags & MERF_ENDPARA);
195 assert(pFirstRunInNext->type == diRun);
196
197 /* if some cursor points at end of paragraph, make it point to the first
198 run of the next joined paragraph */
199 for (i=0; i<editor->nCursors; i++) {
200 if (editor->pCursors[i].pRun == pRun) {
201 editor->pCursors[i].pRun = pFirstRunInNext;
202 editor->pCursors[i].nOffset = 0;
203 }
204 }
205
206 pTmp = pNext;
207 do {
208 pTmp = ME_FindItemFwd(pTmp, diRunOrParagraphOrEnd);
209 if (pTmp->type != diRun)
210 break;
211 TRACE("shifting \"%s\" by %d (previous %d)\n", debugstr_w(pTmp->member.run.strText->szData), shift, pTmp->member.run.nCharOfs);
212 pTmp->member.run.nCharOfs += shift;
213 } while(1);
214
215 ME_Remove(pRun);
216 ME_DestroyDisplayItem(pRun);
217
218 tp->member.para.next_para = pNext->member.para.next_para;
219 pNext->member.para.next_para->member.para.prev_para = tp;
220 ME_Remove(pNext);
221 ME_DestroyDisplayItem(pNext);
222
223 ME_PropagateCharOffset(tp->member.para.next_para, -end_len);
224
225 ME_CheckCharOffsets(editor);
226
227 editor->nParagraphs--;
228 tp->member.para.nFlags |= MEPF_REWRAP;
229 return tp;
230 }
231
232 ME_DisplayItem *ME_GetParagraph(ME_DisplayItem *item) {
233 return ME_FindItemBackOrHere(item, diParagraph);
234 }
235
236 static void ME_DumpStyleEffect(char **p, const char *name, PARAFORMAT2 *fmt, int mask)
237 {
238 *p += sprintf(*p, "%-22s%s\n", name, (fmt->dwMask & mask) ? ((fmt->wEffects & mask) ? "yes" : "no") : "N/A");
239 }
240
241 void ME_DumpParaStyleToBuf(PARAFORMAT2 *pFmt, char buf[2048])
242 {
243 /* FIXME only PARAFORMAT styles implemented */
244 char *p;
245 p = buf;
246 p += sprintf(p, "Alignment: %s\n",
247 !(pFmt->dwMask & PFM_ALIGNMENT) ? "N/A" :
248 ((pFmt->wAlignment == PFA_LEFT) ? "left" :
249 ((pFmt->wAlignment == PFA_RIGHT) ? "right" :
250 ((pFmt->wAlignment == PFA_CENTER) ? "center" :
251 /*((pFmt->wAlignment == PFA_JUSTIFY) ? "justify" : "incorrect")*/
252 "incorrect"))));
253
254 if (pFmt->dwMask & PFM_OFFSET)
255 p += sprintf(p, "Offset: %d\n", (int)pFmt->dxOffset);
256 else
257 p += sprintf(p, "Offset: N/A\n");
258
259 if (pFmt->dwMask & PFM_OFFSETINDENT)
260 p += sprintf(p, "Offset indent: %d\n", (int)pFmt->dxStartIndent);
261 else
262 p += sprintf(p, "Offset indent: N/A\n");
263
264 if (pFmt->dwMask & PFM_STARTINDENT)
265 p += sprintf(p, "Start indent: %d\n", (int)pFmt->dxStartIndent);
266 else
267 p += sprintf(p, "Start indent: N/A\n");
268
269 if (pFmt->dwMask & PFM_RIGHTINDENT)
270 p += sprintf(p, "Right indent: %d\n", (int)pFmt->dxRightIndent);
271 else
272 p += sprintf(p, "Right indent: N/A\n");
273
274 ME_DumpStyleEffect(&p, "Page break before:", pFmt, PFM_PAGEBREAKBEFORE);
275 }
276
277 void ME_SetParaFormat(ME_TextEditor *editor, ME_DisplayItem *para, PARAFORMAT2 *pFmt)
278 {
279 PARAFORMAT2 copy;
280 assert(sizeof(*para->member.para.pFmt) == sizeof(PARAFORMAT2));
281 ME_AddUndoItem(editor, diUndoSetParagraphFormat, para);
282
283 CopyMemory(&copy, para->member.para.pFmt, sizeof(PARAFORMAT2));
284
285 if (pFmt->dwMask & PFM_ALIGNMENT)
286 para->member.para.pFmt->wAlignment = pFmt->wAlignment;
287 if (pFmt->dwMask & PFM_STARTINDENT)
288 para->member.para.pFmt->dxStartIndent = pFmt->dxStartIndent;
289 if (pFmt->dwMask & PFM_OFFSET)
290 para->member.para.pFmt->dxOffset = pFmt->dxOffset;
291 if (pFmt->dwMask & PFM_OFFSETINDENT)
292 para->member.para.pFmt->dxStartIndent += pFmt->dxStartIndent;
293
294 if (pFmt->dwMask & PFM_TABSTOPS)
295 {
296 para->member.para.pFmt->cTabCount = pFmt->cTabCount;
297 memcpy(para->member.para.pFmt->rgxTabs, pFmt->rgxTabs, pFmt->cTabCount*sizeof(int));
298 }
299
300 /* FIXME to be continued (indents, bulleting and such) */
301
302 if (memcmp(&copy, para->member.para.pFmt, sizeof(PARAFORMAT2)))
303 para->member.para.nFlags |= MEPF_REWRAP;
304 }
305
306 void ME_SetSelectionParaFormat(ME_TextEditor *editor, PARAFORMAT2 *pFmt)
307 {
308 int nFrom, nTo;
309 ME_DisplayItem *para, *para_end, *run;
310 int nOffset;
311
312 ME_GetSelection(editor, &nFrom, &nTo);
313 if (nTo>nFrom) /* selection consists of chars from nFrom up to nTo-1 */
314 nTo--;
315
316 ME_RunOfsFromCharOfs(editor, nFrom, &run, &nOffset);
317 para = ME_GetParagraph(run);
318 ME_RunOfsFromCharOfs(editor, nTo, &run, &nOffset);
319 para_end = ME_GetParagraph(run);
320
321 do {
322 ME_SetParaFormat(editor, para, pFmt);
323 if (para == para_end)
324 break;
325 para = para->member.para.next_para;
326 } while(1);
327 }
328
329 void ME_GetParaFormat(ME_TextEditor *editor, ME_DisplayItem *para, PARAFORMAT2 *pFmt)
330 {
331 if (pFmt->cbSize >= sizeof(PARAFORMAT2))
332 {
333 CopyMemory(pFmt, para->member.para.pFmt, sizeof(PARAFORMAT2));
334 return;
335 }
336 CopyMemory(pFmt, para->member.para.pFmt, pFmt->cbSize);
337 }
338
339 void ME_GetSelectionParaFormat(ME_TextEditor *editor, PARAFORMAT2 *pFmt)
340 {
341 int nFrom, nTo;
342 ME_DisplayItem *para, *para_end, *run;
343 int nOffset;
344 PARAFORMAT2 tmp;
345
346 ME_GetSelection(editor, &nFrom, &nTo);
347 if (nTo>nFrom) /* selection consists of chars from nFrom up to nTo-1 */
348 nTo--;
349
350 ME_RunOfsFromCharOfs(editor, nFrom, &run, &nOffset);
351 para = ME_GetParagraph(run);
352 ME_RunOfsFromCharOfs(editor, nTo, &run, &nOffset);
353 para_end = ME_GetParagraph(run);
354
355 ME_GetParaFormat(editor, para, pFmt);
356 if (para == para_end) return;
357
358 do {
359 ZeroMemory(&tmp, sizeof(tmp));
360 tmp.cbSize = sizeof(tmp);
361 ME_GetParaFormat(editor, para, &tmp);
362
363 assert(tmp.dwMask & PFM_ALIGNMENT);
364 if (pFmt->wAlignment != tmp.wAlignment)
365 pFmt->dwMask &= ~PFM_ALIGNMENT;
366
367 assert(tmp.dwMask & PFM_STARTINDENT);
368 if (pFmt->dxStartIndent != tmp.dxStartIndent)
369 pFmt->dwMask &= ~PFM_STARTINDENT;
370
371 assert(tmp.dwMask & PFM_OFFSET);
372 if (pFmt->dxOffset != tmp.dxOffset)
373 pFmt->dwMask &= ~PFM_OFFSET;
374
375 assert(tmp.dwMask & PFM_TABSTOPS);
376 if (pFmt->dwMask & PFM_TABSTOPS) {
377 if (pFmt->cTabCount != tmp.cTabCount)
378 pFmt->dwMask &= ~PFM_TABSTOPS;
379 else
380 if (memcmp(pFmt->rgxTabs, tmp.rgxTabs, tmp.cTabCount*sizeof(int)))
381 pFmt->dwMask &= ~PFM_TABSTOPS;
382 }
383
384 if (para == para_end)
385 return;
386 para = para->member.para.next_para;
387 } while(1);
388 }