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