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