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