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