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