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