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