2 * RichEdit - functions working on paragraphs of text (diParagraph).
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2006 by Phil Krylov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 WINE_DEFAULT_DEBUG_CHANNEL(richedit
);
26 static const WCHAR wszParagraphSign
[] = {0xB6, 0};
28 void ME_MakeFirstParagraph(HDC hDC
, ME_TextBuffer
*text
)
34 ME_DisplayItem
*para
= ME_MakeDI(diParagraph
);
38 hf
= (HFONT
)GetStockObject(SYSTEM_FONT
);
40 GetObjectW(hf
, sizeof(LOGFONTW
), &lf
);
41 ZeroMemory(&cf
, sizeof(cf
));
42 cf
.cbSize
= sizeof(cf
);
43 cf
.dwMask
= CFM_BACKCOLOR
|CFM_COLOR
|CFM_FACE
|CFM_SIZE
|CFM_CHARSET
;
44 cf
.dwMask
|= CFM_ALLCAPS
|CFM_BOLD
|CFM_DISABLED
|CFM_EMBOSS
|CFM_HIDDEN
;
45 cf
.dwMask
|= CFM_IMPRINT
|CFM_ITALIC
|CFM_LINK
|CFM_OUTLINE
|CFM_PROTECTED
;
46 cf
.dwMask
|= CFM_REVISED
|CFM_SHADOW
|CFM_SMALLCAPS
|CFM_STRIKEOUT
;
47 cf
.dwMask
|= CFM_SUBSCRIPT
|CFM_UNDERLINE
;
49 cf
.dwEffects
= CFE_AUTOCOLOR
| CFE_AUTOBACKCOLOR
;
50 lstrcpyW(cf
.szFaceName
, lf
.lfFaceName
);
51 cf
.yHeight
=lf
.lfHeight
*1440/GetDeviceCaps(hDC
, LOGPIXELSY
);
52 if (lf
.lfWeight
>=700) /* FIXME correct weight ? */
53 cf
.dwEffects
|= CFE_BOLD
;
54 cf
.wWeight
= lf
.lfWeight
;
55 if (lf
.lfItalic
) cf
.dwEffects
|= CFE_ITALIC
;
56 if (lf
.lfUnderline
) cf
.dwEffects
|= CFE_UNDERLINE
;
57 if (lf
.lfStrikeOut
) cf
.dwEffects
|= CFE_STRIKEOUT
;
59 ZeroMemory(&fmt
, sizeof(fmt
));
60 fmt
.cbSize
= sizeof(fmt
);
61 fmt
.dwMask
= PFM_ALIGNMENT
| PFM_OFFSET
| PFM_STARTINDENT
| PFM_RIGHTINDENT
| PFM_TABSTOPS
;
63 CopyMemory(para
->member
.para
.pFmt
, &fmt
, sizeof(PARAFORMAT2
));
65 style
= ME_MakeStyle(&cf
);
66 text
->pDefaultStyle
= style
;
68 run
= ME_MakeRun(style
, ME_MakeString(wszParagraphSign
), MERF_ENDPARA
);
69 run
->member
.run
.nCharOfs
= 0;
71 ME_InsertBefore(text
->pLast
, para
);
72 ME_InsertBefore(text
->pLast
, run
);
73 para
->member
.para
.prev_para
= text
->pFirst
;
74 para
->member
.para
.next_para
= text
->pLast
;
75 text
->pFirst
->member
.para
.next_para
= para
;
76 text
->pLast
->member
.para
.prev_para
= para
;
78 text
->pLast
->member
.para
.nCharOfs
= 1;
81 void ME_MarkAllForWrapping(ME_TextEditor
*editor
)
83 ME_MarkForWrapping(editor
, editor
->pBuffer
->pFirst
->member
.para
.next_para
, editor
->pBuffer
->pLast
);
86 void ME_MarkForWrapping(ME_TextEditor
*editor
, ME_DisplayItem
*first
, ME_DisplayItem
*last
)
90 first
->member
.para
.nFlags
|= MEPF_REWRAP
;
91 first
= first
->member
.para
.next_para
;
95 /* split paragraph at the beginning of the run */
96 ME_DisplayItem
*ME_SplitParagraph(ME_TextEditor
*editor
, ME_DisplayItem
*run
, ME_Style
*style
)
98 ME_DisplayItem
*next_para
= NULL
;
99 ME_DisplayItem
*run_para
= NULL
;
100 ME_DisplayItem
*new_para
= ME_MakeDI(diParagraph
);
101 ME_DisplayItem
*end_run
= ME_MakeRun(style
,ME_MakeString(wszParagraphSign
), MERF_ENDPARA
);
102 ME_UndoItem
*undo
= NULL
;
105 int end_len
= (editor
->bEmulateVersion10
? 2 : 1);
107 assert(run
->type
== diRun
);
109 run_para
= ME_GetParagraph(run
);
110 assert(run_para
->member
.para
.pFmt
->cbSize
== sizeof(PARAFORMAT2
));
112 ofs
= end_run
->member
.run
.nCharOfs
= run
->member
.run
.nCharOfs
;
113 next_para
= run_para
->member
.para
.next_para
;
114 assert(next_para
== ME_FindItemFwd(run_para
, diParagraphOrEnd
));
116 undo
= ME_AddUndoItem(editor
, diUndoJoinParagraphs
, NULL
);
118 undo
->nStart
= run_para
->member
.para
.nCharOfs
+ ofs
;
120 /* the new paragraph will have a different starting offset, so let's update its runs */
122 while(pp
->type
== diRun
) {
123 pp
->member
.run
.nCharOfs
-= ofs
;
124 pp
= ME_FindItemFwd(pp
, diRunOrParagraphOrEnd
);
126 new_para
->member
.para
.nCharOfs
= ME_GetParagraph(run
)->member
.para
.nCharOfs
+ofs
;
127 new_para
->member
.para
.nCharOfs
+= end_len
;
129 new_para
->member
.para
.nFlags
= MEPF_REWRAP
; /* FIXME copy flags (if applicable) */
130 /* FIXME initialize format style and call ME_SetParaFormat blah blah */
131 CopyMemory(new_para
->member
.para
.pFmt
, run_para
->member
.para
.pFmt
, sizeof(PARAFORMAT2
));
133 /* FIXME remove this as soon as nLeftMargin etc are replaced with proper fields of PARAFORMAT2 */
134 new_para
->member
.para
.nLeftMargin
= run_para
->member
.para
.nLeftMargin
;
135 new_para
->member
.para
.nRightMargin
= run_para
->member
.para
.nRightMargin
;
136 new_para
->member
.para
.nFirstMargin
= run_para
->member
.para
.nFirstMargin
;
138 new_para
->member
.para
.bTable
= run_para
->member
.para
.bTable
;
140 /* Inherit previous cell definitions if any */
141 new_para
->member
.para
.pCells
= NULL
;
142 if (run_para
->member
.para
.pCells
)
144 ME_TableCell
*pCell
, *pNewCell
;
146 for (pCell
= run_para
->member
.para
.pCells
; pCell
; pCell
= pCell
->next
)
148 pNewCell
= ALLOC_OBJ(ME_TableCell
);
149 pNewCell
->nRightBoundary
= pCell
->nRightBoundary
;
150 pNewCell
->next
= NULL
;
151 if (new_para
->member
.para
.pCells
)
152 new_para
->member
.para
.pLastCell
->next
= pNewCell
;
154 new_para
->member
.para
.pCells
= pNewCell
;
155 new_para
->member
.para
.pLastCell
= pNewCell
;
159 /* fix paragraph properties. FIXME only needed when called from RTF reader */
160 if (run_para
->member
.para
.pCells
&& !run_para
->member
.para
.bTable
)
162 /* Paragraph does not have an \intbl keyword, so any table definition
163 * stored is invalid */
164 ME_DestroyTableCellList(run_para
);
167 /* insert paragraph into paragraph double linked list */
168 new_para
->member
.para
.prev_para
= run_para
;
169 new_para
->member
.para
.next_para
= next_para
;
170 run_para
->member
.para
.next_para
= new_para
;
171 next_para
->member
.para
.prev_para
= new_para
;
173 /* insert end run of the old paragraph, and new paragraph, into DI double linked list */
174 ME_InsertBefore(run
, new_para
);
175 ME_InsertBefore(new_para
, end_run
);
177 /* force rewrap of the */
178 run_para
->member
.para
.prev_para
->member
.para
.nFlags
|= MEPF_REWRAP
;
179 new_para
->member
.para
.prev_para
->member
.para
.nFlags
|= MEPF_REWRAP
;
181 /* we've added the end run, so we need to modify nCharOfs in the next paragraphs */
182 ME_PropagateCharOffset(next_para
, end_len
);
183 editor
->nParagraphs
++;
188 /* join tp with tp->member.para.next_para, keeping tp's style; this
189 * is consistent with the original */
190 ME_DisplayItem
*ME_JoinParagraphs(ME_TextEditor
*editor
, ME_DisplayItem
*tp
)
192 ME_DisplayItem
*pNext
, *pFirstRunInNext
, *pRun
, *pTmp
;
194 ME_UndoItem
*undo
= NULL
;
195 int end_len
= (editor
->bEmulateVersion10
? 2 : 1);
197 assert(tp
->type
== diParagraph
);
198 assert(tp
->member
.para
.next_para
);
199 assert(tp
->member
.para
.next_para
->type
== diParagraph
);
201 pNext
= tp
->member
.para
.next_para
;
204 /* null char format operation to store the original char format for the ENDPARA run */
206 ME_InitCharFormat2W(&fmt
);
207 ME_SetCharFormat(editor
, pNext
->member
.para
.nCharOfs
- end_len
, end_len
, &fmt
);
209 undo
= ME_AddUndoItem(editor
, diUndoSplitParagraph
, NULL
);
212 undo
->nStart
= pNext
->member
.para
.nCharOfs
- end_len
;
213 assert(pNext
->member
.para
.pFmt
->cbSize
== sizeof(PARAFORMAT2
));
214 CopyMemory(undo
->di
.member
.para
.pFmt
, pNext
->member
.para
.pFmt
, sizeof(PARAFORMAT2
));
217 shift
= pNext
->member
.para
.nCharOfs
- tp
->member
.para
.nCharOfs
- end_len
;
219 pRun
= ME_FindItemBack(pNext
, diRunOrParagraph
);
220 pFirstRunInNext
= ME_FindItemFwd(pNext
, diRunOrParagraph
);
223 assert(pRun
->type
== diRun
);
224 assert(pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
225 assert(pFirstRunInNext
->type
== diRun
);
227 /* if some cursor points at end of paragraph, make it point to the first
228 run of the next joined paragraph */
229 for (i
=0; i
<editor
->nCursors
; i
++) {
230 if (editor
->pCursors
[i
].pRun
== pRun
) {
231 editor
->pCursors
[i
].pRun
= pFirstRunInNext
;
232 editor
->pCursors
[i
].nOffset
= 0;
238 pTmp
= ME_FindItemFwd(pTmp
, diRunOrParagraphOrEnd
);
239 if (pTmp
->type
!= diRun
)
241 TRACE("shifting \"%s\" by %d (previous %d)\n", debugstr_w(pTmp
->member
.run
.strText
->szData
), shift
, pTmp
->member
.run
.nCharOfs
);
242 pTmp
->member
.run
.nCharOfs
+= shift
;
246 ME_DestroyDisplayItem(pRun
);
248 tp
->member
.para
.next_para
= pNext
->member
.para
.next_para
;
249 pNext
->member
.para
.next_para
->member
.para
.prev_para
= tp
;
251 ME_DestroyDisplayItem(pNext
);
253 ME_PropagateCharOffset(tp
->member
.para
.next_para
, -end_len
);
255 ME_CheckCharOffsets(editor
);
257 editor
->nParagraphs
--;
258 tp
->member
.para
.nFlags
|= MEPF_REWRAP
;
262 ME_DisplayItem
*ME_GetParagraph(ME_DisplayItem
*item
) {
263 return ME_FindItemBackOrHere(item
, diParagraph
);
266 static void ME_DumpStyleEffect(char **p
, const char *name
, PARAFORMAT2
*fmt
, int mask
)
268 *p
+= sprintf(*p
, "%-22s%s\n", name
, (fmt
->dwMask
& mask
) ? ((fmt
->wEffects
& mask
) ? "yes" : "no") : "N/A");
271 void ME_DumpParaStyleToBuf(PARAFORMAT2
*pFmt
, char buf
[2048])
273 /* FIXME only PARAFORMAT styles implemented */
276 p
+= sprintf(p
, "Alignment: %s\n",
277 !(pFmt
->dwMask
& PFM_ALIGNMENT
) ? "N/A" :
278 ((pFmt
->wAlignment
== PFA_LEFT
) ? "left" :
279 ((pFmt
->wAlignment
== PFA_RIGHT
) ? "right" :
280 ((pFmt
->wAlignment
== PFA_CENTER
) ? "center" :
281 /*((pFmt->wAlignment == PFA_JUSTIFY) ? "justify" : "incorrect")*/
284 if (pFmt
->dwMask
& PFM_OFFSET
)
285 p
+= sprintf(p
, "Offset: %d\n", (int)pFmt
->dxOffset
);
287 p
+= sprintf(p
, "Offset: N/A\n");
289 if (pFmt
->dwMask
& PFM_OFFSETINDENT
)
290 p
+= sprintf(p
, "Offset indent: %d\n", (int)pFmt
->dxStartIndent
);
292 p
+= sprintf(p
, "Offset indent: N/A\n");
294 if (pFmt
->dwMask
& PFM_STARTINDENT
)
295 p
+= sprintf(p
, "Start indent: %d\n", (int)pFmt
->dxStartIndent
);
297 p
+= sprintf(p
, "Start indent: N/A\n");
299 if (pFmt
->dwMask
& PFM_RIGHTINDENT
)
300 p
+= sprintf(p
, "Right indent: %d\n", (int)pFmt
->dxRightIndent
);
302 p
+= sprintf(p
, "Right indent: N/A\n");
304 ME_DumpStyleEffect(&p
, "Page break before:", pFmt
, PFM_PAGEBREAKBEFORE
);
307 void ME_SetParaFormat(ME_TextEditor
*editor
, ME_DisplayItem
*para
, PARAFORMAT2
*pFmt
)
310 assert(sizeof(*para
->member
.para
.pFmt
) == sizeof(PARAFORMAT2
));
311 ME_AddUndoItem(editor
, diUndoSetParagraphFormat
, para
);
313 CopyMemory(©
, para
->member
.para
.pFmt
, sizeof(PARAFORMAT2
));
315 if (pFmt
->dwMask
& PFM_ALIGNMENT
)
316 para
->member
.para
.pFmt
->wAlignment
= pFmt
->wAlignment
;
317 if (pFmt
->dwMask
& PFM_STARTINDENT
)
318 para
->member
.para
.pFmt
->dxStartIndent
= pFmt
->dxStartIndent
;
319 if (pFmt
->dwMask
& PFM_OFFSET
)
320 para
->member
.para
.pFmt
->dxOffset
= pFmt
->dxOffset
;
321 if (pFmt
->dwMask
& PFM_OFFSETINDENT
)
322 para
->member
.para
.pFmt
->dxStartIndent
+= pFmt
->dxStartIndent
;
324 if (pFmt
->dwMask
& PFM_TABSTOPS
)
326 para
->member
.para
.pFmt
->cTabCount
= pFmt
->cTabCount
;
327 memcpy(para
->member
.para
.pFmt
->rgxTabs
, pFmt
->rgxTabs
, pFmt
->cTabCount
*sizeof(int));
330 /* FIXME to be continued (indents, bulleting and such) */
332 if (memcmp(©
, para
->member
.para
.pFmt
, sizeof(PARAFORMAT2
)))
333 para
->member
.para
.nFlags
|= MEPF_REWRAP
;
338 ME_GetSelectionParas(ME_TextEditor
*editor
, ME_DisplayItem
**para
, ME_DisplayItem
**para_end
)
340 ME_Cursor
*pEndCursor
= &editor
->pCursors
[1];
342 *para
= ME_GetParagraph(editor
->pCursors
[0].pRun
);
343 *para_end
= ME_GetParagraph(editor
->pCursors
[1].pRun
);
344 if ((*para_end
)->member
.para
.nCharOfs
< (*para
)->member
.para
.nCharOfs
) {
345 ME_DisplayItem
*tmp
= *para
;
349 pEndCursor
= &editor
->pCursors
[0];
352 /* selection consists of chars from nFrom up to nTo-1 */
353 if ((*para_end
)->member
.para
.nCharOfs
> (*para
)->member
.para
.nCharOfs
) {
354 if (!pEndCursor
->nOffset
) {
355 *para_end
= ME_GetParagraph(ME_FindItemBack(pEndCursor
->pRun
, diRun
));
361 void ME_SetSelectionParaFormat(ME_TextEditor
*editor
, PARAFORMAT2
*pFmt
)
363 ME_DisplayItem
*para
, *para_end
;
365 ME_GetSelectionParas(editor
, ¶
, ¶_end
);
368 ME_SetParaFormat(editor
, para
, pFmt
);
369 if (para
== para_end
)
371 para
= para
->member
.para
.next_para
;
375 void ME_GetParaFormat(ME_TextEditor
*editor
, ME_DisplayItem
*para
, PARAFORMAT2
*pFmt
)
377 if (pFmt
->cbSize
>= sizeof(PARAFORMAT2
))
379 CopyMemory(pFmt
, para
->member
.para
.pFmt
, sizeof(PARAFORMAT2
));
382 CopyMemory(pFmt
, para
->member
.para
.pFmt
, pFmt
->cbSize
);
385 void ME_GetSelectionParaFormat(ME_TextEditor
*editor
, PARAFORMAT2
*pFmt
)
387 ME_DisplayItem
*para
, *para_end
;
390 ME_GetSelectionParas(editor
, ¶
, ¶_end
);
392 ME_GetParaFormat(editor
, para
, pFmt
);
393 if (para
== para_end
) return;
396 ZeroMemory(&tmp
, sizeof(tmp
));
397 tmp
.cbSize
= sizeof(tmp
);
398 ME_GetParaFormat(editor
, para
, &tmp
);
400 assert(tmp
.dwMask
& PFM_ALIGNMENT
);
401 if (pFmt
->wAlignment
!= tmp
.wAlignment
)
402 pFmt
->dwMask
&= ~PFM_ALIGNMENT
;
404 assert(tmp
.dwMask
& PFM_STARTINDENT
);
405 if (pFmt
->dxStartIndent
!= tmp
.dxStartIndent
)
406 pFmt
->dwMask
&= ~PFM_STARTINDENT
;
408 assert(tmp
.dwMask
& PFM_OFFSET
);
409 if (pFmt
->dxOffset
!= tmp
.dxOffset
)
410 pFmt
->dwMask
&= ~PFM_OFFSET
;
412 assert(tmp
.dwMask
& PFM_TABSTOPS
);
413 if (pFmt
->dwMask
& PFM_TABSTOPS
) {
414 if (pFmt
->cTabCount
!= tmp
.cTabCount
)
415 pFmt
->dwMask
&= ~PFM_TABSTOPS
;
417 if (memcmp(pFmt
->rgxTabs
, tmp
.rgxTabs
, tmp
.cTabCount
*sizeof(int)))
418 pFmt
->dwMask
&= ~PFM_TABSTOPS
;
421 if (para
== para_end
)
423 para
= para
->member
.para
.next_para
;