]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
Collapse all those LFUN_XYZ_APPLY to a single LFUN_INSET_APPLY.
[lyx.git] / src / insets / insettext.C
1 /**
2  * \file insettext.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13
14 #include "insettext.h"
15
16 #include "buffer.h"
17 #include "BufferView.h"
18 #include "CutAndPaste.h"
19 #include "debug.h"
20 #include "funcrequest.h"
21 #include "gettext.h"
22 #include "intl.h"
23 #include "LaTeXFeatures.h"
24 #include "LColor.h"
25 #include "lyxfont.h"
26 #include "lyxcursor.h"
27 #include "lyxfind.h"
28 #include "lyxlex.h"
29 #include "lyxrow.h"
30 #include "lyxrc.h"
31 #include "lyxtext.h"
32 #include "paragraph.h"
33 #include "ParagraphParameters.h"
34 #include "trans_mgr.h"
35 #include "undo_funcs.h"
36 #include "WordLangTuple.h"
37 #include "paragraph_funcs.h"
38 #include "sgml.h"
39 #include "rowpainter.h"
40
41 #include "frontends/Alert.h"
42 #include "frontends/Dialogs.h"
43 #include "frontends/font_metrics.h"
44 #include "frontends/LyXView.h"
45 #include "frontends/Painter.h"
46 #include "frontends/screen.h"
47
48 #include "support/textutils.h"
49 #include "support/LAssert.h"
50 #include "support/lstrings.h"
51 #include "support/lyxalgo.h" // lyx::count
52
53 #include <boost/bind.hpp>
54
55 #include <fstream>
56 #include <algorithm>
57 #include <cstdlib>
58 //#include <csignal>
59
60 using std::ostream;
61 using std::ifstream;
62 using std::endl;
63 using std::min;
64 using std::max;
65 using std::make_pair;
66 using std::vector;
67 using std::pair;
68 using std::for_each;
69
70 using lyx::pos_type;
71 using lyx::textclass_type;
72
73 extern unsigned char getCurrentTextClass(Buffer *);
74 extern bool math_insert_greek(BufferView *, char);
75 extern int greek_kb_flag;
76
77
78 // These functions should probably go into bufferview_funcs somehow (Jug)
79
80 void InsetText::saveLyXTextState(LyXText * t) const
81 {
82         // check if my paragraphs are still valid
83         ParagraphList::iterator it = paragraphs.begin();
84         ParagraphList::iterator end = paragraphs.end();
85         for (; it != end; ++it) {
86                 if (&*it == t->cursor.par())
87                         break;
88         }
89
90         if (it != end && t->cursor.pos() <= it->size()) {
91                 sstate.lpar = t->cursor.par();
92                 sstate.pos = t->cursor.pos();
93                 sstate.boundary = t->cursor.boundary();
94                 sstate.selstartpar = t->selection.start.par();
95                 sstate.selstartpos = t->selection.start.pos();
96                 sstate.selstartboundary = t->selection.start.boundary();
97                 sstate.selendpar = t->selection.end.par();
98                 sstate.selendpos = t->selection.end.pos();
99                 sstate.selendboundary = t->selection.end.boundary();
100                 sstate.selection = t->selection.set();
101                 sstate.mark_set = t->selection.mark();
102                 sstate.refresh = t->refresh_row != 0;
103         } else {
104                 sstate.lpar = 0;
105         }
106 }
107
108
109 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
110 {
111         if (sstate.lpar) {
112                 t->selection.set(true);
113                 /* at this point just to avoid the Delete-Empty-Paragraph
114                  * Mechanism when setting the cursor */
115                 t->selection.mark(sstate.mark_set);
116                 if (sstate.selection) {
117                         t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
118                                      true, sstate.selstartboundary);
119                         t->selection.cursor = t->cursor;
120                         t->setCursor(bv, sstate.selendpar, sstate.selendpos,
121                                      true, sstate.selendboundary);
122                         t->setSelection(bv);
123                         t->setCursor(bv, sstate.lpar, sstate.pos);
124                 } else {
125                         t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
126                         t->selection.cursor = t->cursor;
127                         t->selection.set(false);
128                 }
129                 if (sstate.refresh) {
130                 }
131         }
132 }
133
134
135 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
136 {
137         text = t;
138         remove = false;
139 }
140
141
142 InsetText::InsetText(BufferParams const & bp)
143         : UpdatableInset(), lt(0), in_update(false), do_resize(0),
144           do_reinit(false)
145 {
146         paragraphs.set(new Paragraph);
147         paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
148         if (bp.tracking_changes)
149                 paragraphs.begin()->trackChanges();
150         init();
151 }
152
153
154 InsetText::InsetText(InsetText const & in, bool same_id)
155         : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
156           do_reinit(false)
157 {
158         init(&in, same_id);
159 }
160
161
162 InsetText & InsetText::operator=(InsetText const & it)
163 {
164         init(&it);
165         return * this;
166 }
167
168
169 void InsetText::init(InsetText const * ins, bool same_id)
170 {
171         if (ins) {
172                 setParagraphData(&*(ins->paragraphs.begin()), same_id);
173                 autoBreakRows = ins->autoBreakRows;
174                 drawFrame_ = ins->drawFrame_;
175                 frame_color = ins->frame_color;
176                 if (same_id)
177                         id_ = ins->id_;
178         } else {
179                 for_each(paragraphs.begin(), paragraphs.end(),
180                          boost::bind(&Paragraph::setInsetOwner, _1, this));
181
182                 the_locking_inset = 0;
183                 drawFrame_ = NEVER;
184                 frame_color = LColor::insetframe;
185                 autoBreakRows = false;
186         }
187         top_y = 0;
188         insetAscent = 0;
189         insetDescent = 0;
190         insetWidth = 0;
191         old_max_width = 0;
192         no_selection = true;
193         need_update = FULL;
194         drawTextXOffset = 0;
195         drawTextYOffset = 0;
196         locked = false;
197         old_par = 0;
198         last_drawn_width = -1;
199         frame_is_visible = false;
200         cached_bview = 0;
201         sstate.lpar = 0;
202         in_insetAllowed = false;
203 }
204
205
206 InsetText::~InsetText()
207 {
208         paragraphs.clear();
209 }
210
211
212 void InsetText::clear(bool just_mark_erased)
213 {
214         if (just_mark_erased) {
215                 ParagraphList::iterator it = paragraphs.begin();
216                 ParagraphList::iterator end = paragraphs.end();
217                 for (; it != end; ++it) {
218                         it->markErased();
219                 }
220                 need_update = FULL;
221                 return;
222         }
223  
224         // This is a gross hack...
225         LyXLayout_ptr old_layout = paragraphs.begin()->layout();
226
227         paragraphs.clear();
228         paragraphs.set(new Paragraph);
229         paragraphs.begin()->setInsetOwner(this);
230         paragraphs.begin()->layout(old_layout);
231
232         reinitLyXText();
233         need_update = INIT;
234 }
235
236
237 Inset * InsetText::clone(Buffer const &, bool same_id) const
238 {
239         return new InsetText(*this, same_id);
240 }
241
242
243 void InsetText::write(Buffer const * buf, ostream & os) const
244 {
245         os << "Text\n";
246         writeParagraphData(buf, os);
247 }
248
249
250 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
251 {
252         ParagraphList::iterator it = paragraphs.begin();
253         ParagraphList::iterator end = paragraphs.end();
254         Paragraph::depth_type dth = 0;
255         for (; it != end; ++it) {
256                 it->write(buf, os, buf->params, dth);
257         }
258 }
259
260
261 void InsetText::read(Buffer const * buf, LyXLex & lex)
262 {
263         string token;
264         int pos = 0;
265         Paragraph * return_par = 0;
266         Paragraph::depth_type depth = 0;
267         LyXFont font(LyXFont::ALL_INHERIT);
268
269         clear(false);
270
271         if (buf->params.tracking_changes)
272                 paragraphs.begin()->trackChanges();
273  
274         while (lex.isOK()) {
275                 lex.nextToken();
276                 token = lex.getString();
277                 if (token.empty())
278                         continue;
279                 if (token == "\\end_inset") {
280                         break;
281                 }
282
283                 Paragraph * tmp = &*(paragraphs.begin());
284
285                 if (const_cast<Buffer*>(buf)->
286                         parseSingleLyXformat2Token(lex, tmp, return_par,
287                                                    token, pos, depth, font)) {
288                         // the_end read this should NEVER happen
289                         lex.printError("\\the_end read in inset! Error in document!");
290                         return;
291                 }
292                 paragraphs.set(tmp);
293         }
294         if (!return_par)
295                 return_par = &*(paragraphs.begin());
296         paragraphs.set(return_par);
297         while (return_par) {
298                 return_par->setInsetOwner(this);
299                 return_par = return_par->next();
300         }
301
302         if (token != "\\end_inset") {
303                 lex.printError("Missing \\end_inset at this point. "
304                                            "Read: `$$Token'");
305         }
306         need_update = FULL;
307 }
308
309
310 int InsetText::ascent(BufferView * bv, LyXFont const &) const
311 {
312         insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
313                 TEXT_TO_INSET_OFFSET;
314         return insetAscent;
315 }
316
317
318 int InsetText::descent(BufferView * bv, LyXFont const &) const
319 {
320         LyXText * llt = getLyXText(bv);
321         insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
322                 TEXT_TO_INSET_OFFSET;
323         return insetDescent;
324 }
325
326
327 int InsetText::width(BufferView * bv, LyXFont const &) const
328 {
329         insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
330                 (2 * TEXT_TO_INSET_OFFSET);
331         insetWidth = max(insetWidth, 10);
332         return insetWidth;
333 }
334
335
336 int InsetText::textWidth(BufferView * bv, bool fordraw) const
337 {
338         int w;
339         if (!autoBreakRows) {
340                 w = -1;
341         } else {
342                 w = getMaxWidth(bv, this);
343         }
344         if (fordraw) {
345                 return max(w - (2 * TEXT_TO_INSET_OFFSET),
346                            (int)getLyXText(bv)->width);
347         } else if (w < 0) {
348             return -1;
349         }
350         return w - (2 * TEXT_TO_INSET_OFFSET);
351 }
352
353
354 void InsetText::draw(BufferView * bv, LyXFont const & f,
355                      int baseline, float & x, bool cleared) const
356 {
357         if (nodraw())
358                 return;
359
360         Painter & pain = bv->painter();
361
362         // this is the first thing we have to ask because if the x pos
363         // changed we have to do a complete rebreak of the text as we
364         // may have few space to draw in. Well we should check on this too
365         int old_x = top_x;
366         if (top_x != int(x)) {
367                 top_x = int(x);
368                 topx_set = true;
369                 int nw = getMaxWidth(bv, this);
370                 if (nw > 0 && old_max_width != nw) {
371                         need_update = INIT;
372                         old_max_width = nw;
373                         bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
374                         return;
375                 }
376         }
377
378         // call these methods so that insetWidth, insetAscent and
379         // insetDescent have the right values.
380         width(bv, f);
381         ascent(bv, f);
382         descent(bv, f);
383
384         // repaint the background if needed
385         if (cleared && backgroundColor() != LColor::background) {
386                 clearInset(bv, baseline, cleared);
387         }
388
389         // no draw is necessary !!!
390         if ((drawFrame_ == LOCKED) && !locked && paragraphs.begin()->empty()) {
391                 top_baseline = baseline;
392                 x += width(bv, f);
393                 if (need_update & CLEAR_FRAME)
394                         clearFrame(pain, cleared);
395                 need_update = NONE;
396                 return;
397         }
398
399         if (!owner())
400                 x += static_cast<float>(scroll());
401
402         // if top_x differs we did it already
403         if (!cleared && (old_x == int(x))
404             && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
405                 ||(last_drawn_width != insetWidth)))
406         {
407                 // Condition necessary to eliminate bug 59 attachment 37
408                 if (baseline > 0)
409                         clearInset(bv, baseline, cleared);
410         }
411
412         if (cleared)
413                 frame_is_visible = false;
414
415         if (!cleared && (need_update == NONE)) {
416                 if (locked)
417                         drawFrame(pain, cleared);
418                 return;
419         }
420
421         top_baseline = baseline;
422         top_y = baseline - insetAscent;
423
424         if (last_drawn_width != insetWidth) {
425                 if (!cleared)
426                         clearInset(bv, baseline, cleared);
427                 need_update |= FULL;
428                 last_drawn_width = insetWidth;
429         }
430
431         if (the_locking_inset && (cpar(bv) == inset_par)
432                 && (cpos(bv) == inset_pos)) {
433                 inset_x = cix(bv) - top_x + drawTextXOffset;
434                 inset_y = ciy(bv) + drawTextYOffset;
435         }
436         if (!cleared && (need_update == CURSOR)
437             && !getLyXText(bv)->selection.set()) {
438                 drawFrame(pain, cleared);
439                 x += insetWidth;
440                 need_update = NONE;
441                 return;
442         }
443         bool clear = false;
444         if (!lt) {
445                 lt = getLyXText(bv);
446                 clear = true;
447         }
448         x += TEXT_TO_INSET_OFFSET;
449
450         Row * row = lt->firstRow();
451         int y_offset = baseline - row->ascent_of_text();
452         int ph = pain.paperHeight();
453         int first = 0;
454         int y = y_offset;
455         while ((row != 0) && ((y+row->height()) <= 0)) {
456                 y += row->height();
457                 first += row->height();
458                 row = row->next();
459         }
460         if (y_offset < 0) {
461                 lt->first_y = -y_offset;
462                 first = y;
463                 y_offset = 0;
464         } else {
465                 lt->first_y = first;
466                 first = 0;
467         }
468         if (cleared || (need_update&(INIT|FULL))) {
469                 int yf = y_offset + first;
470                 y = 0;
471                 while ((row != 0) && (yf < ph)) {
472                         Row * prev = row->previous();
473                         RowPainter rp(*bv, *lt, *row);
474                         if (rp.paint(y + y_offset + first, int(x), y + lt->first_y, cleared))
475                                 lt->markChangeInDraw(bv, row, prev);
476                         if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
477                                 lt->need_break_row = row;
478                                 lt->fullRebreak(bv);
479                                 lt->setCursor(bv, lt->cursor.par(),
480                                               lt->cursor.pos());
481                                 if (lt->selection.set()) {
482                                         lt->setCursor(bv, lt->selection.start,
483                                                       lt->selection.start.par(),
484                                                       lt->selection.start.pos());
485                                         lt->setCursor(bv, lt->selection.end,
486                                                       lt->selection.end.par(),
487                                                       lt->selection.end.pos());
488                                 }
489                                 break;
490                         }
491                         y += row->height();
492                         yf += row->height();
493                         row = row->next();
494                 }
495         } else if (!locked) {
496                 if (need_update & CURSOR) {
497                         bv->screen().toggleSelection(lt, bv, true, y_offset,int(x));
498                         lt->clearSelection();
499                         lt->selection.cursor = lt->cursor;
500                 }
501                 bv->screen().update(lt, bv, y_offset, int(x));
502         } else {
503                 locked = false;
504                 if (need_update & SELECTION) {
505                         bv->screen().toggleToggle(lt, bv, y_offset, int(x));
506                 } else if (need_update & CURSOR) {
507                         bv->screen().toggleSelection(lt, bv, true, y_offset,int(x));
508                         lt->clearSelection();
509                         lt->selection.cursor = lt->cursor;
510                 }
511                 bv->screen().update(lt, bv, y_offset, int(x));
512                 locked = true;
513         }
514
515         lt->refresh_y = 0;
516         lt->status(bv, LyXText::UNCHANGED);
517         if ((drawFrame_ == ALWAYS) ||
518                 ((cleared || (need_update != CURSOR_PAR)) &&
519                  (drawFrame_ == LOCKED) && locked))
520         {
521                 drawFrame(pain, cleared);
522         } else if (need_update & CLEAR_FRAME) {
523                 clearFrame(pain, cleared);
524         }
525
526         x += insetWidth - TEXT_TO_INSET_OFFSET;
527
528         if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
529                 need_update |= FULL;
530         } else if (need_update != INIT) {
531                 need_update = NONE;
532         }
533         if (clear)
534                 lt = 0;
535 }
536
537
538 void InsetText::drawFrame(Painter & pain, bool cleared) const
539 {
540         static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
541         if (!frame_is_visible || cleared) {
542                 frame_x = top_x + ttoD2;
543                 frame_y = top_baseline - insetAscent + ttoD2;
544                 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
545                 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
546                 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
547                                frame_color);
548                 frame_is_visible = true;
549         }
550 }
551
552
553 void InsetText::clearFrame(Painter & pain, bool cleared) const
554 {
555         if (frame_is_visible) {
556                 if (!cleared) {
557                         pain.rectangle(frame_x, frame_y, frame_w, frame_h,
558                                        backgroundColor());
559                 }
560                 frame_is_visible = false;
561         }
562 }
563
564
565 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
566 {
567         if (in_update) {
568                 if (reinit && owner()) {
569                         reinitLyXText();
570                         owner()->update(bv, font, true);
571                 }
572                 return;
573         }
574         in_update = true;
575         if (reinit || need_update == INIT) {
576                 need_update = FULL;
577                 // we should put this call where we set need_update to INIT!
578                 reinitLyXText();
579                 if (owner())
580                         owner()->update(bv, font, true);
581                 in_update = false;
582                 return;
583         }
584
585         if (!autoBreakRows && paragraphs.begin()->next())
586                 collapseParagraphs(bv);
587
588         if (the_locking_inset) {
589                 inset_x = cix(bv) - top_x + drawTextXOffset;
590                 inset_y = ciy(bv) + drawTextYOffset;
591                 the_locking_inset->update(bv, font, reinit);
592         }
593
594         bool clear = false;
595         if (!lt) {
596                 lt = getLyXText(bv);
597                 clear = true;
598         }
599         if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
600                 the_locking_inset)
601         {
602                 lt->updateInset(bv, the_locking_inset);
603         }
604         if (lt->status() == LyXText::NEED_MORE_REFRESH)
605                 need_update |= FULL;
606         if (clear)
607                 lt = 0;
608         in_update = false;
609 }
610
611
612 void InsetText::setUpdateStatus(BufferView * bv, int what) const
613 {
614         // this does nothing dangerous so use only a localized buffer
615         LyXText * llt = getLyXText(bv);
616
617         need_update |= what;
618         // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
619         // if we don't break row so that we only have one row to update!
620         if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
621             (!autoBreakRows &&
622              (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
623         {
624                 need_update |= FULL;
625         } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
626                 need_update |= CURSOR_PAR;
627         }
628
629         // this to not draw a selection when we redraw all of it!
630         if (need_update & CURSOR && !(need_update & SELECTION)) {
631                 if (llt->selection.set())
632                         need_update = FULL;
633                 llt->clearSelection();
634         }
635 }
636
637
638 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
639 {
640         if (!autoBreakRows && paragraphs.begin()->next())
641                 collapseParagraphs(bv);
642         bool clear = false;
643         if (!lt) {
644                 lt = getLyXText(bv);
645                 clear = true;
646         }
647         lt->fullRebreak(bv);
648         setUpdateStatus(bv, what);
649         bool flag = mark_dirty ||
650                 (((need_update != CURSOR) && (need_update != NONE)) ||
651                  (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
652         if (!lt->selection.set())
653                 lt->selection.cursor = lt->cursor;
654         if (clear)
655                 lt = 0;
656 #if 0
657         // IMO this is not anymore needed as we do this in fitInsetCursor!
658         // and we always get "true" as returnvalue of this function in the
659         // case of a locking inset (Jug 20020412)
660         if (locked && (need_update & CURSOR) && bv->fitCursor())
661                 need_update |= FULL;
662 #else
663         bv->fitCursor();
664 #endif
665         if (flag)
666                 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
667
668         if (need_update == CURSOR)
669                 need_update = NONE;
670         bv->owner()->view_state_changed();
671         bv->owner()->updateMenubar();
672         bv->owner()->updateToolbar();
673         if (old_par != cpar(bv)) {
674                 bv->owner()->setLayout(cpar(bv)->layout()->name());
675                 old_par = cpar(bv);
676         }
677 }
678
679
680 string const InsetText::editMessage() const
681 {
682         return _("Opened Text Inset");
683 }
684
685
686 void InsetText::edit(BufferView * bv, int x, int y, mouse_button::state button)
687 {
688         UpdatableInset::edit(bv, x, y, button);
689
690         if (!bv->lockInset(this)) {
691                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
692                 return;
693         }
694         locked = true;
695         the_locking_inset = 0;
696         inset_pos = inset_x = inset_y = 0;
697         inset_boundary = false;
698         inset_par = 0;
699         old_par = 0;
700         int tmp_y = (y < 0) ? 0 : y;
701         bool clear = false;
702         if (!lt) {
703                 lt = getLyXText(bv);
704                 clear = true;
705         }
706         // we put here -1 and not button as now the button in the
707         // edit call should not be needed we will fix this in 1.3.x
708         // cycle hopefully (Jug 20020509)
709         // FIXME: GUII I've changed this to none: probably WRONG
710         if (!checkAndActivateInset(bv, x, tmp_y, mouse_button::none)) {
711                 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
712                                             y + insetAscent);
713                 lt->cursor.x_fix(lt->cursor.x());
714         }
715         lt->clearSelection();
716         finishUndo();
717         // If the inset is empty set the language of the current font to the
718         // language to the surronding text (if different).
719         if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
720                 bv->getParentLanguage(this) != lt->current_font.language())
721         {
722                 LyXFont font(LyXFont::ALL_IGNORE);
723                 font.setLanguage(bv->getParentLanguage(this));
724                 setFont(bv, font, false);
725         }
726         if (clear)
727                 lt = 0;
728
729         int code = CURSOR;
730         if (drawFrame_ == LOCKED)
731                 code = CURSOR|DRAW_FRAME;
732         updateLocal(bv, code, false);
733         showInsetCursor(bv);
734
735         // Tell the paragraph dialog that we've entered an insettext.
736         bv->owner()->getDialogs().updateParagraph();
737 }
738
739
740 void InsetText::edit(BufferView * bv, bool front)
741 {
742         UpdatableInset::edit(bv, front);
743
744         if (!bv->lockInset(this)) {
745                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
746                 return;
747         }
748         locked = true;
749         the_locking_inset = 0;
750         inset_pos = inset_x = inset_y = 0;
751         inset_boundary = false;
752         inset_par = 0;
753         old_par = 0;
754         bool clear = false;
755         if (!lt) {
756                 lt = getLyXText(bv);
757                 clear = true;
758         }
759         if (front)
760                 lt->setCursor(bv, &*(paragraphs.begin()), 0);
761         else {
762                 ParagraphList::iterator it = paragraphs.begin();
763                 ParagraphList::iterator end = paragraphs.end();
764                 while (boost::next(it) != end)
765                         ++it;
766 //              int const pos = (p->size() ? p->size()-1 : p->size());
767                 lt->setCursor(bv, &*it, it->size());
768         }
769         lt->clearSelection();
770         finishUndo();
771         // If the inset is empty set the language of the current font to the
772         // language to the surronding text (if different).
773         if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
774                 bv->getParentLanguage(this) != lt->current_font.language()) {
775                 LyXFont font(LyXFont::ALL_IGNORE);
776                 font.setLanguage(bv->getParentLanguage(this));
777                 setFont(bv, font, false);
778         }
779         if (clear)
780                 lt = 0;
781         int code = CURSOR;
782         if (drawFrame_ == LOCKED)
783                 code = CURSOR|DRAW_FRAME;
784         updateLocal(bv, code, false);
785         showInsetCursor(bv);
786 }
787
788
789 void InsetText::insetUnlock(BufferView * bv)
790 {
791         if (the_locking_inset) {
792                 the_locking_inset->insetUnlock(bv);
793                 the_locking_inset = 0;
794                 updateLocal(bv, CURSOR_PAR, false);
795         }
796         hideInsetCursor(bv);
797         no_selection = true;
798         locked = false;
799         int code = NONE;
800 #if 0
801         if (drawFrame_ == LOCKED)
802                 code = CURSOR|CLEAR_FRAME;
803         else
804                 code = CURSOR;
805 #else
806         if (drawFrame_ == LOCKED)
807                 code = CLEAR_FRAME;
808 #endif
809         bool clear = false;
810         if (!lt) {
811                 lt = getLyXText(bv);
812                 clear = true;
813         }
814         if (lt->selection.set()) {
815                 lt->clearSelection();
816                 code = FULL;
817         } else if (owner()) {
818                 bv->owner()->setLayout(owner()->getLyXText(bv)
819                                        ->cursor.par()->layout()->name());
820         } else
821                 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
822         // hack for deleteEmptyParMech
823         if (!paragraphs.begin()->empty()) {
824                 lt->setCursor(bv, &*(paragraphs.begin()), 0);
825         } else if (paragraphs.begin()->next()) {
826                 lt->setCursor(bv, paragraphs.begin()->next(), 0);
827         }
828         if (clear)
829                 lt = 0;
830 #if 0
831         updateLocal(bv, code, false);
832 #else
833         if (code != NONE)
834                 setUpdateStatus(bv, code);
835 #endif
836 }
837
838
839 void InsetText::lockInset(BufferView * bv)
840 {
841         locked = true;
842         the_locking_inset = 0;
843         inset_pos = inset_x = inset_y = 0;
844         inset_boundary = false;
845         inset_par = 0;
846         old_par = 0;
847         bool clear = false;
848         if (!lt) {
849                 lt = getLyXText(bv);
850                 clear = true;
851         }
852         lt->setCursor(bv, &*(paragraphs.begin()), 0);
853         lt->clearSelection();
854         finishUndo();
855         // If the inset is empty set the language of the current font to the
856         // language to the surronding text (if different).
857         if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
858                 bv->getParentLanguage(this) != lt->current_font.language()) {
859                 LyXFont font(LyXFont::ALL_IGNORE);
860                 font.setLanguage(bv->getParentLanguage(this));
861                 setFont(bv, font, false);
862         }
863         if (clear)
864                 lt = 0;
865         int code = CURSOR;
866         if (drawFrame_ == LOCKED)
867                 code = CURSOR|DRAW_FRAME;
868         setUpdateStatus(bv, code);
869 }
870
871
872 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
873 {
874         the_locking_inset = inset;
875         inset_x = cix(bv) - top_x + drawTextXOffset;
876         inset_y = ciy(bv) + drawTextYOffset;
877         inset_pos = cpos(bv);
878         inset_par = cpar(bv);
879         inset_boundary = cboundary(bv);
880         updateLocal(bv, CURSOR, false);
881 }
882
883
884 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
885 {
886         lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
887                               << inset << "): ";
888         if (!inset)
889                 return false;
890         if (!the_locking_inset) {
891                 Paragraph * p = &*(paragraphs.begin());
892                 int const id = inset->id();
893                 while(p) {
894                         InsetList::iterator it =
895                                 p->insetlist.begin();
896                         InsetList::iterator const end =
897                                 p->insetlist.end();
898                         for (; it != end; ++it) {
899                                 if (it.getInset() == inset) {
900                                         getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
901                                         lockInset(bv, inset);
902                                         return true;
903                                 }
904                                 if (it.getInset()->getInsetFromID(id)) {
905                                         getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
906                                         it.getInset()->edit(bv);
907                                         return the_locking_inset->lockInsetInInset(bv, inset);
908                                 }
909                         }
910                         p = p->next();
911                 }
912                 return false;
913         }
914         if (inset == cpar(bv)->getInset(cpos(bv))) {
915                 lyxerr[Debug::INSETS] << "OK" << endl;
916                 lockInset(bv, inset);
917                 return true;
918         } else if (the_locking_inset && (the_locking_inset == inset)) {
919                 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
920                         lyxerr[Debug::INSETS] << "OK" << endl;
921                         inset_x = cix(bv) - top_x + drawTextXOffset;
922                         inset_y = ciy(bv) + drawTextYOffset;
923                 } else {
924                         lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
925                 }
926         } else if (the_locking_inset) {
927                 lyxerr[Debug::INSETS] << "MAYBE" << endl;
928                 return the_locking_inset->lockInsetInInset(bv, inset);
929         }
930         lyxerr[Debug::INSETS] << "NOT OK" << endl;
931         return false;
932 }
933
934
935 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
936                                    bool lr)
937 {
938         if (!the_locking_inset)
939                 return false;
940         if (the_locking_inset == inset) {
941                 the_locking_inset->insetUnlock(bv);
942                 getLyXText(bv)->updateInset(bv, inset);
943                 the_locking_inset = 0;
944                 if (lr)
945                         moveRightIntern(bv, true, false);
946                 old_par = 0; // force layout setting
947                 if (scroll())
948                         scroll(bv, 0.0F);
949                 else
950                         updateLocal(bv, CURSOR, false);
951                 return true;
952         }
953         return the_locking_inset->unlockInsetInInset(bv, inset, lr);
954 }
955
956
957 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
958 {
959         if (!autoBreakRows && paragraphs.begin()->next())
960                 collapseParagraphs(bv);
961         if (inset == this)
962                 return true;
963         bool clear = false;
964         if (!lt) {
965                 lt = getLyXText(bv);
966                 clear = true;
967         }
968         if (inset->owner() != this) {
969                 int ustat = CURSOR_PAR;
970                 bool found = false;
971                 UpdatableInset * tl_inset = the_locking_inset;
972                 if (tl_inset)
973                         found = tl_inset->updateInsetInInset(bv, inset);
974                 if (!found) {
975                         tl_inset = static_cast<UpdatableInset *>(inset);
976                         while(tl_inset->owner() && tl_inset->owner() != this)
977                                 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
978                         if (!tl_inset->owner())
979                                 return false;
980                         found = tl_inset->updateInsetInInset(bv, inset);
981                         ustat = FULL;
982                 }
983                 if (found)
984                         lt->updateInset(bv, tl_inset);
985                 if (clear)
986                         lt = 0;
987                 if (found)
988                         setUpdateStatus(bv, ustat);
989                 return found;
990         }
991         bool found = lt->updateInset(bv, inset);
992         if (clear)
993                 lt = 0;
994         if (found) {
995                 setUpdateStatus(bv, CURSOR_PAR);
996                 if (the_locking_inset &&
997                     cpar(bv) == inset_par && cpos(bv) == inset_pos)
998                 {
999                         inset_x = cix(bv) - top_x + drawTextXOffset;
1000                         inset_y = ciy(bv) + drawTextYOffset;
1001                 }
1002         }
1003         return found;
1004 }
1005
1006
1007 void InsetText::lfunMousePress(FuncRequest const & cmd)
1008 {
1009         no_selection = true;
1010
1011         // use this to check mouse motion for selection!
1012         mouse_x = cmd.x;
1013         mouse_y = cmd.y;
1014
1015         BufferView * bv = cmd.view();
1016         FuncRequest cmd1 = cmd;
1017         cmd1.x -= inset_x;
1018         cmd1.y -= inset_y;
1019         if (!locked)
1020                 lockInset(bv);
1021
1022         int tmp_x = cmd.x - drawTextXOffset;
1023         int tmp_y = cmd.y + insetAscent - getLyXText(bv)->first_y;
1024         Inset * inset = getLyXText(bv)->checkInsetHit(bv, tmp_x, tmp_y);
1025
1026         hideInsetCursor(bv);
1027         if (the_locking_inset) {
1028                 if (the_locking_inset == inset) {
1029                         the_locking_inset->localDispatch(cmd1);
1030                         return;
1031                 }
1032 #if 0
1033                 else if (inset) {
1034                         // otherwise unlock the_locking_inset and lock the new inset
1035                         the_locking_inset->insetUnlock(bv);
1036                         inset_x = cix(bv) - top_x + drawTextXOffset;
1037                         inset_y = ciy(bv) + drawTextYOffset;
1038                         the_locking_inset = 0;
1039                         inset->localDispatch(cmd1);
1040 //                      inset->edit(bv, x - inset_x, y - inset_y, button);
1041                         if (the_locking_inset)
1042                                 updateLocal(bv, CURSOR, false);
1043                         return;
1044                 }
1045 #endif
1046                 // otherwise only unlock the_locking_inset
1047                 the_locking_inset->insetUnlock(bv);
1048                 the_locking_inset = 0;
1049         }
1050         if (!inset)
1051                 no_selection = false;
1052
1053         if (bv->theLockingInset()) {
1054                 if (isHighlyEditableInset(inset)) {
1055                         // We just have to lock the inset before calling a
1056                         // PressEvent on it!
1057                         UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
1058                         if (!bv->lockInset(uinset)) {
1059                                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1060                         }
1061                         inset->localDispatch(cmd1);
1062                         if (the_locking_inset)
1063                                 updateLocal(bv, CURSOR, false);
1064                         return;
1065                 }
1066         }
1067         if (!inset) { // && (button == mouse_button::button2)) {
1068                 bool paste_internally = false;
1069                 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
1070                         localDispatch(FuncRequest(bv, LFUN_COPY));
1071                         paste_internally = true;
1072                 }
1073                 bool clear = false;
1074                 if (!lt) {
1075                         lt = getLyXText(bv);
1076                         clear = true;
1077                 }
1078                 int old_first_y = lt->first_y;
1079
1080                 lt->setCursorFromCoordinates(bv, cmd.x - drawTextXOffset,
1081                                              cmd.y + insetAscent);
1082                 // set the selection cursor!
1083                 lt->selection.cursor = lt->cursor;
1084                 lt->cursor.x_fix(lt->cursor.x());
1085
1086                 if (lt->selection.set()) {
1087                         lt->clearSelection();
1088                         if (clear)
1089                                 lt = 0;
1090                         updateLocal(bv, FULL, false);
1091                 } else {
1092                         lt->clearSelection();
1093                         if (clear)
1094                                 lt = 0;
1095                         updateLocal(bv, CURSOR, false);
1096                 }
1097
1098                 bv->owner()->setLayout(cpar(bv)->layout()->name());
1099
1100                 // we moved the view we cannot do mouse selection in this case!
1101                 if (getLyXText(bv)->first_y != old_first_y)
1102                         no_selection = true;
1103                 old_par = cpar(bv);
1104                 // Insert primary selection with middle mouse
1105                 // if there is a local selection in the current buffer,
1106                 // insert this
1107                 if (cmd.button() == mouse_button::button2) {
1108                         if (paste_internally)
1109                                 localDispatch(FuncRequest(bv, LFUN_PASTE));
1110                         else
1111                                 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
1112                 }
1113         } else {
1114                 getLyXText(bv)->clearSelection();
1115         }
1116         showInsetCursor(bv);
1117 }
1118
1119
1120 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
1121 {
1122         BufferView * bv = cmd.view();
1123         FuncRequest cmd1 = cmd;
1124         cmd1.x -= inset_x;
1125         cmd1.y -= inset_y;
1126
1127         no_selection = true;
1128         if (the_locking_inset)
1129                 return the_locking_inset->localDispatch(cmd1);
1130
1131         int tmp_x = cmd.x - drawTextXOffset;
1132         int tmp_y = cmd.y + insetAscent - getLyXText(bv)->first_y;
1133         Inset * inset = getLyXText(bv)->checkInsetHit(bv, tmp_x, tmp_y);
1134         bool ret = false;
1135         if (inset) {
1136                 if (isHighlyEditableInset(inset))
1137                         ret = inset->localDispatch(cmd1);
1138                 else {
1139                         inset_x = cix(bv) - top_x + drawTextXOffset;
1140                         inset_y = ciy(bv) + drawTextYOffset;
1141                         cmd1.x = cmd.x - inset_x;
1142                         cmd1.y = cmd.x - inset_y;
1143 // note that we should do ret = inset->localDispatch(cmd1) 
1144 // and fix this instead (Alfredo);
1145                         ret = true;
1146                         inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
1147                 }
1148                 updateLocal(bv, CURSOR_PAR, false);
1149         }
1150         return ret;
1151 }
1152
1153
1154 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
1155 {
1156         FuncRequest cmd1 = cmd;
1157         cmd1.x -= inset_x;
1158         cmd1.y -= inset_y;
1159
1160         if (the_locking_inset) {
1161                 the_locking_inset->localDispatch(cmd1);
1162                 return;
1163         }
1164
1165         if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
1166                 return;
1167
1168         BufferView * bv = cmd.view();
1169         bool clear = false;
1170         if (!lt) {
1171                 lt = getLyXText(bv);
1172                 clear = true;
1173         }
1174         hideInsetCursor(bv);
1175         LyXCursor cur = lt->cursor;
1176         lt->setCursorFromCoordinates
1177                 (bv, cmd.x - drawTextXOffset, cmd.y + insetAscent);
1178         lt->cursor.x_fix(lt->cursor.x());
1179         if (cur == lt->cursor) {
1180                 if (clear)
1181                         lt = 0;
1182                 return;
1183         }
1184         lt->setSelection(bv);
1185         bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1186                                  lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1187         if (clear)
1188                 lt = 0;
1189         if (flag) {
1190                 updateLocal(bv, SELECTION, false);
1191         }
1192         showInsetCursor(bv);
1193 }
1194
1195
1196 Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
1197 {
1198         BufferView * bv = ev.view();
1199         switch (ev.action) {
1200                 case LFUN_MOUSE_PRESS:
1201                         lfunMousePress(ev);
1202                         return DISPATCHED;
1203                 case LFUN_MOUSE_MOTION:
1204                         lfunMouseMotion(ev);
1205                         return DISPATCHED;
1206                 case LFUN_MOUSE_RELEASE:
1207                         return lfunMouseRelease(ev) ? DISPATCHED : UNDISPATCHED;
1208                 default:
1209                         break;
1210         }
1211
1212         bool was_empty = (paragraphs.begin()->empty() && !paragraphs.begin()->next());
1213         no_selection = false;
1214         RESULT result = UpdatableInset::localDispatch(ev);
1215         if (result != UNDISPATCHED)
1216                 return DISPATCHED;
1217
1218         result = DISPATCHED;
1219         if (ev.action < 0 && ev.argument.empty())
1220                 return FINISHED;
1221
1222         if (the_locking_inset) {
1223                 result = the_locking_inset->localDispatch(ev);
1224                 if (result == DISPATCHED_NOUPDATE)
1225                         return result;
1226                 else if (result == DISPATCHED) {
1227                         updateLocal(bv, CURSOR_PAR, false);
1228                         return result;
1229                 } else if (result >= FINISHED) {
1230                         switch (result) {
1231                         case FINISHED_RIGHT:
1232                                 moveRightIntern(bv, false, false);
1233                                 result = DISPATCHED;
1234                                 break;
1235                         case FINISHED_UP:
1236                                 if ((result = moveUp(bv)) >= FINISHED) {
1237                                         updateLocal(bv, CURSOR, false);
1238                                         bv->unlockInset(this);
1239                                 }
1240                                 break;
1241                         case FINISHED_DOWN:
1242                                 if ((result = moveDown(bv)) >= FINISHED) {
1243                                         updateLocal(bv, CURSOR, false);
1244                                         bv->unlockInset(this);
1245                                 }
1246                                 break;
1247                         default:
1248                                 result = DISPATCHED;
1249                                 break;
1250                         }
1251                         the_locking_inset = 0;
1252                         updateLocal(bv, CURSOR, false);
1253                         return result;
1254                 }
1255         }
1256         hideInsetCursor(bv);
1257         bool clear = false;
1258         if (!lt) {
1259                 lt = getLyXText(bv);
1260                 clear = true;
1261         }
1262         int updwhat = 0;
1263         int updflag = false;
1264         switch (ev.action) {
1265
1266         // Normal chars
1267         case LFUN_SELFINSERT:
1268                 if (bv->buffer()->isReadonly()) {
1269 //          setErrorMessage(N_("Document is read only"));
1270                         break;
1271                 }
1272                 if (!ev.argument.empty()) {
1273                         /* Automatically delete the currently selected
1274                          * text and replace it with what is being
1275                          * typed in now. Depends on lyxrc settings
1276                          * "auto_region_delete", which defaults to
1277                          * true (on). */
1278 #if 0
1279                         // This should not be needed here and is also WRONG!
1280                         setUndo(bv, Undo::INSERT,
1281                                 lt->cursor.par(), lt->cursor.par()->next());
1282 #endif
1283                         bv->switchKeyMap();
1284                         if (lyxrc.auto_region_delete) {
1285                                 if (lt->selection.set()) {
1286                                         lt->cutSelection(bv, false, false);
1287                                 }
1288                         }
1289                         lt->clearSelection();
1290                         for (string::size_type i = 0; i < ev.argument.length(); ++i) {
1291                                 bv->owner()->getIntl().getTransManager().
1292                                         TranslateAndInsert(ev.argument[i], lt);
1293                         }
1294                 }
1295                 lt->selection.cursor = lt->cursor;
1296                 updwhat = CURSOR | CURSOR_PAR;
1297                 updflag = true;
1298                 result = DISPATCHED_NOUPDATE;
1299                 break;
1300                 // --- Cursor Movements -----------------------------------
1301         case LFUN_RIGHTSEL:
1302                 finishUndo();
1303                 moveRight(bv, false, true);
1304                 lt->setSelection(bv);
1305                 updwhat = SELECTION;
1306                 break;
1307         case LFUN_RIGHT:
1308                 result = moveRight(bv);
1309                 finishUndo();
1310                 updwhat = CURSOR;
1311                 break;
1312         case LFUN_LEFTSEL:
1313                 finishUndo();
1314                 moveLeft(bv, false, true);
1315                 lt->setSelection(bv);
1316                 updwhat = SELECTION;
1317                 break;
1318         case LFUN_LEFT:
1319                 finishUndo();
1320                 result = moveLeft(bv);
1321                 updwhat = CURSOR;
1322                 break;
1323         case LFUN_DOWNSEL:
1324                 finishUndo();
1325                 moveDown(bv);
1326                 lt->setSelection(bv);
1327                 updwhat = SELECTION;
1328                 break;
1329         case LFUN_DOWN:
1330                 finishUndo();
1331                 result = moveDown(bv);
1332                 updwhat = CURSOR;
1333                 break;
1334         case LFUN_UPSEL:
1335                 finishUndo();
1336                 moveUp(bv);
1337                 lt->setSelection(bv);
1338                 updwhat = SELECTION;
1339                 break;
1340         case LFUN_UP:
1341                 finishUndo();
1342                 result = moveUp(bv);
1343                 updwhat = CURSOR;
1344                 break;
1345         case LFUN_HOME:
1346                 finishUndo();
1347                 lt->cursorHome(bv);
1348                 updwhat = CURSOR;
1349                 break;
1350         case LFUN_END:
1351                 lt->cursorEnd(bv);
1352                 updwhat = CURSOR;
1353                 break;
1354         case LFUN_PRIOR:
1355                 if (!crow(bv)->previous())
1356                         result = FINISHED_UP;
1357                 else {
1358                         lt->cursorPrevious(bv);
1359                         result = DISPATCHED_NOUPDATE;
1360                 }
1361                 updwhat = CURSOR;
1362                 break;
1363         case LFUN_NEXT:
1364                 if (!crow(bv)->next())
1365                         result = FINISHED_DOWN;
1366                 else {
1367                         lt->cursorNext(bv);
1368                         result = DISPATCHED_NOUPDATE;
1369                 }
1370                 updwhat = CURSOR;
1371                 break;
1372         case LFUN_BACKSPACE: {
1373                 if (lt->selection.set())
1374                         lt->cutSelection(bv, true, false);
1375                 else
1376                         lt->backspace(bv);
1377                 updwhat = CURSOR_PAR;
1378                 updflag = true;
1379         }
1380         break;
1381
1382         case LFUN_DELETE: {
1383                 if (lt->selection.set()) {
1384                         lt->cutSelection(bv, true, false);
1385                 } else {
1386                         lt->Delete(bv);
1387                 }
1388                 updwhat = CURSOR_PAR;
1389                 updflag = true;
1390         }
1391         break;
1392
1393         case LFUN_CUT: {
1394                 lt->cutSelection(bv);
1395                 updwhat = CURSOR_PAR;
1396                 updflag = true;
1397         }
1398         break;
1399
1400         case LFUN_COPY:
1401                 finishUndo();
1402                 lt->copySelection(bv);
1403                 updwhat = CURSOR_PAR;
1404                 break;
1405         case LFUN_PASTESELECTION:
1406         {
1407                 string const clip(bv->getClipboard());
1408
1409                 if (clip.empty())
1410                         break;
1411                 if (ev.argument == "paragraph") {
1412                         lt->insertStringAsParagraphs(bv, clip);
1413                 } else {
1414                         lt->insertStringAsLines(bv, clip);
1415                 }
1416                 // bug 393
1417                 lt->clearSelection();
1418
1419                 updwhat = CURSOR_PAR;
1420                 updflag = true;
1421                 break;
1422         }
1423         case LFUN_PASTE: {
1424                 if (!autoBreakRows) {
1425
1426                         if (CutAndPaste::nrOfParagraphs() > 1) {
1427                                 Alert::alert(_("Impossible operation!"),
1428                                                    _("Cannot include more than one paragraph!"),
1429                                                    _("Sorry."));
1430                                 break;
1431                         }
1432                 }
1433 #if 0
1434                 // This should not be needed here and is also WRONG!
1435                 setUndo(bv, Undo::INSERT,
1436                         lt->cursor.par(), lt->cursor.par()->next());
1437 #endif
1438                 lt->pasteSelection(bv);
1439                 // bug 393
1440                 lt->clearSelection();
1441                 updwhat = CURSOR_PAR;
1442                 updflag = true;
1443         }
1444         break;
1445
1446         case LFUN_BREAKPARAGRAPH:
1447                 if (!autoBreakRows) {
1448                         result = DISPATCHED;
1449                         break;
1450                 }
1451                 lt->breakParagraph(bv, 0);
1452                 updwhat = CURSOR | FULL;
1453                 updflag = true;
1454                 break;
1455         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1456                 if (!autoBreakRows) {
1457                         result = DISPATCHED;
1458                         break;
1459                 }
1460                 lt->breakParagraph(bv, 1);
1461                 updwhat = CURSOR | FULL;
1462                 updflag = true;
1463                 break;
1464
1465         case LFUN_BREAKLINE: {
1466                 if (!autoBreakRows) {
1467                         result = DISPATCHED;
1468                         break;
1469                 }
1470 #if 0
1471                 // This should not be needed here and is also WRONG!
1472                 setUndo(bv, Undo::INSERT,
1473                         lt->cursor.par(), lt->cursor.par()->next());
1474 #endif
1475                 lt->insertChar(bv, Paragraph::META_NEWLINE);
1476                 updwhat = CURSOR | CURSOR_PAR;
1477                 updflag = true;
1478         }
1479         break;
1480
1481         case LFUN_LAYOUT:
1482                 // do not set layouts on non breakable textinsets
1483                 if (autoBreakRows) {
1484                         string cur_layout = cpar(bv)->layout()->name();
1485
1486                         // Derive layout number from given argument (string)
1487                         // and current buffer's textclass (number). */
1488                         LyXTextClass const & tclass =
1489                                 bv->buffer()->params.getLyXTextClass();
1490                         string layout = ev.argument;
1491                         bool hasLayout = tclass.hasLayout(layout);
1492
1493                         // If the entry is obsolete, use the new one instead.
1494                         if (hasLayout) {
1495                                 string const & obs =
1496                                         tclass[layout]->obsoleted_by();
1497                                 if (!obs.empty())
1498                                         layout = obs;
1499                         }
1500
1501                         // see if we found the layout number:
1502                         if (!hasLayout) {
1503                                 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + ev.argument + N_(" not known"));
1504                                 bv->owner()->dispatch(lf);
1505                                 break;
1506                         }
1507
1508                         if (cur_layout != layout) {
1509                                 cur_layout = layout;
1510                                 lt->setLayout(bv, layout);
1511                                 bv->owner()->setLayout(cpar(bv)->layout()->name());
1512                                 updwhat = CURSOR_PAR;
1513                                 updflag = true;
1514                         }
1515                 } else {
1516                         // reset the layout box
1517                         bv->owner()->setLayout(cpar(bv)->layout()->name());
1518                 }
1519                 break;
1520         case LFUN_PARAGRAPH_SPACING:
1521                 // This one is absolutely not working. When fiddling with this
1522                 // it also seems to me that the paragraphs inside the insettext
1523                 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1524         {
1525                 Paragraph * par = lt->cursor.par();
1526                 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1527                 float cur_value = 1.0;
1528                 if (cur_spacing == Spacing::Other) {
1529                         cur_value = par->params().spacing().getValue();
1530                 }
1531
1532                 istringstream istr(ev.argument.c_str());
1533                 string tmp;
1534                 istr >> tmp;
1535                 Spacing::Space new_spacing = cur_spacing;
1536                 float new_value = cur_value;
1537                 if (tmp.empty()) {
1538                         lyxerr << "Missing argument to `paragraph-spacing'"
1539                                    << endl;
1540                 } else if (tmp == "single") {
1541                         new_spacing = Spacing::Single;
1542                 } else if (tmp == "onehalf") {
1543                         new_spacing = Spacing::Onehalf;
1544                 } else if (tmp == "double") {
1545                         new_spacing = Spacing::Double;
1546                 } else if (tmp == "other") {
1547                         new_spacing = Spacing::Other;
1548                         float tmpval = 0.0;
1549                         istr >> tmpval;
1550                         lyxerr << "new_value = " << tmpval << endl;
1551                         if (tmpval != 0.0)
1552                                 new_value = tmpval;
1553                 } else if (tmp == "default") {
1554                         new_spacing = Spacing::Default;
1555                 } else {
1556                         lyxerr << _("Unknown spacing argument: ")
1557                                    << ev.argument << endl;
1558                 }
1559                 if (cur_spacing != new_spacing || cur_value != new_value) {
1560                         par->params().spacing(Spacing(new_spacing, new_value));
1561                         updwhat = CURSOR_PAR;
1562                         updflag = true;
1563                 }
1564         }
1565         break;
1566
1567         default:
1568                 if (!bv->dispatch(ev))
1569                         result = UNDISPATCHED;
1570                 break;
1571         }
1572
1573         if (clear)
1574                 lt = 0;
1575         if (updwhat > 0)
1576                 updateLocal(bv, updwhat, updflag);
1577         /// If the action has deleted all text in the inset, we need to change the
1578         // language to the language of the surronding text.
1579         if (!was_empty && paragraphs.begin()->empty() && !paragraphs.begin()->next()) {
1580                 LyXFont font(LyXFont::ALL_IGNORE);
1581                 font.setLanguage(bv->getParentLanguage(this));
1582                 setFont(bv, font, false);
1583         }
1584
1585         if (result >= FINISHED)
1586                 bv->unlockInset(this);
1587
1588         if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1589                 result = DISPATCHED;
1590         return result;
1591 }
1592
1593
1594 int InsetText::latex(Buffer const * buf, ostream & os,
1595                      bool moving_arg, bool) const
1596 {
1597         TexRow texrow;
1598         buf->latexParagraphs(os, &*(paragraphs.begin()), 0,
1599                              texrow, moving_arg);
1600         return texrow.rows();
1601 }
1602
1603
1604 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1605 {
1606         unsigned int lines = 0;
1607
1608         ParagraphList::iterator beg = paragraphs.begin();
1609         ParagraphList::iterator end = paragraphs.end();
1610         ParagraphList::iterator it = beg;
1611         for (; it != end; ++it) {
1612                 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1613                 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1614                 os << tmp;
1615         }
1616         return lines;
1617 }
1618
1619
1620 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1621 {
1622         Paragraph * p = &*(paragraphs.begin());
1623         unsigned int lines = 0;
1624
1625         vector<string> environment_stack(10);
1626         vector<string> environment_inner(10);
1627
1628         int const command_depth = 0;
1629         string item_name;
1630
1631         Paragraph::depth_type depth = 0; // paragraph depth
1632
1633         while (p) {
1634                 string sgmlparam;
1635                 int desc_on = 0; // description mode
1636
1637                 LyXLayout_ptr const & style = p->layout();
1638
1639                 // environment tag closing
1640                 for (; depth > p->params().depth(); --depth) {
1641                         if (environment_inner[depth] != "!-- --") {
1642                                 item_name = "listitem";
1643                                 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1644                                 if (environment_inner[depth] == "varlistentry")
1645                                         lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1646                         }
1647                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1648                         environment_stack[depth].erase();
1649                         environment_inner[depth].erase();
1650                 }
1651
1652                 if (depth == p->params().depth()
1653                    && environment_stack[depth] != style->latexname()
1654                    && !environment_stack[depth].empty()) {
1655                         if (environment_inner[depth] != "!-- --") {
1656                                 item_name= "listitem";
1657                                 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1658                                 if (environment_inner[depth] == "varlistentry")
1659                                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1660                         }
1661
1662                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1663
1664                         environment_stack[depth].erase();
1665                         environment_inner[depth].erase();
1666                 }
1667
1668                 // Write opening SGML tags.
1669                 switch (style->latextype) {
1670                 case LATEX_PARAGRAPH:
1671                         lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1672                         break;
1673
1674                 case LATEX_COMMAND:
1675                         buf->sgmlError(p, 0,  _("Error: LatexType Command not allowed here.\n"));
1676                         return -1;
1677                         break;
1678
1679                 case LATEX_ENVIRONMENT:
1680                 case LATEX_ITEM_ENVIRONMENT:
1681                         if (depth < p->params().depth()) {
1682                                 depth = p->params().depth();
1683                                 environment_stack[depth].erase();
1684                         }
1685
1686                         if (environment_stack[depth] != style->latexname()) {
1687                                 if (environment_stack.size() == depth + 1) {
1688                                         environment_stack.push_back("!-- --");
1689                                         environment_inner.push_back("!-- --");
1690                                 }
1691                                 environment_stack[depth] = style->latexname();
1692                                 environment_inner[depth] = "!-- --";
1693                                 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1694                         } else {
1695                                 if (environment_inner[depth] != "!-- --") {
1696                                         item_name= "listitem";
1697                                         lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1698                                         if (environment_inner[depth] == "varlistentry")
1699                                                 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1700                                 }
1701                         }
1702
1703                         if (style->latextype == LATEX_ENVIRONMENT) {
1704                                 if (!style->latexparam().empty()) {
1705                                         if (style->latexparam() == "CDATA")
1706                                                 os << "<![CDATA[";
1707                                         else
1708                                           lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1709                                 }
1710                                 break;
1711                         }
1712
1713                         desc_on = (style->labeltype == LABEL_MANUAL);
1714
1715                         environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1716                         lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1717
1718                         item_name = desc_on ? "term" : "para";
1719                         lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1720
1721                         break;
1722                 default:
1723                         lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1724                         break;
1725                 }
1726
1727                 buf->simpleDocBookOnePar(os, p, desc_on, depth + 1 + command_depth);
1728                 p = p->next();
1729
1730                 string end_tag;
1731                 // write closing SGML tags
1732                 switch (style->latextype) {
1733                 case LATEX_ENVIRONMENT:
1734                         if (!style->latexparam().empty()) {
1735                                 if (style->latexparam() == "CDATA")
1736                                         os << "]]>";
1737                                 else
1738                                         lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1739                         }
1740                         break;
1741                 case LATEX_ITEM_ENVIRONMENT:
1742                         if (desc_on == 1) break;
1743                         end_tag= "para";
1744                         lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1745                         break;
1746                 case LATEX_PARAGRAPH:
1747                         lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1748                         break;
1749                 default:
1750                         lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1751                         break;
1752                 }
1753         }
1754
1755         // Close open tags
1756         for (int d = depth; d >= 0; --d) {
1757                 if (!environment_stack[depth].empty()) {
1758                         if (environment_inner[depth] != "!-- --") {
1759                                 item_name = "listitem";
1760                                 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1761                                if (environment_inner[depth] == "varlistentry")
1762                                        lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1763                         }
1764
1765                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1766                 }
1767         }
1768
1769         return lines;
1770 }
1771
1772
1773 void InsetText::validate(LaTeXFeatures & features) const
1774 {
1775         for_each(paragraphs.begin(), paragraphs.end(),
1776                  boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1777 }
1778
1779
1780 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1781 {
1782         if (the_locking_inset) {
1783                 the_locking_inset->getCursorPos(bv, x, y);
1784                 return;
1785         }
1786         x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1787         y = cy(bv) - TEXT_TO_INSET_OFFSET;
1788 }
1789
1790
1791 int InsetText::insetInInsetY() const
1792 {
1793         if (!the_locking_inset)
1794                 return 0;
1795
1796         return (inset_y + the_locking_inset->insetInInsetY());
1797 }
1798
1799
1800 void InsetText::toggleInsetCursor(BufferView * bv)
1801 {
1802         if (the_locking_inset) {
1803                 the_locking_inset->toggleInsetCursor(bv);
1804                 return;
1805         }
1806
1807         LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1808
1809         int const asc = font_metrics::maxAscent(font);
1810         int const desc = font_metrics::maxDescent(font);
1811
1812         if (isCursorVisible())
1813                 bv->hideLockedInsetCursor();
1814         else
1815                 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1816         toggleCursorVisible();
1817 }
1818
1819
1820 void InsetText::showInsetCursor(BufferView * bv, bool show)
1821 {
1822         if (the_locking_inset) {
1823                 the_locking_inset->showInsetCursor(bv, show);
1824                 return;
1825         }
1826         if (!isCursorVisible()) {
1827                 LyXFont const font =
1828                         getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1829
1830                 int const asc = font_metrics::maxAscent(font);
1831                 int const desc = font_metrics::maxDescent(font);
1832
1833                 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1834                 if (show)
1835                         bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1836                 setCursorVisible(true);
1837         }
1838 }
1839
1840
1841 void InsetText::hideInsetCursor(BufferView * bv)
1842 {
1843         if (isCursorVisible()) {
1844                 bv->hideLockedInsetCursor();
1845                 setCursorVisible(false);
1846         }
1847         if (the_locking_inset)
1848                 the_locking_inset->hideInsetCursor(bv);
1849 }
1850
1851
1852 void InsetText::fitInsetCursor(BufferView * bv) const
1853 {
1854         if (the_locking_inset) {
1855                 the_locking_inset->fitInsetCursor(bv);
1856                 return;
1857         }
1858         LyXFont const font =
1859                 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1860
1861         int const asc = font_metrics::maxAscent(font);
1862         int const desc = font_metrics::maxDescent(font);
1863
1864         if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1865                 need_update |= FULL;
1866 }
1867
1868
1869 Inset::RESULT
1870 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1871 {
1872         if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1873                 return moveLeftIntern(bv, false, activate_inset, selecting);
1874         else
1875                 return moveRightIntern(bv, true, activate_inset, selecting);
1876 }
1877
1878
1879 Inset::RESULT
1880 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1881 {
1882         if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1883                 return moveRightIntern(bv, true, activate_inset, selecting);
1884         else
1885                 return moveLeftIntern(bv, false, activate_inset, selecting);
1886 }
1887
1888
1889 Inset::RESULT
1890 InsetText::moveRightIntern(BufferView * bv, bool front,
1891                            bool activate_inset, bool selecting)
1892 {
1893         if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1894                 return FINISHED_RIGHT;
1895         if (activate_inset && checkAndActivateInset(bv, front))
1896                 return DISPATCHED;
1897         getLyXText(bv)->cursorRight(bv);
1898         if (!selecting)
1899                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1900         return DISPATCHED_NOUPDATE;
1901 }
1902
1903
1904 Inset::RESULT
1905 InsetText::moveLeftIntern(BufferView * bv, bool front,
1906                           bool activate_inset, bool selecting)
1907 {
1908         if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1909                 return FINISHED;
1910         getLyXText(bv)->cursorLeft(bv);
1911         if (!selecting)
1912                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1913         if (activate_inset && checkAndActivateInset(bv, front))
1914                 return DISPATCHED;
1915         return DISPATCHED_NOUPDATE;
1916 }
1917
1918
1919 Inset::RESULT InsetText::moveUp(BufferView * bv)
1920 {
1921         if (!crow(bv)->previous())
1922                 return FINISHED_UP;
1923         getLyXText(bv)->cursorUp(bv);
1924         return DISPATCHED_NOUPDATE;
1925 }
1926
1927
1928 Inset::RESULT InsetText::moveDown(BufferView * bv)
1929 {
1930         if (!crow(bv)->next())
1931                 return FINISHED_DOWN;
1932         getLyXText(bv)->cursorDown(bv);
1933         return DISPATCHED_NOUPDATE;
1934 }
1935
1936
1937 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1938 {
1939         if (the_locking_inset) {
1940                 if (the_locking_inset->insetAllowed(inset))
1941                         return the_locking_inset->insertInset(bv, inset);
1942                 return false;
1943         }
1944         inset->setOwner(this);
1945         hideInsetCursor(bv);
1946         getLyXText(bv)->insertInset(bv, inset);
1947         bv->fitCursor();
1948         updateLocal(bv, CURSOR_PAR|CURSOR, true);
1949         return true;
1950 }
1951
1952
1953 bool InsetText::insetAllowed(Inset::Code code) const
1954 {
1955         // in_insetAllowed is a really gross hack,
1956         // to allow us to call the owner's insetAllowed
1957         // without stack overflow, which can happen
1958         // when the owner uses InsetCollapsable::insetAllowed()
1959         bool ret = true;
1960         if (in_insetAllowed)
1961                 return ret;
1962         in_insetAllowed = true;
1963         if (the_locking_inset)
1964                 ret = the_locking_inset->insetAllowed(code);
1965         else if (owner())
1966                 ret = owner()->insetAllowed(code);
1967         in_insetAllowed = false;
1968         return ret;
1969 }
1970
1971
1972 UpdatableInset * InsetText::getLockingInset() const
1973 {
1974         return the_locking_inset ? the_locking_inset->getLockingInset() :
1975                 const_cast<InsetText *>(this);
1976 }
1977
1978
1979 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1980 {
1981         if (c == lyxCode())
1982                 return this;
1983         if (the_locking_inset)
1984                 return the_locking_inset->getFirstLockingInsetOfType(c);
1985         return 0;
1986 }
1987
1988
1989 bool InsetText::showInsetDialog(BufferView * bv) const
1990 {
1991         if (the_locking_inset)
1992                 return the_locking_inset->showInsetDialog(bv);
1993         return false;
1994 }
1995
1996
1997 vector<string> const InsetText::getLabelList() const
1998 {
1999         vector<string> label_list;
2000
2001         ParagraphList::iterator pit = paragraphs.begin();
2002         ParagraphList::iterator pend = paragraphs.end();
2003         for (; pit != pend; ++pit) {
2004                 InsetList::iterator beg = pit->insetlist.begin();
2005                 InsetList::iterator end = pit->insetlist.end();
2006                 for (; beg != end; ++beg) {
2007                         vector<string> const l = beg.getInset()->getLabelList();
2008                         label_list.insert(label_list.end(), l.begin(), l.end());
2009                 }
2010         }
2011         return label_list;
2012 }
2013
2014
2015 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
2016                         bool selectall)
2017 {
2018         if (the_locking_inset) {
2019                 the_locking_inset->setFont(bv, font, toggleall, selectall);
2020                 return;
2021         }
2022         if ((!paragraphs.begin()->next() && paragraphs.begin()->empty()) || cpar(bv)->empty()) {
2023                 getLyXText(bv)->setFont(bv, font, toggleall);
2024                 return;
2025         }
2026         bool clear = false;
2027         if (!lt) {
2028                 lt = getLyXText(bv);
2029                 clear = true;
2030         }
2031         if (lt->selection.set()) {
2032                 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
2033         }
2034         if (selectall)
2035                 selectAll(bv);
2036         lt->toggleFree(bv, font, toggleall);
2037         if (selectall)
2038                 lt->clearSelection();
2039         bv->fitCursor();
2040         bool flag = (selectall || lt->selection.set());
2041         if (clear)
2042                 lt = 0;
2043         if (flag)
2044                 updateLocal(bv, FULL, true);
2045         else
2046                 updateLocal(bv, CURSOR_PAR, true);
2047 }
2048
2049
2050 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
2051 {
2052         if (cpar(bv)->isInset(cpos(bv))) {
2053                 Inset * inset =
2054                         static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
2055                 if (!isHighlyEditableInset(inset))
2056                         return false;
2057                 inset->edit(bv, front);
2058                 if (!the_locking_inset)
2059                         return false;
2060                 updateLocal(bv, CURSOR, false);
2061                 return true;
2062         }
2063         return false;
2064 }
2065
2066
2067 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2068                                       mouse_button::state button)
2069 {
2070         x -= drawTextXOffset;
2071         int dummyx = x;
2072         int dummyy = y + insetAscent;
2073         Inset * inset = getLyXText(bv)->checkInsetHit(bv, dummyx, dummyy);
2074         // we only do the edit() call if the inset was hit by the mouse
2075         // or if it is a highly editable inset. So we should call this
2076         // function from our own edit with button < 0.
2077         // FIXME: GUII jbl. I've changed this to ::none for now which is probably
2078         // WRONG
2079         if (button == mouse_button::none && !isHighlyEditableInset(inset))
2080                 return false;
2081
2082         if (inset) {
2083                 if (x < 0)
2084                         x = insetWidth;
2085                 if (y < 0)
2086                         y = insetDescent;
2087                 inset_x = cix(bv) - top_x + drawTextXOffset;
2088                 inset_y = ciy(bv) + drawTextYOffset;
2089                 inset->edit(bv, x - inset_x, y - inset_y, button);
2090                 if (!the_locking_inset)
2091                         return false;
2092                 updateLocal(bv, CURSOR, false);
2093                 return true;
2094         }
2095         return false;
2096 }
2097
2098
2099 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2100 {
2101 #if 0
2102         int w = UpdatableInset::getMaxWidth(bv, inset);
2103         if (w < 0) {
2104                 return -1;
2105         }
2106         if (owner()) {
2107                 w = w - top_x + owner()->x();
2108                 return w;
2109         }
2110         w -= (2 * TEXT_TO_INSET_OFFSET);
2111         return w - top_x;
2112 #else
2113         return UpdatableInset::getMaxWidth(bv, inset);
2114 #endif
2115 }
2116
2117
2118 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2119 {
2120         // we have to unlock any locked inset otherwise we're in troubles
2121         the_locking_inset = 0;
2122
2123         paragraphs.clear();
2124         paragraphs.set(new Paragraph(*p, same_id));
2125         paragraphs.begin()->setInsetOwner(this);
2126         Paragraph * np = &*(paragraphs.begin());
2127         while (p->next()) {
2128                 p = p->next();
2129                 np->next(new Paragraph(*p, same_id));
2130                 np->next()->previous(np);
2131                 np = np->next();
2132                 np->setInsetOwner(this);
2133         }
2134         reinitLyXText();
2135         need_update = INIT;
2136 }
2137
2138
2139 void InsetText::markNew(bool track_changes)
2140 {
2141         ParagraphList::iterator pit = paragraphs.begin();
2142         ParagraphList::iterator pend = paragraphs.end();
2143         for (; pit != pend; ++pit) {
2144                 if (track_changes) {
2145                         pit->trackChanges();
2146                 } else {
2147                         // no-op when not tracking
2148                         pit->cleanChanges();
2149                 }
2150         }
2151 }
2152
2153  
2154 void InsetText::setText(string const & data, LyXFont const & font)
2155 {
2156         clear(false);
2157         for (unsigned int i = 0; i < data.length(); ++i)
2158                 paragraphs.begin()->insertChar(i, data[i], font);
2159         reinitLyXText();
2160 }
2161
2162
2163 void InsetText::setAutoBreakRows(bool flag)
2164 {
2165         if (flag != autoBreakRows) {
2166                 autoBreakRows = flag;
2167                 if (!flag)
2168                         removeNewlines();
2169                 need_update = INIT;
2170         }
2171 }
2172
2173
2174 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2175 {
2176         if (how != drawFrame_) {
2177                 drawFrame_ = how;
2178                 if (bv)
2179                         updateLocal(bv, DRAW_FRAME, false);
2180         }
2181 }
2182
2183
2184 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2185 {
2186         if (frame_color != col) {
2187                 frame_color = col;
2188                 if (bv)
2189                         updateLocal(bv, DRAW_FRAME, false);
2190         }
2191 }
2192
2193
2194 int InsetText::cx(BufferView * bv) const
2195 {
2196         // we do nothing dangerous so we use a local cache
2197         LyXText * llt = getLyXText(bv);
2198         int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2199         if (the_locking_inset) {
2200                 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2201                                             llt->cursor.pos());
2202                 if (font.isVisibleRightToLeft())
2203                         x -= the_locking_inset->width(bv, font);
2204         }
2205         return x;
2206 }
2207
2208
2209 int InsetText::cix(BufferView * bv) const
2210 {
2211         // we do nothing dangerous so we use a local cache
2212         LyXText * llt = getLyXText(bv);
2213         int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2214         if (the_locking_inset) {
2215                 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2216                                             llt->cursor.pos());
2217                 if (font.isVisibleRightToLeft())
2218                         x -= the_locking_inset->width(bv, font);
2219         }
2220         return x;
2221 }
2222
2223
2224 int InsetText::cy(BufferView * bv) const
2225 {
2226         LyXFont font;
2227         return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2228 }
2229
2230
2231 int InsetText::ciy(BufferView * bv) const
2232 {
2233         LyXFont font;
2234         return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2235 }
2236
2237
2238 pos_type InsetText::cpos(BufferView * bv) const
2239 {
2240         return getLyXText(bv)->cursor.pos();
2241 }
2242
2243
2244 Paragraph * InsetText::cpar(BufferView * bv) const
2245 {
2246         return getLyXText(bv)->cursor.par();
2247 }
2248
2249
2250 bool InsetText::cboundary(BufferView * bv) const
2251 {
2252         return getLyXText(bv)->cursor.boundary();
2253 }
2254
2255
2256 Row * InsetText::crow(BufferView * bv) const
2257 {
2258         return getLyXText(bv)->cursor.row();
2259 }
2260
2261
2262 LyXText * InsetText::getLyXText(BufferView const * lbv,
2263                                 bool const recursive) const
2264 {
2265         if (cached_bview == lbv) {
2266                 if (recursive && the_locking_inset)
2267                         return the_locking_inset->getLyXText(lbv, true);
2268                 LyXText * lt = cached_text.get();
2269                 lyx::Assert(lt && lt->firstRow()->par() == &*(paragraphs.begin()));
2270                 return lt;
2271         }
2272         // Super UGLY! (Lgb)
2273         BufferView * bv = const_cast<BufferView *>(lbv);
2274
2275         cached_bview = bv;
2276         Cache::iterator it = cache.find(bv);
2277
2278         if (it != cache.end()) {
2279                 if (do_reinit) {
2280                         reinitLyXText();
2281                 } else if (do_resize) {
2282                         resizeLyXText(do_resize);
2283                 } else {
2284                         if (lt || !it->second.remove) {
2285                                 lyx::Assert(it->second.text.get());
2286                                 cached_text = it->second.text;
2287                                 if (recursive && the_locking_inset) {
2288                                         return the_locking_inset->getLyXText(bv, true);
2289                                 }
2290                                 return cached_text.get();
2291                         } else if (it->second.remove) {
2292                                 if (locked) {
2293                                         saveLyXTextState(it->second.text.get());
2294                                 } else {
2295                                         sstate.lpar = 0;
2296                                 }
2297                         }
2298                         //
2299                         // when we have to reinit the existing LyXText!
2300                         //
2301                         it->second.text->init(bv);
2302                         restoreLyXTextState(bv, it->second.text.get());
2303                         it->second.remove = false;
2304                 }
2305                 cached_text = it->second.text;
2306                 if (the_locking_inset && recursive) {
2307                         return the_locking_inset->getLyXText(bv);
2308                 }
2309                 return cached_text.get();
2310         }
2311         ///
2312         // we are here only if we don't have a BufferView * in the cache!!!
2313         ///
2314         cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2315         cached_text->init(bv);
2316         restoreLyXTextState(bv, cached_text.get());
2317
2318         cache.insert(make_pair(bv, cached_text));
2319
2320         if (the_locking_inset && recursive) {
2321                 return the_locking_inset->getLyXText(bv);
2322         }
2323         return cached_text.get();
2324 }
2325
2326
2327 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2328 {
2329         cached_bview = 0;
2330
2331         Cache::iterator it = cache.find(bv);
2332
2333         if (it == cache.end()) {
2334                 return;
2335         }
2336
2337         lyx::Assert(it->second.text.get());
2338
2339         it->second.remove = true;
2340         if (recursive) {
2341                 /// then remove all LyXText in text-insets
2342                 for_each(paragraphs.begin(), paragraphs.end(),
2343                          boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2344         }
2345 }
2346
2347
2348 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2349 {
2350         if (lt) {
2351                 // we cannot resize this because we are in use!
2352                 // so do this on the next possible getLyXText()
2353                 do_resize = bv;
2354                 return;
2355         }
2356         do_resize = 0;
2357 //      lyxerr << "InsetText::resizeLyXText\n";
2358         if (!paragraphs.begin()->next() && paragraphs.begin()->empty()) { // no data, resize not neccessary!
2359                 // we have to do this as a fixed width may have changed!
2360                 LyXText * t = getLyXText(bv);
2361                 saveLyXTextState(t);
2362                 t->init(bv, true);
2363                 restoreLyXTextState(bv, t);
2364                 return;
2365         }
2366         // one endless line, resize normally not necessary
2367         if (!force && getMaxWidth(bv, this) < 0)
2368                 return;
2369
2370         Cache::iterator it = cache.find(bv);
2371         if (it == cache.end()) {
2372                 return;
2373         }
2374         lyx::Assert(it->second.text.get());
2375
2376         LyXText * t = it->second.text.get();
2377         saveLyXTextState(t);
2378
2379         for_each(paragraphs.begin(), paragraphs.end(),
2380                  boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2381
2382         t->init(bv, true);
2383         restoreLyXTextState(bv, t);
2384         if (the_locking_inset) {
2385                 inset_x = cix(bv) - top_x + drawTextXOffset;
2386                 inset_y = ciy(bv) + drawTextYOffset;
2387         }
2388
2389         t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2390         if (!owner()) {
2391                 updateLocal(bv, FULL, false);
2392                 // this will scroll the screen such that the cursor becomes visible
2393                 bv->updateScrollbar();
2394         } else {
2395                 need_update |= FULL;
2396         }
2397 }
2398
2399
2400 void InsetText::reinitLyXText() const
2401 {
2402         if (lt) {
2403                 // we cannot resize this because we are in use!
2404                 // so do this on the next possible getLyXText()
2405                 do_reinit = true;
2406                 return;
2407         }
2408         do_reinit = false;
2409         do_resize = 0;
2410 //      lyxerr << "InsetText::reinitLyXText\n";
2411         for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2412                 lyx::Assert(it->second.text.get());
2413
2414                 LyXText * t = it->second.text.get();
2415                 BufferView * bv = it->first;
2416
2417                 saveLyXTextState(t);
2418
2419                 for_each(paragraphs.begin(), paragraphs.end(),
2420                          boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2421
2422                 t->init(bv, true);
2423                 restoreLyXTextState(bv, t);
2424                 if (the_locking_inset) {
2425                         inset_x = cix(bv) - top_x + drawTextXOffset;
2426                         inset_y = ciy(bv) + drawTextYOffset;
2427                 }
2428                 t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2429                 if (!owner()) {
2430                         updateLocal(bv, FULL, false);
2431                         // this will scroll the screen such that the cursor becomes visible
2432                         bv->updateScrollbar();
2433                 } else {
2434                         need_update = FULL;
2435                 }
2436         }
2437 }
2438
2439
2440 void InsetText::removeNewlines()
2441 {
2442         bool changed = false;
2443
2444         ParagraphList::iterator it = paragraphs.begin();
2445         ParagraphList::iterator end = paragraphs.end();
2446         for (; it != end; ++it) {
2447                 for (int i = 0; i < it->size(); ++i) {
2448                         if (it->getChar(i) == Paragraph::META_NEWLINE) {
2449                                 changed = true;
2450                                 it->erase(i);
2451                         }
2452                 }
2453         }
2454         if (changed)
2455                 reinitLyXText();
2456 }
2457
2458
2459 bool InsetText::nodraw() const
2460 {
2461         if (the_locking_inset)
2462                 return the_locking_inset->nodraw();
2463         return UpdatableInset::nodraw();
2464 }
2465
2466
2467 int InsetText::scroll(bool recursive) const
2468 {
2469         int sx = UpdatableInset::scroll(false);
2470
2471         if (recursive && the_locking_inset)
2472                 sx += the_locking_inset->scroll(recursive);
2473
2474         return sx;
2475 }
2476
2477
2478 bool InsetText::doClearArea() const
2479 {
2480         return !locked || (need_update & (FULL|INIT));
2481 }
2482
2483
2484 void InsetText::selectAll(BufferView * bv)
2485 {
2486         getLyXText(bv)->cursorTop(bv);
2487         getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2488         getLyXText(bv)->cursorBottom(bv);
2489         getLyXText(bv)->setSelection(bv);
2490 }
2491
2492
2493 void InsetText::clearSelection(BufferView * bv)
2494 {
2495         getLyXText(bv)->clearSelection();
2496 }
2497
2498
2499 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2500 {
2501         Painter & pain = bv->painter();
2502         int w = insetWidth;
2503         int h = insetAscent + insetDescent;
2504         int ty = baseline - insetAscent;
2505
2506         if (ty < 0) {
2507                 h += ty;
2508                 ty = 0;
2509         }
2510         if ((ty + h) > pain.paperHeight())
2511                 h = pain.paperHeight();
2512         if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2513                 w = pain.paperWidth();
2514 //      w -= TEXT_TO_INSET_OFFSET;
2515         pain.fillRectangle(top_x + 1, ty + 1, w - 1, h - 1, backgroundColor());
2516         cleared = true;
2517         need_update = FULL;
2518         frame_is_visible = false;
2519 }
2520
2521
2522 Paragraph * InsetText::firstParagraph() const
2523 {
2524         Paragraph * result;
2525         if (the_locking_inset)
2526                 if ((result = the_locking_inset->firstParagraph()))
2527                         return result;
2528         return &*(paragraphs.begin());
2529 }
2530
2531
2532 Paragraph * InsetText::getFirstParagraph(int i) const
2533 {
2534         return (i == 0) ? &*(paragraphs.begin()) : 0;
2535 }
2536
2537
2538 LyXCursor const & InsetText::cursor(BufferView * bv) const
2539 {
2540         if (the_locking_inset)
2541                 return the_locking_inset->cursor(bv);
2542         return getLyXText(bv)->cursor;
2543 }
2544
2545
2546 Paragraph * InsetText::paragraph() const
2547 {
2548         return &*(paragraphs.begin());
2549 }
2550
2551
2552 void InsetText::paragraph(Paragraph * p)
2553 {
2554         // GENERAL COMMENT: We don't have to free the old paragraphs as the
2555         // caller of this function has to take care of it. This IS important
2556         // as we could have to insert a paragraph before this one and just
2557         // link the actual to a new ones next and set it with this function
2558         // and are done!
2559         paragraphs.set(p);
2560         // set ourself as owner for all the paragraphs inserted!
2561         for_each(paragraphs.begin(), paragraphs.end(),
2562                  boost::bind(&Paragraph::setInsetOwner, _1, this));
2563
2564         reinitLyXText();
2565         // redraw myself when asked for
2566         need_update = INIT;
2567 }
2568
2569
2570 Inset * InsetText::getInsetFromID(int id_arg) const
2571 {
2572         if (id_arg == id())
2573                 return const_cast<InsetText *>(this);
2574
2575         ParagraphList::iterator pit = paragraphs.begin();
2576         ParagraphList::iterator pend = paragraphs.end();
2577         for (; pit != pend; ++pit) {
2578                 InsetList::iterator it = pit->insetlist.begin();
2579                 InsetList::iterator end = pit->insetlist.end();
2580                 for (; it != end; ++it) {
2581                         if (it.getInset()->id() == id_arg)
2582                                 return it.getInset();
2583                         Inset * in = it.getInset()->getInsetFromID(id_arg);
2584                         if (in)
2585                                 return in;
2586                 }
2587         }
2588         return 0;
2589 }
2590
2591
2592 WordLangTuple const
2593 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2594                                       float & value) const
2595 {
2596         bool clear = false;
2597         WordLangTuple word;
2598
2599         if (!lt) {
2600                 lt = getLyXText(bv);
2601                 clear = true;
2602         }
2603         if (the_locking_inset) {
2604                 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2605                 if (!word.word().empty()) {
2606                         value += cy(bv);
2607                         if (clear)
2608                                 lt = 0;
2609                         return word;
2610                 }
2611                 // we have to go on checking so move cursor to the next char
2612                 lt->cursor.pos(lt->cursor.pos() + 1);
2613         }
2614         word = lt->selectNextWordToSpellcheck(bv, value);
2615         if (word.word().empty())
2616                 bv->unlockInset(const_cast<InsetText *>(this));
2617         else
2618                 value = cy(bv);
2619         if (clear)
2620                 lt = 0;
2621         return word;
2622 }
2623
2624
2625 void InsetText::selectSelectedWord(BufferView * bv)
2626 {
2627         if (the_locking_inset) {
2628                 the_locking_inset->selectSelectedWord(bv);
2629                 return;
2630         }
2631         getLyXText(bv)->selectSelectedWord(bv);
2632         updateLocal(bv, SELECTION, false);
2633 }
2634
2635
2636 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2637 {
2638         if (the_locking_inset) {
2639                 the_locking_inset->toggleSelection(bv, kill_selection);
2640         }
2641         bool clear = false;
2642         if (!lt) {
2643                 lt = getLyXText(bv);
2644                 clear = true;
2645         }
2646
2647         int x = top_x + TEXT_TO_INSET_OFFSET;
2648
2649         Row * row = lt->firstRow();
2650         int y_offset = top_baseline - row->ascent_of_text();
2651         int y = y_offset;
2652         while ((row != 0) && ((y+row->height()) <= 0)) {
2653                 y += row->height();
2654                 row = row->next();
2655         }
2656         if (y_offset < 0)
2657                 y_offset = y;
2658
2659         if (need_update & SELECTION)
2660                 need_update = NONE;
2661         bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2662         if (clear)
2663                 lt = 0;
2664 }
2665
2666
2667 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2668 {
2669         bool clear = false;
2670         if (!lt) {
2671                 lt = getLyXText(bv);
2672                 clear = true;
2673         }
2674         if (the_locking_inset) {
2675                 if (the_locking_inset->nextChange(bv, length))
2676                         return true;
2677                 lt->cursorRight(bv, true);
2678         }
2679         lyxfind::SearchResult result =
2680                 lyxfind::findNextChange(bv, lt, length);
2681
2682         if (result == lyxfind::SR_FOUND) {
2683                 LyXCursor cur = lt->cursor;
2684                 bv->unlockInset(bv->theLockingInset());
2685                 if (bv->lockInset(this))
2686                         locked = true;
2687                 lt->cursor = cur;
2688                 lt->setSelectionRange(bv, length);
2689                 updateLocal(bv, SELECTION, false);
2690         }
2691         if (clear)
2692                 lt = 0;
2693         return result != lyxfind::SR_NOT_FOUND;
2694 }
2695
2696  
2697 bool InsetText::searchForward(BufferView * bv, string const & str,
2698                               bool cs, bool mw)
2699 {
2700         bool clear = false;
2701         if (!lt) {
2702                 lt = getLyXText(bv);
2703                 clear = true;
2704         }
2705         if (the_locking_inset) {
2706                 if (the_locking_inset->searchForward(bv, str, cs, mw))
2707                         return true;
2708                 lt->cursorRight(bv, true);
2709         }
2710         lyxfind::SearchResult result =
2711                 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2712
2713         if (result == lyxfind::SR_FOUND) {
2714                 LyXCursor cur = lt->cursor;
2715                 bv->unlockInset(bv->theLockingInset());
2716                 if (bv->lockInset(this))
2717                         locked = true;
2718                 lt->cursor = cur;
2719                 lt->setSelectionRange(bv, str.length());
2720                 updateLocal(bv, SELECTION, false);
2721         }
2722         if (clear)
2723                 lt = 0;
2724         return (result != lyxfind::SR_NOT_FOUND);
2725 }
2726
2727 bool InsetText::searchBackward(BufferView * bv, string const & str,
2728                                bool cs, bool mw)
2729 {
2730         if (the_locking_inset) {
2731                 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2732                         return true;
2733         }
2734         bool clear = false;
2735         if (!lt) {
2736                 lt = getLyXText(bv);
2737                 clear = true;
2738         }
2739         if (!locked) {
2740                 Paragraph * p = &*(paragraphs.begin());
2741                 while (p->next())
2742                         p = p->next();
2743                 lt->setCursor(bv, p, p->size());
2744         }
2745         lyxfind::SearchResult result =
2746                 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2747
2748         if (result == lyxfind::SR_FOUND) {
2749                 LyXCursor cur = lt->cursor;
2750                 bv->unlockInset(bv->theLockingInset());
2751                 if (bv->lockInset(this))
2752                         locked = true;
2753                 lt->cursor = cur;
2754                 lt->setSelectionRange(bv, str.length());
2755                 updateLocal(bv, SELECTION, false);
2756         }
2757         if (clear)
2758                 lt = 0;
2759         return (result != lyxfind::SR_NOT_FOUND);
2760 }
2761
2762
2763 bool InsetText::checkInsertChar(LyXFont & font)
2764 {
2765         if (owner())
2766                 return owner()->checkInsertChar(font);
2767         return true;
2768 }
2769
2770
2771 void InsetText::collapseParagraphs(BufferView * bv) const
2772 {
2773         BufferParams const & bparams = bv->buffer()->params;
2774         LyXText * llt = getLyXText(bv);
2775
2776         while (paragraphs.begin()->next()) {
2777                 if (!paragraphs.begin()->empty() && !paragraphs.begin()->next()->empty() &&
2778                         !paragraphs.begin()->isSeparator(paragraphs.begin()->size() - 1))
2779                 {
2780                         paragraphs.begin()->insertChar(paragraphs.begin()->size(), ' ');
2781                 }
2782                 if (llt->selection.set()) {
2783                         if (llt->selection.start.par() == paragraphs.begin()->next()) {
2784                                 llt->selection.start.par(&*(paragraphs.begin()));
2785                                 llt->selection.start.pos(
2786                                         llt->selection.start.pos() + paragraphs.begin()->size());
2787                         }
2788                         if (llt->selection.end.par() == paragraphs.begin()->next()) {
2789                                 llt->selection.end.par(&*(paragraphs.begin()));
2790                                 llt->selection.end.pos(
2791                                         llt->selection.end.pos() + paragraphs.begin()->size());
2792                         }
2793                 }
2794                 mergeParagraph(bparams, &*paragraphs.begin());
2795         }
2796         reinitLyXText();
2797 }
2798
2799
2800 void InsetText::getDrawFont(LyXFont & font) const
2801 {
2802         if (!owner())
2803                 return;
2804         owner()->getDrawFont(font);
2805 }
2806
2807
2808 void InsetText::appendParagraphs(BufferParams const & bparams,
2809                                  Paragraph * newpar)
2810 {
2811         Paragraph * buf;
2812         Paragraph * tmpbuf = newpar;
2813         Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2814         if (bparams.tracking_changes)
2815                 buf->cleanChanges();
2816
2817         while (tmpbuf->next()) {
2818                 tmpbuf = tmpbuf->next();
2819                 lastbuffer->next(new Paragraph(*tmpbuf, false));
2820                 lastbuffer->next()->previous(lastbuffer);
2821                 lastbuffer = lastbuffer->next();
2822                 if (bparams.tracking_changes)
2823                         lastbuffer->cleanChanges();
2824         }
2825         lastbuffer = &*(paragraphs.begin());
2826         while (lastbuffer->next())
2827                 lastbuffer = lastbuffer->next();
2828         if (!newpar->empty() && !lastbuffer->empty() &&
2829                 !lastbuffer->isSeparator(lastbuffer->size() - 1))
2830         {
2831                 lastbuffer->insertChar(lastbuffer->size(), ' ');
2832         }
2833
2834         // make the buf exactly the same layout than our last paragraph
2835         buf->makeSameLayout(lastbuffer);
2836
2837         // paste it!
2838         lastbuffer->next(buf);
2839         buf->previous(lastbuffer);
2840         mergeParagraph(bparams, lastbuffer);
2841
2842         reinitLyXText();
2843 }
2844
2845
2846 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2847 {
2848         Paragraph * par = getFirstParagraph(0);
2849         while (par) {
2850                 InsetList::iterator it  = par->insetlist.begin();
2851                 InsetList::iterator end = par->insetlist.end();
2852                 for (; it != end; ++it) {
2853                         it.getInset()->addPreview(loader);
2854                 }
2855
2856                 par = par->next();
2857         }
2858 }