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