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