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"
38 #include "WordLangTuple.h"
40 #include "frontends/Alert.h"
41 #include "frontends/font_metrics.h"
42 #include "frontends/LyXView.h"
43 #include "frontends/Painter.h"
45 #include "support/lyxalgo.h" // lyx::count
47 #include <boost/bind.hpp>
49 using bv_funcs::replaceSelection;
53 using lyx::graphics::PreviewLoader;
55 using lyx::support::isStrUnsignedInt;
56 using lyx::support::strToUnsignedInt;
67 InsetText::InsetText(BufferParams const & bp)
70 autoBreakRows_(false),
72 frame_color_(LColor::insetframe),
73 text_(0, this, true, paragraphs)
75 textwidth_ = 0; // broken
76 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
77 if (bp.tracking_changes)
78 paragraphs.begin()->trackChanges();
83 InsetText::InsetText(InsetText const & in)
85 text_(in.text_.bv_owner, this, true, paragraphs)
92 void InsetText::operator=(InsetText const & in)
94 UpdatableInset::operator=(in);
95 paragraphs = in.paragraphs;
96 autoBreakRows_ = in.autoBreakRows_;
97 drawFrame_ = in.drawFrame_;
98 frame_color_ = in.frame_color_;
99 textwidth_ = in.textwidth_;
100 text_ = LyXText(in.text_.bv_owner, this, true, paragraphs);
105 void InsetText::init()
107 ParagraphList::iterator pit = paragraphs.begin();
108 ParagraphList::iterator end = paragraphs.end();
109 for (; pit != end; ++pit)
110 pit->setInsetOwner(this);
111 text_.paragraphs_ = ¶graphs;
120 the_locking_inset = 0;
122 in_insetAllowed = false;
128 void InsetText::clear(bool just_mark_erased)
130 if (just_mark_erased) {
131 ParagraphList::iterator it = paragraphs.begin();
132 ParagraphList::iterator end = paragraphs.end();
133 for (; it != end; ++it)
138 // This is a gross hack...
139 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
142 paragraphs.push_back(Paragraph());
143 paragraphs.begin()->setInsetOwner(this);
144 paragraphs.begin()->layout(old_layout);
148 auto_ptr<InsetBase> InsetText::clone() const
150 return auto_ptr<InsetBase>(new InsetText(*this));
154 void InsetText::write(Buffer const & buf, ostream & os) const
157 writeParagraphData(buf, os);
161 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
163 ParagraphList::const_iterator it = paragraphs.begin();
164 ParagraphList::const_iterator end = paragraphs.end();
165 Paragraph::depth_type dth = 0;
166 for (; it != end; ++it) {
167 it->write(buf, os, buf.params(), dth);
172 void InsetText::read(Buffer const & buf, LyXLex & lex)
175 Paragraph::depth_type depth = 0;
179 #warning John, look here. Doesnt make much sense.
180 if (buf.params().tracking_changes)
181 paragraphs.begin()->trackChanges();
183 // delete the initial paragraph
184 Paragraph oldpar = *paragraphs.begin();
186 ParagraphList::iterator pit = paragraphs.begin();
190 token = lex.getString();
193 if (token == "\\end_inset") {
197 if (token == "\\end_document") {
198 lex.printError("\\end_document read in inset! Error in document!");
203 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
206 pit = paragraphs.begin();
207 ParagraphList::iterator const end = paragraphs.end();
208 for (; pit != end; ++pit)
209 pit->setInsetOwner(this);
211 if (token != "\\end_inset") {
212 lex.printError("Missing \\end_inset at this point. "
217 // ensure we have at least one par.
218 if (paragraphs.empty())
219 paragraphs.push_back(oldpar);
223 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
225 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
226 textwidth_ = mi.base.textwidth - 30;
227 BufferView * bv = mi.base.bv;
229 text_.metrics(mi, dim);
230 dim.asc += TEXT_TO_INSET_OFFSET;
231 dim.des += TEXT_TO_INSET_OFFSET;
232 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
233 dim.wid = max(dim.wid, 10);
238 int InsetText::textWidth() const
244 void InsetText::draw(PainterInfo & pi, int x, int y) const
246 // update our idea of where we are. Clearly, we should
247 // not have to know this information.
250 int const start_x = x;
252 BufferView * bv = pi.base.bv;
253 Painter & pain = pi.pain;
255 // repaint the background if needed
256 if (backgroundColor() != LColor::background)
257 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
259 // no draw is necessary !!!
260 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
271 top_y = y - dim_.asc;
273 if (the_locking_inset
274 && text_.cursor.par() == inset_par && cpos() == inset_pos) {
279 x += TEXT_TO_INSET_OFFSET;
281 paintTextInset(*bv, text_, x, y);
283 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
284 drawFrame(pain, start_x);
288 void InsetText::drawFrame(Painter & pain, int x) const
290 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
291 int const frame_x = x + ttoD2;
292 int const frame_y = top_baseline - dim_.asc + ttoD2;
293 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
294 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
295 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
299 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
304 if (!autoBreakRows_ && paragraphs.size() > 1)
305 collapseParagraphs(bv);
307 if (!text_.selection.set())
308 text_.selection.cursor = text_.cursor;
311 bv->updateInset(this);
312 bv->owner()->view_state_changed();
313 bv->owner()->updateMenubar();
314 bv->owner()->updateToolbar();
315 if (old_par != text_.cursor.par()) {
316 bv->owner()->setLayout(cpar()->layout()->name());
317 old_par = text_.cursor.par();
322 string const InsetText::editMessage() const
324 return _("Opened Text Inset");
328 void InsetText::insetUnlock(BufferView * bv)
330 if (the_locking_inset) {
331 the_locking_inset->insetUnlock(bv);
332 the_locking_inset = 0;
333 updateLocal(bv, false);
338 if (text_.selection.set())
339 text_.clearSelection();
341 bv->owner()->setLayout(owner()->getLyXText(bv)
342 ->cursorPar()->layout()->name());
344 bv->owner()->setLayout(bv->text->cursorPar()->layout()->name());
345 // hack for deleteEmptyParMech
346 if (!paragraphs.begin()->empty())
347 text_.setCursor(0, 0);
348 else if (paragraphs.size() > 1)
349 text_.setCursor(1, 0);
353 void InsetText::lockInset(BufferView * bv)
356 the_locking_inset = 0;
357 inset_pos = inset_x = inset_y = 0;
358 inset_boundary = false;
361 text_.setCursorIntern(0, 0);
362 text_.clearSelection();
364 // If the inset is empty set the language of the current font to the
365 // language to the surronding text (if different).
366 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
367 bv->getParentLanguage(this) != text_.current_font.language()) {
368 LyXFont font(LyXFont::ALL_IGNORE);
369 font.setLanguage(bv->getParentLanguage(this));
370 setFont(bv, font, false);
375 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
377 the_locking_inset = inset;
378 inset_x = cx() - top_x;
381 inset_par = text_.cursor.par();
382 inset_boundary = cboundary();
386 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
388 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
389 << inset << "): " << endl;
392 if (!the_locking_inset) {
393 ParagraphList::iterator pit = paragraphs.begin();
394 ParagraphList::iterator pend = paragraphs.end();
396 for (; pit != pend; ++pit) {
397 InsetList::iterator it = pit->insetlist.begin();
398 InsetList::iterator const end = pit->insetlist.end();
399 for (; it != end; ++it) {
400 if (it->inset == inset) {
401 lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
402 text_.setCursorIntern(
403 std::distance(paragraphs.begin(), pit), it->pos);
404 lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
405 lyxerr << "bv: " << bv << " inset: " << inset << endl;
406 lockInset(bv, inset);
407 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
412 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
415 if (inset == cpar()->getInset(cpos())) {
416 lyxerr[Debug::INSETS] << "OK" << endl;
417 lockInset(bv, inset);
421 if (the_locking_inset && the_locking_inset == inset) {
422 if (text_.cursor.par() == inset_par && cpos() == inset_pos) {
423 lyxerr[Debug::INSETS] << "OK" << endl;
424 inset_x = cx() - top_x;
427 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
429 } else if (the_locking_inset) {
430 lyxerr[Debug::INSETS] << "MAYBE" << endl;
431 return the_locking_inset->lockInsetInInset(bv, inset);
433 lyxerr[Debug::INSETS] << "NOT OK" << endl;
438 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
441 if (!the_locking_inset)
443 if (the_locking_inset == inset) {
444 the_locking_inset->insetUnlock(bv);
445 the_locking_inset = 0;
447 moveRightIntern(bv, true, false);
448 old_par = -1; // force layout setting
452 updateLocal(bv, false);
455 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
459 void InsetText::lfunMousePress(FuncRequest const & cmd)
463 // use this to check mouse motion for selection!
467 BufferView * bv = cmd.view();
468 FuncRequest cmd1 = cmd;
475 int tmp_y = cmd.y + dim_.asc - bv->top_y();
476 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
478 if (the_locking_inset) {
479 if (the_locking_inset == inset) {
480 the_locking_inset->dispatch(cmd1);
483 // otherwise only unlock the_locking_inset
484 the_locking_inset->insetUnlock(bv);
485 the_locking_inset = 0;
488 no_selection = false;
490 if (bv->theLockingInset()) {
491 if (isHighlyEditableInset(inset)) {
492 // We just have to lock the inset before calling a
494 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
495 if (!bv->lockInset(uinset)) {
496 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
498 inset->dispatch(cmd1);
499 if (the_locking_inset)
500 updateLocal(bv, false);
505 bool paste_internally = false;
506 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
507 dispatch(FuncRequest(bv, LFUN_COPY));
508 paste_internally = true;
510 int old_top_y = bv->top_y();
512 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
513 // set the selection cursor!
514 text_.selection.cursor = text_.cursor;
515 text_.cursor.x_fix(text_.cursor.x());
517 text_.clearSelection();
518 updateLocal(bv, false);
520 bv->owner()->setLayout(cpar()->layout()->name());
522 // we moved the view we cannot do mouse selection in this case!
523 if (bv->top_y() != old_top_y)
525 old_par = text_.cursor.par();
526 // Insert primary selection with middle mouse
527 // if there is a local selection in the current buffer,
529 if (cmd.button() == mouse_button::button2) {
530 if (paste_internally)
531 dispatch(FuncRequest(bv, LFUN_PASTE));
533 dispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
536 getLyXText(bv)->clearSelection();
541 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
543 BufferView * bv = cmd.view();
544 FuncRequest cmd1 = cmd;
549 if (the_locking_inset) {
550 DispatchResult const res = the_locking_inset->dispatch(cmd1);
552 return res.dispatched();
556 int tmp_y = cmd.y + dim_.asc - bv->top_y();
557 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
561 // We still need to deal properly with the whole relative vs.
562 // absolute mouse co-ords thing in a realiable, sensible way
563 DispatchResult const res = inset->dispatch(cmd1);
564 bool const ret = res.dispatched();
565 updateLocal(bv, false);
570 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
572 FuncRequest cmd1 = cmd;
576 if (the_locking_inset) {
577 the_locking_inset->dispatch(cmd1);
581 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
584 BufferView * bv = cmd.view();
585 LyXCursor cur = text_.cursor;
586 text_.setCursorFromCoordinates (cmd.x, cmd.y + dim_.asc);
587 text_.cursor.x_fix(text_.cursor.x());
588 if (cur == text_.cursor)
590 text_.setSelection();
591 updateLocal(bv, false);
596 InsetText::priv_dispatch(FuncRequest const & cmd,
597 idx_type & idx, pos_type & pos)
599 BufferView * bv = cmd.view();
602 switch (cmd.action) {
603 case LFUN_INSET_EDIT: {
604 UpdatableInset::priv_dispatch(cmd, idx, pos);
606 if (!bv->lockInset(this)) {
607 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
608 return DispatchResult(true, true);
612 the_locking_inset = 0;
616 inset_boundary = false;
621 if (cmd.argument.size()) {
622 if (cmd.argument == "left")
623 text_.setCursorIntern(0, 0);
625 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
627 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
628 // we put here -1 and not button as now the button in the
629 // edit call should not be needed we will fix this in 1.3.x
630 // cycle hopefully (Jug 20020509)
631 // FIXME: GUII I've changed this to none: probably WRONG
632 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
633 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
634 text_.cursor.x(text_.cursor.x());
635 text_.cursor.x_fix(text_.cursor.x());
639 text_.clearSelection();
642 // If the inset is empty set the language of the current font to the
643 // language to the surronding text (if different).
644 if (paragraphs.begin()->empty() &&
645 paragraphs.size() == 1 &&
646 bv->getParentLanguage(this) != text_.current_font.language())
648 LyXFont font(LyXFont::ALL_IGNORE);
649 font.setLanguage(bv->getParentLanguage(this));
650 setFont(bv, font, false);
653 updateLocal(bv, false);
654 // Tell the paragraph dialog that we've entered an insettext.
655 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
656 return DispatchResult(true, true);
659 case LFUN_MOUSE_PRESS:
661 return DispatchResult(true, true);
663 case LFUN_MOUSE_MOTION:
664 lfunMouseMotion(cmd);
665 return DispatchResult(true, true);
667 case LFUN_MOUSE_RELEASE:
668 return DispatchResult(lfunMouseRelease(cmd));
674 bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
675 no_selection = false;
677 DispatchResult result = UpdatableInset::priv_dispatch(cmd, idx, pos);
678 if (result.dispatched())
679 return DispatchResult(true, true);
681 result = DispatchResult(true, true);
682 if (cmd.action == LFUN_UNKNOWN_ACTION && cmd.argument.empty())
683 return DispatchResult(false, FINISHED);
685 if (the_locking_inset) {
686 result = the_locking_inset->dispatch(cmd);
688 if (result.dispatched()) {
689 if (result.update()) {
690 result.update(false);
691 updateLocal(bv, false);
696 switch (result.val()) {
698 moveRightIntern(bv, false, false);
699 result = DispatchResult(true, true);
703 if (result.val() >= FINISHED) {
704 updateLocal(bv, false);
705 bv->unlockInset(this);
709 result = moveDown(bv);
710 if (result.val() >= FINISHED) {
711 updateLocal(bv, false);
712 bv->unlockInset(this);
716 result = DispatchResult(true, true);
719 the_locking_inset = 0;
720 updateLocal(bv, false);
721 // make sure status gets reset immediately
722 bv->owner()->clearMessage();
725 bool updflag = false;
727 switch (cmd.action) {
730 case LFUN_SELFINSERT:
731 if (bv->buffer()->isReadonly()) {
732 // setErrorMessage(N_("Document is read only"));
735 if (!cmd.argument.empty()) {
736 /* Automatically delete the currently selected
737 * text and replace it with what is being
738 * typed in now. Depends on lyxrc settings
739 * "auto_region_delete", which defaults to
742 // This should not be needed here and is also WRONG!
743 recordUndo(bv, Undo::INSERT, text_.cursorPar());
747 if (lyxrc.auto_region_delete && text_.selection.set())
748 text_.cutSelection(false, false);
749 text_.clearSelection();
751 for (string::size_type i = 0; i < cmd.argument.length(); ++i)
752 bv->owner()->getIntl().getTransManager().
753 TranslateAndInsert(cmd.argument[i], &text_);
755 text_.selection.cursor = text_.cursor;
757 result = DispatchResult(true);
760 // cursor movements that need special handling
763 result = moveRight(bv);
768 result = moveLeft(bv);
772 result = moveDown(bv);
780 if (crow() == text_.firstRow())
781 result = DispatchResult(false, FINISHED_UP);
783 text_.cursorPrevious();
784 text_.clearSelection();
785 result = DispatchResult(true);
790 if (crow() == text_.lastRow())
791 result = DispatchResult(false, FINISHED_DOWN);
794 text_.clearSelection();
795 result = DispatchResult(true);
800 if (text_.selection.set())
801 text_.cutSelection(true, false);
808 if (text_.selection.set())
809 text_.cutSelection(true, false);
816 if (!autoBreakRows_) {
817 if (CutAndPaste::nrOfParagraphs() > 1) {
819 #warning FIXME horrendously bad UI
821 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
824 replaceSelection(bv->getLyXText());
825 size_t sel_index = 0;
826 string const & arg = cmd.argument;
827 if (isStrUnsignedInt(arg)) {
828 #warning FIXME Check if the arg is in the domain of available selections.
829 sel_index = strToUnsignedInt(arg);
831 text_.pasteSelection(sel_index);
833 text_.clearSelection();
838 case LFUN_BREAKPARAGRAPH:
839 if (!autoBreakRows_) {
840 result = DispatchResult(true, true);
842 replaceSelection(bv->getLyXText());
843 text_.breakParagraph(paragraphs, 0);
848 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
849 if (!autoBreakRows_) {
850 result = DispatchResult(true, true);
852 replaceSelection(bv->getLyXText());
853 text_.breakParagraph(paragraphs, 1);
858 case LFUN_BREAKLINE: {
859 if (!autoBreakRows_) {
860 result = DispatchResult(true, true);
862 replaceSelection(bv->getLyXText());
863 auto_ptr<InsetNewline> ins(new InsetNewline);
864 text_.insertInset(ins.release());
871 // do not set layouts on non breakable textinsets
872 if (autoBreakRows_) {
873 string cur_layout = cpar()->layout()->name();
875 // Derive layout number from given argument (string)
876 // and current buffer's textclass (number).
877 LyXTextClass const & tclass =
878 bv->buffer()->params().getLyXTextClass();
879 string layout = cmd.argument;
880 bool hasLayout = tclass.hasLayout(layout);
882 // If the entry is obsolete, use the new one instead.
884 string const & obs = tclass[layout]->obsoleted_by();
889 // see if we found the layout number:
891 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
892 bv->owner()->dispatch(lf);
896 if (cur_layout != layout) {
898 text_.setLayout(layout);
899 bv->owner()->setLayout(cpar()->layout()->name());
903 // reset the layout box
904 bv->owner()->setLayout(cpar()->layout()->name());
909 if (!bv->dispatch(cmd))
910 result = DispatchResult(false);
914 updateLocal(bv, updflag);
916 /// If the action has deleted all text in the inset, we need to change the
917 // language to the language of the surronding text.
918 if (!was_empty && paragraphs.begin()->empty() &&
919 paragraphs.size() == 1) {
920 LyXFont font(LyXFont::ALL_IGNORE);
921 font.setLanguage(bv->getParentLanguage(this));
922 setFont(bv, font, false);
925 if (result.val() >= FINISHED)
926 bv->unlockInset(this);
928 if (result.val() == NONE)
929 result = DispatchResult(true, true);
934 int InsetText::latex(Buffer const & buf, ostream & os,
935 LatexRunParams const & runparams) const
938 latexParagraphs(buf, paragraphs, os, texrow, runparams);
939 return texrow.rows();
943 int InsetText::ascii(Buffer const & buf, ostream & os,
944 LatexRunParams const & runparams) const
946 ParagraphList::const_iterator beg = paragraphs.begin();
947 ParagraphList::const_iterator end = paragraphs.end();
948 ParagraphList::const_iterator it = beg;
949 for (; it != end; ++it)
950 asciiParagraph(buf, *it, os, runparams, it == beg);
952 //FIXME: Give the total numbers of lines
957 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
958 LatexRunParams const & runparams) const
960 linuxdocParagraphs(buf, paragraphs, os, runparams);
965 int InsetText::docbook(Buffer const & buf, ostream & os,
966 LatexRunParams const & runparams) const
968 docbookParagraphs(buf, paragraphs, os, runparams);
973 void InsetText::validate(LaTeXFeatures & features) const
975 for_each(paragraphs.begin(), paragraphs.end(),
976 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
980 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
982 if (the_locking_inset) {
983 the_locking_inset->getCursor(bv, x, y);
987 y = cy() + InsetText::y();
991 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
993 if (the_locking_inset) {
994 the_locking_inset->getCursorPos(bv, x, y);
997 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
998 y = cy() - TEXT_TO_INSET_OFFSET;
1002 int InsetText::insetInInsetY() const
1004 if (!the_locking_inset)
1007 return inset_y + the_locking_inset->insetInInsetY();
1011 void InsetText::fitInsetCursor(BufferView * bv) const
1013 if (the_locking_inset) {
1014 the_locking_inset->fitInsetCursor(bv);
1018 LyXFont const font = text_.getFont(cpar(), cpos());
1020 int const asc = font_metrics::maxAscent(font);
1021 int const desc = font_metrics::maxDescent(font);
1023 bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1027 DispatchResult InsetText::moveRight(BufferView * bv)
1029 if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1030 return moveLeftIntern(bv, false, true, false);
1032 return moveRightIntern(bv, true, true, false);
1036 DispatchResult InsetText::moveLeft(BufferView * bv)
1038 if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1039 return moveRightIntern(bv, true, true, false);
1041 return moveLeftIntern(bv, false, true, false);
1046 InsetText::moveRightIntern(BufferView * bv, bool front,
1047 bool activate_inset, bool selecting)
1049 ParagraphList::iterator c_par = cpar();
1051 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1052 return DispatchResult(false, FINISHED_RIGHT);
1053 if (activate_inset && checkAndActivateInset(bv, front))
1054 return DispatchResult(true, true);
1055 text_.cursorRight(bv);
1057 text_.clearSelection();
1058 return DispatchResult(true);
1063 InsetText::moveLeftIntern(BufferView * bv, bool front,
1064 bool activate_inset, bool selecting)
1066 if (cpar() == paragraphs.begin() && cpos() <= 0)
1067 return DispatchResult(false, FINISHED);
1068 text_.cursorLeft(bv);
1070 text_.clearSelection();
1071 if (activate_inset && checkAndActivateInset(bv, front))
1072 return DispatchResult(true, true);
1073 return DispatchResult(true);
1077 DispatchResult InsetText::moveUp(BufferView * bv)
1079 if (crow() == text_.firstRow())
1080 return DispatchResult(false, FINISHED_UP);
1082 text_.clearSelection();
1083 return DispatchResult(true);
1087 DispatchResult InsetText::moveDown(BufferView * bv)
1089 if (crow() == text_.lastRow())
1090 return DispatchResult(false, FINISHED_DOWN);
1091 text_.cursorDown(bv);
1092 text_.clearSelection();
1093 return DispatchResult(true);
1097 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1099 if (the_locking_inset) {
1100 if (the_locking_inset->insetAllowed(inset))
1101 return the_locking_inset->insertInset(bv, inset);
1104 inset->setOwner(this);
1105 text_.insertInset(inset);
1107 updateLocal(bv, true);
1112 bool InsetText::insetAllowed(InsetOld::Code code) const
1114 // in_insetAllowed is a really gross hack,
1115 // to allow us to call the owner's insetAllowed
1116 // without stack overflow, which can happen
1117 // when the owner uses InsetCollapsable::insetAllowed()
1119 if (in_insetAllowed)
1121 in_insetAllowed = true;
1122 if (the_locking_inset)
1123 ret = the_locking_inset->insetAllowed(code);
1125 ret = owner()->insetAllowed(code);
1126 in_insetAllowed = false;
1131 UpdatableInset * InsetText::getLockingInset() const
1133 return the_locking_inset ? the_locking_inset->getLockingInset() :
1134 const_cast<InsetText *>(this);
1138 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1142 if (the_locking_inset)
1143 return the_locking_inset->getFirstLockingInsetOfType(c);
1148 bool InsetText::showInsetDialog(BufferView * bv) const
1150 if (the_locking_inset)
1151 return the_locking_inset->showInsetDialog(bv);
1156 void InsetText::getLabelList(Buffer const & buffer,
1157 std::vector<string> & list) const
1159 ParagraphList::const_iterator pit = paragraphs.begin();
1160 ParagraphList::const_iterator pend = paragraphs.end();
1161 for (; pit != pend; ++pit) {
1162 InsetList::const_iterator beg = pit->insetlist.begin();
1163 InsetList::const_iterator end = pit->insetlist.end();
1164 for (; beg != end; ++beg)
1165 beg->inset->getLabelList(buffer, list);
1170 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1173 if (the_locking_inset) {
1174 the_locking_inset->setFont(bv, font, toggleall, selectall);
1178 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1179 || cpar()->empty()) {
1180 text_.setFont(font, toggleall);
1185 if (text_.selection.set())
1186 text_.recUndo(text_.cursor.par());
1190 text_.selection.cursor = text_.cursor;
1191 text_.cursorBottom();
1192 text_.setSelection();
1195 text_.toggleFree(font, toggleall);
1198 text_.clearSelection();
1201 updateLocal(bv, true);
1205 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1207 if (cpos() == cpar()->size())
1209 InsetOld * inset = cpar()->getInset(cpos());
1210 if (!isHighlyEditableInset(inset))
1212 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1213 inset->dispatch(cmd);
1214 if (!the_locking_inset)
1216 updateLocal(bv, false);
1221 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1222 mouse_button::state button)
1225 int dummyy = y + dim_.asc;
1226 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1227 // we only do the edit() call if the inset was hit by the mouse
1228 // or if it is a highly editable inset. So we should call this
1229 // function from our own edit with button < 0.
1230 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1232 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1241 inset_x = cx() - top_x;
1243 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1244 inset->dispatch(cmd);
1245 if (!the_locking_inset)
1247 updateLocal(bv, false);
1252 void InsetText::markNew(bool track_changes)
1254 ParagraphList::iterator pit = paragraphs.begin();
1255 ParagraphList::iterator end = paragraphs.end();
1256 for (; pit != end; ++pit) {
1257 if (track_changes) {
1258 pit->trackChanges();
1260 // no-op when not tracking
1261 pit->cleanChanges();
1267 void InsetText::setText(string const & data, LyXFont const & font)
1270 for (unsigned int i = 0; i < data.length(); ++i)
1271 paragraphs.begin()->insertChar(i, data[i], font);
1275 void InsetText::setAutoBreakRows(bool flag)
1277 if (flag != autoBreakRows_) {
1278 autoBreakRows_ = flag;
1285 void InsetText::setDrawFrame(DrawFrame how)
1291 LColor_color InsetText::frameColor() const
1293 return LColor::color(frame_color_);
1297 void InsetText::setFrameColor(LColor_color col)
1303 int InsetText::cx() const
1305 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1306 if (the_locking_inset) {
1307 LyXFont font = text_.getFont(text_.cursorPar(), text_.cursor.pos());
1308 if (font.isVisibleRightToLeft())
1309 x -= the_locking_inset->width();
1315 int InsetText::cy() const
1317 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1321 pos_type InsetText::cpos() const
1323 return text_.cursor.pos();
1327 ParagraphList::iterator InsetText::cpar() const
1329 return text_.cursorPar();
1333 bool InsetText::cboundary() const
1335 return text_.cursor.boundary();
1339 RowList::iterator InsetText::crow() const
1341 return cpar()->getRow(cpos());
1345 LyXText * InsetText::getLyXText(BufferView const * bv,
1346 bool const recursive) const
1349 if (recursive && the_locking_inset)
1350 return the_locking_inset->getLyXText(bv, true);
1355 void InsetText::setViewCache(BufferView const * bv) const
1358 if (bv != text_.bv_owner) {
1359 //lyxerr << "setting view cache from "
1360 // << text_.bv_owner << " to " << bv << "\n";
1361 text_.init(const_cast<BufferView *>(bv));
1363 text_.bv_owner = const_cast<BufferView *>(bv);
1368 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1371 /// then remove all LyXText in text-insets
1372 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1373 const_cast<ParagraphList&>(paragraphs).end(),
1374 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1379 void InsetText::removeNewlines()
1381 ParagraphList::iterator it = paragraphs.begin();
1382 ParagraphList::iterator end = paragraphs.end();
1383 for (; it != end; ++it)
1384 for (int i = 0; i < it->size(); ++i)
1385 if (it->isNewline(i))
1390 int InsetText::scroll(bool recursive) const
1392 int sx = UpdatableInset::scroll(false);
1394 if (recursive && the_locking_inset)
1395 sx += the_locking_inset->scroll(recursive);
1401 void InsetText::clearSelection(BufferView *)
1403 text_.clearSelection();
1407 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1409 Painter & pain = bv->painter();
1411 int h = dim_.asc + dim_.des;
1412 int ty = baseline - dim_.asc;
1418 if (ty + h > pain.paperHeight())
1419 h = pain.paperHeight();
1420 if (top_x + w > pain.paperWidth())
1421 w = pain.paperWidth();
1422 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1426 ParagraphList * InsetText::getParagraphs(int i) const
1428 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
1432 LyXText * InsetText::getText(int i) const
1434 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
1438 LyXCursor const & InsetText::cursor(BufferView * bv) const
1440 if (the_locking_inset)
1441 return the_locking_inset->cursor(bv);
1442 return getLyXText(bv)->cursor;
1447 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
1450 if (the_locking_inset) {
1451 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
1452 if (!word.word().empty()) {
1456 // we have to go on checking so move cursor to the next char
1457 text_.cursor.pos(text_.cursor.pos() + 1);
1459 word = text_.selectNextWordToSpellcheck(value);
1460 if (word.word().empty())
1461 bv->unlockInset(const_cast<InsetText *>(this));
1468 void InsetText::selectSelectedWord(BufferView * bv)
1470 if (the_locking_inset) {
1471 the_locking_inset->selectSelectedWord(bv);
1474 getLyXText(bv)->selectSelectedWord();
1475 updateLocal(bv, false);
1479 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
1481 if (the_locking_inset) {
1482 if (the_locking_inset->nextChange(bv, length))
1484 text_.cursorRight(true);
1486 lyx::find::SearchResult result =
1487 lyx::find::findNextChange(bv, &text_, length);
1489 if (result == lyx::find::SR_FOUND) {
1490 LyXCursor cur = text_.cursor;
1491 bv->unlockInset(bv->theLockingInset());
1492 if (bv->lockInset(this))
1495 text_.setSelectionRange(length);
1496 updateLocal(bv, false);
1498 return result != lyx::find::SR_NOT_FOUND;
1502 bool InsetText::searchForward(BufferView * bv, string const & str,
1505 if (the_locking_inset) {
1506 if (the_locking_inset->searchForward(bv, str, cs, mw))
1508 text_.cursorRight(true);
1510 lyx::find::SearchResult result =
1511 lyx::find::find(bv, &text_, str, true, cs, mw);
1513 if (result == lyx::find::SR_FOUND) {
1514 LyXCursor cur = text_.cursor;
1515 bv->unlockInset(bv->theLockingInset());
1516 if (bv->lockInset(this))
1519 text_.setSelectionRange(str.length());
1520 updateLocal(bv, false);
1522 return result != lyx::find::SR_NOT_FOUND;
1526 bool InsetText::searchBackward(BufferView * bv, string const & str,
1529 if (the_locking_inset) {
1530 if (the_locking_inset->searchBackward(bv, str, cs, mw))
1534 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
1536 lyx::find::SearchResult result =
1537 lyx::find::find(bv, &text_, str, false, cs, mw);
1539 if (result == lyx::find::SR_FOUND) {
1540 LyXCursor cur = text_.cursor;
1541 bv->unlockInset(bv->theLockingInset());
1542 if (bv->lockInset(this))
1545 text_.setSelectionRange(str.length());
1546 updateLocal(bv, false);
1548 return result != lyx::find::SR_NOT_FOUND;
1552 bool InsetText::checkInsertChar(LyXFont & font)
1554 return owner() ? owner()->checkInsertChar(font) : true;
1558 void InsetText::collapseParagraphs(BufferView * bv)
1560 while (paragraphs.size() > 1) {
1561 ParagraphList::iterator const first = paragraphs.begin();
1562 ParagraphList::iterator second = first;
1564 size_t const first_par_size = first->size();
1566 if (!first->empty() &&
1568 !first->isSeparator(first_par_size - 1)) {
1569 first->insertChar(first_par_size, ' ');
1572 #warning probably broken
1573 if (text_.selection.set()) {
1574 if (text_.selection.start.par() == 1) {
1575 text_.selection.start.par(1);
1576 text_.selection.start.pos(text_.selection.start.pos() + first_par_size);
1578 if (text_.selection.end.par() == 2) {
1579 text_.selection.end.par(1);
1580 text_.selection.end.pos(text_.selection.end.pos() + first_par_size);
1584 mergeParagraph(bv->buffer()->params(), paragraphs, first);
1589 void InsetText::getDrawFont(LyXFont & font) const
1593 owner()->getDrawFont(font);
1597 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1599 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1600 // And it probably does. You have to take a look at this John. (Lgb)
1601 #warning John, have a look here. (Lgb)
1602 ParagraphList::iterator pit = plist.begin();
1603 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1605 mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
1607 ParagraphList::iterator pend = plist.end();
1608 for (; pit != pend; ++pit)
1609 paragraphs.push_back(*pit);
1613 void InsetText::addPreview(PreviewLoader & loader) const
1615 ParagraphList::const_iterator pit = paragraphs.begin();
1616 ParagraphList::const_iterator pend = paragraphs.end();
1618 for (; pit != pend; ++pit) {
1619 InsetList::const_iterator it = pit->insetlist.begin();
1620 InsetList::const_iterator end = pit->insetlist.end();
1621 for (; it != end; ++it)
1622 it->inset->addPreview(loader);