]> git.lyx.org Git - lyx.git/blob - src/text.C
another fix
[lyx.git] / src / text.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #include "lyxtext.h"
14 #include "lyxrow.h"
15 #include "paragraph.h"
16 #include "gettext.h"
17 #include "bufferparams.h"
18 #include "buffer.h"
19 #include "debug.h"
20 #include "intl.h"
21 #include "lyxrc.h"
22 #include "encoding.h"
23 #include "frontends/LyXView.h"
24 #include "frontends/Painter.h"
25 #include "frontends/font_metrics.h"
26 #include "frontends/screen.h"
27 #include "frontends/WorkArea.h"
28 #include "bufferview_funcs.h"
29 #include "BufferView.h"
30 #include "language.h"
31 #include "ParagraphParameters.h"
32 #include "undo_funcs.h"
33 #include "WordLangTuple.h"
34 #include "paragraph_funcs.h"
35
36 #include "insets/insetbib.h"
37 #include "insets/insettext.h"
38
39 #include "support/textutils.h"
40 #include "support/LAssert.h"
41 #include "support/lstrings.h"
42
43 #include <algorithm>
44
45 using std::max;
46 using std::min;
47 using std::endl;
48 using std::pair;
49 using lyx::pos_type;
50
51 namespace {
52
53 int const LYX_PAPER_MARGIN = 20;
54
55 } // namespace anon
56
57 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
58
59
60 int LyXText::workWidth(BufferView * bview) const
61 {
62         if (inset_owner) {
63                 return inset_owner->textWidth(bview);
64         }
65         return bview->workWidth();
66 }
67
68
69 int LyXText::workWidth(BufferView * bview, Inset * inset) const
70 {
71         Paragraph * par = 0;
72         pos_type pos = -1;
73
74         par = inset->parOwner();
75         if (par)
76                 pos = par->getPositionOfInset(inset);
77
78         if (!par || pos == -1) {
79                 lyxerr << "LyXText::workWidth: something is wrong,"
80                         " fall back to the brute force method" << endl;
81                 Buffer::inset_iterator it = bview->buffer()->inset_iterator_begin();
82                 Buffer::inset_iterator end = bview->buffer()->inset_iterator_end();
83                 for (; it != end; ++it) {
84                         if (&(*it) == inset) {
85                                 par = it.getPar();
86                                 pos = it.getPos();
87                                 break;
88                         }
89                 }
90         }
91
92         if (!par) {
93                 return workWidth(bview);
94         }
95
96         LyXLayout_ptr const & layout = par->layout();
97
98         if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
99                 // Optimization here: in most cases, the real row is
100                 // not needed, but only the par/pos values. So we just
101                 // construct a dummy row for leftMargin. (JMarc)
102                 Row dummyrow;
103                 dummyrow.par(par);
104                 dummyrow.pos(pos);
105                 return workWidth(bview) - leftMargin(bview, &dummyrow);
106         } else {
107                 int dummy_y;
108                 Row * row = getRow(par, pos, dummy_y);
109                 Row * frow = row;
110                 while (frow->previous() && frow->par() == frow->previous()->par())
111                         frow = frow->previous();
112                 unsigned int maxw = 0;
113                 while (frow->next() && frow->par() == frow->next()->par()) {
114                         if ((frow != row) && (maxw < frow->width()))
115                                 maxw = frow->width();
116                         frow = frow->next();
117                 }
118                 if (maxw)
119                         return maxw;
120         }
121         return workWidth(bview);
122 }
123
124
125 int LyXText::getRealCursorX(BufferView * bview) const
126 {
127         int x = cursor.x();
128         if (the_locking_inset && (the_locking_inset->getLyXText(bview)!=this))
129                 x = the_locking_inset->getLyXText(bview)->getRealCursorX(bview);
130         return x;
131 }
132
133
134 unsigned char LyXText::transformChar(unsigned char c, Paragraph * par,
135                         pos_type pos) const
136 {
137         if (!Encodings::is_arabic(c))
138                 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
139                         return c + (0xb0 - '0');
140                 else
141                         return c;
142
143         unsigned char const prev_char = pos > 0 ? par->getChar(pos-1) : ' ';
144         unsigned char next_char = ' ';
145
146         for (pos_type i = pos+1; i < par->size(); ++i)
147                 if (!Encodings::IsComposeChar_arabic(par->getChar(i))) {
148                         next_char = par->getChar(i);
149                         break;
150                 }
151
152         if (Encodings::is_arabic(next_char)) {
153                 if (Encodings::is_arabic(prev_char))
154                         return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
155                 else
156                         return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
157         } else {
158                 if (Encodings::is_arabic(prev_char))
159                         return Encodings::TransformChar(c, Encodings::FORM_FINAL);
160                 else
161                         return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
162         }
163 }
164
165 // This is the comments that some of the warnings below refers to.
166 // There are some issues in this file and I don't think they are
167 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
168 // this is a problem that has been here almost from day one and that a
169 // larger userbase with differenct access patters triggers the bad
170 // behaviour. (segfaults.) What I think happen is: In several places
171 // we store the paragraph in the current cursor and then moves the
172 // cursor. This movement of the cursor will delete paragraph at the
173 // old position if it is now empty. This will make the temporary
174 // pointer to the old cursor paragraph invalid and dangerous to use.
175 // And is some cases this will trigger a segfault. I have marked some
176 // of the cases where this happens with a warning, but I am sure there
177 // are others in this file and in text2.C. There is also a note in
178 // Delete() that you should read. In Delete I store the paragraph->id
179 // instead of a pointer to the paragraph. I am pretty sure this faulty
180 // use of temporary pointers to paragraphs that might have gotten
181 // invalidated (through a cursor movement) before they are used, are
182 // the cause of the strange crashes we get reported often.
183 //
184 // It is very tiresom to change this code, especially when it is as
185 // hard to read as it is. Help to fix all the cases where this is done
186 // would be greately appreciated.
187 //
188 // Lgb
189
190 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
191                          pos_type pos) const
192 {
193         char const c = par->getChar(pos);
194         return singleWidth(bview, par, pos, c);
195 }
196
197
198 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
199                          pos_type pos, char c) const
200 {
201         LyXFont const font = getFont(bview->buffer(), par, pos);
202
203         // The most common case is handled first (Asger)
204         if (IsPrintable(c)) {
205                 if (font.language()->RightToLeft()) {
206                         if (font.language()->lang() == "arabic" &&
207                             (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
208                              lyxrc.font_norm_type == LyXRC::ISO_10646_1))
209                                 if (Encodings::IsComposeChar_arabic(c))
210                                         return 0;
211                                 else
212                                         c = transformChar(c, par, pos);
213                         else if (font.language()->lang() == "hebrew" &&
214                                  Encodings::IsComposeChar_hebrew(c))
215                                 return 0;
216                 }
217                 return font_metrics::width(c, font);
218
219         } else if (IsHfillChar(c)) {
220                 // Because of the representation as vertical lines
221                 return 3;
222         } else if (c == Paragraph::META_INSET) {
223                 Inset * tmpinset = par->getInset(pos);
224                 if (tmpinset) {
225 #if 1
226                         // this IS needed otherwise on initialitation we don't get the fill
227                         // of the row right (ONLY on initialization if we read a file!)
228                         // should be changed! (Jug 20011204)
229                         tmpinset->update(bview, font);
230 #endif
231                         return tmpinset->width(bview, font);
232                 } else
233                         return 0;
234
235         } else if (IsSeparatorChar(c))
236                 c = ' ';
237         else if (IsNewlineChar(c))
238                 c = 'n';
239         return font_metrics::width(c, font);
240 }
241
242
243 // Returns the paragraph position of the last character in the specified row
244 pos_type LyXText::rowLast(Row const * row) const
245 {
246         if (!row->next() || row->next()->par() != row->par()) {
247                 return row->par()->size() - 1;
248         } else {
249                 return row->next()->pos() - 1;
250         }
251 }
252
253
254 pos_type LyXText::rowLastPrintable(Row const * row) const
255 {
256         pos_type const last = rowLast(row);
257         bool ignore_the_space_on_the_last_position = true;
258         Inset * ins;
259         // we have to consider a space on the last position in this case!
260         if (row->next() && row->par() == row->next()->par() &&
261             row->next()->par()->getChar(last+1) == Paragraph::META_INSET &&
262             (ins=row->next()->par()->getInset(last+1)) &&
263             (ins->needFullRow() || ins->display()))
264         {
265                 ignore_the_space_on_the_last_position = false;
266         }
267         if (last >= row->pos()
268             && row->next()
269             && row->next()->par() == row->par()
270             && row->par()->isSeparator(last)
271                 && ignore_the_space_on_the_last_position)
272                 return last - 1;
273         else
274                 return last;
275 }
276
277
278 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
279 {
280         bidi_same_direction = true;
281         if (!lyxrc.rtl_support) {
282                 bidi_start = -1;
283                 return;
284         }
285
286         Inset * inset = row->par()->inInset();
287         if (inset && inset->owner() &&
288             inset->owner()->lyxCode() == Inset::ERT_CODE) {
289                 bidi_start = -1;
290                 return;
291         }
292
293         bidi_start = row->pos();
294         bidi_end = rowLastPrintable(row);
295
296         if (bidi_start > bidi_end) {
297                 bidi_start = -1;
298                 return;
299         }
300
301         if (bidi_end + 2 - bidi_start >
302             static_cast<pos_type>(log2vis_list.size())) {
303                 pos_type new_size =
304                         (bidi_end + 2 - bidi_start < 500) ?
305                         500 : 2 * (bidi_end + 2 - bidi_start);
306                 log2vis_list.resize(new_size);
307                 vis2log_list.resize(new_size);
308                 bidi_levels.resize(new_size);
309         }
310
311         vis2log_list[bidi_end + 1 - bidi_start] = -1;
312         log2vis_list[bidi_end + 1 - bidi_start] = -1;
313
314         pos_type stack[2];
315         bool const rtl_par =
316                 row->par()->isRightToLeftPar(buf->params);
317         int level = 0;
318         bool rtl = false;
319         bool rtl0 = false;
320         pos_type const main_body = beginningOfMainBody(buf, row->par());
321
322         for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
323                 bool is_space = row->par()->isLineSeparator(lpos);
324                 pos_type const pos =
325                         (is_space && lpos + 1 <= bidi_end &&
326                          !row->par()->isLineSeparator(lpos + 1) &&
327                          !row->par()->isNewline(lpos + 1))
328                         ? lpos + 1 : lpos;
329                 LyXFont font = row->par()->getFontSettings(buf->params, pos);
330                 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
331                     font.number() == LyXFont::ON &&
332                     row->par()->getFontSettings(buf->params, lpos - 1).number()
333                     == LyXFont::ON) {
334                         font = row->par()->getFontSettings(buf->params, lpos);
335                         is_space = false;
336                 }
337
338
339                 bool new_rtl = font.isVisibleRightToLeft();
340                 bool new_rtl0 = font.isRightToLeft();
341                 int new_level;
342
343                 if (lpos == main_body - 1
344                     && row->pos() < main_body - 1
345                     && is_space) {
346                         new_level = (rtl_par) ? 1 : 0;
347                         new_rtl = new_rtl0 = rtl_par;
348                 } else if (new_rtl0)
349                         new_level = (new_rtl) ? 1 : 2;
350                 else
351                         new_level = (rtl_par) ? 2 : 0;
352
353                 if (is_space && new_level >= level) {
354                         new_level = level;
355                         new_rtl = rtl;
356                         new_rtl0 = rtl0;
357                 }
358
359                 int new_level2 = new_level;
360
361                 if (level == new_level && rtl0 != new_rtl0) {
362                         --new_level2;
363                         log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
364                 } else if (level < new_level) {
365                         log2vis_list[lpos - bidi_start] =  (rtl) ? -1 : 1;
366                         if (new_level > rtl_par)
367                                 bidi_same_direction = false;
368                 } else
369                         log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
370                 rtl = new_rtl;
371                 rtl0 = new_rtl0;
372                 bidi_levels[lpos - bidi_start] = new_level;
373
374                 while (level > new_level2) {
375                         pos_type old_lpos = stack[--level];
376                         int delta = lpos - old_lpos - 1;
377                         if (level % 2)
378                                 delta = -delta;
379                         log2vis_list[lpos - bidi_start] += delta;
380                         log2vis_list[old_lpos - bidi_start] += delta;
381                 }
382                 while (level < new_level)
383                         stack[level++] = lpos;
384         }
385
386         while (level > 0) {
387                 pos_type const old_lpos = stack[--level];
388                 int delta = bidi_end - old_lpos;
389                 if (level % 2)
390                         delta = -delta;
391                 log2vis_list[old_lpos - bidi_start] += delta;
392         }
393
394         pos_type vpos = bidi_start - 1;
395         for (pos_type lpos = bidi_start;
396              lpos <= bidi_end; ++lpos) {
397                 vpos += log2vis_list[lpos - bidi_start];
398                 vis2log_list[vpos - bidi_start] = lpos;
399                 log2vis_list[lpos - bidi_start] = vpos;
400         }
401 }
402
403
404 // This method requires a previous call to ComputeBidiTables()
405 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
406                          pos_type pos) const
407 {
408         if (!lyxrc.rtl_support || pos == 0)
409                 return false;
410
411         if (!bidi_InRange(pos - 1)) {
412                 /// This can happen if pos is the first char of a row.
413                 /// Returning false in this case is incorrect!
414                 return false;
415         }
416
417         bool const rtl = bidi_level(pos - 1) % 2;
418         bool const rtl2 = bidi_InRange(pos)
419                 ? bidi_level(pos) % 2
420                 : par->isRightToLeftPar(buf->params);
421         return rtl != rtl2;
422 }
423
424
425 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
426                          pos_type pos, LyXFont const & font) const
427 {
428         if (!lyxrc.rtl_support)
429                 return false;    // This is just for speedup
430
431         bool const rtl = font.isVisibleRightToLeft();
432         bool const rtl2 = bidi_InRange(pos)
433                 ? bidi_level(pos) % 2
434                 : par->isRightToLeftPar(buf->params);
435         return rtl != rtl2;
436 }
437
438
439 void LyXText::drawNewline(DrawRowParams & p, pos_type const pos)
440 {
441         // Draw end-of-line marker
442         LyXFont const font = getFont(p.bv->buffer(), p.row->par(), pos);
443         int const wid = font_metrics::width('n', font);
444         int const asc = font_metrics::maxAscent(font);
445         int const y = p.yo + p.row->baseline();
446         int xp[3];
447         int yp[3];
448
449         yp[0] = int(y - 0.875 * asc * 0.75);
450         yp[1] = int(y - 0.500 * asc * 0.75);
451         yp[2] = int(y - 0.125 * asc * 0.75);
452
453         if (bidi_level(pos) % 2 == 0) {
454                 xp[0] = int(p.x + wid * 0.375);
455                 xp[1] = int(p.x);
456                 xp[2] = int(p.x + wid * 0.375);
457         } else {
458                 xp[0] = int(p.x + wid * 0.625);
459                 xp[1] = int(p.x + wid);
460                 xp[2] = int(p.x + wid * 0.625);
461         }
462
463         p.pain->lines(xp, yp, 3, LColor::eolmarker);
464
465         yp[0] = int(y - 0.500 * asc * 0.75);
466         yp[1] = int(y - 0.500 * asc * 0.75);
467         yp[2] = int(y - asc * 0.75);
468
469         if (bidi_level(pos) % 2 == 0) {
470                 xp[0] = int(p.x);
471                 xp[1] = int(p.x + wid);
472                 xp[2] = int(p.x + wid);
473         } else {
474                 xp[0] = int(p.x + wid);
475                 xp[1] = int(p.x);
476                 xp[2] = int(p.x);
477         }
478
479         p.pain->lines(xp, yp, 3, LColor::eolmarker);
480
481         p.x += wid;
482 }
483
484
485 bool LyXText::drawInset(DrawRowParams & p, pos_type const pos)
486 {
487         Inset * inset = p.row->par()->getInset(pos);
488
489         // FIXME: shouldn't happen
490         if (!inset) {
491                 return true;
492         }
493
494         LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
495         // we need this here as the row pointer may be illegal
496         // at a later time (Jug20020502)
497         Row * prev = p.row->previous();
498
499         inset->update(p.bv, font, false);
500         inset->draw(p.bv, font, p.yo + p.row->baseline(), p.x, p.cleared);
501
502         if (!need_break_row && !inset_owner
503             && p.bv->text->status() == CHANGED_IN_DRAW) {
504                 if (prev && prev->par() == p.row->par()) {
505                         breakAgainOneRow(p.bv, prev);
506                         if (prev->next() != p.row) {
507                                 // breakAgainOneRow() has removed p.row
508                                 p.row = 0;  // see what this breaks
509                                 need_break_row = prev;
510                         } else {
511                                 need_break_row = p.row;
512                         }
513                 } else if (!prev) {
514                         need_break_row = firstrow;
515                 } else {
516                         need_break_row = prev->next();
517                 }
518                 setCursor(p.bv, cursor.par(), cursor.pos());
519                 return false;
520         }
521         return true;
522 }
523
524
525 void LyXText::drawForeignMark(DrawRowParams & p, float const orig_x, LyXFont const & orig_font)
526 {
527         if (!lyxrc.mark_foreign_language)
528                 return;
529         if (orig_font.language() == latex_language)
530                 return;
531         if (orig_font.language() == p.bv->buffer()->params.language)
532                 return;
533
534         int const y = p.yo + p.row->height() - 1;
535         p.pain->line(int(orig_x), y, int(p.x), y, LColor::language);
536 }
537
538
539 void LyXText::drawHebrewComposeChar(DrawRowParams & p, pos_type & vpos)
540 {
541         pos_type pos = vis2log(vpos);
542
543         string str;
544
545         // first char
546         char c = p.row->par()->getChar(pos);
547         str += c;
548         ++vpos;
549
550         LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
551         int const width = font_metrics::width(c, font);
552         int dx = 0;
553
554         for (pos_type i = pos-1; i >= 0; --i) {
555                 c = p.row->par()->getChar(i);
556                 if (!Encodings::IsComposeChar_hebrew(c)) {
557                         if (IsPrintableNonspace(c)) {
558                                 int const width2 =
559                                         singleWidth(p.bv, p.row->par(), i, c);
560                                 // dalet / resh
561                                 dx = (c == 'ø' || c == 'ã')
562                                         ? width2 - width
563                                         : (width2 - width) / 2;
564                         }
565                         break;
566                 }
567         }
568
569         // Draw nikud
570         p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
571 }
572
573
574 void LyXText::drawArabicComposeChar(DrawRowParams & p, pos_type & vpos)
575 {
576         pos_type pos = vis2log(vpos);
577         string str;
578
579         // first char
580         char c = p.row->par()->getChar(pos);
581         c = transformChar(c, p.row->par(), pos);
582         str +=c;
583         ++vpos;
584
585         LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
586         int const width = font_metrics::width(c, font);
587         int dx = 0;
588
589         for (pos_type i = pos-1; i >= 0; --i) {
590                 c = p.row->par()->getChar(i);
591                 if (!Encodings::IsComposeChar_arabic(c)) {
592                         if (IsPrintableNonspace(c)) {
593                                 int const width2 =
594                                         singleWidth(p.bv, p.row->par(), i, c);
595                                 dx = (width2 - width) / 2;
596                         }
597                         break;
598                 }
599         }
600         // Draw nikud
601         p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
602 }
603
604
605 void LyXText::drawChars(DrawRowParams & p, pos_type & vpos,
606                         bool hebrew, bool arabic)
607 {
608         pos_type pos = vis2log(vpos);
609         pos_type const last = rowLastPrintable(p.row);
610         LyXFont const & orig_font = getFont(p.bv->buffer(), p.row->par(), pos);
611
612         // first character
613         string str;
614         str += p.row->par()->getChar(pos);
615         if (arabic) {
616                 unsigned char c = str[0];
617                 str[0] = transformChar(c, p.row->par(), pos);
618         }
619         ++vpos;
620
621         // collect as much similar chars as we can
622         while (vpos <= last && (pos = vis2log(vpos)) >= 0) {
623                 char c = p.row->par()->getChar(pos);
624
625                 if (!IsPrintableNonspace(c))
626                         break;
627
628                 if (arabic && Encodings::IsComposeChar_arabic(c))
629                         break;
630                 if (hebrew && Encodings::IsComposeChar_hebrew(c))
631                         break;
632
633                 if (orig_font != getFont(p.bv->buffer(), p.row->par(), pos))
634                         break;
635
636                 if (arabic)
637                         c = transformChar(c, p.row->par(), pos);
638                 str += c;
639                 ++vpos;
640         }
641
642         // Draw text and set the new x position
643         p.pain->text(int(p.x), p.yo + p.row->baseline(), str, orig_font);
644         p.x += font_metrics::width(str, orig_font);
645 }
646
647
648 bool LyXText::draw(DrawRowParams & p, pos_type & vpos)
649 {
650         pos_type const pos = vis2log(vpos);
651         Paragraph * par = p.row->par();
652
653         LyXFont const & orig_font = getFont(p.bv->buffer(), par, pos);
654
655         float const orig_x = p.x;
656
657         char const c = par->getChar(pos);
658
659         if (IsNewlineChar(c)) {
660                 ++vpos;
661                 drawNewline(p, pos);
662                 return true;
663         } else if (IsInsetChar(c)) {
664                 if (!drawInset(p, pos))
665                         return false;
666                 ++vpos;
667                 drawForeignMark(p, orig_x, orig_font);
668                 return true;
669         }
670
671         // usual characters, no insets
672
673         // special case languages
674         bool const hebrew = (orig_font.language()->lang() == "hebrew");
675         bool const arabic =
676                 orig_font.language()->lang() == "arabic" &&
677                 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
678                 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
679
680         // draw as many chars as we can
681         if ((!hebrew && !arabic)
682                 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
683                 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
684                 drawChars(p, vpos, hebrew, arabic);
685         } else if (hebrew) {
686                 drawHebrewComposeChar(p, vpos);
687         } else if (arabic) {
688                 drawArabicComposeChar(p, vpos);
689         }
690
691         drawForeignMark(p, orig_x, orig_font);
692
693         return true;
694 }
695
696
697 int LyXText::leftMargin(BufferView * bview, Row const * row) const
698 {
699         Inset * ins;
700         if ((row->par()->getChar(row->pos()) == Paragraph::META_INSET) &&
701                 (ins=row->par()->getInset(row->pos())) &&
702                 (ins->needFullRow() || ins->display()))
703                 return LYX_PAPER_MARGIN;
704
705         LyXTextClass const & tclass =
706                 bview->buffer()->params.getLyXTextClass();
707         LyXLayout_ptr const & layout = row->par()->layout();
708
709         string parindent = layout->parindent;
710
711         int x = LYX_PAPER_MARGIN;
712
713         x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
714
715         // this is the way, LyX handles the LaTeX-Environments.
716         // I have had this idea very late, so it seems to be a
717         // later added hack and this is true
718         if (!row->par()->getDepth()) {
719                 if (row->par()->layout() == tclass.defaultLayout()) {
720                         // find the previous same level paragraph
721                         if (row->par()->previous()) {
722                                 Paragraph * newpar = row->par()
723                                         ->depthHook(row->par()->getDepth());
724                                 if (newpar &&
725                                     newpar->layout()->nextnoindent)
726                                         parindent.erase();
727                         }
728                 }
729         } else {
730                 // find the next level paragraph
731
732                 Paragraph * newpar = row->par()->outerHook();
733
734                 // make a corresponding row. Needed to call LeftMargin()
735
736                 // check wether it is a sufficent paragraph
737                 if (newpar && newpar->layout()->isEnvironment()) {
738                         Row dummyrow;
739                         dummyrow.par(newpar);
740                         dummyrow.pos(newpar->size());
741                         x = leftMargin(bview, &dummyrow);
742                 } else {
743                         // this is no longer an error, because this function
744                         // is used to clear impossible depths after changing
745                         // a layout. Since there is always a redo,
746                         // LeftMargin() is always called
747                         row->par()->params().depth(0);
748                 }
749
750                 if (newpar && row->par()->layout() == tclass.defaultLayout()) {
751                         if (newpar->params().noindent())
752                                 parindent.erase();
753                         else {
754                                 parindent = newpar->layout()->parindent;
755                         }
756
757                 }
758         }
759
760         LyXFont const labelfont = getLabelFont(bview->buffer(), row->par());
761         switch (layout->margintype) {
762         case MARGIN_DYNAMIC:
763                 if (!layout->leftmargin.empty()) {
764                         x += font_metrics::signedWidth(layout->leftmargin,
765                                                   tclass.defaultfont());
766                 }
767                 if (!row->par()->getLabelstring().empty()) {
768                         x += font_metrics::signedWidth(layout->labelindent,
769                                                   labelfont);
770                         x += font_metrics::width(row->par()->getLabelstring(),
771                                             labelfont);
772                         x += font_metrics::width(layout->labelsep, labelfont);
773                 }
774                 break;
775         case MARGIN_MANUAL:
776                 x += font_metrics::signedWidth(layout->labelindent, labelfont);
777                 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
778                         if (!row->par()->getLabelWidthString().empty()) {
779                                 x += font_metrics::width(row->par()->getLabelWidthString(),
780                                                labelfont);
781                                 x += font_metrics::width(layout->labelsep, labelfont);
782                         }
783                 }
784                 break;
785         case MARGIN_STATIC:
786                 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
787                         / (row->par()->getDepth() + 4);
788                 break;
789         case MARGIN_FIRST_DYNAMIC:
790                 if (layout->labeltype == LABEL_MANUAL) {
791                         if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
792                                 x += font_metrics::signedWidth(layout->leftmargin,
793                                                           labelfont);
794                         } else {
795                                 x += font_metrics::signedWidth(layout->labelindent,
796                                                           labelfont);
797                         }
798                 } else if (row->pos()
799                            // Special case to fix problems with
800                            // theorems (JMarc)
801                            || (layout->labeltype == LABEL_STATIC
802                                && layout->latextype == LATEX_ENVIRONMENT
803                                && ! row->par()->isFirstInSequence())) {
804                         x += font_metrics::signedWidth(layout->leftmargin,
805                                                   labelfont);
806                 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
807                            && layout->labeltype != LABEL_BIBLIO
808                            && layout->labeltype !=
809                            LABEL_CENTERED_TOP_ENVIRONMENT) {
810                         x += font_metrics::signedWidth(layout->labelindent,
811                                                   labelfont);
812                         x += font_metrics::width(layout->labelsep, labelfont);
813                         x += font_metrics::width(row->par()->getLabelstring(),
814                                             labelfont);
815                 }
816                 break;
817
818         case MARGIN_RIGHT_ADDRESS_BOX:
819         {
820                 // ok, a terrible hack. The left margin depends on the widest
821                 // row in this paragraph. Do not care about footnotes, they
822                 // are *NOT* allowed in the LaTeX realisation of this layout.
823
824                 // find the first row of this paragraph
825                 Row const * tmprow = row;
826                 while (tmprow->previous()
827                        && tmprow->previous()->par() == row->par())
828                         tmprow = tmprow->previous();
829
830                 int minfill = tmprow->fill();
831                 while (tmprow->next() && tmprow->next()->par() == row->par()) {
832                         tmprow = tmprow->next();
833                         if (tmprow->fill() < minfill)
834                                 minfill = tmprow->fill();
835                 }
836
837                 x += font_metrics::signedWidth(layout->leftmargin,
838                         tclass.defaultfont());
839                 x += minfill;
840         }
841         break;
842         }
843
844         if ((workWidth(bview) > 0) &&
845                 !row->par()->params().leftIndent().zero())
846         {
847                 LyXLength const len = row->par()->params().leftIndent();
848                 int const tw = inset_owner ?
849                         inset_owner->latexTextWidth(bview) : workWidth(bview);
850                 x += len.inPixels(tw, bview->text->defaultHeight());
851         }
852
853         LyXAlignment align; // wrong type
854
855         if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
856                 align = layout->align;
857         else
858                 align = row->par()->params().align();
859
860         // set the correct parindent
861         if (row->pos() == 0) {
862                 if ((layout->labeltype == LABEL_NO_LABEL
863                      || layout->labeltype == LABEL_TOP_ENVIRONMENT
864                      || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
865                      || (layout->labeltype == LABEL_STATIC
866                          && layout->latextype == LATEX_ENVIRONMENT
867                          && ! row->par()->isFirstInSequence()))
868                     && align == LYX_ALIGN_BLOCK
869                     && !row->par()->params().noindent()
870                         // in tabulars and ert paragraphs are never indented!
871                         && (!row->par()->inInset() || !row->par()->inInset()->owner() ||
872                                 (row->par()->inInset()->owner()->lyxCode() != Inset::TABULAR_CODE &&
873                                  row->par()->inInset()->owner()->lyxCode() != Inset::ERT_CODE))
874                     && (row->par()->layout() != tclass.defaultLayout() ||
875                         bview->buffer()->params.paragraph_separation ==
876                         BufferParams::PARSEP_INDENT)) {
877                         x += font_metrics::signedWidth(parindent,
878                                                   tclass.defaultfont());
879                 } else if (layout->labeltype == LABEL_BIBLIO) {
880                         // ale970405 Right width for bibitems
881                         x += bibitemMaxWidth(bview, tclass.defaultfont());
882                 }
883         }
884
885         return x;
886 }
887
888
889 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
890 {
891         Inset * ins;
892         if ((row->par()->getChar(row->pos()) == Paragraph::META_INSET) &&
893                 (ins=row->par()->getInset(row->pos())) &&
894                 (ins->needFullRow() || ins->display()))
895                 return LYX_PAPER_MARGIN;
896
897         LyXTextClass const & tclass = buf->params.getLyXTextClass();
898         LyXLayout_ptr const & layout = row->par()->layout();
899
900         int x = LYX_PAPER_MARGIN
901                 + font_metrics::signedWidth(tclass.rightmargin(),
902                                        tclass.defaultfont());
903
904         // this is the way, LyX handles the LaTeX-Environments.
905         // I have had this idea very late, so it seems to be a
906         // later added hack and this is true
907         if (row->par()->getDepth()) {
908                 // find the next level paragraph
909
910                 Paragraph * newpar = row->par();
911
912                 do {
913                         newpar = newpar->previous();
914                 } while (newpar
915                          && newpar->getDepth() >= row->par()->getDepth());
916
917                 // make a corresponding row. Needed to call LeftMargin()
918
919                 // check wether it is a sufficent paragraph
920                 if (newpar && newpar->layout()->isEnvironment()) {
921                         Row dummyrow;
922                         dummyrow.par(newpar);
923                         dummyrow.pos(0);
924                         x = rightMargin(buf, &dummyrow);
925                 } else {
926                         // this is no longer an error, because this function
927                         // is used to clear impossible depths after changing
928                         // a layout. Since there is always a redo,
929                         // LeftMargin() is always called
930                         row->par()->params().depth(0);
931                 }
932         }
933
934         //lyxerr << "rightmargin: " << layout->rightmargin << endl;
935         x += font_metrics::signedWidth(layout->rightmargin,
936                                        tclass.defaultfont())
937                 * 4 / (row->par()->getDepth() + 4);
938         return x;
939 }
940
941
942 int LyXText::labelEnd(BufferView * bview, Row const * row) const
943 {
944         if (row->par()->layout()->margintype == MARGIN_MANUAL) {
945                 Row tmprow;
946                 tmprow = *row;
947                 tmprow.pos(row->par()->size());
948                 // just the beginning of the main body
949                 return leftMargin(bview, &tmprow);
950         } else {
951                 // LabelEnd is only needed,
952                 // if the layout fills a flushleft label.
953                 return 0;
954         }
955 }
956
957
958 // get the next breakpoint in a given paragraph
959 pos_type
960 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
961 {
962         Paragraph * par = row->par();
963
964         if (width < 0)
965                 return par->size();
966
967         pos_type const pos = row->pos();
968
969         // position of the last possible breakpoint
970         // -1 isn't a suitable value, but a flag
971         pos_type last_separator = -1;
972         width -= rightMargin(bview->buffer(), row);
973
974         pos_type const main_body =
975                 beginningOfMainBody(bview->buffer(), par);
976         LyXLayout_ptr const & layout = par->layout();
977
978         pos_type i = pos;
979
980         if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
981                 // special code for right address boxes, only newlines count
982                 while (i < par->size()) {
983                         if (par->isNewline(i)) {
984                                 last_separator = i;
985                                 i = par->size() - 1; // this means break
986                                 //x = width;
987                         } else if (par->isInset(i) && par->getInset(i)
988                                 && par->getInset(i)->display()) {
989                                 par->getInset(i)->display(false);
990                         }
991                         ++i;
992                 }
993         } else {
994                 // Last position is an invariant
995                 pos_type const last = par->size();
996                 // this is the usual handling
997                 int x = leftMargin(bview, row);
998                 bool doitonetime = true;
999                 while (doitonetime || ((x < width) && (i < last))) {
1000                         doitonetime = false;
1001                         char const c = par->getChar(i);
1002                         Inset * in = 0;
1003                         if (c == Paragraph::META_INSET)
1004                                 in = par->getInset(i);
1005                         if (IsNewlineChar(c)) {
1006                                 last_separator = i;
1007                                 x = width; // this means break
1008                         } else if (in && !in->isChar()) {
1009                                 // check wether a Display() inset is
1010                                 // valid here. if not, change it to
1011                                 // non-display
1012                                 if (in->display() &&
1013                                     (layout->isCommand() ||
1014                                      (layout->labeltype == LABEL_MANUAL
1015                                       && i < beginningOfMainBody(bview->buffer(), par))))
1016                                 {
1017                                         // display istn't allowd
1018                                         in->display(false);
1019                                         x += singleWidth(bview, par, i, c);
1020                                 } else if (in->display() || in->needFullRow()) {
1021                                         // So break the line here
1022                                         if (i == pos) {
1023                                                 if (pos < last-1) {
1024                                                         last_separator = i;
1025                                                         if (par->isLineSeparator(i+1))
1026                                                                 ++last_separator;
1027                                                 } else
1028                                                         last_separator = last; // to avoid extra rows
1029                                         } else
1030                                                 last_separator = i - 1;
1031                                         x = width;  // this means break
1032                                 } else {
1033                                         x += singleWidth(bview, par, i, c);
1034                                         // we have to check this separately as we could have a
1035                                         // lineseparator and then the algorithm below would prefer
1036                                         // that which IS wrong! We should always break on an inset
1037                                         // if it's too long and not on the last separator.
1038                                         // Maybe the only exeption is insets used as chars but
1039                                         // then we would have to have a special function inside
1040                                         // the inset to tell us this. Till then we leave it as
1041                                         // it is now. (Jug 20020106)
1042                                         if (pos < i && x >= width && last_separator >= 0)
1043                                                 last_separator = i - 1;
1044                                 }
1045                         } else  {
1046                                 if (par->isLineSeparator(i))
1047                                         last_separator = i;
1048                                 x += singleWidth(bview, par, i, c);
1049                         }
1050                         ++i;
1051                         if (i == main_body) {
1052                                 x += font_metrics::width(layout->labelsep,
1053                                                     getLabelFont(bview->buffer(), par));
1054                                 if (par->isLineSeparator(i - 1))
1055                                         x-= singleWidth(bview, par, i - 1);
1056                                 int left_margin = labelEnd(bview, row);
1057                                 if (x < left_margin)
1058                                         x = left_margin;
1059                         }
1060                 }
1061                 if ((pos+1 < i) && (last_separator < 0) && (x >= width))
1062                         last_separator = i - 2;
1063                 else if ((pos < i) && (last_separator < 0) && (x >= width))
1064                         last_separator = i - 1;
1065                 // end of paragraph is always a suitable separator
1066                 else if (i == last && x < width)
1067                         last_separator = i;
1068         }
1069
1070         // well, if last_separator is still 0, the line isn't breakable.
1071         // don't care and cut simply at the end
1072         if (last_separator < 0) {
1073                 last_separator = i;
1074         }
1075
1076         // manual labels cannot be broken in LaTeX, do not care
1077         if (main_body && last_separator < main_body)
1078                 last_separator = main_body - 1;
1079
1080         return last_separator;
1081 }
1082
1083
1084 // returns the minimum space a row needs on the screen in pixel
1085 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
1086 {
1087         if (paper_width < 0)
1088                 return 0;
1089
1090         int w;
1091         // get the pure distance
1092         pos_type const last = rowLastPrintable(row);
1093
1094         // special handling of the right address boxes
1095         if (row->par()->layout()->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1096                 int const tmpfill = row->fill();
1097                 row->fill(0); // the minfill in MarginLeft()
1098                 w = leftMargin(bview, row);
1099                 row->fill(tmpfill);
1100         } else
1101                 w = leftMargin(bview, row);
1102
1103         LyXLayout_ptr const & layout = row->par()->layout();
1104
1105         pos_type const main_body =
1106                 beginningOfMainBody(bview->buffer(), row->par());
1107         pos_type i = row->pos();
1108
1109         while (i <= last) {
1110                 if (main_body > 0 && i == main_body) {
1111                         w += font_metrics::width(layout->labelsep, getLabelFont(bview->buffer(), row->par()));
1112                         if (row->par()->isLineSeparator(i - 1))
1113                                 w -= singleWidth(bview, row->par(), i - 1);
1114                         int left_margin = labelEnd(bview, row);
1115                         if (w < left_margin)
1116                                 w = left_margin;
1117                 }
1118                 w += singleWidth(bview, row->par(), i);
1119                 ++i;
1120         }
1121         if (main_body > 0 && main_body > last) {
1122                 w += font_metrics::width(layout->labelsep, getLabelFont(bview->buffer(), row->par()));
1123                 if (last >= 0 && row->par()->isLineSeparator(last))
1124                         w -= singleWidth(bview, row->par(), last);
1125                 int const left_margin = labelEnd(bview, row);
1126                 if (w < left_margin)
1127                         w = left_margin;
1128         }
1129
1130         int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1131         return fill;
1132 }
1133
1134
1135 // returns the minimum space a manual label needs on the screen in pixel
1136 int LyXText::labelFill(BufferView * bview, Row const * row) const
1137 {
1138         pos_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1139         // -1 because a label ends either with a space that is in the label,
1140         // or with the beginning of a footnote that is outside the label.
1141
1142         // I don't understand this code in depth, but sometimes "last" is
1143         // less than 0 and this causes a crash. This fix seems to work
1144         // correctly, but I bet the real error is elsewhere.  The bug is
1145         // triggered when you have an open footnote in a paragraph
1146         // environment with a manual label. (Asger)
1147         if (last < 0) last = 0;
1148
1149         // a separator at this end does not count
1150         if (row->par()->isLineSeparator(last))
1151                 --last;
1152
1153         int w = 0;
1154         pos_type i = row->pos();
1155         while (i <= last) {
1156                 w += singleWidth(bview, row->par(), i);
1157                 ++i;
1158         }
1159
1160         int fill = 0;
1161         if (!row->par()->params().labelWidthString().empty()) {
1162                 fill = max(font_metrics::width(row->par()->params().labelWidthString(),
1163                                           getLabelFont(bview->buffer(), row->par())) - w,
1164                            0);
1165         }
1166
1167         return fill;
1168 }
1169
1170
1171 // returns the number of separators in the specified row. The separator
1172 // on the very last column doesnt count
1173 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1174 {
1175         pos_type last = rowLastPrintable(row);
1176         pos_type p = max(row->pos(), beginningOfMainBody(buf, row->par()));
1177
1178         int n = 0;
1179         for (; p <= last; ++p) {
1180                 if (row->par()->isSeparator(p)) {
1181                         ++n;
1182                 }
1183         }
1184         return n;
1185 }
1186
1187
1188 // returns the number of hfills in the specified row. The LyX-Hfill is
1189 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1190 // ignored. This is *MUCH* more usefull than not to ignore!
1191 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1192 {
1193         pos_type const last = rowLast(row);
1194         pos_type first = row->pos();
1195
1196         if (first) {
1197                 // hfill *DO* count at the beginning of paragraphs!
1198                 while (first <= last && row->par()->isHfill(first)) {
1199                         ++first;
1200                 }
1201         }
1202
1203         first = max(first, beginningOfMainBody(buf, row->par()));
1204         int n = 0;
1205         for (pos_type p = first; p <= last; ++p) {
1206                 // last, because the end is ignored!
1207
1208                 if (row->par()->isHfill(p)) {
1209                         ++n;
1210                 }
1211         }
1212         return n;
1213 }
1214
1215
1216 // like NumberOfHfills, but only those in the manual label!
1217 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1218 {
1219         pos_type last = rowLast(row);
1220         pos_type first = row->pos();
1221         if (first) {
1222                 // hfill *DO* count at the beginning of paragraphs!
1223                 while (first < last && row->par()->isHfill(first))
1224                         ++first;
1225         }
1226
1227         last = min(last, beginningOfMainBody(buf, row->par()));
1228         int n = 0;
1229         for (pos_type p = first; p < last; ++p) {
1230                 // last, because the end is ignored!
1231                 if (row->par()->isHfill(p)) {
1232                         ++n;
1233                 }
1234         }
1235         return n;
1236 }
1237
1238
1239 // returns true, if a expansion is needed.
1240 // Rules are given by LaTeX
1241 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1242                              pos_type pos) const
1243 {
1244         // by the way, is it a hfill?
1245         if (!row_ptr->par()->isHfill(pos))
1246                 return false;
1247
1248         // at the end of a row it does not count
1249         // unless another hfill exists on the line
1250         if (pos >= rowLast(row_ptr)) {
1251                 pos_type i = row_ptr->pos();
1252                 while (i < pos && !row_ptr->par()->isHfill(i)) {
1253                         ++i;
1254                 }
1255                 if (i == pos) {
1256                         return false;
1257                 }
1258         }
1259
1260         // at the beginning of a row it does not count, if it is not
1261         // the first row of a paragaph
1262         if (!row_ptr->pos())
1263                 return true;
1264
1265         // in some labels  it does not count
1266         if (row_ptr->par()->layout()->margintype != MARGIN_MANUAL
1267             && pos < beginningOfMainBody(buf, row_ptr->par()))
1268                 return false;
1269
1270         // if there is anything between the first char of the row and
1271         // the sepcified position that is not a newline and not a hfill,
1272         // the hfill will count, otherwise not
1273         pos_type i = row_ptr->pos();
1274         while (i < pos && (row_ptr->par()->isNewline(i)
1275                            || row_ptr->par()->isHfill(i)))
1276                 ++i;
1277
1278         return i != pos;
1279 }
1280
1281
1282 LColor::color LyXText::backgroundColor()
1283 {
1284         if (inset_owner)
1285                 return inset_owner->backgroundColor();
1286         else
1287                 return LColor::background;
1288 }
1289
1290 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1291 {
1292         // get the maximum ascent and the maximum descent
1293         int asc = 0;
1294         int desc = 0;
1295         float layoutasc = 0;
1296         float layoutdesc = 0;
1297         float tmptop = 0;
1298         LyXFont tmpfont;
1299         Inset * tmpinset = 0;
1300
1301         // ok , let us initialize the maxasc and maxdesc value.
1302         // This depends in LaTeX of the font of the last character
1303         // in the paragraph. The hack below is necessary because
1304         // of the possibility of open footnotes
1305
1306         // Correction: only the fontsize count. The other properties
1307         //  are taken from the layoutfont. Nicer on the screen :)
1308         Paragraph * par = row_ptr->par();
1309         Paragraph * firstpar = row_ptr->par();
1310
1311         LyXLayout_ptr const & layout = firstpar->layout();
1312
1313         // as max get the first character of this row then it can increes but not
1314         // decrees the height. Just some point to start with so we don't have to
1315         // do the assignment below too often.
1316         LyXFont font = getFont(bview->buffer(), par, row_ptr->pos());
1317         LyXFont::FONT_SIZE const tmpsize = font.size();
1318         font = getLayoutFont(bview->buffer(), par);
1319         LyXFont::FONT_SIZE const size = font.size();
1320         font.setSize(tmpsize);
1321
1322         LyXFont labelfont = getLabelFont(bview->buffer(), par);
1323
1324         float spacing_val = 1.0;
1325         if (!row_ptr->par()->params().spacing().isDefault()) {
1326                 spacing_val = row_ptr->par()->params().spacing().getValue();
1327         } else {
1328                 spacing_val = bview->buffer()->params.spacing.getValue();
1329         }
1330         //lyxerr << "spacing_val = " << spacing_val << endl;
1331
1332         int maxasc = int(font_metrics::maxAscent(font) *
1333                          layout->spacing.getValue() *
1334                          spacing_val);
1335         int maxdesc = int(font_metrics::maxDescent(font) *
1336                           layout->spacing.getValue() *
1337                           spacing_val);
1338
1339         pos_type const pos_end = rowLast(row_ptr);
1340         int labeladdon = 0;
1341         int maxwidth = 0;
1342
1343         // Check if any insets are larger
1344         for (pos_type pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1345                 if (row_ptr->par()->isInset(pos)) {
1346                         tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1347                         tmpinset = row_ptr->par()->getInset(pos);
1348                         if (tmpinset) {
1349 #if 1 // this is needed for deep update on initialitation
1350                                 tmpinset->update(bview, tmpfont);
1351 #endif
1352                                 asc = tmpinset->ascent(bview, tmpfont);
1353                                 desc = tmpinset->descent(bview, tmpfont);
1354                                 maxwidth += tmpinset->width(bview, tmpfont);
1355                                 maxasc = max(maxasc, asc);
1356                                 maxdesc = max(maxdesc, desc);
1357                         }
1358                 } else {
1359                         maxwidth += singleWidth(bview, row_ptr->par(), pos);
1360                 }
1361         }
1362
1363         // Check if any custom fonts are larger (Asger)
1364         // This is not completely correct, but we can live with the small,
1365         // cosmetic error for now.
1366         LyXFont::FONT_SIZE maxsize =
1367                 row_ptr->par()->highestFontInRange(row_ptr->pos(), pos_end, size);
1368         if (maxsize > font.size()) {
1369                 font.setSize(maxsize);
1370
1371                 asc = font_metrics::maxAscent(font);
1372                 desc = font_metrics::maxDescent(font);
1373                 if (asc > maxasc)
1374                         maxasc = asc;
1375                 if (desc > maxdesc)
1376                         maxdesc = desc;
1377         }
1378
1379         // This is nicer with box insets:
1380         ++maxasc;
1381         ++maxdesc;
1382
1383         row_ptr->ascent_of_text(maxasc);
1384
1385         // is it a top line?
1386         if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1387
1388                 // some parksips VERY EASY IMPLEMENTATION
1389                 if (bview->buffer()->params.paragraph_separation ==
1390                         BufferParams::PARSEP_SKIP)
1391                 {
1392                         if (layout->isParagraph()
1393                                 && firstpar->getDepth() == 0
1394                                 && firstpar->previous())
1395                         {
1396                                 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1397                         } else if (firstpar->previous() &&
1398                                    firstpar->previous()->layout()->isParagraph() &&
1399                                    firstpar->previous()->getDepth() == 0)
1400                         {
1401                                 // is it right to use defskip here too? (AS)
1402                                 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1403                         }
1404                 }
1405
1406                 // the paper margins
1407                 if (!row_ptr->par()->previous() && bv_owner)
1408                         maxasc += LYX_PAPER_MARGIN;
1409
1410                 // add the vertical spaces, that the user added
1411                 maxasc += getLengthMarkerHeight(bview, firstpar->params().spaceTop());
1412
1413                 // do not forget the DTP-lines!
1414                 // there height depends on the font of the nearest character
1415                 if (firstpar->params().lineTop())
1416
1417                         maxasc += 2 * font_metrics::ascent('x', getFont(bview->buffer(),
1418                                         firstpar, 0));
1419                 // and now the pagebreaks
1420                 if (firstpar->params().pagebreakTop())
1421                         maxasc += 3 * defaultHeight();
1422
1423                 // This is special code for the chapter, since the label of this
1424                 // layout is printed in an extra row
1425                 if (layout->labeltype == LABEL_COUNTER_CHAPTER
1426                         && bview->buffer()->params.secnumdepth >= 0)
1427                 {
1428                         float spacing_val = 1.0;
1429                         if (!row_ptr->par()->params().spacing().isDefault()) {
1430                                 spacing_val = row_ptr->par()->params().spacing().getValue();
1431                         } else {
1432                                 spacing_val = bview->buffer()->params.spacing.getValue();
1433                         }
1434
1435                         labeladdon = int(font_metrics::maxDescent(labelfont) *
1436                                          layout->spacing.getValue() *
1437                                          spacing_val)
1438                                 + int(font_metrics::maxAscent(labelfont) *
1439                                       layout->spacing.getValue() *
1440                                       spacing_val);
1441                 }
1442
1443                 // special code for the top label
1444                 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1445                      || layout->labeltype == LABEL_BIBLIO
1446                      || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1447                     && row_ptr->par()->isFirstInSequence()
1448                     && !row_ptr->par()->getLabelstring().empty())
1449                 {
1450                         float spacing_val = 1.0;
1451                         if (!row_ptr->par()->params().spacing().isDefault()) {
1452                                 spacing_val = row_ptr->par()->params().spacing().getValue();
1453                         } else {
1454                                 spacing_val = bview->buffer()->params.spacing.getValue();
1455                         }
1456
1457                         labeladdon = int(
1458                                 (font_metrics::maxAscent(labelfont) *
1459                                  layout->spacing.getValue() *
1460                                  spacing_val)
1461                                 +(font_metrics::maxDescent(labelfont) *
1462                                   layout->spacing.getValue() *
1463                                   spacing_val)
1464                                 + layout->topsep * defaultHeight()
1465                                 + layout->labelbottomsep *  defaultHeight());
1466                 }
1467
1468                 // and now the layout spaces, for example before and after a section,
1469                 // or between the items of a itemize or enumerate environment
1470
1471                 if (!firstpar->params().pagebreakTop()) {
1472                         Paragraph * prev = row_ptr->par()->previous();
1473                         if (prev)
1474                                 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1475                         if (prev && prev->layout() == firstpar->layout() &&
1476                                 prev->getDepth() == firstpar->getDepth() &&
1477                                 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1478                         {
1479                                 layoutasc = (layout->itemsep * defaultHeight());
1480                         } else if (row_ptr->previous()) {
1481                                 tmptop = layout->topsep;
1482
1483                                 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1484                                         tmptop -= row_ptr->previous()->par()->layout()->bottomsep;
1485
1486                                 if (tmptop > 0)
1487                                         layoutasc = (tmptop * defaultHeight());
1488                         } else if (row_ptr->par()->params().lineTop()) {
1489                                 tmptop = layout->topsep;
1490
1491                                 if (tmptop > 0)
1492                                         layoutasc = (tmptop * defaultHeight());
1493                         }
1494
1495                         prev = row_ptr->par()->outerHook();
1496                         if (prev)  {
1497                                 maxasc += int(prev->layout()->parsep * defaultHeight());
1498                         } else {
1499                                 if (firstpar->previous() &&
1500                                         firstpar->previous()->getDepth() == 0 &&
1501                                         firstpar->previous()->layout() !=
1502                                         firstpar->layout())
1503                                 {
1504                                         // avoid parsep
1505                                 } else if (firstpar->previous()) {
1506                                         maxasc += int(layout->parsep * defaultHeight());
1507                                 }
1508                         }
1509                 }
1510         }
1511
1512         // is it a bottom line?
1513         if (row_ptr->par() == par
1514                 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
1515         {
1516                 // the paper margins
1517                 if (!par->next() && bv_owner)
1518                         maxdesc += LYX_PAPER_MARGIN;
1519
1520                 // add the vertical spaces, that the user added
1521                 maxdesc += getLengthMarkerHeight(bview, firstpar->params().spaceBottom());
1522
1523                 // do not forget the DTP-lines!
1524                 // there height depends on the font of the nearest character
1525                 if (firstpar->params().lineBottom())
1526                         maxdesc += 2 * font_metrics::ascent('x',
1527                                                        getFont(bview->buffer(),
1528                                                                par,
1529                                                                max(pos_type(0), par->size() - 1)));
1530
1531                 // and now the pagebreaks
1532                 if (firstpar->params().pagebreakBottom())
1533                         maxdesc += 3 * defaultHeight();
1534
1535                 // and now the layout spaces, for example before and after
1536                 // a section, or between the items of a itemize or enumerate
1537                 // environment
1538                 if (!firstpar->params().pagebreakBottom()
1539                     && row_ptr->par()->next()) {
1540                         Paragraph * nextpar = row_ptr->par()->next();
1541                         Paragraph * comparepar = row_ptr->par();
1542                         float usual = 0;
1543                         float unusual = 0;
1544
1545                         if (comparepar->getDepth() > nextpar->getDepth()) {
1546                                 usual = (comparepar->layout()->bottomsep * defaultHeight());
1547                                 comparepar = comparepar->depthHook(nextpar->getDepth());
1548                                 if (comparepar->layout()!= nextpar->layout()
1549                                         || nextpar->getLabelWidthString() !=
1550                                         comparepar->getLabelWidthString())
1551                                 {
1552                                         unusual = (comparepar->layout()->bottomsep * defaultHeight());
1553                                 }
1554                                 if (unusual > usual)
1555                                         layoutdesc = unusual;
1556                                 else
1557                                         layoutdesc = usual;
1558                         } else if (comparepar->getDepth() ==  nextpar->getDepth()) {
1559
1560                                 if (comparepar->layout() != nextpar->layout()
1561                                         || nextpar->getLabelWidthString() !=
1562                                         comparepar->getLabelWidthString())
1563                                         layoutdesc = int(comparepar->layout()->bottomsep * defaultHeight());
1564                         }
1565                 }
1566         }
1567
1568         // incalculate the layout spaces
1569         maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1570         maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1571
1572         // calculate the new height of the text
1573         height -= row_ptr->height();
1574
1575         row_ptr->height(maxasc + maxdesc + labeladdon);
1576         row_ptr->baseline(maxasc + labeladdon);
1577
1578         height += row_ptr->height();
1579         float x = 0;
1580         if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1581                 float dummy;
1582                 // this IS needed
1583                 row_ptr->width(maxwidth);
1584                 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1585         }
1586         row_ptr->width(int(maxwidth + x));
1587         if (inset_owner) {
1588                 Row * r = firstrow;
1589                 width = max(0,workWidth(bview));
1590                 while (r) {
1591                         if (r->width() > width)
1592                                 width = r->width();
1593                         r = r->next();
1594                 }
1595         }
1596 }
1597
1598
1599 // Appends the implicit specified paragraph behind the specified row,
1600 // start at the implicit given position
1601 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1602 {
1603         bool not_ready = true;
1604
1605         // The last character position of a paragraph is an invariant so we can
1606         // safely get it here. (Asger)
1607         pos_type const lastposition = row->par()->size();
1608         do {
1609                 // Get the next breakpoint
1610                 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1611
1612                 Row * tmprow = row;
1613
1614                 // Insert the new row
1615                 if (z < lastposition) {
1616                         ++z;
1617                         insertRow(row, row->par(), z);
1618                         row = row->next();
1619
1620                         row->height(0);
1621                 } else
1622                         not_ready = false;
1623
1624                 // Set the dimensions of the row
1625                 // fixed fill setting now by calling inset->update() in
1626                 // SingleWidth when needed!
1627                 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1628                 setHeightOfRow(bview, tmprow);
1629
1630         } while (not_ready);
1631 }
1632
1633
1634 void LyXText::breakAgain(BufferView * bview, Row * row) const
1635 {
1636         bool not_ready = true;
1637
1638         do  {
1639                 // get the next breakpoint
1640                 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1641                 Row * tmprow = row;
1642
1643                 if (z < row->par()->size()) {
1644                         if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1645                                 // insert a new row
1646                                 ++z;
1647                                 insertRow(row, row->par(), z);
1648                                 row = row->next();
1649                                 row->height(0);
1650                         } else  {
1651                                 row = row->next();
1652                                 ++z;
1653                                 if (row->pos() == z)
1654                                         not_ready = false;     // the rest will not change
1655                                 else {
1656                                         row->pos(z);
1657                                 }
1658                         }
1659                 } else {
1660                         // if there are some rows too much, delete them
1661                         // only if you broke the whole paragraph!
1662                         Row * tmprow2 = row;
1663                         while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1664                                 tmprow2 = tmprow2->next();
1665                         }
1666                         while (tmprow2 != row) {
1667                                 tmprow2 = tmprow2->previous();
1668                                 removeRow(tmprow2->next());
1669                         }
1670                         not_ready = false;
1671                 }
1672
1673                 // set the dimensions of the row
1674                 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1675                 setHeightOfRow(bview, tmprow);
1676         } while (not_ready);
1677 }
1678
1679
1680 // this is just a little changed version of break again
1681 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1682 {
1683         // get the next breakpoint
1684         pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1685         Row * tmprow = row;
1686
1687         if (z < row->par()->size()) {
1688                 if (!row->next()
1689                     || (row->next() && row->next()->par() != row->par())) {
1690                         // insert a new row
1691                         ++z;
1692                         insertRow(row, row->par(), z);
1693                         row = row->next();
1694                         row->height(0);
1695                 } else  {
1696                         row = row->next();
1697                         ++z;
1698                         if (row->pos() != z)
1699                                 row->pos(z);
1700                 }
1701         } else {
1702                 // if there are some rows too much, delete them
1703                 // only if you broke the whole paragraph!
1704                 Row * tmprow2 = row;
1705                 while (tmprow2->next()
1706                        && tmprow2->next()->par() == row->par()) {
1707                         tmprow2 = tmprow2->next();
1708                 }
1709                 while (tmprow2 != row) {
1710                         tmprow2 = tmprow2->previous();
1711                         removeRow(tmprow2->next());
1712                 }
1713         }
1714
1715         // set the dimensions of the row
1716         tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1717         setHeightOfRow(bview, tmprow);
1718 }
1719
1720
1721 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1722 {
1723         LyXTextClass const & tclass =
1724                 bview->buffer()->params.getLyXTextClass();
1725         LyXLayout_ptr const & layout = cursor.par()->layout();
1726
1727         // this is only allowed, if the current paragraph is not empty or caption
1728         // and if it has not the keepempty flag aktive
1729         if (cursor.par()->empty()
1730            && layout->labeltype != LABEL_SENSITIVE
1731            && !layout->keepempty)
1732                 return;
1733
1734         setUndo(bview, Undo::FINISH, cursor.par(), cursor.par()->next());
1735
1736         // Always break behind a space
1737         //
1738         // It is better to erase the space (Dekel)
1739         if (cursor.pos() < cursor.par()->size()
1740              && cursor.par()->isLineSeparator(cursor.pos()))
1741            cursor.par()->erase(cursor.pos());
1742         // cursor.pos(cursor.pos() + 1);
1743
1744         // break the paragraph
1745         if (keep_layout)
1746                 keep_layout = 2;
1747         else
1748                 keep_layout = layout->isEnvironment();
1749
1750         // we need to set this before we insert the paragraph. IMO the
1751         // breakParagraph call should return a bool if it inserts the
1752         // paragraph before or behind and we should react on that one
1753         // but we can fix this in 1.3.0 (Jug 20020509)
1754         bool const isempty = (layout->keepempty && cursor.par()->empty());
1755         ::breakParagraph(bview->buffer()->params, cursor.par(), cursor.pos(),
1756                        keep_layout);
1757
1758         // well this is the caption hack since one caption is really enough
1759         if (layout->labeltype == LABEL_SENSITIVE) {
1760                 if (!cursor.pos())
1761                         // set to standard-layout
1762                         cursor.par()->applyLayout(tclass.defaultLayout());
1763                 else
1764                         // set to standard-layout
1765                         cursor.par()->next()->applyLayout(tclass.defaultLayout());
1766         }
1767
1768         // if the cursor is at the beginning of a row without prior newline,
1769         // move one row up!
1770         // This touches only the screen-update. Otherwise we would may have
1771         // an empty row on the screen
1772         if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1773                          && cursor.row()->pos() == cursor.pos())
1774         {
1775                 cursorLeft(bview);
1776         }
1777
1778         status(bview, LyXText::NEED_MORE_REFRESH);
1779         refresh_row = cursor.row();
1780         refresh_y = cursor.y() - cursor.row()->baseline();
1781
1782         // Do not forget the special right address boxes
1783         if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1784                 while (refresh_row->previous() &&
1785                        refresh_row->previous()->par() == refresh_row->par())
1786                 {
1787                         refresh_row = refresh_row->previous();
1788                         refresh_y -= refresh_row->height();
1789                 }
1790         }
1791         removeParagraph(cursor.row());
1792
1793         // set the dimensions of the cursor row
1794         cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1795
1796         setHeightOfRow(bview, cursor.row());
1797
1798         while (!cursor.par()->next()->empty()
1799           && cursor.par()->next()->isNewline(0))
1800            cursor.par()->next()->erase(0);
1801
1802         insertParagraph(bview, cursor.par()->next(), cursor.row());
1803
1804         updateCounters(bview);
1805
1806         // This check is necessary. Otherwise the new empty paragraph will
1807         // be deleted automatically. And it is more friendly for the user!
1808         if (cursor.pos() || isempty)
1809                 setCursor(bview, cursor.par()->next(), 0);
1810         else
1811                 setCursor(bview, cursor.par(), 0);
1812
1813         if (cursor.row()->next())
1814                 breakAgain(bview, cursor.row()->next());
1815
1816         need_break_row = 0;
1817 }
1818
1819
1820 // Just a macro to make some thing easier.
1821 void LyXText::redoParagraph(BufferView * bview) const
1822 {
1823         clearSelection();
1824         redoParagraphs(bview, cursor, cursor.par()->next());
1825         setCursorIntern(bview, cursor.par(), cursor.pos());
1826 }
1827
1828
1829 // insert a character, moves all the following breaks in the
1830 // same Paragraph one to the right and make a rebreak
1831 void LyXText::insertChar(BufferView * bview, char c)
1832 {
1833         setUndo(bview, Undo::INSERT, cursor.par(), cursor.par()->next());
1834
1835         // When the free-spacing option is set for the current layout,
1836         // disable the double-space checking
1837
1838         bool const freeSpacing = cursor.row()->par()->layout()->free_spacing ||
1839                 cursor.row()->par()->isFreeSpacing();
1840
1841         if (lyxrc.auto_number) {
1842                 static string const number_operators = "+-/*";
1843                 static string const number_unary_operators = "+-";
1844                 static string const number_seperators = ".,:";
1845
1846                 if (current_font.number() == LyXFont::ON) {
1847                         if (!IsDigit(c) && !contains(number_operators, c) &&
1848                             !(contains(number_seperators, c) &&
1849                               cursor.pos() >= 1 &&
1850                               cursor.pos() < cursor.par()->size() &&
1851                               getFont(bview->buffer(),
1852                                       cursor.par(),
1853                                       cursor.pos()).number() == LyXFont::ON &&
1854                               getFont(bview->buffer(),
1855                                       cursor.par(),
1856                                       cursor.pos() - 1).number() == LyXFont::ON)
1857                            )
1858                                 number(bview); // Set current_font.number to OFF
1859                 } else if (IsDigit(c) &&
1860                            real_current_font.isVisibleRightToLeft()) {
1861                         number(bview); // Set current_font.number to ON
1862
1863                         if (cursor.pos() > 0) {
1864                                 char const c = cursor.par()->getChar(cursor.pos() - 1);
1865                                 if (contains(number_unary_operators, c) &&
1866                                     (cursor.pos() == 1 ||
1867                                      cursor.par()->isSeparator(cursor.pos() - 2) ||
1868                                      cursor.par()->isNewline(cursor.pos() - 2))
1869                                   ) {
1870                                         setCharFont(bview->buffer(),
1871                                                     cursor.par(),
1872                                                     cursor.pos() - 1,
1873                                                     current_font);
1874                                 } else if (contains(number_seperators, c) &&
1875                                            cursor.pos() >= 2 &&
1876                                            getFont(bview->buffer(),
1877                                                    cursor.par(),
1878                                                    cursor.pos() - 2).number() == LyXFont::ON) {
1879                                         setCharFont(bview->buffer(),
1880                                                     cursor.par(),
1881                                                     cursor.pos() - 1,
1882                                                     current_font);
1883                                 }
1884                         }
1885                 }
1886         }
1887
1888
1889         // First check, if there will be two blanks together or a blank at
1890         // the beginning of a paragraph.
1891         // I decided to handle blanks like normal characters, the main
1892         // difference are the special checks when calculating the row.fill
1893         // (blank does not count at the end of a row) and the check here
1894
1895         // The bug is triggered when we type in a description environment:
1896         // The current_font is not changed when we go from label to main text
1897         // and it should (along with realtmpfont) when we type the space.
1898         // CHECK There is a bug here! (Asger)
1899
1900         LyXFont realtmpfont = real_current_font;
1901         LyXFont rawtmpfont = current_font;
1902         // store the current font.  This is because of the use of cursor
1903         // movements. The moving cursor would refresh the current font
1904
1905         // Get the font that is used to calculate the baselineskip
1906         pos_type const lastpos = cursor.par()->size();
1907         LyXFont rawparfont =
1908                 cursor.par()->getFontSettings(bview->buffer()->params,
1909                                               lastpos - 1);
1910
1911         bool jumped_over_space = false;
1912
1913         if (!freeSpacing && IsLineSeparatorChar(c)) {
1914                 if ((cursor.pos() > 0
1915                      && cursor.par()->isLineSeparator(cursor.pos() - 1))
1916                     || (cursor.pos() > 0
1917                         && cursor.par()->isNewline(cursor.pos() - 1))
1918                     || (cursor.pos() == 0)) {
1919                         static bool sent_space_message = false;
1920                         if (!sent_space_message) {
1921                                 if (cursor.pos() == 0)
1922                                         bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph.  Please read the Tutorial."));
1923                                 else
1924                                         bview->owner()->message(_("You cannot type two spaces this way.  Please read the Tutorial."));
1925                                 sent_space_message = true;
1926                         }
1927                         charInserted();
1928                         return;
1929                 }
1930         } else if (IsNewlineChar(c)) {
1931                 if (cursor.pos() <= beginningOfMainBody(bview->buffer(),
1932                                                         cursor.par()))
1933                 {
1934                         charInserted();
1935                         return;
1936                 }
1937                 // No newline at first position of a paragraph or behind labels.
1938                 // TeX does not allow that
1939
1940                 if (cursor.pos() < cursor.par()->size() &&
1941                     cursor.par()->isLineSeparator(cursor.pos()))
1942                         // newline always after a blank!
1943                         cursorRight(bview);
1944                 cursor.row()->fill(-1);        // to force a new break
1945         }
1946
1947         // the display inset stuff
1948         if (cursor.row()->par()->isInset(cursor.row()->pos())) {
1949                 Inset * inset = cursor.row()->par()->getInset(cursor.row()->pos());
1950                 if (inset && (inset->display() || inset->needFullRow())) {
1951                         // force a new break
1952                         cursor.row()->fill(-1); // to force a new break
1953                 }
1954         }
1955
1956         // get the cursor row fist
1957         Row * row = cursor.row();
1958         int y = cursor.y() - row->baseline();
1959         if (c != Paragraph::META_INSET) {
1960                 // Here case LyXText::InsertInset  already insertet the character
1961                 cursor.par()->insertChar(cursor.pos(), c);
1962         }
1963         setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1964
1965         if (!jumped_over_space) {
1966                 // refresh the positions
1967                 Row * tmprow = row;
1968                 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1969                         tmprow = tmprow->next();
1970                         tmprow->pos(tmprow->pos() + 1);
1971                 }
1972         }
1973
1974         // Is there a break one row above
1975         if (row->previous() && row->previous()->par() == row->par()
1976             && (cursor.par()->isLineSeparator(cursor.pos())
1977                 || cursor.par()->isNewline(cursor.pos())
1978                 || ((cursor.pos() < cursor.par()->size()) &&
1979                     cursor.par()->isInset(cursor.pos()+1))
1980                 || cursor.row()->fill() == -1))
1981         {
1982                 pos_type z = nextBreakPoint(bview,
1983                                                            row->previous(),
1984                                                            workWidth(bview));
1985                 if (z >= row->pos()) {
1986                         row->pos(z + 1);
1987
1988                         // set the dimensions of the row above
1989                         row->previous()->fill(fill(bview,
1990                                                    row->previous(),
1991                                                    workWidth(bview)));
1992
1993                         setHeightOfRow(bview, row->previous());
1994
1995                         y -= row->previous()->height();
1996                         refresh_y = y;
1997                         refresh_row = row->previous();
1998                         status(bview, LyXText::NEED_MORE_REFRESH);
1999
2000                         breakAgainOneRow(bview, row);
2001
2002                         current_font = rawtmpfont;
2003                         real_current_font = realtmpfont;
2004                         setCursor(bview, cursor.par(), cursor.pos() + 1,
2005                                   false, cursor.boundary());
2006                         // cursor MUST be in row now.
2007
2008                         if (row->next() && row->next()->par() == row->par())
2009                                 need_break_row = row->next();
2010                         else
2011                                 need_break_row = 0;
2012
2013                         // check, wether the last characters font has changed.
2014                         if (cursor.pos() && cursor.pos() == cursor.par()->size()
2015                             && rawparfont != rawtmpfont)
2016                                 redoHeightOfParagraph(bview, cursor);
2017
2018                         charInserted();
2019                         return;
2020                 }
2021         }
2022
2023         // recalculate the fill of the row
2024         if (row->fill() >= 0) {
2025                 // needed because a newline will set fill to -1. Otherwise
2026                 // we would not get a rebreak!
2027                 row->fill(fill(bview, row, workWidth(bview)));
2028         }
2029         if (c == Paragraph::META_INSET || row->fill() < 0) {
2030                 refresh_y = y;
2031                 refresh_row = row;
2032                 refresh_x = cursor.x();
2033                 refresh_pos = cursor.pos();
2034                 status(bview, LyXText::NEED_MORE_REFRESH);
2035                 breakAgainOneRow(bview, row);
2036                 // will the cursor be in another row now?
2037                 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
2038                         if (row->next() && row->next()->par() == row->par())
2039                                 // this should always be true
2040                                 row = row->next();
2041                         breakAgainOneRow(bview, row);
2042                 }
2043                 current_font = rawtmpfont;
2044                 real_current_font = realtmpfont;
2045
2046                 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2047                           cursor.boundary());
2048                 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2049                     != cursor.boundary())
2050                         setCursor(bview, cursor.par(), cursor.pos(), false,
2051                           !cursor.boundary());
2052                 if (row->next() && row->next()->par() == row->par())
2053                         need_break_row = row->next();
2054                 else
2055                         need_break_row = 0;
2056         } else {
2057                 refresh_y = y;
2058                 refresh_x = cursor.x();
2059                 refresh_row = row;
2060                 refresh_pos = cursor.pos();
2061
2062                 int const tmpheight = row->height();
2063                 setHeightOfRow(bview, row);
2064                 if (tmpheight == row->height())
2065                         status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2066                 else
2067                         status(bview, LyXText::NEED_MORE_REFRESH);
2068
2069                 current_font = rawtmpfont;
2070                 real_current_font = realtmpfont;
2071                 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2072                           cursor.boundary());
2073         }
2074
2075         // check, wether the last characters font has changed.
2076         if (cursor.pos() && cursor.pos() == cursor.par()->size()
2077             && rawparfont != rawtmpfont) {
2078                 redoHeightOfParagraph(bview, cursor);
2079         } else {
2080                 // now the special right address boxes
2081                 if (cursor.par()->layout()->margintype
2082                     == MARGIN_RIGHT_ADDRESS_BOX) {
2083                         redoDrawingOfParagraph(bview, cursor);
2084                 }
2085         }
2086
2087         charInserted();
2088 }
2089
2090
2091 void LyXText::charInserted()
2092 {
2093         // Here we could call FinishUndo for every 20 characters inserted.
2094         // This is from my experience how emacs does it.
2095         static unsigned int counter;
2096         if (counter < 20) {
2097                 ++counter;
2098         } else {
2099                 finishUndo();
2100                 counter = 0;
2101         }
2102 }
2103
2104
2105 void LyXText::prepareToPrint(BufferView * bview,
2106                              Row * row, float & x,
2107                              float & fill_separator,
2108                              float & fill_hfill,
2109                              float & fill_label_hfill,
2110                              bool bidi) const
2111 {
2112         float nlh;
2113         float ns;
2114
2115         float w = row->fill();
2116         fill_hfill = 0;
2117         fill_label_hfill = 0;
2118         fill_separator = 0;
2119         fill_label_hfill = 0;
2120
2121         bool const is_rtl =
2122                 row->par()->isRightToLeftPar(bview->buffer()->params);
2123         if (is_rtl) {
2124                 x = (workWidth(bview) > 0)
2125                         ? rightMargin(bview->buffer(), row) : 0;
2126         } else
2127                 x = (workWidth(bview) > 0)
2128                         ? leftMargin(bview, row) : 0;
2129
2130         // is there a manual margin with a manual label
2131         LyXLayout_ptr const & layout = row->par()->layout();
2132
2133         if (layout->margintype == MARGIN_MANUAL
2134             && layout->labeltype == LABEL_MANUAL) {
2135                 // one more since labels are left aligned
2136                 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
2137                 if (nlh && !row->par()->getLabelWidthString().empty()) {
2138                         fill_label_hfill = labelFill(bview, row) / nlh;
2139                 }
2140         }
2141
2142         // are there any hfills in the row?
2143         float const nh = numberOfHfills(bview->buffer(), row);
2144
2145         if (nh) {
2146                 if (w > 0)
2147                         fill_hfill = w / nh;
2148         // we don't have to look at the alignment if it is ALIGN_LEFT and
2149         // if the row is already larger then the permitted width as then
2150         // we force the LEFT_ALIGN'edness!
2151         } else if (static_cast<int>(row->width()) < workWidth(bview)) {
2152                 // is it block, flushleft or flushright?
2153                 // set x how you need it
2154                 int align;
2155                 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
2156                         align = layout->align;
2157                 } else {
2158                         align = row->par()->params().align();
2159                 }
2160
2161                 // center displayed insets
2162                 Inset * inset;
2163                 if (row->par()->isInset(row->pos())
2164                     && (inset=row->par()->getInset(row->pos()))
2165                     && (inset->display())) // || (inset->scroll() < 0)))
2166                     align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2167                         ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2168                 // ERT insets should always be LEFT ALIGNED on screen
2169                 inset = row->par()->inInset();
2170                 if (inset && inset->owner() &&
2171                         inset->owner()->lyxCode() == Inset::ERT_CODE)
2172                 {
2173                         align = LYX_ALIGN_LEFT;
2174                 }
2175
2176                 switch (align) {
2177             case LYX_ALIGN_BLOCK:
2178                         ns = numberOfSeparators(bview->buffer(), row);
2179                         if (ns && row->next() && row->next()->par() == row->par() &&
2180                             !(row->next()->par()->isNewline(row->next()->pos() - 1))
2181                             && !(row->next()->par()->isInset(row->next()->pos())
2182                                  && row->next()->par()->getInset(row->next()->pos())
2183                                  && row->next()->par()->getInset(row->next()->pos())->display())
2184                                 )
2185                         {
2186                                 fill_separator = w / ns;
2187                         } else if (is_rtl) {
2188                                 x += w;
2189                         }
2190                         break;
2191             case LYX_ALIGN_RIGHT:
2192                         x += w;
2193                         break;
2194             case LYX_ALIGN_CENTER:
2195                         x += w / 2;
2196                         break;
2197                 }
2198         }
2199         if (!bidi)
2200                 return;
2201
2202         computeBidiTables(bview->buffer(), row);
2203         if (is_rtl) {
2204                 pos_type main_body =
2205                         beginningOfMainBody(bview->buffer(), row->par());
2206                 pos_type last = rowLast(row);
2207
2208                 if (main_body > 0 &&
2209                     (main_body - 1 > last ||
2210                      !row->par()->isLineSeparator(main_body - 1))) {
2211                         x += font_metrics::width(layout->labelsep,
2212                                             getLabelFont(bview->buffer(), row->par()));
2213                         if (main_body - 1 <= last)
2214                                 x += fill_label_hfill;
2215                 }
2216         }
2217 }
2218
2219
2220 // important for the screen
2221
2222
2223 // the cursor set functions have a special mechanism. When they
2224 // realize, that you left an empty paragraph, they will delete it.
2225 // They also delete the corresponding row
2226
2227 void LyXText::cursorRightOneWord(BufferView * bview) const
2228 {
2229         // treat floats, HFills and Insets as words
2230         LyXCursor tmpcursor = cursor;
2231         // CHECK See comment on top of text.C
2232
2233         if (tmpcursor.pos() == tmpcursor.par()->size()
2234             && tmpcursor.par()->next()) {
2235                         tmpcursor.par(tmpcursor.par()->next());
2236                         tmpcursor.pos(0);
2237         } else {
2238                 int steps = 0;
2239
2240                 // Skip through initial nonword stuff.
2241                 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2242                        ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2243                   //    printf("Current pos1 %d", tmpcursor.pos()) ;
2244                         tmpcursor.pos(tmpcursor.pos() + 1);
2245                         ++steps;
2246                 }
2247                 // Advance through word.
2248                 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2249                         tmpcursor.par()->isWord(tmpcursor.pos())) {
2250                   //     printf("Current pos2 %d", tmpcursor.pos()) ;
2251                         tmpcursor.pos(tmpcursor.pos() + 1);
2252                         ++steps;
2253                 }
2254         }
2255         setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2256 }
2257
2258
2259 void LyXText::cursorTab(BufferView * bview) const
2260 {
2261         LyXCursor tmpcursor = cursor;
2262         while (tmpcursor.pos() < tmpcursor.par()->size()
2263            && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2264         tmpcursor.pos(tmpcursor.pos() + 1);
2265
2266         if (tmpcursor.pos() == tmpcursor.par()->size()) {
2267                 if (tmpcursor.par()->next()) {
2268                         tmpcursor.par(tmpcursor.par()->next());
2269                         tmpcursor.pos(0);
2270                 }
2271         } else
2272                 tmpcursor.pos(tmpcursor.pos() + 1);
2273         setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2274 }
2275
2276
2277 // Skip initial whitespace at end of word and move cursor to *start*
2278 // of prior word, not to end of next prior word.
2279 void LyXText::cursorLeftOneWord(BufferView * bview)  const
2280 {
2281         LyXCursor tmpcursor = cursor;
2282         cursorLeftOneWord(tmpcursor);
2283         setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2284 }
2285
2286
2287 void LyXText::cursorLeftOneWord(LyXCursor  & cur)  const
2288 {
2289         // treat HFills, floats and Insets as words
2290         cur = cursor;
2291         while (cur.pos()
2292                && (cur.par()->isSeparator(cur.pos() - 1)
2293                    || cur.par()->isKomma(cur.pos() - 1))
2294                && !(cur.par()->isHfill(cur.pos() - 1)
2295                     || cur.par()->isInset(cur.pos() - 1)))
2296                 cur.pos(cur.pos() - 1);
2297
2298         if (cur.pos()
2299             && (cur.par()->isInset(cur.pos() - 1)
2300                 || cur.par()->isHfill(cur.pos() - 1))) {
2301                 cur.pos(cur.pos() - 1);
2302         } else if (!cur.pos()) {
2303                 if (cur.par()->previous()) {
2304                         cur.par(cur.par()->previous());
2305                         cur.pos(cur.par()->size());
2306                 }
2307         } else {                // Here, cur != 0
2308                 while (cur.pos() > 0 &&
2309                        cur.par()->isWord(cur.pos()-1))
2310                         cur.pos(cur.pos() - 1);
2311         }
2312 }
2313
2314
2315 // Select current word. This depends on behaviour of
2316 // CursorLeftOneWord(), so it is patched as well.
2317 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2318                       word_location const loc) const
2319 {
2320         // first put the cursor where we wana start to select the word
2321         from = cursor;
2322         switch (loc) {
2323         case WHOLE_WORD_STRICT:
2324                 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2325                     || cursor.par()->isSeparator(cursor.pos())
2326                     || cursor.par()->isKomma(cursor.pos())
2327                     || cursor.par()->isSeparator(cursor.pos() -1)
2328                     || cursor.par()->isKomma(cursor.pos() -1)) {
2329                         to = from;
2330                         return;
2331                 }
2332                 // no break here, we go to the next
2333
2334         case WHOLE_WORD:
2335                 // Move cursor to the beginning, when not already there.
2336                 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2337                     && !from.par()->isKomma(from.pos() - 1))
2338                         cursorLeftOneWord(from);
2339                 break;
2340         case PREVIOUS_WORD:
2341                 // always move the cursor to the beginning of previous word
2342                 cursorLeftOneWord(from);
2343                 break;
2344         case NEXT_WORD:
2345                 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2346                 break;
2347         case PARTIAL_WORD:
2348                 break;
2349         }
2350         to = from;
2351         while (to.pos() < to.par()->size()
2352                && !to.par()->isSeparator(to.pos())
2353                && !to.par()->isKomma(to.pos())
2354                && !to.par()->isHfill(to.pos())
2355                && !to.par()->isInset(to.pos()))
2356         {
2357                 to.pos(to.pos() + 1);
2358         }
2359 }
2360
2361
2362 void LyXText::selectWord(BufferView * bview, word_location const loc)
2363 {
2364         LyXCursor from;
2365         LyXCursor to;
2366         getWord(from, to, loc);
2367         if (cursor != from)
2368                 setCursor(bview, from.par(), from.pos());
2369         if (to == from)
2370                 return;
2371         selection.cursor = cursor;
2372         setCursor(bview, to.par(), to.pos());
2373         setSelection(bview);
2374 }
2375
2376
2377 // Select the word currently under the cursor when no
2378 // selection is currently set
2379 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2380                                         word_location const loc)
2381 {
2382         if (!selection.set()) {
2383                 selectWord(bview, loc);
2384                 return selection.set();
2385         }
2386         return false;
2387 }
2388
2389
2390 // This function is only used by the spellchecker for NextWord().
2391 // It doesn't handle LYX_ACCENTs and probably never will.
2392 WordLangTuple const
2393 LyXText::selectNextWordToSpellcheck(BufferView * bview, float & value) const
2394 {
2395         if (the_locking_inset) {
2396                 WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bview, value);
2397                 if (!word.word().empty()) {
2398                         value += float(cursor.y())/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                         pasteParagraph(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                         x += 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 }