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