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