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"
22 #include "dispatchresult.h"
23 #include "errorlist.h"
24 #include "funcrequest.h"
31 #include "metricsinfo.h"
32 #include "output_docbook.h"
33 #include "output_latex.h"
34 #include "output_linuxdoc.h"
35 #include "output_plaintext.h"
36 #include "paragraph.h"
37 #include "paragraph_funcs.h"
38 #include "ParagraphParameters.h"
39 #include "rowpainter.h"
44 #include "frontends/Alert.h"
45 #include "frontends/font_metrics.h"
46 #include "frontends/LyXView.h"
47 #include "frontends/Painter.h"
49 #include "support/lyxalgo.h" // lyx::count
51 #include <boost/bind.hpp>
53 using bv_funcs::replaceSelection;
57 using lyx::graphics::PreviewLoader;
59 using lyx::support::isStrUnsignedInt;
60 using lyx::support::strToUnsignedInt;
71 InsetText::InsetText(BufferParams const & bp)
74 autoBreakRows_(false),
76 frame_color_(LColor::insetframe),
77 text_(0, this, true, paragraphs)
79 textwidth_ = 0; // broken
80 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
81 if (bp.tracking_changes)
82 paragraphs.begin()->trackChanges();
87 InsetText::InsetText(InsetText const & in)
89 text_(in.text_.bv_owner, this, true, paragraphs)
96 void InsetText::operator=(InsetText const & in)
98 UpdatableInset::operator=(in);
99 paragraphs = in.paragraphs;
100 autoBreakRows_ = in.autoBreakRows_;
101 drawFrame_ = in.drawFrame_;
102 frame_color_ = in.frame_color_;
103 textwidth_ = in.textwidth_;
104 text_ = LyXText(in.text_.bv_owner, this, true, paragraphs);
109 void InsetText::init()
111 ParagraphList::iterator pit = paragraphs.begin();
112 ParagraphList::iterator end = paragraphs.end();
113 for (; pit != end; ++pit)
114 pit->setInsetOwner(this);
115 text_.paragraphs_ = ¶graphs;
121 the_locking_inset = 0;
123 in_insetAllowed = false;
129 void InsetText::clear(bool just_mark_erased)
131 if (just_mark_erased) {
132 ParagraphList::iterator it = paragraphs.begin();
133 ParagraphList::iterator end = paragraphs.end();
134 for (; it != end; ++it)
139 // This is a gross hack...
140 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
143 paragraphs.push_back(Paragraph());
144 paragraphs.begin()->setInsetOwner(this);
145 paragraphs.begin()->layout(old_layout);
149 auto_ptr<InsetBase> InsetText::clone() const
151 return auto_ptr<InsetBase>(new InsetText(*this));
155 void InsetText::write(Buffer const & buf, ostream & os) const
158 writeParagraphData(buf, os);
162 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
164 ParagraphList::const_iterator it = paragraphs.begin();
165 ParagraphList::const_iterator end = paragraphs.end();
166 Paragraph::depth_type dth = 0;
167 for (; it != end; ++it) {
168 it->write(buf, os, buf.params(), dth);
173 void InsetText::read(Buffer const & buf, LyXLex & lex)
176 Paragraph::depth_type depth = 0;
180 #warning John, look here. Doesnt make much sense.
181 if (buf.params().tracking_changes)
182 paragraphs.begin()->trackChanges();
184 // delete the initial paragraph
185 Paragraph oldpar = *paragraphs.begin();
187 ParagraphList::iterator pit = paragraphs.begin();
191 token = lex.getString();
194 if (token == "\\end_inset") {
198 if (token == "\\end_document") {
199 lex.printError("\\end_document read in inset! Error in document!");
204 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
207 pit = paragraphs.begin();
208 ParagraphList::iterator const end = paragraphs.end();
209 for (; pit != end; ++pit)
210 pit->setInsetOwner(this);
212 if (token != "\\end_inset") {
213 lex.printError("Missing \\end_inset at this point. "
218 // ensure we have at least one par.
219 if (paragraphs.empty())
220 paragraphs.push_back(oldpar);
224 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
226 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
227 textwidth_ = mi.base.textwidth - 30;
228 BufferView * bv = mi.base.bv;
230 text_.metrics(mi, dim);
231 dim.asc += TEXT_TO_INSET_OFFSET;
232 dim.des += TEXT_TO_INSET_OFFSET;
233 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
234 dim.wid = max(dim.wid, 10);
239 int InsetText::textWidth() const
245 void InsetText::draw(PainterInfo & pi, int x, int y) const
247 // update our idea of where we are. Clearly, we should
248 // not have to know this information.
251 int const start_x = x;
253 BufferView * bv = pi.base.bv;
254 Painter & pain = pi.pain;
256 // repaint the background if needed
257 if (backgroundColor() != LColor::background)
258 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
260 // no draw is necessary !!!
261 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
275 x += TEXT_TO_INSET_OFFSET;
277 paintTextInset(*bv, text_, x, y);
279 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
280 drawFrame(pain, start_x);
284 void InsetText::drawFrame(Painter & pain, int x) const
286 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
287 int const frame_x = x + ttoD2;
288 int const frame_y = top_baseline - dim_.asc + ttoD2;
289 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
290 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
291 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
295 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
300 if (!autoBreakRows_ && paragraphs.size() > 1)
301 collapseParagraphs(bv);
303 if (!text_.selection.set())
304 text_.selection.cursor = text_.cursor;
307 bv->updateInset(this);
308 bv->owner()->view_state_changed();
309 bv->owner()->updateMenubar();
310 bv->owner()->updateToolbar();
311 if (old_par != text_.cursor.par()) {
312 bv->owner()->setLayout(cpar()->layout()->name());
313 old_par = text_.cursor.par();
318 string const InsetText::editMessage() const
320 return _("Opened Text Inset");
324 void InsetText::insetUnlock(BufferView * bv)
326 if (the_locking_inset) {
327 the_locking_inset->insetUnlock(bv);
328 the_locking_inset = 0;
329 updateLocal(bv, false);
334 if (text_.selection.set())
335 text_.clearSelection();
337 bv->owner()->setLayout(owner()->getLyXText(bv)
338 ->cursorPar()->layout()->name());
340 bv->owner()->setLayout(bv->text->cursorPar()->layout()->name());
341 // hack for deleteEmptyParMech
342 if (!paragraphs.begin()->empty())
343 text_.setCursor(0, 0);
344 else if (paragraphs.size() > 1)
345 text_.setCursor(1, 0);
349 void InsetText::lockInset(BufferView * bv)
352 the_locking_inset = 0;
353 inset_x = inset_y = 0;
354 inset_boundary = false;
356 text_.setCursorIntern(0, 0);
357 text_.clearSelection();
359 // If the inset is empty set the language of the current font to the
360 // language to the surronding text (if different).
361 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
362 bv->getParentLanguage(this) != text_.current_font.language()) {
363 LyXFont font(LyXFont::ALL_IGNORE);
364 font.setLanguage(bv->getParentLanguage(this));
365 setFont(bv, font, false);
370 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
372 the_locking_inset = inset;
373 inset_x = cx() - top_x;
375 inset_boundary = cboundary();
379 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
381 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
382 << inset << "): " << endl;
385 if (!the_locking_inset) {
386 ParagraphList::iterator pit = paragraphs.begin();
387 ParagraphList::iterator pend = paragraphs.end();
389 for (; pit != pend; ++pit) {
390 InsetList::iterator it = pit->insetlist.begin();
391 InsetList::iterator const end = pit->insetlist.end();
392 for (; it != end; ++it) {
393 if (it->inset == inset) {
394 lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
395 text_.setCursorIntern(
396 std::distance(paragraphs.begin(), pit), it->pos);
397 lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
398 lyxerr << "bv: " << bv << " inset: " << inset << endl;
399 lockInset(bv, inset);
400 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
405 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
408 if (inset == cpar()->getInset(cpos())) {
409 lyxerr[Debug::INSETS] << "OK" << endl;
410 lockInset(bv, inset);
414 if (the_locking_inset && the_locking_inset == inset) {
415 inset_x = cx() - top_x;
417 } else if (the_locking_inset) {
418 lyxerr[Debug::INSETS] << "MAYBE" << endl;
419 return the_locking_inset->lockInsetInInset(bv, inset);
421 lyxerr[Debug::INSETS] << "NOT OK" << endl;
426 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
429 if (!the_locking_inset)
431 if (the_locking_inset == inset) {
432 the_locking_inset->insetUnlock(bv);
433 the_locking_inset = 0;
435 moveRightIntern(bv, true, false);
436 old_par = -1; // force layout setting
440 updateLocal(bv, false);
443 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
447 void InsetText::lfunMousePress(FuncRequest const & cmd)
451 // use this to check mouse motion for selection!
455 BufferView * bv = cmd.view();
456 FuncRequest cmd1 = cmd;
463 int tmp_y = cmd.y + dim_.asc - bv->top_y();
464 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
466 if (the_locking_inset) {
467 if (the_locking_inset == inset) {
468 the_locking_inset->dispatch(cmd1);
471 // otherwise only unlock the_locking_inset
472 the_locking_inset->insetUnlock(bv);
473 the_locking_inset = 0;
476 no_selection = false;
478 if (bv->theLockingInset()) {
479 if (isHighlyEditableInset(inset)) {
480 // We just have to lock the inset before calling a
482 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
483 if (!bv->lockInset(uinset)) {
484 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
486 inset->dispatch(cmd1);
487 if (the_locking_inset)
488 updateLocal(bv, false);
493 bool paste_internally = false;
494 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
495 dispatch(FuncRequest(bv, LFUN_COPY));
496 paste_internally = true;
498 int old_top_y = bv->top_y();
500 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
501 // set the selection cursor!
502 text_.selection.cursor = text_.cursor;
503 bv->x_target(text_.cursor.x());
505 text_.clearSelection();
506 updateLocal(bv, false);
508 bv->owner()->setLayout(cpar()->layout()->name());
510 // we moved the view we cannot do mouse selection in this case!
511 if (bv->top_y() != old_top_y)
513 old_par = text_.cursor.par();
514 // Insert primary selection with middle mouse
515 // if there is a local selection in the current buffer,
517 if (cmd.button() == mouse_button::button2) {
518 if (paste_internally)
519 dispatch(FuncRequest(bv, LFUN_PASTE));
521 dispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
524 getLyXText(bv)->clearSelection();
529 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
531 BufferView * bv = cmd.view();
532 FuncRequest cmd1 = cmd;
537 if (the_locking_inset) {
538 DispatchResult const res = the_locking_inset->dispatch(cmd1);
539 return res.dispatched();
543 int tmp_y = cmd.y + dim_.asc - bv->top_y();
544 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
548 // We still need to deal properly with the whole relative vs.
549 // absolute mouse co-ords thing in a realiable, sensible way
550 DispatchResult const res = inset->dispatch(cmd1);
551 bool const ret = res.dispatched();
552 updateLocal(bv, false);
557 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
559 FuncRequest cmd1 = cmd;
563 if (the_locking_inset) {
564 the_locking_inset->dispatch(cmd1);
568 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
571 BufferView * bv = cmd.view();
572 LyXCursor cur = text_.cursor;
573 text_.setCursorFromCoordinates (cmd.x, cmd.y + dim_.asc);
574 bv->x_target(text_.cursor.x());
575 if (cur == text_.cursor)
577 text_.setSelection();
578 updateLocal(bv, false);
582 void InsetText::edit(BufferView * bv, bool left)
586 if (!bv->lockInset(this)) {
587 lyxerr << "Cannot lock inset" << endl;
590 lyxerr << "InsetText: edit left/right" << endl;
593 the_locking_inset = 0;
596 inset_boundary = false;
600 text_.setCursorIntern(0, 0);
602 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
604 // If the inset is empty set the language of the current font to the
605 // language to the surronding text (if different).
606 if (paragraphs.begin()->empty() &&
607 paragraphs.size() == 1 &&
608 bv->getParentLanguage(this) != text_.current_font.language())
610 LyXFont font(LyXFont::ALL_IGNORE);
611 font.setLanguage(bv->getParentLanguage(this));
612 setFont(bv, font, false);
615 updateLocal(bv, false);
616 // Tell the paragraph dialog that we've entered an insettext.
617 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
621 void InsetText::edit(BufferView * bv, int x, int y)
623 if (!bv->lockInset(this)) {
624 lyxerr << "Cannot lock inset" << endl;
627 lyxerr << "InsetText: edit xy" << endl;
630 the_locking_inset = 0;
633 inset_boundary = false;
636 int tmp_y = (y < 0) ? 0 : y;
637 // we put here -1 and not button as now the button in the
638 // edit call should not be needed we will fix this in 1.3.x
639 // cycle hopefully (Jug 20020509)
640 // FIXME: GUII I've changed this to none: probably WRONG
641 if (!checkAndActivateInset(bv, x, tmp_y)) {
642 text_.setCursorFromCoordinates(x, y + dim_.asc);
643 text_.cursor.x(text_.cursor.x());
644 bv->x_target(text_.cursor.x());
647 text_.clearSelection();
650 // If the inset is empty set the language of the current font to the
651 // language to the surronding text (if different).
652 if (paragraphs.begin()->empty() &&
653 paragraphs.size() == 1 &&
654 bv->getParentLanguage(this) != text_.current_font.language())
656 LyXFont font(LyXFont::ALL_IGNORE);
657 font.setLanguage(bv->getParentLanguage(this));
658 setFont(bv, font, false);
661 updateLocal(bv, false);
662 // Tell the paragraph dialog that we've entered an insettext.
663 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
668 InsetText::priv_dispatch(FuncRequest const & cmd,
669 idx_type & idx, pos_type & pos)
671 BufferView * bv = cmd.view();
674 switch (cmd.action) {
675 case LFUN_MOUSE_PRESS:
677 return DispatchResult(true, true);
679 case LFUN_MOUSE_MOTION:
680 lfunMouseMotion(cmd);
681 return DispatchResult(true, true);
683 case LFUN_MOUSE_RELEASE:
684 return DispatchResult(lfunMouseRelease(cmd));
690 bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
691 no_selection = false;
693 DispatchResult result = UpdatableInset::priv_dispatch(cmd, idx, pos);
694 if (result.dispatched())
698 // This looks utterly strange. (Lgb)
699 if (cmd.action == LFUN_UNKNOWN_ACTION && cmd.argument.empty())
700 return DispatchResult(false, FINISHED);
703 if (the_locking_inset) {
704 DispatchResult result = the_locking_inset->dispatch(cmd);
706 if (result.dispatched()) {
707 if (result.update()) {
708 result.update(false);
709 updateLocal(bv, false);
714 switch (result.val()) {
716 moveRightIntern(bv, false, false);
717 result.dispatched(true);
722 if (result.val() >= FINISHED) {
723 updateLocal(bv, false);
724 bv->unlockInset(this);
728 result = moveDown(bv);
729 if (result.val() >= FINISHED) {
730 updateLocal(bv, false);
731 bv->unlockInset(this);
735 result.dispatched(true);
739 the_locking_inset = 0;
740 updateLocal(bv, false);
741 // make sure status gets reset immediately
742 bv->owner()->clearMessage();
746 switch (cmd.action) {
748 case LFUN_SELFINSERT:
749 if (bv->buffer()->isReadonly()) {
750 // setErrorMessage(N_("Document is read only"));
753 if (!cmd.argument.empty()) {
754 /* Automatically delete the currently selected
755 * text and replace it with what is being
756 * typed in now. Depends on lyxrc settings
757 * "auto_region_delete", which defaults to
760 // This should not be needed here and is also WRONG!
761 recordUndo(bv, Undo::INSERT, text_.cursorPar());
765 if (lyxrc.auto_region_delete && text_.selection.set())
766 text_.cutSelection(false, false);
767 text_.clearSelection();
769 for (string::size_type i = 0; i < cmd.argument.length(); ++i)
770 bv->owner()->getIntl().getTransManager().
771 TranslateAndInsert(cmd.argument[i], &text_);
773 text_.selection.cursor = text_.cursor;
774 result.dispatched(true);
778 // cursor movements that need special handling
781 result = moveRight(bv);
786 result = moveLeft(bv);
790 result = moveDown(bv);
798 if (crow() == text_.firstRow()) {
799 result.val(FINISHED_UP);
800 lyxerr << "InsetText: cursor pop 1" << endl;
803 text_.cursorPrevious();
804 text_.clearSelection();
805 result.dispatched(true);
810 if (crow() == text_.lastRow()) {
811 result.val(FINISHED_DOWN);
812 lyxerr << "InsetText: cursor pop 2" << endl;
816 text_.clearSelection();
817 result.dispatched(true);
822 if (text_.selection.set())
823 text_.cutSelection(true, false);
826 #warning should be also set dispatched here?
831 if (text_.selection.set())
832 text_.cutSelection(true, false);
835 #warning should be also set dispatched here?
840 if (!autoBreakRows_) {
841 if (CutAndPaste::nrOfParagraphs() > 1) {
843 #warning FIXME horrendously bad UI
845 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
848 replaceSelection(bv->getLyXText());
849 size_t sel_index = 0;
850 string const & arg = cmd.argument;
851 if (isStrUnsignedInt(arg)) {
852 #warning FIXME Check if the arg is in the domain of available selections.
853 sel_index = strToUnsignedInt(arg);
855 text_.pasteSelection(sel_index);
857 text_.clearSelection();
858 #warning should be also set dispatched here?
863 case LFUN_BREAKPARAGRAPH:
864 if (!autoBreakRows_) {
865 result.dispatched(true);
868 replaceSelection(bv->getLyXText());
869 text_.breakParagraph(paragraphs, 0);
870 #warning should be also set dispatched here?
875 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
876 if (!autoBreakRows_) {
877 result.dispatched(true);
880 replaceSelection(bv->getLyXText());
881 text_.breakParagraph(paragraphs, 1);
882 #warning should be also set dispatched here?
887 case LFUN_BREAKLINE: {
888 if (!autoBreakRows_) {
889 result.dispatched(true);
892 replaceSelection(bv->getLyXText());
893 auto_ptr<InsetNewline> ins(new InsetNewline);
894 text_.insertInset(ins.release());
895 #warning should be also set dispatched here?
902 // do not set layouts on non breakable textinsets
903 if (autoBreakRows_) {
904 string cur_layout = cpar()->layout()->name();
906 // Derive layout number from given argument (string)
907 // and current buffer's textclass (number).
908 LyXTextClass const & tclass =
909 bv->buffer()->params().getLyXTextClass();
910 string layout = cmd.argument;
911 bool hasLayout = tclass.hasLayout(layout);
913 // If the entry is obsolete, use the new one instead.
915 string const & obs = tclass[layout]->obsoleted_by();
920 // see if we found the layout number:
922 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
923 bv->owner()->dispatch(lf);
927 if (cur_layout != layout) {
929 text_.setLayout(layout);
930 bv->owner()->setLayout(cpar()->layout()->name());
931 #warning should be also set dispatched here?
935 // reset the layout box
936 bv->owner()->setLayout(cpar()->layout()->name());
944 if (result.update()) {
945 result.update(false);
946 updateLocal(bv, true);
949 /// If the action has deleted all text in the inset, we need to change the
950 // language to the language of the surronding text.
951 if (!was_empty && paragraphs.begin()->empty() &&
952 paragraphs.size() == 1) {
953 LyXFont font(LyXFont::ALL_IGNORE);
954 font.setLanguage(bv->getParentLanguage(this));
955 setFont(bv, font, false);
958 if (result.val() >= FINISHED) {
960 bv->unlockInset(this);
967 int InsetText::latex(Buffer const & buf, ostream & os,
968 OutputParams const & runparams) const
971 latexParagraphs(buf, paragraphs, os, texrow, runparams);
972 return texrow.rows();
976 int InsetText::plaintext(Buffer const & buf, ostream & os,
977 OutputParams const & runparams) const
979 ParagraphList::const_iterator beg = paragraphs.begin();
980 ParagraphList::const_iterator end = paragraphs.end();
981 ParagraphList::const_iterator it = beg;
982 for (; it != end; ++it)
983 asciiParagraph(buf, *it, os, runparams, it == beg);
985 //FIXME: Give the total numbers of lines
990 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
991 OutputParams const & runparams) const
993 linuxdocParagraphs(buf, paragraphs, os, runparams);
998 int InsetText::docbook(Buffer const & buf, ostream & os,
999 OutputParams const & runparams) const
1001 docbookParagraphs(buf, paragraphs, os, runparams);
1006 void InsetText::validate(LaTeXFeatures & features) const
1008 for_each(paragraphs.begin(), paragraphs.end(),
1009 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1013 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1015 if (the_locking_inset) {
1016 the_locking_inset->getCursor(bv, x, y);
1020 y = cy() + InsetText::y();
1024 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1026 if (the_locking_inset) {
1027 the_locking_inset->getCursorPos(bv, x, y);
1030 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1031 y = cy() - TEXT_TO_INSET_OFFSET;
1035 int InsetText::insetInInsetY() const
1037 if (!the_locking_inset)
1040 return inset_y + the_locking_inset->insetInInsetY();
1044 void InsetText::fitInsetCursor(BufferView * bv) const
1046 if (the_locking_inset) {
1047 the_locking_inset->fitInsetCursor(bv);
1051 LyXFont const font = text_.getFont(cpar(), cpos());
1053 int const asc = font_metrics::maxAscent(font);
1054 int const desc = font_metrics::maxDescent(font);
1056 bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1060 DispatchResult InsetText::moveRight(BufferView * bv)
1062 if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1063 return moveLeftIntern(bv, false, true, false);
1065 return moveRightIntern(bv, true, true, false);
1069 DispatchResult InsetText::moveLeft(BufferView * bv)
1071 if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1072 return moveRightIntern(bv, true, true, false);
1074 return moveLeftIntern(bv, false, true, false);
1079 InsetText::moveRightIntern(BufferView * bv, bool front,
1080 bool activate_inset, bool selecting)
1082 ParagraphList::iterator c_par = cpar();
1084 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size()) {
1085 lyxerr << "InsetText: cursor pop 3" << endl;
1087 return DispatchResult(false, FINISHED_RIGHT);
1089 if (activate_inset && checkAndActivateInset(bv, front))
1090 return DispatchResult(true, true);
1091 text_.cursorRight(bv);
1093 text_.clearSelection();
1094 return DispatchResult(true);
1099 InsetText::moveLeftIntern(BufferView * bv, bool front,
1100 bool activate_inset, bool selecting)
1102 if (cpar() == paragraphs.begin() && cpos() <= 0) {
1103 lyxerr << "InsetText: cursor pop 4" << endl;
1105 return DispatchResult(false, FINISHED);
1107 text_.cursorLeft(bv);
1109 text_.clearSelection();
1110 if (activate_inset && checkAndActivateInset(bv, front))
1111 return DispatchResult(true, true);
1112 return DispatchResult(true);
1116 DispatchResult InsetText::moveUp(BufferView * bv)
1118 if (crow() == text_.firstRow()) {
1119 lyxerr << "InsetText: cursor pop 5" << endl;
1121 return DispatchResult(false, FINISHED_UP);
1124 text_.clearSelection();
1125 return DispatchResult(true);
1129 DispatchResult InsetText::moveDown(BufferView * bv)
1131 if (crow() == text_.lastRow()) {
1132 lyxerr << "InsetText: cursor pop 6" << endl;
1134 return DispatchResult(false, FINISHED_DOWN);
1136 text_.cursorDown(bv);
1137 text_.clearSelection();
1138 return DispatchResult(true);
1142 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1144 if (the_locking_inset) {
1145 if (the_locking_inset->insetAllowed(inset))
1146 return the_locking_inset->insertInset(bv, inset);
1149 inset->setOwner(this);
1150 text_.insertInset(inset);
1152 updateLocal(bv, true);
1157 bool InsetText::insetAllowed(InsetOld::Code code) const
1159 // in_insetAllowed is a really gross hack,
1160 // to allow us to call the owner's insetAllowed
1161 // without stack overflow, which can happen
1162 // when the owner uses InsetCollapsable::insetAllowed()
1164 if (in_insetAllowed)
1166 in_insetAllowed = true;
1167 if (the_locking_inset)
1168 ret = the_locking_inset->insetAllowed(code);
1170 ret = owner()->insetAllowed(code);
1171 in_insetAllowed = false;
1176 UpdatableInset * InsetText::getLockingInset() const
1178 return the_locking_inset ? the_locking_inset->getLockingInset() :
1179 const_cast<InsetText *>(this);
1183 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1187 if (the_locking_inset)
1188 return the_locking_inset->getFirstLockingInsetOfType(c);
1193 bool InsetText::showInsetDialog(BufferView * bv) const
1195 if (the_locking_inset)
1196 return the_locking_inset->showInsetDialog(bv);
1201 void InsetText::getLabelList(Buffer const & buffer,
1202 std::vector<string> & list) const
1204 ParagraphList::const_iterator pit = paragraphs.begin();
1205 ParagraphList::const_iterator pend = paragraphs.end();
1206 for (; pit != pend; ++pit) {
1207 InsetList::const_iterator beg = pit->insetlist.begin();
1208 InsetList::const_iterator end = pit->insetlist.end();
1209 for (; beg != end; ++beg)
1210 beg->inset->getLabelList(buffer, list);
1215 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1218 if (the_locking_inset) {
1219 the_locking_inset->setFont(bv, font, toggleall, selectall);
1223 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1224 || cpar()->empty()) {
1225 text_.setFont(font, toggleall);
1229 if (text_.selection.set())
1230 text_.recUndo(text_.cursor.par());
1234 text_.selection.cursor = text_.cursor;
1235 text_.cursorBottom();
1236 text_.setSelection();
1239 text_.toggleFree(font, toggleall);
1242 text_.clearSelection();
1245 updateLocal(bv, true);
1249 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1251 if (cpos() == cpar()->size())
1253 InsetOld * inset = cpar()->getInset(cpos());
1254 if (!isHighlyEditableInset(inset))
1256 inset->edit(bv, front);
1257 if (!the_locking_inset)
1259 updateLocal(bv, false);
1264 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y)
1267 int dummyy = y + dim_.asc;
1268 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1271 if (!isHighlyEditableInset(inset))
1277 inset_x = cx() - top_x;
1279 inset->edit(bv, x - inset_x, y - inset_y);
1280 if (!the_locking_inset)
1282 updateLocal(bv, false);
1287 void InsetText::markNew(bool track_changes)
1289 ParagraphList::iterator pit = paragraphs.begin();
1290 ParagraphList::iterator end = paragraphs.end();
1291 for (; pit != end; ++pit) {
1292 if (track_changes) {
1293 pit->trackChanges();
1295 // no-op when not tracking
1296 pit->cleanChanges();
1302 void InsetText::setText(string const & data, LyXFont const & font)
1305 for (unsigned int i = 0; i < data.length(); ++i)
1306 paragraphs.begin()->insertChar(i, data[i], font);
1310 void InsetText::setAutoBreakRows(bool flag)
1312 if (flag != autoBreakRows_) {
1313 autoBreakRows_ = flag;
1320 void InsetText::setDrawFrame(DrawFrame how)
1326 LColor_color InsetText::frameColor() const
1328 return LColor::color(frame_color_);
1332 void InsetText::setFrameColor(LColor_color col)
1338 int InsetText::cx() const
1340 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1341 if (the_locking_inset) {
1342 LyXFont font = text_.getFont(text_.cursorPar(), text_.cursor.pos());
1343 if (font.isVisibleRightToLeft())
1344 x -= the_locking_inset->width();
1350 int InsetText::cy() const
1352 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1356 pos_type InsetText::cpos() const
1358 return text_.cursor.pos();
1362 ParagraphList::iterator InsetText::cpar() const
1364 return text_.cursorPar();
1368 bool InsetText::cboundary() const
1370 return text_.cursor.boundary();
1374 RowList::iterator InsetText::crow() const
1376 return cpar()->getRow(cpos());
1380 LyXText * InsetText::getLyXText(BufferView const * bv,
1381 bool const recursive) const
1384 if (recursive && the_locking_inset)
1385 return the_locking_inset->getLyXText(bv, true);
1390 void InsetText::setViewCache(BufferView const * bv) const
1393 if (bv != text_.bv_owner) {
1394 //lyxerr << "setting view cache from "
1395 // << text_.bv_owner << " to " << bv << "\n";
1396 text_.init(const_cast<BufferView *>(bv));
1398 text_.bv_owner = const_cast<BufferView *>(bv);
1403 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1406 /// then remove all LyXText in text-insets
1407 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1408 const_cast<ParagraphList&>(paragraphs).end(),
1409 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1414 void InsetText::removeNewlines()
1416 ParagraphList::iterator it = paragraphs.begin();
1417 ParagraphList::iterator end = paragraphs.end();
1418 for (; it != end; ++it)
1419 for (int i = 0; i < it->size(); ++i)
1420 if (it->isNewline(i))
1425 int InsetText::scroll(bool recursive) const
1427 int sx = UpdatableInset::scroll(false);
1429 if (recursive && the_locking_inset)
1430 sx += the_locking_inset->scroll(recursive);
1436 void InsetText::clearSelection(BufferView *)
1438 text_.clearSelection();
1442 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1444 Painter & pain = bv->painter();
1446 int h = dim_.asc + dim_.des;
1447 int ty = baseline - dim_.asc;
1453 if (ty + h > pain.paperHeight())
1454 h = pain.paperHeight();
1455 if (top_x + w > pain.paperWidth())
1456 w = pain.paperWidth();
1457 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1461 ParagraphList * InsetText::getParagraphs(int i) const
1463 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
1467 LyXText * InsetText::getText(int i) const
1469 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
1473 LyXCursor const & InsetText::cursor(BufferView * bv) const
1475 if (the_locking_inset)
1476 return the_locking_inset->cursor(bv);
1477 return getLyXText(bv)->cursor;
1481 bool InsetText::checkInsertChar(LyXFont & font)
1483 return owner() ? owner()->checkInsertChar(font) : true;
1487 void InsetText::collapseParagraphs(BufferView * bv)
1489 while (paragraphs.size() > 1) {
1490 ParagraphList::iterator const first = paragraphs.begin();
1491 ParagraphList::iterator second = first;
1493 size_t const first_par_size = first->size();
1495 if (!first->empty() &&
1497 !first->isSeparator(first_par_size - 1)) {
1498 first->insertChar(first_par_size, ' ');
1501 #warning probably broken
1502 if (text_.selection.set()) {
1503 if (text_.selection.start.par() == 1) {
1504 text_.selection.start.par(1);
1505 text_.selection.start.pos(text_.selection.start.pos() + first_par_size);
1507 if (text_.selection.end.par() == 2) {
1508 text_.selection.end.par(1);
1509 text_.selection.end.pos(text_.selection.end.pos() + first_par_size);
1513 mergeParagraph(bv->buffer()->params(), paragraphs, first);
1518 void InsetText::getDrawFont(LyXFont & font) const
1522 owner()->getDrawFont(font);
1526 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1528 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1529 // And it probably does. You have to take a look at this John. (Lgb)
1530 #warning John, have a look here. (Lgb)
1531 ParagraphList::iterator pit = plist.begin();
1532 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1534 mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
1536 ParagraphList::iterator pend = plist.end();
1537 for (; pit != pend; ++pit)
1538 paragraphs.push_back(*pit);
1542 void InsetText::addPreview(PreviewLoader & loader) const
1544 ParagraphList::const_iterator pit = paragraphs.begin();
1545 ParagraphList::const_iterator pend = paragraphs.end();
1547 for (; pit != pend; ++pit) {
1548 InsetList::const_iterator it = pit->insetlist.begin();
1549 InsetList::const_iterator end = pit->insetlist.end();
1550 for (; it != end; ++it)
1551 it->inset->addPreview(loader);