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