cbfa2b3f771b2d6f27d4c5561eb7723a551253de
[reactos.git] / 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->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, ME_Cursor *c, int *nChars)
279 {
280 int nOfs = ME_GetCursorOfs(c);
281 ME_Cursor c2 = *c;
282 ME_DisplayItem *this_para = c->pPara;
283 ME_DisplayItem *end_para;
284
285 ME_MoveCursorChars(editor, &c2, *nChars);
286 end_para = c2.pPara;
287 if (c2.pRun->member.run.nFlags & MERF_ENDPARA) {
288 /* End offset might be in the middle of the end paragraph run.
289 * If this is the case, then we need to use the next paragraph as the last
290 * paragraphs.
291 */
292 int remaining = nOfs + *nChars - c2.pRun->member.run.nCharOfs
293 - end_para->member.para.nCharOfs;
294 if (remaining)
295 {
296 assert(remaining < c2.pRun->member.run.strText->nLen);
297 end_para = end_para->member.para.next_para;
298 }
299 }
300 if (!editor->bEmulateVersion10) { /* v4.1 */
301 if (this_para->member.para.pCell != end_para->member.para.pCell ||
302 ((this_para->member.para.nFlags|end_para->member.para.nFlags)
303 & (MEPF_ROWSTART|MEPF_ROWEND)))
304 {
305 while (this_para != end_para)
306 {
307 ME_DisplayItem *next_para = this_para->member.para.next_para;
308 BOOL bTruancateDeletion = FALSE;
309 if (this_para->member.para.nFlags & MEPF_ROWSTART) {
310 /* The following while loop assumes that next_para is MEPF_ROWSTART,
311 * so moving back one paragraph let's it be processed as the start
312 * of the row. */
313 next_para = this_para;
314 this_para = this_para->member.para.prev_para;
315 } else if (next_para->member.para.pCell != this_para->member.para.pCell
316 || this_para->member.para.nFlags & MEPF_ROWEND)
317 {
318 /* Start of the deletion from after the start of the table row. */
319 bTruancateDeletion = TRUE;
320 }
321 while (!bTruancateDeletion &&
322 next_para->member.para.nFlags & MEPF_ROWSTART)
323 {
324 next_para = ME_GetTableRowEnd(next_para)->member.para.next_para;
325 if (next_para->member.para.nCharOfs > nOfs + *nChars)
326 {
327 /* End of deletion is not past the end of the table row. */
328 next_para = this_para->member.para.next_para;
329 /* Delete the end paragraph preceding the table row if the
330 * preceding table row will be empty. */
331 if (this_para->member.para.nCharOfs >= nOfs)
332 {
333 next_para = next_para->member.para.next_para;
334 }
335 bTruancateDeletion = TRUE;
336 } else {
337 this_para = next_para->member.para.prev_para;
338 }
339 }
340 if (bTruancateDeletion)
341 {
342 ME_Run *end_run = &ME_FindItemBack(next_para, diRun)->member.run;
343 int nCharsNew = (next_para->member.para.nCharOfs - nOfs
344 - end_run->strText->nLen);
345 nCharsNew = max(nCharsNew, 0);
346 assert(nCharsNew <= *nChars);
347 *nChars = nCharsNew;
348 break;
349 }
350 this_para = next_para;
351 }
352 }
353 } else { /* v1.0 - 3.0 */
354 ME_DisplayItem *pRun;
355 int nCharsToBoundary;
356
357 if ((this_para->member.para.nCharOfs != nOfs || this_para == end_para) &&
358 this_para->member.para.pFmt->dwMask & PFM_TABLE &&
359 this_para->member.para.pFmt->wEffects & PFE_TABLE)
360 {
361 pRun = c->pRun;
362 /* Find the next tab or end paragraph to use as a delete boundary */
363 while (!(pRun->member.run.nFlags & (MERF_TAB|MERF_ENDPARA)))
364 pRun = ME_FindItemFwd(pRun, diRun);
365 nCharsToBoundary = pRun->member.run.nCharOfs
366 - c->pRun->member.run.nCharOfs
367 - c->nOffset;
368 *nChars = min(*nChars, nCharsToBoundary);
369 } else if (end_para->member.para.pFmt->dwMask & PFM_TABLE &&
370 end_para->member.para.pFmt->wEffects & PFE_TABLE)
371 {
372 /* The deletion starts from before the row, so don't join it with
373 * previous non-empty paragraphs. */
374 ME_DisplayItem *curPara;
375 pRun = NULL;
376 if (nOfs > this_para->member.para.nCharOfs) {
377 pRun = ME_FindItemBack(end_para, diRun);
378 curPara = end_para->member.para.prev_para;
379 }
380 if (!pRun) {
381 pRun = ME_FindItemFwd(end_para, diRun);
382 curPara = end_para;
383 }
384 if (pRun)
385 {
386 nCharsToBoundary = curPara->member.para.nCharOfs
387 + pRun->member.run.nCharOfs
388 - nOfs;
389 if (nCharsToBoundary >= 0)
390 *nChars = min(*nChars, nCharsToBoundary);
391 }
392 }
393 if (*nChars < 0)
394 nChars = 0;
395 }
396 }
397
398 ME_DisplayItem* ME_AppendTableRow(ME_TextEditor *editor,
399 ME_DisplayItem *table_row)
400 {
401 WCHAR endl = '\r', tab = '\t';
402 ME_DisplayItem *run;
403 PARAFORMAT2 *pFmt;
404 int i;
405
406 assert(table_row);
407 assert(table_row->type == diParagraph);
408 if (!editor->bEmulateVersion10) { /* v4.1 */
409 ME_DisplayItem *insertedCell, *para, *cell, *prevTableEnd;
410 cell = ME_FindItemFwd(ME_GetTableRowStart(table_row), diCell);
411 prevTableEnd = ME_GetTableRowEnd(table_row);
412 para = prevTableEnd->member.para.next_para;
413 run = ME_FindItemFwd(para, diRun);
414 editor->pCursors[0].pPara = para;
415 editor->pCursors[0].pRun = run;
416 editor->pCursors[0].nOffset = 0;
417 editor->pCursors[1] = editor->pCursors[0];
418 para = ME_InsertTableRowStartFromCursor(editor);
419 insertedCell = ME_FindItemFwd(para, diCell);
420 /* Copy cell properties */
421 insertedCell->member.cell.nRightBoundary = cell->member.cell.nRightBoundary;
422 insertedCell->member.cell.border = cell->member.cell.border;
423 while (cell->member.cell.next_cell) {
424 cell = cell->member.cell.next_cell;
425 para = ME_InsertTableCellFromCursor(editor);
426 insertedCell = ME_FindItemBack(para, diCell);
427 /* Copy cell properties */
428 insertedCell->member.cell.nRightBoundary = cell->member.cell.nRightBoundary;
429 insertedCell->member.cell.border = cell->member.cell.border;
430 };
431 para = ME_InsertTableRowEndFromCursor(editor);
432 *para->member.para.pFmt = *prevTableEnd->member.para.pFmt;
433 /* return the table row start for the inserted paragraph */
434 return ME_FindItemFwd(cell, diParagraph)->member.para.next_para;
435 } else { /* v1.0 - 3.0 */
436 run = ME_FindItemBack(table_row->member.para.next_para, diRun);
437 pFmt = table_row->member.para.pFmt;
438 assert(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE);
439 editor->pCursors[0].pPara = table_row;
440 editor->pCursors[0].pRun = run;
441 editor->pCursors[0].nOffset = 0;
442 editor->pCursors[1] = editor->pCursors[0];
443 ME_InsertTextFromCursor(editor, 0, &endl, 1, run->member.run.style);
444 run = editor->pCursors[0].pRun;
445 for (i = 0; i < pFmt->cTabCount; i++) {
446 ME_InsertTextFromCursor(editor, 0, &tab, 1, run->member.run.style);
447 }
448 return table_row->member.para.next_para;
449 }
450 }
451
452 /* Selects the next table cell or appends a new table row if at end of table */
453 static void ME_SelectOrInsertNextCell(ME_TextEditor *editor,
454 ME_DisplayItem *run)
455 {
456 ME_DisplayItem *para = ME_GetParagraph(run);
457 int i;
458
459 assert(run && run->type == diRun);
460 assert(ME_IsInTable(run));
461 if (!editor->bEmulateVersion10) { /* v4.1 */
462 ME_DisplayItem *cell;
463 /* Get the initial cell */
464 if (para->member.para.nFlags & MEPF_ROWSTART) {
465 cell = para->member.para.next_para->member.para.pCell;
466 } else if (para->member.para.nFlags & MEPF_ROWEND) {
467 cell = para->member.para.prev_para->member.para.pCell;
468 } else {
469 cell = para->member.para.pCell;
470 }
471 assert(cell);
472 /* Get the next cell. */
473 if (cell->member.cell.next_cell &&
474 cell->member.cell.next_cell->member.cell.next_cell)
475 {
476 cell = cell->member.cell.next_cell;
477 } else {
478 para = ME_GetTableRowEnd(ME_FindItemFwd(cell, diParagraph));
479 para = para->member.para.next_para;
480 assert(para);
481 if (para->member.para.nFlags & MEPF_ROWSTART) {
482 cell = para->member.para.next_para->member.para.pCell;
483 } else {
484 /* Insert row */
485 para = para->member.para.prev_para;
486 para = ME_AppendTableRow(editor, ME_GetTableRowStart(para));
487 /* Put cursor at the start of the new table row */
488 para = para->member.para.next_para;
489 editor->pCursors[0].pPara = para;
490 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
491 editor->pCursors[0].nOffset = 0;
492 editor->pCursors[1] = editor->pCursors[0];
493 ME_WrapMarkedParagraphs(editor);
494 return;
495 }
496 }
497 /* Select cell */
498 editor->pCursors[1].pRun = ME_FindItemFwd(cell, diRun);
499 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
500 editor->pCursors[1].nOffset = 0;
501 assert(editor->pCursors[0].pRun);
502 cell = cell->member.cell.next_cell;
503 editor->pCursors[0].pRun = ME_FindItemBack(cell, diRun);
504 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
505 editor->pCursors[0].nOffset = 0;
506 assert(editor->pCursors[1].pRun);
507 } else { /* v1.0 - 3.0 */
508 if (run->member.run.nFlags & MERF_ENDPARA &&
509 ME_IsInTable(ME_FindItemFwd(run, diParagraphOrEnd)))
510 {
511 run = ME_FindItemFwd(run, diRun);
512 assert(run);
513 }
514 for (i = 0; i < 2; i++)
515 {
516 while (!(run->member.run.nFlags & MERF_TAB))
517 {
518 run = ME_FindItemFwd(run, diRunOrParagraphOrEnd);
519 if (run->type != diRun)
520 {
521 para = run;
522 if (ME_IsInTable(para))
523 {
524 run = ME_FindItemFwd(para, diRun);
525 assert(run);
526 editor->pCursors[0].pPara = para;
527 editor->pCursors[0].pRun = run;
528 editor->pCursors[0].nOffset = 0;
529 i = 1;
530 } else {
531 /* Insert table row */
532 para = ME_AppendTableRow(editor, para->member.para.prev_para);
533 /* Put cursor at the start of the new table row */
534 editor->pCursors[0].pPara = para;
535 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
536 editor->pCursors[0].nOffset = 0;
537 editor->pCursors[1] = editor->pCursors[0];
538 ME_WrapMarkedParagraphs(editor);
539 return;
540 }
541 }
542 }
543 if (i == 0)
544 run = ME_FindItemFwd(run, diRun);
545 editor->pCursors[i].pRun = run;
546 editor->pCursors[i].pPara = ME_GetParagraph(run);
547 editor->pCursors[i].nOffset = 0;
548 }
549 }
550 }
551
552
553 void ME_TabPressedInTable(ME_TextEditor *editor, BOOL bSelectedRow)
554 {
555 /* FIXME: Shift tab should move to the previous cell. */
556 ME_Cursor fromCursor, toCursor;
557 ME_InvalidateSelection(editor);
558 {
559 int from, to;
560 from = ME_GetCursorOfs(&editor->pCursors[0]);
561 to = ME_GetCursorOfs(&editor->pCursors[1]);
562 if (from <= to)
563 {
564 fromCursor = editor->pCursors[0];
565 toCursor = editor->pCursors[1];
566 } else {
567 fromCursor = editor->pCursors[1];
568 toCursor = editor->pCursors[0];
569 }
570 }
571 if (!editor->bEmulateVersion10) /* v4.1 */
572 {
573 if (!ME_IsInTable(toCursor.pRun))
574 {
575 editor->pCursors[0] = toCursor;
576 editor->pCursors[1] = toCursor;
577 } else {
578 ME_SelectOrInsertNextCell(editor, toCursor.pRun);
579 }
580 } else { /* v1.0 - 3.0 */
581 if (!ME_IsInTable(fromCursor.pRun)) {
582 editor->pCursors[0] = fromCursor;
583 editor->pCursors[1] = fromCursor;
584 /* FIXME: For some reason the caret is shown at the start of the
585 * previous paragraph in v1.0 to v3.0, and bCaretAtEnd only works
586 * within the paragraph for wrapped lines. */
587 if (ME_FindItemBack(fromCursor.pRun, diRun))
588 editor->bCaretAtEnd = TRUE;
589 } else if ((bSelectedRow || !ME_IsInTable(toCursor.pRun))) {
590 ME_SelectOrInsertNextCell(editor, fromCursor.pRun);
591 } else {
592 if (ME_IsSelection(editor) && !toCursor.nOffset)
593 {
594 ME_DisplayItem *run;
595 run = ME_FindItemBack(toCursor.pRun, diRunOrParagraphOrEnd);
596 if (run->type == diRun && run->member.run.nFlags & MERF_TAB)
597 ME_SelectOrInsertNextCell(editor, run);
598 else
599 ME_SelectOrInsertNextCell(editor, toCursor.pRun);
600 } else {
601 ME_SelectOrInsertNextCell(editor, toCursor.pRun);
602 }
603 }
604 }
605 ME_InvalidateSelection(editor);
606 ME_Repaint(editor);
607 ITextHost_TxShowCaret(editor->texthost, FALSE);
608 ME_ShowCaret(editor);
609 ME_SendSelChange(editor);
610 }
611
612 /* Make sure the cursor is not in the hidden table row start paragraph
613 * without a selection. */
614 void ME_MoveCursorFromTableRowStartParagraph(ME_TextEditor *editor)
615 {
616 ME_DisplayItem *para = editor->pCursors[0].pPara;
617 if (para == editor->pCursors[1].pPara &&
618 para->member.para.nFlags & MEPF_ROWSTART) {
619 /* The cursors should not be at the hidden start row paragraph without
620 * a selection, so the cursor is moved into the first cell. */
621 para = para->member.para.next_para;
622 editor->pCursors[0].pPara = para;
623 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
624 editor->pCursors[0].nOffset = 0;
625 editor->pCursors[1] = editor->pCursors[0];
626 }
627 }
628
629 struct RTFTable *ME_MakeTableDef(ME_TextEditor *editor)
630 {
631 RTFTable *tableDef = ALLOC_OBJ(RTFTable);
632 ZeroMemory(tableDef, sizeof(RTFTable));
633 if (!editor->bEmulateVersion10) /* v4.1 */
634 tableDef->gapH = 10;
635 return tableDef;
636 }
637
638 void ME_InitTableDef(ME_TextEditor *editor, struct RTFTable *tableDef)
639 {
640 ZeroMemory(tableDef->cells, sizeof(tableDef->cells));
641 ZeroMemory(tableDef->border, sizeof(tableDef->border));
642 tableDef->numCellsDefined = 0;
643 tableDef->leftEdge = 0;
644 if (!editor->bEmulateVersion10) /* v4.1 */
645 tableDef->gapH = 10;
646 else /* v1.0 - 3.0 */
647 tableDef->gapH = 0;
648 }