]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
b029c52dd98f577033ffb9a583e2a881cd2ecc18
[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 The LyX Team.
8  *
9  * ======================================================
10  */
11
12 #include <config.h>
13
14 #include <fstream>
15 #include <algorithm>
16 using std::ifstream;
17 using std::min;
18 using std::max;
19
20 #include <cstdlib>
21
22 #ifdef __GNUG__
23 #pragma implementation
24 #endif
25
26 #include "insettext.h"
27 #include "lyxlex.h"
28 #include "debug.h"
29 #include "lyxfont.h"
30 #include "lyxlex.h"
31 #include "commandtags.h"
32 #include "buffer.h"
33 #include "LyXView.h"
34 #include "BufferView.h"
35 #include "support/textutils.h"
36 #include "layout.h"
37 #include "insetlatexaccent.h"
38 #include "insetquotes.h"
39 #include "mathed/formulamacro.h"
40 #include "figinset.h"
41 #include "insetinfo.h"
42 #include "insetinclude.h"
43 #include "insetbib.h"
44 #include "insetcommand.h"
45 #include "insetindex.h"
46 #include "insetlabel.h"
47 #include "insetref.h"
48 //#include "insettabular.h"
49 #include "insetert.h"
50 #include "insetspecialchar.h"
51 #include "LaTeXFeatures.h"
52 #include "Painter.h"
53 #include "lyx_gui_misc.h"
54 #include "support/LAssert.h"
55
56 extern unsigned char getCurrentTextClass(Buffer *);
57
58 InsetText::InsetText(Buffer * buf)
59 {
60     par = new LyXParagraph();
61     the_locking_inset = 0;
62     buffer = buf;
63     cursor_visible = false;
64     maxWidth = old_x = -1;
65     actpos = selection_start = selection_end = 0;
66     interline_space = 1;
67     no_selection = false;
68     init_inset = true;
69     maxAscent = maxDescent = insetWidth = widthOffset = 0;
70     autoBreakRows = false;
71     xpos = 0.0;
72 }
73
74
75 InsetText::InsetText(InsetText const & ins, Buffer * buf)
76 {
77     par = new LyXParagraph(ins.par);
78     the_locking_inset = 0;
79     buffer = buf;
80     cursor_visible = false;
81     maxWidth = old_x = -1;
82     actpos = selection_start = selection_end = 0;
83     interline_space = 1;
84     no_selection = false;
85     init_inset = true;
86     maxAscent = maxDescent = insetWidth = widthOffset = 0;
87     autoBreakRows = false;
88     xpos = 0.0;
89 }
90
91
92 InsetText::~InsetText()
93 {
94     delete par;
95 }
96
97
98 Inset * InsetText::Clone() const
99 {
100     InsetText * t = new InsetText(*this, buffer);
101     return t;
102 }
103
104
105 void InsetText::Write(ostream & os) const
106 {
107     os << "Text\n";
108     WriteParagraphData(os);
109 }
110
111
112 void InsetText::WriteParagraphData(ostream & os) const
113 {
114     par->writeFile(os, buffer->params, 0, 0);
115 }
116
117
118 void InsetText::Read(LyXLex & lex)
119 {
120     string token, tmptok;
121     int pos = 0;
122     LyXParagraph * return_par = 0;
123     char depth = 0; // signed or unsigned?
124     LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
125     LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
126     LyXFont font(LyXFont::ALL_INHERIT);
127
128     delete par;
129     par = new LyXParagraph;
130     
131     while (lex.IsOK()) {
132         lex.nextToken();
133         token = lex.GetString();
134         if (token.empty())
135             continue;
136         if (token == "\\end_inset")
137             break;
138         if (buffer->parseSingleLyXformat2Token(lex, par, return_par,
139                                                token, pos, depth,
140                                                font, footnoteflag,
141                                                footnotekind)) {
142             // the_end read this should NEVER happen
143             lex.printError("\\the_end read in inset! Error in document!");
144             return;
145         }
146     }
147     if (token != "\\end_inset") {
148         lex.printError("Missing \\end_inset at this point. "
149                        "Read: `$$Token'");
150     }
151     init_inset = true;
152 }
153
154
155 int InsetText::ascent(Painter & pain, LyXFont const & font) const
156 {
157     if (init_inset) {
158         computeTextRows(pain, xpos);
159         init_inset = false;
160     }
161     if (maxAscent)
162         return maxAscent;
163     return font.maxAscent();
164 }
165
166
167 int InsetText::descent(Painter & pain, LyXFont const & font) const
168 {
169     if (init_inset) {
170         computeTextRows(pain, xpos);
171         init_inset = false;
172     }
173     if (maxDescent)
174         return maxDescent;
175     return font.maxDescent();
176 }
177
178
179 int InsetText::width(Painter & pain, LyXFont const &) const
180 {
181     if (init_inset) {
182         computeTextRows(pain, xpos);
183         init_inset = false;
184     }
185     return insetWidth;
186 }
187
188
189 void InsetText::draw(Painter & pain, LyXFont const & f,
190                      int baseline, float & x) const
191 {
192     xpos = x;
193     computeTextRows(pain, x);
194     UpdatableInset::draw(pain, f, baseline, x);
195     
196     bool do_reset_pos = (x != top_x) || (baseline != top_baseline);
197     top_x = int(x);
198     top_baseline = baseline;
199     computeBaselines(baseline);
200     for(RowList::size_type r = 0; r < rows.size() - 1; ++r) {
201         drawRowSelection(pain, rows[r].pos, rows[r + 1].pos, r, 
202                          rows[r].baseline, x);
203         drawRowText(pain, rows[r].pos, rows[r + 1].pos, rows[r].baseline, x);
204     }
205     x += insetWidth;
206     if (!the_locking_inset && do_reset_pos) {
207 //        HideInsetCursor(bv);
208 //        resetPos(bv);
209 //        ShowInsetCursor(bv);
210     }
211 }
212
213
214 void InsetText::drawRowSelection(Painter & pain, int startpos, int endpos,
215                                  int row, int baseline, float x) const
216 {
217     if (!hasSelection())
218         return;
219
220     int s_start, s_end;
221     if (selection_start > selection_end) {
222         s_start = selection_end;
223         s_end = selection_start;
224     } else {
225         s_start = selection_start;
226         s_end = selection_end;
227     }
228     if ((s_start > endpos) || (s_end < startpos))
229         return;
230     
231     int esel_x;
232     int ssel_x = esel_x = int(x);
233     LyXFont font;
234     int p = startpos;
235     for(; p < endpos; ++p) {
236         if (p == s_start)
237             ssel_x = int(x);
238         if ((p >= s_start) && (p <= s_end))
239             esel_x = int(x);
240         char ch = par->GetChar(p);
241         font = GetFont(par,p);
242         if (IsFloatChar(ch)) {
243             // skip for now
244         } else if (ch == LyXParagraph::META_INSET) {
245             Inset const * tmpinset = par->GetInset(p);
246             x += tmpinset->width(pain, font);
247         } else {
248             x += pain.width(ch,font);
249         }
250     }
251     if (p == s_start)
252         ssel_x = int(x);
253     if ((p >= s_start) && (p <= s_end))
254         esel_x = int(x);
255     if (ssel_x < esel_x) {
256         pain.fillRectangle(int(ssel_x), baseline-rows[row].asc,
257                            int(esel_x - ssel_x),
258                            rows[row].asc + rows[row].desc,
259                            LColor::selection);
260     }
261 }
262
263
264 void InsetText::drawRowText(Painter & pain, int startpos, int endpos,
265                             int baseline, float x) const
266 {
267     Assert(endpos <= par->Last());
268
269     for(int p = startpos; p < endpos; ++p) {
270         char ch = par->GetChar(p);
271         LyXFont font = GetFont(par,p);
272         if (IsFloatChar(ch)) {
273             // skip for now
274         } else if (ch == LyXParagraph::META_INSET) {
275             Inset * tmpinset = par->GetInset(p);
276             if (tmpinset) 
277                 tmpinset->draw(pain, font, baseline, x);
278         } else {
279             pain.text(int(x), baseline, ch, font);
280             x += pain.width(ch,font);
281         }
282     }
283 }
284
285
286 char const * InsetText::EditMessage() const
287 {
288     return _("Opened Text Inset");
289 }
290
291
292 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
293 {
294     UpdatableInset::Edit(bv, x, y, button);
295
296     bv->lockInset(this);
297     the_locking_inset = 0;
298     inset_pos = inset_x = inset_y = 0;
299     no_selection = true;
300     setPos(bv, x,y);
301     selection_start = selection_end = actpos;
302     current_font = real_current_font = GetFont(par, actpos);
303 }
304
305
306 void InsetText::InsetUnlock(BufferView * bv)
307 {
308     if (the_locking_inset)
309         the_locking_inset->InsetUnlock(bv);
310     HideInsetCursor(bv);
311     if (hasSelection()) {
312         selection_start = selection_end = actpos;
313         UpdateLocal(bv, false);
314     }
315     the_locking_inset = 0;
316     no_selection = false;
317 }
318
319
320 bool InsetText::UnlockInsetInInset(BufferView * bv, Inset * inset, bool lr)
321 {
322     if (!the_locking_inset)
323         return false;
324     if (the_locking_inset == inset) {
325         the_locking_inset->InsetUnlock(bv);
326         the_locking_inset = 0;
327         if (lr)
328             moveRight(bv, false);
329         return true;
330     }
331     return the_locking_inset->UnlockInsetInInset(bv, inset,lr);
332 }
333
334
335 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
336 {
337     if (!the_locking_inset)
338         return false;
339     if (the_locking_inset != inset)
340         return the_locking_inset->UpdateInsetInInset(bv, inset);
341     float x = inset_x;
342     inset->draw(bv->getPainter(), real_current_font, inset_y, x);
343     UpdateLocal(bv, true);
344     return true;
345 }
346
347
348 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
349 {
350     if (the_locking_inset) {
351         the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
352         return;
353     }
354     no_selection = false;
355 }
356
357
358 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
359 {
360     if (hasSelection()) {
361         selection_start = selection_end = actpos;
362         UpdateLocal(bv, false);
363     }
364     no_selection = false;
365     if (the_locking_inset) {
366         setPos(bv, x, y, false);
367         UpdatableInset
368             *inset = 0;
369         if (par->GetChar(actpos) == LyXParagraph::META_INSET)
370             inset = static_cast<UpdatableInset*>(par->GetInset(actpos));
371         if (the_locking_inset == inset) {
372             the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
373             return;
374         } else if (inset) {
375             // otherwise unlock the_locking_inset and lock the new inset
376             inset_x = cx-top_x;
377             inset_y = cy;
378             inset_pos = actpos;
379             the_locking_inset->InsetUnlock(bv);
380             the_locking_inset = inset;
381             the_locking_inset->Edit(bv, x - inset_x, y - inset_y, button);
382             return;
383         }
384         // otherwise only unlock the_locking_inset
385         the_locking_inset->InsetUnlock(bv);
386     }
387     HideInsetCursor(bv);
388     the_locking_inset = 0;
389     setPos(bv, x, y);
390     selection_start = selection_end = actpos;
391     if (!the_locking_inset)
392         ShowInsetCursor(bv);
393 }
394
395
396 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int button)
397 {
398     if (the_locking_inset) {
399         the_locking_inset->InsetMotionNotify(bv, x - inset_x,
400                                              y - inset_y,button);
401         return;
402     }
403     if (!no_selection) {
404         int old = selection_end;
405         setPos(bv, x, y, false);
406         selection_end = actpos;
407         if (old != selection_end)
408             UpdateLocal(bv, false);
409     }
410     no_selection = false;
411 }
412
413
414 void InsetText::InsetKeyPress(XKeyEvent * xke)
415 {
416     if (the_locking_inset) {
417         the_locking_inset->InsetKeyPress(xke);
418         return;
419     }
420 }
421
422
423 UpdatableInset::RESULT
424 InsetText::LocalDispatch(BufferView * bv,
425                          int action, string const & arg)
426 {
427     no_selection = false;
428     if (UpdatableInset::LocalDispatch(bv, action, arg)) {
429         resetPos(bv);
430         return DISPATCHED;
431     }
432
433     UpdatableInset::RESULT
434         result=DISPATCHED;
435
436     if ((action < 0) && arg.empty())
437         return FINISHED;
438
439     if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
440         (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
441         old_x = -1;
442     if (the_locking_inset) {
443         result = the_locking_inset->LocalDispatch(bv, action, arg);
444         if (result == DISPATCHED) {
445             the_locking_inset->ToggleInsetCursor(bv);
446             UpdateLocal(bv, false);
447             the_locking_inset->ToggleInsetCursor(bv);
448             return result;
449         } else if (result == FINISHED) {
450             if ((action == LFUN_RIGHT) || (action == -1)) {
451                 actpos = inset_pos + 1;
452                 resetPos(bv);
453             }
454             the_locking_inset = 0;
455             return DISPATCHED;
456         }
457     }
458     HideInsetCursor(bv);
459     switch (action) {
460         // Normal chars
461       case -1:
462           par->InsertChar(actpos,arg[0]);
463           par->SetFont(actpos,real_current_font);
464           UpdateLocal(bv, true);
465           ++actpos;
466           selection_start = selection_end = actpos;
467           resetPos(bv);
468           break;
469         // --- Cursor Movements ---------------------------------------------
470       case LFUN_RIGHTSEL:
471           moveRight(bv, false);
472           selection_end = actpos;
473           UpdateLocal(bv, false);
474           break;
475       case LFUN_RIGHT:
476           result= DISPATCH_RESULT(moveRight(bv));
477           if (hasSelection()) {
478               selection_start = selection_end = actpos;
479               UpdateLocal(bv, false);
480           } else {
481               selection_start = selection_end = actpos;
482           }
483           break;
484       case LFUN_LEFTSEL:
485           moveLeft(bv, false);
486           selection_end = actpos;
487           UpdateLocal(bv, false);
488           break;
489       case LFUN_LEFT:
490           result= DISPATCH_RESULT(moveLeft(bv));
491           if (hasSelection()) {
492               selection_start = selection_end = actpos;
493               UpdateLocal(bv, false);
494           } else {
495               selection_start = selection_end = actpos;
496           }
497           break;
498       case LFUN_DOWNSEL:
499           moveDown(bv, false);
500           selection_end = actpos;
501           UpdateLocal(bv, false);
502           break;
503       case LFUN_DOWN:
504           result= DISPATCH_RESULT(moveDown(bv));
505           if (hasSelection()) {
506               selection_start = selection_end = actpos;
507               UpdateLocal(bv, false);
508           } else {
509               selection_start = selection_end = actpos;
510           }
511           break;
512       case LFUN_UPSEL:
513           moveUp(bv, false);
514           selection_end = actpos;
515           UpdateLocal(bv, false);
516           break;
517       case LFUN_UP:
518           result= DISPATCH_RESULT(moveUp(bv));
519           if (hasSelection()) {
520               selection_start = selection_end = actpos;
521               UpdateLocal(bv, false);
522           } else {
523               selection_start = selection_end = actpos;
524           }
525           break;
526       case LFUN_BACKSPACE:
527           if (!actpos || par->IsNewline(actpos-1)) {
528               if (hasSelection()) {
529                   selection_start = selection_end = actpos;
530                   UpdateLocal(bv, false);
531               }
532               break;
533           }
534           moveLeft(bv);
535       case LFUN_DELETE:
536           if (Delete()) { // we need update
537               selection_start = selection_end = actpos;
538               UpdateLocal(bv, true);
539           } else if (hasSelection()) {
540               selection_start = selection_end = actpos;
541               UpdateLocal(bv, false);
542           }
543           break;
544       case LFUN_HOME:
545           for(; actpos > rows[actrow].pos; --actpos)
546               cx -= SingleWidth(bv->getPainter(), par, actpos);
547           cx -= SingleWidth(bv->getPainter(), par, actpos);
548           if (hasSelection()) {
549               selection_start = selection_end = actpos;
550               UpdateLocal(bv, false);
551           } else {
552               selection_start = selection_end = actpos;
553           }
554           break;
555       case LFUN_END:
556           for(; actpos < rows[actrow + 1].pos; ++actpos)
557               cx += SingleWidth(bv->getPainter(), par, actpos);
558           if (hasSelection()) {
559               selection_start = selection_end = actpos;
560               UpdateLocal(bv, false);
561           } else {
562               selection_start = selection_end = actpos;
563           }
564           break;
565       case LFUN_MATH_MODE:   // Open or create a math inset
566           InsertInset(bv, new InsetFormula);
567           if (hasSelection()) {
568               selection_start = selection_end = actpos;
569               UpdateLocal(bv, false);
570           } else {
571               selection_start = selection_end = actpos;
572           }
573           return DISPATCHED;
574       default:
575           result = UNDISPATCHED;
576           break;
577     }
578     if (result != FINISHED) {
579         if (!the_locking_inset)
580             ShowInsetCursor(bv);
581     } else
582         bv->unlockInset(this);
583     return result;
584 }
585
586
587 int InsetText::Latex(ostream & os, signed char /*fragile*/) const
588 {
589 #ifdef USE_OSTREAM_ONLY
590         TexRow texrow;
591         int ret = par->SimpleTeXOnePar(os, texrow);
592         return ret;
593 #else
594     string fstr;
595
596     int i = Latex(fstr, fragile);
597     os << fstr;
598     return i;
599 #endif
600 }
601
602
603 #ifndef USE_OSTREAM_ONLY
604 int InsetText::Latex(string & file, signed char /* fragile */) const
605 {
606     TexRow texrow;
607
608     return par->SimpleTeXOnePar(file, texrow);
609 }
610 #endif
611
612
613 void InsetText::Validate(LaTeXFeatures & features) const
614 {
615     par->validate(features);
616 }
617
618
619 // Returns the width of a character at a certain spot
620 int InsetText::SingleWidth(Painter & pain, LyXParagraph * par, int pos) const
621 {
622     LyXFont font = GetFont(par, pos);
623     char c = par->GetChar(pos);
624
625     if (IsPrintable(c)) {
626         return font.width(c);
627     } else if (c == LyXParagraph::META_INSET) {
628         Inset const * tmpinset = par->GetInset(pos);
629         if (tmpinset)
630             return tmpinset->width(pain, font);
631         else
632             return 0;
633     } else if (IsSeparatorChar(c))
634         c = ' ';
635     else if (IsNewlineChar(c))
636         c = 'n';
637     return font.width(c);
638 }
639
640
641 // Returns the width of a character at a certain spot
642 void InsetText::SingleHeight(Painter & pain, LyXParagraph * par,int pos,
643                              int & asc, int & desc) const
644 {
645     LyXFont font = GetFont(par, pos);
646     char c = par->GetChar(pos);
647
648     asc = desc = 0;
649     if (c == LyXParagraph::META_INSET) {
650         Inset const * tmpinset=par->GetInset(pos);
651         if (tmpinset) {
652             asc = tmpinset->ascent(pain, font);
653             desc = tmpinset->descent(pain, font);
654         }
655     } else {
656         asc = font.maxAscent();
657         desc = font.maxDescent();
658     }
659     return;
660 }
661
662
663 // Gets the fully instantiated font at a given position in a paragraph
664 // Basically the same routine as LyXParagraph::getFont() in paragraph.C.
665 // The difference is that this one is used for displaying, and thus we
666 // are allowed to make cosmetic improvements. For instance make footnotes
667 // smaller. (Asger)
668 // If position is -1, we get the layout font of the paragraph.
669 // If position is -2, we get the font of the manual label of the paragraph.
670 LyXFont InsetText::GetFont(LyXParagraph * par, int pos) const
671 {
672     char par_depth = par->GetDepth();
673
674     LyXLayout const & layout =
675             textclasslist.Style(buffer->params.textclass, par->GetLayout());
676
677     // We specialize the 95% common case:
678     if (par->footnoteflag == LyXParagraph::NO_FOOTNOTE && !par_depth) {
679         if (pos >= 0) {
680             // 95% goes here
681             if (layout.labeltype == LABEL_MANUAL
682                 && pos < BeginningOfMainBody(par)) {
683                 // 1% goes here
684                 return par->GetFontSettings(pos).realize(layout.reslabelfont);
685             } else
686                 return par->GetFontSettings(pos).realize(layout.resfont);
687         } else {
688             // 5% goes here.
689             // process layoutfont for pos == -1 and labelfont for pos < -1
690             if (pos == -1)
691                 return layout.resfont;
692             else
693                 return layout.reslabelfont;
694         }
695     }
696     // The uncommon case need not be optimized as much
697
698     LyXFont layoutfont, tmpfont;
699
700     if (pos >= 0){
701         // 95% goes here
702         if (pos < BeginningOfMainBody(par)) {
703             // 1% goes here
704             layoutfont = layout.labelfont;
705         } else {
706             // 99% goes here
707             layoutfont = layout.font;
708         }
709         tmpfont = par->GetFontSettings(pos);
710         tmpfont.realize(layoutfont);
711     } else{
712         // 5% goes here.
713         // process layoutfont for pos == -1 and labelfont for pos < -1
714         if (pos == -1)
715             tmpfont = layout.font;
716         else
717             tmpfont = layout.labelfont;
718     }
719     
720     // Resolve against environment font information
721     //if (par->GetDepth()){ // already in while condition
722     while (par && par_depth && !tmpfont.resolved()) {
723         par = par->DepthHook(par_depth - 1);
724         if (par) {
725             tmpfont.realize(textclasslist.Style(buffer->params.textclass,
726                                                 par->GetLayout()).font);
727             par_depth = par->GetDepth();
728         }
729     }
730     tmpfont.realize((textclasslist.TextClass(buffer->params.textclass).
731                     defaultfont()));
732     return tmpfont;
733 }
734
735
736 int InsetText::BeginningOfMainBody(LyXParagraph * par) const
737 {
738     if (textclasslist.Style(buffer->params.textclass,
739                        par->GetLayout()).labeltype != LABEL_MANUAL)
740         return 0;
741     else
742         return par->BeginningOfMainBody();
743 }
744
745
746 void InsetText::GetCursorPos(int & x, int & y) const
747 {
748     x = cx;
749     y = cy;
750 }
751
752
753 int InsetText::InsetInInsetY()
754 {
755     if (!the_locking_inset)
756         return 0;
757
758     int y = inset_y;
759     return (y + the_locking_inset->InsetInInsetY());
760 }
761
762
763 void InsetText::ToggleInsetCursor(BufferView * bv)
764 {
765     if (the_locking_inset) {
766         the_locking_inset->ToggleInsetCursor(bv);
767         return;
768     }
769
770     LyXFont font = GetFont(par, actpos);
771
772     int asc = font.maxAscent();
773     int desc = font.maxDescent();
774   
775     if (cursor_visible)
776         bv->hideLockedInsetCursor();
777     else
778         bv->showLockedInsetCursor(cx, cy, asc, desc);
779     cursor_visible = !cursor_visible;
780 }
781
782
783 void InsetText::ShowInsetCursor(BufferView * bv)
784 {
785     if (!cursor_visible) {
786         LyXFont font = GetFont(par, actpos);
787         
788         int asc = font.maxAscent();
789         int desc = font.maxDescent();
790         bv->fitLockedInsetCursor(cx, cy, asc, desc);
791         bv->showLockedInsetCursor(cx, cy, asc, desc);
792         cursor_visible = true;
793     }
794 }
795
796
797 void InsetText::HideInsetCursor(BufferView * bv)
798 {
799     if (cursor_visible)
800         ToggleInsetCursor(bv);
801 }
802
803
804 void InsetText::setPos(BufferView * bv, int x, int y, bool activate_inset)
805 {
806         int ox = x;
807         int oy = y;
808         
809     // search right X-pos x==0 -> top_x
810     actpos = actrow = 0;
811     cy = top_baseline;
812     y += cy;
813     for(unsigned int i = 1;
814         ((cy + rows[i - 1].desc) < y) && (i < rows.size() - 1); ++i) {
815         cy = rows[i].baseline;
816         actpos = rows[i].pos;
817         actrow = i;
818     }
819     cy -= top_baseline;
820     cx = top_x;
821     x += top_x;
822
823     int swh;
824     int sw = swh = SingleWidth(bv->getPainter(), par,actpos);
825     if (par->GetChar(actpos)!=LyXParagraph::META_INSET)
826         swh /= 2;
827     while ((actpos < (rows[actrow + 1].pos - 1)) && ((cx + swh) < x)) {
828         cx += sw;
829         ++actpos;
830         sw = swh = SingleWidth(bv->getPainter(), par,actpos);
831         if (par->GetChar(actpos)!=LyXParagraph::META_INSET)
832             swh /= 2;
833     }
834     if (activate_inset && par->GetChar(actpos)==LyXParagraph::META_INSET) {
835         the_locking_inset =
836                 static_cast<UpdatableInset*>(par->GetInset(actpos));
837         inset_x = cx - top_x;
838         inset_y = cy;
839         inset_pos = actpos;
840         the_locking_inset->Edit(bv, ox - inset_x, oy - inset_y, 0);
841     }
842 }
843
844
845 bool InsetText::moveRight(BufferView * bv, bool activate_inset)
846 {
847     if (actpos >= par->Last())
848         return false;
849     if (activate_inset && par->GetChar(actpos)==LyXParagraph::META_INSET) {
850         the_locking_inset =
851                 static_cast<UpdatableInset*>(par->GetInset(actpos));
852         inset_x = cx - top_x;
853         inset_y = cy;
854         inset_pos = actpos;
855         the_locking_inset->Edit(bv, 0, 0, 0);
856     } else {
857         ++actpos;
858         resetPos(bv);
859     }
860     return true;
861 }
862
863
864 bool InsetText::moveLeft(BufferView * bv, bool activate_inset)
865 {
866     if (actpos <= 0)
867         return false;
868     --actpos;
869     if (activate_inset && par->GetChar(actpos)==LyXParagraph::META_INSET) {
870         the_locking_inset =
871                 static_cast<UpdatableInset*>(par->GetInset(actpos));
872         resetPos(bv);
873         inset_x = cx - top_x;
874         inset_y = cy;
875         inset_pos = actpos;
876         the_locking_inset->Edit(bv, the_locking_inset->
877                                 width(bv->getPainter(), GetFont(par,actpos)),
878                                 0, 0);
879     } else {
880         resetPos(bv);
881     }
882     return true;
883 }
884
885
886 bool InsetText::moveUp(BufferView * bv, bool activate_inset)
887 {
888     if (!actrow)
889         return false;
890     cy = rows[actrow - 1].baseline - top_baseline;
891     setPos(bv, cx - top_x, cy, activate_inset);
892     return true;
893 }
894
895
896 bool InsetText::moveDown(BufferView * bv, bool activate_inset)
897 {
898     if (actrow >= int(rows.size() - 2))
899         return false;
900     cy = rows[actrow + 1].baseline - top_baseline;
901     setPos(bv, cx - top_x, cy, activate_inset);
902     return true;
903 }
904
905
906 void InsetText::resetPos(BufferView * bv)
907 {
908     int old_pos = actpos;
909
910     cy = top_baseline;
911     actrow = 0;
912     for(int i = 0; rows[i].pos <= actpos; ++i) {
913         cy = rows[i].baseline;
914         actrow = i;
915     }
916     cy -= top_baseline;
917     setPos(bv, 0, cy, false);
918     cx = top_x;
919     while(actpos < old_pos) {
920         cx += SingleWidth(bv->getPainter(), par,actpos);
921         ++actpos;
922     }
923 }
924
925
926 bool InsetText::Delete()
927 {
928     /* some insets are undeletable here */
929     if (par->GetChar(actpos)==LyXParagraph::META_INSET) {
930         /* force complete redo when erasing display insets */ 
931         /* this is a cruel mathod but save..... Matthias */ 
932         if (par->GetInset(actpos)->Deletable() &&
933             par->GetInset(actpos)->display()) {
934             par->Erase(actpos);
935             return true;
936         }
937         return false;
938     }
939     par->Erase(actpos);
940     return true;
941 }
942
943
944 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
945 {
946     if (inset->Editable() == Inset::IS_EDITABLE) {
947         UpdatableInset *i = (UpdatableInset *)inset;
948         i->setOwner((UpdatableInset *)this);
949     }
950     par->InsertChar(actpos, LyXParagraph::META_INSET);
951     par->InsertInset(actpos, inset);
952     UpdateLocal(bv, true);
953     the_locking_inset = static_cast<UpdatableInset*>(inset);
954     inset_x = cx - top_x;
955     inset_y = cy;
956     inset_pos = actpos;
957     inset->Edit(bv, 0, 0, 0);
958     return true;
959 }
960
961
962 UpdatableInset * InsetText::GetLockingInset()
963 {
964     return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
965 }
966
967
968 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
969 {
970     // if there is no selection just set the current_font
971     if (!hasSelection()) {
972         // Determine basis font
973         LyXFont layoutfont;
974         if (actpos < BeginningOfMainBody(par))
975             layoutfont = GetFont(par, -2);
976         else
977             layoutfont = GetFont(par, -1);
978         
979         // Update current font
980         real_current_font.update(font, toggleall);
981         
982         // Reduce to implicit settings
983         current_font = real_current_font;
984         current_font.reduce(layoutfont);
985         // And resolve it completely
986         real_current_font.realize(layoutfont);
987         return;
988     }
989     
990     int s_start, s_end;
991     if (selection_start > selection_end) {
992         s_start = selection_end;
993         s_end = selection_start;
994     } else {
995         s_start = selection_start;
996         s_end = selection_end;
997     }
998     LyXFont newfont;
999     while(s_start < s_end) {
1000         newfont = GetFont(par,s_start);
1001         newfont.update(font, toggleall);
1002         SetCharFont(s_start, newfont);
1003         ++s_start;
1004     }
1005     UpdateLocal(bv, true);
1006 }
1007
1008
1009 void InsetText::SetCharFont(int pos, LyXFont const & f)
1010 {
1011     /* let the insets convert their font */
1012         LyXFont font(f);
1013         
1014     if (par->GetChar(pos) == LyXParagraph::META_INSET) {
1015         if (par->GetInset(pos))
1016             font = par->GetInset(pos)->ConvertFont(font);
1017     }
1018     LyXLayout const & layout =
1019             textclasslist.Style(buffer->params.textclass,par->GetLayout());
1020
1021     // Get concrete layout font to reduce against
1022     LyXFont layoutfont;
1023
1024     if (pos < BeginningOfMainBody(par))
1025         layoutfont = layout.labelfont;
1026     else
1027         layoutfont = layout.font;
1028
1029
1030     layoutfont.realize((textclasslist.TextClass(buffer->params.textclass).
1031                        defaultfont()));
1032
1033     // Now, reduce font against full layout font
1034     font.reduce(layoutfont);
1035
1036     par->SetFont(pos, font);
1037 }
1038
1039
1040 void InsetText::computeTextRows(Painter & pain, float x) const
1041 {
1042     int p,
1043         nwp = 0,
1044         asc = 0,
1045         desc = 0,
1046         oasc = 0,
1047         odesc = 0,
1048         owidth = 0,
1049         wordAscent,
1050         wordDescent;
1051     row_struct row;
1052
1053     if (rows.size())
1054             rows.clear();
1055     int width = wordAscent = wordDescent = 0;
1056     insetWidth = maxAscent = maxDescent = 0;
1057     row.asc      = 0;
1058     row.desc     = 0;
1059     row.pos      = 0;
1060     row.baseline = 0;
1061     rows.push_back(row);
1062     if (!autoBreakRows) {
1063         for(p = 0; p < par->Last(); ++p) {
1064             insetWidth += SingleWidth(pain, par, p);
1065             SingleHeight(pain, par, p, asc, desc);
1066             maxAscent = max(maxAscent, asc);
1067             maxDescent = max(maxDescent, desc);
1068         }
1069         rows[0].asc = maxAscent;
1070         rows[0].desc = maxDescent;
1071         // alocate a dummy row for the endpos
1072         row.pos = par->Last();
1073         rows.push_back(row);
1074         return;
1075     }
1076
1077     bool is_first_word_in_row = true;
1078
1079     int cw, lastWordWidth = 0;
1080
1081     maxWidth = UpdatableInset::getMaxWidth(pain) - widthOffset;
1082     for(p = 0; p < par->Last(); ++p) {
1083         cw = SingleWidth(pain, par, p);
1084         width += cw;
1085         lastWordWidth += cw;
1086         SingleHeight(pain, par, p, asc, desc);
1087         wordAscent = max(wordAscent, asc);
1088         wordDescent = max(wordDescent, desc);
1089         Inset const * inset = 0;
1090         if (((p + 1) < par->Last()) &&
1091             (par->GetChar(p + 1)==LyXParagraph::META_INSET))
1092             inset = par->GetInset(p + 1);
1093         if (inset && inset->display()) {
1094             if (!is_first_word_in_row && (width >= (maxWidth - x))) {
1095                 // we have to split also the row above
1096                 rows.back().asc = oasc;
1097                 rows.back().desc = odesc;
1098                 row.pos = nwp;
1099                 rows.push_back(row);
1100                 oasc = wordAscent;
1101                 odesc = wordDescent;
1102                 insetWidth = max(insetWidth, owidth);
1103                 width = lastWordWidth;
1104                 lastWordWidth = 0;
1105             } else {
1106                     oasc = max(oasc, wordAscent);
1107                     odesc = max(odesc, wordDescent);
1108             }
1109             rows.back().asc = oasc;
1110             rows.back().desc = odesc;
1111             row.pos = ++p;
1112             rows.push_back(row);
1113             SingleHeight(pain, par, p, asc, desc);
1114             rows.back().asc = asc;
1115             rows.back().desc = desc;
1116             row.pos = nwp = p + 1;
1117             rows.push_back(row);
1118             oasc = odesc = width = lastWordWidth = 0;
1119             is_first_word_in_row = true;
1120             wordAscent = wordDescent = 0;
1121 //          x = 0.0;
1122             continue;
1123         } else if (par->IsSeparator(p)) {
1124             if (width >= maxWidth - x) {
1125                 if (is_first_word_in_row) {
1126                     rows.back().asc = wordAscent;
1127                     rows.back().desc = wordDescent;
1128                     row.pos = p + 1;
1129                     rows.push_back(row);
1130                     oasc = odesc = width = 0;
1131                 } else {
1132                     rows.back().asc = oasc;
1133                     rows.back().desc = odesc;
1134                     row.pos = nwp;
1135                     rows.push_back(row);
1136                     oasc = wordAscent;
1137                     odesc = wordDescent;
1138                     insetWidth = max(insetWidth, owidth);
1139                     width = lastWordWidth;
1140                 }
1141                 wordAscent = wordDescent = lastWordWidth = 0;
1142                 nwp = p + 1;
1143 //              x = 0.0;
1144                 continue;
1145             }
1146             owidth = width;
1147             oasc = max(oasc, wordAscent);
1148             odesc = max(odesc, wordDescent);
1149             wordAscent = wordDescent = lastWordWidth = 0;
1150             nwp = p + 1;
1151             is_first_word_in_row = false;
1152         }
1153     }
1154     // if we have some data in the paragraph we have ascent/descent
1155     if (p) {
1156         if (width >= (maxWidth - x)) {
1157             // assign upper row
1158             rows.back().asc = oasc;
1159             rows.back().desc = odesc;
1160             // assign and allocate lower row
1161             row.pos = nwp;
1162             rows.push_back(row);
1163             rows.back().asc = wordAscent;
1164             rows.back().desc = wordDescent;
1165             width -= lastWordWidth;
1166         } else {
1167             // assign last row data
1168 //          width = lastWordWidth;
1169 //          lastWordWidth = 0;
1170             rows.back().asc = max(oasc, wordAscent);
1171             rows.back().desc = max(odesc, wordDescent);
1172         }
1173     }
1174     insetWidth = max(insetWidth, width);
1175     // alocate a dummy row for the endpos
1176     row.pos = par->Last();
1177     rows.push_back(row);
1178     // calculate maxAscent/Descent
1179     maxAscent = rows[0].asc;
1180     maxDescent = rows[0].desc;
1181     for (RowList::size_type i = 1; i < rows.size() - 1; ++i) {
1182         maxDescent += rows[i].asc + rows[i].desc + interline_space;
1183     }
1184 #if 0
1185     if (the_locking_inset) {
1186         computeBaselines(top_baseline);
1187         actpos = inset_pos;
1188         resetPos(bv);
1189         inset_x = cx - top_x;
1190         inset_y = cy;
1191     }
1192 #endif
1193 }
1194
1195
1196 void InsetText::computeBaselines(int baseline) const
1197 {
1198     rows[0].baseline = baseline;
1199     for (unsigned int i = 1; i < rows.size() - 1; i++) {
1200         rows[i].baseline = rows[i - 1].baseline + rows[i - 1].desc + 
1201             rows[i].asc + interline_space;
1202     }
1203 }
1204
1205 void InsetText::UpdateLocal(BufferView *bv, bool flag)
1206 {
1207     init_inset = flag;
1208     bv->updateInset(this, flag);
1209 }