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