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