]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
43d6badf464b8e4809db50be4bfce7a8646c5ca4
[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::depth_type depth = 0;
266         LyXFont font(LyXFont::ALL_INHERIT);
267
268         clear(false);
269
270         if (buf->params.tracking_changes)
271                 paragraphs.begin()->trackChanges();
272
273         // delete the initial paragraph
274         paragraphs.clear();
275         ParagraphList::iterator pit = paragraphs.begin();
276
277         while (lex.isOK()) {
278                 lex.nextToken();
279                 token = lex.getString();
280                 if (token.empty())
281                         continue;
282                 if (token == "\\end_inset") {
283                         break;
284                 }
285
286                 if (token == "\\the_end") {
287                         lex.printError("\\the_end read in inset! Error in document!");
288                         return;
289                 }
290
291                 // FIXME: ugly.
292
293                 const_cast<Buffer*>(buf)->readToken(lex, paragraphs, pit,
294                                                     token, pos, depth, font);
295         }
296
297         pit = paragraphs.begin();
298         ParagraphList::iterator const end = paragraphs.end();
299         for (; pit != end; ++pit)
300                 pit->setInsetOwner(this);
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->top_y(-y_offset);
462                 first = y;
463                 y_offset = 0;
464         } else {
465                 lt->top_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->top_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)
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)->top_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_top_y = lt->top_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)->top_y() != old_top_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)->top_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, paragraphs, 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, paragraphs, 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         latexParagraphs(buf, paragraphs,
1599                         paragraphs.begin(), paragraphs.end(),
1600                         os, texrow, moving_arg);
1601         return texrow.rows();
1602 }
1603
1604
1605 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1606 {
1607         unsigned int lines = 0;
1608
1609         ParagraphList::iterator beg = paragraphs.begin();
1610         ParagraphList::iterator end = paragraphs.end();
1611         ParagraphList::iterator it = beg;
1612         for (; it != end; ++it) {
1613                 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1614                 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1615                 os << tmp;
1616         }
1617         return lines;
1618 }
1619
1620
1621 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1622 {
1623         Paragraph * p = &*(paragraphs.begin());
1624         unsigned int lines = 0;
1625
1626         vector<string> environment_stack(10);
1627         vector<string> environment_inner(10);
1628
1629         int const command_depth = 0;
1630         string item_name;
1631
1632         Paragraph::depth_type depth = 0; // paragraph depth
1633
1634         while (p) {
1635                 string sgmlparam;
1636                 int desc_on = 0; // description mode
1637
1638                 LyXLayout_ptr const & style = p->layout();
1639
1640                 // environment tag closing
1641                 for (; depth > p->params().depth(); --depth) {
1642                         if (environment_inner[depth] != "!-- --") {
1643                                 item_name = "listitem";
1644                                 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1645                                 if (environment_inner[depth] == "varlistentry")
1646                                         lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1647                         }
1648                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1649                         environment_stack[depth].erase();
1650                         environment_inner[depth].erase();
1651                 }
1652
1653                 if (depth == p->params().depth()
1654                    && environment_stack[depth] != style->latexname()
1655                    && !environment_stack[depth].empty()) {
1656                         if (environment_inner[depth] != "!-- --") {
1657                                 item_name= "listitem";
1658                                 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1659                                 if (environment_inner[depth] == "varlistentry")
1660                                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1661                         }
1662
1663                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1664
1665                         environment_stack[depth].erase();
1666                         environment_inner[depth].erase();
1667                 }
1668
1669                 // Write opening SGML tags.
1670                 switch (style->latextype) {
1671                 case LATEX_PARAGRAPH:
1672                         lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1673                         break;
1674
1675                 case LATEX_COMMAND:
1676                         buf->sgmlError(p, 0,  _("Error: LatexType Command not allowed here.\n"));
1677                         return -1;
1678                         break;
1679
1680                 case LATEX_ENVIRONMENT:
1681                 case LATEX_ITEM_ENVIRONMENT:
1682                         if (depth < p->params().depth()) {
1683                                 depth = p->params().depth();
1684                                 environment_stack[depth].erase();
1685                         }
1686
1687                         if (environment_stack[depth] != style->latexname()) {
1688                                 if (environment_stack.size() == depth + 1) {
1689                                         environment_stack.push_back("!-- --");
1690                                         environment_inner.push_back("!-- --");
1691                                 }
1692                                 environment_stack[depth] = style->latexname();
1693                                 environment_inner[depth] = "!-- --";
1694                                 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1695                         } else {
1696                                 if (environment_inner[depth] != "!-- --") {
1697                                         item_name= "listitem";
1698                                         lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1699                                         if (environment_inner[depth] == "varlistentry")
1700                                                 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1701                                 }
1702                         }
1703
1704                         if (style->latextype == LATEX_ENVIRONMENT) {
1705                                 if (!style->latexparam().empty()) {
1706                                         if (style->latexparam() == "CDATA")
1707                                                 os << "<![CDATA[";
1708                                         else
1709                                           lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1710                                 }
1711                                 break;
1712                         }
1713
1714                         desc_on = (style->labeltype == LABEL_MANUAL);
1715
1716                         environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1717                         lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1718
1719                         item_name = desc_on ? "term" : "para";
1720                         lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1721
1722                         break;
1723                 default:
1724                         lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1725                         break;
1726                 }
1727
1728                 buf->simpleDocBookOnePar(os, p, desc_on, depth + 1 + command_depth);
1729                 p = p->next();
1730
1731                 string end_tag;
1732                 // write closing SGML tags
1733                 switch (style->latextype) {
1734                 case LATEX_ENVIRONMENT:
1735                         if (!style->latexparam().empty()) {
1736                                 if (style->latexparam() == "CDATA")
1737                                         os << "]]>";
1738                                 else
1739                                         lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1740                         }
1741                         break;
1742                 case LATEX_ITEM_ENVIRONMENT:
1743                         if (desc_on == 1) break;
1744                         end_tag= "para";
1745                         lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1746                         break;
1747                 case LATEX_PARAGRAPH:
1748                         lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1749                         break;
1750                 default:
1751                         lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1752                         break;
1753                 }
1754         }
1755
1756         // Close open tags
1757         for (int d = depth; d >= 0; --d) {
1758                 if (!environment_stack[depth].empty()) {
1759                         if (environment_inner[depth] != "!-- --") {
1760                                 item_name = "listitem";
1761                                 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1762                                if (environment_inner[depth] == "varlistentry")
1763                                        lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1764                         }
1765
1766                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1767                 }
1768         }
1769
1770         return lines;
1771 }
1772
1773
1774 void InsetText::validate(LaTeXFeatures & features) const
1775 {
1776         for_each(paragraphs.begin(), paragraphs.end(),
1777                  boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1778 }
1779
1780
1781 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1782 {
1783         if (the_locking_inset) {
1784                 the_locking_inset->getCursorPos(bv, x, y);
1785                 return;
1786         }
1787         x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1788         y = cy(bv) - TEXT_TO_INSET_OFFSET;
1789 }
1790
1791
1792 int InsetText::insetInInsetY() const
1793 {
1794         if (!the_locking_inset)
1795                 return 0;
1796
1797         return (inset_y + the_locking_inset->insetInInsetY());
1798 }
1799
1800
1801 void InsetText::toggleInsetCursor(BufferView * bv)
1802 {
1803         if (the_locking_inset) {
1804                 the_locking_inset->toggleInsetCursor(bv);
1805                 return;
1806         }
1807
1808         LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1809
1810         int const asc = font_metrics::maxAscent(font);
1811         int const desc = font_metrics::maxDescent(font);
1812
1813         if (isCursorVisible())
1814                 bv->hideLockedInsetCursor();
1815         else
1816                 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1817         toggleCursorVisible();
1818 }
1819
1820
1821 void InsetText::showInsetCursor(BufferView * bv, bool show)
1822 {
1823         if (the_locking_inset) {
1824                 the_locking_inset->showInsetCursor(bv, show);
1825                 return;
1826         }
1827         if (!isCursorVisible()) {
1828                 LyXFont const font =
1829                         getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1830
1831                 int const asc = font_metrics::maxAscent(font);
1832                 int const desc = font_metrics::maxDescent(font);
1833
1834                 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1835                 if (show)
1836                         bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1837                 setCursorVisible(true);
1838         }
1839 }
1840
1841
1842 void InsetText::hideInsetCursor(BufferView * bv)
1843 {
1844         if (isCursorVisible()) {
1845                 bv->hideLockedInsetCursor();
1846                 setCursorVisible(false);
1847         }
1848         if (the_locking_inset)
1849                 the_locking_inset->hideInsetCursor(bv);
1850 }
1851
1852
1853 void InsetText::fitInsetCursor(BufferView * bv) const
1854 {
1855         if (the_locking_inset) {
1856                 the_locking_inset->fitInsetCursor(bv);
1857                 return;
1858         }
1859         LyXFont const font =
1860                 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1861
1862         int const asc = font_metrics::maxAscent(font);
1863         int const desc = font_metrics::maxDescent(font);
1864
1865         if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1866                 need_update |= FULL;
1867 }
1868
1869
1870 Inset::RESULT
1871 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1872 {
1873         if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1874                 return moveLeftIntern(bv, false, activate_inset, selecting);
1875         else
1876                 return moveRightIntern(bv, true, activate_inset, selecting);
1877 }
1878
1879
1880 Inset::RESULT
1881 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1882 {
1883         if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1884                 return moveRightIntern(bv, true, activate_inset, selecting);
1885         else
1886                 return moveLeftIntern(bv, false, activate_inset, selecting);
1887 }
1888
1889
1890 Inset::RESULT
1891 InsetText::moveRightIntern(BufferView * bv, bool front,
1892                            bool activate_inset, bool selecting)
1893 {
1894         if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1895                 return FINISHED_RIGHT;
1896         if (activate_inset && checkAndActivateInset(bv, front))
1897                 return DISPATCHED;
1898         getLyXText(bv)->cursorRight(bv);
1899         if (!selecting)
1900                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1901         return DISPATCHED_NOUPDATE;
1902 }
1903
1904
1905 Inset::RESULT
1906 InsetText::moveLeftIntern(BufferView * bv, bool front,
1907                           bool activate_inset, bool selecting)
1908 {
1909         if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1910                 return FINISHED;
1911         getLyXText(bv)->cursorLeft(bv);
1912         if (!selecting)
1913                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1914         if (activate_inset && checkAndActivateInset(bv, front))
1915                 return DISPATCHED;
1916         return DISPATCHED_NOUPDATE;
1917 }
1918
1919
1920 Inset::RESULT InsetText::moveUp(BufferView * bv)
1921 {
1922         if (!crow(bv)->previous())
1923                 return FINISHED_UP;
1924         getLyXText(bv)->cursorUp(bv);
1925         return DISPATCHED_NOUPDATE;
1926 }
1927
1928
1929 Inset::RESULT InsetText::moveDown(BufferView * bv)
1930 {
1931         if (!crow(bv)->next())
1932                 return FINISHED_DOWN;
1933         getLyXText(bv)->cursorDown(bv);
1934         return DISPATCHED_NOUPDATE;
1935 }
1936
1937
1938 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1939 {
1940         if (the_locking_inset) {
1941                 if (the_locking_inset->insetAllowed(inset))
1942                         return the_locking_inset->insertInset(bv, inset);
1943                 return false;
1944         }
1945         inset->setOwner(this);
1946         hideInsetCursor(bv);
1947         getLyXText(bv)->insertInset(bv, inset);
1948         bv->fitCursor();
1949         updateLocal(bv, CURSOR_PAR|CURSOR, true);
1950         return true;
1951 }
1952
1953
1954 bool InsetText::insetAllowed(Inset::Code code) const
1955 {
1956         // in_insetAllowed is a really gross hack,
1957         // to allow us to call the owner's insetAllowed
1958         // without stack overflow, which can happen
1959         // when the owner uses InsetCollapsable::insetAllowed()
1960         bool ret = true;
1961         if (in_insetAllowed)
1962                 return ret;
1963         in_insetAllowed = true;
1964         if (the_locking_inset)
1965                 ret = the_locking_inset->insetAllowed(code);
1966         else if (owner())
1967                 ret = owner()->insetAllowed(code);
1968         in_insetAllowed = false;
1969         return ret;
1970 }
1971
1972
1973 UpdatableInset * InsetText::getLockingInset() const
1974 {
1975         return the_locking_inset ? the_locking_inset->getLockingInset() :
1976                 const_cast<InsetText *>(this);
1977 }
1978
1979
1980 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1981 {
1982         if (c == lyxCode())
1983                 return this;
1984         if (the_locking_inset)
1985                 return the_locking_inset->getFirstLockingInsetOfType(c);
1986         return 0;
1987 }
1988
1989
1990 bool InsetText::showInsetDialog(BufferView * bv) const
1991 {
1992         if (the_locking_inset)
1993                 return the_locking_inset->showInsetDialog(bv);
1994         return false;
1995 }
1996
1997
1998 vector<string> const InsetText::getLabelList() const
1999 {
2000         vector<string> label_list;
2001
2002         ParagraphList::iterator pit = paragraphs.begin();
2003         ParagraphList::iterator pend = paragraphs.end();
2004         for (; pit != pend; ++pit) {
2005                 InsetList::iterator beg = pit->insetlist.begin();
2006                 InsetList::iterator end = pit->insetlist.end();
2007                 for (; beg != end; ++beg) {
2008                         vector<string> const l = beg.getInset()->getLabelList();
2009                         label_list.insert(label_list.end(), l.begin(), l.end());
2010                 }
2011         }
2012         return label_list;
2013 }
2014
2015
2016 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
2017                         bool selectall)
2018 {
2019         if (the_locking_inset) {
2020                 the_locking_inset->setFont(bv, font, toggleall, selectall);
2021                 return;
2022         }
2023         if ((!paragraphs.begin()->next() && paragraphs.begin()->empty()) || cpar(bv)->empty()) {
2024                 getLyXText(bv)->setFont(bv, font, toggleall);
2025                 return;
2026         }
2027         bool clear = false;
2028         if (!lt) {
2029                 lt = getLyXText(bv);
2030                 clear = true;
2031         }
2032         if (lt->selection.set()) {
2033                 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
2034         }
2035         if (selectall)
2036                 selectAll(bv);
2037         lt->toggleFree(bv, font, toggleall);
2038         if (selectall)
2039                 lt->clearSelection();
2040         bv->fitCursor();
2041         bool flag = (selectall || lt->selection.set());
2042         if (clear)
2043                 lt = 0;
2044         if (flag)
2045                 updateLocal(bv, FULL, true);
2046         else
2047                 updateLocal(bv, CURSOR_PAR, true);
2048 }
2049
2050
2051 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
2052 {
2053         if (cpar(bv)->isInset(cpos(bv))) {
2054                 Inset * inset =
2055                         static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
2056                 if (!isHighlyEditableInset(inset))
2057                         return false;
2058                 inset->edit(bv, front);
2059                 if (!the_locking_inset)
2060                         return false;
2061                 updateLocal(bv, CURSOR, false);
2062                 return true;
2063         }
2064         return false;
2065 }
2066
2067
2068 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2069                                       mouse_button::state button)
2070 {
2071         x -= drawTextXOffset;
2072         int dummyx = x;
2073         int dummyy = y + insetAscent;
2074         Inset * inset = getLyXText(bv)->checkInsetHit(bv, dummyx, dummyy);
2075         // we only do the edit() call if the inset was hit by the mouse
2076         // or if it is a highly editable inset. So we should call this
2077         // function from our own edit with button < 0.
2078         // FIXME: GUII jbl. I've changed this to ::none for now which is probably
2079         // WRONG
2080         if (button == mouse_button::none && !isHighlyEditableInset(inset))
2081                 return false;
2082
2083         if (inset) {
2084                 if (x < 0)
2085                         x = insetWidth;
2086                 if (y < 0)
2087                         y = insetDescent;
2088                 inset_x = cix(bv) - top_x + drawTextXOffset;
2089                 inset_y = ciy(bv) + drawTextYOffset;
2090                 inset->edit(bv, x - inset_x, y - inset_y, button);
2091                 if (!the_locking_inset)
2092                         return false;
2093                 updateLocal(bv, CURSOR, false);
2094                 return true;
2095         }
2096         return false;
2097 }
2098
2099
2100 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2101 {
2102 #if 0
2103         int w = UpdatableInset::getMaxWidth(bv, inset);
2104         if (w < 0) {
2105                 return -1;
2106         }
2107         if (owner()) {
2108                 w = w - top_x + owner()->x();
2109                 return w;
2110         }
2111         w -= (2 * TEXT_TO_INSET_OFFSET);
2112         return w - top_x;
2113 #else
2114         return UpdatableInset::getMaxWidth(bv, inset);
2115 #endif
2116 }
2117
2118
2119 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2120 {
2121         // we have to unlock any locked inset otherwise we're in troubles
2122         the_locking_inset = 0;
2123
2124         paragraphs.clear();
2125         paragraphs.set(new Paragraph(*p, same_id));
2126         paragraphs.begin()->setInsetOwner(this);
2127         Paragraph * np = &*(paragraphs.begin());
2128         while (p->next()) {
2129                 p = p->next();
2130                 np->next(new Paragraph(*p, same_id));
2131                 np->next()->previous(np);
2132                 np = np->next();
2133                 np->setInsetOwner(this);
2134         }
2135         reinitLyXText();
2136         need_update = INIT;
2137 }
2138
2139
2140 void InsetText::markNew(bool track_changes)
2141 {
2142         ParagraphList::iterator pit = paragraphs.begin();
2143         ParagraphList::iterator pend = paragraphs.end();
2144         for (; pit != pend; ++pit) {
2145                 if (track_changes) {
2146                         pit->trackChanges();
2147                 } else {
2148                         // no-op when not tracking
2149                         pit->cleanChanges();
2150                 }
2151         }
2152 }
2153
2154
2155 void InsetText::setText(string const & data, LyXFont const & font)
2156 {
2157         clear(false);
2158         for (unsigned int i = 0; i < data.length(); ++i)
2159                 paragraphs.begin()->insertChar(i, data[i], font);
2160         reinitLyXText();
2161 }
2162
2163
2164 void InsetText::setAutoBreakRows(bool flag)
2165 {
2166         if (flag != autoBreakRows) {
2167                 autoBreakRows = flag;
2168                 if (!flag)
2169                         removeNewlines();
2170                 need_update = INIT;
2171         }
2172 }
2173
2174
2175 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2176 {
2177         if (how != drawFrame_) {
2178                 drawFrame_ = how;
2179                 if (bv)
2180                         updateLocal(bv, DRAW_FRAME, false);
2181         }
2182 }
2183
2184
2185 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2186 {
2187         if (frame_color != col) {
2188                 frame_color = col;
2189                 if (bv)
2190                         updateLocal(bv, DRAW_FRAME, false);
2191         }
2192 }
2193
2194
2195 int InsetText::cx(BufferView * bv) const
2196 {
2197         // we do nothing dangerous so we use a local cache
2198         LyXText * llt = getLyXText(bv);
2199         int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2200         if (the_locking_inset) {
2201                 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2202                                             llt->cursor.pos());
2203                 if (font.isVisibleRightToLeft())
2204                         x -= the_locking_inset->width(bv, font);
2205         }
2206         return x;
2207 }
2208
2209
2210 int InsetText::cix(BufferView * bv) const
2211 {
2212         // we do nothing dangerous so we use a local cache
2213         LyXText * llt = getLyXText(bv);
2214         int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2215         if (the_locking_inset) {
2216                 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2217                                             llt->cursor.pos());
2218                 if (font.isVisibleRightToLeft())
2219                         x -= the_locking_inset->width(bv, font);
2220         }
2221         return x;
2222 }
2223
2224
2225 int InsetText::cy(BufferView * bv) const
2226 {
2227         LyXFont font;
2228         return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2229 }
2230
2231
2232 int InsetText::ciy(BufferView * bv) const
2233 {
2234         LyXFont font;
2235         return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2236 }
2237
2238
2239 pos_type InsetText::cpos(BufferView * bv) const
2240 {
2241         return getLyXText(bv)->cursor.pos();
2242 }
2243
2244
2245 Paragraph * InsetText::cpar(BufferView * bv) const
2246 {
2247         return getLyXText(bv)->cursor.par();
2248 }
2249
2250
2251 bool InsetText::cboundary(BufferView * bv) const
2252 {
2253         return getLyXText(bv)->cursor.boundary();
2254 }
2255
2256
2257 Row * InsetText::crow(BufferView * bv) const
2258 {
2259         return getLyXText(bv)->cursor.row();
2260 }
2261
2262
2263 LyXText * InsetText::getLyXText(BufferView const * lbv,
2264                                 bool const recursive) const
2265 {
2266         if (cached_bview == lbv) {
2267                 if (recursive && the_locking_inset)
2268                         return the_locking_inset->getLyXText(lbv, true);
2269                 LyXText * lt = cached_text.get();
2270                 lyx::Assert(lt && lt->firstRow()->par() == &*(paragraphs.begin()));
2271                 return lt;
2272         }
2273         // Super UGLY! (Lgb)
2274         BufferView * bv = const_cast<BufferView *>(lbv);
2275
2276         cached_bview = bv;
2277         Cache::iterator it = cache.find(bv);
2278
2279         if (it != cache.end()) {
2280                 if (do_reinit) {
2281                         reinitLyXText();
2282                 } else if (do_resize) {
2283                         resizeLyXText(do_resize);
2284                 } else {
2285                         if (lt || !it->second.remove) {
2286                                 lyx::Assert(it->second.text.get());
2287                                 cached_text = it->second.text;
2288                                 if (recursive && the_locking_inset) {
2289                                         return the_locking_inset->getLyXText(bv, true);
2290                                 }
2291                                 return cached_text.get();
2292                         } else if (it->second.remove) {
2293                                 if (locked) {
2294                                         saveLyXTextState(it->second.text.get());
2295                                 } else {
2296                                         sstate.lpar = 0;
2297                                 }
2298                         }
2299                         //
2300                         // when we have to reinit the existing LyXText!
2301                         //
2302                         it->second.text->init(bv);
2303                         restoreLyXTextState(bv, it->second.text.get());
2304                         it->second.remove = false;
2305                 }
2306                 cached_text = it->second.text;
2307                 if (the_locking_inset && recursive) {
2308                         return the_locking_inset->getLyXText(bv);
2309                 }
2310                 return cached_text.get();
2311         }
2312         ///
2313         // we are here only if we don't have a BufferView * in the cache!!!
2314         ///
2315         cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2316         cached_text->init(bv);
2317         restoreLyXTextState(bv, cached_text.get());
2318
2319         cache.insert(make_pair(bv, cached_text));
2320
2321         if (the_locking_inset && recursive) {
2322                 return the_locking_inset->getLyXText(bv);
2323         }
2324         return cached_text.get();
2325 }
2326
2327
2328 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2329 {
2330         cached_bview = 0;
2331
2332         Cache::iterator it = cache.find(bv);
2333
2334         if (it == cache.end()) {
2335                 return;
2336         }
2337
2338         lyx::Assert(it->second.text.get());
2339
2340         it->second.remove = true;
2341         if (recursive) {
2342                 /// then remove all LyXText in text-insets
2343                 for_each(paragraphs.begin(), paragraphs.end(),
2344                          boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2345         }
2346 }
2347
2348
2349 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2350 {
2351         if (lt) {
2352                 // we cannot resize this because we are in use!
2353                 // so do this on the next possible getLyXText()
2354                 do_resize = bv;
2355                 return;
2356         }
2357         do_resize = 0;
2358 //      lyxerr << "InsetText::resizeLyXText\n";
2359         if (!paragraphs.begin()->next() && paragraphs.begin()->empty()) { // no data, resize not neccessary!
2360                 // we have to do this as a fixed width may have changed!
2361                 LyXText * t = getLyXText(bv);
2362                 saveLyXTextState(t);
2363                 t->init(bv, true);
2364                 restoreLyXTextState(bv, t);
2365                 return;
2366         }
2367         // one endless line, resize normally not necessary
2368         if (!force && getMaxWidth(bv, this) < 0)
2369                 return;
2370
2371         Cache::iterator it = cache.find(bv);
2372         if (it == cache.end()) {
2373                 return;
2374         }
2375         lyx::Assert(it->second.text.get());
2376
2377         LyXText * t = it->second.text.get();
2378         saveLyXTextState(t);
2379
2380         for_each(paragraphs.begin(), paragraphs.end(),
2381                  boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2382
2383         t->init(bv, true);
2384         restoreLyXTextState(bv, t);
2385         if (the_locking_inset) {
2386                 inset_x = cix(bv) - top_x + drawTextXOffset;
2387                 inset_y = ciy(bv) + drawTextYOffset;
2388         }
2389
2390         t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2391         if (!owner()) {
2392                 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2393                 // this will scroll the screen such that the cursor becomes visible
2394                 bv->updateScrollbar();
2395         } else {
2396                 need_update |= FULL;
2397         }
2398 }
2399
2400
2401 void InsetText::reinitLyXText() const
2402 {
2403         if (lt) {
2404                 // we cannot resize this because we are in use!
2405                 // so do this on the next possible getLyXText()
2406                 do_reinit = true;
2407                 return;
2408         }
2409         do_reinit = false;
2410         do_resize = 0;
2411 //      lyxerr << "InsetText::reinitLyXText\n";
2412         for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2413                 lyx::Assert(it->second.text.get());
2414
2415                 LyXText * t = it->second.text.get();
2416                 BufferView * bv = it->first;
2417
2418                 saveLyXTextState(t);
2419
2420                 for_each(paragraphs.begin(), paragraphs.end(),
2421                          boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2422
2423                 t->init(bv, true);
2424                 restoreLyXTextState(bv, t);
2425                 if (the_locking_inset) {
2426                         inset_x = cix(bv) - top_x + drawTextXOffset;
2427                         inset_y = ciy(bv) + drawTextYOffset;
2428                 }
2429                 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2430                 if (!owner()) {
2431                         const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2432                         // this will scroll the screen such that the cursor becomes visible
2433                         bv->updateScrollbar();
2434                 } else {
2435                         need_update = FULL;
2436                 }
2437         }
2438 }
2439
2440
2441 void InsetText::removeNewlines()
2442 {
2443         bool changed = false;
2444
2445         ParagraphList::iterator it = paragraphs.begin();
2446         ParagraphList::iterator end = paragraphs.end();
2447         for (; it != end; ++it) {
2448                 for (int i = 0; i < it->size(); ++i) {
2449                         if (it->getChar(i) == Paragraph::META_NEWLINE) {
2450                                 changed = true;
2451                                 it->erase(i);
2452                         }
2453                 }
2454         }
2455         if (changed)
2456                 reinitLyXText();
2457 }
2458
2459
2460 bool InsetText::nodraw() const
2461 {
2462         if (the_locking_inset)
2463                 return the_locking_inset->nodraw();
2464         return UpdatableInset::nodraw();
2465 }
2466
2467
2468 int InsetText::scroll(bool recursive) const
2469 {
2470         int sx = UpdatableInset::scroll(false);
2471
2472         if (recursive && the_locking_inset)
2473                 sx += the_locking_inset->scroll(recursive);
2474
2475         return sx;
2476 }
2477
2478
2479 bool InsetText::doClearArea() const
2480 {
2481         return !locked || (need_update & (FULL|INIT));
2482 }
2483
2484
2485 void InsetText::selectAll(BufferView * bv)
2486 {
2487         getLyXText(bv)->cursorTop(bv);
2488         getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2489         getLyXText(bv)->cursorBottom(bv);
2490         getLyXText(bv)->setSelection(bv);
2491 }
2492
2493
2494 void InsetText::clearSelection(BufferView * bv)
2495 {
2496         getLyXText(bv)->clearSelection();
2497 }
2498
2499
2500 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2501 {
2502         Painter & pain = bv->painter();
2503         int w = insetWidth;
2504         int h = insetAscent + insetDescent;
2505         int ty = baseline - insetAscent;
2506
2507         if (ty < 0) {
2508                 h += ty;
2509                 ty = 0;
2510         }
2511         if ((ty + h) > pain.paperHeight())
2512                 h = pain.paperHeight();
2513         if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2514                 w = pain.paperWidth();
2515 //      w -= TEXT_TO_INSET_OFFSET;
2516         pain.fillRectangle(top_x + 1, ty + 1, w - 1, h - 1, backgroundColor());
2517         cleared = true;
2518         need_update = FULL;
2519         frame_is_visible = false;
2520 }
2521
2522
2523 Paragraph * InsetText::firstParagraph() const
2524 {
2525         Paragraph * result;
2526         if (the_locking_inset)
2527                 if ((result = the_locking_inset->firstParagraph()))
2528                         return result;
2529         return &*(paragraphs.begin());
2530 }
2531
2532
2533 Paragraph * InsetText::getFirstParagraph(int i) const
2534 {
2535         return (i == 0) ? &*(paragraphs.begin()) : 0;
2536 }
2537
2538
2539 LyXCursor const & InsetText::cursor(BufferView * bv) const
2540 {
2541         if (the_locking_inset)
2542                 return the_locking_inset->cursor(bv);
2543         return getLyXText(bv)->cursor;
2544 }
2545
2546
2547 Paragraph * InsetText::paragraph() const
2548 {
2549         return &*(paragraphs.begin());
2550 }
2551
2552
2553 void InsetText::paragraph(Paragraph * p)
2554 {
2555         // GENERAL COMMENT: We don't have to free the old paragraphs as the
2556         // caller of this function has to take care of it. This IS important
2557         // as we could have to insert a paragraph before this one and just
2558         // link the actual to a new ones next and set it with this function
2559         // and are done!
2560         paragraphs.set(p);
2561         // set ourself as owner for all the paragraphs inserted!
2562         for_each(paragraphs.begin(), paragraphs.end(),
2563                  boost::bind(&Paragraph::setInsetOwner, _1, this));
2564
2565         reinitLyXText();
2566         // redraw myself when asked for
2567         need_update = INIT;
2568 }
2569
2570
2571 Inset * InsetText::getInsetFromID(int id_arg) const
2572 {
2573         if (id_arg == id())
2574                 return const_cast<InsetText *>(this);
2575
2576         ParagraphList::iterator pit = paragraphs.begin();
2577         ParagraphList::iterator pend = paragraphs.end();
2578         for (; pit != pend; ++pit) {
2579                 InsetList::iterator it = pit->insetlist.begin();
2580                 InsetList::iterator end = pit->insetlist.end();
2581                 for (; it != end; ++it) {
2582                         if (it.getInset()->id() == id_arg)
2583                                 return it.getInset();
2584                         Inset * in = it.getInset()->getInsetFromID(id_arg);
2585                         if (in)
2586                                 return in;
2587                 }
2588         }
2589         return 0;
2590 }
2591
2592
2593 WordLangTuple const
2594 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2595                                       float & value) const
2596 {
2597         bool clear = false;
2598         WordLangTuple word;
2599
2600         if (!lt) {
2601                 lt = getLyXText(bv);
2602                 clear = true;
2603         }
2604         if (the_locking_inset) {
2605                 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2606                 if (!word.word().empty()) {
2607                         value += cy(bv);
2608                         if (clear)
2609                                 lt = 0;
2610                         return word;
2611                 }
2612                 // we have to go on checking so move cursor to the next char
2613                 lt->cursor.pos(lt->cursor.pos() + 1);
2614         }
2615         word = lt->selectNextWordToSpellcheck(bv, value);
2616         if (word.word().empty())
2617                 bv->unlockInset(const_cast<InsetText *>(this));
2618         else
2619                 value = cy(bv);
2620         if (clear)
2621                 lt = 0;
2622         return word;
2623 }
2624
2625
2626 void InsetText::selectSelectedWord(BufferView * bv)
2627 {
2628         if (the_locking_inset) {
2629                 the_locking_inset->selectSelectedWord(bv);
2630                 return;
2631         }
2632         getLyXText(bv)->selectSelectedWord(bv);
2633         updateLocal(bv, SELECTION, false);
2634 }
2635
2636
2637 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2638 {
2639         if (the_locking_inset) {
2640                 the_locking_inset->toggleSelection(bv, kill_selection);
2641         }
2642         bool clear = false;
2643         if (!lt) {
2644                 lt = getLyXText(bv);
2645                 clear = true;
2646         }
2647
2648         int x = top_x + TEXT_TO_INSET_OFFSET;
2649
2650         Row * row = lt->firstRow();
2651         int y_offset = top_baseline - row->ascent_of_text();
2652         int y = y_offset;
2653         while ((row != 0) && ((y+row->height()) <= 0)) {
2654                 y += row->height();
2655                 row = row->next();
2656         }
2657         if (y_offset < 0)
2658                 y_offset = y;
2659
2660         if (need_update & SELECTION)
2661                 need_update = NONE;
2662         bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2663         if (clear)
2664                 lt = 0;
2665 }
2666
2667
2668 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2669 {
2670         bool clear = false;
2671         if (!lt) {
2672                 lt = getLyXText(bv);
2673                 clear = true;
2674         }
2675         if (the_locking_inset) {
2676                 if (the_locking_inset->nextChange(bv, length))
2677                         return true;
2678                 lt->cursorRight(bv, true);
2679         }
2680         lyxfind::SearchResult result =
2681                 lyxfind::findNextChange(bv, lt, length);
2682
2683         if (result == lyxfind::SR_FOUND) {
2684                 LyXCursor cur = lt->cursor;
2685                 bv->unlockInset(bv->theLockingInset());
2686                 if (bv->lockInset(this))
2687                         locked = true;
2688                 lt->cursor = cur;
2689                 lt->setSelectionRange(bv, length);
2690                 updateLocal(bv, SELECTION, false);
2691         }
2692         if (clear)
2693                 lt = 0;
2694         return result != lyxfind::SR_NOT_FOUND;
2695 }
2696
2697
2698 bool InsetText::searchForward(BufferView * bv, string const & str,
2699                               bool cs, bool mw)
2700 {
2701         bool clear = false;
2702         if (!lt) {
2703                 lt = getLyXText(bv);
2704                 clear = true;
2705         }
2706         if (the_locking_inset) {
2707                 if (the_locking_inset->searchForward(bv, str, cs, mw))
2708                         return true;
2709                 lt->cursorRight(bv, true);
2710         }
2711         lyxfind::SearchResult result =
2712                 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2713
2714         if (result == lyxfind::SR_FOUND) {
2715                 LyXCursor cur = lt->cursor;
2716                 bv->unlockInset(bv->theLockingInset());
2717                 if (bv->lockInset(this))
2718                         locked = true;
2719                 lt->cursor = cur;
2720                 lt->setSelectionRange(bv, str.length());
2721                 updateLocal(bv, SELECTION, false);
2722         }
2723         if (clear)
2724                 lt = 0;
2725         return (result != lyxfind::SR_NOT_FOUND);
2726 }
2727
2728 bool InsetText::searchBackward(BufferView * bv, string const & str,
2729                                bool cs, bool mw)
2730 {
2731         if (the_locking_inset) {
2732                 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2733                         return true;
2734         }
2735         bool clear = false;
2736         if (!lt) {
2737                 lt = getLyXText(bv);
2738                 clear = true;
2739         }
2740         if (!locked) {
2741                 Paragraph * p = &*(paragraphs.begin());
2742                 while (p->next())
2743                         p = p->next();
2744                 lt->setCursor(bv, p, p->size());
2745         }
2746         lyxfind::SearchResult result =
2747                 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2748
2749         if (result == lyxfind::SR_FOUND) {
2750                 LyXCursor cur = lt->cursor;
2751                 bv->unlockInset(bv->theLockingInset());
2752                 if (bv->lockInset(this))
2753                         locked = true;
2754                 lt->cursor = cur;
2755                 lt->setSelectionRange(bv, str.length());
2756                 updateLocal(bv, SELECTION, false);
2757         }
2758         if (clear)
2759                 lt = 0;
2760         return (result != lyxfind::SR_NOT_FOUND);
2761 }
2762
2763
2764 bool InsetText::checkInsertChar(LyXFont & font)
2765 {
2766         if (owner())
2767                 return owner()->checkInsertChar(font);
2768         return true;
2769 }
2770
2771
2772 void InsetText::collapseParagraphs(BufferView * bv)
2773 {
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(bv->buffer()->params, paragraphs, 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(Buffer * buffer,
2809                                  Paragraph * newpar)
2810 {
2811         BufferParams const & bparams = buffer->params;
2812         Paragraph * buf;
2813         Paragraph * tmpbuf = newpar;
2814         Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2815         if (bparams.tracking_changes)
2816                 buf->cleanChanges();
2817
2818         while (tmpbuf->next()) {
2819                 tmpbuf = tmpbuf->next();
2820                 lastbuffer->next(new Paragraph(*tmpbuf, false));
2821                 lastbuffer->next()->previous(lastbuffer);
2822                 lastbuffer = lastbuffer->next();
2823                 if (bparams.tracking_changes)
2824                         lastbuffer->cleanChanges();
2825         }
2826         lastbuffer = &*(paragraphs.begin());
2827         while (lastbuffer->next())
2828                 lastbuffer = lastbuffer->next();
2829         if (!newpar->empty() && !lastbuffer->empty() &&
2830                 !lastbuffer->isSeparator(lastbuffer->size() - 1))
2831         {
2832                 lastbuffer->insertChar(lastbuffer->size(), ' ');
2833         }
2834
2835         // make the buf exactly the same layout than our last paragraph
2836         buf->makeSameLayout(lastbuffer);
2837
2838         // paste it!
2839         lastbuffer->next(buf);
2840         buf->previous(lastbuffer);
2841         mergeParagraph(buffer->params, paragraphs, lastbuffer);
2842
2843         reinitLyXText();
2844 }
2845
2846
2847 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2848 {
2849         Paragraph * par = getFirstParagraph(0);
2850         while (par) {
2851                 InsetList::iterator it  = par->insetlist.begin();
2852                 InsetList::iterator end = par->insetlist.end();
2853                 for (; it != end; ++it) {
2854                         it.getInset()->addPreview(loader);
2855                 }
2856
2857                 par = par->next();
2858         }
2859 }