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