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(new 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(new 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,
1138 lt->cursor.par(), boost::next(lt->cursor.par()));
1141 if (lyxrc.auto_region_delete) {
1142 if (lt->selection.set()) {
1143 lt->cutSelection(false, false);
1146 lt->clearSelection();
1147 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
1148 bv->owner()->getIntl().getTransManager().
1149 TranslateAndInsert(cmd.argument[i], lt);
1152 lt->selection.cursor = lt->cursor;
1153 updwhat = CURSOR | CURSOR_PAR;
1155 result = DISPATCHED_NOUPDATE;
1158 // cursor movements that need special handling
1161 result = moveRight(bv);
1163 updwhat = cursor_update;
1167 result = moveLeft(bv);
1168 updwhat = cursor_update;
1172 result = moveDown(bv);
1173 updwhat = cursor_update;
1177 result = moveUp(bv);
1178 updwhat = cursor_update;
1182 if (crow(bv) == lt->rows().begin())
1183 result = FINISHED_UP;
1185 lt->cursorPrevious();
1186 lt->clearSelection();
1187 result = DISPATCHED_NOUPDATE;
1189 updwhat = cursor_update;
1193 if (boost::next(crow(bv)) == lt->rows().end())
1194 result = FINISHED_DOWN;
1197 lt->clearSelection();
1198 result = DISPATCHED_NOUPDATE;
1200 updwhat = cursor_update;
1203 case LFUN_BACKSPACE: {
1204 if (lt->selection.set())
1205 lt->cutSelection(true, false);
1208 updwhat = CURSOR_PAR;
1214 if (lt->selection.set()) {
1215 lt->cutSelection(true, false);
1219 updwhat = CURSOR_PAR;
1225 lt->cutSelection(bv);
1226 updwhat = CURSOR_PAR;
1233 lt->copySelection();
1234 updwhat = CURSOR_PAR;
1237 case LFUN_PASTESELECTION:
1239 string const clip(bv->getClipboard());
1243 if (cmd.argument == "paragraph") {
1244 lt->insertStringAsParagraphs(clip);
1246 lt->insertStringAsLines(clip);
1249 lt->clearSelection();
1251 updwhat = CURSOR_PAR;
1257 if (!autoBreakRows) {
1258 if (CutAndPaste::nrOfParagraphs() > 1) {
1259 #ifdef WITH_WARNINGS
1260 #warning FIXME horrendously bad UI
1262 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1267 lt->pasteSelection();
1269 lt->clearSelection();
1270 updwhat = CURSOR_PAR;
1275 case LFUN_BREAKPARAGRAPH:
1276 if (!autoBreakRows) {
1277 result = DISPATCHED;
1280 lt->breakParagraph(paragraphs, 0);
1281 updwhat = CURSOR | FULL;
1285 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1286 if (!autoBreakRows) {
1287 result = DISPATCHED;
1290 lt->breakParagraph(paragraphs, 1);
1291 updwhat = CURSOR | FULL;
1295 case LFUN_BREAKLINE: {
1296 if (!autoBreakRows) {
1297 result = DISPATCHED;
1301 lt->insertInset(new InsetNewline);
1302 updwhat = CURSOR | CURSOR_PAR;
1308 // do not set layouts on non breakable textinsets
1309 if (autoBreakRows) {
1310 string cur_layout = cpar(bv)->layout()->name();
1312 // Derive layout number from given argument (string)
1313 // and current buffer's textclass (number). */
1314 LyXTextClass const & tclass =
1315 bv->buffer()->params.getLyXTextClass();
1316 string layout = cmd.argument;
1317 bool hasLayout = tclass.hasLayout(layout);
1319 // If the entry is obsolete, use the new one instead.
1321 string const & obs =
1322 tclass[layout]->obsoleted_by();
1327 // see if we found the layout number:
1329 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1330 bv->owner()->dispatch(lf);
1334 if (cur_layout != layout) {
1335 cur_layout = layout;
1336 lt->setLayout(layout);
1337 bv->owner()->setLayout(cpar(bv)->layout()->name());
1338 updwhat = CURSOR_PAR;
1342 // reset the layout box
1343 bv->owner()->setLayout(cpar(bv)->layout()->name());
1346 case LFUN_PARAGRAPH_SPACING:
1347 // This one is absolutely not working. When fiddling with this
1348 // it also seems to me that the paragraphs inside the insettext
1349 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1350 // FIXME: how old is this comment ? ...
1352 ParagraphList::iterator pit = lt->cursor.par();
1353 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1354 float cur_value = 1.0;
1355 if (cur_spacing == Spacing::Other) {
1356 cur_value = pit->params().spacing().getValue();
1359 istringstream istr(STRCONV(cmd.argument));
1362 Spacing::Space new_spacing = cur_spacing;
1363 float new_value = cur_value;
1365 lyxerr << "Missing argument to `paragraph-spacing'"
1367 } else if (tmp == "single") {
1368 new_spacing = Spacing::Single;
1369 } else if (tmp == "onehalf") {
1370 new_spacing = Spacing::Onehalf;
1371 } else if (tmp == "double") {
1372 new_spacing = Spacing::Double;
1373 } else if (tmp == "other") {
1374 new_spacing = Spacing::Other;
1377 lyxerr << "new_value = " << tmpval << endl;
1380 } else if (tmp == "default") {
1381 new_spacing = Spacing::Default;
1383 lyxerr << _("Unknown spacing argument: ")
1384 << cmd.argument << endl;
1386 if (cur_spacing != new_spacing || cur_value != new_value) {
1387 pit->params().spacing(Spacing(new_spacing, new_value));
1388 updwhat = CURSOR_PAR;
1394 // These need to do repaints but don't require
1395 // special handling otherwise. A *lot* of the
1396 // above could probably be done similarly ...
1401 case LFUN_WORDRIGHT:
1402 // these two are really unhandled ...
1404 case LFUN_BEGINNINGBUF:
1405 updwhat = cursor_update;
1406 if (!bv->dispatch(cmd))
1407 result = UNDISPATCHED;
1416 case LFUN_WORDLEFTSEL:
1417 case LFUN_WORDRIGHTSEL:
1418 updwhat = SELECTION;
1423 if (!bv->dispatch(cmd))
1424 result = UNDISPATCHED;
1431 updateLocal(bv, updwhat, updflag);
1432 /// If the action has deleted all text in the inset, we need to change the
1433 // language to the language of the surronding text.
1434 if (!was_empty && paragraphs.begin()->empty() &&
1435 boost::next(paragraphs.begin()) == paragraphs.end()) {
1436 LyXFont font(LyXFont::ALL_IGNORE);
1437 font.setLanguage(bv->getParentLanguage(this));
1438 setFont(bv, font, false);
1441 if (result >= FINISHED)
1442 bv->unlockInset(this);
1444 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1445 result = DISPATCHED;
1450 int InsetText::latex(Buffer const * buf, ostream & os, LatexRunParams const & runparams,
1451 bool fragile, bool) const
1454 latexParagraphs(buf, paragraphs, os, texrow, runparams, fragile);
1455 return texrow.rows();
1459 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1461 unsigned int lines = 0;
1463 ParagraphList::iterator beg = paragraphs.begin();
1464 ParagraphList::iterator end = paragraphs.end();
1465 ParagraphList::iterator it = beg;
1466 for (; it != end; ++it) {
1467 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1468 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1475 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1477 unsigned int lines = 0;
1479 vector<string> environment_stack(10);
1480 vector<string> environment_inner(10);
1482 int const command_depth = 0;
1485 Paragraph::depth_type depth = 0; // paragraph depth
1487 ParagraphList::iterator pit = paragraphs.begin();
1488 ParagraphList::iterator pend = paragraphs.end();
1490 for (; pit != pend; ++pit) {
1492 int desc_on = 0; // description mode
1494 LyXLayout_ptr const & style = pit->layout();
1496 // environment tag closing
1497 for (; depth > pit->params().depth(); --depth) {
1498 if (environment_inner[depth] != "!-- --") {
1499 item_name = "listitem";
1500 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1501 if (environment_inner[depth] == "varlistentry")
1502 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1504 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1505 environment_stack[depth].erase();
1506 environment_inner[depth].erase();
1509 if (depth == pit->params().depth()
1510 && environment_stack[depth] != style->latexname()
1511 && !environment_stack[depth].empty()) {
1512 if (environment_inner[depth] != "!-- --") {
1513 item_name= "listitem";
1514 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1515 if (environment_inner[depth] == "varlistentry")
1516 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1519 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1521 environment_stack[depth].erase();
1522 environment_inner[depth].erase();
1525 // Write opening SGML tags.
1526 switch (style->latextype) {
1527 case LATEX_PARAGRAPH:
1528 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1532 buf->sgmlError(pit, 0, _("Error: LatexType Command not allowed here.\n"));
1536 case LATEX_ENVIRONMENT:
1537 case LATEX_ITEM_ENVIRONMENT:
1538 if (depth < pit->params().depth()) {
1539 depth = pit->params().depth();
1540 environment_stack[depth].erase();
1543 if (environment_stack[depth] != style->latexname()) {
1544 if (environment_stack.size() == depth + 1) {
1545 environment_stack.push_back("!-- --");
1546 environment_inner.push_back("!-- --");
1548 environment_stack[depth] = style->latexname();
1549 environment_inner[depth] = "!-- --";
1550 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1552 if (environment_inner[depth] != "!-- --") {
1553 item_name= "listitem";
1554 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1555 if (environment_inner[depth] == "varlistentry")
1556 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1560 if (style->latextype == LATEX_ENVIRONMENT) {
1561 if (!style->latexparam().empty()) {
1562 if (style->latexparam() == "CDATA")
1565 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1570 desc_on = (style->labeltype == LABEL_MANUAL);
1572 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1573 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1575 item_name = desc_on ? "term" : "para";
1576 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1580 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1584 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1587 // write closing SGML tags
1588 switch (style->latextype) {
1589 case LATEX_ENVIRONMENT:
1590 if (!style->latexparam().empty()) {
1591 if (style->latexparam() == "CDATA")
1594 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1597 case LATEX_ITEM_ENVIRONMENT:
1598 if (desc_on == 1) break;
1600 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1602 case LATEX_PARAGRAPH:
1603 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1606 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1612 for (int d = depth; d >= 0; --d) {
1613 if (!environment_stack[depth].empty()) {
1614 if (environment_inner[depth] != "!-- --") {
1615 item_name = "listitem";
1616 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1617 if (environment_inner[depth] == "varlistentry")
1618 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1621 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1629 void InsetText::validate(LaTeXFeatures & features) const
1631 for_each(paragraphs.begin(), paragraphs.end(),
1632 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1636 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1638 if (the_locking_inset) {
1639 the_locking_inset->getCursor(bv, x, y);
1643 y = cy(&bv) + InsetText::y();
1647 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1649 if (the_locking_inset) {
1650 the_locking_inset->getCursorPos(bv, x, y);
1653 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1654 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1658 int InsetText::insetInInsetY() const
1660 if (!the_locking_inset)
1663 return (inset_y + the_locking_inset->insetInInsetY());
1667 void InsetText::fitInsetCursor(BufferView * bv) const
1669 if (the_locking_inset) {
1670 the_locking_inset->fitInsetCursor(bv);
1673 LyXFont const font =
1674 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1676 int const asc = font_metrics::maxAscent(font);
1677 int const desc = font_metrics::maxDescent(font);
1679 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1680 need_update |= FULL;
1685 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1687 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1688 return moveLeftIntern(bv, false, activate_inset, selecting);
1690 return moveRightIntern(bv, true, activate_inset, selecting);
1695 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1697 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1698 return moveRightIntern(bv, true, activate_inset, selecting);
1700 return moveLeftIntern(bv, false, activate_inset, selecting);
1705 InsetText::moveRightIntern(BufferView * bv, bool front,
1706 bool activate_inset, bool selecting)
1708 LyXText * text = getLyXText(bv);
1710 ParagraphList::iterator c_par = cpar(bv);
1712 if (boost::next(c_par) == paragraphs.end() &&
1713 (cpos(bv) >= c_par->size()))
1714 return FINISHED_RIGHT;
1715 if (activate_inset && checkAndActivateInset(bv, front))
1717 text->cursorRight(bv);
1719 text->clearSelection();
1720 return DISPATCHED_NOUPDATE;
1725 InsetText::moveLeftIntern(BufferView * bv, bool front,
1726 bool activate_inset, bool selecting)
1728 LyXText * text = getLyXText(bv);
1730 if (cpar(bv) == paragraphs.begin() && (cpos(bv) <= 0))
1732 text->cursorLeft(bv);
1734 text->clearSelection();
1735 if (activate_inset && checkAndActivateInset(bv, front))
1737 return DISPATCHED_NOUPDATE;
1741 Inset::RESULT InsetText::moveUp(BufferView * bv)
1743 if (crow(bv) == getLyXText(bv)->rows().begin())
1745 getLyXText(bv)->cursorUp(bv);
1746 getLyXText(bv)->clearSelection();
1747 return DISPATCHED_NOUPDATE;
1751 Inset::RESULT InsetText::moveDown(BufferView * bv)
1753 if (boost::next(crow(bv)) == getLyXText(bv)->rows().end())
1754 return FINISHED_DOWN;
1755 getLyXText(bv)->cursorDown(bv);
1756 getLyXText(bv)->clearSelection();
1757 return DISPATCHED_NOUPDATE;
1761 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1763 if (the_locking_inset) {
1764 if (the_locking_inset->insetAllowed(inset))
1765 return the_locking_inset->insertInset(bv, inset);
1768 inset->setOwner(this);
1769 getLyXText(bv)->insertInset(inset);
1771 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1776 bool InsetText::insetAllowed(Inset::Code code) const
1778 // in_insetAllowed is a really gross hack,
1779 // to allow us to call the owner's insetAllowed
1780 // without stack overflow, which can happen
1781 // when the owner uses InsetCollapsable::insetAllowed()
1783 if (in_insetAllowed)
1785 in_insetAllowed = true;
1786 if (the_locking_inset)
1787 ret = the_locking_inset->insetAllowed(code);
1789 ret = owner()->insetAllowed(code);
1790 in_insetAllowed = false;
1795 UpdatableInset * InsetText::getLockingInset() const
1797 return the_locking_inset ? the_locking_inset->getLockingInset() :
1798 const_cast<InsetText *>(this);
1802 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1806 if (the_locking_inset)
1807 return the_locking_inset->getFirstLockingInsetOfType(c);
1812 bool InsetText::showInsetDialog(BufferView * bv) const
1814 if (the_locking_inset)
1815 return the_locking_inset->showInsetDialog(bv);
1820 vector<string> const InsetText::getLabelList() const
1822 vector<string> label_list;
1824 ParagraphList::iterator pit = paragraphs.begin();
1825 ParagraphList::iterator pend = paragraphs.end();
1826 for (; pit != pend; ++pit) {
1827 InsetList::iterator beg = pit->insetlist.begin();
1828 InsetList::iterator end = pit->insetlist.end();
1829 for (; beg != end; ++beg) {
1830 vector<string> const l = beg.getInset()->getLabelList();
1831 label_list.insert(label_list.end(), l.begin(), l.end());
1838 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1841 if (the_locking_inset) {
1842 the_locking_inset->setFont(bv, font, toggleall, selectall);
1845 if ((boost::next(paragraphs.begin()) == paragraphs.end() &&
1846 paragraphs.begin()->empty()) || cpar(bv)->empty()) {
1847 getLyXText(bv)->setFont(font, toggleall);
1852 lt = getLyXText(bv);
1855 if (lt->selection.set()) {
1856 setUndo(bv, Undo::EDIT, lt->cursor.par(), boost::next(lt->cursor.par()));
1860 lt->toggleFree(font, toggleall);
1862 lt->clearSelection();
1864 bool flag = (selectall || lt->selection.set());
1868 updateLocal(bv, FULL, true);
1870 updateLocal(bv, CURSOR_PAR, true);
1874 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1876 if (cpar(bv)->isInset(cpos(bv))) {
1878 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1879 if (!isHighlyEditableInset(inset))
1881 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1882 inset->localDispatch(cmd);
1883 if (!the_locking_inset)
1885 updateLocal(bv, CURSOR, false);
1892 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1893 mouse_button::state button)
1895 x -= drawTextXOffset;
1897 int dummyy = y + dim_.a;
1898 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1899 // we only do the edit() call if the inset was hit by the mouse
1900 // or if it is a highly editable inset. So we should call this
1901 // function from our own edit with button < 0.
1902 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1904 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1912 inset_x = cix(bv) - top_x + drawTextXOffset;
1913 inset_y = ciy(bv) + drawTextYOffset;
1914 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1915 inset->localDispatch(cmd);
1916 if (!the_locking_inset)
1918 updateLocal(bv, CURSOR, false);
1925 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1928 int w = UpdatableInset::getMaxWidth(bv, inset);
1933 w = w - top_x + owner()->x();
1936 w -= (2 * TEXT_TO_INSET_OFFSET);
1939 return UpdatableInset::getMaxWidth(bv, inset);
1944 void InsetText::setParagraphData(ParagraphList const & plist, bool same_id)
1946 // we have to unlock any locked inset otherwise we're in troubles
1947 the_locking_inset = 0;
1950 ParagraphList::iterator pit = plist.begin();
1951 ParagraphList::iterator pend = plist.end();
1952 for (; pit != pend; ++pit) {
1953 Paragraph * new_par = new Paragraph(*pit, same_id);
1954 new_par->setInsetOwner(this);
1955 paragraphs.push_back(new_par);
1963 void InsetText::markNew(bool track_changes)
1965 ParagraphList::iterator pit = paragraphs.begin();
1966 ParagraphList::iterator pend = paragraphs.end();
1967 for (; pit != pend; ++pit) {
1968 if (track_changes) {
1969 pit->trackChanges();
1971 // no-op when not tracking
1972 pit->cleanChanges();
1978 void InsetText::setText(string const & data, LyXFont const & font)
1981 for (unsigned int i = 0; i < data.length(); ++i)
1982 paragraphs.begin()->insertChar(i, data[i], font);
1987 void InsetText::setAutoBreakRows(bool flag)
1989 if (flag != autoBreakRows) {
1990 autoBreakRows = flag;
1998 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2000 if (how != drawFrame_) {
2003 updateLocal(bv, DRAW_FRAME, false);
2008 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2010 if (frame_color != col) {
2013 updateLocal(bv, DRAW_FRAME, false);
2018 int InsetText::cx(BufferView * bv) const
2020 // we do nothing dangerous so we use a local cache
2021 LyXText * llt = getLyXText(bv);
2022 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2023 if (the_locking_inset) {
2024 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2026 if (font.isVisibleRightToLeft())
2027 x -= the_locking_inset->width(bv, font);
2033 int InsetText::cix(BufferView * bv) const
2035 // we do nothing dangerous so we use a local cache
2036 LyXText * llt = getLyXText(bv);
2037 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2038 if (the_locking_inset) {
2039 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2041 if (font.isVisibleRightToLeft())
2042 x -= the_locking_inset->width(bv, font);
2048 int InsetText::cy(BufferView * bv) const
2051 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2055 int InsetText::ciy(BufferView * bv) const
2058 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2062 pos_type InsetText::cpos(BufferView * bv) const
2064 return getLyXText(bv)->cursor.pos();
2068 ParagraphList::iterator InsetText::cpar(BufferView * bv) const
2070 return getLyXText(bv)->cursor.par();
2074 bool InsetText::cboundary(BufferView * bv) const
2076 return getLyXText(bv)->cursor.boundary();
2080 RowList::iterator InsetText::crow(BufferView * bv) const
2082 return getLyXText(bv)->cursor.row();
2086 LyXText * InsetText::getLyXText(BufferView const * lbv,
2087 bool const recursive) const
2089 if (cached_bview == lbv) {
2090 if (recursive && the_locking_inset)
2091 return the_locking_inset->getLyXText(lbv, true);
2092 LyXText * lt = cached_text.get();
2093 lyx::Assert(lt && lt->rows().begin()->par() == paragraphs.begin());
2096 // Super UGLY! (Lgb)
2097 BufferView * bv = const_cast<BufferView *>(lbv);
2100 Cache::iterator it = cache.find(bv);
2102 if (it != cache.end()) {
2105 } else if (do_resize) {
2106 resizeLyXText(do_resize);
2108 if (lt || !it->second.remove) {
2109 lyx::Assert(it->second.text.get());
2110 cached_text = it->second.text;
2111 if (recursive && the_locking_inset) {
2112 return the_locking_inset->getLyXText(bv, true);
2114 return cached_text.get();
2115 } else if (it->second.remove) {
2117 saveLyXTextState(it->second.text.get());
2123 // when we have to reinit the existing LyXText!
2125 it->second.text->init(bv);
2126 restoreLyXTextState(it->second.text.get());
2127 it->second.remove = false;
2129 cached_text = it->second.text;
2130 if (the_locking_inset && recursive) {
2131 return the_locking_inset->getLyXText(bv);
2133 return cached_text.get();
2136 // we are here only if we don't have a BufferView * in the cache!!!
2138 cached_text.reset(new LyXText(bv, const_cast<InsetText *>(this)));
2139 cached_text->init(bv);
2140 restoreLyXTextState(cached_text.get());
2142 cache.insert(make_pair(bv, cached_text));
2144 if (the_locking_inset && recursive) {
2145 return the_locking_inset->getLyXText(bv);
2147 return cached_text.get();
2151 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2155 Cache::iterator it = cache.find(bv);
2157 if (it == cache.end()) {
2161 lyx::Assert(it->second.text.get());
2163 it->second.remove = true;
2165 /// then remove all LyXText in text-insets
2166 for_each(paragraphs.begin(), paragraphs.end(),
2167 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2172 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2175 // we cannot resize this because we are in use!
2176 // so do this on the next possible getLyXText()
2181 if (boost::next(paragraphs.begin()) == paragraphs.end() &&
2182 paragraphs.begin()->empty()) { // no data, resize not neccessary!
2183 // we have to do this as a fixed width may have changed!
2184 LyXText * t = getLyXText(bv);
2185 saveLyXTextState(t);
2187 restoreLyXTextState(t);
2190 // one endless line, resize normally not necessary
2191 if (!force && getMaxWidth(bv, this) < 0)
2194 Cache::iterator it = cache.find(bv);
2195 if (it == cache.end()) {
2198 lyx::Assert(it->second.text.get());
2200 LyXText * t = it->second.text.get();
2201 saveLyXTextState(t);
2203 for_each(paragraphs.begin(), paragraphs.end(),
2204 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2207 restoreLyXTextState(t);
2208 if (the_locking_inset) {
2209 inset_x = cix(bv) - top_x + drawTextXOffset;
2210 inset_y = ciy(bv) + drawTextYOffset;
2213 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2215 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2216 // this will scroll the screen such that the cursor becomes visible
2217 bv->updateScrollbar();
2219 need_update |= FULL;
2224 void InsetText::reinitLyXText() const
2227 // we cannot resize this because we are in use!
2228 // so do this on the next possible getLyXText()
2234 for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2235 lyx::Assert(it->second.text.get());
2237 LyXText * t = it->second.text.get();
2238 BufferView * bv = it->first;
2240 saveLyXTextState(t);
2242 for_each(paragraphs.begin(), paragraphs.end(),
2243 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2246 restoreLyXTextState(t);
2247 if (the_locking_inset) {
2248 inset_x = cix(bv) - top_x + drawTextXOffset;
2249 inset_y = ciy(bv) + drawTextYOffset;
2251 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2253 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2254 // this will scroll the screen such that the cursor becomes visible
2255 bv->updateScrollbar();
2263 void InsetText::removeNewlines()
2265 bool changed = false;
2267 ParagraphList::iterator it = paragraphs.begin();
2268 ParagraphList::iterator end = paragraphs.end();
2269 for (; it != end; ++it) {
2270 for (int i = 0; i < it->size(); ++i) {
2271 if (it->isNewline(i)) {
2282 bool InsetText::nodraw() const
2284 if (the_locking_inset)
2285 return the_locking_inset->nodraw();
2286 return UpdatableInset::nodraw();
2290 int InsetText::scroll(bool recursive) const
2292 int sx = UpdatableInset::scroll(false);
2294 if (recursive && the_locking_inset)
2295 sx += the_locking_inset->scroll(recursive);
2301 void InsetText::selectAll(BufferView * bv)
2303 getLyXText(bv)->cursorTop();
2304 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2305 getLyXText(bv)->cursorBottom();
2306 getLyXText(bv)->setSelection();
2310 void InsetText::clearSelection(BufferView * bv)
2312 getLyXText(bv)->clearSelection();
2316 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2318 Painter & pain = bv->painter();
2320 int h = dim_.a + dim_.d;
2321 int ty = baseline - dim_.a;
2327 if ((ty + h) > pain.paperHeight())
2328 h = pain.paperHeight();
2329 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2330 w = pain.paperWidth();
2331 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2336 ParagraphList * InsetText::getParagraphs(int i) const
2338 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2342 LyXCursor const & InsetText::cursor(BufferView * bv) const
2344 if (the_locking_inset)
2345 return the_locking_inset->cursor(bv);
2346 return getLyXText(bv)->cursor;
2350 Inset * InsetText::getInsetFromID(int id_arg) const
2353 return const_cast<InsetText *>(this);
2355 ParagraphList::iterator pit = paragraphs.begin();
2356 ParagraphList::iterator pend = paragraphs.end();
2357 for (; pit != pend; ++pit) {
2358 InsetList::iterator it = pit->insetlist.begin();
2359 InsetList::iterator end = pit->insetlist.end();
2360 for (; it != end; ++it) {
2361 if (it.getInset()->id() == id_arg)
2362 return it.getInset();
2363 Inset * in = it.getInset()->getInsetFromID(id_arg);
2373 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2374 float & value) const
2380 lt = getLyXText(bv);
2383 if (the_locking_inset) {
2384 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2385 if (!word.word().empty()) {
2391 // we have to go on checking so move cursor to the next char
2392 lt->cursor.pos(lt->cursor.pos() + 1);
2394 word = lt->selectNextWordToSpellcheck(value);
2395 if (word.word().empty())
2396 bv->unlockInset(const_cast<InsetText *>(this));
2405 void InsetText::selectSelectedWord(BufferView * bv)
2407 if (the_locking_inset) {
2408 the_locking_inset->selectSelectedWord(bv);
2411 getLyXText(bv)->selectSelectedWord();
2412 updateLocal(bv, SELECTION, false);
2416 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2418 if (the_locking_inset) {
2419 the_locking_inset->toggleSelection(bv, kill_selection);
2423 lt = getLyXText(bv);
2427 int x = top_x + TEXT_TO_INSET_OFFSET;
2429 RowList::iterator rowit = lt->rows().begin();
2430 RowList::iterator end = lt->rows().end();
2431 int y_offset = top_baseline - rowit->ascent_of_text();
2433 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2434 y += rowit->height();
2440 if (need_update & SELECTION)
2442 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2448 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2452 lt = getLyXText(bv);
2455 if (the_locking_inset) {
2456 if (the_locking_inset->nextChange(bv, length))
2458 lt->cursorRight(true);
2460 lyxfind::SearchResult result =
2461 lyxfind::findNextChange(bv, lt, length);
2463 if (result == lyxfind::SR_FOUND) {
2464 LyXCursor cur = lt->cursor;
2465 bv->unlockInset(bv->theLockingInset());
2466 if (bv->lockInset(this))
2469 lt->setSelectionRange(length);
2470 updateLocal(bv, SELECTION, false);
2474 return result != lyxfind::SR_NOT_FOUND;
2478 bool InsetText::searchForward(BufferView * bv, string const & str,
2483 lt = getLyXText(bv);
2486 if (the_locking_inset) {
2487 if (the_locking_inset->searchForward(bv, str, cs, mw))
2489 lt->cursorRight(true);
2491 lyxfind::SearchResult result =
2492 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2494 if (result == lyxfind::SR_FOUND) {
2495 LyXCursor cur = lt->cursor;
2496 bv->unlockInset(bv->theLockingInset());
2497 if (bv->lockInset(this))
2500 lt->setSelectionRange(str.length());
2501 updateLocal(bv, SELECTION, false);
2505 return (result != lyxfind::SR_NOT_FOUND);
2508 bool InsetText::searchBackward(BufferView * bv, string const & str,
2511 if (the_locking_inset) {
2512 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2517 lt = getLyXText(bv);
2521 ParagraphList::iterator pit = paragraphs.begin();
2522 ParagraphList::iterator pend = paragraphs.end();
2524 while (boost::next(pit) != pend)
2527 lt->setCursor(pit, pit->size());
2529 lyxfind::SearchResult result =
2530 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2532 if (result == lyxfind::SR_FOUND) {
2533 LyXCursor cur = lt->cursor;
2534 bv->unlockInset(bv->theLockingInset());
2535 if (bv->lockInset(this))
2538 lt->setSelectionRange(str.length());
2539 updateLocal(bv, SELECTION, false);
2543 return (result != lyxfind::SR_NOT_FOUND);
2547 bool InsetText::checkInsertChar(LyXFont & font)
2550 return owner()->checkInsertChar(font);
2555 void InsetText::collapseParagraphs(BufferView * bv)
2557 LyXText * llt = getLyXText(bv);
2559 while (boost::next(paragraphs.begin()) != paragraphs.end()) {
2560 if (!paragraphs.begin()->empty() &&
2561 !boost::next(paragraphs.begin())->empty() &&
2562 !paragraphs.begin()->isSeparator(paragraphs.begin()->size() - 1))
2564 paragraphs.begin()->insertChar(paragraphs.begin()->size(), ' ');
2566 if (llt->selection.set()) {
2567 if (llt->selection.start.par() == boost::next(paragraphs.begin())) {
2568 llt->selection.start.par(paragraphs.begin());
2569 llt->selection.start.pos(
2570 llt->selection.start.pos() + paragraphs.begin()->size());
2572 if (llt->selection.end.par() == boost::next(paragraphs.begin())) {
2573 llt->selection.end.par(paragraphs.begin());
2574 llt->selection.end.pos(
2575 llt->selection.end.pos() + paragraphs.begin()->size());
2578 mergeParagraph(bv->buffer()->params, paragraphs, paragraphs.begin());
2584 void InsetText::getDrawFont(LyXFont & font) const
2588 owner()->getDrawFont(font);
2592 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2594 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2595 // And it probably does. You have to take a look at this John. (Lgb)
2596 #warning John, have a look here. (Lgb)
2598 BufferParams const & bparams = buffer->params;
2600 Paragraph * tmpbuf = newpar;
2601 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2602 if (bparams.tracking_changes)
2603 buf->cleanChanges();
2605 while (tmpbuf->next()) {
2606 tmpbuf = tmpbuf->next();
2607 lastbuffer->next(new Paragraph(*tmpbuf, false));
2608 lastbuffer->next()->previous(lastbuffer);
2609 lastbuffer = lastbuffer->next();
2610 if (bparams.tracking_changes)
2611 lastbuffer->cleanChanges();
2614 lastbuffer = &*(paragraphs.begin());
2615 while (lastbuffer->next())
2616 lastbuffer = lastbuffer->next();
2617 if (!newpar->empty() && !lastbuffer->empty() &&
2618 !lastbuffer->isSeparator(lastbuffer->size() - 1))
2620 lastbuffer->insertChar(lastbuffer->size(), ' ');
2623 // make the buf exactly the same layout than our last paragraph
2624 buf->makeSameLayout(lastbuffer);
2627 lastbuffer->next(buf);
2628 buf->previous(lastbuffer);
2629 mergeParagraph(buffer->params, paragraphs, lastbuffer);
2631 ParagraphList::iterator pit = plist.begin();
2632 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(),
2633 new Paragraph(*pit, false));
2635 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2637 ParagraphList::iterator pend = plist.end();
2638 for (; pit != pend; ++pit) {
2639 paragraphs.push_back(new Paragraph(*pit, false));
2648 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2650 ParagraphList::iterator pit = paragraphs.begin();
2651 ParagraphList::iterator pend = paragraphs.end();
2653 for (; pit != pend; ++pit) {
2654 InsetList::iterator it = pit->insetlist.begin();
2655 InsetList::iterator end = pit->insetlist.end();
2656 for (; it != end; ++it) {
2657 it.getInset()->addPreview(loader);