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