richedit: Used ITextHost methods to avoid using window handle directly.
[wine] / dlls / riched20 / table.c
1 /*
2  * RichEdit functions dealing with on tables
3  *
4  * Copyright 2008 by Dylan Smith
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /*
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.
28  *
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.
34  *
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.
39  *
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.
45  *
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
51  *   just a delimiter.
52  */
53
54 #include "editor.h"
55 #include "rtf.h"
56
57 WINE_DEFAULT_DEBUG_CHANNEL(richedit_lists);
58
59 static ME_DisplayItem* ME_InsertEndParaFromCursor(ME_TextEditor *editor,
60                                                   int nCursor,
61                                                   int numCR,
62                                                   int numLF,
63                                                   int paraFlags)
64 {
65   ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
66   ME_DisplayItem *tp;
67   ME_Cursor* cursor = &editor->pCursors[nCursor];
68   if (cursor->nOffset) {
69     ME_SplitRunSimple(editor, cursor->pRun, cursor->nOffset);
70     cursor = &editor->pCursors[nCursor];
71   }
72
73   tp = ME_SplitParagraph(editor, cursor->pRun, pStyle, numCR, numLF, paraFlags);
74   cursor->pRun = ME_FindItemFwd(tp, diRun);
75   return tp;
76 }
77
78 ME_DisplayItem* ME_InsertTableRowStartFromCursor(ME_TextEditor *editor)
79 {
80   ME_DisplayItem *para;
81   para = ME_InsertEndParaFromCursor(editor, 0, 1, 1, MEPF_ROWSTART);
82   return para->member.para.prev_para;
83 }
84
85 ME_DisplayItem* ME_InsertTableRowStartAtParagraph(ME_TextEditor *editor,
86                                                   ME_DisplayItem *para)
87 {
88   ME_DisplayItem *prev_para, *end_para;
89   ME_Cursor savedCursor = editor->pCursors[0];
90   ME_DisplayItem *startRowPara;
91   editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
92   editor->pCursors[0].nOffset = 0;
93   editor->pCursors[1] = editor->pCursors[0];
94   startRowPara = ME_InsertTableRowStartFromCursor(editor);
95   editor->pCursors[0] = savedCursor;
96   editor->pCursors[1] = editor->pCursors[0];
97
98   end_para = ME_GetParagraph(editor->pCursors[0].pRun)->member.para.next_para;
99   prev_para = startRowPara->member.para.next_para;
100   para = prev_para->member.para.next_para;
101   while (para != end_para)
102   {
103     para->member.para.pCell = prev_para->member.para.pCell;
104     para->member.para.nFlags |= MEPF_CELL;
105     para->member.para.nFlags &= ~(MEPF_ROWSTART|MEPF_ROWEND);
106     para->member.para.pFmt->dwMask |= PFM_TABLE|PFM_TABLEROWDELIMITER;
107     para->member.para.pFmt->wEffects |= PFE_TABLE;
108     para->member.para.pFmt->wEffects &= ~PFE_TABLEROWDELIMITER;
109     prev_para = para;
110     para = para->member.para.next_para;
111   }
112   return startRowPara;
113 }
114
115 /* Inserts a diCell and starts a new paragraph for the next cell.
116  *
117  * Returns the first paragraph of the new cell. */
118 ME_DisplayItem* ME_InsertTableCellFromCursor(ME_TextEditor *editor)
119 {
120   ME_DisplayItem *para;
121   para = ME_InsertEndParaFromCursor(editor, 0, 1, 0, MEPF_CELL);
122   return para;
123 }
124
125 ME_DisplayItem* ME_InsertTableRowEndFromCursor(ME_TextEditor *editor)
126 {
127   ME_DisplayItem *para;
128   para = ME_InsertEndParaFromCursor(editor, 0, 1, 1, MEPF_ROWEND);
129   return para->member.para.prev_para;
130 }
131
132 ME_DisplayItem* ME_GetTableRowEnd(ME_DisplayItem *para)
133 {
134   ME_DisplayItem *cell;
135   assert(para);
136   if (para->member.para.nFlags & MEPF_ROWEND)
137     return para;
138   if (para->member.para.nFlags & MEPF_ROWSTART)
139     para = para->member.para.next_para;
140   cell = para->member.para.pCell;
141   assert(cell && cell->type == diCell);
142   while (cell->member.cell.next_cell)
143     cell = cell->member.cell.next_cell;
144
145   para = ME_FindItemFwd(cell, diParagraph);
146   assert(para && para->member.para.nFlags & MEPF_ROWEND);
147   return para;
148 }
149
150 ME_DisplayItem* ME_GetTableRowStart(ME_DisplayItem *para)
151 {
152   ME_DisplayItem *cell;
153   assert(para);
154   if (para->member.para.nFlags & MEPF_ROWSTART)
155     return para;
156   if (para->member.para.nFlags & MEPF_ROWEND)
157     para = para->member.para.prev_para;
158   cell = para->member.para.pCell;
159   assert(cell && cell->type == diCell);
160   while (cell->member.cell.prev_cell)
161     cell = cell->member.cell.prev_cell;
162
163   para = ME_FindItemBack(cell, diParagraph);
164   assert(para && para->member.para.nFlags & MEPF_ROWSTART);
165   return para;
166 }
167
168 /* Make a bunch of assertions to make sure tables haven't been corrupted.
169  *
170  * These invariants may not hold true in the middle of streaming in rich text
171  * or during an undo and redo of streaming in rich text. It should be safe to
172  * call this method after an event is processed.
173  */
174 void ME_CheckTablesForCorruption(ME_TextEditor *editor)
175 {
176   if(TRACE_ON(richedit_lists))
177   {
178     TRACE("---\n");
179     ME_DumpDocument(editor->pBuffer);
180   }
181 #ifndef NDEBUG
182   {
183     ME_DisplayItem *p, *pPrev;
184     pPrev = editor->pBuffer->pFirst;
185     p = pPrev->next;
186     if (!editor->bEmulateVersion10) /* v4.1 */
187     {
188       while (p->type == diParagraph)
189       {
190         assert(p->member.para.pFmt->dwMask & PFM_TABLE);
191         assert(p->member.para.pFmt->dwMask & PFM_TABLEROWDELIMITER);
192         if (p->member.para.pCell)
193         {
194           assert(p->member.para.nFlags & MEPF_CELL);
195           assert(p->member.para.pFmt->wEffects & PFE_TABLE);
196         }
197         if (p->member.para.pCell != pPrev->member.para.pCell)
198         {
199           /* There must be a diCell in between the paragraphs if pCell changes. */
200           ME_DisplayItem *pCell = ME_FindItemBack(p, diCell);
201           assert(pCell);
202           assert(ME_FindItemBack(p, diRun) == ME_FindItemBack(pCell, diRun));
203         }
204         if (p->member.para.nFlags & MEPF_ROWEND)
205         {
206           /* ROWEND must come after a cell. */
207           assert(pPrev->member.para.pCell);
208           assert(p->member.para.pCell
209                  == pPrev->member.para.pCell->member.cell.parent_cell);
210           assert(p->member.para.pFmt->wEffects & PFE_TABLEROWDELIMITER);
211         }
212         else if (p->member.para.pCell)
213         {
214           assert(!(p->member.para.pFmt->wEffects & PFE_TABLEROWDELIMITER));
215           assert(pPrev->member.para.pCell ||
216                  pPrev->member.para.nFlags & MEPF_ROWSTART);
217           if (pPrev->member.para.pCell &&
218               !(pPrev->member.para.nFlags & MEPF_ROWSTART))
219           {
220             assert(p->member.para.pCell->member.cell.parent_cell
221                    == pPrev->member.para.pCell->member.cell.parent_cell);
222             if (pPrev->member.para.pCell != p->member.para.pCell)
223               assert(pPrev->member.para.pCell
224                      == p->member.para.pCell->member.cell.prev_cell);
225           }
226         }
227         else if (!(p->member.para.nFlags & MEPF_ROWSTART))
228         {
229           assert(!(p->member.para.pFmt->wEffects & PFE_TABLEROWDELIMITER));
230           /* ROWSTART must be followed by a cell. */
231           assert(!(p->member.para.nFlags & MEPF_CELL));
232           /* ROWSTART must be followed by a cell. */
233           assert(!(pPrev->member.para.nFlags & MEPF_ROWSTART));
234         }
235         pPrev = p;
236         p = p->member.para.next_para;
237       }
238     } else { /* v1.0 - 3.0 */
239       while (p->type == diParagraph)
240       {
241         assert(!(p->member.para.nFlags & (MEPF_ROWSTART|MEPF_ROWEND|MEPF_CELL)));
242         assert(p->member.para.pFmt->dwMask & PFM_TABLE);
243         assert(!(p->member.para.pFmt->wEffects & PFM_TABLEROWDELIMITER));
244         assert(!p->member.para.pCell);
245         p = p->member.para.next_para;
246       }
247       return;
248     }
249     assert(p->type == diTextEnd);
250     assert(!pPrev->member.para.pCell);
251   }
252 #endif
253 }
254
255 BOOL ME_IsInTable(ME_DisplayItem *pItem)
256 {
257   PARAFORMAT2 *pFmt;
258   if (!pItem)
259     return FALSE;
260   if (pItem->type == diRun)
261     pItem = ME_GetParagraph(pItem);
262   if (pItem->type != diParagraph)
263     return FALSE;
264   pFmt = pItem->member.para.pFmt;
265   return pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE;
266 }
267
268 /* Table rows should either be deleted completely or not at all. */
269 void ME_ProtectPartialTableDeletion(ME_TextEditor *editor, int nOfs,int *nChars)
270 {
271   ME_Cursor c, c2;
272   ME_DisplayItem *this_para, *end_para;
273   ME_CursorFromCharOfs(editor, nOfs, &c);
274   this_para = ME_GetParagraph(c.pRun);
275   ME_CursorFromCharOfs(editor, nOfs + *nChars, &c2);
276   end_para = ME_GetParagraph(c2.pRun);
277   if (c2.pRun->member.run.nFlags & MERF_ENDPARA) {
278     /* End offset might be in the middle of the end paragraph run.
279      * If this is the case, then we need to use the next paragraph as the last
280      * paragraphs.
281      */
282     int remaining = nOfs + *nChars - c2.pRun->member.run.nCharOfs
283                     - end_para->member.para.nCharOfs;
284     if (remaining)
285     {
286       assert(remaining < c2.pRun->member.run.nCR + c2.pRun->member.run.nLF);
287       end_para = end_para->member.para.next_para;
288     }
289   }
290   if (!editor->bEmulateVersion10) { /* v4.1 */
291     if (this_para->member.para.pCell != end_para->member.para.pCell ||
292         ((this_para->member.para.nFlags|end_para->member.para.nFlags)
293          & (MEPF_ROWSTART|MEPF_ROWEND)))
294     {
295       while (this_para != end_para)
296       {
297         ME_DisplayItem *next_para = this_para->member.para.next_para;
298         BOOL bTruancateDeletion = FALSE;
299         if (this_para->member.para.nFlags & MEPF_ROWSTART) {
300           /* The following while loop assumes that next_para is MEPF_ROWSTART,
301            * so moving back one paragraph let's it be processed as the start
302            * of the row. */
303           next_para = this_para;
304           this_para = this_para->member.para.prev_para;
305         } else if (next_para->member.para.pCell != this_para->member.para.pCell
306                    || this_para->member.para.nFlags & MEPF_ROWEND)
307         {
308           /* Start of the deletion from after the start of the table row. */
309           bTruancateDeletion = TRUE;
310         }
311         while (!bTruancateDeletion &&
312                next_para->member.para.nFlags & MEPF_ROWSTART)
313         {
314           next_para = ME_GetTableRowEnd(next_para)->member.para.next_para;
315           if (next_para->member.para.nCharOfs > nOfs + *nChars)
316           {
317             /* End of deletion is not past the end of the table row. */
318             next_para = this_para->member.para.next_para;
319             /* Delete the end paragraph preceding the table row if the
320              * preceding table row will be empty. */
321             if (this_para->member.para.nCharOfs >= nOfs)
322             {
323               next_para = next_para->member.para.next_para;
324             }
325             bTruancateDeletion = TRUE;
326           } else {
327             this_para = next_para->member.para.prev_para;
328           }
329         }
330         if (bTruancateDeletion)
331         {
332           ME_Run *end_run = &ME_FindItemBack(next_para, diRun)->member.run;
333           int nCharsNew = (next_para->member.para.nCharOfs - nOfs
334                            - end_run->nCR - end_run->nLF);
335           nCharsNew = max(nCharsNew, 0);
336           assert(nCharsNew <= *nChars);
337           *nChars = nCharsNew;
338           break;
339         }
340         this_para = next_para;
341       }
342     }
343   } else { /* v1.0 - 3.0 */
344     ME_DisplayItem *pRun;
345     int nCharsToBoundary;
346
347     if ((this_para->member.para.nCharOfs != nOfs || this_para == end_para) &&
348         this_para->member.para.pFmt->dwMask & PFM_TABLE &&
349         this_para->member.para.pFmt->wEffects & PFE_TABLE)
350     {
351       pRun = c.pRun;
352       /* Find the next tab or end paragraph to use as a delete boundary */
353       while (!(pRun->member.run.nFlags & (MERF_TAB|MERF_ENDPARA)))
354         pRun = ME_FindItemFwd(pRun, diRun);
355       nCharsToBoundary = pRun->member.run.nCharOfs
356                          - c.pRun->member.run.nCharOfs
357                          - c.nOffset;
358       *nChars = min(*nChars, nCharsToBoundary);
359     } else if (end_para->member.para.pFmt->dwMask & PFM_TABLE &&
360                end_para->member.para.pFmt->wEffects & PFE_TABLE)
361     {
362       /* The deletion starts from before the row, so don't join it with
363        * previous non-empty paragraphs. */
364       pRun = NULL;
365       if (nOfs > this_para->member.para.nCharOfs)
366         pRun = ME_FindItemBack(end_para, diRun);
367       if (!pRun)
368         pRun = ME_FindItemFwd(end_para, diRun);
369       if (pRun)
370       {
371         nCharsToBoundary = ME_GetParagraph(pRun)->member.para.nCharOfs
372                            + pRun->member.run.nCharOfs
373                            - nOfs;
374         if (nCharsToBoundary >= 0)
375           *nChars = min(*nChars, nCharsToBoundary);
376       }
377     }
378     if (*nChars < 0)
379       nChars = 0;
380   }
381 }
382
383 ME_DisplayItem* ME_AppendTableRow(ME_TextEditor *editor,
384                                   ME_DisplayItem *table_row)
385 {
386   WCHAR endl = '\r', tab = '\t';
387   ME_DisplayItem *run;
388   PARAFORMAT2 *pFmt;
389   int i;
390
391   assert(table_row);
392   assert(table_row->type == diParagraph);
393   if (!editor->bEmulateVersion10) { /* v4.1 */
394     ME_DisplayItem *insertedCell, *para, *cell, *prevTableEnd;
395     cell = ME_FindItemFwd(ME_GetTableRowStart(table_row), diCell);
396     prevTableEnd = ME_GetTableRowEnd(table_row);
397     run = prevTableEnd->member.para.next_para;
398     run = ME_FindItemFwd(run, diRun);
399     editor->pCursors[0].pRun = run;
400     editor->pCursors[0].nOffset = 0;
401     editor->pCursors[1] = editor->pCursors[0];
402     para = ME_InsertTableRowStartFromCursor(editor);
403     insertedCell = ME_FindItemFwd(para, diCell);
404       /* Copy cell properties */
405     insertedCell->member.cell.nRightBoundary = cell->member.cell.nRightBoundary;
406     insertedCell->member.cell.border = cell->member.cell.border;
407     while (cell->member.cell.next_cell) {
408       cell = cell->member.cell.next_cell;
409       para = ME_InsertTableCellFromCursor(editor);
410       insertedCell = ME_FindItemBack(para, diCell);
411       /* Copy cell properties */
412       insertedCell->member.cell.nRightBoundary = cell->member.cell.nRightBoundary;
413       insertedCell->member.cell.border = cell->member.cell.border;
414     };
415     para = ME_InsertTableRowEndFromCursor(editor);
416     *para->member.para.pFmt = *prevTableEnd->member.para.pFmt;
417     /* return the table row start for the inserted paragraph */
418     return ME_FindItemFwd(cell, diParagraph)->member.para.next_para;
419   } else { /* v1.0 - 3.0 */
420     run = ME_FindItemBack(table_row->member.para.next_para, diRun);
421     pFmt = table_row->member.para.pFmt;
422     assert(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE);
423     editor->pCursors[0].pRun = run;
424     editor->pCursors[0].nOffset = 0;
425     editor->pCursors[1] = editor->pCursors[0];
426     ME_InsertTextFromCursor(editor, 0, &endl, 1, run->member.run.style);
427     run = editor->pCursors[0].pRun;
428     for (i = 0; i < pFmt->cTabCount; i++) {
429       ME_InsertTextFromCursor(editor, 0, &tab, 1, run->member.run.style);
430     }
431     return table_row->member.para.next_para;
432   }
433 }
434
435 /* Selects the next table cell or appends a new table row if at end of table */
436 static void ME_SelectOrInsertNextCell(ME_TextEditor *editor,
437                                       ME_DisplayItem *run)
438 {
439   ME_DisplayItem *para = ME_GetParagraph(run);
440   int i;
441
442   assert(run && run->type == diRun);
443   assert(ME_IsInTable(run));
444   if (!editor->bEmulateVersion10) { /* v4.1 */
445     ME_DisplayItem *cell;
446     /* Get the initial cell */
447     if (para->member.para.nFlags & MEPF_ROWSTART) {
448       cell = para->member.para.next_para->member.para.pCell;
449     } else if (para->member.para.nFlags & MEPF_ROWEND) {
450       cell = para->member.para.prev_para->member.para.pCell;
451     } else {
452       cell = para->member.para.pCell;
453     }
454     assert(cell);
455     /* Get the next cell. */
456     if (cell->member.cell.next_cell &&
457         cell->member.cell.next_cell->member.cell.next_cell)
458     {
459       cell = cell->member.cell.next_cell;
460     } else {
461       para = ME_GetTableRowEnd(ME_FindItemFwd(cell, diParagraph));
462       para = para->member.para.next_para;
463       assert(para);
464       if (para->member.para.nFlags & MEPF_ROWSTART) {
465         cell = para->member.para.next_para->member.para.pCell;
466       } else {
467         /* Insert row */
468         para = para->member.para.prev_para;
469         para = ME_AppendTableRow(editor, ME_GetTableRowStart(para));
470         /* Put cursor at the start of the new table row */
471         para = para->member.para.next_para;
472         editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
473         editor->pCursors[0].nOffset = 0;
474         editor->pCursors[1] = editor->pCursors[0];
475         ME_WrapMarkedParagraphs(editor);
476         return;
477       }
478     }
479     /* Select cell */
480     editor->pCursors[1].pRun = ME_FindItemFwd(cell, diRun);
481     editor->pCursors[1].nOffset = 0;
482     assert(editor->pCursors[0].pRun);
483     cell = cell->member.cell.next_cell;
484     editor->pCursors[0].pRun = ME_FindItemBack(cell, diRun);
485     editor->pCursors[0].nOffset = 0;
486     assert(editor->pCursors[1].pRun);
487   } else { /* v1.0 - 3.0 */
488     if (run->member.run.nFlags & MERF_ENDPARA &&
489         ME_IsInTable(ME_FindItemFwd(run, diParagraphOrEnd)))
490     {
491       run = ME_FindItemFwd(run, diRun);
492       assert(run);
493     }
494     for (i = 0; i < 2; i++)
495     {
496       while (!(run->member.run.nFlags & MERF_TAB))
497       {
498         run = ME_FindItemFwd(run, diRunOrParagraphOrEnd);
499         if (run->type != diRun)
500         {
501           para = run;
502           if (ME_IsInTable(para))
503           {
504             run = ME_FindItemFwd(para, diRun);
505             assert(run);
506             editor->pCursors[0].pRun = run;
507             editor->pCursors[0].nOffset = 0;
508             i = 1;
509           } else {
510             /* Insert table row */
511             para = ME_AppendTableRow(editor, para->member.para.prev_para);
512             /* Put cursor at the start of the new table row */
513             editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
514             editor->pCursors[0].nOffset = 0;
515             editor->pCursors[1] = editor->pCursors[0];
516             ME_WrapMarkedParagraphs(editor);
517             return;
518           }
519         }
520       }
521       if (i == 0)
522         run = ME_FindItemFwd(run, diRun);
523       editor->pCursors[i].pRun = run;
524       editor->pCursors[i].nOffset = 0;
525     }
526   }
527 }
528
529
530 void ME_TabPressedInTable(ME_TextEditor *editor, BOOL bSelectedRow)
531 {
532   /* FIXME: Shift tab should move to the previous cell. */
533   ME_Cursor fromCursor, toCursor;
534   ME_InvalidateSelection(editor);
535   {
536     int from, to;
537     from = ME_GetCursorOfs(editor, 0);
538     to = ME_GetCursorOfs(editor, 1);
539     if (from <= to)
540     {
541       fromCursor = editor->pCursors[0];
542       toCursor = editor->pCursors[1];
543     } else {
544       fromCursor = editor->pCursors[1];
545       toCursor = editor->pCursors[0];
546     }
547   }
548   if (!editor->bEmulateVersion10) /* v4.1 */
549   {
550     if (!ME_IsInTable(toCursor.pRun))
551     {
552       editor->pCursors[0] = toCursor;
553       editor->pCursors[1] = toCursor;
554     } else {
555       ME_SelectOrInsertNextCell(editor, toCursor.pRun);
556     }
557   } else { /* v1.0 - 3.0 */
558     if (!ME_IsInTable(fromCursor.pRun)) {
559       editor->pCursors[0] = fromCursor;
560       editor->pCursors[1] = fromCursor;
561       /* FIXME: For some reason the caret is shown at the start of the
562        *        previous paragraph in v1.0 to v3.0, and bCaretAtEnd only works
563        *        within the paragraph for wrapped lines. */
564       if (ME_FindItemBack(fromCursor.pRun, diRun))
565         editor->bCaretAtEnd = TRUE;
566     } else if ((bSelectedRow || !ME_IsInTable(toCursor.pRun))) {
567       ME_SelectOrInsertNextCell(editor, fromCursor.pRun);
568     } else {
569       if (ME_IsSelection(editor) && !toCursor.nOffset)
570       {
571         ME_DisplayItem *run;
572         run = ME_FindItemBack(toCursor.pRun, diRunOrParagraphOrEnd);
573         if (run->type == diRun && run->member.run.nFlags & MERF_TAB)
574           ME_SelectOrInsertNextCell(editor, run);
575         else
576           ME_SelectOrInsertNextCell(editor, toCursor.pRun);
577       } else {
578         ME_SelectOrInsertNextCell(editor, toCursor.pRun);
579       }
580     }
581   }
582   ME_InvalidateSelection(editor);
583   ME_Repaint(editor);
584   ITextHost_TxShowCaret(editor->texthost, FALSE);
585   ME_ShowCaret(editor);
586   ME_SendSelChange(editor);
587 }
588
589 /* Make sure the cursor is not in the hidden table row start paragraph
590  * without a selection. */
591 void ME_MoveCursorFromTableRowStartParagraph(ME_TextEditor *editor)
592 {
593   ME_DisplayItem *para = ME_GetParagraph(editor->pCursors[0].pRun);
594   if (para == ME_GetParagraph(editor->pCursors[1].pRun) &&
595       para->member.para.nFlags & MEPF_ROWSTART) {
596     /* The cursors should not be at the hidden start row paragraph without
597      * a selection, so the cursor is moved into the first cell. */
598     para = para->member.para.next_para;
599     editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
600     editor->pCursors[0].nOffset = 0;
601     editor->pCursors[1] = editor->pCursors[0];
602   }
603 }
604
605 struct RTFTable *ME_MakeTableDef(ME_TextEditor *editor)
606 {
607   RTFTable *tableDef = ALLOC_OBJ(RTFTable);
608   ZeroMemory(tableDef, sizeof(RTFTable));
609   if (!editor->bEmulateVersion10) /* v4.1 */
610     tableDef->gapH = 10;
611   return tableDef;
612 }
613
614 void ME_InitTableDef(ME_TextEditor *editor, struct RTFTable *tableDef)
615 {
616   ZeroMemory(tableDef->cells, sizeof(tableDef->cells));
617   ZeroMemory(tableDef->border, sizeof(tableDef->border));
618   tableDef->numCellsDefined = 0;
619   tableDef->leftEdge = 0;
620   if (!editor->bEmulateVersion10) /* v4.1 */
621     tableDef->gapH = 10;
622   else /* v1.0 - 3.0 */
623     tableDef->gapH = 0;
624 }