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