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