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