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