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