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