3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "insettext.h"
14 #include "insetnewline.h"
17 #include "bufferparams.h"
18 #include "BufferView.h"
19 #include "CutAndPaste.h"
21 #include "dispatchresult.h"
22 #include "errorlist.h"
23 #include "funcrequest.h"
30 #include "metricsinfo.h"
31 #include "output_docbook.h"
32 #include "output_latex.h"
33 #include "output_linuxdoc.h"
34 #include "output_plaintext.h"
35 #include "paragraph.h"
36 #include "paragraph_funcs.h"
37 #include "ParagraphParameters.h"
38 #include "rowpainter.h"
43 #include "frontends/Alert.h"
44 #include "frontends/font_metrics.h"
45 #include "frontends/LyXView.h"
46 #include "frontends/Painter.h"
48 #include "support/lyxalgo.h" // lyx::count
50 #include <boost/bind.hpp>
52 using bv_funcs::replaceSelection;
56 using lyx::graphics::PreviewLoader;
58 using lyx::support::isStrUnsignedInt;
59 using lyx::support::strToUnsignedInt;
70 InsetText::InsetText(BufferParams const & bp)
73 autoBreakRows_(false),
75 frame_color_(LColor::insetframe),
76 text_(0, this, true, paragraphs)
78 textwidth_ = 0; // broken
79 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
80 if (bp.tracking_changes)
81 paragraphs.begin()->trackChanges();
86 InsetText::InsetText(InsetText const & in)
88 text_(in.text_.bv_owner, this, true, paragraphs)
95 void InsetText::operator=(InsetText const & in)
97 UpdatableInset::operator=(in);
98 paragraphs = in.paragraphs;
99 autoBreakRows_ = in.autoBreakRows_;
100 drawFrame_ = in.drawFrame_;
101 frame_color_ = in.frame_color_;
102 textwidth_ = in.textwidth_;
103 text_ = LyXText(in.text_.bv_owner, this, true, paragraphs);
108 void InsetText::init()
110 ParagraphList::iterator pit = paragraphs.begin();
111 ParagraphList::iterator end = paragraphs.end();
112 for (; pit != end; ++pit)
113 pit->setInsetOwner(this);
114 text_.paragraphs_ = ¶graphs;
120 the_locking_inset = 0;
122 in_insetAllowed = false;
128 void InsetText::clear(bool just_mark_erased)
130 if (just_mark_erased) {
131 ParagraphList::iterator it = paragraphs.begin();
132 ParagraphList::iterator end = paragraphs.end();
133 for (; it != end; ++it)
138 // This is a gross hack...
139 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
142 paragraphs.push_back(Paragraph());
143 paragraphs.begin()->setInsetOwner(this);
144 paragraphs.begin()->layout(old_layout);
148 auto_ptr<InsetBase> InsetText::clone() const
150 return auto_ptr<InsetBase>(new InsetText(*this));
154 void InsetText::write(Buffer const & buf, ostream & os) const
157 writeParagraphData(buf, os);
161 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
163 ParagraphList::const_iterator it = paragraphs.begin();
164 ParagraphList::const_iterator end = paragraphs.end();
165 Paragraph::depth_type dth = 0;
166 for (; it != end; ++it) {
167 it->write(buf, os, buf.params(), dth);
172 void InsetText::read(Buffer const & buf, LyXLex & lex)
175 Paragraph::depth_type depth = 0;
179 #warning John, look here. Doesnt make much sense.
180 if (buf.params().tracking_changes)
181 paragraphs.begin()->trackChanges();
183 // delete the initial paragraph
184 Paragraph oldpar = *paragraphs.begin();
186 ParagraphList::iterator pit = paragraphs.begin();
190 token = lex.getString();
193 if (token == "\\end_inset") {
197 if (token == "\\end_document") {
198 lex.printError("\\end_document read in inset! Error in document!");
203 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
206 pit = paragraphs.begin();
207 ParagraphList::iterator const end = paragraphs.end();
208 for (; pit != end; ++pit)
209 pit->setInsetOwner(this);
211 if (token != "\\end_inset") {
212 lex.printError("Missing \\end_inset at this point. "
217 // ensure we have at least one par.
218 if (paragraphs.empty())
219 paragraphs.push_back(oldpar);
223 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
225 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
226 textwidth_ = mi.base.textwidth - 30;
227 BufferView * bv = mi.base.bv;
229 text_.metrics(mi, dim);
230 dim.asc += TEXT_TO_INSET_OFFSET;
231 dim.des += TEXT_TO_INSET_OFFSET;
232 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
233 dim.wid = max(dim.wid, 10);
238 int InsetText::textWidth() const
244 void InsetText::draw(PainterInfo & pi, int x, int y) const
246 // update our idea of where we are. Clearly, we should
247 // not have to know this information.
250 int const start_x = x;
252 BufferView * bv = pi.base.bv;
253 Painter & pain = pi.pain;
255 // repaint the background if needed
256 if (backgroundColor() != LColor::background)
257 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
259 // no draw is necessary !!!
260 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
274 x += TEXT_TO_INSET_OFFSET;
276 paintTextInset(*bv, text_, x, y);
278 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
279 drawFrame(pain, start_x);
283 void InsetText::drawFrame(Painter & pain, int x) const
285 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
286 int const frame_x = x + ttoD2;
287 int const frame_y = top_baseline - dim_.asc + ttoD2;
288 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
289 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
290 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
294 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
299 if (!autoBreakRows_ && paragraphs.size() > 1)
300 collapseParagraphs(bv);
302 if (!text_.selection.set())
303 text_.selection.cursor = text_.cursor;
306 bv->updateInset(this);
307 bv->owner()->view_state_changed();
308 bv->owner()->updateMenubar();
309 bv->owner()->updateToolbar();
310 if (old_par != text_.cursor.par()) {
311 bv->owner()->setLayout(cpar()->layout()->name());
312 old_par = text_.cursor.par();
317 string const InsetText::editMessage() const
319 return _("Opened Text Inset");
323 void InsetText::insetUnlock(BufferView * bv)
325 if (the_locking_inset) {
326 the_locking_inset->insetUnlock(bv);
327 the_locking_inset = 0;
328 updateLocal(bv, false);
333 if (text_.selection.set())
334 text_.clearSelection();
336 bv->owner()->setLayout(owner()->getLyXText(bv)
337 ->cursorPar()->layout()->name());
339 bv->owner()->setLayout(bv->text->cursorPar()->layout()->name());
340 // hack for deleteEmptyParMech
341 if (!paragraphs.begin()->empty())
342 text_.setCursor(0, 0);
343 else if (paragraphs.size() > 1)
344 text_.setCursor(1, 0);
348 void InsetText::lockInset(BufferView * bv)
351 the_locking_inset = 0;
352 inset_x = inset_y = 0;
353 inset_boundary = false;
355 text_.setCursorIntern(0, 0);
356 text_.clearSelection();
358 // If the inset is empty set the language of the current font to the
359 // language to the surronding text (if different).
360 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
361 bv->getParentLanguage(this) != text_.current_font.language()) {
362 LyXFont font(LyXFont::ALL_IGNORE);
363 font.setLanguage(bv->getParentLanguage(this));
364 setFont(bv, font, false);
369 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
371 the_locking_inset = inset;
372 inset_x = cx() - top_x;
374 inset_boundary = cboundary();
378 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
380 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
381 << inset << "): " << endl;
384 if (!the_locking_inset) {
385 ParagraphList::iterator pit = paragraphs.begin();
386 ParagraphList::iterator pend = paragraphs.end();
388 for (; pit != pend; ++pit) {
389 InsetList::iterator it = pit->insetlist.begin();
390 InsetList::iterator const end = pit->insetlist.end();
391 for (; it != end; ++it) {
392 if (it->inset == inset) {
393 lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
394 text_.setCursorIntern(
395 std::distance(paragraphs.begin(), pit), it->pos);
396 lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
397 lyxerr << "bv: " << bv << " inset: " << inset << endl;
398 lockInset(bv, inset);
399 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
404 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
407 if (inset == cpar()->getInset(cpos())) {
408 lyxerr[Debug::INSETS] << "OK" << endl;
409 lockInset(bv, inset);
413 if (the_locking_inset && the_locking_inset == inset) {
414 inset_x = cx() - top_x;
416 } else if (the_locking_inset) {
417 lyxerr[Debug::INSETS] << "MAYBE" << endl;
418 return the_locking_inset->lockInsetInInset(bv, inset);
420 lyxerr[Debug::INSETS] << "NOT OK" << endl;
425 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
428 if (!the_locking_inset)
430 if (the_locking_inset == inset) {
431 the_locking_inset->insetUnlock(bv);
432 the_locking_inset = 0;
434 moveRightIntern(bv, true, false);
435 old_par = -1; // force layout setting
439 updateLocal(bv, false);
442 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
446 void InsetText::lfunMousePress(FuncRequest const & cmd)
450 // use this to check mouse motion for selection!
454 BufferView * bv = cmd.view();
455 FuncRequest cmd1 = cmd;
462 int tmp_y = cmd.y + dim_.asc - bv->top_y();
463 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
465 if (the_locking_inset) {
466 if (the_locking_inset == inset) {
467 the_locking_inset->dispatch(cmd1);
470 // otherwise only unlock the_locking_inset
471 the_locking_inset->insetUnlock(bv);
472 the_locking_inset = 0;
475 no_selection = false;
477 if (bv->theLockingInset()) {
478 if (isHighlyEditableInset(inset)) {
479 // We just have to lock the inset before calling a
481 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
482 if (!bv->lockInset(uinset)) {
483 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
485 inset->dispatch(cmd1);
486 if (the_locking_inset)
487 updateLocal(bv, false);
492 bool paste_internally = false;
493 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
494 dispatch(FuncRequest(bv, LFUN_COPY));
495 paste_internally = true;
497 int old_top_y = bv->top_y();
499 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
500 // set the selection cursor!
501 text_.selection.cursor = text_.cursor;
502 bv->x_target(text_.cursor.x());
504 text_.clearSelection();
505 updateLocal(bv, false);
507 bv->owner()->setLayout(cpar()->layout()->name());
509 // we moved the view we cannot do mouse selection in this case!
510 if (bv->top_y() != old_top_y)
512 old_par = text_.cursor.par();
513 // Insert primary selection with middle mouse
514 // if there is a local selection in the current buffer,
516 if (cmd.button() == mouse_button::button2) {
517 if (paste_internally)
518 dispatch(FuncRequest(bv, LFUN_PASTE));
520 dispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
523 getLyXText(bv)->clearSelection();
528 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
530 BufferView * bv = cmd.view();
531 FuncRequest cmd1 = cmd;
536 if (the_locking_inset) {
537 DispatchResult const res = the_locking_inset->dispatch(cmd1);
538 return res.dispatched();
542 int tmp_y = cmd.y + dim_.asc - bv->top_y();
543 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
547 // We still need to deal properly with the whole relative vs.
548 // absolute mouse co-ords thing in a realiable, sensible way
549 DispatchResult const res = inset->dispatch(cmd1);
550 bool const ret = res.dispatched();
551 updateLocal(bv, false);
556 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
558 FuncRequest cmd1 = cmd;
562 if (the_locking_inset) {
563 the_locking_inset->dispatch(cmd1);
567 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
570 BufferView * bv = cmd.view();
571 LyXCursor cur = text_.cursor;
572 text_.setCursorFromCoordinates (cmd.x, cmd.y + dim_.asc);
573 bv->x_target(text_.cursor.x());
574 if (cur == text_.cursor)
576 text_.setSelection();
577 updateLocal(bv, false);
581 void InsetText::edit(BufferView * bv, bool left)
585 if (!bv->lockInset(this)) {
586 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
591 the_locking_inset = 0;
594 inset_boundary = false;
598 text_.setCursorIntern(0, 0);
600 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
602 // If the inset is empty set the language of the current font to the
603 // language to the surronding text (if different).
604 if (paragraphs.begin()->empty() &&
605 paragraphs.size() == 1 &&
606 bv->getParentLanguage(this) != text_.current_font.language())
608 LyXFont font(LyXFont::ALL_IGNORE);
609 font.setLanguage(bv->getParentLanguage(this));
610 setFont(bv, font, false);
613 updateLocal(bv, false);
614 // Tell the paragraph dialog that we've entered an insettext.
615 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
619 void InsetText::edit(BufferView * bv, int x, int y)
621 if (!bv->lockInset(this)) {
622 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
627 the_locking_inset = 0;
630 inset_boundary = false;
633 int tmp_y = (y < 0) ? 0 : y;
634 // we put here -1 and not button as now the button in the
635 // edit call should not be needed we will fix this in 1.3.x
636 // cycle hopefully (Jug 20020509)
637 // FIXME: GUII I've changed this to none: probably WRONG
638 if (!checkAndActivateInset(bv, x, tmp_y)) {
639 text_.setCursorFromCoordinates(x, y + dim_.asc);
640 text_.cursor.x(text_.cursor.x());
641 bv->x_target(text_.cursor.x());
644 text_.clearSelection();
647 // If the inset is empty set the language of the current font to the
648 // language to the surronding text (if different).
649 if (paragraphs.begin()->empty() &&
650 paragraphs.size() == 1 &&
651 bv->getParentLanguage(this) != text_.current_font.language())
653 LyXFont font(LyXFont::ALL_IGNORE);
654 font.setLanguage(bv->getParentLanguage(this));
655 setFont(bv, font, false);
658 updateLocal(bv, false);
659 // Tell the paragraph dialog that we've entered an insettext.
660 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
665 InsetText::priv_dispatch(FuncRequest const & cmd,
666 idx_type & idx, pos_type & pos)
668 BufferView * bv = cmd.view();
671 switch (cmd.action) {
672 case LFUN_MOUSE_PRESS:
674 return DispatchResult(true, true);
676 case LFUN_MOUSE_MOTION:
677 lfunMouseMotion(cmd);
678 return DispatchResult(true, true);
680 case LFUN_MOUSE_RELEASE:
681 return DispatchResult(lfunMouseRelease(cmd));
687 bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
688 no_selection = false;
690 DispatchResult result = UpdatableInset::priv_dispatch(cmd, idx, pos);
691 if (result.dispatched())
695 // This looks utterly strange. (Lgb)
696 if (cmd.action == LFUN_UNKNOWN_ACTION && cmd.argument.empty())
697 return DispatchResult(false, FINISHED);
700 if (the_locking_inset) {
701 DispatchResult result = the_locking_inset->dispatch(cmd);
703 if (result.dispatched()) {
704 if (result.update()) {
705 result.update(false);
706 updateLocal(bv, false);
711 switch (result.val()) {
713 moveRightIntern(bv, false, false);
714 result.dispatched(true);
719 if (result.val() >= FINISHED) {
720 updateLocal(bv, false);
721 bv->unlockInset(this);
725 result = moveDown(bv);
726 if (result.val() >= FINISHED) {
727 updateLocal(bv, false);
728 bv->unlockInset(this);
732 result.dispatched(true);
736 the_locking_inset = 0;
737 updateLocal(bv, false);
738 // make sure status gets reset immediately
739 bv->owner()->clearMessage();
743 switch (cmd.action) {
745 case LFUN_SELFINSERT:
746 if (bv->buffer()->isReadonly()) {
747 // setErrorMessage(N_("Document is read only"));
750 if (!cmd.argument.empty()) {
751 /* Automatically delete the currently selected
752 * text and replace it with what is being
753 * typed in now. Depends on lyxrc settings
754 * "auto_region_delete", which defaults to
757 // This should not be needed here and is also WRONG!
758 recordUndo(bv, Undo::INSERT, text_.cursorPar());
762 if (lyxrc.auto_region_delete && text_.selection.set())
763 text_.cutSelection(false, false);
764 text_.clearSelection();
766 for (string::size_type i = 0; i < cmd.argument.length(); ++i)
767 bv->owner()->getIntl().getTransManager().
768 TranslateAndInsert(cmd.argument[i], &text_);
770 text_.selection.cursor = text_.cursor;
771 result.dispatched(true);
775 // cursor movements that need special handling
778 result = moveRight(bv);
783 result = moveLeft(bv);
787 result = moveDown(bv);
795 if (crow() == text_.firstRow())
796 result.val(FINISHED_UP);
798 text_.cursorPrevious();
799 text_.clearSelection();
800 result.dispatched(true);
805 if (crow() == text_.lastRow())
806 result.val(FINISHED_DOWN);
809 text_.clearSelection();
810 result.dispatched(true);
815 if (text_.selection.set())
816 text_.cutSelection(true, false);
819 #warning should be also set dispatched here?
824 if (text_.selection.set())
825 text_.cutSelection(true, false);
828 #warning should be also set dispatched here?
833 if (!autoBreakRows_) {
834 if (CutAndPaste::nrOfParagraphs() > 1) {
836 #warning FIXME horrendously bad UI
838 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
841 replaceSelection(bv->getLyXText());
842 size_t sel_index = 0;
843 string const & arg = cmd.argument;
844 if (isStrUnsignedInt(arg)) {
845 #warning FIXME Check if the arg is in the domain of available selections.
846 sel_index = strToUnsignedInt(arg);
848 text_.pasteSelection(sel_index);
850 text_.clearSelection();
851 #warning should be also set dispatched here?
856 case LFUN_BREAKPARAGRAPH:
857 if (!autoBreakRows_) {
858 result.dispatched(true);
861 replaceSelection(bv->getLyXText());
862 text_.breakParagraph(paragraphs, 0);
863 #warning should be also set dispatched here?
868 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
869 if (!autoBreakRows_) {
870 result.dispatched(true);
873 replaceSelection(bv->getLyXText());
874 text_.breakParagraph(paragraphs, 1);
875 #warning should be also set dispatched here?
880 case LFUN_BREAKLINE: {
881 if (!autoBreakRows_) {
882 result.dispatched(true);
885 replaceSelection(bv->getLyXText());
886 auto_ptr<InsetNewline> ins(new InsetNewline);
887 text_.insertInset(ins.release());
888 #warning should be also set dispatched here?
895 // do not set layouts on non breakable textinsets
896 if (autoBreakRows_) {
897 string cur_layout = cpar()->layout()->name();
899 // Derive layout number from given argument (string)
900 // and current buffer's textclass (number).
901 LyXTextClass const & tclass =
902 bv->buffer()->params().getLyXTextClass();
903 string layout = cmd.argument;
904 bool hasLayout = tclass.hasLayout(layout);
906 // If the entry is obsolete, use the new one instead.
908 string const & obs = tclass[layout]->obsoleted_by();
913 // see if we found the layout number:
915 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
916 bv->owner()->dispatch(lf);
920 if (cur_layout != layout) {
922 text_.setLayout(layout);
923 bv->owner()->setLayout(cpar()->layout()->name());
924 #warning should be also set dispatched here?
928 // reset the layout box
929 bv->owner()->setLayout(cpar()->layout()->name());
937 if (result.update()) {
938 result.update(false);
939 updateLocal(bv, true);
942 /// If the action has deleted all text in the inset, we need to change the
943 // language to the language of the surronding text.
944 if (!was_empty && paragraphs.begin()->empty() &&
945 paragraphs.size() == 1) {
946 LyXFont font(LyXFont::ALL_IGNORE);
947 font.setLanguage(bv->getParentLanguage(this));
948 setFont(bv, font, false);
951 if (result.val() >= FINISHED) {
953 bv->unlockInset(this);
960 int InsetText::latex(Buffer const & buf, ostream & os,
961 OutputParams const & runparams) const
964 latexParagraphs(buf, paragraphs, os, texrow, runparams);
965 return texrow.rows();
969 int InsetText::plaintext(Buffer const & buf, ostream & os,
970 OutputParams const & runparams) const
972 ParagraphList::const_iterator beg = paragraphs.begin();
973 ParagraphList::const_iterator end = paragraphs.end();
974 ParagraphList::const_iterator it = beg;
975 for (; it != end; ++it)
976 asciiParagraph(buf, *it, os, runparams, it == beg);
978 //FIXME: Give the total numbers of lines
983 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
984 OutputParams const & runparams) const
986 linuxdocParagraphs(buf, paragraphs, os, runparams);
991 int InsetText::docbook(Buffer const & buf, ostream & os,
992 OutputParams const & runparams) const
994 docbookParagraphs(buf, paragraphs, os, runparams);
999 void InsetText::validate(LaTeXFeatures & features) const
1001 for_each(paragraphs.begin(), paragraphs.end(),
1002 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1006 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1008 if (the_locking_inset) {
1009 the_locking_inset->getCursor(bv, x, y);
1013 y = cy() + InsetText::y();
1017 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1019 if (the_locking_inset) {
1020 the_locking_inset->getCursorPos(bv, x, y);
1023 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1024 y = cy() - TEXT_TO_INSET_OFFSET;
1028 int InsetText::insetInInsetY() const
1030 if (!the_locking_inset)
1033 return inset_y + the_locking_inset->insetInInsetY();
1037 void InsetText::fitInsetCursor(BufferView * bv) const
1039 if (the_locking_inset) {
1040 the_locking_inset->fitInsetCursor(bv);
1044 LyXFont const font = text_.getFont(cpar(), cpos());
1046 int const asc = font_metrics::maxAscent(font);
1047 int const desc = font_metrics::maxDescent(font);
1049 bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1053 DispatchResult InsetText::moveRight(BufferView * bv)
1055 if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1056 return moveLeftIntern(bv, false, true, false);
1058 return moveRightIntern(bv, true, true, false);
1062 DispatchResult InsetText::moveLeft(BufferView * bv)
1064 if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1065 return moveRightIntern(bv, true, true, false);
1067 return moveLeftIntern(bv, false, true, false);
1072 InsetText::moveRightIntern(BufferView * bv, bool front,
1073 bool activate_inset, bool selecting)
1075 ParagraphList::iterator c_par = cpar();
1077 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1078 return DispatchResult(false, FINISHED_RIGHT);
1079 if (activate_inset && checkAndActivateInset(bv, front))
1080 return DispatchResult(true, true);
1081 text_.cursorRight(bv);
1083 text_.clearSelection();
1084 return DispatchResult(true);
1089 InsetText::moveLeftIntern(BufferView * bv, bool front,
1090 bool activate_inset, bool selecting)
1092 if (cpar() == paragraphs.begin() && cpos() <= 0)
1093 return DispatchResult(false, FINISHED);
1094 text_.cursorLeft(bv);
1096 text_.clearSelection();
1097 if (activate_inset && checkAndActivateInset(bv, front))
1098 return DispatchResult(true, true);
1099 return DispatchResult(true);
1103 DispatchResult InsetText::moveUp(BufferView * bv)
1105 if (crow() == text_.firstRow())
1106 return DispatchResult(false, FINISHED_UP);
1108 text_.clearSelection();
1109 return DispatchResult(true);
1113 DispatchResult InsetText::moveDown(BufferView * bv)
1115 if (crow() == text_.lastRow())
1116 return DispatchResult(false, FINISHED_DOWN);
1117 text_.cursorDown(bv);
1118 text_.clearSelection();
1119 return DispatchResult(true);
1123 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1125 if (the_locking_inset) {
1126 if (the_locking_inset->insetAllowed(inset))
1127 return the_locking_inset->insertInset(bv, inset);
1130 inset->setOwner(this);
1131 text_.insertInset(inset);
1133 updateLocal(bv, true);
1138 bool InsetText::insetAllowed(InsetOld::Code code) const
1140 // in_insetAllowed is a really gross hack,
1141 // to allow us to call the owner's insetAllowed
1142 // without stack overflow, which can happen
1143 // when the owner uses InsetCollapsable::insetAllowed()
1145 if (in_insetAllowed)
1147 in_insetAllowed = true;
1148 if (the_locking_inset)
1149 ret = the_locking_inset->insetAllowed(code);
1151 ret = owner()->insetAllowed(code);
1152 in_insetAllowed = false;
1157 UpdatableInset * InsetText::getLockingInset() const
1159 return the_locking_inset ? the_locking_inset->getLockingInset() :
1160 const_cast<InsetText *>(this);
1164 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1168 if (the_locking_inset)
1169 return the_locking_inset->getFirstLockingInsetOfType(c);
1174 bool InsetText::showInsetDialog(BufferView * bv) const
1176 if (the_locking_inset)
1177 return the_locking_inset->showInsetDialog(bv);
1182 void InsetText::getLabelList(Buffer const & buffer,
1183 std::vector<string> & list) const
1185 ParagraphList::const_iterator pit = paragraphs.begin();
1186 ParagraphList::const_iterator pend = paragraphs.end();
1187 for (; pit != pend; ++pit) {
1188 InsetList::const_iterator beg = pit->insetlist.begin();
1189 InsetList::const_iterator end = pit->insetlist.end();
1190 for (; beg != end; ++beg)
1191 beg->inset->getLabelList(buffer, list);
1196 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1199 if (the_locking_inset) {
1200 the_locking_inset->setFont(bv, font, toggleall, selectall);
1204 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1205 || cpar()->empty()) {
1206 text_.setFont(font, toggleall);
1210 if (text_.selection.set())
1211 text_.recUndo(text_.cursor.par());
1215 text_.selection.cursor = text_.cursor;
1216 text_.cursorBottom();
1217 text_.setSelection();
1220 text_.toggleFree(font, toggleall);
1223 text_.clearSelection();
1226 updateLocal(bv, true);
1230 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1232 if (cpos() == cpar()->size())
1234 InsetOld * inset = cpar()->getInset(cpos());
1235 if (!isHighlyEditableInset(inset))
1237 inset->edit(bv, front);
1238 if (!the_locking_inset)
1240 updateLocal(bv, false);
1245 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y)
1248 int dummyy = y + dim_.asc;
1249 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1252 if (!isHighlyEditableInset(inset))
1258 inset_x = cx() - top_x;
1260 inset->edit(bv, x - inset_x, y - inset_y);
1261 if (!the_locking_inset)
1263 updateLocal(bv, false);
1268 void InsetText::markNew(bool track_changes)
1270 ParagraphList::iterator pit = paragraphs.begin();
1271 ParagraphList::iterator end = paragraphs.end();
1272 for (; pit != end; ++pit) {
1273 if (track_changes) {
1274 pit->trackChanges();
1276 // no-op when not tracking
1277 pit->cleanChanges();
1283 void InsetText::setText(string const & data, LyXFont const & font)
1286 for (unsigned int i = 0; i < data.length(); ++i)
1287 paragraphs.begin()->insertChar(i, data[i], font);
1291 void InsetText::setAutoBreakRows(bool flag)
1293 if (flag != autoBreakRows_) {
1294 autoBreakRows_ = flag;
1301 void InsetText::setDrawFrame(DrawFrame how)
1307 LColor_color InsetText::frameColor() const
1309 return LColor::color(frame_color_);
1313 void InsetText::setFrameColor(LColor_color col)
1319 int InsetText::cx() const
1321 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1322 if (the_locking_inset) {
1323 LyXFont font = text_.getFont(text_.cursorPar(), text_.cursor.pos());
1324 if (font.isVisibleRightToLeft())
1325 x -= the_locking_inset->width();
1331 int InsetText::cy() const
1333 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1337 pos_type InsetText::cpos() const
1339 return text_.cursor.pos();
1343 ParagraphList::iterator InsetText::cpar() const
1345 return text_.cursorPar();
1349 bool InsetText::cboundary() const
1351 return text_.cursor.boundary();
1355 RowList::iterator InsetText::crow() const
1357 return cpar()->getRow(cpos());
1361 LyXText * InsetText::getLyXText(BufferView const * bv,
1362 bool const recursive) const
1365 if (recursive && the_locking_inset)
1366 return the_locking_inset->getLyXText(bv, true);
1371 void InsetText::setViewCache(BufferView const * bv) const
1374 if (bv != text_.bv_owner) {
1375 //lyxerr << "setting view cache from "
1376 // << text_.bv_owner << " to " << bv << "\n";
1377 text_.init(const_cast<BufferView *>(bv));
1379 text_.bv_owner = const_cast<BufferView *>(bv);
1384 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1387 /// then remove all LyXText in text-insets
1388 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1389 const_cast<ParagraphList&>(paragraphs).end(),
1390 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1395 void InsetText::removeNewlines()
1397 ParagraphList::iterator it = paragraphs.begin();
1398 ParagraphList::iterator end = paragraphs.end();
1399 for (; it != end; ++it)
1400 for (int i = 0; i < it->size(); ++i)
1401 if (it->isNewline(i))
1406 int InsetText::scroll(bool recursive) const
1408 int sx = UpdatableInset::scroll(false);
1410 if (recursive && the_locking_inset)
1411 sx += the_locking_inset->scroll(recursive);
1417 void InsetText::clearSelection(BufferView *)
1419 text_.clearSelection();
1423 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1425 Painter & pain = bv->painter();
1427 int h = dim_.asc + dim_.des;
1428 int ty = baseline - dim_.asc;
1434 if (ty + h > pain.paperHeight())
1435 h = pain.paperHeight();
1436 if (top_x + w > pain.paperWidth())
1437 w = pain.paperWidth();
1438 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1442 ParagraphList * InsetText::getParagraphs(int i) const
1444 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
1448 LyXText * InsetText::getText(int i) const
1450 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
1454 LyXCursor const & InsetText::cursor(BufferView * bv) const
1456 if (the_locking_inset)
1457 return the_locking_inset->cursor(bv);
1458 return getLyXText(bv)->cursor;
1462 bool InsetText::checkInsertChar(LyXFont & font)
1464 return owner() ? owner()->checkInsertChar(font) : true;
1468 void InsetText::collapseParagraphs(BufferView * bv)
1470 while (paragraphs.size() > 1) {
1471 ParagraphList::iterator const first = paragraphs.begin();
1472 ParagraphList::iterator second = first;
1474 size_t const first_par_size = first->size();
1476 if (!first->empty() &&
1478 !first->isSeparator(first_par_size - 1)) {
1479 first->insertChar(first_par_size, ' ');
1482 #warning probably broken
1483 if (text_.selection.set()) {
1484 if (text_.selection.start.par() == 1) {
1485 text_.selection.start.par(1);
1486 text_.selection.start.pos(text_.selection.start.pos() + first_par_size);
1488 if (text_.selection.end.par() == 2) {
1489 text_.selection.end.par(1);
1490 text_.selection.end.pos(text_.selection.end.pos() + first_par_size);
1494 mergeParagraph(bv->buffer()->params(), paragraphs, first);
1499 void InsetText::getDrawFont(LyXFont & font) const
1503 owner()->getDrawFont(font);
1507 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1509 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1510 // And it probably does. You have to take a look at this John. (Lgb)
1511 #warning John, have a look here. (Lgb)
1512 ParagraphList::iterator pit = plist.begin();
1513 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1515 mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
1517 ParagraphList::iterator pend = plist.end();
1518 for (; pit != pend; ++pit)
1519 paragraphs.push_back(*pit);
1523 void InsetText::addPreview(PreviewLoader & loader) const
1525 ParagraphList::const_iterator pit = paragraphs.begin();
1526 ParagraphList::const_iterator pend = paragraphs.end();
1528 for (; pit != pend; ++pit) {
1529 InsetList::const_iterator it = pit->insetlist.begin();
1530 InsetList::const_iterator end = pit->insetlist.end();
1531 for (; it != end; ++it)
1532 it->inset->addPreview(loader);