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