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