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];
68 ME_SplitRunSimple(editor, cursor);
70 tp = ME_SplitParagraph(editor, cursor->pRun, pStyle, eol_str, paraFlags);
71 ME_ReleaseStyle(pStyle);
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].pPara = para;
93 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
94 editor->pCursors[0].nOffset = 0;
95 editor->pCursors[1] = editor->pCursors[0];
96 startRowPara = ME_InsertTableRowStartFromCursor(editor);
97 savedCursor.pPara = ME_GetParagraph(savedCursor.pRun);
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(&tab, 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, ME_Cursor *c, int *nChars)
278 int nOfs = ME_GetCursorOfs(c);
280 ME_DisplayItem *this_para = c->pPara;
281 ME_DisplayItem *end_para;
283 ME_MoveCursorChars(editor, &c2, *nChars);
285 if (c2.pRun->member.run.nFlags & MERF_ENDPARA) {
286 /* End offset might be in the middle of the end paragraph run.
287 * If this is the case, then we need to use the next paragraph as the last
290 int remaining = nOfs + *nChars - c2.pRun->member.run.nCharOfs
291 - end_para->member.para.nCharOfs;
294 assert(remaining < c2.pRun->member.run.strText->nLen);
295 end_para = end_para->member.para.next_para;
298 if (!editor->bEmulateVersion10) { /* v4.1 */
299 if (this_para->member.para.pCell != end_para->member.para.pCell ||
300 ((this_para->member.para.nFlags|end_para->member.para.nFlags)
301 & (MEPF_ROWSTART|MEPF_ROWEND)))
303 while (this_para != end_para)
305 ME_DisplayItem *next_para = this_para->member.para.next_para;
306 BOOL bTruancateDeletion = FALSE;
307 if (this_para->member.para.nFlags & MEPF_ROWSTART) {
308 /* The following while loop assumes that next_para is MEPF_ROWSTART,
309 * so moving back one paragraph let's it be processed as the start
311 next_para = this_para;
312 this_para = this_para->member.para.prev_para;
313 } else if (next_para->member.para.pCell != this_para->member.para.pCell
314 || this_para->member.para.nFlags & MEPF_ROWEND)
316 /* Start of the deletion from after the start of the table row. */
317 bTruancateDeletion = TRUE;
319 while (!bTruancateDeletion &&
320 next_para->member.para.nFlags & MEPF_ROWSTART)
322 next_para = ME_GetTableRowEnd(next_para)->member.para.next_para;
323 if (next_para->member.para.nCharOfs > nOfs + *nChars)
325 /* End of deletion is not past the end of the table row. */
326 next_para = this_para->member.para.next_para;
327 /* Delete the end paragraph preceding the table row if the
328 * preceding table row will be empty. */
329 if (this_para->member.para.nCharOfs >= nOfs)
331 next_para = next_para->member.para.next_para;
333 bTruancateDeletion = TRUE;
335 this_para = next_para->member.para.prev_para;
338 if (bTruancateDeletion)
340 ME_Run *end_run = &ME_FindItemBack(next_para, diRun)->member.run;
341 int nCharsNew = (next_para->member.para.nCharOfs - nOfs
342 - end_run->strText->nLen);
343 nCharsNew = max(nCharsNew, 0);
344 assert(nCharsNew <= *nChars);
348 this_para = next_para;
351 } else { /* v1.0 - 3.0 */
352 ME_DisplayItem *pRun;
353 int nCharsToBoundary;
355 if ((this_para->member.para.nCharOfs != nOfs || this_para == end_para) &&
356 this_para->member.para.pFmt->dwMask & PFM_TABLE &&
357 this_para->member.para.pFmt->wEffects & PFE_TABLE)
360 /* Find the next tab or end paragraph to use as a delete boundary */
361 while (!(pRun->member.run.nFlags & (MERF_TAB|MERF_ENDPARA)))
362 pRun = ME_FindItemFwd(pRun, diRun);
363 nCharsToBoundary = pRun->member.run.nCharOfs
364 - c->pRun->member.run.nCharOfs
366 *nChars = min(*nChars, nCharsToBoundary);
367 } else if (end_para->member.para.pFmt->dwMask & PFM_TABLE &&
368 end_para->member.para.pFmt->wEffects & PFE_TABLE)
370 /* The deletion starts from before the row, so don't join it with
371 * previous non-empty paragraphs. */
372 ME_DisplayItem *curPara;
374 if (nOfs > this_para->member.para.nCharOfs) {
375 pRun = ME_FindItemBack(end_para, diRun);
376 curPara = end_para->member.para.prev_para;
379 pRun = ME_FindItemFwd(end_para, diRun);
384 nCharsToBoundary = curPara->member.para.nCharOfs
385 + pRun->member.run.nCharOfs
387 if (nCharsToBoundary >= 0)
388 *nChars = min(*nChars, nCharsToBoundary);
396 ME_DisplayItem* ME_AppendTableRow(ME_TextEditor *editor,
397 ME_DisplayItem *table_row)
399 WCHAR endl = '\r', tab = '\t';
405 assert(table_row->type == diParagraph);
406 if (!editor->bEmulateVersion10) { /* v4.1 */
407 ME_DisplayItem *insertedCell, *para, *cell, *prevTableEnd;
408 cell = ME_FindItemFwd(ME_GetTableRowStart(table_row), diCell);
409 prevTableEnd = ME_GetTableRowEnd(table_row);
410 para = prevTableEnd->member.para.next_para;
411 run = ME_FindItemFwd(para, diRun);
412 editor->pCursors[0].pPara = para;
413 editor->pCursors[0].pRun = run;
414 editor->pCursors[0].nOffset = 0;
415 editor->pCursors[1] = editor->pCursors[0];
416 para = ME_InsertTableRowStartFromCursor(editor);
417 insertedCell = ME_FindItemFwd(para, diCell);
418 /* Copy cell properties */
419 insertedCell->member.cell.nRightBoundary = cell->member.cell.nRightBoundary;
420 insertedCell->member.cell.border = cell->member.cell.border;
421 while (cell->member.cell.next_cell) {
422 cell = cell->member.cell.next_cell;
423 para = ME_InsertTableCellFromCursor(editor);
424 insertedCell = ME_FindItemBack(para, diCell);
425 /* Copy cell properties */
426 insertedCell->member.cell.nRightBoundary = cell->member.cell.nRightBoundary;
427 insertedCell->member.cell.border = cell->member.cell.border;
429 para = ME_InsertTableRowEndFromCursor(editor);
430 *para->member.para.pFmt = *prevTableEnd->member.para.pFmt;
431 /* return the table row start for the inserted paragraph */
432 return ME_FindItemFwd(cell, diParagraph)->member.para.next_para;
433 } else { /* v1.0 - 3.0 */
434 run = ME_FindItemBack(table_row->member.para.next_para, diRun);
435 pFmt = table_row->member.para.pFmt;
436 assert(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE);
437 editor->pCursors[0].pPara = table_row;
438 editor->pCursors[0].pRun = run;
439 editor->pCursors[0].nOffset = 0;
440 editor->pCursors[1] = editor->pCursors[0];
441 ME_InsertTextFromCursor(editor, 0, &endl, 1, run->member.run.style);
442 run = editor->pCursors[0].pRun;
443 for (i = 0; i < pFmt->cTabCount; i++) {
444 ME_InsertTextFromCursor(editor, 0, &tab, 1, run->member.run.style);
446 return table_row->member.para.next_para;
450 /* Selects the next table cell or appends a new table row if at end of table */
451 static void ME_SelectOrInsertNextCell(ME_TextEditor *editor,
454 ME_DisplayItem *para = ME_GetParagraph(run);
457 assert(run && run->type == diRun);
458 assert(ME_IsInTable(run));
459 if (!editor->bEmulateVersion10) { /* v4.1 */
460 ME_DisplayItem *cell;
461 /* Get the initial cell */
462 if (para->member.para.nFlags & MEPF_ROWSTART) {
463 cell = para->member.para.next_para->member.para.pCell;
464 } else if (para->member.para.nFlags & MEPF_ROWEND) {
465 cell = para->member.para.prev_para->member.para.pCell;
467 cell = para->member.para.pCell;
470 /* Get the next cell. */
471 if (cell->member.cell.next_cell &&
472 cell->member.cell.next_cell->member.cell.next_cell)
474 cell = cell->member.cell.next_cell;
476 para = ME_GetTableRowEnd(ME_FindItemFwd(cell, diParagraph));
477 para = para->member.para.next_para;
479 if (para->member.para.nFlags & MEPF_ROWSTART) {
480 cell = para->member.para.next_para->member.para.pCell;
483 para = para->member.para.prev_para;
484 para = ME_AppendTableRow(editor, ME_GetTableRowStart(para));
485 /* Put cursor at the start of the new table row */
486 para = para->member.para.next_para;
487 editor->pCursors[0].pPara = para;
488 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
489 editor->pCursors[0].nOffset = 0;
490 editor->pCursors[1] = editor->pCursors[0];
491 ME_WrapMarkedParagraphs(editor);
496 editor->pCursors[1].pRun = ME_FindItemFwd(cell, diRun);
497 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
498 editor->pCursors[1].nOffset = 0;
499 assert(editor->pCursors[0].pRun);
500 cell = cell->member.cell.next_cell;
501 editor->pCursors[0].pRun = ME_FindItemBack(cell, diRun);
502 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
503 editor->pCursors[0].nOffset = 0;
504 assert(editor->pCursors[1].pRun);
505 } else { /* v1.0 - 3.0 */
506 if (run->member.run.nFlags & MERF_ENDPARA &&
507 ME_IsInTable(ME_FindItemFwd(run, diParagraphOrEnd)))
509 run = ME_FindItemFwd(run, diRun);
512 for (i = 0; i < 2; i++)
514 while (!(run->member.run.nFlags & MERF_TAB))
516 run = ME_FindItemFwd(run, diRunOrParagraphOrEnd);
517 if (run->type != diRun)
520 if (ME_IsInTable(para))
522 run = ME_FindItemFwd(para, diRun);
524 editor->pCursors[0].pPara = para;
525 editor->pCursors[0].pRun = run;
526 editor->pCursors[0].nOffset = 0;
529 /* Insert table row */
530 para = ME_AppendTableRow(editor, para->member.para.prev_para);
531 /* Put cursor at the start of the new table row */
532 editor->pCursors[0].pPara = para;
533 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
534 editor->pCursors[0].nOffset = 0;
535 editor->pCursors[1] = editor->pCursors[0];
536 ME_WrapMarkedParagraphs(editor);
542 run = ME_FindItemFwd(run, diRun);
543 editor->pCursors[i].pRun = run;
544 editor->pCursors[i].pPara = ME_GetParagraph(run);
545 editor->pCursors[i].nOffset = 0;
551 void ME_TabPressedInTable(ME_TextEditor *editor, BOOL bSelectedRow)
553 /* FIXME: Shift tab should move to the previous cell. */
554 ME_Cursor fromCursor, toCursor;
555 ME_InvalidateSelection(editor);
558 from = ME_GetCursorOfs(&editor->pCursors[0]);
559 to = ME_GetCursorOfs(&editor->pCursors[1]);
562 fromCursor = editor->pCursors[0];
563 toCursor = editor->pCursors[1];
565 fromCursor = editor->pCursors[1];
566 toCursor = editor->pCursors[0];
569 if (!editor->bEmulateVersion10) /* v4.1 */
571 if (!ME_IsInTable(toCursor.pRun))
573 editor->pCursors[0] = toCursor;
574 editor->pCursors[1] = toCursor;
576 ME_SelectOrInsertNextCell(editor, toCursor.pRun);
578 } else { /* v1.0 - 3.0 */
579 if (!ME_IsInTable(fromCursor.pRun)) {
580 editor->pCursors[0] = fromCursor;
581 editor->pCursors[1] = fromCursor;
582 /* FIXME: For some reason the caret is shown at the start of the
583 * previous paragraph in v1.0 to v3.0, and bCaretAtEnd only works
584 * within the paragraph for wrapped lines. */
585 if (ME_FindItemBack(fromCursor.pRun, diRun))
586 editor->bCaretAtEnd = TRUE;
587 } else if ((bSelectedRow || !ME_IsInTable(toCursor.pRun))) {
588 ME_SelectOrInsertNextCell(editor, fromCursor.pRun);
590 if (ME_IsSelection(editor) && !toCursor.nOffset)
593 run = ME_FindItemBack(toCursor.pRun, diRunOrParagraphOrEnd);
594 if (run->type == diRun && run->member.run.nFlags & MERF_TAB)
595 ME_SelectOrInsertNextCell(editor, run);
597 ME_SelectOrInsertNextCell(editor, toCursor.pRun);
599 ME_SelectOrInsertNextCell(editor, toCursor.pRun);
603 ME_InvalidateSelection(editor);
605 ITextHost_TxShowCaret(editor->texthost, FALSE);
606 ME_ShowCaret(editor);
607 ME_SendSelChange(editor);
610 /* Make sure the cursor is not in the hidden table row start paragraph
611 * without a selection. */
612 void ME_MoveCursorFromTableRowStartParagraph(ME_TextEditor *editor)
614 ME_DisplayItem *para = editor->pCursors[0].pPara;
615 if (para == editor->pCursors[1].pPara &&
616 para->member.para.nFlags & MEPF_ROWSTART) {
617 /* The cursors should not be at the hidden start row paragraph without
618 * a selection, so the cursor is moved into the first cell. */
619 para = para->member.para.next_para;
620 editor->pCursors[0].pPara = para;
621 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
622 editor->pCursors[0].nOffset = 0;
623 editor->pCursors[1] = editor->pCursors[0];
627 struct RTFTable *ME_MakeTableDef(ME_TextEditor *editor)
629 RTFTable *tableDef = ALLOC_OBJ(RTFTable);
630 ZeroMemory(tableDef, sizeof(RTFTable));
631 if (!editor->bEmulateVersion10) /* v4.1 */
636 void ME_InitTableDef(ME_TextEditor *editor, struct RTFTable *tableDef)
638 ZeroMemory(tableDef->cells, sizeof(tableDef->cells));
639 ZeroMemory(tableDef->border, sizeof(tableDef->border));
640 tableDef->numCellsDefined = 0;
641 tableDef->leftEdge = 0;
642 if (!editor->bEmulateVersion10) /* v4.1 */
644 else /* v1.0 - 3.0 */