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