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