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"
16 #include "BufferView.h"
17 #include "CutAndPaste.h"
19 #include "dimension.h"
20 #include "funcrequest.h"
23 #include "LaTeXFeatures.h"
27 #include "lyxcursor.h"
33 #include "paragraph.h"
34 #include "ParagraphParameters.h"
35 #include "trans_mgr.h"
36 #include "undo_funcs.h"
37 #include "WordLangTuple.h"
38 #include "paragraph_funcs.h"
40 #include "rowpainter.h"
41 #include "insetnewline.h"
44 #include "frontends/Alert.h"
45 #include "frontends/Dialogs.h"
46 #include "frontends/font_metrics.h"
47 #include "frontends/LyXView.h"
48 #include "frontends/Painter.h"
49 #include "frontends/screen.h"
51 #include "support/textutils.h"
52 #include "support/LAssert.h"
53 #include "support/lstrings.h"
54 #include "support/lyxalgo.h" // lyx::count
56 #include <boost/bind.hpp>
74 using lyx::textclass_type;
77 // These functions should probably go into bufferview_funcs somehow (Jug)
79 void InsetText::saveLyXTextState(LyXText * t) const
81 // check if my paragraphs are still valid
82 ParagraphList::iterator it = paragraphs.begin();
83 ParagraphList::iterator end = paragraphs.end();
84 for (; it != end; ++it) {
85 if (&*it == t->cursor.par())
89 if (it != end && t->cursor.pos() <= it->size()) {
90 sstate.lpar = &*t->cursor.par();
91 sstate.pos = t->cursor.pos();
92 sstate.boundary = t->cursor.boundary();
93 sstate.selstartpar = &*t->selection.start.par();
94 sstate.selstartpos = t->selection.start.pos();
95 sstate.selstartboundary = t->selection.start.boundary();
96 sstate.selendpar = &*t->selection.end.par();
97 sstate.selendpos = t->selection.end.pos();
98 sstate.selendboundary = t->selection.end.boundary();
99 sstate.selection = t->selection.set();
100 sstate.mark_set = t->selection.mark();
107 void InsetText::restoreLyXTextState(LyXText * t) const
112 t->selection.set(true);
113 /* at this point just to avoid the DEPM when setting the cursor */
114 t->selection.mark(sstate.mark_set);
115 if (sstate.selection) {
116 t->setCursor(sstate.selstartpar, sstate.selstartpos,
117 true, sstate.selstartboundary);
118 t->selection.cursor = t->cursor;
119 t->setCursor(sstate.selendpar, sstate.selendpos,
120 true, sstate.selendboundary);
122 t->setCursor(sstate.lpar, sstate.pos);
124 t->setCursor(sstate.lpar, sstate.pos, true, sstate.boundary);
125 t->selection.cursor = t->cursor;
126 t->selection.set(false);
131 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
138 InsetText::InsetText(BufferParams const & bp)
139 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
142 paragraphs.push_back(Paragraph());
143 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
144 if (bp.tracking_changes)
145 paragraphs.begin()->trackChanges();
150 InsetText::InsetText(InsetText const & in, bool same_id)
151 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
158 InsetText & InsetText::operator=(InsetText const & it)
165 void InsetText::init(InsetText const * ins, bool same_id)
168 setParagraphData(ins->paragraphs, same_id);
169 autoBreakRows = ins->autoBreakRows;
170 drawFrame_ = ins->drawFrame_;
171 frame_color = ins->frame_color;
175 for_each(paragraphs.begin(), paragraphs.end(),
176 boost::bind(&Paragraph::setInsetOwner, _1, this));
178 the_locking_inset = 0;
180 frame_color = LColor::insetframe;
181 autoBreakRows = false;
191 last_drawn_width = -1;
194 in_insetAllowed = false;
198 InsetText::~InsetText()
204 void InsetText::clear(bool just_mark_erased)
206 if (just_mark_erased) {
207 ParagraphList::iterator it = paragraphs.begin();
208 ParagraphList::iterator end = paragraphs.end();
209 for (; it != end; ++it) {
216 // This is a gross hack...
217 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
220 paragraphs.push_back(Paragraph());
221 paragraphs.begin()->setInsetOwner(this);
222 paragraphs.begin()->layout(old_layout);
229 Inset * InsetText::clone(Buffer const &, bool same_id) const
231 return new InsetText(*this, same_id);
235 void InsetText::write(Buffer const * buf, ostream & os) const
238 writeParagraphData(buf, os);
242 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
244 ParagraphList::iterator it = paragraphs.begin();
245 ParagraphList::iterator end = paragraphs.end();
246 Paragraph::depth_type dth = 0;
247 for (; it != end; ++it) {
248 it->write(buf, os, buf->params, dth);
253 void InsetText::read(Buffer const * buf, LyXLex & lex)
256 Paragraph::depth_type depth = 0;
260 if (buf->params.tracking_changes)
261 paragraphs.begin()->trackChanges();
263 // delete the initial paragraph
265 ParagraphList::iterator pit = paragraphs.begin();
269 token = lex.getString();
272 if (token == "\\end_inset") {
276 if (token == "\\the_end") {
277 lex.printError("\\the_end read in inset! Error in document!");
282 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
285 pit = paragraphs.begin();
286 ParagraphList::iterator const end = paragraphs.end();
287 for (; pit != end; ++pit)
288 pit->setInsetOwner(this);
290 if (token != "\\end_inset") {
291 lex.printError("Missing \\end_inset at this point. "
298 void InsetText::dimension(BufferView * bv, LyXFont const &,
299 Dimension & dim) const
301 LyXText * text = getLyXText(bv);
302 dim.a = text->rows().begin()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
303 dim.d = text->height - dim.a + TEXT_TO_INSET_OFFSET;
304 dim.w = max(textWidth(bv), int(text->width)) + 2 * TEXT_TO_INSET_OFFSET;
305 dim.w = max(dim.w, 10);
311 int InsetText::textWidth(BufferView * bv, bool fordraw) const
313 int w = autoBreakRows ? getMaxWidth(bv, this) : -1;
316 return max(w - 2 * TEXT_TO_INSET_OFFSET,
317 (int)getLyXText(bv)->width);
322 return w - 2 * TEXT_TO_INSET_OFFSET;
326 void InsetText::draw(BufferView * bv, LyXFont const & f,
327 int baseline, float & x) const
332 // update our idea of where we are. Clearly, we should
333 // not have to know this information.
337 int const start_x = int(x);
339 Painter & pain = bv->painter();
341 // call this method so that dim_ has the right value
342 dimension(bv, f, dim_);
344 // repaint the background if needed
345 if (backgroundColor() != LColor::background)
346 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
348 // no draw is necessary !!!
349 if ((drawFrame_ == LOCKED) && !locked && paragraphs.begin()->empty()) {
350 top_baseline = baseline;
357 x += static_cast<float>(scroll());
359 top_baseline = baseline;
360 top_y = baseline - dim_.a;
362 if (last_drawn_width != dim_.w) {
364 last_drawn_width = dim_.w;
367 if (the_locking_inset && (cpar(bv) == inset_par)
368 && (cpos(bv) == inset_pos)) {
369 inset_x = cix(bv) - int(x) + drawTextXOffset;
370 inset_y = ciy(bv) + drawTextYOffset;
378 x += TEXT_TO_INSET_OFFSET;
380 RowList::iterator rowit = lt->rows().begin();
381 RowList::iterator end = lt->rows().end();
383 int y_offset = baseline - rowit->ascent_of_text();
384 int ph = pain.paperHeight();
387 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
388 y += rowit->height();
389 first += rowit->height();
393 lt->top_y(-y_offset);
401 int yf = y_offset + first;
406 while ((rowit != end) && (yf < ph)) {
407 RowPainter rp(*bv, *lt, rowit);
408 rp.paint(y + y_offset + first, int(x), y + lt->top_y());
409 y += rowit->height();
410 yf += rowit->height();
416 if ((drawFrame_ == ALWAYS) || (drawFrame_ == LOCKED && locked)) {
417 drawFrame(pain, int(start_x));
420 x += dim_.w - TEXT_TO_INSET_OFFSET;
422 if (need_update != INIT) {
430 void InsetText::drawFrame(Painter & pain, int x) const
432 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
434 frame_y = top_baseline - dim_.a + ttoD2;
435 frame_w = dim_.w - TEXT_TO_INSET_OFFSET;
436 frame_h = dim_.a + dim_.d - TEXT_TO_INSET_OFFSET;
437 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
442 void InsetText::update(BufferView * bv, bool reinit)
445 if (reinit && owner()) {
447 owner()->update(bv, true);
453 if (reinit || need_update == INIT) {
455 // we should put this call where we set need_update to INIT!
458 owner()->update(bv, true);
461 int nw = getMaxWidth(bv, this);
462 if (nw > 0 && old_max_width != nw) {
469 if (!autoBreakRows &&
470 boost::next(paragraphs.begin()) != paragraphs.end())
471 collapseParagraphs(bv);
473 if (the_locking_inset) {
474 inset_x = cix(bv) - top_x + drawTextXOffset;
475 inset_y = ciy(bv) + drawTextYOffset;
476 the_locking_inset->update(bv, reinit);
484 if ((need_update & CURSOR_PAR) && (lt->refreshStatus() == LyXText::REFRESH_NONE) &&
486 lt->updateInset(the_locking_inset);
489 if (lt->refreshStatus() == LyXText::REFRESH_AREA)
496 int nw = getMaxWidth(bv, this);
497 if (nw > 0 && old_max_width != nw) {
504 void InsetText::setUpdateStatus(BufferView * bv, int what) const
506 // this does nothing dangerous so use only a localized buffer
507 LyXText * llt = getLyXText(bv);
510 // we have to redraw us full if our LyXText REFRESH_AREA or
511 // if we don't break row so that we only have one row to update!
512 if ((llt->refreshStatus() == LyXText::REFRESH_AREA) ||
514 (llt->refreshStatus() == LyXText::REFRESH_ROW)))
517 } else if (llt->refreshStatus() == LyXText::REFRESH_ROW) {
518 need_update |= CURSOR_PAR;
521 // this to not draw a selection when we redraw all of it!
522 if (need_update & CURSOR && !(need_update & SELECTION)) {
523 if (llt->selection.set())
525 llt->clearSelection();
530 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
533 if (!autoBreakRows &&
534 boost::next(paragraphs.begin()) != paragraphs.end())
535 collapseParagraphs(bv);
537 if (!autoBreakRows && paragraphs.size() > 1)
538 collapseParagraphs(bv);
547 setUpdateStatus(bv, what);
548 bool flag = mark_dirty ||
549 (((need_update != CURSOR) && (need_update != NONE)) ||
550 (lt->refreshStatus() != LyXText::REFRESH_NONE) || lt->selection.set());
551 if (!lt->selection.set())
552 lt->selection.cursor = lt->cursor;
558 bv->updateInset(const_cast<InsetText *>(this));
564 if (need_update == CURSOR)
566 bv->owner()->view_state_changed();
567 bv->owner()->updateMenubar();
568 bv->owner()->updateToolbar();
569 if (old_par != cpar(bv)) {
570 bv->owner()->setLayout(cpar(bv)->layout()->name());
576 string const InsetText::editMessage() const
578 return _("Opened Text Inset");
582 void InsetText::insetUnlock(BufferView * bv)
584 if (the_locking_inset) {
585 the_locking_inset->insetUnlock(bv);
586 the_locking_inset = 0;
587 updateLocal(bv, CURSOR_PAR, false);
598 if (lt->selection.set()) {
599 lt->clearSelection();
601 } else if (owner()) {
602 bv->owner()->setLayout(owner()->getLyXText(bv)
603 ->cursor.par()->layout()->name());
605 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
606 // hack for deleteEmptyParMech
607 if (!paragraphs.begin()->empty()) {
608 lt->setCursor(paragraphs.begin(), 0);
609 } else if (boost::next(paragraphs.begin()) != paragraphs.end()) {
610 lt->setCursor(boost::next(paragraphs.begin()), 0);
615 updateLocal(bv, code, false);
618 setUpdateStatus(bv, code);
623 void InsetText::lockInset(BufferView * bv)
626 the_locking_inset = 0;
627 inset_pos = inset_x = inset_y = 0;
628 inset_boundary = false;
636 lt->setCursor(paragraphs.begin(), 0);
637 lt->clearSelection();
639 // If the inset is empty set the language of the current font to the
640 // language to the surronding text (if different).
641 if (paragraphs.begin()->empty() &&
642 boost::next(paragraphs.begin()) == paragraphs.end() &&
643 bv->getParentLanguage(this) != lt->current_font.language()) {
644 LyXFont font(LyXFont::ALL_IGNORE);
645 font.setLanguage(bv->getParentLanguage(this));
646 setFont(bv, font, false);
651 if (drawFrame_ == LOCKED)
652 code = CURSOR|DRAW_FRAME;
653 setUpdateStatus(bv, code);
657 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
659 the_locking_inset = inset;
660 inset_x = cix(bv) - top_x + drawTextXOffset;
661 inset_y = ciy(bv) + drawTextYOffset;
662 inset_pos = cpos(bv);
663 inset_par = cpar(bv);
664 inset_boundary = cboundary(bv);
665 updateLocal(bv, CURSOR, false);
669 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
671 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
675 if (!the_locking_inset) {
676 ParagraphList::iterator pit = paragraphs.begin();
677 ParagraphList::iterator pend = paragraphs.end();
679 int const id = inset->id();
680 for (; pit != pend; ++pit) {
681 InsetList::iterator it =
682 pit->insetlist.begin();
683 InsetList::iterator const end =
684 pit->insetlist.end();
685 for (; it != end; ++it) {
686 if (it.getInset() == inset) {
687 getLyXText(bv)->setCursorIntern(pit, it.getPos());
688 lockInset(bv, inset);
691 if (it.getInset()->getInsetFromID(id)) {
692 getLyXText(bv)->setCursorIntern(pit, it.getPos());
693 it.getInset()->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
694 return the_locking_inset->lockInsetInInset(bv, inset);
700 if (inset == cpar(bv)->getInset(cpos(bv))) {
701 lyxerr[Debug::INSETS] << "OK" << endl;
702 lockInset(bv, inset);
704 } else if (the_locking_inset && (the_locking_inset == inset)) {
705 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
706 lyxerr[Debug::INSETS] << "OK" << endl;
707 inset_x = cix(bv) - top_x + drawTextXOffset;
708 inset_y = ciy(bv) + drawTextYOffset;
710 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
712 } else if (the_locking_inset) {
713 lyxerr[Debug::INSETS] << "MAYBE" << endl;
714 return the_locking_inset->lockInsetInInset(bv, inset);
716 lyxerr[Debug::INSETS] << "NOT OK" << endl;
721 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
724 if (!the_locking_inset)
726 if (the_locking_inset == inset) {
727 the_locking_inset->insetUnlock(bv);
728 getLyXText(bv)->updateInset(inset);
729 the_locking_inset = 0;
731 moveRightIntern(bv, true, false);
732 old_par = 0; // force layout setting
736 updateLocal(bv, CURSOR, false);
739 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
743 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
745 if (!autoBreakRows &&
746 boost::next(paragraphs.begin()) != paragraphs.end())
747 collapseParagraphs(bv);
755 if (inset->owner() != this) {
756 int ustat = CURSOR_PAR;
758 UpdatableInset * tl_inset = the_locking_inset;
760 found = tl_inset->updateInsetInInset(bv, inset);
762 tl_inset = static_cast<UpdatableInset *>(inset);
763 while(tl_inset->owner() && tl_inset->owner() != this)
764 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
765 if (!tl_inset->owner())
767 found = tl_inset->updateInsetInInset(bv, inset);
771 lt->updateInset(tl_inset);
775 setUpdateStatus(bv, ustat);
778 bool found = lt->updateInset(inset);
782 setUpdateStatus(bv, CURSOR_PAR);
783 if (the_locking_inset &&
784 cpar(bv) == inset_par && cpos(bv) == inset_pos)
786 inset_x = cix(bv) - top_x + drawTextXOffset;
787 inset_y = ciy(bv) + drawTextYOffset;
794 void InsetText::lfunMousePress(FuncRequest const & cmd)
798 // use this to check mouse motion for selection!
802 BufferView * bv = cmd.view();
803 FuncRequest cmd1 = cmd;
809 int tmp_x = cmd.x - drawTextXOffset;
810 int tmp_y = cmd.y + dim_.a - getLyXText(bv)->top_y();
811 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
813 if (the_locking_inset) {
814 if (the_locking_inset == inset) {
815 the_locking_inset->localDispatch(cmd1);
818 // otherwise only unlock the_locking_inset
819 the_locking_inset->insetUnlock(bv);
820 the_locking_inset = 0;
823 no_selection = false;
825 if (bv->theLockingInset()) {
826 if (isHighlyEditableInset(inset)) {
827 // We just have to lock the inset before calling a
829 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
830 if (!bv->lockInset(uinset)) {
831 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
833 inset->localDispatch(cmd1);
834 if (the_locking_inset)
835 updateLocal(bv, CURSOR, false);
840 bool paste_internally = false;
841 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
842 localDispatch(FuncRequest(bv, LFUN_COPY));
843 paste_internally = true;
850 int old_top_y = lt->top_y();
852 lt->setCursorFromCoordinates(cmd.x - drawTextXOffset,
854 // set the selection cursor!
855 lt->selection.cursor = lt->cursor;
856 lt->cursor.x_fix(lt->cursor.x());
858 if (lt->selection.set()) {
859 lt->clearSelection();
860 updateLocal(bv, FULL, false);
862 lt->clearSelection();
863 updateLocal(bv, CURSOR, false);
869 bv->owner()->setLayout(cpar(bv)->layout()->name());
871 // we moved the view we cannot do mouse selection in this case!
872 if (getLyXText(bv)->top_y() != old_top_y)
875 // Insert primary selection with middle mouse
876 // if there is a local selection in the current buffer,
878 if (cmd.button() == mouse_button::button2) {
879 if (paste_internally)
880 localDispatch(FuncRequest(bv, LFUN_PASTE));
882 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
885 getLyXText(bv)->clearSelection();
890 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
892 BufferView * bv = cmd.view();
893 FuncRequest cmd1 = cmd;
898 if (the_locking_inset)
899 return the_locking_inset->localDispatch(cmd1);
901 int tmp_x = cmd.x - drawTextXOffset;
902 int tmp_y = cmd.y + dim_.a - getLyXText(bv)->top_y();
903 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
906 // This code should probably be removed now. Simple insets
907 // (!highlyEditable) can actually take the localDispatch,
908 // and turn it into edit() if necessary. But we still
909 // need to deal properly with the whole relative vs.
910 // absolute mouse co-ords thing in a realiable, sensible way
912 if (isHighlyEditableInset(inset))
913 ret = inset->localDispatch(cmd1);
915 inset_x = cix(bv) - top_x + drawTextXOffset;
916 inset_y = ciy(bv) + drawTextYOffset;
917 cmd1.x = cmd.x - inset_x;
918 cmd1.y = cmd.x - inset_y;
919 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
923 ret = inset->localDispatch(cmd1);
924 updateLocal(bv, CURSOR_PAR, false);
931 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
933 FuncRequest cmd1 = cmd;
937 if (the_locking_inset) {
938 the_locking_inset->localDispatch(cmd1);
942 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
945 BufferView * bv = cmd.view();
951 LyXCursor cur = lt->cursor;
952 lt->setCursorFromCoordinates
953 (cmd.x - drawTextXOffset, cmd.y + dim_.a);
954 lt->cursor.x_fix(lt->cursor.x());
955 if (cur == lt->cursor) {
961 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
962 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
966 updateLocal(bv, SELECTION, false);
971 Inset::RESULT InsetText::localDispatch(FuncRequest const & cmd)
973 BufferView * bv = cmd.view();
975 if (cmd.action == LFUN_INSET_EDIT) {
976 UpdatableInset::localDispatch(cmd);
978 if (!bv->lockInset(this)) {
979 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
984 the_locking_inset = 0;
985 inset_pos = inset_x = inset_y = 0;
986 inset_boundary = false;
996 if (cmd.argument.size()) {
997 if (cmd.argument == "left")
998 lt->setCursor(paragraphs.begin(), 0);
1000 ParagraphList::iterator it = paragraphs.begin();
1001 ParagraphList::iterator end = paragraphs.end();
1002 while (boost::next(it) != end)
1004 // int const pos = (p->size() ? p->size()-1 : p->size());
1005 lt->setCursor(it, it->size());
1008 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
1009 // we put here -1 and not button as now the button in the
1010 // edit call should not be needed we will fix this in 1.3.x
1011 // cycle hopefully (Jug 20020509)
1012 // FIXME: GUII I've changed this to none: probably WRONG
1013 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
1014 lt->setCursorFromCoordinates(cmd.x - drawTextXOffset,
1016 lt->cursor.x_fix(lt->cursor.x());
1020 lt->clearSelection();
1023 // If the inset is empty set the language of the current font to the
1024 // language to the surronding text (if different).
1025 if (paragraphs.begin()->empty() &&
1026 boost::next(paragraphs.begin()) == paragraphs.end()&&
1027 bv->getParentLanguage(this) != lt->current_font.language())
1029 LyXFont font(LyXFont::ALL_IGNORE);
1030 font.setLanguage(bv->getParentLanguage(this));
1031 setFont(bv, font, false);
1037 if (drawFrame_ == LOCKED)
1038 code = CURSOR | DRAW_FRAME;
1040 updateLocal(bv, code, false);
1041 // Tell the paragraph dialog that we've entered an insettext.
1042 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1047 switch (cmd.action) {
1048 case LFUN_MOUSE_PRESS:
1049 lfunMousePress(cmd);
1051 case LFUN_MOUSE_MOTION:
1052 lfunMouseMotion(cmd);
1054 case LFUN_MOUSE_RELEASE:
1055 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
1060 bool was_empty = (paragraphs.begin()->empty() &&
1061 boost::next(paragraphs.begin()) == paragraphs.end());
1062 no_selection = false;
1063 RESULT result = UpdatableInset::localDispatch(cmd);
1064 if (result != UNDISPATCHED)
1067 result = DISPATCHED;
1068 if (cmd.action < 0 && cmd.argument.empty())
1071 if (the_locking_inset) {
1072 result = the_locking_inset->localDispatch(cmd);
1073 if (result == DISPATCHED_NOUPDATE)
1075 else if (result == DISPATCHED) {
1076 updateLocal(bv, CURSOR_PAR, false);
1078 } else if (result >= FINISHED) {
1080 case FINISHED_RIGHT:
1081 moveRightIntern(bv, false, false);
1082 result = DISPATCHED;
1085 if ((result = moveUp(bv)) >= FINISHED) {
1086 updateLocal(bv, CURSOR, false);
1087 bv->unlockInset(this);
1091 if ((result = moveDown(bv)) >= FINISHED) {
1092 updateLocal(bv, CURSOR, false);
1093 bv->unlockInset(this);
1097 result = DISPATCHED;
1100 the_locking_inset = 0;
1101 updateLocal(bv, CURSOR, false);
1102 // make sure status gets reset immediately
1103 bv->owner()->clearMessage();
1109 lt = getLyXText(bv);
1113 int updflag = false;
1115 // what type of update to do on a cursor movement
1116 int cursor_update = CURSOR;
1118 if (lt->selection.set())
1119 cursor_update = SELECTION;
1121 switch (cmd.action) {
1124 case LFUN_SELFINSERT:
1125 if (bv->buffer()->isReadonly()) {
1126 // setErrorMessage(N_("Document is read only"));
1129 if (!cmd.argument.empty()) {
1130 /* Automatically delete the currently selected
1131 * text and replace it with what is being
1132 * typed in now. Depends on lyxrc settings
1133 * "auto_region_delete", which defaults to
1136 // This should not be needed here and is also WRONG!
1137 setUndo(bv, Undo::INSERT, lt->cursor.par());
1140 if (lyxrc.auto_region_delete) {
1141 if (lt->selection.set()) {
1142 lt->cutSelection(false, false);
1145 lt->clearSelection();
1146 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
1147 bv->owner()->getIntl().getTransManager().
1148 TranslateAndInsert(cmd.argument[i], lt);
1151 lt->selection.cursor = lt->cursor;
1152 updwhat = CURSOR | CURSOR_PAR;
1154 result = DISPATCHED_NOUPDATE;
1157 // cursor movements that need special handling
1160 result = moveRight(bv);
1162 updwhat = cursor_update;
1166 result = moveLeft(bv);
1167 updwhat = cursor_update;
1171 result = moveDown(bv);
1172 updwhat = cursor_update;
1176 result = moveUp(bv);
1177 updwhat = cursor_update;
1181 if (crow(bv) == lt->rows().begin())
1182 result = FINISHED_UP;
1184 lt->cursorPrevious();
1185 lt->clearSelection();
1186 result = DISPATCHED_NOUPDATE;
1188 updwhat = cursor_update;
1192 if (boost::next(crow(bv)) == lt->rows().end())
1193 result = FINISHED_DOWN;
1196 lt->clearSelection();
1197 result = DISPATCHED_NOUPDATE;
1199 updwhat = cursor_update;
1202 case LFUN_BACKSPACE: {
1203 if (lt->selection.set())
1204 lt->cutSelection(true, false);
1207 updwhat = CURSOR_PAR;
1213 if (lt->selection.set()) {
1214 lt->cutSelection(true, false);
1218 updwhat = CURSOR_PAR;
1224 lt->cutSelection(bv);
1225 updwhat = CURSOR_PAR;
1232 lt->copySelection();
1233 updwhat = CURSOR_PAR;
1236 case LFUN_PASTESELECTION:
1238 string const clip(bv->getClipboard());
1242 if (cmd.argument == "paragraph") {
1243 lt->insertStringAsParagraphs(clip);
1245 lt->insertStringAsLines(clip);
1248 lt->clearSelection();
1250 updwhat = CURSOR_PAR;
1256 if (!autoBreakRows) {
1257 if (CutAndPaste::nrOfParagraphs() > 1) {
1258 #ifdef WITH_WARNINGS
1259 #warning FIXME horrendously bad UI
1261 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1266 lt->pasteSelection();
1268 lt->clearSelection();
1269 updwhat = CURSOR_PAR;
1274 case LFUN_BREAKPARAGRAPH:
1275 if (!autoBreakRows) {
1276 result = DISPATCHED;
1279 lt->breakParagraph(paragraphs, 0);
1280 updwhat = CURSOR | FULL;
1284 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1285 if (!autoBreakRows) {
1286 result = DISPATCHED;
1289 lt->breakParagraph(paragraphs, 1);
1290 updwhat = CURSOR | FULL;
1294 case LFUN_BREAKLINE: {
1295 if (!autoBreakRows) {
1296 result = DISPATCHED;
1300 lt->insertInset(new InsetNewline);
1301 updwhat = CURSOR | CURSOR_PAR;
1307 // do not set layouts on non breakable textinsets
1308 if (autoBreakRows) {
1309 string cur_layout = cpar(bv)->layout()->name();
1311 // Derive layout number from given argument (string)
1312 // and current buffer's textclass (number). */
1313 LyXTextClass const & tclass =
1314 bv->buffer()->params.getLyXTextClass();
1315 string layout = cmd.argument;
1316 bool hasLayout = tclass.hasLayout(layout);
1318 // If the entry is obsolete, use the new one instead.
1320 string const & obs =
1321 tclass[layout]->obsoleted_by();
1326 // see if we found the layout number:
1328 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1329 bv->owner()->dispatch(lf);
1333 if (cur_layout != layout) {
1334 cur_layout = layout;
1335 lt->setLayout(layout);
1336 bv->owner()->setLayout(cpar(bv)->layout()->name());
1337 updwhat = CURSOR_PAR;
1341 // reset the layout box
1342 bv->owner()->setLayout(cpar(bv)->layout()->name());
1345 case LFUN_PARAGRAPH_SPACING:
1346 // This one is absolutely not working. When fiddling with this
1347 // it also seems to me that the paragraphs inside the insettext
1348 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1349 // FIXME: how old is this comment ? ...
1351 ParagraphList::iterator pit = lt->cursor.par();
1352 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1353 float cur_value = 1.0;
1354 if (cur_spacing == Spacing::Other) {
1355 cur_value = pit->params().spacing().getValue();
1358 istringstream istr(STRCONV(cmd.argument));
1361 Spacing::Space new_spacing = cur_spacing;
1362 float new_value = cur_value;
1364 lyxerr << "Missing argument to `paragraph-spacing'"
1366 } else if (tmp == "single") {
1367 new_spacing = Spacing::Single;
1368 } else if (tmp == "onehalf") {
1369 new_spacing = Spacing::Onehalf;
1370 } else if (tmp == "double") {
1371 new_spacing = Spacing::Double;
1372 } else if (tmp == "other") {
1373 new_spacing = Spacing::Other;
1376 lyxerr << "new_value = " << tmpval << endl;
1379 } else if (tmp == "default") {
1380 new_spacing = Spacing::Default;
1382 lyxerr << _("Unknown spacing argument: ")
1383 << cmd.argument << endl;
1385 if (cur_spacing != new_spacing || cur_value != new_value) {
1386 pit->params().spacing(Spacing(new_spacing, new_value));
1387 updwhat = CURSOR_PAR;
1393 // These need to do repaints but don't require
1394 // special handling otherwise. A *lot* of the
1395 // above could probably be done similarly ...
1400 case LFUN_WORDRIGHT:
1401 // these two are really unhandled ...
1403 case LFUN_BEGINNINGBUF:
1404 updwhat = cursor_update;
1405 if (!bv->dispatch(cmd))
1406 result = UNDISPATCHED;
1415 case LFUN_WORDLEFTSEL:
1416 case LFUN_WORDRIGHTSEL:
1417 updwhat = SELECTION;
1422 if (!bv->dispatch(cmd))
1423 result = UNDISPATCHED;
1430 updateLocal(bv, updwhat, updflag);
1431 /// If the action has deleted all text in the inset, we need to change the
1432 // language to the language of the surronding text.
1433 if (!was_empty && paragraphs.begin()->empty() &&
1434 boost::next(paragraphs.begin()) == paragraphs.end()) {
1435 LyXFont font(LyXFont::ALL_IGNORE);
1436 font.setLanguage(bv->getParentLanguage(this));
1437 setFont(bv, font, false);
1440 if (result >= FINISHED)
1441 bv->unlockInset(this);
1443 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1444 result = DISPATCHED;
1449 int InsetText::latex(Buffer const * buf, ostream & os,
1450 LatexRunParams const & runparams) const
1453 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1454 return texrow.rows();
1458 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1460 unsigned int lines = 0;
1462 ParagraphList::iterator beg = paragraphs.begin();
1463 ParagraphList::iterator end = paragraphs.end();
1464 ParagraphList::iterator it = beg;
1465 for (; it != end; ++it) {
1466 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1467 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1474 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1476 unsigned int lines = 0;
1478 vector<string> environment_stack(10);
1479 vector<string> environment_inner(10);
1481 int const command_depth = 0;
1484 Paragraph::depth_type depth = 0; // paragraph depth
1486 ParagraphList::iterator pit = paragraphs.begin();
1487 ParagraphList::iterator pend = paragraphs.end();
1489 for (; pit != pend; ++pit) {
1491 int desc_on = 0; // description mode
1493 LyXLayout_ptr const & style = pit->layout();
1495 // environment tag closing
1496 for (; depth > pit->params().depth(); --depth) {
1497 if (environment_inner[depth] != "!-- --") {
1498 item_name = "listitem";
1499 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1500 if (environment_inner[depth] == "varlistentry")
1501 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1503 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1504 environment_stack[depth].erase();
1505 environment_inner[depth].erase();
1508 if (depth == pit->params().depth()
1509 && environment_stack[depth] != style->latexname()
1510 && !environment_stack[depth].empty()) {
1511 if (environment_inner[depth] != "!-- --") {
1512 item_name= "listitem";
1513 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1514 if (environment_inner[depth] == "varlistentry")
1515 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1518 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1520 environment_stack[depth].erase();
1521 environment_inner[depth].erase();
1524 // Write opening SGML tags.
1525 switch (style->latextype) {
1526 case LATEX_PARAGRAPH:
1527 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1531 buf->sgmlError(pit, 0, _("Error: LatexType Command not allowed here.\n"));
1535 case LATEX_ENVIRONMENT:
1536 case LATEX_ITEM_ENVIRONMENT:
1537 if (depth < pit->params().depth()) {
1538 depth = pit->params().depth();
1539 environment_stack[depth].erase();
1542 if (environment_stack[depth] != style->latexname()) {
1543 if (environment_stack.size() == depth + 1) {
1544 environment_stack.push_back("!-- --");
1545 environment_inner.push_back("!-- --");
1547 environment_stack[depth] = style->latexname();
1548 environment_inner[depth] = "!-- --";
1549 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1551 if (environment_inner[depth] != "!-- --") {
1552 item_name= "listitem";
1553 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1554 if (environment_inner[depth] == "varlistentry")
1555 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1559 if (style->latextype == LATEX_ENVIRONMENT) {
1560 if (!style->latexparam().empty()) {
1561 if (style->latexparam() == "CDATA")
1564 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1569 desc_on = (style->labeltype == LABEL_MANUAL);
1571 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1572 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1574 item_name = desc_on ? "term" : "para";
1575 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1579 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1583 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1586 // write closing SGML tags
1587 switch (style->latextype) {
1588 case LATEX_ENVIRONMENT:
1589 if (!style->latexparam().empty()) {
1590 if (style->latexparam() == "CDATA")
1593 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1596 case LATEX_ITEM_ENVIRONMENT:
1597 if (desc_on == 1) break;
1599 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1601 case LATEX_PARAGRAPH:
1602 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1605 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1611 for (int d = depth; d >= 0; --d) {
1612 if (!environment_stack[depth].empty()) {
1613 if (environment_inner[depth] != "!-- --") {
1614 item_name = "listitem";
1615 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1616 if (environment_inner[depth] == "varlistentry")
1617 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1620 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1628 void InsetText::validate(LaTeXFeatures & features) const
1630 for_each(paragraphs.begin(), paragraphs.end(),
1631 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1635 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1637 if (the_locking_inset) {
1638 the_locking_inset->getCursor(bv, x, y);
1642 y = cy(&bv) + InsetText::y();
1646 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1648 if (the_locking_inset) {
1649 the_locking_inset->getCursorPos(bv, x, y);
1652 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1653 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1657 int InsetText::insetInInsetY() const
1659 if (!the_locking_inset)
1662 return (inset_y + the_locking_inset->insetInInsetY());
1666 void InsetText::fitInsetCursor(BufferView * bv) const
1668 if (the_locking_inset) {
1669 the_locking_inset->fitInsetCursor(bv);
1672 LyXFont const font =
1673 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1675 int const asc = font_metrics::maxAscent(font);
1676 int const desc = font_metrics::maxDescent(font);
1678 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1679 need_update |= FULL;
1684 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1686 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1687 return moveLeftIntern(bv, false, activate_inset, selecting);
1689 return moveRightIntern(bv, true, activate_inset, selecting);
1694 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1696 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1697 return moveRightIntern(bv, true, activate_inset, selecting);
1699 return moveLeftIntern(bv, false, activate_inset, selecting);
1704 InsetText::moveRightIntern(BufferView * bv, bool front,
1705 bool activate_inset, bool selecting)
1707 LyXText * text = getLyXText(bv);
1709 ParagraphList::iterator c_par = cpar(bv);
1711 if (boost::next(c_par) == paragraphs.end() &&
1712 (cpos(bv) >= c_par->size()))
1713 return FINISHED_RIGHT;
1714 if (activate_inset && checkAndActivateInset(bv, front))
1716 text->cursorRight(bv);
1718 text->clearSelection();
1719 return DISPATCHED_NOUPDATE;
1724 InsetText::moveLeftIntern(BufferView * bv, bool front,
1725 bool activate_inset, bool selecting)
1727 LyXText * text = getLyXText(bv);
1729 if (cpar(bv) == paragraphs.begin() && (cpos(bv) <= 0))
1731 text->cursorLeft(bv);
1733 text->clearSelection();
1734 if (activate_inset && checkAndActivateInset(bv, front))
1736 return DISPATCHED_NOUPDATE;
1740 Inset::RESULT InsetText::moveUp(BufferView * bv)
1742 if (crow(bv) == getLyXText(bv)->rows().begin())
1744 getLyXText(bv)->cursorUp(bv);
1745 getLyXText(bv)->clearSelection();
1746 return DISPATCHED_NOUPDATE;
1750 Inset::RESULT InsetText::moveDown(BufferView * bv)
1752 if (boost::next(crow(bv)) == getLyXText(bv)->rows().end())
1753 return FINISHED_DOWN;
1754 getLyXText(bv)->cursorDown(bv);
1755 getLyXText(bv)->clearSelection();
1756 return DISPATCHED_NOUPDATE;
1760 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1762 if (the_locking_inset) {
1763 if (the_locking_inset->insetAllowed(inset))
1764 return the_locking_inset->insertInset(bv, inset);
1767 inset->setOwner(this);
1768 getLyXText(bv)->insertInset(inset);
1770 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1775 bool InsetText::insetAllowed(Inset::Code code) const
1777 // in_insetAllowed is a really gross hack,
1778 // to allow us to call the owner's insetAllowed
1779 // without stack overflow, which can happen
1780 // when the owner uses InsetCollapsable::insetAllowed()
1782 if (in_insetAllowed)
1784 in_insetAllowed = true;
1785 if (the_locking_inset)
1786 ret = the_locking_inset->insetAllowed(code);
1788 ret = owner()->insetAllowed(code);
1789 in_insetAllowed = false;
1794 UpdatableInset * InsetText::getLockingInset() const
1796 return the_locking_inset ? the_locking_inset->getLockingInset() :
1797 const_cast<InsetText *>(this);
1801 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1805 if (the_locking_inset)
1806 return the_locking_inset->getFirstLockingInsetOfType(c);
1811 bool InsetText::showInsetDialog(BufferView * bv) const
1813 if (the_locking_inset)
1814 return the_locking_inset->showInsetDialog(bv);
1819 vector<string> const InsetText::getLabelList() const
1821 vector<string> label_list;
1823 ParagraphList::iterator pit = paragraphs.begin();
1824 ParagraphList::iterator pend = paragraphs.end();
1825 for (; pit != pend; ++pit) {
1826 InsetList::iterator beg = pit->insetlist.begin();
1827 InsetList::iterator end = pit->insetlist.end();
1828 for (; beg != end; ++beg) {
1829 vector<string> const l = beg.getInset()->getLabelList();
1830 label_list.insert(label_list.end(), l.begin(), l.end());
1837 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1840 if (the_locking_inset) {
1841 the_locking_inset->setFont(bv, font, toggleall, selectall);
1844 if ((boost::next(paragraphs.begin()) == paragraphs.end() &&
1845 paragraphs.begin()->empty()) || cpar(bv)->empty()) {
1846 getLyXText(bv)->setFont(font, toggleall);
1851 lt = getLyXText(bv);
1854 if (lt->selection.set()) {
1855 setUndo(bv, Undo::EDIT, lt->cursor.par());
1859 lt->toggleFree(font, toggleall);
1861 lt->clearSelection();
1863 bool flag = (selectall || lt->selection.set());
1867 updateLocal(bv, FULL, true);
1869 updateLocal(bv, CURSOR_PAR, true);
1873 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1875 if (cpar(bv)->isInset(cpos(bv))) {
1877 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1878 if (!isHighlyEditableInset(inset))
1880 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1881 inset->localDispatch(cmd);
1882 if (!the_locking_inset)
1884 updateLocal(bv, CURSOR, false);
1891 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1892 mouse_button::state button)
1894 x -= drawTextXOffset;
1896 int dummyy = y + dim_.a;
1897 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1898 // we only do the edit() call if the inset was hit by the mouse
1899 // or if it is a highly editable inset. So we should call this
1900 // function from our own edit with button < 0.
1901 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1903 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1911 inset_x = cix(bv) - top_x + drawTextXOffset;
1912 inset_y = ciy(bv) + drawTextYOffset;
1913 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1914 inset->localDispatch(cmd);
1915 if (!the_locking_inset)
1917 updateLocal(bv, CURSOR, false);
1924 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1927 int w = UpdatableInset::getMaxWidth(bv, inset);
1932 w = w - top_x + owner()->x();
1935 w -= (2 * TEXT_TO_INSET_OFFSET);
1938 return UpdatableInset::getMaxWidth(bv, inset);
1943 void InsetText::setParagraphData(ParagraphList const & plist, bool same_id)
1945 // we have to unlock any locked inset otherwise we're in troubles
1946 the_locking_inset = 0;
1948 #warning CHECK not adhering to same_id here might wreck havoc (Lgb)
1949 // But it it makes no difference that is a lot better.
1951 lyxerr << "Same_id called with 'true'" << endl;
1952 lyxerr << "Please report this to the list." << endl;
1955 ParagraphList::iterator it = plist.begin();
1956 ParagraphList::iterator end = plist.end();
1957 for (; it != end; ++it) {
1958 int const id = it->id();
1959 paragraphs.push_back(*it);
1960 Paragraph & tmp = paragraphs.back();
1961 tmp.setInsetOwner(this);
1966 // See if this can be simplified when std::list is in effect.
1969 ParagraphList::iterator it = plist.begin();
1970 ParagraphList::iterator end = plist.end();
1971 for (; it != end; ++it) {
1972 paragraphs.push_back(*it);
1973 Paragraph & tmp = paragraphs.back();
1974 tmp.setInsetOwner(this);
1983 void InsetText::markNew(bool track_changes)
1985 ParagraphList::iterator pit = paragraphs.begin();
1986 ParagraphList::iterator pend = paragraphs.end();
1987 for (; pit != pend; ++pit) {
1988 if (track_changes) {
1989 pit->trackChanges();
1991 // no-op when not tracking
1992 pit->cleanChanges();
1998 void InsetText::setText(string const & data, LyXFont const & font)
2001 for (unsigned int i = 0; i < data.length(); ++i)
2002 paragraphs.begin()->insertChar(i, data[i], font);
2007 void InsetText::setAutoBreakRows(bool flag)
2009 if (flag != autoBreakRows) {
2010 autoBreakRows = flag;
2018 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2020 if (how != drawFrame_) {
2023 updateLocal(bv, DRAW_FRAME, false);
2028 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2030 if (frame_color != col) {
2033 updateLocal(bv, DRAW_FRAME, false);
2038 int InsetText::cx(BufferView * bv) const
2040 // we do nothing dangerous so we use a local cache
2041 LyXText * llt = getLyXText(bv);
2042 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2043 if (the_locking_inset) {
2044 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2046 if (font.isVisibleRightToLeft())
2047 x -= the_locking_inset->width(bv, font);
2053 int InsetText::cix(BufferView * bv) const
2055 // we do nothing dangerous so we use a local cache
2056 LyXText * llt = getLyXText(bv);
2057 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2058 if (the_locking_inset) {
2059 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2061 if (font.isVisibleRightToLeft())
2062 x -= the_locking_inset->width(bv, font);
2068 int InsetText::cy(BufferView * bv) const
2071 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2075 int InsetText::ciy(BufferView * bv) const
2078 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2082 pos_type InsetText::cpos(BufferView * bv) const
2084 return getLyXText(bv)->cursor.pos();
2088 ParagraphList::iterator InsetText::cpar(BufferView * bv) const
2090 return getLyXText(bv)->cursor.par();
2094 bool InsetText::cboundary(BufferView * bv) const
2096 return getLyXText(bv)->cursor.boundary();
2100 RowList::iterator InsetText::crow(BufferView * bv) const
2102 return getLyXText(bv)->cursor.row();
2106 LyXText * InsetText::getLyXText(BufferView const * lbv,
2107 bool const recursive) const
2109 if (cached_bview == lbv) {
2110 if (recursive && the_locking_inset)
2111 return the_locking_inset->getLyXText(lbv, true);
2112 LyXText * lt = cached_text.get();
2113 lyx::Assert(lt && lt->rows().begin()->par() == paragraphs.begin());
2116 // Super UGLY! (Lgb)
2117 BufferView * bv = const_cast<BufferView *>(lbv);
2120 Cache::iterator it = cache.find(bv);
2122 if (it != cache.end()) {
2125 } else if (do_resize) {
2126 resizeLyXText(do_resize);
2128 if (lt || !it->second.remove) {
2129 lyx::Assert(it->second.text.get());
2130 cached_text = it->second.text;
2131 if (recursive && the_locking_inset) {
2132 return the_locking_inset->getLyXText(bv, true);
2134 return cached_text.get();
2135 } else if (it->second.remove) {
2137 saveLyXTextState(it->second.text.get());
2143 // when we have to reinit the existing LyXText!
2145 it->second.text->init(bv);
2146 restoreLyXTextState(it->second.text.get());
2147 it->second.remove = false;
2149 cached_text = it->second.text;
2150 if (the_locking_inset && recursive) {
2151 return the_locking_inset->getLyXText(bv);
2153 return cached_text.get();
2156 // we are here only if we don't have a BufferView * in the cache!!!
2158 cached_text.reset(new LyXText(bv, const_cast<InsetText *>(this)));
2159 cached_text->init(bv);
2160 restoreLyXTextState(cached_text.get());
2162 cache.insert(make_pair(bv, cached_text));
2164 if (the_locking_inset && recursive) {
2165 return the_locking_inset->getLyXText(bv);
2167 return cached_text.get();
2171 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2175 Cache::iterator it = cache.find(bv);
2177 if (it == cache.end()) {
2181 lyx::Assert(it->second.text.get());
2183 it->second.remove = true;
2185 /// then remove all LyXText in text-insets
2186 for_each(paragraphs.begin(), paragraphs.end(),
2187 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2192 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2195 // we cannot resize this because we are in use!
2196 // so do this on the next possible getLyXText()
2201 if (boost::next(paragraphs.begin()) == paragraphs.end() &&
2202 paragraphs.begin()->empty()) { // no data, resize not neccessary!
2203 // we have to do this as a fixed width may have changed!
2204 LyXText * t = getLyXText(bv);
2205 saveLyXTextState(t);
2207 restoreLyXTextState(t);
2210 // one endless line, resize normally not necessary
2211 if (!force && getMaxWidth(bv, this) < 0)
2214 Cache::iterator it = cache.find(bv);
2215 if (it == cache.end()) {
2218 lyx::Assert(it->second.text.get());
2220 LyXText * t = it->second.text.get();
2221 saveLyXTextState(t);
2223 for_each(paragraphs.begin(), paragraphs.end(),
2224 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2227 restoreLyXTextState(t);
2228 if (the_locking_inset) {
2229 inset_x = cix(bv) - top_x + drawTextXOffset;
2230 inset_y = ciy(bv) + drawTextYOffset;
2233 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2235 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2236 // this will scroll the screen such that the cursor becomes visible
2237 bv->updateScrollbar();
2239 need_update |= FULL;
2244 void InsetText::reinitLyXText() const
2247 // we cannot resize this because we are in use!
2248 // so do this on the next possible getLyXText()
2254 for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2255 lyx::Assert(it->second.text.get());
2257 LyXText * t = it->second.text.get();
2258 BufferView * bv = it->first;
2260 saveLyXTextState(t);
2262 for_each(paragraphs.begin(), paragraphs.end(),
2263 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2266 restoreLyXTextState(t);
2267 if (the_locking_inset) {
2268 inset_x = cix(bv) - top_x + drawTextXOffset;
2269 inset_y = ciy(bv) + drawTextYOffset;
2271 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2273 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2274 // this will scroll the screen such that the cursor becomes visible
2275 bv->updateScrollbar();
2283 void InsetText::removeNewlines()
2285 bool changed = false;
2287 ParagraphList::iterator it = paragraphs.begin();
2288 ParagraphList::iterator end = paragraphs.end();
2289 for (; it != end; ++it) {
2290 for (int i = 0; i < it->size(); ++i) {
2291 if (it->isNewline(i)) {
2302 bool InsetText::nodraw() const
2304 if (the_locking_inset)
2305 return the_locking_inset->nodraw();
2306 return UpdatableInset::nodraw();
2310 int InsetText::scroll(bool recursive) const
2312 int sx = UpdatableInset::scroll(false);
2314 if (recursive && the_locking_inset)
2315 sx += the_locking_inset->scroll(recursive);
2321 void InsetText::selectAll(BufferView * bv)
2323 getLyXText(bv)->cursorTop();
2324 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2325 getLyXText(bv)->cursorBottom();
2326 getLyXText(bv)->setSelection();
2330 void InsetText::clearSelection(BufferView * bv)
2332 getLyXText(bv)->clearSelection();
2336 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2338 Painter & pain = bv->painter();
2340 int h = dim_.a + dim_.d;
2341 int ty = baseline - dim_.a;
2347 if ((ty + h) > pain.paperHeight())
2348 h = pain.paperHeight();
2349 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2350 w = pain.paperWidth();
2351 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2356 ParagraphList * InsetText::getParagraphs(int i) const
2358 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2362 LyXCursor const & InsetText::cursor(BufferView * bv) const
2364 if (the_locking_inset)
2365 return the_locking_inset->cursor(bv);
2366 return getLyXText(bv)->cursor;
2370 Inset * InsetText::getInsetFromID(int id_arg) const
2373 return const_cast<InsetText *>(this);
2375 ParagraphList::iterator pit = paragraphs.begin();
2376 ParagraphList::iterator pend = paragraphs.end();
2377 for (; pit != pend; ++pit) {
2378 InsetList::iterator it = pit->insetlist.begin();
2379 InsetList::iterator end = pit->insetlist.end();
2380 for (; it != end; ++it) {
2381 if (it.getInset()->id() == id_arg)
2382 return it.getInset();
2383 Inset * in = it.getInset()->getInsetFromID(id_arg);
2393 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2394 float & value) const
2400 lt = getLyXText(bv);
2403 if (the_locking_inset) {
2404 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2405 if (!word.word().empty()) {
2411 // we have to go on checking so move cursor to the next char
2412 lt->cursor.pos(lt->cursor.pos() + 1);
2414 word = lt->selectNextWordToSpellcheck(value);
2415 if (word.word().empty())
2416 bv->unlockInset(const_cast<InsetText *>(this));
2425 void InsetText::selectSelectedWord(BufferView * bv)
2427 if (the_locking_inset) {
2428 the_locking_inset->selectSelectedWord(bv);
2431 getLyXText(bv)->selectSelectedWord();
2432 updateLocal(bv, SELECTION, false);
2436 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2438 if (the_locking_inset) {
2439 the_locking_inset->toggleSelection(bv, kill_selection);
2443 lt = getLyXText(bv);
2447 int x = top_x + TEXT_TO_INSET_OFFSET;
2449 RowList::iterator rowit = lt->rows().begin();
2450 RowList::iterator end = lt->rows().end();
2451 int y_offset = top_baseline - rowit->ascent_of_text();
2453 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2454 y += rowit->height();
2460 if (need_update & SELECTION)
2462 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2468 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2472 lt = getLyXText(bv);
2475 if (the_locking_inset) {
2476 if (the_locking_inset->nextChange(bv, length))
2478 lt->cursorRight(true);
2480 lyxfind::SearchResult result =
2481 lyxfind::findNextChange(bv, lt, length);
2483 if (result == lyxfind::SR_FOUND) {
2484 LyXCursor cur = lt->cursor;
2485 bv->unlockInset(bv->theLockingInset());
2486 if (bv->lockInset(this))
2489 lt->setSelectionRange(length);
2490 updateLocal(bv, SELECTION, false);
2494 return result != lyxfind::SR_NOT_FOUND;
2498 bool InsetText::searchForward(BufferView * bv, string const & str,
2503 lt = getLyXText(bv);
2506 if (the_locking_inset) {
2507 if (the_locking_inset->searchForward(bv, str, cs, mw))
2509 lt->cursorRight(true);
2511 lyxfind::SearchResult result =
2512 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2514 if (result == lyxfind::SR_FOUND) {
2515 LyXCursor cur = lt->cursor;
2516 bv->unlockInset(bv->theLockingInset());
2517 if (bv->lockInset(this))
2520 lt->setSelectionRange(str.length());
2521 updateLocal(bv, SELECTION, false);
2525 return (result != lyxfind::SR_NOT_FOUND);
2528 bool InsetText::searchBackward(BufferView * bv, string const & str,
2531 if (the_locking_inset) {
2532 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2537 lt = getLyXText(bv);
2541 ParagraphList::iterator pit = paragraphs.begin();
2542 ParagraphList::iterator pend = paragraphs.end();
2544 while (boost::next(pit) != pend)
2547 lt->setCursor(pit, pit->size());
2549 lyxfind::SearchResult result =
2550 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2552 if (result == lyxfind::SR_FOUND) {
2553 LyXCursor cur = lt->cursor;
2554 bv->unlockInset(bv->theLockingInset());
2555 if (bv->lockInset(this))
2558 lt->setSelectionRange(str.length());
2559 updateLocal(bv, SELECTION, false);
2563 return (result != lyxfind::SR_NOT_FOUND);
2567 bool InsetText::checkInsertChar(LyXFont & font)
2570 return owner()->checkInsertChar(font);
2575 void InsetText::collapseParagraphs(BufferView * bv)
2577 LyXText * llt = getLyXText(bv);
2579 while (boost::next(paragraphs.begin()) != paragraphs.end()) {
2580 if (!paragraphs.begin()->empty() &&
2581 !boost::next(paragraphs.begin())->empty() &&
2582 !paragraphs.begin()->isSeparator(paragraphs.begin()->size() - 1))
2584 paragraphs.begin()->insertChar(paragraphs.begin()->size(), ' ');
2586 if (llt->selection.set()) {
2587 if (llt->selection.start.par() == boost::next(paragraphs.begin())) {
2588 llt->selection.start.par(paragraphs.begin());
2589 llt->selection.start.pos(
2590 llt->selection.start.pos() + paragraphs.begin()->size());
2592 if (llt->selection.end.par() == boost::next(paragraphs.begin())) {
2593 llt->selection.end.par(paragraphs.begin());
2594 llt->selection.end.pos(
2595 llt->selection.end.pos() + paragraphs.begin()->size());
2598 mergeParagraph(bv->buffer()->params, paragraphs, paragraphs.begin());
2604 void InsetText::getDrawFont(LyXFont & font) const
2608 owner()->getDrawFont(font);
2612 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2614 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2615 // And it probably does. You have to take a look at this John. (Lgb)
2616 #warning John, have a look here. (Lgb)
2618 BufferParams const & bparams = buffer->params;
2620 Paragraph * tmpbuf = newpar;
2621 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2622 if (bparams.tracking_changes)
2623 buf->cleanChanges();
2625 while (tmpbuf->next()) {
2626 tmpbuf = tmpbuf->next();
2627 lastbuffer->next(new Paragraph(*tmpbuf, false));
2628 lastbuffer->next()->previous(lastbuffer);
2629 lastbuffer = lastbuffer->next();
2630 if (bparams.tracking_changes)
2631 lastbuffer->cleanChanges();
2634 lastbuffer = &*(paragraphs.begin());
2635 while (lastbuffer->next())
2636 lastbuffer = lastbuffer->next();
2637 if (!newpar->empty() && !lastbuffer->empty() &&
2638 !lastbuffer->isSeparator(lastbuffer->size() - 1))
2640 lastbuffer->insertChar(lastbuffer->size(), ' ');
2643 // make the buf exactly the same layout than our last paragraph
2644 buf->makeSameLayout(lastbuffer);
2647 lastbuffer->next(buf);
2648 buf->previous(lastbuffer);
2649 mergeParagraph(buffer->params, paragraphs, lastbuffer);
2651 ParagraphList::iterator pit = plist.begin();
2652 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2654 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2656 ParagraphList::iterator pend = plist.end();
2657 for (; pit != pend; ++pit) {
2658 paragraphs.push_back(*pit);
2667 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2669 ParagraphList::iterator pit = paragraphs.begin();
2670 ParagraphList::iterator pend = paragraphs.end();
2672 for (; pit != pend; ++pit) {
2673 InsetList::iterator it = pit->insetlist.begin();
2674 InsetList::iterator end = pit->insetlist.end();
2675 for (; it != end; ++it) {
2676 it.getInset()->addPreview(loader);