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[Debug::INSETS] << "Cannot lock inset" << endl;
592 the_locking_inset = 0;
595 inset_boundary = false;
599 text_.setCursorIntern(0, 0);
601 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
603 // If the inset is empty set the language of the current font to the
604 // language to the surronding text (if different).
605 if (paragraphs.begin()->empty() &&
606 paragraphs.size() == 1 &&
607 bv->getParentLanguage(this) != text_.current_font.language())
609 LyXFont font(LyXFont::ALL_IGNORE);
610 font.setLanguage(bv->getParentLanguage(this));
611 setFont(bv, font, false);
614 updateLocal(bv, false);
615 // Tell the paragraph dialog that we've entered an insettext.
616 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
620 void InsetText::edit(BufferView * bv, int x, int y)
622 if (!bv->lockInset(this)) {
623 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
628 the_locking_inset = 0;
631 inset_boundary = false;
634 int tmp_y = (y < 0) ? 0 : y;
635 // we put here -1 and not button as now the button in the
636 // edit call should not be needed we will fix this in 1.3.x
637 // cycle hopefully (Jug 20020509)
638 // FIXME: GUII I've changed this to none: probably WRONG
639 if (!checkAndActivateInset(bv, x, tmp_y)) {
640 text_.setCursorFromCoordinates(x, y + dim_.asc);
641 text_.cursor.x(text_.cursor.x());
642 bv->x_target(text_.cursor.x());
645 text_.clearSelection();
648 // If the inset is empty set the language of the current font to the
649 // language to the surronding text (if different).
650 if (paragraphs.begin()->empty() &&
651 paragraphs.size() == 1 &&
652 bv->getParentLanguage(this) != text_.current_font.language())
654 LyXFont font(LyXFont::ALL_IGNORE);
655 font.setLanguage(bv->getParentLanguage(this));
656 setFont(bv, font, false);
659 updateLocal(bv, false);
660 // Tell the paragraph dialog that we've entered an insettext.
661 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
666 InsetText::priv_dispatch(FuncRequest const & cmd,
667 idx_type & idx, pos_type & pos)
669 BufferView * bv = cmd.view();
672 switch (cmd.action) {
673 case LFUN_MOUSE_PRESS:
675 return DispatchResult(true, true);
677 case LFUN_MOUSE_MOTION:
678 lfunMouseMotion(cmd);
679 return DispatchResult(true, true);
681 case LFUN_MOUSE_RELEASE:
682 return DispatchResult(lfunMouseRelease(cmd));
688 bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
689 no_selection = false;
691 DispatchResult result = UpdatableInset::priv_dispatch(cmd, idx, pos);
692 if (result.dispatched())
696 // This looks utterly strange. (Lgb)
697 if (cmd.action == LFUN_UNKNOWN_ACTION && cmd.argument.empty())
698 return DispatchResult(false, FINISHED);
701 if (the_locking_inset) {
702 DispatchResult result = the_locking_inset->dispatch(cmd);
704 if (result.dispatched()) {
705 if (result.update()) {
706 result.update(false);
707 updateLocal(bv, false);
712 switch (result.val()) {
714 moveRightIntern(bv, false, false);
715 result.dispatched(true);
720 if (result.val() >= FINISHED) {
721 updateLocal(bv, false);
722 bv->unlockInset(this);
727 result = moveDown(bv);
728 if (result.val() >= FINISHED) {
729 updateLocal(bv, false);
730 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);
801 text_.cursorPrevious();
802 text_.clearSelection();
803 result.dispatched(true);
808 if (crow() == text_.lastRow())
809 result.val(FINISHED_DOWN);
812 text_.clearSelection();
813 result.dispatched(true);
818 if (text_.selection.set())
819 text_.cutSelection(true, false);
822 #warning should be also set dispatched here?
827 if (text_.selection.set())
828 text_.cutSelection(true, false);
831 #warning should be also set dispatched here?
836 if (!autoBreakRows_) {
837 if (CutAndPaste::nrOfParagraphs() > 1) {
839 #warning FIXME horrendously bad UI
841 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
844 replaceSelection(bv->getLyXText());
845 size_t sel_index = 0;
846 string const & arg = cmd.argument;
847 if (isStrUnsignedInt(arg)) {
848 #warning FIXME Check if the arg is in the domain of available selections.
849 sel_index = strToUnsignedInt(arg);
851 text_.pasteSelection(sel_index);
853 text_.clearSelection();
854 #warning should be also set dispatched here?
859 case LFUN_BREAKPARAGRAPH:
860 if (!autoBreakRows_) {
861 result.dispatched(true);
864 replaceSelection(bv->getLyXText());
865 text_.breakParagraph(paragraphs, 0);
866 #warning should be also set dispatched here?
871 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
872 if (!autoBreakRows_) {
873 result.dispatched(true);
876 replaceSelection(bv->getLyXText());
877 text_.breakParagraph(paragraphs, 1);
878 #warning should be also set dispatched here?
883 case LFUN_BREAKLINE: {
884 if (!autoBreakRows_) {
885 result.dispatched(true);
888 replaceSelection(bv->getLyXText());
889 auto_ptr<InsetNewline> ins(new InsetNewline);
890 text_.insertInset(ins.release());
891 #warning should be also set dispatched here?
898 // do not set layouts on non breakable textinsets
899 if (autoBreakRows_) {
900 string cur_layout = cpar()->layout()->name();
902 // Derive layout number from given argument (string)
903 // and current buffer's textclass (number).
904 LyXTextClass const & tclass =
905 bv->buffer()->params().getLyXTextClass();
906 string layout = cmd.argument;
907 bool hasLayout = tclass.hasLayout(layout);
909 // If the entry is obsolete, use the new one instead.
911 string const & obs = tclass[layout]->obsoleted_by();
916 // see if we found the layout number:
918 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
919 bv->owner()->dispatch(lf);
923 if (cur_layout != layout) {
925 text_.setLayout(layout);
926 bv->owner()->setLayout(cpar()->layout()->name());
927 #warning should be also set dispatched here?
931 // reset the layout box
932 bv->owner()->setLayout(cpar()->layout()->name());
940 if (result.update()) {
941 result.update(false);
942 updateLocal(bv, true);
945 /// If the action has deleted all text in the inset, we need to change the
946 // language to the language of the surronding text.
947 if (!was_empty && paragraphs.begin()->empty() &&
948 paragraphs.size() == 1) {
949 LyXFont font(LyXFont::ALL_IGNORE);
950 font.setLanguage(bv->getParentLanguage(this));
951 setFont(bv, font, false);
954 if (result.val() >= FINISHED) {
956 bv->unlockInset(this);
964 int InsetText::latex(Buffer const & buf, ostream & os,
965 OutputParams const & runparams) const
968 latexParagraphs(buf, paragraphs, os, texrow, runparams);
969 return texrow.rows();
973 int InsetText::plaintext(Buffer const & buf, ostream & os,
974 OutputParams const & runparams) const
976 ParagraphList::const_iterator beg = paragraphs.begin();
977 ParagraphList::const_iterator end = paragraphs.end();
978 ParagraphList::const_iterator it = beg;
979 for (; it != end; ++it)
980 asciiParagraph(buf, *it, os, runparams, it == beg);
982 //FIXME: Give the total numbers of lines
987 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
988 OutputParams const & runparams) const
990 linuxdocParagraphs(buf, paragraphs, os, runparams);
995 int InsetText::docbook(Buffer const & buf, ostream & os,
996 OutputParams const & runparams) const
998 docbookParagraphs(buf, paragraphs, os, runparams);
1003 void InsetText::validate(LaTeXFeatures & features) const
1005 for_each(paragraphs.begin(), paragraphs.end(),
1006 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1010 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1012 if (the_locking_inset) {
1013 the_locking_inset->getCursor(bv, x, y);
1017 y = cy() + InsetText::y();
1021 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1023 if (the_locking_inset) {
1024 the_locking_inset->getCursorPos(bv, x, y);
1027 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1028 y = cy() - TEXT_TO_INSET_OFFSET;
1032 int InsetText::insetInInsetY() const
1034 if (!the_locking_inset)
1037 return inset_y + the_locking_inset->insetInInsetY();
1041 void InsetText::fitInsetCursor(BufferView * bv) const
1043 if (the_locking_inset) {
1044 the_locking_inset->fitInsetCursor(bv);
1048 LyXFont const font = text_.getFont(cpar(), cpos());
1050 int const asc = font_metrics::maxAscent(font);
1051 int const desc = font_metrics::maxDescent(font);
1053 bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1057 DispatchResult InsetText::moveRight(BufferView * bv)
1059 if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1060 return moveLeftIntern(bv, false, true, false);
1062 return moveRightIntern(bv, true, true, false);
1066 DispatchResult InsetText::moveLeft(BufferView * bv)
1068 if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1069 return moveRightIntern(bv, true, true, false);
1071 return moveLeftIntern(bv, false, true, false);
1076 InsetText::moveRightIntern(BufferView * bv, bool front,
1077 bool activate_inset, bool selecting)
1079 ParagraphList::iterator c_par = cpar();
1081 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1082 return DispatchResult(false, FINISHED_RIGHT);
1083 if (activate_inset && checkAndActivateInset(bv, front))
1084 return DispatchResult(true, true);
1085 text_.cursorRight(bv);
1087 text_.clearSelection();
1088 return DispatchResult(true);
1093 InsetText::moveLeftIntern(BufferView * bv, bool front,
1094 bool activate_inset, bool selecting)
1096 if (cpar() == paragraphs.begin() && cpos() <= 0)
1097 return DispatchResult(false, FINISHED);
1098 text_.cursorLeft(bv);
1100 text_.clearSelection();
1101 if (activate_inset && checkAndActivateInset(bv, front))
1102 return DispatchResult(true, true);
1103 return DispatchResult(true);
1107 DispatchResult InsetText::moveUp(BufferView * bv)
1109 if (crow() == text_.firstRow())
1110 return DispatchResult(false, FINISHED_UP);
1112 text_.clearSelection();
1113 return DispatchResult(true);
1117 DispatchResult InsetText::moveDown(BufferView * bv)
1119 if (crow() == text_.lastRow())
1120 return DispatchResult(false, FINISHED_DOWN);
1121 text_.cursorDown(bv);
1122 text_.clearSelection();
1123 return DispatchResult(true);
1127 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1129 if (the_locking_inset) {
1130 if (the_locking_inset->insetAllowed(inset))
1131 return the_locking_inset->insertInset(bv, inset);
1134 inset->setOwner(this);
1135 text_.insertInset(inset);
1137 updateLocal(bv, true);
1142 bool InsetText::insetAllowed(InsetOld::Code code) const
1144 // in_insetAllowed is a really gross hack,
1145 // to allow us to call the owner's insetAllowed
1146 // without stack overflow, which can happen
1147 // when the owner uses InsetCollapsable::insetAllowed()
1149 if (in_insetAllowed)
1151 in_insetAllowed = true;
1152 if (the_locking_inset)
1153 ret = the_locking_inset->insetAllowed(code);
1155 ret = owner()->insetAllowed(code);
1156 in_insetAllowed = false;
1161 UpdatableInset * InsetText::getLockingInset() const
1163 return the_locking_inset ? the_locking_inset->getLockingInset() :
1164 const_cast<InsetText *>(this);
1168 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1172 if (the_locking_inset)
1173 return the_locking_inset->getFirstLockingInsetOfType(c);
1178 bool InsetText::showInsetDialog(BufferView * bv) const
1180 if (the_locking_inset)
1181 return the_locking_inset->showInsetDialog(bv);
1186 void InsetText::getLabelList(Buffer const & buffer,
1187 std::vector<string> & list) const
1189 ParagraphList::const_iterator pit = paragraphs.begin();
1190 ParagraphList::const_iterator pend = paragraphs.end();
1191 for (; pit != pend; ++pit) {
1192 InsetList::const_iterator beg = pit->insetlist.begin();
1193 InsetList::const_iterator end = pit->insetlist.end();
1194 for (; beg != end; ++beg)
1195 beg->inset->getLabelList(buffer, list);
1200 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1203 if (the_locking_inset) {
1204 the_locking_inset->setFont(bv, font, toggleall, selectall);
1208 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1209 || cpar()->empty()) {
1210 text_.setFont(font, toggleall);
1214 if (text_.selection.set())
1215 text_.recUndo(text_.cursor.par());
1219 text_.selection.cursor = text_.cursor;
1220 text_.cursorBottom();
1221 text_.setSelection();
1224 text_.toggleFree(font, toggleall);
1227 text_.clearSelection();
1230 updateLocal(bv, true);
1234 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1236 if (cpos() == cpar()->size())
1238 InsetOld * inset = cpar()->getInset(cpos());
1239 if (!isHighlyEditableInset(inset))
1241 inset->edit(bv, front);
1242 if (!the_locking_inset)
1244 updateLocal(bv, false);
1249 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y)
1252 int dummyy = y + dim_.asc;
1253 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1256 if (!isHighlyEditableInset(inset))
1262 inset_x = cx() - top_x;
1264 inset->edit(bv, x - inset_x, y - inset_y);
1265 if (!the_locking_inset)
1267 updateLocal(bv, false);
1272 void InsetText::markNew(bool track_changes)
1274 ParagraphList::iterator pit = paragraphs.begin();
1275 ParagraphList::iterator end = paragraphs.end();
1276 for (; pit != end; ++pit) {
1277 if (track_changes) {
1278 pit->trackChanges();
1280 // no-op when not tracking
1281 pit->cleanChanges();
1287 void InsetText::setText(string const & data, LyXFont const & font)
1290 for (unsigned int i = 0; i < data.length(); ++i)
1291 paragraphs.begin()->insertChar(i, data[i], font);
1295 void InsetText::setAutoBreakRows(bool flag)
1297 if (flag != autoBreakRows_) {
1298 autoBreakRows_ = flag;
1305 void InsetText::setDrawFrame(DrawFrame how)
1311 LColor_color InsetText::frameColor() const
1313 return LColor::color(frame_color_);
1317 void InsetText::setFrameColor(LColor_color col)
1323 int InsetText::cx() const
1325 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1326 if (the_locking_inset) {
1327 LyXFont font = text_.getFont(text_.cursorPar(), text_.cursor.pos());
1328 if (font.isVisibleRightToLeft())
1329 x -= the_locking_inset->width();
1335 int InsetText::cy() const
1337 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1341 pos_type InsetText::cpos() const
1343 return text_.cursor.pos();
1347 ParagraphList::iterator InsetText::cpar() const
1349 return text_.cursorPar();
1353 bool InsetText::cboundary() const
1355 return text_.cursor.boundary();
1359 RowList::iterator InsetText::crow() const
1361 return cpar()->getRow(cpos());
1365 LyXText * InsetText::getLyXText(BufferView const * bv,
1366 bool const recursive) const
1369 if (recursive && the_locking_inset)
1370 return the_locking_inset->getLyXText(bv, true);
1375 void InsetText::setViewCache(BufferView const * bv) const
1378 if (bv != text_.bv_owner) {
1379 //lyxerr << "setting view cache from "
1380 // << text_.bv_owner << " to " << bv << "\n";
1381 text_.init(const_cast<BufferView *>(bv));
1383 text_.bv_owner = const_cast<BufferView *>(bv);
1388 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1391 /// then remove all LyXText in text-insets
1392 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1393 const_cast<ParagraphList&>(paragraphs).end(),
1394 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1399 void InsetText::removeNewlines()
1401 ParagraphList::iterator it = paragraphs.begin();
1402 ParagraphList::iterator end = paragraphs.end();
1403 for (; it != end; ++it)
1404 for (int i = 0; i < it->size(); ++i)
1405 if (it->isNewline(i))
1410 int InsetText::scroll(bool recursive) const
1412 int sx = UpdatableInset::scroll(false);
1414 if (recursive && the_locking_inset)
1415 sx += the_locking_inset->scroll(recursive);
1421 void InsetText::clearSelection(BufferView *)
1423 text_.clearSelection();
1427 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1429 Painter & pain = bv->painter();
1431 int h = dim_.asc + dim_.des;
1432 int ty = baseline - dim_.asc;
1438 if (ty + h > pain.paperHeight())
1439 h = pain.paperHeight();
1440 if (top_x + w > pain.paperWidth())
1441 w = pain.paperWidth();
1442 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1446 ParagraphList * InsetText::getParagraphs(int i) const
1448 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
1452 LyXText * InsetText::getText(int i) const
1454 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
1458 LyXCursor const & InsetText::cursor(BufferView * bv) const
1460 if (the_locking_inset)
1461 return the_locking_inset->cursor(bv);
1462 return getLyXText(bv)->cursor;
1466 bool InsetText::checkInsertChar(LyXFont & font)
1468 return owner() ? owner()->checkInsertChar(font) : true;
1472 void InsetText::collapseParagraphs(BufferView * bv)
1474 while (paragraphs.size() > 1) {
1475 ParagraphList::iterator const first = paragraphs.begin();
1476 ParagraphList::iterator second = first;
1478 size_t const first_par_size = first->size();
1480 if (!first->empty() &&
1482 !first->isSeparator(first_par_size - 1)) {
1483 first->insertChar(first_par_size, ' ');
1486 #warning probably broken
1487 if (text_.selection.set()) {
1488 if (text_.selection.start.par() == 1) {
1489 text_.selection.start.par(1);
1490 text_.selection.start.pos(text_.selection.start.pos() + first_par_size);
1492 if (text_.selection.end.par() == 2) {
1493 text_.selection.end.par(1);
1494 text_.selection.end.pos(text_.selection.end.pos() + first_par_size);
1498 mergeParagraph(bv->buffer()->params(), paragraphs, first);
1503 void InsetText::getDrawFont(LyXFont & font) const
1507 owner()->getDrawFont(font);
1511 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1513 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1514 // And it probably does. You have to take a look at this John. (Lgb)
1515 #warning John, have a look here. (Lgb)
1516 ParagraphList::iterator pit = plist.begin();
1517 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1519 mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
1521 ParagraphList::iterator pend = plist.end();
1522 for (; pit != pend; ++pit)
1523 paragraphs.push_back(*pit);
1527 void InsetText::addPreview(PreviewLoader & loader) const
1529 ParagraphList::const_iterator pit = paragraphs.begin();
1530 ParagraphList::const_iterator pend = paragraphs.end();
1532 for (; pit != pend; ++pit) {
1533 InsetList::const_iterator it = pit->insetlist.begin();
1534 InsetList::const_iterator end = pit->insetlist.end();
1535 for (; it != end; ++it)
1536 it->inset->addPreview(loader);