]> git.lyx.org Git - lyx.git/blob - src/text.C
Boost.Format patch from Herber
[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 "WordLangTuple.h"
34 #include "paragraph_funcs.h"
35
36 #include "insets/insetbib.h"
37 #include "insets/insettext.h"
38
39 #include "support/textutils.h"
40 #include "support/LAssert.h"
41 #include "support/lstrings.h"
42
43 #include <algorithm>
44
45 using std::max;
46 using std::min;
47 using std::endl;
48 using std::pair;
49 using lyx::pos_type;
50
51 namespace {
52
53 int const LYX_PAPER_MARGIN = 20;
54
55 } // namespace anon
56
57 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
58
59
60 int LyXText::workWidth(BufferView * bview) const
61 {
62         if (inset_owner) {
63                 return inset_owner->textWidth(bview);
64         }
65         return bview->workWidth();
66 }
67
68
69 int LyXText::workWidth(BufferView * bview, Inset * inset) const
70 {
71         Paragraph * par = 0;
72         pos_type pos = -1;
73
74         par = inset->parOwner();
75         if (par)
76                 pos = par->getPositionOfInset(inset);
77
78         if (!par || pos == -1) {
79                 lyxerr << "LyXText::workWidth: something is wrong,"
80                         " fall back to the brute force method" << endl;
81                 Buffer::inset_iterator it = bview->buffer()->inset_iterator_begin();
82                 Buffer::inset_iterator end = bview->buffer()->inset_iterator_end();
83                 for (; it != end; ++it) {
84                         if (&(*it) == inset) {
85                                 par = it.getPar();
86                                 pos = it.getPos();
87                                 break;
88                         }
89                 }
90         }
91
92         if (!par) {
93                 return workWidth(bview);
94         }
95
96         LyXLayout_ptr const & layout = par->layout();
97
98         if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
99                 // Optimization here: in most cases, the real row is
100                 // not needed, but only the par/pos values. So we just
101                 // construct a dummy row for leftMargin. (JMarc)
102                 Row dummyrow;
103                 dummyrow.par(par);
104                 dummyrow.pos(pos);
105                 return workWidth(bview) - leftMargin(bview, &dummyrow);
106         } else {
107                 int dummy_y;
108                 Row * row = getRow(par, pos, dummy_y);
109                 Row * frow = row;
110                 while (frow->previous() && frow->par() == frow->previous()->par())
111                         frow = frow->previous();
112                 unsigned int maxw = 0;
113                 while (frow->next() && frow->par() == frow->next()->par()) {
114                         if ((frow != row) && (maxw < frow->width()))
115                                 maxw = frow->width();
116                         frow = frow->next();
117                 }
118                 if (maxw)
119                         return maxw;
120         }
121         return workWidth(bview);
122 }
123
124
125 int LyXText::getRealCursorX(BufferView * bview) const
126 {
127         int x = cursor.x();
128         if (the_locking_inset && (the_locking_inset->getLyXText(bview)!=this))
129                 x = the_locking_inset->getLyXText(bview)->getRealCursorX(bview);
130         return x;
131 }
132
133
134 unsigned char LyXText::transformChar(unsigned char c, Paragraph * par,
135                         pos_type pos) const
136 {
137         if (!Encodings::is_arabic(c))
138                 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
139                         return c + (0xb0 - '0');
140                 else
141                         return c;
142
143         unsigned char const prev_char = pos > 0 ? par->getChar(pos-1) : ' ';
144         unsigned char next_char = ' ';
145
146         for (pos_type i = pos+1; i < par->size(); ++i)
147                 if (!Encodings::IsComposeChar_arabic(par->getChar(i))) {
148                         next_char = par->getChar(i);
149                         break;
150                 }
151
152         if (Encodings::is_arabic(next_char)) {
153                 if (Encodings::is_arabic(prev_char))
154                         return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
155                 else
156                         return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
157         } else {
158                 if (Encodings::is_arabic(prev_char))
159                         return Encodings::TransformChar(c, Encodings::FORM_FINAL);
160                 else
161                         return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
162         }
163 }
164
165 // This is the comments that some of the warnings below refers to.
166 // There are some issues in this file and I don't think they are
167 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
168 // this is a problem that has been here almost from day one and that a
169 // larger userbase with differenct access patters triggers the bad
170 // behaviour. (segfaults.) What I think happen is: In several places
171 // we store the paragraph in the current cursor and then moves the
172 // cursor. This movement of the cursor will delete paragraph at the
173 // old position if it is now empty. This will make the temporary
174 // pointer to the old cursor paragraph invalid and dangerous to use.
175 // And is some cases this will trigger a segfault. I have marked some
176 // of the cases where this happens with a warning, but I am sure there
177 // are others in this file and in text2.C. There is also a note in
178 // Delete() that you should read. In Delete I store the paragraph->id
179 // instead of a pointer to the paragraph. I am pretty sure this faulty
180 // use of temporary pointers to paragraphs that might have gotten
181 // invalidated (through a cursor movement) before they are used, are
182 // the cause of the strange crashes we get reported often.
183 //
184 // It is very tiresom to change this code, especially when it is as
185 // hard to read as it is. Help to fix all the cases where this is done
186 // would be greately appreciated.
187 //
188 // Lgb
189
190 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
191                          pos_type pos) const
192 {
193         char const c = par->getChar(pos);
194         return singleWidth(bview, par, pos, c);
195 }
196
197
198 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
199                          pos_type pos, char c) const
200 {
201         LyXFont const font = getFont(bview->buffer(), par, pos);
202
203         // The most common case is handled first (Asger)
204         if (IsPrintable(c)) {
205                 if (font.language()->RightToLeft()) {
206                         if (font.language()->lang() == "arabic" &&
207                             (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
208                              lyxrc.font_norm_type == LyXRC::ISO_10646_1))
209                                 if (Encodings::IsComposeChar_arabic(c))
210                                         return 0;
211                                 else
212                                         c = transformChar(c, par, 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         } else if (IsHfillChar(c)) {
220                 // Because of the representation as vertical lines
221                 return 3;
222         } else if (c == Paragraph::META_INSET) {
223                 Inset * tmpinset = par->getInset(pos);
224                 if (tmpinset) {
225 #if 1
226                         // this IS needed otherwise on initialitation we don't get the fill
227                         // of the row right (ONLY on initialization if we read a file!)
228                         // should be changed! (Jug 20011204)
229                         tmpinset->update(bview, font);
230 #endif
231                         return tmpinset->width(bview, font);
232                 } else
233                         return 0;
234
235         } else if (IsSeparatorChar(c))
236                 c = ' ';
237         else if (IsNewlineChar(c))
238                 c = 'n';
239         return font_metrics::width(c, font);
240 }
241
242
243 // Returns the paragraph position of the last character in the specified row
244 pos_type LyXText::rowLast(Row const * row) const
245 {
246         if (!row->next() || row->next()->par() != row->par()) {
247                 return row->par()->size() - 1;
248         } else {
249                 return row->next()->pos() - 1;
250         }
251 }
252
253
254 pos_type LyXText::rowLastPrintable(Row const * row) const
255 {
256         pos_type const last = rowLast(row);
257         bool ignore_the_space_on_the_last_position = true;
258         Inset * ins;
259         // we have to consider a space on the last position in this case!
260         if (row->next() && row->par() == row->next()->par() &&
261             row->next()->par()->getChar(last+1) == Paragraph::META_INSET &&
262             (ins=row->next()->par()->getInset(last+1)) &&
263             (ins->needFullRow() || ins->display()))
264         {
265                 ignore_the_space_on_the_last_position = false;
266         }
267         if (last >= row->pos()
268             && row->next()
269             && row->next()->par() == row->par()
270             && row->par()->isSeparator(last)
271                 && ignore_the_space_on_the_last_position)
272                 return last - 1;
273         else
274                 return last;
275 }
276
277
278 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
279 {
280         bidi_same_direction = true;
281         if (!lyxrc.rtl_support) {
282                 bidi_start = -1;
283                 return;
284         }
285
286         Inset * inset = row->par()->inInset();
287         if (inset && inset->owner() &&
288             inset->owner()->lyxCode() == Inset::ERT_CODE) {
289                 bidi_start = -1;
290                 return;
291         }
292
293         bidi_start = row->pos();
294         bidi_end = rowLastPrintable(row);
295
296         if (bidi_start > bidi_end) {
297                 bidi_start = -1;
298                 return;
299         }
300
301         if (bidi_end + 2 - bidi_start >
302             static_cast<pos_type>(log2vis_list.size())) {
303                 pos_type new_size =
304                         (bidi_end + 2 - bidi_start < 500) ?
305                         500 : 2 * (bidi_end + 2 - bidi_start);
306                 log2vis_list.resize(new_size);
307                 vis2log_list.resize(new_size);
308                 bidi_levels.resize(new_size);
309         }
310
311         vis2log_list[bidi_end + 1 - bidi_start] = -1;
312         log2vis_list[bidi_end + 1 - bidi_start] = -1;
313
314         pos_type stack[2];
315         bool const rtl_par =
316                 row->par()->isRightToLeftPar(buf->params);
317         int level = 0;
318         bool rtl = false;
319         bool rtl0 = false;
320         pos_type const main_body = beginningOfMainBody(buf, row->par());
321
322         for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
323                 bool is_space = row->par()->isLineSeparator(lpos);
324                 pos_type const pos =
325                         (is_space && lpos + 1 <= bidi_end &&
326                          !row->par()->isLineSeparator(lpos + 1) &&
327                          !row->par()->isNewline(lpos + 1))
328                         ? lpos + 1 : lpos;
329                 LyXFont font = row->par()->getFontSettings(buf->params, pos);
330                 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
331                     font.number() == LyXFont::ON &&
332                     row->par()->getFontSettings(buf->params, lpos - 1).number()
333                     == LyXFont::ON) {
334                         font = row->par()->getFontSettings(buf->params, lpos);
335                         is_space = false;
336                 }
337
338
339                 bool new_rtl = font.isVisibleRightToLeft();
340                 bool new_rtl0 = font.isRightToLeft();
341                 int new_level;
342
343                 if (lpos == main_body - 1
344                     && row->pos() < main_body - 1
345                     && is_space) {
346                         new_level = (rtl_par) ? 1 : 0;
347                         new_rtl = new_rtl0 = rtl_par;
348                 } else if (new_rtl0)
349                         new_level = (new_rtl) ? 1 : 2;
350                 else
351                         new_level = (rtl_par) ? 2 : 0;
352
353                 if (is_space && new_level >= level) {
354                         new_level = level;
355                         new_rtl = rtl;
356                         new_rtl0 = rtl0;
357                 }
358
359                 int new_level2 = new_level;
360
361                 if (level == new_level && rtl0 != new_rtl0) {
362                         --new_level2;
363                         log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
364                 } else if (level < new_level) {
365                         log2vis_list[lpos - bidi_start] =  (rtl) ? -1 : 1;
366                         if (new_level > rtl_par)
367                                 bidi_same_direction = false;
368                 } else
369                         log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
370                 rtl = new_rtl;
371                 rtl0 = new_rtl0;
372                 bidi_levels[lpos - bidi_start] = new_level;
373
374                 while (level > new_level2) {
375                         pos_type old_lpos = stack[--level];
376                         int delta = lpos - old_lpos - 1;
377                         if (level % 2)
378                                 delta = -delta;
379                         log2vis_list[lpos - bidi_start] += delta;
380                         log2vis_list[old_lpos - bidi_start] += delta;
381                 }
382                 while (level < new_level)
383                         stack[level++] = lpos;
384         }
385
386         while (level > 0) {
387                 pos_type const old_lpos = stack[--level];
388                 int delta = bidi_end - old_lpos;
389                 if (level % 2)
390                         delta = -delta;
391                 log2vis_list[old_lpos - bidi_start] += delta;
392         }
393
394         pos_type vpos = bidi_start - 1;
395         for (pos_type lpos = bidi_start;
396              lpos <= bidi_end; ++lpos) {
397                 vpos += log2vis_list[lpos - bidi_start];
398                 vis2log_list[vpos - bidi_start] = lpos;
399                 log2vis_list[lpos - bidi_start] = vpos;
400         }
401 }
402
403
404 // This method requires a previous call to ComputeBidiTables()
405 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
406                          pos_type pos) const
407 {
408         if (!lyxrc.rtl_support || pos == 0)
409                 return false;
410
411         if (!bidi_InRange(pos - 1)) {
412                 /// This can happen if pos is the first char of a row.
413                 /// Returning false in this case is incorrect!
414                 return false;
415         }
416
417         bool const rtl = bidi_level(pos - 1) % 2;
418         bool const rtl2 = bidi_InRange(pos)
419                 ? bidi_level(pos) % 2
420                 : par->isRightToLeftPar(buf->params);
421         return rtl != rtl2;
422 }
423
424
425 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
426                          pos_type pos, LyXFont const & font) const
427 {
428         if (!lyxrc.rtl_support)
429                 return false;    // This is just for speedup
430
431         bool const rtl = font.isVisibleRightToLeft();
432         bool const rtl2 = bidi_InRange(pos)
433                 ? bidi_level(pos) % 2
434                 : par->isRightToLeftPar(buf->params);
435         return rtl != rtl2;
436 }
437
438
439 void LyXText::drawNewline(DrawRowParams & p, pos_type const pos)
440 {
441         // Draw end-of-line marker
442         LyXFont const font = getFont(p.bv->buffer(), p.row->par(), pos);
443         int const wid = font_metrics::width('n', font);
444         int const asc = font_metrics::maxAscent(font);
445         int const y = p.yo + p.row->baseline();
446         int xp[3];
447         int yp[3];
448
449         yp[0] = int(y - 0.875 * asc * 0.75);
450         yp[1] = int(y - 0.500 * asc * 0.75);
451         yp[2] = int(y - 0.125 * asc * 0.75);
452
453         if (bidi_level(pos) % 2 == 0) {
454                 xp[0] = int(p.x + wid * 0.375);
455                 xp[1] = int(p.x);
456                 xp[2] = int(p.x + wid * 0.375);
457         } else {
458                 xp[0] = int(p.x + wid * 0.625);
459                 xp[1] = int(p.x + wid);
460                 xp[2] = int(p.x + wid * 0.625);
461         }
462
463         p.pain->lines(xp, yp, 3, LColor::eolmarker);
464
465         yp[0] = int(y - 0.500 * asc * 0.75);
466         yp[1] = int(y - 0.500 * asc * 0.75);
467         yp[2] = int(y - asc * 0.75);
468
469         if (bidi_level(pos) % 2 == 0) {
470                 xp[0] = int(p.x);
471                 xp[1] = int(p.x + wid);
472                 xp[2] = int(p.x + wid);
473         } else {
474                 xp[0] = int(p.x + wid);
475                 xp[1] = int(p.x);
476                 xp[2] = int(p.x);
477         }
478
479         p.pain->lines(xp, yp, 3, LColor::eolmarker);
480
481         p.x += wid;
482 }
483
484
485 bool LyXText::drawInset(DrawRowParams & p, pos_type const pos)
486 {
487         Inset * inset = p.row->par()->getInset(pos);
488
489         // FIXME: shouldn't happen
490         if (!inset) {
491                 return true;
492         }
493
494         LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
495         // we need this here as the row pointer may be illegal
496         // at a later time (Jug20020502)
497         Row * prev = p.row->previous();
498
499         inset->update(p.bv, font, false);
500         inset->draw(p.bv, font, p.yo + p.row->baseline(), p.x, p.cleared);
501
502         if (!need_break_row && !inset_owner
503             && p.bv->text->status() == CHANGED_IN_DRAW) {
504                 if (prev && prev->par() == p.row->par()) {
505                         breakAgainOneRow(p.bv, prev);
506                         if (prev->next() != p.row) {
507                                 // breakAgainOneRow() has removed p.row
508                                 p.row = 0;  // see what this breaks
509                                 need_break_row = prev;
510                         } else {
511                                 need_break_row = p.row;
512                         }
513                 } else if (!prev) {
514                         need_break_row = firstrow;
515                 } else {
516                         need_break_row = prev->next();
517                 }
518                 setCursor(p.bv, cursor.par(), cursor.pos());
519                 return false;
520         }
521         return true;
522 }
523
524
525 void LyXText::drawForeignMark(DrawRowParams & p, float const orig_x, LyXFont const & orig_font)
526 {
527         if (!lyxrc.mark_foreign_language)
528                 return;
529         if (orig_font.language() == latex_language)
530                 return;
531         if (orig_font.language() == p.bv->buffer()->params.language)
532                 return;
533
534         int const y = p.yo + p.row->height() - 1;
535         p.pain->line(int(orig_x), y, int(p.x), y, LColor::language);
536 }
537
538
539 void LyXText::drawHebrewComposeChar(DrawRowParams & p, pos_type & vpos)
540 {
541         pos_type pos = vis2log(vpos);
542
543         string str;
544
545         // first char
546         char c = p.row->par()->getChar(pos);
547         str += c;
548         ++vpos;
549
550         LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
551         int const width = font_metrics::width(c, font);
552         int dx = 0;
553
554         for (pos_type i = pos-1; i >= 0; --i) {
555                 c = p.row->par()->getChar(i);
556                 if (!Encodings::IsComposeChar_hebrew(c)) {
557                         if (IsPrintableNonspace(c)) {
558                                 int const width2 =
559                                         singleWidth(p.bv, p.row->par(), i, c);
560                                 // dalet / resh
561                                 dx = (c == 'ø' || c == 'ã')
562                                         ? width2 - width
563                                         : (width2 - width) / 2;
564                         }
565                         break;
566                 }
567         }
568
569         // Draw nikud
570         p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
571 }
572
573
574 void LyXText::drawArabicComposeChar(DrawRowParams & p, pos_type & vpos)
575 {
576         pos_type pos = vis2log(vpos);
577         string str;
578
579         // first char
580         char c = p.row->par()->getChar(pos);
581         c = transformChar(c, p.row->par(), pos);
582         str +=c;
583         ++vpos;
584
585         LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
586         int const width = font_metrics::width(c, font);
587         int dx = 0;
588
589         for (pos_type i = pos-1; i >= 0; --i) {
590                 c = p.row->par()->getChar(i);
591                 if (!Encodings::IsComposeChar_arabic(c)) {
592                         if (IsPrintableNonspace(c)) {
593                                 int const width2 =
594                                         singleWidth(p.bv, p.row->par(), i, c);
595                                 dx = (width2 - width) / 2;
596                         }
597                         break;
598                 }
599         }
600         // Draw nikud
601         p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
602 }
603
604
605 void LyXText::drawChars(DrawRowParams & p, pos_type & vpos,
606                         bool hebrew, bool arabic)
607 {
608         pos_type pos = vis2log(vpos);
609         pos_type const last = rowLastPrintable(p.row);
610         LyXFont const & orig_font = getFont(p.bv->buffer(), p.row->par(), pos);
611
612         // first character
613         string str;
614         str += p.row->par()->getChar(pos);
615         if (arabic) {
616                 unsigned char c = str[0];
617                 str[0] = transformChar(c, p.row->par(), pos);
618         }
619         ++vpos;
620
621         // collect as much similar chars as we can
622         while (vpos <= last && (pos = vis2log(vpos)) >= 0) {
623                 char c = p.row->par()->getChar(pos);
624
625                 if (!IsPrintableNonspace(c))
626                         break;
627
628                 if (arabic && Encodings::IsComposeChar_arabic(c))
629                         break;
630                 if (hebrew && Encodings::IsComposeChar_hebrew(c))
631                         break;
632
633                 if (orig_font != getFont(p.bv->buffer(), p.row->par(), pos))
634                         break;
635
636                 if (arabic)
637                         c = transformChar(c, p.row->par(), pos);
638                 str += c;
639                 ++vpos;
640         }
641
642         // Draw text and set the new x position
643         p.pain->text(int(p.x), p.yo + p.row->baseline(), str, orig_font);
644         p.x += font_metrics::width(str, orig_font);
645 }
646
647
648 bool LyXText::draw(DrawRowParams & p, pos_type & vpos)
649 {
650         pos_type const pos = vis2log(vpos);
651         Paragraph * par = p.row->par();
652
653         LyXFont const & orig_font = getFont(p.bv->buffer(), par, pos);
654
655         float const orig_x = p.x;
656
657         char const c = par->getChar(pos);
658
659         if (IsNewlineChar(c)) {
660                 ++vpos;
661                 drawNewline(p, pos);
662                 return true;
663         } else if (IsInsetChar(c)) {
664                 if (!drawInset(p, pos))
665                         return false;
666                 ++vpos;
667                 drawForeignMark(p, orig_x, orig_font);
668                 return true;
669         }
670
671         // usual characters, no insets
672
673         // special case languages
674         bool const hebrew = (orig_font.language()->lang() == "hebrew");
675         bool const arabic =
676                 orig_font.language()->lang() == "arabic" &&
677                 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
678                 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
679
680         // draw as many chars as we can
681         if ((!hebrew && !arabic)
682                 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
683                 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
684                 drawChars(p, vpos, hebrew, arabic);
685         } else if (hebrew) {
686                 drawHebrewComposeChar(p, vpos);
687         } else if (arabic) {
688                 drawArabicComposeChar(p, vpos);
689         }
690
691         drawForeignMark(p, orig_x, orig_font);
692
693         return true;
694 }
695
696
697 int LyXText::leftMargin(BufferView * bview, Row const * row) const
698 {
699         Inset * ins;
700         if ((row->par()->getChar(row->pos()) == Paragraph::META_INSET) &&
701                 (ins=row->par()->getInset(row->pos())) &&
702                 (ins->needFullRow() || ins->display()))
703                 return LYX_PAPER_MARGIN;
704
705         LyXTextClass const & tclass =
706                 bview->buffer()->params.getLyXTextClass();
707         LyXLayout_ptr const & layout = row->par()->layout();
708
709         string parindent = layout->parindent;
710
711         int x = LYX_PAPER_MARGIN;
712
713         x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
714
715         // this is the way, LyX handles the LaTeX-Environments.
716         // I have had this idea very late, so it seems to be a
717         // later added hack and this is true
718         if (!row->par()->getDepth()) {
719                 if (row->par()->layout() == tclass.defaultLayout()) {
720                         // find the previous same level paragraph
721                         if (row->par()->previous()) {
722                                 Paragraph * newpar = row->par()
723                                         ->depthHook(row->par()->getDepth());
724                                 if (newpar &&
725                                     newpar->layout()->nextnoindent)
726                                         parindent.erase();
727                         }
728                 }
729         } else {
730                 // find the next level paragraph
731
732                 Paragraph * newpar = row->par()->outerHook();
733
734                 // make a corresponding row. Needed to call LeftMargin()
735
736                 // check wether it is a sufficent paragraph
737                 if (newpar && newpar->layout()->isEnvironment()) {
738                         Row dummyrow;
739                         dummyrow.par(newpar);
740                         dummyrow.pos(newpar->size());
741                         x = leftMargin(bview, &dummyrow);
742                 } else {
743                         // this is no longer an error, because this function
744                         // is used to clear impossible depths after changing
745                         // a layout. Since there is always a redo,
746                         // LeftMargin() is always called
747                         row->par()->params().depth(0);
748                 }
749
750                 if (newpar && row->par()->layout() == tclass.defaultLayout()) {
751                         if (newpar->params().noindent())
752                                 parindent.erase();
753                         else {
754                                 parindent = newpar->layout()->parindent;
755                         }
756
757                 }
758         }
759
760         LyXFont const labelfont = getLabelFont(bview->buffer(), row->par());
761         switch (layout->margintype) {
762         case MARGIN_DYNAMIC:
763                 if (!layout->leftmargin.empty()) {
764                         x += font_metrics::signedWidth(layout->leftmargin,
765                                                   tclass.defaultfont());
766                 }
767                 if (!row->par()->getLabelstring().empty()) {
768                         x += font_metrics::signedWidth(layout->labelindent,
769                                                   labelfont);
770                         x += font_metrics::width(row->par()->getLabelstring(),
771                                             labelfont);
772                         x += font_metrics::width(layout->labelsep, labelfont);
773                 }
774                 break;
775         case MARGIN_MANUAL:
776                 x += font_metrics::signedWidth(layout->labelindent, labelfont);
777                 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
778                         if (!row->par()->getLabelWidthString().empty()) {
779                                 x += font_metrics::width(row->par()->getLabelWidthString(),
780                                                labelfont);
781                                 x += font_metrics::width(layout->labelsep, labelfont);
782                         }
783                 }
784                 break;
785         case MARGIN_STATIC:
786                 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
787                         / (row->par()->getDepth() + 4);
788                 break;
789         case MARGIN_FIRST_DYNAMIC:
790                 if (layout->labeltype == LABEL_MANUAL) {
791                         if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
792                                 x += font_metrics::signedWidth(layout->leftmargin,
793                                                           labelfont);
794                         } else {
795                                 x += font_metrics::signedWidth(layout->labelindent,
796                                                           labelfont);
797                         }
798                 } else if (row->pos()
799                            // Special case to fix problems with
800                            // theorems (JMarc)
801                            || (layout->labeltype == LABEL_STATIC
802                                && layout->latextype == LATEX_ENVIRONMENT
803                                && ! row->par()->isFirstInSequence())) {
804                         x += font_metrics::signedWidth(layout->leftmargin,
805                                                   labelfont);
806                 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
807                            && layout->labeltype != LABEL_BIBLIO
808                            && layout->labeltype !=
809                            LABEL_CENTERED_TOP_ENVIRONMENT) {
810                         x += font_metrics::signedWidth(layout->labelindent,
811                                                   labelfont);
812                         x += font_metrics::width(layout->labelsep, labelfont);
813                         x += font_metrics::width(row->par()->getLabelstring(),
814                                             labelfont);
815                 }
816                 break;
817
818         case MARGIN_RIGHT_ADDRESS_BOX:
819         {
820                 // ok, a terrible hack. The left margin depends on the widest
821                 // row in this paragraph. Do not care about footnotes, they
822                 // are *NOT* allowed in the LaTeX realisation of this layout.
823
824                 // find the first row of this paragraph
825                 Row const * tmprow = row;
826                 while (tmprow->previous()
827                        && tmprow->previous()->par() == row->par())
828                         tmprow = tmprow->previous();
829
830                 int minfill = tmprow->fill();
831                 while (tmprow->next() && tmprow->next()->par() == row->par()) {
832                         tmprow = tmprow->next();
833                         if (tmprow->fill() < minfill)
834                                 minfill = tmprow->fill();
835                 }
836
837                 x += font_metrics::signedWidth(layout->leftmargin,
838                         tclass.defaultfont());
839                 x += minfill;
840         }
841         break;
842         }
843
844         if ((workWidth(bview) > 0) &&
845                 !row->par()->params().leftIndent().zero())
846         {
847                 LyXLength const len = row->par()->params().leftIndent();
848                 int const tw = inset_owner ?
849                         inset_owner->latexTextWidth(bview) : workWidth(bview);
850                 x += len.inPixels(tw);
851         }
852
853         LyXAlignment align; // wrong type
854
855         if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
856                 align = layout->align;
857         else
858                 align = row->par()->params().align();
859
860         // set the correct parindent
861         if (row->pos() == 0) {
862                 if ((layout->labeltype == LABEL_NO_LABEL
863                      || layout->labeltype == LABEL_TOP_ENVIRONMENT
864                      || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
865                      || (layout->labeltype == LABEL_STATIC
866                          && layout->latextype == LATEX_ENVIRONMENT
867                          && ! row->par()->isFirstInSequence()))
868                     && align == LYX_ALIGN_BLOCK
869                     && !row->par()->params().noindent()
870                         // in tabulars and ert paragraphs are never indented!
871                         && (!row->par()->inInset() || !row->par()->inInset()->owner() ||
872                                 (row->par()->inInset()->owner()->lyxCode() != Inset::TABULAR_CODE &&
873                                  row->par()->inInset()->owner()->lyxCode() != Inset::ERT_CODE))
874                     && (row->par()->layout() != tclass.defaultLayout() ||
875                         bview->buffer()->params.paragraph_separation ==
876                         BufferParams::PARSEP_INDENT)) {
877                         x += font_metrics::signedWidth(parindent,
878                                                   tclass.defaultfont());
879                 } else if (layout->labeltype == LABEL_BIBLIO) {
880                         // ale970405 Right width for bibitems
881                         x += bibitemMaxWidth(bview, tclass.defaultfont());
882                 }
883         }
884
885         return x;
886 }
887
888
889 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
890 {
891         Inset * ins;
892         if ((row->par()->getChar(row->pos()) == Paragraph::META_INSET) &&
893                 (ins=row->par()->getInset(row->pos())) &&
894                 (ins->needFullRow() || ins->display()))
895                 return LYX_PAPER_MARGIN;
896
897         LyXTextClass const & tclass = buf->params.getLyXTextClass();
898         LyXLayout_ptr const & layout = row->par()->layout();
899
900         int x = LYX_PAPER_MARGIN
901                 + font_metrics::signedWidth(tclass.rightmargin(),
902                                        tclass.defaultfont());
903
904         // this is the way, LyX handles the LaTeX-Environments.
905         // I have had this idea very late, so it seems to be a
906         // later added hack and this is true
907         if (row->par()->getDepth()) {
908                 // find the next level paragraph
909
910                 Paragraph * newpar = row->par();
911
912                 do {
913                         newpar = newpar->previous();
914                 } while (newpar
915                          && newpar->getDepth() >= row->par()->getDepth());
916
917                 // make a corresponding row. Needed to call LeftMargin()
918
919                 // check wether it is a sufficent paragraph
920                 if (newpar && newpar->layout()->isEnvironment()) {
921                         Row dummyrow;
922                         dummyrow.par(newpar);
923                         dummyrow.pos(0);
924                         x = rightMargin(buf, &dummyrow);
925                 } else {
926                         // this is no longer an error, because this function
927                         // is used to clear impossible depths after changing
928                         // a layout. Since there is always a redo,
929                         // LeftMargin() is always called
930                         row->par()->params().depth(0);
931                 }
932         }
933
934         //lyxerr << "rightmargin: " << layout->rightmargin << endl;
935         x += font_metrics::signedWidth(layout->rightmargin,
936                                        tclass.defaultfont())
937                 * 4 / (row->par()->getDepth() + 4);
938         return x;
939 }
940
941
942 int LyXText::labelEnd(BufferView * bview, Row const * row) const
943 {
944         if (row->par()->layout()->margintype == MARGIN_MANUAL) {
945                 Row tmprow;
946                 tmprow = *row;
947                 tmprow.pos(row->par()->size());
948                 // just the beginning of the main body
949                 return leftMargin(bview, &tmprow);
950         } else {
951                 // LabelEnd is only needed,
952                 // if the layout fills a flushleft label.
953                 return 0;
954         }
955 }
956
957
958 // get the next breakpoint in a given paragraph
959 pos_type
960 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
961 {
962         Paragraph * par = row->par();
963
964         if (width < 0)
965                 return par->size();
966
967         pos_type const pos = row->pos();
968
969         // position of the last possible breakpoint
970         // -1 isn't a suitable value, but a flag
971         pos_type last_separator = -1;
972         width -= rightMargin(bview->buffer(), row);
973
974         pos_type const main_body =
975                 beginningOfMainBody(bview->buffer(), par);
976         LyXLayout_ptr const & layout = par->layout();
977
978         pos_type i = pos;
979
980         if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
981                 // special code for right address boxes, only newlines count
982                 while (i < par->size()) {
983                         if (par->isNewline(i)) {
984                                 last_separator = i;
985                                 i = par->size() - 1; // this means break
986                                 //x = width;
987                         } else if (par->isInset(i) && par->getInset(i)
988                                 && par->getInset(i)->display()) {
989                                 par->getInset(i)->display(false);
990                         }
991                         ++i;
992                 }
993         } else {
994                 // Last position is an invariant
995                 pos_type const last = par->size();
996                 // this is the usual handling
997                 int x = leftMargin(bview, row);
998                 bool doitonetime = true;
999                 while (doitonetime || ((x < width) && (i < last))) {
1000                         doitonetime = false;
1001                         char const c = par->getChar(i);
1002                         Inset * in = 0;
1003                         if (c == Paragraph::META_INSET)
1004                                 in = par->getInset(i);
1005                         if (IsNewlineChar(c)) {
1006                                 last_separator = i;
1007                                 x = width; // this means break
1008                         } else if (in && !in->isChar()) {
1009                                 // check wether a Display() inset is
1010                                 // valid here. if not, change it to
1011                                 // non-display
1012                                 if (in->display() &&
1013                                     (layout->isCommand() ||
1014                                      (layout->labeltype == LABEL_MANUAL
1015                                       && i < beginningOfMainBody(bview->buffer(), par))))
1016                                 {
1017                                         // display istn't allowd
1018                                         in->display(false);
1019                                         x += singleWidth(bview, par, i, c);
1020                                 } else if (in->display() || in->needFullRow()) {
1021                                         // So break the line here
1022                                         if (i == pos) {
1023                                                 if (pos < last-1) {
1024                                                         last_separator = i;
1025                                                         if (par->isLineSeparator(i+1))
1026                                                                 ++last_separator;
1027                                                 } else
1028                                                         last_separator = last; // to avoid extra rows
1029                                         } else
1030                                                 last_separator = i - 1;
1031                                         x = width;  // this means break
1032                                 } else {
1033                                         x += singleWidth(bview, par, i, c);
1034                                         // we have to check this separately as we could have a
1035                                         // lineseparator and then the algorithm below would prefer
1036                                         // that which IS wrong! We should always break on an inset
1037                                         // if it's too long and not on the last separator.
1038                                         // Maybe the only exeption is insets used as chars but
1039                                         // then we would have to have a special function inside
1040                                         // the inset to tell us this. Till then we leave it as
1041                                         // it is now. (Jug 20020106)
1042                                         if (pos < i && x >= width && last_separator >= 0)
1043                                                 last_separator = i - 1;
1044                                 }
1045                         } else  {
1046                                 if (par->isLineSeparator(i))
1047                                         last_separator = i;
1048                                 x += singleWidth(bview, par, i, c);
1049                         }
1050                         ++i;
1051                         if (i == main_body) {
1052                                 x += font_metrics::width(layout->labelsep,
1053                                                     getLabelFont(bview->buffer(), par));
1054                                 if (par->isLineSeparator(i - 1))
1055                                         x-= singleWidth(bview, par, i - 1);
1056                                 int left_margin = labelEnd(bview, row);
1057                                 if (x < left_margin)
1058                                         x = left_margin;
1059                         }
1060                 }
1061                 if ((pos+1 < i) && (last_separator < 0) && (x >= width))
1062                         last_separator = i - 2;
1063                 else if ((pos < i) && (last_separator < 0) && (x >= width))
1064                         last_separator = i - 1;
1065                 // end of paragraph is always a suitable separator
1066                 else if (i == last && x < width)
1067                         last_separator = i;
1068         }
1069
1070         // well, if last_separator is still 0, the line isn't breakable.
1071         // don't care and cut simply at the end
1072         if (last_separator < 0) {
1073                 last_separator = i;
1074         }
1075
1076         // manual labels cannot be broken in LaTeX, do not care
1077         if (main_body && last_separator < main_body)
1078                 last_separator = main_body - 1;
1079
1080         return last_separator;
1081 }
1082
1083
1084 // returns the minimum space a row needs on the screen in pixel
1085 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
1086 {
1087         if (paper_width < 0)
1088                 return 0;
1089
1090         int w;
1091         // get the pure distance
1092         pos_type const last = rowLastPrintable(row);
1093
1094         // special handling of the right address boxes
1095         if (row->par()->layout()->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1096                 int const tmpfill = row->fill();
1097                 row->fill(0); // the minfill in MarginLeft()
1098                 w = leftMargin(bview, row);
1099                 row->fill(tmpfill);
1100         } else
1101                 w = leftMargin(bview, row);
1102
1103         LyXLayout_ptr const & layout = row->par()->layout();
1104
1105         pos_type const main_body =
1106                 beginningOfMainBody(bview->buffer(), row->par());
1107         pos_type i = row->pos();
1108
1109         while (i <= last) {
1110                 if (main_body > 0 && i == main_body) {
1111                         w += font_metrics::width(layout->labelsep, getLabelFont(bview->buffer(), row->par()));
1112                         if (row->par()->isLineSeparator(i - 1))
1113                                 w -= singleWidth(bview, row->par(), i - 1);
1114                         int left_margin = labelEnd(bview, row);
1115                         if (w < left_margin)
1116                                 w = left_margin;
1117                 }
1118                 w += singleWidth(bview, row->par(), i);
1119                 ++i;
1120         }
1121         if (main_body > 0 && main_body > last) {
1122                 w += font_metrics::width(layout->labelsep, getLabelFont(bview->buffer(), row->par()));
1123                 if (last >= 0 && row->par()->isLineSeparator(last))
1124                         w -= singleWidth(bview, row->par(), last);
1125                 int const left_margin = labelEnd(bview, row);
1126                 if (w < left_margin)
1127                         w = left_margin;
1128         }
1129
1130         int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1131         return fill;
1132 }
1133
1134
1135 // returns the minimum space a manual label needs on the screen in pixel
1136 int LyXText::labelFill(BufferView * bview, Row const * row) const
1137 {
1138         pos_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1139         // -1 because a label ends either with a space that is in the label,
1140         // or with the beginning of a footnote that is outside the label.
1141
1142         // I don't understand this code in depth, but sometimes "last" is
1143         // less than 0 and this causes a crash. This fix seems to work
1144         // correctly, but I bet the real error is elsewhere.  The bug is
1145         // triggered when you have an open footnote in a paragraph
1146         // environment with a manual label. (Asger)
1147         if (last < 0) last = 0;
1148
1149         // a separator at this end does not count
1150         if (row->par()->isLineSeparator(last))
1151                 --last;
1152
1153         int w = 0;
1154         pos_type i = row->pos();
1155         while (i <= last) {
1156                 w += singleWidth(bview, row->par(), i);
1157                 ++i;
1158         }
1159
1160         int fill = 0;
1161         if (!row->par()->params().labelWidthString().empty()) {
1162                 fill = max(font_metrics::width(row->par()->params().labelWidthString(),
1163                                           getLabelFont(bview->buffer(), row->par())) - w,
1164                            0);
1165         }
1166
1167         return fill;
1168 }
1169
1170
1171 // returns the number of separators in the specified row. The separator
1172 // on the very last column doesnt count
1173 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1174 {
1175         pos_type last = rowLastPrintable(row);
1176         pos_type p = max(row->pos(), beginningOfMainBody(buf, row->par()));
1177
1178         int n = 0;
1179         for (; p <= last; ++p) {
1180                 if (row->par()->isSeparator(p)) {
1181                         ++n;
1182                 }
1183         }
1184         return n;
1185 }
1186
1187
1188 // returns the number of hfills in the specified row. The LyX-Hfill is
1189 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1190 // ignored. This is *MUCH* more usefull than not to ignore!
1191 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1192 {
1193         pos_type const last = rowLast(row);
1194         pos_type first = row->pos();
1195
1196         if (first) {
1197                 // hfill *DO* count at the beginning of paragraphs!
1198                 while (first <= last && row->par()->isHfill(first)) {
1199                         ++first;
1200                 }
1201         }
1202
1203         first = max(first, beginningOfMainBody(buf, row->par()));
1204         int n = 0;
1205         for (pos_type p = first; p <= last; ++p) {
1206                 // last, because the end is ignored!
1207
1208                 if (row->par()->isHfill(p)) {
1209                         ++n;
1210                 }
1211         }
1212         return n;
1213 }
1214
1215
1216 // like NumberOfHfills, but only those in the manual label!
1217 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1218 {
1219         pos_type last = rowLast(row);
1220         pos_type first = row->pos();
1221         if (first) {
1222                 // hfill *DO* count at the beginning of paragraphs!
1223                 while (first < last && row->par()->isHfill(first))
1224                         ++first;
1225         }
1226
1227         last = min(last, beginningOfMainBody(buf, row->par()));
1228         int n = 0;
1229         for (pos_type p = first; p < last; ++p) {
1230                 // last, because the end is ignored!
1231                 if (row->par()->isHfill(p)) {
1232                         ++n;
1233                 }
1234         }
1235         return n;
1236 }
1237
1238
1239 // returns true, if a expansion is needed.
1240 // Rules are given by LaTeX
1241 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1242                              pos_type pos) const
1243 {
1244         // by the way, is it a hfill?
1245         if (!row_ptr->par()->isHfill(pos))
1246                 return false;
1247
1248         // at the end of a row it does not count
1249         // unless another hfill exists on the line
1250         if (pos >= rowLast(row_ptr)) {
1251                 pos_type i = row_ptr->pos();
1252                 while (i < pos && !row_ptr->par()->isHfill(i)) {
1253                         ++i;
1254                 }
1255                 if (i == pos) {
1256                         return false;
1257                 }
1258         }
1259
1260         // at the beginning of a row it does not count, if it is not
1261         // the first row of a paragaph
1262         if (!row_ptr->pos())
1263                 return true;
1264
1265         // in some labels  it does not count
1266         if (row_ptr->par()->layout()->margintype != MARGIN_MANUAL
1267             && pos < beginningOfMainBody(buf, row_ptr->par()))
1268                 return false;
1269
1270         // if there is anything between the first char of the row and
1271         // the sepcified position that is not a newline and not a hfill,
1272         // the hfill will count, otherwise not
1273         pos_type i = row_ptr->pos();
1274         while (i < pos && (row_ptr->par()->isNewline(i)
1275                            || row_ptr->par()->isHfill(i)))
1276                 ++i;
1277
1278         return i != pos;
1279 }
1280
1281
1282 LColor::color LyXText::backgroundColor()
1283 {
1284         if (inset_owner)
1285                 return inset_owner->backgroundColor();
1286         else
1287                 return LColor::background;
1288 }
1289
1290 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1291 {
1292         // get the maximum ascent and the maximum descent
1293         int asc = 0;
1294         int desc = 0;
1295         float layoutasc = 0;
1296         float layoutdesc = 0;
1297         float tmptop = 0;
1298         LyXFont tmpfont;
1299         Inset * tmpinset = 0;
1300
1301         // ok , let us initialize the maxasc and maxdesc value.
1302         // This depends in LaTeX of the font of the last character
1303         // in the paragraph. The hack below is necessary because
1304         // of the possibility of open footnotes
1305
1306         // Correction: only the fontsize count. The other properties
1307         //  are taken from the layoutfont. Nicer on the screen :)
1308         Paragraph * par = row_ptr->par();
1309         Paragraph * firstpar = row_ptr->par();
1310
1311         LyXLayout_ptr const & layout = firstpar->layout();
1312
1313         // as max get the first character of this row then it can increes but not
1314         // decrees the height. Just some point to start with so we don't have to
1315         // do the assignment below too often.
1316         LyXFont font = getFont(bview->buffer(), par, row_ptr->pos());
1317         LyXFont::FONT_SIZE const tmpsize = font.size();
1318         font = getLayoutFont(bview->buffer(), par);
1319         LyXFont::FONT_SIZE const size = font.size();
1320         font.setSize(tmpsize);
1321
1322         LyXFont labelfont = getLabelFont(bview->buffer(), par);
1323
1324         float spacing_val = 1.0;
1325         if (!row_ptr->par()->params().spacing().isDefault()) {
1326                 spacing_val = row_ptr->par()->params().spacing().getValue();
1327         } else {
1328                 spacing_val = bview->buffer()->params.spacing.getValue();
1329         }
1330         //lyxerr << "spacing_val = " << spacing_val << endl;
1331
1332         int maxasc = int(font_metrics::maxAscent(font) *
1333                          layout->spacing.getValue() *
1334                          spacing_val);
1335         int maxdesc = int(font_metrics::maxDescent(font) *
1336                           layout->spacing.getValue() *
1337                           spacing_val);
1338
1339         pos_type const pos_end = rowLast(row_ptr);
1340         int labeladdon = 0;
1341         int maxwidth = 0;
1342
1343         // Check if any insets are larger
1344         for (pos_type pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1345                 if (row_ptr->par()->isInset(pos)) {
1346                         tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1347                         tmpinset = row_ptr->par()->getInset(pos);
1348                         if (tmpinset) {
1349 #if 1 // this is needed for deep update on initialitation
1350                                 tmpinset->update(bview, tmpfont);
1351 #endif
1352                                 asc = tmpinset->ascent(bview, tmpfont);
1353                                 desc = tmpinset->descent(bview, tmpfont);
1354                                 maxwidth += tmpinset->width(bview, tmpfont);
1355                                 maxasc = max(maxasc, asc);
1356                                 maxdesc = max(maxdesc, desc);
1357                         }
1358                 } else {
1359                         maxwidth += singleWidth(bview, row_ptr->par(), pos);
1360                 }
1361         }
1362
1363         // Check if any custom fonts are larger (Asger)
1364         // This is not completely correct, but we can live with the small,
1365         // cosmetic error for now.
1366         LyXFont::FONT_SIZE maxsize =
1367                 row_ptr->par()->highestFontInRange(row_ptr->pos(), pos_end, size);
1368         if (maxsize > font.size()) {
1369                 font.setSize(maxsize);
1370
1371                 asc = font_metrics::maxAscent(font);
1372                 desc = font_metrics::maxDescent(font);
1373                 if (asc > maxasc)
1374                         maxasc = asc;
1375                 if (desc > maxdesc)
1376                         maxdesc = desc;
1377         }
1378
1379         // This is nicer with box insets:
1380         ++maxasc;
1381         ++maxdesc;
1382
1383         row_ptr->ascent_of_text(maxasc);
1384
1385         // is it a top line?
1386         if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1387
1388                 // some parksips VERY EASY IMPLEMENTATION
1389                 if (bview->buffer()->params.paragraph_separation ==
1390                         BufferParams::PARSEP_SKIP)
1391                 {
1392                         if (layout->isParagraph()
1393                                 && firstpar->getDepth() == 0
1394                                 && firstpar->previous())
1395                         {
1396                                 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1397                         } else if (firstpar->previous() &&
1398                                    firstpar->previous()->layout()->isParagraph() &&
1399                                    firstpar->previous()->getDepth() == 0)
1400                         {
1401                                 // is it right to use defskip here too? (AS)
1402                                 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1403                         }
1404                 }
1405
1406                 // the paper margins
1407                 if (!row_ptr->par()->previous() && bv_owner)
1408                         maxasc += LYX_PAPER_MARGIN;
1409
1410                 // add the vertical spaces, that the user added
1411                 maxasc += getLengthMarkerHeight(bview, firstpar->params().spaceTop());
1412
1413                 // do not forget the DTP-lines!
1414                 // there height depends on the font of the nearest character
1415                 if (firstpar->params().lineTop())
1416
1417                         maxasc += 2 * font_metrics::ascent('x', getFont(bview->buffer(),
1418                                         firstpar, 0));
1419                 // and now the pagebreaks
1420                 if (firstpar->params().pagebreakTop())
1421                         maxasc += 3 * defaultHeight();
1422
1423                 // This is special code for the chapter, since the label of this
1424                 // layout is printed in an extra row
1425                 if (layout->labeltype == LABEL_COUNTER_CHAPTER
1426                         && bview->buffer()->params.secnumdepth >= 0)
1427                 {
1428                         float spacing_val = 1.0;
1429                         if (!row_ptr->par()->params().spacing().isDefault()) {
1430                                 spacing_val = row_ptr->par()->params().spacing().getValue();
1431                         } else {
1432                                 spacing_val = bview->buffer()->params.spacing.getValue();
1433                         }
1434
1435                         labeladdon = int(font_metrics::maxDescent(labelfont) *
1436                                          layout->spacing.getValue() *
1437                                          spacing_val)
1438                                 + int(font_metrics::maxAscent(labelfont) *
1439                                       layout->spacing.getValue() *
1440                                       spacing_val);
1441                 }
1442
1443                 // special code for the top label
1444                 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1445                      || layout->labeltype == LABEL_BIBLIO
1446                      || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1447                     && row_ptr->par()->isFirstInSequence()
1448                     && !row_ptr->par()->getLabelstring().empty())
1449                 {
1450                         float spacing_val = 1.0;
1451                         if (!row_ptr->par()->params().spacing().isDefault()) {
1452                                 spacing_val = row_ptr->par()->params().spacing().getValue();
1453                         } else {
1454                                 spacing_val = bview->buffer()->params.spacing.getValue();
1455                         }
1456
1457                         labeladdon = int(
1458                                 (font_metrics::maxAscent(labelfont) *
1459                                  layout->spacing.getValue() *
1460                                  spacing_val)
1461                                 +(font_metrics::maxDescent(labelfont) *
1462                                   layout->spacing.getValue() *
1463                                   spacing_val)
1464                                 + layout->topsep * defaultHeight()
1465                                 + layout->labelbottomsep *  defaultHeight());
1466                 }
1467
1468                 // and now the layout spaces, for example before and after a section,
1469                 // or between the items of a itemize or enumerate environment
1470
1471                 if (!firstpar->params().pagebreakTop()) {
1472                         Paragraph * prev = row_ptr->par()->previous();
1473                         if (prev)
1474                                 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1475                         if (prev && prev->layout() == firstpar->layout() &&
1476                                 prev->getDepth() == firstpar->getDepth() &&
1477                                 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1478                         {
1479                                 layoutasc = (layout->itemsep * defaultHeight());
1480                         } else if (row_ptr->previous()) {
1481                                 tmptop = layout->topsep;
1482
1483                                 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1484                                         tmptop -= row_ptr->previous()->par()->layout()->bottomsep;
1485
1486                                 if (tmptop > 0)
1487                                         layoutasc = (tmptop * defaultHeight());
1488                         } else if (row_ptr->par()->params().lineTop()) {
1489                                 tmptop = layout->topsep;
1490
1491                                 if (tmptop > 0)
1492                                         layoutasc = (tmptop * defaultHeight());
1493                         }
1494
1495                         prev = row_ptr->par()->outerHook();
1496                         if (prev)  {
1497                                 maxasc += int(prev->layout()->parsep * defaultHeight());
1498                         } else {
1499                                 if (firstpar->previous() &&
1500                                         firstpar->previous()->getDepth() == 0 &&
1501                                         firstpar->previous()->layout() !=
1502                                         firstpar->layout())
1503                                 {
1504                                         // avoid parsep
1505                                 } else if (firstpar->previous()) {
1506                                         maxasc += int(layout->parsep * defaultHeight());
1507                                 }
1508                         }
1509                 }
1510         }
1511
1512         // is it a bottom line?
1513         if (row_ptr->par() == par
1514                 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
1515         {
1516                 // the paper margins
1517                 if (!par->next() && bv_owner)
1518                         maxdesc += LYX_PAPER_MARGIN;
1519
1520                 // add the vertical spaces, that the user added
1521                 maxdesc += getLengthMarkerHeight(bview, firstpar->params().spaceBottom());
1522
1523                 // do not forget the DTP-lines!
1524                 // there height depends on the font of the nearest character
1525                 if (firstpar->params().lineBottom())
1526                         maxdesc += 2 * font_metrics::ascent('x',
1527                                                        getFont(bview->buffer(),
1528                                                                par,
1529                                                                max(pos_type(0), par->size() - 1)));
1530
1531                 // and now the pagebreaks
1532                 if (firstpar->params().pagebreakBottom())
1533                         maxdesc += 3 * defaultHeight();
1534
1535                 // and now the layout spaces, for example before and after
1536                 // a section, or between the items of a itemize or enumerate
1537                 // environment
1538                 if (!firstpar->params().pagebreakBottom()
1539                     && row_ptr->par()->next()) {
1540                         Paragraph * nextpar = row_ptr->par()->next();
1541                         Paragraph * comparepar = row_ptr->par();
1542                         float usual = 0;
1543                         float unusual = 0;
1544
1545                         if (comparepar->getDepth() > nextpar->getDepth()) {
1546                                 usual = (comparepar->layout()->bottomsep * defaultHeight());
1547                                 comparepar = comparepar->depthHook(nextpar->getDepth());
1548                                 if (comparepar->layout()!= nextpar->layout()
1549                                         || nextpar->getLabelWidthString() !=
1550                                         comparepar->getLabelWidthString())
1551                                 {
1552                                         unusual = (comparepar->layout()->bottomsep * defaultHeight());
1553                                 }
1554                                 if (unusual > usual)
1555                                         layoutdesc = unusual;
1556                                 else
1557                                         layoutdesc = usual;
1558                         } else if (comparepar->getDepth() ==  nextpar->getDepth()) {
1559
1560                                 if (comparepar->layout() != nextpar->layout()
1561                                         || nextpar->getLabelWidthString() !=
1562                                         comparepar->getLabelWidthString())
1563                                         layoutdesc = int(comparepar->layout()->bottomsep * defaultHeight());
1564                         }
1565                 }
1566         }
1567
1568         // incalculate the layout spaces
1569         maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1570         maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1571
1572         // calculate the new height of the text
1573         height -= row_ptr->height();
1574
1575         row_ptr->height(maxasc + maxdesc + labeladdon);
1576         row_ptr->baseline(maxasc + labeladdon);
1577
1578         height += row_ptr->height();
1579         float x = 0;
1580         if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1581                 float dummy;
1582                 // this IS needed
1583                 row_ptr->width(maxwidth);
1584                 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1585         }
1586         row_ptr->width(int(maxwidth + x));
1587         if (inset_owner) {
1588                 Row * r = firstrow;
1589                 width = max(0,workWidth(bview));
1590                 while (r) {
1591                         if (r->width() > width)
1592                                 width = r->width();
1593                         r = r->next();
1594                 }
1595         }
1596 }
1597
1598
1599 // Appends the implicit specified paragraph behind the specified row,
1600 // start at the implicit given position
1601 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1602 {
1603         bool not_ready = true;
1604
1605         // The last character position of a paragraph is an invariant so we can
1606         // safely get it here. (Asger)
1607         pos_type const lastposition = row->par()->size();
1608         do {
1609                 // Get the next breakpoint
1610                 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1611
1612                 Row * tmprow = row;
1613
1614                 // Insert the new row
1615                 if (z < lastposition) {
1616                         ++z;
1617                         insertRow(row, row->par(), z);
1618                         row = row->next();
1619
1620                         row->height(0);
1621                 } else
1622                         not_ready = false;
1623
1624                 // Set the dimensions of the row
1625                 // fixed fill setting now by calling inset->update() in
1626                 // SingleWidth when needed!
1627                 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1628                 setHeightOfRow(bview, tmprow);
1629
1630         } while (not_ready);
1631 }
1632
1633
1634 void LyXText::breakAgain(BufferView * bview, Row * row) const
1635 {
1636         bool not_ready = true;
1637
1638         do  {
1639                 // get the next breakpoint
1640                 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1641                 Row * tmprow = row;
1642
1643                 if (z < row->par()->size()) {
1644                         if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1645                                 // insert a new row
1646                                 ++z;
1647                                 insertRow(row, row->par(), z);
1648                                 row = row->next();
1649                                 row->height(0);
1650                         } else  {
1651                                 row = row->next();
1652                                 ++z;
1653                                 if (row->pos() == z)
1654                                         not_ready = false;     // the rest will not change
1655                                 else {
1656                                         row->pos(z);
1657                                 }
1658                         }
1659                 } else {
1660                         // if there are some rows too much, delete them
1661                         // only if you broke the whole paragraph!
1662                         Row * tmprow2 = row;
1663                         while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1664                                 tmprow2 = tmprow2->next();
1665                         }
1666                         while (tmprow2 != row) {
1667                                 tmprow2 = tmprow2->previous();
1668                                 removeRow(tmprow2->next());
1669                         }
1670                         not_ready = false;
1671                 }
1672
1673                 // set the dimensions of the row
1674                 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1675                 setHeightOfRow(bview, tmprow);
1676         } while (not_ready);
1677 }
1678
1679
1680 // this is just a little changed version of break again
1681 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1682 {
1683         // get the next breakpoint
1684         pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1685         Row * tmprow = row;
1686
1687         if (z < row->par()->size()) {
1688                 if (!row->next()
1689                     || (row->next() && row->next()->par() != row->par())) {
1690                         // insert a new row
1691                         ++z;
1692                         insertRow(row, row->par(), z);
1693                         row = row->next();
1694                         row->height(0);
1695                 } else  {
1696                         row = row->next();
1697                         ++z;
1698                         if (row->pos() != z)
1699                                 row->pos(z);
1700                 }
1701         } else {
1702                 // if there are some rows too much, delete them
1703                 // only if you broke the whole paragraph!
1704                 Row * tmprow2 = row;
1705                 while (tmprow2->next()
1706                        && tmprow2->next()->par() == row->par()) {
1707                         tmprow2 = tmprow2->next();
1708                 }
1709                 while (tmprow2 != row) {
1710                         tmprow2 = tmprow2->previous();
1711                         removeRow(tmprow2->next());
1712                 }
1713         }
1714
1715         // set the dimensions of the row
1716         tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1717         setHeightOfRow(bview, tmprow);
1718 }
1719
1720
1721 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1722 {
1723         LyXTextClass const & tclass =
1724                 bview->buffer()->params.getLyXTextClass();
1725         LyXLayout_ptr const & layout = cursor.par()->layout();
1726
1727         // this is only allowed, if the current paragraph is not empty or caption
1728         // and if it has not the keepempty flag aktive
1729         if (cursor.par()->empty()
1730            && layout->labeltype != LABEL_SENSITIVE
1731            && !layout->keepempty)
1732                 return;
1733
1734         setUndo(bview, Undo::FINISH, cursor.par(), cursor.par()->next());
1735
1736         // Always break behind a space
1737         //
1738         // It is better to erase the space (Dekel)
1739         if (cursor.pos() < cursor.par()->size()
1740              && cursor.par()->isLineSeparator(cursor.pos()))
1741            cursor.par()->erase(cursor.pos());
1742         // cursor.pos(cursor.pos() + 1);
1743
1744         // break the paragraph
1745         if (keep_layout)
1746                 keep_layout = 2;
1747         else
1748                 keep_layout = layout->isEnvironment();
1749
1750         // we need to set this before we insert the paragraph. IMO the
1751         // breakParagraph call should return a bool if it inserts the
1752         // paragraph before or behind and we should react on that one
1753         // but we can fix this in 1.3.0 (Jug 20020509)
1754         bool const isempty = (layout->keepempty && cursor.par()->empty());
1755         ::breakParagraph(bview->buffer()->params, cursor.par(), cursor.pos(),
1756                        keep_layout);
1757
1758         // well this is the caption hack since one caption is really enough
1759         if (layout->labeltype == LABEL_SENSITIVE) {
1760                 if (!cursor.pos())
1761                         // set to standard-layout
1762                         cursor.par()->applyLayout(tclass.defaultLayout());
1763                 else
1764                         // set to standard-layout
1765                         cursor.par()->next()->applyLayout(tclass.defaultLayout());
1766         }
1767
1768         // if the cursor is at the beginning of a row without prior newline,
1769         // move one row up!
1770         // This touches only the screen-update. Otherwise we would may have
1771         // an empty row on the screen
1772         if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1773                          && cursor.row()->pos() == cursor.pos())
1774         {
1775                 cursorLeft(bview);
1776         }
1777
1778         status(bview, LyXText::NEED_MORE_REFRESH);
1779         refresh_row = cursor.row();
1780         refresh_y = cursor.y() - cursor.row()->baseline();
1781
1782         // Do not forget the special right address boxes
1783         if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1784                 while (refresh_row->previous() &&
1785                        refresh_row->previous()->par() == refresh_row->par())
1786                 {
1787                         refresh_row = refresh_row->previous();
1788                         refresh_y -= refresh_row->height();
1789                 }
1790         }
1791         removeParagraph(cursor.row());
1792
1793         // set the dimensions of the cursor row
1794         cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1795
1796         setHeightOfRow(bview, cursor.row());
1797
1798         while (!cursor.par()->next()->empty()
1799           && cursor.par()->next()->isNewline(0))
1800            cursor.par()->next()->erase(0);
1801
1802         insertParagraph(bview, cursor.par()->next(), cursor.row());
1803
1804         updateCounters(bview);
1805
1806         // This check is necessary. Otherwise the new empty paragraph will
1807         // be deleted automatically. And it is more friendly for the user!
1808         if (cursor.pos() || isempty)
1809                 setCursor(bview, cursor.par()->next(), 0);
1810         else
1811                 setCursor(bview, cursor.par(), 0);
1812
1813         if (cursor.row()->next())
1814                 breakAgain(bview, cursor.row()->next());
1815
1816         need_break_row = 0;
1817 }
1818
1819
1820 // Just a macro to make some thing easier.
1821 void LyXText::redoParagraph(BufferView * bview) const
1822 {
1823         clearSelection();
1824         redoParagraphs(bview, cursor, cursor.par()->next());
1825         setCursorIntern(bview, cursor.par(), cursor.pos());
1826 }
1827
1828
1829 // insert a character, moves all the following breaks in the
1830 // same Paragraph one to the right and make a rebreak
1831 void LyXText::insertChar(BufferView * bview, char c)
1832 {
1833         setUndo(bview, Undo::INSERT, cursor.par(), cursor.par()->next());
1834
1835         // When the free-spacing option is set for the current layout,
1836         // disable the double-space checking
1837
1838         bool const freeSpacing = cursor.row()->par()->layout()->free_spacing ||
1839                 cursor.row()->par()->isFreeSpacing();
1840
1841         if (lyxrc.auto_number) {
1842                 static string const number_operators = "+-/*";
1843                 static string const number_unary_operators = "+-";
1844                 static string const number_seperators = ".,:";
1845
1846                 if (current_font.number() == LyXFont::ON) {
1847                         if (!IsDigit(c) && !contains(number_operators, c) &&
1848                             !(contains(number_seperators, c) &&
1849                               cursor.pos() >= 1 &&
1850                               cursor.pos() < cursor.par()->size() &&
1851                               getFont(bview->buffer(),
1852                                       cursor.par(),
1853                                       cursor.pos()).number() == LyXFont::ON &&
1854                               getFont(bview->buffer(),
1855                                       cursor.par(),
1856                                       cursor.pos() - 1).number() == LyXFont::ON)
1857                            )
1858                                 number(bview); // Set current_font.number to OFF
1859                 } else if (IsDigit(c) &&
1860                            real_current_font.isVisibleRightToLeft()) {
1861                         number(bview); // Set current_font.number to ON
1862
1863                         if (cursor.pos() > 0) {
1864                                 char const c = cursor.par()->getChar(cursor.pos() - 1);
1865                                 if (contains(number_unary_operators, c) &&
1866                                     (cursor.pos() == 1 ||
1867                                      cursor.par()->isSeparator(cursor.pos() - 2) ||
1868                                      cursor.par()->isNewline(cursor.pos() - 2))
1869                                   ) {
1870                                         setCharFont(bview->buffer(),
1871                                                     cursor.par(),
1872                                                     cursor.pos() - 1,
1873                                                     current_font);
1874                                 } else if (contains(number_seperators, c) &&
1875                                            cursor.pos() >= 2 &&
1876                                            getFont(bview->buffer(),
1877                                                    cursor.par(),
1878                                                    cursor.pos() - 2).number() == LyXFont::ON) {
1879                                         setCharFont(bview->buffer(),
1880                                                     cursor.par(),
1881                                                     cursor.pos() - 1,
1882                                                     current_font);
1883                                 }
1884                         }
1885                 }
1886         }
1887
1888
1889         // First check, if there will be two blanks together or a blank at
1890         // the beginning of a paragraph.
1891         // I decided to handle blanks like normal characters, the main
1892         // difference are the special checks when calculating the row.fill
1893         // (blank does not count at the end of a row) and the check here
1894
1895         // The bug is triggered when we type in a description environment:
1896         // The current_font is not changed when we go from label to main text
1897         // and it should (along with realtmpfont) when we type the space.
1898         // CHECK There is a bug here! (Asger)
1899
1900         LyXFont realtmpfont = real_current_font;
1901         LyXFont rawtmpfont = current_font;
1902         // store the current font.  This is because of the use of cursor
1903         // movements. The moving cursor would refresh the current font
1904
1905         // Get the font that is used to calculate the baselineskip
1906         pos_type const lastpos = cursor.par()->size();
1907         LyXFont rawparfont =
1908                 cursor.par()->getFontSettings(bview->buffer()->params,
1909                                               lastpos - 1);
1910
1911         bool jumped_over_space = false;
1912
1913         if (!freeSpacing && IsLineSeparatorChar(c)) {
1914                 if ((cursor.pos() > 0
1915                      && cursor.par()->isLineSeparator(cursor.pos() - 1))
1916                     || (cursor.pos() > 0
1917                         && cursor.par()->isNewline(cursor.pos() - 1))
1918                     || (cursor.pos() == 0)) {
1919                         static bool sent_space_message = false;
1920                         if (!sent_space_message) {
1921                                 if (cursor.pos() == 0)
1922                                         bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph.  Please read the Tutorial."));
1923                                 else
1924                                         bview->owner()->message(_("You cannot type two spaces this way.  Please read the Tutorial."));
1925                                 sent_space_message = true;
1926                         }
1927                         charInserted();
1928                         return;
1929                 }
1930         } else if (IsNewlineChar(c)) {
1931                 if (cursor.pos() <= beginningOfMainBody(bview->buffer(),
1932                                                         cursor.par()))
1933                 {
1934                         charInserted();
1935                         return;
1936                 }
1937                 // No newline at first position of a paragraph or behind labels.
1938                 // TeX does not allow that
1939
1940                 if (cursor.pos() < cursor.par()->size() &&
1941                     cursor.par()->isLineSeparator(cursor.pos()))
1942                         // newline always after a blank!
1943                         cursorRight(bview);
1944                 cursor.row()->fill(-1);        // to force a new break
1945         }
1946
1947         // the display inset stuff
1948         if (cursor.row()->par()->isInset(cursor.row()->pos())) {
1949                 Inset * inset = cursor.row()->par()->getInset(cursor.row()->pos());
1950                 if (inset && (inset->display() || inset->needFullRow())) {
1951                         // force a new break
1952                         cursor.row()->fill(-1); // to force a new break
1953                 }
1954         }
1955
1956         // get the cursor row fist
1957         Row * row = cursor.row();
1958         int y = cursor.y() - row->baseline();
1959         if (c != Paragraph::META_INSET) {
1960                 // Here case LyXText::InsertInset  already insertet the character
1961                 cursor.par()->insertChar(cursor.pos(), c);
1962         }
1963         setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1964
1965         if (!jumped_over_space) {
1966                 // refresh the positions
1967                 Row * tmprow = row;
1968                 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1969                         tmprow = tmprow->next();
1970                         tmprow->pos(tmprow->pos() + 1);
1971                 }
1972         }
1973
1974         // Is there a break one row above
1975         if (row->previous() && row->previous()->par() == row->par()
1976             && (cursor.par()->isLineSeparator(cursor.pos())
1977                 || cursor.par()->isNewline(cursor.pos())
1978                 || ((cursor.pos() < cursor.par()->size()) &&
1979                     cursor.par()->isInset(cursor.pos()+1))
1980                 || cursor.row()->fill() == -1))
1981         {
1982                 pos_type z = nextBreakPoint(bview,
1983                                                            row->previous(),
1984                                                            workWidth(bview));
1985                 if (z >= row->pos()) {
1986                         row->pos(z + 1);
1987
1988                         // set the dimensions of the row above
1989                         row->previous()->fill(fill(bview,
1990                                                    row->previous(),
1991                                                    workWidth(bview)));
1992
1993                         setHeightOfRow(bview, row->previous());
1994
1995                         y -= row->previous()->height();
1996                         refresh_y = y;
1997                         refresh_row = row->previous();
1998                         status(bview, LyXText::NEED_MORE_REFRESH);
1999
2000                         breakAgainOneRow(bview, row);
2001
2002                         current_font = rawtmpfont;
2003                         real_current_font = realtmpfont;
2004                         setCursor(bview, cursor.par(), cursor.pos() + 1,
2005                                   false, cursor.boundary());
2006                         // cursor MUST be in row now.
2007
2008                         if (row->next() && row->next()->par() == row->par())
2009                                 need_break_row = row->next();
2010                         else
2011                                 need_break_row = 0;
2012
2013                         // check, wether the last characters font has changed.
2014                         if (cursor.pos() && cursor.pos() == cursor.par()->size()
2015                             && rawparfont != rawtmpfont)
2016                                 redoHeightOfParagraph(bview, cursor);
2017
2018                         charInserted();
2019                         return;
2020                 }
2021         }
2022
2023         // recalculate the fill of the row
2024         if (row->fill() >= 0) {
2025                 // needed because a newline will set fill to -1. Otherwise
2026                 // we would not get a rebreak!
2027                 row->fill(fill(bview, row, workWidth(bview)));
2028         }
2029  
2030         if (c == Paragraph::META_INSET || row->fill() < 0) {
2031                 refresh_y = y;
2032                 refresh_row = row;
2033                 status(bview, LyXText::NEED_MORE_REFRESH);
2034                 breakAgainOneRow(bview, row);
2035                 // will the cursor be in another row now?
2036                 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
2037                         if (row->next() && row->next()->par() == row->par())
2038                                 // this should always be true
2039                                 row = row->next();
2040                         breakAgainOneRow(bview, row);
2041                 }
2042                 current_font = rawtmpfont;
2043                 real_current_font = realtmpfont;
2044
2045                 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2046                           cursor.boundary());
2047                 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2048                     != cursor.boundary())
2049                         setCursor(bview, cursor.par(), cursor.pos(), false,
2050                           !cursor.boundary());
2051                 if (row->next() && row->next()->par() == row->par())
2052                         need_break_row = row->next();
2053                 else
2054                         need_break_row = 0;
2055         } else {
2056                 refresh_y = y;
2057                 refresh_row = row;
2058
2059                 int const tmpheight = row->height();
2060                 setHeightOfRow(bview, row);
2061                 if (tmpheight == row->height())
2062                         status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2063                 else
2064                         status(bview, LyXText::NEED_MORE_REFRESH);
2065
2066                 current_font = rawtmpfont;
2067                 real_current_font = realtmpfont;
2068                 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2069                           cursor.boundary());
2070         }
2071
2072         // check, wether the last characters font has changed.
2073         if (cursor.pos() && cursor.pos() == cursor.par()->size()
2074             && rawparfont != rawtmpfont) {
2075                 redoHeightOfParagraph(bview, cursor);
2076         } else {
2077                 // now the special right address boxes
2078                 if (cursor.par()->layout()->margintype
2079                     == MARGIN_RIGHT_ADDRESS_BOX) {
2080                         redoDrawingOfParagraph(bview, cursor);
2081                 }
2082         }
2083
2084         charInserted();
2085 }
2086
2087
2088 void LyXText::charInserted()
2089 {
2090         // Here we could call FinishUndo for every 20 characters inserted.
2091         // This is from my experience how emacs does it.
2092         static unsigned int counter;
2093         if (counter < 20) {
2094                 ++counter;
2095         } else {
2096                 finishUndo();
2097                 counter = 0;
2098         }
2099 }
2100
2101
2102 void LyXText::prepareToPrint(BufferView * bview,
2103                              Row * row, float & x,
2104                              float & fill_separator,
2105                              float & fill_hfill,
2106                              float & fill_label_hfill,
2107                              bool bidi) const
2108 {
2109         float nlh;
2110         float ns;
2111
2112         float w = row->fill();
2113         fill_hfill = 0;
2114         fill_label_hfill = 0;
2115         fill_separator = 0;
2116         fill_label_hfill = 0;
2117
2118         bool const is_rtl =
2119                 row->par()->isRightToLeftPar(bview->buffer()->params);
2120         if (is_rtl) {
2121                 x = (workWidth(bview) > 0)
2122                         ? rightMargin(bview->buffer(), row) : 0;
2123         } else
2124                 x = (workWidth(bview) > 0)
2125                         ? leftMargin(bview, row) : 0;
2126
2127         // is there a manual margin with a manual label
2128         LyXLayout_ptr const & layout = row->par()->layout();
2129
2130         if (layout->margintype == MARGIN_MANUAL
2131             && layout->labeltype == LABEL_MANUAL) {
2132                 // one more since labels are left aligned
2133                 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
2134                 if (nlh && !row->par()->getLabelWidthString().empty()) {
2135                         fill_label_hfill = labelFill(bview, row) / nlh;
2136                 }
2137         }
2138
2139         // are there any hfills in the row?
2140         float const nh = numberOfHfills(bview->buffer(), row);
2141
2142         if (nh) {
2143                 if (w > 0)
2144                         fill_hfill = w / nh;
2145         // we don't have to look at the alignment if it is ALIGN_LEFT and
2146         // if the row is already larger then the permitted width as then
2147         // we force the LEFT_ALIGN'edness!
2148         } else if (static_cast<int>(row->width()) < workWidth(bview)) {
2149                 // is it block, flushleft or flushright?
2150                 // set x how you need it
2151                 int align;
2152                 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
2153                         align = layout->align;
2154                 } else {
2155                         align = row->par()->params().align();
2156                 }
2157
2158                 // center displayed insets
2159                 Inset * inset;
2160                 if (row->par()->isInset(row->pos())
2161                     && (inset=row->par()->getInset(row->pos()))
2162                     && (inset->display())) // || (inset->scroll() < 0)))
2163                     align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2164                         ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2165                 // ERT insets should always be LEFT ALIGNED on screen
2166                 inset = row->par()->inInset();
2167                 if (inset && inset->owner() &&
2168                         inset->owner()->lyxCode() == Inset::ERT_CODE)
2169                 {
2170                         align = LYX_ALIGN_LEFT;
2171                 }
2172
2173                 switch (align) {
2174             case LYX_ALIGN_BLOCK:
2175                         ns = numberOfSeparators(bview->buffer(), row);
2176                         if (ns && row->next() && row->next()->par() == row->par() &&
2177                             !(row->next()->par()->isNewline(row->next()->pos() - 1))
2178                             && !(row->next()->par()->isInset(row->next()->pos())
2179                                  && row->next()->par()->getInset(row->next()->pos())
2180                                  && row->next()->par()->getInset(row->next()->pos())->display())
2181                                 )
2182                         {
2183                                 fill_separator = w / ns;
2184                         } else if (is_rtl) {
2185                                 x += w;
2186                         }
2187                         break;
2188             case LYX_ALIGN_RIGHT:
2189                         x += w;
2190                         break;
2191             case LYX_ALIGN_CENTER:
2192                         x += w / 2;
2193                         break;
2194                 }
2195         }
2196         if (!bidi)
2197                 return;
2198
2199         computeBidiTables(bview->buffer(), row);
2200         if (is_rtl) {
2201                 pos_type main_body =
2202                         beginningOfMainBody(bview->buffer(), row->par());
2203                 pos_type last = rowLast(row);
2204
2205                 if (main_body > 0 &&
2206                     (main_body - 1 > last ||
2207                      !row->par()->isLineSeparator(main_body - 1))) {
2208                         x += font_metrics::width(layout->labelsep,
2209                                             getLabelFont(bview->buffer(), row->par()));
2210                         if (main_body - 1 <= last)
2211                                 x += fill_label_hfill;
2212                 }
2213         }
2214 }
2215
2216
2217 // important for the screen
2218
2219
2220 // the cursor set functions have a special mechanism. When they
2221 // realize, that you left an empty paragraph, they will delete it.
2222 // They also delete the corresponding row
2223
2224 void LyXText::cursorRightOneWord(BufferView * bview) const
2225 {
2226         // treat floats, HFills and Insets as words
2227         LyXCursor tmpcursor = cursor;
2228         // CHECK See comment on top of text.C
2229
2230         if (tmpcursor.pos() == tmpcursor.par()->size()
2231             && tmpcursor.par()->next()) {
2232                         tmpcursor.par(tmpcursor.par()->next());
2233                         tmpcursor.pos(0);
2234         } else {
2235                 int steps = 0;
2236
2237                 // Skip through initial nonword stuff.
2238                 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2239                        ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2240                   //    printf("Current pos1 %d", tmpcursor.pos()) ;
2241                         tmpcursor.pos(tmpcursor.pos() + 1);
2242                         ++steps;
2243                 }
2244                 // Advance through word.
2245                 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2246                         tmpcursor.par()->isWord(tmpcursor.pos())) {
2247                   //     printf("Current pos2 %d", tmpcursor.pos()) ;
2248                         tmpcursor.pos(tmpcursor.pos() + 1);
2249                         ++steps;
2250                 }
2251         }
2252         setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2253 }
2254
2255
2256 void LyXText::cursorTab(BufferView * bview) const
2257 {
2258         LyXCursor tmpcursor = cursor;
2259         while (tmpcursor.pos() < tmpcursor.par()->size()
2260            && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2261         tmpcursor.pos(tmpcursor.pos() + 1);
2262
2263         if (tmpcursor.pos() == tmpcursor.par()->size()) {
2264                 if (tmpcursor.par()->next()) {
2265                         tmpcursor.par(tmpcursor.par()->next());
2266                         tmpcursor.pos(0);
2267                 }
2268         } else
2269                 tmpcursor.pos(tmpcursor.pos() + 1);
2270         setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2271 }
2272
2273
2274 // Skip initial whitespace at end of word and move cursor to *start*
2275 // of prior word, not to end of next prior word.
2276 void LyXText::cursorLeftOneWord(BufferView * bview)  const
2277 {
2278         LyXCursor tmpcursor = cursor;
2279         cursorLeftOneWord(tmpcursor);
2280         setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2281 }
2282
2283
2284 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2285 {
2286         // treat HFills, floats and Insets as words
2287         cur = cursor;
2288         while (cur.pos()
2289                && (cur.par()->isSeparator(cur.pos() - 1)
2290                    || cur.par()->isKomma(cur.pos() - 1))
2291                && !(cur.par()->isHfill(cur.pos() - 1)
2292                     || cur.par()->isInset(cur.pos() - 1)))
2293                 cur.pos(cur.pos() - 1);
2294
2295         if (cur.pos()
2296             && (cur.par()->isInset(cur.pos() - 1)
2297                 || cur.par()->isHfill(cur.pos() - 1))) {
2298                 cur.pos(cur.pos() - 1);
2299         } else if (!cur.pos()) {
2300                 if (cur.par()->previous()) {
2301                         cur.par(cur.par()->previous());
2302                         cur.pos(cur.par()->size());
2303                 }
2304         } else {                // Here, cur != 0
2305                 while (cur.pos() > 0 &&
2306                        cur.par()->isWord(cur.pos() - 1))
2307                         cur.pos(cur.pos() - 1);
2308         }
2309 }
2310
2311
2312 // Select current word. This depends on behaviour of
2313 // CursorLeftOneWord(), so it is patched as well.
2314 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2315                       word_location const loc) const
2316 {
2317         // first put the cursor where we wana start to select the word
2318         from = cursor;
2319         switch (loc) {
2320         case WHOLE_WORD_STRICT:
2321                 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2322                     || cursor.par()->isSeparator(cursor.pos())
2323                     || cursor.par()->isKomma(cursor.pos())
2324                     || cursor.par()->isSeparator(cursor.pos() - 1)
2325                     || cursor.par()->isKomma(cursor.pos() - 1)) {
2326                         to = from;
2327                         return;
2328                 }
2329                 // no break here, we go to the next
2330
2331         case WHOLE_WORD:
2332                 // Move cursor to the beginning, when not already there.
2333                 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2334                     && !from.par()->isKomma(from.pos() - 1))
2335                         cursorLeftOneWord(from);
2336                 break;
2337         case PREVIOUS_WORD:
2338                 // always move the cursor to the beginning of previous word
2339                 cursorLeftOneWord(from);
2340                 break;
2341         case NEXT_WORD:
2342                 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2343                 break;
2344         case PARTIAL_WORD:
2345                 break;
2346         }
2347         to = from;
2348         while (to.pos() < to.par()->size()
2349                && !to.par()->isSeparator(to.pos())
2350                && !to.par()->isKomma(to.pos())
2351                && !to.par()->isHfill(to.pos())
2352                && !to.par()->isInset(to.pos()))
2353         {
2354                 to.pos(to.pos() + 1);
2355         }
2356 }
2357
2358
2359 void LyXText::selectWord(BufferView * bview, word_location const loc)
2360 {
2361         LyXCursor from;
2362         LyXCursor to;
2363         getWord(from, to, loc);
2364         if (cursor != from)
2365                 setCursor(bview, from.par(), from.pos());
2366         if (to == from)
2367                 return;
2368         selection.cursor = cursor;
2369         setCursor(bview, to.par(), to.pos());
2370         setSelection(bview);
2371 }
2372
2373
2374 // Select the word currently under the cursor when no
2375 // selection is currently set
2376 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2377                                         word_location const loc)
2378 {
2379         if (!selection.set()) {
2380                 selectWord(bview, loc);
2381                 return selection.set();
2382         }
2383         return false;
2384 }
2385
2386
2387 // This function is only used by the spellchecker for NextWord().
2388 // It doesn't handle LYX_ACCENTs and probably never will.
2389 WordLangTuple const
2390 LyXText::selectNextWordToSpellcheck(BufferView * bview, float & value) const
2391 {
2392         if (the_locking_inset) {
2393                 WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bview, value);
2394                 if (!word.word().empty()) {
2395                         value += float(cursor.y());
2396                         value /= float(height); 
2397                         return word;
2398                 }
2399                 // we have to go on checking so move cursor to the next char
2400                 if (cursor.pos() == cursor.par()->size()) {
2401                         if (!cursor.par()->next())
2402                                 return word;
2403                         cursor.par(cursor.par()->next());
2404                         cursor.pos(0);
2405                 } else
2406                         cursor.pos(cursor.pos() + 1);
2407         }
2408         Paragraph * tmppar = cursor.par();
2409
2410         // If this is not the very first word, skip rest of
2411         // current word because we are probably in the middle
2412         // of a word if there is text here.
2413         if (cursor.pos() || cursor.par()->previous()) {
2414                 while (cursor.pos() < cursor.par()->size()
2415                        && cursor.par()->isLetter(cursor.pos()))
2416                         cursor.pos(cursor.pos() + 1);
2417         }
2418
2419         // Now, skip until we have real text (will jump paragraphs)
2420         while ((cursor.par()->size() > cursor.pos()
2421                && (!cursor.par()->isLetter(cursor.pos()))
2422                && (!cursor.par()->isInset(cursor.pos()) ||
2423                            !cursor.par()->getInset(cursor.pos())->allowSpellcheck()))
2424                || (cursor.par()->size() == cursor.pos()
2425                    && cursor.par()->next()))
2426         {
2427                 if (cursor.pos() == cursor.par()->size()) {
2428                         cursor.par(cursor.par()->next());
2429                         cursor.pos(0);
2430                 } else
2431                         cursor.pos(cursor.pos() + 1);
2432         }
2433
2434         // now check if we hit an inset so it has to be a inset containing text!
2435         if (cursor.pos() < cursor.par()->size() &&
2436             cursor.par()->isInset(cursor.pos()))
2437         {
2438                 // lock the inset!
2439                 cursor.par()->getInset(cursor.pos())->edit(bview);
2440                 // now call us again to do the above trick
2441                 // but obviously we have to start from down below ;)
2442                 return bview->text->selectNextWordToSpellcheck(bview, value);
2443         }
2444
2445         // Update the value if we changed paragraphs
2446         if (cursor.par() != tmppar) {
2447                 setCursor(bview, cursor.par(), cursor.pos());
2448                 value = float(cursor.y())/float(height);
2449         }
2450
2451         // Start the selection from here
2452         selection.cursor = cursor;
2453
2454         string lang_code(
2455                 getFont(bview->buffer(), cursor.par(), cursor.pos())
2456                         .language()->code());
2457         // and find the end of the word (insets like optional hyphens
2458         // and ligature break are part of a word)
2459         while (cursor.pos() < cursor.par()->size()
2460                && (cursor.par()->isLetter(cursor.pos())))
2461                 cursor.pos(cursor.pos() + 1);
2462
2463         // Finally, we copy the word to a string and return it
2464         string str;
2465         if (selection.cursor.pos() < cursor.pos()) {
2466                 pos_type i;
2467                 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2468                         if (!cursor.par()->isInset(i))
2469                                 str += cursor.par()->getChar(i);
2470                 }
2471         }
2472         return WordLangTuple(str, lang_code);
2473 }
2474
2475
2476 // This one is also only for the spellchecker
2477 void LyXText::selectSelectedWord(BufferView * bview)
2478 {
2479         if (the_locking_inset) {
2480                 the_locking_inset->selectSelectedWord(bview);
2481                 return;
2482         }
2483         // move cursor to the beginning
2484         setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2485
2486         // set the sel cursor
2487         selection.cursor = cursor;
2488
2489         // now find the end of the word
2490         while (cursor.pos() < cursor.par()->size()
2491                && (cursor.par()->isLetter(cursor.pos())))
2492                 cursor.pos(cursor.pos() + 1);
2493
2494         setCursor(bview, cursor.par(), cursor.pos());
2495
2496         // finally set the selection
2497         setSelection(bview);
2498 }
2499
2500
2501 // Delete from cursor up to the end of the current or next word.
2502 void LyXText::deleteWordForward(BufferView * bview)
2503 {
2504         if (cursor.par()->empty())
2505                 cursorRight(bview);
2506         else {
2507                 LyXCursor tmpcursor = cursor;
2508                 tmpcursor.row(0); // ??
2509                 selection.set(true); // to avoid deletion
2510                 cursorRightOneWord(bview);
2511                 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2512                 selection.cursor = cursor;
2513                 cursor = tmpcursor;
2514                 setSelection(bview);
2515
2516                 // Great, CutSelection() gets rid of multiple spaces.
2517                 cutSelection(bview, true, false);
2518         }
2519 }
2520
2521
2522 // Delete from cursor to start of current or prior word.
2523 void LyXText::deleteWordBackward(BufferView * bview)
2524 {
2525         if (cursor.par()->empty())
2526                 cursorLeft(bview);
2527         else {
2528                 LyXCursor tmpcursor = cursor;
2529                 tmpcursor.row(0); // ??
2530                 selection.set(true); // to avoid deletion
2531                 cursorLeftOneWord(bview);
2532                 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2533                 selection.cursor = cursor;
2534                 cursor = tmpcursor;
2535                 setSelection(bview);
2536                 cutSelection(bview, true, false);
2537         }
2538 }
2539
2540
2541 // Kill to end of line.
2542 void LyXText::deleteLineForward(BufferView * bview)
2543 {
2544         if (cursor.par()->empty())
2545                 // Paragraph is empty, so we just go to the right
2546                 cursorRight(bview);
2547         else {
2548                 LyXCursor tmpcursor = cursor;
2549                 // We can't store the row over a regular setCursor
2550                 // so we set it to 0 and reset it afterwards.
2551                 tmpcursor.row(0); // ??
2552                 selection.set(true); // to avoid deletion
2553                 cursorEnd(bview);
2554                 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2555                 selection.cursor = cursor;
2556                 cursor = tmpcursor;
2557                 setSelection(bview);
2558                 // What is this test for ??? (JMarc)
2559                 if (!selection.set()) {
2560                         deleteWordForward(bview);
2561                 } else {
2562                         cutSelection(bview, true, false);
2563                 }
2564         }
2565 }
2566
2567
2568 // Change the case of a word at cursor position.
2569 // This function directly manipulates Paragraph::text because there
2570 // is no Paragraph::SetChar currently. I did what I could to ensure
2571 // that it is correct. I guess part of it should be moved to
2572 // Paragraph, but it will have to change for 1.1 anyway. At least
2573 // it does not access outside of the allocated array as the older
2574 // version did. (JMarc)
2575 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2576 {
2577         LyXCursor from;
2578         LyXCursor to;
2579
2580         if (selection.set()) {
2581                 from = selection.start;
2582                 to = selection.end;
2583         } else {
2584                 getWord(from, to, PARTIAL_WORD);
2585                 setCursor(bview, to.par(), to.pos() + 1);
2586         }
2587
2588         changeRegionCase(bview, from, to, action);
2589 }
2590
2591
2592 void LyXText::changeRegionCase(BufferView * bview,
2593                                LyXCursor const & from,
2594                                LyXCursor const & to,
2595                                LyXText::TextCase action)
2596 {
2597         lyx::Assert(from <= to);
2598
2599         setUndo(bview, Undo::FINISH, from.par(), to.par()->next());
2600
2601         pos_type pos = from.pos();
2602         Paragraph * par = from.par();
2603
2604         while (par && (pos != to.pos() || par != to.par())) {
2605                 if (pos == par->size()) {
2606                         par = par->next();
2607                         pos = 0;
2608                         continue;
2609                 }
2610                 unsigned char c = par->getChar(pos);
2611                 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2612                         switch (action) {
2613                         case text_lowercase:
2614                                 c = lowercase(c);
2615                                 break;
2616                         case text_capitalization:
2617                                 c = uppercase(c);
2618                                 action = text_lowercase;
2619                                 break;
2620                         case text_uppercase:
2621                                 c = uppercase(c);
2622                                 break;
2623                         }
2624                 }
2625                 par->setChar(pos, c);
2626                 checkParagraph(bview, par, pos);
2627
2628                 ++pos;
2629         }
2630         if (to.row() != from.row()) {
2631                 refresh_y = from.y() - from.row()->baseline();
2632                 refresh_row = from.row();
2633                 status(bview, LyXText::NEED_MORE_REFRESH);
2634         }
2635 }
2636
2637
2638 void LyXText::transposeChars(BufferView & bview)
2639 {
2640         Paragraph * tmppar = cursor.par();
2641
2642         setUndo(&bview, Undo::FINISH, tmppar, tmppar->next());
2643
2644         pos_type tmppos = cursor.pos();
2645
2646         // First decide if it is possible to transpose at all
2647
2648         // We are at the beginning of a paragraph.
2649         if (tmppos == 0) return;
2650
2651         // We are at the end of a paragraph.
2652         if (tmppos == tmppar->size() - 1) return;
2653
2654         unsigned char c1 = tmppar->getChar(tmppos);
2655         unsigned char c2 = tmppar->getChar(tmppos - 1);
2656
2657         if (c1 != Paragraph::META_INSET
2658             && c2 != Paragraph::META_INSET) {
2659                 tmppar->setChar(tmppos, c2);
2660                 tmppar->setChar(tmppos - 1, c1);
2661         }
2662         // We should have an implementation that handles insets
2663         // as well, but that will have to come later. (Lgb)
2664         checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2665 }
2666
2667
2668 void LyXText::Delete(BufferView * bview)
2669 {
2670         // this is a very easy implementation
2671
2672         LyXCursor old_cursor = cursor;
2673         int const old_cur_par_id = old_cursor.par()->id();
2674         int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2675                 old_cursor.par()->previous()->id() : 0;
2676
2677         // just move to the right
2678         cursorRight(bview);
2679
2680         // CHECK Look at the comment here.
2681         // This check is not very good...
2682         // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2683         // and that can very well delete the par or par->previous in
2684         // old_cursor. Will a solution where we compare paragraph id's
2685         //work better?
2686         if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2687             == old_cur_par_prev_id
2688             && cursor.par()->id() != old_cur_par_id) {
2689                 // delete-empty-paragraph-mechanism has done it
2690                 return;
2691         }
2692
2693         // if you had success make a backspace
2694         if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2695                 LyXCursor tmpcursor = cursor;
2696                 // to make sure undo gets the right cursor position
2697                 cursor = old_cursor;
2698                 setUndo(bview, Undo::DELETE,
2699                         cursor.par(), cursor.par()->next());
2700                 cursor = tmpcursor;
2701                 backspace(bview);
2702         }
2703 }
2704
2705
2706 void LyXText::backspace(BufferView * bview)
2707 {
2708         // Get the font that is used to calculate the baselineskip
2709         pos_type lastpos = cursor.par()->size();
2710         LyXFont rawparfont =
2711                 cursor.par()->getFontSettings(bview->buffer()->params,
2712                                               lastpos - 1);
2713
2714         if (cursor.pos() == 0) {
2715                 // The cursor is at the beginning of a paragraph,
2716                 // so the the backspace will collapse two paragraphs into one.
2717
2718                 // we may paste some paragraphs
2719
2720                 // is it an empty paragraph?
2721
2722                 if ((lastpos == 0
2723                      || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2724                         // This is an empty paragraph and we delete it just by moving the cursor one step
2725                         // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2726                         // of the paragraph.
2727
2728                         if (cursor.par()->previous()) {
2729                                 Paragraph * tmppar = cursor.par()->previous();
2730                                 if (cursor.par()->layout() == tmppar->layout()
2731                                     && cursor.par()->getAlign() == tmppar->getAlign()) {
2732                                         // Inherit bottom DTD from the paragraph below.
2733                                         // (the one we are deleting)
2734                                         tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2735                                         tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2736                                         tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2737                                 }
2738
2739                                 cursorLeft(bview);
2740
2741                                 // the layout things can change the height of a row !
2742                                 int const tmpheight = cursor.row()->height();
2743                                 setHeightOfRow(bview, cursor.row());
2744                                 if (cursor.row()->height() != tmpheight) {
2745                                         refresh_y = cursor.y() - cursor.row()->baseline();
2746                                         refresh_row = cursor.row();
2747                                         status(bview, LyXText::NEED_MORE_REFRESH);
2748                                 }
2749                                 return;
2750                         }
2751                 }
2752
2753                 if (cursor.par()->previous()) {
2754                         setUndo(bview, Undo::DELETE,
2755                                 cursor.par()->previous(), cursor.par()->next());
2756                 }
2757
2758                 Paragraph * tmppar = cursor.par();
2759                 Row * tmprow = cursor.row();
2760
2761                 // We used to do cursorLeftIntern() here, but it is
2762                 // not a good idea since it triggers the auto-delete
2763                 // mechanism. So we do a cursorLeftIntern()-lite,
2764                 // without the dreaded mechanism. (JMarc)
2765                 if (cursor.par()->previous()) {
2766                         // steps into the above paragraph.
2767                         setCursorIntern(bview, cursor.par()->previous(),
2768                                         cursor.par()->previous()->size(),
2769                                         false);
2770                 }
2771
2772                 // Pasting is not allowed, if the paragraphs have different
2773                 // layout. I think it is a real bug of all other
2774                 // word processors to allow it. It confuses the user.
2775                 // Even so with a footnote paragraph and a non-footnote
2776                 // paragraph. I will not allow pasting in this case,
2777                 // because the user would be confused if the footnote behaves
2778                 // different wether it is open or closed.
2779
2780                 //      Correction: Pasting is always allowed with standard-layout
2781                 LyXTextClass const & tclass =
2782                         bview->buffer()->params.getLyXTextClass();
2783
2784                 if (cursor.par() != tmppar
2785                     && (cursor.par()->layout() == tmppar->layout()
2786                         || tmppar->layout() == tclass.defaultLayout())
2787                     && cursor.par()->getAlign() == tmppar->getAlign()) {
2788                         removeParagraph(tmprow);
2789                         removeRow(tmprow);
2790                         mergeParagraph(bview->buffer()->params, cursor.par());
2791
2792                         if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2793                                 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2794                         // strangely enough it seems that commenting out the line above removes
2795                         // most or all of the segfaults. I will however also try to move the
2796                         // two Remove... lines in front of the PasteParagraph too.
2797                         else
2798                                 if (cursor.pos())
2799                                         cursor.pos(cursor.pos() - 1);
2800
2801                         status(bview, LyXText::NEED_MORE_REFRESH);
2802                         refresh_row = cursor.row();
2803                         refresh_y = cursor.y() - cursor.row()->baseline();
2804
2805                         // remove the lost paragraph
2806                         // This one is not safe, since the paragraph that the tmprow and the
2807                         // following rows belong to has been deleted by the PasteParagraph
2808                         // above. The question is... could this be moved in front of the
2809                         // PasteParagraph?
2810                         //RemoveParagraph(tmprow);
2811                         //RemoveRow(tmprow);
2812
2813                         // This rebuilds the rows.
2814                         appendParagraph(bview, cursor.row());
2815                         updateCounters(bview);
2816
2817                         // the row may have changed, block, hfills etc.
2818                         setCursor(bview, cursor.par(), cursor.pos(), false);
2819                 }
2820         } else {
2821                 // this is the code for a normal backspace, not pasting
2822                 // any paragraphs
2823                 setUndo(bview, Undo::DELETE,
2824                         cursor.par(), cursor.par()->next());
2825                 // We used to do cursorLeftIntern() here, but it is
2826                 // not a good idea since it triggers the auto-delete
2827                 // mechanism. So we do a cursorLeftIntern()-lite,
2828                 // without the dreaded mechanism. (JMarc)
2829                 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2830                                 false, cursor.boundary());
2831
2832                 // some insets are undeletable here
2833                 if (cursor.par()->isInset(cursor.pos())) {
2834                         if (!cursor.par()->getInset(cursor.pos())->deletable())
2835                                 return;
2836                         // force complete redo when erasing display insets
2837                         // this is a cruel method but safe..... Matthias
2838                         if (cursor.par()->getInset(cursor.pos())->display() ||
2839                             cursor.par()->getInset(cursor.pos())->needFullRow()) {
2840                                 cursor.par()->erase(cursor.pos());
2841                                 redoParagraph(bview);
2842                                 return;
2843                         }
2844                 }
2845
2846                 Row * row = cursor.row();
2847                 int y = cursor.y() - row->baseline();
2848                 pos_type z;
2849                 // remember that a space at the end of a row doesnt count
2850                 // when calculating the fill
2851                 if (cursor.pos() < rowLast(row) ||
2852                     !cursor.par()->isLineSeparator(cursor.pos())) {
2853                         row->fill(row->fill() + singleWidth(bview,
2854                                                             cursor.par(),
2855                                                             cursor.pos()));
2856                 }
2857
2858                 // some special code when deleting a newline. This is similar
2859                 // to the behavior when pasting paragraphs
2860                 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2861                         cursor.par()->erase(cursor.pos());
2862                         // refresh the positions
2863                         Row * tmprow = row;
2864                         while (tmprow->next() && tmprow->next()->par() == row->par()) {
2865                                 tmprow = tmprow->next();
2866                                 tmprow->pos(tmprow->pos() - 1);
2867                         }
2868                         if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2869                                 cursor.pos(cursor.pos() - 1);
2870
2871                         if (cursor.pos() < cursor.par()->size()
2872                             && !cursor.par()->isSeparator(cursor.pos())) {
2873                                 cursor.par()->insertChar(cursor.pos(), ' ');
2874                                 setCharFont(bview->buffer(), cursor.par(),
2875                                             cursor.pos(), current_font);
2876                                 // refresh the positions
2877                                 tmprow = row;
2878                                 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2879                                         tmprow = tmprow->next();
2880                                         tmprow->pos(tmprow->pos() + 1);
2881                                 }
2882                         }
2883                 } else {
2884                         cursor.par()->erase(cursor.pos());
2885
2886                         // refresh the positions
2887                         Row * tmprow = row;
2888                         while (tmprow->next()
2889                                && tmprow->next()->par() == row->par()) {
2890                                 tmprow = tmprow->next();
2891                                 tmprow->pos(tmprow->pos() - 1);
2892                         }
2893
2894                         // delete newlines at the beginning of paragraphs
2895                         while (!cursor.par()->empty() &&
2896                                cursor.par()->isNewline(cursor.pos()) &&
2897                                cursor.pos() == beginningOfMainBody(bview->buffer(),
2898                                                                    cursor.par())) {
2899                                 cursor.par()->erase(cursor.pos());
2900                                 // refresh the positions
2901                                 tmprow = row;
2902                                 while (tmprow->next() &&
2903                                        tmprow->next()->par() == row->par()) {
2904                                         tmprow = tmprow->next();
2905                                         tmprow->pos(tmprow->pos() - 1);
2906                                 }
2907                         }
2908                 }
2909
2910                 // is there a break one row above
2911                 if (row->previous() && row->previous()->par() == row->par()) {
2912                         z = nextBreakPoint(bview, row->previous(),
2913                                            workWidth(bview));
2914                         if (z >= row->pos()) {
2915                                 row->pos(z + 1);
2916
2917                                 Row * tmprow = row->previous();
2918
2919                                 // maybe the current row is now empty
2920                                 if (row->pos() >= row->par()->size()) {
2921                                         // remove it
2922                                         removeRow(row);
2923                                         need_break_row = 0;
2924                                 } else {
2925                                         breakAgainOneRow(bview, row);
2926                                         if (row->next() && row->next()->par() == row->par())
2927                                                 need_break_row = row->next();
2928                                         else
2929                                                 need_break_row = 0;
2930                                 }
2931
2932                                 // set the dimensions of the row above
2933                                 y -= tmprow->height();
2934                                 tmprow->fill(fill(bview, tmprow,
2935                                                   workWidth(bview)));
2936                                 setHeightOfRow(bview, tmprow);
2937
2938                                 refresh_y = y;
2939                                 refresh_row = tmprow;
2940                                 status(bview, LyXText::NEED_MORE_REFRESH);
2941                                 setCursor(bview, cursor.par(), cursor.pos(),
2942                                           false, cursor.boundary());
2943                                 //current_font = rawtmpfont;
2944                                 //real_current_font = realtmpfont;
2945                                 // check, whether the last character's font has changed.
2946                                 if (rawparfont !=
2947                                     cursor.par()->getFontSettings(bview->buffer()->params,
2948                                                                   cursor.par()->size() - 1))
2949                                         redoHeightOfParagraph(bview, cursor);
2950                                 return;
2951                         }
2952                 }
2953
2954                 // break the cursor row again
2955                 if (row->next() && row->next()->par() == row->par() &&
2956                     (rowLast(row) == row->par()->size() - 1 ||
2957                      nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2958
2959                         // it can happen that a paragraph loses one row
2960                         // without a real breakup. This is when a word
2961                         // is to long to be broken. Well, I don t care this
2962                         // hack ;-)
2963                         if (rowLast(row) == row->par()->size() - 1)
2964                                 removeRow(row->next());
2965
2966                         refresh_y = y;
2967                         refresh_row = row;
2968                         status(bview, LyXText::NEED_MORE_REFRESH);
2969
2970                         breakAgainOneRow(bview, row);
2971                         // will the cursor be in another row now?
2972                         if (row->next() && row->next()->par() == row->par() &&
2973                             rowLast(row) <= cursor.pos()) {
2974                                 row = row->next();
2975                                 breakAgainOneRow(bview, row);
2976                         }
2977
2978                         setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2979
2980                         if (row->next() && row->next()->par() == row->par())
2981                                 need_break_row = row->next();
2982                         else
2983                                 need_break_row = 0;
2984                 } else  {
2985                         // set the dimensions of the row
2986                         row->fill(fill(bview, row, workWidth(bview)));
2987                         int const tmpheight = row->height();
2988                         setHeightOfRow(bview, row);
2989                         if (tmpheight == row->height())
2990                                 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2991                         else
2992                                 status(bview, LyXText::NEED_MORE_REFRESH);
2993                         refresh_y = y;
2994                         refresh_row = row;
2995                         setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2996                 }
2997         }
2998
2999         // current_font = rawtmpfont;
3000         // real_current_font = realtmpfont;
3001
3002         if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
3003             != cursor.boundary())
3004                 setCursor(bview, cursor.par(), cursor.pos(), false,
3005                           !cursor.boundary());
3006
3007         lastpos = cursor.par()->size();
3008         if (cursor.pos() == lastpos)
3009                 setCurrentFont(bview);
3010
3011         // check, whether the last characters font has changed.
3012         if (rawparfont !=
3013             cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
3014                 redoHeightOfParagraph(bview, cursor);
3015         } else {
3016                 // now the special right address boxes
3017                 if (cursor.par()->layout()->margintype
3018                     == MARGIN_RIGHT_ADDRESS_BOX) {
3019                         redoDrawingOfParagraph(bview, cursor);
3020                 }
3021         }
3022 }
3023
3024
3025 bool LyXText::paintRowBackground(DrawRowParams & p)
3026 {
3027         bool clear_area = true;
3028         Inset * inset = 0;
3029         LyXFont font(LyXFont::ALL_SANE);
3030
3031         pos_type const last = rowLastPrintable(p.row);
3032
3033         if (!p.bv->screen().forceClear() && last == p.row->pos()
3034                 && p.row->par()->isInset(p.row->pos())) {
3035                 inset = p.row->par()->getInset(p.row->pos());
3036                 if (inset) {
3037                         clear_area = inset->doClearArea();
3038                 }
3039         }
3040
3041         if (p.cleared) {
3042                 return true;
3043         }
3044
3045         if (clear_area) {
3046                 int const x = p.xo;
3047                 int const y = p.yo < 0 ? 0 : p.yo;
3048                 int const h = p.yo < 0 ? p.row->height() + p.yo : p.row->height();
3049                 p.pain->fillRectangle(x, y, p.width, h, backgroundColor());
3050                 return true;
3051         }
3052
3053         if (inset == 0)
3054                 return false;
3055
3056         int h = p.row->baseline() - inset->ascent(p.bv, font);
3057
3058         // first clear the whole row above the inset!
3059         if (h > 0) {
3060                 p.pain->fillRectangle(p.xo, p.yo, p.width, h, backgroundColor());
3061         }
3062
3063         // clear the space below the inset!
3064         h += inset->ascent(p.bv, font) + inset->descent(p.bv, font);
3065         if ((p.row->height() - h) > 0) {
3066                 p.pain->fillRectangle(p.xo, p.yo + h,
3067                         p.width, p.row->height() - h, backgroundColor());
3068         }
3069
3070         // clear the space behind the inset, if needed
3071         if (!inset->display() && !inset->needFullRow()) {
3072                 int const xp = int(p.x) + inset->width(p.bv, font);
3073                 if (p.width - xp > 0) {
3074                         p.pain->fillRectangle(xp, p.yo, p.width - xp,
3075                                 p.row->height(), backgroundColor());
3076                 }
3077         }
3078
3079         return false;
3080 }
3081
3082
3083 void LyXText::paintRowSelection(DrawRowParams & p)
3084 {
3085         bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3086
3087         // the current selection
3088         int const startx = selection.start.x();
3089         int const endx = selection.end.x();
3090         int const starty = selection.start.y();
3091         int const endy = selection.end.y();
3092         Row const * startrow = selection.start.row();
3093         Row const * endrow = selection.end.row();
3094
3095         Row * row = p.row;
3096
3097         if (bidi_same_direction) {
3098                 int x;
3099                 int y = p.yo;
3100                 int w;
3101                 int h = row->height();
3102
3103                 if (startrow == row && endrow == row) {
3104                         if (startx < endx) {
3105                                 x = p.xo + startx;
3106                                 w = endx - startx;
3107                                 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3108                         } else {
3109                                 x = p.xo + endx;
3110                                 w = startx - endx;
3111                                 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3112                         }
3113                 } else if (startrow == row) {
3114                         int const x = (is_rtl) ? p.xo : (p.xo + startx);
3115                         int const w = (is_rtl) ? startx : (p.width - startx);
3116                         p.pain->fillRectangle(x, y, w, h, LColor::selection);
3117                 } else if (endrow == row) {
3118                         int const x = (is_rtl) ? (p.xo + endx) : p.xo;
3119                         int const w = (is_rtl) ? (p.width - endx) : endx;
3120                         p.pain->fillRectangle(x, y, w, h, LColor::selection);
3121                 } else if (p.y > starty && p.y < endy) {
3122                         p.pain->fillRectangle(p.xo, y, p.width, h, LColor::selection);
3123                 }
3124                 return;
3125         } else if (startrow != row && endrow != row) {
3126                 if (p.y > starty && p.y < endy) {
3127                         int w = p.width;
3128                         int h = row->height();
3129                         p.pain->fillRectangle(p.xo, p.yo, w, h, LColor::selection);
3130                 }
3131                 return;
3132         }
3133
3134         if ((startrow != row && !is_rtl) || (endrow != row && is_rtl))
3135                 p.pain->fillRectangle(p.xo, p.yo, int(p.x), row->height(), LColor::selection);
3136
3137         Buffer const * buffer = p.bv->buffer();
3138         Paragraph * par = row->par();
3139         pos_type main_body = beginningOfMainBody(buffer, par);
3140         pos_type const last = rowLastPrintable(row);
3141         float tmpx = p.x;
3142
3143         for (pos_type vpos = row->pos(); vpos <= last; ++vpos)  {
3144                 pos_type pos = vis2log(vpos);
3145                 float const old_tmpx = tmpx;
3146                 if (main_body > 0 && pos == main_body - 1) {
3147                         LyXLayout_ptr const & layout = par->layout();
3148                         LyXFont const lfont = getLabelFont(buffer, par);
3149
3150                         tmpx += p.label_hfill + font_metrics::width(layout->labelsep, lfont);
3151
3152                         if (par->isLineSeparator(main_body - 1))
3153                                 tmpx -= singleWidth(p.bv, par, main_body - 1);
3154                 }
3155
3156                 if (hfillExpansion(buffer, row, pos)) {
3157                         tmpx += singleWidth(p.bv, par, pos);
3158                         if (pos >= main_body)
3159                                 tmpx += p.hfill;
3160                         else
3161                                 tmpx += p.label_hfill;
3162                 }
3163
3164                 else if (par->isSeparator(pos)) {
3165                         tmpx += singleWidth(p.bv, par, pos);
3166                         if (pos >= main_body)
3167                                 tmpx += p.separator;
3168                 } else {
3169                         tmpx += singleWidth(p.bv, par, pos);
3170                 }
3171
3172                 if ((startrow != row || selection.start.pos() <= pos) &&
3173                         (endrow != row || pos < selection.end.pos())) {
3174                         // Here we do not use p.x as p.xo was added to p.x.
3175                         p.pain->fillRectangle(int(old_tmpx), p.yo,
3176                                 int(tmpx - old_tmpx + 1),
3177                                 row->height(), LColor::selection);
3178                 }
3179         }
3180
3181         if ((startrow != row && is_rtl) || (endrow != row && !is_rtl)) {
3182                 p.pain->fillRectangle(p.xo + int(tmpx),
3183                                       p.yo, int(p.bv->workWidth() - tmpx),
3184                                       row->height(), LColor::selection);
3185         }
3186 }
3187
3188
3189 void LyXText::paintRowAppendix(DrawRowParams & p)
3190 {
3191         // FIXME: can be just p.width ?
3192         int const ww = p.bv->workWidth();
3193         Paragraph * firstpar = p.row->par();
3194
3195         if (firstpar->params().appendix()) {
3196                 p.pain->line(1, p.yo, 1, p.yo + p.row->height(), LColor::appendixline);
3197                 p.pain->line(ww - 2, p.yo, ww - 2, p.yo + p.row->height(), LColor::appendixline);
3198         }
3199 }
3200
3201
3202 void LyXText::paintRowDepthBar(DrawRowParams & p)
3203 {
3204         Paragraph::depth_type const depth = p.row->par()->getDepth();
3205
3206         if (depth <= 0)
3207                 return;
3208
3209         Paragraph::depth_type prev_depth = 0;
3210         if (p.row->previous())
3211                 prev_depth = p.row->previous()->par()->getDepth();
3212         Paragraph::depth_type next_depth = 0;
3213         if (p.row->next())
3214                 next_depth = p.row->next()->par()->getDepth();
3215
3216         for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3217                 int const x = (LYX_PAPER_MARGIN / 5) * i + p.xo;
3218                 int const h = p.yo + p.row->height() - 1 - (i - next_depth - 1) * 3;
3219
3220                 p.pain->line(x, p.yo, x, h, LColor::depthbar);
3221
3222                 int const w = LYX_PAPER_MARGIN / 5;
3223
3224                 if (i > prev_depth) {
3225                         p.pain->fillRectangle(x, p.yo, w, 2, LColor::depthbar);
3226                 }
3227                 if (i > next_depth) {
3228                         p.pain->fillRectangle(x, h, w, 2, LColor::depthbar);
3229                 }
3230         }
3231 }
3232
3233
3234 int LyXText::getLengthMarkerHeight(BufferView * bv, VSpace const & vsp) const
3235 {
3236         int const arrow_size = 4;
3237         int const space_size = int(vsp.inPixels(bv));
3238
3239         if (vsp.kind() != VSpace::LENGTH) {
3240                 return space_size;
3241         }
3242
3243         LyXFont font;
3244         font.decSize();
3245         int const min_size = max(3 * arrow_size,
3246                                       font_metrics::maxAscent(font)
3247                                       + font_metrics::maxDescent(font));
3248
3249         if (vsp.length().len().value() < 0.0)
3250                 return min_size;
3251         else
3252                 return max(min_size, space_size);
3253 }
3254
3255
3256 int LyXText::drawLengthMarker(DrawRowParams & p, string const & prefix,
3257                               VSpace const & vsp, int start)
3258 {
3259         int const arrow_size = 4;
3260         int const size = getLengthMarkerHeight(p.bv, vsp);
3261         int const end = start + size;
3262
3263         // the label to display (if any)
3264         string str;
3265         // y-values for top arrow
3266         int ty1, ty2;
3267         // y-values for bottom arrow
3268         int by1, by2;
3269         switch (vsp.kind()) {
3270         case VSpace::LENGTH:
3271         {
3272                 str = prefix + " (" + vsp.asLyXCommand() + ")";
3273                 // adding or removing space
3274                 bool const added = !(vsp.length().len().value() < 0.0);
3275                 ty1 = added ? (start + arrow_size) : start;
3276                 ty2 = added ? start : (start + arrow_size);
3277                 by1 = added ? (end - arrow_size) : end;
3278                 by2 = added ? end : (end - arrow_size);
3279                 break;
3280         }
3281         case VSpace::VFILL:
3282                 str = prefix + _(" (vertical fill)");
3283                 ty1 = ty2 = start;
3284                 by1 = by2 = end;
3285                 break;
3286         default:
3287                 // nothing to draw here
3288                 return size;
3289         }
3290
3291         int const leftx = p.xo + leftMargin(p.bv, p.row);
3292         int const midx = leftx + arrow_size;
3293         int const rightx = midx + arrow_size;
3294
3295         // first the string
3296         int w = 0;
3297         int a = 0;
3298         int d = 0;
3299
3300         LyXFont font;
3301         font.setColor(LColor::added_space).decSize();
3302         font_metrics::rectText(str, font, w, a, d);
3303
3304         p.pain->rectText(leftx + 2 * arrow_size + 5,
3305                          start + ((end - start) / 2) + d,
3306                          str, font);
3307
3308         // top arrow
3309         p.pain->line(leftx, ty1, midx, ty2, LColor::added_space);
3310         p.pain->line(midx, ty2, rightx, ty1, LColor::added_space);
3311
3312         // bottom arrow
3313         p.pain->line(leftx, by1, midx, by2, LColor::added_space);
3314         p.pain->line(midx, by2, rightx, by1, LColor::added_space);
3315
3316         // joining line
3317         p.pain->line(midx, ty2, midx, by2, LColor::added_space);
3318
3319         return size;
3320 }
3321
3322
3323 int LyXText::paintPageBreak(string const & label, int y, DrawRowParams & p)
3324 {
3325         LyXFont pb_font;
3326         pb_font.setColor(LColor::pagebreak).decSize();
3327
3328         int w = 0;
3329         int a = 0;
3330         int d = 0;
3331         font_metrics::rectText(label, pb_font, w, a, d);
3332
3333         int const text_start = p.xo + ((p.width - w) / 2);
3334         int const text_end = text_start + w;
3335
3336         p.pain->rectText(text_start, y + d, label, pb_font);
3337
3338         p.pain->line(p.xo, y, text_start, y,
3339                 LColor::pagebreak, Painter::line_onoffdash);
3340         p.pain->line(text_end, y, p.xo + p.width, y,
3341                 LColor::pagebreak, Painter::line_onoffdash);
3342
3343         return 3 * defaultHeight();
3344 }
3345
3346
3347 void LyXText::paintFirstRow(DrawRowParams & p)
3348 {
3349         Paragraph * par = p.row->par();
3350         ParagraphParameters const & parparams = par->params();
3351
3352         // start of appendix?
3353         if (parparams.startOfAppendix()) {
3354                 p.pain->line(1, p.yo, p.width - 2, p.yo, LColor::appendixline);
3355         }
3356
3357         int y_top = 0;
3358
3359         // think about the margins
3360         if (!p.row->previous() && bv_owner)
3361                 y_top += LYX_PAPER_MARGIN;
3362
3363         // draw a top pagebreak
3364         if (parparams.pagebreakTop()) {
3365                 y_top += paintPageBreak(_("Page Break (top)"),
3366                         p.yo + y_top + 2 * defaultHeight(), p);
3367         }
3368
3369         // draw the additional space if needed:
3370         y_top += drawLengthMarker(p, _("Space above"),
3371                                   parparams.spaceTop(), p.yo + y_top);
3372
3373         Buffer const * buffer = p.bv->buffer();
3374
3375         LyXLayout_ptr const & layout = par->layout();
3376
3377         // think about the parskip
3378         // some parskips VERY EASY IMPLEMENTATION
3379         if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3380                 if (par->previous()) {
3381                         if (layout->latextype == LATEX_PARAGRAPH
3382                                 && !par->getDepth()) {
3383                                 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3384                         } else {
3385                                 LyXLayout_ptr const & playout =
3386                                         par->previous()->layout();
3387                                 if (playout->latextype == LATEX_PARAGRAPH
3388                                         && !par->previous()->getDepth()) {
3389                                         // is it right to use defskip here, too? (AS)
3390                                         y_top += buffer->params.getDefSkip().inPixels(p.bv);
3391                                 }
3392                         }
3393                 }
3394         }
3395
3396         int const ww = p.bv->workWidth();
3397
3398         // draw a top line
3399         if (parparams.lineTop()) {
3400                 LyXFont font(LyXFont::ALL_SANE);
3401                 int const asc = font_metrics::ascent('x', getFont(buffer, par, 0));
3402
3403                 y_top += asc;
3404
3405                 int const w = (inset_owner ?  inset_owner->width(p.bv, font) : ww);
3406                 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3407                 p.pain->line(xp, p.yo + y_top, xp + w, p.yo + y_top,
3408                         LColor::topline, Painter::line_solid,
3409                         Painter::line_thick);
3410
3411                 y_top += asc;
3412         }
3413
3414         bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3415
3416         // should we print a label?
3417         if (layout->labeltype >= LABEL_STATIC
3418             && (layout->labeltype != LABEL_STATIC
3419                 || layout->latextype != LATEX_ENVIRONMENT
3420                 || par->isFirstInSequence())) {
3421
3422                 LyXFont font = getLabelFont(buffer, par);
3423                 if (!par->getLabelstring().empty()) {
3424                         float x = p.x;
3425                         string const str = par->getLabelstring();
3426
3427                         // this is special code for the chapter layout. This is
3428                         // printed in an extra row and has a pagebreak at
3429                         // the top.
3430                         if (layout->labeltype == LABEL_COUNTER_CHAPTER) {
3431                                 if (buffer->params.secnumdepth >= 0) {
3432                                         float spacing_val = 1.0;
3433                                         if (!parparams.spacing().isDefault()) {
3434                                                 spacing_val = parparams.spacing().getValue();
3435                                         } else {
3436                                                 spacing_val = buffer->params.spacing.getValue();
3437                                         }
3438
3439                                         int const maxdesc =
3440                                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
3441                                                 + int(layout->parsep) * defaultHeight();
3442
3443                                         if (is_rtl) {
3444                                                 x = ww - leftMargin(p.bv, p.row) -
3445                                                         font_metrics::width(str, font);
3446                                         }
3447
3448                                         p.pain->text(int(x),
3449                                                 p.yo + p.row->baseline() -
3450                                                 p.row->ascent_of_text() - maxdesc,
3451                                                 str, font);
3452                                 }
3453                         } else {
3454                                 if (is_rtl) {
3455                                         x = ww - leftMargin(p.bv, p.row)
3456                                                 + font_metrics::width(layout->labelsep, font);
3457                                 } else {
3458                                         x = p.x - font_metrics::width(layout->labelsep, font)
3459                                                 - font_metrics::width(str, font);
3460                                 }
3461
3462                                 p.pain->text(int(x), p.yo + p.row->baseline(), str, font);
3463                         }
3464                 }
3465         // the labels at the top of an environment.
3466         // More or less for bibliography
3467         } else if (par->isFirstInSequence() &&
3468                 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
3469                 layout->labeltype == LABEL_BIBLIO ||
3470                 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
3471                 LyXFont font = getLabelFont(buffer, par);
3472                 if (!par->getLabelstring().empty()) {
3473                         string const str = par->getLabelstring();
3474                         float spacing_val = 1.0;
3475                         if (!parparams.spacing().isDefault()) {
3476                                 spacing_val = parparams.spacing().getValue();
3477                         } else {
3478                                 spacing_val = buffer->params.spacing.getValue();
3479                         }
3480
3481                         int maxdesc =
3482                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
3483                                 + (layout->labelbottomsep * defaultHeight()));
3484
3485                         float x = p.x;
3486                         if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3487                                 x = ((is_rtl ? leftMargin(p.bv, p.row) : p.x)
3488                                          + ww - rightMargin(buffer, p.row)) / 2;
3489                                 x -= font_metrics::width(str, font) / 2;
3490                         } else if (is_rtl) {
3491                                 x = ww - leftMargin(p.bv, p.row) -
3492                                         font_metrics::width(str, font);
3493                         }
3494                         p.pain->text(int(x), p.yo + p.row->baseline()
3495                                   - p.row->ascent_of_text() - maxdesc,
3496                                   str, font);
3497                 }
3498         }
3499
3500         if (layout->labeltype == LABEL_BIBLIO && par->bibkey) {
3501                 LyXFont font = getLayoutFont(buffer, par);
3502                 float x;
3503                 if (is_rtl) {
3504                         x = ww - leftMargin(p.bv, p.row)
3505                                 + font_metrics::width(layout->labelsep, font);
3506                 } else {
3507                         x = p.x - font_metrics::width(layout->labelsep, font)
3508                                 - par->bibkey->width(p.bv, font);
3509                 }
3510                 par->bibkey->draw(p.bv, font, p.yo + p.row->baseline(), x, p.cleared);
3511         }
3512 }
3513
3514
3515 void LyXText::paintLastRow(DrawRowParams & p)
3516 {
3517         Paragraph * par = p.row->par();
3518         ParagraphParameters const & parparams = par->params();
3519         int y_bottom = p.row->height() - 1;
3520
3521         // think about the margins
3522         if (!p.row->next() && bv_owner)
3523                 y_bottom -= LYX_PAPER_MARGIN;
3524
3525         int const ww = p.bv->workWidth();
3526
3527         // draw a bottom pagebreak
3528         if (parparams.pagebreakBottom()) {
3529                 y_bottom -= paintPageBreak(_("Page Break (bottom)"),
3530                         p.yo + y_bottom - 2 * defaultHeight(), p);
3531         }
3532
3533         // draw the additional space if needed:
3534         int const height =  getLengthMarkerHeight(p.bv,
3535                                                   parparams.spaceBottom());
3536         y_bottom -= drawLengthMarker(p, _("Space below"),
3537                                      parparams.spaceBottom(),
3538                                      p.yo + y_bottom - height);
3539
3540         Buffer const * buffer = p.bv->buffer();
3541
3542         // draw a bottom line
3543         if (parparams.lineBottom()) {
3544                 LyXFont font(LyXFont::ALL_SANE);
3545                 int const asc = font_metrics::ascent('x',
3546                         getFont(buffer, par,
3547                         max(pos_type(0), par->size() - 1)));
3548
3549                 y_bottom -= asc;
3550
3551                 int const w = (inset_owner ?  inset_owner->width(p.bv, font) : ww);
3552                 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3553                 int const y = p.yo + y_bottom;
3554                 p.pain->line(xp, y, xp + w, y, LColor::topline, Painter::line_solid,
3555                           Painter::line_thick);
3556
3557                 y_bottom -= asc;
3558         }
3559
3560         bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3561         int const endlabel = par->getEndLabel();
3562
3563         // draw an endlabel
3564         switch (endlabel) {
3565         case END_LABEL_BOX:
3566         case END_LABEL_FILLED_BOX:
3567         {
3568                 LyXFont const font = getLabelFont(buffer, par);
3569                 int const size = int(0.75 * font_metrics::maxAscent(font));
3570                 int const y = (p.yo + p.row->baseline()) - size;
3571                 int x = is_rtl ? LYX_PAPER_MARGIN : ww - LYX_PAPER_MARGIN - size;
3572
3573                 if (p.row->fill() <= size)
3574                         x += (size - p.row->fill() + 1) * (is_rtl ? -1 : 1);
3575
3576                 if (endlabel == END_LABEL_BOX) {
3577                         p.pain->rectangle(x, y, size, size, LColor::eolmarker);
3578                 } else {
3579                         p.pain->fillRectangle(x, y, size, size,
3580                                               LColor::eolmarker);
3581                 }
3582                 break;
3583         }
3584         case END_LABEL_STATIC:
3585         {
3586 #if 0
3587                 LyXFont font(LyXFont::ALL_SANE);
3588                 font = getLabelFont(buffer, par);
3589 #else
3590                 LyXFont font = getLabelFont(buffer, par);
3591 #endif
3592                 string const & str = par->layout()->endlabelstring();
3593                 int const x = is_rtl ?
3594                         int(p.x) - font_metrics::width(str, font)
3595                         : ww - rightMargin(buffer, p.row) - p.row->fill();
3596                 p.pain->text(x, p.yo + p.row->baseline(), str, font);
3597                 break;
3598         }
3599         case END_LABEL_NO_LABEL:
3600                 break;
3601         }
3602 }
3603
3604
3605 void LyXText::paintRowText(DrawRowParams & p)
3606 {
3607         Paragraph * par = p.row->par();
3608         Buffer const * buffer = p.bv->buffer();
3609
3610         pos_type const last = rowLastPrintable(p.row);
3611         pos_type main_body =
3612                 beginningOfMainBody(buffer, par);
3613         if (main_body > 0 &&
3614                 (main_body - 1 > last ||
3615                 !par->isLineSeparator(main_body - 1))) {
3616                 main_body = 0;
3617         }
3618
3619         LyXLayout_ptr const & layout = par->layout();
3620
3621         pos_type vpos = p.row->pos();
3622         while (vpos <= last) {
3623                 if (p.x > p.bv->workWidth())
3624                         break;
3625                 pos_type pos = vis2log(vpos);
3626                 if (p.x + singleWidth(p.bv, par, pos) < 0) {
3627                         p.x += singleWidth(p.bv, par, pos);
3628                         ++vpos;
3629                         continue;
3630                 }
3631                 if (main_body > 0 && pos == main_body - 1) {
3632                         int const lwidth = font_metrics::width(layout->labelsep,
3633                                 getLabelFont(buffer, par));
3634
3635                         p.x += p.label_hfill + lwidth
3636                                 - singleWidth(p.bv, par, main_body - 1);
3637                 }
3638
3639                 if (par->isHfill(pos)) {
3640                         p.x += 1;
3641
3642                         int const y0 = p.yo + p.row->baseline();
3643                         int const y1 = y0 - defaultHeight() / 2;
3644
3645                         p.pain->line(int(p.x), y1, int(p.x), y0,
3646                                      LColor::added_space);
3647
3648                         if (hfillExpansion(buffer, p.row, pos)) {
3649                                 int const y2 = (y0 + y1) / 2;
3650
3651                                 if (pos >= main_body) {
3652                                         p.pain->line(int(p.x), y2,
3653                                                   int(p.x + p.hfill), y2,
3654                                                   LColor::added_space,
3655                                                   Painter::line_onoffdash);
3656                                         p.x += p.hfill;
3657                                 } else {
3658                                         p.pain->line(int(p.x), y2,
3659                                                   int(p.x + p.label_hfill), y2,
3660                                                   LColor::added_space,
3661                                                   Painter::line_onoffdash);
3662                                         p.x += p.label_hfill;
3663                                 }
3664                                 p.pain->line(int(p.x), y1,
3665                                              int(p.x), y0,
3666                                              LColor::added_space);
3667                         }
3668                         p.x += 2;
3669                         ++vpos;
3670                 } else if (par->isSeparator(pos)) {
3671                         p.x += singleWidth(p.bv, par, pos);
3672                         if (pos >= main_body)
3673                                 p.x += p.separator;
3674                         ++vpos;
3675                 } else {
3676                         if (!draw(p, vpos))
3677                                 break;
3678                 }
3679         }
3680 }
3681
3682
3683 void LyXText::getVisibleRow(BufferView * bv, int y_offset, int x_offset,
3684                             Row * row, int y, bool cleared)
3685 {
3686         if (row->height() <= 0) {
3687                 lyxerr << "LYX_ERROR: row.height: "
3688                        << row->height() << endl;
3689                 return;
3690         }
3691
3692         DrawRowParams p;
3693
3694         // set up drawing parameters
3695         p.bv = bv;
3696         p.pain = &bv->painter();
3697         p.row = row;
3698         p.xo = x_offset;
3699         p.yo = y_offset;
3700         prepareToPrint(bv, row, p.x, p.separator, p.hfill, p.label_hfill);
3701         if (inset_owner && (p.x < 0))
3702                 p.x = 0;
3703         p.x += p.xo;
3704         p.y = y;
3705         p.width = inset_owner ? inset_owner->textWidth(bv, true) : bv->workWidth();
3706         p.cleared = cleared;
3707
3708         // start painting
3709
3710         // clear to background if necessary
3711         p.cleared = paintRowBackground(p);
3712
3713         // paint the selection background
3714         if (selection.set()) {
3715                 paintRowSelection(p);
3716         }
3717
3718         // vertical lines for appendix
3719         paintRowAppendix(p);
3720
3721         // environment depth brackets
3722         paintRowDepthBar(p);
3723
3724         // draw any stuff wanted for a first row of a paragraph
3725         if (!row->pos()) {
3726                 paintFirstRow(p);
3727         }
3728
3729         // draw any stuff wanted for the last row of a paragraph
3730         if (!row->next() || (row->next()->par() != row->par())) {
3731                 paintLastRow(p);
3732         }
3733
3734         // paint text
3735         paintRowText(p);
3736 }
3737
3738
3739 int LyXText::defaultHeight() const
3740 {
3741         LyXFont font(LyXFont::ALL_SANE);
3742         return int(font_metrics::maxAscent(font)
3743                  + font_metrics::maxDescent(font) * 1.5);
3744 }
3745
3746
3747 // returns the column near the specified x-coordinate of the row
3748 // x is set to the real beginning of this column
3749 pos_type
3750 LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3751                         bool & boundary) const
3752 {
3753         float tmpx = 0.0;
3754         float fill_separator;
3755         float fill_hfill;
3756         float fill_label_hfill;
3757
3758         prepareToPrint(bview, row, tmpx, fill_separator,
3759                        fill_hfill, fill_label_hfill);
3760
3761         pos_type vc = row->pos();
3762         pos_type last = rowLastPrintable(row);
3763         pos_type c = 0;
3764
3765         LyXLayout_ptr const & layout = row->par()->layout();
3766
3767         bool left_side = false;
3768
3769         pos_type main_body = beginningOfMainBody(bview->buffer(), row->par());
3770         float last_tmpx = tmpx;
3771
3772         if (main_body > 0 &&
3773             (main_body - 1 > last ||
3774              !row->par()->isLineSeparator(main_body - 1)))
3775                 main_body = 0;
3776
3777         while (vc <= last && tmpx <= x) {
3778                 c = vis2log(vc);
3779                 last_tmpx = tmpx;
3780                 if (main_body > 0 && c == main_body-1) {
3781                         tmpx += fill_label_hfill +
3782                                 font_metrics::width(layout->labelsep,
3783                                                getLabelFont(bview->buffer(), row->par()));
3784                         if (row->par()->isLineSeparator(main_body - 1))
3785                                 tmpx -= singleWidth(bview, row->par(), main_body-1);
3786                 }
3787
3788                 if (hfillExpansion(bview->buffer(), row, c)) {
3789                         tmpx += singleWidth(bview, row->par(), c);
3790                         if (c >= main_body)
3791                                 tmpx += fill_hfill;
3792                         else
3793                                 tmpx += fill_label_hfill;
3794                 }
3795                 else if (row->par()->isSeparator(c)) {
3796                         tmpx += singleWidth(bview, row->par(), c);
3797                         if (c >= main_body)
3798                                 tmpx+= fill_separator;
3799                 } else
3800                         tmpx += singleWidth(bview, row->par(), c);
3801                 ++vc;
3802         }
3803
3804         if ((tmpx + last_tmpx) / 2 > x) {
3805                 tmpx = last_tmpx;
3806                 left_side = true;
3807         }
3808
3809         if (vc > last + 1)  // This shouldn't happen.
3810                 vc = last + 1;
3811
3812         boundary = false;
3813         bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3814                                          // some speedup if rtl_support=false
3815                 && (!row->next() || row->next()->par() != row->par());
3816         bool const rtl = (lastrow)
3817                 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3818                 : false; // If lastrow is false, we don't need to compute
3819                          // the value of rtl.
3820
3821         if (row->pos() > last)  // Row is empty?
3822                 c = row->pos();
3823         else if (lastrow &&
3824                  ((rtl &&  left_side && vc == row->pos() && x < tmpx - 5) ||
3825                    (!rtl && !left_side && vc == last + 1   && x > tmpx + 5)))
3826                 c = last + 1;
3827         else if (vc == row->pos()) {
3828                 c = vis2log(vc);
3829                 if (bidi_level(c) % 2 == 1)
3830                         ++c;
3831         } else {
3832                 c = vis2log(vc - 1);
3833                 bool const rtl = (bidi_level(c) % 2 == 1);
3834                 if (left_side == rtl) {
3835                         ++c;
3836                         boundary = isBoundary(bview->buffer(), row->par(), c);
3837                 }
3838         }
3839
3840         if (row->pos() <= last && c > last
3841             && row->par()->isNewline(last)) {
3842                 if (bidi_level(last) % 2 == 0)
3843                         tmpx -= singleWidth(bview, row->par(), last);
3844                 else
3845                         tmpx += singleWidth(bview, row->par(), last);
3846                 c = last;
3847         }
3848
3849         c -= row->pos();
3850         x = int(tmpx);
3851         return c;
3852 }
3853
3854
3855 // returns pointer to a specified row
3856 Row * LyXText::getRow(Paragraph * par, pos_type pos, int & y) const
3857 {
3858         if (!firstrow)
3859                 return 0;
3860
3861         Row * tmprow = firstrow;
3862         y = 0;
3863
3864         // find the first row of the specified paragraph
3865         while (tmprow->next() && tmprow->par() != par) {
3866                 y += tmprow->height();
3867                 tmprow = tmprow->next();
3868         }
3869
3870         // now find the wanted row
3871         while (tmprow->pos() < pos
3872                && tmprow->next()
3873                && tmprow->next()->par() == par
3874                && tmprow->next()->pos() <= pos) {
3875                 y += tmprow->height();
3876                 tmprow = tmprow->next();
3877         }
3878
3879         return tmprow;
3880 }
3881
3882
3883 Row * LyXText::getRowNearY(int & y) const
3884 {
3885 #if 1
3886         // If possible we should optimize this method. (Lgb)
3887         Row * tmprow = firstrow;
3888         int tmpy = 0;
3889
3890         while (tmprow->next() && tmpy + tmprow->height() <= y) {
3891                 tmpy += tmprow->height();
3892                 tmprow = tmprow->next();
3893         }
3894
3895         y = tmpy;   // return the real y
3896
3897         //lyxerr << "returned y = " << y << endl;
3898
3899         return tmprow;
3900 #else
3901         // Search from the current cursor position.
3902
3903         Row * tmprow = cursor.row();
3904         int tmpy = cursor.y() - tmprow->baseline();
3905
3906         lyxerr << "cursor.y() = " << tmpy << endl;
3907         lyxerr << "tmprow->height() = " << tmprow->height() << endl;
3908         lyxerr << "tmprow->baseline() = " << tmprow->baseline() << endl;
3909         lyxerr << "first = " << first << endl;
3910         lyxerr << "y = " << y << endl;
3911
3912         if (y < tmpy) {
3913                 lyxerr << "up" << endl;
3914                 do {
3915                         tmpy -= tmprow->height();
3916                         tmprow = tmprow->previous();
3917                 } while (tmprow && tmpy - tmprow->height() >= y);
3918         } else if (y > tmpy) {
3919                 lyxerr << "down" << endl;
3920
3921                 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3922                         tmpy += tmprow->height();
3923                         tmprow = tmprow->next();
3924                 }
3925         } else {
3926                 lyxerr << "equal" << endl;
3927         }
3928
3929         y = tmpy; // return the real y
3930
3931         lyxerr << "returned y = " << y << endl;
3932
3933         return tmprow;
3934
3935 #endif
3936 }
3937
3938
3939 int LyXText::getDepth() const
3940 {
3941         return cursor.par()->getDepth();
3942 }