]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
Small fixes to compile with compaq cxx
[lyx.git] / src / insets / insettext.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ======================================================
4  * 
5  *           LyX, The Document Processor
6  *
7  *           Copyright 1998-2000 The LyX Team.
8  *
9  * ======================================================
10  */
11
12 #include <config.h>
13
14 #include <fstream>
15 #include <algorithm>
16
17 #include <cstdlib>
18
19 #ifdef __GNUG__
20 #pragma implementation
21 #endif
22
23 #include "insettext.h"
24 #include "lyxparagraph.h"
25 #include "lyxlex.h"
26 #include "debug.h"
27 #include "lyxfont.h"
28 #include "commandtags.h"
29 #include "buffer.h"
30 #include "LyXView.h"
31 #include "BufferView.h"
32 #include "layout.h"
33 #include "LaTeXFeatures.h"
34 #include "Painter.h"
35 #include "lyx_gui_misc.h"
36 #include "lyxtext.h"
37 #include "lyxcursor.h"
38 #include "CutAndPaste.h"
39 #include "font.h"
40 #include "minibuffer.h"
41 #include "toolbar.h"
42 #include "LColor.h"
43 #include "support/textutils.h"
44 #include "support/LAssert.h"
45 #include "lyxrow.h"
46
47 using std::ostream;
48 using std::ifstream;
49 using std::endl;
50 using std::min;
51 using std::max;
52
53 extern unsigned char getCurrentTextClass(Buffer *);
54 #warning BAAAAAAAADDDDDDD current_view (but Lars wanted it :) !!!
55 extern BufferView * current_view;
56
57
58 InsetText::InsetText()
59 {
60     par = new LyXParagraph();
61     init();
62     text = new LyXText(this);
63 }
64
65
66 InsetText::InsetText(InsetText const & ins) : UpdatableInset()
67 {
68     par = 0;
69     init(&ins);
70     text = new LyXText(this);
71     autoBreakRows = ins.autoBreakRows;
72 }
73
74
75 InsetText & InsetText::operator=(InsetText const & it)
76 {
77     init(&it);
78     text = new LyXText(this);
79     autoBreakRows = it.autoBreakRows;
80     return * this;
81 }
82
83 void InsetText::init(InsetText const * ins)
84 {
85     the_locking_inset = 0;
86     cursor_visible = false;
87     cursor.x_fix(-1);
88     interline_space = 1;
89     no_selection = false;
90     init_inset = true;
91     maxAscent = maxDescent = insetWidth = 0;
92     drawTextXOffset = drawTextYOffset = 0;
93     autoBreakRows = drawLockedFrame = false;
94     xpos = 0.0;
95     if (ins) {
96         SetParagraphData(ins->par);
97         autoBreakRows = ins->autoBreakRows;
98         drawLockedFrame = ins->drawLockedFrame;
99     }
100     par->SetInsetOwner(this);
101     cursor.par(par);
102     cursor.pos(0);
103     selection_start_cursor = selection_end_cursor = cursor;
104     frame_color = LColor::insetframe;
105     locked = false;
106 }
107
108
109 InsetText::~InsetText()
110 {
111     delete par;
112 }
113
114
115 Inset * InsetText::Clone() const
116 {
117     InsetText * t = new InsetText(*this);
118     return t;
119 }
120
121
122 void InsetText::Write(Buffer const * buf, ostream & os) const
123 {
124     os << "Text\n";
125     WriteParagraphData(buf, os);
126 }
127
128
129 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
130 {
131     par->writeFile(buf, os, buf->params, 0, 0);
132 }
133
134
135 void InsetText::Read(Buffer const * buf, LyXLex & lex)
136 {
137     string token, tmptok;
138     int pos = 0;
139     LyXParagraph * return_par = 0;
140     char depth = 0; // signed or unsigned?
141     LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
142     LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
143     LyXFont font(LyXFont::ALL_INHERIT);
144
145     delete par;
146     par = new LyXParagraph;
147     par->SetInsetOwner(this);
148     
149     while (lex.IsOK()) {
150         lex.nextToken();
151         token = lex.GetString();
152         if (token.empty())
153             continue;
154         if (token == "\\end_inset")
155             break;
156         if (const_cast<Buffer*>(buf)->parseSingleLyXformat2Token(lex, par, return_par,
157                                             token, pos, depth,
158                                             font, footnoteflag,
159                                             footnotekind)) {
160             // the_end read this should NEVER happen
161             lex.printError("\\the_end read in inset! Error in document!");
162             return;
163         }
164     }
165     if (token != "\\end_inset") {
166         lex.printError("Missing \\end_inset at this point. "
167                        "Read: `$$Token'");
168     }
169     init_inset = true;
170 }
171
172
173 int InsetText::ascent(Painter & pain, LyXFont const & font) const
174 {
175     if (init_inset) {
176         text->init(current_view);
177         computeTextRows(pain);
178         init_inset = false;
179     }
180     long int y_temp = 0;
181     Row * row = text->GetRowNearY(y_temp);
182     return row->ascent_of_text() + 2;
183 }
184
185
186 int InsetText::descent(Painter & pain, LyXFont const & font) const
187 {
188     if (init_inset) {
189         text->init(current_view);
190         computeTextRows(pain);
191         init_inset = false;
192     }
193     long int y = 0;
194     Row * row = text->GetRowNearY(y);
195     return text->height - row->ascent_of_text() + 2;
196 }
197
198
199 int InsetText::width(Painter & pain, LyXFont const &) const
200 {
201     if (init_inset) {
202         text->init(current_view);
203         computeTextRows(pain);
204         init_inset = false;
205     }
206     return std::max(static_cast<long int>(getMaxTextWidth(pain, this)),
207                     text->width);
208 //    return insetWidth;
209 }
210
211
212 void InsetText::draw(Painter & pain, LyXFont const & f,
213                      int baseline, float & x) const
214 {
215     xpos = x;
216     UpdatableInset::draw(pain, f, baseline, x);
217     
218     if (init_inset) {
219         text->init(current_view);
220             computeTextRows(pain);
221             init_inset = false;
222         }
223     if ((baseline != top_baseline) || (top_x != int(x))) {
224         top_baseline = baseline;
225         top_x = int(x);
226         computeBaselines(baseline);
227     }
228     if (the_locking_inset && (text->cursor.pos() == inset_pos)) {
229         resetPos(pain);
230         inset_x = text->cursor.x() - top_x + drawTextXOffset;
231         inset_y = text->cursor.y() + drawTextYOffset;
232     }
233     if (drawLockedFrame && locked) {
234         pain.rectangle(int(x), baseline - ascent(pain, f), width(pain, f),
235                        ascent(pain,f) + descent(pain, f), frame_color);
236     }
237     x += TEXT_TO_INSET_OFFSET; // place for border
238 #if 1
239     // Dump all rowinformation:
240     long y_dummy = 0;
241     Row * tmprow = text->GetRowNearY(y_dummy);
242     lyxerr << "Baseline Paragraph Pos Height Ascent Fill\n";
243     while (tmprow) {
244         lyxerr << tmprow->baseline() << '\t'
245                << tmprow->par() << '\t'
246                << tmprow->pos() << '\t'
247                << tmprow->height() << '\t'
248                << tmprow->ascent_of_text() << '\t'
249                << tmprow->fill() << '\n';
250         tmprow = tmprow->next();
251     }
252     lyxerr.flush();
253     long int y = 0;
254     Row * row = text->GetRowNearY(y);
255     y += baseline - row->ascent_of_text() + 1;
256     text->width = 0;
257     while (row != 0) {
258         text->GetVisibleRow(current_view, y, x, row, y);
259         y += row->height();
260         row = row->next();
261     }
262 #else
263     for(RowList::size_type r = 0; r < rows.size() - 1; ++r) {
264         drawRowSelection(pain, rows[r].pos, rows[r + 1].pos, r, 
265                          rows[r].baseline, x);
266         drawRowText(pain, rows[r].pos, rows[r + 1].pos, rows[r].baseline, x);
267     }
268 #endif
269     x += insetWidth - TEXT_TO_INSET_OFFSET;
270 }
271
272
273 void InsetText::drawRowSelection(Painter & pain, int startpos, int endpos,
274                                  int row, int baseline, float x) const
275 {
276     if (!hasSelection())
277         return;
278
279     int s_start, s_end;
280     if (selection_start_cursor.pos() > selection_end_cursor.pos()) {
281         s_start = selection_end_cursor.pos();
282         s_end = selection_start_cursor.pos();
283     } else {
284         s_start = selection_start_cursor.pos();
285         s_end = selection_end_cursor.pos();
286     }
287     if ((s_start > endpos) || (s_end < startpos))
288         return;
289     
290     int esel_x;
291     int ssel_x = esel_x = int(x);
292     LyXFont font;
293     int p = startpos;
294     for(; p < endpos; ++p) {
295         if (p == s_start)
296             ssel_x = int(x);
297         if ((p >= s_start) && (p <= s_end))
298             esel_x = int(x);
299         char ch = par->GetChar(p);
300         font = GetDrawFont(current_view->buffer(), par, p);
301         if (IsFloatChar(ch)) {
302             // skip for now
303         } else if (ch == LyXParagraph::META_INSET) {
304             Inset const * tmpinset = par->GetInset(p);
305             x += tmpinset->width(pain, font);
306         } else {
307             x += lyxfont::width(ch, font);
308         }
309     }
310     if (p == s_start)
311         ssel_x = int(x);
312     if ((p >= s_start) && (p <= s_end))
313         esel_x = int(x);
314     if (ssel_x < esel_x) {
315         pain.fillRectangle(int(ssel_x), baseline-rows[row].asc,
316                            int(esel_x - ssel_x),
317                            rows[row].asc + rows[row].desc,
318                            LColor::selection);
319     }
320 }
321
322
323 void InsetText::drawRowText(Painter & pain, int startpos, int endpos,
324                             int baseline, float x) const
325 {
326     Assert(endpos <= par->Last());
327
328     for(int p = startpos; p < endpos; ++p) {
329         char ch = par->GetChar(p);
330         LyXFont font = GetDrawFont(current_view->buffer(), par, p);
331         if (IsFloatChar(ch)) {
332             // skip for now
333         } else if (par->IsNewline(p)) {
334                 // Draw end-of-line marker
335                 int wid = lyxfont::width('n', font);
336                 int asc = lyxfont::maxAscent(font);
337                 int y = baseline;
338                 int xp[3], yp[3];
339                 
340                 xp[0] = int(x + wid * 0.375);
341                 yp[0] = int(y - 0.875 * asc * 0.75);
342                 
343                 xp[1] = int(x);
344                 yp[1] = int(y - 0.500 * asc * 0.75);
345                 
346                 xp[2] = int(x + wid * 0.375);
347                 yp[2] = int(y - 0.125 * asc * 0.75);
348                 
349                 pain.lines(xp, yp, 3, LColor::eolmarker);
350                 
351                 xp[0] = int(x);
352                 yp[0] = int(y - 0.500 * asc * 0.75);
353                 
354                 xp[1] = int(x + wid);
355                 yp[1] = int(y - 0.500 * asc * 0.75);
356                 
357                 xp[2] = int(x + wid);
358                 yp[2] = int(y - asc * 0.75);
359                         
360                 pain.lines(xp, yp, 3, LColor::eolmarker);
361                 x += wid;
362         } else if (ch == LyXParagraph::META_INSET) {
363             Inset * tmpinset = par->GetInset(p);
364             if (tmpinset) 
365                 tmpinset->draw(pain, font, baseline, x);
366         } else {
367             pain.text(int(x), baseline, ch, font);
368             x += lyxfont::width(ch, font);
369         }
370     }
371 }
372
373
374 char const * InsetText::EditMessage() const
375 {
376     return _("Opened Text Inset");
377 }
378
379
380 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
381 {
382     par->SetInsetOwner(this);
383     UpdatableInset::Edit(bv, x, y, button);
384
385     if (!bv->lockInset(this)) {
386         lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
387         return;
388     }
389     locked = true;
390     the_locking_inset = 0;
391     inset_pos = inset_x = inset_y = 0;
392     setPos(bv->painter(), x, y);
393     checkAndActivateInset(bv, x, y, button);
394     selection_start_cursor = selection_end_cursor = cursor;
395     current_font = real_current_font =GetFont(bv->buffer(), par, cursor.pos());
396     bv->text->FinishUndo();
397     UpdateLocal(bv, true);
398 }
399
400
401 void InsetText::InsetUnlock(BufferView * bv)
402 {
403     if (the_locking_inset) {
404         the_locking_inset->InsetUnlock(bv);
405         the_locking_inset = 0;
406     }
407     HideInsetCursor(bv);
408     lyxerr[Debug::INSETS] << "InsetText::InsetUnlock(" << this <<
409             ")" << endl;
410     selection_start_cursor = selection_end_cursor = cursor;
411     no_selection = false;
412     locked = false;
413     UpdateLocal(bv, true);
414 }
415
416
417 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
418 {
419     lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
420     if (!inset)
421         return false;
422     if (inset == par->GetInset(text->cursor.pos())) {
423         lyxerr[Debug::INSETS] << "OK" << endl;
424         the_locking_inset = inset;
425         resetPos(bv->painter());
426         inset_x = text->cursor.x() - top_x + drawTextXOffset;
427         inset_y = text->cursor.y() + drawTextYOffset;
428         inset_pos = text->cursor.pos();
429         return true;
430     } else if (the_locking_inset && (the_locking_inset == inset)) {
431         if (text->cursor.pos() == inset_pos) {
432             lyxerr[Debug::INSETS] << "OK" << endl;
433             resetPos(bv->painter());
434             inset_x = text->cursor.x() - top_x + drawTextXOffset;
435             inset_y = text->cursor.y() + drawTextYOffset;
436         } else {
437             lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
438         }
439     } else if (the_locking_inset) {
440         lyxerr[Debug::INSETS] << "MAYBE" << endl;
441         return the_locking_inset->LockInsetInInset(bv, inset);
442     }
443     lyxerr[Debug::INSETS] << "NOT OK" << endl;
444     return false;
445 }
446
447
448 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
449                                    bool lr)
450 {
451     if (!the_locking_inset)
452         return false;
453     if (the_locking_inset == inset) {
454         the_locking_inset->InsetUnlock(bv);
455         the_locking_inset = 0;
456         if (lr)
457             moveRight(bv, false);
458         return true;
459     }
460     return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
461 }
462
463
464 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
465 {
466     if (!the_locking_inset)
467         return false;
468     if (the_locking_inset != inset)
469         return the_locking_inset->UpdateInsetInInset(bv, inset);
470     lyxerr[Debug::INSETS] << "InsetText::UpdateInsetInInset(" << inset <<
471             ")" << endl;
472     UpdateLocal(bv, true);
473     if (text->cursor.pos() == inset_pos) {
474         inset_x = text->cursor.x() - top_x + drawTextXOffset;
475         inset_y = text->cursor.y() + drawTextYOffset;
476     }
477     return true;
478 }
479
480
481 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
482 {
483     if (hasSelection()) {
484         selection_start_cursor = selection_end_cursor = cursor;
485         UpdateLocal(bv, false);
486     }
487     no_selection = false;
488     setPos(bv->painter(), x, y);
489     cursor.x_fix(-1);
490     if (the_locking_inset) {
491         UpdatableInset * inset = 0;
492         if (par->GetChar(cursor.pos()) == LyXParagraph::META_INSET)
493             inset = static_cast<UpdatableInset*>(par->GetInset(cursor.pos()));
494         if (the_locking_inset == inset) {
495             the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
496             return;
497         } else if (inset) {
498             // otherwise unlock the_locking_inset and lock the new inset
499             the_locking_inset->InsetUnlock(bv);
500             inset_x = cursor.x() - top_x + drawTextXOffset;
501             inset_y = cursor.y() + drawTextYOffset;
502             inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
503             inset->Edit(bv, x - inset_x, y - inset_y, button);
504             UpdateLocal(bv, true);
505             return;
506         }
507         // otherwise only unlock the_locking_inset
508         the_locking_inset->InsetUnlock(bv);
509         the_locking_inset = 0;
510     }
511     if (bv->the_locking_inset) {
512         if ((par->GetChar(cursor.pos()) == LyXParagraph::META_INSET) &&
513             par->GetInset(cursor.pos()) &&
514             (par->GetInset(cursor.pos())->Editable() == Inset::HIGHLY_EDITABLE)) {
515             UpdatableInset * inset =
516                 static_cast<UpdatableInset*>(par->GetInset(cursor.pos()));
517             inset_x = cursor.x() - top_x + drawTextXOffset;
518             inset_y = cursor.y() + drawTextYOffset;
519             inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
520             inset->Edit(bv, x - inset_x, y - inset_y, 0);
521             UpdateLocal(bv, true);
522         }
523     }
524     selection_start_cursor = selection_end_cursor = cursor;
525 }
526
527
528 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
529 {
530     UpdatableInset * inset = 0;
531
532     if (the_locking_inset) {
533             the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
534     } else {
535         if (par->GetChar(cursor.pos()) == LyXParagraph::META_INSET) {
536             inset = static_cast<UpdatableInset*>(par->GetInset(cursor.pos()));
537             if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
538                 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
539             } else {
540                 inset_x = cursor.x() - top_x + drawTextXOffset;
541                 inset_y = cursor.y() + drawTextYOffset;
542                 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
543                 inset->Edit(bv, x - inset_x, y - inset_y, button);
544             }
545         }
546     }
547     no_selection = false;
548 }
549
550
551 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
552 {
553     if (the_locking_inset) {
554         the_locking_inset->InsetMotionNotify(bv, x - inset_x,
555                                              y - inset_y,state);
556         return;
557     }
558     if (!no_selection) {
559         LyXCursor old = selection_end_cursor;
560         HideInsetCursor(bv);
561         setPos(bv->painter(), x, y);
562         selection_end_cursor = cursor;
563         if (old != selection_end_cursor)
564             UpdateLocal(bv, false);
565         ShowInsetCursor(bv);
566     }
567     no_selection = false;
568 }
569
570
571 void InsetText::InsetKeyPress(XKeyEvent * xke)
572 {
573     if (the_locking_inset) {
574         the_locking_inset->InsetKeyPress(xke);
575         return;
576     }
577 }
578
579
580 UpdatableInset::RESULT
581 InsetText::LocalDispatch(BufferView * bv,
582                          int action, string const & arg)
583 {
584     no_selection = false;
585     UpdatableInset::RESULT
586         result= UpdatableInset::LocalDispatch(bv, action, arg);
587     if (result != UNDISPATCHED) {
588         resetPos(bv->painter());
589         return DISPATCHED;
590     }
591
592     result=DISPATCHED;
593     if ((action < 0) && arg.empty())
594         return FINISHED;
595
596     if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
597         (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
598         cursor.x_fix(-1);
599     if (the_locking_inset) {
600         result = the_locking_inset->LocalDispatch(bv, action, arg);
601         if (result == DISPATCHED_NOUPDATE)
602             return result;
603         else if (result == DISPATCHED) {
604             the_locking_inset->ToggleInsetCursor(bv);
605             UpdateLocal(bv, false);
606             the_locking_inset->ToggleInsetCursor(bv);
607             return result;
608         } else if (result == FINISHED) {
609             switch(action) {
610             case -1:
611             case LFUN_RIGHT:
612                 cursor.pos(inset_pos + 1);
613                 resetPos(bv->painter());
614                 break;
615             case LFUN_DOWN:
616                 moveDown(bv);
617                 break;
618             }
619             the_locking_inset = 0;
620             return DISPATCHED;
621         }
622     }
623     HideInsetCursor(bv);
624     switch (action) {
625         // Normal chars
626     case -1:
627         bv->text->SetUndo(bv->buffer(), Undo::INSERT, 
628             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
629             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
630         cutSelection(bv->buffer());
631         cursor = selection_start_cursor;
632         par->InsertChar(cursor.pos(), arg[0]);
633         SetCharFont(bv->buffer(), cursor.pos(), current_font);
634         cursor.pos(cursor.pos() + 1);
635         selection_start_cursor = selection_end_cursor = cursor;
636         UpdateLocal(bv, true);
637         break;
638         // --- Cursor Movements ---------------------------------------------
639     case LFUN_RIGHTSEL:
640         bv->text->FinishUndo();
641         moveRight(bv, false);
642         selection_end_cursor = cursor;
643         UpdateLocal(bv, false);
644         break;
645     case LFUN_RIGHT:
646         bv->text->FinishUndo();
647         result = moveRight(bv);
648         if (hasSelection()) {
649             selection_start_cursor = selection_end_cursor = cursor;
650             UpdateLocal(bv, false);
651         } else {
652             selection_start_cursor = selection_end_cursor = cursor;
653         }
654         break;
655     case LFUN_LEFTSEL:
656         bv->text->FinishUndo();
657         moveLeft(bv, false);
658         selection_end_cursor = cursor;
659         UpdateLocal(bv, false);
660         break;
661     case LFUN_LEFT:
662         bv->text->FinishUndo();
663         result= moveLeft(bv);
664         if (hasSelection()) {
665                 selection_start_cursor = selection_end_cursor = cursor;
666                 UpdateLocal(bv, false);
667         } else {
668                 selection_start_cursor = selection_end_cursor = cursor;
669         }
670         break;
671     case LFUN_DOWNSEL:
672         bv->text->FinishUndo();
673         moveDown(bv);
674         selection_end_cursor = cursor;
675         UpdateLocal(bv, false);
676         break;
677     case LFUN_DOWN:
678         bv->text->FinishUndo();
679         result = moveDown(bv);
680         if (hasSelection()) {
681             selection_start_cursor = selection_end_cursor = cursor;
682             UpdateLocal(bv, false);
683         } else {
684             selection_start_cursor = selection_end_cursor = cursor;
685         }
686         break;
687     case LFUN_UPSEL:
688         bv->text->FinishUndo();
689         moveUp(bv);
690         selection_end_cursor = cursor;
691         UpdateLocal(bv, false);
692         break;
693     case LFUN_UP:
694         bv->text->FinishUndo();
695         result = moveUp(bv);
696         if (hasSelection()) {
697             selection_start_cursor = selection_end_cursor = cursor;
698             UpdateLocal(bv, false);
699         } else {
700             selection_start_cursor = selection_end_cursor = cursor;
701         }
702         break;
703     case LFUN_BACKSPACE:
704         if (!cursor.pos()) {
705             if (hasSelection()) {
706                 selection_start_cursor = selection_end_cursor = cursor;
707                 UpdateLocal(bv, false);
708             }
709             break;
710         }
711         moveLeft(bv);
712     case LFUN_DELETE:
713     {
714         bv->text->SetUndo(bv->buffer(), Undo::DELETE, 
715             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
716             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
717         bool ret = true;
718         if (hasSelection()) {
719             LyXParagraph::size_type i = selection_start_cursor.pos();
720             for (; i < selection_end_cursor.pos(); ++i) {
721                 par->Erase(selection_start_cursor.pos());
722             }
723         } else
724             ret = Delete();
725         if (ret) { // we need update
726             selection_start_cursor = selection_end_cursor = cursor;
727             UpdateLocal(bv, true);
728         } else if (hasSelection()) {
729             selection_start_cursor = selection_end_cursor = cursor;
730             UpdateLocal(bv, false);
731         }
732     }
733     resetPos(bv->painter());
734     break;
735     case LFUN_CUT:
736         bv->text->SetUndo(bv->buffer(), Undo::DELETE, 
737           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
738           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
739
740         if (cutSelection(bv->buffer())) {
741             // we need update
742             cursor = selection_end_cursor = selection_start_cursor;
743             UpdateLocal(bv, true);
744         } else if (hasSelection()) {
745             selection_start_cursor = selection_end_cursor = cursor;
746             UpdateLocal(bv, false);
747         }
748         resetPos(bv->painter());
749         break;
750     case LFUN_COPY:
751         bv->text->FinishUndo();
752         if (copySelection(bv->buffer())) {
753             // we need update
754             selection_start_cursor = selection_end_cursor = cursor;
755             UpdateLocal(bv, true);
756         } else if (hasSelection()) {
757             selection_start_cursor = selection_end_cursor = cursor;
758             UpdateLocal(bv, false);
759         }
760         break;
761     case LFUN_PASTE:
762     {
763         bv->text->SetUndo(bv->buffer(), Undo::INSERT, 
764           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
765           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
766         if (pasteSelection(bv->buffer())) {
767             selection_start_cursor = selection_end_cursor = cursor;
768             UpdateLocal(bv, true);
769         }
770     }
771     resetPos(bv->painter());
772     break;
773     case LFUN_HOME:
774         bv->text->FinishUndo();
775         for(; cursor.pos() > rows[actrow].pos;) {
776                 cursor.x(cursor.x() - SingleWidth(bv->painter(), par, cursor.pos()));
777                 cursor.pos(cursor.pos() - 1);
778         }
779         
780         cursor.x(cursor.x() - SingleWidth(bv->painter(), par, cursor.pos()));
781         if (hasSelection()) {
782             selection_start_cursor = selection_end_cursor = cursor;
783             UpdateLocal(bv, false);
784         } else {
785             selection_start_cursor = selection_end_cursor = cursor;
786         }
787         resetPos(bv->painter());
788         break;
789     case LFUN_END:
790     {
791         bv->text->FinishUndo();
792         int checkpos = (int)rows[actrow + 1].pos;
793         if ((actrow + 2) < (int)rows.size())
794             --checkpos;
795         for(; cursor.pos() < checkpos;) {
796             cursor.x(cursor.x() + SingleWidth(bv->painter(), par, cursor.pos()));
797             cursor.pos(cursor.pos() + 1);
798         }
799         if (hasSelection()) {
800             selection_start_cursor = selection_end_cursor = cursor;
801             UpdateLocal(bv, false);
802         } else {
803             selection_start_cursor = selection_end_cursor = cursor;
804         }
805     }
806     resetPos(bv->painter());
807     break;
808     case LFUN_BREAKPARAGRAPH:
809     case LFUN_BREAKLINE:
810         if (!autoBreakRows)
811             return DISPATCHED;
812         bv->text->SetUndo(bv->buffer(), Undo::INSERT, 
813             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
814             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
815         par->InsertChar(cursor.pos(),LyXParagraph::META_NEWLINE);
816         SetCharFont(bv->buffer(), cursor.pos(),current_font);
817         UpdateLocal(bv, true);
818         cursor.pos(cursor.pos() + 1);
819         selection_start_cursor = selection_end_cursor = cursor;
820         resetPos(bv->painter());
821         break;
822     case LFUN_LAYOUT:
823     {
824       static LyXTextClass::size_type cur_layout = par->layout;
825       
826         // Derive layout number from given argument (string)
827         // and current buffer's textclass (number). */    
828         LyXTextClassList::ClassList::size_type tclass =
829             bv->buffer()->params.textclass;
830         std::pair <bool, LyXTextClass::size_type> layout = 
831             textclasslist.NumberOfLayout(tclass, arg);
832
833         // If the entry is obsolete, use the new one instead.
834         if (layout.first) {
835             string obs = textclasslist.Style(tclass,layout.second).
836                 obsoleted_by();
837             if (!obs.empty()) 
838                 layout = textclasslist.NumberOfLayout(tclass, obs);
839         }
840
841         // see if we found the layout number:
842         if (!layout.first) {
843             string msg = string(N_("Layout ")) + arg + N_(" not known");
844
845             bv->owner()->getMiniBuffer()->Set(msg);
846             break;
847         }
848
849         if (cur_layout != layout.second) {
850             cur_layout = layout.second;
851             text->SetLayout(bv, layout.second);
852             bv->owner()->getToolbar()->combox->select(cursor.par()->GetLayout()+1);
853             UpdateLocal(bv, true);
854         }
855     }
856     break;
857     default:
858         result = UNDISPATCHED;
859         break;
860     }
861     if (result != FINISHED) {
862         ShowInsetCursor(bv);
863     } else
864         bv->unlockInset(this);
865     return result;
866 }
867
868
869 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
870 {
871     TexRow texrow;
872     buf->latexParagraphs(os, par, 0, texrow);
873     return texrow.rows();
874 }
875
876
877 void InsetText::Validate(LaTeXFeatures & features) const
878 {
879     par->validate(features);
880 }
881
882
883 // Returns the width of a character at a certain spot
884 int InsetText::SingleWidth(Painter & pain, LyXParagraph * p, int pos) const
885 {
886     LyXFont font = GetDrawFont(current_view->buffer(), p, pos);
887     char c = p->GetChar(pos);
888
889     if (IsPrintable(c)) {
890         return lyxfont::width(c, font);
891     } else if (c == LyXParagraph::META_INSET) {
892         Inset const * tmpinset = p->GetInset(pos);
893         if (tmpinset)
894             return tmpinset->width(pain, font);
895         else
896             return 0;
897     } else if (IsSeparatorChar(c))
898         c = ' ';
899     else if (IsNewlineChar(c))
900         c = 'n';
901     return lyxfont::width(c, font);
902 }
903
904
905 // Returns the width of a character at a certain spot
906 void InsetText::SingleHeight(Painter & pain, LyXParagraph * p,int pos,
907                              int & asc, int & desc) const
908 {
909     LyXFont font = GetDrawFont(current_view->buffer(), p, pos);
910     char c = p->GetChar(pos);
911
912     asc = desc = 0;
913     if (c == LyXParagraph::META_INSET) {
914         Inset const * tmpinset=p->GetInset(pos);
915         if (tmpinset) {
916             asc = tmpinset->ascent(pain, font);
917             desc = tmpinset->descent(pain, font);
918         }
919     } else {
920         asc = lyxfont::maxAscent(font);
921         desc = lyxfont::maxDescent(font);
922     }
923     return;
924 }
925
926
927 // Gets the fully instantiated font at a given position in a paragraph
928 // Basically the same routine as LyXParagraph::getFont() in paragraph.C.
929 // The difference is that this one is used for displaying, and thus we
930 // are allowed to make cosmetic improvements. For instance make footnotes
931 // smaller. (Asger)
932 // If position is -1, we get the layout font of the paragraph.
933 // If position is -2, we get the font of the manual label of the paragraph.
934 LyXFont InsetText::GetFont(Buffer const * buf, LyXParagraph * p, int pos) const
935 {
936     char par_depth = p->GetDepth();
937
938     LyXLayout const & layout =
939             textclasslist.Style(buf->params.textclass, p->GetLayout());
940
941     // We specialize the 95% common case:
942     if (p->footnoteflag == LyXParagraph::NO_FOOTNOTE && !par_depth) {
943         if (pos >= 0) {
944             // 95% goes here
945             if (layout.labeltype == LABEL_MANUAL
946                 && pos < BeginningOfMainBody(buf, p)) {
947                 // 1% goes here
948                 return p->GetFontSettings(buf->params,
949                                           pos).realize(layout.reslabelfont);
950             } else
951                 return p->GetFontSettings(buf->params,
952                                           pos).realize(layout.resfont);
953         } else {
954             // 5% goes here.
955             // process layoutfont for pos == -1 and labelfont for pos < -1
956             if (pos == -1)
957                 return layout.resfont;
958             else
959                 return layout.reslabelfont;
960         }
961     }
962     // The uncommon case need not be optimized as much
963
964     LyXFont layoutfont, tmpfont;
965
966     if (pos >= 0){
967         // 95% goes here
968         if (pos < BeginningOfMainBody(buf, p)) {
969             // 1% goes here
970             layoutfont = layout.labelfont;
971         } else {
972             // 99% goes here
973             layoutfont = layout.font;
974         }
975         tmpfont = p->GetFontSettings(buf->params, pos);
976         tmpfont.realize(layoutfont);
977     } else{
978         // 5% goes here.
979         // process layoutfont for pos == -1 and labelfont for pos < -1
980         if (pos == -1)
981             tmpfont = layout.font;
982         else
983             tmpfont = layout.labelfont;
984     }
985     
986     // Resolve against environment font information
987     //if (par->GetDepth()){ // already in while condition
988     while (p && par_depth && !tmpfont.resolved()) {
989         p = p->DepthHook(par_depth - 1);
990         if (p) {
991             tmpfont.realize(textclasslist.Style(buf->params.textclass,
992                                                 p->GetLayout()).font);
993             par_depth = p->GetDepth();
994         }
995     }
996     tmpfont.realize((textclasslist.TextClass(buf->params.textclass).
997                     defaultfont()));
998     return tmpfont;
999 }
1000
1001
1002 // the font for drawing may be different from the real font
1003 LyXFont InsetText::GetDrawFont(Buffer const * buf, LyXParagraph * p, int pos) const
1004 {
1005     return GetFont(buf, p, pos);
1006 }
1007
1008
1009 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1010 {
1011     if (textclasslist.Style(buf->params.textclass,
1012                             p->GetLayout()).labeltype != LABEL_MANUAL)
1013         return 0;
1014     else
1015         return p->BeginningOfMainBody();
1016 }
1017
1018
1019 void InsetText::GetCursorPos(int & x, int & y) const
1020 {
1021     x = cursor.x();
1022     y = cursor.y();
1023 }
1024
1025
1026 int InsetText::InsetInInsetY()
1027 {
1028     if (!the_locking_inset)
1029         return 0;
1030
1031     return (inset_y + the_locking_inset->InsetInInsetY());
1032 }
1033
1034
1035 void InsetText::ToggleInsetCursor(BufferView * bv)
1036 {
1037     if (the_locking_inset) {
1038         the_locking_inset->ToggleInsetCursor(bv);
1039         return;
1040     }
1041
1042     LyXFont font = GetDrawFont(bv->buffer(), par, text->cursor.pos());
1043
1044     int asc = lyxfont::maxAscent(font);
1045     int desc = lyxfont::maxDescent(font);
1046   
1047     asc = text->cursor.row()->ascent_of_text();
1048     desc = text->cursor.row()->height() - asc;
1049     if (cursor_visible)
1050         bv->hideLockedInsetCursor();
1051     else
1052         bv->showLockedInsetCursor(text->cursor.x(), text->cursor.y(),
1053                                   asc, desc);
1054     cursor_visible = !cursor_visible;
1055 }
1056
1057
1058 void InsetText::ShowInsetCursor(BufferView * bv)
1059 {
1060     if (the_locking_inset) {
1061         the_locking_inset->ShowInsetCursor(bv);
1062         return;
1063     }
1064     if (!cursor_visible) {
1065         LyXFont font = GetDrawFont(bv->buffer(), par, cursor.pos());
1066         
1067         int asc = lyxfont::maxAscent(font);
1068         int desc = lyxfont::maxDescent(font);
1069         asc = text->cursor.row()->ascent_of_text();
1070         desc = text->cursor.row()->height() - asc;
1071         bv->fitLockedInsetCursor(text->cursor.x(), text->cursor.y(), asc, desc);
1072         bv->showLockedInsetCursor(text->cursor.x(), text->cursor.y(), asc, desc);
1073         cursor_visible = true;
1074     }
1075 }
1076
1077
1078 void InsetText::HideInsetCursor(BufferView * bv)
1079 {
1080     if (cursor_visible) {
1081         bv->hideLockedInsetCursor();
1082         cursor_visible = false;
1083     }
1084     if (the_locking_inset)
1085         the_locking_inset->HideInsetCursor(bv);
1086 }
1087
1088
1089 void InsetText::setPos(Painter & pain, int x, int y) const
1090 {
1091     x -= drawTextXOffset;
1092     y -= drawTextYOffset;
1093     // search right X-pos x==0 -> top_x
1094     cursor.pos(0);
1095     actrow = 0;
1096     cursor.y(top_baseline);
1097     y += cursor.y();
1098     for(unsigned int i = 1;
1099         (long(cursor.y() + rows[i - 1].desc) < y)
1100                 && (i < rows.size() - 1); ++i) {
1101         cursor.y(rows[i].baseline);
1102         cursor.pos(rows[i].pos);
1103         actrow = i;
1104     }
1105     cursor.y(cursor.y() - top_baseline);
1106     cursor.x(top_x + 2); // 2 = frame width
1107     x += cursor.x();
1108
1109     int swh;
1110     int sw = swh = SingleWidth(pain, par, cursor.pos());
1111     if (par->GetChar(cursor.pos()) != LyXParagraph::META_INSET)
1112         swh /= 2;
1113     int checkpos = rows[actrow + 1].pos;
1114     if ((actrow + 2) < (int)rows.size())
1115         --checkpos;
1116     while ((cursor.pos() < checkpos) && ((cursor.x() + swh) < x)) {
1117         cursor.x(cursor.x() + sw);
1118         cursor.pos(cursor.pos() + 1);
1119         sw = swh = SingleWidth(pain, par, cursor.pos());
1120         if (par->GetChar(cursor.pos())!=LyXParagraph::META_INSET)
1121             swh /= 2;
1122     }
1123 }
1124
1125
1126 void InsetText::resetPos(Painter & pain) const
1127 {
1128     cursor.par(par);
1129
1130     if (!rows.size())
1131         return;
1132
1133     int old_pos = cursor.pos();
1134
1135     cursor.y(top_baseline);
1136     actrow = 0;
1137     for(unsigned int i = 0;
1138         i < (rows.size() - 1) && rows[i].pos <= cursor.pos();
1139         ++i) {
1140         cursor.y(rows[i].baseline);
1141         actrow = i;
1142     }
1143     cursor.y(cursor.y() - top_baseline);
1144     setPos(pain, 0, cursor.y());
1145     cursor.x(top_x + 2); // 2 = frame width
1146     while(cursor.pos() < old_pos) {
1147         cursor.x(cursor.x() + SingleWidth(pain, par,cursor.pos()));
1148         cursor.pos(cursor.pos() + 1);
1149     }
1150 }
1151
1152
1153 UpdatableInset::RESULT
1154 InsetText::moveRight(BufferView * bv, bool activate_inset)
1155 {
1156     if (cursor.pos() >= par->Last())
1157         return FINISHED;
1158     if (activate_inset && checkAndActivateInset(bv)) {
1159         return DISPATCHED;
1160     }
1161     cursor.pos(cursor.pos() + 1);
1162     resetPos(bv->painter());
1163     real_current_font = current_font =GetFont(bv->buffer(), par, cursor.pos());
1164     return DISPATCHED_NOUPDATE;
1165 }
1166
1167
1168 UpdatableInset::RESULT
1169 InsetText::moveLeft(BufferView * bv, bool activate_inset)
1170 {
1171     if (cursor.pos() <= 0)
1172         return FINISHED;
1173     cursor.pos(cursor.pos() - 1);
1174     resetPos(bv->painter());
1175     if (activate_inset)
1176         if (checkAndActivateInset(bv, -1, -1))
1177             return DISPATCHED;
1178     return DISPATCHED_NOUPDATE;
1179 }
1180
1181
1182 UpdatableInset::RESULT
1183 InsetText::moveUp(BufferView * bv)
1184 {
1185     if (!actrow)
1186         return FINISHED;
1187     cursor.y(rows[actrow - 1].baseline - top_baseline);
1188     if (cursor.x_fix() < 0)
1189         cursor.x_fix(cursor.x());
1190     setPos(bv->painter(),
1191            cursor.x_fix() - top_x + drawTextXOffset, cursor.y());
1192     return DISPATCHED_NOUPDATE;
1193 }
1194
1195
1196 UpdatableInset::RESULT
1197 InsetText::moveDown(BufferView * bv)
1198 {
1199     if (actrow >= int(rows.size() - 2))
1200         return FINISHED;
1201     cursor.y(rows[actrow + 1].baseline - top_baseline);
1202     if (cursor.x_fix() < 0)
1203         cursor.x_fix(cursor.x());
1204     setPos(bv->painter(),
1205            cursor.x_fix() - top_x + drawTextXOffset, cursor.y());
1206     return DISPATCHED_NOUPDATE;
1207 }
1208
1209
1210 bool InsetText::Delete()
1211 {
1212     if ((par->GetChar(cursor.pos())==LyXParagraph::META_INSET) &&
1213         !par->GetInset(cursor.pos())->Deletable()) {
1214         return false;
1215     }
1216     par->Erase(cursor.pos());
1217     return true;
1218 }
1219
1220
1221 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1222 {
1223     if (the_locking_inset) {
1224         if (the_locking_inset->InsertInsetAllowed(inset))
1225             return the_locking_inset->InsertInset(bv, inset);
1226         return false;
1227     }
1228     bv->text->SetUndo(bv->buffer(), Undo::INSERT, 
1229               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1230               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1231     if (inset->Editable() == Inset::IS_EDITABLE) {
1232         UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1233         i->setOwner(static_cast<UpdatableInset *>(this));
1234     }
1235     par->InsertChar(cursor.pos(), LyXParagraph::META_INSET);
1236     par->InsertInset(cursor.pos(), inset);
1237     if (hasSelection()) {
1238         selection_start_cursor = selection_end_cursor = cursor;
1239     } else {
1240         selection_start_cursor = selection_end_cursor = cursor;
1241     }
1242     UpdateLocal(bv, true);
1243     static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1244     return true;
1245 }
1246
1247
1248 UpdatableInset * InsetText::GetLockingInset()
1249 {
1250     return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1251 }
1252
1253
1254 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1255 {
1256     if (c == LyxCode())
1257         return this;
1258     if (the_locking_inset)
1259         return the_locking_inset->GetFirstLockingInsetOfType(c);
1260     return 0;
1261 }
1262
1263
1264 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1265 {
1266     // if there is no selection just set the current_font
1267     if (!hasSelection()) {
1268         // Determine basis font
1269         LyXFont layoutfont;
1270         if (cursor.pos() < BeginningOfMainBody(bv->buffer(), par))
1271             layoutfont = GetFont(bv->buffer(), par, -2);
1272         else
1273             layoutfont = GetFont(bv->buffer(), par, -1);
1274         
1275         // Update current font
1276         real_current_font.update(font, bv->buffer()->params.language_info,
1277                                  toggleall);
1278         
1279         // Reduce to implicit settings
1280         current_font = real_current_font;
1281         current_font.reduce(layoutfont);
1282         // And resolve it completely
1283         real_current_font.realize(layoutfont);
1284         return;
1285     }
1286     
1287     int s_start, s_end;
1288     if (selection_start_cursor.pos() > selection_end_cursor.pos()) {
1289         s_start = selection_end_cursor.pos();
1290         s_end = selection_start_cursor.pos();
1291     } else {
1292         s_start = selection_start_cursor.pos();
1293         s_end = selection_end_cursor.pos();
1294     }
1295     LyXFont newfont;
1296     while(s_start < s_end) {
1297         newfont = GetFont(bv->buffer(), par,s_start);
1298         newfont.update(font, bv->buffer()->params.language_info, toggleall);
1299         SetCharFont(bv->buffer(), s_start, newfont);
1300         ++s_start;
1301     }
1302     UpdateLocal(bv, true);
1303 }
1304
1305
1306 void InsetText::SetCharFont(Buffer const * buf, int pos, LyXFont const & f) // 
1307 {
1308     /* let the insets convert their font */
1309         LyXFont font(f);
1310         
1311     if (par->GetChar(pos) == LyXParagraph::META_INSET) {
1312         if (par->GetInset(pos))
1313             font = par->GetInset(pos)->ConvertFont(font);
1314     }
1315     LyXLayout const & layout =
1316             textclasslist.Style(buf->params.textclass,par->GetLayout());
1317
1318     // Get concrete layout font to reduce against
1319     LyXFont layoutfont;
1320
1321     if (pos < BeginningOfMainBody(buf, par))
1322         layoutfont = layout.labelfont;
1323     else
1324         layoutfont = layout.font;
1325
1326
1327     layoutfont.realize((textclasslist.TextClass(buf->params.textclass).
1328                        defaultfont()));
1329
1330     // Now, reduce font against full layout font
1331     font.reduce(layoutfont);
1332
1333     par->SetFont(pos, font);
1334 }
1335
1336
1337 void InsetText::computeTextRows(Painter & pain) const
1338 {
1339     int p,
1340         nwp = 0,
1341         asc = 0,
1342         desc = 0,
1343         oasc = 0,
1344         odesc = 0,
1345         wordAscent,
1346         wordDescent;
1347     row_struct row;
1348
1349     if (rows.size())
1350             rows.clear();
1351     int width = wordAscent = wordDescent = 0;
1352     insetWidth = maxAscent = maxDescent = 0;
1353     row.asc      = 0;
1354     row.desc     = 0;
1355     row.pos      = 0;
1356     row.baseline = 0;
1357     rows.push_back(row);
1358     if (!autoBreakRows || (getMaxTextWidth(pain, this) < 0)) {
1359         for(p = 0; p < par->Last(); ++p) {
1360             insetWidth += SingleWidth(pain, par, p);
1361             SingleHeight(pain, par, p, asc, desc);
1362             maxAscent = max(maxAscent, asc);
1363             maxDescent = max(maxDescent, desc);
1364         }
1365         insetWidth += (2 * TEXT_TO_INSET_OFFSET);
1366         rows[0].asc = maxAscent;
1367         rows[0].desc = maxDescent;
1368         // alocate a dummy row for the endpos
1369         row.pos = par->Last();
1370         rows.push_back(row);
1371         return;
1372     }
1373
1374     bool is_first_word_in_row = true;
1375     int cw, lastWordWidth = 0;
1376     int maxWidth = getMaxTextWidth(pain, this);
1377     // if we auto break rows than the insetwidth should be always the max
1378     // width as the display is stable it may get larger if we have a really
1379     // large word below and we draw it!!!
1380     insetWidth = maxWidth;
1381
1382     for(p = 0; p < par->Last(); ++p) {
1383         if (par->IsNewline(p)) {
1384             rows.back().asc = wordAscent;
1385             rows.back().desc = wordDescent;
1386             row.pos = p+1;
1387             rows.push_back(row);
1388             nwp = p+1;
1389             width = lastWordWidth = 0;
1390             oasc = odesc = wordAscent = wordDescent = 0;
1391             is_first_word_in_row = true;
1392             continue;
1393         }
1394         cw = SingleWidth(pain, par, p);
1395         Inset * inset = 0;
1396         if (par->GetChar(p) == LyXParagraph::META_INSET)
1397             inset = par->GetInset(p);
1398         if (inset && inset->display()) {
1399             inset->setOwner(const_cast<InsetText *>(this));
1400             if (cw > insetWidth)
1401                 insetWidth = cw;
1402             if (!is_first_word_in_row || (p != nwp)) {
1403                 oasc = max(oasc, wordAscent);
1404                 odesc = max(odesc, wordDescent);
1405                 rows.back().asc = oasc;
1406                 rows.back().desc = odesc;
1407                 row.pos = p;
1408                 rows.push_back(row);
1409             }
1410             SingleHeight(pain, par, p, asc, desc);
1411             rows.back().asc = asc;
1412             rows.back().desc = desc;
1413             row.pos = nwp = p + 1;
1414             rows.push_back(row);
1415             width = lastWordWidth = 0;
1416             oasc = odesc = wordAscent = wordDescent = 0;
1417             is_first_word_in_row = true;
1418             continue;
1419         }
1420         SingleHeight(pain, par, p, asc, desc);
1421         width += cw;
1422         lastWordWidth += cw;
1423         if (width > maxWidth) {
1424             if (is_first_word_in_row) {
1425                 if (!(width-cw)) { // only this character in word
1426                     rows.back().asc = asc;
1427                     rows.back().desc = desc;
1428                     row.pos = p+1;
1429                     rows.push_back(row);
1430                     oasc = 0;
1431                     odesc = 0;
1432                     wordAscent = 0;
1433                     wordDescent = 0;
1434                     nwp = p + 1;
1435                     lastWordWidth = width = 0;
1436                 } else {
1437                     rows.back().asc = wordAscent;
1438                     rows.back().desc = wordDescent;
1439                     row.pos = p;
1440                     rows.push_back(row);
1441                     oasc = 0;
1442                     odesc = 0;
1443                     wordAscent = asc;
1444                     wordDescent = desc;
1445                     lastWordWidth = width = cw;
1446                     nwp = p;
1447                 }
1448             } else {
1449                 rows.back().asc = oasc;
1450                 rows.back().desc = odesc;
1451                 row.pos = nwp;
1452                 rows.push_back(row);
1453                 oasc = wordAscent;
1454                 odesc = wordDescent;
1455                 width = lastWordWidth;  
1456                 wordAscent = max(wordAscent, asc);
1457                 wordDescent = max(wordDescent, desc);
1458                 is_first_word_in_row = true;
1459             }
1460         } else {
1461             wordAscent = max(wordAscent, asc);
1462             wordDescent = max(wordDescent, desc);
1463         }
1464         if (par->IsSeparator(p) || inset) {
1465             if (inset) {
1466                 inset->setOwner(const_cast<InsetText *>(this));
1467                 if (cw > maxWidth)
1468                     insetWidth = cw;
1469             }
1470             oasc = max(oasc, wordAscent);
1471             odesc = max(odesc, wordDescent);
1472             wordAscent = wordDescent = lastWordWidth = 0;
1473             nwp = p + 1;
1474             is_first_word_in_row = false;
1475         }
1476     }
1477     // if we have some data in the paragraph we have ascent/descent
1478     if (p) {
1479         // assign last row data
1480         rows.back().asc = max(oasc, wordAscent);
1481         rows.back().desc = max(odesc, wordDescent);
1482     }
1483     insetWidth += (2 * TEXT_TO_INSET_OFFSET);
1484     // alocate a dummy row for the endpos
1485     row.pos = par->Last();
1486     rows.push_back(row);
1487     // calculate maxAscent/Descent
1488     maxAscent = rows[0].asc;
1489     maxDescent = rows[0].desc;
1490     for (RowList::size_type i = 1; i < rows.size() - 1; ++i) {
1491         maxDescent += rows[i].asc + rows[i].desc + interline_space;
1492     }
1493 }
1494
1495
1496 void InsetText::computeBaselines(int baseline) const
1497 {
1498     rows[0].baseline = baseline;
1499     for (unsigned int i = 1; i < rows.size() - 1; i++) {
1500         rows[i].baseline = rows[i - 1].baseline + rows[i - 1].desc + 
1501             rows[i].asc + interline_space;
1502     }
1503 }
1504
1505
1506 void InsetText::UpdateLocal(BufferView * bv, bool flag)
1507 {
1508     if (flag) {
1509         text->FullRebreak(bv);
1510         computeTextRows(bv->painter());
1511         computeBaselines(top_baseline);
1512     }
1513     bv->updateInset(this, flag);
1514     if (flag)
1515         resetPos(bv->painter());
1516     bv->owner()->getToolbar()->combox->select(cursor.par()->GetLayout()+1);
1517 }
1518
1519
1520 bool InsetText::cutSelection(Buffer const * buf)
1521 {
1522     if (!hasSelection())
1523         return false;
1524
1525     CutAndPaste cap;
1526
1527     LyXParagraph * endpar = par;
1528     int start, end;
1529     if (selection_start_cursor.pos() > selection_end_cursor.pos()) {
1530             start = selection_end_cursor.pos();
1531             end = selection_start_cursor.pos();
1532     } else {
1533             start = selection_start_cursor.pos();
1534             end = selection_end_cursor.pos();
1535     }
1536
1537     return cap.cutSelection(par, &endpar, start, end, buf->params.textclass);
1538 }
1539
1540
1541 bool InsetText::copySelection(Buffer const * buf)
1542 {
1543     if (!hasSelection())
1544         return false;
1545
1546     CutAndPaste cap;
1547
1548     int start, end;
1549     if (selection_start_cursor.pos() > selection_end_cursor.pos()) {
1550             start = selection_end_cursor.pos();
1551             end = selection_start_cursor.pos();
1552     } else {
1553             start = selection_start_cursor.pos();
1554             end = selection_end_cursor.pos();
1555     }
1556     return cap.copySelection(par, par, start, end, buf->params.textclass);
1557 }
1558
1559
1560 bool InsetText::pasteSelection(Buffer const * buf)
1561 {
1562     CutAndPaste cap;
1563
1564     if (cap.nrOfParagraphs() > 1) {
1565         WriteAlert(_("Impossible operation"),
1566                    _("Cannot include more than one paragraph!"),
1567                    _("Sorry."));
1568         return false;
1569     }
1570     LyXParagraph * endpar;
1571     LyXParagraph * actpar = par;
1572
1573     int pos = cursor.pos();
1574     bool ret = cap.pasteSelection(&actpar, &endpar, pos,buf->params.textclass);
1575     cursor.pos(pos);
1576     return ret;
1577 }
1578
1579
1580 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1581                                       int button)
1582 {
1583     if (par->GetChar(cursor.pos()) == LyXParagraph::META_INSET) {
1584         UpdatableInset * inset =
1585             static_cast<UpdatableInset*>(par->GetInset(cursor.pos()));
1586         LyXFont font = GetFont(bv->buffer(), par, cursor.pos());
1587         if (x < 0)
1588             x = inset->width(bv->painter(), font);
1589         if (y < 0)
1590             y = inset->descent(bv->painter(), font);
1591         inset_x = cursor.x() - top_x + drawTextXOffset;
1592         inset_y = cursor.y() + drawTextYOffset;
1593         inset->Edit(bv, x - inset_x, y - inset_y, button);
1594         if (!the_locking_inset)
1595             return false;
1596         UpdateLocal(bv, true);
1597         return true;
1598     }
1599     return false;
1600 }
1601
1602
1603 int InsetText::getMaxTextWidth(Painter & pain, UpdatableInset const * inset) const
1604 {
1605 //    int w=getMaxWidth(pain, inset);
1606 //    return (w - x);
1607     return getMaxWidth(pain, inset) - 4; // 2+2 width of eventual border
1608 }
1609
1610 void InsetText::SetParagraphData(LyXParagraph *p)
1611 {
1612     if (par)
1613         delete par;
1614     par = p->Clone();
1615     par->SetInsetOwner(this);
1616     init_inset = true;
1617 }
1618
1619 void InsetText::SetAutoBreakRows(bool flag)
1620 {
1621     if (flag != autoBreakRows) {
1622         autoBreakRows = flag;
1623         init_inset = true;
1624     }
1625 }
1626
1627 void InsetText::SetDrawLockedFrame(bool flag)
1628 {
1629     if (flag != drawLockedFrame) {
1630         drawLockedFrame = flag;
1631         init_inset = true;
1632     }
1633 }
1634
1635 void InsetText::SetFrameColor(LColor::color col)
1636 {
1637     if (frame_color != col) {
1638         frame_color = col;
1639         init_inset = true;
1640     }
1641 }