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