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