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