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