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