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