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