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