]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
Angus mathed (cleanup) and inset (bib) patch.
[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->the_locking_inset) {
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 (lyxrc.auto_region_delete) {
754                 if (TEXT(bv)->selection){
755                     TEXT(bv)->CutSelection(bv, false);
756                 }
757             }
758             TEXT(bv)->ClearSelection();
759             for (string::size_type i = 0; i < arg.length(); ++i) {
760                 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
761             }
762         }
763         UpdateLocal(bv, CURSOR_PAR, true);
764         break;
765         // --- Cursor Movements ---------------------------------------------
766     case LFUN_RIGHTSEL:
767         bv->text->FinishUndo();
768         moveRight(bv, false, true);
769         TEXT(bv)->SetSelection();
770         UpdateLocal(bv, SELECTION, false);
771         break;
772     case LFUN_RIGHT:
773         result = moveRight(bv);
774         bv->text->FinishUndo();
775         TEXT(bv)->ClearSelection();
776         UpdateLocal(bv, CURSOR, false);
777         break;
778     case LFUN_LEFTSEL:
779         bv->text->FinishUndo();
780         moveLeft(bv, false, true);
781         TEXT(bv)->SetSelection();
782         UpdateLocal(bv, SELECTION, false);
783         break;
784     case LFUN_LEFT:
785         bv->text->FinishUndo();
786         result= moveLeft(bv);
787         TEXT(bv)->ClearSelection();
788         UpdateLocal(bv, CURSOR, false);
789         break;
790     case LFUN_DOWNSEL:
791         bv->text->FinishUndo();
792         moveDown(bv);
793         TEXT(bv)->SetSelection();
794         UpdateLocal(bv, SELECTION, false);
795         break;
796     case LFUN_DOWN:
797         bv->text->FinishUndo();
798         result = moveDown(bv);
799         TEXT(bv)->ClearSelection();
800         UpdateLocal(bv, CURSOR, false);
801         break;
802     case LFUN_UPSEL:
803         bv->text->FinishUndo();
804         moveUp(bv);
805         TEXT(bv)->SetSelection();
806         UpdateLocal(bv, SELECTION, false);
807         break;
808     case LFUN_UP:
809         bv->text->FinishUndo();
810         result = moveUp(bv);
811         TEXT(bv)->ClearSelection();
812         UpdateLocal(bv, CURSOR, false);
813         break;
814     case LFUN_HOME:
815         bv->text->FinishUndo();
816         TEXT(bv)->CursorHome(bv);
817         UpdateLocal(bv, CURSOR, false);
818         break;
819     case LFUN_END:
820         TEXT(bv)->CursorEnd(bv);
821         UpdateLocal(bv, CURSOR, false);
822         break;
823     case LFUN_BACKSPACE:
824         bv->text->SetUndo(bv->buffer(), Undo::DELETE,
825 #ifndef NEW_INSETS
826           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
827           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
828 #else
829           bv->text->cursor.par()->previous,
830           bv->text->cursor.par()->next
831 #endif
832                 );
833         if (TEXT(bv)->selection)
834             TEXT(bv)->CutSelection(bv);
835         else
836             TEXT(bv)->Backspace(bv);
837         UpdateLocal(bv, CURSOR_PAR, true);
838         break;
839     case LFUN_DELETE:
840         bv->text->SetUndo(bv->buffer(), Undo::DELETE,
841 #ifndef NEW_INSETS
842           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
843           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
844 #else
845           bv->text->cursor.par()->previous,
846           bv->text->cursor.par()->next
847 #endif
848                 );
849         if (TEXT(bv)->selection)
850             TEXT(bv)->CutSelection(bv);
851         else
852             TEXT(bv)->Delete(bv);
853         UpdateLocal(bv, CURSOR_PAR, true);
854         break;
855     case LFUN_CUT:
856         bv->text->SetUndo(bv->buffer(), Undo::DELETE,
857 #ifndef NEW_INSETS
858           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
859           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
860 #else
861           bv->text->cursor.par()->previous,
862           bv->text->cursor.par()->next
863 #endif
864                 );
865         TEXT(bv)->CutSelection(bv);
866         UpdateLocal(bv, CURSOR_PAR, true);
867         break;
868     case LFUN_COPY:
869         bv->text->FinishUndo();
870         TEXT(bv)->CopySelection(bv);
871         UpdateLocal(bv, CURSOR_PAR, false);
872         break;
873     case LFUN_PASTESELECTION:
874     {
875         string clip(bv->workarea()->getClipboard());
876         
877         if (clip.empty())
878             break;
879         if (arg == "paragraph") {
880                 TEXT(bv)->InsertStringB(bv, clip);
881         } else {
882                 TEXT(bv)->InsertStringA(bv, clip);
883         }
884         UpdateLocal(bv, CURSOR_PAR, true);
885         break;
886     }
887     case LFUN_PASTE:
888         if (!autoBreakRows) {
889             CutAndPaste cap;
890
891             if (cap.nrOfParagraphs() > 1) {
892                 WriteAlert(_("Impossible operation"),
893                            _("Cannot include more than one paragraph!"),
894                            _("Sorry."));
895                 break;
896             }
897         }
898         bv->text->SetUndo(bv->buffer(), Undo::INSERT,
899 #ifndef NEW_INSETS
900           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
901           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
902 #else
903           bv->text->cursor.par()->previous,
904           bv->text->cursor.par()->next
905 #endif
906                 );
907         TEXT(bv)->PasteSelection(bv);
908         UpdateLocal(bv, CURSOR_PAR, true);
909         break;
910     case LFUN_BREAKPARAGRAPH:
911         if (!autoBreakRows)
912             return DISPATCHED;
913         TEXT(bv)->BreakParagraph(bv, 0);
914         UpdateLocal(bv, FULL, true);
915         break;
916     case LFUN_BREAKLINE:
917         if (!autoBreakRows)
918             return DISPATCHED;
919         bv->text->SetUndo(bv->buffer(), Undo::INSERT,
920 #ifndef NEW_INSETS
921             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
922             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
923 #else
924             bv->text->cursor.par()->previous,
925             bv->text->cursor.par()->next
926 #endif
927                 );
928         TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
929         UpdateLocal(bv, CURSOR_PAR, true);
930         break;
931     case LFUN_LAYOUT:
932         // do not set layouts on non breakable textinsets
933         if (autoBreakRows) {
934             LyXTextClass::size_type cur_layout = cpar(bv)->layout;
935       
936             // Derive layout number from given argument (string)
937             // and current buffer's textclass (number). */    
938             LyXTextClassList::ClassList::size_type tclass =
939                 bv->buffer()->params.textclass;
940             std::pair <bool, LyXTextClass::size_type> layout = 
941                 textclasslist.NumberOfLayout(tclass, arg);
942
943             // If the entry is obsolete, use the new one instead.
944             if (layout.first) {
945                 string obs = textclasslist.Style(tclass,layout.second).
946                     obsoleted_by();
947                 if (!obs.empty()) 
948                     layout = textclasslist.NumberOfLayout(tclass, obs);
949             }
950
951             // see if we found the layout number:
952             if (!layout.first) {
953                 string msg = string(N_("Layout ")) + arg + N_(" not known");
954
955                 bv->owner()->getMiniBuffer()->Set(msg);
956                 break;
957             }
958
959             if (cur_layout != layout.second) {
960                 cur_layout = layout.second;
961                 TEXT(bv)->SetLayout(bv, layout.second);
962                 bv->owner()->setLayout(cpar(bv)->GetLayout());
963                 UpdateLocal(bv, CURSOR_PAR, true);
964             }
965         } else {
966             // reset the layout box
967             bv->owner()->setLayout(cpar(bv)->GetLayout());
968         }
969         break;
970     case LFUN_PARAGRAPH_SPACING:
971             // This one is absolutely not working. When fiddling with this
972             // it also seems to me that the paragraphs inside the insettext
973             // inherit bufferparams/paragraphparams in a strange way. (Lgb)
974     {
975             LyXParagraph * par = TEXT(bv)->cursor.par();
976             Spacing::Space cur_spacing = par->spacing.getSpace();
977             float cur_value = 1.0;
978             if (cur_spacing == Spacing::Other) {
979                     cur_value = par->spacing.getValue();
980             }
981                         
982             std::istringstream istr(arg.c_str());
983             string tmp;
984             istr >> tmp;
985             Spacing::Space new_spacing = cur_spacing;
986             float new_value = cur_value;
987             if (tmp.empty()) {
988                     lyxerr << "Missing argument to `paragraph-spacing'"
989                            << endl;
990             } else if (tmp == "single") {
991                     new_spacing = Spacing::Single;
992             } else if (tmp == "onehalf") {
993                     new_spacing = Spacing::Onehalf;
994             } else if (tmp == "double") {
995                     new_spacing = Spacing::Double;
996             } else if (tmp == "other") {
997                     new_spacing = Spacing::Other;
998                     float tmpval = 0.0;
999                     istr >> tmpval;
1000                     lyxerr << "new_value = " << tmpval << endl;
1001                     if (tmpval != 0.0)
1002                             new_value = tmpval;
1003             } else if (tmp == "default") {
1004                     new_spacing = Spacing::Default;
1005             } else {
1006                     lyxerr << _("Unknown spacing argument: ")
1007                            << arg << endl;
1008             }
1009             if (cur_spacing != new_spacing || cur_value != new_value) {
1010                     par->spacing.set(new_spacing, new_value);
1011                     //TEXT(bv)->RedoParagraph(owner->view());
1012                     UpdateLocal(bv, CURSOR_PAR, true);
1013                     //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1014             }
1015     }
1016     break;
1017         
1018     default:
1019         result = UNDISPATCHED;
1020         break;
1021     }
1022     if (result != FINISHED) {
1023         ShowInsetCursor(bv);
1024     } else
1025         bv->unlockInset(this);
1026     return result;
1027 }
1028
1029
1030 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1031 {
1032     TexRow texrow;
1033     buf->latexParagraphs(os, par, 0, texrow);
1034     return texrow.rows();
1035 }
1036
1037
1038 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1039 {
1040     LyXParagraph * p = par;
1041     unsigned int lines = 0;
1042     
1043     while (p) {
1044         string const tmp = buf->asciiParagraph(p, linelen);
1045         lines += countChar(tmp, '\n');
1046         os << tmp;
1047         p = p->next;
1048     }
1049     os << "\n";
1050     ++lines;
1051     return lines;
1052 }
1053
1054
1055 void InsetText::Validate(LaTeXFeatures & features) const
1056 {
1057     LyXParagraph * p = par;
1058     while(p) {
1059         p->validate(features);
1060         p = p->next;
1061     }
1062 }
1063
1064
1065 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1066 {
1067     if (textclasslist.Style(buf->params.textclass,
1068                             p->GetLayout()).labeltype != LABEL_MANUAL)
1069         return 0;
1070     else
1071         return p->BeginningOfMainBody();
1072 }
1073
1074
1075 void InsetText::GetCursorPos(BufferView * bv,
1076                              int & x, int & y) const
1077 {
1078     x = cx(bv);
1079     y = cy(bv);
1080 }
1081
1082
1083 unsigned int InsetText::InsetInInsetY()
1084 {
1085     if (!the_locking_inset)
1086         return 0;
1087
1088     return (inset_y + the_locking_inset->InsetInInsetY());
1089 }
1090
1091
1092 void InsetText::ToggleInsetCursor(BufferView * bv)
1093 {
1094     if (the_locking_inset) {
1095         the_locking_inset->ToggleInsetCursor(bv);
1096         return;
1097     }
1098
1099     LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1100
1101     int asc = lyxfont::maxAscent(font);
1102     int desc = lyxfont::maxDescent(font);
1103   
1104     if (cursor_visible)
1105         bv->hideLockedInsetCursor();
1106     else
1107         bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1108     cursor_visible = !cursor_visible;
1109 }
1110
1111
1112 void InsetText::ShowInsetCursor(BufferView * bv)
1113 {
1114     if (the_locking_inset) {
1115         the_locking_inset->ShowInsetCursor(bv);
1116         return;
1117     }
1118     if (!cursor_visible) {
1119         LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1120         
1121         int asc = lyxfont::maxAscent(font);
1122         int desc = lyxfont::maxDescent(font);
1123
1124         bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1125         bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1126         cursor_visible = true;
1127     }
1128 }
1129
1130
1131 void InsetText::HideInsetCursor(BufferView * bv)
1132 {
1133     if (cursor_visible) {
1134         bv->hideLockedInsetCursor();
1135         cursor_visible = false;
1136     }
1137     if (the_locking_inset)
1138         the_locking_inset->HideInsetCursor(bv);
1139 }
1140
1141
1142 UpdatableInset::RESULT
1143 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1144 {
1145     if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1146         return FINISHED;
1147     if (activate_inset && checkAndActivateInset(bv, false))
1148         return DISPATCHED;
1149     TEXT(bv)->CursorRight(bv, selecting);
1150     return DISPATCHED_NOUPDATE;
1151 }
1152
1153
1154 UpdatableInset::RESULT
1155 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1156 {
1157     if (!cpar(bv)->previous && (cpos(bv) <= 0))
1158         return FINISHED;
1159     TEXT(bv)->CursorLeft(bv, selecting);
1160     if (activate_inset && checkAndActivateInset(bv, true))
1161         return DISPATCHED;
1162     return DISPATCHED_NOUPDATE;
1163 }
1164
1165
1166 UpdatableInset::RESULT
1167 InsetText::moveUp(BufferView * bv)
1168 {
1169     if (!crow(bv)->previous())
1170         return FINISHED;
1171     TEXT(bv)->CursorUp(bv);
1172     return DISPATCHED_NOUPDATE;
1173 }
1174
1175
1176 UpdatableInset::RESULT
1177 InsetText::moveDown(BufferView * bv)
1178 {
1179     if (!crow(bv)->next())
1180         return FINISHED;
1181     TEXT(bv)->CursorDown(bv);
1182     return DISPATCHED_NOUPDATE;
1183 }
1184
1185
1186 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1187 {
1188     if (the_locking_inset) {
1189         if (the_locking_inset->InsertInsetAllowed(inset))
1190             return the_locking_inset->InsertInset(bv, inset);
1191         return false;
1192     }
1193     bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1194 #ifndef NEW_INSETS
1195               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1196               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1197 #else
1198               bv->text->cursor.par()->previous,
1199               bv->text->cursor.par()->next
1200 #endif
1201             );
1202     if (inset->Editable() == Inset::IS_EDITABLE) {
1203         UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1204         i->setOwner(static_cast<UpdatableInset *>(this));
1205     }
1206     HideInsetCursor(bv);
1207     TEXT(bv)->InsertInset(bv, inset);
1208     TEXT(bv)->selection = 0;
1209     bv->fitCursor(TEXT(bv));
1210     UpdateLocal(bv, CURSOR_PAR, true);
1211     static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1212     ShowInsetCursor(bv);
1213     return true;
1214 }
1215
1216
1217 UpdatableInset * InsetText::GetLockingInset()
1218 {
1219     return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1220 }
1221
1222
1223 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1224 {
1225     if (c == LyxCode())
1226         return this;
1227     if (the_locking_inset)
1228         return the_locking_inset->GetFirstLockingInsetOfType(c);
1229     return 0;
1230 }
1231
1232
1233 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1234 {
1235     bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1236 #ifndef NEW_INSETS
1237               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1238               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1239 #else
1240               bv->text->cursor.par()->previous,
1241               bv->text->cursor.par()->next
1242 #endif
1243             );
1244     TEXT(bv)->SetFont(bv, font, toggleall);
1245     bv->fitCursor(TEXT(bv));
1246     UpdateLocal(bv, CURSOR_PAR, true);
1247 }
1248
1249
1250 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1251 {
1252     if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1253         unsigned int x;
1254         unsigned int y;
1255         Inset * inset =
1256             static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1257         if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1258             return false;
1259         LyXFont const font =
1260                 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1261         if (behind) {
1262             x = inset->width(bv, font);
1263             y = inset->descent(bv, font);
1264         } else {
1265             x = y = 0;
1266         }
1267         inset_x = cx(bv) - top_x + drawTextXOffset;
1268         inset_y = cy(bv) + drawTextYOffset;
1269         inset->Edit(bv, x - inset_x, y - inset_y, 0);
1270         if (!the_locking_inset)
1271             return false;
1272         UpdateLocal(bv, CURSOR_PAR, false);
1273         return true;
1274     }
1275     return false;
1276 }
1277
1278
1279 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1280                                       int button)
1281 {
1282     x = x - drawTextXOffset;
1283     y = y + insetAscent;
1284     Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1285
1286     if (inset) {
1287         if (x < 0)
1288             x = insetWidth;
1289         if (y < 0)
1290             y = insetDescent;
1291         inset_x = cx(bv) - top_x + drawTextXOffset;
1292         inset_y = cy(bv) + drawTextYOffset;
1293         inset->Edit(bv, x - inset_x, y - inset_y, button);
1294         if (!the_locking_inset)
1295             return false;
1296         UpdateLocal(bv, CURSOR_PAR, false);
1297         return true;
1298     }
1299     return false;
1300 }
1301
1302
1303 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1304 {
1305     int w = UpdatableInset::getMaxWidth(pain, inset);
1306     if (w < 0) {
1307         return w;
1308     }
1309     if (owner()) {
1310         w = w - top_x + owner()->x();
1311         return w;
1312     }
1313     w -= (2 * TEXT_TO_INSET_OFFSET);
1314     return w - top_x;
1315 //    return  w - (2*TEXT_TO_INSET_OFFSET);
1316 }
1317
1318
1319 void InsetText::SetParagraphData(LyXParagraph *p)
1320 {
1321     LyXParagraph * np;
1322
1323     if (par) {
1324         np = par->next;
1325         delete par;
1326         while(np) {
1327             par = np;
1328             np = np->next;
1329             delete par;
1330         }
1331     }
1332     par = p->Clone();
1333     par->SetInsetOwner(this);
1334     np = par;
1335     while(p->next) {
1336         p = p->next;
1337         np->next = p->Clone();
1338         np->next->previous = np;
1339         np = np->next;
1340         np->SetInsetOwner(this);
1341     }
1342     need_update = INIT;
1343 }
1344
1345
1346 void InsetText::SetAutoBreakRows(bool flag)
1347 {
1348     if (flag != autoBreakRows) {
1349         autoBreakRows = flag;
1350         need_update = FULL;
1351         if (!flag)
1352             removeNewlines();
1353     }
1354 }
1355
1356
1357 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1358 {
1359     if (how != drawFrame) {
1360         drawFrame = how;
1361         if (bv)
1362             UpdateLocal(bv, DRAW_FRAME, false);
1363     }
1364 }
1365
1366
1367 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1368 {
1369     if (frame_color != col) {
1370         frame_color = col;
1371         if (bv)
1372             UpdateLocal(bv, DRAW_FRAME, false);
1373     }
1374 }
1375
1376 #if 0
1377 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1378 {
1379     return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1380 }
1381 #endif
1382
1383
1384 int InsetText::cx(BufferView * bv) const
1385 {
1386     return TEXT(bv)->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1387 }
1388
1389
1390 int InsetText::cy(BufferView * bv) const
1391 {
1392     LyXFont font;
1393     return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1394 }
1395
1396
1397 int InsetText::cpos(BufferView * bv) const
1398 {
1399     return TEXT(bv)->cursor.pos();
1400 }
1401
1402
1403 LyXParagraph * InsetText::cpar(BufferView * bv) const
1404 {
1405     return TEXT(bv)->cursor.par();
1406 }
1407
1408
1409 Row * InsetText::crow(BufferView * bv) const
1410 {
1411     return TEXT(bv)->cursor.row();
1412 }
1413
1414
1415 LyXText * InsetText::getLyXText(BufferView * bv) const
1416 {
1417     if (cache.find(bv) != cache.end())
1418         return cache[bv];
1419     LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1420     lt->init(bv);
1421     cache[bv] = lt;
1422     if (the_locking_inset) {
1423         lt->SetCursor(bv, inset_par, inset_pos);
1424     }
1425     return lt;
1426 }
1427
1428
1429 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1430 {
1431     if (cache.find(bv) == cache.end())
1432         return;
1433     delete cache[bv];
1434     cache.erase(bv);
1435     if (recursive) {
1436         /// then remove all LyXText in text-insets
1437         LyXParagraph * p = par;
1438         for(;p;p = p->next) {
1439             p->deleteInsetsLyXText(bv);
1440         }
1441     }
1442 }
1443
1444
1445 void InsetText::resizeLyXText(BufferView * bv) const
1446 {
1447     if (!par->next && !par->size()) // resize not neccessary!
1448         return;
1449     if (cache.find(bv) == cache.end())
1450         return;
1451
1452     LyXParagraph * lpar = 0;
1453     LyXParagraph * selstartpar = 0;
1454     LyXParagraph * selendpar = 0;
1455     int pos = 0;
1456     int selstartpos = 0;
1457     int selendpos = 0;
1458     int selection = 0;
1459     int mark_set = 0;
1460
1461 //    ProhibitInput(bv);
1462
1463     if (locked) {
1464         lpar = TEXT(bv)->cursor.par();
1465         pos = TEXT(bv)->cursor.pos();
1466         selstartpar = TEXT(bv)->sel_start_cursor.par();
1467         selstartpos = TEXT(bv)->sel_start_cursor.pos();
1468         selendpar = TEXT(bv)->sel_end_cursor.par();
1469         selendpos = TEXT(bv)->sel_end_cursor.pos();
1470         selection = TEXT(bv)->selection;
1471         mark_set = TEXT(bv)->mark_set;
1472     }
1473     deleteLyXText(bv, (the_locking_inset == 0));
1474
1475     if (lpar) {
1476         TEXT(bv)->selection = true;
1477         /* at this point just to avoid the Delete-Empty-Paragraph
1478          * Mechanism when setting the cursor */
1479         TEXT(bv)->mark_set = mark_set;
1480         if (selection) {
1481             TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1482             TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1483             TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1484             TEXT(bv)->SetSelection();
1485             TEXT(bv)->SetCursor(bv, lpar, pos);
1486         } else {
1487             TEXT(bv)->SetCursor(bv, lpar, pos);
1488             TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1489             TEXT(bv)->selection = false;
1490         }
1491     }
1492     if (bv->screen())
1493             TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1494     // this will scroll the screen such that the cursor becomes visible 
1495     bv->updateScrollbar();
1496 //    AllowInput(bv);
1497     if (the_locking_inset) {
1498         /// then resize all LyXText in text-insets
1499         inset_x = cx(bv) - top_x + drawTextXOffset;
1500         inset_y = cy(bv) + drawTextYOffset;
1501         for(LyXParagraph * p = par; p; p = p->next) {
1502             p->resizeInsetsLyXText(bv);
1503         }
1504     }
1505     need_update = FULL;
1506 }
1507
1508
1509 void InsetText::removeNewlines()
1510 {
1511     for(LyXParagraph * p = par; p; p = p->next) {
1512         for(int i = 0; i < p->Last(); ++i) {
1513             if (p->GetChar(i) == LyXParagraph::META_NEWLINE)
1514                 p->Erase(i);
1515         }
1516     }
1517 }