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