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