]> git.lyx.org Git - lyx.git/blob - src/text.C
8fc195898429250b5e4d7e8b5570e7ba5614a97c
[lyx.git] / src / text.C
1 /* This file is part of
2 * ======================================================
3
4 *           LyX, The Document Processor
5 *        
6 *           Copyright (C) 1995 Matthias Ettrich
7 *           Copyright (C) 1995-1998 The LyX Team.
8 *
9 *======================================================*/
10
11 #include <config.h>
12 #include <stdlib.h>
13 #include <ctype.h>
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 "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
31 //      $Id: text.C,v 1.1 1999/09/27 18:44:38 larsbj Exp $      
32
33 #if !defined(lint) && !defined(WITH_WARNINGS)
34 static char vcid[] = "$Id: text.C,v 1.1 1999/09/27 18:44:38 larsbj Exp $";
35 #endif /* lint */
36
37
38 static const int LYX_PAPER_MARGIN = 20;
39
40 extern int mono_video;
41 extern int reverse_video;
42 extern int fast_selection;
43 extern BufferView *current_view;
44 extern int UnlockInset(UpdatableInset* inset);
45
46 // ale070405
47 extern int bibitemMaxWidth(const class LyXFont &);
48
49 // dnaber 1999-02-02
50 extern MiniBuffer *minibuffer;
51
52 int LyXText::SingleWidth(LyXParagraph *par, int pos)
53 {
54         char c = par->GetChar(pos);
55         return SingleWidth(par, pos, c);
56 }
57
58
59 int LyXText::SingleWidth(LyXParagraph *par, int pos, char c)
60 {
61         LyXFont font = GetFont(par, pos);
62
63         // The most common case is handled first (Asger)
64         if (IsPrintable(c)) {
65                 return font.width(c);
66
67         } else if (IsHfillChar(c)) {
68                 return 3;       /* because of the representation
69                                  * as vertical lines */
70
71         } else if (c == LYX_META_FOOTNOTE ||
72                    c == LYX_META_MARGIN ||
73                    c == LYX_META_FIG ||
74                    c == LYX_META_TAB ||
75                    c == LYX_META_WIDE_FIG ||
76                    c == LYX_META_WIDE_TAB ||
77                    c == LYX_META_ALGORITHM) 
78         {
79                 LString fs;
80                 switch (c) {
81                 case LYX_META_MARGIN:
82                         fs = "margin";
83                         break;
84                 case LYX_META_FIG:
85                         fs = "fig";
86                         break;
87                 case LYX_META_TAB:
88                         fs = "tab";
89                         break;
90                 case LYX_META_ALGORITHM:
91                         fs = "alg";
92                         break;
93                 case LYX_META_WIDE_FIG:
94                         fs = "wide-fig";
95                         break;
96                 case LYX_META_WIDE_TAB:
97                         fs = "wide-tab";
98                         break;
99                 case LYX_META_FOOTNOTE:
100                         fs = "foot";
101                         break;
102                 }
103                 font.decSize();
104                 font.decSize();
105                 return font.stringWidth(fs);
106         } 
107    
108         else if (c == LYX_META_INSET) {
109                 Inset *tmpinset=par->GetInset(pos);
110                 if (tmpinset)
111                         return par->GetInset(pos)->Width(font);
112                 else
113                         return 0;
114
115         } else if (IsSeparatorChar(c))
116                 c = ' ';
117         else if (IsNewlineChar(c))
118                 c = 'n';
119         return font.width(c);
120 }
121
122
123 /* returns the paragraph position of the last character in the 
124  * specified row */
125 int LyXText::RowLast(Row *row)
126 {
127         if (row->next == NULL)
128                 return row->par->Last()-1;
129         else if (row->next->par != row->par) 
130                 return row->par->Last()-1;
131         else 
132                 return row->next->pos - 1;
133 }
134
135
136 void LyXText::Draw(Row *row, int &pos, LyXScreen &scr, int offset, float &x)
137 {
138         char c = row->par->GetChar(pos);
139
140         if (IsNewlineChar(c)) {
141                 pos++;
142                 // Draw end-of-line marker
143
144                 LyXFont font = GetFont(row->par, pos);
145                 int asc = font.maxAscent();
146                 int wid = font.width('n');
147                 int y = (offset + row->baseline);
148                 XPoint p[3];
149                 p[0].x = int(x + wid*0.375); p[0].y = int(y - 0.875*asc*0.75);
150                 p[1].x = int(x);             p[1].y = int(y - 0.500*asc*0.75);
151                 p[2].x = int(x + wid*0.375); p[2].y = int(y - 0.125*asc*0.75);
152                 scr.drawLines(::getGC(gc_new_line),p, 3);
153                 
154                 p[0].x = int(x);             p[0].y = int(y - 0.500*asc*0.75);
155                 p[1].x = int(x + wid);       p[1].y = int(y - 0.500*asc*0.75);
156                 p[2].x = int(x + wid);       p[2].y = int(y - asc*0.75);
157                 scr.drawLines(::getGC(gc_new_line),p, 3);
158                 return;
159         }
160
161         LyXFont font = GetFont(row->par, pos);
162         LyXFont font2 = font;
163
164         if (c == LYX_META_FOOTNOTE ||
165             c == LYX_META_MARGIN ||
166             c == LYX_META_FIG ||
167             c == LYX_META_TAB ||
168             c == LYX_META_WIDE_FIG ||
169             c == LYX_META_WIDE_TAB ||
170             c == LYX_META_ALGORITHM) {
171                 LString fs;
172                 switch (c) {
173                 case LYX_META_MARGIN:
174                         fs = "margin";
175                         /* draw a sign at the left margin! */ 
176                         scr.drawText(font, "!", 1, offset + row->baseline,
177                                      (LYX_PAPER_MARGIN - font.width('!'))/2);
178                         break;
179                 case LYX_META_FIG:
180                         fs = "fig";
181                         break;
182                 case LYX_META_TAB:
183                         fs = "tab";
184                         break;
185                 case LYX_META_ALGORITHM:
186                         fs = "alg";
187                         break;
188                 case LYX_META_WIDE_FIG:
189                         fs = "wide-fig";
190                         break;
191                 case LYX_META_WIDE_TAB:
192                         fs = "wide-tab";
193                         break;
194                 case LYX_META_FOOTNOTE:
195                         fs = "foot";
196                         break;
197                 }
198                 font.decSize();
199                 font.decSize();
200           
201                 /* calculate the position of the footnotemark */
202                 int y = (row->baseline - font2.maxAscent() 
203                          + font.maxAscent());
204           
205                 font.setColor(LyXFont::INSET);
206
207                 float tmpx = x;
208
209                 /* draw it and set new x position */
210                 x += scr.drawString(font, fs, offset + y, int(x));
211
212                 scr.drawLine(gc_foot, offset + row->baseline,
213                              int(tmpx), int(x - tmpx));
214           
215                 pos++;
216                 return;
217         } else if (c == LYX_META_INSET) {
218                 Inset *tmpinset = row->par->GetInset(pos);
219                 if (tmpinset) 
220                         tmpinset->Draw(font, scr, offset + row->baseline, x);
221                 pos++;
222                 return;
223         }
224
225         /* usual characters, no insets */
226
227         // Collect character that we can draw in one command
228
229         // This is dirty, but fast. Notice that it will never be too small.
230         // For the record, I'll note that Microsoft Word has a limit
231         // of 768 here. We have none :-) (Asger)
232         static char textstring[1024];
233
234         int last = RowLast(row);
235         // Prevent crash in the extremely unlikely event
236         // that our array is too small
237         if (last > pos+1020) last = pos + 1020;
238
239         textstring[0] = c;
240         pos++;
241
242         int i = 1;
243
244         while (pos <= last &&
245                (unsigned char) (c = row->par->GetChar(pos)) > ' ' &&
246                font2 == GetFont(row->par, pos)) {
247                 textstring[i++] = c;
248                 pos++;
249         }
250         textstring[i] = 0; 
251
252         float tmpx = x;
253
254         // If monochrome and LaTeX mode, provide reverse background
255         if (mono_video && font.latex() == LyXFont::ON) {
256                 int a=font.maxAscent(), d=font.maxDescent();
257                 scr.fillRectangle(gc_copy, int(tmpx), offset + row->baseline-a,
258                                   font.textWidth(textstring, i), a+d);
259         }
260
261         /* Draw text and set the new x position */ 
262         x += scr.drawText(font, textstring, i, offset + row->baseline, 
263                           int(x));
264         /* what about underbars? */
265         if (font.underbar() == LyXFont::ON && font.latex() != LyXFont::ON) {
266                 scr.drawLine(gc_copy, offset + row->baseline + 2,
267                              int(tmpx), int(x - tmpx));
268         }
269
270         // If we want ulem.sty support, drawing
271         // routines should go here. (Asger)
272         // Why shouldn't LyXFont::drawText handle it internally?
273 }
274
275
276 /* Returns the left beginning of the text. 
277 * This information cannot be taken from the layouts-objekt, because in 
278 * LaTeX the beginning of the text fits in some cases (for example sections)
279 * exactly the label-width. */
280 int LyXText::LeftMargin(Row* row)
281 {
282    int x;
283    LyXLayout *layout;
284    LyXFont labelfont;
285    LyXParagraph *newpar;
286    Row dummyrow;
287    layout = lyxstyle.Style(parameters->textclass, row->par->GetLayout());
288    
289    LString parindent = layout->parindent; 
290
291    /* table stuff -- begin*/ 
292    if (row->par->table)
293       parindent.clean();
294    /* table stuff -- end*/       
295
296    x = LYX_PAPER_MARGIN;
297
298    x += lyxstyle.TextClass(parameters->textclass)->
299      defaultfont.signedStringWidth(lyxstyle.TextClass(parameters->textclass)->leftmargin);
300
301    if (row->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)  {
302         LyXFont font(LyXFont::ALL_SANE);
303         font.setSize(LyXFont::SIZE_SMALL);
304         x += font.textWidth("Mwide-figM", 10) + LYX_PAPER_MARGIN/2;
305    }
306
307    /* this is the way, LyX handles the LaTeX-Environments.
308     * I have had this idea very late, so it seems to be a
309     * later added hack and this is true */ 
310    if (!row->par->GetDepth()) {
311       if (!row->par->GetLayout()) {
312          /* find the previous same level paragraph */
313          if (row->par->FirstPhysicalPar()->Previous()) {
314             newpar = row->par->DepthHook(row->par->GetDepth());
315             if (newpar && lyxstyle.Style(parameters->textclass, newpar->GetLayout())->nextnoindent)
316                parindent.clean();
317          }
318       }
319    }
320    else {
321       /* find the next level paragraph */ 
322       
323       newpar = row->par->DepthHook(row->par->GetDepth()-1);
324
325       /* make a corresponding row. Needed to call LeftMargin() */
326       
327       /* check wether it is a sufficent paragraph */ 
328       if (newpar && newpar->footnoteflag == row->par->footnoteflag
329           && lyxstyle.Style(parameters->textclass, 
330                             newpar->GetLayout())->isEnvironment()) {
331              dummyrow.par = newpar;
332              dummyrow.pos = newpar->Last();
333              x = LeftMargin(&dummyrow);
334       }
335       else {
336          /* this is no longer an error, because this function is used
337           * to clear impossible depths after changing a layout. Since there
338           * is always a redo, LeftMargin() is always called */ 
339          
340          /* printf("LYX_ERROR (text, LeftMargin()) impossible depth \n");*/ 
341          row->par->FirstPhysicalPar()->depth = 0;
342       }
343       
344       if (newpar && !row->par->GetLayout()) {
345          if (newpar->FirstPhysicalPar()->noindent)
346             parindent.clean();
347          else
348             parindent = lyxstyle.Style(parameters->textclass, 
349                                        newpar->GetLayout())->parindent;
350       }
351       
352    }
353    
354    labelfont = GetFont(row->par, -2);
355    switch (layout->margintype) {
356     case MARGIN_DYNAMIC:
357       if (!layout->leftmargin.empty()) {
358         x += lyxstyle.TextClass(parameters->textclass)->defaultfont.signedStringWidth(layout->leftmargin);
359       }
360       if (!row->par->GetLabelString().empty()) {
361             x += labelfont.signedStringWidth(layout->labelindent);
362             x += labelfont.stringWidth(row->par->GetLabelString());
363             x += labelfont.stringWidth(layout->labelsep);
364       }
365       break;
366     case MARGIN_MANUAL:
367       x += labelfont.signedStringWidth(layout->labelindent);
368       if (row->pos >= BeginningOfMainBody(row->par)) {
369          if (!row->par->GetLabelWidthString().empty()) {
370             x += labelfont.stringWidth(row->par->GetLabelWidthString());
371             x += labelfont.stringWidth(layout->labelsep);
372          }
373       }
374       break;
375     case MARGIN_STATIC:
376       x += ( lyxstyle.TextClass(parameters->textclass)->defaultfont.signedStringWidth(layout->leftmargin) * 4
377              / (row->par->GetDepth() + 4));
378       break;
379     case MARGIN_FIRST_DYNAMIC:
380       if (layout->labeltype == LABEL_MANUAL) {
381          if (row->pos >= BeginningOfMainBody(row->par)) {
382             x += labelfont.signedStringWidth(layout->leftmargin);
383          } else {
384             x += labelfont.signedStringWidth(layout->labelindent);
385          }
386       } else {
387          if (row->pos
388              // Special case to fix problems with theorems (JMarc)
389              || (layout->labeltype == LABEL_STATIC
390                  && layout->latextype == LATEX_ENVIRONMENT
391                  && ! row->par->IsFirstInSequence())) {
392                  x += labelfont.signedStringWidth(layout->leftmargin);
393          } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
394                   && layout->labeltype != LABEL_BIBLIO
395                   && layout->labeltype != LABEL_CENTERED_TOP_ENVIRONMENT) {
396                  x += labelfont.signedStringWidth(layout->labelindent);
397                  x += labelfont.stringWidth(layout->labelsep);
398                  x += labelfont.stringWidth(row->par->GetLabelString());
399          } 
400       }
401       break;
402       
403     case MARGIN_RIGHT_ADDRESS_BOX:
404     {
405       /* ok, a terrible hack. The left margin depends on the widest row
406        * in this paragraph. Do not care about footnotes, they are *NOT*
407        * allowed in the LaTeX realisation of this layout. */ 
408       
409       /* find the first row of this paragraph */ 
410       Row *tmprow = row;
411       while (tmprow->previous && tmprow->previous->par == row->par)
412         tmprow = tmprow->previous;
413       
414       int minfill = tmprow->fill;
415       while (tmprow-> next && tmprow->next->par == row->par) {
416          tmprow = tmprow->next;
417          if (tmprow->fill < minfill)
418            minfill = tmprow->fill;
419       }
420       
421       x += lyxstyle.TextClass(parameters->textclass)->defaultfont.signedStringWidth(layout->leftmargin);
422       x += minfill;
423     }
424       break;
425    }
426    if (row->par->pextra_type == PEXTRA_INDENT) {
427        if (!row->par->pextra_widthp.empty()) {
428            x += paperwidth * atoi(row->par->pextra_widthp.c_str()) / 100;
429        } else if (!row->par->pextra_width.empty()) {
430            int xx = VSpace(row->par->pextra_width).inPixels();
431
432            if (xx > paperwidth)
433                xx = paperwidth * 80 / 100;
434            x += xx;
435        } else { // should not happen
436            LyXFont font(LyXFont::ALL_SANE);
437            x += font.stringWidth("XXXXXX");
438        }
439    }
440    
441    int align;
442    if (row->par->FirstPhysicalPar()->align == LYX_ALIGN_LAYOUT)
443      align = layout->align;
444    else
445      align = row->par->FirstPhysicalPar()->align;
446    
447    /* set the correct parindent */
448    if (row->pos == 0) {
449        if ((layout->labeltype == LABEL_NO_LABEL 
450             || layout->labeltype == LABEL_TOP_ENVIRONMENT 
451             || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
452             || (layout->labeltype == LABEL_STATIC
453                 && layout->latextype == LATEX_ENVIRONMENT
454                 && ! row->par->IsFirstInSequence()))
455            && row->par == row->par->FirstPhysicalPar()
456            && align == LYX_ALIGN_BLOCK
457            && !row->par->noindent
458            && (row->par->layout ||
459                parameters->paragraph_separation == LYX_PARSEP_INDENT))
460          x += lyxstyle.TextClass(parameters->textclass)->defaultfont.stringWidth(parindent);
461        else 
462          if (layout->labeltype==LABEL_BIBLIO) { // ale970405 Right width for bibitems
463              x += bibitemMaxWidth(lyxstyle.TextClass(parameters->textclass)->defaultfont);
464          }
465    }
466
467    return x;
468 }
469     
470    
471 int LyXText::RightMargin(Row *row)
472 {
473    int  x;
474    LyXLayout* layout;
475    
476    LyXParagraph *newpar;
477    Row dummyrow;
478    layout = lyxstyle.Style(parameters->textclass, row->par->GetLayout());
479
480    x = LYX_PAPER_MARGIN;
481
482    x += lyxstyle.TextClass(parameters->textclass)->
483      defaultfont.signedStringWidth(lyxstyle.TextClass(parameters->textclass)->rightmargin);
484    if (row->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)  {
485      x += LYX_PAPER_MARGIN/2;
486    }
487
488     /* this is the way, LyX handles the LaTeX-Environments.
489     * I have had this idea very late, so it seems to be a
490     * later added hack and this is true */ 
491    if (row->par->GetDepth()) {
492       /* find the next level paragraph */ 
493       
494       newpar = row->par;
495       
496       do {
497                 newpar = newpar->FirstPhysicalPar()->Previous();
498                 if (newpar) 
499                         newpar = newpar->FirstPhysicalPar();
500       } while (newpar && newpar->GetDepth() >= row->par->GetDepth()
501                && newpar->footnoteflag == row->par->footnoteflag);
502       
503       /* make a corresponding row. Needed to call LeftMargin() */
504
505       /* check wether it is a sufficent paragraph */ 
506       if (newpar && newpar->footnoteflag == row->par->footnoteflag
507           && lyxstyle.Style(parameters->textclass,
508                             newpar->GetLayout())->isEnvironment()) {
509              dummyrow.par = newpar;
510              dummyrow.pos = 0;
511              x = RightMargin(&dummyrow);
512           }
513       else {
514          /* this is no longer an error, because this function is used
515           * to clear impossible depths after changing a layout. Since there
516           * is always a redo, LeftMargin() is always called */ 
517          
518          /* printf("LYX_ERROR (text, LeftMargin()) impossible depth \n");*/ 
519          row->par->FirstPhysicalPar()->depth = 0;
520       }
521    }
522
523    //fprintf(stderr,"rightmargin: %s\n", layout->rightmargin.c_str());
524    //fflush(stderr);
525    x += (lyxstyle.TextClass(parameters->textclass)->defaultfont.signedStringWidth(layout->rightmargin) * 4
526          / (row->par->GetDepth() + 4));
527    return x;
528    
529 }
530
531
532 int LyXText::LabelEnd (Row *row)
533 {
534    if (lyxstyle.Style(parameters->textclass, row->par->GetLayout())->margintype == MARGIN_MANUAL) {
535       Row tmprow;
536       tmprow = *row;
537       tmprow.pos = row->par->Last();
538       return LeftMargin(&tmprow);      /* just the beginning 
539                                         * of the main body */
540    }
541    else
542      return 0;                         /* LabelEnd is only needed, if the  
543                                         * layout fills a flushleft
544                                         * label. */
545 }
546
547
548 /* table stuff -- begin*/ 
549 int LyXText::NumberOfCell(LyXParagraph *par, int pos)
550 {
551    int cell = 0;
552    int tmp_pos = 0;
553    while (tmp_pos < pos) {
554       if (par->IsNewline(tmp_pos))
555          cell++;
556       tmp_pos++;
557    }
558    return cell;
559 }
560
561
562 int LyXText::WidthOfCell(LyXParagraph *par, int &pos)
563 {
564    int w = 0;
565    while (pos < par->Last() && !par->IsNewline(pos)) {
566       w += SingleWidth(par, pos);
567       pos++;
568    }
569    if (par->IsNewline(pos))
570       pos++;
571    return w;
572 }
573
574
575 char LyXText::HitInTable(Row* row, int x)
576 {
577   float tmpx;
578   float fill_separator, fill_hfill, fill_label_hfill;
579   if (!row->par->table)
580     return 0;
581   PrepareToPrint(row, tmpx, fill_separator, fill_hfill, fill_label_hfill);
582   return (x > tmpx && x < tmpx + row->par->table->WidthOfTable());
583 }
584
585
586 bool LyXText::MouseHitInTable(int x, long y)
587 {
588         Row *row = GetRowNearY(y);
589         return HitInTable(row, x);
590 }
591
592
593 /* table stuff -- end*/
594
595
596 /* get the next breakpoint in a given paragraph */
597 int LyXText::NextBreakPoint(Row* row, int width)
598 {
599         int x = 0;
600         int last_separator = -1; /* position of the last possible breakpoint 
601                                   * -1 isn't a suitable value, but a flag */
602         int i = 0;
603         int left_margin;
604         
605         LyXParagraph *par = row->par;
606         int pos = row->pos;
607         
608         /* table stuff -- begin*/ 
609         if (par->table) {
610                 while (pos<par->last 
611                        && (!par->IsNewline(pos) 
612                            || !par->table->IsFirstCell(NumberOfCell(par, pos+1)))) {
613                         if (par->GetChar(pos) == LYX_META_INSET &&
614                             par->GetInset(pos) && par->GetInset(pos)->Display()){
615                                 par->GetInset(pos)->SetDisplay(false);
616                         }
617                         pos++;
618                 }
619                 return pos;
620         }
621         /* table stuff -- end*/ 
622         
623         left_margin = LabelEnd(row);
624         width -= RightMargin(row);
625         int main_body = BeginningOfMainBody(par);
626         LyXLayout* layout = lyxstyle.Style(parameters->textclass, par->GetLayout());
627         i = pos;
628         char c;
629         
630         if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
631                 /* special code for right address boxes, only newlines count */
632                 while (i < par->Last()) {
633                         if (par->IsNewline(i)) {
634                                 last_separator = i;
635                                 i = par->Last() - 1;/* this means break  */
636                                 x = width;
637                         } else if (par->GetChar(i) == LYX_META_INSET &&
638                                    par->GetInset(i) && par->GetInset(i)->Display()){
639                                 par->GetInset(i)->SetDisplay(false);
640                         }
641                         i++;
642                 }
643         } else {
644                 // Last position is an invariant
645                 int const last = par->Last();
646                 /* this is the usual handling */ 
647                 x = LeftMargin(row);
648                 while (x < width && i < last) {
649                         c = par->GetChar(i);
650                         if (IsNewlineChar(c)) {
651                                 last_separator = i;
652                                 x = width;                     /* this means break  */
653                         } else if (c == LYX_META_INSET &&
654                                    par->GetInset(i) && par->GetInset(i)->Display()){
655                                 /* check wether a Display() inset is valid here .
656                                    if not, change it to non-display*/ 
657                                 if (layout->isCommand()
658                                     || (layout->labeltype == LABEL_MANUAL
659                                         && i < BeginningOfMainBody(par))){
660                                         /* display istn't allowd */ 
661                                         par->GetInset(i)->SetDisplay(false);
662                                         x += SingleWidth(par, i, c);
663                                 } else {
664                                         /* inset is display. So break the line here */ 
665                                         if (i==pos){
666                                                 if (pos < last-1) {
667                                                         last_separator = i;
668                                                         if (IsLineSeparatorChar(par->GetChar(i+1)))
669                                                                 last_separator++;
670                                                 } else
671                                                         last_separator = last; // to avoid extra rows
672                                         } else
673                                                 last_separator = i - 1;
674                                         x = width;                     /* this means break  */
675                                 }
676                         } else  {
677                                 if (IsLineSeparatorChar(c))
678                                         last_separator = i;
679                                 x += SingleWidth(par, i, c);
680                         }
681                         i++;
682                         if (i == main_body) {
683                                 x += GetFont(par, -2).stringWidth(layout->labelsep);
684                                 if (par->IsLineSeparator(i - 1))
685                                         x-= SingleWidth(par, i - 1);
686                                 if (x < left_margin)
687                                         x = left_margin;
688                         }
689                 }
690                 /* end of paragraph is always a suitable separator */
691                 if (i == last && x < width)
692                         last_separator = i;
693         }
694         
695         /* well, if last_separator is still 0, the line isn't breakable. 
696          * don't care and cut simply at the end */
697         if (last_separator < 0) {
698                 last_separator = i;
699         }
700         
701         /* manual labels cannot be broken in LaTeX, do not care  */
702         if (main_body && last_separator < main_body)
703                 last_separator = main_body - 1;
704         
705         return last_separator;
706 }
707
708
709 /* returns the minimum space a row needs on the screen in pixel */
710 int LyXText::Fill(Row *row, int paperwidth)
711 {
712    int w,i, last, fill, left_margin;
713    /* get the pure distance */
714    last = RowLast(row);
715    
716    /* table stuff -- begin*/
717    if (row->par->table) {
718       /* for tables FILL does calculate the widthes of each cell in 
719        * the row */ 
720       int pos = row->pos;
721       int cell = NumberOfCell(row->par, pos);
722       w = 0;
723       do {
724          row->par->table->SetWidthOfCell(cell, WidthOfCell(row->par, pos));
725          cell++;
726       } while (pos <= last && !row->par->table->IsFirstCell(cell));
727       /* don't forget the very last table cell without characters */
728       if (cell == row->par->table->GetNumberOfCells()-1)
729           row->par->table->SetWidthOfCell(cell, WidthOfCell(row->par, pos));
730       
731       return 0; /* width of table cannot be returned since
732                  * we cannot guarantee its correct value at
733                  * this point. */ 
734    }
735    /* table stuff -- end*/ 
736
737    left_margin = LabelEnd(row);
738
739      /* if the row ends with newline, this newline will not be relevant */ 
740      if (last >= 0 && row->par->IsNewline(last))
741        last--;
742      
743      /* if the row ends with a space, this space will not be relevant */ 
744      if (last >= 0 && row->par->IsLineSeparator(last))
745        last--;
746
747    /* special handling of the right address boxes */ 
748    if (lyxstyle.Style(parameters->textclass, row->par->GetLayout())->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
749       int tmpfill = row->fill;
750       row->fill = 0;                   /* the minfill in MarginLeft()  */
751       w = LeftMargin(row);
752       row->fill = tmpfill;
753       
754       /* the old way, impossible when using environments: */ 
755       /*  w = LyXStringWidth(lyxstyle.Style(parameters->textclass, row->par->GetLayout())->font, */ 
756       /*                         lyxstyle.Style(parameters->textclass, row->par->GetLayout())->leftmargin); */
757    }
758    else
759      w = LeftMargin(row);
760    
761    int main_body = BeginningOfMainBody(row->par);
762    LyXLayout *layout = lyxstyle.Style(parameters->textclass,
763                                       row->par->GetLayout());
764    i = row->pos;
765    while (i<= last) {
766       w += SingleWidth(row->par, i);
767       i++;
768       if (i == main_body) {
769          w += GetFont(row->par, -2).stringWidth(layout->labelsep);
770          if (row->par->IsLineSeparator(i - 1))
771            w-= SingleWidth(row->par, i - 1);
772          if (w < left_margin)
773            w = left_margin;
774       }
775    }
776    
777    fill = paperwidth - w - RightMargin(row);
778    return fill;
779 }
780
781
782 /* returns the minimum space a manual label needs on the screen in pixel */ 
783 int LyXText::LabelFill(Row *row)
784 {
785    int w,i, last;
786    int fill=0;
787    
788    last = BeginningOfMainBody(row->par) - 1;
789    /* -1 because a label ends either with a space that is in the label, 
790     * or with the beginning of a footnote that is outside the label. */ 
791
792    // I don't understand this code in depth, but sometimes "last" is less than
793    // 0 and this causes a crash. This fix seems to work correctly, but I
794    // bet the real error is elsewhere.  The bug is triggered when you have an 
795    // open footnote in a paragraph environment with a manual label. (Asger)
796    if (last<0) last = 0;
797
798    if (row->par->IsLineSeparator(last))    /* a sepearator at this end 
799                                         * does not count */
800      last--;
801    
802    w = 0;
803    i = row->pos;
804    while (i<= last) {
805       w += SingleWidth(row->par, i);
806       i++;
807    }
808    
809    if (!row->par->labelwidthstring.empty()) {
810       fill = GetFont(row->par, -2).stringWidth(row->par->labelwidthstring) - w;
811    }
812    
813    if (fill < 0)
814      fill = 0;
815    
816    return fill;
817 }
818
819
820 /* returns the number of separators in the specified row. The separator 
821 * on the very last column doesnt count */ 
822 int LyXText::NumberOfSeparators(Row *row)
823 {
824    int n,p,last;
825    
826    last = RowLast(row);
827    n = 0;
828    p = row->pos;
829    int main_body = BeginningOfMainBody(row->par);
830    if (p < main_body)
831      p = main_body;
832    for (; p < last; p++) {
833       if (row->par->IsSeparator(p)) {
834          n++;
835       }
836    }
837    return n;
838 }
839
840
841 /* returns the number of hfills in the specified row. The LyX-Hfill is
842 * a LaTeX \hfill so that the hfills at the beginning and at the end were 
843 * ignored. This is *MUCH* more usefull than not to ignore!  */
844 int LyXText::NumberOfHfills(Row *row)
845 {
846    int n,p,last, first;
847    
848    last = RowLast(row);
849    first = row->pos;
850    if (first) {                        /* hfill *DO* count at the beginning 
851                                         * of paragraphs! */
852      while(first <= last && row->par->IsHfill(first))
853        first++;
854    }
855
856    n = 0;
857    int main_body = BeginningOfMainBody(row->par);
858    if (first < main_body)
859      first = main_body;
860    for (p = first; p <= last; p++) {    /* last, because the end is ignored!  */
861       if (row->par->IsHfill(p)) {
862          n++;
863       }
864    }
865    return n;
866 }
867
868
869 /* like NumberOfHfills, but only those in the manual label! */ 
870 int LyXText::NumberOfLabelHfills(Row *row)
871 {
872    int n,p,last, first;
873    
874    last = RowLast(row);
875    first = row->pos;
876    if (first) {                        /* hfill *DO* count at the beginning 
877                                         * of paragraphs! */
878       while(first < last && row->par->IsHfill(first))
879         first++;
880    }
881
882    n = 0;
883    int main_body = BeginningOfMainBody(row->par);
884    
885    if (last > main_body)
886      last = main_body;
887    
888    for (p = first; p < last; p++) {    /* last, because the end is ignored!  */
889       if (row->par->IsHfill(p)) {
890          n++;
891       }
892    }
893    return n;
894 }
895
896
897 /* returns true, if a expansion is needed.
898  * Rules are given by LaTeX */ 
899 bool LyXText::HfillExpansion(Row *row_ptr, int pos)
900 {
901    /* by the way, is it a hfill? */ 
902    if (!row_ptr->par->IsHfill(pos))
903      return false;
904    
905    /* at the end of a row it does not count */ 
906    if (pos >= RowLast(row_ptr))
907      return false;
908    
909    /* at the beginning of a row it does not count, if it is not 
910     * the first row of a paragaph */ 
911    if (!row_ptr->pos)
912      return true;
913    
914    /* in some labels  it does not count */ 
915    if ( lyxstyle.Style(parameters->textclass, row_ptr->par->GetLayout())->margintype != MARGIN_MANUAL &&
916        pos < BeginningOfMainBody(row_ptr->par))
917      return false; 
918    
919    /* if there is anything between the first char of the row and
920     * the sepcified position that is not a newline and not a hfill,
921     * the hfill will count, otherwise not */ 
922    
923    int i = row_ptr->pos;
924    while (i < pos && (row_ptr->par->IsNewline(i)
925                       || row_ptr->par->IsHfill(i)))
926      i++;
927    
928    return (i != pos);
929 }
930
931
932 void LyXText::SetHeightOfRow(Row *row_ptr)
933 {
934     /* get the maximum ascent and the maximum descent */
935    int asc, maxasc, desc, maxdesc, pos_end, pos, labeladdon;
936    float layoutasc = 0;
937    float layoutdesc = 0;
938    float tmptop = 0;
939    LyXParagraph *par, *firstpar;
940    LyXFont tmpfont;
941    Inset *tmpinset;
942
943    /* this must not happen before the currentrow for clear reasons.
944       so the trick is just to set the current row onto this row */
945    long unused_y;
946    GetRow(row_ptr->par, row_ptr->pos, unused_y);
947
948    /* ok , let us initialize the maxasc and maxdesc value. 
949     * This depends in LaTeX of the font of the last character
950     * in the paragraph. The hack below is necessary because
951     * of the possibility of open footnotes */
952
953    /* Correction: only the fontsize count. The other properties
954       are taken from the layoutfont. Nicer on the screen :) */
955    
956    par = row_ptr->par->LastPhysicalPar();
957    firstpar = row_ptr->par->FirstPhysicalPar();
958    
959    LyXLayout *layout = lyxstyle.Style(parameters->textclass, firstpar->GetLayout());
960    
961    LyXFont font = GetFont(par, par->Last()-1);
962    LyXFont::FONT_SIZE size = font.size();
963    font = GetFont(par, -1);
964    font.setSize(size);
965
966    LyXFont labelfont = GetFont(par, -2);
967
968    maxasc = int(font.maxAscent() *
969                    layout->spacing.getValue() *
970                    parameters->spacing.getValue());
971    maxdesc = int(font.maxDescent() *
972                     layout->spacing.getValue() *
973                     parameters->spacing.getValue());
974
975    pos_end = RowLast(row_ptr);
976    
977    labeladdon = 0;
978
979    // Check if any insets are larger
980    for (pos = row_ptr->pos; pos <= pos_end; pos++) {
981       if (row_ptr->par->GetChar(pos) == LYX_META_INSET) {
982          tmpfont = GetFont(row_ptr->par, pos);
983          tmpinset = row_ptr->par->GetInset(pos);
984          if (tmpinset) {
985             asc = tmpinset->Ascent(tmpfont);
986             desc = tmpinset->Descent(tmpfont);
987             if (asc > maxasc) 
988               maxasc = asc;
989             if (desc > maxdesc)
990               maxdesc = desc;
991          }
992       }
993    }
994
995    // Check if any custom fonts are larger (Asger)
996    // This is not completely correct, but we can live with the small,
997    // cosmetic error for now.
998    LyXFont::FONT_SIZE maxsize = row_ptr->par->HighestFontInRange(row_ptr->pos, pos_end);
999    if (maxsize > font.size()) {
1000         font.setSize(maxsize);
1001
1002         asc = font.maxAscent();
1003         desc = font.maxDescent();
1004         if (asc > maxasc) 
1005                 maxasc = asc;
1006         if (desc > maxdesc)
1007                 maxdesc = desc;
1008    }
1009
1010    /* table stuff -- begin*/
1011    if (row_ptr->par->table){
1012      // stretch the rows a bit
1013       maxasc += 1;
1014       maxdesc +=1;
1015    }
1016    /* table stuff -- end*/
1017
1018    // This is nicer with box insets:
1019    maxasc++;
1020    maxdesc++;
1021
1022    row_ptr->ascent_of_text = maxasc;
1023    
1024    /* is it a top line? */ 
1025    if (row_ptr->pos == 0
1026        && row_ptr->par == firstpar) {
1027       
1028       /* some parksips VERY EASY IMPLEMENTATION */ 
1029       if (parameters->paragraph_separation == LYX_PARSEP_SKIP) {
1030          if (layout->isParagraph()
1031              && firstpar->GetDepth() == 0
1032              && firstpar->Previous())
1033             maxasc += parameters->getDefSkip().inPixels();
1034          else if (firstpar->Previous()
1035                   && lyxstyle.Style(parameters->textclass,
1036                            firstpar->Previous()->GetLayout())->isParagraph()
1037                   && firstpar->Previous()->GetDepth() == 0)
1038            // is it right to use defskip here too? (AS)
1039            maxasc += parameters->getDefSkip().inPixels();
1040       }
1041       
1042       /* the paper margins */ 
1043       if (!row_ptr->par->previous)
1044          maxasc += LYX_PAPER_MARGIN;
1045       
1046       /* add the vertical spaces, that the user added */
1047       if (firstpar->added_space_top.kind() != VSpace::NONE)
1048          maxasc += int(firstpar->added_space_top.inPixels());
1049       
1050       /* do not forget the DTP-lines! 
1051        * there height depends on the font of the nearest character */
1052       if (firstpar->line_top)
1053          maxasc += 2 * GetFont(firstpar, 0).ascent('x');
1054       
1055       /* and now the pagebreaks */ 
1056       if (firstpar->pagebreak_top)
1057          maxasc += 3 * DefaultHeight();
1058       
1059       /*  this is special code for the chapter, since the label of this
1060        * layout is printed in an extra row */ 
1061       if (layout->labeltype == LABEL_COUNTER_CHAPTER
1062           && parameters->secnumdepth>=0) {
1063               labeladdon = int(labelfont.maxDescent() *
1064                                   layout->spacing.getValue() *
1065                                   parameters->spacing.getValue())
1066                       + int(labelfont.maxAscent() *
1067                                layout->spacing.getValue() *
1068                                parameters->spacing.getValue());
1069       }
1070       
1071       /* special code for the top label */ 
1072       if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1073            || layout->labeltype == LABEL_BIBLIO
1074            || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1075           && row_ptr->par->IsFirstInSequence()
1076           && !row_ptr->par->GetLabelString().empty()) {
1077          labeladdon = int(
1078                  (labelfont.maxAscent() *
1079                   layout->spacing.getValue() *
1080                   parameters->spacing.getValue())
1081                  +(labelfont.maxDescent() *
1082                    layout->spacing.getValue() *
1083                    parameters->spacing.getValue())
1084                  + layout->topsep * DefaultHeight()
1085                  + layout->labelbottomsep *  DefaultHeight());
1086       }
1087    
1088       /* and now the layout spaces, for example before and after a section, 
1089        * or between the items of a itemize or enumerate environment */ 
1090       
1091       if (!firstpar->pagebreak_top) {
1092          LyXParagraph *prev = row_ptr->par->Previous();
1093          if (prev)
1094             prev = row_ptr->par->DepthHook(row_ptr->par->GetDepth());
1095          if (prev && prev->GetLayout() == firstpar->GetLayout()
1096              && prev->GetDepth() == firstpar->GetDepth()
1097              && prev->GetLabelWidthString() == firstpar->GetLabelWidthString())
1098            {
1099               layoutasc = (layout->itemsep * DefaultHeight());
1100            }
1101          else if (row_ptr->previous) {
1102             tmptop = layout->topsep;
1103             
1104             if (row_ptr->previous->par->GetDepth() >= row_ptr->par->GetDepth())
1105                tmptop-=lyxstyle.Style(parameters->textclass, row_ptr->previous->par->GetLayout())->bottomsep;
1106             
1107             if (tmptop > 0)
1108                layoutasc = (tmptop * DefaultHeight());
1109          }
1110          else if (row_ptr->par->line_top){
1111             tmptop = layout->topsep;
1112             
1113             if (tmptop > 0)
1114                layoutasc = (tmptop * DefaultHeight());
1115          }
1116          
1117          prev = row_ptr->par->DepthHook(row_ptr->par->GetDepth()-1);
1118          if (prev)  {
1119             maxasc += int(lyxstyle.Style(parameters->textclass,
1120                                          prev->GetLayout())->parsep * DefaultHeight());
1121          }
1122          else {
1123                 if (firstpar->Previous()
1124                     && firstpar->Previous()->GetDepth() == 0
1125                     && firstpar->Previous()->GetLayout() != firstpar->GetLayout()) {
1126                    /* avoid parsep */ 
1127                 }
1128             else if (firstpar->Previous()){
1129                maxasc += int(layout->parsep * DefaultHeight());
1130             }
1131          }
1132       }
1133    }
1134    
1135    /* is it a bottom line? */ 
1136    if (row_ptr->par->ParFromPos(RowLast(row_ptr) + 1) == par
1137        && (!row_ptr->next || row_ptr->next->par != row_ptr->par)) {     
1138           
1139           /* the paper margins */ 
1140           if (!par->next)
1141             maxdesc += LYX_PAPER_MARGIN;
1142         
1143           /* add the vertical spaces, that the user added */
1144           if (firstpar->added_space_bottom.kind() != VSpace::NONE)
1145             maxdesc += int(firstpar->added_space_bottom.inPixels());
1146           
1147           /* do not forget the DTP-lines! 
1148            * there height depends on the font of the nearest character */
1149           if (firstpar->line_bottom)
1150             maxdesc += 2 * (GetFont(par, par->Last()-1).ascent('x'));
1151           
1152           /* and now the pagebreaks */
1153           if (firstpar->pagebreak_bottom)
1154             maxdesc += 3 * DefaultHeight();
1155           
1156           /* and now the layout spaces, for example before and after a section, 
1157            * or between the items of a itemize or enumerate environment */
1158           if (!firstpar->pagebreak_bottom && row_ptr->par->Next()) {
1159              LyXParagraph *nextpar = row_ptr->par->Next();
1160              LyXParagraph *comparepar = row_ptr->par;
1161              float usual = 0;
1162              float  unusual = 0;
1163              
1164              if (comparepar->GetDepth() > nextpar->GetDepth()) {
1165                 usual = (lyxstyle.Style(parameters->textclass, comparepar->GetLayout())->bottomsep * DefaultHeight());
1166                 comparepar = comparepar->DepthHook(nextpar->GetDepth());
1167                 if (comparepar->GetLayout()!=nextpar->GetLayout()
1168                     || nextpar->GetLabelWidthString() != 
1169                         comparepar->GetLabelWidthString())
1170                   unusual = (lyxstyle.Style(parameters->textclass, comparepar->GetLayout())->bottomsep * DefaultHeight());
1171                 
1172                 if (unusual > usual)
1173                   layoutdesc = unusual;
1174                 else
1175                   layoutdesc = usual;
1176              }
1177              else if (comparepar->GetDepth() ==  nextpar->GetDepth()) {
1178                 
1179                 if (comparepar->GetLayout()!= nextpar->GetLayout()
1180                     || nextpar->GetLabelWidthString() !=
1181                         comparepar->GetLabelWidthString())
1182                   layoutdesc = int(lyxstyle.Style(parameters->textclass, comparepar->GetLayout())->bottomsep * DefaultHeight());
1183              }
1184           }
1185        }
1186    
1187    /* incalculate the layout spaces */ 
1188    maxasc += int(layoutasc * 2 / (2 + firstpar->GetDepth()));
1189    maxdesc += int(layoutdesc * 2 / (2 + firstpar->GetDepth()));
1190
1191    /* table stuff -- begin*/
1192    if (row_ptr->par->table){
1193       maxasc += row_ptr->par->table->
1194         AdditionalHeight(NumberOfCell(row_ptr->par, row_ptr->pos));
1195    }
1196    /* table stuff -- end*/
1197    
1198    /* calculate the new height of the text */ 
1199    height -= row_ptr->height;
1200    
1201    row_ptr->height=maxasc+maxdesc+labeladdon;
1202    row_ptr->baseline=maxasc+labeladdon;
1203    
1204    height += row_ptr->height;
1205 }
1206
1207
1208 /* Appends the implicit specified paragraph behind the specified row,
1209  * start at the implicit given position */
1210 void LyXText::AppendParagraph(Row *row)
1211 {
1212    int z;
1213    Row *tmprow;
1214    bool not_ready = true;
1215    
1216    // Get the width of the row
1217    z = row->pos;
1218
1219    // The last character position of a paragraph is an invariant so we can 
1220    // safely get it here. (Asger)
1221    int lastposition = row->par->Last();
1222
1223    do {
1224       // Get the next breakpoint
1225       z = NextBreakPoint(row, paperwidth);
1226       
1227       tmprow = row;
1228
1229       // Insert the new row
1230       if (z < lastposition) {
1231          z++;
1232          InsertRow(row, row->par, z);
1233          row = row->next;
1234
1235          row->height = 0;
1236       } else
1237          not_ready = false;
1238       
1239       // Set the dimensions of the row
1240       tmprow->fill = Fill(tmprow, paperwidth);
1241       SetHeightOfRow(tmprow);
1242
1243    } while (not_ready);
1244 }
1245
1246
1247 void LyXText::BreakAgain(Row *row)
1248 {
1249    int z;
1250    char not_ready;
1251    z = row->pos;
1252    Row *tmprow, *tmprow2;
1253    
1254    not_ready = 1;
1255    
1256    do  {
1257       /* get the next breakpoint */
1258       z = NextBreakPoint(row, paperwidth);
1259       
1260       tmprow = row;
1261       
1262       if (z < row->par->Last() ) {
1263          if (!row->next || (row->next && row->next->par != row->par)) {
1264             /* insert a new row */ 
1265             z++;
1266             InsertRow(row, row->par, z);
1267             row = row->next;
1268             row->height = 0;
1269          }
1270          else  {
1271             row=row->next;
1272             z++;
1273             if (row->pos == z)
1274               not_ready = 0;           /* the rest will not change  */
1275             else {
1276                row->pos = z;
1277             }
1278          }
1279       }
1280       else {
1281          /* if there are some rows too much, delete them */
1282          /* only if you broke the whole paragraph! */ 
1283          tmprow2 = row;
1284          while (tmprow2->next && tmprow2->next->par == row->par) {
1285             tmprow2 = tmprow2->next;
1286          }
1287          while (tmprow2 != row) {
1288             tmprow2 = tmprow2->previous;
1289             RemoveRow(tmprow2->next);
1290          }
1291          not_ready = 0;
1292       }
1293        
1294       /* set the dimensions of the row */ 
1295       tmprow->fill = Fill(tmprow, paperwidth);
1296       SetHeightOfRow(tmprow);
1297    } while (not_ready);
1298 }
1299
1300
1301 /* this is just a little changed version of break again */ 
1302 void LyXText::BreakAgainOneRow(Row *row)
1303 {
1304    int z;
1305    char not_ready;
1306    z = row->pos;
1307    Row *tmprow, *tmprow2;
1308    
1309    not_ready = 1;
1310    
1311    /* get the next breakpoint */
1312    z = NextBreakPoint(row, paperwidth);
1313    
1314    tmprow = row;
1315    
1316    if (z < row->par->Last() ) {
1317       if (!row->next || (row->next && row->next->par != row->par)) {
1318          /* insert a new row */ 
1319          z++;
1320          InsertRow(row, row->par, z);
1321          row = row->next;
1322          row->height = 0;
1323       }
1324       else  {
1325          row=row->next;
1326          z++;
1327          if (row->pos == z)
1328            not_ready = 0;              /* the rest will not change  */
1329          else {
1330             row->pos = z;
1331          }
1332       }
1333    }
1334    else {
1335       /* if there are some rows too much, delete them */
1336       /* only if you broke the whole paragraph! */ 
1337       tmprow2 = row;
1338       while (tmprow2->next && tmprow2->next->par == row->par) {
1339          tmprow2 = tmprow2->next;
1340       }
1341       while (tmprow2 != row) {
1342          tmprow2 = tmprow2->previous;
1343          RemoveRow(tmprow2->next);
1344       }
1345       not_ready = 0;
1346    }
1347    
1348    /* set the dimensions of the row */ 
1349    tmprow->fill = Fill(tmprow, paperwidth);
1350    SetHeightOfRow(tmprow);
1351 }
1352
1353
1354 void LyXText::BreakParagraph(char keep_layout)
1355 {
1356    LyXLayout *layout = lyxstyle.Style(parameters->textclass,
1357                                       cursor.par->GetLayout());
1358    
1359    /* table stuff -- begin*/
1360    if (cursor.par->table) {
1361        // breaking of tables is only allowed at the beginning or the end */
1362        if (cursor.pos && cursor.pos < cursor.par->last &&
1363            !cursor.par->table->ShouldBeVeryLastCell(NumberOfCell(cursor.par, cursor.pos)))
1364            return; /* no breaking of tables allowed */ 
1365    }
1366    /* table stuff -- end*/
1367
1368    /* this is only allowed, if the current paragraph is not empty or caption*/ 
1369    if ((cursor.par->Last() <= 0 && !cursor.par->IsDummy())
1370        && 
1371        layout->labeltype!=LABEL_SENSITIVE)
1372      return;
1373
1374    SetUndo(Undo::INSERT, 
1375            cursor.par->ParFromPos(cursor.pos)->previous, 
1376            cursor.par->ParFromPos(cursor.pos)->next); 
1377
1378    /* table stuff -- begin*/
1379    if (cursor.par->table) {
1380        int cell = NumberOfCell(cursor.par, cursor.pos);
1381        if (cursor.par->table->ShouldBeVeryLastCell(cell))
1382            SetCursor(cursor.par,cursor.par->last);
1383    }
1384    /* table stuff -- end*/
1385    /* please break alway behind a space */ 
1386    if (cursor.pos < cursor.par->Last()
1387        && cursor.par->IsLineSeparator(cursor.pos))
1388      cursor.pos++;
1389    
1390    /* break the paragraph */
1391    if (keep_layout)
1392      keep_layout = 2;
1393    else 
1394      keep_layout = layout->isEnvironment();
1395    cursor.par->BreakParagraph(cursor.pos, keep_layout);
1396
1397    /* table stuff -- begin*/
1398    if (cursor.par->table){
1399      // the table should stay with the contents
1400      if (!cursor.pos){
1401        cursor.par->Next()->table = cursor.par->table;
1402        cursor.par->table = NULL;
1403      }
1404    }
1405    /* table stuff -- end*/
1406
1407    /* well this is the caption hack since one caption is really enough */
1408    if (layout->labeltype == LABEL_SENSITIVE){
1409      if (!cursor.pos)
1410        cursor.par->SetLayout(0); /* set the new paragraph to standard-layout */
1411      else
1412        cursor.par->Next()->SetLayout(0); /* set the new paragraph to standard-layout */
1413          
1414    }
1415    
1416    /* if the cursor is at the beginning of a row without prior newline, 
1417     *  move one row up! 
1418     * This touches only the screen-update. Otherwise we would may have
1419     * an empty row on the screen */
1420    if (cursor.pos && !cursor.row->par->IsNewline(cursor.row->pos -1) &&
1421        cursor.row->pos == cursor.pos) {
1422      CursorLeft();
1423    } 
1424    
1425    status = LyXText::NEED_MORE_REFRESH;
1426    refresh_row = cursor.row;
1427    refresh_y = cursor.y - cursor.row->baseline;
1428    
1429    /* Do not forget the special right address boxes */
1430    if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1431       while (refresh_row->previous &&
1432              refresh_row->previous->par == refresh_row->par) {
1433                 refresh_row = refresh_row->previous;
1434                 refresh_y -= refresh_row->height;
1435              }
1436    }
1437    RemoveParagraph(cursor.row);
1438    
1439    /* set the dimensions of the cursor row */
1440    cursor.row->fill = Fill(cursor.row, paperwidth);
1441
1442    SetHeightOfRow(cursor.row);
1443    
1444    while (!cursor.par->Next()->table && cursor.par->Next()->Last()
1445           && cursor.par->Next()->IsNewline(0))
1446      cursor.par->Next()->Erase(0);
1447    
1448    InsertParagraph(cursor.par->Next(), cursor.row);
1449
1450    UpdateCounters(cursor.row->previous);
1451    
1452    /* this check is necessary. Otherwise the new empty paragraph will
1453     * be deleted automatically. And it is more friendly for the user! */ 
1454    if (cursor.pos)
1455      SetCursor(cursor.par->Next(), 0);
1456    else
1457      SetCursor(cursor.par, 0);
1458    
1459    if (cursor.row->next)
1460      BreakAgain(cursor.row->next);
1461
1462    need_break_row = NULL;
1463 }
1464
1465
1466 void LyXText::OpenFootnote()
1467 {
1468    LyXParagraph *par, *endpar,*tmppar;
1469    Row *row;
1470    
1471    par = cursor.par->ParFromPos(cursor.pos);
1472    
1473    /* if there is no footnote in this paragraph, just return. */ 
1474    if (!par->next
1475        || par->next->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1476      return;
1477    
1478    /* ok, move the cursor right before the footnote */ 
1479    
1480    /* just a little faster than using CursorRight() */
1481    for (cursor.pos=0; cursor.par->ParFromPos(cursor.pos)!=par; cursor.pos++);
1482    /* now the cursor is at the beginning of the physical par */
1483    SetCursor(cursor.par, cursor.pos + cursor.par->ParFromPos(cursor.pos)->last);
1484    
1485    /* the cursor must be exactly before the footnote */ 
1486    par = cursor.par->ParFromPos(cursor.pos);
1487    
1488    status = LyXText::NEED_MORE_REFRESH;
1489    refresh_row = cursor.row;
1490    refresh_y = cursor.y - cursor.row->baseline;
1491    
1492    tmppar = cursor.par;
1493    endpar = cursor.par->Next();
1494    row = cursor.row;
1495    
1496    tmppar->OpenFootnote(cursor.pos);
1497    RemoveParagraph(row);
1498    /* set the dimensions of the cursor row */
1499    row->fill = Fill(row, paperwidth);
1500    SetHeightOfRow(row);
1501    tmppar = tmppar->Next();
1502    
1503    while (tmppar != endpar) {
1504       if (tmppar) {
1505          InsertParagraph(tmppar, row);
1506          while (row->next && row->next->par == tmppar)
1507            row = row->next;
1508          tmppar = tmppar->Next();
1509       }
1510    }
1511    SetCursor(par->next, 0);
1512    sel_cursor = cursor;
1513 }
1514    
1515
1516 /* table stuff -- begin*/
1517
1518 void LyXText::TableFeatures(int feature, LString val)
1519 {
1520     int
1521         actCell;
1522     
1523     if (!cursor.par->table)
1524         return; /* this should never happen */
1525   
1526     actCell = NumberOfCell(cursor.par, cursor.pos);
1527     SetUndo(Undo::FINISH, 
1528             cursor.par->ParFromPos(cursor.pos)->previous, 
1529             cursor.par->ParFromPos(cursor.pos)->next); 
1530
1531     switch (feature){
1532       case LyXTable::SET_PWIDTH:
1533           cursor.par->table->SetPWidth(actCell,val);
1534           break;
1535       case LyXTable::SET_SPECIAL_COLUMN:
1536       case LyXTable::SET_SPECIAL_MULTI:
1537           cursor.par->table->SetAlignSpecial(actCell,val,feature);
1538           break;
1539       default:
1540           break;
1541     }
1542     RedoParagraph();
1543 }
1544
1545
1546 void LyXText::TableFeatures(int feature)
1547 {
1548     int
1549         setLines = 0,
1550         setAlign = LYX_ALIGN_LEFT,
1551         lineSet,
1552         actCell;
1553     bool
1554         what;
1555     
1556     if (!cursor.par->table)
1557         return; /* this should never happen */
1558   
1559     actCell = NumberOfCell(cursor.par, cursor.pos);
1560     SetUndo(Undo::FINISH, 
1561             cursor.par->ParFromPos(cursor.pos)->previous, 
1562             cursor.par->ParFromPos(cursor.pos)->next); 
1563
1564     switch (feature){
1565       case LyXTable::ALIGN_LEFT:
1566           setAlign=LYX_ALIGN_LEFT;
1567           break;
1568       case LyXTable::ALIGN_RIGHT:
1569           setAlign=LYX_ALIGN_RIGHT;
1570           break;
1571       case LyXTable::ALIGN_CENTER:
1572           setAlign=LYX_ALIGN_CENTER;
1573           break;
1574       default:
1575           break;
1576     }
1577     switch (feature){
1578       case LyXTable::APPEND_ROW: {
1579           int pos = cursor.pos;
1580           /* move to the next row */
1581           int cell_org = actCell;
1582           int cell = cell_org;
1583
1584           // if there is a ContRow following this row I have to add
1585           // the row after the ContRow's
1586           if ((pos < cursor.par->Last()) &&
1587               cursor.par->table->RowHasContRow(cell_org)) {
1588               while((pos < cursor.par->Last()) &&
1589                     !cursor.par->table->IsContRow(cell)) {
1590                   while (pos < cursor.par->Last() &&
1591                          !cursor.par->IsNewline(pos))
1592                       pos++;
1593                   if (pos < cursor.par->Last())
1594                       pos++;
1595                   cell++;
1596               }
1597               while((pos < cursor.par->Last()) &&
1598                     cursor.par->table->IsContRow(cell)) {
1599                   while (pos < cursor.par->Last() &&
1600                          !cursor.par->IsNewline(pos))
1601                       pos++;
1602                   if (pos < cursor.par->Last())
1603                       pos++;
1604                   cell++;
1605               }
1606               cell_org = --cell;
1607               if (pos < cursor.par->Last())
1608                   pos--;
1609           }
1610           while (pos < cursor.par->Last() && 
1611                  (cell == cell_org || !cursor.par->table->IsFirstCell(cell))){
1612               while (pos < cursor.par->Last() && !cursor.par->IsNewline(pos))
1613                   pos++;
1614               if (pos < cursor.par->Last())
1615                   pos++;
1616               cell++;
1617           }
1618                 
1619           /* insert the new cells */ 
1620           int number = cursor.par->table->NumberOfCellsInRow(cell_org);
1621           int i;
1622           for (i=0; i<number; i++)
1623               cursor.par->InsertChar(pos, LYX_META_NEWLINE);
1624                 
1625           /* append the row into the table */
1626           cursor.par->table->AppendRow(cell_org);
1627           RedoParagraph();
1628           return;
1629       }
1630       case LyXTable::APPEND_CONT_ROW: {
1631           int pos = cursor.pos;
1632           /* move to the next row */
1633           int cell_org = actCell;
1634           int cell = cell_org;
1635
1636           // if there is already a controw but not for this cell
1637           // the AppendContRow sets only the right values but does
1638           // not actually add a row
1639           if (cursor.par->table->RowHasContRow(cell_org) &&
1640               (cursor.par->table->CellHasContRow(cell_org)<0)) {
1641               cursor.par->table->AppendContRow(cell_org);
1642               RedoParagraph();
1643               return;
1644           }
1645           while (pos < cursor.par->Last() && 
1646                  (cell == cell_org
1647                   || !cursor.par->table->IsFirstCell(cell))){
1648               while (pos < cursor.par->Last() && !cursor.par->IsNewline(pos))
1649                   pos++;
1650               if (pos < cursor.par->Last())
1651                   pos++;
1652               cell++;
1653           }
1654                 
1655           /* insert the new cells */ 
1656           int number = cursor.par->table->NumberOfCellsInRow(cell_org);
1657           int i;
1658           for (i=0; i<number; i++)
1659               cursor.par->InsertChar(pos, LYX_META_NEWLINE);
1660                 
1661           /* append the row into the table */
1662           cursor.par->table->AppendContRow(cell_org);
1663           RedoParagraph();
1664           return;
1665       }
1666       case LyXTable::APPEND_COLUMN: { 
1667           int pos = 0;
1668           int cell_org = actCell;
1669           int cell = 0;
1670           do{
1671               if (pos && (cursor.par->IsNewline(pos-1))){
1672                   if (cursor.par->table->AppendCellAfterCell(cell_org, cell)){
1673                       cursor.par->InsertChar(pos, LYX_META_NEWLINE);
1674                       if (pos<=cursor.pos)
1675                           cursor.pos++;
1676                       pos++;
1677                   }
1678                   cell++;
1679               }
1680               pos++;
1681           } while (pos<= cursor.par->Last());
1682           /* remember that the very last cell doesn't end with a newline.
1683              This saves one byte memory per table ;-) */ 
1684           if (cursor.par->table->AppendCellAfterCell(cell_org, cell))
1685               cursor.par->InsertChar(cursor.par->Last(), LYX_META_NEWLINE);
1686                 
1687           /* append the column into the table */ 
1688           cursor.par->table->AppendColumn(cell_org);
1689                 
1690           RedoParagraph();
1691           return;
1692       }
1693       case LyXTable::DELETE_ROW:
1694           if (current_view->currentBuffer()->the_locking_inset)
1695               UnlockInset(current_view->currentBuffer()->the_locking_inset);
1696           RemoveTableRow(&cursor);
1697           RedoParagraph();
1698           return;
1699         
1700       case LyXTable::DELETE_COLUMN: {
1701           int pos = 0;
1702           int cell_org = actCell;
1703           int cell = 0;
1704           if (current_view->currentBuffer()->the_locking_inset)
1705               UnlockInset(current_view->currentBuffer()->the_locking_inset);
1706           do {
1707               if (!pos || (cursor.par->IsNewline(pos-1))){
1708                   if (cursor.par->table->DeleteCellIfColumnIsDeleted(cell, cell_org)){
1709                       // delete one cell
1710                       while (pos < cursor.par->Last() && !cursor.par->IsNewline(pos))
1711                           cursor.par->Erase(pos);
1712                       if (pos < cursor.par->Last())
1713                           cursor.par->Erase(pos);
1714                       else 
1715                           cursor.par->Erase(pos - 1); // the missing newline at the end of a table
1716                       pos--; // because of pos++ below
1717                   }   
1718                   cell++;
1719               }
1720               pos++;
1721           } while (pos<= cursor.par->Last());
1722                 
1723           /* delete the column from the table */ 
1724           cursor.par->table->DeleteColumn(cell_org);
1725                 
1726           /* set the cursor to the beginning of the table, where else? */ 
1727           cursor.pos = 0;
1728           RedoParagraph();
1729           return;
1730       }
1731       case LyXTable::TOGGLE_LINE_TOP:
1732           lineSet = !cursor.par->table->TopLine(actCell);
1733           if (!selection){
1734               cursor.par->table->SetTopLine(actCell,lineSet);
1735           } else {
1736               int i,n=-1,m=-2;
1737               for (i=sel_start_cursor.pos; i<=sel_end_cursor.pos; i++){
1738                   if ((n=NumberOfCell(sel_start_cursor.par,i)) != m) {
1739                       cursor.par->table->SetTopLine(n,lineSet);
1740                       m = n;
1741                   }
1742               }
1743           }
1744           RedoParagraph();
1745           return;
1746     
1747       case LyXTable::TOGGLE_LINE_BOTTOM:
1748           lineSet = !cursor.par->table->BottomLine(actCell);
1749           if (!selection){
1750               cursor.par->table->SetBottomLine(actCell,lineSet);
1751           } else {
1752               int i,n=-1,m=-2;
1753               for (i=sel_start_cursor.pos; i<=sel_end_cursor.pos; i++){
1754                   if ((n=NumberOfCell(sel_start_cursor.par,i)) != m) {
1755                       cursor.par->table->SetBottomLine(n,lineSet);
1756                       m = n;
1757                   }
1758               }
1759           }
1760           RedoParagraph();
1761           return;
1762                 
1763       case LyXTable::TOGGLE_LINE_LEFT:
1764           lineSet = !cursor.par->table->LeftLine(actCell);
1765           if (!selection){
1766               cursor.par->table->SetLeftLine(actCell,lineSet);
1767           } else {
1768               int i,n=-1,m=-2;
1769               for (i=sel_start_cursor.pos; i<=sel_end_cursor.pos; i++){
1770                   if ((n=NumberOfCell(sel_start_cursor.par,i)) != m) {
1771                       cursor.par->table->SetLeftLine(n,lineSet);
1772                       m = n;
1773                   }
1774               }
1775           }
1776           RedoParagraph();
1777           return;
1778
1779       case LyXTable::TOGGLE_LINE_RIGHT:
1780           lineSet = !cursor.par->table->RightLine(actCell);
1781           if (!selection){
1782               cursor.par->table->SetRightLine(actCell,lineSet);
1783           } else {
1784               int i,n=-1,m=-2;
1785               for (i=sel_start_cursor.pos; i<=sel_end_cursor.pos; i++){
1786                   if ((n=NumberOfCell(sel_start_cursor.par,i)) != m) {
1787                       cursor.par->table->SetRightLine(n,lineSet);
1788                       m = n;
1789                   }
1790               }
1791           }
1792           RedoParagraph();
1793           return;
1794     
1795       case LyXTable::ALIGN_LEFT:
1796       case LyXTable::ALIGN_RIGHT:
1797       case LyXTable::ALIGN_CENTER:
1798           if (!selection){
1799               cursor.par->table->SetAlignment(actCell,setAlign);
1800           } else {
1801               int i,n=-1,m=-2;
1802               for (i=sel_start_cursor.pos; i<=sel_end_cursor.pos; i++){
1803                   if ((n=NumberOfCell(sel_start_cursor.par,i)) != m) {
1804                       cursor.par->table->SetAlignment(n,setAlign);
1805                       m = n;
1806                   }
1807               }
1808           }
1809           RedoParagraph();
1810           return;
1811                 
1812       case LyXTable::DELETE_TABLE:
1813           SetCursorIntern(cursor.par, 0);
1814           delete cursor.par->table;
1815           cursor.par->table = NULL;
1816           // temporary: Should put table in simple_cut_buffer (with before and after
1817           // dummy-paragraph !! 
1818           // not necessar anymore with UNDO :)
1819           int i;
1820           for (i = cursor.par->last-1; i>=0; i--)
1821               cursor.par->Erase(i);
1822           RedoParagraph();
1823           return;
1824                 
1825       case LyXTable::MULTICOLUMN: {
1826           int number = 0;
1827           // check wether we are completly in a multicol
1828           int multicol = cursor.par->table->IsMultiColumn(actCell);
1829           if (multicol && selection && sel_start_cursor.row == sel_end_cursor.row){
1830               multicol = NumberOfCell(sel_start_cursor.par, sel_start_cursor.pos)
1831                   == NumberOfCell(sel_end_cursor.par, sel_end_cursor.pos);
1832           }
1833
1834           if (multicol){
1835               int newlines = cursor.par->table->UnsetMultiColumn(actCell);
1836               int pos = cursor.pos;
1837               while (pos<cursor.par->Last() && !cursor.par->IsNewline(pos))
1838                   pos++;
1839               for (;newlines;newlines--)
1840                   cursor.par->InsertChar(pos, LYX_META_NEWLINE);
1841               RedoParagraph();
1842               return;
1843           }
1844           else {
1845               // selection must be in one row (or no selection)
1846               if (!selection){
1847                   cursor.par->table->SetMultiColumn(NumberOfCell(cursor.par,
1848                                                                  cursor.pos),
1849                                                     1);
1850                   RedoParagraph();
1851                   return;
1852               }
1853               else {
1854                   if (sel_start_cursor.row == sel_end_cursor.row){
1855                       int i;
1856                       number = 1;
1857                       for (i=sel_start_cursor.pos; i<sel_end_cursor.pos; i++){
1858                           if (sel_start_cursor.par->IsNewline(i)){
1859                               sel_start_cursor.par->Erase(i);
1860                               // check for double-blanks
1861                               if ((i && !sel_start_cursor.par->IsLineSeparator(i-1))
1862                                   &&
1863                                   (i<sel_start_cursor.par->Last() 
1864                                    && !sel_start_cursor.par->IsLineSeparator(i)))
1865                                   sel_start_cursor.par->InsertChar(i, ' ');
1866                               else {
1867                                   sel_end_cursor.pos--;
1868                                   i--;
1869                               }
1870                               number++;
1871                           }
1872                       }
1873                       cursor.par->table->
1874                           SetMultiColumn(NumberOfCell(sel_start_cursor.par,
1875                                                       sel_start_cursor.pos),
1876                                          number);
1877                       cursor.pos = sel_start_cursor.pos;
1878                       RedoParagraph();
1879                       return;
1880                   }
1881                   else {
1882                       WriteAlert(_("Impossible Operation!"), 
1883                                  _("Multicolumns can only be horizontally."), 
1884                                  _("Sorry."));
1885                   }
1886               }
1887           }
1888           break;
1889       }
1890       case LyXTable::SET_ALL_LINES:
1891           setLines = 1;
1892       case LyXTable::UNSET_ALL_LINES:
1893           if (!selection){
1894               cursor.par->table->SetAllLines(NumberOfCell(cursor.par,
1895                                                           cursor.pos),
1896                                              setLines);
1897           } else {
1898               int i,n=-1,m=-2;
1899               for (i=sel_start_cursor.pos; i<=sel_end_cursor.pos; i++){
1900                   if ((n=NumberOfCell(sel_start_cursor.par,i)) != m) {
1901                       cursor.par->table->SetAllLines(n,setLines);
1902                       m = n;
1903                   }
1904               }
1905           }
1906           RedoParagraph();
1907           return;
1908       case LyXTable::SET_LONGTABLE:
1909           cursor.par->table->SetLongTable(true);
1910           return;
1911       case LyXTable::UNSET_LONGTABLE:
1912           cursor.par->table->SetLongTable(false);
1913           return;
1914       case LyXTable::SET_ROTATE_TABLE:
1915           cursor.par->table->SetRotateTable(true);
1916           return;
1917       case LyXTable::UNSET_ROTATE_TABLE:
1918           cursor.par->table->SetRotateTable(false);
1919           return;
1920       case LyXTable::SET_ROTATE_CELL:
1921           if (!selection){
1922               cursor.par->table->SetRotateCell(actCell,true);
1923           } else {
1924               int i,n=-1,m=-2;
1925               for (i=sel_start_cursor.pos; i<=sel_end_cursor.pos; i++){
1926                   if ((n=NumberOfCell(sel_start_cursor.par,i)) != m) {
1927                       cursor.par->table->SetRotateCell(n,true);
1928                       m = n;
1929                   }
1930               }
1931           }
1932           return;
1933       case LyXTable::UNSET_ROTATE_CELL:
1934           if (!selection){
1935               cursor.par->table->SetRotateCell(actCell,false);
1936           } else {
1937               int i,n=-1,m=-2;
1938               for (i=sel_start_cursor.pos; i<=sel_end_cursor.pos; i++){
1939                   if ((n=NumberOfCell(sel_start_cursor.par,i)) != m) {
1940                       cursor.par->table->SetRotateCell(n,false);
1941                       m = n;
1942                   }
1943               }
1944           }
1945           return;
1946       case LyXTable::SET_LINEBREAKS:
1947           what = !cursor.par->table->Linebreaks(cursor.par->table->FirstVirtualCell(actCell));
1948           if (!selection){
1949               cursor.par->table->SetLinebreaks(actCell,what);
1950           } else {
1951               int i,n=-1,m=-2;
1952               for (i=sel_start_cursor.pos; i<=sel_end_cursor.pos; i++){
1953                   if ((n=NumberOfCell(sel_start_cursor.par,i)) != m) {
1954                       cursor.par->table->SetLinebreaks(n,what);
1955                       m = n;
1956                   }
1957               }
1958           }
1959           return;
1960       case LyXTable::SET_LTFIRSTHEAD:
1961           cursor.par->table->SetLTHead(actCell,true);
1962           return;
1963       case LyXTable::SET_LTHEAD:
1964           cursor.par->table->SetLTHead(actCell,false);
1965           return;
1966       case LyXTable::SET_LTFOOT:
1967           cursor.par->table->SetLTFoot(actCell,false);
1968           return;
1969       case LyXTable::SET_LTLASTFOOT:
1970           cursor.par->table->SetLTFoot(actCell,true);
1971           return;
1972       case LyXTable::SET_LTNEWPAGE:
1973           what = !cursor.par->table->LTNewPage(actCell);
1974           cursor.par->table->SetLTNewPage(actCell,what);
1975           return;
1976     }
1977 }
1978         
1979
1980 void LyXText::InsertCharInTable(char c)
1981 {
1982         Row *row;
1983         Row *tmprow;
1984         long y;
1985         bool jumped_over_space;
1986         
1987         /* first check, if there will be two blanks together or a blank at 
1988          * the beginning of a paragraph. 
1989          * I decided to handle blanks like normal characters, the main 
1990          * difference are the special checks when calculating the row.fill
1991          * (blank does not count at the end of a row) and the check here */ 
1992         
1993         LyXFont realtmpfont = real_current_font;
1994         LyXFont rawtmpfont = current_font; /* store the current font.
1995                                             * This is because of the use
1996                                             * of cursor movements. The moving
1997                                             * cursor would refresh the 
1998                                             * current font */
1999
2000         // Get the font that is used to calculate the baselineskip
2001         int const lastpos = cursor.par->Last();
2002         LyXFont rawparfont = cursor.par->GetFontSettings(lastpos - 1);
2003
2004         jumped_over_space = false;
2005         if (IsLineSeparatorChar(c)) {
2006                 
2007                 /* avoid double blanks but insert the new blank because
2008                  * of a possible font change */
2009                 if (cursor.pos < lastpos &&
2010                     cursor.par->IsLineSeparator(cursor.pos))
2011                 {
2012                         cursor.par->Erase(cursor.pos);
2013                         jumped_over_space = true;
2014                 }
2015                 else if ((cursor.pos > 0 && 
2016                           cursor.par->IsLineSeparator(cursor.pos - 1))
2017                          || (cursor.pos > 0 && cursor.par->IsNewline(cursor.pos - 1))
2018                           || (cursor.pos == 0 &&
2019                               !(cursor.par->Previous()
2020                               && cursor.par->Previous()->footnoteflag
2021                               == LyXParagraph::OPEN_FOOTNOTE)))
2022                         return;
2023         }
2024         else if (IsNewlineChar(c)) {
2025             if (!IsEmptyTableCell()) {
2026                 TableFeatures(LyXTable::APPEND_CONT_ROW);
2027                 CursorDown();
2028             }
2029           /* the newline character is the separator of the cells */
2030           // cursor.par->InsertChar(cursor.pos, c);
2031           // SetCharFont(cursor.par, cursor.pos, rawtmpfont);
2032           // RedoParagraphs(cursor, cursor.par->Next());
2033           // SetCursor(cursor.par, cursor.pos+1);
2034           return;
2035         }
2036    
2037         row = cursor.row;
2038         y = cursor.y - row->baseline;
2039         if (c != LYX_META_INSET)        /* in this case LyXText::InsertInset 
2040                                          * already inserted the character */
2041                 cursor.par->InsertChar(cursor.pos, c);
2042         SetCharFont(cursor.par, cursor.pos, rawtmpfont);
2043
2044         if (!jumped_over_space) {
2045                 /* refresh the positions */
2046                 tmprow = row;
2047                 while (tmprow->next && tmprow->next->par == row->par) {
2048                         tmprow = tmprow->next;
2049                         tmprow->pos++;
2050                 }
2051         }
2052
2053         cursor.pos++;
2054
2055         CheckParagraphInTable(cursor.par, cursor.pos);
2056         
2057         current_font = rawtmpfont;
2058         real_current_font = realtmpfont;
2059         
2060         /* check, whether the last character's font has changed. */
2061         if (cursor.pos && cursor.pos == cursor.par->Last()
2062             && rawparfont != rawtmpfont)
2063                 RedoHeightOfParagraph(cursor);
2064 }
2065
2066
2067 void LyXText::CheckParagraphInTable(LyXParagraph* par, int pos)
2068 {
2069         Row *row;
2070         long y;
2071         
2072         if (par->GetChar(pos) == LYX_META_INSET &&
2073             par->GetInset(pos) && par->GetInset(pos)->Display()){
2074           par->GetInset(pos)->SetDisplay(false);
2075         }
2076
2077         row = GetRow(par, pos, y);
2078         
2079         int tmpheight = row->height;
2080         SetHeightOfRow(row);
2081         
2082         int tmp_pos = pos;
2083         /* update the table information */
2084         while (tmp_pos && !par->IsNewline(tmp_pos - 1))
2085                 tmp_pos--;
2086         if (par->table->SetWidthOfCell(NumberOfCell(par, pos),
2087                                        WidthOfCell(par, tmp_pos))) {
2088                 LyXCursor tmpcursor = cursor;
2089                 SetCursorIntern(par, pos);
2090                 /* make a complete redraw */
2091                 RedoDrawingOfParagraph(cursor);
2092                 cursor = tmpcursor;
2093         }
2094         else {
2095                 /* redraw only the row */
2096                 LyXCursor tmpcursor = cursor;
2097                 SetCursorIntern(par, pos);
2098                 refresh_y = y;
2099                 refresh_x = cursor.x;
2100                 refresh_row = row;
2101                 refresh_pos = cursor.pos;
2102                 cursor = tmpcursor;
2103                 
2104                 if (tmpheight == row->height)
2105                         status = LyXText::NEED_VERY_LITTLE_REFRESH;
2106                 else
2107                         status = LyXText::NEED_MORE_REFRESH;
2108         }
2109         SetCursorIntern(cursor.par, cursor.pos);
2110 }
2111
2112
2113 void LyXText::BackspaceInTable()
2114 {
2115         Row *tmprow, *row;
2116         long y;
2117         
2118         LyXFont rawtmpfont = current_font;
2119         LyXFont realtmpfont = real_current_font;
2120
2121         // Get the font that is used to calculate the baselineskip
2122         int const lastpos = cursor.par->Last();
2123         LyXFont rawparfont = cursor.par->GetFontSettings(lastpos - 1);
2124         
2125         if (cursor.pos == 0) {
2126                 /* no pasting of table paragraphs */
2127                 
2128                 CursorLeft();
2129         }
2130         else {
2131                 /* this is the code for a normal backspace, not pasting
2132                  * any paragraphs */ 
2133           SetUndo(Undo::DELETE, 
2134                   cursor.par->ParFromPos(cursor.pos)->previous, 
2135                   cursor.par->ParFromPos(cursor.pos)->next); 
2136           
2137                 CursorLeftIntern();
2138                 
2139                 /* some insets are undeletable here */
2140                 if (cursor.par->GetChar(cursor.pos)==LYX_META_INSET) {
2141                         if (!cursor.par->GetInset(cursor.pos)->Deletable())
2142                                 return;
2143                 }
2144                 
2145                 row = cursor.row;
2146                 y = cursor.y - row->baseline;
2147                 
2148                 /* some special code when deleting a newline. */
2149                 if (cursor.par->IsNewline(cursor.pos)) {
2150                         /* nothing :-) */
2151                         return;
2152                 }
2153                 else {
2154                         cursor.par->Erase(cursor.pos);
2155                         
2156                         /* refresh the positions */
2157                         tmprow = row;
2158                         while (tmprow->next && tmprow->next->par == row->par) {
2159                                 tmprow = tmprow->next;
2160                                 tmprow->pos--;
2161                         }
2162                         
2163                         /* delete superfluous blanks */ 
2164                         if (cursor.pos < cursor.par->Last() - 1 &&
2165                         (cursor.par->IsLineSeparator(cursor.pos))) {
2166                                 
2167                                 if (cursor.pos == BeginningOfMainBody(cursor.par)
2168                                 || !cursor.pos 
2169                                 || cursor.par->IsLineSeparator(cursor.pos - 1)) {
2170                                         cursor.par->Erase(cursor.pos);
2171                                         /* refresh the positions */
2172                                         tmprow = row;
2173                                         while (tmprow->next && 
2174                                                tmprow->next->par == row->par) {
2175                                                 tmprow = tmprow->next;
2176                                                 tmprow->pos--;
2177                                         }
2178                                         if (cursor.pos)   /* move one character left */
2179                                                 cursor.pos--;
2180                                 }
2181                         }
2182                 }
2183       
2184                 CheckParagraphInTable(cursor.par, cursor.pos);
2185       
2186                 /* check, wether the last characters font has changed. */ 
2187                 if (cursor.pos && cursor.pos == cursor.par->Last()
2188                     && rawparfont != rawtmpfont)
2189                         RedoHeightOfParagraph(cursor);
2190
2191                 /* restore the current font 
2192                  * That is what a user expects! */
2193                 current_font = rawtmpfont;
2194                 real_current_font = realtmpfont;
2195         }
2196         SetCursorIntern(cursor.par, cursor.pos);
2197 }
2198
2199 /* table stuff -- end*/
2200
2201
2202 /* just a macro to make some thing easier. */ 
2203 void LyXText::RedoParagraph()
2204 {
2205   LyXCursor tmpcursor = cursor;
2206   ClearSelection();
2207   RedoParagraphs(cursor, cursor.par->Next());;
2208   SetCursorIntern(tmpcursor.par, tmpcursor.pos);
2209 }
2210
2211
2212 /* insert a character, moves all the following breaks in the 
2213  * same Paragraph one to the right and make a rebreak */
2214 void  LyXText::InsertChar(char c)
2215 {
2216         Row *row;
2217         Row *tmprow;
2218         int z;
2219         long y;
2220         bool jumped_over_space;
2221         LyXFont realtmpfont;
2222         LyXFont rawtmpfont;
2223         int lastpos;
2224         LyXFont rawparfont;
2225    
2226         SetUndo(Undo::INSERT, 
2227                 cursor.par->ParFromPos(cursor.pos)->previous, 
2228                 cursor.par->ParFromPos(cursor.pos)->next);
2229
2230         /* When the free-spacing option is set for the current layout,
2231          * all spaces are converted to protected spaces. */
2232         bool freeSpacingBo =
2233                 lyxstyle.Style(parameters->textclass,
2234                                cursor.row->par->GetLayout())->free_spacing;
2235         //
2236         // Is this wanted? There cannot be a line break between protected
2237         // separators. Therefore I suggest the way implemented below
2238         // (Matthias)
2239         //
2240         //   if ( freeSpacingBo && IsLineSeparatorChar(c) )
2241         //         c = LYX_META_PROTECTED_SEPARATOR;
2242    
2243         if (freeSpacingBo && IsLineSeparatorChar(c) 
2244             && (!cursor.pos || cursor.par->IsLineSeparator(cursor.pos-1))) 
2245                 c = LYX_META_PROTECTED_SEPARATOR;
2246    
2247         /* table stuff -- begin*/
2248         if (cursor.par->table) {
2249                 InsertCharInTable(c);
2250                 goto out;
2251         }
2252         /* table stuff -- end*/
2253    
2254         /* first check, if there will be two blanks together or a blank at 
2255          * the beginning of a paragraph. 
2256          * I decided to handle blanks like normal characters, the main 
2257          * difference are the special checks when calculating the row.fill
2258          * (blank does not count at the end of a row) and the check here */ 
2259
2260         // The bug is triggered when we type in a description environment:
2261         // The current_font is not changed when we go from label to main text
2262         // and it should (along with realtmpfont) when we type the space.
2263 #ifdef WITH_WARNINGS
2264 #warning There is a bug here! (Asger)
2265 #endif
2266         
2267         realtmpfont = real_current_font;
2268         rawtmpfont = current_font;  /* store the current font.
2269                                      * This is because of the use
2270                                      * of cursor movements. The moving
2271                                      * cursor would refresh the 
2272                                      * current font */
2273
2274         // Get the font that is used to calculate the baselineskip
2275         lastpos = cursor.par->Last();
2276         rawparfont = cursor.par->GetFontSettings(lastpos - 1);
2277
2278         jumped_over_space = false;
2279    
2280         if (IsLineSeparatorChar(c)) {
2281            
2282                 if (cursor.pos < lastpos
2283                     && cursor.par->IsLineSeparator(cursor.pos)) {
2284                         /* the user inserted a space before a space. So we
2285                          * will just make a CursorRight. BUT: The font of this
2286                          * space should be set to current font. That is why
2287                          * we need to rebreak perhaps. If there is a protected
2288                          * blank at the end of a row we have to force
2289                          * a rebreak.*/ 
2290            
2291                         minibuffer->Set(_("You cannot type two spaces this way.  Please read the Tutorial."));
2292                         if (cursor.pos == RowLast(cursor.row)
2293                             && !IsLineSeparatorChar(c))
2294                                 cursor.row->fill = -1;    /* force rebreak  */
2295            
2296                         cursor.par->Erase(cursor.pos);
2297                         jumped_over_space = true;
2298            
2299                 } else if ((cursor.pos > 0 
2300                             && cursor.par->IsLineSeparator(cursor.pos - 1))
2301                            || (cursor.pos > 0
2302                                && cursor.par->IsNewline(cursor.pos - 1))
2303                            || (cursor.pos == 0
2304                                && !(cursor.par->Previous()
2305                                     && cursor.par->Previous()->footnoteflag
2306                                     == LyXParagraph::OPEN_FOOTNOTE))) {
2307                         if (cursor.pos == 0 )
2308                                 minibuffer->Set(_("You cannot insert a space at the beginning of a paragraph.  Please read the Tutorial."));
2309                         else
2310                                 minibuffer->Set(_("You cannot type two spaces this way.  Please read the Tutorial."));
2311                         goto out;
2312                 }
2313         } else if (IsNewlineChar(c)) {
2314                 if (cursor.par->FirstPhysicalPar() == cursor.par
2315                     && cursor.pos <= BeginningOfMainBody(cursor.par))
2316                         goto out;
2317                 /* no newline at first position 
2318                  * of a paragraph or behind labels. 
2319                  * TeX does not allow that. */
2320                 
2321                 if (cursor.pos < cursor.par->Last() &&
2322                     cursor.par->IsLineSeparator(cursor.pos))
2323                         CursorRightIntern(); // newline always after a blank!
2324                 cursor.row->fill = -1;         // to force a new break
2325         }
2326    
2327         /* the display inset stuff */ 
2328         if (cursor.row->par->GetChar(cursor.row->pos) == LYX_META_INSET
2329             && cursor.row->par->GetInset(cursor.row->pos)
2330             && cursor.row->par->GetInset(cursor.row->pos)->Display())
2331                 cursor.row->fill = -1; // to force a new break  
2332
2333         /* get the cursor row fist */
2334         /* this is a dumb solution, i will try to hold the cursor.row
2335            in future */ 
2336         /* row = GetRow(cursor.par, cursor.pos, y);*/
2337         /* ok, heres a better way: */ 
2338         row = cursor.row;
2339         y = cursor.y - row->baseline;
2340         if (c != LYX_META_INSET)  /* in this case LyXText::InsertInset 
2341                                    * already insertet the character */
2342                 cursor.par->InsertChar(cursor.pos, c);
2343         SetCharFont(cursor.par, cursor.pos, rawtmpfont);
2344
2345         if (!jumped_over_space) {
2346                 /* refresh the positions */
2347                 tmprow = row;
2348                 while (tmprow->next && tmprow->next->par == row->par) {
2349                         tmprow = tmprow->next;
2350                         tmprow->pos++;
2351                 }
2352         }
2353    
2354         /* Is there a break one row above */ 
2355         if ((cursor.par->IsLineSeparator(cursor.pos)
2356              || cursor.par->IsNewline(cursor.pos)
2357              || cursor.row->fill == -1)
2358             && row->previous && row->previous->par == row->par) {
2359                 z = NextBreakPoint(row->previous, paperwidth);
2360                 if ( z >= row->pos) {
2361                         row->pos = z + 1;
2362                         
2363                         /* set the dimensions of the row above  */ 
2364                         row->previous->fill = Fill(row->previous, paperwidth);
2365
2366                         SetHeightOfRow(row->previous);
2367              
2368                         y -= row->previous->height;
2369                         refresh_y = y;
2370                         refresh_row = row->previous;
2371                         status = LyXText::NEED_MORE_REFRESH;
2372              
2373                         BreakAgainOneRow(row);
2374                         SetCursor(cursor.par, cursor.pos + 1);
2375                         /* cursor MUST be in row now */
2376              
2377                         if (row->next && row->next->par == row->par)
2378                                 need_break_row = row->next;
2379                         else
2380                                 need_break_row = NULL;
2381
2382                         current_font = rawtmpfont;
2383                         real_current_font = realtmpfont;
2384              
2385                         // check, wether the last characters font has changed. 
2386                         if (cursor.pos && cursor.pos == cursor.par->Last()
2387                             && rawparfont != rawtmpfont)
2388                                 RedoHeightOfParagraph(cursor);
2389                         
2390                         goto out;
2391                 }
2392         }
2393    
2394         /* recalculate the fill of the row */ 
2395         if (row->fill >= 0)  /* needed because a newline
2396                               * will set fill to -1. Otherwise
2397                               * we would not get a rebreak! */
2398                 row->fill = Fill(row, paperwidth);
2399         if (row->fill < 0 ) {
2400                 refresh_y = y;
2401                 refresh_row = row; 
2402                 refresh_x = cursor.x;
2403                 refresh_pos = cursor.pos;
2404                 status = LyXText::NEED_MORE_REFRESH;
2405                 BreakAgainOneRow(row); 
2406                 /* will the cursor be in another row now? */ 
2407                 if (RowLast(row) <= cursor.pos + 1 && row->next) {
2408                         if (row->next && row->next->par == row->par)
2409                                 /* this should
2410                                  * always be true */
2411                                 row = row->next;
2412                         BreakAgainOneRow(row);
2413                 }
2414                 SetCursor(cursor.par, cursor.pos + 1);
2415                 if (row->next && row->next->par == row->par)
2416                         need_break_row = row->next;
2417                 else
2418                         need_break_row = NULL;
2419                 
2420                 current_font = rawtmpfont;
2421                 real_current_font = realtmpfont;
2422         } else {
2423                 refresh_y = y;
2424                 refresh_x = cursor.x;
2425                 refresh_row = row;
2426                 refresh_pos = cursor.pos;
2427                 
2428                 int tmpheight = row->height;
2429                 SetHeightOfRow(row);
2430                 if (tmpheight == row->height)
2431                         status = LyXText::NEED_VERY_LITTLE_REFRESH;
2432                 else
2433                         status = LyXText::NEED_MORE_REFRESH;
2434             
2435                 SetCursor(cursor.par, cursor.pos + 1);
2436                 current_font = rawtmpfont;
2437                 real_current_font = realtmpfont;
2438         }
2439
2440         /* check, wether the last characters font has changed. */ 
2441         if (cursor.pos && cursor.pos == cursor.par->Last()
2442             && rawparfont != rawtmpfont) {
2443                 RedoHeightOfParagraph(cursor);
2444         } else {
2445                 /* now the special right address boxes */
2446                 if (lyxstyle.Style(parameters->textclass,
2447                                    cursor.par->GetLayout())->margintype
2448                     == MARGIN_RIGHT_ADDRESS_BOX) {
2449                         RedoDrawingOfParagraph(cursor); 
2450                 }
2451         }
2452   out:
2453
2454         // Here we could call FinishUndo for every 20 characters inserted.
2455         // This is from my experience how emacs does it.
2456         static unsigned short counter = 0;
2457         if (counter < 20) {
2458                 ++counter;
2459         } else {
2460                 FinishUndo();
2461                 counter = 0;
2462         }
2463         return;
2464 }
2465    
2466    
2467 void LyXText::PrepareToPrint(Row *row, float &x, float &fill_separator, 
2468                              float &fill_hfill, float &fill_label_hfill)
2469 {
2470         float w, nh, nlh, ns;
2471         
2472         w = row->fill;
2473         fill_hfill = 0;
2474         fill_label_hfill = 0;
2475         fill_separator = 0;
2476         fill_label_hfill = 0;
2477         
2478         x = LeftMargin(row);
2479         
2480         /* is there a manual margin with a manual label */ 
2481         if (lyxstyle.Style(parameters->textclass,
2482                            row->par->GetLayout())->margintype == MARGIN_MANUAL
2483             && lyxstyle.Style(parameters->textclass,
2484                               row->par->GetLayout())->labeltype == LABEL_MANUAL) {
2485                
2486                 nlh = NumberOfLabelHfills(row) + 1; /* one more since labels 
2487                                                     * are left aligned*/ 
2488                 if (nlh && !row->par->GetLabelWidthString().empty()) {
2489                         fill_label_hfill = LabelFill(row) / nlh;
2490                 }
2491         }
2492                 
2493         /* are there any hfills in the row? */ 
2494         nh = NumberOfHfills(row);
2495         
2496 /* table stuff -- begin*/
2497         if (row->par->table) {
2498            w = paperwidth - row->par->table->WidthOfTable()
2499            - x - RightMargin(row);
2500            nh = 0; /* ignore hfills in tables */ 
2501         }
2502 /* table stuff -- end*/
2503
2504         if (nh)
2505           fill_hfill = w /nh;
2506         else  {
2507            /* is it block, flushleft or flushright? 
2508             * set x how you need it */
2509            int align;
2510            if (row->par->FirstPhysicalPar()->align == LYX_ALIGN_LAYOUT)
2511              align = lyxstyle.Style(parameters->textclass, row->par->GetLayout())->align;
2512            else
2513              align = row->par->FirstPhysicalPar()->align;
2514            
2515            /* center displayed insets */ 
2516            if (row->par->GetChar(row->pos) == LYX_META_INSET
2517                && row->par->GetInset(row->pos)
2518                && row->par->GetInset(row->pos)->Display())
2519              align = LYX_ALIGN_CENTER;
2520
2521            switch (align) {
2522             case LYX_ALIGN_BLOCK:
2523               ns = NumberOfSeparators(row);
2524               if (ns && row->next && row->next->par == row->par &&
2525                   !(row->next->par->IsNewline(row->next->pos-1))
2526                   && !(row->next->par->GetChar(row->next->pos) == LYX_META_INSET
2527                        && row->next->par->GetInset(row->next->pos)
2528                        && row->next->par->GetInset(row->next->pos)->Display())
2529                   )
2530                 fill_separator = w / ns;
2531               break;
2532             case LYX_ALIGN_RIGHT:
2533               x += w;
2534               break;
2535             case LYX_ALIGN_CENTER:
2536               x += w / 2;
2537               break;
2538            }
2539         }
2540      }
2541
2542       
2543 /* important for the screen */
2544
2545
2546 /* the cursor set functions have a special mechanism. When they
2547 * realize, that you left an empty paragraph, they will delete it.
2548 * They also delete the corresponding row */
2549
2550 void LyXText::CursorRightOneWord()
2551 {
2552         // treat floats, HFills and Insets as words
2553         LyXCursor tmpcursor = cursor;
2554
2555         if (tmpcursor.pos == tmpcursor.par->Last()
2556             && tmpcursor.par->Next())
2557         {
2558                         tmpcursor.par = tmpcursor.par->Next();
2559                         tmpcursor.pos = 0;
2560         } else {
2561                 int steps = 0;
2562
2563                 // Skip through initial nonword stuff.
2564                 while ( tmpcursor.pos < tmpcursor.par->Last() &&
2565                         ! tmpcursor.par->IsWord( tmpcursor.pos ) ) 
2566                 {
2567                   //    printf("Current pos1 %d",tmpcursor.pos) ;
2568                         tmpcursor.pos++;
2569                         steps++;
2570                 }
2571                 // Advance through word.
2572                 while ( tmpcursor.pos < tmpcursor.par->Last() &&
2573                         tmpcursor.par->IsWord( tmpcursor.pos ) )
2574                 {
2575                   //     printf("Current pos2 %d",tmpcursor.pos) ;
2576                         tmpcursor.pos++;
2577                         steps++;
2578                 }
2579         }
2580         SetCursor(tmpcursor.par, tmpcursor.pos);
2581 }
2582
2583
2584 void LyXText::CursorTab()
2585 {
2586     if (cursor.par->table) {
2587         int cell = NumberOfCell(cursor.par, cursor.pos);
2588         while(cursor.par->table->IsContRow(cell)) {
2589             CursorUp();
2590             cell = NumberOfCell(cursor.par, cursor.pos);
2591         }
2592         if (cursor.par->table->ShouldBeVeryLastCell(cell))
2593             TableFeatures(LyXTable::APPEND_ROW);
2594     }
2595     LyXCursor tmpcursor = cursor;
2596     while (tmpcursor.pos < tmpcursor.par->Last()
2597            && !tmpcursor.par->IsNewline(tmpcursor.pos))
2598         tmpcursor.pos++;
2599    
2600     if (tmpcursor.pos == tmpcursor.par->Last()){
2601         if (tmpcursor.par->Next()) {
2602             tmpcursor.par = tmpcursor.par->Next();
2603             tmpcursor.pos = 0;
2604         }
2605     }
2606     else
2607         tmpcursor.pos++;
2608     SetCursor(tmpcursor.par, tmpcursor.pos);
2609     if (cursor.par->table) {
2610         int cell = NumberOfCell(cursor.par, cursor.pos);
2611         while (cursor.par->table->IsContRow(cell) &&
2612                !cursor.par->table->ShouldBeVeryLastCell(cell)) {
2613             tmpcursor = cursor;
2614             while (tmpcursor.pos < tmpcursor.par->Last()
2615                    && !tmpcursor.par->IsNewline(tmpcursor.pos))
2616                 tmpcursor.pos++;
2617    
2618             if (tmpcursor.pos == tmpcursor.par->Last()){
2619                 if (tmpcursor.par->Next()) {
2620                     tmpcursor.par = tmpcursor.par->Next();
2621                     tmpcursor.pos = 0;
2622                 }
2623             }
2624             else
2625                 tmpcursor.pos++;
2626             SetCursor(tmpcursor.par, tmpcursor.pos);
2627             cell = NumberOfCell(cursor.par, cursor.pos);
2628         }
2629     }
2630 }
2631
2632
2633 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2634             of prior word, not to end of next prior word. */
2635
2636 void LyXText::CursorLeftOneWord() 
2637 {
2638         // treat HFills, floats and Insets as words
2639         LyXCursor tmpcursor = cursor;
2640         while (tmpcursor.pos 
2641                && (tmpcursor.par->IsSeparator(tmpcursor.pos - 1) 
2642                    || tmpcursor.par->IsKomma(tmpcursor.pos - 1))
2643                && !(tmpcursor.par->IsHfill(tmpcursor.pos - 1)
2644                     || tmpcursor.par->IsFloat(tmpcursor.pos - 1)
2645                     || tmpcursor.par->IsInset(tmpcursor.pos - 1)))
2646                 tmpcursor.pos--;
2647
2648         if (tmpcursor.pos
2649             && (tmpcursor.par->IsInset(tmpcursor.pos - 1)
2650                 || tmpcursor.par->IsFloat(tmpcursor.pos - 1)
2651                 || tmpcursor.par->IsHfill(tmpcursor.pos - 1))) {
2652                 tmpcursor.pos--;
2653         } else if (!tmpcursor.pos) {
2654                 if (tmpcursor.par->Previous()){
2655                         tmpcursor.par = tmpcursor.par->Previous();
2656                         tmpcursor.pos = tmpcursor.par->Last();
2657                 }
2658         } else {                // Here, tmpcursor != 0 
2659                 while (tmpcursor.pos > 0 &&
2660                        tmpcursor.par->IsWord(tmpcursor.pos-1) )
2661                         tmpcursor.pos-- ;
2662         }
2663         SetCursor(tmpcursor.par, tmpcursor.pos);
2664 }
2665
2666 /* -------> Select current word. This depends on behaviour of CursorLeftOneWord(), so it is
2667                         patched as well. */
2668
2669 void LyXText::SelectWord() 
2670 {
2671         /* Move cursor to the beginning, when not already there. */
2672         if ( cursor.pos
2673              && !cursor.par->IsSeparator(cursor.pos-1)
2674              && !cursor.par->IsKomma(cursor.pos-1) )
2675                 CursorLeftOneWord();
2676
2677         /* set the sel cursor */
2678         sel_cursor = cursor;
2679
2680         while ( cursor.pos < cursor.par->Last()
2681                         && !cursor.par->IsSeparator(cursor.pos)
2682                         && !cursor.par->IsKomma(cursor.pos) )
2683                 cursor.pos++;
2684         SetCursor( cursor.par, cursor.pos );
2685         
2686         /* finally set the selection */ 
2687         SetSelection();
2688 }
2689
2690
2691 /* -------> Select the word currently under the cursor when:
2692                         1: no selection is currently set,
2693                         2: the cursor is not at the borders of the word. */
2694
2695 int LyXText::SelectWordWhenUnderCursor() 
2696 {
2697         if ( selection ) return 0;
2698         if ( cursor.pos < cursor.par->Last()
2699                  && !cursor.par->IsSeparator(cursor.pos)
2700                  && !cursor.par->IsKomma(cursor.pos)
2701                  && cursor.pos 
2702                  && !cursor.par->IsSeparator(cursor.pos -1)
2703                  && !cursor.par->IsKomma(cursor.pos -1) ) {
2704                 SelectWord();
2705                 return 1;
2706         }
2707         return 0;
2708 }
2709
2710
2711 // This function is only used by the spellchecker for NextWord().
2712 // It doesn't handle LYX_ACCENTs and probably never will.
2713 char* LyXText::SelectNextWord(float &value)
2714 {
2715         LyXParagraph* tmppar = cursor.par;
2716         
2717         // If this is not the very first word, skip rest of
2718         // current word because we are probably in the middle
2719         // of a word if there is text here.
2720         if (cursor.pos || cursor.par->previous) {
2721                 while (cursor.pos < cursor.par->Last()
2722                        && cursor.par->IsLetter(cursor.pos))
2723                         cursor.pos++;
2724         }
2725         // Now, skip until we have real text (will jump paragraphs)
2726         while ((cursor.par->Last() > cursor.pos
2727                 && (!cursor.par->IsLetter(cursor.pos)
2728                     || cursor.par->getFont(cursor.pos).latex() == LyXFont::ON))
2729                || (cursor.par->Last() == cursor.pos
2730                    && cursor.par->Next())){
2731                 if (cursor.pos == cursor.par->Last()) {
2732                         cursor.par = cursor.par->Next();
2733                         cursor.pos = 0;
2734                 }
2735                 else
2736                         cursor.pos++;
2737         }
2738   
2739         // Update the value if we changed paragraphs
2740         if (cursor.par != tmppar){
2741                 SetCursor(cursor.par, cursor.pos);
2742                 value = float(cursor.y)/float(height);
2743         }
2744
2745         /* Start the selection from here */
2746         sel_cursor = cursor;
2747
2748         LString latex;
2749    
2750         /* and find the end of the word 
2751            (optional hyphens are part of a word) */
2752         while (cursor.pos < cursor.par->Last()
2753                && (cursor.par->IsLetter(cursor.pos)) 
2754                    || (cursor.par->GetChar(cursor.pos) == LYX_META_INSET &&
2755                        cursor.par->GetInset(cursor.pos) != 0 &&
2756                        cursor.par->GetInset(cursor.pos)->Latex(latex,0)==0 &&
2757                        latex == "\\-"))
2758                 cursor.pos++;
2759
2760         // Finally, we copy the word to a string and return it
2761         char* string = NULL;
2762
2763         if (sel_cursor.pos < cursor.pos) {
2764                 string = new char [cursor.pos - sel_cursor.pos + 2];
2765                 int i,j;
2766                 
2767                 for (i=sel_cursor.pos, j=0; i<cursor.pos; i++) {
2768                         if (cursor.par->GetChar(i) != LYX_META_INSET)
2769                                 string[j++] = cursor.par->GetChar(i);
2770                 }
2771                 string[j] = '\0';
2772         }
2773         return string;
2774 }
2775
2776
2777 // This one is also only for the spellchecker
2778 void LyXText::SelectSelectedWord()
2779 {
2780         /* move cursor to the beginning */
2781         SetCursor(sel_cursor.par, sel_cursor.pos);
2782         
2783         /* set the sel cursor */
2784         sel_cursor = cursor;
2785
2786         LString latex;
2787         
2788         /* now find the end of the word */
2789         while (cursor.pos < cursor.par->Last()
2790                && (cursor.par->IsLetter(cursor.pos)
2791                    || (cursor.par->GetChar(cursor.pos) == LYX_META_INSET &&
2792                        cursor.par->GetInset(cursor.pos) != 0 &&
2793                        cursor.par->GetInset(cursor.pos)->Latex(latex,0)==0 &&
2794                        latex == "\\-")))
2795                 cursor.pos++;
2796         
2797         SetCursor(cursor.par, cursor.pos);
2798         
2799         /* finally set the selection */ 
2800         SetSelection();
2801 }
2802
2803
2804 /* -------> Delete from cursor up to the end of the current or next word. */
2805 void LyXText::DeleteWordForward()
2806 {
2807         LyXCursor tmpcursor = cursor;
2808         
2809         if (!cursor.par->Last())
2810                 CursorRight();
2811         else {
2812                 /* -------> Skip initial non-word stuff. */
2813                 while ( cursor.pos < cursor.par->Last() 
2814                         && (cursor.par->IsSeparator(cursor.pos)
2815                             || cursor.par->IsKomma(cursor.pos)) )
2816                         cursor.pos++;
2817                 
2818                 SetCursorIntern(cursor.par, cursor.pos);
2819                 selection = True; // to avoid deletion 
2820                 CursorRightOneWord();
2821                 sel_cursor = cursor;
2822                 cursor = tmpcursor;
2823                 SetSelection(); 
2824                 
2825                 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2826                 CutSelection();
2827         }
2828 }
2829
2830
2831 /* -------> Delete from cursor to start of current or prior word. */
2832 void LyXText::DeleteWordBackward()
2833 {
2834        LyXCursor tmpcursor = cursor;
2835        if (!cursor.par->Last())
2836          CursorLeft();
2837        else{
2838          selection = True; // to avoid deletion 
2839          CursorLeftOneWord();
2840          sel_cursor = cursor;
2841          cursor = tmpcursor;
2842          SetSelection();
2843          CutSelection();
2844        }
2845 }
2846
2847
2848 /* -------> Kill to end of line. */
2849 void LyXText::DeleteLineForward()
2850 {
2851         LyXCursor tmpcursor = cursor;
2852         if (!cursor.par->Last())
2853                 CursorRight();
2854         else {
2855                 CursorEnd();
2856                 sel_cursor = cursor;
2857                 cursor = tmpcursor;
2858                 SetSelection();
2859                 if (selection == false) {
2860                         DeleteWordForward();
2861                 } else {
2862                         CutSelection();
2863                 }
2864         }
2865 }
2866
2867 // Change the case of a word at cursor position. The meaning of action
2868 // is:
2869 // 0  change to lowercase
2870 // 1  capitalize word
2871 // 2  change to uppercase
2872 // This function directly manipulates LyXParagraph::text because there
2873 // is no LyXParagraph::SetChar currently. I did what I could to ensure
2874 // that it is correct. I guess part of it should be moved to
2875 // LyXParagraph, but it will have to change for 1.1 anyway. At least
2876 // it does not access outside of the allocated array as the older
2877 // version did. (JMarc) 
2878 void LyXText::ChangeWordCase(int action) 
2879 {
2880         LyXParagraph *tmppar = cursor.par->ParFromPos(cursor.pos);
2881         int tmppos = cursor.par->PositionInParFromPos(cursor.pos);
2882
2883         SetUndo(Undo::FINISH, tmppar->previous, tmppar->next); 
2884
2885         while (tmppos < tmppar->last) {
2886                 unsigned char c = tmppar->text[tmppos];
2887                 if (IsKommaChar(c) || IsLineSeparatorChar(c))
2888                         break;
2889                 if (c != LYX_META_INSET) {
2890                         switch (action) {
2891                         case 0:
2892                                 c = tolower(c);
2893                                 break;
2894                         case 1:
2895                                 c = toupper(c);
2896                                 action = 0;
2897                                 break;
2898                         case 2:
2899                                 c = toupper(c);
2900                                 break;
2901                         }
2902                 }
2903                 
2904                 tmppar->text[tmppos] = c;
2905                 tmppos++;
2906         }
2907         CheckParagraph(tmppar, tmppos);
2908         CursorRightOneWord();
2909 }
2910
2911
2912 void  LyXText::Delete()
2913 {
2914    LyXCursor old_cursor = cursor;
2915    LyXCursor tmpcursor;
2916    /* this is a very easy implementation*/ 
2917    
2918    /* just move to the right */ 
2919    CursorRightIntern();
2920    
2921    if (cursor.par->previous == old_cursor.par->previous
2922        && cursor.par != old_cursor.par)
2923      return; // delete-emty-paragraph-mechanism has done it
2924    
2925    /* if you had success make a backspace */ 
2926    if (old_cursor.par != cursor.par || old_cursor.pos != cursor.pos) {
2927      tmpcursor = cursor;
2928      cursor = old_cursor; // to make sure undo gets the right cursor position
2929      SetUndo(Undo::DELETE, 
2930              cursor.par->ParFromPos(cursor.pos)->previous, 
2931              cursor.par->ParFromPos(cursor.pos)->next); 
2932      cursor = tmpcursor;
2933      Backspace();
2934    }
2935 }
2936
2937
2938 void  LyXText::Backspace()
2939 {
2940         LyXParagraph *tmppar;
2941         Row *tmprow, *row;
2942         long y;
2943         int tmpheight;
2944
2945         /* table stuff -- begin*/
2946    
2947         if (cursor.par->table) {
2948                 BackspaceInTable();
2949                 return;
2950         }
2951         /* table stuff -- end*/
2952         
2953         LyXFont rawtmpfont = current_font;
2954         LyXFont realtmpfont = real_current_font;
2955    
2956         // Get the font that is used to calculate the baselineskip
2957         int const lastpos = cursor.par->Last();
2958         LyXFont rawparfont = cursor.par->GetFontSettings(lastpos - 1);
2959
2960         if (cursor.pos == 0) {
2961                 /* we may paste some paragraphs */
2962       
2963                 /* is it an empty paragraph? */
2964       
2965                 if ((lastpos == 0
2966                      || (lastpos == 1 && cursor.par->IsSeparator(0)))
2967                     && !(cursor.par->Next() 
2968                          && cursor.par->footnoteflag ==
2969                          LyXParagraph::NO_FOOTNOTE
2970                          && cursor.par->Next()->footnoteflag ==
2971                          LyXParagraph::OPEN_FOOTNOTE)) {
2972                         
2973                         if (cursor.par->previous) {
2974                                 tmppar = cursor.par->previous->FirstPhysicalPar();
2975                                 if (cursor.par->GetLayout() == tmppar->GetLayout()
2976                                     && cursor.par->footnoteflag == tmppar->footnoteflag
2977                                     && cursor.par->GetAlign() == tmppar->GetAlign()) {
2978                                         
2979                                         tmppar->line_bottom = cursor.par->line_bottom;
2980                                         tmppar->added_space_bottom = cursor.par->added_space_bottom;
2981                                         tmppar->pagebreak_bottom = cursor.par->pagebreak_bottom;
2982                                 }
2983                                 
2984                                 CursorLeftIntern();
2985                      
2986                                 /* the layout things can change the height of a row ! */ 
2987                                 tmpheight = cursor.row->height;
2988                                 SetHeightOfRow(cursor.row);
2989                                 if (cursor.row->height != tmpheight) {
2990                                         refresh_y = cursor.y - cursor.row->baseline;
2991                                         refresh_row = cursor.row;
2992                                         status = LyXText::NEED_MORE_REFRESH;
2993                                 }
2994                                 return;
2995                         }
2996                 }
2997                 if (cursor.par->ParFromPos(cursor.pos)->previous){
2998                         SetUndo(Undo::DELETE, 
2999                                 cursor.par->ParFromPos(cursor.pos)->previous->previous, 
3000                                 cursor.par->ParFromPos(cursor.pos)->next); 
3001                 }
3002                 tmppar = cursor.par;
3003                 tmprow = cursor.row;
3004                 CursorLeftIntern();
3005                 /* Pasting is not allowed, if the paragraphs have different layout.
3006                  * I think it is a real bug of all other word processors to allow
3007                  * it. It confuses the user. Even so with a footnote paragraph and
3008                  * a non-footnote paragraph. I will not allow pasting in this case, 
3009                  * because the user would be confused if the footnote behaves 
3010                  * different wether it is open or closed.
3011                  * 
3012                  * Correction: Pasting is always allowed with standard-layout */
3013                 if (cursor.par != tmppar
3014                     && (cursor.par->GetLayout() == tmppar->GetLayout()
3015                         || !tmppar->GetLayout())
3016                     && cursor.par->footnoteflag == tmppar->footnoteflag
3017                     /* table stuff -- begin*/
3018                     && !cursor.par->table /* no pasting of tables */ 
3019                     /* table stuff -- end*/
3020                     && cursor.par->GetAlign() == tmppar->GetAlign()) {
3021                         
3022                         cursor.par->PasteParagraph();
3023                         
3024                         if (!(cursor.pos &&
3025                               cursor.par->IsSeparator(cursor.pos - 1)))
3026                                 cursor.par->InsertChar(cursor.pos, ' ');
3027                         else
3028                                 if (cursor.pos)
3029                                         cursor.pos--;
3030                         
3031                         status = LyXText::NEED_MORE_REFRESH;
3032                         refresh_row = cursor.row;
3033                         refresh_y = cursor.y - cursor.row->baseline;
3034                         
3035                         /* remove the lost paragraph */
3036                         RemoveParagraph(tmprow);
3037                         RemoveRow(tmprow);  
3038                         
3039                         /* break the paragraph again */ 
3040                         /* BreakAgain(cursor.row); */ 
3041                         
3042                         AppendParagraph(cursor.row);
3043                         UpdateCounters(cursor.row);
3044                         
3045                         /* the row may have changed, block, hfills etc. */ 
3046                         SetCursor(cursor.par, cursor.pos);
3047                 }
3048         } else  {
3049                 /* this is the code for a normal backspace, not pasting
3050                  * any paragraphs */ 
3051                 SetUndo(Undo::DELETE, 
3052                         cursor.par->ParFromPos(cursor.pos)->previous, 
3053                         cursor.par->ParFromPos(cursor.pos)->next); 
3054                 CursorLeftIntern();
3055                 
3056                 /* some insets are undeletable here */
3057                 if (cursor.par->GetChar(cursor.pos)==LYX_META_INSET) {
3058                         if (!cursor.par->GetInset(cursor.pos)->Deletable())
3059                                 return; 
3060                         /* force complete redo when erasing display insets */ 
3061                         /* this is a cruel mathod but save..... Matthias */ 
3062                         if (cursor.par->GetInset(cursor.pos)->Display()){
3063                                 cursor.par->Erase(cursor.pos);
3064                                 RedoParagraph();
3065                                 return;
3066                         }
3067                 }
3068                 
3069                 row = cursor.row;
3070                 y = cursor.y - row->baseline;
3071                 int z;
3072                 
3073                 /* remember that a space at the end of a row doesnt count
3074                  * when calculating the fill */ 
3075                 if (cursor.pos < RowLast(row) ||
3076                     !cursor.par->IsLineSeparator(cursor.pos)) {
3077                         row->fill += SingleWidth(cursor.par, cursor.pos);
3078                 }
3079                 
3080                 /* some special code when deleting a newline. This is similar
3081                  * to the behavior when pasting paragraphs */ 
3082                 if (cursor.pos && cursor.par->IsNewline(cursor.pos)) {
3083                         cursor.par->Erase(cursor.pos);
3084                         /* refresh the positions */
3085                         tmprow = row;
3086                         while (tmprow->next && tmprow->next->par == row->par) {
3087                                 tmprow = tmprow->next;
3088                                 tmprow->pos--;
3089                         }
3090                         if (cursor.par->IsLineSeparator(cursor.pos - 1))
3091                                 cursor.pos--;
3092                         
3093                         if (cursor.pos < cursor.par->Last() && !cursor.par->IsSeparator(cursor.pos)) {
3094                                 cursor.par->InsertChar(cursor.pos, ' ');
3095                                 /* refresh the positions */
3096                                 tmprow = row;
3097                                 while (tmprow->next && tmprow->next->par == row->par) {
3098                                         tmprow = tmprow->next;
3099                                         tmprow->pos++;
3100                                 }
3101                         }
3102                 } else {
3103                         cursor.par->Erase(cursor.pos);
3104                         
3105                         /* refresh the positions */
3106                         tmprow = row;
3107                         while (tmprow->next && tmprow->next->par == row->par) {
3108                                 tmprow = tmprow->next;
3109                                 tmprow->pos--;
3110                         }
3111                         
3112                         /* delete superfluous blanks */ 
3113                         if (cursor.pos < cursor.par->Last() - 1 &&
3114                             (cursor.par->IsLineSeparator(cursor.pos))) {
3115                                 
3116                                 if (cursor.pos == BeginningOfMainBody(cursor.par)
3117                                     || !cursor.pos 
3118                                     || cursor.par->IsLineSeparator(cursor.pos - 1)) {
3119                                         cursor.par->Erase(cursor.pos);
3120                                         /* refresh the positions */
3121                                         tmprow = row;
3122                                         while (tmprow->next && 
3123                                                tmprow->next->par == row->par) {
3124                                                 tmprow = tmprow->next;
3125                                                 tmprow->pos--;
3126                                         }
3127                                         if (cursor.pos)   /* move one character left */
3128                                                 cursor.pos--;
3129                                 }
3130                         }
3131                         
3132                         /* delete newlines at the beginning of paragraphs */ 
3133                         while (cursor.par->Last() &&
3134                                cursor.par->IsNewline(cursor.pos) &&
3135                                cursor.pos == BeginningOfMainBody(cursor.par)
3136                                 ) {
3137                                 cursor.par->Erase(cursor.pos);
3138                                 /* refresh the positions */
3139                                 tmprow = row;
3140                                 while (tmprow->next && 
3141                                        tmprow->next->par == row->par) {
3142                                         tmprow = tmprow->next;
3143                                         tmprow->pos--;
3144                                 }
3145                         }
3146                 }
3147                 
3148                 /* is there a break one row above */ 
3149                 if (row->previous && row->previous->par == row->par) {
3150                         z = NextBreakPoint(row->previous, paperwidth);
3151                         if ( z >= row->pos) {
3152                                 row->pos = z + 1;
3153                                 
3154                                 tmprow = row->previous;
3155                                 
3156                                 /* maybe the current row is now empty */ 
3157                                 if (row->pos >= row->par->Last()) {
3158                                         /* remove it */ 
3159                                         RemoveRow(row);
3160                                         need_break_row = NULL;
3161                                 }
3162                                 else  {
3163                                         BreakAgainOneRow(row);
3164                                         if (row->next && row->next->par == row->par)
3165                                                 need_break_row = row->next;
3166                                         else
3167                                                 need_break_row = NULL;
3168                                 }
3169                                 
3170                                 /* set the dimensions of the row above  */ 
3171                                 y -= tmprow->height;
3172                                 tmprow->fill = Fill(tmprow, paperwidth);
3173                                 SetHeightOfRow(tmprow);
3174                                 
3175                                 refresh_y = y;
3176                                 refresh_row = tmprow;
3177                                 status = LyXText::NEED_MORE_REFRESH;
3178                                 SetCursor(cursor.par, cursor.pos);
3179                                 current_font = rawtmpfont;
3180                                 real_current_font = realtmpfont;
3181                                 /* check, whether the last character's font has changed. */
3182                                 rawtmpfont = cursor.par->GetFontSettings(cursor.par->Last() - 1);
3183                                 if (rawparfont != rawtmpfont)
3184                                         RedoHeightOfParagraph(cursor);
3185                                 return;
3186                         }
3187                 }
3188                 
3189                 /* break the cursor row again */ 
3190                 z = NextBreakPoint(row, paperwidth);
3191                 
3192                 if ( z != RowLast(row) || 
3193                      (row->next && row->next->par == row->par &&
3194                       RowLast(row) == row->par->Last() - 1)){
3195                         
3196                         /* it can happen that a paragraph loses one row
3197                          * without a real breakup. This is when a word
3198                          * is to long to be broken. Well, I don t care this 
3199                          * hack ;-) */ 
3200                         if (row->next && row->next->par == row->par &&
3201                             RowLast(row) == row->par->Last() - 1)
3202                                 RemoveRow(row->next);
3203                         
3204                         refresh_y = y;
3205                         refresh_row = row;
3206                         status = LyXText::NEED_MORE_REFRESH;
3207                         
3208                         BreakAgainOneRow(row);
3209                         
3210                         SetCursor(cursor.par, cursor.pos);
3211                         /* cursor MUST be in row now */
3212                         
3213                         if (row->next && row->next->par == row->par)
3214                                 need_break_row = row->next;
3215                         else
3216                                 need_break_row = NULL;
3217                 } else  {
3218                         /* set the dimensions of the row */ 
3219                         row->fill = Fill(row, paperwidth);
3220                         int tmpheight = row->height;
3221                         SetHeightOfRow(row);
3222                         if (tmpheight == row->height)
3223                                 status = LyXText::NEED_VERY_LITTLE_REFRESH;
3224                         else
3225                                 status = LyXText::NEED_MORE_REFRESH;
3226                         refresh_y = y;
3227                         refresh_row = row;
3228                         SetCursor(cursor.par, cursor.pos);
3229                 }
3230         }
3231    
3232         /* restore the current font 
3233          * That is what a user expects! */
3234         current_font = rawtmpfont; 
3235         real_current_font = realtmpfont;
3236         
3237         /* check, wether the last characters font has changed. */
3238         rawtmpfont = cursor.par->GetFontSettings(cursor.par->Last() - 1);
3239         if (rawparfont != rawtmpfont) {
3240                 RedoHeightOfParagraph(cursor);
3241         } else {
3242                 /* now the special right address boxes */
3243                 if (lyxstyle.Style(parameters->textclass,
3244                                    cursor.par->GetLayout())->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
3245                         RedoDrawingOfParagraph(cursor); 
3246                 }
3247         }
3248 }
3249
3250
3251 void LyXText::GetVisibleRow(LyXScreen &scr, int offset, 
3252                             Row *row_ptr, long y)
3253 {
3254         /* returns a printed row */
3255         
3256         int pos, pos_end;
3257         float x, tmpx;
3258         int y_top, y_bottom;
3259         float fill_separator, fill_hfill, fill_label_hfill;
3260         LyXParagraph *par, *firstpar;
3261         int left_margin;
3262         LyXFont font;
3263         int maxdesc;
3264         if (row_ptr->height <= 0) {
3265                 fprintf(stderr, "LYX_ERROR: row.height: %d \n",
3266                         row_ptr->height);
3267                 return;
3268         }
3269         left_margin = LabelEnd(row_ptr);
3270         PrepareToPrint(row_ptr, x, fill_separator,
3271                        fill_hfill, fill_label_hfill);
3272         
3273         int main_body = BeginningOfMainBody(row_ptr->par);
3274         
3275         /* initialize the pixmap */
3276         
3277         scr.fillRectangle(gc_clear,
3278                           0, offset, paperwidth, row_ptr->height);
3279         // check for NOT FAST SELECTION
3280         if (!fast_selection && !mono_video && selection) {
3281                 /* selection code */ 
3282                 if (sel_start_cursor.row == row_ptr &&
3283                     sel_end_cursor.row == row_ptr) {
3284                         scr.fillRectangle(gc_selection, sel_start_cursor.x,
3285                                           offset,
3286                                           sel_end_cursor.x -
3287                                           sel_start_cursor.x,
3288                                           row_ptr->height);
3289                 }
3290                 else if (sel_start_cursor.row == row_ptr) {
3291                         scr.fillRectangle(gc_selection, sel_start_cursor.x,
3292                                           offset,
3293                                           paperwidth - sel_start_cursor.x,
3294                                           row_ptr->height);
3295                 } else if (sel_end_cursor.row == row_ptr) {
3296                         scr.fillRectangle(gc_selection,0, offset,
3297                                           sel_end_cursor.x, row_ptr->height);
3298                 } else if (y > sel_start_cursor.y && y < sel_end_cursor.y) {
3299                         scr.fillRectangle(gc_selection, 0, offset,
3300                                           paperwidth, row_ptr->height);
3301                         
3302                 }
3303         } // end of NOT FAST SELECTION code
3304         
3305         if (row_ptr->par->appendix){
3306           scr.drawVerticalLine(gc_math, 1, offset, offset+row_ptr->height);
3307           scr.drawVerticalLine(gc_math, paperwidth-2 , offset, offset+row_ptr->height);
3308         }
3309
3310         if (row_ptr->par->pextra_type == PEXTRA_MINIPAGE) {
3311                 /* draw a marker at the left margin! */ 
3312                 LyXFont font = GetFont(row_ptr->par, 0);
3313                 int asc = font.maxAscent();
3314                 int x = (LYX_PAPER_MARGIN - font.width('|')) / 2;
3315                 int y1 = (offset + row_ptr->baseline);
3316                 int y2 = (offset + row_ptr->baseline) - asc;
3317
3318                 scr.drawVerticalLine(gc_minipage, x, y1, y2);
3319         }       
3320         if (row_ptr->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
3321                 LyXFont font(LyXFont::ALL_SANE);
3322                 font.setSize(LyXFont::SIZE_FOOTNOTE);
3323                 font.setColor(LyXFont::RED);
3324                 
3325                 int box_x = LYX_PAPER_MARGIN;
3326                 box_x += font.textWidth(" wide-tab ", 10);
3327                 if (row_ptr->previous && 
3328                     row_ptr->previous->par->footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
3329                         LString fs;
3330                         switch (row_ptr->par->footnotekind) {
3331                         case LyXParagraph::MARGIN:
3332                                 fs = " margin";
3333                                 break;
3334                         case LyXParagraph::FIG:
3335                                 fs = " fig";
3336                                 break;
3337                         case LyXParagraph::TAB:
3338                                 fs = " tab";
3339                                 break;
3340                         case LyXParagraph::WIDE_FIG:
3341                                 fs = " wide-fig";
3342                                 break;
3343                         case LyXParagraph::WIDE_TAB:
3344                                 fs = " wide-tab";
3345                                 break;
3346                         case LyXParagraph::ALGORITHM:
3347                                 fs = " alg";
3348                                 break;
3349                         case LyXParagraph::FOOTNOTE:
3350                                 fs = " foot";
3351                                 break;
3352                         }
3353                         
3354                         // Determine background color.
3355                         gc_type back = gc_lighted;
3356                         if (mono_video) {
3357                                 back = gc_clear;
3358                         }
3359                         scr.fillRectangle(back,LYX_PAPER_MARGIN, offset+1,
3360                                           box_x - LYX_PAPER_MARGIN, 
3361                                           int(font.maxAscent())+
3362                                           int(font.maxDescent()));
3363                         
3364                         scr.drawLine(gc_foot,
3365                                      offset,
3366                                      LYX_PAPER_MARGIN,
3367                                      paperwidth - 2*LYX_PAPER_MARGIN);
3368                         
3369                         scr.drawString(font, fs,
3370                                        offset + int(font.maxAscent())+1,
3371                                        LYX_PAPER_MARGIN);
3372                         scr.drawVerticalLine(gc_foot,
3373                                              LYX_PAPER_MARGIN,
3374                                              offset,
3375                                              offset
3376                                              + int(font.maxAscent())+
3377                                              int(font.maxDescent()));
3378                         
3379                         scr.drawLine(gc_foot,
3380                                      offset
3381                                      + int(font.maxAscent())
3382                                      + int(font.maxDescent()) + 1,
3383                                      LYX_PAPER_MARGIN, box_x - LYX_PAPER_MARGIN); 
3384                 }
3385                 
3386                 /* draw the open floats in a red box */
3387                 scr.drawVerticalLine(gc_foot,
3388                                      box_x,
3389                                      offset,  offset + row_ptr->height);
3390                 
3391                 scr.drawVerticalLine(gc_foot,
3392                                      paperwidth - LYX_PAPER_MARGIN,
3393                                      offset,
3394                                      offset + row_ptr->height);
3395                         
3396                 
3397         } else  {
3398                 if (row_ptr->previous &&
3399                     row_ptr->previous->par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
3400                         LyXFont font(LyXFont::ALL_SANE);
3401                         font.setSize(LyXFont::SIZE_FOOTNOTE);
3402                         
3403                         int box_x = LYX_PAPER_MARGIN;
3404                         box_x += font.textWidth(" wide-tab ", 10);
3405                         
3406                         scr.drawLine(gc_foot,
3407                                      offset,
3408                                      box_x,
3409                                      paperwidth - LYX_PAPER_MARGIN - box_x);
3410                 }
3411         }
3412         
3413         LyXLayout* layout = lyxstyle.Style(parameters->textclass,
3414                                            row_ptr->par->GetLayout());
3415         firstpar = row_ptr->par->FirstPhysicalPar();
3416         
3417         y_top = 0;
3418         y_bottom = row_ptr->height;
3419         
3420         /* is it a first row? */ 
3421         if (row_ptr->pos == 0
3422             && row_ptr->par == firstpar) {
3423                 
3424                  /* start of appendix? */
3425                 if (row_ptr->par->start_of_appendix){
3426                   scr.drawLine(gc_math, 
3427                                offset,
3428                                1, paperwidth-2);
3429                 }
3430
3431                 /* think about the margins */ 
3432                 if (!row_ptr->previous)
3433                         y_top += LYX_PAPER_MARGIN;
3434                 
3435                 if (row_ptr->par->pagebreak_top){ /* draw a top pagebreak  */
3436                         scr.drawOnOffLine(offset + y_top + 2 * DefaultHeight(),
3437                                           0, paperwidth);
3438                         y_top += 3 * DefaultHeight();
3439                 }
3440                 
3441                 if (row_ptr->par->added_space_top.kind() == VSpace::VFILL) {
3442                         /* draw a vfill top  */
3443                         scr.drawLine(gc_fill, 
3444                                      offset + 2 + y_top,
3445                                      0, LYX_PAPER_MARGIN);
3446                         scr.drawLine(gc_fill,
3447                                      offset + y_top + 3 * DefaultHeight(),
3448                                      0, LYX_PAPER_MARGIN);
3449                         scr.drawVerticalOnOffLine(LYX_PAPER_MARGIN / 2, 
3450                                                   offset + 2 + y_top,
3451                                                   offset + y_top + 3 *
3452                                                   DefaultHeight());
3453                         
3454                         y_top += 3 * DefaultHeight();
3455                 }
3456                 
3457                 /* think about user added space */ 
3458                 y_top += int(row_ptr->par->added_space_top.inPixels());
3459                 
3460                 /* think about the parskip */ 
3461                 /* some parskips VERY EASY IMPLEMENTATION */ 
3462                 if (parameters->paragraph_separation == LYX_PARSEP_SKIP) {
3463                         if (layout->latextype == LATEX_PARAGRAPH
3464                             && firstpar->GetDepth() == 0
3465                             && firstpar->Previous())
3466                                 y_top += parameters->getDefSkip().inPixels();
3467                         else if (firstpar->Previous()
3468                                  && lyxstyle.Style(parameters->textclass,
3469                                                    firstpar->Previous()->GetLayout())->latextype == LATEX_PARAGRAPH
3470                                  && firstpar->Previous()->GetDepth() == 0)
3471                                 // is it right to use defskip here, too? (AS) 
3472                                 y_top += parameters->getDefSkip().inPixels();
3473                 }
3474                 
3475                 if (row_ptr->par->line_top) {      /* draw a top line  */
3476                         y_top +=  GetFont(row_ptr->par, 0).ascent('x');
3477
3478                         scr.drawThickLine(offset + y_top,
3479                                           0, paperwidth);
3480                         y_top +=  GetFont(row_ptr->par, 0).ascent('x');
3481                 }
3482                 
3483                 /* should we print a label? */ 
3484                 if (layout->labeltype >= LABEL_STATIC
3485                     && (layout->labeltype != LABEL_STATIC
3486                         || layout->latextype != LATEX_ENVIRONMENT
3487                         || row_ptr->par->IsFirstInSequence())) {
3488                         font = GetFont(row_ptr->par, -2);
3489                         if (!row_ptr->par->GetLabelString().empty()) {
3490                                 tmpx = x;
3491                                 LString tmpstring = row_ptr->par->GetLabelString();
3492                                 
3493                                 if (layout->labeltype == LABEL_COUNTER_CHAPTER) {
3494                                         if (parameters->secnumdepth >=0){
3495                                                 /* this is special code for the chapter layout. This is printed in
3496                                                  * an extra row and has a pagebreak at the top. */
3497                                                 maxdesc = int(font.maxDescent() * layout->spacing.getValue() * parameters->spacing.getValue())
3498                                                         + int(layout->parsep) * DefaultHeight();
3499                                                 scr.drawString(font, tmpstring,
3500                                                                offset + row_ptr->baseline
3501                                                                - row_ptr->ascent_of_text - maxdesc,
3502                                                                int(x));
3503                                         }
3504                                 } else {
3505                                         x -= font.stringWidth( layout->labelsep);
3506                                         x -= font.stringWidth( tmpstring);
3507                                         /* draw it! */
3508                                         scr.drawString(font, tmpstring,
3509                                                        offset + row_ptr->baseline, int(x));
3510                                 }
3511                                 x = tmpx;
3512                         }
3513                         /* the labels at the top of an environment. More or less for bibliography */ 
3514                 } else if (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
3515                            layout->labeltype == LABEL_BIBLIO ||
3516                            layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3517                         if (row_ptr->par->IsFirstInSequence()) {
3518                                 font = GetFont(row_ptr->par, -2);
3519                                 if (!row_ptr->par->GetLabelString().empty()) {
3520                                         LString tmpstring = row_ptr->par->GetLabelString();
3521                                         
3522                                         maxdesc = int(font.maxDescent() * layout->spacing.getValue() * parameters->spacing.getValue()
3523                                                          + (layout->labelbottomsep * DefaultHeight()));
3524                                         
3525                                         int top_label_x = int(x);
3526                                         if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT){
3527                                                 top_label_x = int(x + (paperwidth - RightMargin(row_ptr) - x) / 2); 
3528                                                 top_label_x -= (font.stringWidth( tmpstring)/2);
3529                                         }
3530                                         
3531                                         scr.drawString(font, tmpstring,
3532                                                        offset + row_ptr->baseline
3533                                                        - row_ptr->ascent_of_text - maxdesc,  
3534                                                        top_label_x);                
3535                                 }
3536                         }
3537                 }
3538                 if (layout->labeltype==LABEL_BIBLIO) { // ale970302
3539                         if (row_ptr->par->bibkey) {
3540                                 tmpx = x;
3541                                 x -= font.stringWidth(layout->labelsep);
3542                                 font = GetFont(row_ptr->par, -1);
3543                                 x -= row_ptr->par->bibkey->Width(font);
3544                                 row_ptr->par->bibkey->Draw(font, scr,
3545                                                            offset + row_ptr->baseline, 
3546                                                            x);
3547                                 x = tmpx;
3548                         }
3549                 } 
3550         }
3551         
3552         /* is it a last row? */
3553         par = row_ptr->par->LastPhysicalPar();
3554         if (row_ptr->par->ParFromPos(RowLast(row_ptr) + 1) == par
3555             && (!row_ptr->next || row_ptr->next->par != row_ptr->par)) {     
3556                 
3557                 /* think about the margins */ 
3558                 if (!row_ptr->next)
3559                         y_bottom -= LYX_PAPER_MARGIN;
3560                 
3561                 /* draw a bottom pagebreak */ 
3562                 if (firstpar->pagebreak_bottom) {
3563                         scr.drawOnOffLine(offset + y_bottom - 2 *
3564                                           DefaultHeight(),
3565                                           0, paperwidth);
3566                         y_bottom -= 3 * DefaultHeight();
3567                 }
3568                 
3569                 if (firstpar->added_space_bottom.kind() == VSpace::VFILL) {
3570                         /* draw a vfill bottom  */
3571                         scr.drawLine(gc_fill,
3572                                      offset + y_bottom - 3 * DefaultHeight(),
3573                                      0, LYX_PAPER_MARGIN);
3574                         scr.drawLine(gc_fill, offset + y_bottom - 2,
3575                                      0, LYX_PAPER_MARGIN);
3576                         scr.drawVerticalOnOffLine(LYX_PAPER_MARGIN / 2, 
3577                                                   offset + y_bottom - 3 * DefaultHeight(),
3578                                                   offset + y_bottom - 2
3579                                 );          
3580                         y_bottom -= 3* DefaultHeight();
3581                 }
3582                 
3583                 /* think about user added space */ 
3584                 y_bottom -= int(firstpar->added_space_bottom.inPixels());
3585                 
3586                 if (firstpar->line_bottom) {
3587                         /* draw a bottom line */
3588                         y_bottom -= GetFont(par, par->Last() - 1).ascent('x');
3589
3590                         scr.drawThickLine(offset + y_bottom,
3591                                           0, paperwidth);           
3592                         y_bottom -= GetFont(par, par->Last() - 1).ascent('x');
3593                 }
3594         }
3595         
3596         /* draw the text in the pixmap */  
3597         pos_end = RowLast(row_ptr);
3598         
3599         pos = row_ptr->pos;
3600         /* table stuff -- begin*/
3601         if (row_ptr->par->table) {
3602                 bool on_off;
3603                 int cell = NumberOfCell(row_ptr->par, row_ptr->pos);
3604                 float x_old = x;
3605                 x += row_ptr->par->table->GetBeginningOfTextInCell(cell);
3606                 
3607                 while (pos <= pos_end)  {
3608                         if (row_ptr->par->IsNewline(pos)) {
3609                                 
3610                                 x = x_old + row_ptr->par->table->WidthOfColumn(cell);
3611                                 /* draw the table lines, still very simple */
3612                                 on_off = !row_ptr->par->table->TopLine(cell);
3613                                 if ((!on_off ||
3614                                      !row_ptr->par->table->TopAlreadyDrawed(cell)) &&
3615                                     !row_ptr->par->table->IsContRow(cell))
3616                                         scr.drawTableLine(offset + row_ptr->baseline -
3617                                                           row_ptr->ascent_of_text,
3618                                                           int(x_old), int(x - x_old), on_off);
3619                                 on_off = !row_ptr->par->table->BottomLine(cell);
3620                                 if ((!on_off && !row_ptr->par->table->RowHasContRow(cell)) ||
3621                                     row_ptr->par->table->VeryLastRow(cell))
3622                                         scr.drawTableLine(offset + y_bottom - 1,
3623                                                           int(x_old), int(x - x_old), on_off);
3624                                 on_off = !row_ptr->par->table->LeftLine(cell);
3625                                 
3626                                 scr.drawVerticalTableLine(int(x_old), 
3627                                                           offset + row_ptr->baseline -
3628                                                           row_ptr->ascent_of_text,
3629                                                           offset + y_bottom - 1,
3630                                                           on_off);
3631                                 on_off = !row_ptr->par->table->RightLine(cell);
3632                                 
3633                                 scr.drawVerticalTableLine(int(x) -
3634                                                           row_ptr->par->table->AdditionalWidth(cell),
3635                                                           offset + row_ptr->baseline -
3636                                                           row_ptr->ascent_of_text,
3637                                                           offset + y_bottom - 1,
3638                                                           on_off);
3639                                 x_old = x;
3640                 /* take care about the alignment and other spaces */
3641                                 cell++;
3642                                 x += row_ptr->par->table->GetBeginningOfTextInCell(cell);
3643                                 if (row_ptr->par->table->IsFirstCell(cell))
3644                                         cell--; // little hack, sorry
3645                                 pos++;
3646                         } else if (row_ptr->par->IsHfill(pos)) {
3647                                 x += 1;
3648                                 
3649                                 scr.drawVerticalLine(gc_fill, int(x),  
3650                                                      offset + row_ptr->baseline - DefaultHeight()/2, 
3651                                                      offset + row_ptr->baseline);               
3652                                 x += 2;
3653                                 pos++;
3654                         } else {
3655                                 if (row_ptr->par->IsSeparator(pos)) {
3656                                         tmpx = x;
3657                                         x+=SingleWidth(row_ptr->par, pos);
3658                                         /* -------> Only draw protected spaces when not in
3659                                          * free-spacing mode. */
3660                                         if (row_ptr->par->GetChar(pos)==LYX_META_PROTECTED_SEPARATOR && !layout->free_spacing) {
3661                                                 scr.drawVerticalLine(gc_fill, int(tmpx),
3662                                                                      offset + row_ptr->baseline - 3,
3663                                                                      offset + row_ptr->baseline - 1);
3664                                                 scr.drawLine(gc_fill,
3665                                                              offset + row_ptr->baseline - 1,
3666                                                              int(tmpx),
3667                                                              int(x-tmpx-2));
3668                                                 scr.drawVerticalLine(gc_fill, int(x-2),
3669                                                                      offset + row_ptr->baseline - 3,
3670                                                                      offset + row_ptr->baseline - 1);                   
3671                                                 /* what about underbars? */
3672                                                 font = GetFont(row_ptr->par, pos); 
3673                                                 if (font.underbar() == LyXFont::ON
3674                                                     && font.latex() != LyXFont::ON) {
3675                                                         scr.drawLine(gc_copy,
3676                                                                      offset +
3677                                                                      row_ptr->baseline + 2,
3678                                                                      int(tmpx),
3679                                                                      int(x - tmpx));                        
3680                                                 }
3681                                         }
3682                                         pos++;
3683                                 } else
3684                                         Draw(row_ptr, pos, scr, offset, x);
3685                         }
3686                 }
3687                 
3688                 /* do not forget the very last cell. This has no NEWLINE so 
3689                  * ignored by the code above*/ 
3690                 if (cell == row_ptr->par->table->GetNumberOfCells()-1){
3691                         x = x_old + row_ptr->par->table->WidthOfColumn(cell);
3692                         on_off = !row_ptr->par->table->TopLine(cell);
3693                         if ((!on_off ||
3694                              !row_ptr->par->table->TopAlreadyDrawed(cell)) &&
3695                             !row_ptr->par->table->IsContRow(cell))
3696                                 
3697                                 scr.drawTableLine(offset + row_ptr->baseline -
3698                                                   row_ptr->ascent_of_text,
3699                                                   int(x_old), int(x - x_old), on_off);      
3700                         on_off = !row_ptr->par->table->BottomLine(cell);
3701                         if ((!on_off && !row_ptr->par->table->RowHasContRow(cell)) ||
3702                             row_ptr->par->table->VeryLastRow(cell))
3703                                 
3704                                 scr.drawTableLine(offset + y_bottom - 1,
3705                                                   int(x_old), int(x - x_old), on_off);      
3706                         on_off = !row_ptr->par->table->LeftLine(cell);
3707                         
3708                         scr.drawVerticalTableLine(int(x_old), 
3709                                                   offset + row_ptr->baseline -
3710                                                   row_ptr->ascent_of_text,
3711                                                   offset + y_bottom - 1,
3712                                                   on_off);          
3713                         on_off = !row_ptr->par->table->RightLine(cell);
3714                         
3715                         scr.drawVerticalTableLine(int(x) -
3716                                                   row_ptr->par->table->AdditionalWidth(cell),
3717                                                   offset + row_ptr->baseline -
3718                                                   row_ptr->ascent_of_text,
3719                                                   offset + y_bottom - 1,
3720                                                   on_off);          
3721                 }
3722         } else {
3723                 /* table stuff -- end*/
3724                 
3725                 while (pos <= pos_end)  {
3726                         
3727                         if (row_ptr->par->IsHfill(pos)) {
3728                                 x += 1;
3729                                 scr.drawVerticalLine(gc_fill, int(x),  
3730                                                      offset + row_ptr->baseline - DefaultHeight()/2, 
3731                                                      offset + row_ptr->baseline);               
3732                                 if (HfillExpansion(row_ptr,pos)) {
3733                                         if (pos >= main_body) {
3734                                                 scr.drawOnOffLine(offset + row_ptr->baseline -
3735                                                                   DefaultHeight()/4,
3736                                                                   int(x),
3737                                                                   int(fill_hfill));                     
3738                                                 x += fill_hfill;
3739                     } else {
3740                             scr.drawOnOffLine(offset + row_ptr->baseline -
3741                                               DefaultHeight()/4,
3742                                               int(x),
3743                                               int(fill_label_hfill));
3744                             x += fill_label_hfill;
3745                     }
3746                                         scr.drawVerticalLine(gc_fill, int(x),
3747                                                              offset + row_ptr->baseline -
3748                                                              DefaultHeight()/2, 
3749                                                              offset + row_ptr->baseline);
3750                                 }
3751                                 x += 2;
3752                                 pos++;
3753                         } else {
3754                                 if (row_ptr->par->IsSeparator(pos)) {
3755                                         tmpx = x;
3756                                         x+=SingleWidth(row_ptr->par, pos);
3757                                         if (pos >= main_body)
3758                                                 x+= fill_separator;
3759                                         /* -------> Only draw protected spaces when not in
3760                                          * free-spacing mode. */
3761                                         if (row_ptr->par->GetChar(pos)==LYX_META_PROTECTED_SEPARATOR && !layout->free_spacing) {
3762                                                 
3763                                                 scr.drawVerticalLine(gc_fill, int(tmpx),
3764                                                                      offset + row_ptr->baseline - 3,
3765                                                                      offset + row_ptr->baseline - 1);
3766                                                 scr.drawLine(gc_fill,
3767                                                              offset + row_ptr->baseline - 1,
3768                                                              int(tmpx),
3769                                                              int(x-tmpx-2));
3770                                                 scr.drawVerticalLine(gc_fill, int(x-2),
3771                                                                      offset + row_ptr->baseline - 3,
3772                                                                      offset + row_ptr->baseline - 1);                   
3773                                                 /* what about underbars? */
3774                                                 font = GetFont(row_ptr->par, pos); 
3775                                                 if (font.underbar() == LyXFont::ON
3776                                                     && font.latex() != LyXFont::ON) {
3777                                                         scr.drawLine(gc_copy,
3778                                              offset + row_ptr->baseline + 2,
3779                                                                      int(tmpx),
3780                                                                      int(x - tmpx));
3781                                                 }
3782                                         }
3783                                         pos++;
3784                                 } else
3785                                         Draw(row_ptr, pos, scr, offset, x);
3786                         }
3787                         if (pos == main_body) {
3788                                 x += GetFont(row_ptr->par, -2).stringWidth(
3789                                         layout->labelsep);
3790                                 if (row_ptr->par->IsLineSeparator(pos - 1))
3791                                         x-= SingleWidth(row_ptr->par, pos - 1);
3792                                 if (x < left_margin)
3793                                         x = left_margin;
3794                         }
3795                 }
3796         }
3797         // check for FAST SELECTION
3798         if (fast_selection || mono_video){
3799                 if (selection) {
3800                         
3801                         /* selection code */ 
3802                         if (sel_start_cursor.row == row_ptr && sel_end_cursor.row == row_ptr) {
3803                                 scr.fillRectangle(gc_select,sel_start_cursor.x, offset,
3804                                                   sel_end_cursor.x - sel_start_cursor.x,
3805                                                   row_ptr->height); 
3806                         } else if (sel_start_cursor.row == row_ptr) {
3807                                 scr.fillRectangle(gc_select, sel_start_cursor.x, offset,
3808                                                   paperwidth - sel_start_cursor.x,
3809                                                   row_ptr->height);
3810                         } else if (sel_end_cursor.row == row_ptr) {
3811                                 scr.fillRectangle(gc_select, 0, offset,
3812                                                   sel_end_cursor.x,
3813                                                   row_ptr->height);
3814                         } else if (y > sel_start_cursor.y && y < sel_end_cursor.y) {
3815                                 scr.fillRectangle(gc_select, 0, offset,
3816                                                   paperwidth, row_ptr->height);
3817                                 
3818                         }
3819                 }
3820         }
3821 // end of FAST SELECTION code
3822         /* thats it */ 
3823 }
3824
3825
3826 int LyXText::DefaultHeight()
3827 {
3828         LyXFont font(LyXFont::ALL_SANE);
3829         return int(font.maxAscent() + font.maxDescent() * 1.5);
3830 }
3831
3832    
3833 /* returns the column near the specified x-coordinate of the row 
3834 * x is set to the real beginning of this column  */ 
3835 int  LyXText::GetColumnNearX(Row *row, int& x)
3836 {
3837         int c; 
3838         float tmpx = 0.0;
3839         float fill_separator, fill_hfill, fill_label_hfill;
3840         int left_margin;
3841    
3842         left_margin = LabelEnd(row);
3843         PrepareToPrint(row, tmpx, fill_separator,
3844                        fill_hfill, fill_label_hfill);
3845         int main_body = BeginningOfMainBody(row->par);
3846    
3847         c = row->pos;
3848
3849         int last = RowLast(row);
3850         if (row->par->IsNewline(last))
3851                 last--;
3852    
3853         LyXLayout *layout = lyxstyle.Style(parameters->textclass,
3854                                            row->par->GetLayout());
3855         /* table stuff -- begin*/
3856         if (row->par->table) {
3857                 if (!row->next || row->next->par != row->par)
3858                         last = RowLast(row); /* the last row doesn't need a newline at the end*/
3859                 int cell = NumberOfCell(row->par, row->pos);
3860                 float x_old = tmpx;
3861                 bool ready = false;
3862                 tmpx += row->par->table->GetBeginningOfTextInCell(cell);
3863                 while (c <= last
3864                        && tmpx + (SingleWidth(row->par, c)/2) <= x
3865                        && !ready){
3866                         if (row->par->IsNewline(c)) {
3867                                 if (x_old + row->par->table->WidthOfColumn(cell) <= x){
3868                                         tmpx = x_old + row->par->table->WidthOfColumn(cell);
3869                                         x_old = tmpx;
3870                                         cell++;
3871                                         tmpx += row->par->table->GetBeginningOfTextInCell(cell);
3872                                         c++;
3873                                 } else
3874                                         ready = true;
3875                         } else {
3876                                 tmpx += SingleWidth(row->par, c);
3877                                 c++;
3878                         }
3879                 }
3880         } else
3881                 /* table stuff -- end*/
3882
3883                 while (c <= last
3884                        && tmpx + (SingleWidth(row->par, c)/2)  <= x) {
3885                         
3886                         if (c && c == main_body
3887                             && !row->par->IsLineSeparator(c - 1)) {
3888                                 tmpx += GetFont(row->par, -2)
3889                                         .stringWidth(layout->labelsep);
3890                                 if (tmpx < left_margin)
3891                                         tmpx = left_margin;
3892                         }
3893              
3894                         tmpx += SingleWidth(row->par, c);
3895                         if (HfillExpansion(row, c)) {
3896                                 if (c >= main_body)
3897                                         tmpx += fill_hfill;
3898                                 else
3899                                         tmpx += fill_label_hfill;
3900                         }
3901                         else if (c >= main_body
3902                                  && row->par->IsSeparator(c)) {
3903                                 tmpx+= fill_separator;  
3904                         }
3905                         c++;
3906                         if (c == main_body
3907                             && row->par->IsLineSeparator(c - 1)) {
3908                                 tmpx += GetFont(row->par, -2)
3909                                         .stringWidth(layout->labelsep);
3910                                 tmpx-= SingleWidth(row->par, c - 1);
3911                                 if (tmpx < left_margin)
3912                                         tmpx = left_margin;
3913                         }
3914                 }
3915         /* make sure that a last space in a row doesnt count */
3916         if (c > 0 && c >= last
3917             && row->par->IsLineSeparator(c - 1)
3918             && !(!row->next || row->next->par != row->par)) {
3919                 tmpx -= SingleWidth(row->par, c - 1);
3920                 tmpx -= fill_separator;
3921         }
3922         c-=row->pos;
3923         
3924         x = int(tmpx);
3925         return c;
3926 }
3927
3928    
3929 /* turn the selection into a new environment. If there is no selection,
3930 * create an empty environment */ 
3931 void LyXText::InsertFootnoteEnvironment(LyXParagraph::footnote_kind kind)
3932 {
3933    /* no footnoteenvironment in a footnoteenvironment */ 
3934    if (cursor.par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
3935       WriteAlert(_("Impossible operation"), 
3936                  _("You can't insert a float in a float!"), 
3937                  _("Sorry."));
3938      return;
3939    }
3940    /* no marginpars in minipages */
3941    if (kind == LyXParagraph::MARGIN 
3942       && cursor.par->pextra_type == PEXTRA_MINIPAGE) {
3943       WriteAlert(_("Impossible operation"), 
3944                  _("You can't insert a marginpar in a minipage!"), 
3945                  _("Sorry."));
3946       return;
3947    }
3948    
3949    /* this doesnt make sense, if there is no selection */ 
3950    bool dummy_selection = false;
3951    if (!selection) {
3952       sel_start_cursor = cursor;       /* dummy selection  */
3953       sel_end_cursor = cursor;
3954       dummy_selection = true;
3955    }
3956    
3957    LyXParagraph *tmppar;
3958
3959    if (sel_start_cursor.par->table || sel_end_cursor.par->table){
3960       WriteAlert(_("Impossible operation"), _("Cannot cut table."), _("Sorry."));
3961       return;
3962    }
3963      
3964    /* a test to make sure there is not already a footnote
3965     * in the selection. */
3966    
3967    tmppar = sel_start_cursor.par->ParFromPos(sel_start_cursor.pos);
3968    
3969    while (tmppar != sel_end_cursor.par->ParFromPos(sel_end_cursor.pos) && 
3970           tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
3971      tmppar = tmppar->next;
3972    
3973    if (tmppar != sel_end_cursor.par->ParFromPos(sel_end_cursor.pos)
3974        || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
3975       WriteAlert(_("Impossible operation"), 
3976                  _("Float would include float!"), 
3977                  _("Sorry."));
3978       return;
3979    }
3980    
3981    /* ok we have a selection. This is always between sel_start_cursor
3982     * and sel_end cursor */
3983
3984    SetUndo(Undo::FINISH, 
3985            sel_start_cursor.par->ParFromPos(sel_start_cursor.pos)->previous, 
3986            sel_end_cursor.par->ParFromPos(sel_end_cursor.pos)->next); 
3987    
3988    if (sel_end_cursor.pos > 0 
3989        && sel_end_cursor.par->IsLineSeparator(sel_end_cursor.pos - 1))
3990      sel_end_cursor.pos--;             /* please break before a space at
3991                                         * the end */
3992    if (sel_start_cursor.par == sel_end_cursor.par
3993        && sel_start_cursor.pos > sel_end_cursor.pos)
3994      sel_start_cursor.pos--;
3995
3996    sel_end_cursor.par->BreakParagraphConservative(sel_end_cursor.pos);
3997    
3998    sel_end_cursor.par = sel_end_cursor.par->Next();
3999    sel_end_cursor.pos = 0;
4000    
4001    // don't forget to insert a dummy layout paragraph if necessary
4002    if (sel_start_cursor.par->GetLayout() != sel_end_cursor.par->layout){
4003      sel_end_cursor.par->BreakParagraphConservative(0);
4004      sel_end_cursor.par->layout = LYX_DUMMY_LAYOUT;
4005      sel_end_cursor.par = sel_end_cursor.par->next;
4006    }
4007    else
4008      sel_end_cursor.par->layout = LYX_DUMMY_LAYOUT;
4009
4010    cursor = sel_end_cursor;
4011
4012    /* please break behind a space, if there is one. The space should
4013     * be erased too */ 
4014    if (sel_start_cursor.pos > 0 
4015        && sel_start_cursor.par->IsLineSeparator(sel_start_cursor.pos - 1))
4016      sel_start_cursor.pos--;
4017    if (sel_start_cursor.par->IsLineSeparator(sel_start_cursor.pos)) {
4018       sel_start_cursor.par->Erase(sel_start_cursor.pos);
4019    }
4020    
4021    sel_start_cursor.par->BreakParagraphConservative(sel_start_cursor.pos);
4022    tmppar = sel_start_cursor.par->Next();
4023    
4024    if (dummy_selection) {
4025            tmppar->Clear();
4026            if (kind == LyXParagraph::TAB
4027                || kind == LyXParagraph::FIG 
4028                || kind == LyXParagraph::WIDE_TAB
4029                || kind == LyXParagraph::WIDE_FIG 
4030                || kind == LyXParagraph::ALGORITHM) {
4031                    int lay = lyxstyle.NumberOfLayout(parameters->textclass,
4032                                                      "Caption");
4033                    if (lay == -1) // layout not found
4034                            // use default layout "Standard" (0)
4035                            lay = 0;
4036                    tmppar->SetLayout(lay);
4037            }
4038    }
4039    else {
4040      if (sel_start_cursor.pos > 0) {
4041        /* the footnote-environment should begin with a standard layout.
4042         * Imagine you insert a footnote within an enumeration, you 
4043         * certainly do not want an enumerated footnote! */ 
4044        tmppar->Clear();
4045      }
4046      else {
4047        /* this is a exception the user would sometimes expect, I hope */
4048        sel_start_cursor.par->Clear();
4049      }
4050    }
4051    
4052    while (tmppar != sel_end_cursor.par) {
4053       tmppar->footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
4054       tmppar->footnotekind = kind;
4055       tmppar = tmppar->Next();
4056    } 
4057
4058    RedoParagraphs(sel_start_cursor, sel_end_cursor.par->Next());
4059    
4060    SetCursor(sel_start_cursor.par->Next(), 0);
4061
4062    ClearSelection();
4063 }
4064    
4065
4066 /* returns pointer to a specified row */
4067 Row* LyXText::GetRow(LyXParagraph *par, int pos, long &y)
4068 {
4069    Row* tmprow;
4070
4071    if (currentrow){
4072      if (par == currentrow->par || par == currentrow->par->Previous()){
4073              // do not dereference par, it may have been deleted
4074              // already! (Matthias) 
4075              while (currentrow->previous && currentrow->previous->par != par){
4076                      currentrow = currentrow->previous;
4077                      currentrow_y -= currentrow->height;
4078              }
4079              while (currentrow->previous && currentrow->previous->par == par){
4080                      currentrow = currentrow->previous;
4081                      currentrow_y -= currentrow->height;
4082              }
4083      }
4084      tmprow = currentrow;
4085      y = currentrow_y;
4086      /* find the first row of the specified paragraph */ 
4087      while (tmprow->next && (tmprow->par != par)) {
4088        y += tmprow->height;
4089        tmprow = tmprow->next;
4090      }
4091
4092      if (tmprow->par == par){
4093        /* now find the wanted row */ 
4094        while (tmprow->pos < pos && tmprow->next && tmprow->next->par == par && 
4095               tmprow->next->pos <= pos) {
4096          y += tmprow->height;
4097        tmprow = tmprow->next;
4098        }
4099        currentrow = tmprow;
4100        currentrow_y = y;
4101        return tmprow;
4102      }
4103    }
4104    tmprow = firstrow;
4105    y = 0;
4106    /* find the first row of the specified paragraph */ 
4107    while (tmprow->next && (tmprow->par != par)) {
4108       y += tmprow->height;
4109       tmprow = tmprow->next;
4110    }
4111  
4112    /* now find the wanted row */ 
4113    while (tmprow->pos < pos && tmprow->next && tmprow->next->par == par && 
4114           tmprow->next->pos <= pos) {
4115              y += tmprow->height;
4116              tmprow = tmprow->next;
4117    }
4118    
4119    currentrow = tmprow;
4120    currentrow_y = y;
4121
4122    return tmprow;
4123 }