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