]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
ParagraphParameters and SharedContainer
[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) { // (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 if (!locked) {
363             if (need_update & CURSOR) {
364                 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
365                 TEXT(bv)->ClearSelection(bv);
366                 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
367             }
368             bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
369         } else {
370             locked = false;
371             if (need_update & SELECTION)
372                 bv->screen()->ToggleToggle(TEXT(bv), bv, y_offset, int(x));
373             else if (need_update & CURSOR) {
374                 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
375                 TEXT(bv)->ClearSelection(bv);
376                 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
377             }
378             bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
379             locked = true;
380         }
381     }
382     TEXT(bv)->refresh_y = 0;
383     TEXT(bv)->status = LyXText::UNCHANGED;
384     if ((need_update != CURSOR_PAR) &&
385         ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)))
386     {
387             pain.rectangle(top_x + 1, baseline - insetAscent + 1,
388                            width(bv, f) - 1, insetAscent + insetDescent - 1,
389                            frame_color);
390     } else if (need_update & CLEAR_FRAME) {
391             pain.rectangle(top_x + 1, baseline - insetAscent + 1,
392                            width(bv, f) - 1, insetAscent + insetDescent - 1,
393                            LColor::background);
394     }
395     x += width(bv, f) - TEXT_TO_INSET_OFFSET;
396     if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
397         need_update = INIT;
398     } else if (need_update != INIT)
399         need_update = NONE;
400 }
401
402
403 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
404 {
405     if (reinit) {  // && (need_update != CURSOR)) {
406         need_update = INIT;
407         resizeLyXText(bv);
408         if (owner())
409             owner()->update(bv, font, true);
410         return;
411     }
412     if (the_locking_inset) {
413         inset_x = cx(bv) - top_x + drawTextXOffset;
414         inset_y = cy(bv) + drawTextYOffset;
415         the_locking_inset->update(bv, font, reinit);
416     }
417     if (need_update == INIT) {
418         resizeLyXText(bv);
419         need_update = FULL;
420     }
421     int oldw = insetWidth;
422 #if 1
423     insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
424     // max(textWidth(bv->painter()),
425     // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
426     // (2 * TEXT_TO_INSET_OFFSET);
427 #else
428     insetWidth = textWidth(bv->painter());
429     if (insetWidth < 0)
430             insetWidth = static_cast<int>(TEXT(bv)->width);
431 #endif
432     if (oldw != insetWidth) {
433 //          printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
434 //                 textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
435         resizeLyXText(bv);
436         need_update = FULL;
437         update(bv, font, reinit);
438         return;
439     }
440     if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
441         the_locking_inset)
442     {
443         TEXT(bv)->UpdateInset(bv, the_locking_inset);
444     }
445
446     if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
447         need_update = FULL;
448
449     int y_temp = 0;
450     Row * row = TEXT(bv)->GetRowNearY(y_temp);
451     insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
452     insetDescent = TEXT(bv)->height - row->ascent_of_text() +
453         TEXT_TO_INSET_OFFSET;
454 }
455
456 void InsetText::SetUpdateStatus(BufferView * bv, int what)
457 {
458     need_update |= what;
459     if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
460         need_update |= FULL;
461     else if (TEXT(bv)->status == LyXText::NEED_VERY_LITTLE_REFRESH)
462         need_update |= CURSOR_PAR;
463
464     // this to not draw a selection when we redraw all of it!
465     if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
466         TEXT(bv)->ClearSelection(bv);
467 }
468
469 void InsetText::UpdateLocal(BufferView * bv, int what, bool mark_dirty)
470 {
471     TEXT(bv)->FullRebreak(bv);
472     SetUpdateStatus(bv, what);
473     if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
474         TEXT(bv)->selection)
475             bv->updateInset(this, mark_dirty);
476     bv->owner()->showState();
477     if (old_par != cpar(bv)) {
478             bv->owner()->setLayout(cpar(bv)->GetLayout());
479             old_par = cpar(bv);
480     }
481 }
482
483
484 string const InsetText::EditMessage() const
485 {
486     return _("Opened Text Inset");
487 }
488
489
490 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
491 {
492 //    par->SetInsetOwner(this);
493     UpdatableInset::Edit(bv, x, y, button);
494
495     if (!bv->lockInset(this)) {
496         lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
497         return;
498     }
499     locked = true;
500     the_locking_inset = 0;
501     inset_pos = inset_x = inset_y = 0;
502     inset_boundary = false;
503     inset_par = 0;
504     old_par = 0;
505     if (!checkAndActivateInset(bv, x, y, button))
506         TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
507                                            y+insetAscent);
508     TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
509     bv->text->FinishUndo();
510     ShowInsetCursor(bv);
511     UpdateLocal(bv, FULL, false);
512
513     // If the inset is empty set the language of the current font to the
514     // language to the surronding text.
515     if (par->Last() == 0 && !par->next) {
516         LyXFont font(LyXFont::ALL_IGNORE);
517         font.setLanguage(bv->getParentLanguage(this));
518         SetFont(bv, font, false);
519     }
520 }
521
522
523 void InsetText::InsetUnlock(BufferView * bv)
524 {
525     if (the_locking_inset) {
526         the_locking_inset->InsetUnlock(bv);
527         the_locking_inset = 0;
528     }
529     HideInsetCursor(bv);
530     no_selection = false;
531     locked = false;
532     UpdateLocal(bv, CLEAR_FRAME|CURSOR, 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 #if 0
555         TEXT(bv)->ClearSelection(bv);
556         TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
557         TEXT(bv)->UpdateInset(bv, the_locking_inset);
558 #else
559         UpdateLocal(bv, CURSOR, false);
560 #endif
561         return true;
562     } else if (the_locking_inset && (the_locking_inset == inset)) {
563         if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
564             lyxerr[Debug::INSETS] << "OK" << endl;
565             inset_x = cx(bv) - top_x + drawTextXOffset;
566             inset_y = cy(bv) + drawTextYOffset;
567         } else {
568             lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
569         }
570     } else if (the_locking_inset) {
571         lyxerr[Debug::INSETS] << "MAYBE" << endl;
572         return the_locking_inset->LockInsetInInset(bv, inset);
573     }
574     lyxerr[Debug::INSETS] << "NOT OK" << endl;
575     return false;
576 }
577
578
579 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
580                                    bool lr)
581 {
582     if (!the_locking_inset)
583         return false;
584     if (the_locking_inset == inset) {
585         the_locking_inset->InsetUnlock(bv);
586         TEXT(bv)->UpdateInset(bv, inset);
587         the_locking_inset = 0;
588         if (lr)
589             moveRight(bv, false);
590         old_par = 0; // force layout setting
591         UpdateLocal(bv, CURSOR_PAR, false);
592         return true;
593     }
594     return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
595 }
596
597
598 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
599 {
600     if (!the_locking_inset)
601         return false;
602     if (the_locking_inset != inset) {
603         TEXT(bv)->UpdateInset(bv, the_locking_inset);
604         SetUpdateStatus(bv, CURSOR_PAR);
605         return the_locking_inset->UpdateInsetInInset(bv, inset);
606     }
607 //    UpdateLocal(bv, FULL, false);
608     if (TEXT(bv)->UpdateInset(bv, inset))
609         UpdateLocal(bv, CURSOR_PAR, false);
610     if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
611         inset_x = cx(bv) - top_x + drawTextXOffset;
612         inset_y = cy(bv) + drawTextYOffset;
613     }
614     return true;
615 }
616
617
618 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
619 {
620     no_selection = true;
621
622     int tmp_x = x - drawTextXOffset;
623     int tmp_y = y + insetAscent - TEXT(bv)->first;
624     Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
625
626     HideInsetCursor(bv);
627     if (the_locking_inset) {
628         if (the_locking_inset == inset) {
629             the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
630             no_selection = false;
631             return;
632         } else if (inset) {
633             // otherwise unlock the_locking_inset and lock the new inset
634             the_locking_inset->InsetUnlock(bv);
635             inset_x = cx(bv) - top_x + drawTextXOffset;
636             inset_y = cy(bv) + drawTextYOffset;
637             the_locking_inset = static_cast<UpdatableInset*>(inset);
638             inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
639             inset->Edit(bv, x - inset_x, y - inset_y, button);
640             if (the_locking_inset)
641                 UpdateLocal(bv, CURSOR, false);
642             no_selection = false;
643             return;
644         }
645         // otherwise only unlock the_locking_inset
646         the_locking_inset->InsetUnlock(bv);
647         the_locking_inset = 0;
648     }
649     if (bv->theLockingInset()) {
650         if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
651             UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
652             inset_x = cx(bv) - top_x + drawTextXOffset;
653             inset_y = cy(bv) + drawTextYOffset;
654             inset_pos = cpos(bv);
655             inset_par = cpar(bv);
656             inset_boundary = cboundary(bv);
657             the_locking_inset = uinset;
658             uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
659             uinset->Edit(bv, x - inset_x, y - inset_y, 0);
660             if (the_locking_inset)
661                 UpdateLocal(bv, CURSOR, false);
662             no_selection = false;
663             return;
664         }
665     }
666     if (!inset) {
667         bool paste_internally = false;
668         if ((button == 2) && TEXT(bv)->selection) {
669             LocalDispatch(bv, LFUN_COPY, "");
670             paste_internally = true;
671         }
672         TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
673                                            y + insetAscent);
674         TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
675         UpdateLocal(bv, CURSOR, false);
676         bv->owner()->setLayout(cpar(bv)->GetLayout());
677         old_par = cpar(bv);
678         // Insert primary selection with middle mouse
679         // if there is a local selection in the current buffer,
680         // insert this
681         if (button == 2) {
682             if (paste_internally)
683                 LocalDispatch(bv, LFUN_PASTE, "");
684             else
685                 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
686         }
687     }
688     ShowInsetCursor(bv);
689     no_selection = false;
690 }
691
692
693 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
694 {
695     UpdatableInset * inset = 0;
696
697     if (the_locking_inset) {
698             the_locking_inset->InsetButtonRelease(bv,
699                                                   x - inset_x, y - inset_y,
700                                                   button);
701     } else {
702         if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
703             inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
704             if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
705                 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
706             } else {
707                 inset_x = cx(bv) - top_x + drawTextXOffset;
708                 inset_y = cy(bv) + drawTextYOffset;
709                 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
710                 inset->Edit(bv, x - inset_x, y - inset_y, button);
711             }
712             UpdateLocal(bv, CURSOR_PAR, false);
713         }
714     }
715     no_selection = false;
716 }
717
718
719 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
720 {
721     if (no_selection)
722         return;
723     if (the_locking_inset) {
724         the_locking_inset->InsetMotionNotify(bv, x - inset_x,
725                                              y - inset_y,state);
726         return;
727     }
728     HideInsetCursor(bv);
729     TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
730                                        y+insetAscent);
731     TEXT(bv)->SetSelection(bv);
732     if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
733         TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
734         UpdateLocal(bv, SELECTION, false);
735     ShowInsetCursor(bv);
736 }
737
738
739 void InsetText::InsetKeyPress(XKeyEvent * xke)
740 {
741     if (the_locking_inset) {
742         the_locking_inset->InsetKeyPress(xke);
743         return;
744     }
745 }
746
747
748 UpdatableInset::RESULT
749 InsetText::LocalDispatch(BufferView * bv,
750                          kb_action action, string const & arg)
751 {
752     no_selection = false;
753     UpdatableInset::RESULT
754         result= UpdatableInset::LocalDispatch(bv, action, arg);
755     if (result != UNDISPATCHED) {
756         return DISPATCHED;
757     }
758
759     result=DISPATCHED;
760     if ((action < 0) && arg.empty())
761         return FINISHED;
762
763     if (the_locking_inset) {
764         result = the_locking_inset->LocalDispatch(bv, action, arg);
765         if (result == DISPATCHED_NOUPDATE)
766             return result;
767         else if (result == DISPATCHED) {
768             UpdateLocal(bv, CURSOR_PAR, false);
769             return result;
770         } else if (result == FINISHED) {
771             bool dispatched = false;
772             switch (action) {
773             case LFUN_UNKNOWN_ACTION:
774             case LFUN_BREAKPARAGRAPH:
775             case LFUN_BREAKLINE:
776                moveRightIntern(bv, false, false);
777                break;
778             case LFUN_RIGHT:
779                 if (!TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
780                     moveRightIntern(bv, false, false);
781                 dispatched = true;
782                 break;
783             case LFUN_LEFT:
784                 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
785                     moveRightIntern(bv, false, false);
786                 dispatched = true;
787                 break;
788             default:
789                 break;
790             }
791             the_locking_inset = 0;
792             if (dispatched)
793                 return DISPATCHED;
794         }
795     }
796     HideInsetCursor(bv);
797     switch (action) {
798         // Normal chars
799     case LFUN_UNKNOWN_ACTION:
800         if (bv->buffer()->isReadonly()) {
801 //          setErrorMessage(N_("Document is read only"));
802             break;
803         }
804         if (!arg.empty()) {
805             /* Automatically delete the currently selected
806              * text and replace it with what is being
807              * typed in now. Depends on lyxrc settings
808              * "auto_region_delete", which defaults to
809              * true (on). */
810
811             bv->text->SetUndo(bv->buffer(), Undo::INSERT,
812 #ifndef NEW_INSETS
813                               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
814                               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
815 #else
816                               bv->text->cursor.par()->previous,
817                               bv->text->cursor.par()->next
818 #endif
819                     );
820             bv->setState();
821             if (lyxrc.auto_region_delete) {
822                 if (TEXT(bv)->selection){
823                     TEXT(bv)->CutSelection(bv, false);
824                 }
825             }
826             TEXT(bv)->ClearSelection(bv);
827             for (string::size_type i = 0; i < arg.length(); ++i) {
828                 if (greek_kb_flag) {
829                     if (!math_insert_greek(bv, arg[i])) {
830                         bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
831                     } else if (!the_locking_inset) {
832                         (void)moveRight(bv, false);
833                     }
834                 } else {
835                     bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
836                 }
837             }
838         }
839         TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
840         UpdateLocal(bv, CURSOR_PAR, true);
841         result=DISPATCHED_NOUPDATE;
842         break;
843         // --- Cursor Movements ---------------------------------------------
844     case LFUN_RIGHTSEL:
845         bv->text->FinishUndo();
846         moveRight(bv, false, true);
847         TEXT(bv)->SetSelection(bv);
848         UpdateLocal(bv, SELECTION, false);
849         break;
850     case LFUN_RIGHT:
851         result = moveRight(bv);
852         bv->text->FinishUndo();
853         UpdateLocal(bv, CURSOR, false);
854         break;
855     case LFUN_LEFTSEL:
856         bv->text->FinishUndo();
857         moveLeft(bv, false, true);
858         TEXT(bv)->SetSelection(bv);
859         UpdateLocal(bv, SELECTION, false);
860         break;
861     case LFUN_LEFT:
862         bv->text->FinishUndo();
863         result = moveLeft(bv);
864         UpdateLocal(bv, CURSOR, false);
865         break;
866     case LFUN_DOWNSEL:
867         bv->text->FinishUndo();
868         moveDown(bv);
869         TEXT(bv)->SetSelection(bv);
870         UpdateLocal(bv, SELECTION, false);
871         break;
872     case LFUN_DOWN:
873         bv->text->FinishUndo();
874         result = moveDown(bv);
875         UpdateLocal(bv, CURSOR, false);
876         break;
877     case LFUN_UPSEL:
878         bv->text->FinishUndo();
879         moveUp(bv);
880         TEXT(bv)->SetSelection(bv);
881         UpdateLocal(bv, SELECTION, false);
882         break;
883     case LFUN_UP:
884         bv->text->FinishUndo();
885         result = moveUp(bv);
886         UpdateLocal(bv, CURSOR, false);
887         break;
888     case LFUN_HOME:
889         bv->text->FinishUndo();
890         TEXT(bv)->CursorHome(bv);
891         UpdateLocal(bv, CURSOR, false);
892         break;
893     case LFUN_END:
894         TEXT(bv)->CursorEnd(bv);
895         UpdateLocal(bv, CURSOR, false);
896         break;
897     case LFUN_BACKSPACE:
898         bv->text->SetUndo(bv->buffer(), Undo::DELETE,
899 #ifndef NEW_INSETS
900           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
901           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
902 #else
903           bv->text->cursor.par()->previous,
904           bv->text->cursor.par()->next
905 #endif
906                 );
907         if (TEXT(bv)->selection)
908             TEXT(bv)->CutSelection(bv);
909         else
910             TEXT(bv)->Backspace(bv);
911         UpdateLocal(bv, CURSOR_PAR, true);
912         break;
913     case LFUN_DELETE:
914         bv->text->SetUndo(bv->buffer(), Undo::DELETE,
915 #ifndef NEW_INSETS
916           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
917           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
918 #else
919           bv->text->cursor.par()->previous,
920           bv->text->cursor.par()->next
921 #endif
922                 );
923         if (TEXT(bv)->selection)
924             TEXT(bv)->CutSelection(bv);
925         else
926             TEXT(bv)->Delete(bv);
927         UpdateLocal(bv, CURSOR_PAR, true);
928         break;
929     case LFUN_CUT:
930         bv->text->SetUndo(bv->buffer(), Undo::DELETE,
931 #ifndef NEW_INSETS
932           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
933           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
934 #else
935           bv->text->cursor.par()->previous,
936           bv->text->cursor.par()->next
937 #endif
938                 );
939         TEXT(bv)->CutSelection(bv);
940         UpdateLocal(bv, CURSOR_PAR, true);
941         break;
942     case LFUN_COPY:
943         bv->text->FinishUndo();
944         TEXT(bv)->CopySelection(bv);
945         UpdateLocal(bv, CURSOR_PAR, false);
946         break;
947     case LFUN_PASTESELECTION:
948     {
949         string clip(bv->workarea()->getClipboard());
950         
951         if (clip.empty())
952             break;
953         if (arg == "paragraph") {
954                 TEXT(bv)->InsertStringB(bv, clip);
955         } else {
956                 TEXT(bv)->InsertStringA(bv, clip);
957         }
958         UpdateLocal(bv, CURSOR_PAR, true);
959         break;
960     }
961     case LFUN_PASTE:
962         if (!autoBreakRows) {
963             CutAndPaste cap;
964
965             if (cap.nrOfParagraphs() > 1) {
966                 WriteAlert(_("Impossible operation"),
967                            _("Cannot include more than one paragraph!"),
968                            _("Sorry."));
969                 break;
970             }
971         }
972         bv->text->SetUndo(bv->buffer(), Undo::INSERT,
973 #ifndef NEW_INSETS
974           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
975           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
976 #else
977           bv->text->cursor.par()->previous,
978           bv->text->cursor.par()->next
979 #endif
980                 );
981         TEXT(bv)->PasteSelection(bv);
982         UpdateLocal(bv, CURSOR_PAR, true);
983         break;
984     case LFUN_BREAKPARAGRAPH:
985         if (!autoBreakRows)
986             return DISPATCHED;
987         TEXT(bv)->BreakParagraph(bv, 0);
988         UpdateLocal(bv, FULL, true);
989         break;
990     case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
991         if (!autoBreakRows)
992             return DISPATCHED;
993         TEXT(bv)->BreakParagraph(bv, 1);
994         UpdateLocal(bv, FULL, true);
995         break;
996     case LFUN_BREAKLINE:
997         if (!autoBreakRows)
998             return DISPATCHED;
999         bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1000 #ifndef NEW_INSETS
1001             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1002             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1003 #else
1004             bv->text->cursor.par()->previous,
1005             bv->text->cursor.par()->next
1006 #endif
1007                 );
1008         TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
1009         UpdateLocal(bv, CURSOR_PAR, true);
1010         break;
1011     case LFUN_LAYOUT:
1012         // do not set layouts on non breakable textinsets
1013         if (autoBreakRows) {
1014             LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1015       
1016             // Derive layout number from given argument (string)
1017             // and current buffer's textclass (number). */    
1018             LyXTextClassList::ClassList::size_type tclass =
1019                 bv->buffer()->params.textclass;
1020             std::pair <bool, LyXTextClass::size_type> layout = 
1021                 textclasslist.NumberOfLayout(tclass, arg);
1022
1023             // If the entry is obsolete, use the new one instead.
1024             if (layout.first) {
1025                 string obs = textclasslist.Style(tclass,layout.second).
1026                     obsoleted_by();
1027                 if (!obs.empty()) 
1028                     layout = textclasslist.NumberOfLayout(tclass, obs);
1029             }
1030
1031             // see if we found the layout number:
1032             if (!layout.first) {
1033                 string msg = string(N_("Layout ")) + arg + N_(" not known");
1034
1035                 bv->owner()->getMiniBuffer()->Set(msg);
1036                 break;
1037             }
1038
1039             if (cur_layout != layout.second) {
1040                 cur_layout = layout.second;
1041                 TEXT(bv)->SetLayout(bv, layout.second);
1042                 bv->owner()->setLayout(cpar(bv)->GetLayout());
1043                 UpdateLocal(bv, CURSOR_PAR, true);
1044             }
1045         } else {
1046             // reset the layout box
1047             bv->owner()->setLayout(cpar(bv)->GetLayout());
1048         }
1049         break;
1050     case LFUN_PARAGRAPH_SPACING:
1051             // This one is absolutely not working. When fiddling with this
1052             // it also seems to me that the paragraphs inside the insettext
1053             // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1054     {
1055             LyXParagraph * par = TEXT(bv)->cursor.par();
1056             Spacing::Space cur_spacing = par->params.spacing().getSpace();
1057             float cur_value = 1.0;
1058             if (cur_spacing == Spacing::Other) {
1059                     cur_value = par->params.spacing().getValue();
1060             }
1061                         
1062             std::istringstream istr(arg.c_str());
1063             string tmp;
1064             istr >> tmp;
1065             Spacing::Space new_spacing = cur_spacing;
1066             float new_value = cur_value;
1067             if (tmp.empty()) {
1068                     lyxerr << "Missing argument to `paragraph-spacing'"
1069                            << endl;
1070             } else if (tmp == "single") {
1071                     new_spacing = Spacing::Single;
1072             } else if (tmp == "onehalf") {
1073                     new_spacing = Spacing::Onehalf;
1074             } else if (tmp == "double") {
1075                     new_spacing = Spacing::Double;
1076             } else if (tmp == "other") {
1077                     new_spacing = Spacing::Other;
1078                     float tmpval = 0.0;
1079                     istr >> tmpval;
1080                     lyxerr << "new_value = " << tmpval << endl;
1081                     if (tmpval != 0.0)
1082                             new_value = tmpval;
1083             } else if (tmp == "default") {
1084                     new_spacing = Spacing::Default;
1085             } else {
1086                     lyxerr << _("Unknown spacing argument: ")
1087                            << arg << endl;
1088             }
1089             if (cur_spacing != new_spacing || cur_value != new_value) {
1090                     par->params.spacing(Spacing(new_spacing, new_value));
1091                     //TEXT(bv)->RedoParagraph(owner->view());
1092                     UpdateLocal(bv, CURSOR_PAR, true);
1093                     //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1094             }
1095     }
1096     break;
1097         
1098     default:
1099         if (!bv->Dispatch(action, arg))
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     bv->fitCursor(TEXT(bv));
1342     UpdateLocal(bv, CURSOR_PAR|CURSOR, true);
1343     ShowInsetCursor(bv);
1344     return true;
1345 }
1346
1347
1348 UpdatableInset * InsetText::GetLockingInset()
1349 {
1350     return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1351 }
1352
1353
1354 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1355 {
1356     if (c == LyxCode())
1357         return this;
1358     if (the_locking_inset)
1359         return the_locking_inset->GetFirstLockingInsetOfType(c);
1360     return 0;
1361 }
1362
1363
1364 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1365 {
1366     if (TEXT(bv)->selection) {
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     }
1377     TEXT(bv)->SetFont(bv, font, toggleall);
1378     bv->fitCursor(TEXT(bv));
1379     UpdateLocal(bv, CURSOR_PAR, true);
1380 }
1381
1382
1383 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1384 {
1385     if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1386         unsigned int x;
1387         unsigned int y;
1388         Inset * inset =
1389             static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1390         if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1391             return false;
1392         LyXFont const font =
1393                 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1394         if (behind) {
1395             x = inset->width(bv, font);
1396             y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1397         } else {
1398             x = 0;
1399             y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1400         }
1401         //inset_x = cx(bv) - top_x + drawTextXOffset;
1402         //inset_y = cy(bv) + drawTextYOffset;
1403         inset->Edit(bv, x, y, 0);
1404         if (!the_locking_inset)
1405             return false;
1406         UpdateLocal(bv, CURSOR_PAR, false);
1407         return true;
1408     }
1409     return false;
1410 }
1411
1412
1413 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1414                                       int button)
1415 {
1416     x = x - drawTextXOffset;
1417     y = y + insetAscent;
1418     Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1419
1420     if (inset) {
1421         if (x < 0)
1422             x = insetWidth;
1423         if (y < 0)
1424             y = insetDescent;
1425         inset_x = cx(bv) - top_x + drawTextXOffset;
1426         inset_y = cy(bv) + drawTextYOffset;
1427         inset->Edit(bv, x - inset_x, y - inset_y, button);
1428         if (!the_locking_inset)
1429             return false;
1430         UpdateLocal(bv, CURSOR_PAR, false);
1431         return true;
1432     }
1433     return false;
1434 }
1435
1436
1437 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1438 {
1439     int w = UpdatableInset::getMaxWidth(pain, inset);
1440     if (w < 0) {
1441         return w;
1442     }
1443     if (owner()) {
1444         w = w - top_x + owner()->x();
1445         return w;
1446     }
1447     w -= (2 * TEXT_TO_INSET_OFFSET);
1448     return w - top_x;
1449 //    return  w - (2*TEXT_TO_INSET_OFFSET);
1450 }
1451
1452
1453 void InsetText::SetParagraphData(LyXParagraph *p)
1454 {
1455     // delete all instances of LyXText before deleting the paragraps used
1456     // by it.
1457     for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1458         delete (*cit).second;
1459         (*cit).second = 0;
1460     }
1461
1462     LyXParagraph * np;
1463     if (par) {
1464         np = par->next;
1465         delete par;
1466         while(np) {
1467             par = np;
1468             np = np->next;
1469             delete par;
1470         }
1471     }
1472     par = p->Clone();
1473     par->SetInsetOwner(this);
1474     np = par;
1475     while(p->next) {
1476         p = p->next;
1477         np->next = p->Clone();
1478         np->next->previous = np;
1479         np = np->next;
1480         np->SetInsetOwner(this);
1481     }
1482     need_update = INIT;
1483 }
1484
1485
1486 void InsetText::SetText(string const & data)
1487 {
1488     clear();
1489     LyXFont font(LyXFont::ALL_SANE);
1490     for(unsigned int i=0; i < data.length(); ++i)
1491         par->InsertChar(i, data[i], font);
1492 }
1493
1494
1495 void InsetText::SetAutoBreakRows(bool flag)
1496 {
1497     if (flag != autoBreakRows) {
1498         autoBreakRows = flag;
1499         need_update = FULL;
1500         if (!flag)
1501             removeNewlines();
1502     }
1503 }
1504
1505
1506 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1507 {
1508     if (how != drawFrame) {
1509         drawFrame = how;
1510         if (bv)
1511             UpdateLocal(bv, DRAW_FRAME, false);
1512     }
1513 }
1514
1515
1516 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1517 {
1518     if (frame_color != col) {
1519         frame_color = col;
1520         if (bv)
1521             UpdateLocal(bv, DRAW_FRAME, false);
1522     }
1523 }
1524
1525 #if 0
1526 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1527 {
1528     return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1529 }
1530 #endif
1531
1532
1533 int InsetText::cx(BufferView * bv) const
1534 {
1535         LyXText * text = TEXT(bv);
1536         int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1537         if (the_locking_inset) {
1538                 LyXFont font = text->GetFont(bv->buffer(),
1539                                      text->cursor.par(), text->cursor.pos());
1540                 if (font.isVisibleRightToLeft())
1541                         x -= the_locking_inset->width(bv, font);
1542         }
1543         return x;
1544 }
1545
1546
1547 int InsetText::cy(BufferView * bv) const
1548 {
1549     LyXFont font;
1550     return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1551 }
1552
1553
1554 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1555 {
1556     return TEXT(bv)->cursor.pos();
1557 }
1558
1559
1560 LyXParagraph * InsetText::cpar(BufferView * bv) const
1561 {
1562     return TEXT(bv)->cursor.par();
1563 }
1564
1565 bool InsetText::cboundary(BufferView * bv) const
1566 {
1567     return TEXT(bv)->cursor.boundary();
1568 }
1569
1570
1571 Row * InsetText::crow(BufferView * bv) const
1572 {
1573     return TEXT(bv)->cursor.row();
1574 }
1575
1576
1577 LyXText * InsetText::getLyXText(BufferView const * lbv, bool const recursive) const
1578 {
1579     // Super UGLY! (Lgb)
1580     BufferView * bv = const_cast<BufferView *>(lbv);
1581         
1582     if ((cache.find(bv) != cache.end()) && cache[bv]) {
1583         if (recursive && the_locking_inset)
1584             return the_locking_inset->getLyXText(bv);
1585         return cache[bv];
1586     }
1587     LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1588     lt->init(bv);
1589     cache[bv] = lt;
1590     if (the_locking_inset) {
1591        lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1592        if (recursive)
1593            return the_locking_inset->getLyXText(bv);
1594     }
1595     return lt;
1596 }
1597
1598
1599 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1600 {
1601     if ((cache.find(bv) == cache.end()) || !cache[bv])
1602         return;
1603     delete cache[bv];
1604     cache.erase(bv);
1605     if (recursive) {
1606         /// then remove all LyXText in text-insets
1607         LyXParagraph * p = par;
1608         for (;p;p = p->next) {
1609             p->deleteInsetsLyXText(bv);
1610         }
1611     }
1612 }
1613
1614
1615 void InsetText::resizeLyXText(BufferView * bv) const
1616 {
1617     if (!par->next && !par->size()) // resize not neccessary!
1618         return;
1619     if ((cache.find(bv) == cache.end()) || !cache[bv])
1620         return;
1621
1622     LyXParagraph * lpar = 0;
1623     LyXParagraph * selstartpar = 0;
1624     LyXParagraph * selendpar = 0;
1625     LyXParagraph::size_type pos = 0;
1626     LyXParagraph::size_type selstartpos = 0;
1627     LyXParagraph::size_type selendpos = 0;
1628     bool boundary = false;
1629     bool selstartboundary = false;
1630     bool selendboundary = false;
1631     int selection = 0;
1632     int mark_set = 0;
1633
1634 //    ProhibitInput(bv);
1635
1636     if (locked) {
1637         lpar = TEXT(bv)->cursor.par();
1638         pos = TEXT(bv)->cursor.pos();
1639         boundary = TEXT(bv)->cursor.boundary();
1640         selstartpar = TEXT(bv)->sel_start_cursor.par();
1641         selstartpos = TEXT(bv)->sel_start_cursor.pos();
1642         selstartboundary = TEXT(bv)->sel_start_cursor.boundary();
1643         selendpar = TEXT(bv)->sel_end_cursor.par();
1644         selendpos = TEXT(bv)->sel_end_cursor.pos();
1645         selendboundary = TEXT(bv)->sel_end_cursor.boundary();
1646         selection = TEXT(bv)->selection;
1647         mark_set = TEXT(bv)->mark_set;
1648     }
1649     deleteLyXText(bv, (the_locking_inset == 0));
1650
1651     if (lpar) {
1652         TEXT(bv)->selection = true;
1653         /* at this point just to avoid the Delete-Empty-Paragraph
1654          * Mechanism when setting the cursor */
1655         TEXT(bv)->mark_set = mark_set;
1656         if (selection) {
1657             TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,true, 
1658                                 selstartboundary);
1659             TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1660             TEXT(bv)->SetCursor(bv, selendpar, selendpos, true, selendboundary);
1661             TEXT(bv)->SetSelection(bv);
1662             TEXT(bv)->SetCursor(bv, lpar, pos);
1663         } else {
1664             TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1665             TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1666             TEXT(bv)->selection = false;
1667         }
1668     }
1669     if (bv->screen())
1670             TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1671     // this will scroll the screen such that the cursor becomes visible 
1672     bv->updateScrollbar();
1673 //    AllowInput(bv);
1674     if (the_locking_inset) {
1675         /// then resize all LyXText in text-insets
1676         inset_x = cx(bv) - top_x + drawTextXOffset;
1677         inset_y = cy(bv) + drawTextYOffset;
1678         for (LyXParagraph * p = par; p; p = p->next) {
1679             p->resizeInsetsLyXText(bv);
1680         }
1681     }
1682     need_update = FULL;
1683 }
1684
1685
1686 void InsetText::removeNewlines()
1687 {
1688     for (LyXParagraph * p = par; p; p = p->next) {
1689         for (int i = 0; i < p->Last(); ++i) {
1690             if (p->GetChar(i) == LyXParagraph::META_NEWLINE)
1691                 p->Erase(i);
1692         }
1693     }
1694 }