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"
22 #include "errorlist.h"
24 #include "LaTeXFeatures.h"
28 #include "lyxcursor.h"
34 #include "paragraph.h"
35 #include "ParagraphParameters.h"
36 #include "trans_mgr.h"
37 #include "undo_funcs.h"
38 #include "WordLangTuple.h"
39 #include "paragraph_funcs.h"
41 #include "rowpainter.h"
42 #include "insetnewline.h"
43 #include "metricsinfo.h"
45 #include "frontends/Alert.h"
46 #include "frontends/Dialogs.h"
47 #include "frontends/font_metrics.h"
48 #include "frontends/LyXView.h"
49 #include "frontends/Painter.h"
50 #include "frontends/screen.h"
52 #include "support/textutils.h"
53 #include "support/LAssert.h"
54 #include "support/lstrings.h"
55 #include "support/lyxalgo.h" // lyx::count
57 #include <boost/bind.hpp>
75 using lyx::textclass_type;
78 // These functions should probably go into bufferview_funcs somehow (Jug)
80 void InsetText::saveLyXTextState(LyXText * t) const
82 // check if my paragraphs are still valid
83 ParagraphList::iterator it = const_cast<ParagraphList&>(paragraphs).begin();
84 ParagraphList::iterator end = const_cast<ParagraphList&>(paragraphs).end();
85 for (; it != end; ++it) {
86 if (it == t->cursor.par())
90 if (it != end && t->cursor.pos() <= it->size()) {
91 sstate.lpar = t->cursor.par();
92 sstate.pos = t->cursor.pos();
93 sstate.boundary = t->cursor.boundary();
94 sstate.selstartpar = t->selection.start.par();
95 sstate.selstartpos = t->selection.start.pos();
96 sstate.selstartboundary = t->selection.start.boundary();
97 sstate.selendpar = t->selection.end.par();
98 sstate.selendpos = t->selection.end.pos();
99 sstate.selendboundary = t->selection.end.boundary();
100 sstate.selection = t->selection.set();
101 sstate.mark_set = t->selection.mark();
103 sstate.lpar = const_cast<ParagraphList&>(paragraphs).end();
108 void InsetText::restoreLyXTextState(LyXText * t) const
110 if (sstate.lpar == const_cast<ParagraphList&>(paragraphs).end())
113 t->selection.set(true);
114 /* at this point just to avoid the DEPM when setting the cursor */
115 t->selection.mark(sstate.mark_set);
116 if (sstate.selection) {
117 t->setCursor(sstate.selstartpar, sstate.selstartpos,
118 true, sstate.selstartboundary);
119 t->selection.cursor = t->cursor;
120 t->setCursor(sstate.selendpar, sstate.selendpos,
121 true, sstate.selendboundary);
123 t->setCursor(sstate.lpar, sstate.pos);
125 t->setCursor(sstate.lpar, sstate.pos, true, sstate.boundary);
126 t->selection.cursor = t->cursor;
127 t->selection.set(false);
132 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
139 InsetText::InsetText(BufferParams const & bp)
140 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
143 paragraphs.push_back(Paragraph());
144 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
145 if (bp.tracking_changes)
146 paragraphs.begin()->trackChanges();
151 InsetText::InsetText(InsetText const & in)
152 : UpdatableInset(in), lt(0), in_update(false), do_resize(0),
159 InsetText & InsetText::operator=(InsetText const & it)
166 void InsetText::init(InsetText const * ins)
169 setParagraphData(ins->paragraphs);
170 autoBreakRows = ins->autoBreakRows;
171 drawFrame_ = ins->drawFrame_;
172 frame_color = ins->frame_color;
174 for_each(paragraphs.begin(), paragraphs.end(),
175 boost::bind(&Paragraph::setInsetOwner, _1, this));
177 the_locking_inset = 0;
179 frame_color = LColor::insetframe;
180 autoBreakRows = false;
189 old_par = paragraphs.end();
190 last_drawn_width = -1;
192 sstate.lpar = paragraphs.end();
193 in_insetAllowed = false;
197 void InsetText::clear(bool just_mark_erased)
199 if (just_mark_erased) {
200 ParagraphList::iterator it = paragraphs.begin();
201 ParagraphList::iterator end = paragraphs.end();
202 for (; it != end; ++it) {
209 // This is a gross hack...
210 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
213 paragraphs.push_back(Paragraph());
214 paragraphs.begin()->setInsetOwner(this);
215 paragraphs.begin()->layout(old_layout);
222 InsetBase * InsetText::clone() const
224 return new InsetText(*this);
228 void InsetText::write(Buffer const * buf, ostream & os) const
231 writeParagraphData(buf, os);
235 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
237 ParagraphList::const_iterator it = paragraphs.begin();
238 ParagraphList::const_iterator end = paragraphs.end();
239 Paragraph::depth_type dth = 0;
240 for (; it != end; ++it) {
241 it->write(buf, os, buf->params, dth);
246 void InsetText::read(Buffer const * buf, LyXLex & lex)
249 Paragraph::depth_type depth = 0;
253 if (buf->params.tracking_changes)
254 paragraphs.begin()->trackChanges();
256 // delete the initial paragraph
258 ParagraphList::iterator pit = paragraphs.begin();
262 token = lex.getString();
265 if (token == "\\end_inset") {
269 if (token == "\\the_end") {
270 lex.printError("\\the_end read in inset! Error in document!");
275 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
278 pit = paragraphs.begin();
279 ParagraphList::iterator const end = paragraphs.end();
280 for (; pit != end; ++pit)
281 pit->setInsetOwner(this);
283 if (token != "\\end_inset") {
284 lex.printError("Missing \\end_inset at this point. "
291 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
293 BufferView * bv = mi.base.bv;
294 LyXText * text = getLyXText(bv);
295 dim.asc = text->rows().begin()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
296 dim.des = text->height - dim.asc + TEXT_TO_INSET_OFFSET;
297 dim.wid = max(textWidth(bv), int(text->width)) + 2 * TEXT_TO_INSET_OFFSET;
298 dim.wid = max(dim.wid, 10);
303 int InsetText::textWidth(BufferView * bv, bool fordraw) const
305 int w = autoBreakRows ? getMaxWidth(bv, this) : -1;
308 return max(w - 2 * TEXT_TO_INSET_OFFSET,
309 (int)getLyXText(bv)->width);
314 return w - 2 * TEXT_TO_INSET_OFFSET;
318 void InsetText::draw(PainterInfo & pi, int x, int baseline) const
323 // update our idea of where we are. Clearly, we should
324 // not have to know this information.
328 int const start_x = x;
330 BufferView * bv = pi.base.bv;
331 Painter & pain = pi.pain;
333 // repaint the background if needed
334 if (backgroundColor() != LColor::background)
335 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
337 // no draw is necessary !!!
338 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
339 top_baseline = baseline;
347 top_baseline = baseline;
348 top_y = baseline - dim_.asc;
350 if (last_drawn_width != dim_.wid) {
352 last_drawn_width = dim_.wid;
355 if (the_locking_inset && (cpar(bv) == inset_par)
356 && (cpos(bv) == inset_pos)) {
357 inset_x = cix(bv) - int(x) + drawTextXOffset;
358 inset_y = ciy(bv) + drawTextYOffset;
366 x += TEXT_TO_INSET_OFFSET;
368 RowList::iterator rowit = lt->rows().begin();
369 RowList::iterator end = lt->rows().end();
371 int y_offset = baseline - rowit->ascent_of_text();
372 int ph = pain.paperHeight();
375 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
376 y += rowit->height();
377 first += rowit->height();
381 lt->top_y(-y_offset);
389 int yf = y_offset + first;
394 while ((rowit != end) && (yf < ph)) {
395 RowPainter rp(*bv, *lt, rowit);
396 rp.paint(y + y_offset + first, int(x), y + lt->top_y());
397 y += rowit->height();
398 yf += rowit->height();
404 if ((drawFrame_ == ALWAYS) || (drawFrame_ == LOCKED && locked)) {
405 drawFrame(pain, int(start_x));
408 x += dim_.wid - TEXT_TO_INSET_OFFSET;
410 if (need_update != INIT) {
418 void InsetText::drawFrame(Painter & pain, int x) const
420 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
422 frame_y = top_baseline - dim_.asc + ttoD2;
423 frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
424 frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
425 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
430 void InsetText::update(BufferView * bv, bool reinit)
433 if (reinit && owner()) {
435 owner()->update(bv, true);
441 if (reinit || need_update == INIT) {
443 // we should put this call where we set need_update to INIT!
446 owner()->update(bv, true);
449 int nw = getMaxWidth(bv, this);
450 if (nw > 0 && old_max_width != nw) {
457 if (!autoBreakRows && paragraphs.size() > 1)
458 collapseParagraphs(bv);
460 if (the_locking_inset) {
461 inset_x = cix(bv) - top_x + drawTextXOffset;
462 inset_y = ciy(bv) + drawTextYOffset;
463 the_locking_inset->update(bv, reinit);
471 if ((need_update & CURSOR_PAR) && (lt->refreshStatus() == LyXText::REFRESH_NONE) &&
473 lt->updateInset(the_locking_inset);
476 if (lt->refreshStatus() == LyXText::REFRESH_AREA)
483 int nw = getMaxWidth(bv, this);
484 if (nw > 0 && old_max_width != nw) {
491 void InsetText::setUpdateStatus(BufferView * bv, int what) const
493 // this does nothing dangerous so use only a localized buffer
494 LyXText * llt = getLyXText(bv);
497 // we have to redraw us full if our LyXText REFRESH_AREA or
498 // if we don't break row so that we only have one row to update!
499 if ((llt->refreshStatus() == LyXText::REFRESH_AREA) ||
501 (llt->refreshStatus() == LyXText::REFRESH_ROW)))
504 } else if (llt->refreshStatus() == LyXText::REFRESH_ROW) {
505 need_update |= CURSOR_PAR;
508 // this to not draw a selection when we redraw all of it!
509 if (need_update & CURSOR && !(need_update & SELECTION)) {
510 if (llt->selection.set())
512 llt->clearSelection();
517 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
519 if (!autoBreakRows && paragraphs.size() > 1)
520 collapseParagraphs(bv);
527 lt->partialRebreak();
528 setUpdateStatus(bv, what);
529 bool flag = mark_dirty ||
530 (((need_update != CURSOR) && (need_update != NONE)) ||
531 (lt->refreshStatus() != LyXText::REFRESH_NONE) || lt->selection.set());
532 if (!lt->selection.set())
533 lt->selection.cursor = lt->cursor;
539 bv->updateInset(const_cast<InsetText *>(this));
545 if (need_update == CURSOR)
547 bv->owner()->view_state_changed();
548 bv->owner()->updateMenubar();
549 bv->owner()->updateToolbar();
550 if (old_par != cpar(bv)) {
551 bv->owner()->setLayout(cpar(bv)->layout()->name());
557 string const InsetText::editMessage() const
559 return _("Opened Text Inset");
563 void InsetText::insetUnlock(BufferView * bv)
565 if (the_locking_inset) {
566 the_locking_inset->insetUnlock(bv);
567 the_locking_inset = 0;
568 updateLocal(bv, CURSOR_PAR, false);
579 if (lt->selection.set()) {
580 lt->clearSelection();
582 } else if (owner()) {
583 bv->owner()->setLayout(owner()->getLyXText(bv)
584 ->cursor.par()->layout()->name());
586 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
587 // hack for deleteEmptyParMech
588 ParagraphList::iterator first_par = paragraphs.begin();
589 if (!first_par->empty()) {
590 lt->setCursor(first_par, 0);
591 } else if (paragraphs.size() > 1) {
592 lt->setCursor(boost::next(first_par), 0);
597 updateLocal(bv, code, false);
600 setUpdateStatus(bv, code);
605 void InsetText::lockInset(BufferView * bv)
608 the_locking_inset = 0;
609 inset_pos = inset_x = inset_y = 0;
610 inset_boundary = false;
611 inset_par = paragraphs.end();
612 old_par = paragraphs.end();
618 lt->setCursor(paragraphs.begin(), 0);
619 lt->clearSelection();
621 // If the inset is empty set the language of the current font to the
622 // language to the surronding text (if different).
623 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
624 bv->getParentLanguage(this) != lt->current_font.language()) {
625 LyXFont font(LyXFont::ALL_IGNORE);
626 font.setLanguage(bv->getParentLanguage(this));
627 setFont(bv, font, false);
632 if (drawFrame_ == LOCKED)
633 code = CURSOR|DRAW_FRAME;
634 setUpdateStatus(bv, code);
638 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
640 the_locking_inset = inset;
641 inset_x = cix(bv) - top_x + drawTextXOffset;
642 inset_y = ciy(bv) + drawTextYOffset;
643 inset_pos = cpos(bv);
644 inset_par = cpar(bv);
645 inset_boundary = cboundary(bv);
646 updateLocal(bv, CURSOR, false);
650 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
652 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
656 if (!the_locking_inset) {
657 ParagraphList::iterator pit = paragraphs.begin();
658 ParagraphList::iterator pend = paragraphs.end();
660 int const id = inset->id();
661 for (; pit != pend; ++pit) {
662 InsetList::iterator it =
663 pit->insetlist.begin();
664 InsetList::iterator const end =
665 pit->insetlist.end();
666 for (; it != end; ++it) {
667 if (it->inset == inset) {
668 getLyXText(bv)->setCursorIntern(pit, it->pos);
669 lockInset(bv, inset);
672 if (it->inset->getInsetFromID(id)) {
673 getLyXText(bv)->setCursorIntern(pit, it->pos);
674 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
675 return the_locking_inset->lockInsetInInset(bv, inset);
681 if (inset == cpar(bv)->getInset(cpos(bv))) {
682 lyxerr[Debug::INSETS] << "OK" << endl;
683 lockInset(bv, inset);
685 } else if (the_locking_inset && (the_locking_inset == inset)) {
686 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
687 lyxerr[Debug::INSETS] << "OK" << endl;
688 inset_x = cix(bv) - top_x + drawTextXOffset;
689 inset_y = ciy(bv) + drawTextYOffset;
691 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
693 } else if (the_locking_inset) {
694 lyxerr[Debug::INSETS] << "MAYBE" << endl;
695 return the_locking_inset->lockInsetInInset(bv, inset);
697 lyxerr[Debug::INSETS] << "NOT OK" << endl;
702 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
705 if (!the_locking_inset)
707 if (the_locking_inset == inset) {
708 the_locking_inset->insetUnlock(bv);
709 getLyXText(bv)->updateInset(inset);
710 the_locking_inset = 0;
712 moveRightIntern(bv, true, false);
713 old_par = paragraphs.end(); // force layout setting
717 updateLocal(bv, CURSOR, false);
720 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
724 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
726 if (!autoBreakRows && paragraphs.size() > 1)
727 collapseParagraphs(bv);
737 if (inset->owner() != this) {
738 int ustat = CURSOR_PAR;
740 UpdatableInset * tl_inset = the_locking_inset;
742 found = tl_inset->updateInsetInInset(bv, inset);
744 tl_inset = static_cast<UpdatableInset *>(inset);
745 while(tl_inset->owner() && tl_inset->owner() != this)
746 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
747 if (!tl_inset->owner())
749 found = tl_inset->updateInsetInInset(bv, inset);
753 lt->updateInset(tl_inset);
757 setUpdateStatus(bv, ustat);
760 bool found = lt->updateInset(inset);
764 setUpdateStatus(bv, CURSOR_PAR);
765 if (the_locking_inset &&
766 cpar(bv) == inset_par && cpos(bv) == inset_pos)
768 inset_x = cix(bv) - top_x + drawTextXOffset;
769 inset_y = ciy(bv) + drawTextYOffset;
776 void InsetText::lfunMousePress(FuncRequest const & cmd)
780 // use this to check mouse motion for selection!
784 BufferView * bv = cmd.view();
785 FuncRequest cmd1 = cmd;
791 int tmp_x = cmd.x - drawTextXOffset;
792 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
793 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
795 if (the_locking_inset) {
796 if (the_locking_inset == inset) {
797 the_locking_inset->localDispatch(cmd1);
800 // otherwise only unlock the_locking_inset
801 the_locking_inset->insetUnlock(bv);
802 the_locking_inset = 0;
805 no_selection = false;
807 if (bv->theLockingInset()) {
808 if (isHighlyEditableInset(inset)) {
809 // We just have to lock the inset before calling a
811 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
812 if (!bv->lockInset(uinset)) {
813 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
815 inset->localDispatch(cmd1);
816 if (the_locking_inset)
817 updateLocal(bv, CURSOR, false);
822 bool paste_internally = false;
823 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
824 localDispatch(FuncRequest(bv, LFUN_COPY));
825 paste_internally = true;
832 int old_top_y = lt->top_y();
834 lt->setCursorFromCoordinates(cmd.x - drawTextXOffset,
836 // set the selection cursor!
837 lt->selection.cursor = lt->cursor;
838 lt->cursor.x_fix(lt->cursor.x());
840 if (lt->selection.set()) {
841 lt->clearSelection();
842 updateLocal(bv, FULL, false);
844 lt->clearSelection();
845 updateLocal(bv, CURSOR, false);
851 bv->owner()->setLayout(cpar(bv)->layout()->name());
853 // we moved the view we cannot do mouse selection in this case!
854 if (getLyXText(bv)->top_y() != old_top_y)
857 // Insert primary selection with middle mouse
858 // if there is a local selection in the current buffer,
860 if (cmd.button() == mouse_button::button2) {
861 if (paste_internally)
862 localDispatch(FuncRequest(bv, LFUN_PASTE));
864 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
867 getLyXText(bv)->clearSelection();
872 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
874 BufferView * bv = cmd.view();
875 FuncRequest cmd1 = cmd;
880 if (the_locking_inset)
881 return the_locking_inset->localDispatch(cmd1);
883 int tmp_x = cmd.x - drawTextXOffset;
884 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
885 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
888 // This code should probably be removed now. Simple insets
889 // (!highlyEditable) can actually take the localDispatch,
890 // and turn it into edit() if necessary. But we still
891 // need to deal properly with the whole relative vs.
892 // absolute mouse co-ords thing in a realiable, sensible way
894 if (isHighlyEditableInset(inset))
895 ret = inset->localDispatch(cmd1);
897 inset_x = cix(bv) - top_x + drawTextXOffset;
898 inset_y = ciy(bv) + drawTextYOffset;
899 cmd1.x = cmd.x - inset_x;
900 cmd1.y = cmd.x - inset_y;
901 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
905 ret = inset->localDispatch(cmd1);
906 updateLocal(bv, CURSOR_PAR, false);
913 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
915 FuncRequest cmd1 = cmd;
919 if (the_locking_inset) {
920 the_locking_inset->localDispatch(cmd1);
924 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
927 BufferView * bv = cmd.view();
933 LyXCursor cur = lt->cursor;
934 lt->setCursorFromCoordinates
935 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
936 lt->cursor.x_fix(lt->cursor.x());
937 if (cur == lt->cursor) {
943 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
944 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
948 updateLocal(bv, SELECTION, false);
953 Inset::RESULT InsetText::localDispatch(FuncRequest const & cmd)
955 BufferView * bv = cmd.view();
957 if (cmd.action == LFUN_INSET_EDIT) {
958 UpdatableInset::localDispatch(cmd);
960 if (!bv->lockInset(this)) {
961 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
966 the_locking_inset = 0;
967 inset_pos = inset_x = inset_y = 0;
968 inset_boundary = false;
969 inset_par = paragraphs.end();
970 old_par = paragraphs.end();
978 if (cmd.argument.size()) {
979 if (cmd.argument == "left")
980 lt->setCursor(paragraphs.begin(), 0);
982 ParagraphList::iterator it = paragraphs.begin();
983 ParagraphList::iterator end = paragraphs.end();
984 while (boost::next(it) != end)
986 // int const pos = (p->size() ? p->size()-1 : p->size());
987 lt->setCursor(it, it->size());
990 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
991 // we put here -1 and not button as now the button in the
992 // edit call should not be needed we will fix this in 1.3.x
993 // cycle hopefully (Jug 20020509)
994 // FIXME: GUII I've changed this to none: probably WRONG
995 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
996 lt->setCursorFromCoordinates(cmd.x - drawTextXOffset,
998 lt->cursor.x_fix(lt->cursor.x());
1002 lt->clearSelection();
1005 // If the inset is empty set the language of the current font to the
1006 // language to the surronding text (if different).
1007 if (paragraphs.begin()->empty() &&
1008 paragraphs.size() == 1 &&
1009 bv->getParentLanguage(this) != lt->current_font.language())
1011 LyXFont font(LyXFont::ALL_IGNORE);
1012 font.setLanguage(bv->getParentLanguage(this));
1013 setFont(bv, font, false);
1019 if (drawFrame_ == LOCKED)
1020 code = CURSOR | DRAW_FRAME;
1022 updateLocal(bv, code, false);
1023 // Tell the paragraph dialog that we've entered an insettext.
1024 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1029 switch (cmd.action) {
1030 case LFUN_MOUSE_PRESS:
1031 lfunMousePress(cmd);
1033 case LFUN_MOUSE_MOTION:
1034 lfunMouseMotion(cmd);
1036 case LFUN_MOUSE_RELEASE:
1037 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
1042 bool was_empty = (paragraphs.begin()->empty() &&
1043 paragraphs.size() == 1);
1045 no_selection = false;
1046 RESULT result = UpdatableInset::localDispatch(cmd);
1047 if (result != UNDISPATCHED)
1050 result = DISPATCHED;
1051 if (cmd.action < 0 && cmd.argument.empty())
1054 if (the_locking_inset) {
1055 result = the_locking_inset->localDispatch(cmd);
1056 if (result == DISPATCHED_NOUPDATE)
1058 else if (result == DISPATCHED) {
1059 updateLocal(bv, CURSOR_PAR, false);
1061 } else if (result >= FINISHED) {
1063 case FINISHED_RIGHT:
1064 moveRightIntern(bv, false, false);
1065 result = DISPATCHED;
1068 if ((result = moveUp(bv)) >= FINISHED) {
1069 updateLocal(bv, CURSOR, false);
1070 bv->unlockInset(this);
1074 if ((result = moveDown(bv)) >= FINISHED) {
1075 updateLocal(bv, CURSOR, false);
1076 bv->unlockInset(this);
1080 result = DISPATCHED;
1083 the_locking_inset = 0;
1084 updateLocal(bv, CURSOR, false);
1085 // make sure status gets reset immediately
1086 bv->owner()->clearMessage();
1092 lt = getLyXText(bv);
1096 int updflag = false;
1098 // what type of update to do on a cursor movement
1099 int cursor_update = CURSOR;
1101 if (lt->selection.set())
1102 cursor_update = SELECTION;
1104 switch (cmd.action) {
1107 case LFUN_SELFINSERT:
1108 if (bv->buffer()->isReadonly()) {
1109 // setErrorMessage(N_("Document is read only"));
1112 if (!cmd.argument.empty()) {
1113 /* Automatically delete the currently selected
1114 * text and replace it with what is being
1115 * typed in now. Depends on lyxrc settings
1116 * "auto_region_delete", which defaults to
1119 // This should not be needed here and is also WRONG!
1120 setUndo(bv, Undo::INSERT, lt->cursor.par());
1123 if (lyxrc.auto_region_delete) {
1124 if (lt->selection.set()) {
1125 lt->cutSelection(false, false);
1128 lt->clearSelection();
1129 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
1130 bv->owner()->getIntl().getTransManager().
1131 TranslateAndInsert(cmd.argument[i], lt);
1134 lt->selection.cursor = lt->cursor;
1135 updwhat = CURSOR | CURSOR_PAR;
1137 result = DISPATCHED_NOUPDATE;
1140 // cursor movements that need special handling
1143 result = moveRight(bv);
1145 updwhat = cursor_update;
1149 result = moveLeft(bv);
1150 updwhat = cursor_update;
1154 result = moveDown(bv);
1155 updwhat = cursor_update;
1159 result = moveUp(bv);
1160 updwhat = cursor_update;
1164 if (crow(bv) == lt->rows().begin())
1165 result = FINISHED_UP;
1167 lt->cursorPrevious();
1168 lt->clearSelection();
1169 result = DISPATCHED_NOUPDATE;
1171 updwhat = cursor_update;
1175 if (boost::next(crow(bv)) == lt->rows().end())
1176 result = FINISHED_DOWN;
1179 lt->clearSelection();
1180 result = DISPATCHED_NOUPDATE;
1182 updwhat = cursor_update;
1185 case LFUN_BACKSPACE: {
1186 if (lt->selection.set())
1187 lt->cutSelection(true, false);
1190 updwhat = CURSOR_PAR;
1196 if (lt->selection.set()) {
1197 lt->cutSelection(true, false);
1201 updwhat = CURSOR_PAR;
1207 lt->cutSelection(true, true);
1208 updwhat = CURSOR_PAR;
1215 lt->copySelection();
1216 updwhat = CURSOR_PAR;
1219 case LFUN_PASTESELECTION:
1221 string const clip(bv->getClipboard());
1225 if (cmd.argument == "paragraph") {
1226 lt->insertStringAsParagraphs(clip);
1228 lt->insertStringAsLines(clip);
1231 lt->clearSelection();
1233 updwhat = CURSOR_PAR;
1239 if (!autoBreakRows) {
1240 if (CutAndPaste::nrOfParagraphs() > 1) {
1241 #ifdef WITH_WARNINGS
1242 #warning FIXME horrendously bad UI
1244 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1249 size_t sel_index = 0;
1250 string const & arg = cmd.argument;
1251 if (isStrUnsignedInt(arg)) {
1252 size_t const paste_arg = strToUnsignedInt(arg);
1253 #warning FIXME Check if the arg is in the domain of available selections.
1254 sel_index = paste_arg;
1256 lt->pasteSelection(sel_index);
1258 lt->clearSelection();
1259 updwhat = CURSOR_PAR;
1264 case LFUN_BREAKPARAGRAPH:
1265 if (!autoBreakRows) {
1266 result = DISPATCHED;
1269 lt->breakParagraph(paragraphs, 0);
1270 updwhat = CURSOR | FULL;
1274 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1275 if (!autoBreakRows) {
1276 result = DISPATCHED;
1279 lt->breakParagraph(paragraphs, 1);
1280 updwhat = CURSOR | FULL;
1284 case LFUN_BREAKLINE: {
1285 if (!autoBreakRows) {
1286 result = DISPATCHED;
1290 lt->insertInset(new InsetNewline);
1291 updwhat = CURSOR | CURSOR_PAR;
1297 // do not set layouts on non breakable textinsets
1298 if (autoBreakRows) {
1299 string cur_layout = cpar(bv)->layout()->name();
1301 // Derive layout number from given argument (string)
1302 // and current buffer's textclass (number). */
1303 LyXTextClass const & tclass =
1304 bv->buffer()->params.getLyXTextClass();
1305 string layout = cmd.argument;
1306 bool hasLayout = tclass.hasLayout(layout);
1308 // If the entry is obsolete, use the new one instead.
1310 string const & obs =
1311 tclass[layout]->obsoleted_by();
1316 // see if we found the layout number:
1318 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1319 bv->owner()->dispatch(lf);
1323 if (cur_layout != layout) {
1324 cur_layout = layout;
1325 lt->setLayout(layout);
1326 bv->owner()->setLayout(cpar(bv)->layout()->name());
1327 updwhat = CURSOR_PAR;
1331 // reset the layout box
1332 bv->owner()->setLayout(cpar(bv)->layout()->name());
1335 case LFUN_PARAGRAPH_SPACING:
1336 // This one is absolutely not working. When fiddling with this
1337 // it also seems to me that the paragraphs inside the insettext
1338 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1339 // FIXME: how old is this comment ? ...
1341 ParagraphList::iterator pit = lt->cursor.par();
1342 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1343 float cur_value = 1.0;
1344 if (cur_spacing == Spacing::Other) {
1345 cur_value = pit->params().spacing().getValue();
1348 istringstream istr(STRCONV(cmd.argument));
1351 Spacing::Space new_spacing = cur_spacing;
1352 float new_value = cur_value;
1354 lyxerr << "Missing argument to `paragraph-spacing'"
1356 } else if (tmp == "single") {
1357 new_spacing = Spacing::Single;
1358 } else if (tmp == "onehalf") {
1359 new_spacing = Spacing::Onehalf;
1360 } else if (tmp == "double") {
1361 new_spacing = Spacing::Double;
1362 } else if (tmp == "other") {
1363 new_spacing = Spacing::Other;
1366 lyxerr << "new_value = " << tmpval << endl;
1369 } else if (tmp == "default") {
1370 new_spacing = Spacing::Default;
1372 lyxerr << _("Unknown spacing argument: ")
1373 << cmd.argument << endl;
1375 if (cur_spacing != new_spacing || cur_value != new_value) {
1376 pit->params().spacing(Spacing(new_spacing, new_value));
1377 updwhat = CURSOR_PAR;
1383 // These need to do repaints but don't require
1384 // special handling otherwise. A *lot* of the
1385 // above could probably be done similarly ...
1390 case LFUN_WORDRIGHT:
1391 // these two are really unhandled ...
1393 case LFUN_BEGINNINGBUF:
1394 updwhat = cursor_update;
1395 if (!bv->dispatch(cmd))
1396 result = UNDISPATCHED;
1405 case LFUN_WORDLEFTSEL:
1406 case LFUN_WORDRIGHTSEL:
1407 updwhat = SELECTION;
1412 if (!bv->dispatch(cmd))
1413 result = UNDISPATCHED;
1420 updateLocal(bv, updwhat, updflag);
1421 /// If the action has deleted all text in the inset, we need to change the
1422 // language to the language of the surronding text.
1423 if (!was_empty && paragraphs.begin()->empty() &&
1424 paragraphs.size() == 1) {
1425 LyXFont font(LyXFont::ALL_IGNORE);
1426 font.setLanguage(bv->getParentLanguage(this));
1427 setFont(bv, font, false);
1430 if (result >= FINISHED)
1431 bv->unlockInset(this);
1433 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1434 result = DISPATCHED;
1439 int InsetText::latex(Buffer const * buf, ostream & os,
1440 LatexRunParams const & runparams) const
1443 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1444 return texrow.rows();
1448 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1450 unsigned int lines = 0;
1452 ParagraphList::const_iterator beg = paragraphs.begin();
1453 ParagraphList::const_iterator end = paragraphs.end();
1454 ParagraphList::const_iterator it = beg;
1455 for (; it != end; ++it) {
1456 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1457 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1464 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1466 unsigned int lines = 0;
1468 vector<string> environment_stack(10);
1469 vector<string> environment_inner(10);
1471 int const command_depth = 0;
1474 Paragraph::depth_type depth = 0; // paragraph depth
1476 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1477 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1479 for (; pit != pend; ++pit) {
1481 int desc_on = 0; // description mode
1483 LyXLayout_ptr const & style = pit->layout();
1485 // environment tag closing
1486 for (; depth > pit->params().depth(); --depth) {
1487 if (environment_inner[depth] != "!-- --") {
1488 item_name = "listitem";
1489 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1490 if (environment_inner[depth] == "varlistentry")
1491 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1493 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1494 environment_stack[depth].erase();
1495 environment_inner[depth].erase();
1498 if (depth == pit->params().depth()
1499 && environment_stack[depth] != style->latexname()
1500 && !environment_stack[depth].empty()) {
1501 if (environment_inner[depth] != "!-- --") {
1502 item_name= "listitem";
1503 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1504 if (environment_inner[depth] == "varlistentry")
1505 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1508 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1510 environment_stack[depth].erase();
1511 environment_inner[depth].erase();
1514 // Write opening SGML tags.
1515 switch (style->latextype) {
1516 case LATEX_PARAGRAPH:
1517 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1521 buf->parseError(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1525 case LATEX_ENVIRONMENT:
1526 case LATEX_ITEM_ENVIRONMENT:
1527 if (depth < pit->params().depth()) {
1528 depth = pit->params().depth();
1529 environment_stack[depth].erase();
1532 if (environment_stack[depth] != style->latexname()) {
1533 if (environment_stack.size() == depth + 1) {
1534 environment_stack.push_back("!-- --");
1535 environment_inner.push_back("!-- --");
1537 environment_stack[depth] = style->latexname();
1538 environment_inner[depth] = "!-- --";
1539 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1541 if (environment_inner[depth] != "!-- --") {
1542 item_name= "listitem";
1543 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1544 if (environment_inner[depth] == "varlistentry")
1545 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1549 if (style->latextype == LATEX_ENVIRONMENT) {
1550 if (!style->latexparam().empty()) {
1551 if (style->latexparam() == "CDATA")
1554 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1559 desc_on = (style->labeltype == LABEL_MANUAL);
1561 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1562 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1564 item_name = desc_on ? "term" : "para";
1565 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1569 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1573 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1576 // write closing SGML tags
1577 switch (style->latextype) {
1578 case LATEX_ENVIRONMENT:
1579 if (!style->latexparam().empty()) {
1580 if (style->latexparam() == "CDATA")
1583 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1586 case LATEX_ITEM_ENVIRONMENT:
1587 if (desc_on == 1) break;
1589 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1591 case LATEX_PARAGRAPH:
1592 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1595 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1601 for (int d = depth; d >= 0; --d) {
1602 if (!environment_stack[depth].empty()) {
1603 if (environment_inner[depth] != "!-- --") {
1604 item_name = "listitem";
1605 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1606 if (environment_inner[depth] == "varlistentry")
1607 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1610 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1618 void InsetText::validate(LaTeXFeatures & features) const
1620 for_each(paragraphs.begin(), paragraphs.end(),
1621 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1625 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1627 if (the_locking_inset) {
1628 the_locking_inset->getCursor(bv, x, y);
1632 y = cy(&bv) + InsetText::y();
1636 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1638 if (the_locking_inset) {
1639 the_locking_inset->getCursorPos(bv, x, y);
1642 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1643 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1647 int InsetText::insetInInsetY() const
1649 if (!the_locking_inset)
1652 return (inset_y + the_locking_inset->insetInInsetY());
1656 void InsetText::fitInsetCursor(BufferView * bv) const
1658 if (the_locking_inset) {
1659 the_locking_inset->fitInsetCursor(bv);
1662 LyXFont const font =
1663 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1665 int const asc = font_metrics::maxAscent(font);
1666 int const desc = font_metrics::maxDescent(font);
1668 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1669 need_update |= FULL;
1674 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1676 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1677 return moveLeftIntern(bv, false, activate_inset, selecting);
1679 return moveRightIntern(bv, true, activate_inset, selecting);
1684 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1686 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1687 return moveRightIntern(bv, true, activate_inset, selecting);
1689 return moveLeftIntern(bv, false, activate_inset, selecting);
1694 InsetText::moveRightIntern(BufferView * bv, bool front,
1695 bool activate_inset, bool selecting)
1697 LyXText * text = getLyXText(bv);
1699 ParagraphList::iterator c_par = cpar(bv);
1701 if (boost::next(c_par) == paragraphs.end() &&
1702 (cpos(bv) >= c_par->size()))
1703 return FINISHED_RIGHT;
1704 if (activate_inset && checkAndActivateInset(bv, front))
1706 text->cursorRight(bv);
1708 text->clearSelection();
1709 return DISPATCHED_NOUPDATE;
1714 InsetText::moveLeftIntern(BufferView * bv, bool front,
1715 bool activate_inset, bool selecting)
1717 LyXText * text = getLyXText(bv);
1719 if (cpar(bv) == paragraphs.begin() && (cpos(bv) <= 0))
1721 text->cursorLeft(bv);
1723 text->clearSelection();
1724 if (activate_inset && checkAndActivateInset(bv, front))
1726 return DISPATCHED_NOUPDATE;
1730 Inset::RESULT InsetText::moveUp(BufferView * bv)
1732 if (crow(bv) == getLyXText(bv)->rows().begin())
1734 getLyXText(bv)->cursorUp(bv);
1735 getLyXText(bv)->clearSelection();
1736 return DISPATCHED_NOUPDATE;
1740 Inset::RESULT InsetText::moveDown(BufferView * bv)
1742 if (boost::next(crow(bv)) == getLyXText(bv)->rows().end())
1743 return FINISHED_DOWN;
1744 getLyXText(bv)->cursorDown(bv);
1745 getLyXText(bv)->clearSelection();
1746 return DISPATCHED_NOUPDATE;
1750 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1752 if (the_locking_inset) {
1753 if (the_locking_inset->insetAllowed(inset))
1754 return the_locking_inset->insertInset(bv, inset);
1757 inset->setOwner(this);
1758 getLyXText(bv)->insertInset(inset);
1760 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1765 bool InsetText::insetAllowed(Inset::Code code) const
1767 // in_insetAllowed is a really gross hack,
1768 // to allow us to call the owner's insetAllowed
1769 // without stack overflow, which can happen
1770 // when the owner uses InsetCollapsable::insetAllowed()
1772 if (in_insetAllowed)
1774 in_insetAllowed = true;
1775 if (the_locking_inset)
1776 ret = the_locking_inset->insetAllowed(code);
1778 ret = owner()->insetAllowed(code);
1779 in_insetAllowed = false;
1784 UpdatableInset * InsetText::getLockingInset() const
1786 return the_locking_inset ? the_locking_inset->getLockingInset() :
1787 const_cast<InsetText *>(this);
1791 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1795 if (the_locking_inset)
1796 return the_locking_inset->getFirstLockingInsetOfType(c);
1801 bool InsetText::showInsetDialog(BufferView * bv) const
1803 if (the_locking_inset)
1804 return the_locking_inset->showInsetDialog(bv);
1809 void InsetText::getLabelList(std::vector<string> & list) const
1811 ParagraphList::const_iterator pit = paragraphs.begin();
1812 ParagraphList::const_iterator pend = paragraphs.end();
1813 for (; pit != pend; ++pit) {
1814 InsetList::const_iterator beg = pit->insetlist.begin();
1815 InsetList::const_iterator end = pit->insetlist.end();
1816 for (; beg != end; ++beg)
1817 beg->inset->getLabelList(list);
1822 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1825 if (the_locking_inset) {
1826 the_locking_inset->setFont(bv, font, toggleall, selectall);
1830 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1831 || cpar(bv)->empty()) {
1832 getLyXText(bv)->setFont(font, toggleall);
1838 lt = getLyXText(bv);
1842 if (lt->selection.set()) {
1843 setUndo(bv, Undo::EDIT, lt->cursor.par());
1849 lt->toggleFree(font, toggleall);
1852 lt->clearSelection();
1856 bool flag = (selectall || lt->selection.set());
1862 updateLocal(bv, FULL, true);
1864 updateLocal(bv, CURSOR_PAR, true);
1868 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1870 if (cpar(bv)->isInset(cpos(bv))) {
1872 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1873 if (!isHighlyEditableInset(inset))
1875 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1876 inset->localDispatch(cmd);
1877 if (!the_locking_inset)
1879 updateLocal(bv, CURSOR, false);
1886 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1887 mouse_button::state button)
1889 x -= drawTextXOffset;
1891 int dummyy = y + dim_.asc;
1892 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1893 // we only do the edit() call if the inset was hit by the mouse
1894 // or if it is a highly editable inset. So we should call this
1895 // function from our own edit with button < 0.
1896 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1898 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1906 inset_x = cix(bv) - top_x + drawTextXOffset;
1907 inset_y = ciy(bv) + drawTextYOffset;
1908 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1909 inset->localDispatch(cmd);
1910 if (!the_locking_inset)
1912 updateLocal(bv, CURSOR, false);
1919 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1922 int w = UpdatableInset::getMaxWidth(bv, inset);
1927 w = w - top_x + owner()->x();
1930 w -= (2 * TEXT_TO_INSET_OFFSET);
1933 return UpdatableInset::getMaxWidth(bv, inset);
1938 void InsetText::setParagraphData(ParagraphList const & plist)
1940 // we have to unlock any locked inset otherwise we're in troubles
1941 the_locking_inset = 0;
1943 // But it it makes no difference that is a lot better.
1945 // See if this can be simplified when std::list is in effect.
1948 ParagraphList::const_iterator it = plist.begin();
1949 ParagraphList::const_iterator end = plist.end();
1950 for (; it != end; ++it) {
1951 paragraphs.push_back(*it);
1952 Paragraph & tmp = paragraphs.back();
1953 tmp.setInsetOwner(this);
1961 void InsetText::markNew(bool track_changes)
1963 ParagraphList::iterator pit = paragraphs.begin();
1964 ParagraphList::iterator pend = paragraphs.end();
1965 for (; pit != pend; ++pit) {
1966 if (track_changes) {
1967 pit->trackChanges();
1969 // no-op when not tracking
1970 pit->cleanChanges();
1976 void InsetText::setText(string const & data, LyXFont const & font)
1979 for (unsigned int i = 0; i < data.length(); ++i)
1980 paragraphs.begin()->insertChar(i, data[i], font);
1985 void InsetText::setAutoBreakRows(bool flag)
1987 if (flag != autoBreakRows) {
1988 autoBreakRows = flag;
1996 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1998 if (how != drawFrame_) {
2001 updateLocal(bv, DRAW_FRAME, false);
2006 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2008 if (frame_color != col) {
2011 updateLocal(bv, DRAW_FRAME, false);
2016 int InsetText::cx(BufferView * bv) const
2018 // we do nothing dangerous so we use a local cache
2019 LyXText * llt = getLyXText(bv);
2020 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2021 if (the_locking_inset) {
2022 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2024 if (font.isVisibleRightToLeft())
2025 x -= the_locking_inset->width(bv, font);
2031 int InsetText::cix(BufferView * bv) const
2033 // we do nothing dangerous so we use a local cache
2034 LyXText * llt = getLyXText(bv);
2035 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2036 if (the_locking_inset) {
2037 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2039 if (font.isVisibleRightToLeft())
2040 x -= the_locking_inset->width(bv, font);
2046 int InsetText::cy(BufferView * bv) const
2049 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2053 int InsetText::ciy(BufferView * bv) const
2056 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2060 pos_type InsetText::cpos(BufferView * bv) const
2062 return getLyXText(bv)->cursor.pos();
2066 ParagraphList::iterator InsetText::cpar(BufferView * bv) const
2068 return getLyXText(bv)->cursor.par();
2072 bool InsetText::cboundary(BufferView * bv) const
2074 return getLyXText(bv)->cursor.boundary();
2078 RowList::iterator InsetText::crow(BufferView * bv) const
2080 return getLyXText(bv)->cursorRow();
2084 LyXText * InsetText::getLyXText(BufferView const * lbv,
2085 bool const recursive) const
2087 if (cached_bview == lbv) {
2088 if (recursive && the_locking_inset)
2089 return the_locking_inset->getLyXText(lbv, true);
2090 LyXText * lt = cached_text.get();
2091 lyx::Assert(lt && lt->rows().begin()->par() == const_cast<ParagraphList&>(paragraphs).begin());
2094 // Super UGLY! (Lgb)
2095 BufferView * bv = const_cast<BufferView *>(lbv);
2098 Cache::iterator it = cache.find(bv);
2100 if (it != cache.end()) {
2103 } else if (do_resize) {
2104 resizeLyXText(do_resize);
2106 if (lt || !it->second.remove) {
2107 lyx::Assert(it->second.text.get());
2108 cached_text = it->second.text;
2109 if (recursive && the_locking_inset) {
2110 return the_locking_inset->getLyXText(bv, true);
2112 return cached_text.get();
2113 } else if (it->second.remove) {
2115 saveLyXTextState(it->second.text.get());
2117 sstate.lpar = const_cast<ParagraphList&>(paragraphs).end();
2121 // when we have to reinit the existing LyXText!
2123 it->second.text->init(bv);
2124 restoreLyXTextState(it->second.text.get());
2125 it->second.remove = false;
2127 cached_text = it->second.text;
2128 if (the_locking_inset && recursive) {
2129 return the_locking_inset->getLyXText(bv);
2131 return cached_text.get();
2134 // we are here only if we don't have a BufferView * in the cache!!!
2136 cached_text.reset(new LyXText(bv, const_cast<InsetText *>(this)));
2137 cached_text->init(bv);
2138 restoreLyXTextState(cached_text.get());
2140 cache.insert(make_pair(bv, cached_text));
2142 if (the_locking_inset && recursive) {
2143 return the_locking_inset->getLyXText(bv);
2145 return cached_text.get();
2149 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2153 Cache::iterator it = cache.find(bv);
2155 if (it == cache.end()) {
2159 lyx::Assert(it->second.text.get());
2161 it->second.remove = true;
2163 /// then remove all LyXText in text-insets
2164 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2165 const_cast<ParagraphList&>(paragraphs).end(),
2166 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2171 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2174 // we cannot resize this because we are in use!
2175 // so do this on the next possible getLyXText()
2180 if (paragraphs.size() == 1 && paragraphs.begin()->empty()) {
2181 // no data, resize not neccessary!
2182 // we have to do this as a fixed width may have changed!
2183 LyXText * t = getLyXText(bv);
2184 saveLyXTextState(t);
2186 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()) {
2199 lyx::Assert(it->second.text.get());
2201 LyXText * t = it->second.text.get();
2202 saveLyXTextState(t);
2204 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2205 const_cast<ParagraphList&>(paragraphs).end(),
2206 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2209 restoreLyXTextState(t);
2211 if (the_locking_inset) {
2212 inset_x = cix(bv) - top_x + drawTextXOffset;
2213 inset_y = ciy(bv) + drawTextYOffset;
2216 t->top_y(bv->screen().topCursorVisible(t));
2218 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2219 // this will scroll the screen such that the cursor becomes visible
2220 bv->updateScrollbar();
2222 need_update |= FULL;
2227 void InsetText::reinitLyXText() const
2230 // we cannot resize this because we are in use!
2231 // so do this on the next possible getLyXText()
2237 for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2238 lyx::Assert(it->second.text.get());
2240 LyXText * t = it->second.text.get();
2241 BufferView * bv = it->first;
2243 saveLyXTextState(t);
2245 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2246 const_cast<ParagraphList&>(paragraphs).end(),
2247 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2250 restoreLyXTextState(t);
2251 if (the_locking_inset) {
2252 inset_x = cix(bv) - top_x + drawTextXOffset;
2253 inset_y = ciy(bv) + drawTextYOffset;
2255 t->top_y(bv->screen().topCursorVisible(t));
2257 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2258 // this will scroll the screen such that the cursor becomes visible
2259 bv->updateScrollbar();
2267 void InsetText::removeNewlines()
2269 bool changed = false;
2271 ParagraphList::iterator it = paragraphs.begin();
2272 ParagraphList::iterator end = paragraphs.end();
2273 for (; it != end; ++it) {
2274 for (int i = 0; i < it->size(); ++i) {
2275 if (it->isNewline(i)) {
2286 bool InsetText::nodraw() const
2288 if (the_locking_inset)
2289 return the_locking_inset->nodraw();
2290 return UpdatableInset::nodraw();
2294 int InsetText::scroll(bool recursive) const
2296 int sx = UpdatableInset::scroll(false);
2298 if (recursive && the_locking_inset)
2299 sx += the_locking_inset->scroll(recursive);
2305 void InsetText::selectAll(BufferView * bv)
2307 getLyXText(bv)->cursorTop();
2308 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2309 getLyXText(bv)->cursorBottom();
2310 getLyXText(bv)->setSelection();
2314 void InsetText::clearSelection(BufferView * bv)
2316 getLyXText(bv)->clearSelection();
2320 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2322 Painter & pain = bv->painter();
2324 int h = dim_.asc + dim_.des;
2325 int ty = baseline - dim_.asc;
2331 if ((ty + h) > pain.paperHeight())
2332 h = pain.paperHeight();
2333 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2334 w = pain.paperWidth();
2335 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2340 ParagraphList * InsetText::getParagraphs(int i) const
2342 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2346 LyXCursor const & InsetText::cursor(BufferView * bv) const
2348 if (the_locking_inset)
2349 return the_locking_inset->cursor(bv);
2350 return getLyXText(bv)->cursor;
2354 Inset * InsetText::getInsetFromID(int id_arg) const
2357 return const_cast<InsetText *>(this);
2359 ParagraphList::const_iterator pit = paragraphs.begin();
2360 ParagraphList::const_iterator pend = paragraphs.end();
2361 for (; pit != pend; ++pit) {
2362 InsetList::const_iterator it = pit->insetlist.begin();
2363 InsetList::const_iterator end = pit->insetlist.end();
2364 for (; it != end; ++it) {
2365 if (it->inset->id() == id_arg)
2367 Inset * in = it->inset->getInsetFromID(id_arg);
2377 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2378 float & value) const
2384 lt = getLyXText(bv);
2387 if (the_locking_inset) {
2388 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2389 if (!word.word().empty()) {
2395 // we have to go on checking so move cursor to the next char
2396 lt->cursor.pos(lt->cursor.pos() + 1);
2398 word = lt->selectNextWordToSpellcheck(value);
2399 if (word.word().empty())
2400 bv->unlockInset(const_cast<InsetText *>(this));
2409 void InsetText::selectSelectedWord(BufferView * bv)
2411 if (the_locking_inset) {
2412 the_locking_inset->selectSelectedWord(bv);
2415 getLyXText(bv)->selectSelectedWord();
2416 updateLocal(bv, SELECTION, false);
2420 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2422 if (the_locking_inset) {
2423 the_locking_inset->toggleSelection(bv, kill_selection);
2427 lt = getLyXText(bv);
2431 int x = top_x + TEXT_TO_INSET_OFFSET;
2433 RowList::iterator rowit = lt->rows().begin();
2434 RowList::iterator end = lt->rows().end();
2435 int y_offset = top_baseline - rowit->ascent_of_text();
2437 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2438 y += rowit->height();
2444 if (need_update & SELECTION)
2446 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2452 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2456 lt = getLyXText(bv);
2459 if (the_locking_inset) {
2460 if (the_locking_inset->nextChange(bv, length))
2462 lt->cursorRight(true);
2464 lyxfind::SearchResult result =
2465 lyxfind::findNextChange(bv, lt, length);
2467 if (result == lyxfind::SR_FOUND) {
2468 LyXCursor cur = lt->cursor;
2469 bv->unlockInset(bv->theLockingInset());
2470 if (bv->lockInset(this))
2473 lt->setSelectionRange(length);
2474 updateLocal(bv, SELECTION, false);
2478 return result != lyxfind::SR_NOT_FOUND;
2482 bool InsetText::searchForward(BufferView * bv, string const & str,
2487 lt = getLyXText(bv);
2490 if (the_locking_inset) {
2491 if (the_locking_inset->searchForward(bv, str, cs, mw))
2493 lt->cursorRight(true);
2495 lyxfind::SearchResult result =
2496 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2498 if (result == lyxfind::SR_FOUND) {
2499 LyXCursor cur = lt->cursor;
2500 bv->unlockInset(bv->theLockingInset());
2501 if (bv->lockInset(this))
2504 lt->setSelectionRange(str.length());
2505 updateLocal(bv, SELECTION, false);
2509 return (result != lyxfind::SR_NOT_FOUND);
2512 bool InsetText::searchBackward(BufferView * bv, string const & str,
2515 if (the_locking_inset) {
2516 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2521 lt = getLyXText(bv);
2525 ParagraphList::iterator pit = paragraphs.begin();
2526 ParagraphList::iterator pend = paragraphs.end();
2528 while (boost::next(pit) != pend)
2531 lt->setCursor(pit, pit->size());
2533 lyxfind::SearchResult result =
2534 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2536 if (result == lyxfind::SR_FOUND) {
2537 LyXCursor cur = lt->cursor;
2538 bv->unlockInset(bv->theLockingInset());
2539 if (bv->lockInset(this))
2542 lt->setSelectionRange(str.length());
2543 updateLocal(bv, SELECTION, false);
2547 return (result != lyxfind::SR_NOT_FOUND);
2551 bool InsetText::checkInsertChar(LyXFont & font)
2554 return owner()->checkInsertChar(font);
2559 void InsetText::collapseParagraphs(BufferView * bv)
2561 LyXText * llt = getLyXText(bv);
2563 while (paragraphs.size() > 1) {
2564 ParagraphList::iterator first_par = paragraphs.begin();
2565 ParagraphList::iterator next_par = boost::next(first_par);
2566 size_t const first_par_size = first_par->size();
2568 if (!first_par->empty() &&
2569 !next_par->empty() &&
2570 !first_par->isSeparator(first_par_size - 1)) {
2571 first_par->insertChar(first_par_size, ' ');
2574 if (llt->selection.set()) {
2575 if (llt->selection.start.par() == next_par) {
2576 llt->selection.start.par(first_par);
2577 llt->selection.start.pos(
2578 llt->selection.start.pos() + first_par_size);
2580 if (llt->selection.end.par() == next_par) {
2581 llt->selection.end.par(first_par);
2582 llt->selection.end.pos(
2583 llt->selection.end.pos() + first_par_size);
2587 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2593 void InsetText::getDrawFont(LyXFont & font) const
2597 owner()->getDrawFont(font);
2601 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2603 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2604 // And it probably does. You have to take a look at this John. (Lgb)
2605 #warning John, have a look here. (Lgb)
2606 ParagraphList::iterator pit = plist.begin();
2607 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2609 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2611 ParagraphList::iterator pend = plist.end();
2612 for (; pit != pend; ++pit) {
2613 paragraphs.push_back(*pit);
2620 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2622 ParagraphList::const_iterator pit = paragraphs.begin();
2623 ParagraphList::const_iterator pend = paragraphs.end();
2625 for (; pit != pend; ++pit) {
2626 InsetList::const_iterator it = pit->insetlist.begin();
2627 InsetList::const_iterator end = pit->insetlist.end();
2628 for (; it != end; ++it) {
2629 it->inset->addPreview(loader);