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