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"
42 #include "metricsinfo.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 = const_cast<ParagraphList&>(paragraphs).begin();
83 ParagraphList::iterator end = const_cast<ParagraphList&>(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();
102 sstate.lpar = const_cast<ParagraphList&>(paragraphs).end();
107 void InsetText::restoreLyXTextState(LyXText * t) const
109 if (sstate.lpar == const_cast<ParagraphList&>(paragraphs).end())
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)
151 : UpdatableInset(in), lt(0), in_update(false), do_resize(0),
158 InsetText & InsetText::operator=(InsetText const & it)
165 void InsetText::init(InsetText const * ins)
168 setParagraphData(ins->paragraphs);
169 autoBreakRows = ins->autoBreakRows;
170 drawFrame_ = ins->drawFrame_;
171 frame_color = ins->frame_color;
173 for_each(paragraphs.begin(), paragraphs.end(),
174 boost::bind(&Paragraph::setInsetOwner, _1, this));
176 the_locking_inset = 0;
178 frame_color = LColor::insetframe;
179 autoBreakRows = false;
188 old_par = paragraphs.end();
189 last_drawn_width = -1;
191 sstate.lpar = paragraphs.end();
192 in_insetAllowed = false;
196 void InsetText::clear(bool just_mark_erased)
198 if (just_mark_erased) {
199 ParagraphList::iterator it = paragraphs.begin();
200 ParagraphList::iterator end = paragraphs.end();
201 for (; it != end; ++it) {
208 // This is a gross hack...
209 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
212 paragraphs.push_back(Paragraph());
213 paragraphs.begin()->setInsetOwner(this);
214 paragraphs.begin()->layout(old_layout);
221 InsetBase * InsetText::clone() const
223 return new InsetText(*this);
227 void InsetText::write(Buffer const * buf, ostream & os) const
230 writeParagraphData(buf, os);
234 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
236 ParagraphList::const_iterator it = paragraphs.begin();
237 ParagraphList::const_iterator end = paragraphs.end();
238 Paragraph::depth_type dth = 0;
239 for (; it != end; ++it) {
240 it->write(buf, os, buf->params, dth);
245 void InsetText::read(Buffer const * buf, LyXLex & lex)
248 Paragraph::depth_type depth = 0;
252 if (buf->params.tracking_changes)
253 paragraphs.begin()->trackChanges();
255 // delete the initial paragraph
257 ParagraphList::iterator pit = paragraphs.begin();
261 token = lex.getString();
264 if (token == "\\end_inset") {
268 if (token == "\\the_end") {
269 lex.printError("\\the_end read in inset! Error in document!");
274 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
277 pit = paragraphs.begin();
278 ParagraphList::iterator const end = paragraphs.end();
279 for (; pit != end; ++pit)
280 pit->setInsetOwner(this);
282 if (token != "\\end_inset") {
283 lex.printError("Missing \\end_inset at this point. "
290 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
292 BufferView * bv = mi.base.bv;
293 LyXText * text = getLyXText(bv);
294 dim.asc = text->rows().begin()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
295 dim.des = text->height - dim.asc + TEXT_TO_INSET_OFFSET;
296 dim.wid = max(textWidth(bv), int(text->width)) + 2 * TEXT_TO_INSET_OFFSET;
297 dim.wid = max(dim.wid, 10);
302 int InsetText::textWidth(BufferView * bv, bool fordraw) const
304 int w = autoBreakRows ? getMaxWidth(bv, this) : -1;
307 return max(w - 2 * TEXT_TO_INSET_OFFSET,
308 (int)getLyXText(bv)->width);
313 return w - 2 * TEXT_TO_INSET_OFFSET;
317 void InsetText::draw(PainterInfo & pi, int x, int baseline) const
322 // update our idea of where we are. Clearly, we should
323 // not have to know this information.
327 int const start_x = x;
329 BufferView * bv = pi.base.bv;
330 Painter & pain = pi.pain;
332 // repaint the background if needed
333 if (backgroundColor() != LColor::background)
334 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
336 // no draw is necessary !!!
337 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
338 top_baseline = baseline;
346 top_baseline = baseline;
347 top_y = baseline - dim_.asc;
349 if (last_drawn_width != dim_.wid) {
351 last_drawn_width = dim_.wid;
354 if (the_locking_inset && (cpar(bv) == inset_par)
355 && (cpos(bv) == inset_pos)) {
356 inset_x = cix(bv) - int(x) + drawTextXOffset;
357 inset_y = ciy(bv) + drawTextYOffset;
365 x += TEXT_TO_INSET_OFFSET;
367 RowList::iterator rowit = lt->rows().begin();
368 RowList::iterator end = lt->rows().end();
370 int y_offset = baseline - rowit->ascent_of_text();
371 int ph = pain.paperHeight();
374 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
375 y += rowit->height();
376 first += rowit->height();
380 lt->top_y(-y_offset);
388 int yf = y_offset + first;
393 while ((rowit != end) && (yf < ph)) {
394 RowPainter rp(*bv, *lt, rowit);
395 rp.paint(y + y_offset + first, int(x), y + lt->top_y());
396 y += rowit->height();
397 yf += rowit->height();
403 if ((drawFrame_ == ALWAYS) || (drawFrame_ == LOCKED && locked)) {
404 drawFrame(pain, int(start_x));
407 x += dim_.wid - TEXT_TO_INSET_OFFSET;
409 if (need_update != INIT) {
417 void InsetText::drawFrame(Painter & pain, int x) const
419 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
421 frame_y = top_baseline - dim_.asc + ttoD2;
422 frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
423 frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
424 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
429 void InsetText::update(BufferView * bv, bool reinit)
432 if (reinit && owner()) {
434 owner()->update(bv, true);
440 if (reinit || need_update == INIT) {
442 // we should put this call where we set need_update to INIT!
445 owner()->update(bv, true);
448 int nw = getMaxWidth(bv, this);
449 if (nw > 0 && old_max_width != nw) {
456 if (!autoBreakRows && paragraphs.size() > 1)
457 collapseParagraphs(bv);
459 if (the_locking_inset) {
460 inset_x = cix(bv) - top_x + drawTextXOffset;
461 inset_y = ciy(bv) + drawTextYOffset;
462 the_locking_inset->update(bv, reinit);
470 if ((need_update & CURSOR_PAR) && (lt->refreshStatus() == LyXText::REFRESH_NONE) &&
472 lt->updateInset(the_locking_inset);
475 if (lt->refreshStatus() == LyXText::REFRESH_AREA)
482 int nw = getMaxWidth(bv, this);
483 if (nw > 0 && old_max_width != nw) {
490 void InsetText::setUpdateStatus(BufferView * bv, int what) const
492 // this does nothing dangerous so use only a localized buffer
493 LyXText * llt = getLyXText(bv);
496 // we have to redraw us full if our LyXText REFRESH_AREA or
497 // if we don't break row so that we only have one row to update!
498 if ((llt->refreshStatus() == LyXText::REFRESH_AREA) ||
500 (llt->refreshStatus() == LyXText::REFRESH_ROW)))
503 } else if (llt->refreshStatus() == LyXText::REFRESH_ROW) {
504 need_update |= CURSOR_PAR;
507 // this to not draw a selection when we redraw all of it!
508 if (need_update & CURSOR && !(need_update & SELECTION)) {
509 if (llt->selection.set())
511 llt->clearSelection();
516 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
518 if (!autoBreakRows && paragraphs.size() > 1)
519 collapseParagraphs(bv);
527 setUpdateStatus(bv, what);
528 bool flag = mark_dirty ||
529 (((need_update != CURSOR) && (need_update != NONE)) ||
530 (lt->refreshStatus() != LyXText::REFRESH_NONE) || lt->selection.set());
531 if (!lt->selection.set())
532 lt->selection.cursor = lt->cursor;
538 bv->updateInset(const_cast<InsetText *>(this));
544 if (need_update == CURSOR)
546 bv->owner()->view_state_changed();
547 bv->owner()->updateMenubar();
548 bv->owner()->updateToolbar();
549 if (old_par != cpar(bv)) {
550 bv->owner()->setLayout(cpar(bv)->layout()->name());
556 string const InsetText::editMessage() const
558 return _("Opened Text Inset");
562 void InsetText::insetUnlock(BufferView * bv)
564 if (the_locking_inset) {
565 the_locking_inset->insetUnlock(bv);
566 the_locking_inset = 0;
567 updateLocal(bv, CURSOR_PAR, false);
578 if (lt->selection.set()) {
579 lt->clearSelection();
581 } else if (owner()) {
582 bv->owner()->setLayout(owner()->getLyXText(bv)
583 ->cursor.par()->layout()->name());
585 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
586 // hack for deleteEmptyParMech
587 ParagraphList::iterator first_par = paragraphs.begin();
588 if (!first_par->empty()) {
589 lt->setCursor(first_par, 0);
590 } else if (paragraphs.size() > 1) {
591 lt->setCursor(boost::next(first_par), 0);
596 updateLocal(bv, code, false);
599 setUpdateStatus(bv, code);
604 void InsetText::lockInset(BufferView * bv)
607 the_locking_inset = 0;
608 inset_pos = inset_x = inset_y = 0;
609 inset_boundary = false;
610 inset_par = paragraphs.end();
611 old_par = paragraphs.end();
617 lt->setCursor(paragraphs.begin(), 0);
618 lt->clearSelection();
620 // If the inset is empty set the language of the current font to the
621 // language to the surronding text (if different).
622 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
623 bv->getParentLanguage(this) != lt->current_font.language()) {
624 LyXFont font(LyXFont::ALL_IGNORE);
625 font.setLanguage(bv->getParentLanguage(this));
626 setFont(bv, font, false);
631 if (drawFrame_ == LOCKED)
632 code = CURSOR|DRAW_FRAME;
633 setUpdateStatus(bv, code);
637 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
639 the_locking_inset = inset;
640 inset_x = cix(bv) - top_x + drawTextXOffset;
641 inset_y = ciy(bv) + drawTextYOffset;
642 inset_pos = cpos(bv);
643 inset_par = cpar(bv);
644 inset_boundary = cboundary(bv);
645 updateLocal(bv, CURSOR, false);
649 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
651 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
655 if (!the_locking_inset) {
656 ParagraphList::iterator pit = paragraphs.begin();
657 ParagraphList::iterator pend = paragraphs.end();
659 int const id = inset->id();
660 for (; pit != pend; ++pit) {
661 InsetList::iterator it =
662 pit->insetlist.begin();
663 InsetList::iterator const end =
664 pit->insetlist.end();
665 for (; it != end; ++it) {
666 if (it->inset == inset) {
667 getLyXText(bv)->setCursorIntern(pit, it->pos);
668 lockInset(bv, inset);
671 if (it->inset->getInsetFromID(id)) {
672 getLyXText(bv)->setCursorIntern(pit, it->pos);
673 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
674 return the_locking_inset->lockInsetInInset(bv, inset);
680 if (inset == cpar(bv)->getInset(cpos(bv))) {
681 lyxerr[Debug::INSETS] << "OK" << endl;
682 lockInset(bv, inset);
684 } else if (the_locking_inset && (the_locking_inset == inset)) {
685 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
686 lyxerr[Debug::INSETS] << "OK" << endl;
687 inset_x = cix(bv) - top_x + drawTextXOffset;
688 inset_y = ciy(bv) + drawTextYOffset;
690 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
692 } else if (the_locking_inset) {
693 lyxerr[Debug::INSETS] << "MAYBE" << endl;
694 return the_locking_inset->lockInsetInInset(bv, inset);
696 lyxerr[Debug::INSETS] << "NOT OK" << endl;
701 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
704 if (!the_locking_inset)
706 if (the_locking_inset == inset) {
707 the_locking_inset->insetUnlock(bv);
708 getLyXText(bv)->updateInset(inset);
709 the_locking_inset = 0;
711 moveRightIntern(bv, true, false);
712 old_par = paragraphs.end(); // force layout setting
716 updateLocal(bv, CURSOR, false);
719 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
723 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
725 if (!autoBreakRows && paragraphs.size() > 1)
726 collapseParagraphs(bv);
736 if (inset->owner() != this) {
737 int ustat = CURSOR_PAR;
739 UpdatableInset * tl_inset = the_locking_inset;
741 found = tl_inset->updateInsetInInset(bv, inset);
743 tl_inset = static_cast<UpdatableInset *>(inset);
744 while(tl_inset->owner() && tl_inset->owner() != this)
745 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
746 if (!tl_inset->owner())
748 found = tl_inset->updateInsetInInset(bv, inset);
752 lt->updateInset(tl_inset);
756 setUpdateStatus(bv, ustat);
759 bool found = lt->updateInset(inset);
763 setUpdateStatus(bv, CURSOR_PAR);
764 if (the_locking_inset &&
765 cpar(bv) == inset_par && cpos(bv) == inset_pos)
767 inset_x = cix(bv) - top_x + drawTextXOffset;
768 inset_y = ciy(bv) + drawTextYOffset;
775 void InsetText::lfunMousePress(FuncRequest const & cmd)
779 // use this to check mouse motion for selection!
783 BufferView * bv = cmd.view();
784 FuncRequest cmd1 = cmd;
790 int tmp_x = cmd.x - drawTextXOffset;
791 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
792 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
794 if (the_locking_inset) {
795 if (the_locking_inset == inset) {
796 the_locking_inset->localDispatch(cmd1);
799 // otherwise only unlock the_locking_inset
800 the_locking_inset->insetUnlock(bv);
801 the_locking_inset = 0;
804 no_selection = false;
806 if (bv->theLockingInset()) {
807 if (isHighlyEditableInset(inset)) {
808 // We just have to lock the inset before calling a
810 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
811 if (!bv->lockInset(uinset)) {
812 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
814 inset->localDispatch(cmd1);
815 if (the_locking_inset)
816 updateLocal(bv, CURSOR, false);
821 bool paste_internally = false;
822 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
823 localDispatch(FuncRequest(bv, LFUN_COPY));
824 paste_internally = true;
831 int old_top_y = lt->top_y();
833 lt->setCursorFromCoordinates(cmd.x - drawTextXOffset,
835 // set the selection cursor!
836 lt->selection.cursor = lt->cursor;
837 lt->cursor.x_fix(lt->cursor.x());
839 if (lt->selection.set()) {
840 lt->clearSelection();
841 updateLocal(bv, FULL, false);
843 lt->clearSelection();
844 updateLocal(bv, CURSOR, false);
850 bv->owner()->setLayout(cpar(bv)->layout()->name());
852 // we moved the view we cannot do mouse selection in this case!
853 if (getLyXText(bv)->top_y() != old_top_y)
856 // Insert primary selection with middle mouse
857 // if there is a local selection in the current buffer,
859 if (cmd.button() == mouse_button::button2) {
860 if (paste_internally)
861 localDispatch(FuncRequest(bv, LFUN_PASTE));
863 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
866 getLyXText(bv)->clearSelection();
871 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
873 BufferView * bv = cmd.view();
874 FuncRequest cmd1 = cmd;
879 if (the_locking_inset)
880 return the_locking_inset->localDispatch(cmd1);
882 int tmp_x = cmd.x - drawTextXOffset;
883 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
884 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
887 // This code should probably be removed now. Simple insets
888 // (!highlyEditable) can actually take the localDispatch,
889 // and turn it into edit() if necessary. But we still
890 // need to deal properly with the whole relative vs.
891 // absolute mouse co-ords thing in a realiable, sensible way
893 if (isHighlyEditableInset(inset))
894 ret = inset->localDispatch(cmd1);
896 inset_x = cix(bv) - top_x + drawTextXOffset;
897 inset_y = ciy(bv) + drawTextYOffset;
898 cmd1.x = cmd.x - inset_x;
899 cmd1.y = cmd.x - inset_y;
900 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
904 ret = inset->localDispatch(cmd1);
905 updateLocal(bv, CURSOR_PAR, false);
912 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
914 FuncRequest cmd1 = cmd;
918 if (the_locking_inset) {
919 the_locking_inset->localDispatch(cmd1);
923 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
926 BufferView * bv = cmd.view();
932 LyXCursor cur = lt->cursor;
933 lt->setCursorFromCoordinates
934 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
935 lt->cursor.x_fix(lt->cursor.x());
936 if (cur == lt->cursor) {
942 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
943 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
947 updateLocal(bv, SELECTION, false);
952 Inset::RESULT InsetText::localDispatch(FuncRequest const & cmd)
954 BufferView * bv = cmd.view();
956 if (cmd.action == LFUN_INSET_EDIT) {
957 UpdatableInset::localDispatch(cmd);
959 if (!bv->lockInset(this)) {
960 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
965 the_locking_inset = 0;
966 inset_pos = inset_x = inset_y = 0;
967 inset_boundary = false;
968 inset_par = paragraphs.end();
969 old_par = paragraphs.end();
977 if (cmd.argument.size()) {
978 if (cmd.argument == "left")
979 lt->setCursor(paragraphs.begin(), 0);
981 ParagraphList::iterator it = paragraphs.begin();
982 ParagraphList::iterator end = paragraphs.end();
983 while (boost::next(it) != end)
985 // int const pos = (p->size() ? p->size()-1 : p->size());
986 lt->setCursor(it, it->size());
989 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
990 // we put here -1 and not button as now the button in the
991 // edit call should not be needed we will fix this in 1.3.x
992 // cycle hopefully (Jug 20020509)
993 // FIXME: GUII I've changed this to none: probably WRONG
994 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
995 lt->setCursorFromCoordinates(cmd.x - drawTextXOffset,
997 lt->cursor.x_fix(lt->cursor.x());
1001 lt->clearSelection();
1004 // If the inset is empty set the language of the current font to the
1005 // language to the surronding text (if different).
1006 if (paragraphs.begin()->empty() &&
1007 paragraphs.size() == 1 &&
1008 bv->getParentLanguage(this) != lt->current_font.language())
1010 LyXFont font(LyXFont::ALL_IGNORE);
1011 font.setLanguage(bv->getParentLanguage(this));
1012 setFont(bv, font, false);
1018 if (drawFrame_ == LOCKED)
1019 code = CURSOR | DRAW_FRAME;
1021 updateLocal(bv, code, false);
1022 // Tell the paragraph dialog that we've entered an insettext.
1023 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1028 switch (cmd.action) {
1029 case LFUN_MOUSE_PRESS:
1030 lfunMousePress(cmd);
1032 case LFUN_MOUSE_MOTION:
1033 lfunMouseMotion(cmd);
1035 case LFUN_MOUSE_RELEASE:
1036 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
1041 bool was_empty = (paragraphs.begin()->empty() &&
1042 paragraphs.size() == 1);
1044 no_selection = false;
1045 RESULT result = UpdatableInset::localDispatch(cmd);
1046 if (result != UNDISPATCHED)
1049 result = DISPATCHED;
1050 if (cmd.action < 0 && cmd.argument.empty())
1053 if (the_locking_inset) {
1054 result = the_locking_inset->localDispatch(cmd);
1055 if (result == DISPATCHED_NOUPDATE)
1057 else if (result == DISPATCHED) {
1058 updateLocal(bv, CURSOR_PAR, false);
1060 } else if (result >= FINISHED) {
1062 case FINISHED_RIGHT:
1063 moveRightIntern(bv, false, false);
1064 result = DISPATCHED;
1067 if ((result = moveUp(bv)) >= FINISHED) {
1068 updateLocal(bv, CURSOR, false);
1069 bv->unlockInset(this);
1073 if ((result = moveDown(bv)) >= FINISHED) {
1074 updateLocal(bv, CURSOR, false);
1075 bv->unlockInset(this);
1079 result = DISPATCHED;
1082 the_locking_inset = 0;
1083 updateLocal(bv, CURSOR, false);
1084 // make sure status gets reset immediately
1085 bv->owner()->clearMessage();
1091 lt = getLyXText(bv);
1095 int updflag = false;
1097 // what type of update to do on a cursor movement
1098 int cursor_update = CURSOR;
1100 if (lt->selection.set())
1101 cursor_update = SELECTION;
1103 switch (cmd.action) {
1106 case LFUN_SELFINSERT:
1107 if (bv->buffer()->isReadonly()) {
1108 // setErrorMessage(N_("Document is read only"));
1111 if (!cmd.argument.empty()) {
1112 /* Automatically delete the currently selected
1113 * text and replace it with what is being
1114 * typed in now. Depends on lyxrc settings
1115 * "auto_region_delete", which defaults to
1118 // This should not be needed here and is also WRONG!
1119 setUndo(bv, Undo::INSERT, lt->cursor.par());
1122 if (lyxrc.auto_region_delete) {
1123 if (lt->selection.set()) {
1124 lt->cutSelection(false, false);
1127 lt->clearSelection();
1128 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
1129 bv->owner()->getIntl().getTransManager().
1130 TranslateAndInsert(cmd.argument[i], lt);
1133 lt->selection.cursor = lt->cursor;
1134 updwhat = CURSOR | CURSOR_PAR;
1136 result = DISPATCHED_NOUPDATE;
1139 // cursor movements that need special handling
1142 result = moveRight(bv);
1144 updwhat = cursor_update;
1148 result = moveLeft(bv);
1149 updwhat = cursor_update;
1153 result = moveDown(bv);
1154 updwhat = cursor_update;
1158 result = moveUp(bv);
1159 updwhat = cursor_update;
1163 if (crow(bv) == lt->rows().begin())
1164 result = FINISHED_UP;
1166 lt->cursorPrevious();
1167 lt->clearSelection();
1168 result = DISPATCHED_NOUPDATE;
1170 updwhat = cursor_update;
1174 if (boost::next(crow(bv)) == lt->rows().end())
1175 result = FINISHED_DOWN;
1178 lt->clearSelection();
1179 result = DISPATCHED_NOUPDATE;
1181 updwhat = cursor_update;
1184 case LFUN_BACKSPACE: {
1185 if (lt->selection.set())
1186 lt->cutSelection(true, false);
1189 updwhat = CURSOR_PAR;
1195 if (lt->selection.set()) {
1196 lt->cutSelection(true, false);
1200 updwhat = CURSOR_PAR;
1206 lt->cutSelection(true, true);
1207 updwhat = CURSOR_PAR;
1214 lt->copySelection();
1215 updwhat = CURSOR_PAR;
1218 case LFUN_PASTESELECTION:
1220 string const clip(bv->getClipboard());
1224 if (cmd.argument == "paragraph") {
1225 lt->insertStringAsParagraphs(clip);
1227 lt->insertStringAsLines(clip);
1230 lt->clearSelection();
1232 updwhat = CURSOR_PAR;
1238 if (!autoBreakRows) {
1239 if (CutAndPaste::nrOfParagraphs() > 1) {
1240 #ifdef WITH_WARNINGS
1241 #warning FIXME horrendously bad UI
1243 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1248 size_t sel_index = 0;
1249 string const & arg = cmd.argument;
1250 if (isStrUnsignedInt(arg)) {
1251 size_t const paste_arg = strToUnsignedInt(arg);
1252 #warning FIXME Check if the arg is in the domain of available selections.
1253 sel_index = paste_arg;
1255 lt->pasteSelection(sel_index);
1257 lt->clearSelection();
1258 updwhat = CURSOR_PAR;
1263 case LFUN_BREAKPARAGRAPH:
1264 if (!autoBreakRows) {
1265 result = DISPATCHED;
1268 lt->breakParagraph(paragraphs, 0);
1269 updwhat = CURSOR | FULL;
1273 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1274 if (!autoBreakRows) {
1275 result = DISPATCHED;
1278 lt->breakParagraph(paragraphs, 1);
1279 updwhat = CURSOR | FULL;
1283 case LFUN_BREAKLINE: {
1284 if (!autoBreakRows) {
1285 result = DISPATCHED;
1289 lt->insertInset(new InsetNewline);
1290 updwhat = CURSOR | CURSOR_PAR;
1296 // do not set layouts on non breakable textinsets
1297 if (autoBreakRows) {
1298 string cur_layout = cpar(bv)->layout()->name();
1300 // Derive layout number from given argument (string)
1301 // and current buffer's textclass (number). */
1302 LyXTextClass const & tclass =
1303 bv->buffer()->params.getLyXTextClass();
1304 string layout = cmd.argument;
1305 bool hasLayout = tclass.hasLayout(layout);
1307 // If the entry is obsolete, use the new one instead.
1309 string const & obs =
1310 tclass[layout]->obsoleted_by();
1315 // see if we found the layout number:
1317 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1318 bv->owner()->dispatch(lf);
1322 if (cur_layout != layout) {
1323 cur_layout = layout;
1324 lt->setLayout(layout);
1325 bv->owner()->setLayout(cpar(bv)->layout()->name());
1326 updwhat = CURSOR_PAR;
1330 // reset the layout box
1331 bv->owner()->setLayout(cpar(bv)->layout()->name());
1334 case LFUN_PARAGRAPH_SPACING:
1335 // This one is absolutely not working. When fiddling with this
1336 // it also seems to me that the paragraphs inside the insettext
1337 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1338 // FIXME: how old is this comment ? ...
1340 ParagraphList::iterator pit = lt->cursor.par();
1341 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1342 float cur_value = 1.0;
1343 if (cur_spacing == Spacing::Other) {
1344 cur_value = pit->params().spacing().getValue();
1347 istringstream istr(STRCONV(cmd.argument));
1350 Spacing::Space new_spacing = cur_spacing;
1351 float new_value = cur_value;
1353 lyxerr << "Missing argument to `paragraph-spacing'"
1355 } else if (tmp == "single") {
1356 new_spacing = Spacing::Single;
1357 } else if (tmp == "onehalf") {
1358 new_spacing = Spacing::Onehalf;
1359 } else if (tmp == "double") {
1360 new_spacing = Spacing::Double;
1361 } else if (tmp == "other") {
1362 new_spacing = Spacing::Other;
1365 lyxerr << "new_value = " << tmpval << endl;
1368 } else if (tmp == "default") {
1369 new_spacing = Spacing::Default;
1371 lyxerr << _("Unknown spacing argument: ")
1372 << cmd.argument << endl;
1374 if (cur_spacing != new_spacing || cur_value != new_value) {
1375 pit->params().spacing(Spacing(new_spacing, new_value));
1376 updwhat = CURSOR_PAR;
1382 // These need to do repaints but don't require
1383 // special handling otherwise. A *lot* of the
1384 // above could probably be done similarly ...
1389 case LFUN_WORDRIGHT:
1390 // these two are really unhandled ...
1392 case LFUN_BEGINNINGBUF:
1393 updwhat = cursor_update;
1394 if (!bv->dispatch(cmd))
1395 result = UNDISPATCHED;
1404 case LFUN_WORDLEFTSEL:
1405 case LFUN_WORDRIGHTSEL:
1406 updwhat = SELECTION;
1411 if (!bv->dispatch(cmd))
1412 result = UNDISPATCHED;
1419 updateLocal(bv, updwhat, updflag);
1420 /// If the action has deleted all text in the inset, we need to change the
1421 // language to the language of the surronding text.
1422 if (!was_empty && paragraphs.begin()->empty() &&
1423 paragraphs.size() == 1) {
1424 LyXFont font(LyXFont::ALL_IGNORE);
1425 font.setLanguage(bv->getParentLanguage(this));
1426 setFont(bv, font, false);
1429 if (result >= FINISHED)
1430 bv->unlockInset(this);
1432 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1433 result = DISPATCHED;
1438 int InsetText::latex(Buffer const * buf, ostream & os,
1439 LatexRunParams const & runparams) const
1442 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1443 return texrow.rows();
1447 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1449 unsigned int lines = 0;
1451 ParagraphList::const_iterator beg = paragraphs.begin();
1452 ParagraphList::const_iterator end = paragraphs.end();
1453 ParagraphList::const_iterator it = beg;
1454 for (; it != end; ++it) {
1455 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1456 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1463 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1465 unsigned int lines = 0;
1467 vector<string> environment_stack(10);
1468 vector<string> environment_inner(10);
1470 int const command_depth = 0;
1473 Paragraph::depth_type depth = 0; // paragraph depth
1475 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1476 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1478 for (; pit != pend; ++pit) {
1480 int desc_on = 0; // description mode
1482 LyXLayout_ptr const & style = pit->layout();
1484 // environment tag closing
1485 for (; depth > pit->params().depth(); --depth) {
1486 if (environment_inner[depth] != "!-- --") {
1487 item_name = "listitem";
1488 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1489 if (environment_inner[depth] == "varlistentry")
1490 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1492 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1493 environment_stack[depth].erase();
1494 environment_inner[depth].erase();
1497 if (depth == pit->params().depth()
1498 && environment_stack[depth] != style->latexname()
1499 && !environment_stack[depth].empty()) {
1500 if (environment_inner[depth] != "!-- --") {
1501 item_name= "listitem";
1502 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1503 if (environment_inner[depth] == "varlistentry")
1504 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1507 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1509 environment_stack[depth].erase();
1510 environment_inner[depth].erase();
1513 // Write opening SGML tags.
1514 switch (style->latextype) {
1515 case LATEX_PARAGRAPH:
1516 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1520 buf->sgmlError(pit, 0, _("Error: LatexType Command not allowed here.\n"));
1524 case LATEX_ENVIRONMENT:
1525 case LATEX_ITEM_ENVIRONMENT:
1526 if (depth < pit->params().depth()) {
1527 depth = pit->params().depth();
1528 environment_stack[depth].erase();
1531 if (environment_stack[depth] != style->latexname()) {
1532 if (environment_stack.size() == depth + 1) {
1533 environment_stack.push_back("!-- --");
1534 environment_inner.push_back("!-- --");
1536 environment_stack[depth] = style->latexname();
1537 environment_inner[depth] = "!-- --";
1538 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1540 if (environment_inner[depth] != "!-- --") {
1541 item_name= "listitem";
1542 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1543 if (environment_inner[depth] == "varlistentry")
1544 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1548 if (style->latextype == LATEX_ENVIRONMENT) {
1549 if (!style->latexparam().empty()) {
1550 if (style->latexparam() == "CDATA")
1553 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1558 desc_on = (style->labeltype == LABEL_MANUAL);
1560 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1561 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1563 item_name = desc_on ? "term" : "para";
1564 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1568 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1572 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1575 // write closing SGML tags
1576 switch (style->latextype) {
1577 case LATEX_ENVIRONMENT:
1578 if (!style->latexparam().empty()) {
1579 if (style->latexparam() == "CDATA")
1582 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1585 case LATEX_ITEM_ENVIRONMENT:
1586 if (desc_on == 1) break;
1588 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1590 case LATEX_PARAGRAPH:
1591 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1594 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1600 for (int d = depth; d >= 0; --d) {
1601 if (!environment_stack[depth].empty()) {
1602 if (environment_inner[depth] != "!-- --") {
1603 item_name = "listitem";
1604 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1605 if (environment_inner[depth] == "varlistentry")
1606 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1609 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1617 void InsetText::validate(LaTeXFeatures & features) const
1619 for_each(paragraphs.begin(), paragraphs.end(),
1620 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1624 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1626 if (the_locking_inset) {
1627 the_locking_inset->getCursor(bv, x, y);
1631 y = cy(&bv) + InsetText::y();
1635 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1637 if (the_locking_inset) {
1638 the_locking_inset->getCursorPos(bv, x, y);
1641 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1642 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1646 int InsetText::insetInInsetY() const
1648 if (!the_locking_inset)
1651 return (inset_y + the_locking_inset->insetInInsetY());
1655 void InsetText::fitInsetCursor(BufferView * bv) const
1657 if (the_locking_inset) {
1658 the_locking_inset->fitInsetCursor(bv);
1661 LyXFont const font =
1662 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1664 int const asc = font_metrics::maxAscent(font);
1665 int const desc = font_metrics::maxDescent(font);
1667 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1668 need_update |= FULL;
1673 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1675 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1676 return moveLeftIntern(bv, false, activate_inset, selecting);
1678 return moveRightIntern(bv, true, activate_inset, selecting);
1683 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1685 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1686 return moveRightIntern(bv, true, activate_inset, selecting);
1688 return moveLeftIntern(bv, false, activate_inset, selecting);
1693 InsetText::moveRightIntern(BufferView * bv, bool front,
1694 bool activate_inset, bool selecting)
1696 LyXText * text = getLyXText(bv);
1698 ParagraphList::iterator c_par = cpar(bv);
1700 if (boost::next(c_par) == paragraphs.end() &&
1701 (cpos(bv) >= c_par->size()))
1702 return FINISHED_RIGHT;
1703 if (activate_inset && checkAndActivateInset(bv, front))
1705 text->cursorRight(bv);
1707 text->clearSelection();
1708 return DISPATCHED_NOUPDATE;
1713 InsetText::moveLeftIntern(BufferView * bv, bool front,
1714 bool activate_inset, bool selecting)
1716 LyXText * text = getLyXText(bv);
1718 if (cpar(bv) == paragraphs.begin() && (cpos(bv) <= 0))
1720 text->cursorLeft(bv);
1722 text->clearSelection();
1723 if (activate_inset && checkAndActivateInset(bv, front))
1725 return DISPATCHED_NOUPDATE;
1729 Inset::RESULT InsetText::moveUp(BufferView * bv)
1731 if (crow(bv) == getLyXText(bv)->rows().begin())
1733 getLyXText(bv)->cursorUp(bv);
1734 getLyXText(bv)->clearSelection();
1735 return DISPATCHED_NOUPDATE;
1739 Inset::RESULT InsetText::moveDown(BufferView * bv)
1741 if (boost::next(crow(bv)) == getLyXText(bv)->rows().end())
1742 return FINISHED_DOWN;
1743 getLyXText(bv)->cursorDown(bv);
1744 getLyXText(bv)->clearSelection();
1745 return DISPATCHED_NOUPDATE;
1749 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1751 if (the_locking_inset) {
1752 if (the_locking_inset->insetAllowed(inset))
1753 return the_locking_inset->insertInset(bv, inset);
1756 inset->setOwner(this);
1757 getLyXText(bv)->insertInset(inset);
1759 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1764 bool InsetText::insetAllowed(Inset::Code code) const
1766 // in_insetAllowed is a really gross hack,
1767 // to allow us to call the owner's insetAllowed
1768 // without stack overflow, which can happen
1769 // when the owner uses InsetCollapsable::insetAllowed()
1771 if (in_insetAllowed)
1773 in_insetAllowed = true;
1774 if (the_locking_inset)
1775 ret = the_locking_inset->insetAllowed(code);
1777 ret = owner()->insetAllowed(code);
1778 in_insetAllowed = false;
1783 UpdatableInset * InsetText::getLockingInset() const
1785 return the_locking_inset ? the_locking_inset->getLockingInset() :
1786 const_cast<InsetText *>(this);
1790 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1794 if (the_locking_inset)
1795 return the_locking_inset->getFirstLockingInsetOfType(c);
1800 bool InsetText::showInsetDialog(BufferView * bv) const
1802 if (the_locking_inset)
1803 return the_locking_inset->showInsetDialog(bv);
1808 void InsetText::getLabelList(std::vector<string> & list) const
1810 ParagraphList::const_iterator pit = paragraphs.begin();
1811 ParagraphList::const_iterator pend = paragraphs.end();
1812 for (; pit != pend; ++pit) {
1813 InsetList::const_iterator beg = pit->insetlist.begin();
1814 InsetList::const_iterator end = pit->insetlist.end();
1815 for (; beg != end; ++beg)
1816 beg->inset->getLabelList(list);
1821 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1824 if (the_locking_inset) {
1825 the_locking_inset->setFont(bv, font, toggleall, selectall);
1829 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1830 || cpar(bv)->empty()) {
1831 getLyXText(bv)->setFont(font, toggleall);
1837 lt = getLyXText(bv);
1841 if (lt->selection.set()) {
1842 setUndo(bv, Undo::EDIT, lt->cursor.par());
1848 lt->toggleFree(font, toggleall);
1851 lt->clearSelection();
1855 bool flag = (selectall || lt->selection.set());
1861 updateLocal(bv, FULL, true);
1863 updateLocal(bv, CURSOR_PAR, true);
1867 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1869 if (cpar(bv)->isInset(cpos(bv))) {
1871 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1872 if (!isHighlyEditableInset(inset))
1874 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1875 inset->localDispatch(cmd);
1876 if (!the_locking_inset)
1878 updateLocal(bv, CURSOR, false);
1885 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1886 mouse_button::state button)
1888 x -= drawTextXOffset;
1890 int dummyy = y + dim_.asc;
1891 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1892 // we only do the edit() call if the inset was hit by the mouse
1893 // or if it is a highly editable inset. So we should call this
1894 // function from our own edit with button < 0.
1895 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1897 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1905 inset_x = cix(bv) - top_x + drawTextXOffset;
1906 inset_y = ciy(bv) + drawTextYOffset;
1907 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1908 inset->localDispatch(cmd);
1909 if (!the_locking_inset)
1911 updateLocal(bv, CURSOR, false);
1918 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1921 int w = UpdatableInset::getMaxWidth(bv, inset);
1926 w = w - top_x + owner()->x();
1929 w -= (2 * TEXT_TO_INSET_OFFSET);
1932 return UpdatableInset::getMaxWidth(bv, inset);
1937 void InsetText::setParagraphData(ParagraphList const & plist)
1939 // we have to unlock any locked inset otherwise we're in troubles
1940 the_locking_inset = 0;
1942 // But it it makes no difference that is a lot better.
1944 // See if this can be simplified when std::list is in effect.
1947 ParagraphList::const_iterator it = plist.begin();
1948 ParagraphList::const_iterator end = plist.end();
1949 for (; it != end; ++it) {
1950 paragraphs.push_back(*it);
1951 Paragraph & tmp = paragraphs.back();
1952 tmp.setInsetOwner(this);
1960 void InsetText::markNew(bool track_changes)
1962 ParagraphList::iterator pit = paragraphs.begin();
1963 ParagraphList::iterator pend = paragraphs.end();
1964 for (; pit != pend; ++pit) {
1965 if (track_changes) {
1966 pit->trackChanges();
1968 // no-op when not tracking
1969 pit->cleanChanges();
1975 void InsetText::setText(string const & data, LyXFont const & font)
1978 for (unsigned int i = 0; i < data.length(); ++i)
1979 paragraphs.begin()->insertChar(i, data[i], font);
1984 void InsetText::setAutoBreakRows(bool flag)
1986 if (flag != autoBreakRows) {
1987 autoBreakRows = flag;
1995 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1997 if (how != drawFrame_) {
2000 updateLocal(bv, DRAW_FRAME, false);
2005 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2007 if (frame_color != col) {
2010 updateLocal(bv, DRAW_FRAME, false);
2015 int InsetText::cx(BufferView * bv) const
2017 // we do nothing dangerous so we use a local cache
2018 LyXText * llt = getLyXText(bv);
2019 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2020 if (the_locking_inset) {
2021 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2023 if (font.isVisibleRightToLeft())
2024 x -= the_locking_inset->width(bv, font);
2030 int InsetText::cix(BufferView * bv) const
2032 // we do nothing dangerous so we use a local cache
2033 LyXText * llt = getLyXText(bv);
2034 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2035 if (the_locking_inset) {
2036 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2038 if (font.isVisibleRightToLeft())
2039 x -= the_locking_inset->width(bv, font);
2045 int InsetText::cy(BufferView * bv) const
2048 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2052 int InsetText::ciy(BufferView * bv) const
2055 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2059 pos_type InsetText::cpos(BufferView * bv) const
2061 return getLyXText(bv)->cursor.pos();
2065 ParagraphList::iterator InsetText::cpar(BufferView * bv) const
2067 return getLyXText(bv)->cursor.par();
2071 bool InsetText::cboundary(BufferView * bv) const
2073 return getLyXText(bv)->cursor.boundary();
2077 RowList::iterator InsetText::crow(BufferView * bv) const
2079 return getLyXText(bv)->cursor.row();
2083 LyXText * InsetText::getLyXText(BufferView const * lbv,
2084 bool const recursive) const
2086 if (cached_bview == lbv) {
2087 if (recursive && the_locking_inset)
2088 return the_locking_inset->getLyXText(lbv, true);
2089 LyXText * lt = cached_text.get();
2090 lyx::Assert(lt && lt->rows().begin()->par() == const_cast<ParagraphList&>(paragraphs).begin());
2093 // Super UGLY! (Lgb)
2094 BufferView * bv = const_cast<BufferView *>(lbv);
2097 Cache::iterator it = cache.find(bv);
2099 if (it != cache.end()) {
2102 } else if (do_resize) {
2103 resizeLyXText(do_resize);
2105 if (lt || !it->second.remove) {
2106 lyx::Assert(it->second.text.get());
2107 cached_text = it->second.text;
2108 if (recursive && the_locking_inset) {
2109 return the_locking_inset->getLyXText(bv, true);
2111 return cached_text.get();
2112 } else if (it->second.remove) {
2114 saveLyXTextState(it->second.text.get());
2116 sstate.lpar = const_cast<ParagraphList&>(paragraphs).end();
2120 // when we have to reinit the existing LyXText!
2122 it->second.text->init(bv);
2123 restoreLyXTextState(it->second.text.get());
2124 it->second.remove = false;
2126 cached_text = it->second.text;
2127 if (the_locking_inset && recursive) {
2128 return the_locking_inset->getLyXText(bv);
2130 return cached_text.get();
2133 // we are here only if we don't have a BufferView * in the cache!!!
2135 cached_text.reset(new LyXText(bv, const_cast<InsetText *>(this)));
2136 cached_text->init(bv);
2137 restoreLyXTextState(cached_text.get());
2139 cache.insert(make_pair(bv, cached_text));
2141 if (the_locking_inset && recursive) {
2142 return the_locking_inset->getLyXText(bv);
2144 return cached_text.get();
2148 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2152 Cache::iterator it = cache.find(bv);
2154 if (it == cache.end()) {
2158 lyx::Assert(it->second.text.get());
2160 it->second.remove = true;
2162 /// then remove all LyXText in text-insets
2163 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2164 const_cast<ParagraphList&>(paragraphs).end(),
2165 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2170 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2173 // we cannot resize this because we are in use!
2174 // so do this on the next possible getLyXText()
2179 if (paragraphs.size() == 1 && paragraphs.begin()->empty()) {
2180 // no data, resize not neccessary!
2181 // we have to do this as a fixed width may have changed!
2182 LyXText * t = getLyXText(bv);
2183 saveLyXTextState(t);
2185 restoreLyXTextState(t);
2189 // one endless line, resize normally not necessary
2190 if (!force && getMaxWidth(bv, this) < 0)
2193 Cache::iterator it = cache.find(bv);
2194 if (it == cache.end()) {
2198 lyx::Assert(it->second.text.get());
2200 LyXText * t = it->second.text.get();
2201 saveLyXTextState(t);
2203 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2204 const_cast<ParagraphList&>(paragraphs).end(),
2205 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2208 restoreLyXTextState(t);
2210 if (the_locking_inset) {
2211 inset_x = cix(bv) - top_x + drawTextXOffset;
2212 inset_y = ciy(bv) + drawTextYOffset;
2215 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2217 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2218 // this will scroll the screen such that the cursor becomes visible
2219 bv->updateScrollbar();
2221 need_update |= FULL;
2226 void InsetText::reinitLyXText() const
2229 // we cannot resize this because we are in use!
2230 // so do this on the next possible getLyXText()
2236 for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2237 lyx::Assert(it->second.text.get());
2239 LyXText * t = it->second.text.get();
2240 BufferView * bv = it->first;
2242 saveLyXTextState(t);
2244 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2245 const_cast<ParagraphList&>(paragraphs).end(),
2246 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2249 restoreLyXTextState(t);
2250 if (the_locking_inset) {
2251 inset_x = cix(bv) - top_x + drawTextXOffset;
2252 inset_y = ciy(bv) + drawTextYOffset;
2254 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2256 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2257 // this will scroll the screen such that the cursor becomes visible
2258 bv->updateScrollbar();
2266 void InsetText::removeNewlines()
2268 bool changed = false;
2270 ParagraphList::iterator it = paragraphs.begin();
2271 ParagraphList::iterator end = paragraphs.end();
2272 for (; it != end; ++it) {
2273 for (int i = 0; i < it->size(); ++i) {
2274 if (it->isNewline(i)) {
2285 bool InsetText::nodraw() const
2287 if (the_locking_inset)
2288 return the_locking_inset->nodraw();
2289 return UpdatableInset::nodraw();
2293 int InsetText::scroll(bool recursive) const
2295 int sx = UpdatableInset::scroll(false);
2297 if (recursive && the_locking_inset)
2298 sx += the_locking_inset->scroll(recursive);
2304 void InsetText::selectAll(BufferView * bv)
2306 getLyXText(bv)->cursorTop();
2307 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2308 getLyXText(bv)->cursorBottom();
2309 getLyXText(bv)->setSelection();
2313 void InsetText::clearSelection(BufferView * bv)
2315 getLyXText(bv)->clearSelection();
2319 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2321 Painter & pain = bv->painter();
2323 int h = dim_.asc + dim_.des;
2324 int ty = baseline - dim_.asc;
2330 if ((ty + h) > pain.paperHeight())
2331 h = pain.paperHeight();
2332 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2333 w = pain.paperWidth();
2334 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2339 ParagraphList * InsetText::getParagraphs(int i) const
2341 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2345 LyXCursor const & InsetText::cursor(BufferView * bv) const
2347 if (the_locking_inset)
2348 return the_locking_inset->cursor(bv);
2349 return getLyXText(bv)->cursor;
2353 Inset * InsetText::getInsetFromID(int id_arg) const
2356 return const_cast<InsetText *>(this);
2358 ParagraphList::const_iterator pit = paragraphs.begin();
2359 ParagraphList::const_iterator pend = paragraphs.end();
2360 for (; pit != pend; ++pit) {
2361 InsetList::const_iterator it = pit->insetlist.begin();
2362 InsetList::const_iterator end = pit->insetlist.end();
2363 for (; it != end; ++it) {
2364 if (it->inset->id() == id_arg)
2366 Inset * in = it->inset->getInsetFromID(id_arg);
2376 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2377 float & value) const
2383 lt = getLyXText(bv);
2386 if (the_locking_inset) {
2387 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2388 if (!word.word().empty()) {
2394 // we have to go on checking so move cursor to the next char
2395 lt->cursor.pos(lt->cursor.pos() + 1);
2397 word = lt->selectNextWordToSpellcheck(value);
2398 if (word.word().empty())
2399 bv->unlockInset(const_cast<InsetText *>(this));
2408 void InsetText::selectSelectedWord(BufferView * bv)
2410 if (the_locking_inset) {
2411 the_locking_inset->selectSelectedWord(bv);
2414 getLyXText(bv)->selectSelectedWord();
2415 updateLocal(bv, SELECTION, false);
2419 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2421 if (the_locking_inset) {
2422 the_locking_inset->toggleSelection(bv, kill_selection);
2426 lt = getLyXText(bv);
2430 int x = top_x + TEXT_TO_INSET_OFFSET;
2432 RowList::iterator rowit = lt->rows().begin();
2433 RowList::iterator end = lt->rows().end();
2434 int y_offset = top_baseline - rowit->ascent_of_text();
2436 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2437 y += rowit->height();
2443 if (need_update & SELECTION)
2445 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2451 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2455 lt = getLyXText(bv);
2458 if (the_locking_inset) {
2459 if (the_locking_inset->nextChange(bv, length))
2461 lt->cursorRight(true);
2463 lyxfind::SearchResult result =
2464 lyxfind::findNextChange(bv, lt, length);
2466 if (result == lyxfind::SR_FOUND) {
2467 LyXCursor cur = lt->cursor;
2468 bv->unlockInset(bv->theLockingInset());
2469 if (bv->lockInset(this))
2472 lt->setSelectionRange(length);
2473 updateLocal(bv, SELECTION, false);
2477 return result != lyxfind::SR_NOT_FOUND;
2481 bool InsetText::searchForward(BufferView * bv, string const & str,
2486 lt = getLyXText(bv);
2489 if (the_locking_inset) {
2490 if (the_locking_inset->searchForward(bv, str, cs, mw))
2492 lt->cursorRight(true);
2494 lyxfind::SearchResult result =
2495 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2497 if (result == lyxfind::SR_FOUND) {
2498 LyXCursor cur = lt->cursor;
2499 bv->unlockInset(bv->theLockingInset());
2500 if (bv->lockInset(this))
2503 lt->setSelectionRange(str.length());
2504 updateLocal(bv, SELECTION, false);
2508 return (result != lyxfind::SR_NOT_FOUND);
2511 bool InsetText::searchBackward(BufferView * bv, string const & str,
2514 if (the_locking_inset) {
2515 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2520 lt = getLyXText(bv);
2524 ParagraphList::iterator pit = paragraphs.begin();
2525 ParagraphList::iterator pend = paragraphs.end();
2527 while (boost::next(pit) != pend)
2530 lt->setCursor(pit, pit->size());
2532 lyxfind::SearchResult result =
2533 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2535 if (result == lyxfind::SR_FOUND) {
2536 LyXCursor cur = lt->cursor;
2537 bv->unlockInset(bv->theLockingInset());
2538 if (bv->lockInset(this))
2541 lt->setSelectionRange(str.length());
2542 updateLocal(bv, SELECTION, false);
2546 return (result != lyxfind::SR_NOT_FOUND);
2550 bool InsetText::checkInsertChar(LyXFont & font)
2553 return owner()->checkInsertChar(font);
2558 void InsetText::collapseParagraphs(BufferView * bv)
2560 LyXText * llt = getLyXText(bv);
2562 while (paragraphs.size() > 1) {
2563 ParagraphList::iterator first_par = paragraphs.begin();
2564 ParagraphList::iterator next_par = boost::next(first_par);
2565 size_t const first_par_size = first_par->size();
2567 if (!first_par->empty() &&
2568 !next_par->empty() &&
2569 !first_par->isSeparator(first_par_size - 1)) {
2570 first_par->insertChar(first_par_size, ' ');
2573 if (llt->selection.set()) {
2574 if (llt->selection.start.par() == next_par) {
2575 llt->selection.start.par(first_par);
2576 llt->selection.start.pos(
2577 llt->selection.start.pos() + first_par_size);
2579 if (llt->selection.end.par() == next_par) {
2580 llt->selection.end.par(first_par);
2581 llt->selection.end.pos(
2582 llt->selection.end.pos() + first_par_size);
2586 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2592 void InsetText::getDrawFont(LyXFont & font) const
2596 owner()->getDrawFont(font);
2600 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2602 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2603 // And it probably does. You have to take a look at this John. (Lgb)
2604 #warning John, have a look here. (Lgb)
2605 ParagraphList::iterator pit = plist.begin();
2606 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2608 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2610 ParagraphList::iterator pend = plist.end();
2611 for (; pit != pend; ++pit) {
2612 paragraphs.push_back(*pit);
2619 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2621 ParagraphList::const_iterator pit = paragraphs.begin();
2622 ParagraphList::const_iterator pend = paragraphs.end();
2624 for (; pit != pend; ++pit) {
2625 InsetList::const_iterator it = pit->insetlist.begin();
2626 InsetList::const_iterator end = pit->insetlist.end();
2627 for (; it != end; ++it) {
2628 it->inset->addPreview(loader);