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