]> git.lyx.org Git - lyx.git/blob - src/text.C
Remove #include "LColor.h" from insettext.h. Deps down 10 to 95.
[lyx.git] / src / text.C
1 /**
2  * \file text.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author John Levon
10  * \author André Pönitz
11  * \author Dekel Tsur
12  * \author Jürgen Vigna
13  *
14  * Full author contact details are available in file CREDITS.
15  */
16
17 #include <config.h>
18
19 #include "lyxtext.h"
20
21 #include "buffer.h"
22 #include "bufferparams.h"
23 #include "BufferView.h"
24 #include "debug.h"
25 #include "encoding.h"
26 #include "funcrequest.h"
27 #include "gettext.h"
28 #include "language.h"
29 #include "LColor.h"
30 #include "lyxlength.h"
31 #include "lyxrc.h"
32 #include "lyxrow.h"
33 #include "lyxrow_funcs.h"
34 #include "paragraph_funcs.h"
35 #include "ParagraphParameters.h"
36 #include "rowpainter.h"
37 #include "text_funcs.h"
38 #include "undo_funcs.h"
39 #include "vspace.h"
40 #include "WordLangTuple.h"
41
42 #include "frontends/font_metrics.h"
43 #include "frontends/LyXView.h"
44
45 #include "insets/insettext.h"
46
47 #include "support/lstrings.h"
48 #include "support/textutils.h"
49
50 using bv_funcs::number;
51
52 using lyx::pos_type;
53 using lyx::word_location;
54
55 using lyx::support::contains;
56 using lyx::support::lowercase;
57 using lyx::support::uppercase;
58
59 using std::max;
60 using std::endl;
61
62
63 /// top, right, bottom pixel margin
64 extern int const PAPER_MARGIN = 20;
65 /// margin for changebar
66 extern int const CHANGEBAR_MARGIN = 10;
67 /// left margin
68 extern int const LEFT_MARGIN = PAPER_MARGIN + CHANGEBAR_MARGIN;
69
70
71
72 int bibitemMaxWidth(BufferView *, LyXFont const &);
73
74
75 BufferView * LyXText::bv()
76 {
77         BOOST_ASSERT(bv_owner != 0);
78         return bv_owner;
79 }
80
81
82 BufferView * LyXText::bv() const
83 {
84         BOOST_ASSERT(bv_owner != 0);
85         return bv_owner;
86 }
87
88
89 void LyXText::updateRowPositions()
90 {
91         ParagraphList::iterator pit = ownerParagraphs().begin();
92         ParagraphList::iterator end = ownerParagraphs().end();
93         for (height = 0; pit != end; ++pit) {
94                 RowList::iterator rit = pit->rows.begin();
95                 RowList::iterator rend = pit->rows.end();
96                 for ( ; rit != rend ; ++rit) {
97                         rit->y(height);
98                         height += rit->height();
99                 }
100         }
101 }
102
103
104 int LyXText::workWidth() const
105 {
106         return inset_owner ? inset_owner->textWidth() : bv()->workWidth();
107 }
108
109
110 int LyXText::getRealCursorX() const
111 {
112         int x = cursor.x();
113         if (the_locking_inset && (the_locking_inset->getLyXText(bv())!= this))
114                 x = the_locking_inset->getLyXText(bv())->getRealCursorX();
115         return x;
116 }
117
118
119 #warning FIXME  This function seems to belong outside of LyxText.
120 unsigned char LyXText::transformChar(unsigned char c, Paragraph const & par,
121                                      pos_type pos) const
122 {
123         if (!Encodings::is_arabic(c))
124                 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
125                         return c + (0xb0 - '0');
126                 else
127                         return c;
128
129         unsigned char const prev_char = pos > 0 ? par.getChar(pos - 1) : ' ';
130         unsigned char next_char = ' ';
131
132         pos_type const par_size = par.size();
133
134         for (pos_type i = pos + 1; i < par_size; ++i) {
135                 unsigned char const par_char = par.getChar(i);
136                 if (!Encodings::IsComposeChar_arabic(par_char)) {
137                         next_char = par_char;
138                         break;
139                 }
140         }
141
142         if (Encodings::is_arabic(next_char)) {
143                 if (Encodings::is_arabic(prev_char) &&
144                         !Encodings::is_arabic_special(prev_char))
145                         return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
146                 else
147                         return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
148         } else {
149                 if (Encodings::is_arabic(prev_char) &&
150                         !Encodings::is_arabic_special(prev_char))
151                         return Encodings::TransformChar(c, Encodings::FORM_FINAL);
152                 else
153                         return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
154         }
155 }
156
157 // This is the comments that some of the warnings below refers to.
158 // There are some issues in this file and I don't think they are
159 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
160 // this is a problem that has been here almost from day one and that a
161 // larger userbase with differenct access patters triggers the bad
162 // behaviour. (segfaults.) What I think happen is: In several places
163 // we store the paragraph in the current cursor and then moves the
164 // cursor. This movement of the cursor will delete paragraph at the
165 // old position if it is now empty. This will make the temporary
166 // pointer to the old cursor paragraph invalid and dangerous to use.
167 // And is some cases this will trigger a segfault. I have marked some
168 // of the cases where this happens with a warning, but I am sure there
169 // are others in this file and in text2.C. There is also a note in
170 // Delete() that you should read. In Delete I store the paragraph->id
171 // instead of a pointer to the paragraph. I am pretty sure this faulty
172 // use of temporary pointers to paragraphs that might have gotten
173 // invalidated (through a cursor movement) before they are used, are
174 // the cause of the strange crashes we get reported often.
175 //
176 // It is very tiresom to change this code, especially when it is as
177 // hard to read as it is. Help to fix all the cases where this is done
178 // would be greately appreciated.
179 //
180 // Lgb
181
182 int LyXText::singleWidth(ParagraphList::iterator pit, pos_type pos) const
183 {
184         if (pos >= pit->size())
185                 return 0;
186
187         char const c = pit->getChar(pos);
188         LyXFont const & font = getFont(pit, pos);
189         return singleWidth(pit, pos, c, font);
190 }
191
192
193 int LyXText::singleWidth(ParagraphList::iterator pit,
194                          pos_type pos, char c, LyXFont const & font) const
195 {
196         if (pos >= pit->size()) {
197                 lyxerr << "in singleWidth(), pos: " << pos << endl;
198                 BOOST_ASSERT(false);
199                 return 0;
200         }
201
202
203         // The most common case is handled first (Asger)
204         if (IsPrintable(c)) {
205                 if (font.language()->RightToLeft()) {
206                         if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
207                              lyxrc.font_norm_type == LyXRC::ISO_10646_1)
208                             && font.language()->lang() == "arabic") {
209                                 if (Encodings::IsComposeChar_arabic(c))
210                                         return 0;
211                                 else
212                                         c = transformChar(c, *pit, pos);
213                         } else if (font.language()->lang() == "hebrew" &&
214                                  Encodings::IsComposeChar_hebrew(c))
215                                 return 0;
216                 }
217                 return font_metrics::width(c, font);
218         }
219
220         if (c == Paragraph::META_INSET) {
221                 InsetOld * tmpinset = pit->getInset(pos);
222                 BOOST_ASSERT(tmpinset);
223                 if (tmpinset->lyxCode() == InsetOld::HFILL_CODE) {
224                         // Because of the representation as vertical lines
225                         return 3;
226                 }
227                 return tmpinset->width();
228         }
229
230         if (IsSeparatorChar(c))
231                 c = ' ';
232         return font_metrics::width(c, font);
233 }
234
235
236 lyx::pos_type LyXText::log2vis(lyx::pos_type pos) const
237 {
238         if (bidi_start == -1)
239                 return pos;
240         else
241                 return log2vis_list[pos - bidi_start];
242 }
243
244
245 lyx::pos_type LyXText::vis2log(lyx::pos_type pos) const
246 {
247         if (bidi_start == -1)
248                 return pos;
249         else
250                 return vis2log_list[pos - bidi_start];
251 }
252
253
254 lyx::pos_type LyXText::bidi_level(lyx::pos_type pos) const
255 {
256         if (bidi_start == -1)
257                 return 0;
258         else
259                 return bidi_levels[pos - bidi_start];
260 }
261
262
263 bool LyXText::bidi_InRange(lyx::pos_type pos) const
264 {
265         return bidi_start == -1 ||
266                 (bidi_start <= pos && pos <= bidi_end);
267 }
268
269
270 void LyXText::computeBidiTables(ParagraphList::iterator pit,
271    Buffer const & buf, RowList::iterator row) const
272 {
273         bidi_same_direction = true;
274         if (!lyxrc.rtl_support) {
275                 bidi_start = -1;
276                 return;
277         }
278
279         InsetOld * inset = pit->inInset();
280         if (inset && inset->owner() &&
281             inset->owner()->lyxCode() == InsetOld::ERT_CODE) {
282                 bidi_start = -1;
283                 return;
284         }
285
286         bidi_start = row->pos();
287         bidi_end = lastPos(*pit, row);
288
289         if (bidi_start > bidi_end) {
290                 bidi_start = -1;
291                 return;
292         }
293
294         if (bidi_end + 2 - bidi_start >
295             static_cast<pos_type>(log2vis_list.size())) {
296                 pos_type new_size =
297                         (bidi_end + 2 - bidi_start < 500) ?
298                         500 : 2 * (bidi_end + 2 - bidi_start);
299                 log2vis_list.resize(new_size);
300                 vis2log_list.resize(new_size);
301                 bidi_levels.resize(new_size);
302         }
303
304         vis2log_list[bidi_end + 1 - bidi_start] = -1;
305         log2vis_list[bidi_end + 1 - bidi_start] = -1;
306
307         BufferParams const & bufparams = buf.params();
308         pos_type stack[2];
309         bool const rtl_par =
310                 pit->isRightToLeftPar(bufparams);
311         int level = 0;
312         bool rtl = false;
313         bool rtl0 = false;
314         pos_type const body_pos = pit->beginningOfBody();
315
316         for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
317                 bool is_space = pit->isLineSeparator(lpos);
318                 pos_type const pos =
319                         (is_space && lpos + 1 <= bidi_end &&
320                          !pit->isLineSeparator(lpos + 1) &&
321                          !pit->isNewline(lpos + 1))
322                         ? lpos + 1 : lpos;
323                 LyXFont font = pit->getFontSettings(bufparams, pos);
324                 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
325                     font.number() == LyXFont::ON &&
326                     pit->getFontSettings(bufparams, lpos - 1).number()
327                     == LyXFont::ON) {
328                         font = pit->getFontSettings(bufparams, lpos);
329                         is_space = false;
330                 }
331
332
333                 bool new_rtl = font.isVisibleRightToLeft();
334                 bool new_rtl0 = font.isRightToLeft();
335                 int new_level;
336
337                 if (lpos == body_pos - 1
338                     && row->pos() < body_pos - 1
339                     && is_space) {
340                         new_level = (rtl_par) ? 1 : 0;
341                         new_rtl = new_rtl0 = rtl_par;
342                 } else if (new_rtl0)
343                         new_level = (new_rtl) ? 1 : 2;
344                 else
345                         new_level = (rtl_par) ? 2 : 0;
346
347                 if (is_space && new_level >= level) {
348                         new_level = level;
349                         new_rtl = rtl;
350                         new_rtl0 = rtl0;
351                 }
352
353                 int new_level2 = new_level;
354
355                 if (level == new_level && rtl0 != new_rtl0) {
356                         --new_level2;
357                         log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
358                 } else if (level < new_level) {
359                         log2vis_list[lpos - bidi_start] =  (rtl) ? -1 : 1;
360                         if (new_level > rtl_par)
361                                 bidi_same_direction = false;
362                 } else
363                         log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
364                 rtl = new_rtl;
365                 rtl0 = new_rtl0;
366                 bidi_levels[lpos - bidi_start] = new_level;
367
368                 while (level > new_level2) {
369                         pos_type old_lpos = stack[--level];
370                         int delta = lpos - old_lpos - 1;
371                         if (level % 2)
372                                 delta = -delta;
373                         log2vis_list[lpos - bidi_start] += delta;
374                         log2vis_list[old_lpos - bidi_start] += delta;
375                 }
376                 while (level < new_level)
377                         stack[level++] = lpos;
378         }
379
380         while (level > 0) {
381                 pos_type const old_lpos = stack[--level];
382                 int delta = bidi_end - old_lpos;
383                 if (level % 2)
384                         delta = -delta;
385                 log2vis_list[old_lpos - bidi_start] += delta;
386         }
387
388         pos_type vpos = bidi_start - 1;
389         for (pos_type lpos = bidi_start;
390              lpos <= bidi_end; ++lpos) {
391                 vpos += log2vis_list[lpos - bidi_start];
392                 vis2log_list[vpos - bidi_start] = lpos;
393                 log2vis_list[lpos - bidi_start] = vpos;
394         }
395 }
396
397
398 // This method requires a previous call to ComputeBidiTables()
399 bool LyXText::isBoundary(Buffer const & buf, Paragraph const & par,
400                          pos_type pos) const
401 {
402         if (!lyxrc.rtl_support || pos == 0)
403                 return false;
404
405         if (!bidi_InRange(pos - 1)) {
406                 /// This can happen if pos is the first char of a row.
407                 /// Returning false in this case is incorrect!
408                 return false;
409         }
410
411         bool const rtl = bidi_level(pos - 1) % 2;
412         bool const rtl2 = bidi_InRange(pos)
413                 ? bidi_level(pos) % 2
414                 : par.isRightToLeftPar(buf.params());
415         return rtl != rtl2;
416 }
417
418
419 bool LyXText::isBoundary(Buffer const & buf, Paragraph const & par,
420                          pos_type pos, LyXFont const & font) const
421 {
422         if (!lyxrc.rtl_support)
423                 return false;    // This is just for speedup
424
425         bool const rtl = font.isVisibleRightToLeft();
426         bool const rtl2 = bidi_InRange(pos)
427                 ? bidi_level(pos) % 2
428                 : par.isRightToLeftPar(buf.params());
429         return rtl != rtl2;
430 }
431
432
433 int LyXText::leftMargin(ParagraphList::iterator pit, Row const & row) const
434 {
435         LyXTextClass const & tclass =
436                 bv()->buffer()->params().getLyXTextClass();
437         LyXLayout_ptr const & layout = pit->layout();
438
439         string parindent = layout->parindent;
440
441         int x = LEFT_MARGIN;
442
443         x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
444
445         // this is the way, LyX handles the LaTeX-Environments.
446         // I have had this idea very late, so it seems to be a
447         // later added hack and this is true
448         if (!pit->getDepth()) {
449                 if (pit->layout() == tclass.defaultLayout()) {
450                         // find the previous same level paragraph
451                         if (pit != ownerParagraphs().begin()) {
452                                 ParagraphList::iterator newpit =
453                                         depthHook(pit, ownerParagraphs(),
454                                                   pit->getDepth());
455                                 if (newpit == pit &&
456                                     newpit->layout()->nextnoindent)
457                                         parindent.erase();
458                         }
459                 }
460         } else {
461                 // find the next level paragraph
462
463                 ParagraphList::iterator newpar = outerHook(pit,
464                                                            ownerParagraphs());
465
466                 // make a corresponding row. Needed to call leftMargin()
467
468                 // check wether it is a sufficent paragraph
469                 if (newpar != ownerParagraphs().end() &&
470                     newpar->layout()->isEnvironment()) {
471                         x = leftMargin(newpar, Row(newpar->size()));
472                 }
473
474                 if (newpar != ownerParagraphs().end() &&
475                     pit->layout() == tclass.defaultLayout()) {
476                         if (newpar->params().noindent())
477                                 parindent.erase();
478                         else {
479                                 parindent = newpar->layout()->parindent;
480                         }
481
482                 }
483         }
484
485         LyXFont const labelfont = getLabelFont(pit);
486         switch (layout->margintype) {
487         case MARGIN_DYNAMIC:
488                 if (!layout->leftmargin.empty()) {
489                         x += font_metrics::signedWidth(layout->leftmargin,
490                                                   tclass.defaultfont());
491                 }
492                 if (!pit->getLabelstring().empty()) {
493                         x += font_metrics::signedWidth(layout->labelindent,
494                                                   labelfont);
495                         x += font_metrics::width(pit->getLabelstring(),
496                                             labelfont);
497                         x += font_metrics::width(layout->labelsep, labelfont);
498                 }
499                 break;
500         case MARGIN_MANUAL:
501                 x += font_metrics::signedWidth(layout->labelindent, labelfont);
502                 // The width of an empty par, even with manual label, should be 0
503                 if (!pit->empty() && row.pos() >= pit->beginningOfBody()) {
504                         if (!pit->getLabelWidthString().empty()) {
505                                 x += font_metrics::width(pit->getLabelWidthString(),
506                                                labelfont);
507                                 x += font_metrics::width(layout->labelsep, labelfont);
508                         }
509                 }
510                 break;
511         case MARGIN_STATIC:
512                 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
513                         / (pit->getDepth() + 4);
514                 break;
515         case MARGIN_FIRST_DYNAMIC:
516                 if (layout->labeltype == LABEL_MANUAL) {
517                         if (row.pos() >= pit->beginningOfBody()) {
518                                 x += font_metrics::signedWidth(layout->leftmargin,
519                                                           labelfont);
520                         } else {
521                                 x += font_metrics::signedWidth(layout->labelindent,
522                                                           labelfont);
523                         }
524                 } else if (row.pos()
525                            // Special case to fix problems with
526                            // theorems (JMarc)
527                            || (layout->labeltype == LABEL_STATIC
528                                && layout->latextype == LATEX_ENVIRONMENT
529                                && !isFirstInSequence(pit, ownerParagraphs()))) {
530                         x += font_metrics::signedWidth(layout->leftmargin,
531                                                   labelfont);
532                 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
533                            && layout->labeltype != LABEL_BIBLIO
534                            && layout->labeltype !=
535                            LABEL_CENTERED_TOP_ENVIRONMENT) {
536                         x += font_metrics::signedWidth(layout->labelindent,
537                                                   labelfont);
538                         x += font_metrics::width(layout->labelsep, labelfont);
539                         x += font_metrics::width(pit->getLabelstring(),
540                                             labelfont);
541                 }
542                 break;
543
544         case MARGIN_RIGHT_ADDRESS_BOX:
545         {
546                 // ok, a terrible hack. The left margin depends on the widest
547                 // row in this paragraph. Do not care about footnotes, they
548                 // are *NOT* allowed in the LaTeX realisation of this layout.
549
550                 // find the first row of this paragraph
551                 RowList::iterator rit = pit->rows.begin();
552                 RowList::iterator end = pit->rows.end();
553                 int minfill = rit->fill();
554                 for ( ; rit != end; ++rit)
555                         if (rit->fill() < minfill)
556                                 minfill = rit->fill();
557
558                 x += font_metrics::signedWidth(layout->leftmargin,
559                         tclass.defaultfont());
560                 x += minfill;
561         }
562         break;
563         }
564
565         if (workWidth() > 0 && !pit->params().leftIndent().zero()) {
566                 LyXLength const len = pit->params().leftIndent();
567                 int const tw = inset_owner ?
568                         inset_owner->latexTextWidth(bv()) : workWidth();
569                 x += len.inPixels(tw);
570         }
571
572         LyXAlignment align;
573
574         if (pit->params().align() == LYX_ALIGN_LAYOUT)
575                 align = layout->align;
576         else
577                 align = pit->params().align();
578
579         // set the correct parindent
580         if (row.pos() == 0) {
581                 if ((layout->labeltype == LABEL_NO_LABEL
582                      || layout->labeltype == LABEL_TOP_ENVIRONMENT
583                      || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
584                      || (layout->labeltype == LABEL_STATIC
585                          && layout->latextype == LATEX_ENVIRONMENT
586                          && !isFirstInSequence(pit, ownerParagraphs())))
587                     && align == LYX_ALIGN_BLOCK
588                     && !pit->params().noindent()
589                         // in tabulars and ert paragraphs are never indented!
590                         && (!pit->inInset() || !pit->inInset()->owner() ||
591                                 (pit->inInset()->owner()->lyxCode() != InsetOld::TABULAR_CODE &&
592                                  pit->inInset()->owner()->lyxCode() != InsetOld::ERT_CODE))
593                     && (pit->layout() != tclass.defaultLayout() ||
594                         bv()->buffer()->params().paragraph_separation ==
595                         BufferParams::PARSEP_INDENT)) {
596                         x += font_metrics::signedWidth(parindent,
597                                                   tclass.defaultfont());
598                 } else if (layout->labeltype == LABEL_BIBLIO) {
599                         // ale970405 Right width for bibitems
600                         x += bibitemMaxWidth(bv(), tclass.defaultfont());
601                 }
602         }
603
604         return x;
605 }
606
607
608 int LyXText::rightMargin(ParagraphList::iterator pit,
609         Buffer const & buf, Row const &) const
610 {
611         LyXTextClass const & tclass = buf.params().getLyXTextClass();
612         LyXLayout_ptr const & layout = pit->layout();
613
614         return PAPER_MARGIN
615                 + font_metrics::signedWidth(tclass.rightmargin(),
616                                        tclass.defaultfont());
617                 + font_metrics::signedWidth(layout->rightmargin,
618                                        tclass.defaultfont())
619                 * 4 / (pit->getDepth() + 4);
620 }
621
622
623 int LyXText::labelEnd(ParagraphList::iterator pit, Row const & row) const
624 {
625         if (pit->layout()->margintype == MARGIN_MANUAL) {
626                 Row tmprow = row;
627                 tmprow.pos(pit->size());
628                 // return the beginning of the body
629                 return leftMargin(pit, tmprow);
630         }
631
632         // LabelEnd is only needed if the layout
633         // fills a flushleft label.
634         return 0;
635 }
636
637
638 namespace {
639
640 // this needs special handling - only newlines count as a break point
641 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
642 {
643         for (; i < par.size(); ++i) {
644                 if (par.isNewline(i))
645                         return i;
646         }
647
648         return par.size();
649 }
650
651 };
652
653
654 pos_type LyXText::rowBreakPoint(ParagraphList::iterator pit,
655         Row const & row) const
656 {
657         // maximum pixel width of a row.
658         int width = workWidth()
659                 - rightMargin(pit, *bv()->buffer(), row);
660
661         // inset->textWidth() returns -1 via workWidth(),
662         // but why ?
663         if (width < 0)
664                 return pit->size();
665
666         LyXLayout_ptr const & layout = pit->layout();
667
668         if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
669                 return addressBreakPoint(row.pos(), *pit);
670
671         pos_type const pos = row.pos();
672         pos_type const body_pos = pit->beginningOfBody();
673         pos_type const last = pit->size();
674         pos_type point = last;
675
676         if (pos == last)
677                 return last;
678
679         // Now we iterate through until we reach the right margin
680         // or the end of the par, then choose the possible break
681         // nearest that.
682
683         int const left = leftMargin(pit, row);
684         int x = left;
685
686         // pixel width since last breakpoint
687         int chunkwidth = 0;
688
689         pos_type i = pos;
690
691         // We re-use the font resolution for the entire font span when possible
692         LyXFont font = getFont(pit, i);
693         lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
694
695         for (; i < last; ++i) {
696                 if (pit->isNewline(i)) {
697                         point = i;
698                         break;
699                 }
700
701                 char const c = pit->getChar(i);
702                 if (i > endPosOfFontSpan) {
703                         font = getFont(pit, i);
704                         endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
705                 }
706
707                 int thiswidth;
708
709                 // add the auto-hfill from label end to the body
710                 if (body_pos && i == body_pos) {
711                         thiswidth = font_metrics::width(layout->labelsep, getLabelFont(pit));
712                         if (pit->isLineSeparator(i - 1))
713                                 thiswidth -= singleWidth(pit, i - 1);
714                         int left_margin = labelEnd(pit, row);
715                         if (thiswidth + x < left_margin)
716                                 thiswidth = left_margin - x;
717                         thiswidth += singleWidth(pit, i, c, font);
718                 } else {
719                         thiswidth = singleWidth(pit, i, c, font);
720                 }
721
722                 x += thiswidth;
723                 chunkwidth += thiswidth;
724
725                 InsetOld * in = pit->isInset(i) ? pit->getInset(i) : 0;
726
727                 // break before a character that will fall off
728                 // the right of the row
729                 if (x >= width) {
730                         // if no break before, break here
731                         if (point == last || chunkwidth >= (width - left)) {
732                                 if (pos < i)
733                                         point = i - 1;
734                                 else
735                                         point = i;
736                         }
737                         break;
738                 }
739
740                 if (!in || in->isChar()) {
741                         // some insets are line separators too
742                         if (pit->isLineSeparator(i)) {
743                                 point = i;
744                                 chunkwidth = 0;
745                         }
746                         continue;
747                 }
748
749                 continue;
750         }
751
752         if (point == last && x >= width) {
753                 // didn't find one, break at the point we reached the edge
754                 point = i;
755         } else if (i == last && x < width) {
756                 // found one, but we fell off the end of the par, so prefer
757                 // that.
758                 point = last;
759         }
760
761         // manual labels cannot be broken in LaTeX. But we
762         // want to make our on-screen rendering of footnotes
763         // etc. still break
764         if (body_pos && point < body_pos)
765                 point = body_pos - 1;
766
767         return point;
768 }
769
770
771 // returns the minimum space a row needs on the screen in pixel
772 int LyXText::fill(ParagraphList::iterator pit,
773         RowList::iterator row, int paper_width) const
774 {
775         if (paper_width < 0)
776                 return 0;
777
778         int w;
779         // get the pure distance
780         pos_type const last = lastPos(*pit, row);
781
782         LyXLayout_ptr const & layout = pit->layout();
783
784         // special handling of the right address boxes
785         if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
786                 int const tmpfill = row->fill();
787                 row->fill(0); // the minfill in MarginLeft()
788                 w = leftMargin(pit, *row);
789                 row->fill(tmpfill);
790         } else
791                 w = leftMargin(pit, *row);
792
793         pos_type const body_pos = pit->beginningOfBody();
794         pos_type i = row->pos();
795
796         if (! pit->empty() && i <= last) {
797                 // We re-use the font resolution for the entire span when possible
798                 LyXFont font = getFont(pit, i);
799                 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
800                 while (i <= last) {
801                         if (body_pos > 0 && i == body_pos) {
802                                 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
803                                 if (pit->isLineSeparator(i - 1))
804                                         w -= singleWidth(pit, i - 1);
805                                 int left_margin = labelEnd(pit, *row);
806                                 if (w < left_margin)
807                                         w = left_margin;
808                         }
809                         char const c = pit->getChar(i);
810                         if (IsPrintable(c) && i > endPosOfFontSpan) {
811                                 // We need to get the next font
812                                 font = getFont(pit, i);
813                                 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
814                         }
815                         w += singleWidth(pit, i, c, font);
816                         ++i;
817                 }
818         }
819         if (body_pos > 0 && body_pos > last) {
820                 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
821                 if (last >= 0 && pit->isLineSeparator(last))
822                         w -= singleWidth(pit, last);
823                 int const left_margin = labelEnd(pit, *row);
824                 if (w < left_margin)
825                         w = left_margin;
826         }
827
828         int const fill = paper_width - w - rightMargin(pit, *bv()->buffer(), *row);
829
830         // If this case happens, it means that our calculation
831         // of the widths of the chars when we do rowBreakPoint()
832         // went wrong for some reason. Typically in list bodies.
833         // Things just about hobble on anyway, though you'll end
834         // up with a "fill_separator" less than zero, which corresponds
835         // to inter-word spacing being too small. Hopefully this problem
836         // will die when the label hacks die.
837         if (lyxerr.debugging() && fill < 0) {
838                 lyxerr[Debug::GUI] << "Eek, fill() was < 0: " << fill
839                         << " w " << w << " paper_width " << paper_width
840                         << " right margin " << rightMargin(pit, *bv()->buffer(), *row) << endl;
841         }
842         return fill;
843 }
844
845
846 // returns the minimum space a manual label needs on the screen in pixel
847 int LyXText::labelFill(ParagraphList::iterator pit, Row const & row) const
848 {
849         pos_type last = pit->beginningOfBody();
850
851         BOOST_ASSERT(last > 0);
852
853         // -1 because a label ends either with a space that is in the label,
854         // or with the beginning of a footnote that is outside the label.
855         --last;
856
857         // a separator at this end does not count
858         if (pit->isLineSeparator(last))
859                 --last;
860
861         int w = 0;
862         pos_type i = row.pos();
863         while (i <= last) {
864                 w += singleWidth(pit, i);
865                 ++i;
866         }
867
868         int fill = 0;
869         string const & labwidstr = pit->params().labelWidthString();
870         if (!labwidstr.empty()) {
871                 LyXFont const labfont = getLabelFont(pit);
872                 int const labwidth = font_metrics::width(labwidstr, labfont);
873                 fill = max(labwidth - w, 0);
874         }
875
876         return fill;
877 }
878
879
880 EnumLColor LyXText::backgroundColor() const
881 {
882         if (inset_owner)
883                 return inset_owner->backgroundColor();
884         else
885                 return LColor::background;
886 }
887
888
889 void LyXText::setHeightOfRow(ParagraphList::iterator pit, RowList::iterator rit)
890 {
891         // get the maximum ascent and the maximum descent
892         double layoutasc = 0;
893         double layoutdesc = 0;
894         double tmptop = 0;
895
896         // ok, let us initialize the maxasc and maxdesc value.
897         // Only the fontsize count. The other properties
898         // are taken from the layoutfont. Nicer on the screen :)
899         LyXLayout_ptr const & layout = pit->layout();
900
901         // as max get the first character of this row then it can increase but not
902         // decrease the height. Just some point to start with so we don't have to
903         // do the assignment below too often.
904         LyXFont font = getFont(pit, rit->pos());
905         LyXFont::FONT_SIZE const tmpsize = font.size();
906         font = getLayoutFont(pit);
907         LyXFont::FONT_SIZE const size = font.size();
908         font.setSize(tmpsize);
909
910         LyXFont labelfont = getLabelFont(pit);
911
912         double spacing_val = 1.0;
913         if (!pit->params().spacing().isDefault())
914                 spacing_val = pit->params().spacing().getValue();
915         else
916                 spacing_val = bv()->buffer()->params().spacing().getValue();
917         //lyxerr << "spacing_val = " << spacing_val << endl;
918
919         int maxasc  = int(font_metrics::maxAscent(font) *
920                           layout->spacing.getValue() * spacing_val);
921         int maxdesc = int(font_metrics::maxDescent(font) *
922                           layout->spacing.getValue() * spacing_val);
923
924         pos_type const pos_end = lastPos(*pit, rit);
925         int labeladdon = 0;
926         int maxwidth = 0;
927
928         if (!pit->empty()) {
929                 // We re-use the font resolution for the entire font span when possible
930                 LyXFont font = getFont(pit, rit->pos());
931                 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(rit->pos());
932
933                 // Optimisation
934                 Paragraph const & par = *pit;
935
936                 // Check if any insets are larger
937                 for (pos_type pos = rit->pos(); pos <= pos_end; ++pos) {
938                         // Manual inlined optimised version of common case of
939                         // "maxwidth += singleWidth(pit, pos);"
940                         char const c = par.getChar(pos);
941
942                         if (IsPrintable(c)) {
943                                 if (pos > endPosOfFontSpan) {
944                                         // We need to get the next font
945                                         font = getFont(pit, pos);
946                                         endPosOfFontSpan = par.getEndPosOfFontSpan(pos);
947                                 }
948                                 if (! font.language()->RightToLeft()) {
949                                         maxwidth += font_metrics::width(c, font);
950                                 } else {
951                                         // Fall-back to normal case
952                                         maxwidth += singleWidth(pit, pos, c, font);
953                                         // And flush font cache
954                                         endPosOfFontSpan = 0;
955                                 }
956                         } else {
957                                 // Special handling of insets - are any larger?
958                                 if (par.isInset(pos)) {
959                                         InsetOld const * tmpinset = par.getInset(pos);
960                                         if (tmpinset) {
961                                                 maxwidth += tmpinset->width();
962                                                 maxasc = max(maxasc, tmpinset->ascent());
963                                                 maxdesc = max(maxdesc, tmpinset->descent());
964                                         }
965                                 } else {
966                                         // Fall-back to normal case
967                                         maxwidth += singleWidth(pit, pos, c, font);
968                                         // And flush font cache
969                                         endPosOfFontSpan = 0;
970                                 }
971                         }
972                 }
973         }
974
975         // Check if any custom fonts are larger (Asger)
976         // This is not completely correct, but we can live with the small,
977         // cosmetic error for now.
978         LyXFont::FONT_SIZE maxsize =
979                 pit->highestFontInRange(rit->pos(), pos_end, size);
980         if (maxsize > font.size()) {
981                 font.setSize(maxsize);
982                 maxasc = max(maxasc, font_metrics::maxAscent(font));
983                 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
984         }
985
986         // This is nicer with box insets:
987         ++maxasc;
988         ++maxdesc;
989
990         rit->ascent_of_text(maxasc);
991
992         // is it a top line?
993         if (!rit->pos()) {
994                 BufferParams const & bufparams = bv()->buffer()->params();
995                 // some parksips VERY EASY IMPLEMENTATION
996                 if (bv()->buffer()->params().paragraph_separation ==
997                         BufferParams::PARSEP_SKIP)
998                 {
999                         if (layout->isParagraph()
1000                                 && pit->getDepth() == 0
1001                                 && pit != ownerParagraphs().begin())
1002                         {
1003                                 maxasc += bufparams.getDefSkip().inPixels(*bv());
1004                         } else if (pit != ownerParagraphs().begin() &&
1005                                    boost::prior(pit)->layout()->isParagraph() &&
1006                                    boost::prior(pit)->getDepth() == 0)
1007                         {
1008                                 // is it right to use defskip here too? (AS)
1009                                 maxasc += bufparams.getDefSkip().inPixels(*bv());
1010                         }
1011                 }
1012
1013                 // the top margin
1014                 if (pit == ownerParagraphs().begin() && !isInInset())
1015                         maxasc += PAPER_MARGIN;
1016
1017                 // add the vertical spaces, that the user added
1018                 maxasc += getLengthMarkerHeight(*bv(), pit->params().spaceTop());
1019
1020                 // do not forget the DTP-lines!
1021                 // there height depends on the font of the nearest character
1022                 if (pit->params().lineTop())
1023
1024                         maxasc += 2 * font_metrics::ascent('x', getFont(pit, 0));
1025                 // and now the pagebreaks
1026                 if (pit->params().pagebreakTop())
1027                         maxasc += 3 * defaultRowHeight();
1028
1029                 if (pit->params().startOfAppendix())
1030                         maxasc += 3 * defaultRowHeight();
1031
1032                 // This is special code for the chapter, since the label of this
1033                 // layout is printed in an extra row
1034                 if (layout->counter == "chapter" && bufparams.secnumdepth >= 0) {
1035                         float spacing_val = 1.0;
1036                         if (!pit->params().spacing().isDefault()) {
1037                                 spacing_val = pit->params().spacing().getValue();
1038                         } else {
1039                                 spacing_val = bufparams.spacing().getValue();
1040                         }
1041
1042                         labeladdon = int(font_metrics::maxDescent(labelfont) *
1043                                          layout->spacing.getValue() *
1044                                          spacing_val)
1045                                 + int(font_metrics::maxAscent(labelfont) *
1046                                       layout->spacing.getValue() *
1047                                       spacing_val);
1048                 }
1049
1050                 // special code for the top label
1051                 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1052                      || layout->labeltype == LABEL_BIBLIO
1053                      || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1054                     && isFirstInSequence(pit, ownerParagraphs())
1055                     && !pit->getLabelstring().empty())
1056                 {
1057                         float spacing_val = 1.0;
1058                         if (!pit->params().spacing().isDefault()) {
1059                                 spacing_val = pit->params().spacing().getValue();
1060                         } else {
1061                                 spacing_val = bufparams.spacing().getValue();
1062                         }
1063
1064                         labeladdon = int(
1065                                 (font_metrics::maxAscent(labelfont) +
1066                                  font_metrics::maxDescent(labelfont)) *
1067                                   layout->spacing.getValue() *
1068                                   spacing_val
1069                                 + layout->topsep * defaultRowHeight()
1070                                 + layout->labelbottomsep * defaultRowHeight());
1071                 }
1072
1073                 // And now the layout spaces, for example before and after
1074                 // a section, or between the items of a itemize or enumerate
1075                 // environment.
1076
1077                 if (!pit->params().pagebreakTop()) {
1078                         ParagraphList::iterator prev =
1079                                 depthHook(pit, ownerParagraphs(),
1080                                           pit->getDepth());
1081                         if (prev != pit && prev->layout() == layout &&
1082                                 prev->getDepth() == pit->getDepth() &&
1083                                 prev->getLabelWidthString() == pit->getLabelWidthString())
1084                         {
1085                                 layoutasc = (layout->itemsep * defaultRowHeight());
1086                         } else if (rit != firstRow()) {
1087                                 tmptop = layout->topsep;
1088
1089                                 //if (boost::prior(pit)->getDepth() >= pit->getDepth())
1090                                 //      tmptop -= getPar(previousRow(rit))->layout()->bottomsep;
1091
1092                                 if (tmptop > 0)
1093                                         layoutasc = (tmptop * defaultRowHeight());
1094                         } else if (pit->params().lineTop()) {
1095                                 tmptop = layout->topsep;
1096
1097                                 if (tmptop > 0)
1098                                         layoutasc = (tmptop * defaultRowHeight());
1099                         }
1100
1101                         prev = outerHook(pit, ownerParagraphs());
1102                         if (prev != ownerParagraphs().end())  {
1103                                 maxasc += int(prev->layout()->parsep * defaultRowHeight());
1104                         } else if (pit != ownerParagraphs().begin()) {
1105                                 ParagraphList::iterator prior_pit = boost::prior(pit);
1106                                 if (prior_pit->getDepth() != 0 ||
1107                                     prior_pit->layout() == layout) {
1108                                         maxasc += int(layout->parsep * defaultRowHeight());
1109                                 }
1110                         }
1111                 }
1112         }
1113
1114         // is it a bottom line?
1115         if (boost::next(rit) == pit->rows.end()) {
1116                 // the bottom margin
1117                 ParagraphList::iterator nextpit = boost::next(pit);
1118                 if (nextpit == ownerParagraphs().end() && !isInInset())
1119                         maxdesc += PAPER_MARGIN;
1120
1121                 // add the vertical spaces, that the user added
1122                 maxdesc += getLengthMarkerHeight(*bv(), pit->params().spaceBottom());
1123
1124                 // do not forget the DTP-lines!
1125                 // there height depends on the font of the nearest character
1126                 if (pit->params().lineBottom())
1127                         maxdesc += 2 * font_metrics::ascent('x',
1128                                         getFont(pit, max(pos_type(0), pit->size() - 1)));
1129
1130                 // and now the pagebreaks
1131                 if (pit->params().pagebreakBottom())
1132                         maxdesc += 3 * defaultRowHeight();
1133
1134                 // and now the layout spaces, for example before and after
1135                 // a section, or between the items of a itemize or enumerate
1136                 // environment
1137                 if (!pit->params().pagebreakBottom()
1138                     && nextpit != ownerParagraphs().end()) {
1139                         ParagraphList::iterator comparepit = pit;
1140                         float usual = 0;
1141                         float unusual = 0;
1142
1143                         if (comparepit->getDepth() > nextpit->getDepth()) {
1144                                 usual = (comparepit->layout()->bottomsep * defaultRowHeight());
1145                                 comparepit = depthHook(comparepit, ownerParagraphs(), nextpit->getDepth());
1146                                 if (comparepit->layout()!= nextpit->layout()
1147                                         || nextpit->getLabelWidthString() !=
1148                                         comparepit->getLabelWidthString())
1149                                 {
1150                                         unusual = (comparepit->layout()->bottomsep * defaultRowHeight());
1151                                 }
1152                                 if (unusual > usual)
1153                                         layoutdesc = unusual;
1154                                 else
1155                                         layoutdesc = usual;
1156                         } else if (comparepit->getDepth() ==  nextpit->getDepth()) {
1157
1158                                 if (comparepit->layout() != nextpit->layout()
1159                                         || nextpit->getLabelWidthString() !=
1160                                         comparepit->getLabelWidthString())
1161                                         layoutdesc = int(comparepit->layout()->bottomsep * defaultRowHeight());
1162                         }
1163                 }
1164         }
1165
1166         // incalculate the layout spaces
1167         maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
1168         maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
1169
1170         rit->height(maxasc + maxdesc + labeladdon);
1171         rit->baseline(maxasc + labeladdon);
1172         rit->top_of_text(rit->baseline() - font_metrics::maxAscent(font));
1173
1174         double x = 0;
1175         rit->width(int(maxwidth + x));
1176         if (inset_owner) {
1177                 width = max(0, workWidth());
1178                 RowList::iterator rit = firstRow();
1179                 RowList::iterator end = endRow();
1180                 ParagraphList::iterator it = ownerParagraphs().begin();
1181                 while (rit != end) {
1182                         if (rit->width() > width)
1183                                 width = rit->width();
1184                         nextRow(it, rit);
1185                 }
1186         }
1187 }
1188
1189
1190 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
1191 {
1192         // allow only if at start or end, or all previous is new text
1193         if (cursor.pos() && cursor.pos() != cursor.par()->size()
1194                 && cursor.par()->isChangeEdited(0, cursor.pos()))
1195                 return;
1196
1197         LyXTextClass const & tclass =
1198                 bv()->buffer()->params().getLyXTextClass();
1199         LyXLayout_ptr const & layout = cursor.par()->layout();
1200
1201         // this is only allowed, if the current paragraph is not empty or caption
1202         // and if it has not the keepempty flag active
1203         if (cursor.par()->empty() && !cursor.par()->allowEmpty()
1204            && layout->labeltype != LABEL_SENSITIVE)
1205                 return;
1206
1207         recordUndo(bv(), Undo::ATOMIC, cursor.par());
1208
1209         // Always break behind a space
1210         //
1211         // It is better to erase the space (Dekel)
1212         if (cursor.pos() < cursor.par()->size()
1213              && cursor.par()->isLineSeparator(cursor.pos()))
1214            cursor.par()->erase(cursor.pos());
1215
1216         // break the paragraph
1217         if (keep_layout)
1218                 keep_layout = 2;
1219         else
1220                 keep_layout = layout->isEnvironment();
1221
1222         // we need to set this before we insert the paragraph. IMO the
1223         // breakParagraph call should return a bool if it inserts the
1224         // paragraph before or behind and we should react on that one
1225         // but we can fix this in 1.3.0 (Jug 20020509)
1226         bool const isempty = (cursor.par()->allowEmpty() && cursor.par()->empty());
1227         ::breakParagraph(bv()->buffer()->params(), paragraphs, cursor.par(),
1228                          cursor.pos(), keep_layout);
1229
1230 #warning Trouble Point! (Lgb)
1231         // When ::breakParagraph is called from within an inset we must
1232         // ensure that the correct ParagraphList is used. Today that is not
1233         // the case and the Buffer::paragraphs is used. Not good. (Lgb)
1234         ParagraphList::iterator next_par = boost::next(cursor.par());
1235
1236         // well this is the caption hack since one caption is really enough
1237         if (layout->labeltype == LABEL_SENSITIVE) {
1238                 if (!cursor.pos())
1239                         // set to standard-layout
1240                         cursor.par()->applyLayout(tclass.defaultLayout());
1241                 else
1242                         // set to standard-layout
1243                         next_par->applyLayout(tclass.defaultLayout());
1244         }
1245
1246         // if the cursor is at the beginning of a row without prior newline,
1247         // move one row up!
1248         // This touches only the screen-update. Otherwise we would may have
1249         // an empty row on the screen
1250         if (cursor.pos() && cursorRow()->pos() == cursor.pos()
1251             && !cursor.par()->isNewline(cursor.pos() - 1))
1252         {
1253                 cursorLeft(bv());
1254         }
1255
1256         while (!next_par->empty() && next_par->isNewline(0))
1257                 next_par->erase(0);
1258
1259         updateCounters();
1260         redoParagraph(cursor.par());
1261         redoParagraph(next_par);
1262
1263         // This check is necessary. Otherwise the new empty paragraph will
1264         // be deleted automatically. And it is more friendly for the user!
1265         if (cursor.pos() || isempty)
1266                 setCursor(next_par, 0);
1267         else
1268                 setCursor(cursor.par(), 0);
1269 }
1270
1271
1272 // convenience function
1273 void LyXText::redoParagraph()
1274 {
1275         clearSelection();
1276         redoParagraph(cursor.par());
1277         setCursorIntern(cursor.par(), cursor.pos());
1278 }
1279
1280
1281 // insert a character, moves all the following breaks in the
1282 // same Paragraph one to the right and make a rebreak
1283 void LyXText::insertChar(char c)
1284 {
1285         recordUndo(bv(), Undo::INSERT, cursor.par());
1286
1287         // When the free-spacing option is set for the current layout,
1288         // disable the double-space checking
1289
1290         bool const freeSpacing = cursor.par()->layout()->free_spacing ||
1291                 cursor.par()->isFreeSpacing();
1292
1293         if (lyxrc.auto_number) {
1294                 static string const number_operators = "+-/*";
1295                 static string const number_unary_operators = "+-";
1296                 static string const number_seperators = ".,:";
1297
1298                 if (current_font.number() == LyXFont::ON) {
1299                         if (!IsDigit(c) && !contains(number_operators, c) &&
1300                             !(contains(number_seperators, c) &&
1301                               cursor.pos() >= 1 &&
1302                               cursor.pos() < cursor.par()->size() &&
1303                               getFont(cursor.par(), cursor.pos()).number() == LyXFont::ON &&
1304                               getFont(cursor.par(), cursor.pos() - 1).number() == LyXFont::ON)
1305                            )
1306                                 number(bv()); // Set current_font.number to OFF
1307                 } else if (IsDigit(c) &&
1308                            real_current_font.isVisibleRightToLeft()) {
1309                         number(bv()); // Set current_font.number to ON
1310
1311                         if (cursor.pos() > 0) {
1312                                 char const c = cursor.par()->getChar(cursor.pos() - 1);
1313                                 if (contains(number_unary_operators, c) &&
1314                                     (cursor.pos() == 1 ||
1315                                      cursor.par()->isSeparator(cursor.pos() - 2) ||
1316                                      cursor.par()->isNewline(cursor.pos() - 2))
1317                                   ) {
1318                                         setCharFont(
1319                                                     cursor.par(),
1320                                                     cursor.pos() - 1,
1321                                                     current_font);
1322                                 } else if (contains(number_seperators, c) &&
1323                                            cursor.pos() >= 2 &&
1324                                            getFont(
1325                                                    cursor.par(),
1326                                                    cursor.pos() - 2).number() == LyXFont::ON) {
1327                                         setCharFont(
1328                                                     cursor.par(),
1329                                                     cursor.pos() - 1,
1330                                                     current_font);
1331                                 }
1332                         }
1333                 }
1334         }
1335
1336
1337         // First check, if there will be two blanks together or a blank at
1338         // the beginning of a paragraph.
1339         // I decided to handle blanks like normal characters, the main
1340         // difference are the special checks when calculating the row.fill
1341         // (blank does not count at the end of a row) and the check here
1342
1343         // The bug is triggered when we type in a description environment:
1344         // The current_font is not changed when we go from label to main text
1345         // and it should (along with realtmpfont) when we type the space.
1346         // CHECK There is a bug here! (Asger)
1347
1348         // store the current font.  This is because of the use of cursor
1349         // movements. The moving cursor would refresh the current font
1350         LyXFont realtmpfont = real_current_font;
1351         LyXFont rawtmpfont = current_font;
1352
1353         if (!freeSpacing && IsLineSeparatorChar(c)) {
1354                 if ((cursor.pos() > 0
1355                      && cursor.par()->isLineSeparator(cursor.pos() - 1))
1356                     || (cursor.pos() > 0
1357                         && cursor.par()->isNewline(cursor.pos() - 1))
1358                     || (cursor.pos() == 0)) {
1359                         static bool sent_space_message = false;
1360                         if (!sent_space_message) {
1361                                 if (cursor.pos() == 0)
1362                                         bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1363                                 else
1364                                         bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1365                                 sent_space_message = true;
1366                         }
1367                         charInserted();
1368                         return;
1369                 }
1370         }
1371
1372         // Here case LyXText::InsertInset already inserted the character
1373         if (c != Paragraph::META_INSET)
1374                 cursor.par()->insertChar(cursor.pos(), c);
1375
1376         setCharFont(cursor.par(), cursor.pos(), rawtmpfont);
1377
1378         current_font = rawtmpfont;
1379         real_current_font = realtmpfont;
1380         redoParagraph(cursor.par());
1381         setCursor(cursor.par(), cursor.pos() + 1, false, cursor.boundary());
1382
1383         charInserted();
1384 }
1385
1386
1387 void LyXText::charInserted()
1388 {
1389         // Here we could call finishUndo for every 20 characters inserted.
1390         // This is from my experience how emacs does it. (Lgb)
1391         static unsigned int counter;
1392         if (counter < 20) {
1393                 ++counter;
1394         } else {
1395                 finishUndo();
1396                 counter = 0;
1397         }
1398 }
1399
1400
1401 void LyXText::prepareToPrint(ParagraphList::iterator pit,
1402            RowList::iterator const rit) const
1403 {
1404         double w = rit->fill();
1405         double fill_hfill = 0;
1406         double fill_label_hfill = 0;
1407         double fill_separator = 0;
1408         double x = 0;
1409
1410         bool const is_rtl =
1411                 pit->isRightToLeftPar(bv()->buffer()->params());
1412         if (is_rtl)
1413                 x = workWidth() > 0 ? rightMargin(pit, *bv()->buffer(), *rit) : 0;
1414         else
1415                 x = workWidth() > 0 ? leftMargin(pit, *rit) : 0;
1416
1417         // is there a manual margin with a manual label
1418         LyXLayout_ptr const & layout = pit->layout();
1419
1420         if (layout->margintype == MARGIN_MANUAL
1421             && layout->labeltype == LABEL_MANUAL) {
1422                 /// We might have real hfills in the label part
1423                 int nlh = numberOfLabelHfills(*pit, rit);
1424
1425                 // A manual label par (e.g. List) has an auto-hfill
1426                 // between the label text and the body of the
1427                 // paragraph too.
1428                 // But we don't want to do this auto hfill if the par
1429                 // is empty.
1430                 if (!pit->empty())
1431                         ++nlh;
1432
1433                 if (nlh && !pit->getLabelWidthString().empty()) {
1434                         fill_label_hfill = labelFill(pit, *rit) / double(nlh);
1435                 }
1436         }
1437
1438         // are there any hfills in the row?
1439         int const nh = numberOfHfills(*pit, rit);
1440
1441         if (nh) {
1442                 if (w > 0)
1443                         fill_hfill = w / nh;
1444         // we don't have to look at the alignment if it is ALIGN_LEFT and
1445         // if the row is already larger then the permitted width as then
1446         // we force the LEFT_ALIGN'edness!
1447         } else if (int(rit->width()) < workWidth()) {
1448                 // is it block, flushleft or flushright?
1449                 // set x how you need it
1450                 int align;
1451                 if (pit->params().align() == LYX_ALIGN_LAYOUT) {
1452                         align = layout->align;
1453                 } else {
1454                         align = pit->params().align();
1455                 }
1456                 InsetOld * inset = 0;
1457                 // ERT insets should always be LEFT ALIGNED on screen
1458                 inset = pit->inInset();
1459                 if (inset && inset->owner() &&
1460                         inset->owner()->lyxCode() == InsetOld::ERT_CODE)
1461                 {
1462                         align = LYX_ALIGN_LEFT;
1463                 }
1464
1465                 switch (align) {
1466             case LYX_ALIGN_BLOCK:
1467                 {
1468                         int const ns = numberOfSeparators(*pit, rit);
1469                         RowList::iterator next_row = boost::next(rit);
1470                         if (ns
1471                                 && next_row != pit->rows.end()
1472                                 && !pit->isNewline(next_row->pos() - 1)
1473                                 ) {
1474                                         fill_separator = w / ns;
1475                         } else if (is_rtl) {
1476                                 x += w;
1477                         }
1478                         break;
1479             }
1480             case LYX_ALIGN_RIGHT:
1481                         x += w;
1482                         break;
1483             case LYX_ALIGN_CENTER:
1484                         x += w / 2;
1485                         break;
1486                 }
1487         }
1488
1489         computeBidiTables(pit, *bv()->buffer(), rit);
1490         if (is_rtl) {
1491                 pos_type body_pos = pit->beginningOfBody();
1492                 pos_type last = lastPos(*pit, rit);
1493
1494                 if (body_pos > 0 &&
1495                                 (body_pos - 1 > last ||
1496                                  !pit->isLineSeparator(body_pos - 1))) {
1497                         x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1498                         if (body_pos - 1 <= last)
1499                                 x += fill_label_hfill;
1500                 }
1501         }
1502
1503         rit->fill_hfill(fill_hfill);
1504         rit->fill_label_hfill(fill_label_hfill);
1505         rit->fill_separator(fill_separator);
1506         rit->x(x);
1507 }
1508
1509
1510 // important for the screen
1511
1512
1513 // the cursor set functions have a special mechanism. When they
1514 // realize, that you left an empty paragraph, they will delete it.
1515 // They also delete the corresponding row
1516
1517 void LyXText::cursorRightOneWord()
1518 {
1519         ::cursorRightOneWord(cursor, ownerParagraphs());
1520         setCursor(cursor.par(), cursor.pos());
1521 }
1522
1523
1524 // Skip initial whitespace at end of word and move cursor to *start*
1525 // of prior word, not to end of next prior word.
1526 void LyXText::cursorLeftOneWord()
1527 {
1528         LyXCursor tmpcursor = cursor;
1529         ::cursorLeftOneWord(tmpcursor, ownerParagraphs());
1530         setCursor(tmpcursor.par(), tmpcursor.pos());
1531 }
1532
1533
1534 void LyXText::selectWord(word_location loc)
1535 {
1536         LyXCursor from = cursor;
1537         LyXCursor to;
1538         ::getWord(from, to, loc, ownerParagraphs());
1539         if (cursor != from)
1540                 setCursor(from.par(), from.pos());
1541         if (to == from)
1542                 return;
1543         selection.cursor = cursor;
1544         setCursor(to.par(), to.pos());
1545         setSelection();
1546 }
1547
1548
1549 // Select the word currently under the cursor when no
1550 // selection is currently set
1551 bool LyXText::selectWordWhenUnderCursor(word_location loc)
1552 {
1553         if (!selection.set()) {
1554                 selectWord(loc);
1555                 return selection.set();
1556         }
1557         return false;
1558 }
1559
1560
1561 void LyXText::acceptChange()
1562 {
1563         if (!selection.set() && cursor.par()->size())
1564                 return;
1565
1566         if (selection.start.par() == selection.end.par()) {
1567                 LyXCursor & startc = selection.start;
1568                 LyXCursor & endc = selection.end;
1569                 recordUndo(bv(), Undo::INSERT, startc.par());
1570                 startc.par()->acceptChange(startc.pos(), endc.pos());
1571                 finishUndo();
1572                 clearSelection();
1573                 redoParagraph(startc.par());
1574                 setCursorIntern(startc.par(), 0);
1575         }
1576 #warning handle multi par selection
1577 }
1578
1579
1580 void LyXText::rejectChange()
1581 {
1582         if (!selection.set() && cursor.par()->size())
1583                 return;
1584
1585         if (selection.start.par() == selection.end.par()) {
1586                 LyXCursor & startc = selection.start;
1587                 LyXCursor & endc = selection.end;
1588                 recordUndo(bv(), Undo::INSERT, startc.par());
1589                 startc.par()->rejectChange(startc.pos(), endc.pos());
1590                 finishUndo();
1591                 clearSelection();
1592                 redoParagraph(startc.par());
1593                 setCursorIntern(startc.par(), 0);
1594         }
1595 #warning handle multi par selection
1596 }
1597
1598
1599 // This function is only used by the spellchecker for NextWord().
1600 // It doesn't handle LYX_ACCENTs and probably never will.
1601 WordLangTuple const
1602 LyXText::selectNextWordToSpellcheck(float & value)
1603 {
1604         if (the_locking_inset) {
1605                 WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bv(), value);
1606                 if (!word.word().empty()) {
1607                         value += float(cursor.y());
1608                         value /= float(height);
1609                         return word;
1610                 }
1611                 // we have to go on checking so move cursor to the next char
1612                 if (cursor.pos() == cursor.par()->size()) {
1613                         if (boost::next(cursor.par()) == ownerParagraphs().end())
1614                                 return word;
1615                         cursor.par(boost::next(cursor.par()));
1616                         cursor.pos(0);
1617                 } else
1618                         cursor.pos(cursor.pos() + 1);
1619         }
1620         ParagraphList::iterator tmppit = cursor.par();
1621
1622         // If this is not the very first word, skip rest of
1623         // current word because we are probably in the middle
1624         // of a word if there is text here.
1625         if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
1626                 while (cursor.pos() < cursor.par()->size()
1627                        && cursor.par()->isLetter(cursor.pos()))
1628                         cursor.pos(cursor.pos() + 1);
1629         }
1630
1631         // Now, skip until we have real text (will jump paragraphs)
1632         while (true) {
1633                 ParagraphList::iterator cpit = cursor.par();
1634                 pos_type const cpos(cursor.pos());
1635
1636                 if (cpos == cpit->size()) {
1637                         if (boost::next(cpit) != ownerParagraphs().end()) {
1638                                 cursor.par(boost::next(cpit));
1639                                 cursor.pos(0);
1640                                 continue;
1641                         }
1642                         break;
1643                 }
1644
1645                 bool const is_good_inset = cpit->isInset(cpos)
1646                         && cpit->getInset(cpos)->allowSpellcheck();
1647
1648                 if (!isDeletedText(*cpit, cpos)
1649                     && (is_good_inset || cpit->isLetter(cpos)))
1650                         break;
1651
1652                 cursor.pos(cpos + 1);
1653         }
1654
1655         // now check if we hit an inset so it has to be a inset containing text!
1656         if (cursor.pos() < cursor.par()->size() &&
1657             cursor.par()->isInset(cursor.pos())) {
1658                 // lock the inset!
1659                 FuncRequest cmd(bv(), LFUN_INSET_EDIT, "left");
1660                 cursor.par()->getInset(cursor.pos())->localDispatch(cmd);
1661                 // now call us again to do the above trick
1662                 // but obviously we have to start from down below ;)
1663                 return bv()->text->selectNextWordToSpellcheck(value);
1664         }
1665
1666         // Update the value if we changed paragraphs
1667         if (cursor.par() != tmppit) {
1668                 setCursor(cursor.par(), cursor.pos());
1669                 value = float(cursor.y())/float(height);
1670         }
1671
1672         // Start the selection from here
1673         selection.cursor = cursor;
1674
1675         string lang_code = getFont(cursor.par(), cursor.pos()).language()->code();
1676         // and find the end of the word (insets like optional hyphens
1677         // and ligature break are part of a word)
1678         while (cursor.pos() < cursor.par()->size()
1679                && cursor.par()->isLetter(cursor.pos())
1680                && !isDeletedText(*cursor.par(), cursor.pos()))
1681                 cursor.pos(cursor.pos() + 1);
1682
1683         // Finally, we copy the word to a string and return it
1684         string str;
1685         if (selection.cursor.pos() < cursor.pos()) {
1686                 pos_type i;
1687                 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
1688                         if (!cursor.par()->isInset(i))
1689                                 str += cursor.par()->getChar(i);
1690                 }
1691         }
1692         return WordLangTuple(str, lang_code);
1693 }
1694
1695
1696 // This one is also only for the spellchecker
1697 void LyXText::selectSelectedWord()
1698 {
1699         if (the_locking_inset) {
1700                 the_locking_inset->selectSelectedWord(bv());
1701                 return;
1702         }
1703         // move cursor to the beginning
1704         setCursor(selection.cursor.par(), selection.cursor.pos());
1705
1706         // set the sel cursor
1707         selection.cursor = cursor;
1708
1709         // now find the end of the word
1710         while (cursor.pos() < cursor.par()->size()
1711                && cursor.par()->isLetter(cursor.pos()))
1712                 cursor.pos(cursor.pos() + 1);
1713
1714         setCursor(cursor.par(), cursor.pos());
1715
1716         // finally set the selection
1717         setSelection();
1718 }
1719
1720
1721 // Delete from cursor up to the end of the current or next word.
1722 void LyXText::deleteWordForward()
1723 {
1724         if (cursor.par()->empty())
1725                 cursorRight(bv());
1726         else {
1727                 LyXCursor tmpcursor = cursor;
1728                 selection.set(true); // to avoid deletion
1729                 cursorRightOneWord();
1730                 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1731                 selection.cursor = cursor;
1732                 cursor = tmpcursor;
1733                 setSelection();
1734
1735                 // Great, CutSelection() gets rid of multiple spaces.
1736                 cutSelection(true, false);
1737         }
1738 }
1739
1740
1741 // Delete from cursor to start of current or prior word.
1742 void LyXText::deleteWordBackward()
1743 {
1744         if (cursor.par()->empty())
1745                 cursorLeft(bv());
1746         else {
1747                 LyXCursor tmpcursor = cursor;
1748                 selection.set(true); // to avoid deletion
1749                 cursorLeftOneWord();
1750                 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1751                 selection.cursor = cursor;
1752                 cursor = tmpcursor;
1753                 setSelection();
1754                 cutSelection(true, false);
1755         }
1756 }
1757
1758
1759 // Kill to end of line.
1760 void LyXText::deleteLineForward()
1761 {
1762         if (cursor.par()->empty())
1763                 // Paragraph is empty, so we just go to the right
1764                 cursorRight(bv());
1765         else {
1766                 LyXCursor tmpcursor = cursor;
1767                 // We can't store the row over a regular setCursor
1768                 // so we set it to 0 and reset it afterwards.
1769                 selection.set(true); // to avoid deletion
1770                 cursorEnd();
1771                 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1772                 selection.cursor = cursor;
1773                 cursor = tmpcursor;
1774                 setSelection();
1775                 // What is this test for ??? (JMarc)
1776                 if (!selection.set()) {
1777                         deleteWordForward();
1778                 } else {
1779                         cutSelection(true, false);
1780                 }
1781         }
1782 }
1783
1784
1785 void LyXText::changeCase(LyXText::TextCase action)
1786 {
1787         LyXCursor from;
1788         LyXCursor to;
1789
1790         if (selection.set()) {
1791                 from = selection.start;
1792                 to = selection.end;
1793         } else {
1794                 from = cursor;
1795                 ::getWord(from, to, lyx::PARTIAL_WORD, ownerParagraphs());
1796                 setCursor(to.par(), to.pos() + 1);
1797         }
1798
1799         recordUndo(bv(), Undo::ATOMIC, from.par(), to.par());
1800
1801         pos_type pos = from.pos();
1802         ParagraphList::iterator pit = from.par();
1803
1804         while (pit != ownerParagraphs().end() &&
1805                (pos != to.pos() || pit != to.par())) {
1806                 if (pos == pit->size()) {
1807                         ++pit;
1808                         pos = 0;
1809                         continue;
1810                 }
1811                 unsigned char c = pit->getChar(pos);
1812                 if (!IsInsetChar(c)) {
1813                         switch (action) {
1814                         case text_lowercase:
1815                                 c = lowercase(c);
1816                                 break;
1817                         case text_capitalization:
1818                                 c = uppercase(c);
1819                                 action = text_lowercase;
1820                                 break;
1821                         case text_uppercase:
1822                                 c = uppercase(c);
1823                                 break;
1824                         }
1825                 }
1826 #warning changes
1827                 pit->setChar(pos, c);
1828                 ++pos;
1829         }
1830 }
1831
1832
1833 void LyXText::Delete()
1834 {
1835         // this is a very easy implementation
1836
1837         LyXCursor old_cursor = cursor;
1838         int const old_cur_par_id = old_cursor.par()->id();
1839         int const old_cur_par_prev_id =
1840                 (old_cursor.par() != ownerParagraphs().begin() ?
1841                  boost::prior(old_cursor.par())->id() : -1);
1842
1843         // just move to the right
1844         cursorRight(bv());
1845
1846         // CHECK Look at the comment here.
1847         // This check is not very good...
1848         // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
1849         // and that can very well delete the par or par->previous in
1850         // old_cursor. Will a solution where we compare paragraph id's
1851         //work better?
1852         if ((cursor.par() != ownerParagraphs().begin() ? boost::prior(cursor.par())->id() : -1)
1853             == old_cur_par_prev_id
1854             && cursor.par()->id() != old_cur_par_id) {
1855                 // delete-empty-paragraph-mechanism has done it
1856                 return;
1857         }
1858
1859         // if you had success make a backspace
1860         if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
1861                 LyXCursor tmpcursor = cursor;
1862                 // to make sure undo gets the right cursor position
1863                 cursor = old_cursor;
1864                 recordUndo(bv(), Undo::DELETE, cursor.par());
1865                 cursor = tmpcursor;
1866                 backspace();
1867         }
1868 }
1869
1870
1871 void LyXText::backspace()
1872 {
1873         // Get the font that is used to calculate the baselineskip
1874         pos_type lastpos = cursor.par()->size();
1875
1876         if (cursor.pos() == 0) {
1877                 // The cursor is at the beginning of a paragraph,
1878                 // so the the backspace will collapse two paragraphs into one.
1879
1880                 // but it's not allowed unless it's new
1881                 if (cursor.par()->isChangeEdited(0, cursor.par()->size()))
1882                         return;
1883
1884                 // we may paste some paragraphs
1885
1886                 // is it an empty paragraph?
1887
1888                 if (lastpos == 0
1889                      || (lastpos == 1 && cursor.par()->isSeparator(0))) {
1890                         // This is an empty paragraph and we delete it just
1891                         // by moving the cursor one step
1892                         // left and let the DeleteEmptyParagraphMechanism
1893                         // handle the actual deletion of the paragraph.
1894
1895                         if (cursor.par() != ownerParagraphs().begin()) {
1896                                 ParagraphList::iterator tmppit = boost::prior(cursor.par());
1897                                 if (cursor.par()->layout() == tmppit->layout()
1898                                     && cursor.par()->getAlign() == tmppit->getAlign()) {
1899                                         // Inherit bottom DTD from the paragraph below.
1900                                         // (the one we are deleting)
1901                                         tmppit->params().lineBottom(cursor.par()->params().lineBottom());
1902                                         tmppit->params().spaceBottom(cursor.par()->params().spaceBottom());
1903                                         tmppit->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
1904                                 }
1905
1906                                 cursorLeft(bv());
1907
1908                                 // the layout things can change the height of a row !
1909                                 redoParagraph();
1910                                 return;
1911                         }
1912                 }
1913
1914                 if (cursor.par() != ownerParagraphs().begin()) {
1915                         recordUndo(bv(), Undo::DELETE,
1916                                 boost::prior(cursor.par()),
1917                                 cursor.par());
1918                 }
1919
1920                 ParagraphList::iterator tmppit = cursor.par();
1921                 // We used to do cursorLeftIntern() here, but it is
1922                 // not a good idea since it triggers the auto-delete
1923                 // mechanism. So we do a cursorLeftIntern()-lite,
1924                 // without the dreaded mechanism. (JMarc)
1925                 if (cursor.par() != ownerParagraphs().begin()) {
1926                         // steps into the above paragraph.
1927                         setCursorIntern(boost::prior(cursor.par()),
1928                                         boost::prior(cursor.par())->size(),
1929                                         false);
1930                 }
1931
1932                 // Pasting is not allowed, if the paragraphs have different
1933                 // layout. I think it is a real bug of all other
1934                 // word processors to allow it. It confuses the user.
1935                 // Correction: Pasting is always allowed with standard-layout
1936                 Buffer & buf = *bv()->buffer();
1937                 BufferParams const & bufparams = buf.params();
1938                 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1939
1940                 if (cursor.par() != tmppit
1941                     && (cursor.par()->layout() == tmppit->layout()
1942                         || tmppit->layout() == tclass.defaultLayout())
1943                     && cursor.par()->getAlign() == tmppit->getAlign()) {
1944                         mergeParagraph(bufparams,
1945                                        buf.paragraphs(), cursor.par());
1946
1947                         if (cursor.pos() && cursor.par()->isSeparator(cursor.pos() - 1))
1948                                 cursor.pos(cursor.pos() - 1);
1949
1950                         // the row may have changed, block, hfills etc.
1951                         updateCounters();
1952                         setCursor(cursor.par(), cursor.pos(), false);
1953                 }
1954         } else {
1955                 // this is the code for a normal backspace, not pasting
1956                 // any paragraphs
1957                 recordUndo(bv(), Undo::DELETE, cursor.par());
1958                 // We used to do cursorLeftIntern() here, but it is
1959                 // not a good idea since it triggers the auto-delete
1960                 // mechanism. So we do a cursorLeftIntern()-lite,
1961                 // without the dreaded mechanism. (JMarc)
1962                 setCursorIntern(cursor.par(), cursor.pos() - 1,
1963                                 false, cursor.boundary());
1964                 cursor.par()->erase(cursor.pos());
1965         }
1966
1967         lastpos = cursor.par()->size();
1968         if (cursor.pos() == lastpos)
1969                 setCurrentFont();
1970
1971         redoParagraph();
1972         setCursor(cursor.par(), cursor.pos(), false, !cursor.boundary());
1973 }
1974
1975
1976 RowList::iterator LyXText::cursorRow() const
1977 {
1978         return getRow(cursor.par(), cursor.pos());
1979 }
1980
1981
1982 RowList::iterator LyXText::getRow(LyXCursor const & cur) const
1983 {
1984         return getRow(cur.par(), cur.pos());
1985 }
1986
1987
1988 RowList::iterator
1989 LyXText::getRow(ParagraphList::iterator pit, pos_type pos) const
1990 {
1991         RowList::iterator rit = boost::prior(pit->rows.end());
1992         RowList::iterator const begin = pit->rows.begin();
1993
1994         while (rit != begin && rit->pos() > pos)
1995                 --rit;
1996
1997         return rit;
1998 }
1999
2000
2001 // returns pointer to some fancy row 'below' specified row
2002 RowList::iterator LyXText::cursorIRow() const
2003 {
2004         return getRow(cursor.par(), cursor.pos());
2005 }
2006
2007
2008 RowList::iterator LyXText::getRowNearY(int y,
2009         ParagraphList::iterator & pit) const
2010 {
2011         //lyxerr << "getRowNearY: y " << y << endl;
2012
2013         pit = boost::prior(ownerParagraphs().end());
2014
2015         RowList::iterator rit = lastRow();
2016         RowList::iterator rbegin = firstRow();
2017
2018         while (rit != rbegin && static_cast<int>(rit->y()) > y)
2019                 previousRow(pit, rit);
2020
2021         return rit;
2022 }
2023
2024
2025 int LyXText::getDepth() const
2026 {
2027         return cursor.par()->getDepth();
2028 }
2029
2030
2031 RowList::iterator LyXText::firstRow() const
2032 {
2033         return ownerParagraphs().front().rows.begin();
2034 }
2035
2036
2037 RowList::iterator LyXText::lastRow() const
2038 {
2039         return boost::prior(endRow());
2040 }
2041
2042
2043 RowList::iterator LyXText::endRow() const
2044 {
2045         return ownerParagraphs().back().rows.end();
2046 }
2047
2048
2049 void LyXText::nextRow(ParagraphList::iterator & pit,
2050         RowList::iterator & rit) const
2051 {
2052         ++rit;
2053         if (rit == pit->rows.end()) {
2054                 ++pit;
2055                 if (pit == ownerParagraphs().end())
2056                         --pit;
2057                 else
2058                         rit = pit->rows.begin();
2059         }
2060 }
2061
2062
2063 void LyXText::previousRow(ParagraphList::iterator & pit,
2064         RowList::iterator & rit) const
2065 {
2066         if (rit != pit->rows.begin())
2067                 --rit;
2068         else {
2069                 BOOST_ASSERT(pit != ownerParagraphs().begin());
2070                 --pit;
2071                 rit = boost::prior(pit->rows.end());
2072         }
2073 }
2074
2075
2076 bool LyXText::noRows() const
2077 {
2078         return ownerParagraphs().begin()->rows.empty();
2079 }