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