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