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