]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
some work on Floats and some cleanup in WorkArea, some changes to compile more easily...
[lyx.git] / src / insets / insettext.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ======================================================
4  * 
5  *           LyX, The Document Processor
6  *
7  *           Copyright 1998-2000 The LyX Team.
8  *
9  * ======================================================
10  */
11
12 #include <config.h>
13
14 #include <fstream>
15 #include <algorithm>
16
17 #include <cstdlib>
18
19 #ifdef __GNUG__
20 #pragma implementation
21 #endif
22
23 #include "insettext.h"
24 #include "lyxparagraph.h"
25 #include "lyxlex.h"
26 #include "debug.h"
27 #include "lyxfont.h"
28 #include "commandtags.h"
29 #include "buffer.h"
30 #include "LyXView.h"
31 #include "BufferView.h"
32 #include "layout.h"
33 #include "LaTeXFeatures.h"
34 #include "Painter.h"
35 #include "lyx_gui_misc.h"
36 #include "lyxtext.h"
37 #include "lyxcursor.h"
38 #include "CutAndPaste.h"
39 #include "font.h"
40 #include "minibuffer.h"
41 #include "LColor.h"
42 #include "support/textutils.h"
43 #include "support/LAssert.h"
44 #include "lyxrow.h"
45 #include "lyxrc.h"
46 #include "intl.h"
47 #include "trans_mgr.h"
48 #include "lyxscreen.h"
49 #include "WorkArea.h"
50
51 using std::ostream;
52 using std::ifstream;
53 using std::endl;
54 using std::min;
55 using std::max;
56
57 extern unsigned char getCurrentTextClass(Buffer *);
58
59 InsetText::InsetText()
60 {
61     par = new LyXParagraph();
62     init();
63 }
64
65
66 InsetText::InsetText(InsetText const & ins)
67         : UpdatableInset()
68 {
69     par = 0;
70     init(&ins);
71     autoBreakRows = ins.autoBreakRows;
72 }
73
74
75 InsetText & InsetText::operator=(InsetText const & it)
76 {
77     init(&it);
78     autoBreakRows = it.autoBreakRows;
79     return * this;
80 }
81
82
83 void InsetText::init(InsetText const * ins)
84 {
85     top_y = 0;
86     last_width = 0;
87     last_height = 0;
88     insetAscent = 0;
89     insetDescent = 0;
90     insetWidth = 0;
91     the_locking_inset = 0;
92     cursor_visible = false;
93     interline_space = 1;
94     no_selection = false;
95     need_update = INIT;
96     drawTextXOffset = 0;
97     drawTextYOffset = 0;
98     autoBreakRows = false;
99     drawFrame = NEVER;
100     xpos = 0.0;
101     if (ins) {
102         SetParagraphData(ins->par);
103         autoBreakRows = ins->autoBreakRows;
104         drawFrame = ins->drawFrame;
105     }
106     par->SetInsetOwner(this);
107     frame_color = LColor::insetframe;
108     locked = false;
109     old_par = 0;
110 }
111
112
113 InsetText::~InsetText()
114 {
115     // delete all instances of LyXText before deleting the paragraps used
116     // by it.
117     for (Cache::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 #if 0
803                 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
804 #else
805                 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
806 #endif
807             }
808         }
809         UpdateLocal(bv, CURSOR_PAR, true);
810         result=DISPATCHED_NOUPDATE;
811         break;
812         // --- Cursor Movements ---------------------------------------------
813     case LFUN_RIGHTSEL:
814         bv->text->FinishUndo();
815         moveRight(bv, false, true);
816         TEXT(bv)->SetSelection();
817         UpdateLocal(bv, SELECTION, false);
818         break;
819     case LFUN_RIGHT:
820         result = moveRight(bv);
821         bv->text->FinishUndo();
822         TEXT(bv)->ClearSelection();
823         UpdateLocal(bv, CURSOR, false);
824         break;
825     case LFUN_LEFTSEL:
826         bv->text->FinishUndo();
827         moveLeft(bv, false, true);
828         TEXT(bv)->SetSelection();
829         UpdateLocal(bv, SELECTION, false);
830         break;
831     case LFUN_LEFT:
832         bv->text->FinishUndo();
833         result= moveLeft(bv);
834         TEXT(bv)->ClearSelection();
835         TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
836         UpdateLocal(bv, CURSOR, false);
837         break;
838     case LFUN_DOWNSEL:
839         bv->text->FinishUndo();
840         moveDown(bv);
841         TEXT(bv)->SetSelection();
842         UpdateLocal(bv, SELECTION, false);
843         break;
844     case LFUN_DOWN:
845         bv->text->FinishUndo();
846         result = moveDown(bv);
847         TEXT(bv)->ClearSelection();
848         TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
849         UpdateLocal(bv, CURSOR, false);
850         break;
851     case LFUN_UPSEL:
852         bv->text->FinishUndo();
853         moveUp(bv);
854         TEXT(bv)->SetSelection();
855         UpdateLocal(bv, SELECTION, false);
856         break;
857     case LFUN_UP:
858         bv->text->FinishUndo();
859         result = moveUp(bv);
860         TEXT(bv)->ClearSelection();
861         TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
862         UpdateLocal(bv, CURSOR, false);
863         break;
864     case LFUN_HOME:
865         bv->text->FinishUndo();
866         TEXT(bv)->CursorHome(bv);
867         TEXT(bv)->ClearSelection();
868         TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
869         UpdateLocal(bv, CURSOR, false);
870         break;
871     case LFUN_END:
872         TEXT(bv)->CursorEnd(bv);
873         TEXT(bv)->ClearSelection();
874         TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
875         UpdateLocal(bv, CURSOR, false);
876         break;
877     case LFUN_BACKSPACE:
878         bv->text->SetUndo(bv->buffer(), Undo::DELETE,
879 #ifndef NEW_INSETS
880           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
881           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
882 #else
883           bv->text->cursor.par()->previous,
884           bv->text->cursor.par()->next
885 #endif
886                 );
887         if (TEXT(bv)->selection)
888             TEXT(bv)->CutSelection(bv);
889         else
890             TEXT(bv)->Backspace(bv);
891         UpdateLocal(bv, CURSOR_PAR, true);
892         break;
893     case LFUN_DELETE:
894         bv->text->SetUndo(bv->buffer(), Undo::DELETE,
895 #ifndef NEW_INSETS
896           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
897           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
898 #else
899           bv->text->cursor.par()->previous,
900           bv->text->cursor.par()->next
901 #endif
902                 );
903         if (TEXT(bv)->selection)
904             TEXT(bv)->CutSelection(bv);
905         else
906             TEXT(bv)->Delete(bv);
907         UpdateLocal(bv, CURSOR_PAR, true);
908         break;
909     case LFUN_CUT:
910         bv->text->SetUndo(bv->buffer(), Undo::DELETE,
911 #ifndef NEW_INSETS
912           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
913           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
914 #else
915           bv->text->cursor.par()->previous,
916           bv->text->cursor.par()->next
917 #endif
918                 );
919         TEXT(bv)->CutSelection(bv);
920         UpdateLocal(bv, CURSOR_PAR, true);
921         break;
922     case LFUN_COPY:
923         bv->text->FinishUndo();
924         TEXT(bv)->CopySelection(bv);
925         UpdateLocal(bv, CURSOR_PAR, false);
926         break;
927     case LFUN_PASTESELECTION:
928     {
929         string clip(bv->workarea()->getClipboard());
930         
931         if (clip.empty())
932             break;
933         if (arg == "paragraph") {
934                 TEXT(bv)->InsertStringB(bv, clip);
935         } else {
936                 TEXT(bv)->InsertStringA(bv, clip);
937         }
938         UpdateLocal(bv, CURSOR_PAR, true);
939         break;
940     }
941     case LFUN_PASTE:
942         if (!autoBreakRows) {
943             CutAndPaste cap;
944
945             if (cap.nrOfParagraphs() > 1) {
946                 WriteAlert(_("Impossible operation"),
947                            _("Cannot include more than one paragraph!"),
948                            _("Sorry."));
949                 break;
950             }
951         }
952         bv->text->SetUndo(bv->buffer(), Undo::INSERT,
953 #ifndef NEW_INSETS
954           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
955           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
956 #else
957           bv->text->cursor.par()->previous,
958           bv->text->cursor.par()->next
959 #endif
960                 );
961         TEXT(bv)->PasteSelection(bv);
962         UpdateLocal(bv, CURSOR_PAR, true);
963         break;
964     case LFUN_BREAKPARAGRAPH:
965         if (!autoBreakRows)
966             return DISPATCHED;
967         TEXT(bv)->BreakParagraph(bv, 0);
968         UpdateLocal(bv, FULL, true);
969         break;
970     case LFUN_BREAKLINE:
971         if (!autoBreakRows)
972             return DISPATCHED;
973         bv->text->SetUndo(bv->buffer(), Undo::INSERT,
974 #ifndef NEW_INSETS
975             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
976             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
977 #else
978             bv->text->cursor.par()->previous,
979             bv->text->cursor.par()->next
980 #endif
981                 );
982         TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
983         UpdateLocal(bv, CURSOR_PAR, true);
984         break;
985     case LFUN_LAYOUT:
986         // do not set layouts on non breakable textinsets
987         if (autoBreakRows) {
988             LyXTextClass::size_type cur_layout = cpar(bv)->layout;
989       
990             // Derive layout number from given argument (string)
991             // and current buffer's textclass (number). */    
992             LyXTextClassList::ClassList::size_type tclass =
993                 bv->buffer()->params.textclass;
994             std::pair <bool, LyXTextClass::size_type> layout = 
995                 textclasslist.NumberOfLayout(tclass, arg);
996
997             // If the entry is obsolete, use the new one instead.
998             if (layout.first) {
999                 string obs = textclasslist.Style(tclass,layout.second).
1000                     obsoleted_by();
1001                 if (!obs.empty()) 
1002                     layout = textclasslist.NumberOfLayout(tclass, obs);
1003             }
1004
1005             // see if we found the layout number:
1006             if (!layout.first) {
1007                 string msg = string(N_("Layout ")) + arg + N_(" not known");
1008
1009                 bv->owner()->getMiniBuffer()->Set(msg);
1010                 break;
1011             }
1012
1013             if (cur_layout != layout.second) {
1014                 cur_layout = layout.second;
1015                 TEXT(bv)->SetLayout(bv, layout.second);
1016                 bv->owner()->setLayout(cpar(bv)->GetLayout());
1017                 UpdateLocal(bv, CURSOR_PAR, true);
1018             }
1019         } else {
1020             // reset the layout box
1021             bv->owner()->setLayout(cpar(bv)->GetLayout());
1022         }
1023         break;
1024     case LFUN_PARAGRAPH_SPACING:
1025             // This one is absolutely not working. When fiddling with this
1026             // it also seems to me that the paragraphs inside the insettext
1027             // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1028     {
1029             LyXParagraph * par = TEXT(bv)->cursor.par();
1030             Spacing::Space cur_spacing = par->spacing.getSpace();
1031             float cur_value = 1.0;
1032             if (cur_spacing == Spacing::Other) {
1033                     cur_value = par->spacing.getValue();
1034             }
1035                         
1036             std::istringstream istr(arg.c_str());
1037             string tmp;
1038             istr >> tmp;
1039             Spacing::Space new_spacing = cur_spacing;
1040             float new_value = cur_value;
1041             if (tmp.empty()) {
1042                     lyxerr << "Missing argument to `paragraph-spacing'"
1043                            << endl;
1044             } else if (tmp == "single") {
1045                     new_spacing = Spacing::Single;
1046             } else if (tmp == "onehalf") {
1047                     new_spacing = Spacing::Onehalf;
1048             } else if (tmp == "double") {
1049                     new_spacing = Spacing::Double;
1050             } else if (tmp == "other") {
1051                     new_spacing = Spacing::Other;
1052                     float tmpval = 0.0;
1053                     istr >> tmpval;
1054                     lyxerr << "new_value = " << tmpval << endl;
1055                     if (tmpval != 0.0)
1056                             new_value = tmpval;
1057             } else if (tmp == "default") {
1058                     new_spacing = Spacing::Default;
1059             } else {
1060                     lyxerr << _("Unknown spacing argument: ")
1061                            << arg << endl;
1062             }
1063             if (cur_spacing != new_spacing || cur_value != new_value) {
1064                     par->spacing.set(new_spacing, new_value);
1065                     //TEXT(bv)->RedoParagraph(owner->view());
1066                     UpdateLocal(bv, CURSOR_PAR, true);
1067                     //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1068             }
1069     }
1070     break;
1071         
1072     default:
1073         result = UNDISPATCHED;
1074         break;
1075     }
1076     if (result != FINISHED) {
1077         ShowInsetCursor(bv);
1078     } else
1079         bv->unlockInset(this);
1080     return result;
1081 }
1082
1083
1084 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1085 {
1086     TexRow texrow;
1087     buf->latexParagraphs(os, par, 0, texrow);
1088     return texrow.rows();
1089 }
1090
1091
1092 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1093 {
1094     LyXParagraph * p = par;
1095     unsigned int lines = 0;
1096     
1097     string tmp;
1098     while (p) {
1099         tmp = buf->asciiParagraph(p, linelen);
1100         lines += countChar(tmp, '\n');
1101         os << tmp;
1102         p = p->next;
1103     }
1104     return lines;
1105 }
1106
1107
1108 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1109 {
1110     LyXParagraph * p = par;
1111     unsigned int lines = 0;
1112     int desc=0;
1113     
1114     string tmp;
1115     while (p) {
1116         buf->SimpleDocBookOnePar(os,tmp,p,desc,0);
1117         p = p->next;
1118     }
1119     
1120     return lines;
1121 }
1122
1123
1124 void InsetText::Validate(LaTeXFeatures & features) const
1125 {
1126     LyXParagraph * p = par;
1127     while(p) {
1128         p->validate(features);
1129         p = p->next;
1130     }
1131 }
1132
1133
1134 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1135 {
1136     if (textclasslist.Style(buf->params.textclass,
1137                             p->GetLayout()).labeltype != LABEL_MANUAL)
1138         return 0;
1139     else
1140         return p->BeginningOfMainBody();
1141 }
1142
1143
1144 void InsetText::GetCursorPos(BufferView * bv,
1145                              int & x, int & y) const
1146 {
1147     x = cx(bv);
1148     y = cy(bv);
1149 }
1150
1151
1152 unsigned int InsetText::InsetInInsetY()
1153 {
1154     if (!the_locking_inset)
1155         return 0;
1156
1157     return (inset_y + the_locking_inset->InsetInInsetY());
1158 }
1159
1160
1161 void InsetText::ToggleInsetCursor(BufferView * bv)
1162 {
1163     if (the_locking_inset) {
1164         the_locking_inset->ToggleInsetCursor(bv);
1165         return;
1166     }
1167
1168     LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1169
1170     int asc = lyxfont::maxAscent(font);
1171     int desc = lyxfont::maxDescent(font);
1172   
1173     if (cursor_visible)
1174         bv->hideLockedInsetCursor();
1175     else
1176         bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1177     cursor_visible = !cursor_visible;
1178 }
1179
1180
1181 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1182 {
1183     if (the_locking_inset) {
1184         the_locking_inset->ShowInsetCursor(bv);
1185         return;
1186     }
1187     if (!cursor_visible) {
1188         LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1189         
1190         int asc = lyxfont::maxAscent(font);
1191         int desc = lyxfont::maxDescent(font);
1192
1193         bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1194         if (show)
1195             bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1196         cursor_visible = true;
1197     }
1198 }
1199
1200
1201 void InsetText::HideInsetCursor(BufferView * bv)
1202 {
1203     if (cursor_visible) {
1204         bv->hideLockedInsetCursor();
1205         cursor_visible = false;
1206     }
1207     if (the_locking_inset)
1208         the_locking_inset->HideInsetCursor(bv);
1209 }
1210
1211
1212 UpdatableInset::RESULT
1213 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1214 {
1215     if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1216         return FINISHED;
1217     if (activate_inset && checkAndActivateInset(bv, false))
1218         return DISPATCHED;
1219     TEXT(bv)->CursorRight(bv, selecting);
1220     return DISPATCHED_NOUPDATE;
1221 }
1222
1223
1224 UpdatableInset::RESULT
1225 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1226 {
1227     if (!cpar(bv)->previous && (cpos(bv) <= 0))
1228         return FINISHED;
1229     TEXT(bv)->CursorLeft(bv, selecting);
1230     if (activate_inset && checkAndActivateInset(bv, true))
1231         return DISPATCHED;
1232     return DISPATCHED_NOUPDATE;
1233 }
1234
1235
1236 UpdatableInset::RESULT
1237 InsetText::moveUp(BufferView * bv)
1238 {
1239     if (!crow(bv)->previous())
1240         return FINISHED;
1241     TEXT(bv)->CursorUp(bv);
1242     return DISPATCHED_NOUPDATE;
1243 }
1244
1245
1246 UpdatableInset::RESULT
1247 InsetText::moveDown(BufferView * bv)
1248 {
1249     if (!crow(bv)->next())
1250         return FINISHED;
1251     TEXT(bv)->CursorDown(bv);
1252     return DISPATCHED_NOUPDATE;
1253 }
1254
1255
1256 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1257 {
1258     if (the_locking_inset) {
1259         if (the_locking_inset->InsertInsetAllowed(inset))
1260             return the_locking_inset->InsertInset(bv, inset);
1261         return false;
1262     }
1263     bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1264 #ifndef NEW_INSETS
1265               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1266               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1267 #else
1268               bv->text->cursor.par()->previous,
1269               bv->text->cursor.par()->next
1270 #endif
1271             );
1272     inset->setOwner(this);
1273     HideInsetCursor(bv);
1274     TEXT(bv)->InsertInset(bv, inset);
1275     if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1276         (cpar(bv)->GetInset(cpos(bv)) != inset))
1277         TEXT(bv)->CursorLeft(bv);
1278     TEXT(bv)->selection = 0;
1279     bv->fitCursor(TEXT(bv));
1280     UpdateLocal(bv, CURSOR_PAR, true);
1281     ShowInsetCursor(bv);
1282     return true;
1283 }
1284
1285
1286 UpdatableInset * InsetText::GetLockingInset()
1287 {
1288     return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1289 }
1290
1291
1292 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1293 {
1294     if (c == LyxCode())
1295         return this;
1296     if (the_locking_inset)
1297         return the_locking_inset->GetFirstLockingInsetOfType(c);
1298     return 0;
1299 }
1300
1301
1302 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1303 {
1304     bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1305 #ifndef NEW_INSETS
1306               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1307               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1308 #else
1309               bv->text->cursor.par()->previous,
1310               bv->text->cursor.par()->next
1311 #endif
1312             );
1313     TEXT(bv)->SetFont(bv, font, toggleall);
1314     bv->fitCursor(TEXT(bv));
1315     UpdateLocal(bv, CURSOR_PAR, true);
1316 }
1317
1318
1319 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1320 {
1321     if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1322         unsigned int x;
1323         unsigned int y;
1324         Inset * inset =
1325             static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1326         if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1327             return false;
1328         LyXFont const font =
1329                 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1330         if (behind) {
1331             x = inset->width(bv, font);
1332             y = inset->descent(bv, font);
1333         } else {
1334             x = y = 0;
1335         }
1336         inset_x = cx(bv) - top_x + drawTextXOffset;
1337         inset_y = cy(bv) + drawTextYOffset;
1338         inset->Edit(bv, x - inset_x, y - inset_y, 0);
1339         if (!the_locking_inset)
1340             return false;
1341         UpdateLocal(bv, CURSOR_PAR, false);
1342         return true;
1343     }
1344     return false;
1345 }
1346
1347
1348 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1349                                       int button)
1350 {
1351     x = x - drawTextXOffset;
1352     y = y + insetAscent;
1353     Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1354
1355     if (inset) {
1356         if (x < 0)
1357             x = insetWidth;
1358         if (y < 0)
1359             y = insetDescent;
1360         inset_x = cx(bv) - top_x + drawTextXOffset;
1361         inset_y = cy(bv) + drawTextYOffset;
1362         inset->Edit(bv, x - inset_x, y - inset_y, button);
1363         if (!the_locking_inset)
1364             return false;
1365         UpdateLocal(bv, CURSOR_PAR, false);
1366         return true;
1367     }
1368     return false;
1369 }
1370
1371
1372 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1373 {
1374     int w = UpdatableInset::getMaxWidth(pain, inset);
1375     if (w < 0) {
1376         return w;
1377     }
1378     if (owner()) {
1379         w = w - top_x + owner()->x();
1380         return w;
1381     }
1382     w -= (2 * TEXT_TO_INSET_OFFSET);
1383     return w - top_x;
1384 //    return  w - (2*TEXT_TO_INSET_OFFSET);
1385 }
1386
1387
1388 void InsetText::SetParagraphData(LyXParagraph *p)
1389 {
1390     // delete all instances of LyXText before deleting the paragraps used
1391     // by it.
1392     for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1393         delete (*cit).second;
1394         (*cit).second = 0;
1395     }
1396
1397     LyXParagraph * np;
1398     if (par) {
1399         np = par->next;
1400         delete par;
1401         while(np) {
1402             par = np;
1403             np = np->next;
1404             delete par;
1405         }
1406     }
1407     par = p->Clone();
1408     par->SetInsetOwner(this);
1409     np = par;
1410     while(p->next) {
1411         p = p->next;
1412         np->next = p->Clone();
1413         np->next->previous = np;
1414         np = np->next;
1415         np->SetInsetOwner(this);
1416     }
1417     need_update = INIT;
1418 }
1419
1420
1421 void InsetText::SetAutoBreakRows(bool flag)
1422 {
1423     if (flag != autoBreakRows) {
1424         autoBreakRows = flag;
1425         need_update = FULL;
1426         if (!flag)
1427             removeNewlines();
1428     }
1429 }
1430
1431
1432 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1433 {
1434     if (how != drawFrame) {
1435         drawFrame = how;
1436         if (bv)
1437             UpdateLocal(bv, DRAW_FRAME, false);
1438     }
1439 }
1440
1441
1442 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1443 {
1444     if (frame_color != col) {
1445         frame_color = col;
1446         if (bv)
1447             UpdateLocal(bv, DRAW_FRAME, false);
1448     }
1449 }
1450
1451 #if 0
1452 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1453 {
1454     return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1455 }
1456 #endif
1457
1458
1459 int InsetText::cx(BufferView * bv) const
1460 {
1461         LyXText * text = TEXT(bv);
1462         int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1463         if (the_locking_inset) {
1464                 LyXFont font = text->GetFont(bv->buffer(),
1465                                      text->cursor.par(), text->cursor.pos());
1466                 if (font.isVisibleRightToLeft())
1467                         x -= the_locking_inset->width(bv, font);
1468         }
1469         return x;
1470 }
1471
1472
1473 int InsetText::cy(BufferView * bv) const
1474 {
1475     LyXFont font;
1476     return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1477 }
1478
1479
1480 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1481 {
1482     return TEXT(bv)->cursor.pos();
1483 }
1484
1485
1486 LyXParagraph * InsetText::cpar(BufferView * bv) const
1487 {
1488     return TEXT(bv)->cursor.par();
1489 }
1490
1491 bool InsetText::cboundary(BufferView * bv) const
1492 {
1493     return TEXT(bv)->cursor.boundary();
1494 }
1495
1496
1497 Row * InsetText::crow(BufferView * bv) const
1498 {
1499     return TEXT(bv)->cursor.row();
1500 }
1501
1502
1503 LyXText * InsetText::getLyXText(BufferView const * lbv) const
1504 {
1505     // Super UGLY! (Lgb)
1506     BufferView * bv = const_cast<BufferView *>(lbv);
1507         
1508     if ((cache.find(bv) != cache.end()) && cache[bv])
1509         return cache[bv];
1510     LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1511     lt->init(bv);
1512     cache[bv] = lt;
1513     if (the_locking_inset) {
1514        lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1515     }
1516     return lt;
1517 }
1518
1519
1520 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1521 {
1522     if ((cache.find(bv) == cache.end()) || !cache[bv])
1523         return;
1524     delete cache[bv];
1525     cache.erase(bv);
1526     if (recursive) {
1527         /// then remove all LyXText in text-insets
1528         LyXParagraph * p = par;
1529         for (;p;p = p->next) {
1530             p->deleteInsetsLyXText(bv);
1531         }
1532     }
1533 }
1534
1535
1536 void InsetText::resizeLyXText(BufferView * bv) const
1537 {
1538     if (!par->next && !par->size()) // resize not neccessary!
1539         return;
1540     if ((cache.find(bv) == cache.end()) || !cache[bv])
1541         return;
1542
1543     LyXParagraph * lpar = 0;
1544     LyXParagraph * selstartpar = 0;
1545     LyXParagraph * selendpar = 0;
1546     LyXParagraph::size_type pos = 0;
1547     LyXParagraph::size_type selstartpos = 0;
1548     LyXParagraph::size_type selendpos = 0;
1549     bool boundary = false;
1550     bool selstartboundary = false;
1551     bool selendboundary = false;
1552     int selection = 0;
1553     int mark_set = 0;
1554
1555 //    ProhibitInput(bv);
1556
1557     if (locked) {
1558         lpar = TEXT(bv)->cursor.par();
1559         pos = TEXT(bv)->cursor.pos();
1560         boundary = TEXT(bv)->cursor.boundary();
1561         selstartpar = TEXT(bv)->sel_start_cursor.par();
1562         selstartpos = TEXT(bv)->sel_start_cursor.pos();
1563         selstartboundary = TEXT(bv)->sel_start_cursor.boundary();
1564         selendpar = TEXT(bv)->sel_end_cursor.par();
1565         selendpos = TEXT(bv)->sel_end_cursor.pos();
1566         selendboundary = TEXT(bv)->sel_end_cursor.boundary();
1567         selection = TEXT(bv)->selection;
1568         mark_set = TEXT(bv)->mark_set;
1569     }
1570     deleteLyXText(bv, (the_locking_inset == 0));
1571
1572     if (lpar) {
1573         TEXT(bv)->selection = true;
1574         /* at this point just to avoid the Delete-Empty-Paragraph
1575          * Mechanism when setting the cursor */
1576         TEXT(bv)->mark_set = mark_set;
1577         if (selection) {
1578             TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,true, 
1579                                 selstartboundary);
1580             TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1581             TEXT(bv)->SetCursor(bv, selendpar, selendpos, true, selendboundary);
1582             TEXT(bv)->SetSelection();
1583             TEXT(bv)->SetCursor(bv, lpar, pos);
1584         } else {
1585             TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1586             TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1587             TEXT(bv)->selection = false;
1588         }
1589     }
1590     if (bv->screen())
1591             TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1592     // this will scroll the screen such that the cursor becomes visible 
1593     bv->updateScrollbar();
1594 //    AllowInput(bv);
1595     if (the_locking_inset) {
1596         /// then resize all LyXText in text-insets
1597         inset_x = cx(bv) - top_x + drawTextXOffset;
1598         inset_y = cy(bv) + drawTextYOffset;
1599         for (LyXParagraph * p = par; p; p = p->next) {
1600             p->resizeInsetsLyXText(bv);
1601         }
1602     }
1603     need_update = FULL;
1604 }
1605
1606
1607 void InsetText::removeNewlines()
1608 {
1609     for (LyXParagraph * p = par; p; p = p->next) {
1610         for (int i = 0; i < p->Last(); ++i) {
1611             if (p->GetChar(i) == LyXParagraph::META_NEWLINE)
1612                 p->Erase(i);
1613         }
1614     }
1615 }