]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
removing update calls
[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 #include "insetnewline.h"
15
16 #include "buffer.h"
17 #include "bufferparams.h"
18 #include "BufferView.h"
19 #include "CutAndPaste.h"
20 #include "cursor.h"
21 #include "debug.h"
22 #include "dispatchresult.h"
23 #include "errorlist.h"
24 #include "funcrequest.h"
25 #include "gettext.h"
26 #include "intl.h"
27 #include "LColor.h"
28 #include "lyxfind.h"
29 #include "lyxlex.h"
30 #include "lyxrc.h"
31 #include "lyxtext.h"
32 #include "metricsinfo.h"
33 #include "output_docbook.h"
34 #include "output_latex.h"
35 #include "output_linuxdoc.h"
36 #include "output_plaintext.h"
37 #include "paragraph.h"
38 #include "paragraph_funcs.h"
39 #include "ParagraphParameters.h"
40 #include "rowpainter.h"
41 #include "sgml.h"
42 #include "texrow.h"
43 #include "undo.h"
44
45 #include "frontends/Alert.h"
46 #include "frontends/font_metrics.h"
47 #include "frontends/LyXView.h"
48 #include "frontends/Painter.h"
49
50 #include "support/lyxalgo.h" // lyx::count
51
52 #include <boost/bind.hpp>
53
54 using bv_funcs::replaceSelection;
55
56 using lyx::pos_type;
57
58 using lyx::graphics::PreviewLoader;
59
60 using lyx::support::isStrUnsignedInt;
61 using lyx::support::strToUnsignedInt;
62
63 using std::endl;
64 using std::for_each;
65 using std::max;
66 using std::string;
67 using std::auto_ptr;
68 using std::ostream;
69 using std::vector;
70
71
72 InsetText::InsetText(BufferParams const & bp)
73         : UpdatableInset(),
74           paragraphs(1),
75           autoBreakRows_(false),
76           drawFrame_(NEVER),
77           frame_color_(LColor::insetframe),
78           text_(0, this, true, paragraphs)
79 {
80         textwidth_ = 0; // broken
81         paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
82         if (bp.tracking_changes)
83                 paragraphs.begin()->trackChanges();
84         init();
85 }
86
87
88 InsetText::InsetText(InsetText const & in)
89         : UpdatableInset(in),
90           text_(in.text_.bv_owner, this, true, paragraphs)
91 {
92         // this is ugly...
93         operator=(in);
94 }
95
96
97 void InsetText::operator=(InsetText const & in)
98 {
99         UpdatableInset::operator=(in);
100         paragraphs = in.paragraphs;
101         autoBreakRows_ = in.autoBreakRows_;
102         drawFrame_ = in.drawFrame_;
103         frame_color_ = in.frame_color_;
104         textwidth_ = in.textwidth_;
105         text_ = LyXText(in.text_.bv_owner, this, true, paragraphs);
106         init();
107 }
108
109
110 void InsetText::init()
111 {
112         ParagraphList::iterator pit = paragraphs.begin();
113         ParagraphList::iterator end = paragraphs.end();
114         for (; pit != end; ++pit)
115                 pit->setInsetOwner(this);
116         text_.paragraphs_ = &paragraphs;
117         no_selection = true;
118         old_par = -1;
119         in_insetAllowed = false;
120         mouse_x = 0;
121         mouse_y = 0;
122 }
123
124
125 void InsetText::clear(bool just_mark_erased)
126 {
127         if (just_mark_erased) {
128                 ParagraphList::iterator it = paragraphs.begin();
129                 ParagraphList::iterator end = paragraphs.end();
130                 for (; it != end; ++it)
131                         it->markErased();
132                 return;
133         }
134
135         // This is a gross hack...
136         LyXLayout_ptr old_layout = paragraphs.begin()->layout();
137
138         paragraphs.clear();
139         paragraphs.push_back(Paragraph());
140         paragraphs.begin()->setInsetOwner(this);
141         paragraphs.begin()->layout(old_layout);
142 }
143
144
145 auto_ptr<InsetBase> InsetText::clone() const
146 {
147         return auto_ptr<InsetBase>(new InsetText(*this));
148 }
149
150
151 void InsetText::write(Buffer const & buf, ostream & os) const
152 {
153         os << "Text\n";
154         writeParagraphData(buf, os);
155 }
156
157
158 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
159 {
160         ParagraphList::const_iterator it = paragraphs.begin();
161         ParagraphList::const_iterator end = paragraphs.end();
162         Paragraph::depth_type dth = 0;
163         for (; it != end; ++it) {
164                 it->write(buf, os, buf.params(), dth);
165         }
166 }
167
168
169 void InsetText::read(Buffer const & buf, LyXLex & lex)
170 {
171         string token;
172         Paragraph::depth_type depth = 0;
173
174         clear(false);
175
176 #warning John, look here. Doesnt make much sense.
177         if (buf.params().tracking_changes)
178                 paragraphs.begin()->trackChanges();
179
180         // delete the initial paragraph
181         Paragraph oldpar = *paragraphs.begin();
182         paragraphs.clear();
183         ParagraphList::iterator pit = paragraphs.begin();
184
185         while (lex.isOK()) {
186                 lex.nextToken();
187                 token = lex.getString();
188                 if (token.empty())
189                         continue;
190                 if (token == "\\end_inset") {
191                         break;
192                 }
193
194                 if (token == "\\end_document") {
195                         lex.printError("\\end_document read in inset! Error in document!");
196                         return;
197                 }
198
199                 // FIXME: ugly.
200                 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
201         }
202
203         pit = paragraphs.begin();
204         ParagraphList::iterator const end = paragraphs.end();
205         for (; pit != end; ++pit)
206                 pit->setInsetOwner(this);
207
208         if (token != "\\end_inset") {
209                 lex.printError("Missing \\end_inset at this point. "
210                                            "Read: `$$Token'");
211         }
212
213         // sanity check
214         // ensure we have at least one par.
215         if (paragraphs.empty())
216                 paragraphs.push_back(oldpar);
217 }
218
219
220 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
221 {
222         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
223         textwidth_ = max(40, mi.base.textwidth - 30);
224         BufferView * bv = mi.base.bv;
225         setViewCache(bv);
226         text_.metrics(mi, dim);
227         dim.asc += TEXT_TO_INSET_OFFSET;
228         dim.des += TEXT_TO_INSET_OFFSET;
229         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
230         dim_ = dim;
231 }
232
233
234 int InsetText::textWidth() const
235 {
236         return textwidth_;
237 }
238
239
240 void InsetText::draw(PainterInfo & pi, int x, int y) const
241 {
242         // update our idea of where we are. Clearly, we should
243         // not have to know this information.
244         xo_ = x;
245         yo_ = y;
246
247         int const start_x = x;
248
249         BufferView * bv = pi.base.bv;
250         Painter & pain = pi.pain;
251
252         // repaint the background if needed
253         if (backgroundColor() != LColor::background)
254                 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
255
256         bv->hideCursor();
257
258         if (!owner())
259                 x += scroll();
260
261         x += TEXT_TO_INSET_OFFSET;
262
263         paintTextInset(*bv, text_, x, y);
264
265         if (drawFrame_ == ALWAYS || drawFrame_ == LOCKED)
266                 drawFrame(pain, start_x);
267 }
268
269
270 void InsetText::drawFrame(Painter & pain, int x) const
271 {
272         int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
273         int const frame_x = x + ttoD2;
274         int const frame_y = yo_ - dim_.asc + ttoD2;
275         int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
276         int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
277         pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
278 }
279
280
281 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
282 {
283         if (!bv)
284                 return;
285
286         if (!autoBreakRows_ && paragraphs.size() > 1)
287                 collapseParagraphs(bv);
288
289         if (!text_.selection.set())
290                 text_.selection.cursor = text_.cursor;
291
292 //      bv->fitCursor();
293         bv->owner()->view_state_changed();
294         bv->owner()->updateMenubar();
295         bv->owner()->updateToolbar();
296         if (old_par != text_.cursor.par()) {
297                 bv->owner()->setLayout(cpar()->layout()->name());
298                 old_par = text_.cursor.par();
299         }
300 }
301
302
303 string const InsetText::editMessage() const
304 {
305         return _("Opened Text Inset");
306 }
307
308
309 void InsetText::sanitizeEmptyText(BufferView * bv)
310 {
311         if (paragraphs.size() == 1
312                         && paragraphs.begin()->empty() 
313                         && bv->getParentLanguage(this) != text_.current_font.language()) {
314                 LyXFont font(LyXFont::ALL_IGNORE);
315                 font.setLanguage(bv->getParentLanguage(this));
316                 setFont(bv, font, false);
317         }
318 }
319
320
321 extern LCursor theTempCursor;
322
323 void InsetText::lfunMousePress(FuncRequest const & cmd)
324 {
325         lyxerr << "InsetText::lfunMousePress, inset: " << this << endl;
326         no_selection = true;
327
328         // use this to check mouse motion for selection
329         mouse_x = cmd.x;
330         mouse_y = cmd.y;
331
332         BufferView * bv = cmd.view();
333         no_selection = false;
334         text_.clearSelection();
335
336         // set global cursor
337         bv->cursor() = theTempCursor;
338         lyxerr << "new global cursor: \n" << bv->cursor() << endl;
339         text_.setCursorFromCoordinates(cmd.x, cmd.y);
340 }
341
342
343 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
344 {
345         lyxerr << "InsetText::lfunMouseMotion, inset: " << this << endl;
346         if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
347                 return;
348
349         BufferView * bv = cmd.view();
350         LyXCursor cur = text_.cursor;
351         text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
352         bv->x_target(text_.cursor.x());
353         if (cur != text_.cursor) {
354                 text_.setSelection();
355                 updateLocal(bv, false);
356         }
357 }
358
359
360 void InsetText::lfunMouseRelease(FuncRequest const &)
361 {
362         lyxerr << "InsetText::lfunMouseRelease, inset: " << this << endl;
363         no_selection = true;
364 }
365
366
367 void InsetText::edit(BufferView * bv, bool left)
368 {
369         lyxerr << "InsetText: edit left/right" << endl;
370         setViewCache(bv);
371
372         old_par = -1;
373
374         if (left)
375                 text_.setCursorIntern(0, 0);
376         else
377                 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
378
379         sanitizeEmptyText(bv);
380         updateLocal(bv, false);
381         bv->updateParagraphDialog();
382 }
383
384
385 void InsetText::edit(BufferView * bv, int x, int y)
386 {
387         lyxerr << "InsetText::edit xy" << endl;
388         old_par = -1;
389         sanitizeEmptyText(bv);
390         text_.setCursorFromCoordinates(x, y + dim_.asc);
391         text_.cursor.x(text_.cursor.x());
392         bv->x_target(text_.cursor.x());
393
394         text_.clearSelection();
395         finishUndo();
396
397         updateLocal(bv, false);
398         bv->updateParagraphDialog();
399 }
400
401
402 DispatchResult InsetText::priv_dispatch(FuncRequest const & cmd,
403         idx_type &, pos_type &)
404 {
405         lyxerr << "InsetText::priv_dispatch (begin), act: "
406                 << cmd.action << " " << endl;
407         BufferView * bv = cmd.view();
408         setViewCache(bv);
409         DispatchResult result;
410         result.dispatched(true);
411
412         bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
413         if (cmd.action != LFUN_MOUSE_PRESS
414                         && cmd.action != LFUN_MOUSE_MOTION
415                         && cmd.action != LFUN_MOUSE_RELEASE)
416                 no_selection = false;
417
418         switch (cmd.action) {
419         case LFUN_MOUSE_PRESS:
420                 lfunMousePress(cmd);
421                 result = DispatchResult(true, true);    
422                 break;
423
424         case LFUN_MOUSE_MOTION:
425                 lfunMouseMotion(cmd);
426                 result = DispatchResult(true, true);
427                 break;
428
429         case LFUN_MOUSE_RELEASE:
430                 lfunMouseRelease(cmd);
431                 result = DispatchResult(true, true);
432                 break;
433
434         case LFUN_SELFINSERT:
435                 if (bv->buffer()->isReadonly()) {
436                         // setErrorMessage(N_("Document is read only"));
437                         break;
438                 }
439                 if (!cmd.argument.empty()) {
440                         /* Automatically delete the currently selected
441                          * text and replace it with what is being
442                          * typed in now. Depends on lyxrc settings
443                          * "auto_region_delete", which defaults to
444                          * true (on). */
445 #if 0
446                         // This should not be needed here and is also WRONG!
447                         recordUndo(bv, Undo::INSERT, text_.cursorPar());
448 #endif
449                         bv->switchKeyMap();
450
451                         if (lyxrc.auto_region_delete && text_.selection.set())
452                                 text_.cutSelection(false, false);
453                         text_.clearSelection();
454
455                         for (string::size_type i = 0; i < cmd.argument.length(); ++i)
456                                 bv->owner()->getIntl().getTransManager().
457                                         TranslateAndInsert(cmd.argument[i], &text_);
458                 }
459                 text_.selection.cursor = text_.cursor;
460                 result.dispatched(true);
461                 result.update(true);
462                 break;
463
464         case LFUN_RIGHT:
465                 result = moveRight(bv);
466                 finishUndo();
467                 break;
468
469         case LFUN_LEFT:
470                 finishUndo();
471                 result = moveLeft(bv);
472                 break;
473
474         case LFUN_DOWN:
475                 finishUndo();
476                 result = moveDown(bv);
477                 break;
478
479         case LFUN_UP:
480                 finishUndo();
481                 result = moveUp(bv);
482                 break;
483
484         case LFUN_PRIOR:
485                 if (crow() == text_.firstRow()) {
486                         result.val(FINISHED_UP);
487                 } else {
488                         text_.cursorPrevious();
489                         text_.clearSelection();
490                         result.dispatched(true);
491                 }
492                 break;
493
494         case LFUN_NEXT:
495                 if (crow() == text_.lastRow()) {
496                         result.val(FINISHED_DOWN);
497                 } else {
498                         text_.cursorNext();
499                         text_.clearSelection();
500                         result.dispatched(true);
501                 }
502                 break;
503
504         case LFUN_BACKSPACE:
505                 if (text_.selection.set())
506                         text_.cutSelection(true, false);
507                 else
508                         text_.backspace();
509 #warning should be also set dispatched here?
510                 result.update(true);
511                 break;
512
513         case LFUN_DELETE:
514                 if (text_.selection.set())
515                         text_.cutSelection(true, false);
516                 else
517                         text_.Delete();
518 #warning should be also set dispatched here?
519                 result.update(true);
520                 break;
521
522         case LFUN_PASTE:
523                 if (!autoBreakRows_) {
524                         if (CutAndPaste::nrOfParagraphs() > 1) {
525 #ifdef WITH_WARNINGS
526 #warning FIXME horrendously bad UI
527 #endif
528                                 Alert::error(_("Paste failed"),
529                                         _("Cannot include more than one paragraph."));
530                         }
531                 } else {
532                         replaceSelection(bv->getLyXText());
533                         size_t sel_index = 0;
534                         string const & arg = cmd.argument;
535                         if (isStrUnsignedInt(arg)) {
536 #warning FIXME Check if the arg is in the domain of available selections.
537                                 sel_index = strToUnsignedInt(arg);
538                         }
539                         text_.pasteSelection(sel_index);
540                         // bug 393
541                         text_.clearSelection();
542 #warning should be also set dispatched here?
543                         result.update(true);
544                 }
545                 break;
546
547         case LFUN_BREAKPARAGRAPH:
548                 if (!autoBreakRows_) {
549                         result.dispatched(true);
550                         result.update(true);
551                 } else {
552                         replaceSelection(bv->getLyXText());
553                         text_.breakParagraph(paragraphs, 0);
554 #warning should be also set dispatched here?
555                         result.update(true);
556                 }
557                 break;
558
559         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
560                 if (!autoBreakRows_) {
561                         result.dispatched(true);
562                         result.update(true);
563                 } else {
564                         replaceSelection(bv->getLyXText());
565                         text_.breakParagraph(paragraphs, 1);
566 #warning should be also set dispatched here?
567                         result.update(true);
568                 }
569                 break;
570
571         case LFUN_BREAKLINE: {
572                 if (!autoBreakRows_) {
573                         result.dispatched(true);
574                         result.update(true);
575                 } else {
576                         replaceSelection(bv->getLyXText());
577                         auto_ptr<InsetNewline> ins(new InsetNewline);
578                         text_.insertInset(ins.release());
579 #warning should be also set dispatched here?
580                         result.update(true);
581                 }
582                 break;
583         }
584
585         case LFUN_LAYOUT:
586                 // do not set layouts on non breakable textinsets
587                 if (autoBreakRows_) {
588                         string cur_layout = cpar()->layout()->name();
589
590                         // Derive layout number from given argument (string)
591                         // and current buffer's textclass (number).
592                         LyXTextClass const & tclass =
593                                 bv->buffer()->params().getLyXTextClass();
594                         string layout = cmd.argument;
595                         bool hasLayout = tclass.hasLayout(layout);
596
597                         // If the entry is obsolete, use the new one instead.
598                         if (hasLayout) {
599                                 string const & obs = tclass[layout]->obsoleted_by();
600                                 if (!obs.empty())
601                                         layout = obs;
602                         }
603
604                         // see if we found the layout number:
605                         if (!hasLayout) {
606                                 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
607                                 bv->owner()->dispatch(lf);
608                                 break;
609                         }
610
611                         if (cur_layout != layout) {
612                                 cur_layout = layout;
613                                 text_.setLayout(layout);
614                                 bv->owner()->setLayout(cpar()->layout()->name());
615 #warning should be also set dispatched here?
616                                 result.update(true);
617                         }
618                 } else {
619                         // reset the layout box
620                         bv->owner()->setLayout(cpar()->layout()->name());
621                 }
622                 break;
623
624         default:
625                 result = text_.dispatch(cmd);
626                 break;
627         }
628
629         /// If the action has deleted all text in the inset, we need to change the
630         // language to the language of the surronding text.
631         if (!was_empty && paragraphs.begin()->empty() &&
632             paragraphs.size() == 1) {
633                 LyXFont font(LyXFont::ALL_IGNORE);
634                 font.setLanguage(bv->getParentLanguage(this));
635                 setFont(bv, font, false);
636         }
637
638         lyxerr << "InsetText::priv_dispatch (end)" << endl;
639         return result;
640 }
641
642
643 int InsetText::latex(Buffer const & buf, ostream & os,
644                      OutputParams const & runparams) const
645 {
646         TexRow texrow;
647         latexParagraphs(buf, paragraphs, os, texrow, runparams);
648         return texrow.rows();
649 }
650
651
652 int InsetText::plaintext(Buffer const & buf, ostream & os,
653                      OutputParams const & runparams) const
654 {
655         ParagraphList::const_iterator beg = paragraphs.begin();
656         ParagraphList::const_iterator end = paragraphs.end();
657         ParagraphList::const_iterator it = beg;
658         for (; it != end; ++it)
659                 asciiParagraph(buf, *it, os, runparams, it == beg);
660
661         //FIXME: Give the total numbers of lines
662         return 0;
663 }
664
665
666 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
667                         OutputParams const & runparams) const
668 {
669         linuxdocParagraphs(buf, paragraphs, os, runparams);
670         return 0;
671 }
672
673
674 int InsetText::docbook(Buffer const & buf, ostream & os,
675                        OutputParams const & runparams) const
676 {
677         docbookParagraphs(buf, paragraphs, os, runparams);
678         return 0;
679 }
680
681
682 void InsetText::validate(LaTeXFeatures & features) const
683 {
684         for_each(paragraphs.begin(), paragraphs.end(),
685                  boost::bind(&Paragraph::validate, _1, boost::ref(features)));
686 }
687
688
689 void InsetText::getCursorPos(BufferView *, int & x, int & y) const
690 {
691         x = cx() - xo_;
692         y = cy();
693 }
694
695
696 int InsetText::insetInInsetY() const
697 {
698         return 0;
699 }
700
701
702 void InsetText::fitInsetCursor(BufferView * bv) const
703 {
704         LyXFont const font = text_.getFont(cpar(), cpos());
705         int const asc = font_metrics::maxAscent(font);
706         int const desc = font_metrics::maxDescent(font);
707         bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
708 }
709
710
711 DispatchResult InsetText::moveRight(BufferView * bv)
712 {
713         if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
714                 return moveLeftIntern(bv, false, true, false);
715         else
716                 return moveRightIntern(bv, true, true, false);
717 }
718
719
720 DispatchResult InsetText::moveLeft(BufferView * bv)
721 {
722         if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
723                 return moveRightIntern(bv, true, true, false);
724         else
725                 return moveLeftIntern(bv, false, true, false);
726 }
727
728
729 DispatchResult InsetText::moveRightIntern(BufferView * bv, bool front,
730                            bool activate_inset, bool selecting)
731 {
732         ParagraphList::iterator c_par = cpar();
733         if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
734                 return DispatchResult(false, FINISHED_RIGHT);
735         if (activate_inset && checkAndActivateInset(bv, front))
736                 return DispatchResult(true, true);
737         text_.cursorRight(bv);
738         if (!selecting)
739                 text_.clearSelection();
740         return DispatchResult(true);
741 }
742
743
744 DispatchResult InsetText::moveLeftIntern(BufferView * bv, bool front,
745                           bool activate_inset, bool selecting)
746 {
747         if (cpar() == paragraphs.begin() && cpos() <= 0)
748                 return DispatchResult(false, FINISHED);
749         text_.cursorLeft(bv);
750         if (!selecting)
751                 text_.clearSelection();
752         if (activate_inset && checkAndActivateInset(bv, front))
753                 return DispatchResult(true, true);
754         return DispatchResult(true);
755 }
756
757
758 DispatchResult InsetText::moveUp(BufferView * bv)
759 {
760         if (crow() == text_.firstRow())
761                 return DispatchResult(false, FINISHED_UP);
762         text_.cursorUp(bv);
763         text_.clearSelection();
764         return DispatchResult(true);
765 }
766
767
768 DispatchResult InsetText::moveDown(BufferView * bv)
769 {
770         if (crow() == text_.lastRow())
771                 return DispatchResult(false, FINISHED_DOWN);
772         text_.cursorDown(bv);
773         text_.clearSelection();
774         return DispatchResult(true);
775 }
776
777
778 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
779 {
780         inset->setOwner(this);
781         text_.insertInset(inset);
782 //      bv->fitCursor();
783         updateLocal(bv, true);
784         return true;
785 }
786
787
788 bool InsetText::insetAllowed(InsetOld::Code code) const
789 {
790         // in_insetAllowed is a really gross hack,
791         // to allow us to call the owner's insetAllowed
792         // without stack overflow, which can happen
793         // when the owner uses InsetCollapsable::insetAllowed()
794         bool ret = true;
795         if (in_insetAllowed)
796                 return ret;
797         in_insetAllowed = true;
798         if (owner())
799                 ret = owner()->insetAllowed(code);
800         in_insetAllowed = false;
801         return ret;
802 }
803
804
805 bool InsetText::showInsetDialog(BufferView *) const
806 {
807         return false;
808 }
809
810
811 void InsetText::getLabelList(Buffer const & buffer,
812                              std::vector<string> & list) const
813 {
814         ParagraphList::const_iterator pit = paragraphs.begin();
815         ParagraphList::const_iterator pend = paragraphs.end();
816         for (; pit != pend; ++pit) {
817                 InsetList::const_iterator beg = pit->insetlist.begin();
818                 InsetList::const_iterator end = pit->insetlist.end();
819                 for (; beg != end; ++beg)
820                         beg->inset->getLabelList(buffer, list);
821         }
822 }
823
824
825 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
826                         bool selectall)
827 {
828         if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
829             || cpar()->empty()) {
830                 text_.setFont(font, toggleall);
831                 return;
832         }
833
834         if (text_.selection.set())
835                 text_.recUndo(text_.cursor.par());
836
837         if (selectall) {
838                 text_.cursorTop();
839                 text_.selection.cursor = text_.cursor;
840                 text_.cursorBottom();
841                 text_.setSelection();
842         }
843
844         text_.toggleFree(font, toggleall);
845
846         if (selectall)
847                 text_.clearSelection();
848
849 //      bv->fitCursor();
850         updateLocal(bv, true);
851 }
852
853
854 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
855 {
856         if (cpos() == cpar()->size())
857                 return false;
858         InsetOld * inset = cpar()->getInset(cpos());
859         if (!isHighlyEditableInset(inset))
860                 return false;
861         inset->edit(bv, front);
862         updateLocal(bv, false);
863         return true;
864 }
865
866
867 void InsetText::markNew(bool track_changes)
868 {
869         ParagraphList::iterator pit = paragraphs.begin();
870         ParagraphList::iterator end = paragraphs.end();
871         for (; pit != end; ++pit) {
872                 if (track_changes) {
873                         pit->trackChanges();
874                 } else {
875                         // no-op when not tracking
876                         pit->cleanChanges();
877                 }
878         }
879 }
880
881
882 void InsetText::setText(string const & data, LyXFont const & font)
883 {
884         clear(false);
885         for (unsigned int i = 0; i < data.length(); ++i)
886                 paragraphs.begin()->insertChar(i, data[i], font);
887 }
888
889
890 void InsetText::setAutoBreakRows(bool flag)
891 {
892         if (flag != autoBreakRows_) {
893                 autoBreakRows_ = flag;
894                 if (!flag)
895                         removeNewlines();
896         }
897 }
898
899
900 void InsetText::setDrawFrame(DrawFrame how)
901 {
902         drawFrame_ = how;
903 }
904
905
906 LColor_color InsetText::frameColor() const
907 {
908         return LColor::color(frame_color_);
909 }
910
911
912 void InsetText::setFrameColor(LColor_color col)
913 {
914         frame_color_ = col;
915 }
916
917
918 int InsetText::cx() const
919 {
920         return text_.cursor.x() + xo_ + TEXT_TO_INSET_OFFSET;
921 }
922
923
924 int InsetText::cy() const
925 {
926         return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
927 }
928
929
930 pos_type InsetText::cpos() const
931 {
932         return text_.cursor.pos();
933 }
934
935
936 ParagraphList::iterator InsetText::cpar() const
937 {
938         return text_.cursorPar();
939 }
940
941
942 RowList::iterator InsetText::crow() const
943 {
944         return cpar()->getRow(cpos());
945 }
946
947
948 void InsetText::setViewCache(BufferView const * bv) const
949 {
950         if (bv) {
951                 if (bv != text_.bv_owner) {
952                         //lyxerr << "setting view cache from "
953                         //      << text_.bv_owner << " to " << bv << "\n";
954                         text_.init(const_cast<BufferView *>(bv));
955                 }
956                 text_.bv_owner = const_cast<BufferView *>(bv);
957         }
958 }
959
960
961 void InsetText::deleteLyXText(BufferView * bv) const
962 {
963         /// then remove all LyXText in text-insets
964         for_each(const_cast<ParagraphList&>(paragraphs).begin(),
965                  const_cast<ParagraphList&>(paragraphs).end(),
966                  boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
967 }
968
969
970 void InsetText::removeNewlines()
971 {
972         ParagraphList::iterator it = paragraphs.begin();
973         ParagraphList::iterator end = paragraphs.end();
974         for (; it != end; ++it)
975                 for (int i = 0; i < it->size(); ++i)
976                         if (it->isNewline(i))
977                                 it->erase(i);
978 }
979
980
981 int InsetText::scroll(bool /*recursive*/) const
982 {
983         return UpdatableInset::scroll(false);
984 }
985
986
987 void InsetText::clearSelection(BufferView *)
988 {
989         text_.clearSelection();
990 }
991
992
993 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
994 {
995         Painter & pain = bv->painter();
996         int w = dim_.wid;
997         int h = dim_.asc + dim_.des;
998         int ty = baseline - dim_.asc;
999
1000         if (ty < 0) {
1001                 h += ty;
1002                 ty = 0;
1003         }
1004         if (ty + h > pain.paperHeight())
1005                 h = pain.paperHeight();
1006         if (xo_ + w > pain.paperWidth())
1007                 w = pain.paperWidth();
1008         pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1009 }
1010
1011
1012 ParagraphList * InsetText::getParagraphs(int i) const
1013 {
1014         return (i == 0) ? const_cast<ParagraphList*>(&paragraphs) : 0;
1015 }
1016
1017
1018 LyXText * InsetText::getText(int i) const
1019 {
1020         return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
1021 }
1022
1023
1024 bool InsetText::checkInsertChar(LyXFont & font)
1025 {
1026         return owner() ? owner()->checkInsertChar(font) : true;
1027 }
1028
1029
1030 void InsetText::collapseParagraphs(BufferView * bv)
1031 {
1032         while (paragraphs.size() > 1) {
1033                 ParagraphList::iterator const first = paragraphs.begin();
1034                 ParagraphList::iterator second = first;
1035                 ++second;
1036                 size_t const first_par_size = first->size();
1037
1038                 if (!first->empty() &&
1039                     !second->empty() &&
1040                     !first->isSeparator(first_par_size - 1)) {
1041                         first->insertChar(first_par_size, ' ');
1042                 }
1043
1044 #warning probably broken
1045                 if (text_.selection.set()) {
1046                         if (text_.selection.start.par() == 1) {
1047                                 text_.selection.start.par(1);
1048                                 text_.selection.start.pos(text_.selection.start.pos() + first_par_size);
1049                         }
1050                         if (text_.selection.end.par() == 2) {
1051                                 text_.selection.end.par(1);
1052                                 text_.selection.end.pos(text_.selection.end.pos() + first_par_size);
1053                         }
1054                 }
1055
1056                 mergeParagraph(bv->buffer()->params(), paragraphs, first);
1057         }
1058 }
1059
1060
1061 void InsetText::getDrawFont(LyXFont & font) const
1062 {
1063         if (owner())
1064                 owner()->getDrawFont(font);
1065 }
1066
1067
1068 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1069 {
1070 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1071 // And it probably does. You have to take a look at this John. (Lgb)
1072 #warning John, have a look here. (Lgb)
1073         ParagraphList::iterator pit = plist.begin();
1074         ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1075         ++pit;
1076         mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
1077
1078         ParagraphList::iterator pend = plist.end();
1079         for (; pit != pend; ++pit)
1080                 paragraphs.push_back(*pit);
1081 }
1082
1083
1084 void InsetText::addPreview(PreviewLoader & loader) const
1085 {
1086         ParagraphList::const_iterator pit = paragraphs.begin();
1087         ParagraphList::const_iterator pend = paragraphs.end();
1088
1089         for (; pit != pend; ++pit) {
1090                 InsetList::const_iterator it  = pit->insetlist.begin();
1091                 InsetList::const_iterator end = pit->insetlist.end();
1092                 for (; it != end; ++it)
1093                         it->inset->addPreview(loader);
1094         }
1095 }