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;
119 the_locking_inset = 0;
121 in_insetAllowed = false;
127 void InsetText::clear(bool just_mark_erased)
129 if (just_mark_erased) {
130 ParagraphList::iterator it = paragraphs.begin();
131 ParagraphList::iterator end = paragraphs.end();
132 for (; it != end; ++it)
137 // This is a gross hack...
138 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
141 paragraphs.push_back(Paragraph());
142 paragraphs.begin()->setInsetOwner(this);
143 paragraphs.begin()->layout(old_layout);
147 auto_ptr<InsetBase> InsetText::clone() const
149 return auto_ptr<InsetBase>(new InsetText(*this));
153 void InsetText::write(Buffer const & buf, ostream & os) const
156 writeParagraphData(buf, os);
160 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
162 ParagraphList::const_iterator it = paragraphs.begin();
163 ParagraphList::const_iterator end = paragraphs.end();
164 Paragraph::depth_type dth = 0;
165 for (; it != end; ++it) {
166 it->write(buf, os, buf.params(), dth);
171 void InsetText::read(Buffer const & buf, LyXLex & lex)
174 Paragraph::depth_type depth = 0;
178 #warning John, look here. Doesnt make much sense.
179 if (buf.params().tracking_changes)
180 paragraphs.begin()->trackChanges();
182 // delete the initial paragraph
183 Paragraph oldpar = *paragraphs.begin();
185 ParagraphList::iterator pit = paragraphs.begin();
189 token = lex.getString();
192 if (token == "\\end_inset") {
196 if (token == "\\end_document") {
197 lex.printError("\\end_document read in inset! Error in document!");
202 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
205 pit = paragraphs.begin();
206 ParagraphList::iterator const end = paragraphs.end();
207 for (; pit != end; ++pit)
208 pit->setInsetOwner(this);
210 if (token != "\\end_inset") {
211 lex.printError("Missing \\end_inset at this point. "
216 // ensure we have at least one par.
217 if (paragraphs.empty())
218 paragraphs.push_back(oldpar);
222 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
224 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
225 textwidth_ = mi.base.textwidth - 30;
226 BufferView * bv = mi.base.bv;
228 text_.metrics(mi, dim);
229 dim.asc += TEXT_TO_INSET_OFFSET;
230 dim.des += TEXT_TO_INSET_OFFSET;
231 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
232 dim.wid = max(dim.wid, 10);
237 int InsetText::textWidth() const
243 void InsetText::draw(PainterInfo & pi, int x, int y) const
245 // update our idea of where we are. Clearly, we should
246 // not have to know this information.
249 int const start_x = x;
251 BufferView * bv = pi.base.bv;
252 Painter & pain = pi.pain;
254 // repaint the background if needed
255 if (backgroundColor() != LColor::background)
256 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
258 // no draw is necessary !!!
259 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
270 top_y = y - dim_.asc;
272 if (the_locking_inset
273 && text_.cursor.par() == inset_par && cpos() == inset_pos) {
278 x += TEXT_TO_INSET_OFFSET;
280 paintTextInset(*bv, text_, x, y);
282 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
283 drawFrame(pain, start_x);
287 void InsetText::drawFrame(Painter & pain, int x) const
289 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
290 int const frame_x = x + ttoD2;
291 int const frame_y = top_baseline - dim_.asc + ttoD2;
292 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
293 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
294 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
298 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
303 if (!autoBreakRows_ && paragraphs.size() > 1)
304 collapseParagraphs(bv);
306 if (!text_.selection.set())
307 text_.selection.cursor = text_.cursor;
310 bv->updateInset(this);
311 bv->owner()->view_state_changed();
312 bv->owner()->updateMenubar();
313 bv->owner()->updateToolbar();
314 if (old_par != text_.cursor.par()) {
315 bv->owner()->setLayout(cpar()->layout()->name());
316 old_par = text_.cursor.par();
321 string const InsetText::editMessage() const
323 return _("Opened Text Inset");
327 void InsetText::insetUnlock(BufferView * bv)
329 if (the_locking_inset) {
330 the_locking_inset->insetUnlock(bv);
331 the_locking_inset = 0;
332 updateLocal(bv, false);
337 if (text_.selection.set())
338 text_.clearSelection();
340 bv->owner()->setLayout(owner()->getLyXText(bv)
341 ->cursorPar()->layout()->name());
343 bv->owner()->setLayout(bv->text->cursorPar()->layout()->name());
344 // hack for deleteEmptyParMech
345 if (!paragraphs.begin()->empty())
346 text_.setCursor(0, 0);
347 else if (paragraphs.size() > 1)
348 text_.setCursor(1, 0);
352 void InsetText::lockInset(BufferView * bv)
355 the_locking_inset = 0;
356 inset_pos = inset_x = inset_y = 0;
357 inset_boundary = false;
360 text_.setCursorIntern(0, 0);
361 text_.clearSelection();
363 // If the inset is empty set the language of the current font to the
364 // language to the surronding text (if different).
365 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
366 bv->getParentLanguage(this) != text_.current_font.language()) {
367 LyXFont font(LyXFont::ALL_IGNORE);
368 font.setLanguage(bv->getParentLanguage(this));
369 setFont(bv, font, false);
374 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
376 the_locking_inset = inset;
377 inset_x = cx() - top_x;
380 inset_par = text_.cursor.par();
381 inset_boundary = cboundary();
385 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
387 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
388 << inset << "): " << endl;
391 if (!the_locking_inset) {
392 ParagraphList::iterator pit = paragraphs.begin();
393 ParagraphList::iterator pend = paragraphs.end();
395 for (; pit != pend; ++pit) {
396 InsetList::iterator it = pit->insetlist.begin();
397 InsetList::iterator const end = pit->insetlist.end();
398 for (; it != end; ++it) {
399 if (it->inset == inset) {
400 lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
401 text_.setCursorIntern(
402 std::distance(paragraphs.begin(), pit), it->pos);
403 lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
404 lyxerr << "bv: " << bv << " inset: " << inset << endl;
405 lockInset(bv, inset);
406 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
411 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
414 if (inset == cpar()->getInset(cpos())) {
415 lyxerr[Debug::INSETS] << "OK" << endl;
416 lockInset(bv, inset);
420 if (the_locking_inset && the_locking_inset == inset) {
421 if (text_.cursor.par() == inset_par && cpos() == inset_pos) {
422 lyxerr[Debug::INSETS] << "OK" << endl;
423 inset_x = cx() - top_x;
426 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
428 } else if (the_locking_inset) {
429 lyxerr[Debug::INSETS] << "MAYBE" << endl;
430 return the_locking_inset->lockInsetInInset(bv, inset);
432 lyxerr[Debug::INSETS] << "NOT OK" << endl;
437 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
440 if (!the_locking_inset)
442 if (the_locking_inset == inset) {
443 the_locking_inset->insetUnlock(bv);
444 the_locking_inset = 0;
446 moveRightIntern(bv, true, false);
447 old_par = -1; // force layout setting
451 updateLocal(bv, false);
454 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
458 void InsetText::lfunMousePress(FuncRequest const & cmd)
462 // use this to check mouse motion for selection!
466 BufferView * bv = cmd.view();
467 FuncRequest cmd1 = cmd;
474 int tmp_y = cmd.y + dim_.asc - bv->top_y();
475 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
477 if (the_locking_inset) {
478 if (the_locking_inset == inset) {
479 the_locking_inset->dispatch(cmd1);
482 // otherwise only unlock the_locking_inset
483 the_locking_inset->insetUnlock(bv);
484 the_locking_inset = 0;
487 no_selection = false;
489 if (bv->theLockingInset()) {
490 if (isHighlyEditableInset(inset)) {
491 // We just have to lock the inset before calling a
493 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
494 if (!bv->lockInset(uinset)) {
495 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
497 inset->dispatch(cmd1);
498 if (the_locking_inset)
499 updateLocal(bv, false);
504 bool paste_internally = false;
505 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
506 dispatch(FuncRequest(bv, LFUN_COPY));
507 paste_internally = true;
509 int old_top_y = bv->top_y();
511 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
512 // set the selection cursor!
513 text_.selection.cursor = text_.cursor;
514 bv->x_target(text_.cursor.x());
516 text_.clearSelection();
517 updateLocal(bv, false);
519 bv->owner()->setLayout(cpar()->layout()->name());
521 // we moved the view we cannot do mouse selection in this case!
522 if (bv->top_y() != old_top_y)
524 old_par = text_.cursor.par();
525 // Insert primary selection with middle mouse
526 // if there is a local selection in the current buffer,
528 if (cmd.button() == mouse_button::button2) {
529 if (paste_internally)
530 dispatch(FuncRequest(bv, LFUN_PASTE));
532 dispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
535 getLyXText(bv)->clearSelection();
540 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
542 BufferView * bv = cmd.view();
543 FuncRequest cmd1 = cmd;
548 if (the_locking_inset) {
549 DispatchResult const res = the_locking_inset->dispatch(cmd1);
551 return res.dispatched();
555 int tmp_y = cmd.y + dim_.asc - bv->top_y();
556 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
560 // We still need to deal properly with the whole relative vs.
561 // absolute mouse co-ords thing in a realiable, sensible way
562 DispatchResult const res = inset->dispatch(cmd1);
563 bool const ret = res.dispatched();
564 updateLocal(bv, false);
569 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
571 FuncRequest cmd1 = cmd;
575 if (the_locking_inset) {
576 the_locking_inset->dispatch(cmd1);
580 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
583 BufferView * bv = cmd.view();
584 LyXCursor cur = text_.cursor;
585 text_.setCursorFromCoordinates (cmd.x, cmd.y + dim_.asc);
586 bv->x_target(text_.cursor.x());
587 if (cur == text_.cursor)
589 text_.setSelection();
590 updateLocal(bv, false);
595 InsetText::priv_dispatch(FuncRequest const & cmd,
596 idx_type & idx, pos_type & pos)
598 BufferView * bv = cmd.view();
601 switch (cmd.action) {
602 case LFUN_INSET_EDIT: {
603 UpdatableInset::priv_dispatch(cmd, idx, pos);
605 if (!bv->lockInset(this)) {
606 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
607 return DispatchResult(true, true);
611 the_locking_inset = 0;
615 inset_boundary = false;
620 if (cmd.argument.size()) {
621 if (cmd.argument == "left")
622 text_.setCursorIntern(0, 0);
624 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
626 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
627 // we put here -1 and not button as now the button in the
628 // edit call should not be needed we will fix this in 1.3.x
629 // cycle hopefully (Jug 20020509)
630 // FIXME: GUII I've changed this to none: probably WRONG
631 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
632 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
633 text_.cursor.x(text_.cursor.x());
634 bv->x_target(text_.cursor.x());
638 text_.clearSelection();
641 // If the inset is empty set the language of the current font to the
642 // language to the surronding text (if different).
643 if (paragraphs.begin()->empty() &&
644 paragraphs.size() == 1 &&
645 bv->getParentLanguage(this) != text_.current_font.language())
647 LyXFont font(LyXFont::ALL_IGNORE);
648 font.setLanguage(bv->getParentLanguage(this));
649 setFont(bv, font, false);
652 updateLocal(bv, false);
653 // Tell the paragraph dialog that we've entered an insettext.
654 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
655 return DispatchResult(true, true);
658 case LFUN_MOUSE_PRESS:
660 return DispatchResult(true, true);
662 case LFUN_MOUSE_MOTION:
663 lfunMouseMotion(cmd);
664 return DispatchResult(true, true);
666 case LFUN_MOUSE_RELEASE:
667 return DispatchResult(lfunMouseRelease(cmd));
673 bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
674 no_selection = false;
676 DispatchResult result = UpdatableInset::priv_dispatch(cmd, idx, pos);
677 if (result.dispatched())
681 // This looks utterly strange. (Lgb)
682 if (cmd.action == LFUN_UNKNOWN_ACTION && cmd.argument.empty())
683 return DispatchResult(false, FINISHED);
686 if (the_locking_inset) {
687 DispatchResult result = the_locking_inset->dispatch(cmd);
689 if (result.dispatched()) {
690 if (result.update()) {
691 result.update(false);
692 updateLocal(bv, false);
697 switch (result.val()) {
699 moveRightIntern(bv, false, false);
700 result.dispatched(true);
705 if (result.val() >= FINISHED) {
706 updateLocal(bv, false);
707 bv->unlockInset(this);
711 result = moveDown(bv);
712 if (result.val() >= FINISHED) {
713 updateLocal(bv, false);
714 bv->unlockInset(this);
718 result.dispatched(true);
722 the_locking_inset = 0;
723 updateLocal(bv, false);
724 // make sure status gets reset immediately
725 bv->owner()->clearMessage();
729 switch (cmd.action) {
731 case LFUN_SELFINSERT:
732 if (bv->buffer()->isReadonly()) {
733 // setErrorMessage(N_("Document is read only"));
736 if (!cmd.argument.empty()) {
737 /* Automatically delete the currently selected
738 * text and replace it with what is being
739 * typed in now. Depends on lyxrc settings
740 * "auto_region_delete", which defaults to
743 // This should not be needed here and is also WRONG!
744 recordUndo(bv, Undo::INSERT, text_.cursorPar());
748 if (lyxrc.auto_region_delete && text_.selection.set())
749 text_.cutSelection(false, false);
750 text_.clearSelection();
752 for (string::size_type i = 0; i < cmd.argument.length(); ++i)
753 bv->owner()->getIntl().getTransManager().
754 TranslateAndInsert(cmd.argument[i], &text_);
756 text_.selection.cursor = text_.cursor;
757 result.dispatched(true);
761 // cursor movements that need special handling
764 result = moveRight(bv);
769 result = moveLeft(bv);
773 result = moveDown(bv);
781 if (crow() == text_.firstRow())
782 result.val(FINISHED_UP);
784 text_.cursorPrevious();
785 text_.clearSelection();
786 result.dispatched(true);
791 if (crow() == text_.lastRow())
792 result.val(FINISHED_DOWN);
795 text_.clearSelection();
796 result.dispatched(true);
801 if (text_.selection.set())
802 text_.cutSelection(true, false);
805 #warning should be also set dispatched here?
810 if (text_.selection.set())
811 text_.cutSelection(true, false);
814 #warning should be also set dispatched here?
819 if (!autoBreakRows_) {
820 if (CutAndPaste::nrOfParagraphs() > 1) {
822 #warning FIXME horrendously bad UI
824 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
827 replaceSelection(bv->getLyXText());
828 size_t sel_index = 0;
829 string const & arg = cmd.argument;
830 if (isStrUnsignedInt(arg)) {
831 #warning FIXME Check if the arg is in the domain of available selections.
832 sel_index = strToUnsignedInt(arg);
834 text_.pasteSelection(sel_index);
836 text_.clearSelection();
837 #warning should be also set dispatched here?
842 case LFUN_BREAKPARAGRAPH:
843 if (!autoBreakRows_) {
844 result.dispatched(true);
847 replaceSelection(bv->getLyXText());
848 text_.breakParagraph(paragraphs, 0);
849 #warning should be also set dispatched here?
854 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
855 if (!autoBreakRows_) {
856 result.dispatched(true);
859 replaceSelection(bv->getLyXText());
860 text_.breakParagraph(paragraphs, 1);
861 #warning should be also set dispatched here?
866 case LFUN_BREAKLINE: {
867 if (!autoBreakRows_) {
868 result.dispatched(true);
871 replaceSelection(bv->getLyXText());
872 auto_ptr<InsetNewline> ins(new InsetNewline);
873 text_.insertInset(ins.release());
874 #warning should be also set dispatched here?
881 // do not set layouts on non breakable textinsets
882 if (autoBreakRows_) {
883 string cur_layout = cpar()->layout()->name();
885 // Derive layout number from given argument (string)
886 // and current buffer's textclass (number).
887 LyXTextClass const & tclass =
888 bv->buffer()->params().getLyXTextClass();
889 string layout = cmd.argument;
890 bool hasLayout = tclass.hasLayout(layout);
892 // If the entry is obsolete, use the new one instead.
894 string const & obs = tclass[layout]->obsoleted_by();
899 // see if we found the layout number:
901 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
902 bv->owner()->dispatch(lf);
906 if (cur_layout != layout) {
908 text_.setLayout(layout);
909 bv->owner()->setLayout(cpar()->layout()->name());
910 #warning should be also set dispatched here?
914 // reset the layout box
915 bv->owner()->setLayout(cpar()->layout()->name());
923 if (result.update()) {
924 result.update(false);
925 updateLocal(bv, true);
928 /// If the action has deleted all text in the inset, we need to change the
929 // language to the language of the surronding text.
930 if (!was_empty && paragraphs.begin()->empty() &&
931 paragraphs.size() == 1) {
932 LyXFont font(LyXFont::ALL_IGNORE);
933 font.setLanguage(bv->getParentLanguage(this));
934 setFont(bv, font, false);
937 if (result.val() >= FINISHED) {
939 bv->unlockInset(this);
946 int InsetText::latex(Buffer const & buf, ostream & os,
947 LatexRunParams const & runparams) const
950 latexParagraphs(buf, paragraphs, os, texrow, runparams);
951 return texrow.rows();
955 int InsetText::ascii(Buffer const & buf, ostream & os,
956 LatexRunParams const & runparams) const
958 ParagraphList::const_iterator beg = paragraphs.begin();
959 ParagraphList::const_iterator end = paragraphs.end();
960 ParagraphList::const_iterator it = beg;
961 for (; it != end; ++it)
962 asciiParagraph(buf, *it, os, runparams, it == beg);
964 //FIXME: Give the total numbers of lines
969 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
970 LatexRunParams const & runparams) const
972 linuxdocParagraphs(buf, paragraphs, os, runparams);
977 int InsetText::docbook(Buffer const & buf, ostream & os,
978 LatexRunParams const & runparams) const
980 docbookParagraphs(buf, paragraphs, os, runparams);
985 void InsetText::validate(LaTeXFeatures & features) const
987 for_each(paragraphs.begin(), paragraphs.end(),
988 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
992 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
994 if (the_locking_inset) {
995 the_locking_inset->getCursor(bv, x, y);
999 y = cy() + InsetText::y();
1003 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1005 if (the_locking_inset) {
1006 the_locking_inset->getCursorPos(bv, x, y);
1009 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1010 y = cy() - TEXT_TO_INSET_OFFSET;
1014 int InsetText::insetInInsetY() const
1016 if (!the_locking_inset)
1019 return inset_y + the_locking_inset->insetInInsetY();
1023 void InsetText::fitInsetCursor(BufferView * bv) const
1025 if (the_locking_inset) {
1026 the_locking_inset->fitInsetCursor(bv);
1030 LyXFont const font = text_.getFont(cpar(), cpos());
1032 int const asc = font_metrics::maxAscent(font);
1033 int const desc = font_metrics::maxDescent(font);
1035 bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1039 DispatchResult InsetText::moveRight(BufferView * bv)
1041 if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1042 return moveLeftIntern(bv, false, true, false);
1044 return moveRightIntern(bv, true, true, false);
1048 DispatchResult InsetText::moveLeft(BufferView * bv)
1050 if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1051 return moveRightIntern(bv, true, true, false);
1053 return moveLeftIntern(bv, false, true, false);
1058 InsetText::moveRightIntern(BufferView * bv, bool front,
1059 bool activate_inset, bool selecting)
1061 ParagraphList::iterator c_par = cpar();
1063 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1064 return DispatchResult(false, FINISHED_RIGHT);
1065 if (activate_inset && checkAndActivateInset(bv, front))
1066 return DispatchResult(true, true);
1067 text_.cursorRight(bv);
1069 text_.clearSelection();
1070 return DispatchResult(true);
1075 InsetText::moveLeftIntern(BufferView * bv, bool front,
1076 bool activate_inset, bool selecting)
1078 if (cpar() == paragraphs.begin() && cpos() <= 0)
1079 return DispatchResult(false, FINISHED);
1080 text_.cursorLeft(bv);
1082 text_.clearSelection();
1083 if (activate_inset && checkAndActivateInset(bv, front))
1084 return DispatchResult(true, true);
1085 return DispatchResult(true);
1089 DispatchResult InsetText::moveUp(BufferView * bv)
1091 if (crow() == text_.firstRow())
1092 return DispatchResult(false, FINISHED_UP);
1094 text_.clearSelection();
1095 return DispatchResult(true);
1099 DispatchResult InsetText::moveDown(BufferView * bv)
1101 if (crow() == text_.lastRow())
1102 return DispatchResult(false, FINISHED_DOWN);
1103 text_.cursorDown(bv);
1104 text_.clearSelection();
1105 return DispatchResult(true);
1109 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1111 if (the_locking_inset) {
1112 if (the_locking_inset->insetAllowed(inset))
1113 return the_locking_inset->insertInset(bv, inset);
1116 inset->setOwner(this);
1117 text_.insertInset(inset);
1119 updateLocal(bv, true);
1124 bool InsetText::insetAllowed(InsetOld::Code code) const
1126 // in_insetAllowed is a really gross hack,
1127 // to allow us to call the owner's insetAllowed
1128 // without stack overflow, which can happen
1129 // when the owner uses InsetCollapsable::insetAllowed()
1131 if (in_insetAllowed)
1133 in_insetAllowed = true;
1134 if (the_locking_inset)
1135 ret = the_locking_inset->insetAllowed(code);
1137 ret = owner()->insetAllowed(code);
1138 in_insetAllowed = false;
1143 UpdatableInset * InsetText::getLockingInset() const
1145 return the_locking_inset ? the_locking_inset->getLockingInset() :
1146 const_cast<InsetText *>(this);
1150 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1154 if (the_locking_inset)
1155 return the_locking_inset->getFirstLockingInsetOfType(c);
1160 bool InsetText::showInsetDialog(BufferView * bv) const
1162 if (the_locking_inset)
1163 return the_locking_inset->showInsetDialog(bv);
1168 void InsetText::getLabelList(Buffer const & buffer,
1169 std::vector<string> & list) const
1171 ParagraphList::const_iterator pit = paragraphs.begin();
1172 ParagraphList::const_iterator pend = paragraphs.end();
1173 for (; pit != pend; ++pit) {
1174 InsetList::const_iterator beg = pit->insetlist.begin();
1175 InsetList::const_iterator end = pit->insetlist.end();
1176 for (; beg != end; ++beg)
1177 beg->inset->getLabelList(buffer, list);
1182 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1185 if (the_locking_inset) {
1186 the_locking_inset->setFont(bv, font, toggleall, selectall);
1190 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1191 || cpar()->empty()) {
1192 text_.setFont(font, toggleall);
1197 if (text_.selection.set())
1198 text_.recUndo(text_.cursor.par());
1202 text_.selection.cursor = text_.cursor;
1203 text_.cursorBottom();
1204 text_.setSelection();
1207 text_.toggleFree(font, toggleall);
1210 text_.clearSelection();
1213 updateLocal(bv, true);
1217 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1219 if (cpos() == cpar()->size())
1221 InsetOld * inset = cpar()->getInset(cpos());
1222 if (!isHighlyEditableInset(inset))
1224 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1225 inset->dispatch(cmd);
1226 if (!the_locking_inset)
1228 updateLocal(bv, false);
1233 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1234 mouse_button::state button)
1237 int dummyy = y + dim_.asc;
1238 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1239 // we only do the edit() call if the inset was hit by the mouse
1240 // or if it is a highly editable inset. So we should call this
1241 // function from our own edit with button < 0.
1242 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1244 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1253 inset_x = cx() - top_x;
1255 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1256 inset->dispatch(cmd);
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::nextChange(BufferView * bv, lyx::pos_type & length)
1460 if (the_locking_inset) {
1461 if (the_locking_inset->nextChange(bv, length))
1463 text_.cursorRight(true);
1465 lyx::find::SearchResult result =
1466 lyx::find::findNextChange(bv, &text_, length);
1468 if (result == lyx::find::SR_FOUND) {
1469 LyXCursor cur = text_.cursor;
1470 bv->unlockInset(bv->theLockingInset());
1471 if (bv->lockInset(this))
1474 text_.setSelectionRange(length);
1475 updateLocal(bv, false);
1477 return result != lyx::find::SR_NOT_FOUND;
1481 bool InsetText::searchForward(BufferView * bv, string const & str,
1484 if (the_locking_inset) {
1485 if (the_locking_inset->searchForward(bv, str, cs, mw))
1487 text_.cursorRight(true);
1489 lyx::find::SearchResult result =
1490 lyx::find::find(bv, &text_, str, true, cs, mw);
1492 if (result == lyx::find::SR_FOUND) {
1493 LyXCursor cur = text_.cursor;
1494 bv->unlockInset(bv->theLockingInset());
1495 if (bv->lockInset(this))
1498 text_.setSelectionRange(str.length());
1499 updateLocal(bv, false);
1501 return result != lyx::find::SR_NOT_FOUND;
1505 bool InsetText::searchBackward(BufferView * bv, string const & str,
1508 if (the_locking_inset) {
1509 if (the_locking_inset->searchBackward(bv, str, cs, mw))
1513 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
1515 lyx::find::SearchResult result =
1516 lyx::find::find(bv, &text_, str, false, cs, mw);
1518 if (result == lyx::find::SR_FOUND) {
1519 LyXCursor cur = text_.cursor;
1520 bv->unlockInset(bv->theLockingInset());
1521 if (bv->lockInset(this))
1524 text_.setSelectionRange(str.length());
1525 updateLocal(bv, false);
1527 return result != lyx::find::SR_NOT_FOUND;
1531 bool InsetText::checkInsertChar(LyXFont & font)
1533 return owner() ? owner()->checkInsertChar(font) : true;
1537 void InsetText::collapseParagraphs(BufferView * bv)
1539 while (paragraphs.size() > 1) {
1540 ParagraphList::iterator const first = paragraphs.begin();
1541 ParagraphList::iterator second = first;
1543 size_t const first_par_size = first->size();
1545 if (!first->empty() &&
1547 !first->isSeparator(first_par_size - 1)) {
1548 first->insertChar(first_par_size, ' ');
1551 #warning probably broken
1552 if (text_.selection.set()) {
1553 if (text_.selection.start.par() == 1) {
1554 text_.selection.start.par(1);
1555 text_.selection.start.pos(text_.selection.start.pos() + first_par_size);
1557 if (text_.selection.end.par() == 2) {
1558 text_.selection.end.par(1);
1559 text_.selection.end.pos(text_.selection.end.pos() + first_par_size);
1563 mergeParagraph(bv->buffer()->params(), paragraphs, first);
1568 void InsetText::getDrawFont(LyXFont & font) const
1572 owner()->getDrawFont(font);
1576 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1578 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1579 // And it probably does. You have to take a look at this John. (Lgb)
1580 #warning John, have a look here. (Lgb)
1581 ParagraphList::iterator pit = plist.begin();
1582 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1584 mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
1586 ParagraphList::iterator pend = plist.end();
1587 for (; pit != pend; ++pit)
1588 paragraphs.push_back(*pit);
1592 void InsetText::addPreview(PreviewLoader & loader) const
1594 ParagraphList::const_iterator pit = paragraphs.begin();
1595 ParagraphList::const_iterator pend = paragraphs.end();
1597 for (; pit != pend; ++pit) {
1598 InsetList::const_iterator it = pit->insetlist.begin();
1599 InsetList::const_iterator end = pit->insetlist.end();
1600 for (; it != end; ++it)
1601 it->inset->addPreview(loader);