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);
74 cursor->pRun = ME_FindItemFwd(tp, diRun);
78 ME_DisplayItem* ME_InsertTableRowStartFromCursor(ME_TextEditor *editor)
81 WCHAR cr_lf[] = {'\r', '\n', 0};
82 ME_String *eol_str = ME_MakeStringN(cr_lf, 2);
83 para = ME_InsertEndParaFromCursor(editor, 0, eol_str, MEPF_ROWSTART);
84 return para->member.para.prev_para;
87 ME_DisplayItem* ME_InsertTableRowStartAtParagraph(ME_TextEditor *editor,
90 ME_DisplayItem *prev_para, *end_para;
91 ME_Cursor savedCursor = editor->pCursors[0];
92 ME_DisplayItem *startRowPara;
93 editor->pCursors[0].pPara = para;
94 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
95 editor->pCursors[0].nOffset = 0;
96 editor->pCursors[1] = editor->pCursors[0];
97 startRowPara = ME_InsertTableRowStartFromCursor(editor);
98 editor->pCursors[0] = savedCursor;
99 editor->pCursors[1] = editor->pCursors[0];
101 end_para = editor->pCursors[0].pPara->member.para.next_para;
102 prev_para = startRowPara->member.para.next_para;
103 para = prev_para->member.para.next_para;
104 while (para != end_para)
106 para->member.para.pCell = prev_para->member.para.pCell;
107 para->member.para.nFlags |= MEPF_CELL;
108 para->member.para.nFlags &= ~(MEPF_ROWSTART|MEPF_ROWEND);
109 para->member.para.pFmt->dwMask |= PFM_TABLE|PFM_TABLEROWDELIMITER;
110 para->member.para.pFmt->wEffects |= PFE_TABLE;
111 para->member.para.pFmt->wEffects &= ~PFE_TABLEROWDELIMITER;
113 para = para->member.para.next_para;
118 /* Inserts a diCell and starts a new paragraph for the next cell.
120 * Returns the first paragraph of the new cell. */
121 ME_DisplayItem* ME_InsertTableCellFromCursor(ME_TextEditor *editor)
123 ME_DisplayItem *para;
125 ME_String *eol_str = ME_MakeStringN(&cr, 1);
126 para = ME_InsertEndParaFromCursor(editor, 0, eol_str, MEPF_CELL);
130 ME_DisplayItem* ME_InsertTableRowEndFromCursor(ME_TextEditor *editor)
132 ME_DisplayItem *para;
133 WCHAR cr_lf[] = {'\r', '\n', 0};
134 ME_String *eol_str = ME_MakeStringN(cr_lf, 2);
135 para = ME_InsertEndParaFromCursor(editor, 0, eol_str, MEPF_ROWEND);
136 return para->member.para.prev_para;
139 ME_DisplayItem* ME_GetTableRowEnd(ME_DisplayItem *para)
141 ME_DisplayItem *cell;
143 if (para->member.para.nFlags & MEPF_ROWEND)
145 if (para->member.para.nFlags & MEPF_ROWSTART)
146 para = para->member.para.next_para;
147 cell = para->member.para.pCell;
148 assert(cell && cell->type == diCell);
149 while (cell->member.cell.next_cell)
150 cell = cell->member.cell.next_cell;
152 para = ME_FindItemFwd(cell, diParagraph);
153 assert(para && para->member.para.nFlags & MEPF_ROWEND);
157 ME_DisplayItem* ME_GetTableRowStart(ME_DisplayItem *para)
159 ME_DisplayItem *cell;
161 if (para->member.para.nFlags & MEPF_ROWSTART)
163 if (para->member.para.nFlags & MEPF_ROWEND)
164 para = para->member.para.prev_para;
165 cell = para->member.para.pCell;
166 assert(cell && cell->type == diCell);
167 while (cell->member.cell.prev_cell)
168 cell = cell->member.cell.prev_cell;
170 para = ME_FindItemBack(cell, diParagraph);
171 assert(para && para->member.para.nFlags & MEPF_ROWSTART);
175 /* Make a bunch of assertions to make sure tables haven't been corrupted.
177 * These invariants may not hold true in the middle of streaming in rich text
178 * or during an undo and redo of streaming in rich text. It should be safe to
179 * call this method after an event is processed.
181 void ME_CheckTablesForCorruption(ME_TextEditor *editor)
183 if(TRACE_ON(richedit_lists))
186 ME_DumpDocument(editor->pBuffer);
190 ME_DisplayItem *p, *pPrev;
191 pPrev = editor->pBuffer->pFirst;
193 if (!editor->bEmulateVersion10) /* v4.1 */
195 while (p->type == diParagraph)
197 assert(p->member.para.pFmt->dwMask & PFM_TABLE);
198 assert(p->member.para.pFmt->dwMask & PFM_TABLEROWDELIMITER);
199 if (p->member.para.pCell)
201 assert(p->member.para.nFlags & MEPF_CELL);
202 assert(p->member.para.pFmt->wEffects & PFE_TABLE);
204 if (p->member.para.pCell != pPrev->member.para.pCell)
206 /* There must be a diCell in between the paragraphs if pCell changes. */
207 ME_DisplayItem *pCell = ME_FindItemBack(p, diCell);
209 assert(ME_FindItemBack(p, diRun) == ME_FindItemBack(pCell, diRun));
211 if (p->member.para.nFlags & MEPF_ROWEND)
213 /* ROWEND must come after a cell. */
214 assert(pPrev->member.para.pCell);
215 assert(p->member.para.pCell
216 == pPrev->member.para.pCell->member.cell.parent_cell);
217 assert(p->member.para.pFmt->wEffects & PFE_TABLEROWDELIMITER);
219 else if (p->member.para.pCell)
221 assert(!(p->member.para.pFmt->wEffects & PFE_TABLEROWDELIMITER));
222 assert(pPrev->member.para.pCell ||
223 pPrev->member.para.nFlags & MEPF_ROWSTART);
224 if (pPrev->member.para.pCell &&
225 !(pPrev->member.para.nFlags & MEPF_ROWSTART))
227 assert(p->member.para.pCell->member.cell.parent_cell
228 == pPrev->member.para.pCell->member.cell.parent_cell);
229 if (pPrev->member.para.pCell != p->member.para.pCell)
230 assert(pPrev->member.para.pCell
231 == p->member.para.pCell->member.cell.prev_cell);
234 else if (!(p->member.para.nFlags & MEPF_ROWSTART))
236 assert(!(p->member.para.pFmt->wEffects & PFE_TABLEROWDELIMITER));
237 /* ROWSTART must be followed by a cell. */
238 assert(!(p->member.para.nFlags & MEPF_CELL));
239 /* ROWSTART must be followed by a cell. */
240 assert(!(pPrev->member.para.nFlags & MEPF_ROWSTART));
243 p = p->member.para.next_para;
245 } else { /* v1.0 - 3.0 */
246 while (p->type == diParagraph)
248 assert(!(p->member.para.nFlags & (MEPF_ROWSTART|MEPF_ROWEND|MEPF_CELL)));
249 assert(p->member.para.pFmt->dwMask & PFM_TABLE);
250 assert(!(p->member.para.pFmt->wEffects & PFM_TABLEROWDELIMITER));
251 assert(!p->member.para.pCell);
252 p = p->member.para.next_para;
256 assert(p->type == diTextEnd);
257 assert(!pPrev->member.para.pCell);
262 BOOL ME_IsInTable(ME_DisplayItem *pItem)
267 if (pItem->type == diRun)
268 pItem = ME_GetParagraph(pItem);
269 if (pItem->type != diParagraph)
271 pFmt = pItem->member.para.pFmt;
272 return pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE;
275 /* Table rows should either be deleted completely or not at all. */
276 void ME_ProtectPartialTableDeletion(ME_TextEditor *editor, int nOfs,int *nChars)
279 ME_DisplayItem *this_para, *end_para;
280 ME_CursorFromCharOfs(editor, nOfs, &c);
282 ME_CursorFromCharOfs(editor, nOfs + *nChars, &c2);
284 if (c2.pRun->member.run.nFlags & MERF_ENDPARA) {
285 /* End offset might be in the middle of the end paragraph run.
286 * If this is the case, then we need to use the next paragraph as the last
289 int remaining = nOfs + *nChars - c2.pRun->member.run.nCharOfs
290 - end_para->member.para.nCharOfs;
293 assert(remaining < c2.pRun->member.run.strText->nLen);
294 end_para = end_para->member.para.next_para;
297 if (!editor->bEmulateVersion10) { /* v4.1 */
298 if (this_para->member.para.pCell != end_para->member.para.pCell ||
299 ((this_para->member.para.nFlags|end_para->member.para.nFlags)
300 & (MEPF_ROWSTART|MEPF_ROWEND)))
302 while (this_para != end_para)
304 ME_DisplayItem *next_para = this_para->member.para.next_para;
305 BOOL bTruancateDeletion = FALSE;
306 if (this_para->member.para.nFlags & MEPF_ROWSTART) {
307 /* The following while loop assumes that next_para is MEPF_ROWSTART,
308 * so moving back one paragraph let's it be processed as the start
310 next_para = this_para;
311 this_para = this_para->member.para.prev_para;
312 } else if (next_para->member.para.pCell != this_para->member.para.pCell
313 || this_para->member.para.nFlags & MEPF_ROWEND)
315 /* Start of the deletion from after the start of the table row. */
316 bTruancateDeletion = TRUE;
318 while (!bTruancateDeletion &&
319 next_para->member.para.nFlags & MEPF_ROWSTART)
321 next_para = ME_GetTableRowEnd(next_para)->member.para.next_para;
322 if (next_para->member.para.nCharOfs > nOfs + *nChars)
324 /* End of deletion is not past the end of the table row. */
325 next_para = this_para->member.para.next_para;
326 /* Delete the end paragraph preceding the table row if the
327 * preceding table row will be empty. */
328 if (this_para->member.para.nCharOfs >= nOfs)
330 next_para = next_para->member.para.next_para;
332 bTruancateDeletion = TRUE;
334 this_para = next_para->member.para.prev_para;
337 if (bTruancateDeletion)
339 ME_Run *end_run = &ME_FindItemBack(next_para, diRun)->member.run;
340 int nCharsNew = (next_para->member.para.nCharOfs - nOfs
341 - end_run->strText->nLen);
342 nCharsNew = max(nCharsNew, 0);
343 assert(nCharsNew <= *nChars);
347 this_para = next_para;
350 } else { /* v1.0 - 3.0 */
351 ME_DisplayItem *pRun;
352 int nCharsToBoundary;
354 if ((this_para->member.para.nCharOfs != nOfs || this_para == end_para) &&
355 this_para->member.para.pFmt->dwMask & PFM_TABLE &&
356 this_para->member.para.pFmt->wEffects & PFE_TABLE)
359 /* Find the next tab or end paragraph to use as a delete boundary */
360 while (!(pRun->member.run.nFlags & (MERF_TAB|MERF_ENDPARA)))
361 pRun = ME_FindItemFwd(pRun, diRun);
362 nCharsToBoundary = pRun->member.run.nCharOfs
363 - c.pRun->member.run.nCharOfs
365 *nChars = min(*nChars, nCharsToBoundary);
366 } else if (end_para->member.para.pFmt->dwMask & PFM_TABLE &&
367 end_para->member.para.pFmt->wEffects & PFE_TABLE)
369 /* The deletion starts from before the row, so don't join it with
370 * previous non-empty paragraphs. */
372 if (nOfs > this_para->member.para.nCharOfs)
373 pRun = ME_FindItemBack(end_para, diRun);
375 pRun = ME_FindItemFwd(end_para, diRun);
378 nCharsToBoundary = ME_GetParagraph(pRun)->member.para.nCharOfs
379 + pRun->member.run.nCharOfs
381 if (nCharsToBoundary >= 0)
382 *nChars = min(*nChars, nCharsToBoundary);
390 ME_DisplayItem* ME_AppendTableRow(ME_TextEditor *editor,
391 ME_DisplayItem *table_row)
393 WCHAR endl = '\r', tab = '\t';
399 assert(table_row->type == diParagraph);
400 if (!editor->bEmulateVersion10) { /* v4.1 */
401 ME_DisplayItem *insertedCell, *para, *cell, *prevTableEnd;
402 cell = ME_FindItemFwd(ME_GetTableRowStart(table_row), diCell);
403 prevTableEnd = ME_GetTableRowEnd(table_row);
404 para = prevTableEnd->member.para.next_para;
405 run = ME_FindItemFwd(para, diRun);
406 editor->pCursors[0].pPara = para;
407 editor->pCursors[0].pRun = run;
408 editor->pCursors[0].nOffset = 0;
409 editor->pCursors[1] = editor->pCursors[0];
410 para = ME_InsertTableRowStartFromCursor(editor);
411 insertedCell = ME_FindItemFwd(para, diCell);
412 /* Copy cell properties */
413 insertedCell->member.cell.nRightBoundary = cell->member.cell.nRightBoundary;
414 insertedCell->member.cell.border = cell->member.cell.border;
415 while (cell->member.cell.next_cell) {
416 cell = cell->member.cell.next_cell;
417 para = ME_InsertTableCellFromCursor(editor);
418 insertedCell = ME_FindItemBack(para, diCell);
419 /* Copy cell properties */
420 insertedCell->member.cell.nRightBoundary = cell->member.cell.nRightBoundary;
421 insertedCell->member.cell.border = cell->member.cell.border;
423 para = ME_InsertTableRowEndFromCursor(editor);
424 *para->member.para.pFmt = *prevTableEnd->member.para.pFmt;
425 /* return the table row start for the inserted paragraph */
426 return ME_FindItemFwd(cell, diParagraph)->member.para.next_para;
427 } else { /* v1.0 - 3.0 */
428 run = ME_FindItemBack(table_row->member.para.next_para, diRun);
429 pFmt = table_row->member.para.pFmt;
430 assert(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE);
431 editor->pCursors[0].pPara = table_row;
432 editor->pCursors[0].pRun = run;
433 editor->pCursors[0].nOffset = 0;
434 editor->pCursors[1] = editor->pCursors[0];
435 ME_InsertTextFromCursor(editor, 0, &endl, 1, run->member.run.style);
436 run = editor->pCursors[0].pRun;
437 for (i = 0; i < pFmt->cTabCount; i++) {
438 ME_InsertTextFromCursor(editor, 0, &tab, 1, run->member.run.style);
440 return table_row->member.para.next_para;
444 /* Selects the next table cell or appends a new table row if at end of table */
445 static void ME_SelectOrInsertNextCell(ME_TextEditor *editor,
448 ME_DisplayItem *para = ME_GetParagraph(run);
451 assert(run && run->type == diRun);
452 assert(ME_IsInTable(run));
453 if (!editor->bEmulateVersion10) { /* v4.1 */
454 ME_DisplayItem *cell;
455 /* Get the initial cell */
456 if (para->member.para.nFlags & MEPF_ROWSTART) {
457 cell = para->member.para.next_para->member.para.pCell;
458 } else if (para->member.para.nFlags & MEPF_ROWEND) {
459 cell = para->member.para.prev_para->member.para.pCell;
461 cell = para->member.para.pCell;
464 /* Get the next cell. */
465 if (cell->member.cell.next_cell &&
466 cell->member.cell.next_cell->member.cell.next_cell)
468 cell = cell->member.cell.next_cell;
470 para = ME_GetTableRowEnd(ME_FindItemFwd(cell, diParagraph));
471 para = para->member.para.next_para;
473 if (para->member.para.nFlags & MEPF_ROWSTART) {
474 cell = para->member.para.next_para->member.para.pCell;
477 para = para->member.para.prev_para;
478 para = ME_AppendTableRow(editor, ME_GetTableRowStart(para));
479 /* Put cursor at the start of the new table row */
480 para = para->member.para.next_para;
481 editor->pCursors[0].pPara = para;
482 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
483 editor->pCursors[0].nOffset = 0;
484 editor->pCursors[1] = editor->pCursors[0];
485 ME_WrapMarkedParagraphs(editor);
490 editor->pCursors[1].pRun = ME_FindItemFwd(cell, diRun);
491 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
492 editor->pCursors[1].nOffset = 0;
493 assert(editor->pCursors[0].pRun);
494 cell = cell->member.cell.next_cell;
495 editor->pCursors[0].pRun = ME_FindItemBack(cell, diRun);
496 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
497 editor->pCursors[0].nOffset = 0;
498 assert(editor->pCursors[1].pRun);
499 } else { /* v1.0 - 3.0 */
500 if (run->member.run.nFlags & MERF_ENDPARA &&
501 ME_IsInTable(ME_FindItemFwd(run, diParagraphOrEnd)))
503 run = ME_FindItemFwd(run, diRun);
506 for (i = 0; i < 2; i++)
508 while (!(run->member.run.nFlags & MERF_TAB))
510 run = ME_FindItemFwd(run, diRunOrParagraphOrEnd);
511 if (run->type != diRun)
514 if (ME_IsInTable(para))
516 run = ME_FindItemFwd(para, diRun);
518 editor->pCursors[0].pPara = para;
519 editor->pCursors[0].pRun = run;
520 editor->pCursors[0].nOffset = 0;
523 /* Insert table row */
524 para = ME_AppendTableRow(editor, para->member.para.prev_para);
525 /* Put cursor at the start of the new table row */
526 editor->pCursors[0].pPara = para;
527 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
528 editor->pCursors[0].nOffset = 0;
529 editor->pCursors[1] = editor->pCursors[0];
530 ME_WrapMarkedParagraphs(editor);
536 run = ME_FindItemFwd(run, diRun);
537 editor->pCursors[i].pRun = run;
538 editor->pCursors[i].pPara = ME_GetParagraph(run);
539 editor->pCursors[i].nOffset = 0;
545 void ME_TabPressedInTable(ME_TextEditor *editor, BOOL bSelectedRow)
547 /* FIXME: Shift tab should move to the previous cell. */
548 ME_Cursor fromCursor, toCursor;
549 ME_InvalidateSelection(editor);
552 from = ME_GetCursorOfs(editor, 0);
553 to = ME_GetCursorOfs(editor, 1);
556 fromCursor = editor->pCursors[0];
557 toCursor = editor->pCursors[1];
559 fromCursor = editor->pCursors[1];
560 toCursor = editor->pCursors[0];
563 if (!editor->bEmulateVersion10) /* v4.1 */
565 if (!ME_IsInTable(toCursor.pRun))
567 editor->pCursors[0] = toCursor;
568 editor->pCursors[1] = toCursor;
570 ME_SelectOrInsertNextCell(editor, toCursor.pRun);
572 } else { /* v1.0 - 3.0 */
573 if (!ME_IsInTable(fromCursor.pRun)) {
574 editor->pCursors[0] = fromCursor;
575 editor->pCursors[1] = fromCursor;
576 /* FIXME: For some reason the caret is shown at the start of the
577 * previous paragraph in v1.0 to v3.0, and bCaretAtEnd only works
578 * within the paragraph for wrapped lines. */
579 if (ME_FindItemBack(fromCursor.pRun, diRun))
580 editor->bCaretAtEnd = TRUE;
581 } else if ((bSelectedRow || !ME_IsInTable(toCursor.pRun))) {
582 ME_SelectOrInsertNextCell(editor, fromCursor.pRun);
584 if (ME_IsSelection(editor) && !toCursor.nOffset)
587 run = ME_FindItemBack(toCursor.pRun, diRunOrParagraphOrEnd);
588 if (run->type == diRun && run->member.run.nFlags & MERF_TAB)
589 ME_SelectOrInsertNextCell(editor, run);
591 ME_SelectOrInsertNextCell(editor, toCursor.pRun);
593 ME_SelectOrInsertNextCell(editor, toCursor.pRun);
597 ME_InvalidateSelection(editor);
599 ITextHost_TxShowCaret(editor->texthost, FALSE);
600 ME_ShowCaret(editor);
601 ME_SendSelChange(editor);
604 /* Make sure the cursor is not in the hidden table row start paragraph
605 * without a selection. */
606 void ME_MoveCursorFromTableRowStartParagraph(ME_TextEditor *editor)
608 ME_DisplayItem *para = editor->pCursors[0].pPara;
609 if (para == editor->pCursors[1].pPara &&
610 para->member.para.nFlags & MEPF_ROWSTART) {
611 /* The cursors should not be at the hidden start row paragraph without
612 * a selection, so the cursor is moved into the first cell. */
613 para = para->member.para.next_para;
614 editor->pCursors[0].pPara = para;
615 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
616 editor->pCursors[0].nOffset = 0;
617 editor->pCursors[1] = editor->pCursors[0];
621 struct RTFTable *ME_MakeTableDef(ME_TextEditor *editor)
623 RTFTable *tableDef = ALLOC_OBJ(RTFTable);
624 ZeroMemory(tableDef, sizeof(RTFTable));
625 if (!editor->bEmulateVersion10) /* v4.1 */
630 void ME_InitTableDef(ME_TextEditor *editor, struct RTFTable *tableDef)
632 ZeroMemory(tableDef->cells, sizeof(tableDef->cells));
633 ZeroMemory(tableDef->border, sizeof(tableDef->border));
634 tableDef->numCellsDefined = 0;
635 tableDef->leftEdge = 0;
636 if (!editor->bEmulateVersion10) /* v4.1 */
638 else /* v1.0 - 3.0 */