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