]> git.lyx.org Git - lyx.git/blob - src/text.C
make redoParagraph more independent of current cursor
[lyx.git] / src / text.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #include "lyxtext.h"
14 #include "lyxrow.h"
15 #include "paragraph.h"
16 #include "gettext.h"
17 #include "bufferparams.h"
18 #include "buffer.h"
19 #include "debug.h"
20 #include "intl.h"
21 #include "lyxrc.h"
22 #include "encoding.h"
23 #include "frontends/LyXView.h"
24 #include "frontends/Painter.h"
25 #include "frontends/font_metrics.h"
26 #include "frontends/screen.h"
27 #include "frontends/WorkArea.h"
28 #include "bufferview_funcs.h"
29 #include "BufferView.h"
30 #include "language.h"
31 #include "ParagraphParameters.h"
32 #include "undo_funcs.h"
33 #include "text_funcs.h"
34 #include "WordLangTuple.h"
35 #include "paragraph_funcs.h"
36 #include "rowpainter.h"
37 #include "lyxrow_funcs.h"
38 #include "metricsinfo.h"
39
40 #include "insets/insettext.h"
41
42 #include "support/textutils.h"
43 #include "support/LAssert.h"
44 #include "support/lstrings.h"
45
46 #include <algorithm>
47
48 using namespace lyx::support;
49
50 using std::max;
51 using std::min;
52 using std::endl;
53 using std::pair;
54
55 using lyx::pos_type;
56 using lyx::word_location;
57
58 using namespace bv_funcs;
59
60 /// top, right, bottom pixel margin
61 extern int const PAPER_MARGIN = 20;
62 /// margin for changebar
63 extern int const CHANGEBAR_MARGIN = 10;
64 /// left margin
65 extern int const LEFT_MARGIN = PAPER_MARGIN + CHANGEBAR_MARGIN;
66
67 int bibitemMaxWidth(BufferView *, LyXFont const &);
68
69
70 BufferView * LyXText::bv()
71 {
72         Assert(bv_owner != 0);
73         return bv_owner;
74 }
75
76
77 BufferView * LyXText::bv() const
78 {
79         Assert(bv_owner != 0);
80         return bv_owner;
81 }
82
83
84 void LyXText::updateRowPositions()
85 {
86         RowList::iterator rit = rows().begin();
87         RowList::iterator rend = rows().end();
88         for (int y = 0; rit != rend ; ++rit) {
89                 rit->y(y);
90                 y += rit->height();
91         }
92 }
93
94
95 int LyXText::top_y() const
96 {
97         if (anchor_row_ == rowlist_.end())
98                 return 0;
99
100         return anchor_row_->y() + anchor_row_offset_;
101 }
102
103
104 void LyXText::top_y(int newy)
105 {
106         if (rows().empty())
107                 return;
108
109         if (isInInset()) {
110                 anchor_row_ = rows().begin();
111                 anchor_row_offset_ = newy;
112                 return;
113         }
114
115         lyxerr[Debug::GUI] << "setting top y = " << newy << endl;
116
117         int y = newy;
118         RowList::iterator rit = getRowNearY(y);
119
120         if (rit == anchor_row_ && anchor_row_offset_ == newy - y) {
121                 lyxerr[Debug::GUI] << "top_y to same value, skipping update" << endl;
122                 return;
123         }
124
125         anchor_row_ = rit;
126         anchor_row_offset_ = newy - y;
127         lyxerr[Debug::GUI] << "changing reference to row: " << &*anchor_row_
128                << " offset: " << anchor_row_offset_ << endl;
129         postPaint();
130 }
131
132
133 void LyXText::anchor_row(RowList::iterator rit)
134 {
135         int old_y = top_y();
136         anchor_row_offset_ = 0;
137         anchor_row_ = rit;
138         anchor_row_offset_ = old_y - top_y();
139         lyxerr[Debug::GUI] << "anchor_row(): changing reference to row: "
140                            << &*anchor_row_ << " offset: "
141                            << anchor_row_offset_ << endl;
142 }
143
144
145 int LyXText::workWidth() const
146 {
147         return inset_owner ? inset_owner->textWidth() : bv()->workWidth();
148 }
149
150
151 int LyXText::getRealCursorX() const
152 {
153         int x = cursor.x();
154         if (the_locking_inset && (the_locking_inset->getLyXText(bv())!= this))
155                 x = the_locking_inset->getLyXText(bv())->getRealCursorX();
156         return x;
157 }
158
159
160 #warning FIXME  This function seems to belong outside of LyxText.
161 unsigned char LyXText::transformChar(unsigned char c, Paragraph const & par,
162                                      pos_type pos) const
163 {
164         if (!Encodings::is_arabic(c))
165                 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
166                         return c + (0xb0 - '0');
167                 else
168                         return c;
169
170         unsigned char const prev_char = pos > 0 ? par.getChar(pos - 1) : ' ';
171         unsigned char next_char = ' ';
172
173         pos_type const par_size = par.size();
174
175         for (pos_type i = pos + 1; i < par_size; ++i) {
176                 unsigned char const par_char = par.getChar(i);
177                 if (!Encodings::IsComposeChar_arabic(par_char)) {
178                         next_char = par_char;
179                         break;
180                 }
181         }
182
183         if (Encodings::is_arabic(next_char)) {
184                 if (Encodings::is_arabic(prev_char) &&
185                         !Encodings::is_arabic_special(prev_char))
186                         return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
187                 else
188                         return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
189         } else {
190                 if (Encodings::is_arabic(prev_char) &&
191                         !Encodings::is_arabic_special(prev_char))
192                         return Encodings::TransformChar(c, Encodings::FORM_FINAL);
193                 else
194                         return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
195         }
196 }
197
198 // This is the comments that some of the warnings below refers to.
199 // There are some issues in this file and I don't think they are
200 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
201 // this is a problem that has been here almost from day one and that a
202 // larger userbase with differenct access patters triggers the bad
203 // behaviour. (segfaults.) What I think happen is: In several places
204 // we store the paragraph in the current cursor and then moves the
205 // cursor. This movement of the cursor will delete paragraph at the
206 // old position if it is now empty. This will make the temporary
207 // pointer to the old cursor paragraph invalid and dangerous to use.
208 // And is some cases this will trigger a segfault. I have marked some
209 // of the cases where this happens with a warning, but I am sure there
210 // are others in this file and in text2.C. There is also a note in
211 // Delete() that you should read. In Delete I store the paragraph->id
212 // instead of a pointer to the paragraph. I am pretty sure this faulty
213 // use of temporary pointers to paragraphs that might have gotten
214 // invalidated (through a cursor movement) before they are used, are
215 // the cause of the strange crashes we get reported often.
216 //
217 // It is very tiresom to change this code, especially when it is as
218 // hard to read as it is. Help to fix all the cases where this is done
219 // would be greately appreciated.
220 //
221 // Lgb
222
223 int LyXText::singleWidth(ParagraphList::iterator pit, pos_type pos) const
224 {
225         if (pos >= pit->size())
226                 return 0;
227
228         char const c = pit->getChar(pos);
229         return singleWidth(pit, pos, c);
230 }
231
232
233 int LyXText::singleWidth(ParagraphList::iterator pit,
234                          pos_type pos, char c) const
235 {
236         if (pos >= pit->size())
237                 return 0;
238
239         LyXFont const & font = getFont(pit, pos);
240
241         // The most common case is handled first (Asger)
242         if (IsPrintable(c)) {
243                 if (font.language()->RightToLeft()) {
244                         if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
245                              lyxrc.font_norm_type == LyXRC::ISO_10646_1)
246                             && font.language()->lang() == "arabic") {
247                                 if (Encodings::IsComposeChar_arabic(c))
248                                         return 0;
249                                 else
250                                         c = transformChar(c, *pit, pos);
251                         } else if (font.language()->lang() == "hebrew" &&
252                                  Encodings::IsComposeChar_hebrew(c))
253                                 return 0;
254                 }
255                 return font_metrics::width(c, font);
256         }
257
258         if (c == Paragraph::META_INSET) {
259                 InsetOld * tmpinset = pit->getInset(pos);
260                 if (tmpinset) {
261                         if (tmpinset->lyxCode() == InsetOld::HFILL_CODE) {
262                                 // Because of the representation as vertical lines
263                                 return 3;
264                         }
265 #if 0
266 #warning enabling this fixes the 'insets of width 0 on load' problem
267                         // this IS needed otherwise on initialitation we don't get the fill
268                         // of the row right (ONLY on initialization if we read a file!)
269                         // should be changed! (Jug 20011204)
270                         //tmpinset->update(bv());
271                         Dimension dim;
272                         MetricsInfo mi(bv(), font, workWidth());
273                         tmpinset->metrics(mi, dim);
274                         return dim.wid;
275 #else
276                         return tmpinset->width();
277 #endif
278                 }
279                 return 0;
280         }
281
282         if (IsSeparatorChar(c))
283                 c = ' ';
284         return font_metrics::width(c, font);
285 }
286
287
288 lyx::pos_type LyXText::log2vis(lyx::pos_type pos) const
289 {
290         if (bidi_start == -1)
291                 return pos;
292         else
293                 return log2vis_list[pos - bidi_start];
294 }
295
296
297 lyx::pos_type LyXText::vis2log(lyx::pos_type pos) const
298 {
299         if (bidi_start == -1)
300                 return pos;
301         else
302                 return vis2log_list[pos - bidi_start];
303 }
304
305
306 lyx::pos_type LyXText::bidi_level(lyx::pos_type pos) const
307 {
308         if (bidi_start == -1)
309                 return 0;
310         else
311                 return bidi_levels[pos - bidi_start];
312 }
313
314
315 bool LyXText::bidi_InRange(lyx::pos_type pos) const
316 {
317         return bidi_start == -1 ||
318                 (bidi_start <= pos && pos <= bidi_end);
319 }
320
321
322 void LyXText::computeBidiTables(Buffer const * buf,
323                                 RowList::iterator row) const
324 {
325         bidi_same_direction = true;
326         if (!lyxrc.rtl_support) {
327                 bidi_start = -1;
328                 return;
329         }
330
331         ParagraphList::iterator row_par = row->par();
332
333         InsetOld * inset = row_par->inInset();
334         if (inset && inset->owner() &&
335             inset->owner()->lyxCode() == InsetOld::ERT_CODE) {
336                 bidi_start = -1;
337                 return;
338         }
339
340         bidi_start = row->pos();
341         bidi_end = lastPrintablePos(*this, row);
342
343         if (bidi_start > bidi_end) {
344                 bidi_start = -1;
345                 return;
346         }
347
348         if (bidi_end + 2 - bidi_start >
349             static_cast<pos_type>(log2vis_list.size())) {
350                 pos_type new_size =
351                         (bidi_end + 2 - bidi_start < 500) ?
352                         500 : 2 * (bidi_end + 2 - bidi_start);
353                 log2vis_list.resize(new_size);
354                 vis2log_list.resize(new_size);
355                 bidi_levels.resize(new_size);
356         }
357
358         vis2log_list[bidi_end + 1 - bidi_start] = -1;
359         log2vis_list[bidi_end + 1 - bidi_start] = -1;
360
361         pos_type stack[2];
362         bool const rtl_par =
363                 row_par->isRightToLeftPar(buf->params);
364         int level = 0;
365         bool rtl = false;
366         bool rtl0 = false;
367         pos_type const body_pos = row_par->beginningOfBody();
368
369         for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
370                 bool is_space = row_par->isLineSeparator(lpos);
371                 pos_type const pos =
372                         (is_space && lpos + 1 <= bidi_end &&
373                          !row_par->isLineSeparator(lpos + 1) &&
374                          !row_par->isNewline(lpos + 1))
375                         ? lpos + 1 : lpos;
376                 LyXFont font = row_par->getFontSettings(buf->params, pos);
377                 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
378                     font.number() == LyXFont::ON &&
379                     row_par->getFontSettings(buf->params, lpos - 1).number()
380                     == LyXFont::ON) {
381                         font = row_par->getFontSettings(buf->params, lpos);
382                         is_space = false;
383                 }
384
385
386                 bool new_rtl = font.isVisibleRightToLeft();
387                 bool new_rtl0 = font.isRightToLeft();
388                 int new_level;
389
390                 if (lpos == body_pos - 1
391                     && row->pos() < body_pos - 1
392                     && is_space) {
393                         new_level = (rtl_par) ? 1 : 0;
394                         new_rtl = new_rtl0 = rtl_par;
395                 } else if (new_rtl0)
396                         new_level = (new_rtl) ? 1 : 2;
397                 else
398                         new_level = (rtl_par) ? 2 : 0;
399
400                 if (is_space && new_level >= level) {
401                         new_level = level;
402                         new_rtl = rtl;
403                         new_rtl0 = rtl0;
404                 }
405
406                 int new_level2 = new_level;
407
408                 if (level == new_level && rtl0 != new_rtl0) {
409                         --new_level2;
410                         log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
411                 } else if (level < new_level) {
412                         log2vis_list[lpos - bidi_start] =  (rtl) ? -1 : 1;
413                         if (new_level > rtl_par)
414                                 bidi_same_direction = false;
415                 } else
416                         log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
417                 rtl = new_rtl;
418                 rtl0 = new_rtl0;
419                 bidi_levels[lpos - bidi_start] = new_level;
420
421                 while (level > new_level2) {
422                         pos_type old_lpos = stack[--level];
423                         int delta = lpos - old_lpos - 1;
424                         if (level % 2)
425                                 delta = -delta;
426                         log2vis_list[lpos - bidi_start] += delta;
427                         log2vis_list[old_lpos - bidi_start] += delta;
428                 }
429                 while (level < new_level)
430                         stack[level++] = lpos;
431         }
432
433         while (level > 0) {
434                 pos_type const old_lpos = stack[--level];
435                 int delta = bidi_end - old_lpos;
436                 if (level % 2)
437                         delta = -delta;
438                 log2vis_list[old_lpos - bidi_start] += delta;
439         }
440
441         pos_type vpos = bidi_start - 1;
442         for (pos_type lpos = bidi_start;
443              lpos <= bidi_end; ++lpos) {
444                 vpos += log2vis_list[lpos - bidi_start];
445                 vis2log_list[vpos - bidi_start] = lpos;
446                 log2vis_list[lpos - bidi_start] = vpos;
447         }
448 }
449
450
451 // This method requires a previous call to ComputeBidiTables()
452 bool LyXText::isBoundary(Buffer const * buf, Paragraph const & par,
453                          pos_type pos) const
454 {
455         if (!lyxrc.rtl_support || pos == 0)
456                 return false;
457
458         if (!bidi_InRange(pos - 1)) {
459                 /// This can happen if pos is the first char of a row.
460                 /// Returning false in this case is incorrect!
461                 return false;
462         }
463
464         bool const rtl = bidi_level(pos - 1) % 2;
465         bool const rtl2 = bidi_InRange(pos)
466                 ? bidi_level(pos) % 2
467                 : par.isRightToLeftPar(buf->params);
468         return rtl != rtl2;
469 }
470
471
472 bool LyXText::isBoundary(Buffer const * buf, Paragraph const & par,
473                          pos_type pos, LyXFont const & font) const
474 {
475         if (!lyxrc.rtl_support)
476                 return false;    // This is just for speedup
477
478         bool const rtl = font.isVisibleRightToLeft();
479         bool const rtl2 = bidi_InRange(pos)
480                 ? bidi_level(pos) % 2
481                 : par.isRightToLeftPar(buf->params);
482         return rtl != rtl2;
483 }
484
485
486 int LyXText::leftMargin(Row const & row) const
487 {
488         InsetOld * ins;
489
490         if (row.pos() < row.par()->size())
491                 if ((row.par()->getChar(row.pos()) == Paragraph::META_INSET) &&
492                     (ins = row.par()->getInset(row.pos())) &&
493                     (ins->needFullRow() || ins->display()))
494                         return LEFT_MARGIN;
495
496         LyXTextClass const & tclass =
497                 bv()->buffer()->params.getLyXTextClass();
498         LyXLayout_ptr const & layout = row.par()->layout();
499
500         string parindent = layout->parindent;
501
502         int x = LEFT_MARGIN;
503
504         x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
505
506         // this is the way, LyX handles the LaTeX-Environments.
507         // I have had this idea very late, so it seems to be a
508         // later added hack and this is true
509         if (!row.par()->getDepth()) {
510                 if (row.par()->layout() == tclass.defaultLayout()) {
511                         // find the previous same level paragraph
512                         if (row.par() != ownerParagraphs().begin()) {
513                                 ParagraphList::iterator newpit =
514                                         depthHook(row.par(), ownerParagraphs(),
515                                                   row.par()->getDepth());
516                                 if (newpit == row.par() &&
517                                     newpit->layout()->nextnoindent)
518                                         parindent.erase();
519                         }
520                 }
521         } else {
522                 // find the next level paragraph
523
524                 ParagraphList::iterator newpar = outerHook(row.par(),
525                                                            ownerParagraphs());
526
527                 // make a corresponding row. Needed to call leftMargin()
528
529                 // check wether it is a sufficent paragraph
530                 if (newpar != ownerParagraphs().end() &&
531                     newpar->layout()->isEnvironment()) {
532                         Row dummyrow;
533                         dummyrow.par(newpar);
534                         dummyrow.pos(newpar->size());
535                         x = leftMargin(dummyrow);
536                 }
537
538                 if (newpar != ownerParagraphs().end() &&
539                     row.par()->layout() == tclass.defaultLayout()) {
540                         if (newpar->params().noindent())
541                                 parindent.erase();
542                         else {
543                                 parindent = newpar->layout()->parindent;
544                         }
545
546                 }
547         }
548
549         LyXFont const labelfont = getLabelFont(row.par());
550         switch (layout->margintype) {
551         case MARGIN_DYNAMIC:
552                 if (!layout->leftmargin.empty()) {
553                         x += font_metrics::signedWidth(layout->leftmargin,
554                                                   tclass.defaultfont());
555                 }
556                 if (!row.par()->getLabelstring().empty()) {
557                         x += font_metrics::signedWidth(layout->labelindent,
558                                                   labelfont);
559                         x += font_metrics::width(row.par()->getLabelstring(),
560                                             labelfont);
561                         x += font_metrics::width(layout->labelsep, labelfont);
562                 }
563                 break;
564         case MARGIN_MANUAL:
565                 x += font_metrics::signedWidth(layout->labelindent, labelfont);
566                 // The width of an empty par, even with manual label, should be 0
567                 if (!row.par()->empty() && row.pos() >= row.par()->beginningOfBody()) {
568                         if (!row.par()->getLabelWidthString().empty()) {
569                                 x += font_metrics::width(row.par()->getLabelWidthString(),
570                                                labelfont);
571                                 x += font_metrics::width(layout->labelsep, labelfont);
572                         }
573                 }
574                 break;
575         case MARGIN_STATIC:
576                 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
577                         / (row.par()->getDepth() + 4);
578                 break;
579         case MARGIN_FIRST_DYNAMIC:
580                 if (layout->labeltype == LABEL_MANUAL) {
581                         if (row.pos() >= row.par()->beginningOfBody()) {
582                                 x += font_metrics::signedWidth(layout->leftmargin,
583                                                           labelfont);
584                         } else {
585                                 x += font_metrics::signedWidth(layout->labelindent,
586                                                           labelfont);
587                         }
588                 } else if (row.pos()
589                            // Special case to fix problems with
590                            // theorems (JMarc)
591                            || (layout->labeltype == LABEL_STATIC
592                                && layout->latextype == LATEX_ENVIRONMENT
593                                && !isFirstInSequence(row.par(), ownerParagraphs()))) {
594                         x += font_metrics::signedWidth(layout->leftmargin,
595                                                   labelfont);
596                 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
597                            && layout->labeltype != LABEL_BIBLIO
598                            && layout->labeltype !=
599                            LABEL_CENTERED_TOP_ENVIRONMENT) {
600                         x += font_metrics::signedWidth(layout->labelindent,
601                                                   labelfont);
602                         x += font_metrics::width(layout->labelsep, labelfont);
603                         x += font_metrics::width(row.par()->getLabelstring(),
604                                             labelfont);
605                 }
606                 break;
607
608         case MARGIN_RIGHT_ADDRESS_BOX:
609         {
610                 // ok, a terrible hack. The left margin depends on the widest
611                 // row in this paragraph. Do not care about footnotes, they
612                 // are *NOT* allowed in the LaTeX realisation of this layout.
613
614                 // find the first row of this paragraph
615                 RowList::iterator tmprit = rowlist_.begin();
616                 while (tmprit != rowlist_.end()
617                        && tmprit->par() != row.par())
618                         ++tmprit;
619
620                 int minfill = tmprit->fill();
621                 while (boost::next(tmprit) != rowlist_.end() &&
622                        boost::next(tmprit)->par() == row.par()) {
623                         ++tmprit;
624                         if (tmprit->fill() < minfill)
625                                 minfill = tmprit->fill();
626                 }
627
628                 x += font_metrics::signedWidth(layout->leftmargin,
629                         tclass.defaultfont());
630                 x += minfill;
631         }
632         break;
633         }
634
635         if ((workWidth() > 0) &&
636                 !row.par()->params().leftIndent().zero())
637         {
638                 LyXLength const len = row.par()->params().leftIndent();
639                 int const tw = inset_owner ?
640                         inset_owner->latexTextWidth(bv()) : workWidth();
641                 x += len.inPixels(tw);
642         }
643
644         LyXAlignment align;
645
646         if (row.par()->params().align() == LYX_ALIGN_LAYOUT)
647                 align = layout->align;
648         else
649                 align = row.par()->params().align();
650
651         // set the correct parindent
652         if (row.pos() == 0) {
653                 if ((layout->labeltype == LABEL_NO_LABEL
654                      || layout->labeltype == LABEL_TOP_ENVIRONMENT
655                      || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
656                      || (layout->labeltype == LABEL_STATIC
657                          && layout->latextype == LATEX_ENVIRONMENT
658                          && !isFirstInSequence(row.par(), ownerParagraphs())))
659                     && align == LYX_ALIGN_BLOCK
660                     && !row.par()->params().noindent()
661                         // in tabulars and ert paragraphs are never indented!
662                         && (!row.par()->inInset() || !row.par()->inInset()->owner() ||
663                                 (row.par()->inInset()->owner()->lyxCode() != InsetOld::TABULAR_CODE &&
664                                  row.par()->inInset()->owner()->lyxCode() != InsetOld::ERT_CODE))
665                     && (row.par()->layout() != tclass.defaultLayout() ||
666                         bv()->buffer()->params.paragraph_separation ==
667                         BufferParams::PARSEP_INDENT)) {
668                         x += font_metrics::signedWidth(parindent,
669                                                   tclass.defaultfont());
670                 } else if (layout->labeltype == LABEL_BIBLIO) {
671                         // ale970405 Right width for bibitems
672                         x += bibitemMaxWidth(bv(), tclass.defaultfont());
673                 }
674         }
675
676         return x;
677 }
678
679
680 int LyXText::rightMargin(Buffer const & buf, Row const & row) const
681 {
682         InsetOld * ins;
683
684         if (row.pos() < row.par()->size())
685                 if ((row.par()->getChar(row.pos()) == Paragraph::META_INSET) &&
686                     (ins = row.par()->getInset(row.pos())) &&
687                     (ins->needFullRow() || ins->display()))
688                         return PAPER_MARGIN;
689
690         LyXTextClass const & tclass = buf.params.getLyXTextClass();
691         LyXLayout_ptr const & layout = row.par()->layout();
692
693         return PAPER_MARGIN
694                 + font_metrics::signedWidth(tclass.rightmargin(),
695                                        tclass.defaultfont());
696                 + font_metrics::signedWidth(layout->rightmargin,
697                                        tclass.defaultfont())
698                 * 4 / (row.par()->getDepth() + 4);
699 }
700
701
702 int LyXText::labelEnd(Row const & row) const
703 {
704         if (row.par()->layout()->margintype == MARGIN_MANUAL) {
705                 Row tmprow = row;
706                 tmprow.pos(row.par()->size());
707                 // return the beginning of the body
708                 return leftMargin(tmprow);
709         }
710
711         // LabelEnd is only needed if the layout
712         // fills a flushleft label.
713         return 0;
714 }
715
716
717 namespace {
718
719 // this needs special handling - only newlines count as a break point
720 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
721 {
722         for (; i < par.size(); ++i) {
723                 if (par.isNewline(i))
724                         return i;
725         }
726
727         return par.size();
728 }
729
730 };
731
732
733 pos_type LyXText::rowBreakPoint(Row const & row) const
734 {
735         ParagraphList::iterator pit = row.par();
736
737         // maximum pixel width of a row.
738         int width = workWidth() - rightMargin(*bv()->buffer(), row);
739
740         // inset->textWidth() returns -1 via workWidth(),
741         // but why ?
742         if (width < 0)
743                 return pit->size();
744
745         LyXLayout_ptr const & layout = pit->layout();
746
747         if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
748                 return addressBreakPoint(row.pos(), *pit);
749
750         pos_type const pos = row.pos();
751         pos_type const body_pos = pit->beginningOfBody();
752         pos_type const last = pit->size();
753         pos_type point = last;
754
755         if (pos == last)
756                 return last;
757
758         // Now we iterate through until we reach the right margin
759         // or the end of the par, then choose the possible break
760         // nearest that.
761
762         int const left = leftMargin(row);
763         int x = left;
764
765         // pixel width since last breakpoint
766         int chunkwidth = 0;
767         bool fullrow = false;
768
769         pos_type i = pos;
770
771         // We re-use the font resolution for the entire font span when possible
772         LyXFont font = getFont(pit, i);
773         lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
774
775         for (; i < last; ++i) {
776                 if (pit->isNewline(i)) {
777                         point = i;
778                         break;
779                 }
780
781                 char const c = pit->getChar(i);
782
783                 int thiswidth;
784
785                 // add the auto-hfill from label end to the body
786                 if (body_pos && i == body_pos) {
787                         thiswidth = font_metrics::width(layout->labelsep, getLabelFont(pit));
788                         if (pit->isLineSeparator(i - 1))
789                                 thiswidth -= singleWidth(pit, i - 1);
790                         int left_margin = labelEnd(row);
791                         if (thiswidth + x < left_margin)
792                                 thiswidth = left_margin - x;
793                         thiswidth += singleWidth(pit, i, c);
794                 } else {
795                         // Manual inlined optimised version of common case of "thiswidth = singleWidth(pit, i, c);"
796                         if (IsPrintable(c)) {
797                                 if (pos > endPosOfFontSpan) {
798                                         // We need to get the next font
799                                         font = getFont(pit, i);
800                                         endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
801                                 }
802                                 if (! font.language()->RightToLeft()) {
803                                         thiswidth = font_metrics::width(c, font);
804                                 } else {
805                                         // Fall-back to normal case
806                                         thiswidth = singleWidth(pit, i, c);
807                                         // And flush font cache
808                                         endPosOfFontSpan = 0;
809                                 }
810                         } else {
811                                 // Fall-back to normal case
812                                 thiswidth = singleWidth(pit, i, c);
813                                 // And flush font cache
814                                 endPosOfFontSpan = 0;
815                         }
816                 }
817
818                 x += thiswidth;
819                 chunkwidth += thiswidth;
820
821                 InsetOld * in = pit->isInset(i) ? pit->getInset(i) : 0;
822                 fullrow = in && (in->display() || in->needFullRow());
823
824                 // break before a character that will fall off
825                 // the right of the row
826                 if (x >= width) {
827                         // if no break before or we are at an inset
828                         // that will take up a row, break here
829                         if (point == last || fullrow || chunkwidth >= (width - left)) {
830                                 if (pos < i)
831                                         point = i - 1;
832                                 else
833                                         point = i;
834                         }
835                         break;
836                 }
837
838                 if (!in || in->isChar()) {
839                         // some insets are line separators too
840                         if (pit->isLineSeparator(i)) {
841                                 point = i;
842                                 chunkwidth = 0;
843                         }
844                         continue;
845                 }
846
847                 if (!fullrow)
848                         continue;
849
850                 // full row insets start at a new row
851                 if (i == pos) {
852                         if (pos < last - 1) {
853                                 point = i;
854                                 if (pit->isLineSeparator(i + 1))
855                                         ++point;
856                         } else {
857                                 // to avoid extra rows
858                                 point = last;
859                         }
860                 } else {
861                         point = i - 1;
862                 }
863
864                 return point;
865         }
866
867         if (point == last && x >= width) {
868                 // didn't find one, break at the point we reached the edge
869                 point = i;
870         } else if (i == last && x < width) {
871                 // found one, but we fell off the end of the par, so prefer
872                 // that.
873                 point = last;
874         }
875
876         // manual labels cannot be broken in LaTeX. But we
877         // want to make our on-screen rendering of footnotes
878         // etc. still break
879         if (!fullrow && body_pos && point < body_pos)
880                 point = body_pos - 1;
881
882         return point;
883 }
884
885
886 // returns the minimum space a row needs on the screen in pixel
887 int LyXText::fill(RowList::iterator row, int paper_width) const
888 {
889         if (paper_width < 0)
890                 return 0;
891
892         int w;
893         // get the pure distance
894         pos_type const last = lastPrintablePos(*this, row);
895
896         ParagraphList::iterator pit = row->par();
897         LyXLayout_ptr const & layout = pit->layout();
898
899         // special handling of the right address boxes
900         if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
901                 int const tmpfill = row->fill();
902                 row->fill(0); // the minfill in MarginLeft()
903                 w = leftMargin(*row);
904                 row->fill(tmpfill);
905         } else
906                 w = leftMargin(*row);
907
908         pos_type const body_pos = pit->beginningOfBody();
909         pos_type i = row->pos();
910
911         if (! pit->empty() && i <= last) {
912                 // We re-use the font resolution for the entire span when possible
913                 LyXFont font = getFont(pit, i);
914                 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
915                 while (i <= last) {
916                         if (body_pos > 0 && i == body_pos) {
917                                 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
918                                 if (pit->isLineSeparator(i - 1))
919                                         w -= singleWidth(pit, i - 1);
920                                 int left_margin = labelEnd(*row);
921                                 if (w < left_margin)
922                                         w = left_margin;
923                         }
924                         { // Manual inlined an optimised version of the common case of "w += singleWidth(pit, i);"
925                                 char const c = pit->getChar(i);
926
927                                 if (IsPrintable(c)) {
928                                         if (i > endPosOfFontSpan) {
929                                                 // We need to get the next font
930                                                 font = getFont(pit, i);
931                                                 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
932                                         }
933                                         if (!font.language()->RightToLeft()) {
934                                                 w += font_metrics::width(c, font);
935                                         } else {
936                                                 // Fall-back to the normal case
937                                                 w += singleWidth(pit, i, c);
938                                                 // And flush font cache
939                                                 endPosOfFontSpan = 0;
940                                         }
941                                 } else {
942                                         // Fall-back to the normal case
943                                         w += singleWidth(pit, i, c);
944                                         // And flush font cache
945                                         endPosOfFontSpan = 0;
946                                 }
947                         }
948                         ++i;
949                 }
950         }
951         if (body_pos > 0 && body_pos > last) {
952                 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
953                 if (last >= 0 && pit->isLineSeparator(last))
954                         w -= singleWidth(pit, last);
955                 int const left_margin = labelEnd(*row);
956                 if (w < left_margin)
957                         w = left_margin;
958         }
959
960         int const fill = paper_width - w - rightMargin(*bv()->buffer(), *row);
961
962         // If this case happens, it means that our calculation
963         // of the widths of the chars when we do rowBreakPoint()
964         // went wrong for some reason. Typically in list bodies.
965         // Things just about hobble on anyway, though you'll end
966         // up with a "fill_separator" less than zero, which corresponds
967         // to inter-word spacing being too small. Hopefully this problem
968         // will die when the label hacks die.
969         if (lyxerr.debugging() && fill < 0) {
970                 lyxerr[Debug::GUI] << "Eek, fill() was < 0: " << fill
971                         << " w " << w << " paper_width " << paper_width
972                         << " right margin " << rightMargin(*bv()->buffer(), *row) << endl;
973         }
974         return fill;
975 }
976
977
978 // returns the minimum space a manual label needs on the screen in pixel
979 int LyXText::labelFill(Row const & row) const
980 {
981         ParagraphList::iterator pit = row.par();
982
983         pos_type last = pit->beginningOfBody();
984
985         Assert(last > 0);
986
987         // -1 because a label ends either with a space that is in the label,
988         // or with the beginning of a footnote that is outside the label.
989         --last;
990
991         // a separator at this end does not count
992         if (pit->isLineSeparator(last))
993                 --last;
994
995         int w = 0;
996         pos_type i = row.pos();
997         while (i <= last) {
998                 w += singleWidth(pit, i);
999                 ++i;
1000         }
1001
1002         int fill = 0;
1003         string const & labwidstr = pit->params().labelWidthString();
1004         if (!labwidstr.empty()) {
1005                 LyXFont const labfont = getLabelFont(pit);
1006                 int const labwidth = font_metrics::width(labwidstr, labfont);
1007                 fill = max(labwidth - w, 0);
1008         }
1009
1010         return fill;
1011 }
1012
1013
1014 LColor::color LyXText::backgroundColor() const
1015 {
1016         if (inset_owner)
1017                 return inset_owner->backgroundColor();
1018         else
1019                 return LColor::background;
1020 }
1021
1022
1023 void LyXText::setHeightOfRow(RowList::iterator rit)
1024 {
1025         Assert(rit != rows().end());
1026
1027         // get the maximum ascent and the maximum descent
1028         double layoutasc = 0;
1029         double layoutdesc = 0;
1030         double tmptop = 0;
1031
1032         // ok, let us initialize the maxasc and maxdesc value.
1033         // Only the fontsize count. The other properties
1034         // are taken from the layoutfont. Nicer on the screen :)
1035         ParagraphList::iterator pit = rit->par();
1036
1037         LyXLayout_ptr const & layout = pit->layout();
1038
1039         // as max get the first character of this row then it can increase but not
1040         // decrease the height. Just some point to start with so we don't have to
1041         // do the assignment below too often.
1042         LyXFont font = getFont(pit, rit->pos());
1043         LyXFont::FONT_SIZE const tmpsize = font.size();
1044         font = getLayoutFont(pit);
1045         LyXFont::FONT_SIZE const size = font.size();
1046         font.setSize(tmpsize);
1047
1048         LyXFont labelfont = getLabelFont(pit);
1049
1050         double spacing_val = 1.0;
1051         if (!pit->params().spacing().isDefault())
1052                 spacing_val = pit->params().spacing().getValue();
1053         else
1054                 spacing_val = bv()->buffer()->params.spacing.getValue();
1055         //lyxerr << "spacing_val = " << spacing_val << endl;
1056
1057         int maxasc  = int(font_metrics::maxAscent(font) *
1058                           layout->spacing.getValue() * spacing_val);
1059         int maxdesc = int(font_metrics::maxDescent(font) *
1060                           layout->spacing.getValue() * spacing_val);
1061
1062         pos_type const pos_end = lastPos(*this, rit);
1063         int labeladdon = 0;
1064         int maxwidth = 0;
1065
1066         if (!pit->empty()) {
1067                 // We re-use the font resolution for the entire font span when possible
1068                 LyXFont font = getFont(pit, rit->pos());
1069                 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(rit->pos());
1070
1071                 // Optimisation
1072                 Paragraph const & par = *pit;
1073
1074                 // Check if any insets are larger
1075                 for (pos_type pos = rit->pos(); pos <= pos_end; ++pos) {
1076                         // Manual inlined optimised version of common case of
1077                         // "maxwidth += singleWidth(pit, pos);"
1078                         char const c = par.getChar(pos);
1079
1080                         if (IsPrintable(c)) {
1081                                 if (pos > endPosOfFontSpan) {
1082                                         // We need to get the next font
1083                                         font = getFont(pit, pos);
1084                                         endPosOfFontSpan = par.getEndPosOfFontSpan(pos);
1085                                 }
1086                                 if (! font.language()->RightToLeft()) {
1087                                         maxwidth += font_metrics::width(c, font);
1088                                 } else {
1089                                         // Fall-back to normal case
1090                                         maxwidth += singleWidth(pit, pos, c);
1091                                         // And flush font cache
1092                                         endPosOfFontSpan = 0;
1093                                 }
1094                         } else {
1095                                 // Special handling of insets - are any larger?
1096                                 if (par.isInset(pos)) {
1097                                         LyXFont const tmpfont = getFont(pit, pos);
1098                                         InsetOld const * tmpinset = par.getInset(pos);
1099                                         if (tmpinset) {
1100 #if 1 // this is needed for deep update on initialitation
1101 #warning inset->update FIXME
1102                                                 //tmpinset->update(bv());
1103                                                 Dimension dim;
1104                                                 MetricsInfo mi(bv(), tmpfont, workWidth());
1105                                                 tmpinset->metrics(mi, dim);
1106                                                 maxwidth += dim.wid;
1107                                                 maxasc = max(maxasc, dim.asc);
1108                                                 maxdesc = max(maxdesc, dim.des);
1109 #else
1110                                                 maxwidth += tmpinset->width();
1111                                                 maxasc = max(maxasc, tmpinset->ascent());
1112                                                 maxdesc = max(maxdesc, tmpinset->descent());
1113 #endif
1114                                         }
1115                                 } else {
1116                                         // Fall-back to normal case
1117                                         maxwidth += singleWidth(pit, pos, c);
1118                                         // And flush font cache
1119                                         endPosOfFontSpan = 0;
1120                                 }
1121                         }
1122                 }
1123         }
1124
1125         // Check if any custom fonts are larger (Asger)
1126         // This is not completely correct, but we can live with the small,
1127         // cosmetic error for now.
1128         LyXFont::FONT_SIZE maxsize =
1129                 pit->highestFontInRange(rit->pos(), pos_end, size);
1130         if (maxsize > font.size()) {
1131                 font.setSize(maxsize);
1132                 maxasc = max(maxasc, font_metrics::maxAscent(font));
1133                 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
1134         }
1135
1136         // This is nicer with box insets:
1137         ++maxasc;
1138         ++maxdesc;
1139
1140         rit->ascent_of_text(maxasc);
1141
1142         // is it a top line?
1143         if (!rit->pos()) {
1144
1145                 // some parksips VERY EASY IMPLEMENTATION
1146                 if (bv()->buffer()->params.paragraph_separation ==
1147                         BufferParams::PARSEP_SKIP)
1148                 {
1149                         if (layout->isParagraph()
1150                                 && pit->getDepth() == 0
1151                                 && pit != ownerParagraphs().begin())
1152                         {
1153                                 maxasc += bv()->buffer()->params.getDefSkip().inPixels(*bv());
1154                         } else if (pit != ownerParagraphs().begin() &&
1155                                    boost::prior(pit)->layout()->isParagraph() &&
1156                                    boost::prior(pit)->getDepth() == 0)
1157                         {
1158                                 // is it right to use defskip here too? (AS)
1159                                 maxasc += bv()->buffer()->params.getDefSkip().inPixels(*bv());
1160                         }
1161                 }
1162
1163                 // the top margin
1164                 if (pit == ownerParagraphs().begin() && !isInInset())
1165                         maxasc += PAPER_MARGIN;
1166
1167                 // add the vertical spaces, that the user added
1168                 maxasc += getLengthMarkerHeight(*bv(), pit->params().spaceTop());
1169
1170                 // do not forget the DTP-lines!
1171                 // there height depends on the font of the nearest character
1172                 if (pit->params().lineTop())
1173
1174                         maxasc += 2 * font_metrics::ascent('x', getFont(pit, 0));
1175                 // and now the pagebreaks
1176                 if (pit->params().pagebreakTop())
1177                         maxasc += 3 * defaultRowHeight();
1178
1179                 if (pit->params().startOfAppendix())
1180                         maxasc += 3 * defaultRowHeight();
1181
1182                 // This is special code for the chapter, since the label of this
1183                 // layout is printed in an extra row
1184                 if (layout->labeltype == LABEL_COUNTER_CHAPTER
1185                         && bv()->buffer()->params.secnumdepth >= 0)
1186                 {
1187                         float spacing_val = 1.0;
1188                         if (!pit->params().spacing().isDefault()) {
1189                                 spacing_val = pit->params().spacing().getValue();
1190                         } else {
1191                                 spacing_val = bv()->buffer()->params.spacing.getValue();
1192                         }
1193
1194                         labeladdon = int(font_metrics::maxDescent(labelfont) *
1195                                          layout->spacing.getValue() *
1196                                          spacing_val)
1197                                 + int(font_metrics::maxAscent(labelfont) *
1198                                       layout->spacing.getValue() *
1199                                       spacing_val);
1200                 }
1201
1202                 // special code for the top label
1203                 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1204                      || layout->labeltype == LABEL_BIBLIO
1205                      || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1206                     && isFirstInSequence(pit, ownerParagraphs())
1207                     && !pit->getLabelstring().empty())
1208                 {
1209                         float spacing_val = 1.0;
1210                         if (!pit->params().spacing().isDefault()) {
1211                                 spacing_val = pit->params().spacing().getValue();
1212                         } else {
1213                                 spacing_val = bv()->buffer()->params.spacing.getValue();
1214                         }
1215
1216                         labeladdon = int(
1217                                 (font_metrics::maxAscent(labelfont) *
1218                                  layout->spacing.getValue() *
1219                                  spacing_val)
1220                                 +(font_metrics::maxDescent(labelfont) *
1221                                   layout->spacing.getValue() *
1222                                   spacing_val)
1223                                 + layout->topsep * defaultRowHeight()
1224                                 + layout->labelbottomsep * defaultRowHeight());
1225                 }
1226
1227                 // And now the layout spaces, for example before and after
1228                 // a section, or between the items of a itemize or enumerate
1229                 // environment.
1230
1231                 if (!pit->params().pagebreakTop()) {
1232                         ParagraphList::iterator prev =
1233                                 depthHook(pit, ownerParagraphs(),
1234                                           pit->getDepth());
1235                         if (prev != pit && prev->layout() == layout &&
1236                                 prev->getDepth() == pit->getDepth() &&
1237                                 prev->getLabelWidthString() == pit->getLabelWidthString())
1238                         {
1239                                 layoutasc = (layout->itemsep * defaultRowHeight());
1240                         } else if (rit != rows().begin()) {
1241                                 tmptop = layout->topsep;
1242
1243                                 if (boost::prior(pit)->getDepth() >= pit->getDepth())
1244                                         tmptop -= boost::prior(rit)->par()->layout()->bottomsep;
1245
1246                                 if (tmptop > 0)
1247                                         layoutasc = (tmptop * defaultRowHeight());
1248                         } else if (pit->params().lineTop()) {
1249                                 tmptop = layout->topsep;
1250
1251                                 if (tmptop > 0)
1252                                         layoutasc = (tmptop * defaultRowHeight());
1253                         }
1254
1255                         prev = outerHook(pit, ownerParagraphs());
1256                         if (prev != ownerParagraphs().end())  {
1257                                 maxasc += int(prev->layout()->parsep * defaultRowHeight());
1258                         } else if (pit != ownerParagraphs().begin()) {
1259                                 ParagraphList::iterator prior_pit = boost::prior(pit);
1260                                 if (prior_pit->getDepth() != 0 ||
1261                                     prior_pit->layout() == layout) {
1262                                         maxasc += int(layout->parsep * defaultRowHeight());
1263                                 }
1264                         }
1265                 }
1266         }
1267
1268         // is it a bottom line?
1269         RowList::iterator next_rit = boost::next(rit);
1270         if (next_rit == rows().end() || next_rit->par() != pit) {
1271                 // the bottom margin
1272                 ParagraphList::iterator nextpit = boost::next(pit);
1273                 if (nextpit == ownerParagraphs().end() &&
1274                     !isInInset())
1275                         maxdesc += PAPER_MARGIN;
1276
1277                 // add the vertical spaces, that the user added
1278                 maxdesc += getLengthMarkerHeight(*bv(), pit->params().spaceBottom());
1279
1280                 // do not forget the DTP-lines!
1281                 // there height depends on the font of the nearest character
1282                 if (pit->params().lineBottom())
1283                         maxdesc += 2 * font_metrics::ascent('x',
1284                                         getFont(pit, max(pos_type(0), pit->size() - 1)));
1285
1286                 // and now the pagebreaks
1287                 if (pit->params().pagebreakBottom())
1288                         maxdesc += 3 * defaultRowHeight();
1289
1290                 // and now the layout spaces, for example before and after
1291                 // a section, or between the items of a itemize or enumerate
1292                 // environment
1293                 if (!pit->params().pagebreakBottom()
1294                     && nextpit != ownerParagraphs().end()) {
1295                         ParagraphList::iterator comparepit = pit;
1296                         float usual = 0;
1297                         float unusual = 0;
1298
1299                         if (comparepit->getDepth() > nextpit->getDepth()) {
1300                                 usual = (comparepit->layout()->bottomsep * defaultRowHeight());
1301                                 comparepit = depthHook(comparepit, ownerParagraphs(), nextpit->getDepth());
1302                                 if (comparepit->layout()!= nextpit->layout()
1303                                         || nextpit->getLabelWidthString() !=
1304                                         comparepit->getLabelWidthString())
1305                                 {
1306                                         unusual = (comparepit->layout()->bottomsep * defaultRowHeight());
1307                                 }
1308                                 if (unusual > usual)
1309                                         layoutdesc = unusual;
1310                                 else
1311                                         layoutdesc = usual;
1312                         } else if (comparepit->getDepth() ==  nextpit->getDepth()) {
1313
1314                                 if (comparepit->layout() != nextpit->layout()
1315                                         || nextpit->getLabelWidthString() !=
1316                                         comparepit->getLabelWidthString())
1317                                         layoutdesc = int(comparepit->layout()->bottomsep * defaultRowHeight());
1318                         }
1319                 }
1320         }
1321
1322         // incalculate the layout spaces
1323         maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
1324         maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
1325
1326         // calculate the new height of the text
1327         height -= rit->height();
1328
1329         rit->height(maxasc + maxdesc + labeladdon);
1330         rit->baseline(maxasc + labeladdon);
1331
1332         height += rit->height();
1333
1334         rit->top_of_text(rit->baseline() - font_metrics::maxAscent(font));
1335
1336         double x = 0;
1337         if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1338                 // this IS needed
1339                 rit->width(maxwidth);
1340                 double dummy;
1341                 prepareToPrint(rit, x, dummy, dummy, dummy, false);
1342         }
1343         rit->width(int(maxwidth + x));
1344         if (inset_owner) {
1345                 width = max(0, workWidth());
1346                 RowList::iterator it = rows().begin();
1347                 RowList::iterator end = rows().end();
1348                 for (; it != end; ++it)
1349                         if (it->width() > width)
1350                                 width = it->width();
1351         }
1352 }
1353
1354
1355 // Appends the implicit specified paragraph before the specified row,
1356 // start at the implicit given position
1357 void LyXText::appendParagraph(RowList::iterator rowit)
1358 {
1359         Assert(rowit != rowlist_.end());
1360
1361         pos_type const last = rowit->par()->size();
1362         bool done = false;
1363
1364         do {
1365                 pos_type z = rowBreakPoint(*rowit);
1366
1367                 RowList::iterator tmprow = rowit;
1368
1369                 if (z < last) {
1370                         ++z;
1371                         Row newrow(rowit->par(), z);
1372                         rowit = rowlist_.insert(boost::next(rowit), newrow);
1373                 } else {
1374                         done = true;
1375                 }
1376
1377                 // Set the dimensions of the row
1378                 // fixed fill setting now by calling inset->update() in
1379                 // SingleWidth when needed!
1380                 tmprow->fill(fill(tmprow, workWidth()));
1381                 setHeightOfRow(tmprow);
1382
1383         } while (!done);
1384 }
1385
1386
1387 void LyXText::breakAgain(RowList::iterator rit)
1388 {
1389         Assert(rit != rows().end());
1390
1391         bool not_ready = true;
1392
1393         do  {
1394                 pos_type z = rowBreakPoint(*rit);
1395                 RowList::iterator tmprit = rit;
1396                 RowList::iterator end = rows().end();
1397
1398                 if (z < rit->par()->size()) {
1399                         RowList::iterator next_rit = boost::next(rit);
1400
1401                         if (next_rit == end ||
1402                             (next_rit != end &&
1403                              next_rit->par() != rit->par())) {
1404                                 // insert a new row
1405                                 ++z;
1406                                 Row newrow(rit->par(), z);
1407                                 rit = rowlist_.insert(next_rit, newrow);
1408                         } else  {
1409                                 ++rit;
1410                                 ++z;
1411                                 if (rit->pos() == z)
1412                                         not_ready = false; // the rest will not change
1413                                 else {
1414                                         rit->pos(z);
1415                                 }
1416                         }
1417                 } else {
1418                         // if there are some rows too much, delete them
1419                         // only if you broke the whole paragraph!
1420                         RowList::iterator tmprit2 = rit;
1421                         while (boost::next(tmprit2) != end
1422                                && boost::next(tmprit2)->par() == rit->par()) {
1423                                 ++tmprit2;
1424                         }
1425                         while (tmprit2 != rit) {
1426                                 --tmprit2;
1427                                 removeRow(boost::next(tmprit2));
1428                         }
1429                         not_ready = false;
1430                 }
1431
1432                 // set the dimensions of the row
1433                 tmprit->fill(fill(tmprit, workWidth()));
1434                 setHeightOfRow(tmprit);
1435         } while (not_ready);
1436 }
1437
1438
1439 // this is just a little changed version of break again
1440 void LyXText::breakAgainOneRow(RowList::iterator rit)
1441 {
1442         Assert(rit != rows().end());
1443
1444         pos_type z = rowBreakPoint(*rit);
1445         RowList::iterator tmprit = rit;
1446         RowList::iterator end = rows().end();
1447
1448         if (z < rit->par()->size()) {
1449                 RowList::iterator next_rit = boost::next(rit);
1450
1451                 if (next_rit == end ||
1452                     (next_rit != end &&
1453                      next_rit->par() != rit->par())) {
1454                         // insert a new row
1455                         ++z;
1456                         Row newrow(rit->par(), z);
1457                         rit = rowlist_.insert(next_rit, newrow);
1458                 } else  {
1459                         ++rit;
1460                         ++z;
1461                         if (rit->pos() != z)
1462                                 rit->pos(z);
1463                 }
1464         } else {
1465                 // if there are some rows too much, delete them
1466                 // only if you broke the whole paragraph!
1467                 RowList::iterator tmprit2 = rit;
1468                 while (boost::next(tmprit2) != end
1469                        && boost::next(tmprit2)->par() == rit->par()) {
1470                         ++tmprit2;
1471                 }
1472                 while (tmprit2 != rit) {
1473                         --tmprit2;
1474                         removeRow(boost::next(tmprit2));
1475                 }
1476         }
1477
1478         // set the dimensions of the row
1479         tmprit->fill(fill(tmprit, workWidth()));
1480         setHeightOfRow(tmprit);
1481 }
1482
1483
1484 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
1485 {
1486         // allow only if at start or end, or all previous is new text
1487         if (cursor.pos() && cursor.pos() != cursor.par()->size()
1488                 && cursor.par()->isChangeEdited(0, cursor.pos()))
1489                 return;
1490
1491         LyXTextClass const & tclass =
1492                 bv()->buffer()->params.getLyXTextClass();
1493         LyXLayout_ptr const & layout = cursor.par()->layout();
1494
1495         // this is only allowed, if the current paragraph is not empty or caption
1496         // and if it has not the keepempty flag active
1497         if (cursor.par()->empty() && !cursor.par()->allowEmpty()
1498            && layout->labeltype != LABEL_SENSITIVE)
1499                 return;
1500
1501         recordUndo(bv(), Undo::ATOMIC, cursor.par());
1502
1503         // Always break behind a space
1504         //
1505         // It is better to erase the space (Dekel)
1506         if (cursor.pos() < cursor.par()->size()
1507              && cursor.par()->isLineSeparator(cursor.pos()))
1508            cursor.par()->erase(cursor.pos());
1509         // cursor.pos(cursor.pos() + 1);
1510
1511         // break the paragraph
1512         if (keep_layout)
1513                 keep_layout = 2;
1514         else
1515                 keep_layout = layout->isEnvironment();
1516
1517         // we need to set this before we insert the paragraph. IMO the
1518         // breakParagraph call should return a bool if it inserts the
1519         // paragraph before or behind and we should react on that one
1520         // but we can fix this in 1.3.0 (Jug 20020509)
1521         bool const isempty = (cursor.par()->allowEmpty() && cursor.par()->empty());
1522         ::breakParagraph(bv()->buffer()->params, paragraphs, cursor.par(),
1523                          cursor.pos(), keep_layout);
1524
1525         // well this is the caption hack since one caption is really enough
1526         if (layout->labeltype == LABEL_SENSITIVE) {
1527                 if (!cursor.pos())
1528                         // set to standard-layout
1529                         cursor.par()->applyLayout(tclass.defaultLayout());
1530                 else
1531                         // set to standard-layout
1532                         boost::next(cursor.par())->applyLayout(tclass.defaultLayout());
1533         }
1534
1535         // if the cursor is at the beginning of a row without prior newline,
1536         // move one row up!
1537         // This touches only the screen-update. Otherwise we would may have
1538         // an empty row on the screen
1539         if (cursor.pos() && cursorRow()->pos() == cursor.pos()
1540             && !cursorRow()->par()->isNewline(cursor.pos() - 1))
1541         {
1542                 cursorLeft(bv());
1543         }
1544
1545         postPaint();
1546
1547         removeParagraph(cursorRow());
1548
1549         // set the dimensions of the cursor row
1550         cursorRow()->fill(fill(cursorRow(), workWidth()));
1551
1552         setHeightOfRow(cursorRow());
1553
1554 #warning Trouble Point! (Lgb)
1555         // When ::breakParagraph is called from within an inset we must
1556         // ensure that the correct ParagraphList is used. Today that is not
1557         // the case and the Buffer::paragraphs is used. Not good. (Lgb)
1558         ParagraphList::iterator next_par = boost::next(cursor.par());
1559
1560         while (!next_par->empty() && next_par->isNewline(0))
1561                 next_par->erase(0);
1562
1563         insertParagraph(next_par, boost::next(cursorRow()));
1564         updateCounters();
1565
1566         // This check is necessary. Otherwise the new empty paragraph will
1567         // be deleted automatically. And it is more friendly for the user!
1568         if (cursor.pos() || isempty)
1569                 setCursor(next_par, 0);
1570         else
1571                 setCursor(cursor.par(), 0);
1572
1573         if (boost::next(cursorRow()) != rows().end())
1574                 breakAgain(boost::next(cursorRow()));
1575 }
1576
1577
1578 // Just a macro to make some thing easier.
1579 void LyXText::redoParagraph()
1580 {
1581         clearSelection();
1582         redoParagraph(cursor.par());
1583         setCursorIntern(cursor.par(), cursor.pos());
1584 }
1585
1586
1587 // insert a character, moves all the following breaks in the
1588 // same Paragraph one to the right and make a rebreak
1589 void LyXText::insertChar(char c)
1590 {
1591         recordUndo(bv(), Undo::INSERT, cursor.par());
1592
1593         // When the free-spacing option is set for the current layout,
1594         // disable the double-space checking
1595
1596         bool const freeSpacing = cursorRow()->par()->layout()->free_spacing ||
1597                 cursorRow()->par()->isFreeSpacing();
1598
1599         if (lyxrc.auto_number) {
1600                 static string const number_operators = "+-/*";
1601                 static string const number_unary_operators = "+-";
1602                 static string const number_seperators = ".,:";
1603
1604                 if (current_font.number() == LyXFont::ON) {
1605                         if (!IsDigit(c) && !contains(number_operators, c) &&
1606                             !(contains(number_seperators, c) &&
1607                               cursor.pos() >= 1 &&
1608                               cursor.pos() < cursor.par()->size() &&
1609                               getFont(cursor.par(), cursor.pos()).number() == LyXFont::ON &&
1610                               getFont(cursor.par(), cursor.pos() - 1).number() == LyXFont::ON)
1611                            )
1612                                 number(bv()); // Set current_font.number to OFF
1613                 } else if (IsDigit(c) &&
1614                            real_current_font.isVisibleRightToLeft()) {
1615                         number(bv()); // Set current_font.number to ON
1616
1617                         if (cursor.pos() > 0) {
1618                                 char const c = cursor.par()->getChar(cursor.pos() - 1);
1619                                 if (contains(number_unary_operators, c) &&
1620                                     (cursor.pos() == 1 ||
1621                                      cursor.par()->isSeparator(cursor.pos() - 2) ||
1622                                      cursor.par()->isNewline(cursor.pos() - 2))
1623                                   ) {
1624                                         setCharFont(
1625                                                     cursor.par(),
1626                                                     cursor.pos() - 1,
1627                                                     current_font);
1628                                 } else if (contains(number_seperators, c) &&
1629                                            cursor.pos() >= 2 &&
1630                                            getFont(
1631                                                    cursor.par(),
1632                                                    cursor.pos() - 2).number() == LyXFont::ON) {
1633                                         setCharFont(
1634                                                     cursor.par(),
1635                                                     cursor.pos() - 1,
1636                                                     current_font);
1637                                 }
1638                         }
1639                 }
1640         }
1641
1642
1643         // First check, if there will be two blanks together or a blank at
1644         // the beginning of a paragraph.
1645         // I decided to handle blanks like normal characters, the main
1646         // difference are the special checks when calculating the row.fill
1647         // (blank does not count at the end of a row) and the check here
1648
1649         // The bug is triggered when we type in a description environment:
1650         // The current_font is not changed when we go from label to main text
1651         // and it should (along with realtmpfont) when we type the space.
1652         // CHECK There is a bug here! (Asger)
1653
1654         LyXFont realtmpfont = real_current_font;
1655         LyXFont rawtmpfont = current_font;
1656         // store the current font.  This is because of the use of cursor
1657         // movements. The moving cursor would refresh the current font
1658
1659         // Get the font that is used to calculate the baselineskip
1660         pos_type const lastpos = cursor.par()->size();
1661         LyXFont rawparfont =
1662                 cursor.par()->getFontSettings(bv()->buffer()->params,
1663                                               lastpos - 1);
1664
1665         bool jumped_over_space = false;
1666
1667         if (!freeSpacing && IsLineSeparatorChar(c)) {
1668                 if ((cursor.pos() > 0
1669                      && cursor.par()->isLineSeparator(cursor.pos() - 1))
1670                     || (cursor.pos() > 0
1671                         && cursor.par()->isNewline(cursor.pos() - 1))
1672                     || (cursor.pos() == 0)) {
1673                         static bool sent_space_message = false;
1674                         if (!sent_space_message) {
1675                                 if (cursor.pos() == 0)
1676                                         bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1677                                 else
1678                                         bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1679                                 sent_space_message = true;
1680                         }
1681                         charInserted();
1682                         return;
1683                 }
1684         }
1685
1686         // the display inset stuff
1687         if (cursorRow()->pos() < cursorRow()->par()->size()
1688             && cursorRow()->par()->isInset(cursorRow()->pos())) {
1689                 InsetOld * inset = cursorRow()->par()->getInset(cursorRow()->pos());
1690                 if (inset && (inset->display() || inset->needFullRow())) {
1691                         // force a new break
1692                         cursorRow()->fill(-1); // to force a new break
1693                 }
1694         }
1695
1696         // get the cursor row fist
1697         RowList::iterator row = cursorRow();
1698         if (c != Paragraph::META_INSET) {
1699                 // Here case LyXText::InsertInset  already inserted the character
1700                 cursor.par()->insertChar(cursor.pos(), c);
1701         }
1702         setCharFont(cursor.par(), cursor.pos(), rawtmpfont);
1703
1704         if (!jumped_over_space) {
1705                 // refresh the positions
1706                 RowList::iterator tmprow = row;
1707                 while (boost::next(tmprow) != rows().end() &&
1708                        boost::next(tmprow)->par() == row->par()) {
1709                         ++tmprow;
1710                         tmprow->pos(tmprow->pos() + 1);
1711                 }
1712         }
1713
1714         // Is there a break one row above
1715         if (row != rows().begin() &&
1716             boost::prior(row)->par() == row->par()
1717             && (cursor.par()->isLineSeparator(cursor.pos())
1718                 || cursor.par()->isNewline(cursor.pos())
1719                 || ((cursor.pos() + 1 < cursor.par()->size()) &&
1720                     cursor.par()->isInset(cursor.pos() + 1))
1721                 || cursorRow()->fill() == -1))
1722         {
1723                 pos_type z = rowBreakPoint(*boost::prior(row));
1724
1725                 if (z >= row->pos()) {
1726                         row->pos(z + 1);
1727
1728                         // set the dimensions of the row above
1729                         boost::prior(row)->fill(fill(
1730                                                    boost::prior(row),
1731                                                    workWidth()));
1732
1733                         setHeightOfRow(boost::prior(row));
1734
1735                         postPaint();
1736
1737                         breakAgainOneRow(row);
1738
1739                         current_font = rawtmpfont;
1740                         real_current_font = realtmpfont;
1741                         setCursor(cursor.par(), cursor.pos() + 1,
1742                                   false, cursor.boundary());
1743
1744                         // cursor MUST be in row now.
1745                         // check, wether the last characters font has changed.
1746                         if (cursor.pos() && cursor.pos() == cursor.par()->size()
1747                             && rawparfont != rawtmpfont)
1748                                 redoHeightOfParagraph();
1749
1750                         charInserted();
1751                         return;
1752                 }
1753         }
1754
1755         // recalculate the fill of the row
1756         if (row->fill() >= 0) {
1757                 // needed because a newline will set fill to -1. Otherwise
1758                 // we would not get a rebreak!
1759                 row->fill(fill(row, workWidth()));
1760         }
1761
1762         if (c == Paragraph::META_INSET || row->fill() < 0) {
1763                 postPaint();
1764                 breakAgainOneRow(row);
1765
1766                 RowList::iterator next_row = boost::next(row);
1767
1768                 // will the cursor be in another row now?
1769                 if (lastPos(*this, row) <= cursor.pos() + 1 &&
1770                     next_row != rows().end()) {
1771                         if (next_row != rows().end() &&
1772                             next_row->par() == row->par()) {
1773                                 // this should always be true
1774                                 ++row;
1775                         }
1776
1777                         breakAgainOneRow(row);
1778                 }
1779                 current_font = rawtmpfont;
1780                 real_current_font = realtmpfont;
1781
1782                 setCursor(cursor.par(), cursor.pos() + 1, false,
1783                           cursor.boundary());
1784                 if (isBoundary(bv()->buffer(), *cursor.par(), cursor.pos())
1785                     != cursor.boundary())
1786                         setCursor(cursor.par(), cursor.pos(), false,
1787                           !cursor.boundary());
1788         } else {
1789                 // FIXME: similar code is duplicated all over - make resetHeightOfRow
1790                 setHeightOfRow(row);
1791                 postPaint();
1792
1793                 current_font = rawtmpfont;
1794                 real_current_font = realtmpfont;
1795                 setCursor(cursor.par(), cursor.pos() + 1, false,
1796                           cursor.boundary());
1797         }
1798
1799         // check, wether the last characters font has changed.
1800         if (cursor.pos() && cursor.pos() == cursor.par()->size()
1801             && rawparfont != rawtmpfont) {
1802                 redoHeightOfParagraph();
1803         }
1804
1805         charInserted();
1806 }
1807
1808
1809 void LyXText::charInserted()
1810 {
1811         // Here we could call FinishUndo for every 20 characters inserted.
1812         // This is from my experience how emacs does it. (Lgb)
1813         static unsigned int counter;
1814         if (counter < 20) {
1815                 ++counter;
1816         } else {
1817                 finishUndo();
1818                 counter = 0;
1819         }
1820 }
1821
1822
1823 void LyXText::prepareToPrint(RowList::iterator rit, double & x,
1824                              double & fill_separator,
1825                              double & fill_hfill,
1826                              double & fill_label_hfill,
1827                              bool bidi) const
1828 {
1829         double w = rit->fill();
1830         fill_hfill = 0;
1831         fill_label_hfill = 0;
1832         fill_separator = 0;
1833         fill_label_hfill = 0;
1834
1835         ParagraphList::iterator pit = rit->par();
1836
1837         bool const is_rtl =
1838                 pit->isRightToLeftPar(bv()->buffer()->params);
1839         if (is_rtl)
1840                 x = workWidth() > 0 ? rightMargin(*bv()->buffer(), *rit) : 0;
1841         else
1842                 x = workWidth() > 0 ? leftMargin(*rit) : 0;
1843
1844         // is there a manual margin with a manual label
1845         LyXLayout_ptr const & layout = pit->layout();
1846
1847         if (layout->margintype == MARGIN_MANUAL
1848             && layout->labeltype == LABEL_MANUAL) {
1849                 /// We might have real hfills in the label part
1850                 int nlh = numberOfLabelHfills(*this, rit);
1851
1852                 // A manual label par (e.g. List) has an auto-hfill
1853                 // between the label text and the body of the
1854                 // paragraph too.
1855                 // But we don't want to do this auto hfill if the par
1856                 // is empty.
1857                 if (!pit->empty())
1858                         ++nlh;
1859
1860                 if (nlh && !pit->getLabelWidthString().empty()) {
1861                         fill_label_hfill = labelFill(*rit) / double(nlh);
1862                 }
1863         }
1864
1865         // are there any hfills in the row?
1866         int const nh = numberOfHfills(*this, rit);
1867
1868         if (nh) {
1869                 if (w > 0)
1870                         fill_hfill = w / nh;
1871         // we don't have to look at the alignment if it is ALIGN_LEFT and
1872         // if the row is already larger then the permitted width as then
1873         // we force the LEFT_ALIGN'edness!
1874         } else if (int(rit->width()) < workWidth()) {
1875                 // is it block, flushleft or flushright?
1876                 // set x how you need it
1877                 int align;
1878                 if (pit->params().align() == LYX_ALIGN_LAYOUT) {
1879                         align = layout->align;
1880                 } else {
1881                         align = pit->params().align();
1882                 }
1883
1884                 // center displayed insets
1885                 InsetOld * inset = 0;
1886                 if (rit->pos() < pit->size()
1887                     && pit->isInset(rit->pos())
1888                     && (inset = pit->getInset(rit->pos()))
1889                     && (inset->display())) // || (inset->scroll() < 0)))
1890                     align = (inset->lyxCode() == InsetOld::MATHMACRO_CODE)
1891                         ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
1892                 // ERT insets should always be LEFT ALIGNED on screen
1893                 inset = pit->inInset();
1894                 if (inset && inset->owner() &&
1895                         inset->owner()->lyxCode() == InsetOld::ERT_CODE)
1896                 {
1897                         align = LYX_ALIGN_LEFT;
1898                 }
1899
1900                 switch (align) {
1901             case LYX_ALIGN_BLOCK:
1902             {
1903                         int const ns = numberOfSeparators(*this, rit);
1904                         RowList::iterator next_row = boost::next(rit);
1905                         ParagraphList::iterator next_pit = next_row->par();
1906
1907                         if (ns && next_row != rowlist_.end() &&
1908                             next_pit == pit &&
1909                             !(next_pit->isNewline(next_row->pos() - 1))
1910                             && !(next_pit->isInset(next_row->pos()) &&
1911                                  next_pit->getInset(next_row->pos()) &&
1912                                  next_pit->getInset(next_row->pos())->display())
1913                                 ) {
1914                                 fill_separator = w / ns;
1915                         } else if (is_rtl) {
1916                                 x += w;
1917                         }
1918                         break;
1919             }
1920             case LYX_ALIGN_RIGHT:
1921                         x += w;
1922                         break;
1923             case LYX_ALIGN_CENTER:
1924                         x += w / 2;
1925                         break;
1926                 }
1927         }
1928         if (!bidi)
1929                 return;
1930
1931         computeBidiTables(bv()->buffer(), rit);
1932         if (is_rtl) {
1933                 pos_type body_pos = pit->beginningOfBody();
1934                 pos_type last = lastPos(*this, rit);
1935
1936                 if (body_pos > 0 &&
1937                     (body_pos - 1 > last ||
1938                      !pit->isLineSeparator(body_pos - 1))) {
1939                         x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1940                         if (body_pos - 1 <= last)
1941                                 x += fill_label_hfill;
1942                 }
1943         }
1944 }
1945
1946
1947 // important for the screen
1948
1949
1950 // the cursor set functions have a special mechanism. When they
1951 // realize, that you left an empty paragraph, they will delete it.
1952 // They also delete the corresponding row
1953
1954 void LyXText::cursorRightOneWord()
1955 {
1956         ::cursorRightOneWord(cursor, ownerParagraphs());
1957         setCursor(cursor.par(), cursor.pos());
1958 }
1959
1960
1961 // Skip initial whitespace at end of word and move cursor to *start*
1962 // of prior word, not to end of next prior word.
1963 void LyXText::cursorLeftOneWord()
1964 {
1965         LyXCursor tmpcursor = cursor;
1966         ::cursorLeftOneWord(tmpcursor, ownerParagraphs());
1967         setCursor(tmpcursor.par(), tmpcursor.pos());
1968 }
1969
1970
1971 void LyXText::selectWord(word_location loc)
1972 {
1973         LyXCursor from = cursor;
1974         LyXCursor to;
1975         ::getWord(from, to, loc, ownerParagraphs());
1976         if (cursor != from)
1977                 setCursor(from.par(), from.pos());
1978         if (to == from)
1979                 return;
1980         selection.cursor = cursor;
1981         setCursor(to.par(), to.pos());
1982         setSelection();
1983 }
1984
1985
1986 // Select the word currently under the cursor when no
1987 // selection is currently set
1988 bool LyXText::selectWordWhenUnderCursor(word_location loc)
1989 {
1990         if (!selection.set()) {
1991                 selectWord(loc);
1992                 return selection.set();
1993         }
1994         return false;
1995 }
1996
1997
1998 void LyXText::acceptChange()
1999 {
2000         if (!selection.set() && cursor.par()->size())
2001                 return;
2002
2003         if (selection.start.par() == selection.end.par()) {
2004                 LyXCursor & startc = selection.start;
2005                 LyXCursor & endc = selection.end;
2006                 recordUndo(bv(), Undo::INSERT, startc.par());
2007                 startc.par()->acceptChange(startc.pos(), endc.pos());
2008                 finishUndo();
2009                 clearSelection();
2010                 redoParagraph(startc.par());
2011                 setCursorIntern(startc.par(), 0);
2012         }
2013 #warning handle multi par selection
2014 }
2015
2016
2017 void LyXText::rejectChange()
2018 {
2019         if (!selection.set() && cursor.par()->size())
2020                 return;
2021
2022         if (selection.start.par() == selection.end.par()) {
2023                 LyXCursor & startc = selection.start;
2024                 LyXCursor & endc = selection.end;
2025                 recordUndo(bv(), Undo::INSERT, startc.par());
2026                 startc.par()->rejectChange(startc.pos(), endc.pos());
2027                 finishUndo();
2028                 clearSelection();
2029                 redoParagraph(startc.par());
2030                 setCursorIntern(startc.par(), 0);
2031         }
2032 #warning handle multi par selection
2033 }
2034
2035
2036 // This function is only used by the spellchecker for NextWord().
2037 // It doesn't handle LYX_ACCENTs and probably never will.
2038 WordLangTuple const
2039 LyXText::selectNextWordToSpellcheck(float & value)
2040 {
2041         if (the_locking_inset) {
2042                 WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bv(), value);
2043                 if (!word.word().empty()) {
2044                         value += float(cursor.y());
2045                         value /= float(height);
2046                         return word;
2047                 }
2048                 // we have to go on checking so move cursor to the next char
2049                 if (cursor.pos() == cursor.par()->size()) {
2050                         if (boost::next(cursor.par()) == ownerParagraphs().end())
2051                                 return word;
2052                         cursor.par(boost::next(cursor.par()));
2053                         cursor.pos(0);
2054                 } else
2055                         cursor.pos(cursor.pos() + 1);
2056         }
2057         ParagraphList::iterator tmppit = cursor.par();
2058
2059         // If this is not the very first word, skip rest of
2060         // current word because we are probably in the middle
2061         // of a word if there is text here.
2062         if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
2063                 while (cursor.pos() < cursor.par()->size()
2064                        && cursor.par()->isLetter(cursor.pos()))
2065                         cursor.pos(cursor.pos() + 1);
2066         }
2067
2068         // Now, skip until we have real text (will jump paragraphs)
2069         while (true) {
2070                 ParagraphList::iterator cpit = cursor.par();
2071                 pos_type const cpos(cursor.pos());
2072
2073                 if (cpos == cpit->size()) {
2074                         if (boost::next(cpit) != ownerParagraphs().end()) {
2075                                 cursor.par(boost::next(cpit));
2076                                 cursor.pos(0);
2077                                 continue;
2078                         }
2079                         break;
2080                 }
2081
2082                 bool const is_good_inset = cpit->isInset(cpos)
2083                         && cpit->getInset(cpos)->allowSpellcheck();
2084
2085                 if (!isDeletedText(*cpit, cpos)
2086                     && (is_good_inset || cpit->isLetter(cpos)))
2087                         break;
2088
2089                 cursor.pos(cpos + 1);
2090         }
2091
2092         // now check if we hit an inset so it has to be a inset containing text!
2093         if (cursor.pos() < cursor.par()->size() &&
2094             cursor.par()->isInset(cursor.pos())) {
2095                 // lock the inset!
2096                 FuncRequest cmd(bv(), LFUN_INSET_EDIT, "left");
2097                 cursor.par()->getInset(cursor.pos())->localDispatch(cmd);
2098                 // now call us again to do the above trick
2099                 // but obviously we have to start from down below ;)
2100                 return bv()->text->selectNextWordToSpellcheck(value);
2101         }
2102
2103         // Update the value if we changed paragraphs
2104         if (cursor.par() != tmppit) {
2105                 setCursor(cursor.par(), cursor.pos());
2106                 value = float(cursor.y())/float(height);
2107         }
2108
2109         // Start the selection from here
2110         selection.cursor = cursor;
2111
2112         string lang_code = getFont(cursor.par(), cursor.pos()).language()->code();
2113         // and find the end of the word (insets like optional hyphens
2114         // and ligature break are part of a word)
2115         while (cursor.pos() < cursor.par()->size()
2116                && cursor.par()->isLetter(cursor.pos())
2117                && !isDeletedText(*cursor.par(), cursor.pos()))
2118                 cursor.pos(cursor.pos() + 1);
2119
2120         // Finally, we copy the word to a string and return it
2121         string str;
2122         if (selection.cursor.pos() < cursor.pos()) {
2123                 pos_type i;
2124                 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2125                         if (!cursor.par()->isInset(i))
2126                                 str += cursor.par()->getChar(i);
2127                 }
2128         }
2129         return WordLangTuple(str, lang_code);
2130 }
2131
2132
2133 // This one is also only for the spellchecker
2134 void LyXText::selectSelectedWord()
2135 {
2136         if (the_locking_inset) {
2137                 the_locking_inset->selectSelectedWord(bv());
2138                 return;
2139         }
2140         // move cursor to the beginning
2141         setCursor(selection.cursor.par(), selection.cursor.pos());
2142
2143         // set the sel cursor
2144         selection.cursor = cursor;
2145
2146         // now find the end of the word
2147         while (cursor.pos() < cursor.par()->size()
2148                && (cursor.par()->isLetter(cursor.pos())))
2149                 cursor.pos(cursor.pos() + 1);
2150
2151         setCursor(cursor.par(), cursor.pos());
2152
2153         // finally set the selection
2154         setSelection();
2155 }
2156
2157
2158 // Delete from cursor up to the end of the current or next word.
2159 void LyXText::deleteWordForward()
2160 {
2161         if (cursor.par()->empty())
2162                 cursorRight(bv());
2163         else {
2164                 LyXCursor tmpcursor = cursor;
2165                 selection.set(true); // to avoid deletion
2166                 cursorRightOneWord();
2167                 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
2168                 selection.cursor = cursor;
2169                 cursor = tmpcursor;
2170                 setSelection();
2171
2172                 // Great, CutSelection() gets rid of multiple spaces.
2173                 cutSelection(true, false);
2174         }
2175 }
2176
2177
2178 // Delete from cursor to start of current or prior word.
2179 void LyXText::deleteWordBackward()
2180 {
2181         if (cursor.par()->empty())
2182                 cursorLeft(bv());
2183         else {
2184                 LyXCursor tmpcursor = cursor;
2185                 selection.set(true); // to avoid deletion
2186                 cursorLeftOneWord();
2187                 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
2188                 selection.cursor = cursor;
2189                 cursor = tmpcursor;
2190                 setSelection();
2191                 cutSelection(true, false);
2192         }
2193 }
2194
2195
2196 // Kill to end of line.
2197 void LyXText::deleteLineForward()
2198 {
2199         if (cursor.par()->empty())
2200                 // Paragraph is empty, so we just go to the right
2201                 cursorRight(bv());
2202         else {
2203                 LyXCursor tmpcursor = cursor;
2204                 // We can't store the row over a regular setCursor
2205                 // so we set it to 0 and reset it afterwards.
2206                 selection.set(true); // to avoid deletion
2207                 cursorEnd();
2208                 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
2209                 selection.cursor = cursor;
2210                 cursor = tmpcursor;
2211                 setSelection();
2212                 // What is this test for ??? (JMarc)
2213                 if (!selection.set()) {
2214                         deleteWordForward();
2215                 } else {
2216                         cutSelection(true, false);
2217                 }
2218         }
2219 }
2220
2221
2222 void LyXText::changeCase(LyXText::TextCase action)
2223 {
2224         LyXCursor from;
2225         LyXCursor to;
2226
2227         if (selection.set()) {
2228                 from = selection.start;
2229                 to = selection.end;
2230         } else {
2231                 from = cursor;
2232                 ::getWord(from, to, lyx::PARTIAL_WORD, ownerParagraphs());
2233                 setCursor(to.par(), to.pos() + 1);
2234         }
2235
2236         recordUndo(bv(), Undo::ATOMIC, from.par(), to.par());
2237
2238         pos_type pos = from.pos();
2239         ParagraphList::iterator pit = from.par();
2240
2241         while (pit != ownerParagraphs().end() &&
2242                (pos != to.pos() || pit != to.par())) {
2243                 if (pos == pit->size()) {
2244                         ++pit;
2245                         pos = 0;
2246                         continue;
2247                 }
2248                 unsigned char c = pit->getChar(pos);
2249                 if (!IsInsetChar(c)) {
2250                         switch (action) {
2251                         case text_lowercase:
2252                                 c = lowercase(c);
2253                                 break;
2254                         case text_capitalization:
2255                                 c = uppercase(c);
2256                                 action = text_lowercase;
2257                                 break;
2258                         case text_uppercase:
2259                                 c = uppercase(c);
2260                                 break;
2261                         }
2262                 }
2263 #warning changes
2264                 pit->setChar(pos, c);
2265                 checkParagraph(pit, pos);
2266
2267                 ++pos;
2268         }
2269
2270         if (getRow(to) != getRow(from))
2271                 postPaint();
2272 }
2273
2274
2275 void LyXText::Delete()
2276 {
2277         // this is a very easy implementation
2278
2279         LyXCursor old_cursor = cursor;
2280         int const old_cur_par_id = old_cursor.par()->id();
2281         int const old_cur_par_prev_id =
2282                 (old_cursor.par() != ownerParagraphs().begin() ?
2283                  boost::prior(old_cursor.par())->id() : -1);
2284
2285         // just move to the right
2286         cursorRight(bv());
2287
2288         // CHECK Look at the comment here.
2289         // This check is not very good...
2290         // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2291         // and that can very well delete the par or par->previous in
2292         // old_cursor. Will a solution where we compare paragraph id's
2293         //work better?
2294         if ((cursor.par() != ownerParagraphs().begin() ? boost::prior(cursor.par())->id() : -1)
2295             == old_cur_par_prev_id
2296             && cursor.par()->id() != old_cur_par_id) {
2297                 // delete-empty-paragraph-mechanism has done it
2298                 return;
2299         }
2300
2301         // if you had success make a backspace
2302         if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2303                 LyXCursor tmpcursor = cursor;
2304                 // to make sure undo gets the right cursor position
2305                 cursor = old_cursor;
2306                 recordUndo(bv(), Undo::DELETE, cursor.par());
2307                 cursor = tmpcursor;
2308                 backspace();
2309         }
2310 }
2311
2312
2313 void LyXText::backspace()
2314 {
2315         // Get the font that is used to calculate the baselineskip
2316         pos_type lastpos = cursor.par()->size();
2317         LyXFont rawparfont =
2318                 cursor.par()->getFontSettings(bv()->buffer()->params,
2319                                               lastpos - 1);
2320
2321         if (cursor.pos() == 0) {
2322                 // The cursor is at the beginning of a paragraph,
2323                 // so the the backspace will collapse two paragraphs into one.
2324
2325                 // but it's not allowed unless it's new
2326                 if (cursor.par()->isChangeEdited(0, cursor.par()->size()))
2327                         return;
2328
2329                 // we may paste some paragraphs
2330
2331                 // is it an empty paragraph?
2332
2333                 if ((lastpos == 0
2334                      || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2335                         // This is an empty paragraph and we delete it just by moving the cursor one step
2336                         // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2337                         // of the paragraph.
2338
2339                         if (cursor.par() != ownerParagraphs().begin()) {
2340                                 ParagraphList::iterator tmppit = boost::prior(cursor.par());
2341                                 if (cursor.par()->layout() == tmppit->layout()
2342                                     && cursor.par()->getAlign() == tmppit->getAlign()) {
2343                                         // Inherit bottom DTD from the paragraph below.
2344                                         // (the one we are deleting)
2345                                         tmppit->params().lineBottom(cursor.par()->params().lineBottom());
2346                                         tmppit->params().spaceBottom(cursor.par()->params().spaceBottom());
2347                                         tmppit->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2348                                 }
2349
2350                                 cursorLeft(bv());
2351
2352                                 // the layout things can change the height of a row !
2353                                 int const tmpheight = cursorRow()->height();
2354                                 setHeightOfRow(cursorRow());
2355                                 if (cursorRow()->height() != tmpheight)
2356                                         postPaint();
2357                                 return;
2358                         }
2359                 }
2360
2361                 if (cursor.par() != ownerParagraphs().begin()) {
2362                         recordUndo(bv(), Undo::DELETE,
2363                                 boost::prior(cursor.par()),
2364                                 cursor.par());
2365                 }
2366
2367                 ParagraphList::iterator tmppit = cursor.par();
2368                 RowList::iterator tmprow = cursorRow();
2369
2370                 // We used to do cursorLeftIntern() here, but it is
2371                 // not a good idea since it triggers the auto-delete
2372                 // mechanism. So we do a cursorLeftIntern()-lite,
2373                 // without the dreaded mechanism. (JMarc)
2374                 if (cursor.par() != ownerParagraphs().begin()) {
2375                         // steps into the above paragraph.
2376                         setCursorIntern(boost::prior(cursor.par()),
2377                                         boost::prior(cursor.par())->size(),
2378                                         false);
2379                 }
2380
2381                 // Pasting is not allowed, if the paragraphs have different
2382                 // layout. I think it is a real bug of all other
2383                 // word processors to allow it. It confuses the user.
2384                 // Even so with a footnote paragraph and a non-footnote
2385                 // paragraph. I will not allow pasting in this case,
2386                 // because the user would be confused if the footnote behaves
2387                 // different wether it is open or closed.
2388
2389                 //      Correction: Pasting is always allowed with standard-layout
2390                 LyXTextClass const & tclass =
2391                         bv()->buffer()->params.getLyXTextClass();
2392
2393                 if (cursor.par() != tmppit
2394                     && (cursor.par()->layout() == tmppit->layout()
2395                         || tmppit->layout() == tclass.defaultLayout())
2396                     && cursor.par()->getAlign() == tmppit->getAlign()) {
2397                         removeParagraph(tmprow);
2398                         removeRow(tmprow);
2399                         mergeParagraph(bv()->buffer()->params, bv()->buffer()->paragraphs, cursor.par());
2400
2401                         if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2402                                 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2403                         // strangely enough it seems that commenting out the line above removes
2404                         // most or all of the segfaults. I will however also try to move the
2405                         // two Remove... lines in front of the PasteParagraph too.
2406                         else
2407                                 if (cursor.pos())
2408                                         cursor.pos(cursor.pos() - 1);
2409
2410                         postPaint();
2411
2412                         // remove the lost paragraph
2413                         // This one is not safe, since the paragraph that the tmprow and the
2414                         // following rows belong to has been deleted by the PasteParagraph
2415                         // above. The question is... could this be moved in front of the
2416                         // PasteParagraph?
2417                         //RemoveParagraph(tmprow);
2418                         //RemoveRow(tmprow);
2419
2420                         // This rebuilds the rows.
2421                         appendParagraph(cursorRow());
2422                         updateCounters();
2423
2424                         // the row may have changed, block, hfills etc.
2425                         setCursor(cursor.par(), cursor.pos(), false);
2426                 }
2427         } else {
2428                 // this is the code for a normal backspace, not pasting
2429                 // any paragraphs
2430                 recordUndo(bv(), Undo::DELETE, cursor.par());
2431                 // We used to do cursorLeftIntern() here, but it is
2432                 // not a good idea since it triggers the auto-delete
2433                 // mechanism. So we do a cursorLeftIntern()-lite,
2434                 // without the dreaded mechanism. (JMarc)
2435                 setCursorIntern(cursor.par(), cursor.pos()- 1,
2436                                 false, cursor.boundary());
2437
2438                 if (cursor.par()->isInset(cursor.pos())) {
2439                         // force complete redo when erasing display insets
2440                         // this is a cruel method but safe..... Matthias
2441                         if (cursor.par()->getInset(cursor.pos())->display() ||
2442                             cursor.par()->getInset(cursor.pos())->needFullRow()) {
2443                                 cursor.par()->erase(cursor.pos());
2444                                 redoParagraph();
2445                                 return;
2446                         }
2447                 }
2448
2449                 RowList::iterator row = cursorRow();
2450                 int y = cursor.y() - row->baseline();
2451                 pos_type z;
2452                 // remember that a space at the end of a row doesnt count
2453                 // when calculating the fill
2454                 if (cursor.pos() < lastPos(*this, row) ||
2455                     !cursor.par()->isLineSeparator(cursor.pos())) {
2456                         row->fill(row->fill() + singleWidth(
2457                                                             cursor.par(),
2458                                                             cursor.pos()));
2459                 }
2460
2461                 // some special code when deleting a newline. This is similar
2462                 // to the behavior when pasting paragraphs
2463                 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2464                         cursor.par()->erase(cursor.pos());
2465                         // refresh the positions
2466                         RowList::iterator tmprow = row;
2467                         while (boost::next(tmprow) != rows().end() &&
2468                                boost::next(tmprow)->par() == row->par()) {
2469                                 ++tmprow;
2470                                 tmprow->pos(tmprow->pos() - 1);
2471                         }
2472                         if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2473                                 cursor.pos(cursor.pos() - 1);
2474
2475                         if (cursor.pos() < cursor.par()->size()
2476                             && !cursor.par()->isSeparator(cursor.pos())) {
2477                                 cursor.par()->insertChar(cursor.pos(), ' ');
2478                                 setCharFont(cursor.par(), cursor.pos(), current_font);
2479                                 // refresh the positions
2480                                 tmprow = row;
2481                                 while (boost::next(tmprow) != rows().end() &&
2482                                        boost::next(tmprow)->par() == row->par()) {
2483                                         ++tmprow;
2484                                         tmprow->pos(tmprow->pos() + 1);
2485                                 }
2486                         }
2487                 } else {
2488                         cursor.par()->erase(cursor.pos());
2489
2490                         // refresh the positions
2491                         RowList::iterator tmprow = row;
2492                         while (boost::next(tmprow) != rows().end() &&
2493                                boost::next(tmprow)->par() == row->par()) {
2494                                 ++tmprow;
2495                                 tmprow->pos(tmprow->pos() - 1);
2496                         }
2497
2498                         // delete newlines at the beginning of paragraphs
2499                         while (!cursor.par()->empty() &&
2500                                cursor.pos() < cursor.par()->size() &&
2501                                cursor.par()->isNewline(cursor.pos()) &&
2502                                cursor.pos() == cursor.par()->beginningOfBody()) {
2503                                 cursor.par()->erase(cursor.pos());
2504                                 // refresh the positions
2505                                 tmprow = row;
2506                                 while (boost::next(tmprow) != rows().end() &&
2507                                        boost::next(tmprow)->par() == row->par()) {
2508                                         ++tmprow;
2509                                         tmprow->pos(tmprow->pos() - 1);
2510                                 }
2511                         }
2512                 }
2513
2514                 // is there a break one row above
2515                 if (row != rows().begin() && boost::prior(row)->par() == row->par()) {
2516                         z = rowBreakPoint(*boost::prior(row));
2517                         if (z >= row->pos()) {
2518                                 row->pos(z + 1);
2519
2520                                 RowList::iterator tmprow = boost::prior(row);
2521
2522                                 // maybe the current row is now empty
2523                                 if (row->pos() >= row->par()->size()) {
2524                                         // remove it
2525                                         removeRow(row);
2526                                 } else {
2527                                         breakAgainOneRow(row);
2528                                 }
2529
2530                                 // set the dimensions of the row above
2531                                 y -= tmprow->height();
2532                                 tmprow->fill(fill(tmprow, workWidth()));
2533                                 setHeightOfRow(tmprow);
2534                                 postPaint();
2535
2536                                 setCursor(cursor.par(), cursor.pos(),
2537                                           false, cursor.boundary());
2538                                 //current_font = rawtmpfont;
2539                                 //real_current_font = realtmpfont;
2540                                 // check, whether the last character's font has changed.
2541                                 if (rawparfont !=
2542                                     cursor.par()->getFontSettings(bv()->buffer()->params,
2543                                                                   cursor.par()->size() - 1))
2544                                         redoHeightOfParagraph();
2545                                 return;
2546                         }
2547                 }
2548
2549                 // break the cursor row again
2550                 if (boost::next(row) != rows().end() &&
2551                     boost::next(row)->par() == row->par() &&
2552                     (lastPos(*this, row) == row->par()->size() - 1 ||
2553                      rowBreakPoint(*row) != lastPos(*this, row))) {
2554
2555                         // it can happen that a paragraph loses one row
2556                         // without a real breakup. This is when a word
2557                         // is to long to be broken. Well, I don t care this
2558                         // hack ;-)
2559                         if (lastPos(*this, row) == row->par()->size() - 1)
2560                                 removeRow(boost::next(row));
2561
2562                         postPaint();
2563
2564                         breakAgainOneRow(row);
2565                         // will the cursor be in another row now?
2566                         if (boost::next(row) != rows().end() &&
2567                             boost::next(row)->par() == row->par() &&
2568                             lastPos(*this, row) <= cursor.pos()) {
2569                                 ++row;
2570                                 breakAgainOneRow(row);
2571                         }
2572
2573                         setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
2574                 } else  {
2575                         // set the dimensions of the row
2576                         row->fill(fill(row, workWidth()));
2577                         setHeightOfRow(row);
2578                         postPaint();
2579                         setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
2580                 }
2581         }
2582
2583         // current_font = rawtmpfont;
2584         // real_current_font = realtmpfont;
2585
2586         if (isBoundary(bv()->buffer(), *cursor.par(), cursor.pos())
2587             != cursor.boundary())
2588                 setCursor(cursor.par(), cursor.pos(), false, !cursor.boundary());
2589
2590         lastpos = cursor.par()->size();
2591         if (cursor.pos() == lastpos)
2592                 setCurrentFont();
2593
2594         // check, whether the last characters font has changed.
2595         if (rawparfont !=
2596             cursor.par()->getFontSettings(bv()->buffer()->params, lastpos - 1)) {
2597                 redoHeightOfParagraph();
2598         }
2599 }
2600
2601
2602 RowList::iterator LyXText::cursorRow() const
2603 {
2604         return getRow(cursor.par(), cursor.pos());
2605 }
2606
2607
2608 RowList::iterator LyXText::getRow(LyXCursor const & cur) const
2609 {
2610         return getRow(cur.par(), cur.pos());
2611 }
2612
2613
2614 RowList::iterator
2615 LyXText::getRow(ParagraphList::iterator pit, pos_type pos) const
2616 {
2617         if (rows().empty())
2618                 return rowlist_.end();
2619
2620         // find the first row of the specified paragraph
2621         RowList::iterator rit = rowlist_.begin();
2622         RowList::iterator end = rowlist_.end();
2623         while (boost::next(rit) != end && rit->par() != pit) {
2624                 ++rit;
2625         }
2626
2627         // now find the wanted row
2628         while (rit->pos() < pos
2629                && boost::next(rit) != end
2630                && boost::next(rit)->par() == pit
2631                && boost::next(rit)->pos() <= pos) {
2632                 ++rit;
2633         }
2634
2635         return rit;
2636 }
2637
2638
2639 // returns pointer to a specified row
2640 RowList::iterator
2641 LyXText::getRow(ParagraphList::iterator pit, pos_type pos, int & y) const
2642 {
2643         y = 0;
2644
2645         if (rows().empty())
2646                 return rowlist_.end();
2647
2648         // find the first row of the specified paragraph
2649         RowList::iterator rit = rowlist_.begin();
2650         RowList::iterator end = rowlist_.end();
2651         while (boost::next(rit) != end && rit->par() != pit) {
2652                 y += rit->height();
2653                 ++rit;
2654         }
2655
2656         // now find the wanted row
2657         while (rit->pos() < pos
2658                && boost::next(rit) != end
2659                && boost::next(rit)->par() == pit
2660                && boost::next(rit)->pos() <= pos) {
2661                 y += rit->height();
2662                 ++rit;
2663         }
2664
2665         return rit;
2666 }
2667
2668
2669 // returns pointer to some fancy row 'below' specified row
2670 RowList::iterator LyXText::cursorIRow() const
2671 {
2672         int y = 0;
2673         return getRow(cursor.par(), cursor.pos(), y);
2674 }
2675
2676
2677 RowList::iterator LyXText::getRowNearY(int & y) const
2678 {
2679         RowList::iterator rit = anchor_row_;
2680         RowList::iterator const beg = rows().begin();
2681         RowList::iterator const end = rows().end();
2682
2683         if (rows().empty()) {
2684                 y = 0;
2685                 return end;
2686         }
2687         if (rit == end)
2688                 rit = beg;
2689
2690         int tmpy = rit->y();
2691
2692         if (tmpy <= y) {
2693                 while (rit != end && tmpy <= y) {
2694                         tmpy += rit->height();
2695                         ++rit;
2696                 }
2697                 if (rit != beg) {
2698                         --rit;
2699                         tmpy -= rit->height();
2700                 }
2701         } else {
2702                 while (rit != beg && tmpy > y) {
2703                         --rit;
2704                         tmpy -= rit->height();
2705                 }
2706         }
2707         if (tmpy < 0 || rit == end) {
2708                 tmpy = 0;
2709                 rit = beg;
2710         }
2711
2712         // return the rel y
2713         y = tmpy;
2714
2715         return rit;
2716 }
2717
2718
2719 int LyXText::getDepth() const
2720 {
2721         return cursor.par()->getDepth();
2722 }