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