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 "paragraph.h"
32 #include "paragraph_funcs.h"
33 #include "ParagraphParameters.h"
34 #include "rowpainter.h"
39 #include "frontends/Alert.h"
40 #include "frontends/font_metrics.h"
41 #include "frontends/LyXView.h"
42 #include "frontends/Painter.h"
44 #include "support/lyxalgo.h" // lyx::count
46 #include <boost/bind.hpp>
48 using bv_funcs::replaceSelection;
52 using lyx::graphics::PreviewLoader;
54 using lyx::support::isStrUnsignedInt;
55 using lyx::support::strToUnsignedInt;
66 InsetText::InsetText(BufferParams const & bp)
69 autoBreakRows_(false),
71 frame_color_(LColor::insetframe),
72 text_(0, this, true, paragraphs)
74 textwidth_ = 0; // broken
75 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
76 if (bp.tracking_changes)
77 paragraphs.begin()->trackChanges();
82 InsetText::InsetText(InsetText const & in)
84 text_(in.text_.bv_owner, this, true, paragraphs)
91 void InsetText::operator=(InsetText const & in)
93 UpdatableInset::operator=(in);
94 paragraphs = in.paragraphs;
95 autoBreakRows_ = in.autoBreakRows_;
96 drawFrame_ = in.drawFrame_;
97 frame_color_ = in.frame_color_;
98 textwidth_ = in.textwidth_;
99 text_ = LyXText(in.text_.bv_owner, this, true, paragraphs);
104 void InsetText::init()
106 ParagraphList::iterator pit = paragraphs.begin();
107 ParagraphList::iterator end = paragraphs.end();
108 for (; pit != end; ++pit)
109 pit->setInsetOwner(this);
110 text_.paragraphs_ = ¶graphs;
116 the_locking_inset = 0;
118 in_insetAllowed = false;
124 void InsetText::clear(bool just_mark_erased)
126 if (just_mark_erased) {
127 ParagraphList::iterator it = paragraphs.begin();
128 ParagraphList::iterator end = paragraphs.end();
129 for (; it != end; ++it)
134 // This is a gross hack...
135 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
138 paragraphs.push_back(Paragraph());
139 paragraphs.begin()->setInsetOwner(this);
140 paragraphs.begin()->layout(old_layout);
144 auto_ptr<InsetBase> InsetText::clone() const
146 return auto_ptr<InsetBase>(new InsetText(*this));
150 void InsetText::write(Buffer const & buf, ostream & os) const
153 writeParagraphData(buf, os);
157 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
159 ParagraphList::const_iterator it = paragraphs.begin();
160 ParagraphList::const_iterator end = paragraphs.end();
161 Paragraph::depth_type dth = 0;
162 for (; it != end; ++it) {
163 it->write(buf, os, buf.params(), dth);
168 void InsetText::read(Buffer const & buf, LyXLex & lex)
171 Paragraph::depth_type depth = 0;
175 #warning John, look here. Doesnt make much sense.
176 if (buf.params().tracking_changes)
177 paragraphs.begin()->trackChanges();
179 // delete the initial paragraph
180 Paragraph oldpar = *paragraphs.begin();
182 ParagraphList::iterator pit = paragraphs.begin();
186 token = lex.getString();
189 if (token == "\\end_inset") {
193 if (token == "\\end_document") {
194 lex.printError("\\end_document read in inset! Error in document!");
199 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
202 pit = paragraphs.begin();
203 ParagraphList::iterator const end = paragraphs.end();
204 for (; pit != end; ++pit)
205 pit->setInsetOwner(this);
207 if (token != "\\end_inset") {
208 lex.printError("Missing \\end_inset at this point. "
213 // ensure we have at least one par.
214 if (paragraphs.empty())
215 paragraphs.push_back(oldpar);
219 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
221 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
222 textwidth_ = mi.base.textwidth - 30;
223 BufferView * bv = mi.base.bv;
225 text_.metrics(mi, dim);
226 dim.asc += TEXT_TO_INSET_OFFSET;
227 dim.des += TEXT_TO_INSET_OFFSET;
228 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
229 dim.wid = max(dim.wid, 10);
234 int InsetText::textWidth() const
240 void InsetText::draw(PainterInfo & pi, int x, int y) const
242 // update our idea of where we are. Clearly, we should
243 // not have to know this information.
246 int const start_x = x;
248 BufferView * bv = pi.base.bv;
249 Painter & pain = pi.pain;
251 // repaint the background if needed
252 if (backgroundColor() != LColor::background)
253 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
255 // no draw is necessary !!!
256 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
270 x += TEXT_TO_INSET_OFFSET;
272 paintTextInset(*bv, text_, x, y);
274 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
275 drawFrame(pain, start_x);
279 void InsetText::drawFrame(Painter & pain, int x) const
281 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
282 int const frame_x = x + ttoD2;
283 int const frame_y = top_baseline - dim_.asc + ttoD2;
284 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
285 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
286 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
290 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
295 if (!autoBreakRows_ && paragraphs.size() > 1)
296 collapseParagraphs(bv);
298 if (!text_.selection.set())
299 text_.selection.cursor = text_.cursor;
302 bv->updateInset(this);
303 bv->owner()->view_state_changed();
304 bv->owner()->updateMenubar();
305 bv->owner()->updateToolbar();
306 if (old_par != text_.cursor.par()) {
307 bv->owner()->setLayout(cpar()->layout()->name());
308 old_par = text_.cursor.par();
313 string const InsetText::editMessage() const
315 return _("Opened Text Inset");
319 void InsetText::insetUnlock(BufferView * bv)
321 if (the_locking_inset) {
322 the_locking_inset->insetUnlock(bv);
323 the_locking_inset = 0;
324 updateLocal(bv, false);
329 if (text_.selection.set())
330 text_.clearSelection();
332 bv->owner()->setLayout(owner()->getLyXText(bv)
333 ->cursorPar()->layout()->name());
335 bv->owner()->setLayout(bv->text->cursorPar()->layout()->name());
336 // hack for deleteEmptyParMech
337 if (!paragraphs.begin()->empty())
338 text_.setCursor(0, 0);
339 else if (paragraphs.size() > 1)
340 text_.setCursor(1, 0);
344 void InsetText::lockInset(BufferView * bv)
347 the_locking_inset = 0;
348 inset_x = inset_y = 0;
349 inset_boundary = false;
351 text_.setCursorIntern(0, 0);
352 text_.clearSelection();
354 // If the inset is empty set the language of the current font to the
355 // language to the surronding text (if different).
356 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
357 bv->getParentLanguage(this) != text_.current_font.language()) {
358 LyXFont font(LyXFont::ALL_IGNORE);
359 font.setLanguage(bv->getParentLanguage(this));
360 setFont(bv, font, false);
365 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
367 the_locking_inset = inset;
368 inset_x = cx() - top_x;
370 inset_boundary = cboundary();
374 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
376 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
377 << inset << "): " << endl;
380 if (!the_locking_inset) {
381 ParagraphList::iterator pit = paragraphs.begin();
382 ParagraphList::iterator pend = paragraphs.end();
384 for (; pit != pend; ++pit) {
385 InsetList::iterator it = pit->insetlist.begin();
386 InsetList::iterator const end = pit->insetlist.end();
387 for (; it != end; ++it) {
388 if (it->inset == inset) {
389 lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
390 text_.setCursorIntern(
391 std::distance(paragraphs.begin(), pit), it->pos);
392 lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
393 lyxerr << "bv: " << bv << " inset: " << inset << endl;
394 lockInset(bv, inset);
395 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
400 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
403 if (inset == cpar()->getInset(cpos())) {
404 lyxerr[Debug::INSETS] << "OK" << endl;
405 lockInset(bv, inset);
409 if (the_locking_inset && the_locking_inset == inset) {
410 inset_x = cx() - top_x;
412 } else if (the_locking_inset) {
413 lyxerr[Debug::INSETS] << "MAYBE" << endl;
414 return the_locking_inset->lockInsetInInset(bv, inset);
416 lyxerr[Debug::INSETS] << "NOT OK" << endl;
421 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
424 if (!the_locking_inset)
426 if (the_locking_inset == inset) {
427 the_locking_inset->insetUnlock(bv);
428 the_locking_inset = 0;
430 moveRightIntern(bv, true, false);
431 old_par = -1; // force layout setting
435 updateLocal(bv, false);
438 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
442 void InsetText::lfunMousePress(FuncRequest const & cmd)
446 // use this to check mouse motion for selection!
450 BufferView * bv = cmd.view();
451 FuncRequest cmd1 = cmd;
458 int tmp_y = cmd.y + dim_.asc - bv->top_y();
459 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
461 if (the_locking_inset) {
462 if (the_locking_inset == inset) {
463 the_locking_inset->dispatch(cmd1);
466 // otherwise only unlock the_locking_inset
467 the_locking_inset->insetUnlock(bv);
468 the_locking_inset = 0;
471 no_selection = false;
473 if (bv->theLockingInset()) {
474 if (isHighlyEditableInset(inset)) {
475 // We just have to lock the inset before calling a
477 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
478 if (!bv->lockInset(uinset)) {
479 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
481 inset->dispatch(cmd1);
482 if (the_locking_inset)
483 updateLocal(bv, false);
488 bool paste_internally = false;
489 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
490 dispatch(FuncRequest(bv, LFUN_COPY));
491 paste_internally = true;
493 int old_top_y = bv->top_y();
495 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
496 // set the selection cursor!
497 text_.selection.cursor = text_.cursor;
498 bv->x_target(text_.cursor.x());
500 text_.clearSelection();
501 updateLocal(bv, false);
503 bv->owner()->setLayout(cpar()->layout()->name());
505 // we moved the view we cannot do mouse selection in this case!
506 if (bv->top_y() != old_top_y)
508 old_par = text_.cursor.par();
509 // Insert primary selection with middle mouse
510 // if there is a local selection in the current buffer,
512 if (cmd.button() == mouse_button::button2) {
513 if (paste_internally)
514 dispatch(FuncRequest(bv, LFUN_PASTE));
516 dispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
519 getLyXText(bv)->clearSelection();
524 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
526 BufferView * bv = cmd.view();
527 FuncRequest cmd1 = cmd;
532 if (the_locking_inset) {
533 DispatchResult const res = the_locking_inset->dispatch(cmd1);
534 return res.dispatched();
538 int tmp_y = cmd.y + dim_.asc - bv->top_y();
539 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
543 // We still need to deal properly with the whole relative vs.
544 // absolute mouse co-ords thing in a realiable, sensible way
545 DispatchResult const res = inset->dispatch(cmd1);
546 bool const ret = res.dispatched();
547 updateLocal(bv, false);
552 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
554 FuncRequest cmd1 = cmd;
558 if (the_locking_inset) {
559 the_locking_inset->dispatch(cmd1);
563 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
566 BufferView * bv = cmd.view();
567 LyXCursor cur = text_.cursor;
568 text_.setCursorFromCoordinates (cmd.x, cmd.y + dim_.asc);
569 bv->x_target(text_.cursor.x());
570 if (cur == text_.cursor)
572 text_.setSelection();
573 updateLocal(bv, false);
577 void InsetText::edit(BufferView * bv, bool left)
581 if (!bv->lockInset(this)) {
582 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
587 the_locking_inset = 0;
590 inset_boundary = false;
594 text_.setCursorIntern(0, 0);
596 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
598 // If the inset is empty set the language of the current font to the
599 // language to the surronding text (if different).
600 if (paragraphs.begin()->empty() &&
601 paragraphs.size() == 1 &&
602 bv->getParentLanguage(this) != text_.current_font.language())
604 LyXFont font(LyXFont::ALL_IGNORE);
605 font.setLanguage(bv->getParentLanguage(this));
606 setFont(bv, font, false);
609 updateLocal(bv, false);
610 // Tell the paragraph dialog that we've entered an insettext.
611 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
615 void InsetText::edit(BufferView * bv, int x, int y)
617 if (!bv->lockInset(this)) {
618 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
623 the_locking_inset = 0;
626 inset_boundary = false;
629 int tmp_y = (y < 0) ? 0 : y;
630 // we put here -1 and not button as now the button in the
631 // edit call should not be needed we will fix this in 1.3.x
632 // cycle hopefully (Jug 20020509)
633 // FIXME: GUII I've changed this to none: probably WRONG
634 if (!checkAndActivateInset(bv, x, tmp_y)) {
635 text_.setCursorFromCoordinates(x, y + dim_.asc);
636 text_.cursor.x(text_.cursor.x());
637 bv->x_target(text_.cursor.x());
640 text_.clearSelection();
643 // If the inset is empty set the language of the current font to the
644 // language to the surronding text (if different).
645 if (paragraphs.begin()->empty() &&
646 paragraphs.size() == 1 &&
647 bv->getParentLanguage(this) != text_.current_font.language())
649 LyXFont font(LyXFont::ALL_IGNORE);
650 font.setLanguage(bv->getParentLanguage(this));
651 setFont(bv, font, false);
654 updateLocal(bv, false);
655 // Tell the paragraph dialog that we've entered an insettext.
656 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
661 InsetText::priv_dispatch(FuncRequest const & cmd,
662 idx_type & idx, pos_type & pos)
664 BufferView * bv = cmd.view();
667 switch (cmd.action) {
668 case LFUN_MOUSE_PRESS:
670 return DispatchResult(true, true);
672 case LFUN_MOUSE_MOTION:
673 lfunMouseMotion(cmd);
674 return DispatchResult(true, true);
676 case LFUN_MOUSE_RELEASE:
677 return DispatchResult(lfunMouseRelease(cmd));
683 bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
684 no_selection = false;
686 DispatchResult result = UpdatableInset::priv_dispatch(cmd, idx, pos);
687 if (result.dispatched())
691 // This looks utterly strange. (Lgb)
692 if (cmd.action == LFUN_UNKNOWN_ACTION && cmd.argument.empty())
693 return DispatchResult(false, FINISHED);
696 if (the_locking_inset) {
697 DispatchResult result = the_locking_inset->dispatch(cmd);
699 if (result.dispatched()) {
700 if (result.update()) {
701 result.update(false);
702 updateLocal(bv, false);
707 switch (result.val()) {
709 moveRightIntern(bv, false, false);
710 result.dispatched(true);
715 if (result.val() >= FINISHED) {
716 updateLocal(bv, false);
717 bv->unlockInset(this);
721 result = moveDown(bv);
722 if (result.val() >= FINISHED) {
723 updateLocal(bv, false);
724 bv->unlockInset(this);
728 result.dispatched(true);
732 the_locking_inset = 0;
733 updateLocal(bv, false);
734 // make sure status gets reset immediately
735 bv->owner()->clearMessage();
739 switch (cmd.action) {
741 case LFUN_SELFINSERT:
742 if (bv->buffer()->isReadonly()) {
743 // setErrorMessage(N_("Document is read only"));
746 if (!cmd.argument.empty()) {
747 /* Automatically delete the currently selected
748 * text and replace it with what is being
749 * typed in now. Depends on lyxrc settings
750 * "auto_region_delete", which defaults to
753 // This should not be needed here and is also WRONG!
754 recordUndo(bv, Undo::INSERT, text_.cursorPar());
758 if (lyxrc.auto_region_delete && text_.selection.set())
759 text_.cutSelection(false, false);
760 text_.clearSelection();
762 for (string::size_type i = 0; i < cmd.argument.length(); ++i)
763 bv->owner()->getIntl().getTransManager().
764 TranslateAndInsert(cmd.argument[i], &text_);
766 text_.selection.cursor = text_.cursor;
767 result.dispatched(true);
771 // cursor movements that need special handling
774 result = moveRight(bv);
779 result = moveLeft(bv);
783 result = moveDown(bv);
791 if (crow() == text_.firstRow())
792 result.val(FINISHED_UP);
794 text_.cursorPrevious();
795 text_.clearSelection();
796 result.dispatched(true);
801 if (crow() == text_.lastRow())
802 result.val(FINISHED_DOWN);
805 text_.clearSelection();
806 result.dispatched(true);
811 if (text_.selection.set())
812 text_.cutSelection(true, false);
815 #warning should be also set dispatched here?
820 if (text_.selection.set())
821 text_.cutSelection(true, false);
824 #warning should be also set dispatched here?
829 if (!autoBreakRows_) {
830 if (CutAndPaste::nrOfParagraphs() > 1) {
832 #warning FIXME horrendously bad UI
834 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
837 replaceSelection(bv->getLyXText());
838 size_t sel_index = 0;
839 string const & arg = cmd.argument;
840 if (isStrUnsignedInt(arg)) {
841 #warning FIXME Check if the arg is in the domain of available selections.
842 sel_index = strToUnsignedInt(arg);
844 text_.pasteSelection(sel_index);
846 text_.clearSelection();
847 #warning should be also set dispatched here?
852 case LFUN_BREAKPARAGRAPH:
853 if (!autoBreakRows_) {
854 result.dispatched(true);
857 replaceSelection(bv->getLyXText());
858 text_.breakParagraph(paragraphs, 0);
859 #warning should be also set dispatched here?
864 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
865 if (!autoBreakRows_) {
866 result.dispatched(true);
869 replaceSelection(bv->getLyXText());
870 text_.breakParagraph(paragraphs, 1);
871 #warning should be also set dispatched here?
876 case LFUN_BREAKLINE: {
877 if (!autoBreakRows_) {
878 result.dispatched(true);
881 replaceSelection(bv->getLyXText());
882 auto_ptr<InsetNewline> ins(new InsetNewline);
883 text_.insertInset(ins.release());
884 #warning should be also set dispatched here?
891 // do not set layouts on non breakable textinsets
892 if (autoBreakRows_) {
893 string cur_layout = cpar()->layout()->name();
895 // Derive layout number from given argument (string)
896 // and current buffer's textclass (number).
897 LyXTextClass const & tclass =
898 bv->buffer()->params().getLyXTextClass();
899 string layout = cmd.argument;
900 bool hasLayout = tclass.hasLayout(layout);
902 // If the entry is obsolete, use the new one instead.
904 string const & obs = tclass[layout]->obsoleted_by();
909 // see if we found the layout number:
911 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
912 bv->owner()->dispatch(lf);
916 if (cur_layout != layout) {
918 text_.setLayout(layout);
919 bv->owner()->setLayout(cpar()->layout()->name());
920 #warning should be also set dispatched here?
924 // reset the layout box
925 bv->owner()->setLayout(cpar()->layout()->name());
933 if (result.update()) {
934 result.update(false);
935 updateLocal(bv, true);
938 /// If the action has deleted all text in the inset, we need to change the
939 // language to the language of the surronding text.
940 if (!was_empty && paragraphs.begin()->empty() &&
941 paragraphs.size() == 1) {
942 LyXFont font(LyXFont::ALL_IGNORE);
943 font.setLanguage(bv->getParentLanguage(this));
944 setFont(bv, font, false);
947 if (result.val() >= FINISHED) {
949 bv->unlockInset(this);
956 int InsetText::latex(Buffer const & buf, ostream & os,
957 LatexRunParams const & runparams) const
960 latexParagraphs(buf, paragraphs, os, texrow, runparams);
961 return texrow.rows();
965 int InsetText::ascii(Buffer const & buf, ostream & os,
966 LatexRunParams const & runparams) const
968 ParagraphList::const_iterator beg = paragraphs.begin();
969 ParagraphList::const_iterator end = paragraphs.end();
970 ParagraphList::const_iterator it = beg;
971 for (; it != end; ++it)
972 asciiParagraph(buf, *it, os, runparams, it == beg);
974 //FIXME: Give the total numbers of lines
979 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
980 LatexRunParams const & runparams) const
982 linuxdocParagraphs(buf, paragraphs, os, runparams);
987 int InsetText::docbook(Buffer const & buf, ostream & os,
988 LatexRunParams const & runparams) const
990 docbookParagraphs(buf, paragraphs, os, runparams);
995 void InsetText::validate(LaTeXFeatures & features) const
997 for_each(paragraphs.begin(), paragraphs.end(),
998 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1002 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1004 if (the_locking_inset) {
1005 the_locking_inset->getCursor(bv, x, y);
1009 y = cy() + InsetText::y();
1013 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1015 if (the_locking_inset) {
1016 the_locking_inset->getCursorPos(bv, x, y);
1019 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1020 y = cy() - TEXT_TO_INSET_OFFSET;
1024 int InsetText::insetInInsetY() const
1026 if (!the_locking_inset)
1029 return inset_y + the_locking_inset->insetInInsetY();
1033 void InsetText::fitInsetCursor(BufferView * bv) const
1035 if (the_locking_inset) {
1036 the_locking_inset->fitInsetCursor(bv);
1040 LyXFont const font = text_.getFont(cpar(), cpos());
1042 int const asc = font_metrics::maxAscent(font);
1043 int const desc = font_metrics::maxDescent(font);
1045 bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1049 DispatchResult InsetText::moveRight(BufferView * bv)
1051 if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1052 return moveLeftIntern(bv, false, true, false);
1054 return moveRightIntern(bv, true, true, false);
1058 DispatchResult InsetText::moveLeft(BufferView * bv)
1060 if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1061 return moveRightIntern(bv, true, true, false);
1063 return moveLeftIntern(bv, false, true, false);
1068 InsetText::moveRightIntern(BufferView * bv, bool front,
1069 bool activate_inset, bool selecting)
1071 ParagraphList::iterator c_par = cpar();
1073 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1074 return DispatchResult(false, FINISHED_RIGHT);
1075 if (activate_inset && checkAndActivateInset(bv, front))
1076 return DispatchResult(true, true);
1077 text_.cursorRight(bv);
1079 text_.clearSelection();
1080 return DispatchResult(true);
1085 InsetText::moveLeftIntern(BufferView * bv, bool front,
1086 bool activate_inset, bool selecting)
1088 if (cpar() == paragraphs.begin() && cpos() <= 0)
1089 return DispatchResult(false, FINISHED);
1090 text_.cursorLeft(bv);
1092 text_.clearSelection();
1093 if (activate_inset && checkAndActivateInset(bv, front))
1094 return DispatchResult(true, true);
1095 return DispatchResult(true);
1099 DispatchResult InsetText::moveUp(BufferView * bv)
1101 if (crow() == text_.firstRow())
1102 return DispatchResult(false, FINISHED_UP);
1104 text_.clearSelection();
1105 return DispatchResult(true);
1109 DispatchResult InsetText::moveDown(BufferView * bv)
1111 if (crow() == text_.lastRow())
1112 return DispatchResult(false, FINISHED_DOWN);
1113 text_.cursorDown(bv);
1114 text_.clearSelection();
1115 return DispatchResult(true);
1119 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1121 if (the_locking_inset) {
1122 if (the_locking_inset->insetAllowed(inset))
1123 return the_locking_inset->insertInset(bv, inset);
1126 inset->setOwner(this);
1127 text_.insertInset(inset);
1129 updateLocal(bv, true);
1134 bool InsetText::insetAllowed(InsetOld::Code code) const
1136 // in_insetAllowed is a really gross hack,
1137 // to allow us to call the owner's insetAllowed
1138 // without stack overflow, which can happen
1139 // when the owner uses InsetCollapsable::insetAllowed()
1141 if (in_insetAllowed)
1143 in_insetAllowed = true;
1144 if (the_locking_inset)
1145 ret = the_locking_inset->insetAllowed(code);
1147 ret = owner()->insetAllowed(code);
1148 in_insetAllowed = false;
1153 UpdatableInset * InsetText::getLockingInset() const
1155 return the_locking_inset ? the_locking_inset->getLockingInset() :
1156 const_cast<InsetText *>(this);
1160 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1164 if (the_locking_inset)
1165 return the_locking_inset->getFirstLockingInsetOfType(c);
1170 bool InsetText::showInsetDialog(BufferView * bv) const
1172 if (the_locking_inset)
1173 return the_locking_inset->showInsetDialog(bv);
1178 void InsetText::getLabelList(Buffer const & buffer,
1179 std::vector<string> & list) const
1181 ParagraphList::const_iterator pit = paragraphs.begin();
1182 ParagraphList::const_iterator pend = paragraphs.end();
1183 for (; pit != pend; ++pit) {
1184 InsetList::const_iterator beg = pit->insetlist.begin();
1185 InsetList::const_iterator end = pit->insetlist.end();
1186 for (; beg != end; ++beg)
1187 beg->inset->getLabelList(buffer, list);
1192 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1195 if (the_locking_inset) {
1196 the_locking_inset->setFont(bv, font, toggleall, selectall);
1200 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1201 || cpar()->empty()) {
1202 text_.setFont(font, toggleall);
1206 if (text_.selection.set())
1207 text_.recUndo(text_.cursor.par());
1211 text_.selection.cursor = text_.cursor;
1212 text_.cursorBottom();
1213 text_.setSelection();
1216 text_.toggleFree(font, toggleall);
1219 text_.clearSelection();
1222 updateLocal(bv, true);
1226 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1228 if (cpos() == cpar()->size())
1230 InsetOld * inset = cpar()->getInset(cpos());
1231 if (!isHighlyEditableInset(inset))
1233 inset->edit(bv, front);
1234 if (!the_locking_inset)
1236 updateLocal(bv, false);
1241 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y)
1244 int dummyy = y + dim_.asc;
1245 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1248 if (!isHighlyEditableInset(inset))
1254 inset_x = cx() - top_x;
1256 inset->edit(bv, x - inset_x, y - inset_y);
1257 if (!the_locking_inset)
1259 updateLocal(bv, false);
1264 void InsetText::markNew(bool track_changes)
1266 ParagraphList::iterator pit = paragraphs.begin();
1267 ParagraphList::iterator end = paragraphs.end();
1268 for (; pit != end; ++pit) {
1269 if (track_changes) {
1270 pit->trackChanges();
1272 // no-op when not tracking
1273 pit->cleanChanges();
1279 void InsetText::setText(string const & data, LyXFont const & font)
1282 for (unsigned int i = 0; i < data.length(); ++i)
1283 paragraphs.begin()->insertChar(i, data[i], font);
1287 void InsetText::setAutoBreakRows(bool flag)
1289 if (flag != autoBreakRows_) {
1290 autoBreakRows_ = flag;
1297 void InsetText::setDrawFrame(DrawFrame how)
1303 LColor_color InsetText::frameColor() const
1305 return LColor::color(frame_color_);
1309 void InsetText::setFrameColor(LColor_color col)
1315 int InsetText::cx() const
1317 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1318 if (the_locking_inset) {
1319 LyXFont font = text_.getFont(text_.cursorPar(), text_.cursor.pos());
1320 if (font.isVisibleRightToLeft())
1321 x -= the_locking_inset->width();
1327 int InsetText::cy() const
1329 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1333 pos_type InsetText::cpos() const
1335 return text_.cursor.pos();
1339 ParagraphList::iterator InsetText::cpar() const
1341 return text_.cursorPar();
1345 bool InsetText::cboundary() const
1347 return text_.cursor.boundary();
1351 RowList::iterator InsetText::crow() const
1353 return cpar()->getRow(cpos());
1357 LyXText * InsetText::getLyXText(BufferView const * bv,
1358 bool const recursive) const
1361 if (recursive && the_locking_inset)
1362 return the_locking_inset->getLyXText(bv, true);
1367 void InsetText::setViewCache(BufferView const * bv) const
1370 if (bv != text_.bv_owner) {
1371 //lyxerr << "setting view cache from "
1372 // << text_.bv_owner << " to " << bv << "\n";
1373 text_.init(const_cast<BufferView *>(bv));
1375 text_.bv_owner = const_cast<BufferView *>(bv);
1380 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1383 /// then remove all LyXText in text-insets
1384 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1385 const_cast<ParagraphList&>(paragraphs).end(),
1386 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1391 void InsetText::removeNewlines()
1393 ParagraphList::iterator it = paragraphs.begin();
1394 ParagraphList::iterator end = paragraphs.end();
1395 for (; it != end; ++it)
1396 for (int i = 0; i < it->size(); ++i)
1397 if (it->isNewline(i))
1402 int InsetText::scroll(bool recursive) const
1404 int sx = UpdatableInset::scroll(false);
1406 if (recursive && the_locking_inset)
1407 sx += the_locking_inset->scroll(recursive);
1413 void InsetText::clearSelection(BufferView *)
1415 text_.clearSelection();
1419 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1421 Painter & pain = bv->painter();
1423 int h = dim_.asc + dim_.des;
1424 int ty = baseline - dim_.asc;
1430 if (ty + h > pain.paperHeight())
1431 h = pain.paperHeight();
1432 if (top_x + w > pain.paperWidth())
1433 w = pain.paperWidth();
1434 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1438 ParagraphList * InsetText::getParagraphs(int i) const
1440 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
1444 LyXText * InsetText::getText(int i) const
1446 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
1450 LyXCursor const & InsetText::cursor(BufferView * bv) const
1452 if (the_locking_inset)
1453 return the_locking_inset->cursor(bv);
1454 return getLyXText(bv)->cursor;
1458 bool InsetText::checkInsertChar(LyXFont & font)
1460 return owner() ? owner()->checkInsertChar(font) : true;
1464 void InsetText::collapseParagraphs(BufferView * bv)
1466 while (paragraphs.size() > 1) {
1467 ParagraphList::iterator const first = paragraphs.begin();
1468 ParagraphList::iterator second = first;
1470 size_t const first_par_size = first->size();
1472 if (!first->empty() &&
1474 !first->isSeparator(first_par_size - 1)) {
1475 first->insertChar(first_par_size, ' ');
1478 #warning probably broken
1479 if (text_.selection.set()) {
1480 if (text_.selection.start.par() == 1) {
1481 text_.selection.start.par(1);
1482 text_.selection.start.pos(text_.selection.start.pos() + first_par_size);
1484 if (text_.selection.end.par() == 2) {
1485 text_.selection.end.par(1);
1486 text_.selection.end.pos(text_.selection.end.pos() + first_par_size);
1490 mergeParagraph(bv->buffer()->params(), paragraphs, first);
1495 void InsetText::getDrawFont(LyXFont & font) const
1499 owner()->getDrawFont(font);
1503 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1505 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1506 // And it probably does. You have to take a look at this John. (Lgb)
1507 #warning John, have a look here. (Lgb)
1508 ParagraphList::iterator pit = plist.begin();
1509 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1511 mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
1513 ParagraphList::iterator pend = plist.end();
1514 for (; pit != pend; ++pit)
1515 paragraphs.push_back(*pit);
1519 void InsetText::addPreview(PreviewLoader & loader) const
1521 ParagraphList::const_iterator pit = paragraphs.begin();
1522 ParagraphList::const_iterator pend = paragraphs.end();
1524 for (; pit != pend; ++pit) {
1525 InsetList::const_iterator it = pit->insetlist.begin();
1526 InsetList::const_iterator end = pit->insetlist.end();
1527 for (; it != end; ++it)
1528 it->inset->addPreview(loader);