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