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