2 * RichEdit functions dealing with on tables
4 * Copyright 2008 by Dylan Smith
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * The implementation of tables differs greatly between version 3.0
23 * (in riched20.dll) and version 4.1 (in msftedit.dll) of richedit controls.
24 * Currently Wine is not distinguishing between version 3.0 and version 4.1,
25 * so v4.1 is assumed unless v1.0 is being emulated (i.e. riched32.dll is used).
26 * If this lack of distinction causes a bug in a Windows application, then Wine
27 * will need to start making this distinction.
29 * Richedit version 1.0 - 3.0:
30 * Tables are implemented in these versions using tabs at the end of cells,
31 * and tab stops to position the cells. The paragraph format flag PFE_TABLE
32 * will indicate that the paragraph is a table row. Note that in this
33 * implementation there is one paragraph per table row.
35 * Richedit version 4.1:
36 * Tables are implemented such that cells can contain multiple paragraphs,
37 * each with it's own paragraph format, and cells may even contain tables
38 * nested within the cell.
40 * There is also a paragraph at the start of each table row that contains
41 * the rows paragraph format (e.g. to change the row alignment to row), and a
42 * paragraph at the end of the table row with the PFE_TABLEROWDELIMITER flag
43 * set. The paragraphs at the start and end of the table row should always be
44 * empty, but should have a length of 2.
46 * Wine implements this using display items (ME_DisplayItem) with a type of
47 * diCell. These cell display items store the cell properties, and are
48 * inserted into the editors linked list before each cell, and at the end of
49 * the last cell. The cell display item for a cell comes before the paragraphs
50 * for the cell, but the last cell display item refers to no cell, so it is
57 WINE_DEFAULT_DEBUG_CHANNEL(richedit_lists);
59 static ME_DisplayItem* ME_InsertEndParaFromCursor(ME_TextEditor *editor,
64 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
66 ME_Cursor* cursor = &editor->pCursors[nCursor];
67 if (cursor->nOffset) {
68 ME_SplitRunSimple(editor, cursor->pRun, cursor->nOffset);
69 cursor = &editor->pCursors[nCursor];
72 tp = ME_SplitParagraph(editor, cursor->pRun, pStyle, eol_str, paraFlags);
73 cursor->pRun = ME_FindItemFwd(tp, diRun);
77 ME_DisplayItem* ME_InsertTableRowStartFromCursor(ME_TextEditor *editor)
80 WCHAR cr_lf[] = {'\r', '\n', 0};
81 ME_String *eol_str = ME_MakeStringN(cr_lf, 2);
82 para = ME_InsertEndParaFromCursor(editor, 0, eol_str, MEPF_ROWSTART);
83 return para->member.para.prev_para;
86 ME_DisplayItem* ME_InsertTableRowStartAtParagraph(ME_TextEditor *editor,
89 ME_DisplayItem *prev_para, *end_para;
90 ME_Cursor savedCursor = editor->pCursors[0];
91 ME_DisplayItem *startRowPara;
92 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
93 editor->pCursors[0].nOffset = 0;
94 editor->pCursors[1] = editor->pCursors[0];
95 startRowPara = ME_InsertTableRowStartFromCursor(editor);
96 editor->pCursors[0] = savedCursor;
97 editor->pCursors[1] = editor->pCursors[0];
99 end_para = ME_GetParagraph(editor->pCursors[0].pRun)->member.para.next_para;
100 prev_para = startRowPara->member.para.next_para;
101 para = prev_para->member.para.next_para;
102 while (para != end_para)
104 para->member.para.pCell = prev_para->member.para.pCell;
105 para->member.para.nFlags |= MEPF_CELL;
106 para->member.para.nFlags &= ~(MEPF_ROWSTART|MEPF_ROWEND);
107 para->member.para.pFmt->dwMask |= PFM_TABLE|PFM_TABLEROWDELIMITER;
108 para->member.para.pFmt->wEffects |= PFE_TABLE;
109 para->member.para.pFmt->wEffects &= ~PFE_TABLEROWDELIMITER;
111 para = para->member.para.next_para;
116 /* Inserts a diCell and starts a new paragraph for the next cell.
118 * Returns the first paragraph of the new cell. */
119 ME_DisplayItem* ME_InsertTableCellFromCursor(ME_TextEditor *editor)
121 ME_DisplayItem *para;
123 ME_String *eol_str = ME_MakeStringN(&cr, 1);
124 para = ME_InsertEndParaFromCursor(editor, 0, eol_str, MEPF_CELL);
128 ME_DisplayItem* ME_InsertTableRowEndFromCursor(ME_TextEditor *editor)
130 ME_DisplayItem *para;
131 WCHAR cr_lf[] = {'\r', '\n', 0};
132 ME_String *eol_str = ME_MakeStringN(cr_lf, 2);
133 para = ME_InsertEndParaFromCursor(editor, 0, eol_str, MEPF_ROWEND);
134 return para->member.para.prev_para;
137 ME_DisplayItem* ME_GetTableRowEnd(ME_DisplayItem *para)
139 ME_DisplayItem *cell;
141 if (para->member.para.nFlags & MEPF_ROWEND)
143 if (para->member.para.nFlags & MEPF_ROWSTART)
144 para = para->member.para.next_para;
145 cell = para->member.para.pCell;
146 assert(cell && cell->type == diCell);
147 while (cell->member.cell.next_cell)
148 cell = cell->member.cell.next_cell;
150 para = ME_FindItemFwd(cell, diParagraph);
151 assert(para && para->member.para.nFlags & MEPF_ROWEND);
155 ME_DisplayItem* ME_GetTableRowStart(ME_DisplayItem *para)
157 ME_DisplayItem *cell;
159 if (para->member.para.nFlags & MEPF_ROWSTART)
161 if (para->member.para.nFlags & MEPF_ROWEND)
162 para = para->member.para.prev_para;
163 cell = para->member.para.pCell;
164 assert(cell && cell->type == diCell);
165 while (cell->member.cell.prev_cell)
166 cell = cell->member.cell.prev_cell;
168 para = ME_FindItemBack(cell, diParagraph);
169 assert(para && para->member.para.nFlags & MEPF_ROWSTART);
173 /* Make a bunch of assertions to make sure tables haven't been corrupted.
175 * These invariants may not hold true in the middle of streaming in rich text
176 * or during an undo and redo of streaming in rich text. It should be safe to
177 * call this method after an event is processed.
179 void ME_CheckTablesForCorruption(ME_TextEditor *editor)
181 if(TRACE_ON(richedit_lists))
184 ME_DumpDocument(editor->pBuffer);
188 ME_DisplayItem *p, *pPrev;
189 pPrev = editor->pBuffer->pFirst;
191 if (!editor->bEmulateVersion10) /* v4.1 */
193 while (p->type == diParagraph)
195 assert(p->member.para.pFmt->dwMask & PFM_TABLE);
196 assert(p->member.para.pFmt->dwMask & PFM_TABLEROWDELIMITER);
197 if (p->member.para.pCell)
199 assert(p->member.para.nFlags & MEPF_CELL);
200 assert(p->member.para.pFmt->wEffects & PFE_TABLE);
202 if (p->member.para.pCell != pPrev->member.para.pCell)
204 /* There must be a diCell in between the paragraphs if pCell changes. */
205 ME_DisplayItem *pCell = ME_FindItemBack(p, diCell);
207 assert(ME_FindItemBack(p, diRun) == ME_FindItemBack(pCell, diRun));
209 if (p->member.para.nFlags & MEPF_ROWEND)
211 /* ROWEND must come after a cell. */
212 assert(pPrev->member.para.pCell);
213 assert(p->member.para.pCell
214 == pPrev->member.para.pCell->member.cell.parent_cell);
215 assert(p->member.para.pFmt->wEffects & PFE_TABLEROWDELIMITER);
217 else if (p->member.para.pCell)
219 assert(!(p->member.para.pFmt->wEffects & PFE_TABLEROWDELIMITER));
220 assert(pPrev->member.para.pCell ||
221 pPrev->member.para.nFlags & MEPF_ROWSTART);
222 if (pPrev->member.para.pCell &&
223 !(pPrev->member.para.nFlags & MEPF_ROWSTART))
225 assert(p->member.para.pCell->member.cell.parent_cell
226 == pPrev->member.para.pCell->member.cell.parent_cell);
227 if (pPrev->member.para.pCell != p->member.para.pCell)
228 assert(pPrev->member.para.pCell
229 == p->member.para.pCell->member.cell.prev_cell);
232 else if (!(p->member.para.nFlags & MEPF_ROWSTART))
234 assert(!(p->member.para.pFmt->wEffects & PFE_TABLEROWDELIMITER));
235 /* ROWSTART must be followed by a cell. */
236 assert(!(p->member.para.nFlags & MEPF_CELL));
237 /* ROWSTART must be followed by a cell. */
238 assert(!(pPrev->member.para.nFlags & MEPF_ROWSTART));
241 p = p->member.para.next_para;
243 } else { /* v1.0 - 3.0 */
244 while (p->type == diParagraph)
246 assert(!(p->member.para.nFlags & (MEPF_ROWSTART|MEPF_ROWEND|MEPF_CELL)));
247 assert(p->member.para.pFmt->dwMask & PFM_TABLE);
248 assert(!(p->member.para.pFmt->wEffects & PFM_TABLEROWDELIMITER));
249 assert(!p->member.para.pCell);
250 p = p->member.para.next_para;
254 assert(p->type == diTextEnd);
255 assert(!pPrev->member.para.pCell);
260 BOOL ME_IsInTable(ME_DisplayItem *pItem)
265 if (pItem->type == diRun)
266 pItem = ME_GetParagraph(pItem);
267 if (pItem->type != diParagraph)
269 pFmt = pItem->member.para.pFmt;
270 return pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE;
273 /* Table rows should either be deleted completely or not at all. */
274 void ME_ProtectPartialTableDeletion(ME_TextEditor *editor, int nOfs,int *nChars)
277 ME_DisplayItem *this_para, *end_para;
278 ME_CursorFromCharOfs(editor, nOfs, &c);
279 this_para = ME_GetParagraph(c.pRun);
280 ME_CursorFromCharOfs(editor, nOfs + *nChars, &c2);
281 end_para = ME_GetParagraph(c2.pRun);
282 if (c2.pRun->member.run.nFlags & MERF_ENDPARA) {
283 /* End offset might be in the middle of the end paragraph run.
284 * If this is the case, then we need to use the next paragraph as the last
287 int remaining = nOfs + *nChars - c2.pRun->member.run.nCharOfs
288 - end_para->member.para.nCharOfs;
291 assert(remaining < c2.pRun->member.run.strText->nLen);
292 end_para = end_para->member.para.next_para;
295 if (!editor->bEmulateVersion10) { /* v4.1 */
296 if (this_para->member.para.pCell != end_para->member.para.pCell ||
297 ((this_para->member.para.nFlags|end_para->member.para.nFlags)
298 & (MEPF_ROWSTART|MEPF_ROWEND)))
300 while (this_para != end_para)
302 ME_DisplayItem *next_para = this_para->member.para.next_para;
303 BOOL bTruancateDeletion = FALSE;
304 if (this_para->member.para.nFlags & MEPF_ROWSTART) {
305 /* The following while loop assumes that next_para is MEPF_ROWSTART,
306 * so moving back one paragraph let's it be processed as the start
308 next_para = this_para;
309 this_para = this_para->member.para.prev_para;
310 } else if (next_para->member.para.pCell != this_para->member.para.pCell
311 || this_para->member.para.nFlags & MEPF_ROWEND)
313 /* Start of the deletion from after the start of the table row. */
314 bTruancateDeletion = TRUE;
316 while (!bTruancateDeletion &&
317 next_para->member.para.nFlags & MEPF_ROWSTART)
319 next_para = ME_GetTableRowEnd(next_para)->member.para.next_para;
320 if (next_para->member.para.nCharOfs > nOfs + *nChars)
322 /* End of deletion is not past the end of the table row. */
323 next_para = this_para->member.para.next_para;
324 /* Delete the end paragraph preceding the table row if the
325 * preceding table row will be empty. */
326 if (this_para->member.para.nCharOfs >= nOfs)
328 next_para = next_para->member.para.next_para;
330 bTruancateDeletion = TRUE;
332 this_para = next_para->member.para.prev_para;
335 if (bTruancateDeletion)
337 ME_Run *end_run = &ME_FindItemBack(next_para, diRun)->member.run;
338 int nCharsNew = (next_para->member.para.nCharOfs - nOfs
339 - end_run->strText->nLen);
340 nCharsNew = max(nCharsNew, 0);
341 assert(nCharsNew <= *nChars);
345 this_para = next_para;
348 } else { /* v1.0 - 3.0 */
349 ME_DisplayItem *pRun;
350 int nCharsToBoundary;
352 if ((this_para->member.para.nCharOfs != nOfs || this_para == end_para) &&
353 this_para->member.para.pFmt->dwMask & PFM_TABLE &&
354 this_para->member.para.pFmt->wEffects & PFE_TABLE)
357 /* Find the next tab or end paragraph to use as a delete boundary */
358 while (!(pRun->member.run.nFlags & (MERF_TAB|MERF_ENDPARA)))
359 pRun = ME_FindItemFwd(pRun, diRun);
360 nCharsToBoundary = pRun->member.run.nCharOfs
361 - c.pRun->member.run.nCharOfs
363 *nChars = min(*nChars, nCharsToBoundary);
364 } else if (end_para->member.para.pFmt->dwMask & PFM_TABLE &&
365 end_para->member.para.pFmt->wEffects & PFE_TABLE)
367 /* The deletion starts from before the row, so don't join it with
368 * previous non-empty paragraphs. */
370 if (nOfs > this_para->member.para.nCharOfs)
371 pRun = ME_FindItemBack(end_para, diRun);
373 pRun = ME_FindItemFwd(end_para, diRun);
376 nCharsToBoundary = ME_GetParagraph(pRun)->member.para.nCharOfs
377 + pRun->member.run.nCharOfs
379 if (nCharsToBoundary >= 0)
380 *nChars = min(*nChars, nCharsToBoundary);
388 ME_DisplayItem* ME_AppendTableRow(ME_TextEditor *editor,
389 ME_DisplayItem *table_row)
391 WCHAR endl = '\r', tab = '\t';
397 assert(table_row->type == diParagraph);
398 if (!editor->bEmulateVersion10) { /* v4.1 */
399 ME_DisplayItem *insertedCell, *para, *cell, *prevTableEnd;
400 cell = ME_FindItemFwd(ME_GetTableRowStart(table_row), diCell);
401 prevTableEnd = ME_GetTableRowEnd(table_row);
402 run = prevTableEnd->member.para.next_para;
403 run = ME_FindItemFwd(run, diRun);
404 editor->pCursors[0].pRun = run;
405 editor->pCursors[0].nOffset = 0;
406 editor->pCursors[1] = editor->pCursors[0];
407 para = ME_InsertTableRowStartFromCursor(editor);
408 insertedCell = ME_FindItemFwd(para, diCell);
409 /* Copy cell properties */
410 insertedCell->member.cell.nRightBoundary = cell->member.cell.nRightBoundary;
411 insertedCell->member.cell.border = cell->member.cell.border;
412 while (cell->member.cell.next_cell) {
413 cell = cell->member.cell.next_cell;
414 para = ME_InsertTableCellFromCursor(editor);
415 insertedCell = ME_FindItemBack(para, diCell);
416 /* Copy cell properties */
417 insertedCell->member.cell.nRightBoundary = cell->member.cell.nRightBoundary;
418 insertedCell->member.cell.border = cell->member.cell.border;
420 para = ME_InsertTableRowEndFromCursor(editor);
421 *para->member.para.pFmt = *prevTableEnd->member.para.pFmt;
422 /* return the table row start for the inserted paragraph */
423 return ME_FindItemFwd(cell, diParagraph)->member.para.next_para;
424 } else { /* v1.0 - 3.0 */
425 run = ME_FindItemBack(table_row->member.para.next_para, diRun);
426 pFmt = table_row->member.para.pFmt;
427 assert(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE);
428 editor->pCursors[0].pRun = run;
429 editor->pCursors[0].nOffset = 0;
430 editor->pCursors[1] = editor->pCursors[0];
431 ME_InsertTextFromCursor(editor, 0, &endl, 1, run->member.run.style);
432 run = editor->pCursors[0].pRun;
433 for (i = 0; i < pFmt->cTabCount; i++) {
434 ME_InsertTextFromCursor(editor, 0, &tab, 1, run->member.run.style);
436 return table_row->member.para.next_para;
440 /* Selects the next table cell or appends a new table row if at end of table */
441 static void ME_SelectOrInsertNextCell(ME_TextEditor *editor,
444 ME_DisplayItem *para = ME_GetParagraph(run);
447 assert(run && run->type == diRun);
448 assert(ME_IsInTable(run));
449 if (!editor->bEmulateVersion10) { /* v4.1 */
450 ME_DisplayItem *cell;
451 /* Get the initial cell */
452 if (para->member.para.nFlags & MEPF_ROWSTART) {
453 cell = para->member.para.next_para->member.para.pCell;
454 } else if (para->member.para.nFlags & MEPF_ROWEND) {
455 cell = para->member.para.prev_para->member.para.pCell;
457 cell = para->member.para.pCell;
460 /* Get the next cell. */
461 if (cell->member.cell.next_cell &&
462 cell->member.cell.next_cell->member.cell.next_cell)
464 cell = cell->member.cell.next_cell;
466 para = ME_GetTableRowEnd(ME_FindItemFwd(cell, diParagraph));
467 para = para->member.para.next_para;
469 if (para->member.para.nFlags & MEPF_ROWSTART) {
470 cell = para->member.para.next_para->member.para.pCell;
473 para = para->member.para.prev_para;
474 para = ME_AppendTableRow(editor, ME_GetTableRowStart(para));
475 /* Put cursor at the start of the new table row */
476 para = para->member.para.next_para;
477 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
478 editor->pCursors[0].nOffset = 0;
479 editor->pCursors[1] = editor->pCursors[0];
480 ME_WrapMarkedParagraphs(editor);
485 editor->pCursors[1].pRun = ME_FindItemFwd(cell, diRun);
486 editor->pCursors[1].nOffset = 0;
487 assert(editor->pCursors[0].pRun);
488 cell = cell->member.cell.next_cell;
489 editor->pCursors[0].pRun = ME_FindItemBack(cell, diRun);
490 editor->pCursors[0].nOffset = 0;
491 assert(editor->pCursors[1].pRun);
492 } else { /* v1.0 - 3.0 */
493 if (run->member.run.nFlags & MERF_ENDPARA &&
494 ME_IsInTable(ME_FindItemFwd(run, diParagraphOrEnd)))
496 run = ME_FindItemFwd(run, diRun);
499 for (i = 0; i < 2; i++)
501 while (!(run->member.run.nFlags & MERF_TAB))
503 run = ME_FindItemFwd(run, diRunOrParagraphOrEnd);
504 if (run->type != diRun)
507 if (ME_IsInTable(para))
509 run = ME_FindItemFwd(para, diRun);
511 editor->pCursors[0].pRun = run;
512 editor->pCursors[0].nOffset = 0;
515 /* Insert table row */
516 para = ME_AppendTableRow(editor, para->member.para.prev_para);
517 /* Put cursor at the start of the new table row */
518 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
519 editor->pCursors[0].nOffset = 0;
520 editor->pCursors[1] = editor->pCursors[0];
521 ME_WrapMarkedParagraphs(editor);
527 run = ME_FindItemFwd(run, diRun);
528 editor->pCursors[i].pRun = run;
529 editor->pCursors[i].nOffset = 0;
535 void ME_TabPressedInTable(ME_TextEditor *editor, BOOL bSelectedRow)
537 /* FIXME: Shift tab should move to the previous cell. */
538 ME_Cursor fromCursor, toCursor;
539 ME_InvalidateSelection(editor);
542 from = ME_GetCursorOfs(editor, 0);
543 to = ME_GetCursorOfs(editor, 1);
546 fromCursor = editor->pCursors[0];
547 toCursor = editor->pCursors[1];
549 fromCursor = editor->pCursors[1];
550 toCursor = editor->pCursors[0];
553 if (!editor->bEmulateVersion10) /* v4.1 */
555 if (!ME_IsInTable(toCursor.pRun))
557 editor->pCursors[0] = toCursor;
558 editor->pCursors[1] = toCursor;
560 ME_SelectOrInsertNextCell(editor, toCursor.pRun);
562 } else { /* v1.0 - 3.0 */
563 if (!ME_IsInTable(fromCursor.pRun)) {
564 editor->pCursors[0] = fromCursor;
565 editor->pCursors[1] = fromCursor;
566 /* FIXME: For some reason the caret is shown at the start of the
567 * previous paragraph in v1.0 to v3.0, and bCaretAtEnd only works
568 * within the paragraph for wrapped lines. */
569 if (ME_FindItemBack(fromCursor.pRun, diRun))
570 editor->bCaretAtEnd = TRUE;
571 } else if ((bSelectedRow || !ME_IsInTable(toCursor.pRun))) {
572 ME_SelectOrInsertNextCell(editor, fromCursor.pRun);
574 if (ME_IsSelection(editor) && !toCursor.nOffset)
577 run = ME_FindItemBack(toCursor.pRun, diRunOrParagraphOrEnd);
578 if (run->type == diRun && run->member.run.nFlags & MERF_TAB)
579 ME_SelectOrInsertNextCell(editor, run);
581 ME_SelectOrInsertNextCell(editor, toCursor.pRun);
583 ME_SelectOrInsertNextCell(editor, toCursor.pRun);
587 ME_InvalidateSelection(editor);
589 ITextHost_TxShowCaret(editor->texthost, FALSE);
590 ME_ShowCaret(editor);
591 ME_SendSelChange(editor);
594 /* Make sure the cursor is not in the hidden table row start paragraph
595 * without a selection. */
596 void ME_MoveCursorFromTableRowStartParagraph(ME_TextEditor *editor)
598 ME_DisplayItem *para = ME_GetParagraph(editor->pCursors[0].pRun);
599 if (para == ME_GetParagraph(editor->pCursors[1].pRun) &&
600 para->member.para.nFlags & MEPF_ROWSTART) {
601 /* The cursors should not be at the hidden start row paragraph without
602 * a selection, so the cursor is moved into the first cell. */
603 para = para->member.para.next_para;
604 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
605 editor->pCursors[0].nOffset = 0;
606 editor->pCursors[1] = editor->pCursors[0];
610 struct RTFTable *ME_MakeTableDef(ME_TextEditor *editor)
612 RTFTable *tableDef = ALLOC_OBJ(RTFTable);
613 ZeroMemory(tableDef, sizeof(RTFTable));
614 if (!editor->bEmulateVersion10) /* v4.1 */
619 void ME_InitTableDef(ME_TextEditor *editor, struct RTFTable *tableDef)
621 ZeroMemory(tableDef->cells, sizeof(tableDef->cells));
622 ZeroMemory(tableDef->border, sizeof(tableDef->border));
623 tableDef->numCellsDefined = 0;
624 tableDef->leftEdge = 0;
625 if (!editor->bEmulateVersion10) /* v4.1 */
627 else /* v1.0 - 3.0 */