]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
Moved the_locking_inset form BufferView to LyXText (where it belongs!)
[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 (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, bool show)
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         if (show)
1126             bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1127         cursor_visible = true;
1128     }
1129 }
1130
1131
1132 void InsetText::HideInsetCursor(BufferView * bv)
1133 {
1134     if (cursor_visible) {
1135         bv->hideLockedInsetCursor();
1136         cursor_visible = false;
1137     }
1138     if (the_locking_inset)
1139         the_locking_inset->HideInsetCursor(bv);
1140 }
1141
1142
1143 UpdatableInset::RESULT
1144 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1145 {
1146     if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1147         return FINISHED;
1148     if (activate_inset && checkAndActivateInset(bv, false))
1149         return DISPATCHED;
1150     TEXT(bv)->CursorRight(bv, selecting);
1151     return DISPATCHED_NOUPDATE;
1152 }
1153
1154
1155 UpdatableInset::RESULT
1156 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1157 {
1158     if (!cpar(bv)->previous && (cpos(bv) <= 0))
1159         return FINISHED;
1160     TEXT(bv)->CursorLeft(bv, selecting);
1161     if (activate_inset && checkAndActivateInset(bv, true))
1162         return DISPATCHED;
1163     return DISPATCHED_NOUPDATE;
1164 }
1165
1166
1167 UpdatableInset::RESULT
1168 InsetText::moveUp(BufferView * bv)
1169 {
1170     if (!crow(bv)->previous())
1171         return FINISHED;
1172     TEXT(bv)->CursorUp(bv);
1173     return DISPATCHED_NOUPDATE;
1174 }
1175
1176
1177 UpdatableInset::RESULT
1178 InsetText::moveDown(BufferView * bv)
1179 {
1180     if (!crow(bv)->next())
1181         return FINISHED;
1182     TEXT(bv)->CursorDown(bv);
1183     return DISPATCHED_NOUPDATE;
1184 }
1185
1186
1187 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1188 {
1189     if (the_locking_inset) {
1190         if (the_locking_inset->InsertInsetAllowed(inset))
1191             return the_locking_inset->InsertInset(bv, inset);
1192         return false;
1193     }
1194     bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1195 #ifndef NEW_INSETS
1196               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1197               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1198 #else
1199               bv->text->cursor.par()->previous,
1200               bv->text->cursor.par()->next
1201 #endif
1202             );
1203     if (inset->Editable() == Inset::IS_EDITABLE) {
1204         UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1205         i->setOwner(static_cast<UpdatableInset *>(this));
1206     }
1207     HideInsetCursor(bv);
1208     TEXT(bv)->InsertInset(bv, inset);
1209     TEXT(bv)->selection = 0;
1210     bv->fitCursor(TEXT(bv));
1211     UpdateLocal(bv, CURSOR_PAR, true);
1212     static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1213     ShowInsetCursor(bv);
1214     return true;
1215 }
1216
1217
1218 UpdatableInset * InsetText::GetLockingInset()
1219 {
1220     return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1221 }
1222
1223
1224 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1225 {
1226     if (c == LyxCode())
1227         return this;
1228     if (the_locking_inset)
1229         return the_locking_inset->GetFirstLockingInsetOfType(c);
1230     return 0;
1231 }
1232
1233
1234 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1235 {
1236     bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1237 #ifndef NEW_INSETS
1238               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1239               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1240 #else
1241               bv->text->cursor.par()->previous,
1242               bv->text->cursor.par()->next
1243 #endif
1244             );
1245     TEXT(bv)->SetFont(bv, font, toggleall);
1246     bv->fitCursor(TEXT(bv));
1247     UpdateLocal(bv, CURSOR_PAR, true);
1248 }
1249
1250
1251 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1252 {
1253     if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1254         unsigned int x;
1255         unsigned int y;
1256         Inset * inset =
1257             static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1258         if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1259             return false;
1260         LyXFont const font =
1261                 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1262         if (behind) {
1263             x = inset->width(bv, font);
1264             y = inset->descent(bv, font);
1265         } else {
1266             x = y = 0;
1267         }
1268         inset_x = cx(bv) - top_x + drawTextXOffset;
1269         inset_y = cy(bv) + drawTextYOffset;
1270         inset->Edit(bv, x - inset_x, y - inset_y, 0);
1271         if (!the_locking_inset)
1272             return false;
1273         UpdateLocal(bv, CURSOR_PAR, false);
1274         return true;
1275     }
1276     return false;
1277 }
1278
1279
1280 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1281                                       int button)
1282 {
1283     x = x - drawTextXOffset;
1284     y = y + insetAscent;
1285     Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1286
1287     if (inset) {
1288         if (x < 0)
1289             x = insetWidth;
1290         if (y < 0)
1291             y = insetDescent;
1292         inset_x = cx(bv) - top_x + drawTextXOffset;
1293         inset_y = cy(bv) + drawTextYOffset;
1294         inset->Edit(bv, x - inset_x, y - inset_y, button);
1295         if (!the_locking_inset)
1296             return false;
1297         UpdateLocal(bv, CURSOR_PAR, false);
1298         return true;
1299     }
1300     return false;
1301 }
1302
1303
1304 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1305 {
1306     int w = UpdatableInset::getMaxWidth(pain, inset);
1307     if (w < 0) {
1308         return w;
1309     }
1310     if (owner()) {
1311         w = w - top_x + owner()->x();
1312         return w;
1313     }
1314     w -= (2 * TEXT_TO_INSET_OFFSET);
1315     return w - top_x;
1316 //    return  w - (2*TEXT_TO_INSET_OFFSET);
1317 }
1318
1319
1320 void InsetText::SetParagraphData(LyXParagraph *p)
1321 {
1322     LyXParagraph * np;
1323
1324     if (par) {
1325         np = par->next;
1326         delete par;
1327         while(np) {
1328             par = np;
1329             np = np->next;
1330             delete par;
1331         }
1332     }
1333     par = p->Clone();
1334     par->SetInsetOwner(this);
1335     np = par;
1336     while(p->next) {
1337         p = p->next;
1338         np->next = p->Clone();
1339         np->next->previous = np;
1340         np = np->next;
1341         np->SetInsetOwner(this);
1342     }
1343     need_update = INIT;
1344 }
1345
1346
1347 void InsetText::SetAutoBreakRows(bool flag)
1348 {
1349     if (flag != autoBreakRows) {
1350         autoBreakRows = flag;
1351         need_update = FULL;
1352         if (!flag)
1353             removeNewlines();
1354     }
1355 }
1356
1357
1358 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1359 {
1360     if (how != drawFrame) {
1361         drawFrame = how;
1362         if (bv)
1363             UpdateLocal(bv, DRAW_FRAME, false);
1364     }
1365 }
1366
1367
1368 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1369 {
1370     if (frame_color != col) {
1371         frame_color = col;
1372         if (bv)
1373             UpdateLocal(bv, DRAW_FRAME, false);
1374     }
1375 }
1376
1377 #if 0
1378 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1379 {
1380     return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1381 }
1382 #endif
1383
1384
1385 int InsetText::cx(BufferView * bv) const
1386 {
1387     return TEXT(bv)->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1388 }
1389
1390
1391 int InsetText::cy(BufferView * bv) const
1392 {
1393     LyXFont font;
1394     return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1395 }
1396
1397
1398 int InsetText::cpos(BufferView * bv) const
1399 {
1400     return TEXT(bv)->cursor.pos();
1401 }
1402
1403
1404 LyXParagraph * InsetText::cpar(BufferView * bv) const
1405 {
1406     return TEXT(bv)->cursor.par();
1407 }
1408
1409
1410 Row * InsetText::crow(BufferView * bv) const
1411 {
1412     return TEXT(bv)->cursor.row();
1413 }
1414
1415
1416 LyXText * InsetText::getLyXText(BufferView * bv) const
1417 {
1418     if (cache.find(bv) != cache.end())
1419         return cache[bv];
1420     LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1421     lt->init(bv);
1422     cache[bv] = lt;
1423     if (the_locking_inset) {
1424         lt->SetCursor(bv, inset_par, inset_pos);
1425     }
1426     return lt;
1427 }
1428
1429
1430 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1431 {
1432     if (cache.find(bv) == cache.end())
1433         return;
1434     delete cache[bv];
1435     cache.erase(bv);
1436     if (recursive) {
1437         /// then remove all LyXText in text-insets
1438         LyXParagraph * p = par;
1439         for(;p;p = p->next) {
1440             p->deleteInsetsLyXText(bv);
1441         }
1442     }
1443 }
1444
1445
1446 void InsetText::resizeLyXText(BufferView * bv) const
1447 {
1448     if (!par->next && !par->size()) // resize not neccessary!
1449         return;
1450     if (cache.find(bv) == cache.end())
1451         return;
1452
1453     LyXParagraph * lpar = 0;
1454     LyXParagraph * selstartpar = 0;
1455     LyXParagraph * selendpar = 0;
1456     int pos = 0;
1457     int selstartpos = 0;
1458     int selendpos = 0;
1459     int selection = 0;
1460     int mark_set = 0;
1461
1462 //    ProhibitInput(bv);
1463
1464     if (locked) {
1465         lpar = TEXT(bv)->cursor.par();
1466         pos = TEXT(bv)->cursor.pos();
1467         selstartpar = TEXT(bv)->sel_start_cursor.par();
1468         selstartpos = TEXT(bv)->sel_start_cursor.pos();
1469         selendpar = TEXT(bv)->sel_end_cursor.par();
1470         selendpos = TEXT(bv)->sel_end_cursor.pos();
1471         selection = TEXT(bv)->selection;
1472         mark_set = TEXT(bv)->mark_set;
1473     }
1474     deleteLyXText(bv, (the_locking_inset == 0));
1475
1476     if (lpar) {
1477         TEXT(bv)->selection = true;
1478         /* at this point just to avoid the Delete-Empty-Paragraph
1479          * Mechanism when setting the cursor */
1480         TEXT(bv)->mark_set = mark_set;
1481         if (selection) {
1482             TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1483             TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1484             TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1485             TEXT(bv)->SetSelection();
1486             TEXT(bv)->SetCursor(bv, lpar, pos);
1487         } else {
1488             TEXT(bv)->SetCursor(bv, lpar, pos);
1489             TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1490             TEXT(bv)->selection = false;
1491         }
1492     }
1493     if (bv->screen())
1494             TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1495     // this will scroll the screen such that the cursor becomes visible 
1496     bv->updateScrollbar();
1497 //    AllowInput(bv);
1498     if (the_locking_inset) {
1499         /// then resize all LyXText in text-insets
1500         inset_x = cx(bv) - top_x + drawTextXOffset;
1501         inset_y = cy(bv) + drawTextYOffset;
1502         for(LyXParagraph * p = par; p; p = p->next) {
1503             p->resizeInsetsLyXText(bv);
1504         }
1505     }
1506     need_update = FULL;
1507 }
1508
1509
1510 void InsetText::removeNewlines()
1511 {
1512     for(LyXParagraph * p = par; p; p = p->next) {
1513         for(int i = 0; i < p->Last(); ++i) {
1514             if (p->GetChar(i) == LyXParagraph::META_NEWLINE)
1515                 p->Erase(i);
1516         }
1517     }
1518 }