]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
e4e6d3ad49bb0a054c815913e6b984f64cd5c2dd
[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-2000 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 "lyxparagraph.h"
25 #include "lyxlex.h"
26 #include "debug.h"
27 #include "lyxfont.h"
28 #include "commandtags.h"
29 #include "buffer.h"
30 #include "LyXView.h"
31 #include "BufferView.h"
32 #include "layout.h"
33 #include "LaTeXFeatures.h"
34 #include "Painter.h"
35 #include "lyx_gui_misc.h"
36 #include "lyxtext.h"
37 #include "lyxcursor.h"
38 #include "CutAndPaste.h"
39 #include "font.h"
40 #include "minibuffer.h"
41 #include "LColor.h"
42 #include "support/textutils.h"
43 #include "support/LAssert.h"
44 #include "lyxrow.h"
45 #include "lyxrc.h"
46 #include "intl.h"
47 #include "trans_mgr.h"
48 #include "lyxscreen.h"
49 #include "WorkArea.h"
50
51 using std::ostream;
52 using std::ifstream;
53 using std::endl;
54 using std::min;
55 using std::max;
56
57 extern unsigned char getCurrentTextClass(Buffer *);
58
59 InsetText::InsetText()
60 {
61     par = new LyXParagraph();
62     init();
63 }
64
65
66 InsetText::InsetText(InsetText const & ins)
67         : UpdatableInset()
68 {
69     par = 0;
70     init(&ins);
71     autoBreakRows = ins.autoBreakRows;
72 }
73
74
75 InsetText & InsetText::operator=(InsetText const & it)
76 {
77     init(&it);
78     autoBreakRows = it.autoBreakRows;
79     return * this;
80 }
81
82
83 void InsetText::init(InsetText const * ins)
84 {
85     top_y = 0;
86     last_width = 0;
87     last_height = 0;
88     insetAscent = 0;
89     insetDescent = 0;
90     insetWidth = 0;
91     the_locking_inset = 0;
92     cursor_visible = false;
93     interline_space = 1;
94     no_selection = false;
95     need_update = INIT;
96     drawTextXOffset = 0;
97     drawTextYOffset = 0;
98     autoBreakRows = false;
99     drawFrame = NEVER;
100     xpos = 0.0;
101     if (ins) {
102         SetParagraphData(ins->par);
103         autoBreakRows = ins->autoBreakRows;
104         drawFrame = ins->drawFrame;
105     }
106     par->SetInsetOwner(this);
107     frame_color = LColor::insetframe;
108     locked = false;
109     old_par = 0;
110 }
111
112
113 InsetText::~InsetText()
114 {
115     for(Cache::const_iterator cit=cache.begin(); cit != cache.end(); ++cit)
116         delete (*cit).second;
117 //      deleteLyXText((*cit).first);
118     LyXParagraph * p = par->next;
119     delete par;
120     while(p) {
121         par = p;
122         p = p->next;
123         delete par;
124     }
125 }
126
127
128 void InsetText::clear()
129 {
130     LyXParagraph * p = par->next;
131     delete par;
132     while(p) {
133         par = p;
134         p = p->next;
135         delete par;
136     }
137     par = new LyXParagraph();
138 }
139
140
141 Inset * InsetText::Clone() const
142 {
143     InsetText * t = new InsetText(*this);
144     return t;
145 }
146
147
148 void InsetText::Write(Buffer const * buf, ostream & os) const
149 {
150     os << "Text\n";
151     WriteParagraphData(buf, os);
152 }
153
154
155 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
156 {
157     par->writeFile(buf, os, buf->params, 0, 0);
158 }
159
160
161 void InsetText::Read(Buffer const * buf, LyXLex & lex)
162 {
163     string token;
164     int pos = 0;
165     LyXParagraph * return_par = 0;
166     char depth = 0; // signed or unsigned?
167 #ifndef NEW_INSETS
168     LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
169     LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
170 #endif
171     LyXFont font(LyXFont::ALL_INHERIT);
172
173     LyXParagraph * p = par->next;
174     delete par;
175     while(p) {
176         par = p;
177         p = p->next;
178         delete par;
179     }
180     par = new LyXParagraph;
181     while (lex.IsOK()) {
182         lex.nextToken();
183         token = lex.GetString();
184         if (token.empty())
185             continue;
186         if (token == "\\end_inset")
187             break;
188         if (const_cast<Buffer*>(buf)->
189             parseSingleLyXformat2Token(lex, par, return_par,token, pos, depth,
190                                        font
191 #ifndef NEW_INSETS
192                                        , footnoteflag, footnotekind
193 #endif
194                                        ))
195         {
196             // the_end read this should NEVER happen
197             lex.printError("\\the_end read in inset! Error in document!");
198             return;
199         }
200     }
201     if (!return_par)
202             return_par = par;
203     par = return_par;
204     while(return_par) {
205         return_par->SetInsetOwner(this);
206         return_par = return_par->next;
207     }
208     
209     if (token != "\\end_inset") {
210         lex.printError("Missing \\end_inset at this point. "
211                        "Read: `$$Token'");
212     }
213     need_update = INIT;
214 }
215
216
217 int InsetText::ascent(BufferView * bv, LyXFont const &) const
218 {
219     int y_temp = 0;
220     Row * row = TEXT(bv)->GetRowNearY(y_temp);
221     insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
222     return insetAscent;
223 }
224
225
226 int InsetText::descent(BufferView * bv, LyXFont const &) const
227 {
228     int y_temp = 0;
229     Row * row = TEXT(bv)->GetRowNearY(y_temp);
230     insetDescent = TEXT(bv)->height - row->ascent_of_text() +
231         TEXT_TO_INSET_OFFSET;
232     return insetDescent;
233 }
234
235
236 int InsetText::width(BufferView * bv, LyXFont const &) const
237 {
238     insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
239     return insetWidth;
240 }
241
242
243 int InsetText::textWidth(Painter & pain) const
244 {
245     int w = getMaxWidth(pain, this);
246     return w;
247 }
248
249
250 void InsetText::draw(BufferView * bv, LyXFont const & f,
251                      int baseline, float & x, bool cleared) const
252 {
253     Painter & pain = bv->painter();
254
255     // no draw is necessary !!!
256     if ((drawFrame == LOCKED) && !locked && !par->size()) {
257         if (!cleared && (need_update == CLEAR_FRAME)) {
258             pain.rectangle(top_x + 1, baseline - insetAscent + 1,
259                            width(bv, f) - 1,
260                            insetAscent + insetDescent - 1,
261                            LColor::background);
262         }
263         top_x = int(x);
264         top_baseline = baseline;
265         x += width(bv, f);
266         need_update = NONE;
267         return;
268     }
269
270     xpos = x;
271     UpdatableInset::draw(bv, f, baseline, x, cleared);
272
273     if (!cleared && ((need_update==FULL) || (top_x!=int(x)) ||
274                      (top_baseline!=baseline))) {
275         int w =  insetWidth;
276         int h = insetAscent + insetDescent;
277         int ty = baseline - insetAscent;
278         
279         if (ty < 0) {
280             h += ty;
281             ty = 0;
282         }
283         if ((ty + h) > pain.paperHeight())
284             h = pain.paperHeight();
285         if ((top_x + drawTextXOffset + w) > pain.paperWidth())
286             w = pain.paperWidth();
287         pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
288         cleared = true;
289         need_update = FULL;
290     }
291     if (!cleared && (need_update == NONE))
292         return;
293
294     if (top_x != int(x)) {
295         need_update = INIT;
296         top_x = int(x);
297         bv->text->status = LyXText::CHANGED_IN_DRAW;
298         return;
299     }
300
301     top_baseline = baseline;
302     top_y = baseline - ascent(bv, f);
303     last_width = width(bv, f);
304     last_height = ascent(bv, f) + descent(bv, f);
305
306     if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
307         inset_x = cx(bv) - top_x + drawTextXOffset;
308         inset_y = cy(bv) + drawTextYOffset;
309     }
310     if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
311         x += width(bv, f);
312         need_update = NONE;
313         return;
314     }
315     x += TEXT_TO_INSET_OFFSET;
316     int y = 0;
317     Row * row = TEXT(bv)->GetRowNearY(y);
318     y += baseline - row->ascent_of_text();
319     if (cleared || !locked || (need_update == FULL)) {
320         while (row != 0) {
321             TEXT(bv)->GetVisibleRow(bv, int(y), int(x), row, y, cleared);
322             y += row->height();
323             row = row->next();
324         }
325     } else if (need_update == SELECTION) {
326         bv->screen()->ToggleToggle(TEXT(bv), int(y), int(x));
327     } else {
328         locked = false;
329         if (need_update == CURSOR) {
330             bv->screen()->ToggleSelection(TEXT(bv), true, int(y), int(x));
331             TEXT(bv)->ClearSelection();
332             TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
333         }
334         bv->screen()->Update(TEXT(bv), int(y), int(x));
335         locked = true;
336     }
337     TEXT(bv)->refresh_y = 0;
338     TEXT(bv)->status = LyXText::UNCHANGED;
339     if ((need_update != CURSOR_PAR) &&
340         ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)))
341     {
342             pain.rectangle(top_x + 1, baseline - insetAscent + 1,
343                            width(bv, f) - 1, insetAscent + insetDescent - 1,
344                            frame_color);
345     } else if (need_update == CLEAR_FRAME) {
346             pain.rectangle(top_x + 1, baseline - insetAscent + 1,
347                            width(bv, f) - 1, insetAscent + insetDescent - 1,
348                            LColor::background);
349     }
350     x += width(bv, f) - TEXT_TO_INSET_OFFSET;
351     if (bv->text->status==LyXText::CHANGED_IN_DRAW)
352         need_update = INIT;
353     else if (need_update != INIT)
354         need_update = NONE;
355 }
356
357
358 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
359 {
360     if (reinit) {  // && (need_update != CURSOR)) {
361         need_update = INIT;
362         resizeLyXText(bv);
363         if (owner())
364             owner()->update(bv, font, true);
365         return;
366     }
367     if (the_locking_inset) {
368         inset_x = cx(bv) - top_x + drawTextXOffset;
369         inset_y = cy(bv) + drawTextYOffset;
370         the_locking_inset->update(bv, font, reinit);
371     }
372     if (need_update == INIT) {
373         resizeLyXText(bv);
374         need_update = FULL;
375 //      if (!owner() && bv->text)
376 //          bv->text->UpdateInset(bv, this);
377     }
378     int oldw = insetWidth;
379 #if 1
380     insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
381     // max(textWidth(bv->painter()),
382     // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
383     // (2 * TEXT_TO_INSET_OFFSET);
384 #else
385     insetWidth = textWidth(bv->painter());
386     if (insetWidth < 0)
387             insetWidth = static_cast<int>(TEXT(bv)->width);
388 #endif
389     if (oldw != insetWidth) {
390 //          printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
391 //                 textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
392         resizeLyXText(bv);
393         need_update = FULL;
394 #if 0
395         if (owner()) {
396             owner()->update(bv, font, reinit);
397             return;
398         } else {
399             update(bv, font, reinit);
400         }
401 #else
402 #if 1
403         update(bv, font, reinit);
404 #else
405         UpdateLocal(bv, INIT, false);
406 #endif
407 #endif
408         return;
409     }
410     if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
411         the_locking_inset)
412     {
413         TEXT(bv)->UpdateInset(bv, the_locking_inset);
414     }
415
416     if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
417         need_update = FULL;
418
419     int y_temp = 0;
420     Row * row = TEXT(bv)->GetRowNearY(y_temp);
421     insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
422     insetDescent = TEXT(bv)->height - row->ascent_of_text() +
423         TEXT_TO_INSET_OFFSET;
424 }
425
426
427 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
428 {
429     TEXT(bv)->FullRebreak(bv);
430     if (need_update != INIT) {
431         if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
432             need_update = FULL;
433         else if (!the_locking_inset || (what != CURSOR))
434             need_update = what;
435     }
436     if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
437         TEXT(bv)->selection)
438             bv->updateInset(this, mark_dirty);
439     bv->owner()->showState();
440     if (old_par != cpar(bv)) {
441             bv->owner()->setLayout(cpar(bv)->GetLayout());
442             old_par = cpar(bv);
443     }
444 }
445
446
447 string const InsetText::EditMessage() const
448 {
449     return _("Opened Text Inset");
450 }
451
452
453 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
454 {
455 //    par->SetInsetOwner(this);
456     UpdatableInset::Edit(bv, x, y, button);
457
458     if (!bv->lockInset(this)) {
459         lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
460         return;
461     }
462     locked = true;
463     the_locking_inset = 0;
464     inset_pos = inset_x = inset_y = 0;
465     inset_par = 0;
466     old_par = 0;
467     if (!checkAndActivateInset(bv, x, y, button))
468         TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
469                                            y+TEXT(bv)->first+insetAscent);
470     TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
471     bv->text->FinishUndo();
472     ShowInsetCursor(bv);
473     UpdateLocal(bv, FULL, false);
474 }
475
476
477 void InsetText::InsetUnlock(BufferView * bv)
478 {
479     if (the_locking_inset) {
480         the_locking_inset->InsetUnlock(bv);
481         the_locking_inset = 0;
482     }
483     HideInsetCursor(bv);
484     no_selection = false;
485     locked = false;
486     TEXT(bv)->selection = 0;
487     UpdateLocal(bv, CLEAR_FRAME, false);
488     if (owner())
489             bv->owner()->setLayout(owner()->getLyXText(bv)
490                                     ->cursor.par()->GetLayout());
491     else
492             bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
493 }
494
495
496 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
497 {
498     lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
499     if (!inset)
500         return false;
501     if (inset == cpar(bv)->GetInset(cpos(bv))) {
502         lyxerr[Debug::INSETS] << "OK" << endl;
503         the_locking_inset = inset;
504         inset_x = cx(bv) - top_x + drawTextXOffset;
505         inset_y = cy(bv) + drawTextYOffset;
506         inset_pos = cpos(bv);
507         inset_par = cpar(bv);
508         TEXT(bv)->UpdateInset(bv, the_locking_inset);
509         return true;
510     } else if (the_locking_inset && (the_locking_inset == inset)) {
511         if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
512             lyxerr[Debug::INSETS] << "OK" << endl;
513             inset_x = cx(bv) - top_x + drawTextXOffset;
514             inset_y = cy(bv) + drawTextYOffset;
515         } else {
516             lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
517         }
518     } else if (the_locking_inset) {
519         lyxerr[Debug::INSETS] << "MAYBE" << endl;
520         return the_locking_inset->LockInsetInInset(bv, inset);
521     }
522     lyxerr[Debug::INSETS] << "NOT OK" << endl;
523     return false;
524 }
525
526
527 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
528                                    bool lr)
529 {
530     if (!the_locking_inset)
531         return false;
532     if (the_locking_inset == inset) {
533         the_locking_inset->InsetUnlock(bv);
534         TEXT(bv)->UpdateInset(bv, inset);
535         the_locking_inset = 0;
536         if (lr)
537             moveRight(bv, false);
538         old_par = 0; // force layout setting
539         UpdateLocal(bv, CURSOR_PAR, false);
540         return true;
541     }
542     return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
543 }
544
545
546 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
547 {
548     if (!the_locking_inset)
549         return false;
550     if (the_locking_inset != inset) {
551         TEXT(bv)->UpdateInset(bv, the_locking_inset);
552         need_update = CURSOR_PAR;
553         return the_locking_inset->UpdateInsetInInset(bv, inset);
554     }
555 //    UpdateLocal(bv, FULL, false);
556     if (TEXT(bv)->UpdateInset(bv, inset))
557         UpdateLocal(bv, CURSOR_PAR, false);
558     if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
559         inset_x = cx(bv) - top_x + drawTextXOffset;
560         inset_y = cy(bv) + drawTextYOffset;
561     }
562     return true;
563 }
564
565
566 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
567 {
568     no_selection = false;
569
570     int tmp_x = x - drawTextXOffset;
571     int tmp_y = y + insetAscent;
572     Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
573
574     HideInsetCursor(bv);
575     if (the_locking_inset) {
576         if (the_locking_inset == inset) {
577             the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
578             return;
579         } else if (inset) {
580             // otherwise unlock the_locking_inset and lock the new inset
581             the_locking_inset->InsetUnlock(bv);
582             inset_x = cx(bv) - top_x + drawTextXOffset;
583             inset_y = cy(bv) + drawTextYOffset;
584             inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
585             inset->Edit(bv, x - inset_x, y - inset_y, button);
586             if (the_locking_inset) {
587                 UpdateLocal(bv, CURSOR_PAR, false);
588             }
589             return;
590         }
591         // otherwise only unlock the_locking_inset
592         the_locking_inset->InsetUnlock(bv);
593         the_locking_inset = 0;
594     }
595     if (bv->theLockingInset()) {
596         if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
597             UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
598             inset_x = cx(bv) - top_x + drawTextXOffset;
599             inset_y = cy(bv) + drawTextYOffset;
600             inset_pos = cpos(bv);
601             inset_par = cpar(bv);
602             uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
603             uinset->Edit(bv, x - inset_x, y - inset_y, 0);
604             if (the_locking_inset) {
605                 UpdateLocal(bv, CURSOR_PAR, false);
606             }
607             return;
608         }
609     }
610     if (!inset) {
611         bool paste_internally = false;
612         if ((button == 2) && TEXT(bv)->selection) {
613             LocalDispatch(bv, LFUN_COPY, "");
614             paste_internally = true;
615         }
616         TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
617                                            y+TEXT(bv)->first+insetAscent);
618         TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
619         UpdateLocal(bv, CURSOR, false);
620         bv->owner()->setLayout(cpar(bv)->GetLayout());
621         old_par = cpar(bv);
622         // Insert primary selection with middle mouse
623         // if there is a local selection in the current buffer,
624         // insert this
625         if (button == 2) {
626             if (paste_internally)
627                 LocalDispatch(bv, LFUN_PASTE, "");
628             else
629                 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
630         }
631     }
632     ShowInsetCursor(bv);
633 }
634
635
636 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
637 {
638     UpdatableInset * inset = 0;
639
640     if (the_locking_inset) {
641             the_locking_inset->InsetButtonRelease(bv,
642                                                   x - inset_x, y - inset_y,
643                                                   button);
644     } else {
645         if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
646             inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
647             if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
648                 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
649             } else {
650                 inset_x = cx(bv) - top_x + drawTextXOffset;
651                 inset_y = cy(bv) + drawTextYOffset;
652                 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
653                 inset->Edit(bv, x - inset_x, y - inset_y, button);
654             }
655             UpdateLocal(bv, CURSOR_PAR, false);
656         }
657     }
658     no_selection = false;
659 }
660
661
662 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
663 {
664     if (the_locking_inset) {
665         the_locking_inset->InsetMotionNotify(bv, x - inset_x,
666                                              y - inset_y,state);
667         return;
668     }
669     if (!no_selection) {
670         HideInsetCursor(bv);
671         TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
672                                            y+TEXT(bv)->first+insetAscent);
673         TEXT(bv)->SetSelection();
674         if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
675             TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
676             UpdateLocal(bv, SELECTION, false);
677         ShowInsetCursor(bv);
678     }
679     no_selection = false;
680 }
681
682
683 void InsetText::InsetKeyPress(XKeyEvent * xke)
684 {
685     if (the_locking_inset) {
686         the_locking_inset->InsetKeyPress(xke);
687         return;
688     }
689 }
690
691
692 UpdatableInset::RESULT
693 InsetText::LocalDispatch(BufferView * bv,
694                          int action, string const & arg)
695 {
696     no_selection = false;
697     UpdatableInset::RESULT
698         result= UpdatableInset::LocalDispatch(bv, action, arg);
699     if (result != UNDISPATCHED) {
700         return DISPATCHED;
701     }
702
703     result=DISPATCHED;
704     if ((action < 0) && arg.empty())
705         return FINISHED;
706
707     if (the_locking_inset) {
708         result = the_locking_inset->LocalDispatch(bv, action, arg);
709         if (result == DISPATCHED_NOUPDATE)
710             return result;
711         else if (result == DISPATCHED) {
712             UpdateLocal(bv, CURSOR_PAR, false);
713             return result;
714         } else if (result == FINISHED) {
715             switch(action) {
716             case -1:
717             case LFUN_RIGHT:
718                 moveRight(bv, false);
719                 break;
720             case LFUN_DOWN:
721                 moveDown(bv);
722                 break;
723             }
724             the_locking_inset = 0;
725             return DISPATCHED;
726         }
727     }
728     HideInsetCursor(bv);
729     switch (action) {
730         // Normal chars
731     case -1:
732         if (bv->buffer()->isReadonly()) {
733             LyXBell();
734 //          setErrorMessage(N_("Document is read only"));
735             break;
736         }
737         if (!arg.empty()) {
738             /* Automatically delete the currently selected
739              * text and replace it with what is being
740              * typed in now. Depends on lyxrc settings
741              * "auto_region_delete", which defaults to
742              * true (on). */
743
744             bv->text->SetUndo(bv->buffer(), Undo::INSERT,
745 #ifndef NEW_INSETS
746                               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
747                               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
748 #else
749                               bv->text->cursor.par()->previous,
750                               bv->text->cursor.par()->next
751 #endif
752                     );
753             // if an empty paragraph set the language to the surronding
754             // paragraph language on insertion of the first character!
755             if (!par->Last() && !par->next) {
756                 LyXText * text = 0;
757                 if (owner()) {
758                     Inset * inset = owner();
759                     while(inset && inset->getLyXText(bv) == TEXT(bv))
760                         inset = inset->owner();
761                     if (inset)
762                         text = inset->getLyXText(bv);
763                 }
764                 if (!text)
765                     text = bv->text;
766                 LyXFont font(LyXFont::ALL_IGNORE);
767                 font.setLanguage(text->cursor.par()->getParLanguage(bv->buffer()->params));
768                 SetFont(bv, font, false);
769             }
770             if (lyxrc.auto_region_delete) {
771                 if (TEXT(bv)->selection){
772                     TEXT(bv)->CutSelection(bv, false);
773                 }
774             }
775             TEXT(bv)->ClearSelection();
776             for (string::size_type i = 0; i < arg.length(); ++i) {
777                 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
778             }
779         }
780         UpdateLocal(bv, CURSOR_PAR, true);
781         break;
782         // --- Cursor Movements ---------------------------------------------
783     case LFUN_RIGHTSEL:
784         bv->text->FinishUndo();
785         moveRight(bv, false, true);
786         TEXT(bv)->SetSelection();
787         UpdateLocal(bv, SELECTION, false);
788         break;
789     case LFUN_RIGHT:
790         result = moveRight(bv);
791         bv->text->FinishUndo();
792         TEXT(bv)->ClearSelection();
793         UpdateLocal(bv, CURSOR, false);
794         break;
795     case LFUN_LEFTSEL:
796         bv->text->FinishUndo();
797         moveLeft(bv, false, true);
798         TEXT(bv)->SetSelection();
799         UpdateLocal(bv, SELECTION, false);
800         break;
801     case LFUN_LEFT:
802         bv->text->FinishUndo();
803         result= moveLeft(bv);
804         TEXT(bv)->ClearSelection();
805         UpdateLocal(bv, CURSOR, false);
806         break;
807     case LFUN_DOWNSEL:
808         bv->text->FinishUndo();
809         moveDown(bv);
810         TEXT(bv)->SetSelection();
811         UpdateLocal(bv, SELECTION, false);
812         break;
813     case LFUN_DOWN:
814         bv->text->FinishUndo();
815         result = moveDown(bv);
816         TEXT(bv)->ClearSelection();
817         UpdateLocal(bv, CURSOR, false);
818         break;
819     case LFUN_UPSEL:
820         bv->text->FinishUndo();
821         moveUp(bv);
822         TEXT(bv)->SetSelection();
823         UpdateLocal(bv, SELECTION, false);
824         break;
825     case LFUN_UP:
826         bv->text->FinishUndo();
827         result = moveUp(bv);
828         TEXT(bv)->ClearSelection();
829         UpdateLocal(bv, CURSOR, false);
830         break;
831     case LFUN_HOME:
832         bv->text->FinishUndo();
833         TEXT(bv)->CursorHome(bv);
834         UpdateLocal(bv, CURSOR, false);
835         break;
836     case LFUN_END:
837         TEXT(bv)->CursorEnd(bv);
838         UpdateLocal(bv, CURSOR, false);
839         break;
840     case LFUN_BACKSPACE:
841         bv->text->SetUndo(bv->buffer(), Undo::DELETE,
842 #ifndef NEW_INSETS
843           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
844           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
845 #else
846           bv->text->cursor.par()->previous,
847           bv->text->cursor.par()->next
848 #endif
849                 );
850         if (TEXT(bv)->selection)
851             TEXT(bv)->CutSelection(bv);
852         else
853             TEXT(bv)->Backspace(bv);
854         UpdateLocal(bv, CURSOR_PAR, true);
855         break;
856     case LFUN_DELETE:
857         bv->text->SetUndo(bv->buffer(), Undo::DELETE,
858 #ifndef NEW_INSETS
859           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
860           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
861 #else
862           bv->text->cursor.par()->previous,
863           bv->text->cursor.par()->next
864 #endif
865                 );
866         if (TEXT(bv)->selection)
867             TEXT(bv)->CutSelection(bv);
868         else
869             TEXT(bv)->Delete(bv);
870         UpdateLocal(bv, CURSOR_PAR, true);
871         break;
872     case LFUN_CUT:
873         bv->text->SetUndo(bv->buffer(), Undo::DELETE,
874 #ifndef NEW_INSETS
875           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
876           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
877 #else
878           bv->text->cursor.par()->previous,
879           bv->text->cursor.par()->next
880 #endif
881                 );
882         TEXT(bv)->CutSelection(bv);
883         UpdateLocal(bv, CURSOR_PAR, true);
884         break;
885     case LFUN_COPY:
886         bv->text->FinishUndo();
887         TEXT(bv)->CopySelection(bv);
888         UpdateLocal(bv, CURSOR_PAR, false);
889         break;
890     case LFUN_PASTESELECTION:
891     {
892         string clip(bv->workarea()->getClipboard());
893         
894         if (clip.empty())
895             break;
896         if (arg == "paragraph") {
897                 TEXT(bv)->InsertStringB(bv, clip);
898         } else {
899                 TEXT(bv)->InsertStringA(bv, clip);
900         }
901         UpdateLocal(bv, CURSOR_PAR, true);
902         break;
903     }
904     case LFUN_PASTE:
905         if (!autoBreakRows) {
906             CutAndPaste cap;
907
908             if (cap.nrOfParagraphs() > 1) {
909                 WriteAlert(_("Impossible operation"),
910                            _("Cannot include more than one paragraph!"),
911                            _("Sorry."));
912                 break;
913             }
914         }
915         bv->text->SetUndo(bv->buffer(), Undo::INSERT,
916 #ifndef NEW_INSETS
917           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
918           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
919 #else
920           bv->text->cursor.par()->previous,
921           bv->text->cursor.par()->next
922 #endif
923                 );
924         TEXT(bv)->PasteSelection(bv);
925         UpdateLocal(bv, CURSOR_PAR, true);
926         break;
927     case LFUN_BREAKPARAGRAPH:
928         if (!autoBreakRows)
929             return DISPATCHED;
930         TEXT(bv)->BreakParagraph(bv, 0);
931         UpdateLocal(bv, FULL, true);
932         break;
933     case LFUN_BREAKLINE:
934         if (!autoBreakRows)
935             return DISPATCHED;
936         bv->text->SetUndo(bv->buffer(), Undo::INSERT,
937 #ifndef NEW_INSETS
938             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
939             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
940 #else
941             bv->text->cursor.par()->previous,
942             bv->text->cursor.par()->next
943 #endif
944                 );
945         TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
946         UpdateLocal(bv, CURSOR_PAR, true);
947         break;
948     case LFUN_LAYOUT:
949         // do not set layouts on non breakable textinsets
950         if (autoBreakRows) {
951             LyXTextClass::size_type cur_layout = cpar(bv)->layout;
952       
953             // Derive layout number from given argument (string)
954             // and current buffer's textclass (number). */    
955             LyXTextClassList::ClassList::size_type tclass =
956                 bv->buffer()->params.textclass;
957             std::pair <bool, LyXTextClass::size_type> layout = 
958                 textclasslist.NumberOfLayout(tclass, arg);
959
960             // If the entry is obsolete, use the new one instead.
961             if (layout.first) {
962                 string obs = textclasslist.Style(tclass,layout.second).
963                     obsoleted_by();
964                 if (!obs.empty()) 
965                     layout = textclasslist.NumberOfLayout(tclass, obs);
966             }
967
968             // see if we found the layout number:
969             if (!layout.first) {
970                 string msg = string(N_("Layout ")) + arg + N_(" not known");
971
972                 bv->owner()->getMiniBuffer()->Set(msg);
973                 break;
974             }
975
976             if (cur_layout != layout.second) {
977                 cur_layout = layout.second;
978                 TEXT(bv)->SetLayout(bv, layout.second);
979                 bv->owner()->setLayout(cpar(bv)->GetLayout());
980                 UpdateLocal(bv, CURSOR_PAR, true);
981             }
982         } else {
983             // reset the layout box
984             bv->owner()->setLayout(cpar(bv)->GetLayout());
985         }
986         break;
987     case LFUN_PARAGRAPH_SPACING:
988             // This one is absolutely not working. When fiddling with this
989             // it also seems to me that the paragraphs inside the insettext
990             // inherit bufferparams/paragraphparams in a strange way. (Lgb)
991     {
992             LyXParagraph * par = TEXT(bv)->cursor.par();
993             Spacing::Space cur_spacing = par->spacing.getSpace();
994             float cur_value = 1.0;
995             if (cur_spacing == Spacing::Other) {
996                     cur_value = par->spacing.getValue();
997             }
998                         
999             std::istringstream istr(arg.c_str());
1000             string tmp;
1001             istr >> tmp;
1002             Spacing::Space new_spacing = cur_spacing;
1003             float new_value = cur_value;
1004             if (tmp.empty()) {
1005                     lyxerr << "Missing argument to `paragraph-spacing'"
1006                            << endl;
1007             } else if (tmp == "single") {
1008                     new_spacing = Spacing::Single;
1009             } else if (tmp == "onehalf") {
1010                     new_spacing = Spacing::Onehalf;
1011             } else if (tmp == "double") {
1012                     new_spacing = Spacing::Double;
1013             } else if (tmp == "other") {
1014                     new_spacing = Spacing::Other;
1015                     float tmpval = 0.0;
1016                     istr >> tmpval;
1017                     lyxerr << "new_value = " << tmpval << endl;
1018                     if (tmpval != 0.0)
1019                             new_value = tmpval;
1020             } else if (tmp == "default") {
1021                     new_spacing = Spacing::Default;
1022             } else {
1023                     lyxerr << _("Unknown spacing argument: ")
1024                            << arg << endl;
1025             }
1026             if (cur_spacing != new_spacing || cur_value != new_value) {
1027                     par->spacing.set(new_spacing, new_value);
1028                     //TEXT(bv)->RedoParagraph(owner->view());
1029                     UpdateLocal(bv, CURSOR_PAR, true);
1030                     //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1031             }
1032     }
1033     break;
1034         
1035     default:
1036         result = UNDISPATCHED;
1037         break;
1038     }
1039     if (result != FINISHED) {
1040         ShowInsetCursor(bv);
1041     } else
1042         bv->unlockInset(this);
1043     return result;
1044 }
1045
1046
1047 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1048 {
1049     TexRow texrow;
1050     buf->latexParagraphs(os, par, 0, texrow);
1051     return texrow.rows();
1052 }
1053
1054
1055 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1056 {
1057     LyXParagraph * p = par;
1058     unsigned int lines = 0;
1059     
1060     while (p) {
1061         string const tmp = buf->asciiParagraph(p, linelen);
1062         lines += countChar(tmp, '\n');
1063         os << tmp;
1064         p = p->next;
1065     }
1066     os << "\n";
1067     ++lines;
1068     return lines;
1069 }
1070
1071
1072 void InsetText::Validate(LaTeXFeatures & features) const
1073 {
1074     LyXParagraph * p = par;
1075     while(p) {
1076         p->validate(features);
1077         p = p->next;
1078     }
1079 }
1080
1081
1082 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1083 {
1084     if (textclasslist.Style(buf->params.textclass,
1085                             p->GetLayout()).labeltype != LABEL_MANUAL)
1086         return 0;
1087     else
1088         return p->BeginningOfMainBody();
1089 }
1090
1091
1092 void InsetText::GetCursorPos(BufferView * bv,
1093                              int & x, int & y) const
1094 {
1095     x = cx(bv);
1096     y = cy(bv);
1097 }
1098
1099
1100 unsigned int InsetText::InsetInInsetY()
1101 {
1102     if (!the_locking_inset)
1103         return 0;
1104
1105     return (inset_y + the_locking_inset->InsetInInsetY());
1106 }
1107
1108
1109 void InsetText::ToggleInsetCursor(BufferView * bv)
1110 {
1111     if (the_locking_inset) {
1112         the_locking_inset->ToggleInsetCursor(bv);
1113         return;
1114     }
1115
1116     LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1117
1118     int asc = lyxfont::maxAscent(font);
1119     int desc = lyxfont::maxDescent(font);
1120   
1121     if (cursor_visible)
1122         bv->hideLockedInsetCursor();
1123     else
1124         bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1125     cursor_visible = !cursor_visible;
1126 }
1127
1128
1129 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1130 {
1131     if (the_locking_inset) {
1132         the_locking_inset->ShowInsetCursor(bv);
1133         return;
1134     }
1135     if (!cursor_visible) {
1136         LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1137         
1138         int asc = lyxfont::maxAscent(font);
1139         int desc = lyxfont::maxDescent(font);
1140
1141         bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1142         if (show)
1143             bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1144         cursor_visible = true;
1145     }
1146 }
1147
1148
1149 void InsetText::HideInsetCursor(BufferView * bv)
1150 {
1151     if (cursor_visible) {
1152         bv->hideLockedInsetCursor();
1153         cursor_visible = false;
1154     }
1155     if (the_locking_inset)
1156         the_locking_inset->HideInsetCursor(bv);
1157 }
1158
1159
1160 UpdatableInset::RESULT
1161 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1162 {
1163     if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1164         return FINISHED;
1165     if (activate_inset && checkAndActivateInset(bv, false))
1166         return DISPATCHED;
1167     TEXT(bv)->CursorRight(bv, selecting);
1168     return DISPATCHED_NOUPDATE;
1169 }
1170
1171
1172 UpdatableInset::RESULT
1173 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1174 {
1175     if (!cpar(bv)->previous && (cpos(bv) <= 0))
1176         return FINISHED;
1177     TEXT(bv)->CursorLeft(bv, selecting);
1178     if (activate_inset && checkAndActivateInset(bv, true))
1179         return DISPATCHED;
1180     return DISPATCHED_NOUPDATE;
1181 }
1182
1183
1184 UpdatableInset::RESULT
1185 InsetText::moveUp(BufferView * bv)
1186 {
1187     if (!crow(bv)->previous())
1188         return FINISHED;
1189     TEXT(bv)->CursorUp(bv);
1190     return DISPATCHED_NOUPDATE;
1191 }
1192
1193
1194 UpdatableInset::RESULT
1195 InsetText::moveDown(BufferView * bv)
1196 {
1197     if (!crow(bv)->next())
1198         return FINISHED;
1199     TEXT(bv)->CursorDown(bv);
1200     return DISPATCHED_NOUPDATE;
1201 }
1202
1203
1204 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1205 {
1206     if (the_locking_inset) {
1207         if (the_locking_inset->InsertInsetAllowed(inset))
1208             return the_locking_inset->InsertInset(bv, inset);
1209         return false;
1210     }
1211     bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1212 #ifndef NEW_INSETS
1213               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1214               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1215 #else
1216               bv->text->cursor.par()->previous,
1217               bv->text->cursor.par()->next
1218 #endif
1219             );
1220     if (inset->Editable() == Inset::IS_EDITABLE) {
1221         UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1222         i->setOwner(static_cast<UpdatableInset *>(this));
1223     }
1224     HideInsetCursor(bv);
1225     TEXT(bv)->InsertInset(bv, inset);
1226     TEXT(bv)->selection = 0;
1227     bv->fitCursor(TEXT(bv));
1228     UpdateLocal(bv, CURSOR_PAR, true);
1229     static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1230     ShowInsetCursor(bv);
1231     return true;
1232 }
1233
1234
1235 UpdatableInset * InsetText::GetLockingInset()
1236 {
1237     return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1238 }
1239
1240
1241 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1242 {
1243     if (c == LyxCode())
1244         return this;
1245     if (the_locking_inset)
1246         return the_locking_inset->GetFirstLockingInsetOfType(c);
1247     return 0;
1248 }
1249
1250
1251 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1252 {
1253     bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1254 #ifndef NEW_INSETS
1255               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1256               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1257 #else
1258               bv->text->cursor.par()->previous,
1259               bv->text->cursor.par()->next
1260 #endif
1261             );
1262     TEXT(bv)->SetFont(bv, font, toggleall);
1263     bv->fitCursor(TEXT(bv));
1264     UpdateLocal(bv, CURSOR_PAR, true);
1265 }
1266
1267
1268 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1269 {
1270     if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1271         unsigned int x;
1272         unsigned int y;
1273         Inset * inset =
1274             static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1275         if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1276             return false;
1277         LyXFont const font =
1278                 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1279         if (behind) {
1280             x = inset->width(bv, font);
1281             y = inset->descent(bv, font);
1282         } else {
1283             x = y = 0;
1284         }
1285         inset_x = cx(bv) - top_x + drawTextXOffset;
1286         inset_y = cy(bv) + drawTextYOffset;
1287         inset->Edit(bv, x - inset_x, y - inset_y, 0);
1288         if (!the_locking_inset)
1289             return false;
1290         UpdateLocal(bv, CURSOR_PAR, false);
1291         return true;
1292     }
1293     return false;
1294 }
1295
1296
1297 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1298                                       int button)
1299 {
1300     x = x - drawTextXOffset;
1301     y = y + insetAscent;
1302     Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1303
1304     if (inset) {
1305         if (x < 0)
1306             x = insetWidth;
1307         if (y < 0)
1308             y = insetDescent;
1309         inset_x = cx(bv) - top_x + drawTextXOffset;
1310         inset_y = cy(bv) + drawTextYOffset;
1311         inset->Edit(bv, x - inset_x, y - inset_y, button);
1312         if (!the_locking_inset)
1313             return false;
1314         UpdateLocal(bv, CURSOR_PAR, false);
1315         return true;
1316     }
1317     return false;
1318 }
1319
1320
1321 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1322 {
1323     int w = UpdatableInset::getMaxWidth(pain, inset);
1324     if (w < 0) {
1325         return w;
1326     }
1327     if (owner()) {
1328         w = w - top_x + owner()->x();
1329         return w;
1330     }
1331     w -= (2 * TEXT_TO_INSET_OFFSET);
1332     return w - top_x;
1333 //    return  w - (2*TEXT_TO_INSET_OFFSET);
1334 }
1335
1336
1337 void InsetText::SetParagraphData(LyXParagraph *p)
1338 {
1339     LyXParagraph * np;
1340
1341     if (par) {
1342         np = par->next;
1343         delete par;
1344         while(np) {
1345             par = np;
1346             np = np->next;
1347             delete par;
1348         }
1349     }
1350     par = p->Clone();
1351     par->SetInsetOwner(this);
1352     np = par;
1353     while(p->next) {
1354         p = p->next;
1355         np->next = p->Clone();
1356         np->next->previous = np;
1357         np = np->next;
1358         np->SetInsetOwner(this);
1359     }
1360     need_update = INIT;
1361 }
1362
1363
1364 void InsetText::SetAutoBreakRows(bool flag)
1365 {
1366     if (flag != autoBreakRows) {
1367         autoBreakRows = flag;
1368         need_update = FULL;
1369         if (!flag)
1370             removeNewlines();
1371     }
1372 }
1373
1374
1375 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1376 {
1377     if (how != drawFrame) {
1378         drawFrame = how;
1379         if (bv)
1380             UpdateLocal(bv, DRAW_FRAME, false);
1381     }
1382 }
1383
1384
1385 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1386 {
1387     if (frame_color != col) {
1388         frame_color = col;
1389         if (bv)
1390             UpdateLocal(bv, DRAW_FRAME, false);
1391     }
1392 }
1393
1394 #if 0
1395 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1396 {
1397     return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1398 }
1399 #endif
1400
1401
1402 int InsetText::cx(BufferView * bv) const
1403 {
1404     return TEXT(bv)->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1405 }
1406
1407
1408 int InsetText::cy(BufferView * bv) const
1409 {
1410     LyXFont font;
1411     return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1412 }
1413
1414
1415 int InsetText::cpos(BufferView * bv) const
1416 {
1417     return TEXT(bv)->cursor.pos();
1418 }
1419
1420
1421 LyXParagraph * InsetText::cpar(BufferView * bv) const
1422 {
1423     return TEXT(bv)->cursor.par();
1424 }
1425
1426
1427 Row * InsetText::crow(BufferView * bv) const
1428 {
1429     return TEXT(bv)->cursor.row();
1430 }
1431
1432
1433 LyXText * InsetText::getLyXText(BufferView * bv) const
1434 {
1435     if (cache.find(bv) != cache.end())
1436         return cache[bv];
1437     LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1438     lt->init(bv);
1439     cache[bv] = lt;
1440     if (the_locking_inset) {
1441         lt->SetCursor(bv, inset_par, inset_pos);
1442     }
1443     return lt;
1444 }
1445
1446
1447 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1448 {
1449     if (cache.find(bv) == cache.end())
1450         return;
1451     delete cache[bv];
1452     cache.erase(bv);
1453     if (recursive) {
1454         /// then remove all LyXText in text-insets
1455         LyXParagraph * p = par;
1456         for(;p;p = p->next) {
1457             p->deleteInsetsLyXText(bv);
1458         }
1459     }
1460 }
1461
1462
1463 void InsetText::resizeLyXText(BufferView * bv) const
1464 {
1465     if (!par->next && !par->size()) // resize not neccessary!
1466         return;
1467     if (cache.find(bv) == cache.end())
1468         return;
1469
1470     LyXParagraph * lpar = 0;
1471     LyXParagraph * selstartpar = 0;
1472     LyXParagraph * selendpar = 0;
1473     int pos = 0;
1474     int selstartpos = 0;
1475     int selendpos = 0;
1476     int selection = 0;
1477     int mark_set = 0;
1478
1479 //    ProhibitInput(bv);
1480
1481     if (locked) {
1482         lpar = TEXT(bv)->cursor.par();
1483         pos = TEXT(bv)->cursor.pos();
1484         selstartpar = TEXT(bv)->sel_start_cursor.par();
1485         selstartpos = TEXT(bv)->sel_start_cursor.pos();
1486         selendpar = TEXT(bv)->sel_end_cursor.par();
1487         selendpos = TEXT(bv)->sel_end_cursor.pos();
1488         selection = TEXT(bv)->selection;
1489         mark_set = TEXT(bv)->mark_set;
1490     }
1491     deleteLyXText(bv, (the_locking_inset == 0));
1492
1493     if (lpar) {
1494         TEXT(bv)->selection = true;
1495         /* at this point just to avoid the Delete-Empty-Paragraph
1496          * Mechanism when setting the cursor */
1497         TEXT(bv)->mark_set = mark_set;
1498         if (selection) {
1499             TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1500             TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1501             TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1502             TEXT(bv)->SetSelection();
1503             TEXT(bv)->SetCursor(bv, lpar, pos);
1504         } else {
1505             TEXT(bv)->SetCursor(bv, lpar, pos);
1506             TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1507             TEXT(bv)->selection = false;
1508         }
1509     }
1510     if (bv->screen())
1511             TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1512     // this will scroll the screen such that the cursor becomes visible 
1513     bv->updateScrollbar();
1514 //    AllowInput(bv);
1515     if (the_locking_inset) {
1516         /// then resize all LyXText in text-insets
1517         inset_x = cx(bv) - top_x + drawTextXOffset;
1518         inset_y = cy(bv) + drawTextYOffset;
1519         for(LyXParagraph * p = par; p; p = p->next) {
1520             p->resizeInsetsLyXText(bv);
1521         }
1522     }
1523     need_update = FULL;
1524 }
1525
1526
1527 void InsetText::removeNewlines()
1528 {
1529     for(LyXParagraph * p = par; p; p = p->next) {
1530         for(int i = 0; i < p->Last(); ++i) {
1531             if (p->GetChar(i) == LyXParagraph::META_NEWLINE)
1532                 p->Erase(i);
1533         }
1534     }
1535 }