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