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