2 * RichEdit - functions working on paragraphs of text (diParagraph).
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 static WCHAR wszParagraphSign
[] = {0xB6, 0};
27 void ME_MakeFirstParagraph(HDC hDC
, ME_TextBuffer
*text
)
33 ME_DisplayItem
*para
= ME_MakeDI(diParagraph
);
37 hf
= (HFONT
)GetStockObject(SYSTEM_FONT
);
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
;
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
;
58 ZeroMemory(&fmt
, sizeof(fmt
));
59 fmt
.cbSize
= sizeof(fmt
);
60 fmt
.dwMask
= PFM_ALIGNMENT
| PFM_OFFSET
| PFM_STARTINDENT
| PFM_RIGHTINDENT
| PFM_TABSTOPS
;
62 CopyMemory(para
->member
.para
.pFmt
, &fmt
, sizeof(PARAFORMAT2
));
64 style
= ME_MakeStyle(&cf
);
65 text
->pDefaultStyle
= style
;
67 run
= ME_MakeRun(style
, ME_MakeString(wszParagraphSign
), MERF_ENDPARA
);
68 run
->member
.run
.nCharOfs
= 0;
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
;
77 text
->pLast
->member
.para
.nCharOfs
= 1;
80 void ME_MarkAllForWrapping(ME_TextEditor
*editor
)
82 ME_MarkForWrapping(editor
, editor
->pBuffer
->pFirst
->member
.para
.next_para
, editor
->pBuffer
->pLast
);
85 void ME_MarkForWrapping(ME_TextEditor
*editor
, ME_DisplayItem
*first
, ME_DisplayItem
*last
)
89 first
->member
.para
.nFlags
|= MEPF_REWRAP
;
90 first
= first
->member
.para
.next_para
;
94 /* split paragraph at the beginning of the run */
95 ME_DisplayItem
*ME_SplitParagraph(ME_TextEditor
*editor
, ME_DisplayItem
*run
, ME_Style
*style
)
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
;
105 assert(run
->type
== diRun
);
107 run_para
= ME_GetParagraph(run
);
108 assert(run_para
->member
.para
.pFmt
->cbSize
== sizeof(PARAFORMAT2
));
110 ofs
= end_run
->member
.run
.nCharOfs
= run
->member
.run
.nCharOfs
;
111 next_para
= run_para
->member
.para
.next_para
;
112 assert(next_para
== ME_FindItemFwd(run_para
, diParagraphOrEnd
));
114 undo
= ME_AddUndoItem(editor
, diUndoJoinParagraphs
, NULL
);
116 undo
->nStart
= run_para
->member
.para
.nCharOfs
+ ofs
;
118 /* the new paragraph will have a different starting offset, so let's update its runs */
120 while(pp
->type
== diRun
) {
121 pp
->member
.run
.nCharOfs
-= ofs
;
122 pp
= ME_FindItemFwd(pp
, diRunOrParagraphOrEnd
);
124 new_para
->member
.para
.nCharOfs
= ME_GetParagraph(run
)->member
.para
.nCharOfs
+ofs
;
125 new_para
->member
.para
.nCharOfs
+= 1;
127 new_para
->member
.para
.nFlags
= MEPF_REWRAP
; /* FIXME copy flags (if applicable) */
128 /* FIXME initialize format style and call ME_SetParaFormat blah blah */
129 CopyMemory(new_para
->member
.para
.pFmt
, run_para
->member
.para
.pFmt
, sizeof(PARAFORMAT2
));
131 /* FIXME remove this as soon as nLeftMargin etc are replaced with proper fields of PARAFORMAT2 */
132 new_para
->member
.para
.nLeftMargin
= run_para
->member
.para
.nLeftMargin
;
133 new_para
->member
.para
.nRightMargin
= run_para
->member
.para
.nRightMargin
;
134 new_para
->member
.para
.nFirstMargin
= run_para
->member
.para
.nFirstMargin
;
136 /* insert paragraph into paragraph double linked list */
137 new_para
->member
.para
.prev_para
= run_para
;
138 new_para
->member
.para
.next_para
= next_para
;
139 run_para
->member
.para
.next_para
= new_para
;
140 next_para
->member
.para
.prev_para
= new_para
;
142 /* insert end run of the old paragraph, and new paragraph, into DI double linked list */
143 ME_InsertBefore(run
, new_para
);
144 ME_InsertBefore(new_para
, end_run
);
146 /* force rewrap of the */
147 run_para
->member
.para
.prev_para
->member
.para
.nFlags
|= MEPF_REWRAP
;
148 new_para
->member
.para
.prev_para
->member
.para
.nFlags
|= MEPF_REWRAP
;
150 /* we've added the end run, so we need to modify nCharOfs in the next paragraphs */
151 ME_PropagateCharOffset(next_para
, 1);
152 editor
->nParagraphs
++;
157 /* join tp with tp->member.para.next_para, keeping tp's style; this
158 * is consistent with the original */
159 ME_DisplayItem
*ME_JoinParagraphs(ME_TextEditor
*editor
, ME_DisplayItem
*tp
)
161 ME_DisplayItem
*pNext
, *pFirstRunInNext
, *pRun
, *pTmp
;
163 ME_UndoItem
*undo
= NULL
;
165 assert(tp
->type
== diParagraph
);
166 assert(tp
->member
.para
.next_para
);
167 assert(tp
->member
.para
.next_para
->type
== diParagraph
);
169 pNext
= tp
->member
.para
.next_para
;
172 /* null char format operation to store the original char format for the ENDPARA run */
174 ME_InitCharFormat2W(&fmt
);
175 ME_SetCharFormat(editor
, pNext
->member
.para
.nCharOfs
-1, 1, &fmt
);
177 undo
= ME_AddUndoItem(editor
, diUndoSplitParagraph
, NULL
);
180 undo
->nStart
= pNext
->member
.para
.nCharOfs
-1;
181 assert(pNext
->member
.para
.pFmt
->cbSize
== sizeof(PARAFORMAT2
));
182 CopyMemory(undo
->di
.member
.para
.pFmt
, pNext
->member
.para
.pFmt
, sizeof(PARAFORMAT2
));
185 shift
= pNext
->member
.para
.nCharOfs
- tp
->member
.para
.nCharOfs
- 1;
187 pRun
= ME_FindItemBack(pNext
, diRunOrParagraph
);
188 pFirstRunInNext
= ME_FindItemFwd(pNext
, diRunOrParagraph
);
191 assert(pRun
->type
== diRun
);
192 assert(pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
193 assert(pFirstRunInNext
->type
== diRun
);
195 /* if some cursor points at end of paragraph, make it point to the first
196 run of the next joined paragraph */
197 for (i
=0; i
<editor
->nCursors
; i
++) {
198 if (editor
->pCursors
[i
].pRun
== pRun
) {
199 editor
->pCursors
[i
].pRun
= pFirstRunInNext
;
200 editor
->pCursors
[i
].nOffset
= 0;
206 pTmp
= ME_FindItemFwd(pTmp
, diRunOrParagraphOrEnd
);
207 if (pTmp
->type
!= diRun
)
209 TRACE("shifting \"%s\" by %d (previous %d)\n", debugstr_w(pTmp
->member
.run
.strText
->szData
), shift
, pTmp
->member
.run
.nCharOfs
);
210 pTmp
->member
.run
.nCharOfs
+= shift
;
214 ME_DestroyDisplayItem(pRun
);
216 tp
->member
.para
.next_para
= pNext
->member
.para
.next_para
;
217 pNext
->member
.para
.next_para
->member
.para
.prev_para
= tp
;
219 ME_DestroyDisplayItem(pNext
);
221 ME_PropagateCharOffset(tp
->member
.para
.next_para
, -1);
223 ME_CheckCharOffsets(editor
);
225 editor
->nParagraphs
--;
226 tp
->member
.para
.nFlags
|= MEPF_REWRAP
;
230 ME_DisplayItem
*ME_GetParagraph(ME_DisplayItem
*item
) {
231 return ME_FindItemBackOrHere(item
, diParagraph
);
234 static void ME_DumpStyleEffect(char **p
, const char *name
, PARAFORMAT2
*fmt
, int mask
)
236 *p
+= sprintf(*p
, "%-22s%s\n", name
, (fmt
->dwMask
& mask
) ? ((fmt
->wEffects
& mask
) ? "yes" : "no") : "N/A");
239 void ME_DumpParaStyleToBuf(PARAFORMAT2
*pFmt
, char buf
[2048])
241 /* FIXME only PARAFORMAT styles implemented */
244 p
+= sprintf(p
, "Alignment: %s\n",
245 !(pFmt
->dwMask
& PFM_ALIGNMENT
) ? "N/A" :
246 ((pFmt
->wAlignment
== PFA_LEFT
) ? "left" :
247 ((pFmt
->wAlignment
== PFA_RIGHT
) ? "right" :
248 ((pFmt
->wAlignment
== PFA_CENTER
) ? "center" :
249 /*((pFmt->wAlignment == PFA_JUSTIFY) ? "justify" : "incorrect")*/
252 if (pFmt
->dwMask
& PFM_OFFSET
)
253 p
+= sprintf(p
, "Offset: %d\n", (int)pFmt
->dxOffset
);
255 p
+= sprintf(p
, "Offset: N/A\n");
257 if (pFmt
->dwMask
& PFM_OFFSETINDENT
)
258 p
+= sprintf(p
, "Offset indent: %d\n", (int)pFmt
->dxStartIndent
);
260 p
+= sprintf(p
, "Offset indent: N/A\n");
262 if (pFmt
->dwMask
& PFM_STARTINDENT
)
263 p
+= sprintf(p
, "Start indent: %d\n", (int)pFmt
->dxStartIndent
);
265 p
+= sprintf(p
, "Start indent: N/A\n");
267 if (pFmt
->dwMask
& PFM_RIGHTINDENT
)
268 p
+= sprintf(p
, "Right indent: %d\n", (int)pFmt
->dxRightIndent
);
270 p
+= sprintf(p
, "Right indent: N/A\n");
272 ME_DumpStyleEffect(&p
, "Page break before:", pFmt
, PFM_PAGEBREAKBEFORE
);
275 void ME_SetParaFormat(ME_TextEditor
*editor
, ME_DisplayItem
*para
, PARAFORMAT2
*pFmt
)
278 assert(sizeof(*para
->member
.para
.pFmt
) == sizeof(PARAFORMAT2
));
279 ME_AddUndoItem(editor
, diUndoSetParagraphFormat
, para
);
281 CopyMemory(©
, para
->member
.para
.pFmt
, sizeof(PARAFORMAT2
));
283 if (pFmt
->dwMask
& PFM_ALIGNMENT
)
284 para
->member
.para
.pFmt
->wAlignment
= pFmt
->wAlignment
;
285 if (pFmt
->dwMask
& PFM_STARTINDENT
)
286 para
->member
.para
.pFmt
->dxStartIndent
= pFmt
->dxStartIndent
;
287 if (pFmt
->dwMask
& PFM_OFFSET
)
288 para
->member
.para
.pFmt
->dxOffset
= pFmt
->dxOffset
;
289 if (pFmt
->dwMask
& PFM_OFFSETINDENT
)
290 para
->member
.para
.pFmt
->dxStartIndent
+= pFmt
->dxStartIndent
;
292 if (pFmt
->dwMask
& PFM_TABSTOPS
)
294 para
->member
.para
.pFmt
->cTabCount
= pFmt
->cTabCount
;
295 memcpy(para
->member
.para
.pFmt
->rgxTabs
, pFmt
->rgxTabs
, pFmt
->cTabCount
*sizeof(int));
298 /* FIXME to be continued (indents, bulleting and such) */
300 if (memcmp(©
, para
->member
.para
.pFmt
, sizeof(PARAFORMAT2
)))
301 para
->member
.para
.nFlags
|= MEPF_REWRAP
;
304 void ME_SetSelectionParaFormat(ME_TextEditor
*editor
, PARAFORMAT2
*pFmt
)
307 ME_DisplayItem
*para
, *para_end
, *run
;
310 ME_GetSelection(editor
, &nFrom
, &nTo
);
311 if (nTo
>nFrom
) /* selection consists of chars from nFrom up to nTo-1 */
314 ME_RunOfsFromCharOfs(editor
, nFrom
, &run
, &nOffset
);
315 para
= ME_GetParagraph(run
);
316 ME_RunOfsFromCharOfs(editor
, nTo
, &run
, &nOffset
);
317 para_end
= ME_GetParagraph(run
);
320 ME_SetParaFormat(editor
, para
, pFmt
);
321 if (para
== para_end
)
323 para
= para
->member
.para
.next_para
;
327 void ME_GetParaFormat(ME_TextEditor
*editor
, ME_DisplayItem
*para
, PARAFORMAT2
*pFmt
)
329 if (pFmt
->cbSize
>= sizeof(PARAFORMAT2
))
331 CopyMemory(pFmt
, para
->member
.para
.pFmt
, sizeof(PARAFORMAT2
));
334 CopyMemory(pFmt
, para
->member
.para
.pFmt
, pFmt
->cbSize
);
337 void ME_GetSelectionParaFormat(ME_TextEditor
*editor
, PARAFORMAT2
*pFmt
)
340 ME_DisplayItem
*para
, *para_end
, *run
;
344 ME_GetSelection(editor
, &nFrom
, &nTo
);
345 if (nTo
>nFrom
) /* selection consists of chars from nFrom up to nTo-1 */
348 ME_RunOfsFromCharOfs(editor
, nFrom
, &run
, &nOffset
);
349 para
= ME_GetParagraph(run
);
350 ME_RunOfsFromCharOfs(editor
, nTo
, &run
, &nOffset
);
351 para_end
= ME_GetParagraph(run
);
353 ME_GetParaFormat(editor
, para
, pFmt
);
354 if (para
== para_end
) return;
357 ZeroMemory(&tmp
, sizeof(tmp
));
358 tmp
.cbSize
= sizeof(tmp
);
359 ME_GetParaFormat(editor
, para
, &tmp
);
361 assert(tmp
.dwMask
& PFM_ALIGNMENT
);
362 if (pFmt
->wAlignment
!= tmp
.wAlignment
)
363 pFmt
->dwMask
&= ~PFM_ALIGNMENT
;
365 assert(tmp
.dwMask
& PFM_STARTINDENT
);
366 if (pFmt
->dxStartIndent
!= tmp
.dxStartIndent
)
367 pFmt
->dwMask
&= ~PFM_STARTINDENT
;
369 assert(tmp
.dwMask
& PFM_OFFSET
);
370 if (pFmt
->dxOffset
!= tmp
.dxOffset
)
371 pFmt
->dwMask
&= ~PFM_OFFSET
;
373 assert(tmp
.dwMask
& PFM_TABSTOPS
);
374 if (pFmt
->dwMask
& PFM_TABSTOPS
) {
375 if (pFmt
->cTabCount
!= tmp
.cTabCount
)
376 pFmt
->dwMask
&= ~PFM_TABSTOPS
;
378 if (memcmp(pFmt
->rgxTabs
, tmp
.rgxTabs
, tmp
.cTabCount
*sizeof(int)))
379 pFmt
->dwMask
&= ~PFM_TABSTOPS
;
382 if (para
== para_end
)
384 para
= para
->member
.para
.next_para
;