]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
9040338c6074c632d1d4a652e619be353527d686
[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         unsigned 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 ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)) {
340             pain.rectangle(top_x + 1, baseline - insetAscent + 1,
341                            width(bv, f) - 1, insetAscent + insetDescent - 1,
342                            frame_color);
343     } else if (need_update == CLEAR_FRAME) {
344             pain.rectangle(top_x + 1, baseline - insetAscent + 1,
345                            width(bv, f) - 1, insetAscent + insetDescent - 1,
346                            LColor::background);
347     }
348     x += width(bv, f) - TEXT_TO_INSET_OFFSET;
349     if (bv->text->status==LyXText::CHANGED_IN_DRAW)
350         need_update = INIT;
351     else if (need_update != INIT)
352         need_update = NONE;
353 }
354
355
356 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
357 {
358     if (reinit) {  // && (need_update != CURSOR)) {
359         need_update = INIT;
360         resizeLyXText(bv);
361         if (owner())
362             owner()->update(bv, font, true);
363         return;
364     }
365     if (the_locking_inset) {
366         inset_x = cx(bv) - top_x + drawTextXOffset;
367         inset_y = cy(bv) + drawTextYOffset;
368         the_locking_inset->update(bv, font, reinit);
369     }
370     if (need_update == INIT) {
371         resizeLyXText(bv);
372         need_update = FULL;
373 //      if (!owner() && bv->text)
374 //          bv->text->UpdateInset(bv, this);
375     }
376     int oldw = insetWidth;
377 #if 1
378     insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
379     // max(textWidth(bv->painter()),
380     // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
381     // (2 * TEXT_TO_INSET_OFFSET);
382 #else
383     insetWidth = textWidth(bv->painter());
384     if (insetWidth < 0)
385             insetWidth = static_cast<int>(TEXT(bv)->width);
386 #endif
387     if (oldw != insetWidth) {
388 //          printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
389 //                 textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
390         resizeLyXText(bv);
391         need_update = FULL;
392 #if 0
393         if (owner()) {
394             owner()->update(bv, font, reinit);
395             return;
396         } else {
397             update(bv, font, reinit);
398         }
399 #else
400 #if 1
401         update(bv, font, reinit);
402 #else
403         UpdateLocal(bv, INIT, false);
404 #endif
405 #endif
406         return;
407     }
408     if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
409         the_locking_inset)
410     {
411         TEXT(bv)->UpdateInset(bv, the_locking_inset);
412     }
413
414     if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
415         need_update = FULL;
416
417     int y_temp = 0;
418     Row * row = TEXT(bv)->GetRowNearY(y_temp);
419     insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
420     insetDescent = TEXT(bv)->height - row->ascent_of_text() +
421         TEXT_TO_INSET_OFFSET;
422 }
423
424
425 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
426 {
427     TEXT(bv)->FullRebreak(bv);
428     if (need_update != INIT) {
429         if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
430             need_update = FULL;
431         else if (!the_locking_inset || (what != CURSOR))
432             need_update = what;
433     }
434     if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
435         TEXT(bv)->selection)
436             bv->updateInset(this, mark_dirty);
437     bv->owner()->showState();
438     if (old_par != cpar(bv)) {
439             bv->owner()->setLayout(cpar(bv)->GetLayout());
440             old_par = cpar(bv);
441     }
442 }
443
444
445 string const InsetText::EditMessage() const
446 {
447     return _("Opened Text Inset");
448 }
449
450
451 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
452 {
453 //    par->SetInsetOwner(this);
454     UpdatableInset::Edit(bv, x, y, button);
455
456     if (!bv->lockInset(this)) {
457         lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
458         return;
459     }
460     locked = true;
461     the_locking_inset = 0;
462     inset_pos = inset_x = inset_y = 0;
463     inset_par = 0;
464     old_par = 0;
465     if (!checkAndActivateInset(bv, x, y, button))
466         TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
467                                            y+TEXT(bv)->first+insetAscent);
468     TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
469     bv->text->FinishUndo();
470     UpdateLocal(bv, FULL, false);
471 }
472
473
474 void InsetText::InsetUnlock(BufferView * bv)
475 {
476     if (the_locking_inset) {
477         the_locking_inset->InsetUnlock(bv);
478         the_locking_inset = 0;
479     }
480     HideInsetCursor(bv);
481     no_selection = false;
482     locked = false;
483     TEXT(bv)->selection = 0;
484     UpdateLocal(bv, CLEAR_FRAME, false);
485     if (owner())
486             bv->owner()->setLayout(owner()->getLyXText(bv)
487                                     ->cursor.par()->GetLayout());
488     else
489             bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
490 }
491
492
493 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
494 {
495     lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
496     if (!inset)
497         return false;
498     if (inset == cpar(bv)->GetInset(cpos(bv))) {
499         lyxerr[Debug::INSETS] << "OK" << endl;
500         the_locking_inset = inset;
501         inset_x = cx(bv) - top_x + drawTextXOffset;
502         inset_y = cy(bv) + drawTextYOffset;
503         inset_pos = cpos(bv);
504         inset_par = cpar(bv);
505         TEXT(bv)->UpdateInset(bv, the_locking_inset);
506         return true;
507     } else if (the_locking_inset && (the_locking_inset == inset)) {
508         if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
509             lyxerr[Debug::INSETS] << "OK" << endl;
510             inset_x = cx(bv) - top_x + drawTextXOffset;
511             inset_y = cy(bv) + drawTextYOffset;
512         } else {
513             lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
514         }
515     } else if (the_locking_inset) {
516         lyxerr[Debug::INSETS] << "MAYBE" << endl;
517         return the_locking_inset->LockInsetInInset(bv, inset);
518     }
519     lyxerr[Debug::INSETS] << "NOT OK" << endl;
520     return false;
521 }
522
523
524 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
525                                    bool lr)
526 {
527     if (!the_locking_inset)
528         return false;
529     if (the_locking_inset == inset) {
530         the_locking_inset->InsetUnlock(bv);
531         TEXT(bv)->UpdateInset(bv, inset);
532         the_locking_inset = 0;
533         if (lr)
534             moveRight(bv, false);
535         old_par = 0; // force layout setting
536         UpdateLocal(bv, CURSOR_PAR, false);
537         return true;
538     }
539     return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
540 }
541
542
543 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
544 {
545     if (!the_locking_inset)
546         return false;
547     if (the_locking_inset != inset) {
548         TEXT(bv)->UpdateInset(bv, the_locking_inset);
549         need_update = CURSOR_PAR;
550         return the_locking_inset->UpdateInsetInInset(bv, inset);
551     }
552 //    UpdateLocal(bv, FULL, false);
553     if (TEXT(bv)->UpdateInset(bv, inset))
554         UpdateLocal(bv, CURSOR_PAR, false);
555     if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
556         inset_x = cx(bv) - top_x + drawTextXOffset;
557         inset_y = cy(bv) + drawTextYOffset;
558     }
559     return true;
560 }
561
562
563 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
564 {
565     no_selection = false;
566
567     int tmp_x = x - drawTextXOffset;
568     int tmp_y = y + insetAscent;
569     Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
570
571     HideInsetCursor(bv);
572     if (the_locking_inset) {
573         if (the_locking_inset == inset) {
574             the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
575             return;
576         } else if (inset) {
577             // otherwise unlock the_locking_inset and lock the new inset
578             the_locking_inset->InsetUnlock(bv);
579             inset_x = cx(bv) - top_x + drawTextXOffset;
580             inset_y = cy(bv) + drawTextYOffset;
581             inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
582             inset->Edit(bv, x - inset_x, y - inset_y, button);
583             if (the_locking_inset) {
584                 UpdateLocal(bv, CURSOR_PAR, false);
585             }
586             return;
587         }
588         // otherwise only unlock the_locking_inset
589         the_locking_inset->InsetUnlock(bv);
590         the_locking_inset = 0;
591     }
592     if (bv->the_locking_inset) {
593         if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
594             UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
595             inset_x = cx(bv) - top_x + drawTextXOffset;
596             inset_y = cy(bv) + drawTextYOffset;
597             inset_pos = cpos(bv);
598             inset_par = cpar(bv);
599             uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
600             uinset->Edit(bv, x - inset_x, y - inset_y, 0);
601             if (the_locking_inset) {
602                 UpdateLocal(bv, CURSOR_PAR, false);
603             }
604             return;
605         }
606     }
607     if (!inset) {
608         bool paste_internally = false;
609         if ((button == 2) && TEXT(bv)->selection) {
610             LocalDispatch(bv, LFUN_COPY, "");
611             paste_internally = true;
612         }
613         TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
614                                            y+TEXT(bv)->first+insetAscent);
615         TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
616         UpdateLocal(bv, CURSOR, false);
617         bv->owner()->setLayout(cpar(bv)->GetLayout());
618         old_par = cpar(bv);
619         // Insert primary selection with middle mouse
620         // if there is a local selection in the current buffer,
621         // insert this
622         if (button == 2) {
623             if (paste_internally)
624                 LocalDispatch(bv, LFUN_PASTE, "");
625             else
626                 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
627         }
628     }
629     ShowInsetCursor(bv);
630 }
631
632
633 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
634 {
635     UpdatableInset * inset = 0;
636
637     if (the_locking_inset) {
638             the_locking_inset->InsetButtonRelease(bv,
639                                                   x - inset_x, y - inset_y,
640                                                   button);
641     } else {
642         if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
643             inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
644             if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
645                 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
646             } else {
647                 inset_x = cx(bv) - top_x + drawTextXOffset;
648                 inset_y = cy(bv) + drawTextYOffset;
649                 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
650                 inset->Edit(bv, x - inset_x, y - inset_y, button);
651             }
652             UpdateLocal(bv, CURSOR_PAR, false);
653         }
654     }
655     no_selection = false;
656 }
657
658
659 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
660 {
661     if (the_locking_inset) {
662         the_locking_inset->InsetMotionNotify(bv, x - inset_x,
663                                              y - inset_y,state);
664         return;
665     }
666     if (!no_selection) {
667         HideInsetCursor(bv);
668         TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
669                                            y+TEXT(bv)->first+insetAscent);
670         TEXT(bv)->SetSelection();
671         if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
672             TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
673             UpdateLocal(bv, SELECTION, false);
674         ShowInsetCursor(bv);
675     }
676     no_selection = false;
677 }
678
679
680 void InsetText::InsetKeyPress(XKeyEvent * xke)
681 {
682     if (the_locking_inset) {
683         the_locking_inset->InsetKeyPress(xke);
684         return;
685     }
686 }
687
688
689 UpdatableInset::RESULT
690 InsetText::LocalDispatch(BufferView * bv,
691                          int action, string const & arg)
692 {
693     no_selection = false;
694     UpdatableInset::RESULT
695         result= UpdatableInset::LocalDispatch(bv, action, arg);
696     if (result != UNDISPATCHED) {
697         return DISPATCHED;
698     }
699
700     result=DISPATCHED;
701     if ((action < 0) && arg.empty())
702         return FINISHED;
703
704     if (the_locking_inset) {
705         result = the_locking_inset->LocalDispatch(bv, action, arg);
706         if (result == DISPATCHED_NOUPDATE)
707             return result;
708         else if (result == DISPATCHED) {
709             UpdateLocal(bv, CURSOR_PAR, false);
710             return result;
711         } else if (result == FINISHED) {
712             switch(action) {
713             case -1:
714             case LFUN_RIGHT:
715                 moveRight(bv, false);
716                 break;
717             case LFUN_DOWN:
718                 moveDown(bv);
719                 break;
720             }
721             the_locking_inset = 0;
722             return DISPATCHED;
723         }
724     }
725     HideInsetCursor(bv);
726     switch (action) {
727         // Normal chars
728     case -1:
729         if (bv->buffer()->isReadonly()) {
730             LyXBell();
731 //          setErrorMessage(N_("Document is read only"));
732             break;
733         }
734         if (!arg.empty()) {
735             /* Automatically delete the currently selected
736              * text and replace it with what is being
737              * typed in now. Depends on lyxrc settings
738              * "auto_region_delete", which defaults to
739              * true (on). */
740
741             bv->text->SetUndo(bv->buffer(), Undo::INSERT,
742 #ifndef NEW_INSETS
743                               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
744                               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
745 #else
746                               bv->text->cursor.par()->previous,
747                               bv->text->cursor.par()->next
748 #endif
749                     );
750             if (lyxrc.auto_region_delete) {
751                 if (TEXT(bv)->selection){
752                     TEXT(bv)->CutSelection(bv, false);
753                 }
754             }
755             TEXT(bv)->ClearSelection();
756             for (string::size_type i = 0; i < arg.length(); ++i) {
757                 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
758             }
759         }
760         UpdateLocal(bv, CURSOR_PAR, true);
761         break;
762         // --- Cursor Movements ---------------------------------------------
763     case LFUN_RIGHTSEL:
764         bv->text->FinishUndo();
765         moveRight(bv, false, true);
766         TEXT(bv)->SetSelection();
767         UpdateLocal(bv, SELECTION, false);
768         break;
769     case LFUN_RIGHT:
770         result = moveRight(bv);
771         bv->text->FinishUndo();
772         UpdateLocal(bv, CURSOR, false);
773         break;
774     case LFUN_LEFTSEL:
775         bv->text->FinishUndo();
776         moveLeft(bv, false, true);
777         TEXT(bv)->SetSelection();
778         UpdateLocal(bv, SELECTION, false);
779         break;
780     case LFUN_LEFT:
781         bv->text->FinishUndo();
782         result= moveLeft(bv);
783         UpdateLocal(bv, CURSOR, false);
784         break;
785     case LFUN_DOWNSEL:
786         bv->text->FinishUndo();
787         moveDown(bv);
788         TEXT(bv)->SetSelection();
789         UpdateLocal(bv, SELECTION, false);
790         break;
791     case LFUN_DOWN:
792         bv->text->FinishUndo();
793         result = moveDown(bv);
794         UpdateLocal(bv, CURSOR, false);
795         break;
796     case LFUN_UPSEL:
797         bv->text->FinishUndo();
798         moveUp(bv);
799         TEXT(bv)->SetSelection();
800         UpdateLocal(bv, SELECTION, false);
801         break;
802     case LFUN_UP:
803         bv->text->FinishUndo();
804         result = moveUp(bv);
805         UpdateLocal(bv, CURSOR, false);
806         break;
807     case LFUN_HOME:
808         bv->text->FinishUndo();
809         TEXT(bv)->CursorHome(bv);
810         UpdateLocal(bv, CURSOR, false);
811         break;
812     case LFUN_END:
813         TEXT(bv)->CursorEnd(bv);
814         UpdateLocal(bv, CURSOR, false);
815         break;
816     case LFUN_BACKSPACE:
817         bv->text->SetUndo(bv->buffer(), Undo::DELETE,
818 #ifndef NEW_INSETS
819           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
820           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
821 #else
822           bv->text->cursor.par()->previous,
823           bv->text->cursor.par()->next
824 #endif
825                 );
826         if (TEXT(bv)->selection)
827             TEXT(bv)->CutSelection(bv);
828         else
829             TEXT(bv)->Backspace(bv);
830         UpdateLocal(bv, CURSOR_PAR, true);
831         break;
832     case LFUN_DELETE:
833         bv->text->SetUndo(bv->buffer(), Undo::DELETE,
834 #ifndef NEW_INSETS
835           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
836           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
837 #else
838           bv->text->cursor.par()->previous,
839           bv->text->cursor.par()->next
840 #endif
841                 );
842         if (TEXT(bv)->selection)
843             TEXT(bv)->CutSelection(bv);
844         else
845             TEXT(bv)->Delete(bv);
846         UpdateLocal(bv, CURSOR_PAR, true);
847         break;
848     case LFUN_CUT:
849         bv->text->SetUndo(bv->buffer(), Undo::DELETE,
850 #ifndef NEW_INSETS
851           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
852           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
853 #else
854           bv->text->cursor.par()->previous,
855           bv->text->cursor.par()->next
856 #endif
857                 );
858         TEXT(bv)->CutSelection(bv);
859         UpdateLocal(bv, CURSOR_PAR, true);
860         break;
861     case LFUN_COPY:
862         bv->text->FinishUndo();
863         TEXT(bv)->CopySelection(bv);
864         UpdateLocal(bv, CURSOR_PAR, false);
865         break;
866     case LFUN_PASTESELECTION:
867     {
868         string clip(bv->workarea()->getClipboard());
869         
870         if (clip.empty())
871             break;
872         if (arg == "paragraph") {
873                 TEXT(bv)->InsertStringB(bv, clip);
874         } else {
875                 TEXT(bv)->InsertStringA(bv, clip);
876         }
877         UpdateLocal(bv, CURSOR_PAR, true);
878         break;
879     }
880     case LFUN_PASTE:
881         if (!autoBreakRows) {
882             CutAndPaste cap;
883
884             if (cap.nrOfParagraphs() > 1) {
885                 WriteAlert(_("Impossible operation"),
886                            _("Cannot include more than one paragraph!"),
887                            _("Sorry."));
888                 break;
889             }
890         }
891         bv->text->SetUndo(bv->buffer(), Undo::INSERT,
892 #ifndef NEW_INSETS
893           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
894           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
895 #else
896           bv->text->cursor.par()->previous,
897           bv->text->cursor.par()->next
898 #endif
899                 );
900         TEXT(bv)->PasteSelection(bv);
901         UpdateLocal(bv, CURSOR_PAR, true);
902         break;
903     case LFUN_BREAKPARAGRAPH:
904         if (!autoBreakRows)
905             return DISPATCHED;
906         TEXT(bv)->BreakParagraph(bv, 0);
907         UpdateLocal(bv, FULL, true);
908         break;
909     case LFUN_BREAKLINE:
910         if (!autoBreakRows)
911             return DISPATCHED;
912         bv->text->SetUndo(bv->buffer(), Undo::INSERT,
913 #ifndef NEW_INSETS
914             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
915             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
916 #else
917             bv->text->cursor.par()->previous,
918             bv->text->cursor.par()->next
919 #endif
920                 );
921         TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
922         UpdateLocal(bv, CURSOR_PAR, true);
923         break;
924     case LFUN_LAYOUT:
925         // do not set layouts on non breakable textinsets
926         if (autoBreakRows) {
927             LyXTextClass::size_type cur_layout = cpar(bv)->layout;
928       
929             // Derive layout number from given argument (string)
930             // and current buffer's textclass (number). */    
931             LyXTextClassList::ClassList::size_type tclass =
932                 bv->buffer()->params.textclass;
933             std::pair <bool, LyXTextClass::size_type> layout = 
934                 textclasslist.NumberOfLayout(tclass, arg);
935
936             // If the entry is obsolete, use the new one instead.
937             if (layout.first) {
938                 string obs = textclasslist.Style(tclass,layout.second).
939                     obsoleted_by();
940                 if (!obs.empty()) 
941                     layout = textclasslist.NumberOfLayout(tclass, obs);
942             }
943
944             // see if we found the layout number:
945             if (!layout.first) {
946                 string msg = string(N_("Layout ")) + arg + N_(" not known");
947
948                 bv->owner()->getMiniBuffer()->Set(msg);
949                 break;
950             }
951
952             if (cur_layout != layout.second) {
953                 cur_layout = layout.second;
954                 TEXT(bv)->SetLayout(bv, layout.second);
955                 bv->owner()->setLayout(cpar(bv)->GetLayout());
956                 UpdateLocal(bv, CURSOR_PAR, true);
957             }
958         } else {
959             // reset the layout box
960             bv->owner()->setLayout(cpar(bv)->GetLayout());
961         }
962         break;
963     case LFUN_PARAGRAPH_SPACING:
964             // This one is absolutely not working. When fiddling with this
965             // it also seems to me that the paragraphs inside the insettext
966             // inherit bufferparams/paragraphparams in a strange way. (Lgb)
967     {
968             LyXParagraph * par = TEXT(bv)->cursor.par();
969             Spacing::Space cur_spacing = par->spacing.getSpace();
970             float cur_value = 1.0;
971             if (cur_spacing == Spacing::Other) {
972                     cur_value = par->spacing.getValue();
973             }
974                         
975             std::istringstream istr(arg.c_str());
976             string tmp;
977             istr >> tmp;
978             Spacing::Space new_spacing = cur_spacing;
979             float new_value = cur_value;
980             if (tmp.empty()) {
981                     lyxerr << "Missing argument to `paragraph-spacing'"
982                            << endl;
983             } else if (tmp == "single") {
984                     new_spacing = Spacing::Single;
985             } else if (tmp == "onehalf") {
986                     new_spacing = Spacing::Onehalf;
987             } else if (tmp == "double") {
988                     new_spacing = Spacing::Double;
989             } else if (tmp == "other") {
990                     new_spacing = Spacing::Other;
991                     float tmpval = 0.0;
992                     istr >> tmpval;
993                     lyxerr << "new_value = " << tmpval << endl;
994                     if (tmpval != 0.0)
995                             new_value = tmpval;
996             } else if (tmp == "default") {
997                     new_spacing = Spacing::Default;
998             } else {
999                     lyxerr << _("Unknown spacing argument: ")
1000                            << arg << endl;
1001             }
1002             if (cur_spacing != new_spacing || cur_value != new_value) {
1003                     par->spacing.set(new_spacing, new_value);
1004                     //TEXT(bv)->RedoParagraph(owner->view());
1005                     UpdateLocal(bv, CURSOR_PAR, true);
1006                     //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1007             }
1008     }
1009     break;
1010         
1011     default:
1012         result = UNDISPATCHED;
1013         break;
1014     }
1015     if (result != FINISHED) {
1016         ShowInsetCursor(bv);
1017     } else
1018         bv->unlockInset(this);
1019     return result;
1020 }
1021
1022
1023 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1024 {
1025     TexRow texrow;
1026     buf->latexParagraphs(os, par, 0, texrow);
1027     return texrow.rows();
1028 }
1029
1030
1031 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1032 {
1033     LyXParagraph * p = par;
1034     unsigned int lines = 0;
1035     
1036     while (p) {
1037         string const tmp = buf->asciiParagraph(p, linelen);
1038         lines += countChar(tmp, '\n');
1039         os << tmp;
1040         p = p->next;
1041     }
1042     os << "\n";
1043     ++lines;
1044     return lines;
1045 }
1046
1047
1048 void InsetText::Validate(LaTeXFeatures & features) const
1049 {
1050     LyXParagraph * p = par;
1051     while(p) {
1052         p->validate(features);
1053         p = p->next;
1054     }
1055 }
1056
1057
1058 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1059 {
1060     if (textclasslist.Style(buf->params.textclass,
1061                             p->GetLayout()).labeltype != LABEL_MANUAL)
1062         return 0;
1063     else
1064         return p->BeginningOfMainBody();
1065 }
1066
1067
1068 void InsetText::GetCursorPos(BufferView * bv,
1069                              int & x, int & y) const
1070 {
1071     x = cx(bv);
1072     y = cy(bv);
1073 }
1074
1075
1076 unsigned int InsetText::InsetInInsetY()
1077 {
1078     if (!the_locking_inset)
1079         return 0;
1080
1081     return (inset_y + the_locking_inset->InsetInInsetY());
1082 }
1083
1084
1085 void InsetText::ToggleInsetCursor(BufferView * bv)
1086 {
1087     if (the_locking_inset) {
1088         the_locking_inset->ToggleInsetCursor(bv);
1089         return;
1090     }
1091
1092     LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1093
1094     int asc = lyxfont::maxAscent(font);
1095     int desc = lyxfont::maxDescent(font);
1096   
1097     if (cursor_visible)
1098         bv->hideLockedInsetCursor();
1099     else
1100         bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1101     cursor_visible = !cursor_visible;
1102 }
1103
1104
1105 void InsetText::ShowInsetCursor(BufferView * bv)
1106 {
1107     if (the_locking_inset) {
1108         the_locking_inset->ShowInsetCursor(bv);
1109         return;
1110     }
1111     if (!cursor_visible) {
1112         LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1113         
1114         int asc = lyxfont::maxAscent(font);
1115         int desc = lyxfont::maxDescent(font);
1116
1117         bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1118         bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1119         cursor_visible = true;
1120     }
1121 }
1122
1123
1124 void InsetText::HideInsetCursor(BufferView * bv)
1125 {
1126     if (cursor_visible) {
1127         bv->hideLockedInsetCursor();
1128         cursor_visible = false;
1129     }
1130     if (the_locking_inset)
1131         the_locking_inset->HideInsetCursor(bv);
1132 }
1133
1134
1135 UpdatableInset::RESULT
1136 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1137 {
1138     if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1139         return FINISHED;
1140     if (activate_inset && checkAndActivateInset(bv, false))
1141         return DISPATCHED;
1142     TEXT(bv)->CursorRight(bv, selecting);
1143     return DISPATCHED_NOUPDATE;
1144 }
1145
1146
1147 UpdatableInset::RESULT
1148 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1149 {
1150     if (!cpar(bv)->previous && (cpos(bv) <= 0))
1151         return FINISHED;
1152     TEXT(bv)->CursorLeft(bv, selecting);
1153     if (activate_inset && checkAndActivateInset(bv, true))
1154         return DISPATCHED;
1155     return DISPATCHED_NOUPDATE;
1156 }
1157
1158
1159 UpdatableInset::RESULT
1160 InsetText::moveUp(BufferView * bv)
1161 {
1162     if (!crow(bv)->previous())
1163         return FINISHED;
1164     TEXT(bv)->CursorUp(bv);
1165     return DISPATCHED_NOUPDATE;
1166 }
1167
1168
1169 UpdatableInset::RESULT
1170 InsetText::moveDown(BufferView * bv)
1171 {
1172     if (!crow(bv)->next())
1173         return FINISHED;
1174     TEXT(bv)->CursorDown(bv);
1175     return DISPATCHED_NOUPDATE;
1176 }
1177
1178
1179 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1180 {
1181     if (the_locking_inset) {
1182         if (the_locking_inset->InsertInsetAllowed(inset))
1183             return the_locking_inset->InsertInset(bv, inset);
1184         return false;
1185     }
1186     bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1187 #ifndef NEW_INSETS
1188               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1189               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1190 #else
1191               bv->text->cursor.par()->previous,
1192               bv->text->cursor.par()->next
1193 #endif
1194             );
1195     if (inset->Editable() == Inset::IS_EDITABLE) {
1196         UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1197         i->setOwner(static_cast<UpdatableInset *>(this));
1198     }
1199     HideInsetCursor(bv);
1200     TEXT(bv)->InsertInset(bv, inset);
1201     TEXT(bv)->selection = 0;
1202     bv->fitCursor(TEXT(bv));
1203     UpdateLocal(bv, CURSOR_PAR, true);
1204     static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1205     ShowInsetCursor(bv);
1206     return true;
1207 }
1208
1209
1210 UpdatableInset * InsetText::GetLockingInset()
1211 {
1212     return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1213 }
1214
1215
1216 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1217 {
1218     if (c == LyxCode())
1219         return this;
1220     if (the_locking_inset)
1221         return the_locking_inset->GetFirstLockingInsetOfType(c);
1222     return 0;
1223 }
1224
1225
1226 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1227 {
1228     TEXT(bv)->SetFont(bv, font, toggleall);
1229 }
1230
1231
1232 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1233 {
1234     if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1235         unsigned int x;
1236         unsigned int y;
1237         Inset * inset =
1238             static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1239         if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1240             return false;
1241         LyXFont const font =
1242                 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1243         if (behind) {
1244             x = inset->width(bv, font);
1245             y = inset->descent(bv, font);
1246         } else {
1247             x = y = 0;
1248         }
1249         inset_x = cx(bv) - top_x + drawTextXOffset;
1250         inset_y = cy(bv) + drawTextYOffset;
1251         inset->Edit(bv, x - inset_x, y - inset_y, 0);
1252         if (!the_locking_inset)
1253             return false;
1254         UpdateLocal(bv, CURSOR_PAR, false);
1255         return true;
1256     }
1257     return false;
1258 }
1259
1260
1261 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1262                                       int button)
1263 {
1264     x = x - drawTextXOffset;
1265     y = y + insetAscent;
1266     Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1267
1268     if (inset) {
1269         if (x < 0)
1270             x = insetWidth;
1271         if (y < 0)
1272             y = insetDescent;
1273         inset_x = cx(bv) - top_x + drawTextXOffset;
1274         inset_y = cy(bv) + drawTextYOffset;
1275         inset->Edit(bv, x - inset_x, y - inset_y, button);
1276         if (!the_locking_inset)
1277             return false;
1278         UpdateLocal(bv, CURSOR_PAR, false);
1279         return true;
1280     }
1281     return false;
1282 }
1283
1284
1285 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1286 {
1287     int w = UpdatableInset::getMaxWidth(pain, inset);
1288     if (w < 0) {
1289         return w;
1290     }
1291     if (owner()) {
1292         w = w - top_x + owner()->x();
1293         return w;
1294     }
1295     w -= (2 * TEXT_TO_INSET_OFFSET);
1296     return w - top_x;
1297 //    return  w - (2*TEXT_TO_INSET_OFFSET);
1298 }
1299
1300
1301 void InsetText::SetParagraphData(LyXParagraph *p)
1302 {
1303     LyXParagraph * np;
1304
1305     if (par) {
1306         np = par->next;
1307         delete par;
1308         while(np) {
1309             par = np;
1310             np = np->next;
1311             delete par;
1312         }
1313     }
1314     par = p->Clone();
1315     par->SetInsetOwner(this);
1316     np = par;
1317     while(p->next) {
1318         p = p->next;
1319         np->next = p->Clone();
1320         np->next->previous = np;
1321         np = np->next;
1322         np->SetInsetOwner(this);
1323     }
1324     need_update = INIT;
1325 }
1326
1327
1328 void InsetText::SetAutoBreakRows(bool flag)
1329 {
1330     if (flag != autoBreakRows) {
1331         autoBreakRows = flag;
1332         need_update = FULL;
1333         if (!flag)
1334             removeNewlines();
1335     }
1336 }
1337
1338
1339 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1340 {
1341     if (how != drawFrame) {
1342         drawFrame = how;
1343         if (bv)
1344             UpdateLocal(bv, DRAW_FRAME, false);
1345     }
1346 }
1347
1348
1349 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1350 {
1351     if (frame_color != col) {
1352         frame_color = col;
1353         if (bv)
1354             UpdateLocal(bv, DRAW_FRAME, false);
1355     }
1356 }
1357
1358 #if 0
1359 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1360 {
1361     return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1362 }
1363 #endif
1364
1365
1366 int InsetText::cx(BufferView * bv) const
1367 {
1368     return TEXT(bv)->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1369 }
1370
1371
1372 int InsetText::cy(BufferView * bv) const
1373 {
1374     LyXFont font;
1375     return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1376 }
1377
1378
1379 int InsetText::cpos(BufferView * bv) const
1380 {
1381     return TEXT(bv)->cursor.pos();
1382 }
1383
1384
1385 LyXParagraph * InsetText::cpar(BufferView * bv) const
1386 {
1387     return TEXT(bv)->cursor.par();
1388 }
1389
1390
1391 Row * InsetText::crow(BufferView * bv) const
1392 {
1393     return TEXT(bv)->cursor.row();
1394 }
1395
1396
1397 LyXText * InsetText::getLyXText(BufferView * bv) const
1398 {
1399     if (cache.find(bv) != cache.end())
1400         return cache[bv];
1401     LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1402     lt->init(bv);
1403     cache[bv] = lt;
1404     if (the_locking_inset) {
1405         lt->SetCursor(bv, inset_par, inset_pos);
1406     }
1407     return lt;
1408 }
1409
1410
1411 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1412 {
1413     if (cache.find(bv) == cache.end())
1414         return;
1415     delete cache[bv];
1416     cache.erase(bv);
1417     if (recursive) {
1418         /// then remove all LyXText in text-insets
1419         LyXParagraph * p = par;
1420         for(;p;p = p->next) {
1421             p->deleteInsetsLyXText(bv);
1422         }
1423     }
1424 }
1425
1426
1427 void InsetText::resizeLyXText(BufferView * bv) const
1428 {
1429     if (!par->next && !par->size()) // resize not neccessary!
1430         return;
1431     if (cache.find(bv) == cache.end())
1432         return;
1433
1434     LyXParagraph * lpar = 0;
1435     LyXParagraph * selstartpar = 0;
1436     LyXParagraph * selendpar = 0;
1437     int pos = 0;
1438     int selstartpos = 0;
1439     int selendpos = 0;
1440     int selection = 0;
1441     int mark_set = 0;
1442
1443 //    ProhibitInput(bv);
1444
1445     if (locked) {
1446         lpar = TEXT(bv)->cursor.par();
1447         pos = TEXT(bv)->cursor.pos();
1448         selstartpar = TEXT(bv)->sel_start_cursor.par();
1449         selstartpos = TEXT(bv)->sel_start_cursor.pos();
1450         selendpar = TEXT(bv)->sel_end_cursor.par();
1451         selendpos = TEXT(bv)->sel_end_cursor.pos();
1452         selection = TEXT(bv)->selection;
1453         mark_set = TEXT(bv)->mark_set;
1454     }
1455     deleteLyXText(bv, (the_locking_inset == 0));
1456
1457     if (lpar) {
1458         TEXT(bv)->selection = true;
1459         /* at this point just to avoid the Delete-Empty-Paragraph
1460          * Mechanism when setting the cursor */
1461         TEXT(bv)->mark_set = mark_set;
1462         if (selection) {
1463             TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1464             TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1465             TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1466             TEXT(bv)->SetSelection();
1467             TEXT(bv)->SetCursor(bv, lpar, pos);
1468         } else {
1469             TEXT(bv)->SetCursor(bv, lpar, pos);
1470             TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1471             TEXT(bv)->selection = false;
1472         }
1473     }
1474     if (bv->screen())
1475             TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1476     // this will scroll the screen such that the cursor becomes visible 
1477     bv->updateScrollbar();
1478 //    AllowInput(bv);
1479     if (the_locking_inset) {
1480         /// then resize all LyXText in text-insets
1481         inset_x = cx(bv) - top_x + drawTextXOffset;
1482         inset_y = cy(bv) + drawTextYOffset;
1483         for(LyXParagraph * p = par; p; p = p->next) {
1484             p->resizeInsetsLyXText(bv);
1485         }
1486     }
1487     need_update = FULL;
1488 }
1489
1490
1491 void InsetText::removeNewlines()
1492 {
1493     for(LyXParagraph * p = par; p; p = p->next) {
1494         for(int i = 0; i < p->Last(); ++i) {
1495             if (p->GetChar(i) == LyXParagraph::META_NEWLINE)
1496                 p->Erase(i);
1497         }
1498     }
1499 }