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