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