]> git.lyx.org Git - features.git/blob - src/text.C
45bd46d8989e83718b92bcab5936cc0cabbedd3c
[features.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-1999 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12 #include <cstdlib>
13 #include <cctype>
14
15 #ifdef __GNUG__
16 #pragma implementation "table.h"
17 #endif
18
19 #include "layout.h"
20 #include "lyxparagraph.h"
21 #include "lyxtext.h"
22 #include "support/textutils.h"
23 #include "lyx_gui_misc.h"
24 #include "lyxdraw.h"
25 #include "gettext.h"
26 #include "bufferparams.h"
27 #include "buffer.h"
28 #include "lyxscreen.h"
29 #include "minibuffer.h"
30 #include "debug.h"
31 #include "lyxrc.h"
32 #include "LyXView.h"
33 #include "lyxrow.h"
34 #include "Painter.h"
35
36 using std::max;
37 using std::min;
38
39 static const int LYX_PAPER_MARGIN = 20;
40
41 extern int mono_video;
42 extern int reverse_video;
43 extern int fast_selection;
44 extern BufferView * current_view;
45 extern LyXRC * lyxrc;
46
47 // ale070405
48 #ifdef USE_PAINTER
49 extern int bibitemMaxWidth(Painter &, LyXFont const &);
50 #else
51 extern int bibitemMaxWidth(LyXFont const &);
52 #endif
53
54 #define FIX_DOUBLE_SPACE 1
55
56 int LyXText::SingleWidth(LyXParagraph * par,
57                          LyXParagraph::size_type pos) const
58 {
59         char c = par->GetChar(pos);
60         return SingleWidth(par, pos, c);
61 }
62
63
64 #ifdef USE_PAINTER
65 int LyXText::SingleWidth(LyXParagraph * par,
66                          LyXParagraph::size_type pos, char c) const
67 {
68         LyXFont font = GetFont(par, pos);
69
70         // The most common case is handled first (Asger)
71         if (IsPrintable(c)) {
72                 return font.width(c);
73
74         } else if (IsHfillChar(c)) {
75                 return 3;       /* Because of the representation
76                                  * as vertical lines */
77         } else if (c == LyXParagraph::META_FOOTNOTE ||
78                    c == LyXParagraph::META_MARGIN ||
79                    c == LyXParagraph::META_FIG ||
80                    c == LyXParagraph::META_TAB ||
81                    c == LyXParagraph::META_WIDE_FIG ||
82                    c == LyXParagraph::META_WIDE_TAB ||
83                    c == LyXParagraph::META_ALGORITHM) {
84                 string fs;
85                 switch (c) {
86                 case LyXParagraph::META_MARGIN:
87                         fs = "margin";
88                         break;
89                 case LyXParagraph::META_FIG:
90                         fs = "fig";
91                         break;
92                 case LyXParagraph::META_TAB:
93                         fs = "tab";
94                         break;
95                 case LyXParagraph::META_ALGORITHM:
96                         fs = "alg";
97                         break;
98                 case LyXParagraph::META_WIDE_FIG:
99                         fs = "wide-fig";
100                         break;
101                 case LyXParagraph::META_WIDE_TAB:
102                         fs = "wide-tab";
103                         break;
104                 case LyXParagraph::META_FOOTNOTE:
105                         fs = "foot";
106                         break;
107                 }
108                 font.decSize();
109                 font.decSize();
110                 return font.stringWidth(fs);
111         } else if (c == LyXParagraph::META_INSET) {
112                 Inset * tmpinset= par->GetInset(pos);
113                 if (tmpinset)
114                         return par->GetInset(pos)->width(owner_->painter(),
115                                                          font);
116                 else
117                         return 0;
118
119         } else if (IsSeparatorChar(c))
120                 c = ' ';
121         else if (IsNewlineChar(c))
122                 c = 'n';
123         return font.width(c);
124 }
125 #else
126 int LyXText::SingleWidth(LyXParagraph * par,
127                          LyXParagraph::size_type pos, char c) const
128 {
129         LyXFont font = GetFont(par, pos);
130
131         // The most common case is handled first (Asger)
132         if (IsPrintable(c)) {
133                 return font.width(c);
134
135         } else if (IsHfillChar(c)) {
136                 return 3;       /* Because of the representation
137                                  * as vertical lines */
138
139         } else if (c == LyXParagraph::META_FOOTNOTE ||
140                    c == LyXParagraph::META_MARGIN ||
141                    c == LyXParagraph::META_FIG ||
142                    c == LyXParagraph::META_TAB ||
143                    c == LyXParagraph::META_WIDE_FIG ||
144                    c == LyXParagraph::META_WIDE_TAB ||
145                    c == LyXParagraph::META_ALGORITHM) 
146         {
147                 string fs;
148                 switch (c) {
149                 case LyXParagraph::META_MARGIN:
150                         fs = "margin";
151                         break;
152                 case LyXParagraph::META_FIG:
153                         fs = "fig";
154                         break;
155                 case LyXParagraph::META_TAB:
156                         fs = "tab";
157                         break;
158                 case LyXParagraph::META_ALGORITHM:
159                         fs = "alg";
160                         break;
161                 case LyXParagraph::META_WIDE_FIG:
162                         fs = "wide-fig";
163                         break;
164                 case LyXParagraph::META_WIDE_TAB:
165                         fs = "wide-tab";
166                         break;
167                 case LyXParagraph::META_FOOTNOTE:
168                         fs = "foot";
169                         break;
170                 }
171                 font.decSize();
172                 font.decSize();
173                 return font.stringWidth(fs);
174         } 
175    
176         else if (c == LyXParagraph::META_INSET) {
177                 Inset *tmpinset= par->GetInset(pos);
178                 if (tmpinset)
179                         return par->GetInset(pos)->Width(font);
180                 else
181                         return 0;
182
183         } else if (IsSeparatorChar(c))
184                 c = ' ';
185         else if (IsNewlineChar(c))
186                 c = 'n';
187         return font.width(c);
188 }
189 #endif
190
191
192 // Returns the paragraph position of the last character in the specified row
193 LyXParagraph::size_type LyXText::RowLast(Row const * row) const
194 {
195         if (row->next == 0)
196                 return row->par->Last() - 1;
197         else if (row->next->par != row->par) 
198                 return row->par->Last() - 1;
199         else 
200                 return row->next->pos - 1;
201 }
202
203
204 LyXDirection BufferParams::getDocumentDirection() const
205 {
206         return (lyxrc->rtl_support && language == "hebrew")
207                 ? LYX_DIR_RIGHT_TO_LEFT : LYX_DIR_LEFT_TO_RIGHT;
208 }
209
210 LyXDirection LyXParagraph::getParDirection() const
211 {
212         if (!lyxrc->rtl_support || table)
213                 return LYX_DIR_LEFT_TO_RIGHT;
214
215         if (size() > 0)
216                 return (getFont(0).direction() ==  LyXFont::RTL_DIR)
217                         ? LYX_DIR_RIGHT_TO_LEFT : LYX_DIR_LEFT_TO_RIGHT;
218         else
219                 return current_view->buffer()->params.getDocumentDirection();
220 }
221
222 LyXDirection LyXFont::getFontDirection() const
223 {
224         if (lyxrc->rtl_support 
225             && direction() == LyXFont::RTL_DIR
226             && latex() != LyXFont::ON)
227                 return LYX_DIR_RIGHT_TO_LEFT;
228         else
229                 return LYX_DIR_LEFT_TO_RIGHT;
230 }
231
232 LyXDirection
233 LyXParagraph::getLetterDirection(LyXParagraph::size_type pos) const
234 {
235         if (!lyxrc->rtl_support)
236                 return LYX_DIR_LEFT_TO_RIGHT;
237
238         LyXDirection direction = getFont(pos).getFontDirection();
239         if (IsLineSeparator(pos) && 0 < pos && pos < Last() - 1
240             && !IsLineSeparator(pos + 1)
241             && !(table && IsNewline(pos + 1))
242             && (getFont(pos - 1).getFontDirection() != direction
243                 || getFont(pos + 1).getFontDirection() != direction))
244                 return getParDirection();
245         else
246                 return direction;
247 }
248
249
250 void LyXText::ComputeBidiTables(Row * row) const
251 {
252
253         if (!lyxrc->rtl_support) {
254                 bidi_start = -1;
255                 return;
256         }
257         LyXParagraph::size_type last = RowLast(row);
258         bidi_start = row->pos;
259
260         if (bidi_start > last) {
261                 bidi_start = -1;
262                 return;
263         }
264
265         if (last + 2 - bidi_start >
266             static_cast<LyXParagraph::size_type>(log2vis_list.size())) {
267                 LyXParagraph::size_type new_size = 
268                         (last + 2 - bidi_start < 500) ?
269                         500 : 2 * (last + 2 - bidi_start);
270                 log2vis_list.resize(new_size);
271                 vis2log_list.resize(new_size);
272         }
273
274         vis2log_list[last + 1 - bidi_start] = -1;
275         log2vis_list[last + 1 - bidi_start] = -1;
276
277         LyXParagraph::size_type main_body = BeginningOfMainBody(row->par);
278         if (main_body > 0 && row->pos < main_body - 1 && main_body - 1 <= last
279             && row->par->IsLineSeparator(main_body - 1)) {
280                 // This is needed in case there is a direction change in
281                 // the label which is continued into the main body
282                 if (row->par->getParDirection() == LYX_DIR_LEFT_TO_RIGHT) {
283                         ComputeBidiTablesFromTo(row, bidi_start,
284                                                 main_body - 2, 0);
285                         log2vis_list[main_body - 1 - bidi_start] =
286                                 main_body - 1;
287                         vis2log_list[main_body - 1 - bidi_start] =
288                                 main_body - 1;
289                         if (main_body <= last)
290                                 ComputeBidiTablesFromTo(row,
291                                                         main_body,last, 0);
292                 } else {
293                         ComputeBidiTablesFromTo(row, bidi_start,
294                                                 main_body - 2,
295                                                 last - main_body + 2);
296                         log2vis_list[main_body - 1 - bidi_start] =
297                                 last - main_body + 1 + bidi_start;
298                         vis2log_list[last - main_body + 1 - bidi_start] =
299                                 main_body - 1;
300                         if (main_body <= last)
301                                 ComputeBidiTablesFromTo(row, main_body,
302                                                         last, -main_body);
303                 }
304         } else
305                 ComputeBidiTablesFromTo(row, bidi_start, last, 0);
306 }
307
308
309 void LyXText::ComputeBidiTablesFromTo(Row * row,
310                                       LyXParagraph::size_type from,
311                                       LyXParagraph::size_type to,
312                                       LyXParagraph::size_type offset) const
313 {
314         LyXParagraph::size_type vpos, old_lpos, stack[2];
315         LyXDirection par_direction = row->par->getParDirection();
316         LyXDirection direction = par_direction;
317         LyXParagraph::size_type lpos = from;
318         int level = 0;
319
320         while (lpos <= to) {
321                 if (row->par->getLetterDirection(lpos) == direction) {
322                         log2vis_list[lpos - bidi_start] = direction;
323                         ++lpos;
324                 } else {
325                         if (level == 0 ||
326                             (level == 1 && direction == LYX_DIR_RIGHT_TO_LEFT
327                              && row->par->getFont(lpos).direction() ==
328                              LyXFont::RTL_DIR
329                              && row->par->getFont(lpos).latex() ==
330                              LyXFont::ON ) ) {
331                                 // The last check is needed when the
332                                 // char is a space
333                                 stack[level++] = lpos;
334                         } else {
335                                 old_lpos = stack[--level];
336                                 log2vis_list[old_lpos - bidi_start] = 
337                                         log2vis_list[lpos - bidi_start] =
338                                         (old_lpos - lpos) * direction;
339                                 ++lpos;
340                         }
341                         direction = static_cast<LyXDirection>(-direction);
342                 }
343         }
344
345         while (level > 0) {
346                 old_lpos = stack[--level];
347                 log2vis_list[old_lpos - bidi_start] =
348                         (old_lpos - (to + 1)) * direction; 
349                 direction = static_cast<LyXDirection>(-direction);
350         }
351
352         vpos = (par_direction == LYX_DIR_LEFT_TO_RIGHT)
353                 ? from - 1 : to + 1;
354         vpos += offset;
355         for (lpos = from; lpos <= to; ++lpos) {
356                 vpos += log2vis_list[lpos - bidi_start];
357                 vis2log_list[vpos - bidi_start] = lpos;
358                 log2vis_list[lpos - bidi_start] = vpos;
359         }
360 }
361
362
363 #ifdef USE_PAINTER
364 void LyXText::draw(Row const * row,
365                    LyXParagraph::size_type & vpos,
366                    int offset, float & x)
367 {
368         Painter & pain = owner_->painter();
369         
370         LyXParagraph::size_type pos = vis2log(vpos);
371         char c = row->par->GetChar(pos);
372
373         if (IsNewlineChar(c)) {
374                 ++vpos;
375                 // Draw end-of-line marker
376                 LyXFont font = GetFont(row->par, pos);
377                 int wid = font.width('n');
378                 int asc = font.maxAscent();
379                 int y = offset + row->baseline;
380 #ifdef USE_PAINTER
381                 int xp[3], yp[3];
382                 
383                 if (row->par->getLetterDirection(pos) == LYX_DIR_LEFT_TO_RIGHT) {
384                         xp[0] = int(x + wid * 0.375);
385                         yp[0] = int(y - 0.875 * asc * 0.75);
386                         
387                         xp[1] = int(x);
388                         yp[1] = int(y - 0.500 * asc * 0.75);
389                         
390                         xp[2] = int(x + wid * 0.375);
391                         yp[2] = int(y - 0.125 * asc * 0.75);
392                         
393                         pain.lines(xp, yp, 3, LColor::eolmarker);
394                         
395                         xp[0] = int(x);
396                         yp[0] = int(y - 0.500 * asc * 0.75);
397                         
398                         xp[1] = int(x + wid);
399                         yp[1] = int(y - 0.500 * asc * 0.75);
400                         
401                         xp[2] = int(x + wid);
402                         yp[2] = int(y - asc * 0.75);
403                         
404                         pain.lines(xp, yp, 3, LColor::eolmarker);
405                 } else {
406                         xp[0] = int(x + wid * 0.625);
407                         yp[0] = int(y - 0.875 * asc * 0.75);
408                         
409                         xp[1] = int(x + wid);
410                         yp[1] = int(y - 0.500 * asc * 0.75);
411                         
412                         xp[2] = int(x + wid * 0.625);
413                         yp[2] = int(y - 0.125 * asc * 0.75);
414                         
415                         pain.lines(xp, yp, 3, LColor::eolmarker);
416                         
417                         xp[0] = int(x + wid);
418                         yp[0] = int(y - 0.500 * asc * 0.75);
419                         
420                         xp[1] = int(x);
421                         yp[1] = int(y - 0.500 * asc * 0.75);
422                         
423                         xp[2] = int(x);
424                         yp[2] = int(y - asc * 0.75);
425                         
426                         pain.lines(xp, yp, 3, LColor::eolmarker);
427                 }
428 #else
429                 XPoint p[3];
430                 
431                 if (row->par->getLetterDirection(pos) == LYX_DIR_LEFT_TO_RIGHT) {
432                         p[0].x = int(x + wid*0.375); p[0].y = int(y - 0.875*asc*0.75);
433                         p[1].x = int(x);             p[1].y = int(y - 0.500*asc*0.75);
434                         p[2].x = int(x + wid*0.375); p[2].y = int(y - 0.125*asc*0.75);
435                         scr.drawLines(::getGC(gc_new_line), p, 3);
436                 
437                         p[0].x = int(x);             p[0].y = int(y - 0.500*asc*0.75);
438                         p[1].x = int(x + wid);       p[1].y = int(y - 0.500*asc*0.75);
439                         p[2].x = int(x + wid);       p[2].y = int(y - asc*0.75);
440                         scr.drawLines(::getGC(gc_new_line), p, 3);
441                 } else {
442                         p[0].x = int(x + wid*0.625); p[0].y = int(y - 0.875*asc*0.75);
443                         p[1].x = int(x + wid);       p[1].y = int(y - 0.500*asc*0.75);
444                         p[2].x = int(x + wid*0.625); p[2].y = int(y - 0.125*asc*0.75);
445                         scr.drawLines(::getGC(gc_new_line), p, 3);
446                 
447                         p[0].x = int(x + wid);       p[0].y = int(y - 0.500*asc*0.75);
448                         p[1].x = int(x);             p[1].y = int(y - 0.500*asc*0.75);
449                         p[2].x = int(x);             p[2].y = int(y - asc*0.75);
450                         scr.drawLines(::getGC(gc_new_line), p, 3);
451                 }
452 #endif
453                 x += wid;
454                 return;
455         }
456
457         LyXFont font = GetFont(row->par, pos);
458         LyXFont font2 = font;
459
460         if (c == LyXParagraph::META_FOOTNOTE
461             || c == LyXParagraph::META_MARGIN
462             || c == LyXParagraph::META_FIG
463             || c == LyXParagraph::META_TAB
464             || c == LyXParagraph::META_WIDE_FIG
465             || c == LyXParagraph::META_WIDE_TAB
466             || c == LyXParagraph::META_ALGORITHM) {
467                 string fs;
468                 switch (c) {
469                 case LyXParagraph::META_MARGIN:
470                         fs = "margin";
471                         // Draw a sign at the left margin!
472                         owner_->painter()
473                                 .text((LYX_PAPER_MARGIN - font.width('!'))/2,
474                                       offset + row->baseline, "!", 1, font);
475                         break;
476                 case LyXParagraph::META_FIG:
477                         fs = "fig";
478                         break;
479                 case LyXParagraph::META_TAB:
480                         fs = "tab";
481                         break;
482                 case LyXParagraph::META_ALGORITHM:
483                         fs = "alg";
484                         break;
485                 case LyXParagraph::META_WIDE_FIG:
486                         fs = "wide-fig";
487                         break;
488                 case LyXParagraph::META_WIDE_TAB:
489                         fs = "wide-tab";
490                         break;
491                 case LyXParagraph::META_FOOTNOTE:
492                         fs = "foot";
493                         break;
494                 }
495                 font.decSize();
496                 font.decSize();
497           
498                 // calculate the position of the footnotemark
499                 int y = (row->baseline - font2.maxAscent() 
500                          + font.maxAscent());
501           
502                 font.setColor(LColor::footnote);
503
504                 float tmpx = x;
505
506                 // draw it and set new x position
507                 
508                 pain.text(int(x), offset + y, fs, font);
509                 x += pain.width(fs, font);
510                 pain.line(int(tmpx), offset + row->baseline,
511                           int(x), offset + row->baseline,
512                           LColor::footnote);
513
514           
515                 ++vpos;
516                 return;
517         } else if (c == LyXParagraph::META_INSET) {
518                 Inset * tmpinset = row->par->GetInset(pos);
519                 if (tmpinset) 
520                         tmpinset->draw(owner_->painter(), font,
521                                        offset + row->baseline, x);
522                 ++vpos;
523                 return;
524         }
525
526         /* usual characters, no insets */
527
528         // Collect character that we can draw in one command
529
530         // This is dirty, but fast. Notice that it will never be too small.
531         // For the record, I'll note that Microsoft Word has a limit
532         // of 768 here. We have none :-) (Asger)
533         // Ok. I am the first to admit that the use of std::string will be
534         // a tiny bit slower than using a POD char array. However, I claim
535         // that this slowdown is so small that it is close to inperceptive.
536         // So IMHO we should go with the easier and clearer implementation.
537         // And even if 1024 is a large number here it might overflow, string
538         // will only overflow if the machine is out of memory...
539         static string textstring;
540         textstring = c;
541         ++vpos;
542
543         LyXParagraph::size_type last = RowLast(row);
544         
545         while (vpos <= last &&
546                (pos = vis2log(vpos)) >= 0
547                && static_cast<unsigned char>(c = row->par->GetChar(pos)) > ' '
548                && font2 == GetFont(row->par, pos)) {
549                 textstring += c;
550                 ++vpos;
551         }
552         float tmpx = x;
553
554 #if 0
555         // If monochrome and LaTeX mode, provide reverse background
556         if (mono_video &&
557             font.latex() == LyXFont::ON) {
558                 int a = font.maxAscent();
559                 int d = font.maxDescent();
560                 scr.fillRectangle(gc_copy, int(tmpx),
561                                   offset + row->baseline - a,
562                                   font.textWidth(textstring.c_str(),
563                                                  textstring.length()), a + d);
564         }
565 #endif
566
567         // Draw text and set the new x position
568         pain.text(int(x), offset + row->baseline, textstring, font);
569         x += pain.width(textstring, font);
570         
571         // what about underbars?
572         if (font.underbar() == LyXFont::ON && font.latex() != LyXFont::ON) {
573                 pain.line(tmpx, offset + row->baseline + 2,
574                           x, offset + row->baseline + 2);
575                 
576         }
577
578         // If we want ulem.sty support, drawing
579         // routines should go here. (Asger)
580         // Why shouldn't LyXFont::drawText handle it internally?
581 }
582 #else
583 void LyXText::Draw(Row const * row, LyXParagraph::size_type & vpos,
584                    LyXScreen & scr, int offset, float & x)
585 {
586         LyXParagraph::size_type pos = vis2log(vpos);
587         char c = row->par->GetChar(pos);
588
589         if (IsNewlineChar(c)) {
590                 ++vpos;
591                 // Draw end-of-line marker
592
593                 LyXFont font = GetFont(row->par, pos);
594                 int asc = font.maxAscent();
595                 int wid = font.width('n');
596                 int y = (offset + row->baseline);
597                 XPoint p[3];
598                 if (row->par->getLetterDirection(pos) == LYX_DIR_LEFT_TO_RIGHT) {
599                         p[0].x = int(x + wid*0.375); p[0].y = int(y - 0.875*asc*0.75);
600                         p[1].x = int(x);             p[1].y = int(y - 0.500*asc*0.75);
601                         p[2].x = int(x + wid*0.375); p[2].y = int(y - 0.125*asc*0.75);
602                         scr.drawLines(::getGC(gc_new_line), p, 3);
603                 
604                         p[0].x = int(x);             p[0].y = int(y - 0.500*asc*0.75);
605                         p[1].x = int(x + wid);       p[1].y = int(y - 0.500*asc*0.75);
606                         p[2].x = int(x + wid);       p[2].y = int(y - asc*0.75);
607                         scr.drawLines(::getGC(gc_new_line), p, 3);
608                 } else {
609                         p[0].x = int(x + wid*0.625); p[0].y = int(y - 0.875*asc*0.75);
610                         p[1].x = int(x + wid);       p[1].y = int(y - 0.500*asc*0.75);
611                         p[2].x = int(x + wid*0.625); p[2].y = int(y - 0.125*asc*0.75);
612                         scr.drawLines(::getGC(gc_new_line), p, 3);
613                 
614                         p[0].x = int(x + wid);       p[0].y = int(y - 0.500*asc*0.75);
615                         p[1].x = int(x);             p[1].y = int(y - 0.500*asc*0.75);
616                         p[2].x = int(x);             p[2].y = int(y - asc*0.75);
617                         scr.drawLines(::getGC(gc_new_line), p, 3);
618                 }
619                 x += wid;
620                 return;
621         }
622
623         LyXFont font = GetFont(row->par, pos);
624         LyXFont font2 = font;
625
626         if (c == LyXParagraph::META_FOOTNOTE
627             || c == LyXParagraph::META_MARGIN
628             || c == LyXParagraph::META_FIG
629             || c == LyXParagraph::META_TAB
630             || c == LyXParagraph::META_WIDE_FIG
631             || c == LyXParagraph::META_WIDE_TAB
632             || c == LyXParagraph::META_ALGORITHM) {
633                 string fs;
634                 switch (c) {
635                 case LyXParagraph::META_MARGIN:
636                         fs = "margin";
637                         // Draw a sign at the left margin! 
638                         scr.drawText(font, "!", 1, offset + row->baseline,
639                                      (LYX_PAPER_MARGIN - font.width('!'))/2);
640                         break;
641                 case LyXParagraph::META_FIG:
642                         fs = "fig";
643                         break;
644                 case LyXParagraph::META_TAB:
645                         fs = "tab";
646                         break;
647                 case LyXParagraph::META_ALGORITHM:
648                         fs = "alg";
649                         break;
650                 case LyXParagraph::META_WIDE_FIG:
651                         fs = "wide-fig";
652                         break;
653                 case LyXParagraph::META_WIDE_TAB:
654                         fs = "wide-tab";
655                         break;
656                 case LyXParagraph::META_FOOTNOTE:
657                         fs = "foot";
658                         break;
659                 }
660                 font.decSize();
661                 font.decSize();
662           
663                 // calculate the position of the footnotemark
664                 int y = (row->baseline - font2.maxAscent() 
665                          + font.maxAscent());
666
667 #ifdef USE_PAINTER
668                 font.setColor(LColor::footnote);
669 #else
670                 font.setColor(LyXFont::RED);
671 #endif
672
673                 float tmpx = x;
674
675                 // draw it and set new x position
676                 x += scr.drawString(font, fs, offset + y, int(x));
677
678                 scr.drawLine(gc_foot, offset + row->baseline,
679                              int(tmpx), int(x - tmpx));
680           
681                 ++vpos;
682                 return;
683         } else if (c == LyXParagraph::META_INSET) {
684                 Inset * tmpinset = row->par->GetInset(pos);
685                 if (tmpinset) 
686                         tmpinset->Draw(font, scr, offset + row->baseline, x);
687                 ++vpos;
688                 return;
689         }
690
691         /* usual characters, no insets */
692
693         // Collect character that we can draw in one command
694
695         // This is dirty, but fast. Notice that it will never be too small.
696         // For the record, I'll note that Microsoft Word has a limit
697         // of 768 here. We have none :-) (Asger)
698         // Ok. I am the first to admit that the use of std::string will be
699         // a tiny bit slower than using a POD char array. However, I claim
700         // that this slowdown is so small that it is close to inperceptive.
701         // So IMHO we should go with the easier and clearer implementation.
702         // And even if 1024 is a large number here it might overflow, string
703         // will only overflow if the machine is out of memory...
704         static string textstring;
705         textstring = c;
706         ++vpos;
707
708         LyXParagraph::size_type last = RowLast(row);
709         
710         while (vpos <= last &&
711                (pos = vis2log(vpos)) >= 0
712                && static_cast<unsigned char>(c = row->par->GetChar(pos)) > ' '
713                && font2 == GetFont(row->par, pos)) {
714                 textstring += c;
715                 ++vpos;
716         }
717         float tmpx = x;
718
719         // If monochrome and LaTeX mode, provide reverse background
720         if (mono_video &&
721             font.latex() == LyXFont::ON) {
722                 int a = font.maxAscent();
723                 int d = font.maxDescent();
724                 scr.fillRectangle(gc_copy, int(tmpx),
725                                   offset + row->baseline - a,
726                                   font.textWidth(textstring.c_str(),
727                                                  textstring.length()), a + d);
728         }
729
730         // Draw text and set the new x position
731         x += scr.drawText(font, textstring.c_str(), textstring.length(),
732                           offset + row->baseline, 
733                           int(x));
734         
735         // what about underbars?
736         if (font.underbar() == LyXFont::ON && font.latex() != LyXFont::ON) {
737                 scr.drawLine(gc_copy, offset + row->baseline + 2,
738                              int(tmpx), int(x - tmpx));
739         }
740
741         // If we want ulem.sty support, drawing
742         // routines should go here. (Asger)
743         // Why shouldn't LyXFont::drawText handle it internally?
744 }
745 #endif
746
747
748 // Returns the left beginning of the text. 
749 // This information cannot be taken from the layouts-objekt, because in 
750 // LaTeX the beginning of the text fits in some cases (for example sections)
751 // exactly the label-width.
752 int LyXText::LeftMargin(Row const * row) const
753 {
754         LyXLayout const & layout = textclasslist.Style(parameters->textclass,
755                                                        row->par->GetLayout());
756         
757         string parindent = layout.parindent; 
758         
759         /* table stuff -- begin */ 
760         if (row->par->table)
761                 parindent.clear();
762         /* table stuff -- end */
763         
764         int x = LYX_PAPER_MARGIN;
765         
766         x += textclasslist.TextClass(parameters->textclass)
767                 .defaultfont()
768                 .signedStringWidth(textclasslist
769                                    .TextClass(parameters->textclass)
770                                    .leftmargin());
771         
772         if (row->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)  {
773                 LyXFont font(LyXFont::ALL_SANE);
774                 font.setSize(LyXFont::SIZE_SMALL);
775                 x += font.textWidth("Mwide-figM", 10) + LYX_PAPER_MARGIN/2;
776         }
777         
778         // this is the way, LyX handles the LaTeX-Environments.
779         // I have had this idea very late, so it seems to be a
780         // later added hack and this is true
781         if (!row->par->GetDepth()) {
782                 if (!row->par->GetLayout()) {
783                         // find the previous same level paragraph
784                         if (row->par->FirstPhysicalPar()->Previous()) {
785                                 LyXParagraph * newpar = row->par
786                                         ->DepthHook(row->par->GetDepth());
787                                 if (newpar &&
788                                     textclasslist.Style(parameters->textclass,
789                                                         newpar->GetLayout())
790                                     .nextnoindent)
791                                         parindent.clear();
792                         }
793                 }
794         } else {
795                 // find the next level paragraph
796                 
797                 LyXParagraph * newpar = row->par->DepthHook(row->par->GetDepth()-1);
798                 
799                 // make a corresponding row. Needed to call LeftMargin()
800                 
801                 // check wether it is a sufficent paragraph 
802                 if (newpar && newpar->footnoteflag == row->par->footnoteflag
803                     && textclasslist
804                         .Style(parameters->textclass, 
805                                newpar->GetLayout()).isEnvironment()) {
806                         Row dummyrow;
807                         dummyrow.par = newpar;
808                         dummyrow.pos = newpar->Last();
809                         x = LeftMargin(&dummyrow);
810                 } else {
811                         // this is no longer an error, because this function
812                         // is used to clear impossible depths after changing
813                         // a layout. Since there is always a redo,
814                         // LeftMargin() is always called
815                         row->par->FirstPhysicalPar()->depth = 0;
816                 }
817                 
818                 if (newpar && !row->par->GetLayout()) {
819                         if (newpar->FirstPhysicalPar()->noindent)
820                                 parindent.clear();
821                         else
822                                 parindent = textclasslist
823                                         .Style(parameters->textclass, 
824                                                newpar->GetLayout()).parindent;
825                 }
826                 
827         }
828         
829         LyXFont labelfont = GetFont(row->par, -2);
830         switch (layout.margintype) {
831         case MARGIN_DYNAMIC:
832                 if (!layout.leftmargin.empty()) {
833                         x += textclasslist
834                                 .TextClass(parameters->textclass)
835                                 .defaultfont()
836                                 .signedStringWidth(layout.leftmargin);
837                 }
838                 if (!row->par->GetLabestring().empty()) {
839                         x += labelfont.signedStringWidth(layout.labelindent);
840                         x += labelfont.stringWidth(row->par->GetLabestring());
841                         x += labelfont.stringWidth(layout.labelsep);
842                 }
843                 break;
844         case MARGIN_MANUAL:
845                 x += labelfont.signedStringWidth(layout.labelindent);
846                 if (row->pos >= BeginningOfMainBody(row->par)) {
847                         if (!row->par->GetLabelWidthString().empty()) {
848                                 x += labelfont
849                                         .stringWidth(row->par
850                                                      ->GetLabelWidthString());
851                                 x += labelfont.stringWidth(layout.labelsep);
852                         }
853                 }
854                 break;
855         case MARGIN_STATIC:
856                 x += textclasslist.TextClass(parameters->textclass)
857                         .defaultfont().signedStringWidth(layout.leftmargin) * 4
858                         / (row->par->GetDepth() + 4);
859                 break;
860         case MARGIN_FIRST_DYNAMIC:
861                 if (layout.labeltype == LABEL_MANUAL) {
862                         if (row->pos >= BeginningOfMainBody(row->par)) {
863                                 x += labelfont
864                                         .signedStringWidth(layout.leftmargin);
865                         } else {
866                                 x += labelfont
867                                         .signedStringWidth(layout.labelindent);
868                         }
869                 } else if (row->pos
870                            // Special case to fix problems with
871                            // theorems (JMarc)
872                            || (layout.labeltype == LABEL_STATIC
873                                && layout.latextype == LATEX_ENVIRONMENT
874                                && ! row->par->IsFirstInSequence())) {
875                         x += labelfont.signedStringWidth(layout.leftmargin);
876                 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
877                            && layout.labeltype != LABEL_BIBLIO
878                            && layout.labeltype !=
879                            LABEL_CENTERED_TOP_ENVIRONMENT) {
880                         x += labelfont.signedStringWidth(layout.labelindent);
881                         x += labelfont.stringWidth(layout.labelsep);
882                         x += labelfont.stringWidth(row->par->GetLabestring());
883                 } 
884                 break;
885                 
886         case MARGIN_RIGHT_ADDRESS_BOX:
887         {
888                 // ok, a terrible hack. The left margin depends on the widest
889                 // row in this paragraph. Do not care about footnotes, they
890                 // are *NOT* allowed in the LaTeX realisation of this layout.
891                 
892                 // find the first row of this paragraph
893                 Row const * tmprow = row;
894                 while (tmprow->previous && tmprow->previous->par == row->par)
895                         tmprow = tmprow->previous;
896                 
897                 int minfill = tmprow->fill;
898                 while (tmprow->next && tmprow->next->par == row->par) {
899                         tmprow = tmprow->next;
900                         if (tmprow->fill < minfill)
901                                 minfill = tmprow->fill;
902                 }
903                 
904                 x += textclasslist.TextClass(parameters->textclass)
905                         .defaultfont().signedStringWidth(layout.leftmargin);
906                 x += minfill;
907         }
908         break;
909         }
910         if (row->par->pextra_type == LyXParagraph::PEXTRA_INDENT) {
911                 if (!row->par->pextra_widthp.empty()) {
912                         x += paperwidth *
913                                 atoi(row->par->pextra_widthp.c_str()) / 100;
914                 } else if (!row->par->pextra_width.empty()) {
915                         int xx = VSpace(row->par->pextra_width).inPixels();
916                         
917                         if (xx > paperwidth)
918                                 xx = paperwidth * 80 / 100;
919                         x += xx;
920                 } else { // should not happen
921                         LyXFont font(LyXFont::ALL_SANE);
922                         x += font.stringWidth("XXXXXX");
923                 }
924         }
925         
926         int align;
927         if (row->par->FirstPhysicalPar()->align == LYX_ALIGN_LAYOUT)
928                 align = layout.align;
929         else
930                 align = row->par->FirstPhysicalPar()->align;
931         
932         // set the correct parindent
933         if (row->pos == 0) {
934                 if ((layout.labeltype == LABEL_NO_LABEL 
935                      || layout.labeltype == LABEL_TOP_ENVIRONMENT 
936                      || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
937                      || (layout.labeltype == LABEL_STATIC
938                          && layout.latextype == LATEX_ENVIRONMENT
939                          && ! row->par->IsFirstInSequence()))
940                     && row->par == row->par->FirstPhysicalPar()
941                     && align == LYX_ALIGN_BLOCK
942                     && !row->par->noindent
943                     && (row->par->layout ||
944                         parameters->paragraph_separation ==
945                         BufferParams::PARSEP_INDENT))
946                         x += textclasslist.TextClass(parameters->textclass)
947                                 .defaultfont().signedStringWidth(parindent);
948                 else 
949                         if (layout.labeltype == LABEL_BIBLIO) {
950                                 // ale970405 Right width for bibitems
951 #ifdef USE_PAINTER
952                                 x += bibitemMaxWidth(owner_->painter(),
953                                                      textclasslist
954                                                      .TextClass(parameters
955                                                                 ->textclass)
956                                                      .defaultfont());
957 #else
958                                 x += bibitemMaxWidth(textclasslist
959                                                      .TextClass(parameters
960                                                                 ->textclass)
961                                                      .defaultfont());
962 #endif
963                         }
964         }
965         
966         return x;
967 }
968     
969    
970 int LyXText::RightMargin(Row const * row) const
971 {
972         LyXLayout const & layout =
973                 textclasslist.Style(parameters->textclass,
974                                     row->par->GetLayout());
975         
976         int x = LYX_PAPER_MARGIN
977                 + textclasslist
978                 .TextClass(parameters->textclass)
979                 .defaultfont()
980                 .signedStringWidth(textclasslist
981                                    .TextClass(parameters->textclass)
982                                    .rightmargin());
983         
984         if (row->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)  {
985                 x += LYX_PAPER_MARGIN / 2;
986         }
987         
988         // this is the way, LyX handles the LaTeX-Environments.
989         // I have had this idea very late, so it seems to be a
990         // later added hack and this is true
991         if (row->par->GetDepth()) {
992                 // find the next level paragraph
993                 
994                 LyXParagraph * newpar = row->par;
995                 
996                 do {
997                         newpar = newpar->FirstPhysicalPar()->Previous();
998                         if (newpar) 
999                                 newpar = newpar->FirstPhysicalPar();
1000                 } while (newpar && newpar->GetDepth() >= row->par->GetDepth()
1001                          && newpar->footnoteflag == row->par->footnoteflag);
1002                 
1003                 // make a corresponding row. Needed to call LeftMargin()
1004                 
1005                 // check wether it is a sufficent paragraph
1006                 if (newpar && newpar->footnoteflag == row->par->footnoteflag
1007                     && textclasslist.Style(parameters->textclass,
1008                                            newpar->GetLayout())
1009                        .isEnvironment()) {
1010                         Row dummyrow;
1011                         dummyrow.par = newpar;
1012                         dummyrow.pos = 0;
1013                         x = RightMargin(&dummyrow);
1014                 } else {
1015                         // this is no longer an error, because this function
1016                         // is used to clear impossible depths after changing
1017                         // a layout. Since there is always a redo,
1018                         // LeftMargin() is always called
1019                         row->par->FirstPhysicalPar()->depth = 0;
1020                 }
1021         }
1022         
1023         //lyxerr << "rightmargin: " << layout->rightmargin << endl;
1024         x += textclasslist.TextClass(parameters->textclass)
1025                 .defaultfont()
1026                 .signedStringWidth(layout.rightmargin) * 4
1027               / (row->par->GetDepth() + 4);
1028         return x;
1029 }
1030
1031
1032 int LyXText::LabelEnd (Row const * row) const
1033 {
1034         if (textclasslist.Style(parameters->textclass,
1035                                 row->par->GetLayout()).margintype
1036             == MARGIN_MANUAL) {
1037                 Row tmprow;
1038                 tmprow = *row;
1039                 tmprow.pos = row->par->Last();
1040                 return LeftMargin(&tmprow);  /* just the beginning 
1041                                                 of the main body */
1042         } else
1043                 return 0;  /* LabelEnd is only needed, if the  
1044                               layout fills a flushleft
1045                               label. */
1046 }
1047
1048
1049 /* table stuff -- begin*/
1050 int LyXText::NumberOfCell(LyXParagraph * par,
1051                           LyXParagraph::size_type pos) const
1052 {
1053    int cell = 0;
1054    LyXParagraph::size_type tmp_pos = 0;
1055    while (tmp_pos < pos) {
1056       if (par->IsNewline(tmp_pos))
1057          ++cell;
1058       ++tmp_pos;
1059    }
1060    return cell;
1061 }
1062
1063
1064 int LyXText::WidthOfCell(LyXParagraph * par,
1065                          LyXParagraph::size_type & pos) const
1066 {
1067    int w = 0;
1068    while (pos < par->Last() && !par->IsNewline(pos)) {
1069       w += SingleWidth(par, pos);
1070       ++pos;
1071    }
1072    if (par->IsNewline(pos))
1073       ++pos;
1074    return w;
1075 }
1076
1077
1078 bool LyXText::HitInTable(Row * row, int x) const
1079 {
1080         float tmpx;
1081         float fill_separator, fill_hfill, fill_label_hfill;
1082         if (!row->par->table)
1083                 return false;
1084         PrepareToPrint(row, tmpx, fill_separator,
1085                        fill_hfill, fill_label_hfill, false);
1086         return (x > tmpx && x < tmpx + row->par->table->WidthOfTable());
1087 }
1088
1089
1090 bool LyXText::MouseHitInTable(int x, long y) const
1091 {
1092         Row * row = GetRowNearY(y);
1093         return HitInTable(row, x);
1094 }
1095
1096
1097 /* table stuff -- end*/
1098
1099
1100 // get the next breakpoint in a given paragraph
1101 LyXParagraph::size_type
1102 LyXText::NextBreakPoint(Row const * row, int width) const
1103 {
1104         LyXParagraph * par = row->par;
1105         LyXParagraph::size_type pos = row->pos;
1106         
1107         /* table stuff -- begin*/ 
1108         if (par->table) {
1109                 while (pos < par->size()
1110                        && (!par->IsNewline(pos) 
1111                            || !par->table->IsFirstCell(NumberOfCell(par, pos+1)))) {
1112                         if (par->GetChar(pos) == LyXParagraph::META_INSET &&
1113                             par->GetInset(pos) && par->GetInset(pos)->display()){
1114                                 par->GetInset(pos)->display(false);
1115                         }
1116                         ++pos;
1117                 }
1118                 return pos;
1119         }
1120         /* table stuff -- end*/ 
1121         
1122         // position of the last possible breakpoint 
1123         // -1 isn't a suitable value, but a flag
1124         LyXParagraph::size_type last_separator = -1;
1125         int left_margin = LabelEnd(row);
1126         width -= RightMargin(row);
1127         
1128         LyXParagraph::size_type main_body = BeginningOfMainBody(par);
1129         LyXLayout const & layout =
1130                 textclasslist.Style(parameters->textclass, par->GetLayout());
1131         LyXParagraph::size_type i = pos;
1132
1133         if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1134                 /* special code for right address boxes, only newlines count */
1135                 while (i < par->Last()) {
1136                         if (par->IsNewline(i)) {
1137                                 last_separator = i;
1138                                 i = par->Last() - 1; // this means break
1139                                 //x = width;
1140                         } else if (par->GetChar(i) == LyXParagraph::META_INSET &&
1141                                    par->GetInset(i) && par->GetInset(i)->display()){
1142                                 par->GetInset(i)->display(false);
1143                         }
1144                         ++i;
1145                 }
1146         } else {
1147                 // Last position is an invariant
1148                 LyXParagraph::size_type const last = 
1149                         par->Last();
1150                 // this is the usual handling
1151                 int x = LeftMargin(row);
1152                 while (x < width && i < last) {
1153                         char c = par->GetChar(i);
1154                         if (IsNewlineChar(c)) {
1155                                 last_separator = i;
1156                                 x = width; // this means break
1157                         } else if (c == LyXParagraph::META_INSET &&
1158                                    par->GetInset(i) && par->GetInset(i)->display()){
1159                                 // check wether a Display() inset is
1160                                 // valid here. if not, change it to
1161                                 // non-display
1162                                 if (layout.isCommand()
1163                                     || (layout.labeltype == LABEL_MANUAL
1164                                         && i < BeginningOfMainBody(par))){
1165                                         // display istn't allowd
1166                                         par->GetInset(i)->display(false);
1167                                         x += SingleWidth(par, i, c);
1168                                 } else {
1169                                         // inset is display. So break the line here
1170                                         if (i == pos){
1171                                                 if (pos < last-1) {
1172                                                         last_separator = i;
1173                                                         if (IsLineSeparatorChar(par->GetChar(i+1)))
1174                                                                 ++last_separator;
1175                                                 } else
1176                                                         last_separator = last; // to avoid extra rows
1177                                         } else
1178                                                 last_separator = i - 1;
1179                                         x = width;  // this means break
1180                                 }
1181                         } else  {
1182                                 if (IsLineSeparatorChar(c))
1183                                         last_separator = i;
1184                                 x += SingleWidth(par, i, c);
1185                         }
1186                         ++i;
1187                         if (i == main_body) {
1188                                 x += GetFont(par, -2).stringWidth(layout.labelsep);
1189                                 if (par->IsLineSeparator(i - 1))
1190                                         x-= SingleWidth(par, i - 1);
1191                                 if (x < left_margin)
1192                                         x = left_margin;
1193                         }
1194                 }
1195                 // end of paragraph is always a suitable separator
1196                 if (i == last && x < width)
1197                         last_separator = i;
1198         }
1199         
1200         // well, if last_separator is still 0, the line isn't breakable. 
1201         // don't care and cut simply at the end
1202         if (last_separator < 0) {
1203                 last_separator = i;
1204         }
1205         
1206         // manual labels cannot be broken in LaTeX, do not care
1207         if (main_body && last_separator < main_body)
1208                 last_separator = main_body - 1;
1209         
1210         return last_separator;
1211 }
1212
1213
1214 // returns the minimum space a row needs on the screen in pixel
1215 int LyXText::Fill(Row const * row, int paper_width) const
1216 {
1217         int w, fill;
1218         // get the pure distance
1219         LyXParagraph::size_type last = RowLast(row);
1220         /* table stuff -- begin */
1221         if (row->par->table) {
1222                 // for tables FILL does calculate the widthes of each cell in 
1223                 // the row
1224                 LyXParagraph::size_type pos = row->pos;
1225                 int cell = NumberOfCell(row->par, pos);
1226                 w = 0;
1227                 do {
1228                         row->par->table->SetWidthOfCell(cell,
1229                                                         WidthOfCell(row->par,
1230                                                                     pos));
1231                         ++cell;
1232                 } while (pos <= last && !row->par->table->IsFirstCell(cell));
1233                 // don't forget the very last table cell without characters
1234                 if (cell == row->par->table->GetNumberOfCells() - 1)
1235                         row->par->table->SetWidthOfCell(cell,
1236                                                         WidthOfCell(row->par,
1237                                                                     pos));
1238                 
1239                 return 0; /* width of table cannot be returned since
1240                            * we cannot guarantee its correct value at
1241                            * this point. */ 
1242         }
1243         /* table stuff -- end*/ 
1244         
1245         int left_margin = LabelEnd(row);
1246         
1247         // if the row ends with newline, this newline will not be relevant
1248         //if (last >= 0 && row->par->IsNewline(last))
1249         //      --last;
1250         
1251         // if the row ends with a space, this space will not be relevant
1252         if (last >= 0 && row->par->IsLineSeparator(last))
1253                 --last;
1254         
1255         // special handling of the right address boxes
1256         if (textclasslist.Style(parameters->textclass,
1257                                 row->par->GetLayout()).margintype
1258             == MARGIN_RIGHT_ADDRESS_BOX) {
1259                 int tmpfill = row->fill;
1260                 row->fill = 0; // the minfill in MarginLeft()
1261                 w = LeftMargin(row);
1262                 row->fill = tmpfill;
1263         } else
1264                 w = LeftMargin(row);
1265         
1266         LyXLayout const & layout = textclasslist.Style(parameters->textclass,
1267                                                        row->par->GetLayout());
1268         LyXParagraph::size_type main_body = 
1269                 BeginningOfMainBody(row->par);
1270         LyXParagraph::size_type i = row->pos;
1271
1272         while (i <= last) {
1273                 if (main_body > 0 && i == main_body) {
1274                         w += GetFont(row->par, -2).
1275                                 stringWidth(layout.labelsep);
1276                         if (row->par->IsLineSeparator(i - 1))
1277                                 w -= SingleWidth(row->par, i - 1);
1278                         if (w < left_margin)
1279                                 w = left_margin;
1280                 }
1281                 w += SingleWidth(row->par, i);
1282                 ++i;
1283         }
1284         if (main_body > 0 && main_body > last) {
1285                 w += GetFont(row->par, -2).stringWidth(layout.labelsep);
1286                 if (last >= 0 && row->par->IsLineSeparator(last))
1287                         w -= SingleWidth(row->par, last);
1288                 if (w < left_margin)
1289                         w = left_margin;
1290         }
1291         
1292         fill = paper_width - w - RightMargin(row);
1293         return fill;
1294 }
1295
1296
1297 // returns the minimum space a manual label needs on the screen in pixel
1298 int LyXText::LabelFill(Row const * row) const
1299 {
1300         LyXParagraph::size_type last = BeginningOfMainBody(row->par) - 1;
1301         // -1 because a label ends either with a space that is in the label, 
1302         // or with the beginning of a footnote that is outside the label.
1303
1304         // I don't understand this code in depth, but sometimes "last" is
1305         // less than 0 and this causes a crash. This fix seems to work
1306         // correctly, but I bet the real error is elsewhere.  The bug is
1307         // triggered when you have an open footnote in a paragraph
1308         // environment with a manual label. (Asger)
1309         if (last < 0) last = 0;
1310         
1311         if (row->par->IsLineSeparator(last)) /* a sepearator at this end 
1312                                                 does not count */
1313                 --last;
1314         
1315         int w = 0;
1316         int i = row->pos;
1317         while (i <= last) {
1318                 w += SingleWidth(row->par, i);
1319                 ++i;
1320         }
1321         
1322         int fill = 0;
1323         if (!row->par->labelwidthstring.empty()) {
1324                 fill = GetFont(row->par, -2)
1325                         .stringWidth(row->par->labelwidthstring) - w;
1326         }
1327         
1328         if (fill < 0)
1329                 fill = 0;
1330         
1331         return fill;
1332 }
1333
1334
1335 // returns the number of separators in the specified row. The separator 
1336 // on the very last column doesnt count
1337 int LyXText::NumberOfSeparators(Row const * row) const
1338 {
1339         int last = RowLast(row);
1340         //int p = row->pos;
1341         //int main_body = BeginningOfMainBody(row->par);
1342         //if (p < main_body)
1343         //      p = main_body;
1344         // I think this is equivalent to the above. (Lgb)
1345         int p = max(row->pos, BeginningOfMainBody(row->par));
1346         int n = 0;
1347         for (; p < last; ++p) {
1348                 if (row->par->IsSeparator(p)) {
1349                         ++n;
1350                 }
1351         }
1352         return n;
1353 }
1354
1355
1356 // returns the number of hfills in the specified row. The LyX-Hfill is
1357 // a LaTeX \hfill so that the hfills at the beginning and at the end were 
1358 // ignored. This is *MUCH* more usefull than not to ignore!
1359 int LyXText::NumberOfHfills(Row const * row) const
1360 {
1361         int last = RowLast(row);
1362         int first = row->pos;
1363         if (first) { /* hfill *DO* count at the beginning 
1364                       * of paragraphs! */
1365                 while(first <= last && row->par->IsHfill(first))
1366                         ++first;
1367         }
1368
1369         //int main_body = BeginningOfMainBody(row->par);
1370         //if (first < main_body)
1371         //      first = main_body;
1372         // I think this is equivalent to the above. (Lgb)
1373         first = max(first, BeginningOfMainBody(row->par));
1374         int n = 0;
1375         for (int p = first; p <= last; ++p) { // last, because the end is ignored!
1376                 if (row->par->IsHfill(p)) {
1377                         ++n;
1378                 }
1379         }
1380         return n;
1381 }
1382
1383
1384 // like NumberOfHfills, but only those in the manual label!
1385 int LyXText::NumberOfLabelHfills(Row const * row) const
1386 {
1387         LyXParagraph::size_type last = RowLast(row);
1388         LyXParagraph::size_type first = row->pos;
1389         if (first) { /* hfill *DO* count at the beginning 
1390                       * of paragraphs! */
1391                 while(first < last && row->par->IsHfill(first))
1392                         ++first;
1393         }
1394         //LyXParagraph::size_type main_body = 
1395         //BeginningOfMainBody(row->par);
1396         //if (last > main_body)
1397         //last = main_body;
1398         // I think this is eqvialent to the above. (Lgb)
1399         last = min(last, BeginningOfMainBody(row->par));
1400         int n = 0;
1401         for (LyXParagraph::size_type p = first;
1402              p < last; ++p) {  // last, because the end is ignored!
1403                 if (row->par->IsHfill(p)) {
1404                         ++n;
1405                 }
1406         }
1407         return n;
1408 }
1409
1410
1411 // returns true, if a expansion is needed.
1412 // Rules are given by LaTeX
1413 bool LyXText::HfillExpansion(Row const * row_ptr,
1414                              LyXParagraph::size_type pos) const
1415 {
1416         // by the way, is it a hfill?
1417         if (!row_ptr->par->IsHfill(pos))
1418                 return false;
1419         
1420         // at the end of a row it does not count
1421         if (pos >= RowLast(row_ptr))
1422                 return false;
1423         
1424         // at the beginning of a row it does not count, if it is not 
1425         // the first row of a paragaph
1426         if (!row_ptr->pos)
1427                 return true;
1428         
1429         // in some labels  it does not count
1430         if (textclasslist.Style(parameters->textclass,
1431                                 row_ptr->par->GetLayout()).margintype
1432             != MARGIN_MANUAL
1433             && pos < BeginningOfMainBody(row_ptr->par))
1434                 return false; 
1435         
1436         // if there is anything between the first char of the row and
1437         // the sepcified position that is not a newline and not a hfill,
1438         // the hfill will count, otherwise not
1439         LyXParagraph::size_type i = row_ptr->pos;
1440         while (i < pos && (row_ptr->par->IsNewline(i)
1441                            || row_ptr->par->IsHfill(i)))
1442                 ++i;
1443         
1444         return i != pos;
1445 }
1446
1447
1448 #ifdef USE_PAINTER
1449 void LyXText::SetHeightOfRow(Row * row_ptr) const
1450 {
1451     /* get the maximum ascent and the maximum descent */
1452    int asc, desc, pos;
1453    float layoutasc = 0;
1454    float layoutdesc = 0;
1455    float tmptop = 0;
1456    LyXFont tmpfont;
1457    Inset * tmpinset;
1458
1459    /* this must not happen before the currentrow for clear reasons.
1460       so the trick is just to set the current row onto this row */
1461    long unused_y;
1462    GetRow(row_ptr->par, row_ptr->pos, unused_y);
1463
1464    /* ok , let us initialize the maxasc and maxdesc value. 
1465     * This depends in LaTeX of the font of the last character
1466     * in the paragraph. The hack below is necessary because
1467     * of the possibility of open footnotes */
1468
1469    /* Correction: only the fontsize count. The other properties
1470       are taken from the layoutfont. Nicer on the screen :) */
1471    
1472    LyXParagraph * par = row_ptr->par->LastPhysicalPar();
1473    LyXParagraph * firstpar = row_ptr->par->FirstPhysicalPar();
1474    
1475    LyXLayout const & layout = textclasslist.Style(parameters->textclass,
1476                                                   firstpar->GetLayout());
1477    
1478    LyXFont font = GetFont(par, par->Last()-1);
1479    LyXFont::FONT_SIZE size = font.size();
1480    font = GetFont(par, -1);
1481    font.setSize(size);
1482
1483    LyXFont labelfont = GetFont(par, -2);
1484
1485    int maxasc = int(font.maxAscent() *
1486                    layout.spacing.getValue() *
1487                    parameters->spacing.getValue());
1488    int maxdesc = int(font.maxDescent() *
1489                     layout.spacing.getValue() *
1490                     parameters->spacing.getValue());
1491
1492    int pos_end = RowLast(row_ptr);
1493    
1494    int labeladdon = 0;
1495
1496    // Check if any insets are larger
1497    for (pos = row_ptr->pos; pos <= pos_end; ++pos) {
1498       if (row_ptr->par->GetChar(pos) == LyXParagraph::META_INSET) {
1499          tmpfont = GetFont(row_ptr->par, pos);
1500          tmpinset = row_ptr->par->GetInset(pos);
1501          if (tmpinset) {
1502             asc = tmpinset->ascent(owner_->painter(), tmpfont);
1503             desc = tmpinset->descent(owner_->painter(), tmpfont);
1504             if (asc > maxasc) 
1505               maxasc = asc;
1506             if (desc > maxdesc)
1507               maxdesc = desc;
1508          }
1509       }
1510    }
1511
1512    // Check if any custom fonts are larger (Asger)
1513    // This is not completely correct, but we can live with the small,
1514    // cosmetic error for now.
1515    LyXFont::FONT_SIZE maxsize = row_ptr->par->HighestFontInRange(row_ptr->pos,
1516                                                                  pos_end);
1517    if (maxsize > font.size()) {
1518         font.setSize(maxsize);
1519
1520         asc = font.maxAscent();
1521         desc = font.maxDescent();
1522         if (asc > maxasc) 
1523                 maxasc = asc;
1524         if (desc > maxdesc)
1525                 maxdesc = desc;
1526    }
1527
1528    /* table stuff -- begin*/
1529    if (row_ptr->par->table){
1530      // stretch the rows a bit
1531       maxasc += 1;
1532       maxdesc += 1;
1533    }
1534    /* table stuff -- end*/
1535
1536    // This is nicer with box insets:
1537    ++maxasc;
1538    ++maxdesc;
1539
1540    row_ptr->ascent_of_text = maxasc;
1541    
1542    /* is it a top line? */ 
1543    if (row_ptr->pos == 0
1544        && row_ptr->par == firstpar) {
1545       
1546       /* some parksips VERY EASY IMPLEMENTATION */ 
1547       if (parameters->paragraph_separation == BufferParams::PARSEP_SKIP) {
1548          if (layout.isParagraph()
1549              && firstpar->GetDepth() == 0
1550              && firstpar->Previous())
1551             maxasc += parameters->getDefSkip().inPixels();
1552          else if (firstpar->Previous()
1553                   && textclasslist.Style(parameters->textclass,
1554                            firstpar->Previous()->GetLayout()).isParagraph()
1555                   && firstpar->Previous()->GetDepth() == 0)
1556            // is it right to use defskip here too? (AS)
1557            maxasc += parameters->getDefSkip().inPixels();
1558       }
1559       
1560       /* the paper margins */ 
1561       if (!row_ptr->par->previous)
1562          maxasc += LYX_PAPER_MARGIN;
1563       
1564       /* add the vertical spaces, that the user added */
1565       if (firstpar->added_space_top.kind() != VSpace::NONE)
1566          maxasc += int(firstpar->added_space_top.inPixels());
1567       
1568       /* do not forget the DTP-lines! 
1569        * there height depends on the font of the nearest character */
1570       if (firstpar->line_top)
1571          maxasc += 2 * GetFont(firstpar, 0).ascent('x');
1572       
1573       /* and now the pagebreaks */ 
1574       if (firstpar->pagebreak_top)
1575          maxasc += 3 * DefaultHeight();
1576       
1577       /*  this is special code for the chapter, since the label of this
1578        * layout is printed in an extra row */ 
1579       if (layout.labeltype == LABEL_COUNTER_CHAPTER
1580           && parameters->secnumdepth>= 0) {
1581               labeladdon = int(labelfont.maxDescent() *
1582                                   layout.spacing.getValue() *
1583                                   parameters->spacing.getValue())
1584                       + int(labelfont.maxAscent() *
1585                                layout.spacing.getValue() *
1586                                parameters->spacing.getValue());
1587       }
1588       
1589       /* special code for the top label */ 
1590       if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1591            || layout.labeltype == LABEL_BIBLIO
1592            || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1593           && row_ptr->par->IsFirstInSequence()
1594           && !row_ptr->par->GetLabestring().empty()) {
1595          labeladdon = int(
1596                  (labelfont.maxAscent() *
1597                   layout.spacing.getValue() *
1598                   parameters->spacing.getValue())
1599                  +(labelfont.maxDescent() *
1600                    layout.spacing.getValue() *
1601                    parameters->spacing.getValue())
1602                  + layout.topsep * DefaultHeight()
1603                  + layout.labelbottomsep *  DefaultHeight());
1604       }
1605    
1606       /* and now the layout spaces, for example before and after a section, 
1607        * or between the items of a itemize or enumerate environment */ 
1608       
1609       if (!firstpar->pagebreak_top) {
1610          LyXParagraph * prev = row_ptr->par->Previous();
1611          if (prev)
1612             prev = row_ptr->par->DepthHook(row_ptr->par->GetDepth());
1613          if (prev && prev->GetLayout() == firstpar->GetLayout()
1614              && prev->GetDepth() == firstpar->GetDepth()
1615              && prev->GetLabelWidthString() == firstpar->GetLabelWidthString())
1616            {
1617               layoutasc = (layout.itemsep * DefaultHeight());
1618            }
1619          else if (row_ptr->previous) {
1620             tmptop = layout.topsep;
1621             
1622             if (row_ptr->previous->par->GetDepth() >= row_ptr->par->GetDepth())
1623                tmptop-= textclasslist.Style(parameters->textclass, row_ptr->previous->par->GetLayout()).bottomsep;
1624             
1625             if (tmptop > 0)
1626                layoutasc = (tmptop * DefaultHeight());
1627          }
1628          else if (row_ptr->par->line_top){
1629             tmptop = layout.topsep;
1630             
1631             if (tmptop > 0)
1632                layoutasc = (tmptop * DefaultHeight());
1633          }
1634          
1635          prev = row_ptr->par->DepthHook(row_ptr->par->GetDepth()-1);
1636          if (prev)  {
1637             maxasc += int(textclasslist.Style(parameters->textclass,
1638                                          prev->GetLayout()).parsep * DefaultHeight());
1639          }
1640          else {
1641                 if (firstpar->Previous()
1642                     && firstpar->Previous()->GetDepth() == 0
1643                     && firstpar->Previous()->GetLayout() != firstpar->GetLayout()) {
1644                    /* avoid parsep */ 
1645                 }
1646             else if (firstpar->Previous()){
1647                maxasc += int(layout.parsep * DefaultHeight());
1648             }
1649          }
1650       }
1651    }
1652    
1653    /* is it a bottom line? */ 
1654    if (row_ptr->par->ParFromPos(RowLast(row_ptr) + 1) == par
1655        && (!row_ptr->next || row_ptr->next->par != row_ptr->par)) {     
1656           
1657           /* the paper margins */ 
1658           if (!par->next)
1659             maxdesc += LYX_PAPER_MARGIN;
1660         
1661           /* add the vertical spaces, that the user added */
1662           if (firstpar->added_space_bottom.kind() != VSpace::NONE)
1663             maxdesc += int(firstpar->added_space_bottom.inPixels());
1664           
1665           /* do not forget the DTP-lines! 
1666            * there height depends on the font of the nearest character */
1667           if (firstpar->line_bottom)
1668             maxdesc += 2 * (GetFont(par, par->Last()-1).ascent('x'));
1669           
1670           /* and now the pagebreaks */
1671           if (firstpar->pagebreak_bottom)
1672             maxdesc += 3 * DefaultHeight();
1673           
1674           /* and now the layout spaces, for example before and after a section, 
1675            * or between the items of a itemize or enumerate environment */
1676           if (!firstpar->pagebreak_bottom && row_ptr->par->Next()) {
1677              LyXParagraph * nextpar = row_ptr->par->Next();
1678              LyXParagraph * comparepar = row_ptr->par;
1679              float usual = 0;
1680              float  unusual = 0;
1681              
1682              if (comparepar->GetDepth() > nextpar->GetDepth()) {
1683                 usual = (textclasslist.Style(parameters->textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1684                 comparepar = comparepar->DepthHook(nextpar->GetDepth());
1685                 if (comparepar->GetLayout()!= nextpar->GetLayout()
1686                     || nextpar->GetLabelWidthString() != 
1687                         comparepar->GetLabelWidthString())
1688                   unusual = (textclasslist.Style(parameters->textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1689                 
1690                 if (unusual > usual)
1691                   layoutdesc = unusual;
1692                 else
1693                   layoutdesc = usual;
1694              }
1695              else if (comparepar->GetDepth() ==  nextpar->GetDepth()) {
1696                 
1697                 if (comparepar->GetLayout()!= nextpar->GetLayout()
1698                     || nextpar->GetLabelWidthString() != 
1699                         comparepar->GetLabelWidthString())
1700                   layoutdesc = int(textclasslist.Style(parameters->textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1701              }
1702           }
1703        }
1704    
1705    /* incalculate the layout spaces */ 
1706    maxasc += int(layoutasc * 2 / (2 + firstpar->GetDepth()));
1707    maxdesc += int(layoutdesc * 2 / (2 + firstpar->GetDepth()));
1708
1709    /* table stuff -- begin*/
1710    if (row_ptr->par->table){
1711       maxasc += row_ptr->par->table->
1712         AdditionalHeight(NumberOfCell(row_ptr->par, row_ptr->pos));
1713    }
1714    /* table stuff -- end*/
1715    
1716    /* calculate the new height of the text */ 
1717    height -= row_ptr->height;
1718    
1719    row_ptr->height = maxasc + maxdesc + labeladdon;
1720    row_ptr->baseline = maxasc + labeladdon;
1721    
1722    height += row_ptr->height;
1723 }
1724 #else
1725 void LyXText::SetHeightOfRow(Row * row_ptr) const
1726 {
1727     /* get the maximum ascent and the maximum descent */
1728    int asc, maxasc, desc, maxdesc, pos_end, pos, labeladdon;
1729    float layoutasc = 0;
1730    float layoutdesc = 0;
1731    float tmptop = 0;
1732    LyXFont tmpfont;
1733    Inset * tmpinset;
1734
1735    /* this must not happen before the currentrow for clear reasons.
1736       so the trick is just to set the current row onto this row */
1737    long unused_y;
1738    GetRow(row_ptr->par, row_ptr->pos, unused_y);
1739
1740    /* ok , let us initialize the maxasc and maxdesc value. 
1741     * This depends in LaTeX of the font of the last character
1742     * in the paragraph. The hack below is necessary because
1743     * of the possibility of open footnotes */
1744
1745    /* Correction: only the fontsize count. The other properties
1746       are taken from the layoutfont. Nicer on the screen :) */
1747    
1748    LyXParagraph * par = row_ptr->par->LastPhysicalPar();
1749    LyXParagraph * firstpar = row_ptr->par->FirstPhysicalPar();
1750    
1751    LyXLayout const & layout = textclasslist.Style(parameters->textclass, firstpar->GetLayout());
1752    
1753    LyXFont font = GetFont(par, par->Last()-1);
1754    LyXFont::FONT_SIZE size = font.size();
1755    font = GetFont(par, -1);
1756    font.setSize(size);
1757
1758    LyXFont labelfont = GetFont(par, -2);
1759
1760    maxasc = int(font.maxAscent() *
1761                    layout.spacing.getValue() *
1762                    parameters->spacing.getValue());
1763    maxdesc = int(font.maxDescent() *
1764                     layout.spacing.getValue() *
1765                     parameters->spacing.getValue());
1766
1767    pos_end = RowLast(row_ptr);
1768    
1769    labeladdon = 0;
1770
1771    // Check if any insets are larger
1772    for (pos = row_ptr->pos; pos <= pos_end; ++pos) {
1773       if (row_ptr->par->GetChar(pos) == LyXParagraph::META_INSET) {
1774          tmpfont = GetFont(row_ptr->par, pos);
1775          tmpinset = row_ptr->par->GetInset(pos);
1776          if (tmpinset) {
1777             asc = tmpinset->Ascent(tmpfont);
1778             desc = tmpinset->Descent(tmpfont);
1779             if (asc > maxasc) 
1780               maxasc = asc;
1781             if (desc > maxdesc)
1782               maxdesc = desc;
1783          }
1784       }
1785    }
1786
1787    // Check if any custom fonts are larger (Asger)
1788    // This is not completely correct, but we can live with the small,
1789    // cosmetic error for now.
1790    LyXFont::FONT_SIZE maxsize = row_ptr->par->HighestFontInRange(row_ptr->pos,
1791                                                                  pos_end);
1792    if (maxsize > font.size()) {
1793         font.setSize(maxsize);
1794
1795         asc = font.maxAscent();
1796         desc = font.maxDescent();
1797         if (asc > maxasc) 
1798                 maxasc = asc;
1799         if (desc > maxdesc)
1800                 maxdesc = desc;
1801    }
1802
1803    /* table stuff -- begin*/
1804    if (row_ptr->par->table){
1805      // stretch the rows a bit
1806       maxasc += 1;
1807       maxdesc += 1;
1808    }
1809    /* table stuff -- end*/
1810
1811    // This is nicer with box insets:
1812    ++maxasc;
1813    ++maxdesc;
1814
1815    row_ptr->ascent_of_text = maxasc;
1816    
1817    /* is it a top line? */ 
1818    if (row_ptr->pos == 0
1819        && row_ptr->par == firstpar) {
1820       
1821       /* some parksips VERY EASY IMPLEMENTATION */ 
1822       if (parameters->paragraph_separation == BufferParams::PARSEP_SKIP) {
1823          if (layout.isParagraph()
1824              && firstpar->GetDepth() == 0
1825              && firstpar->Previous())
1826             maxasc += parameters->getDefSkip().inPixels();
1827          else if (firstpar->Previous()
1828                   && textclasslist.Style(parameters->textclass,
1829                            firstpar->Previous()->GetLayout()).isParagraph()
1830                   && firstpar->Previous()->GetDepth() == 0)
1831            // is it right to use defskip here too? (AS)
1832            maxasc += parameters->getDefSkip().inPixels();
1833       }
1834       
1835       /* the paper margins */ 
1836       if (!row_ptr->par->previous)
1837          maxasc += LYX_PAPER_MARGIN;
1838       
1839       /* add the vertical spaces, that the user added */
1840       if (firstpar->added_space_top.kind() != VSpace::NONE)
1841          maxasc += int(firstpar->added_space_top.inPixels());
1842       
1843       /* do not forget the DTP-lines! 
1844        * there height depends on the font of the nearest character */
1845       if (firstpar->line_top)
1846          maxasc += 2 * GetFont(firstpar, 0).ascent('x');
1847       
1848       /* and now the pagebreaks */ 
1849       if (firstpar->pagebreak_top)
1850          maxasc += 3 * DefaultHeight();
1851       
1852       /*  this is special code for the chapter, since the label of this
1853        * layout is printed in an extra row */ 
1854       if (layout.labeltype == LABEL_COUNTER_CHAPTER
1855           && parameters->secnumdepth>= 0) {
1856               labeladdon = int(labelfont.maxDescent() *
1857                                   layout.spacing.getValue() *
1858                                   parameters->spacing.getValue())
1859                       + int(labelfont.maxAscent() *
1860                                layout.spacing.getValue() *
1861                                parameters->spacing.getValue());
1862       }
1863       
1864       /* special code for the top label */ 
1865       if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1866            || layout.labeltype == LABEL_BIBLIO
1867            || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1868           && row_ptr->par->IsFirstInSequence()
1869           && !row_ptr->par->GetLabestring().empty()) {
1870          labeladdon = int(
1871                  (labelfont.maxAscent() *
1872                   layout.spacing.getValue() *
1873                   parameters->spacing.getValue())
1874                  +(labelfont.maxDescent() *
1875                    layout.spacing.getValue() *
1876                    parameters->spacing.getValue())
1877                  + layout.topsep * DefaultHeight()
1878                  + layout.labelbottomsep *  DefaultHeight());
1879       }
1880    
1881       /* and now the layout spaces, for example before and after a section, 
1882        * or between the items of a itemize or enumerate environment */ 
1883       
1884       if (!firstpar->pagebreak_top) {
1885          LyXParagraph *prev = row_ptr->par->Previous();
1886          if (prev)
1887             prev = row_ptr->par->DepthHook(row_ptr->par->GetDepth());
1888          if (prev && prev->GetLayout() == firstpar->GetLayout()
1889              && prev->GetDepth() == firstpar->GetDepth()
1890              && prev->GetLabelWidthString() == firstpar->GetLabelWidthString())
1891            {
1892               layoutasc = (layout.itemsep * DefaultHeight());
1893            }
1894          else if (row_ptr->previous) {
1895             tmptop = layout.topsep;
1896             
1897             if (row_ptr->previous->par->GetDepth() >= row_ptr->par->GetDepth())
1898                tmptop-= textclasslist.Style(parameters->textclass, row_ptr->previous->par->GetLayout()).bottomsep;
1899             
1900             if (tmptop > 0)
1901                layoutasc = (tmptop * DefaultHeight());
1902          }
1903          else if (row_ptr->par->line_top){
1904             tmptop = layout.topsep;
1905             
1906             if (tmptop > 0)
1907                layoutasc = (tmptop * DefaultHeight());
1908          }
1909          
1910          prev = row_ptr->par->DepthHook(row_ptr->par->GetDepth()-1);
1911          if (prev)  {
1912             maxasc += int(textclasslist.Style(parameters->textclass,
1913                                          prev->GetLayout()).parsep * DefaultHeight());
1914          }
1915          else {
1916                 if (firstpar->Previous()
1917                     && firstpar->Previous()->GetDepth() == 0
1918                     && firstpar->Previous()->GetLayout() != firstpar->GetLayout()) {
1919                    /* avoid parsep */ 
1920                 }
1921             else if (firstpar->Previous()){
1922                maxasc += int(layout.parsep * DefaultHeight());
1923             }
1924          }
1925       }
1926    }
1927    
1928    /* is it a bottom line? */ 
1929    if (row_ptr->par->ParFromPos(RowLast(row_ptr) + 1) == par
1930        && (!row_ptr->next || row_ptr->next->par != row_ptr->par)) {     
1931           
1932           /* the paper margins */ 
1933           if (!par->next)
1934             maxdesc += LYX_PAPER_MARGIN;
1935         
1936           /* add the vertical spaces, that the user added */
1937           if (firstpar->added_space_bottom.kind() != VSpace::NONE)
1938             maxdesc += int(firstpar->added_space_bottom.inPixels());
1939           
1940           /* do not forget the DTP-lines! 
1941            * there height depends on the font of the nearest character */
1942           if (firstpar->line_bottom)
1943             maxdesc += 2 * (GetFont(par, par->Last()-1).ascent('x'));
1944           
1945           /* and now the pagebreaks */
1946           if (firstpar->pagebreak_bottom)
1947             maxdesc += 3 * DefaultHeight();
1948           
1949           /* and now the layout spaces, for example before and after a section, 
1950            * or between the items of a itemize or enumerate environment */
1951           if (!firstpar->pagebreak_bottom && row_ptr->par->Next()) {
1952              LyXParagraph *nextpar = row_ptr->par->Next();
1953              LyXParagraph *comparepar = row_ptr->par;
1954              float usual = 0;
1955              float  unusual = 0;
1956              
1957              if (comparepar->GetDepth() > nextpar->GetDepth()) {
1958                 usual = (textclasslist.Style(parameters->textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1959                 comparepar = comparepar->DepthHook(nextpar->GetDepth());
1960                 if (comparepar->GetLayout()!= nextpar->GetLayout()
1961                     || nextpar->GetLabelWidthString() != 
1962                         comparepar->GetLabelWidthString())
1963                   unusual = (textclasslist.Style(parameters->textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1964                 
1965                 if (unusual > usual)
1966                   layoutdesc = unusual;
1967                 else
1968                   layoutdesc = usual;
1969              }
1970              else if (comparepar->GetDepth() ==  nextpar->GetDepth()) {
1971                 
1972                 if (comparepar->GetLayout()!= nextpar->GetLayout()
1973                     || nextpar->GetLabelWidthString() != 
1974                         comparepar->GetLabelWidthString())
1975                   layoutdesc = int(textclasslist.Style(parameters->textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1976              }
1977           }
1978        }
1979    
1980    /* incalculate the layout spaces */ 
1981    maxasc += int(layoutasc * 2 / (2 + firstpar->GetDepth()));
1982    maxdesc += int(layoutdesc * 2 / (2 + firstpar->GetDepth()));
1983
1984    /* table stuff -- begin*/
1985    if (row_ptr->par->table){
1986       maxasc += row_ptr->par->table->
1987         AdditionalHeight(NumberOfCell(row_ptr->par, row_ptr->pos));
1988    }
1989    /* table stuff -- end*/
1990    
1991    /* calculate the new height of the text */ 
1992    height -= row_ptr->height;
1993    
1994    row_ptr->height= maxasc+maxdesc+labeladdon;
1995    row_ptr->baseline= maxasc+labeladdon;
1996    
1997    height += row_ptr->height;
1998 }
1999 #endif
2000
2001
2002 /* Appends the implicit specified paragraph behind the specified row,
2003  * start at the implicit given position */
2004 void LyXText::AppendParagraph(Row * row) const
2005 {
2006    bool not_ready = true;
2007    
2008    // The last character position of a paragraph is an invariant so we can 
2009    // safely get it here. (Asger)
2010    int lastposition = row->par->Last();
2011
2012    do {
2013       // Get the next breakpoint
2014       int z = NextBreakPoint(row, paperwidth);
2015       
2016       Row * tmprow = row;
2017
2018       // Insert the new row
2019       if (z < lastposition) {
2020          ++z;
2021          InsertRow(row, row->par, z);
2022          row = row->next;
2023
2024          row->height = 0;
2025       } else
2026          not_ready = false;
2027       
2028       // Set the dimensions of the row
2029       tmprow->fill = Fill(tmprow, paperwidth);
2030       SetHeightOfRow(tmprow);
2031
2032    } while (not_ready);
2033 }
2034
2035
2036 void LyXText::BreakAgain(Row * row) const
2037 {
2038    bool not_ready = true;
2039    
2040    do  {
2041       /* get the next breakpoint */
2042         LyXParagraph::size_type z = 
2043                 NextBreakPoint(row, paperwidth);
2044       Row * tmprow = row;
2045       
2046       if (z < row->par->Last() ) {
2047          if (!row->next || (row->next && row->next->par != row->par)) {
2048                  // insert a new row
2049             ++z;
2050             InsertRow(row, row->par, z);
2051             row = row->next;
2052             row->height = 0;
2053          } else  {
2054             row = row->next;
2055             ++z;
2056             if (row->pos == z)
2057                     not_ready = false;     // the rest will not change
2058             else {
2059                row->pos = z;
2060             }
2061          }
2062       } else {
2063          /* if there are some rows too much, delete them */
2064          /* only if you broke the whole paragraph! */ 
2065          Row * tmprow2 = row;
2066          while (tmprow2->next && tmprow2->next->par == row->par) {
2067             tmprow2 = tmprow2->next;
2068          }
2069          while (tmprow2 != row) {
2070             tmprow2 = tmprow2->previous;
2071             RemoveRow(tmprow2->next);
2072          }
2073          not_ready = false;
2074       }
2075        
2076       /* set the dimensions of the row */ 
2077       tmprow->fill = Fill(tmprow, paperwidth);
2078       SetHeightOfRow(tmprow);
2079    } while (not_ready);
2080 }
2081
2082
2083 /* this is just a little changed version of break again */ 
2084 void LyXText::BreakAgainOneRow(Row * row)
2085 {
2086    /* get the next breakpoint */
2087    LyXParagraph::size_type z = NextBreakPoint(row, paperwidth);
2088    Row * tmprow = row;
2089    
2090    if (z < row->par->Last() ) {
2091       if (!row->next || (row->next && row->next->par != row->par)) {
2092          /* insert a new row */ 
2093          ++z;
2094          InsertRow(row, row->par, z);
2095          row = row->next;
2096          row->height = 0;
2097       }
2098       else  {
2099          row= row->next;
2100          ++z;
2101          if (row->pos != z)
2102             row->pos = z;
2103       }
2104    }
2105    else {
2106       /* if there are some rows too much, delete them */
2107       /* only if you broke the whole paragraph! */ 
2108       Row * tmprow2 = row;
2109       while (tmprow2->next && tmprow2->next->par == row->par) {
2110          tmprow2 = tmprow2->next;
2111       }
2112       while (tmprow2 != row) {
2113          tmprow2 = tmprow2->previous;
2114          RemoveRow(tmprow2->next);
2115       }
2116    }
2117    
2118    /* set the dimensions of the row */ 
2119    tmprow->fill = Fill(tmprow, paperwidth);
2120    SetHeightOfRow(tmprow);
2121 }
2122
2123
2124 void LyXText::BreakParagraph(char keep_layout)
2125 {
2126    LyXLayout const & layout = textclasslist.Style(parameters->textclass,
2127                                       cursor.par->GetLayout());
2128    
2129    /* table stuff -- begin */
2130    if (cursor.par->table) {
2131        // breaking of tables is only allowed at the beginning or the end */
2132        if (cursor.pos && cursor.pos < cursor.par->size() &&
2133            !cursor.par->table->ShouldBeVeryLastCell(NumberOfCell(cursor.par, cursor.pos)))
2134                return; // no breaking of tables allowed
2135    }
2136    /* table stuff -- end */
2137
2138    // this is only allowed, if the current paragraph is not empty or caption
2139    if ((cursor.par->Last() <= 0 && !cursor.par->IsDummy())
2140        && 
2141        layout.labeltype!= LABEL_SENSITIVE)
2142      return;
2143
2144    SetUndo(Undo::INSERT, 
2145            cursor.par->ParFromPos(cursor.pos)->previous, 
2146            cursor.par->ParFromPos(cursor.pos)->next); 
2147
2148    /* table stuff -- begin */
2149    if (cursor.par->table) {
2150        int cell = NumberOfCell(cursor.par, cursor.pos);
2151        if (cursor.par->table->ShouldBeVeryLastCell(cell))
2152            SetCursor(cursor.par, cursor.par->text.size());
2153    }
2154    /* table stuff -- end */
2155    
2156    // please break always behind a space
2157    if (cursor.pos < cursor.par->Last()
2158        && cursor.par->IsLineSeparator(cursor.pos))
2159      cursor.pos++;
2160    
2161    // break the paragraph
2162    if (keep_layout)
2163      keep_layout = 2;
2164    else 
2165      keep_layout = layout.isEnvironment();
2166    cursor.par->BreakParagraph(cursor.pos, keep_layout);
2167
2168    /* table stuff -- begin */
2169    if (cursor.par->table){
2170      // the table should stay with the contents
2171      if (!cursor.pos){
2172        cursor.par->Next()->table = cursor.par->table;
2173        cursor.par->table = 0;
2174      }
2175    }
2176    /* table stuff -- end */
2177
2178    // well this is the caption hack since one caption is really enough
2179    if (layout.labeltype == LABEL_SENSITIVE){
2180      if (!cursor.pos)
2181              cursor.par->SetLayout(0); // set to standard-layout
2182      else
2183              cursor.par->Next()->SetLayout(0); // set to standard-layout
2184    }
2185    
2186    /* if the cursor is at the beginning of a row without prior newline, 
2187     * move one row up! 
2188     * This touches only the screen-update. Otherwise we would may have
2189     * an empty row on the screen */
2190    if (cursor.pos && !cursor.row->par->IsNewline(cursor.row->pos -1) &&
2191        cursor.row->pos == cursor.pos) {
2192      CursorLeft();
2193    } 
2194    
2195    status = LyXText::NEED_MORE_REFRESH;
2196    refresh_row = cursor.row;
2197    refresh_y = cursor.y - cursor.row->baseline;
2198    
2199    // Do not forget the special right address boxes
2200    if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
2201       while (refresh_row->previous &&
2202              refresh_row->previous->par == refresh_row->par) {
2203                 refresh_row = refresh_row->previous;
2204                 refresh_y -= refresh_row->height;
2205              }
2206    }
2207    RemoveParagraph(cursor.row);
2208    
2209    // set the dimensions of the cursor row
2210    cursor.row->fill = Fill(cursor.row, paperwidth);
2211
2212    SetHeightOfRow(cursor.row);
2213    
2214    while (!cursor.par->Next()->table && cursor.par->Next()->Last()
2215           && cursor.par->Next()->IsNewline(0))
2216      cursor.par->Next()->Erase(0);
2217    
2218    InsertParagraph(cursor.par->Next(), cursor.row);
2219
2220    UpdateCounters(cursor.row->previous);
2221    
2222    /* This check is necessary. Otherwise the new empty paragraph will
2223     * be deleted automatically. And it is more friendly for the user! */ 
2224    if (cursor.pos)
2225      SetCursor(cursor.par->Next(), 0);
2226    else
2227      SetCursor(cursor.par, 0);
2228    
2229    if (cursor.row->next)
2230      BreakAgain(cursor.row->next);
2231
2232    need_break_row = 0;
2233 }
2234
2235
2236 void LyXText::OpenFootnote()
2237 {
2238    LyXParagraph * endpar,* tmppar;
2239    Row * row;
2240    
2241    LyXParagraph * par = cursor.par->ParFromPos(cursor.pos);
2242    
2243    /* if there is no footnote in this paragraph, just return. */ 
2244    if (!par->next
2245        || par->next->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
2246      return;
2247    
2248    /* ok, move the cursor right before the footnote */ 
2249    
2250    /* just a little faster than using CursorRight() */
2251    for (cursor.pos = 0;
2252         cursor.par->ParFromPos(cursor.pos) != par; cursor.pos++);
2253    /* now the cursor is at the beginning of the physical par */
2254    SetCursor(cursor.par,
2255              cursor.pos + cursor.par->ParFromPos(cursor.pos)->text.size());
2256    
2257    /* the cursor must be exactly before the footnote */ 
2258    par = cursor.par->ParFromPos(cursor.pos);
2259    
2260    status = LyXText::NEED_MORE_REFRESH;
2261    refresh_row = cursor.row;
2262    refresh_y = cursor.y - cursor.row->baseline;
2263    
2264    tmppar = cursor.par;
2265    endpar = cursor.par->Next();
2266    row = cursor.row;
2267    
2268    tmppar->OpenFootnote(cursor.pos);
2269    RemoveParagraph(row);
2270    /* set the dimensions of the cursor row */
2271    row->fill = Fill(row, paperwidth);
2272    SetHeightOfRow(row);
2273    tmppar = tmppar->Next();
2274    
2275    while (tmppar != endpar) {
2276       if (tmppar) {
2277          InsertParagraph(tmppar, row);
2278          while (row->next && row->next->par == tmppar)
2279            row = row->next;
2280          tmppar = tmppar->Next();
2281       }
2282    }
2283    SetCursor(par->next, 0);
2284    sel_cursor = cursor;
2285 }
2286    
2287
2288 /* table stuff -- begin*/
2289
2290 void LyXText::TableFeatures(int feature, string const & val) const
2291 {
2292         if (!cursor.par->table)
2293                 return; /* this should never happen */
2294   
2295         int actCell = NumberOfCell(cursor.par, cursor.pos);
2296         SetUndo(Undo::FINISH, 
2297                 cursor.par->ParFromPos(cursor.pos)->previous, 
2298                 cursor.par->ParFromPos(cursor.pos)->next); 
2299         
2300         switch (feature){
2301         case LyXTable::SET_PWIDTH:
2302                 cursor.par->table->SetPWidth(actCell, val);
2303                 break;
2304         case LyXTable::SET_SPECIAL_COLUMN:
2305         case LyXTable::SET_SPECIAL_MULTI:
2306                 cursor.par->table->SetAlignSpecial(actCell, val, feature);
2307                 break;
2308         default:
2309                 break;
2310         }
2311         RedoParagraph();
2312 }
2313
2314
2315 void LyXText::TableFeatures(int feature) const
2316 {
2317         int setLines = 0;
2318         int setAlign = LYX_ALIGN_LEFT;
2319         int lineSet;
2320         bool what;
2321     
2322     if (!cursor.par->table)
2323         return; /* this should never happen */
2324   
2325     int actCell = NumberOfCell(cursor.par, cursor.pos);
2326     SetUndo(Undo::FINISH, 
2327             cursor.par->ParFromPos(cursor.pos)->previous, 
2328             cursor.par->ParFromPos(cursor.pos)->next); 
2329
2330     switch (feature){
2331       case LyXTable::ALIGN_LEFT:
2332           setAlign= LYX_ALIGN_LEFT;
2333           break;
2334       case LyXTable::ALIGN_RIGHT:
2335           setAlign= LYX_ALIGN_RIGHT;
2336           break;
2337       case LyXTable::ALIGN_CENTER:
2338           setAlign= LYX_ALIGN_CENTER;
2339           break;
2340       default:
2341           break;
2342     }
2343     switch (feature){
2344       case LyXTable::APPEND_ROW: {
2345               LyXParagraph::size_type pos = cursor.pos;
2346
2347               /* move to the next row */
2348           int cell_org = actCell;
2349           int cell = cell_org;
2350
2351           // if there is a ContRow following this row I have to add
2352           // the row after the ContRow's
2353           if ((pos < cursor.par->Last()) &&
2354               cursor.par->table->RowHasContRow(cell_org)) {
2355               while((pos < cursor.par->Last()) &&
2356                     !cursor.par->table->IsContRow(cell)) {
2357                   while (pos < cursor.par->Last() &&
2358                          !cursor.par->IsNewline(pos))
2359                       ++pos;
2360                   if (pos < cursor.par->Last())
2361                       ++pos;
2362                   ++cell;
2363               }
2364               while((pos < cursor.par->Last()) &&
2365                     cursor.par->table->IsContRow(cell)) {
2366                   while (pos < cursor.par->Last() &&
2367                          !cursor.par->IsNewline(pos))
2368                       ++pos;
2369                   if (pos < cursor.par->Last())
2370                       ++pos;
2371                   ++cell;
2372               }
2373               cell_org = --cell;
2374               if (pos < cursor.par->Last())
2375                   --pos;
2376           }
2377           while (pos < cursor.par->Last() && 
2378                  (cell == cell_org || !cursor.par->table->IsFirstCell(cell))){
2379               while (pos < cursor.par->Last() && !cursor.par->IsNewline(pos))
2380                   ++pos;
2381               if (pos < cursor.par->Last())
2382                   ++pos;
2383               ++cell;
2384           }
2385                 
2386           /* insert the new cells */ 
2387           int number = cursor.par->table->NumberOfCellsInRow(cell_org);
2388           for (int i = 0; i < number; ++i)
2389               cursor.par->InsertChar(pos, LyXParagraph::META_NEWLINE);
2390                 
2391           /* append the row into the table */
2392           cursor.par->table->AppendRow(cell_org);
2393           RedoParagraph();
2394           return;
2395       }
2396       case LyXTable::APPEND_CONT_ROW: {
2397               LyXParagraph::size_type pos = cursor.pos;
2398           /* move to the next row */
2399           int cell_org = actCell;
2400           int cell = cell_org;
2401
2402           // if there is already a controw but not for this cell
2403           // the AppendContRow sets only the right values but does
2404           // not actually add a row
2405           if (cursor.par->table->RowHasContRow(cell_org) &&
2406               (cursor.par->table->CellHasContRow(cell_org)<0)) {
2407               cursor.par->table->AppendContRow(cell_org);
2408               RedoParagraph();
2409               return;
2410           }
2411           while (pos < cursor.par->Last() && 
2412                  (cell == cell_org
2413                   || !cursor.par->table->IsFirstCell(cell))){
2414               while (pos < cursor.par->Last() && !cursor.par->IsNewline(pos))
2415                   ++pos;
2416               if (pos < cursor.par->Last())
2417                   ++pos;
2418               ++cell;
2419           }
2420                 
2421           /* insert the new cells */ 
2422           int number = cursor.par->table->NumberOfCellsInRow(cell_org);
2423           for (int i = 0; i < number; ++i)
2424               cursor.par->InsertChar(pos, LyXParagraph::META_NEWLINE);
2425                 
2426           /* append the row into the table */
2427           cursor.par->table->AppendContRow(cell_org);
2428           RedoParagraph();
2429           return;
2430       }
2431       case LyXTable::APPEND_COLUMN: {
2432               LyXParagraph::size_type pos = 0;
2433           int cell_org = actCell;
2434           int cell = 0;
2435           do{
2436               if (pos && (cursor.par->IsNewline(pos-1))){
2437                   if (cursor.par->table->AppendCellAfterCell(cell_org, cell)){
2438                       cursor.par->InsertChar(pos, LyXParagraph::META_NEWLINE);
2439                       if (pos <= cursor.pos)
2440                           cursor.pos++;
2441                       ++pos;
2442                   }
2443                   ++cell;
2444               }
2445               ++pos;
2446           } while (pos <= cursor.par->Last());
2447           /* remember that the very last cell doesn't end with a newline.
2448              This saves one byte memory per table ;-) */ 
2449           if (cursor.par->table->AppendCellAfterCell(cell_org, cell))
2450               cursor.par->InsertChar(cursor.par->Last(), LyXParagraph::META_NEWLINE);
2451                 
2452           /* append the column into the table */ 
2453           cursor.par->table->AppendColumn(cell_org);
2454                 
2455           RedoParagraph();
2456           return;
2457       }
2458       case LyXTable::DELETE_ROW:
2459           if (current_view->the_locking_inset)
2460               current_view->unlockInset(current_view->the_locking_inset);
2461           RemoveTableRow(&cursor);
2462           RedoParagraph();
2463           return;
2464         
2465       case LyXTable::DELETE_COLUMN: {
2466               LyXParagraph::size_type pos = 0;
2467           int cell_org = actCell;
2468           int cell = 0;
2469           if (current_view->the_locking_inset)
2470               current_view->unlockInset(current_view->the_locking_inset);
2471           do {
2472               if (!pos || (cursor.par->IsNewline(pos-1))){
2473                   if (cursor.par->table->DeleteCellIfColumnIsDeleted(cell, cell_org)){
2474                       // delete one cell
2475                       while (pos < cursor.par->Last() && !cursor.par->IsNewline(pos))
2476                           cursor.par->Erase(pos);
2477                       if (pos < cursor.par->Last())
2478                           cursor.par->Erase(pos);
2479                       else 
2480                           cursor.par->Erase(pos - 1); // the missing newline at the end of a table
2481                       --pos; // because of pos++ below
2482                   }   
2483                   ++cell;
2484               }
2485               ++pos;
2486           } while (pos <= cursor.par->Last());
2487                 
2488           /* delete the column from the table */ 
2489           cursor.par->table->DeleteColumn(cell_org);
2490                 
2491           /* set the cursor to the beginning of the table, where else? */ 
2492           cursor.pos = 0;
2493           RedoParagraph();
2494           return;
2495       }
2496       case LyXTable::TOGGLE_LINE_TOP:
2497           lineSet = !cursor.par->table->TopLine(actCell);
2498           if (!selection){
2499               cursor.par->table->SetTopLine(actCell, lineSet);
2500           } else {
2501                   LyXParagraph::size_type i;
2502                   int n = -1, m = -2;
2503               for (i = sel_start_cursor.pos; i <= sel_end_cursor.pos; ++i){
2504                   if ((n = NumberOfCell(sel_start_cursor.par, i)) != m) {
2505                       cursor.par->table->SetTopLine(n, lineSet);
2506                       m = n;
2507                   }
2508               }
2509           }
2510           RedoParagraph();
2511           return;
2512     
2513       case LyXTable::TOGGLE_LINE_BOTTOM:
2514           lineSet = !cursor.par->table->BottomLine(actCell);
2515           if (!selection){
2516               cursor.par->table->SetBottomLine(actCell, lineSet);
2517           } else {
2518                   LyXParagraph::size_type i;
2519                   int n = -1, m = -2;
2520               for (i = sel_start_cursor.pos; i <= sel_end_cursor.pos; ++i) {
2521                   if ((n = NumberOfCell(sel_start_cursor.par, i)) != m) {
2522                       cursor.par->table->SetBottomLine(n, lineSet);
2523                       m = n;
2524                   }
2525               }
2526           }
2527           RedoParagraph();
2528           return;
2529                 
2530       case LyXTable::TOGGLE_LINE_LEFT:
2531           lineSet = !cursor.par->table->LeftLine(actCell);
2532           if (!selection){
2533               cursor.par->table->SetLeftLine(actCell, lineSet);
2534           } else {
2535                   LyXParagraph::size_type i;
2536                   int n = -1, m = -2;
2537               for (i = sel_start_cursor.pos; i <= sel_end_cursor.pos; ++i){
2538                   if ((n= NumberOfCell(sel_start_cursor.par, i)) != m) {
2539                       cursor.par->table->SetLeftLine(n, lineSet);
2540                       m = n;
2541                   }
2542               }
2543           }
2544           RedoParagraph();
2545           return;
2546
2547       case LyXTable::TOGGLE_LINE_RIGHT:
2548           lineSet = !cursor.par->table->RightLine(actCell);
2549           if (!selection){
2550               cursor.par->table->SetRightLine(actCell, lineSet);
2551           } else {
2552                   int n = -1, m = -2;
2553                   LyXParagraph::size_type i = sel_start_cursor.pos;
2554               for (; i <= sel_end_cursor.pos; ++i) {
2555                   if ((n= NumberOfCell(sel_start_cursor.par, i)) != m) {
2556                       cursor.par->table->SetRightLine(n, lineSet);
2557                       m = n;
2558                   }
2559               }
2560           }
2561           RedoParagraph();
2562           return;
2563     
2564       case LyXTable::ALIGN_LEFT:
2565       case LyXTable::ALIGN_RIGHT:
2566       case LyXTable::ALIGN_CENTER:
2567           if (!selection){
2568               cursor.par->table->SetAlignment(actCell, setAlign);
2569           } else {
2570               int n = -1, m = -2;
2571               LyXParagraph::size_type i = sel_start_cursor.pos;
2572               for (; i <= sel_end_cursor.pos; ++i) {
2573                   if ((n= NumberOfCell(sel_start_cursor.par, i)) != m) {
2574                       cursor.par->table->SetAlignment(n, setAlign);
2575                       m = n;
2576                   }
2577               }
2578           }
2579           RedoParagraph();
2580           return;
2581                 
2582       case LyXTable::DELETE_TABLE:
2583           SetCursorIntern(cursor.par, 0);
2584           delete cursor.par->table;
2585           cursor.par->table = 0;
2586           // temporary: Should put table in simple_cut_buffer (with before and after
2587           // dummy-paragraph !! 
2588           // not necessar anymore with UNDO :)
2589           for (LyXParagraph::size_type i = 
2590                        cursor.par->text.size() - 1; i >= 0; --i)
2591               cursor.par->Erase(i);
2592           RedoParagraph();
2593           return;
2594                 
2595       case LyXTable::MULTICOLUMN: {
2596           int number = 0;
2597           // check wether we are completly in a multicol
2598           int multicol = cursor.par->table->IsMultiColumn(actCell);
2599           if (multicol && selection && sel_start_cursor.row == sel_end_cursor.row){
2600               multicol = NumberOfCell(sel_start_cursor.par, sel_start_cursor.pos)
2601                   == NumberOfCell(sel_end_cursor.par, sel_end_cursor.pos);
2602           }
2603
2604           if (multicol){
2605               int newlines = cursor.par->table->UnsetMultiColumn(actCell);
2606               LyXParagraph::size_type pos = cursor.pos;
2607               while (pos < cursor.par->Last() && !cursor.par->IsNewline(pos))
2608                   ++pos;
2609               for (; newlines; --newlines)
2610                   cursor.par->InsertChar(pos, LyXParagraph::META_NEWLINE);
2611               RedoParagraph();
2612               return;
2613           }
2614           else {
2615               // selection must be in one row (or no selection)
2616               if (!selection){
2617                   cursor.par->table->SetMultiColumn(NumberOfCell(cursor.par,
2618                                                                  cursor.pos),
2619                                                     1);
2620                   RedoParagraph();
2621                   return;
2622               }
2623               else {
2624                   if (sel_start_cursor.row == sel_end_cursor.row){
2625                       LyXParagraph::size_type i;
2626                       number = 1;
2627                       for (i = sel_start_cursor.pos;
2628                            i < sel_end_cursor.pos; ++i){
2629                           if (sel_start_cursor.par->IsNewline(i)){
2630                               sel_start_cursor.par->Erase(i);
2631                               // check for double-blanks
2632                               if ((i && !sel_start_cursor.par->IsLineSeparator(i-1))
2633                                   &&
2634                                   (i < sel_start_cursor.par->Last() 
2635                                    && !sel_start_cursor.par->IsLineSeparator(i)))
2636                                   sel_start_cursor.par->InsertChar(i, ' ');
2637                               else {
2638                                   sel_end_cursor.pos--;
2639                                   --i;
2640                               }
2641                               ++number;
2642                           }
2643                       }
2644                       cursor.par->table->
2645                           SetMultiColumn(NumberOfCell(sel_start_cursor.par,
2646                                                       sel_start_cursor.pos),
2647                                          number);
2648                       cursor.pos = sel_start_cursor.pos;
2649                       RedoParagraph();
2650                       return;
2651                   }
2652                   else {
2653                       WriteAlert(_("Impossible Operation!"), 
2654                                  _("Multicolumns can only be horizontally."), 
2655                                  _("Sorry."));
2656                   }
2657               }
2658           }
2659           break;
2660       }
2661       case LyXTable::SET_ALL_LINES:
2662           setLines = 1;
2663       case LyXTable::UNSET_ALL_LINES:
2664           if (!selection){
2665               cursor.par->table->SetAllLines(NumberOfCell(cursor.par,
2666                                                           cursor.pos),
2667                                              setLines);
2668           } else {
2669                   LyXParagraph::size_type i;
2670                   int n = -1, m = -2;
2671               for (i = sel_start_cursor.pos; i <= sel_end_cursor.pos; ++i) {
2672                   if ((n= NumberOfCell(sel_start_cursor.par, i)) != m) {
2673                       cursor.par->table->SetAllLines(n, setLines);
2674                       m = n;
2675                   }
2676               }
2677           }
2678           RedoParagraph();
2679           return;
2680       case LyXTable::SET_LONGTABLE:
2681           cursor.par->table->SetLongTable(true);
2682           return;
2683       case LyXTable::UNSET_LONGTABLE:
2684           cursor.par->table->SetLongTable(false);
2685           return;
2686       case LyXTable::SET_ROTATE_TABLE:
2687           cursor.par->table->SetRotateTable(true);
2688           return;
2689       case LyXTable::UNSET_ROTATE_TABLE:
2690           cursor.par->table->SetRotateTable(false);
2691           return;
2692       case LyXTable::SET_ROTATE_CELL:
2693           if (!selection){
2694               cursor.par->table->SetRotateCell(actCell, true);
2695           } else {
2696                   LyXParagraph::size_type i;
2697                   int n = -1, m = -2;
2698               for (i = sel_start_cursor.pos; i <= sel_end_cursor.pos; ++i){
2699                   if ((n = NumberOfCell(sel_start_cursor.par, i)) != m) {
2700                       cursor.par->table->SetRotateCell(n, true);
2701                       m = n;
2702                   }
2703               }
2704           }
2705           return;
2706       case LyXTable::UNSET_ROTATE_CELL:
2707           if (!selection){
2708               cursor.par->table->SetRotateCell(actCell, false);
2709           } else {
2710                   int n = -1, m = -2;
2711                   LyXParagraph::size_type i = sel_start_cursor.pos;
2712               for (; i <= sel_end_cursor.pos; ++i) {
2713                   if ((n= NumberOfCell(sel_start_cursor.par, i)) != m) {
2714                       cursor.par->table->SetRotateCell(n, false);
2715                       m = n;
2716                   }
2717               }
2718           }
2719           return;
2720       case LyXTable::SET_LINEBREAKS:
2721           what = !cursor.par->table->Linebreaks(cursor.par->table->FirstVirtualCell(actCell));
2722           if (!selection){
2723               cursor.par->table->SetLinebreaks(actCell, what);
2724           } else {
2725                   LyXParagraph::size_type i;
2726                   int n = -1, m = -2;
2727               for (i = sel_start_cursor.pos; i <= sel_end_cursor.pos; ++i) {
2728                   if ((n = NumberOfCell(sel_start_cursor.par, i)) != m) {
2729                       cursor.par->table->SetLinebreaks(n, what);
2730                       m = n;
2731                   }
2732               }
2733           }
2734           return;
2735       case LyXTable::SET_LTFIRSTHEAD:
2736           cursor.par->table->SetLTHead(actCell, true);
2737           return;
2738       case LyXTable::SET_LTHEAD:
2739           cursor.par->table->SetLTHead(actCell, false);
2740           return;
2741       case LyXTable::SET_LTFOOT:
2742           cursor.par->table->SetLTFoot(actCell, false);
2743           return;
2744       case LyXTable::SET_LTLASTFOOT:
2745           cursor.par->table->SetLTFoot(actCell, true);
2746           return;
2747       case LyXTable::SET_LTNEWPAGE:
2748           what = !cursor.par->table->LTNewPage(actCell);
2749           cursor.par->table->SetLTNewPage(actCell, what);
2750           return;
2751     }
2752 }
2753         
2754
2755 void LyXText::InsertCharInTable(char c)
2756 {
2757         Row * row;
2758         Row * tmprow;
2759         long y;
2760         bool jumped_over_space;
2761         
2762         /* first check, if there will be two blanks together or a blank at 
2763          * the beginning of a paragraph. 
2764          * I decided to handle blanks like normal characters, the main 
2765          * difference are the special checks when calculating the row.fill
2766          * (blank does not count at the end of a row) and the check here */ 
2767         
2768         LyXFont realtmpfont = real_current_font;
2769         LyXFont rawtmpfont = current_font; /* store the current font.
2770                                             * This is because of the use
2771                                             * of cursor movements. The moving
2772                                             * cursor would refresh the 
2773                                             * current font */
2774
2775         // Get the font that is used to calculate the baselineskip
2776         LyXParagraph::size_type const lastpos = 
2777                 cursor.par->Last();
2778         LyXFont rawparfont = cursor.par->GetFontSettings(lastpos - 1);
2779
2780         jumped_over_space = false;
2781         if (IsLineSeparatorChar(c)) {
2782                 
2783                 /* avoid double blanks but insert the new blank because
2784                  * of a possible font change */
2785                 if (cursor.pos < lastpos &&
2786                     cursor.par->IsLineSeparator(cursor.pos))
2787                 {
2788                         cursor.par->Erase(cursor.pos);
2789                         jumped_over_space = true;
2790                 }
2791                 else if ((cursor.pos > 0 && 
2792                           cursor.par->IsLineSeparator(cursor.pos - 1))
2793                          || (cursor.pos > 0 && cursor.par->IsNewline(cursor.pos - 1))
2794                           || (cursor.pos == 0 &&
2795                               !(cursor.par->Previous()
2796                               && cursor.par->Previous()->footnoteflag
2797                               == LyXParagraph::OPEN_FOOTNOTE)))
2798                         return;
2799         }
2800         else if (IsNewlineChar(c)) {
2801             if (!IsEmptyTableCell()) {
2802                 TableFeatures(LyXTable::APPEND_CONT_ROW);
2803                 CursorDown();
2804             }
2805           return;
2806         }
2807    
2808         row = cursor.row;
2809         y = cursor.y - row->baseline;
2810         if (c != LyXParagraph::META_INSET)      /* in this case LyXText::InsertInset 
2811                                          * already inserted the character */
2812                 cursor.par->InsertChar(cursor.pos, c);
2813         SetCharFont(cursor.par, cursor.pos, rawtmpfont);
2814
2815         if (!jumped_over_space) {
2816                 /* refresh the positions */
2817                 tmprow = row;
2818                 while (tmprow->next && tmprow->next->par == row->par) {
2819                         tmprow = tmprow->next;
2820                         tmprow->pos++;
2821                 }
2822         }
2823
2824         cursor.pos++;
2825
2826         CheckParagraphInTable(cursor.par, cursor.pos);
2827         
2828         current_font = rawtmpfont;
2829         real_current_font = realtmpfont;
2830         
2831         /* check, whether the last character's font has changed. */
2832         if (cursor.pos && cursor.pos == cursor.par->Last()
2833             && rawparfont != rawtmpfont)
2834                 RedoHeightOfParagraph(cursor);
2835 }
2836
2837
2838 void LyXText::CheckParagraphInTable(LyXParagraph * par,
2839                                     LyXParagraph::size_type pos)
2840 {
2841         
2842         if (par->GetChar(pos) == LyXParagraph::META_INSET &&
2843             par->GetInset(pos) && par->GetInset(pos)->display()){
2844           par->GetInset(pos)->display(false);
2845         }
2846
2847         long y;
2848         Row * row = GetRow(par, pos, y);
2849         
2850         int tmpheight = row->height;
2851         SetHeightOfRow(row);
2852
2853         LyXParagraph::size_type tmp_pos = pos;
2854         /* update the table information */
2855         while (tmp_pos && !par->IsNewline(tmp_pos - 1))
2856                 --tmp_pos;
2857         if (par->table->SetWidthOfCell(NumberOfCell(par, pos),
2858                                        WidthOfCell(par, tmp_pos))) {
2859                 LyXCursor tmpcursor = cursor;
2860                 SetCursorIntern(par, pos);
2861                 /* make a complete redraw */
2862                 RedoDrawingOfParagraph(cursor);
2863                 cursor = tmpcursor;
2864         }
2865         else {
2866                 /* redraw only the row */
2867                 LyXCursor tmpcursor = cursor;
2868                 SetCursorIntern(par, pos);
2869                 refresh_y = y;
2870                 refresh_x = cursor.x;
2871                 refresh_row = row;
2872                 refresh_pos = cursor.pos;
2873                 cursor = tmpcursor;
2874                 
2875                 if (tmpheight == row->height)
2876                         status = LyXText::NEED_VERY_LITTLE_REFRESH;
2877                 else
2878                         status = LyXText::NEED_MORE_REFRESH;
2879         }
2880         SetCursorIntern(cursor.par, cursor.pos);
2881 }
2882
2883
2884 void LyXText::BackspaceInTable()
2885 {
2886         Row * tmprow, * row;
2887         long y;
2888         
2889         LyXFont rawtmpfont = current_font;
2890         LyXFont realtmpfont = real_current_font;
2891
2892         // Get the font that is used to calculate the baselineskip
2893         int const lastpos = cursor.par->Last();
2894         LyXFont rawparfont = cursor.par->GetFontSettings(lastpos - 1);
2895         
2896         if (cursor.pos == 0) {
2897                 /* no pasting of table paragraphs */
2898                 
2899                 CursorLeft();
2900         }
2901         else {
2902                 /* this is the code for a normal backspace, not pasting
2903                  * any paragraphs */ 
2904           SetUndo(Undo::DELETE, 
2905                   cursor.par->ParFromPos(cursor.pos)->previous, 
2906                   cursor.par->ParFromPos(cursor.pos)->next); 
2907           
2908                 CursorLeftIntern();
2909                 
2910                 /* some insets are undeletable here */
2911                 if (cursor.par->GetChar(cursor.pos) == LyXParagraph::META_INSET) {
2912                         if (!cursor.par->GetInset(cursor.pos)->Deletable())
2913                                 return;
2914                 }
2915                 
2916                 row = cursor.row;
2917                 y = cursor.y - row->baseline;
2918                 
2919                 /* some special code when deleting a newline. */
2920                 if (cursor.par->IsNewline(cursor.pos)) {
2921                         /* nothing :-) */
2922                         return;
2923                 }
2924                 else {
2925                         cursor.par->Erase(cursor.pos);
2926                         
2927                         /* refresh the positions */
2928                         tmprow = row;
2929                         while (tmprow->next && tmprow->next->par == row->par) {
2930                                 tmprow = tmprow->next;
2931                                 tmprow->pos--;
2932                         }
2933                         
2934                         /* delete superfluous blanks */ 
2935                         if (cursor.pos < cursor.par->Last() - 1 &&
2936                         (cursor.par->IsLineSeparator(cursor.pos))) {
2937                                 
2938                                 if (cursor.pos == BeginningOfMainBody(cursor.par)
2939                                 || !cursor.pos 
2940                                 || cursor.par->IsLineSeparator(cursor.pos - 1)) {
2941                                         cursor.par->Erase(cursor.pos);
2942                                         /* refresh the positions */
2943                                         tmprow = row;
2944                                         while (tmprow->next && 
2945                                                tmprow->next->par == row->par) {
2946                                                 tmprow = tmprow->next;
2947                                                 tmprow->pos--;
2948                                         }
2949                                         if (cursor.pos)   /* move one character left */
2950                                                 cursor.pos--;
2951                                 }
2952                         }
2953                 }
2954       
2955                 CheckParagraphInTable(cursor.par, cursor.pos);
2956       
2957                 /* check, wether the last characters font has changed. */ 
2958                 if (cursor.pos && cursor.pos == cursor.par->Last()
2959                     && rawparfont != rawtmpfont)
2960                         RedoHeightOfParagraph(cursor);
2961
2962                 /* restore the current font 
2963                  * That is what a user expects! */
2964                 current_font = rawtmpfont;
2965                 real_current_font = realtmpfont;
2966         }
2967         SetCursorIntern(cursor.par, cursor.pos);
2968 }
2969
2970 /* table stuff -- end*/
2971
2972
2973 /* just a macro to make some thing easier. */ 
2974 void LyXText::RedoParagraph() const
2975 {
2976 #if 1
2977         // I suspect this version will work
2978         // also.
2979         ClearSelection();
2980         RedoParagraphs(cursor, cursor.par->Next());
2981         SetCursorIntern(cursor.par, cursor.pos);
2982 #else
2983         LyXCursor tmpcursor = cursor;
2984         ClearSelection();
2985         RedoParagraphs(cursor, cursor.par->Next());
2986         SetCursorIntern(tmpcursor.par, tmpcursor.pos);
2987 #endif
2988 }
2989
2990
2991 /* insert a character, moves all the following breaks in the 
2992  * same Paragraph one to the right and make a rebreak */
2993 void  LyXText::InsertChar(char c)
2994 {
2995         SetUndo(Undo::INSERT, 
2996                 cursor.par->ParFromPos(cursor.pos)->previous, 
2997                 cursor.par->ParFromPos(cursor.pos)->next);
2998
2999         /* When the free-spacing option is set for the current layout,
3000          * all spaces are converted to protected spaces. */
3001         bool freeSpacingBo = 
3002                 textclasslist.Style(parameters->textclass,
3003                                cursor.row->par->GetLayout()).free_spacing;
3004    
3005         if (freeSpacingBo && IsLineSeparatorChar(c) 
3006             && (!cursor.pos || cursor.par->IsLineSeparator(cursor.pos - 1))) 
3007                 c = LyXParagraph::META_PROTECTED_SEPARATOR;
3008    
3009         /* table stuff -- begin*/
3010         if (cursor.par->table) {
3011                 InsertCharInTable(c);
3012                 charInserted();
3013                 return;
3014         }
3015         /* table stuff -- end*/
3016    
3017         /* First check, if there will be two blanks together or a blank at 
3018           the beginning of a paragraph. 
3019           I decided to handle blanks like normal characters, the main 
3020           difference are the special checks when calculating the row.fill
3021           (blank does not count at the end of a row) and the check here */ 
3022
3023         // The bug is triggered when we type in a description environment:
3024         // The current_font is not changed when we go from label to main text
3025         // and it should (along with realtmpfont) when we type the space.
3026 #ifdef WITH_WARNINGS
3027 #warning There is a bug here! (Asger)
3028 #endif
3029         
3030         LyXFont realtmpfont = real_current_font;
3031         LyXFont rawtmpfont = current_font;  /* store the current font.
3032                                      * This is because of the use
3033                                      * of cursor movements. The moving
3034                                      * cursor would refresh the 
3035                                      * current font */
3036
3037         // Get the font that is used to calculate the baselineskip
3038         LyXParagraph::size_type lastpos = cursor.par->Last();
3039         LyXFont rawparfont = cursor.par->GetFontSettings(lastpos - 1);
3040
3041         bool jumped_over_space = false;
3042    
3043         if (IsLineSeparatorChar(c)) {
3044 #ifndef FIX_DOUBLE_SPACE
3045                 if (cursor.pos < lastpos
3046                     && cursor.par->IsLineSeparator(cursor.pos)) {
3047                         /* the user inserted a space before a space. So we
3048                          * will just make a CursorRight. BUT: The font of this
3049                          * space should be set to current font. That is why
3050                          * we need to rebreak perhaps. If there is a protected
3051                          * blank at the end of a row we have to force
3052                          * a rebreak.*/ 
3053            
3054                         current_view->owner()->getMiniBuffer()
3055                                 ->Set(_("You cannot type two spaces this way. "
3056                                         " Please read the Tutorial."));
3057 #if 1
3058                         // How can this ever happen?
3059                         if (cursor.pos == RowLast(cursor.row)
3060                             && !IsLineSeparatorChar(c))
3061                                 cursor.row->fill = -1;  // force rebreak
3062                         cursor.par->Erase(cursor.pos);
3063                         jumped_over_space = true;
3064 #else
3065                         // Seems to me that this works just as well.
3066                         CursorRight();
3067                         charInserted();
3068                         return;
3069 #endif
3070                 } else
3071 #endif   
3072                 if ((cursor.pos > 0 
3073                      && cursor.par->IsLineSeparator(cursor.pos - 1))
3074                     || (cursor.pos > 0
3075                         && cursor.par->IsNewline(cursor.pos - 1))
3076                     || (cursor.pos == 0
3077                         && !(cursor.par->Previous()
3078                              && cursor.par->Previous()->footnoteflag
3079                              == LyXParagraph::OPEN_FOOTNOTE))) {
3080                         if (cursor.pos == 0 )
3081                                 current_view->owner()->getMiniBuffer()->Set(_("You cannot insert a space at the beginning of a paragraph.  Please read the Tutorial."));
3082                         else
3083                                 current_view->owner()->getMiniBuffer()->Set(_("You cannot type two spaces this way.  Please read the Tutorial."));
3084                         charInserted();
3085                         return;
3086                 }
3087         } else if (IsNewlineChar(c)) {
3088                 if (cursor.par->FirstPhysicalPar() == cursor.par
3089                     && cursor.pos <= BeginningOfMainBody(cursor.par)) {
3090                         charInserted();
3091                         return;
3092                 }
3093                 /* No newline at first position 
3094                  * of a paragraph or behind labels. 
3095                  * TeX does not allow that. */
3096                 
3097                 if (cursor.pos < cursor.par->Last() &&
3098                     cursor.par->IsLineSeparator(cursor.pos))
3099                         CursorRightIntern(); // newline always after a blank!
3100                 cursor.row->fill = -1;         // to force a new break
3101         }
3102    
3103         // the display inset stuff
3104         if (cursor.row->par->GetChar(cursor.row->pos) == LyXParagraph::META_INSET
3105             && cursor.row->par->GetInset(cursor.row->pos)
3106             && cursor.row->par->GetInset(cursor.row->pos)->display())
3107                 cursor.row->fill = -1; // to force a new break  
3108
3109         // get the cursor row fist
3110         Row * row = cursor.row;
3111         long y = cursor.y - row->baseline;
3112         if (c != LyXParagraph::META_INSET) /* Here case LyXText::InsertInset 
3113                                             * already insertet the character */
3114                 cursor.par->InsertChar(cursor.pos, c);
3115         SetCharFont(cursor.par, cursor.pos, rawtmpfont);
3116
3117         if (!jumped_over_space) {
3118                 // refresh the positions
3119                 Row * tmprow = row;
3120                 while (tmprow->next && tmprow->next->par == row->par) {
3121                         tmprow = tmprow->next;
3122                         tmprow->pos++;
3123                 }
3124         }
3125    
3126         // Is there a break one row above
3127         if ((cursor.par->IsLineSeparator(cursor.pos)
3128              || cursor.par->IsNewline(cursor.pos)
3129              || cursor.row->fill == -1)
3130             && row->previous && row->previous->par == row->par) {
3131                 LyXParagraph::size_type z = NextBreakPoint(row->previous,
3132                                                            paperwidth);
3133                 if ( z >= row->pos) {
3134                         row->pos = z + 1;
3135                         
3136                         // set the dimensions of the row above
3137                         row->previous->fill = Fill(row->previous, paperwidth);
3138
3139                         SetHeightOfRow(row->previous);
3140              
3141                         y -= row->previous->height;
3142                         refresh_y = y;
3143                         refresh_row = row->previous;
3144                         status = LyXText::NEED_MORE_REFRESH;
3145              
3146                         BreakAgainOneRow(row);
3147
3148                         current_font = rawtmpfont;
3149                         real_current_font = realtmpfont;
3150                         SetCursor(cursor.par, cursor.pos + 1, false);
3151                         /* cursor MUST be in row now */
3152              
3153                         if (row->next && row->next->par == row->par)
3154                                 need_break_row = row->next;
3155                         else
3156                                 need_break_row = 0;
3157              
3158                         // check, wether the last characters font has changed. 
3159                         if (cursor.pos && cursor.pos == cursor.par->Last()
3160                             && rawparfont != rawtmpfont)
3161                                 RedoHeightOfParagraph(cursor);
3162                         
3163                         charInserted();
3164                         return;
3165                 }
3166         }
3167    
3168         /* recalculate the fill of the row */ 
3169         if (row->fill >= 0)  /* needed because a newline
3170                               * will set fill to -1. Otherwise
3171                               * we would not get a rebreak! */
3172                 row->fill = Fill(row, paperwidth);
3173         if (row->fill < 0 ) {
3174                 refresh_y = y;
3175                 refresh_row = row; 
3176                 refresh_x = cursor.x;
3177                 refresh_pos = cursor.pos;
3178                 status = LyXText::NEED_MORE_REFRESH;
3179                 BreakAgainOneRow(row); 
3180                 /* will the cursor be in another row now? */ 
3181                 if (RowLast(row) <= cursor.pos + 1 && row->next) {
3182                         if (row->next && row->next->par == row->par)
3183                                 /* this should
3184                                  * always be true */
3185                                 row = row->next;
3186                         BreakAgainOneRow(row);
3187                 }
3188                 current_font = rawtmpfont;
3189                 real_current_font = realtmpfont;
3190                 SetCursor(cursor.par, cursor.pos + 1, false);
3191                 if (row->next && row->next->par == row->par)
3192                         need_break_row = row->next;
3193                 else
3194                         need_break_row = 0;             
3195         } else {
3196                 refresh_y = y;
3197                 refresh_x = cursor.x;
3198                 refresh_row = row;
3199                 refresh_pos = cursor.pos;
3200                 
3201                 int tmpheight = row->height;
3202                 SetHeightOfRow(row);
3203                 if (tmpheight == row->height)
3204                         status = LyXText::NEED_VERY_LITTLE_REFRESH;
3205                 else
3206                         status = LyXText::NEED_MORE_REFRESH;
3207             
3208                 current_font = rawtmpfont;
3209                 real_current_font = realtmpfont;
3210                 SetCursor(cursor.par, cursor.pos + 1, false);
3211         }
3212
3213         /* check, wether the last characters font has changed. */ 
3214         if (cursor.pos && cursor.pos == cursor.par->Last()
3215             && rawparfont != rawtmpfont) {
3216                 RedoHeightOfParagraph(cursor);
3217         } else {
3218                 /* now the special right address boxes */
3219                 if (textclasslist.Style(parameters->textclass,
3220                                    cursor.par->GetLayout()).margintype
3221                     == MARGIN_RIGHT_ADDRESS_BOX) {
3222                         RedoDrawingOfParagraph(cursor); 
3223                 }
3224         }
3225
3226         charInserted();
3227 }
3228    
3229
3230 void LyXText::charInserted()
3231 {
3232         // Here we could call FinishUndo for every 20 characters inserted.
3233         // This is from my experience how emacs does it.
3234         static unsigned int counter = 0;
3235         if (counter < 20) {
3236                 ++counter;
3237         } else {
3238                 FinishUndo();
3239                 counter = 0;
3240         }
3241 }
3242
3243 void LyXText::PrepareToPrint(Row * row, float & x,
3244                              float & fill_separator, 
3245                              float & fill_hfill,
3246                              float & fill_label_hfill,
3247                              bool bidi) const
3248 {
3249         float nh, nlh, ns;
3250         
3251         float w = row->fill;
3252         fill_hfill = 0;
3253         fill_label_hfill = 0;
3254         fill_separator = 0;
3255         fill_label_hfill = 0;
3256
3257         LyXDirection direction = row->par->getParDirection();
3258
3259         if (direction == LYX_DIR_RIGHT_TO_LEFT) {
3260                 x = RightMargin(row);
3261                 if (row->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
3262                         LyXFont font(LyXFont::ALL_SANE);
3263                         font.setSize(LyXFont::SIZE_SMALL);
3264                         x += font.textWidth("Mwide-figM", 10);
3265                 }
3266         }
3267         else
3268                 x = LeftMargin(row);
3269         
3270         /* is there a manual margin with a manual label */ 
3271         if (textclasslist.Style(parameters->textclass,
3272                            row->par->GetLayout()).margintype == MARGIN_MANUAL
3273             && textclasslist.Style(parameters->textclass,
3274                               row->par->GetLayout()).labeltype == LABEL_MANUAL) {
3275                
3276                 nlh = NumberOfLabelHfills(row) + 1; /* one more since labels 
3277                                                     * are left aligned*/ 
3278                 if (nlh && !row->par->GetLabelWidthString().empty()) {
3279                         fill_label_hfill = LabelFill(row) / nlh;
3280                 }
3281         }
3282                 
3283         /* are there any hfills in the row? */ 
3284         nh = NumberOfHfills(row);
3285         
3286 /* table stuff -- begin*/
3287         if (row->par->table) {
3288            w = paperwidth - row->par->table->WidthOfTable()
3289            - x - RightMargin(row);
3290            nh = 0; /* ignore hfills in tables */ 
3291         }
3292 /* table stuff -- end*/
3293
3294         if (nh)
3295           fill_hfill = w /nh;
3296         else  {
3297            /* is it block, flushleft or flushright? 
3298             * set x how you need it */
3299         int align;
3300         if (row->par->FirstPhysicalPar()->align == LYX_ALIGN_LAYOUT)
3301           align = textclasslist.Style(parameters->textclass, row->par->GetLayout()).align;
3302         else
3303           align = row->par->FirstPhysicalPar()->align;
3304            
3305            /* center displayed insets */ 
3306            if (row->par->GetChar(row->pos) == LyXParagraph::META_INSET
3307                && row->par->GetInset(row->pos)
3308                && row->par->GetInset(row->pos)->display())
3309              align = LYX_ALIGN_CENTER;
3310
3311            switch (align) {
3312             case LYX_ALIGN_BLOCK:
3313               ns = NumberOfSeparators(row);
3314               if (ns && row->next && row->next->par == row->par &&
3315                   !(row->next->par->IsNewline(row->next->pos-1))
3316                   && !(row->next->par->GetChar(row->next->pos) == LyXParagraph::META_INSET
3317                        && row->next->par->GetInset(row->next->pos)
3318                        && row->next->par->GetInset(row->next->pos)->display())
3319                   )
3320                 fill_separator = w / ns;
3321               else if (direction == LYX_DIR_RIGHT_TO_LEFT)
3322                 x += w;
3323               break;
3324             case LYX_ALIGN_RIGHT:
3325               x += w;
3326               break;
3327             case LYX_ALIGN_CENTER:
3328               x += w / 2;
3329               break;
3330            }
3331         }
3332         if (!bidi)
3333                 return;
3334
3335         ComputeBidiTables(row);
3336         if (direction == LYX_DIR_RIGHT_TO_LEFT) {
3337                 LyXParagraph::size_type main_body = 
3338                         BeginningOfMainBody(row->par);
3339                 LyXParagraph::size_type last = RowLast(row);
3340
3341                 if (row->pos <= last
3342                     && !row->par->table
3343                     && last != vis2log(last)
3344                     && row->par->IsLineSeparator(last)) {
3345                         if (!(main_body > 0 && main_body-1 == last))
3346                                 x -= fill_separator+SingleWidth(row->par,last);
3347                 } else if (main_body > 0 &&
3348                            (main_body-1 > last || 
3349                             !row->par->IsLineSeparator(main_body-1))) {
3350                         LyXLayout const & layout = textclasslist.Style(parameters->textclass,
3351                                                                        row->par->GetLayout());
3352                         x += GetFont(row->par, -2).stringWidth(layout.labelsep);
3353                         if (main_body-1 <= last)
3354                                 x += fill_label_hfill;
3355                 }
3356         }
3357 }
3358       
3359 /* important for the screen */
3360
3361
3362 /* the cursor set functions have a special mechanism. When they
3363 * realize, that you left an empty paragraph, they will delete it.
3364 * They also delete the corresponding row */
3365
3366 void LyXText::CursorRightOneWord() const
3367 {
3368         // treat floats, HFills and Insets as words
3369         LyXCursor tmpcursor = cursor;
3370
3371         if (tmpcursor.pos == tmpcursor.par->Last()
3372             && tmpcursor.par->Next())
3373         {
3374                         tmpcursor.par = tmpcursor.par->Next();
3375                         tmpcursor.pos = 0;
3376         } else {
3377                 int steps = 0;
3378
3379                 // Skip through initial nonword stuff.
3380                 while ( tmpcursor.pos < tmpcursor.par->Last() &&
3381                         ! tmpcursor.par->IsWord( tmpcursor.pos ) ) 
3382                 {
3383                   //    printf("Current pos1 %d", tmpcursor.pos) ;
3384                         tmpcursor.pos++;
3385                         ++steps;
3386                 }
3387                 // Advance through word.
3388                 while ( tmpcursor.pos < tmpcursor.par->Last() &&
3389                         tmpcursor.par->IsWord( tmpcursor.pos ) )
3390                 {
3391                   //     printf("Current pos2 %d", tmpcursor.pos) ;
3392                         tmpcursor.pos++;
3393                         ++steps;
3394                 }
3395         }
3396         SetCursor(tmpcursor.par, tmpcursor.pos);
3397 }
3398
3399
3400 void LyXText::CursorTab() const
3401 {
3402     if (cursor.par->table) {
3403         int cell = NumberOfCell(cursor.par, cursor.pos);
3404         while(cursor.par->table->IsContRow(cell)) {
3405             CursorUp();
3406             cell = NumberOfCell(cursor.par, cursor.pos);
3407         }
3408         if (cursor.par->table->ShouldBeVeryLastCell(cell))
3409             TableFeatures(LyXTable::APPEND_ROW);
3410     }
3411     LyXCursor tmpcursor = cursor;
3412     while (tmpcursor.pos < tmpcursor.par->Last()
3413            && !tmpcursor.par->IsNewline(tmpcursor.pos))
3414         tmpcursor.pos++;
3415    
3416     if (tmpcursor.pos == tmpcursor.par->Last()){
3417         if (tmpcursor.par->Next()) {
3418             tmpcursor.par = tmpcursor.par->Next();
3419             tmpcursor.pos = 0;
3420         }
3421     }
3422     else
3423         tmpcursor.pos++;
3424     SetCursor(tmpcursor.par, tmpcursor.pos);
3425     if (cursor.par->table) {
3426         int cell = NumberOfCell(cursor.par, cursor.pos);
3427         while (cursor.par->table->IsContRow(cell) &&
3428                !cursor.par->table->ShouldBeVeryLastCell(cell)) {
3429             tmpcursor = cursor;
3430             while (tmpcursor.pos < tmpcursor.par->Last()
3431                    && !tmpcursor.par->IsNewline(tmpcursor.pos))
3432                 tmpcursor.pos++;
3433    
3434             if (tmpcursor.pos == tmpcursor.par->Last()){
3435                 if (tmpcursor.par->Next()) {
3436                     tmpcursor.par = tmpcursor.par->Next();
3437                     tmpcursor.pos = 0;
3438                 }
3439             }
3440             else
3441                 tmpcursor.pos++;
3442             SetCursor(tmpcursor.par, tmpcursor.pos);
3443             cell = NumberOfCell(cursor.par, cursor.pos);
3444         }
3445     }
3446 }
3447
3448
3449 /* -------> Skip initial whitespace at end of word and move cursor to *start*
3450             of prior word, not to end of next prior word. */
3451
3452 void LyXText::CursorLeftOneWord()  const
3453 {
3454         // treat HFills, floats and Insets as words
3455         LyXCursor tmpcursor = cursor;
3456         while (tmpcursor.pos 
3457                && (tmpcursor.par->IsSeparator(tmpcursor.pos - 1) 
3458                    || tmpcursor.par->IsKomma(tmpcursor.pos - 1))
3459                && !(tmpcursor.par->IsHfill(tmpcursor.pos - 1)
3460                     || tmpcursor.par->IsFloat(tmpcursor.pos - 1)
3461                     || tmpcursor.par->IsInset(tmpcursor.pos - 1)))
3462                 tmpcursor.pos--;
3463
3464         if (tmpcursor.pos
3465             && (tmpcursor.par->IsInset(tmpcursor.pos - 1)
3466                 || tmpcursor.par->IsFloat(tmpcursor.pos - 1)
3467                 || tmpcursor.par->IsHfill(tmpcursor.pos - 1))) {
3468                 tmpcursor.pos--;
3469         } else if (!tmpcursor.pos) {
3470                 if (tmpcursor.par->Previous()){
3471                         tmpcursor.par = tmpcursor.par->Previous();
3472                         tmpcursor.pos = tmpcursor.par->Last();
3473                 }
3474         } else {                // Here, tmpcursor != 0 
3475                 while (tmpcursor.pos > 0 &&
3476                        tmpcursor.par->IsWord(tmpcursor.pos-1) )
3477                         tmpcursor.pos-- ;
3478         }
3479         SetCursor(tmpcursor.par, tmpcursor.pos);
3480 }
3481
3482 /* -------> Select current word. This depends on behaviour of CursorLeftOneWord(), so it is
3483                         patched as well. */
3484
3485 void LyXText::SelectWord() 
3486 {
3487         /* Move cursor to the beginning, when not already there. */
3488         if ( cursor.pos
3489              && !cursor.par->IsSeparator(cursor.pos-1)
3490              && !cursor.par->IsKomma(cursor.pos-1) )
3491                 CursorLeftOneWord();
3492
3493         /* set the sel cursor */
3494         sel_cursor = cursor;
3495
3496         while ( cursor.pos < cursor.par->Last()
3497                         && !cursor.par->IsSeparator(cursor.pos)
3498                         && !cursor.par->IsKomma(cursor.pos) )
3499                 cursor.pos++;
3500         SetCursor( cursor.par, cursor.pos );
3501         
3502         /* finally set the selection */ 
3503         SetSelection();
3504 }
3505
3506
3507 /* -------> Select the word currently under the cursor when:
3508                         1: no selection is currently set,
3509                         2: the cursor is not at the borders of the word. */
3510
3511 int LyXText::SelectWordWhenUnderCursor() 
3512 {
3513         if ( selection ) return 0;
3514         if ( cursor.pos < cursor.par->Last()
3515                  && !cursor.par->IsSeparator(cursor.pos)
3516                  && !cursor.par->IsKomma(cursor.pos)
3517                  && cursor.pos 
3518                  && !cursor.par->IsSeparator(cursor.pos -1)
3519                  && !cursor.par->IsKomma(cursor.pos -1) ) {
3520                 SelectWord();
3521                 return 1;
3522         }
3523         return 0;
3524 }
3525
3526
3527 // This function is only used by the spellchecker for NextWord().
3528 // It doesn't handle LYX_ACCENTs and probably never will.
3529 char * LyXText::SelectNextWord(float & value)
3530 {
3531         LyXParagraph * tmppar = cursor.par;
3532         
3533         // If this is not the very first word, skip rest of
3534         // current word because we are probably in the middle
3535         // of a word if there is text here.
3536         if (cursor.pos || cursor.par->previous) {
3537                 while (cursor.pos < cursor.par->Last()
3538                        && cursor.par->IsLetter(cursor.pos))
3539                         cursor.pos++;
3540         }
3541         // Now, skip until we have real text (will jump paragraphs)
3542         while ((cursor.par->Last() > cursor.pos
3543                 && (!cursor.par->IsLetter(cursor.pos)
3544                     || cursor.par->getFont(cursor.pos).latex() == LyXFont::ON))
3545                || (cursor.par->Last() == cursor.pos
3546                    && cursor.par->Next())){
3547                 if (cursor.pos == cursor.par->Last()) {
3548                         cursor.par = cursor.par->Next();
3549                         cursor.pos = 0;
3550                 }
3551                 else
3552                         cursor.pos++;
3553         }
3554   
3555         // Update the value if we changed paragraphs
3556         if (cursor.par != tmppar){
3557                 SetCursor(cursor.par, cursor.pos);
3558                 value = float(cursor.y)/float(height);
3559         }
3560
3561         /* Start the selection from here */
3562         sel_cursor = cursor;
3563
3564         string latex;
3565    
3566         /* and find the end of the word 
3567            (optional hyphens are part of a word) */
3568         while (cursor.pos < cursor.par->Last()
3569                && (cursor.par->IsLetter(cursor.pos)) 
3570                    || (cursor.par->GetChar(cursor.pos) == LyXParagraph::META_INSET &&
3571                        cursor.par->GetInset(cursor.pos) != 0 &&
3572                        cursor.par->GetInset(cursor.pos)->Latex(latex, 0) == 0 &&
3573                        latex == "\\-"))
3574                 cursor.pos++;
3575
3576         // Finally, we copy the word to a string and return it
3577         char * str = 0;
3578
3579         if (sel_cursor.pos < cursor.pos) {
3580                 str = new char [cursor.pos - sel_cursor.pos + 2];
3581                 LyXParagraph::size_type i, j;
3582                 for (i = sel_cursor.pos, j = 0; i < cursor.pos; ++i) {
3583                         if (cursor.par->GetChar(i) != LyXParagraph::META_INSET)
3584                                 str[j++] = cursor.par->GetChar(i);
3585                 }
3586                 str[j] = '\0';
3587         }
3588         return str;
3589 }
3590
3591
3592 // This one is also only for the spellchecker
3593 void LyXText::SelectSelectedWord()
3594 {
3595         /* move cursor to the beginning */
3596         SetCursor(sel_cursor.par, sel_cursor.pos);
3597         
3598         /* set the sel cursor */
3599         sel_cursor = cursor;
3600
3601         string latex;
3602         
3603         /* now find the end of the word */
3604         while (cursor.pos < cursor.par->Last()
3605                && (cursor.par->IsLetter(cursor.pos)
3606                    || (cursor.par->GetChar(cursor.pos) == LyXParagraph::META_INSET &&
3607                        cursor.par->GetInset(cursor.pos) != 0 &&
3608                        cursor.par->GetInset(cursor.pos)->Latex(latex, 0) == 0 &&
3609                        latex == "\\-")))
3610                 cursor.pos++;
3611         
3612         SetCursor(cursor.par, cursor.pos);
3613         
3614         /* finally set the selection */ 
3615         SetSelection();
3616 }
3617
3618
3619 /* -------> Delete from cursor up to the end of the current or next word. */
3620 void LyXText::DeleteWordForward()
3621 {
3622         LyXCursor tmpcursor = cursor;
3623         
3624         if (!cursor.par->Last())
3625                 CursorRight();
3626         else {
3627                 /* -------> Skip initial non-word stuff. */
3628                 while ( cursor.pos < cursor.par->Last() 
3629                         && (cursor.par->IsSeparator(cursor.pos)
3630                             || cursor.par->IsKomma(cursor.pos)) )
3631                         cursor.pos++;
3632                 
3633                 SetCursorIntern(cursor.par, cursor.pos);
3634                 selection = True; // to avoid deletion 
3635                 CursorRightOneWord();
3636                 sel_cursor = cursor;
3637                 cursor = tmpcursor;
3638                 SetSelection(); 
3639                 
3640                 /* -----> Great, CutSelection() gets rid of multiple spaces. */
3641                 CutSelection();
3642         }
3643 }
3644
3645
3646 /* -------> Delete from cursor to start of current or prior word. */
3647 void LyXText::DeleteWordBackward()
3648 {
3649        LyXCursor tmpcursor = cursor;
3650        if (!cursor.par->Last())
3651          CursorLeft();
3652        else{
3653          selection = true; // to avoid deletion 
3654          CursorLeftOneWord();
3655          sel_cursor = cursor;
3656          cursor = tmpcursor;
3657          SetSelection();
3658          CutSelection();
3659        }
3660 }
3661
3662
3663 /* -------> Kill to end of line. */
3664 void LyXText::DeleteLineForward()
3665 {
3666         LyXCursor tmpcursor = cursor;
3667         if (!cursor.par->Last())
3668                 CursorRight();
3669         else {
3670                 CursorEnd();
3671                 sel_cursor = cursor;
3672                 cursor = tmpcursor;
3673                 SetSelection();
3674                 if (selection == false) {
3675                         DeleteWordForward();
3676                 } else {
3677                         CutSelection();
3678                 }
3679         }
3680 }
3681
3682
3683 // Change the case of a word at cursor position. The meaning of action
3684 // is:
3685 // 0  change to lowercase
3686 // 1  capitalize word
3687 // 2  change to uppercase
3688 // This function directly manipulates LyXParagraph::text because there
3689 // is no LyXParagraph::SetChar currently. I did what I could to ensure
3690 // that it is correct. I guess part of it should be moved to
3691 // LyXParagraph, but it will have to change for 1.1 anyway. At least
3692 // it does not access outside of the allocated array as the older
3693 // version did. (JMarc) 
3694 void LyXText::ChangeWordCase(LyXText::TextCase action) 
3695 {
3696         LyXParagraph * tmppar = cursor.par->ParFromPos(cursor.pos);
3697
3698         SetUndo(Undo::FINISH, tmppar->previous, tmppar->next); 
3699
3700         LyXParagraph::size_type tmppos = 
3701                 cursor.par->PositionInParFromPos(cursor.pos);
3702         while (tmppos < tmppar->size()) {
3703                 unsigned char c = tmppar->text[tmppos];
3704                 if (IsKommaChar(c) || IsLineSeparatorChar(c))
3705                         break;
3706                 if (c != LyXParagraph::META_INSET) {
3707                         switch (action) {
3708                         case text_lowercase:
3709                                 c = tolower(c);
3710                                 break;
3711                         case text_capitalization:
3712                                 c = toupper(c);
3713                                 action = text_lowercase;
3714                                 break;
3715                         case text_uppercase:
3716                                 c = toupper(c);
3717                                 break;
3718                         }
3719                 }
3720                 
3721                 tmppar->text[tmppos] = c;
3722                 ++tmppos;
3723         }
3724         CheckParagraph(tmppar, tmppos);
3725         CursorRightOneWord();
3726 }
3727
3728
3729 void LyXText::Delete()
3730 {
3731         // this is a very easy implementation
3732
3733         LyXCursor old_cursor = cursor;
3734         int old_cur_par_id = old_cursor.par->id();
3735         int old_cur_par_prev_id = old_cursor.par->previous ?
3736                 old_cursor.par->previous->id() : 0;
3737         
3738         // just move to the right
3739         CursorRightIntern();
3740
3741         // This check is not very good...
3742         // The CursorRightIntern calls DeleteEmptyParagrapgMechanism
3743         // and that can very well delete the par or par->previous in
3744         // old_cursor. Will a solution where we compare paragraph id's
3745         //work better?
3746 #if 1
3747         if ((cursor.par->previous ? cursor.par->previous->id() : 0)
3748             == old_cur_par_prev_id
3749             && cursor.par->id() != old_cur_par_id)
3750                 return; // delete-empty-paragraph-mechanism has done it
3751 #else
3752         if (cursor.par->previous == old_cursor.par->previous
3753             && cursor.par != old_cursor.par)
3754                 return; // delete-empty-paragraph-mechanism has done it
3755 #endif
3756         // if you had success make a backspace
3757         if (old_cursor.par != cursor.par || old_cursor.pos != cursor.pos) {
3758                 LyXCursor tmpcursor = cursor;
3759                 cursor = old_cursor; // to make sure undo gets the right cursor position
3760                 SetUndo(Undo::DELETE, 
3761                         cursor.par->ParFromPos(cursor.pos)->previous, 
3762                         cursor.par->ParFromPos(cursor.pos)->next); 
3763                 cursor = tmpcursor;
3764                 Backspace();
3765         }
3766 }
3767
3768
3769 void  LyXText::Backspace()
3770 {
3771         LyXParagraph * tmppar;
3772         Row * tmprow, * row;
3773         long y;
3774         int tmpheight;
3775
3776         /* table stuff -- begin */
3777         if (cursor.par->table) {
3778                 BackspaceInTable();
3779                 return;
3780         }
3781         /* table stuff -- end */
3782         
3783         LyXFont rawtmpfont = current_font;
3784         LyXFont realtmpfont = real_current_font;
3785    
3786         // Get the font that is used to calculate the baselineskip
3787         int const lastpos = cursor.par->Last();
3788         LyXFont rawparfont = cursor.par->GetFontSettings(lastpos - 1);
3789
3790         if (cursor.pos == 0) {
3791                 // we may paste some paragraphs
3792       
3793                 // is it an empty paragraph?
3794       
3795                 if ((lastpos == 0
3796                      || (lastpos == 1 && cursor.par->IsSeparator(0)))
3797                     && !(cursor.par->Next() 
3798                          && cursor.par->footnoteflag == 
3799                          LyXParagraph::NO_FOOTNOTE
3800                          && cursor.par->Next()->footnoteflag == 
3801                          LyXParagraph::OPEN_FOOTNOTE)) {
3802                         
3803                         if (cursor.par->previous) {
3804                                 tmppar = cursor.par->previous->FirstPhysicalPar();
3805                                 if (cursor.par->GetLayout() == tmppar->GetLayout()
3806                                     && cursor.par->footnoteflag == tmppar->footnoteflag
3807                                     && cursor.par->GetAlign() == tmppar->GetAlign()) {
3808                                         
3809                                         tmppar->line_bottom = cursor.par->line_bottom;
3810                                         tmppar->added_space_bottom = cursor.par->added_space_bottom;
3811                                         tmppar->pagebreak_bottom = cursor.par->pagebreak_bottom;
3812                                 }
3813                                 
3814                                 CursorLeftIntern();
3815                      
3816                                 // the layout things can change the height of a row !
3817                                 tmpheight = cursor.row->height;
3818                                 SetHeightOfRow(cursor.row);
3819                                 if (cursor.row->height != tmpheight) {
3820                                         refresh_y = cursor.y - cursor.row->baseline;
3821                                         refresh_row = cursor.row;
3822                                         status = LyXText::NEED_MORE_REFRESH;
3823                                 }
3824                                 return;
3825                         }
3826                 }
3827                 if (cursor.par->ParFromPos(cursor.pos)->previous){
3828                         SetUndo(Undo::DELETE,
3829                                 cursor.par->ParFromPos(cursor.pos)->previous->previous,
3830                                 cursor.par->ParFromPos(cursor.pos)->next);
3831                 }
3832                 tmppar = cursor.par;
3833                 tmprow = cursor.row;
3834                 CursorLeftIntern();
3835                 /* Pasting is not allowed, if the paragraphs have different
3836                    layout. I think it is a real bug of all other
3837                    word processors to allow it. It confuses the user.
3838                    Even so with a footnote paragraph and a non-footnote
3839                    paragraph. I will not allow pasting in this case, 
3840                    because the user would be confused if the footnote behaves 
3841                    different wether it is open or closed.
3842                   
3843                    Correction: Pasting is always allowed with standard-layout
3844                 */
3845                 if (cursor.par != tmppar
3846                     && (cursor.par->GetLayout() == tmppar->GetLayout()
3847                         || !tmppar->GetLayout())
3848                     && cursor.par->footnoteflag == tmppar->footnoteflag
3849                     /* table stuff -- begin*/
3850                     && !cursor.par->table /* no pasting of tables */ 
3851                     /* table stuff -- end*/
3852                     && cursor.par->GetAlign() == tmppar->GetAlign()) {
3853                         
3854                         cursor.par->PasteParagraph();
3855                         
3856                         if (!(cursor.pos &&
3857                               cursor.par->IsSeparator(cursor.pos - 1)))
3858                                 cursor.par->InsertChar(cursor.pos, ' ');
3859                         else
3860                                 if (cursor.pos)
3861                                         cursor.pos--;
3862                         
3863                         status = LyXText::NEED_MORE_REFRESH;
3864                         refresh_row = cursor.row;
3865                         refresh_y = cursor.y - cursor.row->baseline;
3866                         
3867                         // remove the lost paragraph
3868                         RemoveParagraph(tmprow);
3869                         RemoveRow(tmprow);  
3870                         
3871                         AppendParagraph(cursor.row);
3872                         UpdateCounters(cursor.row);
3873                         
3874                         // the row may have changed, block, hfills etc.
3875                         SetCursor(cursor.par, cursor.pos);
3876                 }
3877         } else {
3878                 /* this is the code for a normal backspace, not pasting
3879                  * any paragraphs */ 
3880                 SetUndo(Undo::DELETE, 
3881                         cursor.par->ParFromPos(cursor.pos)->previous, 
3882                         cursor.par->ParFromPos(cursor.pos)->next); 
3883                 CursorLeftIntern();
3884                 
3885                 // some insets are undeletable here
3886                 if (cursor.par->GetChar(cursor.pos) == LyXParagraph::META_INSET) {
3887                         if (!cursor.par->GetInset(cursor.pos)->Deletable())
3888                                 return; 
3889                         // force complete redo when erasing display insets
3890                         // this is a cruel mathod but save..... Matthias 
3891                         if (cursor.par->GetInset(cursor.pos)->display()){
3892                                 cursor.par->Erase(cursor.pos);
3893                                 RedoParagraph();
3894                                 return;
3895                         }
3896                 }
3897                 
3898                 row = cursor.row;
3899                 y = cursor.y - row->baseline;
3900                 LyXParagraph::size_type z;
3901                 /* remember that a space at the end of a row doesnt count
3902                  * when calculating the fill */ 
3903                 if (cursor.pos < RowLast(row) ||
3904                     !cursor.par->IsLineSeparator(cursor.pos)) {
3905                         row->fill += SingleWidth(cursor.par, cursor.pos);
3906                 }
3907                 
3908                 /* some special code when deleting a newline. This is similar
3909                  * to the behavior when pasting paragraphs */ 
3910                 if (cursor.pos && cursor.par->IsNewline(cursor.pos)) {
3911                         cursor.par->Erase(cursor.pos);
3912                         // refresh the positions
3913                         tmprow = row;
3914                         while (tmprow->next && tmprow->next->par == row->par) {
3915                                 tmprow = tmprow->next;
3916                                 tmprow->pos--;
3917                         }
3918                         if (cursor.par->IsLineSeparator(cursor.pos - 1))
3919                                 cursor.pos--;
3920                         
3921                         if (cursor.pos < cursor.par->Last() && !cursor.par->IsSeparator(cursor.pos)) {
3922                                 cursor.par->InsertChar(cursor.pos, ' ');
3923                                 // refresh the positions
3924                                 tmprow = row;
3925                                 while (tmprow->next && tmprow->next->par == row->par) {
3926                                         tmprow = tmprow->next;
3927                                         tmprow->pos++;
3928                                 }
3929                         }
3930                 } else {
3931                         cursor.par->Erase(cursor.pos);
3932                         
3933                         // refresh the positions
3934                         tmprow = row;
3935                         while (tmprow->next && tmprow->next->par == row->par) {
3936                                 tmprow = tmprow->next;
3937                                 tmprow->pos--;
3938                         }
3939
3940 #ifndef FIX_DOUBLE_SPACE
3941                         // delete superfluous blanks 
3942                         if (cursor.pos < cursor.par->Last() - 1 &&
3943                             (cursor.par->IsLineSeparator(cursor.pos))) {
3944                                 
3945                                 if (cursor.pos == BeginningOfMainBody(cursor.par)
3946                                     || !cursor.pos 
3947                                     || cursor.par->IsLineSeparator(cursor.pos - 1)) {
3948                                         cursor.par->Erase(cursor.pos);
3949                                         // refresh the positions
3950                                         tmprow = row;
3951                                         while (tmprow->next && 
3952                                                tmprow->next->par == row->par) {
3953                                                 tmprow = tmprow->next;
3954                                                 tmprow->pos--;
3955                                         }
3956                                         if (cursor.pos)   // move one character left
3957                                                 cursor.pos--;
3958                                 }
3959                         }
3960 #endif
3961                         
3962                         // delete newlines at the beginning of paragraphs
3963                         while (cursor.par->Last() &&
3964                                cursor.par->IsNewline(cursor.pos) &&
3965                                cursor.pos == BeginningOfMainBody(cursor.par)) {
3966                                 cursor.par->Erase(cursor.pos);
3967                                 // refresh the positions
3968                                 tmprow = row;
3969                                 while (tmprow->next && 
3970                                        tmprow->next->par == row->par) {
3971                                         tmprow = tmprow->next;
3972                                         tmprow->pos--;
3973                                 }
3974                         }
3975                 }
3976                 
3977                 // is there a break one row above
3978                 if (row->previous && row->previous->par == row->par) {
3979                         z = NextBreakPoint(row->previous, paperwidth);
3980                         if ( z >= row->pos) {
3981                                 row->pos = z + 1;
3982                                 
3983                                 tmprow = row->previous;
3984                                 
3985                                 // maybe the current row is now empty
3986                                 if (row->pos >= row->par->Last()) {
3987                                         // remove it
3988                                         RemoveRow(row);
3989                                         need_break_row = 0;
3990                                 } else {
3991                                         BreakAgainOneRow(row);
3992                                         if (row->next && row->next->par == row->par)
3993                                                 need_break_row = row->next;
3994                                         else
3995                                                 need_break_row = 0;
3996                                 }
3997                                 
3998                                 // set the dimensions of the row above
3999                                 y -= tmprow->height;
4000                                 tmprow->fill = Fill(tmprow, paperwidth);
4001                                 SetHeightOfRow(tmprow);
4002                                 
4003                                 refresh_y = y;
4004                                 refresh_row = tmprow;
4005                                 status = LyXText::NEED_MORE_REFRESH;
4006                                 current_font = rawtmpfont;
4007                                 real_current_font = realtmpfont;
4008                                 SetCursor(cursor.par, cursor.pos, false);
4009                                 // check, whether the last character's font has changed.
4010                                 rawtmpfont = cursor.par->GetFontSettings(cursor.par->Last() - 1);
4011                                 if (rawparfont != rawtmpfont)
4012                                         RedoHeightOfParagraph(cursor);
4013                                 return;
4014                         }
4015                 }
4016                 
4017                 // break the cursor row again
4018                 z = NextBreakPoint(row, paperwidth);
4019                 
4020                 if (z != RowLast(row) || 
4021                     (row->next && row->next->par == row->par &&
4022                      RowLast(row) == row->par->Last() - 1)){
4023                         
4024                         /* it can happen that a paragraph loses one row
4025                          * without a real breakup. This is when a word
4026                          * is to long to be broken. Well, I don t care this 
4027                          * hack ;-) */ 
4028                         if (row->next && row->next->par == row->par &&
4029                             RowLast(row) == row->par->Last() - 1)
4030                                 RemoveRow(row->next);
4031                         
4032                         refresh_y = y;
4033                         refresh_row = row;
4034                         status = LyXText::NEED_MORE_REFRESH;
4035                         
4036                         BreakAgainOneRow(row);
4037                         current_font = rawtmpfont; 
4038                         real_current_font = realtmpfont;
4039                         SetCursor(cursor.par, cursor.pos, false);
4040                         // cursor MUST be in row now
4041                         
4042                         if (row->next && row->next->par == row->par)
4043                                 need_break_row = row->next;
4044                         else
4045                                 need_break_row = 0;
4046                 } else  {
4047                         // set the dimensions of the row
4048                         row->fill = Fill(row, paperwidth);
4049                         int tmpheight = row->height;
4050                         SetHeightOfRow(row);
4051                         if (tmpheight == row->height)
4052                                 status = LyXText::NEED_VERY_LITTLE_REFRESH;
4053                         else
4054                                 status = LyXText::NEED_MORE_REFRESH;
4055                         refresh_y = y;
4056                         refresh_row = row;
4057                         current_font = rawtmpfont; 
4058                         real_current_font = realtmpfont;
4059                         SetCursor(cursor.par, cursor.pos, false);
4060                 }
4061         }
4062    
4063         // restore the current font
4064         // That is what a user expects!
4065         current_font = rawtmpfont; 
4066         real_current_font = realtmpfont;
4067         
4068         // check, wether the last characters font has changed.
4069         rawtmpfont = cursor.par->GetFontSettings(cursor.par->Last() - 1);
4070         if (rawparfont != rawtmpfont) {
4071                 RedoHeightOfParagraph(cursor);
4072         } else {
4073                 // now the special right address boxes
4074                 if (textclasslist.Style(parameters->textclass,
4075                                         cursor.par->GetLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
4076                         RedoDrawingOfParagraph(cursor); 
4077                 }
4078         }
4079 }
4080
4081
4082 #ifdef USE_PAINTER
4083 void LyXText::GetVisibleRow(int offset, 
4084                             Row * row_ptr, long y)
4085 {
4086         /* returns a printed row */
4087         Painter & pain = owner_->painter();
4088         
4089         LyXDirection direction = row_ptr->par->getParDirection();
4090         LyXParagraph::size_type vpos, pos, pos_end;
4091         float x, tmpx;
4092         int y_top, y_bottom;
4093         float fill_separator, fill_hfill, fill_label_hfill;
4094         LyXParagraph * par, * firstpar;
4095         LyXFont font;
4096         int maxdesc;
4097         if (row_ptr->height <= 0) {
4098                 lyxerr << "LYX_ERROR: row.height: " << row_ptr->height << endl;
4099                 return;
4100         }
4101         PrepareToPrint(row_ptr, x, fill_separator,
4102                        fill_hfill, fill_label_hfill);
4103
4104         /* initialize the pixmap */
4105         
4106         pain.fillRectangle(0, offset, paperwidth, row_ptr->height);
4107         
4108         // check for NOT FAST SELECTION
4109         if (!fast_selection && !mono_video && selection) {
4110                 /* selection code */ 
4111                 if (sel_start_cursor.row == row_ptr &&
4112                     sel_end_cursor.row == row_ptr) {
4113                         if (sel_start_cursor.x < sel_end_cursor.x)
4114                                 pain.fillRectangle(sel_start_cursor.x, offset,
4115                                                    sel_end_cursor.x - sel_start_cursor.x,
4116                                                    row_ptr->height,
4117                                                    LColor::selection);
4118                         else
4119                                 pain.fillRectangle(sel_end_cursor.x, offset,
4120                                                    sel_start_cursor.x - sel_end_cursor.x,
4121                                                    row_ptr->height,
4122                                                    LColor::selection);
4123                 }
4124                 else if (sel_start_cursor.row == row_ptr) {
4125                      if (direction == LYX_DIR_LEFT_TO_RIGHT)
4126                              pain.fillRectangle(sel_start_cursor.x, offset,
4127                                                 paperwidth - sel_start_cursor.x,
4128                                                 row_ptr->height,
4129                                                 LColor::selection);
4130                      else
4131                              pain.fillRectangle(0, offset,
4132                                                 sel_start_cursor.x,
4133                                                 row_ptr->height,
4134                                                 LColor::selection);
4135                 } else if (sel_end_cursor.row == row_ptr) {
4136                      if (direction == LYX_DIR_LEFT_TO_RIGHT)
4137                              pain.fillRectangle(0, offset,
4138                                                 sel_end_cursor.x,
4139                                                 row_ptr->height,
4140                                                 LColor::selection);
4141                      else
4142                              pain.fillRectangle(sel_end_cursor.x, offset,
4143                                                 paperwidth - sel_end_cursor.x,
4144                                                 row_ptr->height,
4145                                                 LColor::selection);
4146
4147                 } else if (y > sel_start_cursor.y && y < sel_end_cursor.y) {
4148                         pain.fillRectangle(0, offset,
4149                                            paperwidth, row_ptr->height,
4150                                            LColor::selection);
4151                 }
4152         } // end of NOT FAST SELECTION code
4153         
4154         if (row_ptr->par->appendix){
4155                 pain.line(1, offset,
4156                           1, offset + row_ptr->height,
4157                           LColor::appendixline);
4158                 pain.line(paperwidth - 2, offset,
4159                           paperwidth - 2, offset + row_ptr->height,
4160                           LColor::appendixline);
4161         }
4162
4163         if (row_ptr->par->pextra_type == LyXParagraph::PEXTRA_MINIPAGE) {
4164                 /* draw a marker at the left margin! */ 
4165                 LyXFont font = GetFont(row_ptr->par, 0);
4166                 int asc = font.maxAscent();
4167                 int x = (LYX_PAPER_MARGIN - font.width('|')) / 2;
4168                 int y1 = (offset + row_ptr->baseline);
4169                 int y2 = (offset + row_ptr->baseline) - asc;
4170                 pain.line(x, y1, x, y2, LColor::minipageline);
4171         }       
4172         if (row_ptr->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
4173                 LyXFont font(LyXFont::ALL_SANE);
4174                 font.setSize(LyXFont::SIZE_FOOTNOTE);
4175                 font.setColor(LColor::footnote);
4176                 
4177                 int box_x = LYX_PAPER_MARGIN;
4178                 box_x += font.textWidth(" wide-tab ", 10);
4179                 if (row_ptr->previous && 
4180                     row_ptr->previous->par->footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
4181                         string fs;
4182                         switch (row_ptr->par->footnotekind) {
4183                         case LyXParagraph::MARGIN:
4184                                 fs = " margin";
4185                                 break;
4186                         case LyXParagraph::FIG:
4187                                 fs = " fig";
4188                                 break;
4189                         case LyXParagraph::TAB:
4190                                 fs = " tab";
4191                                 break;
4192                         case LyXParagraph::WIDE_FIG:
4193                                 fs = " wide-fig";
4194                                 break;
4195                         case LyXParagraph::WIDE_TAB:
4196                                 fs = " wide-tab";
4197                                 break;
4198                         case LyXParagraph::ALGORITHM:
4199                                 fs = " alg";
4200                                 break;
4201                         case LyXParagraph::FOOTNOTE:
4202                                 fs = " foot";
4203                                 break;
4204                         }
4205
4206 #if 0
4207                         // Determine background color.
4208                         gc_type back = gc_lighted;
4209                         if (mono_video) {
4210                                 back = gc_clear;
4211                         }
4212 #endif
4213                         pain.fillRectangle(LYX_PAPER_MARGIN,
4214                                            offset + 1,
4215                                            box_x - LYX_PAPER_MARGIN,
4216                                            int(font.maxAscent()
4217                                                + font.maxDescent()),
4218                                            LColor::footnotebg);
4219
4220                         pain.line(LYX_PAPER_MARGIN, offset,
4221                                   paperwidth - LYX_PAPER_MARGIN, offset,
4222                                   LColor::footnoteframe);
4223
4224                         pain.text(LYX_PAPER_MARGIN,
4225                                   offset + int(font.maxAscent()) + 1,
4226                                   fs, font);
4227
4228                         pain.line(LYX_PAPER_MARGIN, offset,
4229                                   LYX_PAPER_MARGIN,
4230                                   offset + int(font.maxAscent()
4231                                                + font.maxDescent()),
4232                                   LColor::footnoteframe);
4233
4234                         pain.line(LYX_PAPER_MARGIN,
4235                                   offset + int(font.maxAscent()
4236                                                + font.maxDescent()) + 1,
4237                                   box_x,
4238                                   offset + int(font.maxAscent()
4239                                                + font.maxDescent()) + 1,
4240                                   LColor::footnoteframe);
4241                         
4242                 }
4243                 
4244                 /* draw the open floats in a red box */
4245                 pain.line(box_x, offset,
4246                           box_x, offset + row_ptr->height,
4247                           LColor::footnoteframe);
4248
4249                 pain.line(paperwidth - LYX_PAPER_MARGIN,
4250                           offset,
4251                           paperwidth - LYX_PAPER_MARGIN,
4252                           offset + row_ptr->height,
4253                           LColor::footnoteframe);
4254         } else  {
4255                 if (row_ptr->previous &&
4256                     row_ptr->previous->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
4257                         LyXFont font(LyXFont::ALL_SANE);
4258                         font.setSize(LyXFont::SIZE_FOOTNOTE);
4259
4260                         int box_x = LYX_PAPER_MARGIN;
4261                         box_x += font.textWidth(" wide-tab ", 10);
4262
4263                         pain.line(box_x, offset,
4264                                   paperwidth - LYX_PAPER_MARGIN,
4265                                   offset, LColor::footnote);
4266                 }
4267         }
4268         
4269         LyXLayout const & layout = textclasslist.Style(parameters->textclass,
4270                                            row_ptr->par->GetLayout());
4271         firstpar = row_ptr->par->FirstPhysicalPar();
4272         
4273         y_top = 0;
4274         y_bottom = row_ptr->height;
4275         
4276         /* is it a first row? */ 
4277         if (row_ptr->pos == 0
4278             && row_ptr->par == firstpar) {
4279                 
4280                  /* start of appendix? */
4281                 if (row_ptr->par->start_of_appendix){
4282                         pain.line(1, offset,
4283                                   paperwidth - 2, offset,
4284                                   LColor::appendixline);
4285                 }
4286
4287                 /* think about the margins */ 
4288                 if (!row_ptr->previous)
4289                         y_top += LYX_PAPER_MARGIN;
4290                 
4291                 if (row_ptr->par->pagebreak_top){ /* draw a top pagebreak  */
4292                         pain.line(0, offset + y_top + 2 * DefaultHeight(),
4293                                   paperwidth,
4294                                   offset + y_top + 2 * DefaultHeight(),
4295                                   LColor::pagebreak, Painter::line_onoffdash);
4296                         y_top += 3 * DefaultHeight();
4297                 }
4298                 
4299                 if (row_ptr->par->added_space_top.kind() == VSpace::VFILL) {
4300                         /* draw a vfill top  */
4301                         pain.line(0, offset + 2 + y_top,
4302                                   LYX_PAPER_MARGIN, offset + 2 + y_top,
4303                                   LColor::vfillline);
4304                         
4305                         pain.line(0, offset + y_top + 3 * DefaultHeight(),
4306                                   LYX_PAPER_MARGIN,
4307                                   offset + y_top + 3 * DefaultHeight(),
4308                                   LColor::vfillline);
4309                         
4310                         pain.line(LYX_PAPER_MARGIN / 2, offset + 2 + y_top,
4311                                   LYX_PAPER_MARGIN / 2,
4312                                   offset + y_top + 3 * DefaultHeight(),
4313                                   LColor::vfillline);
4314                         
4315                         y_top += 3 * DefaultHeight();
4316                 }
4317                 
4318                 /* think about user added space */ 
4319                 y_top += int(row_ptr->par->added_space_top.inPixels());
4320                 
4321                 /* think about the parskip */ 
4322                 /* some parskips VERY EASY IMPLEMENTATION */ 
4323                 if (parameters->paragraph_separation == BufferParams::PARSEP_SKIP) {
4324                         if (layout.latextype == LATEX_PARAGRAPH
4325                             && firstpar->GetDepth() == 0
4326                             && firstpar->Previous())
4327                                 y_top += parameters->getDefSkip().inPixels();
4328                         else if (firstpar->Previous()
4329                                  && textclasslist.Style(parameters->textclass,
4330                                                    firstpar->Previous()->GetLayout()).latextype == LATEX_PARAGRAPH
4331                                  && firstpar->Previous()->GetDepth() == 0)
4332                                 // is it right to use defskip here, too? (AS) 
4333                                 y_top += parameters->getDefSkip().inPixels();
4334                 }
4335                 
4336                 if (row_ptr->par->line_top) {      /* draw a top line  */
4337                         y_top +=  GetFont(row_ptr->par, 0).ascent('x');
4338
4339                         pain.line(0, offset + y_top,
4340                                   paperwidth, offset + y_top,
4341                                   LColor::topline,
4342                                   Painter::line_solid,
4343                                   Painter::line_thick);
4344                         
4345                         y_top +=  GetFont(row_ptr->par, 0).ascent('x');
4346                 }
4347                 
4348                 /* should we print a label? */ 
4349                 if (layout.labeltype >= LABEL_STATIC
4350                     && (layout.labeltype != LABEL_STATIC
4351                         || layout.latextype != LATEX_ENVIRONMENT
4352                         || row_ptr->par->IsFirstInSequence())) {
4353                         font = GetFont(row_ptr->par, -2);
4354                         if (!row_ptr->par->GetLabestring().empty()) {
4355                                 tmpx = x;
4356                                 string tmpstring = row_ptr->par->GetLabestring();
4357                                 
4358                                 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
4359                                         if (parameters->secnumdepth >= 0){
4360                                                 /* this is special code for the chapter layout. This is printed in
4361                                                  * an extra row and has a pagebreak at the top. */
4362                                                 maxdesc = int(font.maxDescent() * layout.spacing.getValue() * parameters->spacing.getValue())
4363                                                         + int(layout.parsep) * DefaultHeight();
4364                                                 if (direction == LYX_DIR_RIGHT_TO_LEFT)
4365                                                         tmpx = paperwidth - LeftMargin(row_ptr) - 
4366                                                                 font.stringWidth(tmpstring);
4367                                                 pain.text(int(tmpx),
4368                                                           offset + row_ptr->baseline - row_ptr->ascent_of_text - maxdesc,
4369                                                           tmpstring, font);
4370                                         }
4371                                 } else {
4372                                         if (direction == LYX_DIR_LEFT_TO_RIGHT)
4373                                                 tmpx = x - font.stringWidth(layout.labelsep)
4374                                                         - font.stringWidth(tmpstring);
4375                                         else {
4376                                                 tmpx = paperwidth - LeftMargin(row_ptr)
4377                                                         + font.stringWidth(layout.labelsep);
4378                                                 if (row_ptr->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)  {
4379                                                         LyXFont font(LyXFont::ALL_SANE);
4380                                                         font.setSize(LyXFont::SIZE_SMALL);
4381                                                         tmpx += font.textWidth("Mwide-figM", 10);
4382                                                 }
4383                                         }
4384                                         /* draw it! */
4385                                         pain.text(int(tmpx),
4386                                                   offset + row_ptr->baseline,
4387                                                   tmpstring, font);
4388                                 }
4389                         }
4390                         /* the labels at the top of an environment. More or less for bibliography */ 
4391                 } else if (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
4392                            layout.labeltype == LABEL_BIBLIO ||
4393                            layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
4394                         if (row_ptr->par->IsFirstInSequence()) {
4395                                 font = GetFont(row_ptr->par, -2);
4396                                 if (!row_ptr->par->GetLabestring().empty()) {
4397                                         string tmpstring = row_ptr->par->GetLabestring();
4398                                         
4399                                         maxdesc = int(font.maxDescent() * layout.spacing.getValue() * parameters->spacing.getValue()
4400                                                          + (layout.labelbottomsep * DefaultHeight()));
4401                                         
4402                                         tmpx = x;
4403                                         if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT){
4404                                                 tmpx = ( ((direction == LYX_DIR_LEFT_TO_RIGHT)
4405                                                           ? x : LeftMargin(row_ptr) )
4406                                                          + paperwidth - RightMargin(row_ptr) ) / 2; 
4407                                                 tmpx -= (font.stringWidth(tmpstring)/2);
4408                                         } else if (direction == LYX_DIR_RIGHT_TO_LEFT)
4409                                                 tmpx = paperwidth - LeftMargin(row_ptr) - 
4410                                                         font.stringWidth(tmpstring);
4411                                         pain.text(int(tmpx),
4412                                                   offset + row_ptr->baseline
4413                                                   - row_ptr->ascent_of_text
4414                                                   - maxdesc,
4415                                                   tmpstring, font);
4416                                 }
4417                         }
4418                 }
4419                 if (layout.labeltype == LABEL_BIBLIO) { // ale970302
4420                         if (row_ptr->par->bibkey) {
4421                                 font = GetFont(row_ptr->par, -1);
4422                                 if (direction == LYX_DIR_LEFT_TO_RIGHT)
4423                                         tmpx = x - font.stringWidth(layout.labelsep)
4424                                                 - row_ptr->par->bibkey->width(owner_->painter(), font);
4425                                 else
4426                                         tmpx = paperwidth - LeftMargin(row_ptr)
4427                                                 + font.stringWidth(layout.labelsep);
4428                                 row_ptr->par->bibkey->draw(owner_->painter(),
4429                                                            font,
4430                                                            offset + row_ptr->baseline, 
4431                                                            tmpx);
4432                         }
4433                 } 
4434         }
4435         
4436         /* is it a last row? */
4437         par = row_ptr->par->LastPhysicalPar();
4438         if (row_ptr->par->ParFromPos(RowLast(row_ptr) + 1) == par
4439             && (!row_ptr->next || row_ptr->next->par != row_ptr->par)) {     
4440                 
4441                 /* think about the margins */ 
4442                 if (!row_ptr->next)
4443                         y_bottom -= LYX_PAPER_MARGIN;
4444                 
4445                 /* draw a bottom pagebreak */ 
4446                 if (firstpar->pagebreak_bottom) {
4447                         pain.line(0, offset + y_bottom - 2 * DefaultHeight(),
4448                                   paperwidth,
4449                                   offset + y_bottom - 2 * DefaultHeight(),
4450                                   LColor::pagebreak);
4451                         y_bottom -= 3 * DefaultHeight();
4452                 }
4453                 
4454                 if (firstpar->added_space_bottom.kind() == VSpace::VFILL) {
4455                         /* draw a vfill bottom  */
4456                         pain.line(0, offset + y_bottom - 3 * DefaultHeight(),
4457                                   LYX_PAPER_MARGIN,
4458                                   offset + y_bottom - 3 * DefaultHeight(),
4459                                   LColor::vfillline);
4460                         pain.line(0, offset + y_bottom - 2,
4461                                   LYX_PAPER_MARGIN,
4462                                   offset + y_bottom - 2,
4463                                   LColor::vfillline);
4464                         pain.line(LYX_PAPER_MARGIN / 2,
4465                                   offset + y_bottom - 3 * DefaultHeight(),
4466                                   LYX_PAPER_MARGIN / 2,
4467                                   offset + y_bottom - 2,
4468                                   LColor::vfillline);
4469                         y_bottom -= 3* DefaultHeight();
4470                 }
4471                 
4472                 /* think about user added space */ 
4473                 y_bottom -= int(firstpar->added_space_bottom.inPixels());
4474                 
4475                 if (firstpar->line_bottom) {
4476                         /* draw a bottom line */
4477                         y_bottom -= GetFont(par, par->Last() - 1).ascent('x');
4478                         pain.line(0, offset + y_bottom,
4479                                   paperwidth, offset + y_bottom,
4480                                   LColor::topline, Painter::line_solid,
4481                                   Painter::line_thick);
4482                         y_bottom -= GetFont(par, par->Last() - 1).ascent('x');
4483                 }
4484         }
4485         
4486         /* draw the text in the pixmap */  
4487         pos_end = RowLast(row_ptr);
4488         
4489         vpos = row_ptr->pos;
4490         /* table stuff -- begin*/
4491         if (row_ptr->par->table) {
4492                 bool on_off;
4493                 int cell = NumberOfCell(row_ptr->par, row_ptr->pos);
4494                 float x_old = x;
4495                 x += row_ptr->par->table->GetBeginningOfTextInCell(cell);
4496                 
4497                 while (vpos <= pos_end)  {
4498                         pos = vis2log(vpos);
4499                         if (row_ptr->par->IsNewline(pos)) {
4500                                 
4501                                 x = x_old + row_ptr->par->table->WidthOfColumn(cell);
4502                                 /* draw the table lines, still very simple */
4503                                 on_off = !row_ptr->par->table->TopLine(cell);
4504                                 if ((!on_off ||
4505                                      !row_ptr->par->table->TopAlreadyDrawed(cell)) &&
4506                                     !row_ptr->par->table->IsContRow(cell))
4507                                         pain.line(int(x_old),
4508                                                   offset + row_ptr->baseline - row_ptr->ascent_of_text,
4509                                                   int(x),
4510                                                   offset + row_ptr->baseline - row_ptr->ascent_of_text,
4511                                                   LColor::tableline,
4512                                                   on_off ? Painter::line_onoffdash : Painter::line_solid);
4513                                 
4514                                 on_off = !row_ptr->par->table->BottomLine(cell);
4515                                 if ((!on_off && !row_ptr->par->table->RowHasContRow(cell)) ||
4516                                     row_ptr->par->table->VeryLastRow(cell))
4517
4518                                         pain.line(int(x_old),
4519                                                   offset + y_bottom - 1,
4520                                                   int(x),
4521                                                   offset + y_bottom - 1,
4522                                                   LColor::tableline,
4523                                                   on_off ? Painter::line_onoffdash : Painter::line_solid);
4524
4525                                 on_off = !row_ptr->par->table->LeftLine(cell);
4526
4527                                 pain.line(int(x_old),
4528                                           offset + row_ptr->baseline - row_ptr->ascent_of_text,
4529                                           int(x_old),
4530                                           offset + y_bottom - 1,
4531                                           LColor::tableline,
4532                                           on_off ? Painter::line_onoffdash : Painter::line_solid);
4533                                 
4534                                 on_off = !row_ptr->par->table->RightLine(cell);
4535
4536                                 pain.line(int(x) - row_ptr->par->table->AdditionalWidth(cell),
4537                                           offset + row_ptr->baseline - row_ptr->ascent_of_text,
4538                                           int(x) - row_ptr->par->table->AdditionalWidth(cell),
4539                                           offset + y_bottom - 1,
4540                                           LColor::tableline,
4541                                           on_off ? Painter::line_onoffdash : Painter::line_solid);
4542                                 
4543                                 x_old = x;
4544                 /* take care about the alignment and other spaces */
4545                                 ++cell;
4546                                 x += row_ptr->par->table->GetBeginningOfTextInCell(cell);
4547                                 if (row_ptr->par->table->IsFirstCell(cell))
4548                                         --cell; // little hack, sorry
4549                                 ++vpos;
4550                         } else if (row_ptr->par->IsHfill(pos)) {
4551                                 x += 1;
4552
4553                                 pain.line(int(x),
4554                                           offset + row_ptr->baseline - DefaultHeight() / 2,
4555                                           int(x),
4556                                           offset + row_ptr->baseline,
4557                                           LColor::vfillline);
4558                                 
4559                                 x += 2;
4560                                 ++vpos;
4561                         } else {
4562                                 if (row_ptr->par->IsSeparator(pos)) {
4563                                         tmpx = x;
4564                                         x+= SingleWidth(row_ptr->par, pos);
4565                                         /* -------> Only draw protected spaces when not in
4566                                          * free-spacing mode. */
4567                                         if (row_ptr->par->GetChar(pos) == LyXParagraph::META_PROTECTED_SEPARATOR && !layout.free_spacing) {
4568                                                 pain.line(int(tmpx),
4569                                                           offset + row_ptr->baseline - 3,
4570                                                           int(tmpx),
4571                                                           offset + row_ptr->baseline - 1,
4572                                                           LColor::vfillline);
4573                                                 
4574                                                 pain.line(int(tmpx),
4575                                                           offset + row_ptr->baseline - 1,
4576                                                           int(x - 2),
4577                                                           offset + row_ptr->baseline - 1,
4578                                                           LColor::vfillline);
4579                                                 
4580                                                 pain.line(int(x - 2),
4581                                                           offset + row_ptr->baseline - 3,
4582                                                           int(x - 2),
4583                                                           offset + row_ptr->baseline - 1,
4584                                                           LColor::vfillline);
4585                                                 
4586                                                 /* what about underbars? */
4587                                                 font = GetFont(row_ptr->par, pos); 
4588                                                 if (font.underbar() == LyXFont::ON
4589                                                     && font.latex() != LyXFont::ON) {
4590                                                         pain.line(int(tmpx),
4591                                                                   offset + row_ptr->baseline + 2,
4592                                                                   int(x - tmpx),
4593                                                                   offset + row_ptr->baseline + 2);
4594                                                 }
4595                                         }
4596                                         ++vpos;
4597                                 } else
4598                                         draw(row_ptr, vpos, offset, x);
4599                         }
4600                 }
4601                 
4602                 /* do not forget the very last cell. This has no NEWLINE so 
4603                  * ignored by the code above*/ 
4604                 if (cell == row_ptr->par->table->GetNumberOfCells()-1){
4605                         x = x_old + row_ptr->par->table->WidthOfColumn(cell);
4606                         on_off = !row_ptr->par->table->TopLine(cell);
4607                         if ((!on_off ||
4608                              !row_ptr->par->table->TopAlreadyDrawed(cell)) &&
4609                             !row_ptr->par->table->IsContRow(cell))
4610
4611                                 pain.line(int(x_old),
4612                                           offset + row_ptr->baseline - row_ptr->ascent_of_text,
4613                                           int(x),
4614                                           offset + row_ptr->baseline - row_ptr->ascent_of_text,
4615                                           LColor::tableline,
4616                                           on_off ? Painter::line_onoffdash : Painter::line_solid);
4617                         on_off = !row_ptr->par->table->BottomLine(cell);
4618                         if ((!on_off && !row_ptr->par->table->RowHasContRow(cell)) ||
4619                             row_ptr->par->table->VeryLastRow(cell))
4620
4621                                 pain.line(int(x_old),
4622                                           offset + y_bottom - 1,
4623                                           int(x),
4624                                           offset + y_bottom - 1,
4625                                           LColor::tableline,
4626                                           on_off ? Painter::line_onoffdash : Painter::line_solid);
4627                         
4628                         on_off = !row_ptr->par->table->LeftLine(cell);
4629
4630                         pain.line(int(x_old),
4631                                   offset + row_ptr->baseline - row_ptr->ascent_of_text,
4632                                   int(x_old),
4633                                   offset + y_bottom - 1,
4634                                   LColor::tableline,
4635                                   on_off ? Painter::line_onoffdash : Painter::line_solid);
4636                         
4637                         on_off = !row_ptr->par->table->RightLine(cell);
4638
4639                         pain.line(int(x) - row_ptr->par->table->AdditionalWidth(cell),
4640                                   offset + row_ptr->baseline - row_ptr->ascent_of_text,
4641                                   int(x) - row_ptr->par->table->AdditionalWidth(cell),
4642                                   offset + y_bottom - 1,
4643                                   LColor::tableline,
4644                                   on_off ? Painter::line_onoffdash : Painter::line_solid);
4645                 }
4646         } else {
4647                 /* table stuff -- end*/
4648                 LyXParagraph::size_type main_body = 
4649                         BeginningOfMainBody(row_ptr->par);
4650                 if (main_body > 0 &&
4651                     (main_body-1 > pos_end || 
4652                      !row_ptr->par->IsLineSeparator(main_body-1)))
4653                         main_body = 0;
4654
4655                 while (vpos <= pos_end)  {
4656                         pos = vis2log(vpos);
4657                         if (main_body > 0 && pos == main_body-1) {
4658                                 x += fill_label_hfill
4659                                         + GetFont(row_ptr->par, -2).stringWidth(layout.labelsep)
4660                                         - SingleWidth(row_ptr->par, main_body-1);
4661                         }
4662
4663                         if (row_ptr->par->IsHfill(pos)) {
4664                                 x += 1;
4665                                 pain.line(int(x),
4666                                           offset + row_ptr->baseline - DefaultHeight() / 2,
4667                                           int(x),
4668                                           offset + row_ptr->baseline,
4669                                           LColor::vfillline);
4670                                 
4671                                 if (HfillExpansion(row_ptr, pos)) {
4672                                         if (pos >= main_body) {
4673                                                 pain.line(int(x),
4674                                                           offset + row_ptr->baseline - DefaultHeight() / 4,
4675                                                           int(x + fill_hfill),
4676                                                           offset + row_ptr->baseline - DefaultHeight() / 4,
4677                                                           LColor::vfillline,
4678                                                           Painter::line_onoffdash);
4679                                                 x += fill_hfill;
4680                     } else {
4681                             pain.line(int(x),
4682                                       offset + row_ptr->baseline - DefaultHeight() / 4,
4683                                       int(x + fill_label_hfill),
4684                                       offset + row_ptr->baseline - DefaultHeight() / 4,
4685                                       LColor::vfillline,
4686                                       Painter::line_onoffdash);
4687                             
4688                             x += fill_label_hfill;
4689                     }
4690                                         pain.line(int(x),
4691                                                   offset + row_ptr->baseline - DefaultHeight() / 2,
4692                                                   int(x),
4693                                                   offset + row_ptr->baseline,
4694                                                   LColor::vfillline);
4695                                 }
4696                                 x += 2;
4697                                 ++vpos;
4698                         } else {
4699                                 if (row_ptr->par->IsSeparator(pos)) {
4700                                         tmpx = x;
4701                                         x+= SingleWidth(row_ptr->par, pos);
4702                                         if (pos >= main_body)
4703                                                 x+= fill_separator;
4704                                         /* -------> Only draw protected spaces when not in
4705                                          * free-spacing mode. */
4706                                         if (row_ptr->par->GetChar(pos) == LyXParagraph::META_PROTECTED_SEPARATOR && !layout.free_spacing) {
4707
4708                                                 pain.line(int(tmpx),
4709                                                           offset + row_ptr->baseline - 3,
4710                                                           int(tmpx),
4711                                                           offset + row_ptr->baseline - 1,
4712                                                           LColor::vfillline);
4713                                                 
4714                                                 pain.line(int(tmpx),
4715                                                           offset + row_ptr->baseline - 1,
4716                                                           int(x - 2),
4717                                                           offset + row_ptr->baseline - 1,
4718                                                           LColor::vfillline);
4719
4720                                                 pain.line(int(x - 2),
4721                                                           offset + row_ptr->baseline - 3,
4722                                                           int(x - 2),
4723                                                           offset + row_ptr->baseline - 1,
4724                                                           LColor::vfillline);
4725                                                 
4726                                                 /* what about underbars? */
4727                                                 font = GetFont(row_ptr->par, pos); 
4728                                                 if (font.underbar() == LyXFont::ON
4729                                                     && font.latex() != LyXFont::ON) {
4730                                                         pain.line(int(tmpx),
4731                                                                   offset + row_ptr->baseline + 2,
4732                                                                   int(x - tmpx),
4733                                                                   offset + row_ptr->baseline + 2);
4734                                                 }
4735                                         }
4736                                         ++vpos;
4737                                 } else
4738                                         draw(row_ptr, vpos, offset, x);
4739                         }
4740                 }
4741         }
4742 #if 0
4743         // check for FAST SELECTION
4744         if (fast_selection || mono_video) {
4745                 if (selection) {
4746                         
4747                         /* selection code */ 
4748                         if (sel_start_cursor.row == row_ptr && sel_end_cursor.row == row_ptr) {
4749                                 scr.fillRectangle(gc_select, sel_start_cursor.x, offset,
4750                                                   sel_end_cursor.x - sel_start_cursor.x,
4751                                                   row_ptr->height); 
4752                         } else if (sel_start_cursor.row == row_ptr) {
4753                                 scr.fillRectangle(gc_select, sel_start_cursor.x, offset,
4754                                                   paperwidth - sel_start_cursor.x,
4755                                                   row_ptr->height);
4756                         } else if (sel_end_cursor.row == row_ptr) {
4757                                 scr.fillRectangle(gc_select, 0, offset,
4758                                                   sel_end_cursor.x,
4759                                                   row_ptr->height);
4760                         } else if (y > sel_start_cursor.y && y < sel_end_cursor.y) {
4761                                 scr.fillRectangle(gc_select, 0, offset,
4762                                                   paperwidth, row_ptr->height);
4763                                 
4764                         }
4765                 }
4766         }
4767 // end of FAST SELECTION code
4768         /* thats it */
4769 #endif
4770 }
4771 #else
4772 void LyXText::GetVisibleRow(LyXScreen & scr, int offset, 
4773                             Row * row_ptr, long y)
4774 {
4775         /* returns a printed row */
4776         LyXDirection direction = row_ptr->par->getParDirection();
4777         LyXParagraph::size_type vpos, pos, pos_end;
4778         float x, tmpx;
4779         int y_top, y_bottom;
4780         float fill_separator, fill_hfill, fill_label_hfill;
4781         LyXParagraph * par, * firstpar;
4782         LyXFont font;
4783         int maxdesc;
4784         if (row_ptr->height <= 0) {
4785                 lyxerr << "LYX_ERROR: row.height: " << row_ptr->height << endl;
4786                 return;
4787         }
4788         PrepareToPrint(row_ptr, x, fill_separator,
4789                        fill_hfill, fill_label_hfill);
4790
4791         /* initialize the pixmap */
4792         
4793         scr.fillRectangle(gc_clear,
4794                           0, offset, paperwidth, row_ptr->height);
4795         // check for NOT FAST SELECTION
4796         if (!fast_selection && !mono_video && selection) {
4797                 /* selection code */ 
4798                 if (sel_start_cursor.row == row_ptr &&
4799                     sel_end_cursor.row == row_ptr) {
4800                         if (sel_start_cursor.x < sel_end_cursor.x)
4801                           scr.fillRectangle(gc_selection, sel_start_cursor.x,
4802                                           offset,
4803                                           sel_end_cursor.x - sel_start_cursor.x,
4804                                           row_ptr->height);
4805                         else
4806                           scr.fillRectangle(gc_selection, sel_end_cursor.x,
4807                                           offset,
4808                                           sel_start_cursor.x - sel_end_cursor.x,
4809                                           row_ptr->height);
4810                 }
4811                 else if (sel_start_cursor.row == row_ptr) {
4812                      if (direction == LYX_DIR_LEFT_TO_RIGHT)
4813                         scr.fillRectangle(gc_selection, sel_start_cursor.x,
4814                                           offset,
4815                                           paperwidth - sel_start_cursor.x,
4816                                           row_ptr->height);
4817                      else
4818                         scr.fillRectangle(gc_selection, 0, offset,
4819                                           sel_start_cursor.x, row_ptr->height);
4820                 } else if (sel_end_cursor.row == row_ptr) {
4821                      if (direction == LYX_DIR_LEFT_TO_RIGHT)
4822                         scr.fillRectangle(gc_selection, 0, offset,
4823                                           sel_end_cursor.x, row_ptr->height);
4824                      else
4825                         scr.fillRectangle(gc_selection, sel_end_cursor.x,
4826                                           offset,
4827                                           paperwidth - sel_end_cursor.x,
4828                                           row_ptr->height);
4829                 } else if (y > sel_start_cursor.y && y < sel_end_cursor.y) {
4830                         scr.fillRectangle(gc_selection, 0, offset,
4831                                           paperwidth, row_ptr->height);
4832                         
4833                 }
4834         } // end of NOT FAST SELECTION code
4835         
4836         if (row_ptr->par->appendix){
4837           scr.drawVerticalLine(gc_math, 1, offset, offset+row_ptr->height);
4838           scr.drawVerticalLine(gc_math, paperwidth-2 , offset, offset+row_ptr->height);
4839         }
4840
4841         if (row_ptr->par->pextra_type == LyXParagraph::PEXTRA_MINIPAGE) {
4842                 /* draw a marker at the left margin! */ 
4843                 LyXFont font = GetFont(row_ptr->par, 0);
4844                 int asc = font.maxAscent();
4845                 int x = (LYX_PAPER_MARGIN - font.width('|')) / 2;
4846                 int y1 = (offset + row_ptr->baseline);
4847                 int y2 = (offset + row_ptr->baseline) - asc;
4848
4849                 scr.drawVerticalLine(gc_minipage, x, y1, y2);
4850         }       
4851         if (row_ptr->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
4852                 LyXFont font(LyXFont::ALL_SANE);
4853                 font.setSize(LyXFont::SIZE_FOOTNOTE);
4854 #ifdef USE_PAINTER
4855                 font.setColor(LColor::footnote);
4856 #else
4857                 font.setColor(LyXFont::RED);
4858 #endif
4859                 
4860                 int box_x = LYX_PAPER_MARGIN;
4861                 box_x += font.textWidth(" wide-tab ", 10);
4862                 if (row_ptr->previous && 
4863                     row_ptr->previous->par->footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
4864                         string fs;
4865                         switch (row_ptr->par->footnotekind) {
4866                         case LyXParagraph::MARGIN:
4867                                 fs = " margin";
4868                                 break;
4869                         case LyXParagraph::FIG:
4870                                 fs = " fig";
4871                                 break;
4872                         case LyXParagraph::TAB:
4873                                 fs = " tab";
4874                                 break;
4875                         case LyXParagraph::WIDE_FIG:
4876                                 fs = " wide-fig";
4877                                 break;
4878                         case LyXParagraph::WIDE_TAB:
4879                                 fs = " wide-tab";
4880                                 break;
4881                         case LyXParagraph::ALGORITHM:
4882                                 fs = " alg";
4883                                 break;
4884                         case LyXParagraph::FOOTNOTE:
4885                                 fs = " foot";
4886                                 break;
4887                         }
4888                         
4889                         // Determine background color.
4890                         gc_type back = gc_lighted;
4891                         if (mono_video) {
4892                                 back = gc_clear;
4893                         }
4894                         scr.fillRectangle(back, LYX_PAPER_MARGIN, offset+1,
4895                                           box_x - LYX_PAPER_MARGIN, 
4896                                           int(font.maxAscent())+
4897                                           int(font.maxDescent()));
4898                         
4899                         scr.drawLine(gc_foot,
4900                                      offset,
4901                                      LYX_PAPER_MARGIN,
4902                                      paperwidth - 2*LYX_PAPER_MARGIN);
4903                         
4904                         scr.drawString(font, fs,
4905                                        offset + int(font.maxAscent())+1,
4906                                        LYX_PAPER_MARGIN);
4907                         scr.drawVerticalLine(gc_foot,
4908                                              LYX_PAPER_MARGIN,
4909                                              offset,
4910                                              offset
4911                                              + int(font.maxAscent())+
4912                                              int(font.maxDescent()));
4913                         
4914                         scr.drawLine(gc_foot,
4915                                      offset
4916                                      + int(font.maxAscent())
4917                                      + int(font.maxDescent()) + 1,
4918                                      LYX_PAPER_MARGIN, box_x - LYX_PAPER_MARGIN); 
4919                 }
4920                 
4921                 /* draw the open floats in a red box */
4922                 scr.drawVerticalLine(gc_foot,
4923                                      box_x,
4924                                      offset,  offset + row_ptr->height);
4925                 
4926                 scr.drawVerticalLine(gc_foot,
4927                                      paperwidth - LYX_PAPER_MARGIN,
4928                                      offset,
4929                                      offset + row_ptr->height);
4930                                         
4931         } else  {
4932                 if (row_ptr->previous &&
4933                     row_ptr->previous->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
4934                         LyXFont font(LyXFont::ALL_SANE);
4935                         font.setSize(LyXFont::SIZE_FOOTNOTE);
4936
4937                         int box_x = LYX_PAPER_MARGIN;
4938                         box_x += font.textWidth(" wide-tab ", 10);
4939                         
4940                         scr.drawLine(gc_foot,
4941                                      offset,
4942                                      box_x,
4943                                      paperwidth - LYX_PAPER_MARGIN - box_x);
4944                 }
4945         }
4946         
4947         LyXLayout const & layout = textclasslist.Style(parameters->textclass,
4948                                            row_ptr->par->GetLayout());
4949         firstpar = row_ptr->par->FirstPhysicalPar();
4950         
4951         y_top = 0;
4952         y_bottom = row_ptr->height;
4953         
4954         /* is it a first row? */ 
4955         if (row_ptr->pos == 0
4956             && row_ptr->par == firstpar) {
4957                 
4958                  /* start of appendix? */
4959                 if (row_ptr->par->start_of_appendix){
4960                   scr.drawLine(gc_math, 
4961                                offset,
4962                                1, paperwidth-2);
4963                 }
4964
4965                 /* think about the margins */ 
4966                 if (!row_ptr->previous)
4967                         y_top += LYX_PAPER_MARGIN;
4968                 
4969                 if (row_ptr->par->pagebreak_top){ /* draw a top pagebreak  */
4970                         scr.drawOnOffLine(offset + y_top + 2 * DefaultHeight(),
4971                                           0, paperwidth);
4972                         y_top += 3 * DefaultHeight();
4973                 }
4974                 
4975                 if (row_ptr->par->added_space_top.kind() == VSpace::VFILL) {
4976                         /* draw a vfill top  */
4977                         scr.drawLine(gc_fill, 
4978                                      offset + 2 + y_top,
4979                                      0, LYX_PAPER_MARGIN);
4980                         scr.drawLine(gc_fill,
4981                                      offset + y_top + 3 * DefaultHeight(),
4982                                      0, LYX_PAPER_MARGIN);
4983                         scr.drawVerticalOnOffLine(LYX_PAPER_MARGIN / 2, 
4984                                                   offset + 2 + y_top,
4985                                                   offset + y_top + 3 *
4986                                                   DefaultHeight());
4987                         
4988                         y_top += 3 * DefaultHeight();
4989                 }
4990                 
4991                 /* think about user added space */ 
4992                 y_top += int(row_ptr->par->added_space_top.inPixels());
4993                 
4994                 /* think about the parskip */ 
4995                 /* some parskips VERY EASY IMPLEMENTATION */ 
4996                 if (parameters->paragraph_separation == BufferParams::PARSEP_SKIP) {
4997                         if (layout.latextype == LATEX_PARAGRAPH
4998                             && firstpar->GetDepth() == 0
4999                             && firstpar->Previous())
5000                                 y_top += parameters->getDefSkip().inPixels();
5001                         else if (firstpar->Previous()
5002                                  && textclasslist.Style(parameters->textclass,
5003                                                    firstpar->Previous()->GetLayout()).latextype == LATEX_PARAGRAPH
5004                                  && firstpar->Previous()->GetDepth() == 0)
5005                                 // is it right to use defskip here, too? (AS) 
5006                                 y_top += parameters->getDefSkip().inPixels();
5007                 }
5008                 
5009                 if (row_ptr->par->line_top) {      /* draw a top line  */
5010                         y_top +=  GetFont(row_ptr->par, 0).ascent('x');
5011
5012                         scr.drawThickLine(offset + y_top,
5013                                           0, paperwidth);
5014                         y_top +=  GetFont(row_ptr->par, 0).ascent('x');
5015                 }
5016                 
5017                 /* should we print a label? */ 
5018                 if (layout.labeltype >= LABEL_STATIC
5019                     && (layout.labeltype != LABEL_STATIC
5020                         || layout.latextype != LATEX_ENVIRONMENT
5021                         || row_ptr->par->IsFirstInSequence())) {
5022                         font = GetFont(row_ptr->par, -2);
5023                         if (!row_ptr->par->GetLabestring().empty()) {
5024                                 tmpx = x;
5025                                 string tmpstring = row_ptr->par->GetLabestring();
5026                                 
5027                                 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
5028                                         if (parameters->secnumdepth >= 0){
5029                                                 /* this is special code for the chapter layout. This is printed in
5030                                                  * an extra row and has a pagebreak at the top. */
5031                                                 maxdesc = int(font.maxDescent() * layout.spacing.getValue() * parameters->spacing.getValue())
5032                                                         + int(layout.parsep) * DefaultHeight();
5033                                                 if (direction == LYX_DIR_RIGHT_TO_LEFT)
5034                                                         tmpx = paperwidth - LeftMargin(row_ptr) - 
5035                                                                 font.stringWidth(tmpstring);
5036                                                 scr.drawString(font, tmpstring,
5037                                                                offset + row_ptr->baseline
5038                                                                - row_ptr->ascent_of_text - maxdesc,
5039                                                                int(tmpx));
5040                                         }
5041                                 } else {
5042                                         if (direction == LYX_DIR_LEFT_TO_RIGHT)
5043                                                 tmpx = x - font.stringWidth(layout.labelsep)
5044                                                         - font.stringWidth(tmpstring);
5045                                         else {
5046                                                 tmpx = paperwidth - LeftMargin(row_ptr)
5047                                                         + font.stringWidth(layout.labelsep);
5048                                                 if (row_ptr->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)  {
5049                                                         LyXFont font(LyXFont::ALL_SANE);
5050                                                         font.setSize(LyXFont::SIZE_SMALL);
5051                                                         tmpx += font.textWidth("Mwide-figM", 10);
5052                                                 }
5053                                         }
5054                                         /* draw it! */
5055                                         scr.drawString(font, tmpstring,
5056                                                        offset + row_ptr->baseline, int(tmpx));
5057                                 }
5058                         }
5059                         /* the labels at the top of an environment. More or less for bibliography */ 
5060                 } else if (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
5061                            layout.labeltype == LABEL_BIBLIO ||
5062                            layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
5063                         if (row_ptr->par->IsFirstInSequence()) {
5064                                 font = GetFont(row_ptr->par, -2);
5065                                 if (!row_ptr->par->GetLabestring().empty()) {
5066                                         string tmpstring = row_ptr->par->GetLabestring();
5067                                         
5068                                         maxdesc = int(font.maxDescent() * layout.spacing.getValue() * parameters->spacing.getValue()
5069                                                          + (layout.labelbottomsep * DefaultHeight()));
5070                                         
5071                                         tmpx = x;
5072                                         if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT){
5073                                                 tmpx = ( ((direction == LYX_DIR_LEFT_TO_RIGHT)
5074                                                           ? x : LeftMargin(row_ptr) )
5075                                                          + paperwidth - RightMargin(row_ptr) ) / 2; 
5076                                                 tmpx -= (font.stringWidth(tmpstring)/2);
5077                                         } else if (direction == LYX_DIR_RIGHT_TO_LEFT)
5078                                                 tmpx = paperwidth - LeftMargin(row_ptr) - 
5079                                                         font.stringWidth(tmpstring);
5080
5081                                         scr.drawString(font, tmpstring,
5082                                                        offset + row_ptr->baseline
5083                                                        - row_ptr->ascent_of_text - maxdesc,  
5084                                                        int(tmpx));                  
5085                                 }
5086                         }
5087                 }
5088                 if (layout.labeltype == LABEL_BIBLIO) { // ale970302
5089                         if (row_ptr->par->bibkey) {
5090                                 font = GetFont(row_ptr->par, -1);
5091                                 if (direction == LYX_DIR_LEFT_TO_RIGHT)
5092                                         tmpx = x - font.stringWidth(layout.labelsep)
5093                                                 - row_ptr->par->bibkey->Width(font);
5094                                 else
5095                                         tmpx = paperwidth - LeftMargin(row_ptr)
5096                                                 + font.stringWidth(layout.labelsep);
5097                                 row_ptr->par->bibkey->Draw(font, scr,
5098                                                            offset + row_ptr->baseline, 
5099                                                            tmpx);
5100                         }
5101                 } 
5102         }
5103         
5104         /* is it a last row? */
5105         par = row_ptr->par->LastPhysicalPar();
5106         if (row_ptr->par->ParFromPos(RowLast(row_ptr) + 1) == par
5107             && (!row_ptr->next || row_ptr->next->par != row_ptr->par)) {     
5108                 
5109                 /* think about the margins */ 
5110                 if (!row_ptr->next)
5111                         y_bottom -= LYX_PAPER_MARGIN;
5112                 
5113                 /* draw a bottom pagebreak */ 
5114                 if (firstpar->pagebreak_bottom) {
5115                         scr.drawOnOffLine(offset + y_bottom - 2 *
5116                                           DefaultHeight(),
5117                                           0, paperwidth);
5118                         y_bottom -= 3 * DefaultHeight();
5119                 }
5120                 
5121                 if (firstpar->added_space_bottom.kind() == VSpace::VFILL) {
5122                         /* draw a vfill bottom  */
5123                         scr.drawLine(gc_fill,
5124                                      offset + y_bottom - 3 * DefaultHeight(),
5125                                      0, LYX_PAPER_MARGIN);
5126                         scr.drawLine(gc_fill, offset + y_bottom - 2,
5127                                      0, LYX_PAPER_MARGIN);
5128                         scr.drawVerticalOnOffLine(LYX_PAPER_MARGIN / 2, 
5129                                                   offset + y_bottom - 3 * DefaultHeight(),
5130                                                   offset + y_bottom - 2
5131                                 );          
5132                         y_bottom -= 3* DefaultHeight();
5133                 }
5134                 
5135                 /* think about user added space */ 
5136                 y_bottom -= int(firstpar->added_space_bottom.inPixels());
5137                 
5138                 if (firstpar->line_bottom) {
5139                         /* draw a bottom line */
5140                         y_bottom -= GetFont(par, par->Last() - 1).ascent('x');
5141
5142                         scr.drawThickLine(offset + y_bottom,
5143                                           0, paperwidth);           
5144                         y_bottom -= GetFont(par, par->Last() - 1).ascent('x');
5145                 }
5146         }
5147         
5148         /* draw the text in the pixmap */  
5149         pos_end = RowLast(row_ptr);
5150         
5151         vpos = row_ptr->pos;
5152         /* table stuff -- begin*/
5153         if (row_ptr->par->table) {
5154                 bool on_off;
5155                 int cell = NumberOfCell(row_ptr->par, row_ptr->pos);
5156                 float x_old = x;
5157                 x += row_ptr->par->table->GetBeginningOfTextInCell(cell);
5158                 
5159                 while (vpos <= pos_end)  {
5160                         pos = vis2log(vpos);
5161                         if (row_ptr->par->IsNewline(pos)) {
5162                                 
5163                                 x = x_old + row_ptr->par->table->WidthOfColumn(cell);
5164                                 /* draw the table lines, still very simple */
5165                                 on_off = !row_ptr->par->table->TopLine(cell);
5166                                 if ((!on_off ||
5167                                      !row_ptr->par->table->TopAlreadyDrawed(cell)) &&
5168                                     !row_ptr->par->table->IsContRow(cell))
5169                                         scr.drawTableLine(offset + row_ptr->baseline -
5170                                                           row_ptr->ascent_of_text,
5171                                                           int(x_old), int(x - x_old), on_off);
5172                                 on_off = !row_ptr->par->table->BottomLine(cell);
5173                                 if ((!on_off && !row_ptr->par->table->RowHasContRow(cell)) ||
5174                                     row_ptr->par->table->VeryLastRow(cell))
5175                                         scr.drawTableLine(offset + y_bottom - 1,
5176                                                           int(x_old), int(x - x_old), on_off);
5177                                 on_off = !row_ptr->par->table->LeftLine(cell);
5178                                 
5179                                 scr.drawVerticalTableLine(int(x_old), 
5180                                                           offset + row_ptr->baseline -
5181                                                           row_ptr->ascent_of_text,
5182                                                           offset + y_bottom - 1,
5183                                                           on_off);
5184                                 on_off = !row_ptr->par->table->RightLine(cell);
5185                                 
5186                                 scr.drawVerticalTableLine(int(x) -
5187                                                           row_ptr->par->table->AdditionalWidth(cell),
5188                                                           offset + row_ptr->baseline -
5189                                                           row_ptr->ascent_of_text,
5190                                                           offset + y_bottom - 1,
5191                                                           on_off);
5192                                 x_old = x;
5193                 /* take care about the alignment and other spaces */
5194                                 ++cell;
5195                                 x += row_ptr->par->table->GetBeginningOfTextInCell(cell);
5196                                 if (row_ptr->par->table->IsFirstCell(cell))
5197                                         --cell; // little hack, sorry
5198                                 ++vpos;
5199                         } else if (row_ptr->par->IsHfill(pos)) {
5200                                 x += 1;
5201                                 
5202                                 scr.drawVerticalLine(gc_fill, int(x),  
5203                                                      offset + row_ptr->baseline - DefaultHeight()/2, 
5204                                                      offset + row_ptr->baseline);               
5205                                 x += 2;
5206                                 ++vpos;
5207                         } else {
5208                                 if (row_ptr->par->IsSeparator(pos)) {
5209                                         tmpx = x;
5210                                         x+= SingleWidth(row_ptr->par, pos);
5211                                         /* -------> Only draw protected spaces when not in
5212                                          * free-spacing mode. */
5213                                         if (row_ptr->par->GetChar(pos) == LyXParagraph::META_PROTECTED_SEPARATOR && !layout.free_spacing) {
5214                                                 scr.drawVerticalLine(gc_fill, int(tmpx),
5215                                                                      offset + row_ptr->baseline - 3,
5216                                                                      offset + row_ptr->baseline - 1);
5217                                                 scr.drawLine(gc_fill,
5218                                                              offset + row_ptr->baseline - 1,
5219                                                              int(tmpx),
5220                                                              int(x-tmpx-2));
5221                                                 scr.drawVerticalLine(gc_fill, int(x-2),
5222                                                                      offset + row_ptr->baseline - 3,
5223                                                                      offset + row_ptr->baseline - 1);                   
5224                                                 /* what about underbars? */
5225                                                 font = GetFont(row_ptr->par, pos); 
5226                                                 if (font.underbar() == LyXFont::ON
5227                                                     && font.latex() != LyXFont::ON) {
5228                                                         scr.drawLine(gc_copy,
5229                                                                      offset +
5230                                                                      row_ptr->baseline + 2,
5231                                                                      int(tmpx),
5232                                                                      int(x - tmpx));                        
5233                                                 }
5234                                         }
5235                                         ++vpos;
5236                                 } else
5237                                         Draw(row_ptr, vpos, scr, offset, x);
5238                         }
5239                 }
5240                 
5241                 /* do not forget the very last cell. This has no NEWLINE so 
5242                  * ignored by the code above*/ 
5243                 if (cell == row_ptr->par->table->GetNumberOfCells()-1){
5244                         x = x_old + row_ptr->par->table->WidthOfColumn(cell);
5245                         on_off = !row_ptr->par->table->TopLine(cell);
5246                         if ((!on_off ||
5247                              !row_ptr->par->table->TopAlreadyDrawed(cell)) &&
5248                             !row_ptr->par->table->IsContRow(cell))
5249                                 
5250                                 scr.drawTableLine(offset + row_ptr->baseline -
5251                                                   row_ptr->ascent_of_text,
5252                                                   int(x_old), int(x - x_old), on_off);      
5253                         on_off = !row_ptr->par->table->BottomLine(cell);
5254                         if ((!on_off && !row_ptr->par->table->RowHasContRow(cell)) ||
5255                             row_ptr->par->table->VeryLastRow(cell))
5256                                 
5257                                 scr.drawTableLine(offset + y_bottom - 1,
5258                                                   int(x_old), int(x - x_old), on_off);      
5259                         on_off = !row_ptr->par->table->LeftLine(cell);
5260                         
5261                         scr.drawVerticalTableLine(int(x_old), 
5262                                                   offset + row_ptr->baseline -
5263                                                   row_ptr->ascent_of_text,
5264                                                   offset + y_bottom - 1,
5265                                                   on_off);          
5266                         on_off = !row_ptr->par->table->RightLine(cell);
5267                         
5268                         scr.drawVerticalTableLine(int(x) -
5269                                                   row_ptr->par->table->AdditionalWidth(cell),
5270                                                   offset + row_ptr->baseline -
5271                                                   row_ptr->ascent_of_text,
5272                                                   offset + y_bottom - 1,
5273                                                   on_off);          
5274                 }
5275         } else {
5276                 /* table stuff -- end*/
5277                 LyXParagraph::size_type main_body = 
5278                         BeginningOfMainBody(row_ptr->par);
5279                 if (main_body > 0 &&
5280                     (main_body-1 > pos_end || 
5281                      !row_ptr->par->IsLineSeparator(main_body-1)))
5282                         main_body = 0;
5283
5284                 while (vpos <= pos_end)  {
5285                         pos = vis2log(vpos);
5286                         if (main_body > 0 && pos == main_body-1) {
5287                                 x += fill_label_hfill
5288                                         + GetFont(row_ptr->par, -2).stringWidth(layout.labelsep)
5289                                         - SingleWidth(row_ptr->par, main_body-1);
5290                         }
5291
5292                         if (row_ptr->par->IsHfill(pos)) {
5293                                 x += 1;
5294                                 scr.drawVerticalLine(gc_fill, int(x),  
5295                                                      offset + row_ptr->baseline - DefaultHeight()/2, 
5296                                                      offset + row_ptr->baseline);               
5297                                 if (HfillExpansion(row_ptr, pos)) {
5298                                         if (pos >= main_body) {
5299                                                 scr.drawOnOffLine(offset + row_ptr->baseline -
5300                                                                   DefaultHeight()/4,
5301                                                                   int(x),
5302                                                                   int(fill_hfill));                     
5303                                                 x += fill_hfill;
5304                     } else {
5305                             scr.drawOnOffLine(offset + row_ptr->baseline -
5306                                               DefaultHeight()/4,
5307                                               int(x),
5308                                               int(fill_label_hfill));
5309                             x += fill_label_hfill;
5310                     }
5311                                         scr.drawVerticalLine(gc_fill, int(x),
5312                                                              offset + row_ptr->baseline -
5313                                                              DefaultHeight()/2, 
5314                                                              offset + row_ptr->baseline);
5315                                 }
5316                                 x += 2;
5317                                 ++vpos;
5318                         } else {
5319                                 if (row_ptr->par->IsSeparator(pos)) {
5320                                         tmpx = x;
5321                                         x+= SingleWidth(row_ptr->par, pos);
5322                                         if (pos >= main_body)
5323                                                 x+= fill_separator;
5324                                         /* -------> Only draw protected spaces when not in
5325                                          * free-spacing mode. */
5326                                         if (row_ptr->par->GetChar(pos) == LyXParagraph::META_PROTECTED_SEPARATOR && !layout.free_spacing) {
5327                                                 
5328                                                 scr.drawVerticalLine(gc_fill, int(tmpx),
5329                                                                      offset + row_ptr->baseline - 3,
5330                                                                      offset + row_ptr->baseline - 1);
5331                                                 scr.drawLine(gc_fill,
5332                                                              offset + row_ptr->baseline - 1,
5333                                                              int(tmpx),
5334                                                              int(x-tmpx-2));
5335                                                 scr.drawVerticalLine(gc_fill, int(x-2),
5336                                                                      offset + row_ptr->baseline - 3,
5337                                                                      offset + row_ptr->baseline - 1);                   
5338                                                 /* what about underbars? */
5339                                                 font = GetFont(row_ptr->par, pos); 
5340                                                 if (font.underbar() == LyXFont::ON
5341                                                     && font.latex() != LyXFont::ON) {
5342                                                         scr.drawLine(gc_copy,
5343                                              offset + row_ptr->baseline + 2,
5344                                                                      int(tmpx),
5345                                                                      int(x - tmpx));
5346                                                 }
5347                                         }
5348                                         ++vpos;
5349                                 } else
5350                                         Draw(row_ptr, vpos, scr, offset, x);
5351                         }
5352                 }
5353         }
5354         // check for FAST SELECTION
5355         if (fast_selection || mono_video) {
5356                 if (selection) {
5357                         
5358                         /* selection code */ 
5359                         if (sel_start_cursor.row == row_ptr && sel_end_cursor.row == row_ptr) {
5360                                 scr.fillRectangle(gc_select, sel_start_cursor.x, offset,
5361                                                   sel_end_cursor.x - sel_start_cursor.x,
5362                                                   row_ptr->height); 
5363                         } else if (sel_start_cursor.row == row_ptr) {
5364                                 scr.fillRectangle(gc_select, sel_start_cursor.x, offset,
5365                                                   paperwidth - sel_start_cursor.x,
5366                                                   row_ptr->height);
5367                         } else if (sel_end_cursor.row == row_ptr) {
5368                                 scr.fillRectangle(gc_select, 0, offset,
5369                                                   sel_end_cursor.x,
5370                                                   row_ptr->height);
5371                         } else if (y > sel_start_cursor.y && y < sel_end_cursor.y) {
5372                                 scr.fillRectangle(gc_select, 0, offset,
5373                                                   paperwidth, row_ptr->height);
5374                                 
5375                         }
5376                 }
5377         }
5378 // end of FAST SELECTION code
5379         /* thats it */ 
5380 }
5381 #endif
5382
5383 int LyXText::DefaultHeight() const
5384 {
5385         LyXFont font(LyXFont::ALL_SANE);
5386         return int(font.maxAscent() + font.maxDescent() * 1.5);
5387 }
5388
5389    
5390 /* returns the column near the specified x-coordinate of the row 
5391 * x is set to the real beginning of this column  */ 
5392 int LyXText::GetColumnNearX(Row * row, int & x) const
5393 {
5394         float tmpx = 0.0;
5395         float fill_separator, fill_hfill, fill_label_hfill;
5396    
5397         PrepareToPrint(row, tmpx, fill_separator,
5398                        fill_hfill, fill_label_hfill);
5399
5400         LyXDirection direction = row->par->getParDirection();
5401         LyXParagraph::size_type vc = row->pos;
5402         LyXParagraph::size_type last = RowLast(row);
5403         LyXParagraph::size_type c;
5404
5405         LyXLayout const & layout = textclasslist.Style(parameters->textclass,
5406                                            row->par->GetLayout());
5407         /* table stuff -- begin */
5408         if (row->par->table) {
5409                 if (row->next && row->next->par == row->par //the last row doesn't need a newline at the end
5410                     && row->par->IsNewline(last))
5411                         last--;
5412                 int cell = NumberOfCell(row->par, row->pos);
5413                 float x_old = tmpx;
5414                 bool ready = false;
5415                 tmpx += row->par->table->GetBeginningOfTextInCell(cell);
5416                 while (vc <= last
5417                        && (c = vis2log(vc)) >= 0
5418                        && tmpx + (SingleWidth(row->par, c)/2) <= x
5419                        && !ready){
5420                         if (row->par->IsNewline(c)) {
5421                                 if (x_old + row->par->table->WidthOfColumn(cell) <= x){
5422                                         tmpx = x_old + row->par->table->WidthOfColumn(cell);
5423                                         x_old = tmpx;
5424                                         ++cell;
5425                                         tmpx += row->par->table->GetBeginningOfTextInCell(cell);
5426                                         ++vc;
5427                                 } else
5428                                         ready = true;
5429                         } else {
5430                                 tmpx += SingleWidth(row->par, c);
5431                                 ++vc;
5432                         }
5433                 }
5434         } else {
5435                 /* table stuff -- end*/
5436                 LyXParagraph::size_type main_body = BeginningOfMainBody(row->par);
5437                 float last_tmpx = tmpx;
5438
5439                 if (main_body > 0 &&
5440                     (main_body-1 > last || 
5441                      !row->par->IsLineSeparator(main_body-1)))
5442                         main_body = 0;
5443
5444                 while (vc <= last && tmpx <= x) {
5445                         c = vis2log(vc);
5446                         last_tmpx = tmpx;
5447                         if (main_body > 0 && c == main_body-1) {
5448                                 tmpx += fill_label_hfill +
5449                                         GetFont(row->par, -2).stringWidth(layout.labelsep);
5450                                 if (row->par->IsLineSeparator(main_body-1))
5451                                         tmpx -= SingleWidth(row->par, main_body-1);
5452                         }
5453              
5454                         tmpx += SingleWidth(row->par, c);
5455                         if (HfillExpansion(row, c)) {
5456                                 if (c >= main_body)
5457                                         tmpx += fill_hfill;
5458                                 else
5459                                         tmpx += fill_label_hfill;
5460                         }
5461                         else if (c >= main_body
5462                                  && row->par->IsSeparator(c)) {
5463                                 tmpx+= fill_separator;  
5464                         }
5465                         ++vc;
5466                 }
5467
5468                 if (vc > row->pos && (tmpx+last_tmpx)/2 > x) {
5469                         vc--;
5470                         tmpx = last_tmpx;
5471                 }
5472         }
5473         /* make sure that a last space in a row doesnt count */
5474         if (row->pos <= last
5475             && !(!row->next || row->next->par != row->par))
5476                 if (direction == LYX_DIR_LEFT_TO_RIGHT && vc > last
5477                     && row->par->IsLineSeparator(vis2log(last)) ) {
5478                         vc = last;
5479                         tmpx -= fill_separator+SingleWidth(row->par, vis2log(last));
5480                 } else if (direction == LYX_DIR_RIGHT_TO_LEFT 
5481                            && vc == row->pos
5482                            && row->par->IsLineSeparator(vis2log(row->pos)) ) {
5483                         vc = row->pos+1;
5484                         tmpx += fill_separator+SingleWidth(row->par, vis2log(row->pos));
5485                 }
5486
5487         if (row->pos > last)  // Row is empty?
5488                 c = row->pos;
5489         else if (vc <= last) {
5490                 c = vis2log(vc);
5491                 LyXDirection direction = row->par->getLetterDirection(c);
5492                 if (vc > row->pos && row->par->IsLineSeparator(c)
5493                     && row->par->getLetterDirection(vis2log(vc - 1)) != direction)
5494                         c = vis2log(vc-1);
5495                 if (direction == LYX_DIR_RIGHT_TO_LEFT)
5496                         ++c;
5497         } else {
5498                 c = vis2log(last)+1;
5499                 if (row->par->getLetterDirection(c - 1) == LYX_DIR_RIGHT_TO_LEFT)
5500                         --c;            
5501         }
5502
5503         if (!row->par->table && row->pos <= last && c > last
5504             && row->par->IsNewline(last)) {
5505                 if (row->par->getLetterDirection(last) == LYX_DIR_LEFT_TO_RIGHT)
5506                         tmpx -= SingleWidth(row->par, last);
5507                 else
5508                         tmpx += SingleWidth(row->par, last);
5509                 c = last;
5510         }
5511
5512         c -= row->pos;
5513         x = int(tmpx);
5514         return c;
5515 }
5516
5517    
5518 /* turn the selection into a new environment. If there is no selection,
5519 * create an empty environment */ 
5520 void LyXText::InsertFootnoteEnvironment(LyXParagraph::footnote_kind kind)
5521 {
5522    /* no footnoteenvironment in a footnoteenvironment */ 
5523    if (cursor.par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
5524       WriteAlert(_("Impossible operation"), 
5525                  _("You can't insert a float in a float!"), 
5526                  _("Sorry."));
5527      return;
5528    }
5529    /* no marginpars in minipages */
5530    if (kind == LyXParagraph::MARGIN 
5531       && cursor.par->pextra_type == LyXParagraph::PEXTRA_MINIPAGE) {
5532       WriteAlert(_("Impossible operation"), 
5533                  _("You can't insert a marginpar in a minipage!"), 
5534                  _("Sorry."));
5535       return;
5536    }
5537    
5538    /* this doesnt make sense, if there is no selection */ 
5539    bool dummy_selection = false;
5540    if (!selection) {
5541       sel_start_cursor = cursor;       /* dummy selection  */
5542       sel_end_cursor = cursor;
5543       dummy_selection = true;
5544    }
5545    
5546    LyXParagraph *tmppar;
5547
5548    if (sel_start_cursor.par->table || sel_end_cursor.par->table){
5549       WriteAlert(_("Impossible operation"), _("Cannot cut table."), _("Sorry."));
5550       return;
5551    }
5552      
5553    /* a test to make sure there is not already a footnote
5554     * in the selection. */
5555    
5556    tmppar = sel_start_cursor.par->ParFromPos(sel_start_cursor.pos);
5557    
5558    while (tmppar != sel_end_cursor.par->ParFromPos(sel_end_cursor.pos) && 
5559           tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
5560      tmppar = tmppar->next;
5561    
5562    if (tmppar != sel_end_cursor.par->ParFromPos(sel_end_cursor.pos)
5563        || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
5564       WriteAlert(_("Impossible operation"), 
5565                  _("Float would include float!"), 
5566                  _("Sorry."));
5567       return;
5568    }
5569    
5570    /* ok we have a selection. This is always between sel_start_cursor
5571     * and sel_end cursor */
5572
5573    SetUndo(Undo::FINISH, 
5574            sel_start_cursor.par->ParFromPos(sel_start_cursor.pos)->previous, 
5575            sel_end_cursor.par->ParFromPos(sel_end_cursor.pos)->next); 
5576    
5577    if (sel_end_cursor.pos > 0 
5578        && sel_end_cursor.par->IsLineSeparator(sel_end_cursor.pos - 1))
5579      sel_end_cursor.pos--;             /* please break before a space at
5580                                         * the end */
5581    if (sel_start_cursor.par == sel_end_cursor.par
5582        && sel_start_cursor.pos > sel_end_cursor.pos)
5583      sel_start_cursor.pos--;
5584
5585    sel_end_cursor.par->BreakParagraphConservative(sel_end_cursor.pos);
5586    
5587    sel_end_cursor.par = sel_end_cursor.par->Next();
5588    sel_end_cursor.pos = 0;
5589    
5590    // don't forget to insert a dummy layout paragraph if necessary
5591    if (sel_start_cursor.par->GetLayout() != sel_end_cursor.par->layout){
5592      sel_end_cursor.par->BreakParagraphConservative(0);
5593      sel_end_cursor.par->layout = LYX_DUMMY_LAYOUT;
5594      sel_end_cursor.par = sel_end_cursor.par->next;
5595    }
5596    else
5597      sel_end_cursor.par->layout = LYX_DUMMY_LAYOUT;
5598
5599    cursor = sel_end_cursor;
5600
5601    /* please break behind a space, if there is one. The space should
5602     * be erased too */ 
5603    if (sel_start_cursor.pos > 0 
5604        && sel_start_cursor.par->IsLineSeparator(sel_start_cursor.pos - 1))
5605      sel_start_cursor.pos--;
5606    if (sel_start_cursor.par->IsLineSeparator(sel_start_cursor.pos)) {
5607       sel_start_cursor.par->Erase(sel_start_cursor.pos);
5608    }
5609    
5610    sel_start_cursor.par->BreakParagraphConservative(sel_start_cursor.pos);
5611    tmppar = sel_start_cursor.par->Next();
5612    
5613    if (dummy_selection) {
5614            tmppar->Clear();
5615            if (kind == LyXParagraph::TAB
5616                || kind == LyXParagraph::FIG 
5617                || kind == LyXParagraph::WIDE_TAB
5618                || kind == LyXParagraph::WIDE_FIG 
5619                || kind == LyXParagraph::ALGORITHM) {
5620                    int lay = textclasslist.NumberOfLayout(parameters->textclass,
5621                                                      "Caption").second;
5622                    if (lay == -1) // layout not found
5623                            // use default layout "Standard" (0)
5624                            lay = 0;
5625                    tmppar->SetLayout(lay);
5626            }
5627    }
5628    else {
5629      if (sel_start_cursor.pos > 0) {
5630        /* the footnote-environment should begin with a standard layout.
5631         * Imagine you insert a footnote within an enumeration, you 
5632         * certainly do not want an enumerated footnote! */ 
5633        tmppar->Clear();
5634      }
5635      else {
5636        /* this is a exception the user would sometimes expect, I hope */
5637        sel_start_cursor.par->Clear();
5638      }
5639    }
5640    
5641    while (tmppar != sel_end_cursor.par) {
5642       tmppar->footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
5643       tmppar->footnotekind = kind;
5644       tmppar = tmppar->Next();
5645    } 
5646
5647    RedoParagraphs(sel_start_cursor, sel_end_cursor.par->Next());
5648    
5649    SetCursor(sel_start_cursor.par->Next(), 0);
5650
5651    ClearSelection();
5652 }
5653    
5654
5655 // returns pointer to a specified row
5656 Row * LyXText::GetRow(LyXParagraph * par,
5657                       LyXParagraph::size_type pos, long & y) const
5658 {
5659         Row * tmprow;
5660         
5661         if (currentrow) {
5662                 if (par == currentrow->par || par == currentrow->par->Previous()) {
5663                         // do not dereference par, it may have been deleted
5664                         // already! (Matthias) 
5665                         while (currentrow->previous && currentrow->previous->par != par) {
5666                                 currentrow = currentrow->previous;
5667                                 currentrow_y -= currentrow->height;
5668                         }
5669                         while (currentrow->previous && currentrow->previous->par == par) {
5670                                 currentrow = currentrow->previous;
5671                                 currentrow_y -= currentrow->height;
5672                         }
5673                 }
5674                 tmprow = currentrow;
5675                 y = currentrow_y;
5676                 // find the first row of the specified paragraph
5677                 while (tmprow->next && (tmprow->par != par)) {
5678                         y += tmprow->height;
5679                         tmprow = tmprow->next;
5680                 }
5681                 
5682                 if (tmprow->par == par){
5683                         // now find the wanted row
5684                         while (tmprow->pos < pos && tmprow->next && tmprow->next->par == par && 
5685                                tmprow->next->pos <= pos) {
5686                                 y += tmprow->height;
5687                                 tmprow = tmprow->next;
5688                         }
5689                         currentrow = tmprow;
5690                         currentrow_y = y;
5691                         return tmprow;
5692                 }
5693         }
5694         tmprow = firstrow;
5695         y = 0;
5696         // find the first row of the specified paragraph
5697         while (tmprow->next && (tmprow->par != par)) {
5698                 y += tmprow->height;
5699                 tmprow = tmprow->next;
5700         }
5701         
5702         // now find the wanted row
5703         while (tmprow->pos < pos && tmprow->next && tmprow->next->par == par && 
5704                tmprow->next->pos <= pos) {
5705                 y += tmprow->height;
5706                 tmprow = tmprow->next;
5707         }
5708         
5709         currentrow = tmprow;
5710         currentrow_y = y;
5711         
5712         return tmprow;
5713 }