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