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