]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
slimmer interface to paintrows
[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 #include "insettext.h"
14
15 #include "buffer.h"
16 #include "BufferView.h"
17 #include "bufferview_funcs.h"
18 #include "CutAndPaste.h"
19 #include "debug.h"
20 #include "dimension.h"
21 #include "funcrequest.h"
22 #include "gettext.h"
23 #include "errorlist.h"
24 #include "intl.h"
25 #include "LaTeXFeatures.h"
26 #include "LColor.h"
27 #include "Lsstream.h"
28 #include "lyxfont.h"
29 #include "lyxcursor.h"
30 #include "lyxfind.h"
31 #include "lyxlex.h"
32 #include "lyxrow.h"
33 #include "lyxrc.h"
34 #include "lyxtext.h"
35 #include "paragraph.h"
36 #include "ParagraphParameters.h"
37 #include "trans_mgr.h"
38 #include "undo_funcs.h"
39 #include "WordLangTuple.h"
40 #include "paragraph_funcs.h"
41 #include "sgml.h"
42 #include "rowpainter.h"
43 #include "insetnewline.h"
44 #include "metricsinfo.h"
45
46 #include "frontends/Alert.h"
47 #include "frontends/Dialogs.h"
48 #include "frontends/font_metrics.h"
49 #include "frontends/LyXView.h"
50 #include "frontends/Painter.h"
51 #include "frontends/screen.h"
52
53 #include "support/textutils.h"
54 #include "support/LAssert.h"
55 #include "support/lstrings.h"
56 #include "support/lyxalgo.h" // lyx::count
57
58 #include <boost/bind.hpp>
59
60 #include <fstream>
61 #include <algorithm>
62 #include <cstdlib>
63
64 using std::ostream;
65 using std::ifstream;
66 using std::endl;
67 using std::min;
68 using std::max;
69 using std::make_pair;
70 using std::vector;
71 using std::pair;
72 using std::for_each;
73 using std::auto_ptr;
74
75 using namespace lyx::support;
76 using namespace lyx::graphics;
77 using namespace bv_funcs;
78
79 using lyx::pos_type;
80 using lyx::textclass_type;
81
82 // These functions should probably go into bufferview_funcs somehow (Jug)
83
84 void InsetText::saveLyXTextState() const
85 {
86         // check if my paragraphs are still valid
87         ParagraphList::iterator it = const_cast<ParagraphList&>(paragraphs).begin();
88         ParagraphList::iterator end = const_cast<ParagraphList&>(paragraphs).end();
89         for (; it != end; ++it) {
90                 if (it == text_.cursor.par())
91                         break;
92         }
93
94         if (it != end && text_.cursor.pos() <= it->size())
95                 sstate = text_; // slicing intended
96         else
97                 sstate.cursor.par(end);
98 }
99
100
101 void InsetText::restoreLyXTextState() const
102 {
103         if (sstate.cursor.par() == const_cast<ParagraphList&>(paragraphs).end())
104                 return;
105
106         text_.selection.set(true);
107         // at this point just to avoid the DEPM when setting the cursor
108         text_.selection.mark(sstate.selection.mark());
109         if (sstate.selection.set()) {
110                 text_.setCursor(sstate.selection.start.par(),
111                         sstate.selection.start.pos(),
112                         true, sstate.selection.start.boundary());
113                 text_.selection.cursor = text_.cursor;
114                 text_.setCursor(sstate.selection.end.par(), sstate.selection.end.pos(),
115                         true, sstate.selection.end.boundary());
116                 text_.setSelection();
117                 text_.setCursor(sstate.cursor.par(), sstate.cursor.pos());
118         } else {
119                 text_.setCursor(sstate.cursor.par(), sstate.cursor.pos(),
120                         true, sstate.cursor.boundary());
121                 text_.selection.cursor = text_.cursor;
122                 text_.selection.set(false);
123         }
124 }
125
126
127 InsetText::InsetText(BufferParams const & bp)
128         : UpdatableInset(), text_(0, this)
129 {
130         paragraphs.push_back(Paragraph());
131         paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
132         if (bp.tracking_changes)
133                 paragraphs.begin()->trackChanges();
134         init(0);
135 }
136
137
138 InsetText::InsetText(InsetText const & in)
139         : UpdatableInset(in), text_(0, this)
140 {
141         init(&in);
142 }
143
144
145 InsetText & InsetText::operator=(InsetText const & it)
146 {
147         init(&it);
148         return *this;
149 }
150
151
152 void InsetText::init(InsetText const * ins)
153 {
154         if (ins) {
155                 textwidth_ = ins->textwidth_;
156                 text_.bv_owner = ins->text_.bv_owner;
157                 setParagraphData(ins->paragraphs);
158                 autoBreakRows = ins->autoBreakRows;
159                 drawFrame_ = ins->drawFrame_;
160                 frame_color = ins->frame_color;
161         } else {
162                 textwidth_ = 0; // unbounded
163                 drawFrame_ = NEVER;
164                 frame_color = LColor::insetframe;
165                 autoBreakRows = false;
166         }
167         the_locking_inset = 0;
168         for_each(paragraphs.begin(), paragraphs.end(),
169                  boost::bind(&Paragraph::setInsetOwner, _1, this));
170         top_y = 0;
171         no_selection = true;
172         need_update = FULL;
173         drawTextXOffset = 0;
174         drawTextYOffset = 0;
175         locked = false;
176         old_par = paragraphs.end();
177         last_drawn_width = -1;
178         sstate.cursor.par(paragraphs.end());
179         in_insetAllowed = false;
180 }
181
182
183 void InsetText::clear(bool just_mark_erased)
184 {
185         if (just_mark_erased) {
186                 ParagraphList::iterator it = paragraphs.begin();
187                 ParagraphList::iterator end = paragraphs.end();
188                 for (; it != end; ++it) {
189                         it->markErased();
190                 }
191                 need_update = FULL;
192                 return;
193         }
194
195         // This is a gross hack...
196         LyXLayout_ptr old_layout = paragraphs.begin()->layout();
197
198         paragraphs.clear();
199         paragraphs.push_back(Paragraph());
200         paragraphs.begin()->setInsetOwner(this);
201         paragraphs.begin()->layout(old_layout);
202
203         reinitLyXText();
204         need_update = INIT;
205 }
206
207
208 auto_ptr<InsetBase> InsetText::clone() const
209 {
210         return auto_ptr<InsetBase>(new InsetText(*this));
211 }
212
213
214 void InsetText::write(Buffer const * buf, ostream & os) const
215 {
216         os << "Text\n";
217         writeParagraphData(buf, os);
218 }
219
220
221 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
222 {
223         ParagraphList::const_iterator it = paragraphs.begin();
224         ParagraphList::const_iterator end = paragraphs.end();
225         Paragraph::depth_type dth = 0;
226         for (; it != end; ++it) {
227                 it->write(buf, os, buf->params, dth);
228         }
229 }
230
231
232 void InsetText::read(Buffer const * buf, LyXLex & lex)
233 {
234         string token;
235         Paragraph::depth_type depth = 0;
236
237         clear(false);
238
239         if (buf->params.tracking_changes)
240                 paragraphs.begin()->trackChanges();
241
242         // delete the initial paragraph
243         paragraphs.clear();
244         ParagraphList::iterator pit = paragraphs.begin();
245
246         while (lex.isOK()) {
247                 lex.nextToken();
248                 token = lex.getString();
249                 if (token.empty())
250                         continue;
251                 if (token == "\\end_inset") {
252                         break;
253                 }
254
255                 if (token == "\\the_end") {
256                         lex.printError("\\the_end read in inset! Error in document!");
257                         return;
258                 }
259
260                 // FIXME: ugly.
261                 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
262         }
263
264         pit = paragraphs.begin();
265         ParagraphList::iterator const end = paragraphs.end();
266         for (; pit != end; ++pit)
267                 pit->setInsetOwner(this);
268
269         if (token != "\\end_inset") {
270                 lex.printError("Missing \\end_inset at this point. "
271                                            "Read: `$$Token'");
272         }
273         need_update = FULL;
274 }
275
276
277 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
278 {
279         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << "\n";
280
281         textwidth_ = mi.base.textwidth;
282         BufferView * bv = mi.base.bv;
283         setViewCache(bv);
284         text_.metrics(mi, dim);
285         dim.asc += TEXT_TO_INSET_OFFSET;
286         dim.des += TEXT_TO_INSET_OFFSET;
287         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
288         dim.wid = max(dim.wid, 10);
289         dim_ = dim;
290 }
291
292
293 int InsetText::textWidth() const
294 {
295         return textwidth_;
296 }
297
298
299 void InsetText::draw(PainterInfo & pi, int x, int baseline) const
300 {
301         if (nodraw())
302                 return;
303
304         // update our idea of where we are. Clearly, we should
305         // not have to know this information.
306         top_x = x;
307
308         int const start_x = x;
309
310         BufferView * bv = pi.base.bv;
311         Painter & pain = pi.pain;
312
313         // repaint the background if needed
314         if (backgroundColor() != LColor::background)
315                 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
316
317         // no draw is necessary !!!
318         if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
319                 top_baseline = baseline;
320                 need_update = NONE;
321                 return;
322         }
323
324         bv->hideCursor();
325
326         if (!owner())
327                 x += scroll();
328
329         top_baseline = baseline;
330         top_y = baseline - dim_.asc;
331
332         if (last_drawn_width != dim_.wid) {
333                 need_update |= FULL;
334                 last_drawn_width = dim_.wid;
335         }
336
337         if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
338                 inset_x = cix() - x + drawTextXOffset;
339                 inset_y = ciy() + drawTextYOffset;
340         }
341
342         x += TEXT_TO_INSET_OFFSET;
343
344         RowList::iterator rit = text_.rows().begin();
345         RowList::iterator end = text_.rows().end();
346
347         int y_offset = baseline - rit->ascent_of_text();
348         int y2 = pain.paperHeight();
349         int first = 0;
350         int y = y_offset;
351         while (rit != end && y + rit->height() <= 0) {
352                 y += rit->height();
353                 first += rit->height();
354                 ++rit;
355         }
356         if (y_offset < 0) {
357                 text_.top_y(-y_offset);
358                 first = y;
359                 y_offset = 0;
360         } else {
361                 text_.top_y(first);
362                 first = 0;
363         }
364
365         int yo = y_offset + first;
366
367         paintRows(*bv, text_, rit, x, 0, yo, y2, yo);
368
369         text_.clearPaint();
370
371         if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
372                 drawFrame(pain, int(start_x));
373
374         if (need_update != INIT)
375                 need_update = NONE;
376 }
377
378
379 void InsetText::drawFrame(Painter & pain, int x) const
380 {
381         static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
382         frame_x = x + ttoD2;
383         frame_y = top_baseline - dim_.asc + ttoD2;
384         frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
385         frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
386         pain.rectangle(frame_x, frame_y, frame_w, frame_h,
387                        frame_color);
388 }
389
390
391 void InsetText::setUpdateStatus(int what) const
392 {
393         need_update |= what;
394         // we will to redraw us full if our LyXText wants it
395         if (text_.needRefresh())
396                 need_update |= FULL;
397
398         // this to not draw a selection when we redraw all of it!
399         if (need_update & CURSOR && !(need_update & SELECTION)) {
400                 if (text_.selection.set())
401                         need_update = FULL;
402                 text_.clearSelection();
403         }
404 }
405
406
407 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
408 {
409         if (!bv)
410                 return;
411
412         if (!autoBreakRows && paragraphs.size() > 1)
413                 collapseParagraphs(bv);
414
415         text_.partialRebreak();
416         setUpdateStatus(what);
417         bool flag = mark_dirty ||
418                 ((need_update != CURSOR && need_update != NONE) ||
419                  text_.needRefresh() || text_.selection.set());
420         if (!text_.selection.set())
421                 text_.selection.cursor = text_.cursor;
422
423         bv->fitCursor();
424
425         if (flag) {
426                 text_.postPaint();
427                 bv->updateInset(const_cast<InsetText *>(this));
428         }
429
430         if (need_update == CURSOR)
431                 need_update = NONE;
432         bv->owner()->view_state_changed();
433         bv->owner()->updateMenubar();
434         bv->owner()->updateToolbar();
435         if (old_par != cpar()) {
436                 bv->owner()->setLayout(cpar()->layout()->name());
437                 old_par = cpar();
438         }
439 }
440
441
442 string const InsetText::editMessage() const
443 {
444         return _("Opened Text Inset");
445 }
446
447
448 void InsetText::insetUnlock(BufferView * bv)
449 {
450         if (the_locking_inset) {
451                 the_locking_inset->insetUnlock(bv);
452                 the_locking_inset = 0;
453                 updateLocal(bv, CURSOR_PAR, false);
454         }
455         no_selection = true;
456         locked = false;
457         int code = NONE;
458
459         if (text_.selection.set()) {
460                 text_.clearSelection();
461                 code = FULL;
462         } else if (owner()) {
463                 bv->owner()->setLayout(owner()->getLyXText(bv)
464                                        ->cursor.par()->layout()->name());
465         } else
466                 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
467         // hack for deleteEmptyParMech
468         ParagraphList::iterator first_par = paragraphs.begin();
469         if (!first_par->empty()) {
470                 text_.setCursor(first_par, 0);
471         } else if (paragraphs.size() > 1) {
472                 text_.setCursor(boost::next(first_par), 0);
473         }
474 #if 0
475         updateLocal(bv, code, false);
476 #else
477         if (code != NONE)
478                 setUpdateStatus(code);
479 #endif
480 }
481
482
483 void InsetText::lockInset(BufferView * bv)
484 {
485         locked = true;
486         the_locking_inset = 0;
487         inset_pos = inset_x = inset_y = 0;
488         inset_boundary = false;
489         inset_par = paragraphs.end();
490         old_par = paragraphs.end();
491         text_.setCursorIntern(paragraphs.begin(), 0);
492         text_.clearSelection();
493         finishUndo();
494         // If the inset is empty set the language of the current font to the
495         // language to the surronding text (if different).
496         if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
497                 bv->getParentLanguage(this) != text_.current_font.language()) {
498                 LyXFont font(LyXFont::ALL_IGNORE);
499                 font.setLanguage(bv->getParentLanguage(this));
500                 setFont(bv, font, false);
501         }
502         int code = CURSOR;
503         if (drawFrame_ == LOCKED)
504                 code = CURSOR|DRAW_FRAME;
505         setUpdateStatus(code);
506 }
507
508
509 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
510 {
511         the_locking_inset = inset;
512         inset_x = cix() - top_x + drawTextXOffset;
513         inset_y = ciy() + drawTextYOffset;
514         inset_pos = cpos();
515         inset_par = cpar();
516         inset_boundary = cboundary();
517         //updateLocal(bv, CURSOR, false);
518 }
519
520
521 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
522 {
523         lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
524                               << inset << "): ";
525         if (!inset)
526                 return false;
527         if (!the_locking_inset) {
528                 ParagraphList::iterator pit = paragraphs.begin();
529                 ParagraphList::iterator pend = paragraphs.end();
530
531                 int const id = inset->id();
532                 for (; pit != pend; ++pit) {
533                         InsetList::iterator it = pit->insetlist.begin();
534                         InsetList::iterator const end = pit->insetlist.end();
535                         for (; it != end; ++it) {
536                                 if (it->inset == inset) {
537                                         lyxerr << "InsetText::lockInsetInInset: 1 a\n";
538                                         text_.setCursorIntern(pit, it->pos);
539                                         lyxerr << "InsetText::lockInsetInInset: 1 b\n";
540                                         lyxerr << "bv: " << bv << " inset: " << inset << "\n";
541                                         lockInset(bv, inset);
542                                         lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
543                                         return true;
544                                 }
545                                 if (it->inset->getInsetFromID(id)) {
546                                         lyxerr << "InsetText::lockInsetInInset: 2\n";
547                                         text_.setCursorIntern(pit, it->pos);
548                                         it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
549                                         return the_locking_inset->lockInsetInInset(bv, inset);
550                                 }
551                         }
552                 }
553                 lyxerr << "InsetText::lockInsetInInset: 3\n";
554                 return false;
555         }
556         if (inset == cpar()->getInset(cpos())) {
557                 lyxerr[Debug::INSETS] << "OK" << endl;
558                 lockInset(bv, inset);
559                 return true;
560         }
561
562         if (the_locking_inset && the_locking_inset == inset) {
563                 if (cpar() == inset_par && cpos() == inset_pos) {
564                         lyxerr[Debug::INSETS] << "OK" << endl;
565                         inset_x = cix() - top_x + drawTextXOffset;
566                         inset_y = ciy() + drawTextYOffset;
567                 } else {
568                         lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
569                 }
570         } else if (the_locking_inset) {
571                 lyxerr[Debug::INSETS] << "MAYBE" << endl;
572                 return the_locking_inset->lockInsetInInset(bv, inset);
573         }
574         lyxerr[Debug::INSETS] << "NOT OK" << endl;
575         return false;
576 }
577
578
579 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
580                                    bool lr)
581 {
582         if (!the_locking_inset)
583                 return false;
584         if (the_locking_inset == inset) {
585                 the_locking_inset->insetUnlock(bv);
586                 getLyXText(bv)->updateInset(inset);
587                 the_locking_inset = 0;
588                 if (lr)
589                         moveRightIntern(bv, true, false);
590                 old_par = paragraphs.end(); // force layout setting
591                 if (scroll())
592                         scroll(bv, 0.0F);
593                 else
594                         updateLocal(bv, CURSOR, false);
595                 return true;
596         }
597         return the_locking_inset->unlockInsetInInset(bv, inset, lr);
598 }
599
600
601 bool InsetText::updateInsetInInset(BufferView * bv, InsetOld * inset)
602 {
603         if (!autoBreakRows && paragraphs.size() > 1)
604                 collapseParagraphs(bv);
605
606         if (inset == this)
607                 return true;
608
609         if (inset->owner() != this) {
610                 int ustat = CURSOR_PAR;
611                 bool found = false;
612                 UpdatableInset * tl_inset = the_locking_inset;
613                 if (tl_inset)
614                         found = tl_inset->updateInsetInInset(bv, inset);
615                 if (!found) {
616                         tl_inset = static_cast<UpdatableInset *>(inset);
617                         while(tl_inset->owner() && tl_inset->owner() != this)
618                                 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
619                         if (!tl_inset->owner())
620                                 return false;
621                         found = tl_inset->updateInsetInInset(bv, inset);
622                         ustat = FULL;
623                 } else {
624                         text_.updateInset(tl_inset);
625                         setUpdateStatus(ustat);
626                 }
627                 return found;
628         }
629         bool found = text_.updateInset(inset);
630         if (found) {
631                 setUpdateStatus(CURSOR_PAR);
632                 if (the_locking_inset &&
633                     cpar() == inset_par && cpos() == inset_pos)
634                 {
635                         inset_x = cix() - top_x + drawTextXOffset;
636                         inset_y = ciy() + drawTextYOffset;
637                 }
638         }
639         return found;
640 }
641
642
643 void InsetText::lfunMousePress(FuncRequest const & cmd)
644 {
645         no_selection = true;
646
647         // use this to check mouse motion for selection!
648         mouse_x = cmd.x;
649         mouse_y = cmd.y;
650
651         BufferView * bv = cmd.view();
652         FuncRequest cmd1 = cmd;
653         cmd1.x -= inset_x;
654         cmd1.y -= inset_y;
655         if (!locked)
656                 lockInset(bv);
657
658         int tmp_x = cmd.x - drawTextXOffset;
659         int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
660         InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
661
662         if (the_locking_inset) {
663                 if (the_locking_inset == inset) {
664                         the_locking_inset->localDispatch(cmd1);
665                         return;
666                 }
667                 // otherwise only unlock the_locking_inset
668                 the_locking_inset->insetUnlock(bv);
669                 the_locking_inset = 0;
670         }
671         if (!inset)
672                 no_selection = false;
673
674         if (bv->theLockingInset()) {
675                 if (isHighlyEditableInset(inset)) {
676                         // We just have to lock the inset before calling a
677                         // PressEvent on it!
678                         UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
679                         if (!bv->lockInset(uinset)) {
680                                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
681                         }
682                         inset->localDispatch(cmd1);
683                         if (the_locking_inset)
684                                 updateLocal(bv, CURSOR, false);
685                         return;
686                 }
687         }
688         if (!inset) {
689                 bool paste_internally = false;
690                 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
691                         localDispatch(FuncRequest(bv, LFUN_COPY));
692                         paste_internally = true;
693                 }
694                 int old_top_y = text_.top_y();
695
696                 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
697                                              cmd.y + dim_.asc);
698                 // set the selection cursor!
699                 text_.selection.cursor = text_.cursor;
700                 text_.cursor.x_fix(text_.cursor.x());
701
702                 if (text_.selection.set()) {
703                         text_.clearSelection();
704                         updateLocal(bv, FULL, false);
705                 } else {
706                         text_.clearSelection();
707                         updateLocal(bv, CURSOR, false);
708                 }
709
710                 bv->owner()->setLayout(cpar()->layout()->name());
711
712                 // we moved the view we cannot do mouse selection in this case!
713                 if (getLyXText(bv)->top_y() != old_top_y)
714                         no_selection = true;
715                 old_par = cpar();
716                 // Insert primary selection with middle mouse
717                 // if there is a local selection in the current buffer,
718                 // insert this
719                 if (cmd.button() == mouse_button::button2) {
720                         if (paste_internally)
721                                 localDispatch(FuncRequest(bv, LFUN_PASTE));
722                         else
723                                 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
724                 }
725         } else {
726                 getLyXText(bv)->clearSelection();
727         }
728 }
729
730
731 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
732 {
733         BufferView * bv = cmd.view();
734         FuncRequest cmd1 = cmd;
735         cmd1.x -= inset_x;
736         cmd1.y -= inset_y;
737
738         no_selection = true;
739         if (the_locking_inset)
740                 return the_locking_inset->localDispatch(cmd1);
741
742         int tmp_x = cmd.x - drawTextXOffset;
743         int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
744         InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
745         bool ret = false;
746         if (inset) {
747 // This code should probably be removed now. Simple insets
748 // (!highlyEditable) can actually take the localDispatch,
749 // and turn it into edit() if necessary. But we still
750 // need to deal properly with the whole relative vs.
751 // absolute mouse co-ords thing in a realiable, sensible way
752 #if 0
753                 if (isHighlyEditableInset(inset))
754                         ret = inset->localDispatch(cmd1);
755                 else {
756                         inset_x = cix(bv) - top_x + drawTextXOffset;
757                         inset_y = ciy() + drawTextYOffset;
758                         cmd1.x = cmd.x - inset_x;
759                         cmd1.y = cmd.x - inset_y;
760                         inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
761                         ret = true;
762                 }
763 #endif
764                 ret = inset->localDispatch(cmd1);
765                 updateLocal(bv, CURSOR_PAR, false);
766
767         }
768         return ret;
769 }
770
771
772 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
773 {
774         FuncRequest cmd1 = cmd;
775         cmd1.x -= inset_x;
776         cmd1.y -= inset_y;
777
778         if (the_locking_inset) {
779                 the_locking_inset->localDispatch(cmd1);
780                 return;
781         }
782
783         if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
784                 return;
785
786         BufferView * bv = cmd.view();
787         LyXCursor cur = text_.cursor;
788         text_.setCursorFromCoordinates
789                 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
790         text_.cursor.x_fix(text_.cursor.x());
791         if (cur == text_.cursor)
792                 return;
793         text_.setSelection();
794         bool flag = (text_.toggle_cursor.par() != text_.toggle_end_cursor.par() ||
795                                  text_.toggle_cursor.pos() != text_.toggle_end_cursor.pos());
796         if (flag) {
797                 updateLocal(bv, SELECTION, false);
798         }
799 }
800
801
802 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
803 {
804         BufferView * bv = cmd.view();
805         setViewCache(bv);
806
807         switch (cmd.action) {
808         case LFUN_INSET_EDIT: {
809                 UpdatableInset::localDispatch(cmd);
810
811                 if (!bv->lockInset(this)) {
812                         lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
813                         return DISPATCHED;
814                 }
815
816                 locked = true;
817                 the_locking_inset = 0;
818                 inset_pos = 0;
819                 inset_x = 0;
820                 inset_y = 0;
821                 inset_boundary = false;
822                 inset_par = paragraphs.end();
823                 old_par = paragraphs.end();
824
825
826                 if (cmd.argument.size()) {
827                         if (cmd.argument == "left")
828                                 text_.setCursorIntern(paragraphs.begin(), 0);
829                         else {
830                                 ParagraphList::iterator it = boost::prior(paragraphs.end());
831                                 text_.setCursor(it, it->size());
832                         }
833                 } else {
834                         int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
835                         // we put here -1 and not button as now the button in the
836                         // edit call should not be needed we will fix this in 1.3.x
837                         // cycle hopefully (Jug 20020509)
838                         // FIXME: GUII I've changed this to none: probably WRONG
839                         if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
840                                 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
841                                                                         cmd.y + dim_.asc);
842                                 text_.cursor.x_fix(text_.cursor.x());
843                         }
844                 }
845
846                 text_.clearSelection();
847                 finishUndo();
848
849                 // If the inset is empty set the language of the current font to the
850                 // language to the surronding text (if different).
851                 if (paragraphs.begin()->empty() &&
852                     paragraphs.size() == 1 &&
853                     bv->getParentLanguage(this) != text_.current_font.language())
854                 {
855                         LyXFont font(LyXFont::ALL_IGNORE);
856                         font.setLanguage(bv->getParentLanguage(this));
857                         setFont(bv, font, false);
858                 }
859
860                 int code = CURSOR;
861                 if (drawFrame_ == LOCKED)
862                         code = CURSOR | DRAW_FRAME;
863
864                 updateLocal(bv, code, false);
865                 // Tell the paragraph dialog that we've entered an insettext.
866                 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
867                 return DISPATCHED;
868         }
869
870         case LFUN_MOUSE_PRESS:
871                 lfunMousePress(cmd);
872                 return DISPATCHED;
873
874         case LFUN_MOUSE_MOTION:
875                 lfunMouseMotion(cmd);
876                 return DISPATCHED;
877
878         case LFUN_MOUSE_RELEASE:
879                 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
880
881         default:
882                 break;
883         }
884
885         bool was_empty = (paragraphs.begin()->empty() &&
886                           paragraphs.size() == 1);
887
888         no_selection = false;
889         RESULT result = UpdatableInset::localDispatch(cmd);
890         if (result != UNDISPATCHED)
891                 return DISPATCHED;
892
893         result = DISPATCHED;
894         if (cmd.action < 0 && cmd.argument.empty())
895                 return FINISHED;
896
897         if (the_locking_inset) {
898                 result = the_locking_inset->localDispatch(cmd);
899                 if (result == DISPATCHED_NOUPDATE)
900                         return result;
901                 else if (result == DISPATCHED) {
902                         updateLocal(bv, CURSOR_PAR, false);
903                         return result;
904                 } else if (result >= FINISHED) {
905                         switch (result) {
906                         case FINISHED_RIGHT:
907                                 moveRightIntern(bv, false, false);
908                                 result = DISPATCHED;
909                                 break;
910                         case FINISHED_UP:
911                                 if ((result = moveUp(bv)) >= FINISHED) {
912                                         updateLocal(bv, CURSOR, false);
913                                         bv->unlockInset(this);
914                                 }
915                                 break;
916                         case FINISHED_DOWN:
917                                 if ((result = moveDown(bv)) >= FINISHED) {
918                                         updateLocal(bv, CURSOR, false);
919                                         bv->unlockInset(this);
920                                 }
921                                 break;
922                         default:
923                                 result = DISPATCHED;
924                                 break;
925                         }
926                         the_locking_inset = 0;
927                         updateLocal(bv, CURSOR, false);
928                         // make sure status gets reset immediately
929                         bv->owner()->clearMessage();
930                         return result;
931                 }
932         }
933         int updwhat = 0;
934         int updflag = false;
935
936         // what type of update to do on a cursor movement
937         int cursor_update = CURSOR;
938
939         if (text_.selection.set())
940                 cursor_update = SELECTION;
941
942         switch (cmd.action) {
943
944         // Normal chars
945         case LFUN_SELFINSERT:
946                 if (bv->buffer()->isReadonly()) {
947 //          setErrorMessage(N_("Document is read only"));
948                         break;
949                 }
950                 if (!cmd.argument.empty()) {
951                         /* Automatically delete the currently selected
952                          * text and replace it with what is being
953                          * typed in now. Depends on lyxrc settings
954                          * "auto_region_delete", which defaults to
955                          * true (on). */
956 #if 0
957                         // This should not be needed here and is also WRONG!
958                         recordUndo(bv, Undo::INSERT, text_.cursor.par());
959 #endif
960                         bv->switchKeyMap();
961                         if (lyxrc.auto_region_delete) {
962                                 if (text_.selection.set()) {
963                                         text_.cutSelection(false, false);
964                                 }
965                         }
966                         text_.clearSelection();
967                         for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
968                                 bv->owner()->getIntl().getTransManager().
969                                         TranslateAndInsert(cmd.argument[i], &text_);
970                         }
971                 }
972                 text_.selection.cursor = text_.cursor;
973                 updwhat = CURSOR | CURSOR_PAR;
974                 updflag = true;
975                 result = DISPATCHED_NOUPDATE;
976                 break;
977
978         // cursor movements that need special handling
979
980         case LFUN_RIGHT:
981                 result = moveRight(bv);
982                 finishUndo();
983                 updwhat = cursor_update;
984                 break;
985         case LFUN_LEFT:
986                 finishUndo();
987                 result = moveLeft(bv);
988                 updwhat = cursor_update;
989                 break;
990         case LFUN_DOWN:
991                 finishUndo();
992                 result = moveDown(bv);
993                 updwhat = cursor_update;
994                 break;
995         case LFUN_UP:
996                 finishUndo();
997                 result = moveUp(bv);
998                 updwhat = cursor_update;
999                 break;
1000
1001         case LFUN_PRIOR:
1002                 if (crow() == text_.rows().begin())
1003                         result = FINISHED_UP;
1004                 else {
1005                         text_.cursorPrevious();
1006                         text_.clearSelection();
1007                         result = DISPATCHED_NOUPDATE;
1008                 }
1009                 updwhat = cursor_update;
1010                 break;
1011
1012         case LFUN_NEXT:
1013                 if (boost::next(crow()) == text_.rows().end())
1014                         result = FINISHED_DOWN;
1015                 else {
1016                         text_.cursorNext();
1017                         text_.clearSelection();
1018                         result = DISPATCHED_NOUPDATE;
1019                 }
1020                 updwhat = cursor_update;
1021                 break;
1022
1023         case LFUN_BACKSPACE: {
1024                 if (text_.selection.set())
1025                         text_.cutSelection(true, false);
1026                 else
1027                         text_.backspace();
1028                 updwhat = CURSOR_PAR;
1029                 updflag = true;
1030                 break;
1031         }
1032
1033         case LFUN_DELETE: {
1034                 if (text_.selection.set()) {
1035                         text_.cutSelection(true, false);
1036                 } else {
1037                         text_.Delete();
1038                 }
1039                 updwhat = CURSOR_PAR;
1040                 updflag = true;
1041                 break;
1042         }
1043
1044         case LFUN_CUT: {
1045                 text_.cutSelection(true, true);
1046                 updwhat = CURSOR_PAR;
1047                 updflag = true;
1048                 break;
1049         }
1050
1051         case LFUN_COPY:
1052                 finishUndo();
1053                 text_.copySelection();
1054                 updwhat = CURSOR_PAR;
1055                 break;
1056
1057         case LFUN_PASTESELECTION:
1058         {
1059                 string const clip(bv->getClipboard());
1060
1061                 if (clip.empty())
1062                         break;
1063                 if (cmd.argument == "paragraph") {
1064                         text_.insertStringAsParagraphs(clip);
1065                 } else {
1066                         text_.insertStringAsLines(clip);
1067                 }
1068                 // bug 393
1069                 text_.clearSelection();
1070
1071                 updwhat = CURSOR_PAR;
1072                 updflag = true;
1073                 break;
1074         }
1075
1076         case LFUN_PASTE: {
1077                 if (!autoBreakRows) {
1078                         if (CutAndPaste::nrOfParagraphs() > 1) {
1079 #ifdef WITH_WARNINGS
1080 #warning FIXME horrendously bad UI
1081 #endif
1082                                 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1083                                 break;
1084                         }
1085                 }
1086
1087                 replaceSelection(bv->getLyXText());
1088                 size_t sel_index = 0;
1089                 string const & arg = cmd.argument;
1090                 if (isStrUnsignedInt(arg)) {
1091                         size_t const paste_arg = strToUnsignedInt(arg);
1092 #warning FIXME Check if the arg is in the domain of available selections.
1093                         sel_index = paste_arg;
1094                 }
1095                 text_.pasteSelection(sel_index);
1096                 // bug 393
1097                 text_.clearSelection();
1098                 updwhat = CURSOR_PAR;
1099                 updflag = true;
1100                 break;
1101         }
1102
1103         case LFUN_BREAKPARAGRAPH:
1104                 if (!autoBreakRows) {
1105                         result = DISPATCHED;
1106                         break;
1107                 }
1108                 replaceSelection(bv->getLyXText());
1109                 text_.breakParagraph(paragraphs, 0);
1110                 updwhat = CURSOR | FULL;
1111                 updflag = true;
1112                 break;
1113
1114         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1115                 if (!autoBreakRows) {
1116                         result = DISPATCHED;
1117                         break;
1118                 }
1119                 replaceSelection(bv->getLyXText());
1120                 text_.breakParagraph(paragraphs, 1);
1121                 updwhat = CURSOR | FULL;
1122                 updflag = true;
1123                 break;
1124
1125         case LFUN_BREAKLINE: {
1126                 if (!autoBreakRows) {
1127                         result = DISPATCHED;
1128                         break;
1129                 }
1130
1131                 replaceSelection(bv->getLyXText());
1132                 text_.insertInset(new InsetNewline);
1133                 updwhat = CURSOR | CURSOR_PAR;
1134                 updflag = true;
1135                 break;
1136         }
1137
1138         case LFUN_LAYOUT:
1139                 // do not set layouts on non breakable textinsets
1140                 if (autoBreakRows) {
1141                         string cur_layout = cpar()->layout()->name();
1142
1143                         // Derive layout number from given argument (string)
1144                         // and current buffer's textclass (number). */
1145                         LyXTextClass const & tclass =
1146                                 bv->buffer()->params.getLyXTextClass();
1147                         string layout = cmd.argument;
1148                         bool hasLayout = tclass.hasLayout(layout);
1149
1150                         // If the entry is obsolete, use the new one instead.
1151                         if (hasLayout) {
1152                                 string const & obs =
1153                                         tclass[layout]->obsoleted_by();
1154                                 if (!obs.empty())
1155                                         layout = obs;
1156                         }
1157
1158                         // see if we found the layout number:
1159                         if (!hasLayout) {
1160                                 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1161                                 bv->owner()->dispatch(lf);
1162                                 break;
1163                         }
1164
1165                         if (cur_layout != layout) {
1166                                 cur_layout = layout;
1167                                 text_.setLayout(layout);
1168                                 bv->owner()->setLayout(cpar()->layout()->name());
1169                                 updwhat = CURSOR_PAR;
1170                                 updflag = true;
1171                         }
1172                 } else {
1173                         // reset the layout box
1174                         bv->owner()->setLayout(cpar()->layout()->name());
1175                 }
1176                 break;
1177         case LFUN_PARAGRAPH_SPACING:
1178                 // This one is absolutely not working. When fiddling with this
1179                 // it also seems to me that the paragraphs inside the insettext
1180                 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1181                 // FIXME: how old is this comment ? ...
1182         {
1183                 ParagraphList::iterator pit = text_.cursor.par();
1184                 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1185                 float cur_value = 1.0;
1186                 if (cur_spacing == Spacing::Other) {
1187                         cur_value = pit->params().spacing().getValue();
1188                 }
1189
1190                 istringstream istr(STRCONV(cmd.argument));
1191                 string tmp;
1192                 istr >> tmp;
1193                 Spacing::Space new_spacing = cur_spacing;
1194                 float new_value = cur_value;
1195                 if (tmp.empty()) {
1196                         lyxerr << "Missing argument to `paragraph-spacing'"
1197                                    << endl;
1198                 } else if (tmp == "single") {
1199                         new_spacing = Spacing::Single;
1200                 } else if (tmp == "onehalf") {
1201                         new_spacing = Spacing::Onehalf;
1202                 } else if (tmp == "double") {
1203                         new_spacing = Spacing::Double;
1204                 } else if (tmp == "other") {
1205                         new_spacing = Spacing::Other;
1206                         float tmpval = 0.0;
1207                         istr >> tmpval;
1208                         lyxerr << "new_value = " << tmpval << endl;
1209                         if (tmpval != 0.0)
1210                                 new_value = tmpval;
1211                 } else if (tmp == "default") {
1212                         new_spacing = Spacing::Default;
1213                 } else {
1214                         lyxerr << _("Unknown spacing argument: ")
1215                                    << cmd.argument << endl;
1216                 }
1217                 if (cur_spacing != new_spacing || cur_value != new_value) {
1218                         pit->params().spacing(Spacing(new_spacing, new_value));
1219                         updwhat = CURSOR_PAR;
1220                         updflag = true;
1221                 }
1222         }
1223         break;
1224
1225         // These need to do repaints but don't require
1226         // special handling otherwise. A *lot* of the
1227         // above could probably be done similarly ...
1228
1229         case LFUN_HOME:
1230         case LFUN_END:
1231         case LFUN_WORDLEFT:
1232         case LFUN_WORDRIGHT:
1233         // these two are really unhandled ...
1234         case LFUN_ENDBUF:
1235         case LFUN_BEGINNINGBUF:
1236                 updwhat = cursor_update;
1237                 if (!bv->dispatch(cmd))
1238                         result = UNDISPATCHED;
1239                 break;
1240
1241         case LFUN_RIGHTSEL:
1242         case LFUN_UPSEL:
1243         case LFUN_DOWNSEL:
1244         case LFUN_LEFTSEL:
1245         case LFUN_HOMESEL:
1246         case LFUN_ENDSEL:
1247         case LFUN_WORDLEFTSEL:
1248         case LFUN_WORDRIGHTSEL:
1249                 updwhat = SELECTION;
1250
1251                 // fallthrough
1252
1253         default:
1254                 if (!bv->dispatch(cmd))
1255                         result = UNDISPATCHED;
1256                 break;
1257         }
1258
1259         if (updwhat > 0)
1260                 updateLocal(bv, updwhat, updflag);
1261         /// If the action has deleted all text in the inset, we need to change the
1262         // language to the language of the surronding text.
1263         if (!was_empty && paragraphs.begin()->empty() &&
1264             paragraphs.size() == 1) {
1265                 LyXFont font(LyXFont::ALL_IGNORE);
1266                 font.setLanguage(bv->getParentLanguage(this));
1267                 setFont(bv, font, false);
1268         }
1269
1270         if (result >= FINISHED)
1271                 bv->unlockInset(this);
1272
1273         if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1274                 result = DISPATCHED;
1275         return result;
1276 }
1277
1278
1279 int InsetText::latex(Buffer const * buf, ostream & os,
1280                      LatexRunParams const & runparams) const
1281 {
1282         TexRow texrow;
1283         latexParagraphs(buf, paragraphs, os, texrow, runparams);
1284         return texrow.rows();
1285 }
1286
1287
1288 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1289 {
1290         unsigned int lines = 0;
1291
1292         ParagraphList::const_iterator beg = paragraphs.begin();
1293         ParagraphList::const_iterator end = paragraphs.end();
1294         ParagraphList::const_iterator it = beg;
1295         for (; it != end; ++it) {
1296                 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1297                 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1298                 os << tmp;
1299         }
1300         return lines;
1301 }
1302
1303
1304 int InsetText::linuxdoc(Buffer const * buf, ostream & os) const
1305 {
1306         ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1307         ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1308
1309         // There is a confusion between the empty paragraph and the default paragraph
1310         // The default paragraph is <p></p>, the empty paragraph is *empty*
1311         // Since none of the floats of linuxdoc accepts standard paragraphs
1312         // I disable them. I don't expect problems. (jamatos 2003/07/27)
1313         for (; pit != pend; ++pit) {
1314                 const string name = pit->layout()->latexname();
1315                 if (name != "p")
1316                         sgml::openTag(os, 1, 0, name);
1317                 buf->simpleLinuxDocOnePar(os, pit, 0);
1318                 if (name != "p")
1319                         sgml::closeTag(os, 1, 0, name);
1320         }
1321         return 0;
1322 }
1323
1324
1325 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1326 {
1327         unsigned int lines = 0;
1328
1329         vector<string> environment_stack(10);
1330         vector<string> environment_inner(10);
1331
1332         int const command_depth = 0;
1333         string item_name;
1334
1335         Paragraph::depth_type depth = 0; // paragraph depth
1336
1337         ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1338         ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1339
1340         for (; pit != pend; ++pit) {
1341                 string sgmlparam;
1342                 int desc_on = 0; // description mode
1343
1344                 LyXLayout_ptr const & style = pit->layout();
1345
1346                 // environment tag closing
1347                 for (; depth > pit->params().depth(); --depth) {
1348                         if (environment_inner[depth] != "!-- --") {
1349                                 item_name = "listitem";
1350                                 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1351                                 if (environment_inner[depth] == "varlistentry")
1352                                         lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1353                         }
1354                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1355                         environment_stack[depth].erase();
1356                         environment_inner[depth].erase();
1357                 }
1358
1359                 if (depth == pit->params().depth()
1360                    && environment_stack[depth] != style->latexname()
1361                    && !environment_stack[depth].empty()) {
1362                         if (environment_inner[depth] != "!-- --") {
1363                                 item_name= "listitem";
1364                                 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1365                                 if (environment_inner[depth] == "varlistentry")
1366                                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1367                         }
1368
1369                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1370
1371                         environment_stack[depth].erase();
1372                         environment_inner[depth].erase();
1373                 }
1374
1375                 // Write opening SGML tags.
1376                 switch (style->latextype) {
1377                 case LATEX_PARAGRAPH:
1378                         lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1379                         break;
1380
1381                 case LATEX_COMMAND:
1382                         buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1383                         return -1;
1384                         break;
1385
1386                 case LATEX_ENVIRONMENT:
1387                 case LATEX_ITEM_ENVIRONMENT:
1388                         if (depth < pit->params().depth()) {
1389                                 depth = pit->params().depth();
1390                                 environment_stack[depth].erase();
1391                         }
1392
1393                         if (environment_stack[depth] != style->latexname()) {
1394                                 if (environment_stack.size() == depth + 1) {
1395                                         environment_stack.push_back("!-- --");
1396                                         environment_inner.push_back("!-- --");
1397                                 }
1398                                 environment_stack[depth] = style->latexname();
1399                                 environment_inner[depth] = "!-- --";
1400                                 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1401                         } else {
1402                                 if (environment_inner[depth] != "!-- --") {
1403                                         item_name= "listitem";
1404                                         lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1405                                         if (environment_inner[depth] == "varlistentry")
1406                                                 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1407                                 }
1408                         }
1409
1410                         if (style->latextype == LATEX_ENVIRONMENT) {
1411                                 if (!style->latexparam().empty()) {
1412                                         if (style->latexparam() == "CDATA")
1413                                                 os << "<![CDATA[";
1414                                         else
1415                                           lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1416                                 }
1417                                 break;
1418                         }
1419
1420                         desc_on = (style->labeltype == LABEL_MANUAL);
1421
1422                         environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1423                         lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1424
1425                         item_name = desc_on ? "term" : "para";
1426                         lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1427
1428                         break;
1429                 default:
1430                         lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1431                         break;
1432                 }
1433
1434                 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1435
1436                 string end_tag;
1437                 // write closing SGML tags
1438                 switch (style->latextype) {
1439                 case LATEX_ENVIRONMENT:
1440                         if (!style->latexparam().empty()) {
1441                                 if (style->latexparam() == "CDATA")
1442                                         os << "]]>";
1443                                 else
1444                                         lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1445                         }
1446                         break;
1447                 case LATEX_ITEM_ENVIRONMENT:
1448                         if (desc_on == 1) break;
1449                         end_tag= "para";
1450                         lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1451                         break;
1452                 case LATEX_PARAGRAPH:
1453                         lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1454                         break;
1455                 default:
1456                         lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1457                         break;
1458                 }
1459         }
1460
1461         // Close open tags
1462         for (int d = depth; d >= 0; --d) {
1463                 if (!environment_stack[depth].empty()) {
1464                         if (environment_inner[depth] != "!-- --") {
1465                                 item_name = "listitem";
1466                                 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1467                                if (environment_inner[depth] == "varlistentry")
1468                                        lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1469                         }
1470
1471                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1472                 }
1473         }
1474
1475         return lines;
1476 }
1477
1478
1479 void InsetText::validate(LaTeXFeatures & features) const
1480 {
1481         for_each(paragraphs.begin(), paragraphs.end(),
1482                  boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1483 }
1484
1485
1486 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1487 {
1488         if (the_locking_inset) {
1489                 the_locking_inset->getCursor(bv, x, y);
1490                 return;
1491         }
1492         x = cx();
1493         y = cy() + InsetText::y();
1494 }
1495
1496
1497 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1498 {
1499         if (the_locking_inset) {
1500                 the_locking_inset->getCursorPos(bv, x, y);
1501                 return;
1502         }
1503         x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1504         y = cy() - TEXT_TO_INSET_OFFSET;
1505 }
1506
1507
1508 int InsetText::insetInInsetY() const
1509 {
1510         if (!the_locking_inset)
1511                 return 0;
1512
1513         return inset_y + the_locking_inset->insetInInsetY();
1514 }
1515
1516
1517 void InsetText::fitInsetCursor(BufferView * bv) const
1518 {
1519         if (the_locking_inset) {
1520                 the_locking_inset->fitInsetCursor(bv);
1521                 return;
1522         }
1523
1524         LyXFont const font = text_.getFont(cpar(), cpos());
1525
1526         int const asc = font_metrics::maxAscent(font);
1527         int const desc = font_metrics::maxDescent(font);
1528
1529         if (bv->fitLockedInsetCursor(cx(), cy(), asc, desc))
1530                 need_update |= FULL;
1531 }
1532
1533
1534 InsetOld::RESULT
1535 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1536 {
1537         if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1538                 return moveLeftIntern(bv, false, activate_inset, selecting);
1539         else
1540                 return moveRightIntern(bv, true, activate_inset, selecting);
1541 }
1542
1543
1544 InsetOld::RESULT
1545 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1546 {
1547         if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1548                 return moveRightIntern(bv, true, activate_inset, selecting);
1549         else
1550                 return moveLeftIntern(bv, false, activate_inset, selecting);
1551 }
1552
1553
1554 InsetOld::RESULT
1555 InsetText::moveRightIntern(BufferView * bv, bool front,
1556                            bool activate_inset, bool selecting)
1557 {
1558         ParagraphList::iterator c_par = cpar();
1559
1560         if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1561                 return FINISHED_RIGHT;
1562         if (activate_inset && checkAndActivateInset(bv, front))
1563                 return DISPATCHED;
1564         text_.cursorRight(bv);
1565         if (!selecting)
1566                 text_.clearSelection();
1567         return DISPATCHED_NOUPDATE;
1568 }
1569
1570
1571 InsetOld::RESULT
1572 InsetText::moveLeftIntern(BufferView * bv, bool front,
1573                           bool activate_inset, bool selecting)
1574 {
1575         if (cpar() == paragraphs.begin() && cpos() <= 0)
1576                 return FINISHED;
1577         text_.cursorLeft(bv);
1578         if (!selecting)
1579                 text_.clearSelection();
1580         if (activate_inset && checkAndActivateInset(bv, front))
1581                 return DISPATCHED;
1582         return DISPATCHED_NOUPDATE;
1583 }
1584
1585
1586 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1587 {
1588         if (crow() == text_.rows().begin())
1589                 return FINISHED_UP;
1590         text_.cursorUp(bv);
1591         text_.clearSelection();
1592         return DISPATCHED_NOUPDATE;
1593 }
1594
1595
1596 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1597 {
1598         if (boost::next(crow()) == text_.rows().end())
1599                 return FINISHED_DOWN;
1600         text_.cursorDown(bv);
1601         text_.clearSelection();
1602         return DISPATCHED_NOUPDATE;
1603 }
1604
1605
1606 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1607 {
1608         if (the_locking_inset) {
1609                 if (the_locking_inset->insetAllowed(inset))
1610                         return the_locking_inset->insertInset(bv, inset);
1611                 return false;
1612         }
1613         inset->setOwner(this);
1614         text_.insertInset(inset);
1615         bv->fitCursor();
1616         updateLocal(bv, CURSOR_PAR|CURSOR, true);
1617         return true;
1618 }
1619
1620
1621 bool InsetText::insetAllowed(InsetOld::Code code) const
1622 {
1623         // in_insetAllowed is a really gross hack,
1624         // to allow us to call the owner's insetAllowed
1625         // without stack overflow, which can happen
1626         // when the owner uses InsetCollapsable::insetAllowed()
1627         bool ret = true;
1628         if (in_insetAllowed)
1629                 return ret;
1630         in_insetAllowed = true;
1631         if (the_locking_inset)
1632                 ret = the_locking_inset->insetAllowed(code);
1633         else if (owner())
1634                 ret = owner()->insetAllowed(code);
1635         in_insetAllowed = false;
1636         return ret;
1637 }
1638
1639
1640 UpdatableInset * InsetText::getLockingInset() const
1641 {
1642         return the_locking_inset ? the_locking_inset->getLockingInset() :
1643                 const_cast<InsetText *>(this);
1644 }
1645
1646
1647 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1648 {
1649         if (c == lyxCode())
1650                 return this;
1651         if (the_locking_inset)
1652                 return the_locking_inset->getFirstLockingInsetOfType(c);
1653         return 0;
1654 }
1655
1656
1657 bool InsetText::showInsetDialog(BufferView * bv) const
1658 {
1659         if (the_locking_inset)
1660                 return the_locking_inset->showInsetDialog(bv);
1661         return false;
1662 }
1663
1664
1665 void InsetText::getLabelList(std::vector<string> & list) const
1666 {
1667         ParagraphList::const_iterator pit = paragraphs.begin();
1668         ParagraphList::const_iterator pend = paragraphs.end();
1669         for (; pit != pend; ++pit) {
1670                 InsetList::const_iterator beg = pit->insetlist.begin();
1671                 InsetList::const_iterator end = pit->insetlist.end();
1672                 for (; beg != end; ++beg)
1673                         beg->inset->getLabelList(list);
1674         }
1675 }
1676
1677
1678 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1679                         bool selectall)
1680 {
1681         if (the_locking_inset) {
1682                 the_locking_inset->setFont(bv, font, toggleall, selectall);
1683                 return;
1684         }
1685
1686         if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1687             || cpar()->empty()) {
1688                 text_.setFont(font, toggleall);
1689                 return;
1690         }
1691
1692
1693         if (text_.selection.set())
1694                 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1695
1696         if (selectall) {
1697                 text_.cursorTop();
1698                 text_.selection.cursor = text_.cursor;
1699                 text_.cursorBottom();
1700                 text_.setSelection();
1701         }
1702
1703         text_.toggleFree(font, toggleall);
1704
1705         if (selectall)
1706                 text_.clearSelection();
1707
1708         bv->fitCursor();
1709
1710         bool flag = (selectall || text_.selection.set());
1711
1712         if (flag)
1713                 updateLocal(bv, FULL, true);
1714         else
1715                 updateLocal(bv, CURSOR_PAR, true);
1716 }
1717
1718
1719 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1720 {
1721         InsetOld * inset = cpar()->getInset(cpos());
1722         if (!isHighlyEditableInset(inset))
1723                 return false;
1724         FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1725         inset->localDispatch(cmd);
1726         if (!the_locking_inset)
1727                 return false;
1728         updateLocal(bv, CURSOR, false);
1729         return true;
1730 }
1731
1732
1733 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1734                                       mouse_button::state button)
1735 {
1736         x -= drawTextXOffset;
1737         int dummyx = x;
1738         int dummyy = y + dim_.asc;
1739         InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1740         // we only do the edit() call if the inset was hit by the mouse
1741         // or if it is a highly editable inset. So we should call this
1742         // function from our own edit with button < 0.
1743         // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1744         // WRONG
1745         if (button == mouse_button::none && !isHighlyEditableInset(inset))
1746                 return false;
1747
1748         if (!inset)
1749                 return false;
1750         if (x < 0)
1751                 x = dim_.wid;
1752         if (y < 0)
1753                 y = dim_.des;
1754         inset_x = cix() - top_x + drawTextXOffset;
1755         inset_y = ciy() + drawTextYOffset;
1756         FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1757         inset->localDispatch(cmd);
1758         if (!the_locking_inset)
1759                 return false;
1760         updateLocal(bv, CURSOR, false);
1761         return true;
1762 }
1763
1764
1765 void InsetText::setParagraphData(ParagraphList const & plist)
1766 {
1767         // we have to unlock any locked inset otherwise we're in troubles
1768         the_locking_inset = 0;
1769
1770         // But it it makes no difference that is a lot better.
1771 #warning FIXME.
1772         // See if this can be simplified when std::list is in effect.
1773         paragraphs.clear();
1774
1775         ParagraphList::const_iterator it = plist.begin();
1776         ParagraphList::const_iterator end = plist.end();
1777         for (; it != end; ++it) {
1778                 paragraphs.push_back(*it);
1779                 paragraphs.back().setInsetOwner(this);
1780         }
1781
1782         reinitLyXText();
1783         need_update = INIT;
1784 }
1785
1786
1787 void InsetText::markNew(bool track_changes)
1788 {
1789         ParagraphList::iterator pit = paragraphs.begin();
1790         ParagraphList::iterator pend = paragraphs.end();
1791         for (; pit != pend; ++pit) {
1792                 if (track_changes) {
1793                         pit->trackChanges();
1794                 } else {
1795                         // no-op when not tracking
1796                         pit->cleanChanges();
1797                 }
1798         }
1799 }
1800
1801
1802 void InsetText::setText(string const & data, LyXFont const & font)
1803 {
1804         clear(false);
1805         for (unsigned int i = 0; i < data.length(); ++i)
1806                 paragraphs.begin()->insertChar(i, data[i], font);
1807         reinitLyXText();
1808 }
1809
1810
1811 void InsetText::setAutoBreakRows(bool flag)
1812 {
1813         if (flag != autoBreakRows) {
1814                 autoBreakRows = flag;
1815                 if (!flag)
1816                         removeNewlines();
1817                 need_update = INIT;
1818         }
1819 }
1820
1821
1822 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1823 {
1824         if (how != drawFrame_) {
1825                 drawFrame_ = how;
1826                 if (bv)
1827                         updateLocal(bv, DRAW_FRAME, false);
1828         }
1829 }
1830
1831
1832 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1833 {
1834         if (frame_color != col) {
1835                 frame_color = col;
1836                 if (bv)
1837                         updateLocal(bv, DRAW_FRAME, false);
1838         }
1839 }
1840
1841
1842 int InsetText::cx() const
1843 {
1844         int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1845         if (the_locking_inset) {
1846                 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1847                 if (font.isVisibleRightToLeft())
1848                         x -= the_locking_inset->width();
1849         }
1850         return x;
1851 }
1852
1853
1854 int InsetText::cix() const
1855 {
1856         int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1857         if (the_locking_inset) {
1858                 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1859                 if (font.isVisibleRightToLeft())
1860                         x -= the_locking_inset->width();
1861         }
1862         return x;
1863 }
1864
1865
1866 int InsetText::cy() const
1867 {
1868         return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1869 }
1870
1871
1872 int InsetText::ciy() const
1873 {
1874         return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1875 }
1876
1877
1878 pos_type InsetText::cpos() const
1879 {
1880         return text_.cursor.pos();
1881 }
1882
1883
1884 ParagraphList::iterator InsetText::cpar() const
1885 {
1886         return text_.cursor.par();
1887 }
1888
1889
1890 bool InsetText::cboundary() const
1891 {
1892         return text_.cursor.boundary();
1893 }
1894
1895
1896 RowList::iterator InsetText::crow() const
1897 {
1898         return text_.cursorRow();
1899 }
1900
1901
1902 LyXText * InsetText::getLyXText(BufferView const * bv,
1903                                 bool const recursive) const
1904 {
1905         setViewCache(bv);
1906         if (recursive && the_locking_inset)
1907                 return the_locking_inset->getLyXText(bv, true);
1908         return &text_;
1909 }
1910
1911
1912 void InsetText::setViewCache(BufferView const * bv) const
1913 {
1914         if (bv)
1915                 text_.bv_owner = const_cast<BufferView *>(bv);
1916 }
1917
1918
1919 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1920 {
1921         if (recursive) {
1922                 /// then remove all LyXText in text-insets
1923                 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1924                          const_cast<ParagraphList&>(paragraphs).end(),
1925                          boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1926         }
1927 }
1928
1929
1930 void InsetText::resizeLyXText(BufferView * bv, bool /*force*/) const
1931 {
1932         if (paragraphs.size() == 1 && paragraphs.begin()->empty()) {
1933                 // no data, resize not neccessary!
1934                 // we have to do this as a fixed width may have changed!
1935                 saveLyXTextState();
1936                 text_.init(bv);
1937                 restoreLyXTextState();
1938                 return;
1939         }
1940
1941         if (!bv)
1942                 return;
1943
1944         Assert(bv);
1945         setViewCache(bv);
1946
1947         saveLyXTextState();
1948
1949         for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1950                  const_cast<ParagraphList&>(paragraphs).end(),
1951                  boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1952
1953         text_.init(bv);
1954         restoreLyXTextState();
1955
1956         // seems to be unneeded
1957 #if 1
1958         if (the_locking_inset) {
1959                 inset_x = cix() - top_x + drawTextXOffset;
1960                 inset_y = ciy() + drawTextYOffset;
1961         }
1962
1963         text_.top_y(bv->screen().topCursorVisible(&text_));
1964         if (!owner()) {
1965                 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
1966                 // this will scroll the screen such that the cursor becomes visible
1967                 bv->updateScrollbar();
1968         } else {
1969                 need_update |= FULL;
1970         }
1971 #endif
1972 }
1973
1974
1975 void InsetText::reinitLyXText() const
1976 {
1977         BufferView * bv = text_.bv_owner;
1978
1979         if (!bv)
1980                 return;
1981
1982         saveLyXTextState();
1983
1984         for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1985                  const_cast<ParagraphList&>(paragraphs).end(),
1986                  boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1987
1988         text_.init(bv);
1989         restoreLyXTextState();
1990         if (the_locking_inset) {
1991                 inset_x = cix() - top_x + drawTextXOffset;
1992                 inset_y = ciy() + drawTextYOffset;
1993         }
1994         text_.top_y(bv->screen().topCursorVisible(&text_));
1995         if (!owner()) {
1996                 // this will scroll the screen such that the cursor becomes visible
1997                 bv->updateScrollbar();
1998         } else {
1999                 need_update = FULL;
2000         }
2001 }
2002
2003
2004 void InsetText::removeNewlines()
2005 {
2006         bool changed = false;
2007
2008         ParagraphList::iterator it = paragraphs.begin();
2009         ParagraphList::iterator end = paragraphs.end();
2010         for (; it != end; ++it) {
2011                 for (int i = 0; i < it->size(); ++i) {
2012                         if (it->isNewline(i)) {
2013                                 changed = true;
2014                                 it->erase(i);
2015                         }
2016                 }
2017         }
2018         if (changed)
2019                 reinitLyXText();
2020 }
2021
2022
2023 bool InsetText::nodraw() const
2024 {
2025         if (the_locking_inset)
2026                 return the_locking_inset->nodraw();
2027         return UpdatableInset::nodraw();
2028 }
2029
2030
2031 int InsetText::scroll(bool recursive) const
2032 {
2033         int sx = UpdatableInset::scroll(false);
2034
2035         if (recursive && the_locking_inset)
2036                 sx += the_locking_inset->scroll(recursive);
2037
2038         return sx;
2039 }
2040
2041
2042 void InsetText::clearSelection(BufferView * bv)
2043 {
2044         getLyXText(bv)->clearSelection();
2045 }
2046
2047
2048 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2049 {
2050         Painter & pain = bv->painter();
2051         int w = dim_.wid;
2052         int h = dim_.asc + dim_.des;
2053         int ty = baseline - dim_.asc;
2054
2055         if (ty < 0) {
2056                 h += ty;
2057                 ty = 0;
2058         }
2059         if ((ty + h) > pain.paperHeight())
2060                 h = pain.paperHeight();
2061         if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2062                 w = pain.paperWidth();
2063         pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2064         need_update = FULL;
2065 }
2066
2067
2068 ParagraphList * InsetText::getParagraphs(int i) const
2069 {
2070         return (i == 0) ? const_cast<ParagraphList*>(&paragraphs) : 0;
2071 }
2072
2073
2074 LyXCursor const & InsetText::cursor(BufferView * bv) const
2075 {
2076         if (the_locking_inset)
2077                 return the_locking_inset->cursor(bv);
2078         return getLyXText(bv)->cursor;
2079 }
2080
2081
2082 InsetOld * InsetText::getInsetFromID(int id_arg) const
2083 {
2084         if (id_arg == id())
2085                 return const_cast<InsetText *>(this);
2086
2087         ParagraphList::const_iterator pit = paragraphs.begin();
2088         ParagraphList::const_iterator pend = paragraphs.end();
2089         for (; pit != pend; ++pit) {
2090                 InsetList::const_iterator it = pit->insetlist.begin();
2091                 InsetList::const_iterator end = pit->insetlist.end();
2092                 for (; it != end; ++it) {
2093                         if (it->inset->id() == id_arg)
2094                                 return it->inset;
2095                         InsetOld * in = it->inset->getInsetFromID(id_arg);
2096                         if (in)
2097                                 return in;
2098                 }
2099         }
2100         return 0;
2101 }
2102
2103
2104 WordLangTuple const
2105 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2106 {
2107         WordLangTuple word;
2108         if (the_locking_inset) {
2109                 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2110                 if (!word.word().empty()) {
2111                         value += cy();
2112                         return word;
2113                 }
2114                 // we have to go on checking so move cursor to the next char
2115                 text_.cursor.pos(text_.cursor.pos() + 1);
2116         }
2117         word = text_.selectNextWordToSpellcheck(value);
2118         if (word.word().empty())
2119                 bv->unlockInset(const_cast<InsetText *>(this));
2120         else
2121                 value = cy();
2122         return word;
2123 }
2124
2125
2126 void InsetText::selectSelectedWord(BufferView * bv)
2127 {
2128         if (the_locking_inset) {
2129                 the_locking_inset->selectSelectedWord(bv);
2130                 return;
2131         }
2132         getLyXText(bv)->selectSelectedWord();
2133         updateLocal(bv, SELECTION, false);
2134 }
2135
2136
2137 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2138 {
2139         if (the_locking_inset)
2140                 the_locking_inset->toggleSelection(bv, kill_selection);
2141
2142         int x = top_x + TEXT_TO_INSET_OFFSET;
2143
2144         RowList::iterator rit = text_.rows().begin();
2145         RowList::iterator end = text_.rows().end();
2146         int y_offset = top_baseline - rit->ascent_of_text();
2147         int y = y_offset;
2148         while (rit != end && y + rit->height() <= 0) {
2149                 y += rit->height();
2150                 ++rit;
2151         }
2152         if (y_offset < 0)
2153                 y_offset = y;
2154
2155         if (need_update & SELECTION)
2156                 need_update = NONE;
2157         bv->screen().toggleSelection(&text_, bv, kill_selection, y_offset, x);
2158 }
2159
2160
2161 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2162 {
2163         if (the_locking_inset) {
2164                 if (the_locking_inset->nextChange(bv, length))
2165                         return true;
2166                 text_.cursorRight(true);
2167         }
2168         lyx::find::SearchResult result =
2169                 lyx::find::findNextChange(bv, &text_, length);
2170
2171         if (result == lyx::find::SR_FOUND) {
2172                 LyXCursor cur = text_.cursor;
2173                 bv->unlockInset(bv->theLockingInset());
2174                 if (bv->lockInset(this))
2175                         locked = true;
2176                 text_.cursor = cur;
2177                 text_.setSelectionRange(length);
2178                 updateLocal(bv, SELECTION, false);
2179         }
2180         return result != lyx::find::SR_NOT_FOUND;
2181 }
2182
2183
2184 bool InsetText::searchForward(BufferView * bv, string const & str,
2185                               bool cs, bool mw)
2186 {
2187         if (the_locking_inset) {
2188                 if (the_locking_inset->searchForward(bv, str, cs, mw))
2189                         return true;
2190                 text_.cursorRight(true);
2191         }
2192         lyx::find::SearchResult result =
2193                 lyx::find::find(bv, &text_, str, true, cs, mw);
2194
2195         if (result == lyx::find::SR_FOUND) {
2196                 LyXCursor cur = text_.cursor;
2197                 bv->unlockInset(bv->theLockingInset());
2198                 if (bv->lockInset(this))
2199                         locked = true;
2200                 text_.cursor = cur;
2201                 text_.setSelectionRange(str.length());
2202                 updateLocal(bv, SELECTION, false);
2203         }
2204         return result != lyx::find::SR_NOT_FOUND;
2205 }
2206
2207
2208 bool InsetText::searchBackward(BufferView * bv, string const & str,
2209                                bool cs, bool mw)
2210 {
2211         if (the_locking_inset) {
2212                 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2213                         return true;
2214         }
2215         if (!locked) {
2216                 ParagraphList::iterator pit = paragraphs.begin();
2217                 ParagraphList::iterator pend = paragraphs.end();
2218
2219                 while (boost::next(pit) != pend)
2220                         ++pit;
2221
2222                 text_.setCursor(pit, pit->size());
2223         }
2224         lyx::find::SearchResult result =
2225                 lyx::find::find(bv, &text_, str, false, cs, mw);
2226
2227         if (result == lyx::find::SR_FOUND) {
2228                 LyXCursor cur = text_.cursor;
2229                 bv->unlockInset(bv->theLockingInset());
2230                 if (bv->lockInset(this))
2231                         locked = true;
2232                 text_.cursor = cur;
2233                 text_.setSelectionRange(str.length());
2234                 updateLocal(bv, SELECTION, false);
2235         }
2236         return result != lyx::find::SR_NOT_FOUND;
2237 }
2238
2239
2240 bool InsetText::checkInsertChar(LyXFont & font)
2241 {
2242         if (owner())
2243                 return owner()->checkInsertChar(font);
2244         return true;
2245 }
2246
2247
2248 void InsetText::collapseParagraphs(BufferView * bv)
2249 {
2250         while (paragraphs.size() > 1) {
2251                 ParagraphList::iterator first_par = paragraphs.begin();
2252                 ParagraphList::iterator next_par = boost::next(first_par);
2253                 size_t const first_par_size = first_par->size();
2254
2255                 if (!first_par->empty() &&
2256                     !next_par->empty() &&
2257                     !first_par->isSeparator(first_par_size - 1)) {
2258                         first_par->insertChar(first_par_size, ' ');
2259                 }
2260
2261                 if (text_.selection.set()) {
2262                         if (text_.selection.start.par() == next_par) {
2263                                 text_.selection.start.par(first_par);
2264                                 text_.selection.start.pos(
2265                                         text_.selection.start.pos() + first_par_size);
2266                         }
2267                         if (text_.selection.end.par() == next_par) {
2268                                 text_.selection.end.par(first_par);
2269                                 text_.selection.end.pos(
2270                                         text_.selection.end.pos() + first_par_size);
2271                         }
2272                 }
2273
2274                 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2275         }
2276         reinitLyXText();
2277 }
2278
2279
2280 void InsetText::getDrawFont(LyXFont & font) const
2281 {
2282         if (!owner())
2283                 return;
2284         owner()->getDrawFont(font);
2285 }
2286
2287
2288 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2289 {
2290 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2291 // And it probably does. You have to take a look at this John. (Lgb)
2292 #warning John, have a look here. (Lgb)
2293         ParagraphList::iterator pit = plist.begin();
2294         ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2295         ++pit;
2296         mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2297
2298         ParagraphList::iterator pend = plist.end();
2299         for (; pit != pend; ++pit) {
2300                 paragraphs.push_back(*pit);
2301         }
2302
2303         reinitLyXText();
2304 }
2305
2306
2307 void InsetText::addPreview(PreviewLoader & loader) const
2308 {
2309         ParagraphList::const_iterator pit = paragraphs.begin();
2310         ParagraphList::const_iterator pend = paragraphs.end();
2311
2312         for (; pit != pend; ++pit) {
2313                 InsetList::const_iterator it  = pit->insetlist.begin();
2314                 InsetList::const_iterator end = pit->insetlist.end();
2315                 for (; it != end; ++it) {
2316                         it->inset->addPreview(loader);
2317                 }
2318         }
2319 }