]> git.lyx.org Git - lyx.git/blob - src/text.C
new painter,workarea and lcolor. Read the diff/sources and ChangeLog...
[lyx.git] / src / text.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-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,
4116                                                    row_ptr->height,
4117                                                    LColor::selection);
4118                         else
4119                                 pain.fillRectangle(sel_end_cursor.x, offset,
4120                                                    sel_start_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::red);
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                         owner_->painter().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, offset + y_top + 2 * DefaultHeight(),
4294                                   LColor::pagebreak, Painter::line_onoffdash);
4295                         y_top += 3 * DefaultHeight();
4296                 }
4297                 
4298                 if (row_ptr->par->added_space_top.kind() == VSpace::VFILL) {
4299                         /* draw a vfill top  */
4300                         pain.line(0, offset + 2 + y_top,
4301                                   LYX_PAPER_MARGIN, offset + 2 + y_top,
4302                                   LColor::vfillline);
4303                         
4304                         pain.line(0, offset + y_top + 3 * DefaultHeight(),
4305                                   LYX_PAPER_MARGIN,
4306                                   offset + y_top + 3 * DefaultHeight(),
4307                                   LColor::vfillline);
4308                         
4309                         pain.line(LYX_PAPER_MARGIN / 2, offset + 2 + y_top,
4310                                   LYX_PAPER_MARGIN / 2,
4311                                   offset + y_top + 3 * DefaultHeight(),
4312                                   LColor::vfillline);
4313                         
4314                         y_top += 3 * DefaultHeight();
4315                 }
4316                 
4317                 /* think about user added space */ 
4318                 y_top += int(row_ptr->par->added_space_top.inPixels());
4319                 
4320                 /* think about the parskip */ 
4321                 /* some parskips VERY EASY IMPLEMENTATION */ 
4322                 if (parameters->paragraph_separation == BufferParams::PARSEP_SKIP) {
4323                         if (layout.latextype == LATEX_PARAGRAPH
4324                             && firstpar->GetDepth() == 0
4325                             && firstpar->Previous())
4326                                 y_top += parameters->getDefSkip().inPixels();
4327                         else if (firstpar->Previous()
4328                                  && textclasslist.Style(parameters->textclass,
4329                                                    firstpar->Previous()->GetLayout()).latextype == LATEX_PARAGRAPH
4330                                  && firstpar->Previous()->GetDepth() == 0)
4331                                 // is it right to use defskip here, too? (AS) 
4332                                 y_top += parameters->getDefSkip().inPixels();
4333                 }
4334                 
4335                 if (row_ptr->par->line_top) {      /* draw a top line  */
4336                         y_top +=  GetFont(row_ptr->par, 0).ascent('x');
4337
4338                         pain.line(0, offset + y_top,
4339                                   paperwidth, offset + y_top,
4340                                   LColor::topline,
4341                                   Painter::line_solid,
4342                                   Painter::line_thick);
4343                         
4344                         y_top +=  GetFont(row_ptr->par, 0).ascent('x');
4345                 }
4346                 
4347                 /* should we print a label? */ 
4348                 if (layout.labeltype >= LABEL_STATIC
4349                     && (layout.labeltype != LABEL_STATIC
4350                         || layout.latextype != LATEX_ENVIRONMENT
4351                         || row_ptr->par->IsFirstInSequence())) {
4352                         font = GetFont(row_ptr->par, -2);
4353                         if (!row_ptr->par->GetLabestring().empty()) {
4354                                 tmpx = x;
4355                                 string tmpstring = row_ptr->par->GetLabestring();
4356                                 
4357                                 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
4358                                         if (parameters->secnumdepth >= 0){
4359                                                 /* this is special code for the chapter layout. This is printed in
4360                                                  * an extra row and has a pagebreak at the top. */
4361                                                 maxdesc = int(font.maxDescent() * layout.spacing.getValue() * parameters->spacing.getValue())
4362                                                         + int(layout.parsep) * DefaultHeight();
4363                                                 if (direction == LYX_DIR_RIGHT_TO_LEFT)
4364                                                         tmpx = paperwidth - LeftMargin(row_ptr) - 
4365                                                                 font.stringWidth(tmpstring);
4366                                                 pain.text(int(tmpx),
4367                                                           offset + row_ptr->baseline - row_ptr->ascent_of_text - maxdesc,
4368                                                           tmpstring, font);
4369                                         }
4370                                 } else {
4371                                         if (direction == LYX_DIR_LEFT_TO_RIGHT)
4372                                                 tmpx = x - font.stringWidth(layout.labelsep)
4373                                                         - font.stringWidth(tmpstring);
4374                                         else {
4375                                                 tmpx = paperwidth - LeftMargin(row_ptr)
4376                                                         + font.stringWidth(layout.labelsep);
4377                                                 if (row_ptr->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)  {
4378                                                         LyXFont font(LyXFont::ALL_SANE);
4379                                                         font.setSize(LyXFont::SIZE_SMALL);
4380                                                         tmpx += font.textWidth("Mwide-figM", 10);
4381                                                 }
4382                                         }
4383                                         /* draw it! */
4384                                         pain.text(int(tmpx),
4385                                                   offset + row_ptr->baseline,
4386                                                   tmpstring, font);
4387                                 }
4388                         }
4389                         /* the labels at the top of an environment. More or less for bibliography */ 
4390                 } else if (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
4391                            layout.labeltype == LABEL_BIBLIO ||
4392                            layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
4393                         if (row_ptr->par->IsFirstInSequence()) {
4394                                 font = GetFont(row_ptr->par, -2);
4395                                 if (!row_ptr->par->GetLabestring().empty()) {
4396                                         string tmpstring = row_ptr->par->GetLabestring();
4397                                         
4398                                         maxdesc = int(font.maxDescent() * layout.spacing.getValue() * parameters->spacing.getValue()
4399                                                          + (layout.labelbottomsep * DefaultHeight()));
4400                                         
4401                                         tmpx = x;
4402                                         if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT){
4403                                                 tmpx = ( ((direction == LYX_DIR_LEFT_TO_RIGHT)
4404                                                           ? x : LeftMargin(row_ptr) )
4405                                                          + paperwidth - RightMargin(row_ptr) ) / 2; 
4406                                                 tmpx -= (font.stringWidth(tmpstring)/2);
4407                                         } else if (direction == LYX_DIR_RIGHT_TO_LEFT)
4408                                                 tmpx = paperwidth - LeftMargin(row_ptr) - 
4409                                                         font.stringWidth(tmpstring);
4410                                         pain.text(int(tmpx),
4411                                                   offset + row_ptr->baseline
4412                                                   - row_ptr->ascent_of_text
4413                                                   - maxdesc,
4414                                                   tmpstring, font);
4415                                 }
4416                         }
4417                 }
4418                 if (layout.labeltype == LABEL_BIBLIO) { // ale970302
4419                         if (row_ptr->par->bibkey) {
4420                                 font = GetFont(row_ptr->par, -1);
4421                                 if (direction == LYX_DIR_LEFT_TO_RIGHT)
4422                                         tmpx = x - font.stringWidth(layout.labelsep)
4423                                                 - row_ptr->par->bibkey->width(owner_->painter(), font);
4424                                 else
4425                                         tmpx = paperwidth - LeftMargin(row_ptr)
4426                                                 + font.stringWidth(layout.labelsep);
4427                                 row_ptr->par->bibkey->draw(owner_->painter(),
4428                                                            font,
4429                                                            offset + row_ptr->baseline, 
4430                                                            tmpx);
4431                         }
4432                 } 
4433         }
4434         
4435         /* is it a last row? */
4436         par = row_ptr->par->LastPhysicalPar();
4437         if (row_ptr->par->ParFromPos(RowLast(row_ptr) + 1) == par
4438             && (!row_ptr->next || row_ptr->next->par != row_ptr->par)) {     
4439                 
4440                 /* think about the margins */ 
4441                 if (!row_ptr->next)
4442                         y_bottom -= LYX_PAPER_MARGIN;
4443                 
4444                 /* draw a bottom pagebreak */ 
4445                 if (firstpar->pagebreak_bottom) {
4446                         pain.line(0, offset + y_bottom - 2 * DefaultHeight(),
4447                                   paperwidth,
4448                                   offset + y_bottom - 2 * DefaultHeight(),
4449                                   LColor::pagebreak);
4450                         y_bottom -= 3 * DefaultHeight();
4451                 }
4452                 
4453                 if (firstpar->added_space_bottom.kind() == VSpace::VFILL) {
4454                         /* draw a vfill bottom  */
4455                         pain.line(0, offset + y_bottom - 3 * DefaultHeight(),
4456                                   LYX_PAPER_MARGIN,
4457                                   offset + y_bottom - 3 * DefaultHeight(),
4458                                   LColor::vfillline);
4459                         pain.line(0, offset + y_bottom - 2,
4460                                   LYX_PAPER_MARGIN,
4461                                   offset + y_bottom - 2,
4462                                   LColor::vfillline);
4463                         pain.line(LYX_PAPER_MARGIN / 2,
4464                                   offset + y_bottom - 3 * DefaultHeight(),
4465                                   LYX_PAPER_MARGIN / 2,
4466                                   offset + y_bottom - 2,
4467                                   LColor::vfillline);
4468                         y_bottom -= 3* DefaultHeight();
4469                 }
4470                 
4471                 /* think about user added space */ 
4472                 y_bottom -= int(firstpar->added_space_bottom.inPixels());
4473                 
4474                 if (firstpar->line_bottom) {
4475                         /* draw a bottom line */
4476                         y_bottom -= GetFont(par, par->Last() - 1).ascent('x');
4477                         pain.line(0, offset + y_bottom,
4478                                   paperwidth, offset + y_bottom,
4479                                   LColor::topline, Painter::line_solid,
4480                                   Painter::line_thick);
4481                         y_bottom -= GetFont(par, par->Last() - 1).ascent('x');
4482                 }
4483         }
4484         
4485         /* draw the text in the pixmap */  
4486         pos_end = RowLast(row_ptr);
4487         
4488         vpos = row_ptr->pos;
4489         /* table stuff -- begin*/
4490         if (row_ptr->par->table) {
4491                 bool on_off;
4492                 int cell = NumberOfCell(row_ptr->par, row_ptr->pos);
4493                 float x_old = x;
4494                 x += row_ptr->par->table->GetBeginningOfTextInCell(cell);
4495                 
4496                 while (vpos <= pos_end)  {
4497                         pos = vis2log(vpos);
4498                         if (row_ptr->par->IsNewline(pos)) {
4499                                 
4500                                 x = x_old + row_ptr->par->table->WidthOfColumn(cell);
4501                                 /* draw the table lines, still very simple */
4502                                 on_off = !row_ptr->par->table->TopLine(cell);
4503                                 if ((!on_off ||
4504                                      !row_ptr->par->table->TopAlreadyDrawed(cell)) &&
4505                                     !row_ptr->par->table->IsContRow(cell))
4506                                         pain.line(int(x_old),
4507                                                   offset + row_ptr->baseline - row_ptr->ascent_of_text,
4508                                                   int(x - x_old),
4509                                                   offset + row_ptr->baseline - row_ptr->ascent_of_text,
4510                                                   LColor::tableline);
4511                                 
4512                                 on_off = !row_ptr->par->table->BottomLine(cell);
4513                                 if ((!on_off && !row_ptr->par->table->RowHasContRow(cell)) ||
4514                                     row_ptr->par->table->VeryLastRow(cell))
4515
4516                                         pain.line(int(x_old),
4517                                                   offset + y_bottom - 1,
4518                                                   int(x - x_old),
4519                                                   offset + y_bottom - 1,
4520                                                   LColor::tableline);
4521
4522                                 on_off = !row_ptr->par->table->LeftLine(cell);
4523
4524                                 pain.line(int(x_old),
4525                                           offset + row_ptr->baseline - row_ptr->ascent_of_text,
4526                                           int(x_old),
4527                                           offset + y_bottom,
4528                                           LColor::tableline,
4529                                           Painter::line_onoffdash);
4530                                 
4531                                 on_off = !row_ptr->par->table->RightLine(cell);
4532
4533                                 pain.line(int(x) - row_ptr->par->table->AdditionalWidth(cell),
4534                                           offset + row_ptr->baseline - row_ptr->ascent_of_text,
4535                                           int(x) - row_ptr->par->table->AdditionalWidth(cell),
4536                                           offset + y_bottom - 1,
4537                                           LColor::tableline,
4538                                           Painter::line_onoffdash);
4539                                 
4540                                 x_old = x;
4541                 /* take care about the alignment and other spaces */
4542                                 ++cell;
4543                                 x += row_ptr->par->table->GetBeginningOfTextInCell(cell);
4544                                 if (row_ptr->par->table->IsFirstCell(cell))
4545                                         --cell; // little hack, sorry
4546                                 ++vpos;
4547                         } else if (row_ptr->par->IsHfill(pos)) {
4548                                 x += 1;
4549
4550                                 pain.line(int(x),
4551                                           offset + row_ptr->baseline - DefaultHeight() / 2,
4552                                           int(x),
4553                                           offset + row_ptr->baseline,
4554                                           LColor::vfillline);
4555                                 
4556                                 x += 2;
4557                                 ++vpos;
4558                         } else {
4559                                 if (row_ptr->par->IsSeparator(pos)) {
4560                                         tmpx = x;
4561                                         x+= SingleWidth(row_ptr->par, pos);
4562                                         /* -------> Only draw protected spaces when not in
4563                                          * free-spacing mode. */
4564                                         if (row_ptr->par->GetChar(pos) == LyXParagraph::META_PROTECTED_SEPARATOR && !layout.free_spacing) {
4565                                                 pain.line(int(tmpx),
4566                                                           offset + row_ptr->baseline - 3,
4567                                                           int(tmpx),
4568                                                           offset + row_ptr->baseline - 1,
4569                                                           LColor::vfillline);
4570                                                 
4571                                                 pain.line(int(tmpx),
4572                                                           offset + row_ptr->baseline - 1,
4573                                                           int(x - tmpx - 2),
4574                                                           offset + row_ptr->baseline - 1,
4575                                                           LColor::vfillline);
4576                                                 
4577                                                 pain.line(int(x - 2),
4578                                                           offset + row_ptr->baseline - 3,
4579                                                           int(x - 2),
4580                                                           offset + row_ptr->baseline - 1,
4581                                                           LColor::vfillline);
4582                                                 
4583                                                 /* what about underbars? */
4584                                                 font = GetFont(row_ptr->par, pos); 
4585                                                 if (font.underbar() == LyXFont::ON
4586                                                     && font.latex() != LyXFont::ON) {
4587                                                         pain.line(int(tmpx),
4588                                                                   offset + row_ptr->baseline + 2,
4589                                                                   int(x - tmpx),
4590                                                                   offset + row_ptr->baseline + 2);
4591                                                 }
4592                                         }
4593                                         ++vpos;
4594                                 } else
4595                                         draw(row_ptr, vpos, offset, x);
4596                         }
4597                 }
4598                 
4599                 /* do not forget the very last cell. This has no NEWLINE so 
4600                  * ignored by the code above*/ 
4601                 if (cell == row_ptr->par->table->GetNumberOfCells()-1){
4602                         x = x_old + row_ptr->par->table->WidthOfColumn(cell);
4603                         on_off = !row_ptr->par->table->TopLine(cell);
4604                         if ((!on_off ||
4605                              !row_ptr->par->table->TopAlreadyDrawed(cell)) &&
4606                             !row_ptr->par->table->IsContRow(cell))
4607
4608                                 pain.line(int(x_old),
4609                                           offset + row_ptr->baseline - row_ptr->ascent_of_text,
4610                                           int(x - x_old),
4611                                           offset + row_ptr->baseline - row_ptr->ascent_of_text,
4612                                           LColor::tableline,
4613                                           Painter::line_onoffdash);
4614                         on_off = !row_ptr->par->table->BottomLine(cell);
4615                         if ((!on_off && !row_ptr->par->table->RowHasContRow(cell)) ||
4616                             row_ptr->par->table->VeryLastRow(cell))
4617
4618                                 pain.line(int(x_old),
4619                                           offset + y_bottom - 1,
4620                                           int(x - x_old),
4621                                           offset + y_bottom - 1,
4622                                           LColor::tableline,
4623                                           Painter::line_onoffdash);
4624                         
4625                         on_off = !row_ptr->par->table->LeftLine(cell);
4626
4627                         pain.line(int(x_old),
4628                                   offset + row_ptr->baseline - row_ptr->ascent_of_text,
4629                                   int(x_old),
4630                                   offset + y_bottom - 1,
4631                                   LColor::tableline,
4632                                   Painter::line_onoffdash);
4633                         
4634                         on_off = !row_ptr->par->table->RightLine(cell);
4635
4636                         pain.line(int(x) - row_ptr->par->table->AdditionalWidth(cell),
4637                                   offset + row_ptr->baseline - row_ptr->ascent_of_text,
4638                                   int(x) - row_ptr->par->table->AdditionalWidth(cell),
4639                                   offset + y_bottom - 1,
4640                                   LColor::tableline,
4641                                   Painter::line_onoffdash);
4642                 }
4643         } else {
4644                 /* table stuff -- end*/
4645                 LyXParagraph::size_type main_body = 
4646                         BeginningOfMainBody(row_ptr->par);
4647                 if (main_body > 0 &&
4648                     (main_body-1 > pos_end || 
4649                      !row_ptr->par->IsLineSeparator(main_body-1)))
4650                         main_body = 0;
4651
4652                 while (vpos <= pos_end)  {
4653                         pos = vis2log(vpos);
4654                         if (main_body > 0 && pos == main_body-1) {
4655                                 x += fill_label_hfill
4656                                         + GetFont(row_ptr->par, -2).stringWidth(layout.labelsep)
4657                                         - SingleWidth(row_ptr->par, main_body-1);
4658                         }
4659
4660                         if (row_ptr->par->IsHfill(pos)) {
4661                                 x += 1;
4662                                 pain.line(int(x),
4663                                           offset + row_ptr->baseline - DefaultHeight() / 2,
4664                                           int(x),
4665                                           offset + row_ptr->baseline,
4666                                           LColor::vfillline);
4667                                 
4668                                 if (HfillExpansion(row_ptr, pos)) {
4669                                         if (pos >= main_body) {
4670                                                 pain.line(int(x),
4671                                                           offset + row_ptr->baseline - DefaultHeight() / 4,
4672                                                           int(fill_hfill),
4673                                                           offset + row_ptr->baseline - DefaultHeight() / 4,
4674                                                           LColor::vfillline,
4675                                                           Painter::line_onoffdash);
4676                                                 x += fill_hfill;
4677                     } else {
4678                             pain.line(int(x),
4679                                       offset + row_ptr->baseline - DefaultHeight() / 4,
4680                                       int(fill_label_hfill),
4681                                       offset + row_ptr->baseline - DefaultHeight() / 4,
4682                                       LColor::vfillline,
4683                                       Painter::line_onoffdash);
4684                             
4685                             x += fill_label_hfill;
4686                     }
4687                                         pain.line(int(x),
4688                                                   offset + row_ptr->baseline - DefaultHeight() / 2,
4689                                                   int(x),
4690                                                   offset + row_ptr->baseline,
4691                                                   LColor::vfillline);
4692                                 }
4693                                 x += 2;
4694                                 ++vpos;
4695                         } else {
4696                                 if (row_ptr->par->IsSeparator(pos)) {
4697                                         tmpx = x;
4698                                         x+= SingleWidth(row_ptr->par, pos);
4699                                         if (pos >= main_body)
4700                                                 x+= fill_separator;
4701                                         /* -------> Only draw protected spaces when not in
4702                                          * free-spacing mode. */
4703                                         if (row_ptr->par->GetChar(pos) == LyXParagraph::META_PROTECTED_SEPARATOR && !layout.free_spacing) {
4704
4705                                                 pain.line(int(tmpx),
4706                                                           offset + row_ptr->baseline - 3,
4707                                                           int(tmpx),
4708                                                           offset + row_ptr->baseline - 1,
4709                                                           LColor::vfillline);
4710                                                 
4711                                                 pain.line(int(tmpx),
4712                                                           offset + row_ptr->baseline - 1,
4713                                                           int(tmpx),
4714                                                           offset + row_ptr->baseline - 1,
4715                                                           LColor::vfillline);
4716
4717                                                 pain.line(int(x - 2),
4718                                                           offset + row_ptr->baseline - 3,
4719                                                           int(x - 2),
4720                                                           offset + row_ptr->baseline - 1,
4721                                                           LColor::vfillline);
4722                                                 
4723                                                 /* what about underbars? */
4724                                                 font = GetFont(row_ptr->par, pos); 
4725                                                 if (font.underbar() == LyXFont::ON
4726                                                     && font.latex() != LyXFont::ON) {
4727                                                         pain.line(int(tmpx),
4728                                                                   offset + row_ptr->baseline + 2,
4729                                                                   int(x - tmpx),
4730                                                                   offset + row_ptr->baseline + 2);
4731                                                 }
4732                                         }
4733                                         ++vpos;
4734                                 } else
4735                                         draw(row_ptr, vpos, offset, x);
4736                         }
4737                 }
4738         }
4739 #if 0
4740         // check for FAST SELECTION
4741         if (fast_selection || mono_video) {
4742                 if (selection) {
4743                         
4744                         /* selection code */ 
4745                         if (sel_start_cursor.row == row_ptr && sel_end_cursor.row == row_ptr) {
4746                                 scr.fillRectangle(gc_select, sel_start_cursor.x, offset,
4747                                                   sel_end_cursor.x - sel_start_cursor.x,
4748                                                   row_ptr->height); 
4749                         } else if (sel_start_cursor.row == row_ptr) {
4750                                 scr.fillRectangle(gc_select, sel_start_cursor.x, offset,
4751                                                   paperwidth - sel_start_cursor.x,
4752                                                   row_ptr->height);
4753                         } else if (sel_end_cursor.row == row_ptr) {
4754                                 scr.fillRectangle(gc_select, 0, offset,
4755                                                   sel_end_cursor.x,
4756                                                   row_ptr->height);
4757                         } else if (y > sel_start_cursor.y && y < sel_end_cursor.y) {
4758                                 scr.fillRectangle(gc_select, 0, offset,
4759                                                   paperwidth, row_ptr->height);
4760                                 
4761                         }
4762                 }
4763         }
4764 // end of FAST SELECTION code
4765         /* thats it */
4766 #endif
4767 }
4768 #else
4769 void LyXText::GetVisibleRow(LyXScreen & scr, int offset, 
4770                             Row * row_ptr, long y)
4771 {
4772         /* returns a printed row */
4773         LyXDirection direction = row_ptr->par->getParDirection();
4774         LyXParagraph::size_type vpos, pos, pos_end;
4775         float x, tmpx;
4776         int y_top, y_bottom;
4777         float fill_separator, fill_hfill, fill_label_hfill;
4778         LyXParagraph * par, * firstpar;
4779         LyXFont font;
4780         int maxdesc;
4781         if (row_ptr->height <= 0) {
4782                 lyxerr << "LYX_ERROR: row.height: " << row_ptr->height << endl;
4783                 return;
4784         }
4785         PrepareToPrint(row_ptr, x, fill_separator,
4786                        fill_hfill, fill_label_hfill);
4787
4788         /* initialize the pixmap */
4789         
4790         scr.fillRectangle(gc_clear,
4791                           0, offset, paperwidth, row_ptr->height);
4792         // check for NOT FAST SELECTION
4793         if (!fast_selection && !mono_video && selection) {
4794                 /* selection code */ 
4795                 if (sel_start_cursor.row == row_ptr &&
4796                     sel_end_cursor.row == row_ptr) {
4797                         if (sel_start_cursor.x < sel_end_cursor.x)
4798                           scr.fillRectangle(gc_selection, sel_start_cursor.x,
4799                                           offset,
4800                                           sel_end_cursor.x - sel_start_cursor.x,
4801                                           row_ptr->height);
4802                         else
4803                           scr.fillRectangle(gc_selection, sel_end_cursor.x,
4804                                           offset,
4805                                           sel_start_cursor.x - sel_end_cursor.x,
4806                                           row_ptr->height);
4807                 }
4808                 else if (sel_start_cursor.row == row_ptr) {
4809                      if (direction == LYX_DIR_LEFT_TO_RIGHT)
4810                         scr.fillRectangle(gc_selection, sel_start_cursor.x,
4811                                           offset,
4812                                           paperwidth - sel_start_cursor.x,
4813                                           row_ptr->height);
4814                      else
4815                         scr.fillRectangle(gc_selection, 0, offset,
4816                                           sel_start_cursor.x, row_ptr->height);
4817                 } else if (sel_end_cursor.row == row_ptr) {
4818                      if (direction == LYX_DIR_LEFT_TO_RIGHT)
4819                         scr.fillRectangle(gc_selection, 0, offset,
4820                                           sel_end_cursor.x, row_ptr->height);
4821                      else
4822                         scr.fillRectangle(gc_selection, sel_end_cursor.x,
4823                                           offset,
4824                                           paperwidth - sel_end_cursor.x,
4825                                           row_ptr->height);
4826                 } else if (y > sel_start_cursor.y && y < sel_end_cursor.y) {
4827                         scr.fillRectangle(gc_selection, 0, offset,
4828                                           paperwidth, row_ptr->height);
4829                         
4830                 }
4831         } // end of NOT FAST SELECTION code
4832         
4833         if (row_ptr->par->appendix){
4834           scr.drawVerticalLine(gc_math, 1, offset, offset+row_ptr->height);
4835           scr.drawVerticalLine(gc_math, paperwidth-2 , offset, offset+row_ptr->height);
4836         }
4837
4838         if (row_ptr->par->pextra_type == LyXParagraph::PEXTRA_MINIPAGE) {
4839                 /* draw a marker at the left margin! */ 
4840                 LyXFont font = GetFont(row_ptr->par, 0);
4841                 int asc = font.maxAscent();
4842                 int x = (LYX_PAPER_MARGIN - font.width('|')) / 2;
4843                 int y1 = (offset + row_ptr->baseline);
4844                 int y2 = (offset + row_ptr->baseline) - asc;
4845
4846                 scr.drawVerticalLine(gc_minipage, x, y1, y2);
4847         }       
4848         if (row_ptr->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
4849                 LyXFont font(LyXFont::ALL_SANE);
4850                 font.setSize(LyXFont::SIZE_FOOTNOTE);
4851 #ifdef USE_PAINTER
4852                 font.setColor(LColor::footnote);
4853 #else
4854                 font.setColor(LyXFont::RED);
4855 #endif
4856                 
4857                 int box_x = LYX_PAPER_MARGIN;
4858                 box_x += font.textWidth(" wide-tab ", 10);
4859                 if (row_ptr->previous && 
4860                     row_ptr->previous->par->footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
4861                         string fs;
4862                         switch (row_ptr->par->footnotekind) {
4863                         case LyXParagraph::MARGIN:
4864                                 fs = " margin";
4865                                 break;
4866                         case LyXParagraph::FIG:
4867                                 fs = " fig";
4868                                 break;
4869                         case LyXParagraph::TAB:
4870                                 fs = " tab";
4871                                 break;
4872                         case LyXParagraph::WIDE_FIG:
4873                                 fs = " wide-fig";
4874                                 break;
4875                         case LyXParagraph::WIDE_TAB:
4876                                 fs = " wide-tab";
4877                                 break;
4878                         case LyXParagraph::ALGORITHM:
4879                                 fs = " alg";
4880                                 break;
4881                         case LyXParagraph::FOOTNOTE:
4882                                 fs = " foot";
4883                                 break;
4884                         }
4885                         
4886                         // Determine background color.
4887                         gc_type back = gc_lighted;
4888                         if (mono_video) {
4889                                 back = gc_clear;
4890                         }
4891                         scr.fillRectangle(back, LYX_PAPER_MARGIN, offset+1,
4892                                           box_x - LYX_PAPER_MARGIN, 
4893                                           int(font.maxAscent())+
4894                                           int(font.maxDescent()));
4895                         
4896                         scr.drawLine(gc_foot,
4897                                      offset,
4898                                      LYX_PAPER_MARGIN,
4899                                      paperwidth - 2*LYX_PAPER_MARGIN);
4900                         
4901                         scr.drawString(font, fs,
4902                                        offset + int(font.maxAscent())+1,
4903                                        LYX_PAPER_MARGIN);
4904                         scr.drawVerticalLine(gc_foot,
4905                                              LYX_PAPER_MARGIN,
4906                                              offset,
4907                                              offset
4908                                              + int(font.maxAscent())+
4909                                              int(font.maxDescent()));
4910                         
4911                         scr.drawLine(gc_foot,
4912                                      offset
4913                                      + int(font.maxAscent())
4914                                      + int(font.maxDescent()) + 1,
4915                                      LYX_PAPER_MARGIN, box_x - LYX_PAPER_MARGIN); 
4916                 }
4917                 
4918                 /* draw the open floats in a red box */
4919                 scr.drawVerticalLine(gc_foot,
4920                                      box_x,
4921                                      offset,  offset + row_ptr->height);
4922                 
4923                 scr.drawVerticalLine(gc_foot,
4924                                      paperwidth - LYX_PAPER_MARGIN,
4925                                      offset,
4926                                      offset + row_ptr->height);
4927                                         
4928         } else  {
4929                 if (row_ptr->previous &&
4930                     row_ptr->previous->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
4931                         LyXFont font(LyXFont::ALL_SANE);
4932                         font.setSize(LyXFont::SIZE_FOOTNOTE);
4933
4934                         int box_x = LYX_PAPER_MARGIN;
4935                         box_x += font.textWidth(" wide-tab ", 10);
4936                         
4937                         scr.drawLine(gc_foot,
4938                                      offset,
4939                                      box_x,
4940                                      paperwidth - LYX_PAPER_MARGIN - box_x);
4941                 }
4942         }
4943         
4944         LyXLayout const & layout = textclasslist.Style(parameters->textclass,
4945                                            row_ptr->par->GetLayout());
4946         firstpar = row_ptr->par->FirstPhysicalPar();
4947         
4948         y_top = 0;
4949         y_bottom = row_ptr->height;
4950         
4951         /* is it a first row? */ 
4952         if (row_ptr->pos == 0
4953             && row_ptr->par == firstpar) {
4954                 
4955                  /* start of appendix? */
4956                 if (row_ptr->par->start_of_appendix){
4957                   scr.drawLine(gc_math, 
4958                                offset,
4959                                1, paperwidth-2);
4960                 }
4961
4962                 /* think about the margins */ 
4963                 if (!row_ptr->previous)
4964                         y_top += LYX_PAPER_MARGIN;
4965                 
4966                 if (row_ptr->par->pagebreak_top){ /* draw a top pagebreak  */
4967                         scr.drawOnOffLine(offset + y_top + 2 * DefaultHeight(),
4968                                           0, paperwidth);
4969                         y_top += 3 * DefaultHeight();
4970                 }
4971                 
4972                 if (row_ptr->par->added_space_top.kind() == VSpace::VFILL) {
4973                         /* draw a vfill top  */
4974                         scr.drawLine(gc_fill, 
4975                                      offset + 2 + y_top,
4976                                      0, LYX_PAPER_MARGIN);
4977                         scr.drawLine(gc_fill,
4978                                      offset + y_top + 3 * DefaultHeight(),
4979                                      0, LYX_PAPER_MARGIN);
4980                         scr.drawVerticalOnOffLine(LYX_PAPER_MARGIN / 2, 
4981                                                   offset + 2 + y_top,
4982                                                   offset + y_top + 3 *
4983                                                   DefaultHeight());
4984                         
4985                         y_top += 3 * DefaultHeight();
4986                 }
4987                 
4988                 /* think about user added space */ 
4989                 y_top += int(row_ptr->par->added_space_top.inPixels());
4990                 
4991                 /* think about the parskip */ 
4992                 /* some parskips VERY EASY IMPLEMENTATION */ 
4993                 if (parameters->paragraph_separation == BufferParams::PARSEP_SKIP) {
4994                         if (layout.latextype == LATEX_PARAGRAPH
4995                             && firstpar->GetDepth() == 0
4996                             && firstpar->Previous())
4997                                 y_top += parameters->getDefSkip().inPixels();
4998                         else if (firstpar->Previous()
4999                                  && textclasslist.Style(parameters->textclass,
5000                                                    firstpar->Previous()->GetLayout()).latextype == LATEX_PARAGRAPH
5001                                  && firstpar->Previous()->GetDepth() == 0)
5002                                 // is it right to use defskip here, too? (AS) 
5003                                 y_top += parameters->getDefSkip().inPixels();
5004                 }
5005                 
5006                 if (row_ptr->par->line_top) {      /* draw a top line  */
5007                         y_top +=  GetFont(row_ptr->par, 0).ascent('x');
5008
5009                         scr.drawThickLine(offset + y_top,
5010                                           0, paperwidth);
5011                         y_top +=  GetFont(row_ptr->par, 0).ascent('x');
5012                 }
5013                 
5014                 /* should we print a label? */ 
5015                 if (layout.labeltype >= LABEL_STATIC
5016                     && (layout.labeltype != LABEL_STATIC
5017                         || layout.latextype != LATEX_ENVIRONMENT
5018                         || row_ptr->par->IsFirstInSequence())) {
5019                         font = GetFont(row_ptr->par, -2);
5020                         if (!row_ptr->par->GetLabestring().empty()) {
5021                                 tmpx = x;
5022                                 string tmpstring = row_ptr->par->GetLabestring();
5023                                 
5024                                 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
5025                                         if (parameters->secnumdepth >= 0){
5026                                                 /* this is special code for the chapter layout. This is printed in
5027                                                  * an extra row and has a pagebreak at the top. */
5028                                                 maxdesc = int(font.maxDescent() * layout.spacing.getValue() * parameters->spacing.getValue())
5029                                                         + int(layout.parsep) * DefaultHeight();
5030                                                 if (direction == LYX_DIR_RIGHT_TO_LEFT)
5031                                                         tmpx = paperwidth - LeftMargin(row_ptr) - 
5032                                                                 font.stringWidth(tmpstring);
5033                                                 scr.drawString(font, tmpstring,
5034                                                                offset + row_ptr->baseline
5035                                                                - row_ptr->ascent_of_text - maxdesc,
5036                                                                int(tmpx));
5037                                         }
5038                                 } else {
5039                                         if (direction == LYX_DIR_LEFT_TO_RIGHT)
5040                                                 tmpx = x - font.stringWidth(layout.labelsep)
5041                                                         - font.stringWidth(tmpstring);
5042                                         else {
5043                                                 tmpx = paperwidth - LeftMargin(row_ptr)
5044                                                         + font.stringWidth(layout.labelsep);
5045                                                 if (row_ptr->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)  {
5046                                                         LyXFont font(LyXFont::ALL_SANE);
5047                                                         font.setSize(LyXFont::SIZE_SMALL);
5048                                                         tmpx += font.textWidth("Mwide-figM", 10);
5049                                                 }
5050                                         }
5051                                         /* draw it! */
5052                                         scr.drawString(font, tmpstring,
5053                                                        offset + row_ptr->baseline, int(tmpx));
5054                                 }
5055                         }
5056                         /* the labels at the top of an environment. More or less for bibliography */ 
5057                 } else if (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
5058                            layout.labeltype == LABEL_BIBLIO ||
5059                            layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
5060                         if (row_ptr->par->IsFirstInSequence()) {
5061                                 font = GetFont(row_ptr->par, -2);
5062                                 if (!row_ptr->par->GetLabestring().empty()) {
5063                                         string tmpstring = row_ptr->par->GetLabestring();
5064                                         
5065                                         maxdesc = int(font.maxDescent() * layout.spacing.getValue() * parameters->spacing.getValue()
5066                                                          + (layout.labelbottomsep * DefaultHeight()));
5067                                         
5068                                         tmpx = x;
5069                                         if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT){
5070                                                 tmpx = ( ((direction == LYX_DIR_LEFT_TO_RIGHT)
5071                                                           ? x : LeftMargin(row_ptr) )
5072                                                          + paperwidth - RightMargin(row_ptr) ) / 2; 
5073                                                 tmpx -= (font.stringWidth(tmpstring)/2);
5074                                         } else if (direction == LYX_DIR_RIGHT_TO_LEFT)
5075                                                 tmpx = paperwidth - LeftMargin(row_ptr) - 
5076                                                         font.stringWidth(tmpstring);
5077
5078                                         scr.drawString(font, tmpstring,
5079                                                        offset + row_ptr->baseline
5080                                                        - row_ptr->ascent_of_text - maxdesc,  
5081                                                        int(tmpx));                  
5082                                 }
5083                         }
5084                 }
5085                 if (layout.labeltype == LABEL_BIBLIO) { // ale970302
5086                         if (row_ptr->par->bibkey) {
5087                                 font = GetFont(row_ptr->par, -1);
5088                                 if (direction == LYX_DIR_LEFT_TO_RIGHT)
5089                                         tmpx = x - font.stringWidth(layout.labelsep)
5090                                                 - row_ptr->par->bibkey->Width(font);
5091                                 else
5092                                         tmpx = paperwidth - LeftMargin(row_ptr)
5093                                                 + font.stringWidth(layout.labelsep);
5094                                 row_ptr->par->bibkey->Draw(font, scr,
5095                                                            offset + row_ptr->baseline, 
5096                                                            tmpx);
5097                         }
5098                 } 
5099         }
5100         
5101         /* is it a last row? */
5102         par = row_ptr->par->LastPhysicalPar();
5103         if (row_ptr->par->ParFromPos(RowLast(row_ptr) + 1) == par
5104             && (!row_ptr->next || row_ptr->next->par != row_ptr->par)) {     
5105                 
5106                 /* think about the margins */ 
5107                 if (!row_ptr->next)
5108                         y_bottom -= LYX_PAPER_MARGIN;
5109                 
5110                 /* draw a bottom pagebreak */ 
5111                 if (firstpar->pagebreak_bottom) {
5112                         scr.drawOnOffLine(offset + y_bottom - 2 *
5113                                           DefaultHeight(),
5114                                           0, paperwidth);
5115                         y_bottom -= 3 * DefaultHeight();
5116                 }
5117                 
5118                 if (firstpar->added_space_bottom.kind() == VSpace::VFILL) {
5119                         /* draw a vfill bottom  */
5120                         scr.drawLine(gc_fill,
5121                                      offset + y_bottom - 3 * DefaultHeight(),
5122                                      0, LYX_PAPER_MARGIN);
5123                         scr.drawLine(gc_fill, offset + y_bottom - 2,
5124                                      0, LYX_PAPER_MARGIN);
5125                         scr.drawVerticalOnOffLine(LYX_PAPER_MARGIN / 2, 
5126                                                   offset + y_bottom - 3 * DefaultHeight(),
5127                                                   offset + y_bottom - 2
5128                                 );          
5129                         y_bottom -= 3* DefaultHeight();
5130                 }
5131                 
5132                 /* think about user added space */ 
5133                 y_bottom -= int(firstpar->added_space_bottom.inPixels());
5134                 
5135                 if (firstpar->line_bottom) {
5136                         /* draw a bottom line */
5137                         y_bottom -= GetFont(par, par->Last() - 1).ascent('x');
5138
5139                         scr.drawThickLine(offset + y_bottom,
5140                                           0, paperwidth);           
5141                         y_bottom -= GetFont(par, par->Last() - 1).ascent('x');
5142                 }
5143         }
5144         
5145         /* draw the text in the pixmap */  
5146         pos_end = RowLast(row_ptr);
5147         
5148         vpos = row_ptr->pos;
5149         /* table stuff -- begin*/
5150         if (row_ptr->par->table) {
5151                 bool on_off;
5152                 int cell = NumberOfCell(row_ptr->par, row_ptr->pos);
5153                 float x_old = x;
5154                 x += row_ptr->par->table->GetBeginningOfTextInCell(cell);
5155                 
5156                 while (vpos <= pos_end)  {
5157                         pos = vis2log(vpos);
5158                         if (row_ptr->par->IsNewline(pos)) {
5159                                 
5160                                 x = x_old + row_ptr->par->table->WidthOfColumn(cell);
5161                                 /* draw the table lines, still very simple */
5162                                 on_off = !row_ptr->par->table->TopLine(cell);
5163                                 if ((!on_off ||
5164                                      !row_ptr->par->table->TopAlreadyDrawed(cell)) &&
5165                                     !row_ptr->par->table->IsContRow(cell))
5166                                         scr.drawTableLine(offset + row_ptr->baseline -
5167                                                           row_ptr->ascent_of_text,
5168                                                           int(x_old), int(x - x_old), on_off);
5169                                 on_off = !row_ptr->par->table->BottomLine(cell);
5170                                 if ((!on_off && !row_ptr->par->table->RowHasContRow(cell)) ||
5171                                     row_ptr->par->table->VeryLastRow(cell))
5172                                         scr.drawTableLine(offset + y_bottom - 1,
5173                                                           int(x_old), int(x - x_old), on_off);
5174                                 on_off = !row_ptr->par->table->LeftLine(cell);
5175                                 
5176                                 scr.drawVerticalTableLine(int(x_old), 
5177                                                           offset + row_ptr->baseline -
5178                                                           row_ptr->ascent_of_text,
5179                                                           offset + y_bottom - 1,
5180                                                           on_off);
5181                                 on_off = !row_ptr->par->table->RightLine(cell);
5182                                 
5183                                 scr.drawVerticalTableLine(int(x) -
5184                                                           row_ptr->par->table->AdditionalWidth(cell),
5185                                                           offset + row_ptr->baseline -
5186                                                           row_ptr->ascent_of_text,
5187                                                           offset + y_bottom - 1,
5188                                                           on_off);
5189                                 x_old = x;
5190                 /* take care about the alignment and other spaces */
5191                                 ++cell;
5192                                 x += row_ptr->par->table->GetBeginningOfTextInCell(cell);
5193                                 if (row_ptr->par->table->IsFirstCell(cell))
5194                                         --cell; // little hack, sorry
5195                                 ++vpos;
5196                         } else if (row_ptr->par->IsHfill(pos)) {
5197                                 x += 1;
5198                                 
5199                                 scr.drawVerticalLine(gc_fill, int(x),  
5200                                                      offset + row_ptr->baseline - DefaultHeight()/2, 
5201                                                      offset + row_ptr->baseline);               
5202                                 x += 2;
5203                                 ++vpos;
5204                         } else {
5205                                 if (row_ptr->par->IsSeparator(pos)) {
5206                                         tmpx = x;
5207                                         x+= SingleWidth(row_ptr->par, pos);
5208                                         /* -------> Only draw protected spaces when not in
5209                                          * free-spacing mode. */
5210                                         if (row_ptr->par->GetChar(pos) == LyXParagraph::META_PROTECTED_SEPARATOR && !layout.free_spacing) {
5211                                                 scr.drawVerticalLine(gc_fill, int(tmpx),
5212                                                                      offset + row_ptr->baseline - 3,
5213                                                                      offset + row_ptr->baseline - 1);
5214                                                 scr.drawLine(gc_fill,
5215                                                              offset + row_ptr->baseline - 1,
5216                                                              int(tmpx),
5217                                                              int(x-tmpx-2));
5218                                                 scr.drawVerticalLine(gc_fill, int(x-2),
5219                                                                      offset + row_ptr->baseline - 3,
5220                                                                      offset + row_ptr->baseline - 1);                   
5221                                                 /* what about underbars? */
5222                                                 font = GetFont(row_ptr->par, pos); 
5223                                                 if (font.underbar() == LyXFont::ON
5224                                                     && font.latex() != LyXFont::ON) {
5225                                                         scr.drawLine(gc_copy,
5226                                                                      offset +
5227                                                                      row_ptr->baseline + 2,
5228                                                                      int(tmpx),
5229                                                                      int(x - tmpx));                        
5230                                                 }
5231                                         }
5232                                         ++vpos;
5233                                 } else
5234                                         Draw(row_ptr, vpos, scr, offset, x);
5235                         }
5236                 }
5237                 
5238                 /* do not forget the very last cell. This has no NEWLINE so 
5239                  * ignored by the code above*/ 
5240                 if (cell == row_ptr->par->table->GetNumberOfCells()-1){
5241                         x = x_old + row_ptr->par->table->WidthOfColumn(cell);
5242                         on_off = !row_ptr->par->table->TopLine(cell);
5243                         if ((!on_off ||
5244                              !row_ptr->par->table->TopAlreadyDrawed(cell)) &&
5245                             !row_ptr->par->table->IsContRow(cell))
5246                                 
5247                                 scr.drawTableLine(offset + row_ptr->baseline -
5248                                                   row_ptr->ascent_of_text,
5249                                                   int(x_old), int(x - x_old), on_off);      
5250                         on_off = !row_ptr->par->table->BottomLine(cell);
5251                         if ((!on_off && !row_ptr->par->table->RowHasContRow(cell)) ||
5252                             row_ptr->par->table->VeryLastRow(cell))
5253                                 
5254                                 scr.drawTableLine(offset + y_bottom - 1,
5255                                                   int(x_old), int(x - x_old), on_off);      
5256                         on_off = !row_ptr->par->table->LeftLine(cell);
5257                         
5258                         scr.drawVerticalTableLine(int(x_old), 
5259                                                   offset + row_ptr->baseline -
5260                                                   row_ptr->ascent_of_text,
5261                                                   offset + y_bottom - 1,
5262                                                   on_off);          
5263                         on_off = !row_ptr->par->table->RightLine(cell);
5264                         
5265                         scr.drawVerticalTableLine(int(x) -
5266                                                   row_ptr->par->table->AdditionalWidth(cell),
5267                                                   offset + row_ptr->baseline -
5268                                                   row_ptr->ascent_of_text,
5269                                                   offset + y_bottom - 1,
5270                                                   on_off);          
5271                 }
5272         } else {
5273                 /* table stuff -- end*/
5274                 LyXParagraph::size_type main_body = 
5275                         BeginningOfMainBody(row_ptr->par);
5276                 if (main_body > 0 &&
5277                     (main_body-1 > pos_end || 
5278                      !row_ptr->par->IsLineSeparator(main_body-1)))
5279                         main_body = 0;
5280
5281                 while (vpos <= pos_end)  {
5282                         pos = vis2log(vpos);
5283                         if (main_body > 0 && pos == main_body-1) {
5284                                 x += fill_label_hfill
5285                                         + GetFont(row_ptr->par, -2).stringWidth(layout.labelsep)
5286                                         - SingleWidth(row_ptr->par, main_body-1);
5287                         }
5288
5289                         if (row_ptr->par->IsHfill(pos)) {
5290                                 x += 1;
5291                                 scr.drawVerticalLine(gc_fill, int(x),  
5292                                                      offset + row_ptr->baseline - DefaultHeight()/2, 
5293                                                      offset + row_ptr->baseline);               
5294                                 if (HfillExpansion(row_ptr, pos)) {
5295                                         if (pos >= main_body) {
5296                                                 scr.drawOnOffLine(offset + row_ptr->baseline -
5297                                                                   DefaultHeight()/4,
5298                                                                   int(x),
5299                                                                   int(fill_hfill));                     
5300                                                 x += fill_hfill;
5301                     } else {
5302                             scr.drawOnOffLine(offset + row_ptr->baseline -
5303                                               DefaultHeight()/4,
5304                                               int(x),
5305                                               int(fill_label_hfill));
5306                             x += fill_label_hfill;
5307                     }
5308                                         scr.drawVerticalLine(gc_fill, int(x),
5309                                                              offset + row_ptr->baseline -
5310                                                              DefaultHeight()/2, 
5311                                                              offset + row_ptr->baseline);
5312                                 }
5313                                 x += 2;
5314                                 ++vpos;
5315                         } else {
5316                                 if (row_ptr->par->IsSeparator(pos)) {
5317                                         tmpx = x;
5318                                         x+= SingleWidth(row_ptr->par, pos);
5319                                         if (pos >= main_body)
5320                                                 x+= fill_separator;
5321                                         /* -------> Only draw protected spaces when not in
5322                                          * free-spacing mode. */
5323                                         if (row_ptr->par->GetChar(pos) == LyXParagraph::META_PROTECTED_SEPARATOR && !layout.free_spacing) {
5324                                                 
5325                                                 scr.drawVerticalLine(gc_fill, int(tmpx),
5326                                                                      offset + row_ptr->baseline - 3,
5327                                                                      offset + row_ptr->baseline - 1);
5328                                                 scr.drawLine(gc_fill,
5329                                                              offset + row_ptr->baseline - 1,
5330                                                              int(tmpx),
5331                                                              int(x-tmpx-2));
5332                                                 scr.drawVerticalLine(gc_fill, int(x-2),
5333                                                                      offset + row_ptr->baseline - 3,
5334                                                                      offset + row_ptr->baseline - 1);                   
5335                                                 /* what about underbars? */
5336                                                 font = GetFont(row_ptr->par, pos); 
5337                                                 if (font.underbar() == LyXFont::ON
5338                                                     && font.latex() != LyXFont::ON) {
5339                                                         scr.drawLine(gc_copy,
5340                                              offset + row_ptr->baseline + 2,
5341                                                                      int(tmpx),
5342                                                                      int(x - tmpx));
5343                                                 }
5344                                         }
5345                                         ++vpos;
5346                                 } else
5347                                         Draw(row_ptr, vpos, scr, offset, x);
5348                         }
5349                 }
5350         }
5351         // check for FAST SELECTION
5352         if (fast_selection || mono_video) {
5353                 if (selection) {
5354                         
5355                         /* selection code */ 
5356                         if (sel_start_cursor.row == row_ptr && sel_end_cursor.row == row_ptr) {
5357                                 scr.fillRectangle(gc_select, sel_start_cursor.x, offset,
5358                                                   sel_end_cursor.x - sel_start_cursor.x,
5359                                                   row_ptr->height); 
5360                         } else if (sel_start_cursor.row == row_ptr) {
5361                                 scr.fillRectangle(gc_select, sel_start_cursor.x, offset,
5362                                                   paperwidth - sel_start_cursor.x,
5363                                                   row_ptr->height);
5364                         } else if (sel_end_cursor.row == row_ptr) {
5365                                 scr.fillRectangle(gc_select, 0, offset,
5366                                                   sel_end_cursor.x,
5367                                                   row_ptr->height);
5368                         } else if (y > sel_start_cursor.y && y < sel_end_cursor.y) {
5369                                 scr.fillRectangle(gc_select, 0, offset,
5370                                                   paperwidth, row_ptr->height);
5371                                 
5372                         }
5373                 }
5374         }
5375 // end of FAST SELECTION code
5376         /* thats it */ 
5377 }
5378 #endif
5379
5380 int LyXText::DefaultHeight() const
5381 {
5382         LyXFont font(LyXFont::ALL_SANE);
5383         return int(font.maxAscent() + font.maxDescent() * 1.5);
5384 }
5385
5386    
5387 /* returns the column near the specified x-coordinate of the row 
5388 * x is set to the real beginning of this column  */ 
5389 int LyXText::GetColumnNearX(Row * row, int & x) const
5390 {
5391         float tmpx = 0.0;
5392         float fill_separator, fill_hfill, fill_label_hfill;
5393    
5394         PrepareToPrint(row, tmpx, fill_separator,
5395                        fill_hfill, fill_label_hfill);
5396
5397         LyXDirection direction = row->par->getParDirection();
5398         LyXParagraph::size_type vc = row->pos;
5399         LyXParagraph::size_type last = RowLast(row);
5400         LyXParagraph::size_type c;
5401
5402         LyXLayout const & layout = textclasslist.Style(parameters->textclass,
5403                                            row->par->GetLayout());
5404         /* table stuff -- begin */
5405         if (row->par->table) {
5406                 if (row->next && row->next->par == row->par //the last row doesn't need a newline at the end
5407                     && row->par->IsNewline(last))
5408                         last--;
5409                 int cell = NumberOfCell(row->par, row->pos);
5410                 float x_old = tmpx;
5411                 bool ready = false;
5412                 tmpx += row->par->table->GetBeginningOfTextInCell(cell);
5413                 while (vc <= last
5414                        && (c = vis2log(vc)) >= 0
5415                        && tmpx + (SingleWidth(row->par, c)/2) <= x
5416                        && !ready){
5417                         if (row->par->IsNewline(c)) {
5418                                 if (x_old + row->par->table->WidthOfColumn(cell) <= x){
5419                                         tmpx = x_old + row->par->table->WidthOfColumn(cell);
5420                                         x_old = tmpx;
5421                                         ++cell;
5422                                         tmpx += row->par->table->GetBeginningOfTextInCell(cell);
5423                                         ++vc;
5424                                 } else
5425                                         ready = true;
5426                         } else {
5427                                 tmpx += SingleWidth(row->par, c);
5428                                 ++vc;
5429                         }
5430                 }
5431         } else {
5432                 /* table stuff -- end*/
5433                 LyXParagraph::size_type main_body = BeginningOfMainBody(row->par);
5434                 float last_tmpx = tmpx;
5435
5436                 if (main_body > 0 &&
5437                     (main_body-1 > last || 
5438                      !row->par->IsLineSeparator(main_body-1)))
5439                         main_body = 0;
5440
5441                 while (vc <= last && tmpx <= x) {
5442                         c = vis2log(vc);
5443                         last_tmpx = tmpx;
5444                         if (main_body > 0 && c == main_body-1) {
5445                                 tmpx += fill_label_hfill +
5446                                         GetFont(row->par, -2).stringWidth(layout.labelsep);
5447                                 if (row->par->IsLineSeparator(main_body-1))
5448                                         tmpx -= SingleWidth(row->par, main_body-1);
5449                         }
5450              
5451                         tmpx += SingleWidth(row->par, c);
5452                         if (HfillExpansion(row, c)) {
5453                                 if (c >= main_body)
5454                                         tmpx += fill_hfill;
5455                                 else
5456                                         tmpx += fill_label_hfill;
5457                         }
5458                         else if (c >= main_body
5459                                  && row->par->IsSeparator(c)) {
5460                                 tmpx+= fill_separator;  
5461                         }
5462                         ++vc;
5463                 }
5464
5465                 if (vc > row->pos && (tmpx+last_tmpx)/2 > x) {
5466                         vc--;
5467                         tmpx = last_tmpx;
5468                 }
5469         }
5470         /* make sure that a last space in a row doesnt count */
5471         if (row->pos <= last
5472             && !(!row->next || row->next->par != row->par))
5473                 if (direction == LYX_DIR_LEFT_TO_RIGHT && vc > last
5474                     && row->par->IsLineSeparator(vis2log(last)) ) {
5475                         vc = last;
5476                         tmpx -= fill_separator+SingleWidth(row->par, vis2log(last));
5477                 } else if (direction == LYX_DIR_RIGHT_TO_LEFT 
5478                            && vc == row->pos
5479                            && row->par->IsLineSeparator(vis2log(row->pos)) ) {
5480                         vc = row->pos+1;
5481                         tmpx += fill_separator+SingleWidth(row->par, vis2log(row->pos));
5482                 }
5483
5484         if (row->pos > last)  // Row is empty?
5485                 c = row->pos;
5486         else if (vc <= last) {
5487                 c = vis2log(vc);
5488                 LyXDirection direction = row->par->getLetterDirection(c);
5489                 if (vc > row->pos && row->par->IsLineSeparator(c)
5490                     && row->par->getLetterDirection(vis2log(vc - 1)) != direction)
5491                         c = vis2log(vc-1);
5492                 if (direction == LYX_DIR_RIGHT_TO_LEFT)
5493                         ++c;
5494         } else {
5495                 c = vis2log(last)+1;
5496                 if (row->par->getLetterDirection(c - 1) == LYX_DIR_RIGHT_TO_LEFT)
5497                         --c;            
5498         }
5499
5500         if (!row->par->table && row->pos <= last && c > last
5501             && row->par->IsNewline(last)) {
5502                 if (row->par->getLetterDirection(last) == LYX_DIR_LEFT_TO_RIGHT)
5503                         tmpx -= SingleWidth(row->par, last);
5504                 else
5505                         tmpx += SingleWidth(row->par, last);
5506                 c = last;
5507         }
5508
5509         c -= row->pos;
5510         x = int(tmpx);
5511         return c;
5512 }
5513
5514    
5515 /* turn the selection into a new environment. If there is no selection,
5516 * create an empty environment */ 
5517 void LyXText::InsertFootnoteEnvironment(LyXParagraph::footnote_kind kind)
5518 {
5519    /* no footnoteenvironment in a footnoteenvironment */ 
5520    if (cursor.par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
5521       WriteAlert(_("Impossible operation"), 
5522                  _("You can't insert a float in a float!"), 
5523                  _("Sorry."));
5524      return;
5525    }
5526    /* no marginpars in minipages */
5527    if (kind == LyXParagraph::MARGIN 
5528       && cursor.par->pextra_type == LyXParagraph::PEXTRA_MINIPAGE) {
5529       WriteAlert(_("Impossible operation"), 
5530                  _("You can't insert a marginpar in a minipage!"), 
5531                  _("Sorry."));
5532       return;
5533    }
5534    
5535    /* this doesnt make sense, if there is no selection */ 
5536    bool dummy_selection = false;
5537    if (!selection) {
5538       sel_start_cursor = cursor;       /* dummy selection  */
5539       sel_end_cursor = cursor;
5540       dummy_selection = true;
5541    }
5542    
5543    LyXParagraph *tmppar;
5544
5545    if (sel_start_cursor.par->table || sel_end_cursor.par->table){
5546       WriteAlert(_("Impossible operation"), _("Cannot cut table."), _("Sorry."));
5547       return;
5548    }
5549      
5550    /* a test to make sure there is not already a footnote
5551     * in the selection. */
5552    
5553    tmppar = sel_start_cursor.par->ParFromPos(sel_start_cursor.pos);
5554    
5555    while (tmppar != sel_end_cursor.par->ParFromPos(sel_end_cursor.pos) && 
5556           tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
5557      tmppar = tmppar->next;
5558    
5559    if (tmppar != sel_end_cursor.par->ParFromPos(sel_end_cursor.pos)
5560        || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
5561       WriteAlert(_("Impossible operation"), 
5562                  _("Float would include float!"), 
5563                  _("Sorry."));
5564       return;
5565    }
5566    
5567    /* ok we have a selection. This is always between sel_start_cursor
5568     * and sel_end cursor */
5569
5570    SetUndo(Undo::FINISH, 
5571            sel_start_cursor.par->ParFromPos(sel_start_cursor.pos)->previous, 
5572            sel_end_cursor.par->ParFromPos(sel_end_cursor.pos)->next); 
5573    
5574    if (sel_end_cursor.pos > 0 
5575        && sel_end_cursor.par->IsLineSeparator(sel_end_cursor.pos - 1))
5576      sel_end_cursor.pos--;             /* please break before a space at
5577                                         * the end */
5578    if (sel_start_cursor.par == sel_end_cursor.par
5579        && sel_start_cursor.pos > sel_end_cursor.pos)
5580      sel_start_cursor.pos--;
5581
5582    sel_end_cursor.par->BreakParagraphConservative(sel_end_cursor.pos);
5583    
5584    sel_end_cursor.par = sel_end_cursor.par->Next();
5585    sel_end_cursor.pos = 0;
5586    
5587    // don't forget to insert a dummy layout paragraph if necessary
5588    if (sel_start_cursor.par->GetLayout() != sel_end_cursor.par->layout){
5589      sel_end_cursor.par->BreakParagraphConservative(0);
5590      sel_end_cursor.par->layout = LYX_DUMMY_LAYOUT;
5591      sel_end_cursor.par = sel_end_cursor.par->next;
5592    }
5593    else
5594      sel_end_cursor.par->layout = LYX_DUMMY_LAYOUT;
5595
5596    cursor = sel_end_cursor;
5597
5598    /* please break behind a space, if there is one. The space should
5599     * be erased too */ 
5600    if (sel_start_cursor.pos > 0 
5601        && sel_start_cursor.par->IsLineSeparator(sel_start_cursor.pos - 1))
5602      sel_start_cursor.pos--;
5603    if (sel_start_cursor.par->IsLineSeparator(sel_start_cursor.pos)) {
5604       sel_start_cursor.par->Erase(sel_start_cursor.pos);
5605    }
5606    
5607    sel_start_cursor.par->BreakParagraphConservative(sel_start_cursor.pos);
5608    tmppar = sel_start_cursor.par->Next();
5609    
5610    if (dummy_selection) {
5611            tmppar->Clear();
5612            if (kind == LyXParagraph::TAB
5613                || kind == LyXParagraph::FIG 
5614                || kind == LyXParagraph::WIDE_TAB
5615                || kind == LyXParagraph::WIDE_FIG 
5616                || kind == LyXParagraph::ALGORITHM) {
5617                    int lay = textclasslist.NumberOfLayout(parameters->textclass,
5618                                                      "Caption").second;
5619                    if (lay == -1) // layout not found
5620                            // use default layout "Standard" (0)
5621                            lay = 0;
5622                    tmppar->SetLayout(lay);
5623            }
5624    }
5625    else {
5626      if (sel_start_cursor.pos > 0) {
5627        /* the footnote-environment should begin with a standard layout.
5628         * Imagine you insert a footnote within an enumeration, you 
5629         * certainly do not want an enumerated footnote! */ 
5630        tmppar->Clear();
5631      }
5632      else {
5633        /* this is a exception the user would sometimes expect, I hope */
5634        sel_start_cursor.par->Clear();
5635      }
5636    }
5637    
5638    while (tmppar != sel_end_cursor.par) {
5639       tmppar->footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
5640       tmppar->footnotekind = kind;
5641       tmppar = tmppar->Next();
5642    } 
5643
5644    RedoParagraphs(sel_start_cursor, sel_end_cursor.par->Next());
5645    
5646    SetCursor(sel_start_cursor.par->Next(), 0);
5647
5648    ClearSelection();
5649 }
5650    
5651
5652 // returns pointer to a specified row
5653 Row * LyXText::GetRow(LyXParagraph * par,
5654                       LyXParagraph::size_type pos, long & y) const
5655 {
5656         Row * tmprow;
5657         
5658         if (currentrow) {
5659                 if (par == currentrow->par || par == currentrow->par->Previous()) {
5660                         // do not dereference par, it may have been deleted
5661                         // already! (Matthias) 
5662                         while (currentrow->previous && currentrow->previous->par != par) {
5663                                 currentrow = currentrow->previous;
5664                                 currentrow_y -= currentrow->height;
5665                         }
5666                         while (currentrow->previous && currentrow->previous->par == par) {
5667                                 currentrow = currentrow->previous;
5668                                 currentrow_y -= currentrow->height;
5669                         }
5670                 }
5671                 tmprow = currentrow;
5672                 y = currentrow_y;
5673                 // find the first row of the specified paragraph
5674                 while (tmprow->next && (tmprow->par != par)) {
5675                         y += tmprow->height;
5676                         tmprow = tmprow->next;
5677                 }
5678                 
5679                 if (tmprow->par == par){
5680                         // now find the wanted row
5681                         while (tmprow->pos < pos && tmprow->next && tmprow->next->par == par && 
5682                                tmprow->next->pos <= pos) {
5683                                 y += tmprow->height;
5684                                 tmprow = tmprow->next;
5685                         }
5686                         currentrow = tmprow;
5687                         currentrow_y = y;
5688                         return tmprow;
5689                 }
5690         }
5691         tmprow = firstrow;
5692         y = 0;
5693         // find the first row of the specified paragraph
5694         while (tmprow->next && (tmprow->par != par)) {
5695                 y += tmprow->height;
5696                 tmprow = tmprow->next;
5697         }
5698         
5699         // now find the wanted row
5700         while (tmprow->pos < pos && tmprow->next && tmprow->next->par == par && 
5701                tmprow->next->pos <= pos) {
5702                 y += tmprow->height;
5703                 tmprow = tmprow->next;
5704         }
5705         
5706         currentrow = tmprow;
5707         currentrow_y = y;
5708         
5709         return tmprow;
5710 }