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