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