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 = *t; // slicing intended
93 sstate.cursor.par(end);
97 void InsetText::restoreLyXTextState(LyXText * t) const
99 if (sstate.cursor.par() == const_cast<ParagraphList&>(paragraphs).end())
102 t->selection.set(true);
103 // at this point just to avoid the DEPM when setting the cursor
104 t->selection.mark(sstate.selection.mark());
105 if (sstate.selection.set()) {
106 t->setCursor(sstate.selection.start.par(),
107 sstate.selection.start.pos(),
108 true, sstate.selection.start.boundary());
109 t->selection.cursor = t->cursor;
110 t->setCursor(sstate.selection.end.par(), sstate.selection.end.pos(),
111 true, sstate.selection.end.boundary());
113 t->setCursor(sstate.cursor.par(), sstate.cursor.pos());
115 t->setCursor(sstate.cursor.par(), sstate.cursor.pos(),
116 true, sstate.cursor.boundary());
117 t->selection.cursor = t->cursor;
118 t->selection.set(false);
123 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
130 InsetText::InsetText(BufferParams const & bp)
131 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
134 paragraphs.push_back(Paragraph());
135 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
136 if (bp.tracking_changes)
137 paragraphs.begin()->trackChanges();
142 InsetText::InsetText(InsetText const & in)
143 : UpdatableInset(in), lt(0), in_update(false), do_resize(0),
150 InsetText & InsetText::operator=(InsetText const & it)
157 void InsetText::init(InsetText const * ins)
160 setParagraphData(ins->paragraphs);
161 autoBreakRows = ins->autoBreakRows;
162 drawFrame_ = ins->drawFrame_;
163 frame_color = ins->frame_color;
165 for_each(paragraphs.begin(), paragraphs.end(),
166 boost::bind(&Paragraph::setInsetOwner, _1, this));
168 the_locking_inset = 0;
170 frame_color = LColor::insetframe;
171 autoBreakRows = false;
180 old_par = paragraphs.end();
181 last_drawn_width = -1;
183 sstate.cursor.par(paragraphs.end());
184 in_insetAllowed = false;
188 void InsetText::clear(bool just_mark_erased)
190 if (just_mark_erased) {
191 ParagraphList::iterator it = paragraphs.begin();
192 ParagraphList::iterator end = paragraphs.end();
193 for (; it != end; ++it) {
200 // This is a gross hack...
201 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
204 paragraphs.push_back(Paragraph());
205 paragraphs.begin()->setInsetOwner(this);
206 paragraphs.begin()->layout(old_layout);
213 InsetBase * InsetText::clone() const
215 return new InsetText(*this);
219 void InsetText::write(Buffer const * buf, ostream & os) const
222 writeParagraphData(buf, os);
226 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
228 ParagraphList::const_iterator it = paragraphs.begin();
229 ParagraphList::const_iterator end = paragraphs.end();
230 Paragraph::depth_type dth = 0;
231 for (; it != end; ++it) {
232 it->write(buf, os, buf->params, dth);
237 void InsetText::read(Buffer const * buf, LyXLex & lex)
240 Paragraph::depth_type depth = 0;
244 if (buf->params.tracking_changes)
245 paragraphs.begin()->trackChanges();
247 // delete the initial paragraph
249 ParagraphList::iterator pit = paragraphs.begin();
253 token = lex.getString();
256 if (token == "\\end_inset") {
260 if (token == "\\the_end") {
261 lex.printError("\\the_end read in inset! Error in document!");
266 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
269 pit = paragraphs.begin();
270 ParagraphList::iterator const end = paragraphs.end();
271 for (; pit != end; ++pit)
272 pit->setInsetOwner(this);
274 if (token != "\\end_inset") {
275 lex.printError("Missing \\end_inset at this point. "
282 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
284 BufferView * bv = mi.base.bv;
285 LyXText * text = getLyXText(bv);
286 dim.asc = text->rows().begin()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
287 dim.des = text->height - dim.asc + TEXT_TO_INSET_OFFSET;
288 dim.wid = max(textWidth(bv), int(text->width)) + 2 * TEXT_TO_INSET_OFFSET;
289 dim.wid = max(dim.wid, 10);
294 int InsetText::textWidth(BufferView * bv, bool fordraw) const
296 int w = autoBreakRows ? getMaxWidth(bv, this) : -1;
299 return max(w - 2 * TEXT_TO_INSET_OFFSET,
300 (int)getLyXText(bv)->width);
305 return w - 2 * TEXT_TO_INSET_OFFSET;
309 void InsetText::draw(PainterInfo & pi, int x, int baseline) const
314 // update our idea of where we are. Clearly, we should
315 // not have to know this information.
319 int const start_x = x;
321 BufferView * bv = pi.base.bv;
322 Painter & pain = pi.pain;
324 // repaint the background if needed
325 if (backgroundColor() != LColor::background)
326 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
328 // no draw is necessary !!!
329 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
330 top_baseline = baseline;
338 top_baseline = baseline;
339 top_y = baseline - dim_.asc;
341 if (last_drawn_width != dim_.wid) {
343 last_drawn_width = dim_.wid;
346 if (the_locking_inset && (cpar(bv) == inset_par)
347 && (cpos(bv) == inset_pos)) {
348 inset_x = cix(bv) - int(x) + drawTextXOffset;
349 inset_y = ciy(bv) + drawTextYOffset;
357 x += TEXT_TO_INSET_OFFSET;
359 RowList::iterator rowit = lt->rows().begin();
360 RowList::iterator end = lt->rows().end();
362 int y_offset = baseline - rowit->ascent_of_text();
363 int ph = pain.paperHeight();
366 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
367 y += rowit->height();
368 first += rowit->height();
372 lt->top_y(-y_offset);
380 int yf = y_offset + first;
385 while ((rowit != end) && (yf < ph)) {
386 RowPainter rp(*bv, *lt, rowit);
387 rp.paint(y + y_offset + first, int(x), y + lt->top_y());
388 y += rowit->height();
389 yf += rowit->height();
395 if ((drawFrame_ == ALWAYS) || (drawFrame_ == LOCKED && locked)) {
396 drawFrame(pain, int(start_x));
399 x += dim_.wid - TEXT_TO_INSET_OFFSET;
401 if (need_update != INIT) {
409 void InsetText::drawFrame(Painter & pain, int x) const
411 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
413 frame_y = top_baseline - dim_.asc + ttoD2;
414 frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
415 frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
416 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
421 void InsetText::update(BufferView * bv, bool reinit)
424 if (reinit && owner()) {
426 owner()->update(bv, true);
432 if (reinit || need_update == INIT) {
434 // we should put this call where we set need_update to INIT!
437 owner()->update(bv, true);
440 int nw = getMaxWidth(bv, this);
441 if (nw > 0 && old_max_width != nw) {
448 if (!autoBreakRows && paragraphs.size() > 1)
449 collapseParagraphs(bv);
451 if (the_locking_inset) {
452 inset_x = cix(bv) - top_x + drawTextXOffset;
453 inset_y = ciy(bv) + drawTextYOffset;
454 the_locking_inset->update(bv, reinit);
462 if ((need_update & CURSOR_PAR) && (lt->refreshStatus() == LyXText::REFRESH_NONE) &&
464 lt->updateInset(the_locking_inset);
467 if (lt->refreshStatus() == LyXText::REFRESH_AREA)
474 int nw = getMaxWidth(bv, this);
475 if (nw > 0 && old_max_width != nw) {
482 void InsetText::setUpdateStatus(BufferView * bv, int what) const
484 // this does nothing dangerous so use only a localized buffer
485 LyXText * llt = getLyXText(bv);
488 // we have to redraw us full if our LyXText REFRESH_AREA or
489 // if we don't break row so that we only have one row to update!
490 if ((llt->refreshStatus() == LyXText::REFRESH_AREA) ||
492 (llt->refreshStatus() == LyXText::REFRESH_ROW)))
495 } else if (llt->refreshStatus() == LyXText::REFRESH_ROW) {
496 need_update |= CURSOR_PAR;
499 // this to not draw a selection when we redraw all of it!
500 if (need_update & CURSOR && !(need_update & SELECTION)) {
501 if (llt->selection.set())
503 llt->clearSelection();
508 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
510 if (!autoBreakRows && paragraphs.size() > 1)
511 collapseParagraphs(bv);
518 lt->partialRebreak();
519 setUpdateStatus(bv, what);
520 bool flag = mark_dirty ||
521 (((need_update != CURSOR) && (need_update != NONE)) ||
522 (lt->refreshStatus() != LyXText::REFRESH_NONE) || lt->selection.set());
523 if (!lt->selection.set())
524 lt->selection.cursor = lt->cursor;
530 bv->updateInset(const_cast<InsetText *>(this));
536 if (need_update == CURSOR)
538 bv->owner()->view_state_changed();
539 bv->owner()->updateMenubar();
540 bv->owner()->updateToolbar();
541 if (old_par != cpar(bv)) {
542 bv->owner()->setLayout(cpar(bv)->layout()->name());
548 string const InsetText::editMessage() const
550 return _("Opened Text Inset");
554 void InsetText::insetUnlock(BufferView * bv)
556 if (the_locking_inset) {
557 the_locking_inset->insetUnlock(bv);
558 the_locking_inset = 0;
559 updateLocal(bv, CURSOR_PAR, false);
570 if (lt->selection.set()) {
571 lt->clearSelection();
573 } else if (owner()) {
574 bv->owner()->setLayout(owner()->getLyXText(bv)
575 ->cursor.par()->layout()->name());
577 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
578 // hack for deleteEmptyParMech
579 ParagraphList::iterator first_par = paragraphs.begin();
580 if (!first_par->empty()) {
581 lt->setCursor(first_par, 0);
582 } else if (paragraphs.size() > 1) {
583 lt->setCursor(boost::next(first_par), 0);
588 updateLocal(bv, code, false);
591 setUpdateStatus(bv, code);
596 void InsetText::lockInset(BufferView * bv)
599 the_locking_inset = 0;
600 inset_pos = inset_x = inset_y = 0;
601 inset_boundary = false;
602 inset_par = paragraphs.end();
603 old_par = paragraphs.end();
609 lt->setCursor(paragraphs.begin(), 0);
610 lt->clearSelection();
612 // If the inset is empty set the language of the current font to the
613 // language to the surronding text (if different).
614 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
615 bv->getParentLanguage(this) != lt->current_font.language()) {
616 LyXFont font(LyXFont::ALL_IGNORE);
617 font.setLanguage(bv->getParentLanguage(this));
618 setFont(bv, font, false);
623 if (drawFrame_ == LOCKED)
624 code = CURSOR|DRAW_FRAME;
625 setUpdateStatus(bv, code);
629 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
631 the_locking_inset = inset;
632 inset_x = cix(bv) - top_x + drawTextXOffset;
633 inset_y = ciy(bv) + drawTextYOffset;
634 inset_pos = cpos(bv);
635 inset_par = cpar(bv);
636 inset_boundary = cboundary(bv);
637 updateLocal(bv, CURSOR, false);
641 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
643 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
647 if (!the_locking_inset) {
648 ParagraphList::iterator pit = paragraphs.begin();
649 ParagraphList::iterator pend = paragraphs.end();
651 int const id = inset->id();
652 for (; pit != pend; ++pit) {
653 InsetList::iterator it =
654 pit->insetlist.begin();
655 InsetList::iterator const end =
656 pit->insetlist.end();
657 for (; it != end; ++it) {
658 if (it->inset == inset) {
659 getLyXText(bv)->setCursorIntern(pit, it->pos);
660 lockInset(bv, inset);
663 if (it->inset->getInsetFromID(id)) {
664 getLyXText(bv)->setCursorIntern(pit, it->pos);
665 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
666 return the_locking_inset->lockInsetInInset(bv, inset);
672 if (inset == cpar(bv)->getInset(cpos(bv))) {
673 lyxerr[Debug::INSETS] << "OK" << endl;
674 lockInset(bv, inset);
676 } else if (the_locking_inset && (the_locking_inset == inset)) {
677 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
678 lyxerr[Debug::INSETS] << "OK" << endl;
679 inset_x = cix(bv) - top_x + drawTextXOffset;
680 inset_y = ciy(bv) + drawTextYOffset;
682 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
684 } else if (the_locking_inset) {
685 lyxerr[Debug::INSETS] << "MAYBE" << endl;
686 return the_locking_inset->lockInsetInInset(bv, inset);
688 lyxerr[Debug::INSETS] << "NOT OK" << endl;
693 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
696 if (!the_locking_inset)
698 if (the_locking_inset == inset) {
699 the_locking_inset->insetUnlock(bv);
700 getLyXText(bv)->updateInset(inset);
701 the_locking_inset = 0;
703 moveRightIntern(bv, true, false);
704 old_par = paragraphs.end(); // force layout setting
708 updateLocal(bv, CURSOR, false);
711 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
715 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
717 if (!autoBreakRows && paragraphs.size() > 1)
718 collapseParagraphs(bv);
728 if (inset->owner() != this) {
729 int ustat = CURSOR_PAR;
731 UpdatableInset * tl_inset = the_locking_inset;
733 found = tl_inset->updateInsetInInset(bv, inset);
735 tl_inset = static_cast<UpdatableInset *>(inset);
736 while(tl_inset->owner() && tl_inset->owner() != this)
737 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
738 if (!tl_inset->owner())
740 found = tl_inset->updateInsetInInset(bv, inset);
744 lt->updateInset(tl_inset);
748 setUpdateStatus(bv, ustat);
751 bool found = lt->updateInset(inset);
755 setUpdateStatus(bv, CURSOR_PAR);
756 if (the_locking_inset &&
757 cpar(bv) == inset_par && cpos(bv) == inset_pos)
759 inset_x = cix(bv) - top_x + drawTextXOffset;
760 inset_y = ciy(bv) + drawTextYOffset;
767 void InsetText::lfunMousePress(FuncRequest const & cmd)
771 // use this to check mouse motion for selection!
775 BufferView * bv = cmd.view();
776 FuncRequest cmd1 = cmd;
782 int tmp_x = cmd.x - drawTextXOffset;
783 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
784 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
786 if (the_locking_inset) {
787 if (the_locking_inset == inset) {
788 the_locking_inset->localDispatch(cmd1);
791 // otherwise only unlock the_locking_inset
792 the_locking_inset->insetUnlock(bv);
793 the_locking_inset = 0;
796 no_selection = false;
798 if (bv->theLockingInset()) {
799 if (isHighlyEditableInset(inset)) {
800 // We just have to lock the inset before calling a
802 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
803 if (!bv->lockInset(uinset)) {
804 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
806 inset->localDispatch(cmd1);
807 if (the_locking_inset)
808 updateLocal(bv, CURSOR, false);
813 bool paste_internally = false;
814 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
815 localDispatch(FuncRequest(bv, LFUN_COPY));
816 paste_internally = true;
823 int old_top_y = lt->top_y();
825 lt->setCursorFromCoordinates(cmd.x - drawTextXOffset,
827 // set the selection cursor!
828 lt->selection.cursor = lt->cursor;
829 lt->cursor.x_fix(lt->cursor.x());
831 if (lt->selection.set()) {
832 lt->clearSelection();
833 updateLocal(bv, FULL, false);
835 lt->clearSelection();
836 updateLocal(bv, CURSOR, false);
842 bv->owner()->setLayout(cpar(bv)->layout()->name());
844 // we moved the view we cannot do mouse selection in this case!
845 if (getLyXText(bv)->top_y() != old_top_y)
848 // Insert primary selection with middle mouse
849 // if there is a local selection in the current buffer,
851 if (cmd.button() == mouse_button::button2) {
852 if (paste_internally)
853 localDispatch(FuncRequest(bv, LFUN_PASTE));
855 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
858 getLyXText(bv)->clearSelection();
863 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
865 BufferView * bv = cmd.view();
866 FuncRequest cmd1 = cmd;
871 if (the_locking_inset)
872 return the_locking_inset->localDispatch(cmd1);
874 int tmp_x = cmd.x - drawTextXOffset;
875 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
876 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
879 // This code should probably be removed now. Simple insets
880 // (!highlyEditable) can actually take the localDispatch,
881 // and turn it into edit() if necessary. But we still
882 // need to deal properly with the whole relative vs.
883 // absolute mouse co-ords thing in a realiable, sensible way
885 if (isHighlyEditableInset(inset))
886 ret = inset->localDispatch(cmd1);
888 inset_x = cix(bv) - top_x + drawTextXOffset;
889 inset_y = ciy(bv) + drawTextYOffset;
890 cmd1.x = cmd.x - inset_x;
891 cmd1.y = cmd.x - inset_y;
892 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
896 ret = inset->localDispatch(cmd1);
897 updateLocal(bv, CURSOR_PAR, false);
904 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
906 FuncRequest cmd1 = cmd;
910 if (the_locking_inset) {
911 the_locking_inset->localDispatch(cmd1);
915 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
918 BufferView * bv = cmd.view();
924 LyXCursor cur = lt->cursor;
925 lt->setCursorFromCoordinates
926 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
927 lt->cursor.x_fix(lt->cursor.x());
928 if (cur == lt->cursor) {
934 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
935 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
939 updateLocal(bv, SELECTION, false);
944 Inset::RESULT InsetText::localDispatch(FuncRequest const & cmd)
946 BufferView * bv = cmd.view();
948 if (cmd.action == LFUN_INSET_EDIT) {
949 UpdatableInset::localDispatch(cmd);
951 if (!bv->lockInset(this)) {
952 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
957 the_locking_inset = 0;
958 inset_pos = inset_x = inset_y = 0;
959 inset_boundary = false;
960 inset_par = paragraphs.end();
961 old_par = paragraphs.end();
969 if (cmd.argument.size()) {
970 if (cmd.argument == "left")
971 lt->setCursor(paragraphs.begin(), 0);
973 ParagraphList::iterator it = paragraphs.begin();
974 ParagraphList::iterator end = paragraphs.end();
975 while (boost::next(it) != end)
977 // int const pos = (p->size() ? p->size()-1 : p->size());
978 lt->setCursor(it, it->size());
981 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
982 // we put here -1 and not button as now the button in the
983 // edit call should not be needed we will fix this in 1.3.x
984 // cycle hopefully (Jug 20020509)
985 // FIXME: GUII I've changed this to none: probably WRONG
986 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
987 lt->setCursorFromCoordinates(cmd.x - drawTextXOffset,
989 lt->cursor.x_fix(lt->cursor.x());
993 lt->clearSelection();
996 // If the inset is empty set the language of the current font to the
997 // language to the surronding text (if different).
998 if (paragraphs.begin()->empty() &&
999 paragraphs.size() == 1 &&
1000 bv->getParentLanguage(this) != lt->current_font.language())
1002 LyXFont font(LyXFont::ALL_IGNORE);
1003 font.setLanguage(bv->getParentLanguage(this));
1004 setFont(bv, font, false);
1010 if (drawFrame_ == LOCKED)
1011 code = CURSOR | DRAW_FRAME;
1013 updateLocal(bv, code, false);
1014 // Tell the paragraph dialog that we've entered an insettext.
1015 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1020 switch (cmd.action) {
1021 case LFUN_MOUSE_PRESS:
1022 lfunMousePress(cmd);
1024 case LFUN_MOUSE_MOTION:
1025 lfunMouseMotion(cmd);
1027 case LFUN_MOUSE_RELEASE:
1028 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
1033 bool was_empty = (paragraphs.begin()->empty() &&
1034 paragraphs.size() == 1);
1036 no_selection = false;
1037 RESULT result = UpdatableInset::localDispatch(cmd);
1038 if (result != UNDISPATCHED)
1041 result = DISPATCHED;
1042 if (cmd.action < 0 && cmd.argument.empty())
1045 if (the_locking_inset) {
1046 result = the_locking_inset->localDispatch(cmd);
1047 if (result == DISPATCHED_NOUPDATE)
1049 else if (result == DISPATCHED) {
1050 updateLocal(bv, CURSOR_PAR, false);
1052 } else if (result >= FINISHED) {
1054 case FINISHED_RIGHT:
1055 moveRightIntern(bv, false, false);
1056 result = DISPATCHED;
1059 if ((result = moveUp(bv)) >= FINISHED) {
1060 updateLocal(bv, CURSOR, false);
1061 bv->unlockInset(this);
1065 if ((result = moveDown(bv)) >= FINISHED) {
1066 updateLocal(bv, CURSOR, false);
1067 bv->unlockInset(this);
1071 result = DISPATCHED;
1074 the_locking_inset = 0;
1075 updateLocal(bv, CURSOR, false);
1076 // make sure status gets reset immediately
1077 bv->owner()->clearMessage();
1083 lt = getLyXText(bv);
1087 int updflag = false;
1089 // what type of update to do on a cursor movement
1090 int cursor_update = CURSOR;
1092 if (lt->selection.set())
1093 cursor_update = SELECTION;
1095 switch (cmd.action) {
1098 case LFUN_SELFINSERT:
1099 if (bv->buffer()->isReadonly()) {
1100 // setErrorMessage(N_("Document is read only"));
1103 if (!cmd.argument.empty()) {
1104 /* Automatically delete the currently selected
1105 * text and replace it with what is being
1106 * typed in now. Depends on lyxrc settings
1107 * "auto_region_delete", which defaults to
1110 // This should not be needed here and is also WRONG!
1111 setUndo(bv, Undo::INSERT, lt->cursor.par());
1114 if (lyxrc.auto_region_delete) {
1115 if (lt->selection.set()) {
1116 lt->cutSelection(false, false);
1119 lt->clearSelection();
1120 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
1121 bv->owner()->getIntl().getTransManager().
1122 TranslateAndInsert(cmd.argument[i], lt);
1125 lt->selection.cursor = lt->cursor;
1126 updwhat = CURSOR | CURSOR_PAR;
1128 result = DISPATCHED_NOUPDATE;
1131 // cursor movements that need special handling
1134 result = moveRight(bv);
1136 updwhat = cursor_update;
1140 result = moveLeft(bv);
1141 updwhat = cursor_update;
1145 result = moveDown(bv);
1146 updwhat = cursor_update;
1150 result = moveUp(bv);
1151 updwhat = cursor_update;
1155 if (crow(bv) == lt->rows().begin())
1156 result = FINISHED_UP;
1158 lt->cursorPrevious();
1159 lt->clearSelection();
1160 result = DISPATCHED_NOUPDATE;
1162 updwhat = cursor_update;
1166 if (boost::next(crow(bv)) == lt->rows().end())
1167 result = FINISHED_DOWN;
1170 lt->clearSelection();
1171 result = DISPATCHED_NOUPDATE;
1173 updwhat = cursor_update;
1176 case LFUN_BACKSPACE: {
1177 if (lt->selection.set())
1178 lt->cutSelection(true, false);
1181 updwhat = CURSOR_PAR;
1187 if (lt->selection.set()) {
1188 lt->cutSelection(true, false);
1192 updwhat = CURSOR_PAR;
1198 lt->cutSelection(true, true);
1199 updwhat = CURSOR_PAR;
1206 lt->copySelection();
1207 updwhat = CURSOR_PAR;
1210 case LFUN_PASTESELECTION:
1212 string const clip(bv->getClipboard());
1216 if (cmd.argument == "paragraph") {
1217 lt->insertStringAsParagraphs(clip);
1219 lt->insertStringAsLines(clip);
1222 lt->clearSelection();
1224 updwhat = CURSOR_PAR;
1230 if (!autoBreakRows) {
1231 if (CutAndPaste::nrOfParagraphs() > 1) {
1232 #ifdef WITH_WARNINGS
1233 #warning FIXME horrendously bad UI
1235 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1240 size_t sel_index = 0;
1241 string const & arg = cmd.argument;
1242 if (isStrUnsignedInt(arg)) {
1243 size_t const paste_arg = strToUnsignedInt(arg);
1244 #warning FIXME Check if the arg is in the domain of available selections.
1245 sel_index = paste_arg;
1247 lt->pasteSelection(sel_index);
1249 lt->clearSelection();
1250 updwhat = CURSOR_PAR;
1255 case LFUN_BREAKPARAGRAPH:
1256 if (!autoBreakRows) {
1257 result = DISPATCHED;
1260 lt->breakParagraph(paragraphs, 0);
1261 updwhat = CURSOR | FULL;
1265 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1266 if (!autoBreakRows) {
1267 result = DISPATCHED;
1270 lt->breakParagraph(paragraphs, 1);
1271 updwhat = CURSOR | FULL;
1275 case LFUN_BREAKLINE: {
1276 if (!autoBreakRows) {
1277 result = DISPATCHED;
1281 lt->insertInset(new InsetNewline);
1282 updwhat = CURSOR | CURSOR_PAR;
1288 // do not set layouts on non breakable textinsets
1289 if (autoBreakRows) {
1290 string cur_layout = cpar(bv)->layout()->name();
1292 // Derive layout number from given argument (string)
1293 // and current buffer's textclass (number). */
1294 LyXTextClass const & tclass =
1295 bv->buffer()->params.getLyXTextClass();
1296 string layout = cmd.argument;
1297 bool hasLayout = tclass.hasLayout(layout);
1299 // If the entry is obsolete, use the new one instead.
1301 string const & obs =
1302 tclass[layout]->obsoleted_by();
1307 // see if we found the layout number:
1309 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1310 bv->owner()->dispatch(lf);
1314 if (cur_layout != layout) {
1315 cur_layout = layout;
1316 lt->setLayout(layout);
1317 bv->owner()->setLayout(cpar(bv)->layout()->name());
1318 updwhat = CURSOR_PAR;
1322 // reset the layout box
1323 bv->owner()->setLayout(cpar(bv)->layout()->name());
1326 case LFUN_PARAGRAPH_SPACING:
1327 // This one is absolutely not working. When fiddling with this
1328 // it also seems to me that the paragraphs inside the insettext
1329 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1330 // FIXME: how old is this comment ? ...
1332 ParagraphList::iterator pit = lt->cursor.par();
1333 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1334 float cur_value = 1.0;
1335 if (cur_spacing == Spacing::Other) {
1336 cur_value = pit->params().spacing().getValue();
1339 istringstream istr(STRCONV(cmd.argument));
1342 Spacing::Space new_spacing = cur_spacing;
1343 float new_value = cur_value;
1345 lyxerr << "Missing argument to `paragraph-spacing'"
1347 } else if (tmp == "single") {
1348 new_spacing = Spacing::Single;
1349 } else if (tmp == "onehalf") {
1350 new_spacing = Spacing::Onehalf;
1351 } else if (tmp == "double") {
1352 new_spacing = Spacing::Double;
1353 } else if (tmp == "other") {
1354 new_spacing = Spacing::Other;
1357 lyxerr << "new_value = " << tmpval << endl;
1360 } else if (tmp == "default") {
1361 new_spacing = Spacing::Default;
1363 lyxerr << _("Unknown spacing argument: ")
1364 << cmd.argument << endl;
1366 if (cur_spacing != new_spacing || cur_value != new_value) {
1367 pit->params().spacing(Spacing(new_spacing, new_value));
1368 updwhat = CURSOR_PAR;
1374 // These need to do repaints but don't require
1375 // special handling otherwise. A *lot* of the
1376 // above could probably be done similarly ...
1381 case LFUN_WORDRIGHT:
1382 // these two are really unhandled ...
1384 case LFUN_BEGINNINGBUF:
1385 updwhat = cursor_update;
1386 if (!bv->dispatch(cmd))
1387 result = UNDISPATCHED;
1396 case LFUN_WORDLEFTSEL:
1397 case LFUN_WORDRIGHTSEL:
1398 updwhat = SELECTION;
1403 if (!bv->dispatch(cmd))
1404 result = UNDISPATCHED;
1411 updateLocal(bv, updwhat, updflag);
1412 /// If the action has deleted all text in the inset, we need to change the
1413 // language to the language of the surronding text.
1414 if (!was_empty && paragraphs.begin()->empty() &&
1415 paragraphs.size() == 1) {
1416 LyXFont font(LyXFont::ALL_IGNORE);
1417 font.setLanguage(bv->getParentLanguage(this));
1418 setFont(bv, font, false);
1421 if (result >= FINISHED)
1422 bv->unlockInset(this);
1424 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1425 result = DISPATCHED;
1430 int InsetText::latex(Buffer const * buf, ostream & os,
1431 LatexRunParams const & runparams) const
1434 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1435 return texrow.rows();
1439 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1441 unsigned int lines = 0;
1443 ParagraphList::const_iterator beg = paragraphs.begin();
1444 ParagraphList::const_iterator end = paragraphs.end();
1445 ParagraphList::const_iterator it = beg;
1446 for (; it != end; ++it) {
1447 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1448 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1455 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1457 unsigned int lines = 0;
1459 vector<string> environment_stack(10);
1460 vector<string> environment_inner(10);
1462 int const command_depth = 0;
1465 Paragraph::depth_type depth = 0; // paragraph depth
1467 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1468 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1470 for (; pit != pend; ++pit) {
1472 int desc_on = 0; // description mode
1474 LyXLayout_ptr const & style = pit->layout();
1476 // environment tag closing
1477 for (; depth > pit->params().depth(); --depth) {
1478 if (environment_inner[depth] != "!-- --") {
1479 item_name = "listitem";
1480 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1481 if (environment_inner[depth] == "varlistentry")
1482 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1484 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1485 environment_stack[depth].erase();
1486 environment_inner[depth].erase();
1489 if (depth == pit->params().depth()
1490 && environment_stack[depth] != style->latexname()
1491 && !environment_stack[depth].empty()) {
1492 if (environment_inner[depth] != "!-- --") {
1493 item_name= "listitem";
1494 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1495 if (environment_inner[depth] == "varlistentry")
1496 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1499 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1501 environment_stack[depth].erase();
1502 environment_inner[depth].erase();
1505 // Write opening SGML tags.
1506 switch (style->latextype) {
1507 case LATEX_PARAGRAPH:
1508 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1512 buf->parseError(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1516 case LATEX_ENVIRONMENT:
1517 case LATEX_ITEM_ENVIRONMENT:
1518 if (depth < pit->params().depth()) {
1519 depth = pit->params().depth();
1520 environment_stack[depth].erase();
1523 if (environment_stack[depth] != style->latexname()) {
1524 if (environment_stack.size() == depth + 1) {
1525 environment_stack.push_back("!-- --");
1526 environment_inner.push_back("!-- --");
1528 environment_stack[depth] = style->latexname();
1529 environment_inner[depth] = "!-- --";
1530 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1532 if (environment_inner[depth] != "!-- --") {
1533 item_name= "listitem";
1534 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1535 if (environment_inner[depth] == "varlistentry")
1536 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1540 if (style->latextype == LATEX_ENVIRONMENT) {
1541 if (!style->latexparam().empty()) {
1542 if (style->latexparam() == "CDATA")
1545 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1550 desc_on = (style->labeltype == LABEL_MANUAL);
1552 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1553 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1555 item_name = desc_on ? "term" : "para";
1556 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1560 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1564 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1567 // write closing SGML tags
1568 switch (style->latextype) {
1569 case LATEX_ENVIRONMENT:
1570 if (!style->latexparam().empty()) {
1571 if (style->latexparam() == "CDATA")
1574 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1577 case LATEX_ITEM_ENVIRONMENT:
1578 if (desc_on == 1) break;
1580 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1582 case LATEX_PARAGRAPH:
1583 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1586 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1592 for (int d = depth; d >= 0; --d) {
1593 if (!environment_stack[depth].empty()) {
1594 if (environment_inner[depth] != "!-- --") {
1595 item_name = "listitem";
1596 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1597 if (environment_inner[depth] == "varlistentry")
1598 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1601 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1609 void InsetText::validate(LaTeXFeatures & features) const
1611 for_each(paragraphs.begin(), paragraphs.end(),
1612 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1616 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1618 if (the_locking_inset) {
1619 the_locking_inset->getCursor(bv, x, y);
1623 y = cy(&bv) + InsetText::y();
1627 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1629 if (the_locking_inset) {
1630 the_locking_inset->getCursorPos(bv, x, y);
1633 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1634 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1638 int InsetText::insetInInsetY() const
1640 if (!the_locking_inset)
1643 return (inset_y + the_locking_inset->insetInInsetY());
1647 void InsetText::fitInsetCursor(BufferView * bv) const
1649 if (the_locking_inset) {
1650 the_locking_inset->fitInsetCursor(bv);
1653 LyXFont const font =
1654 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1656 int const asc = font_metrics::maxAscent(font);
1657 int const desc = font_metrics::maxDescent(font);
1659 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1660 need_update |= FULL;
1665 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1667 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1668 return moveLeftIntern(bv, false, activate_inset, selecting);
1670 return moveRightIntern(bv, true, activate_inset, selecting);
1675 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1677 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1678 return moveRightIntern(bv, true, activate_inset, selecting);
1680 return moveLeftIntern(bv, false, activate_inset, selecting);
1685 InsetText::moveRightIntern(BufferView * bv, bool front,
1686 bool activate_inset, bool selecting)
1688 LyXText * text = getLyXText(bv);
1690 ParagraphList::iterator c_par = cpar(bv);
1692 if (boost::next(c_par) == paragraphs.end() &&
1693 (cpos(bv) >= c_par->size()))
1694 return FINISHED_RIGHT;
1695 if (activate_inset && checkAndActivateInset(bv, front))
1697 text->cursorRight(bv);
1699 text->clearSelection();
1700 return DISPATCHED_NOUPDATE;
1705 InsetText::moveLeftIntern(BufferView * bv, bool front,
1706 bool activate_inset, bool selecting)
1708 LyXText * text = getLyXText(bv);
1710 if (cpar(bv) == paragraphs.begin() && (cpos(bv) <= 0))
1712 text->cursorLeft(bv);
1714 text->clearSelection();
1715 if (activate_inset && checkAndActivateInset(bv, front))
1717 return DISPATCHED_NOUPDATE;
1721 Inset::RESULT InsetText::moveUp(BufferView * bv)
1723 if (crow(bv) == getLyXText(bv)->rows().begin())
1725 getLyXText(bv)->cursorUp(bv);
1726 getLyXText(bv)->clearSelection();
1727 return DISPATCHED_NOUPDATE;
1731 Inset::RESULT InsetText::moveDown(BufferView * bv)
1733 if (boost::next(crow(bv)) == getLyXText(bv)->rows().end())
1734 return FINISHED_DOWN;
1735 getLyXText(bv)->cursorDown(bv);
1736 getLyXText(bv)->clearSelection();
1737 return DISPATCHED_NOUPDATE;
1741 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1743 if (the_locking_inset) {
1744 if (the_locking_inset->insetAllowed(inset))
1745 return the_locking_inset->insertInset(bv, inset);
1748 inset->setOwner(this);
1749 getLyXText(bv)->insertInset(inset);
1751 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1756 bool InsetText::insetAllowed(Inset::Code code) const
1758 // in_insetAllowed is a really gross hack,
1759 // to allow us to call the owner's insetAllowed
1760 // without stack overflow, which can happen
1761 // when the owner uses InsetCollapsable::insetAllowed()
1763 if (in_insetAllowed)
1765 in_insetAllowed = true;
1766 if (the_locking_inset)
1767 ret = the_locking_inset->insetAllowed(code);
1769 ret = owner()->insetAllowed(code);
1770 in_insetAllowed = false;
1775 UpdatableInset * InsetText::getLockingInset() const
1777 return the_locking_inset ? the_locking_inset->getLockingInset() :
1778 const_cast<InsetText *>(this);
1782 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1786 if (the_locking_inset)
1787 return the_locking_inset->getFirstLockingInsetOfType(c);
1792 bool InsetText::showInsetDialog(BufferView * bv) const
1794 if (the_locking_inset)
1795 return the_locking_inset->showInsetDialog(bv);
1800 void InsetText::getLabelList(std::vector<string> & list) const
1802 ParagraphList::const_iterator pit = paragraphs.begin();
1803 ParagraphList::const_iterator pend = paragraphs.end();
1804 for (; pit != pend; ++pit) {
1805 InsetList::const_iterator beg = pit->insetlist.begin();
1806 InsetList::const_iterator end = pit->insetlist.end();
1807 for (; beg != end; ++beg)
1808 beg->inset->getLabelList(list);
1813 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1816 if (the_locking_inset) {
1817 the_locking_inset->setFont(bv, font, toggleall, selectall);
1821 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1822 || cpar(bv)->empty()) {
1823 getLyXText(bv)->setFont(font, toggleall);
1829 lt = getLyXText(bv);
1833 if (lt->selection.set()) {
1834 setUndo(bv, Undo::EDIT, lt->cursor.par());
1840 lt->toggleFree(font, toggleall);
1843 lt->clearSelection();
1847 bool flag = (selectall || lt->selection.set());
1853 updateLocal(bv, FULL, true);
1855 updateLocal(bv, CURSOR_PAR, true);
1859 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1861 if (cpar(bv)->isInset(cpos(bv))) {
1863 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1864 if (!isHighlyEditableInset(inset))
1866 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1867 inset->localDispatch(cmd);
1868 if (!the_locking_inset)
1870 updateLocal(bv, CURSOR, false);
1877 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1878 mouse_button::state button)
1880 x -= drawTextXOffset;
1882 int dummyy = y + dim_.asc;
1883 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1884 // we only do the edit() call if the inset was hit by the mouse
1885 // or if it is a highly editable inset. So we should call this
1886 // function from our own edit with button < 0.
1887 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1889 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1897 inset_x = cix(bv) - top_x + drawTextXOffset;
1898 inset_y = ciy(bv) + drawTextYOffset;
1899 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1900 inset->localDispatch(cmd);
1901 if (!the_locking_inset)
1903 updateLocal(bv, CURSOR, false);
1910 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1913 int w = UpdatableInset::getMaxWidth(bv, inset);
1918 w = w - top_x + owner()->x();
1921 w -= (2 * TEXT_TO_INSET_OFFSET);
1924 return UpdatableInset::getMaxWidth(bv, inset);
1929 void InsetText::setParagraphData(ParagraphList const & plist)
1931 // we have to unlock any locked inset otherwise we're in troubles
1932 the_locking_inset = 0;
1934 // But it it makes no difference that is a lot better.
1936 // See if this can be simplified when std::list is in effect.
1939 ParagraphList::const_iterator it = plist.begin();
1940 ParagraphList::const_iterator end = plist.end();
1941 for (; it != end; ++it) {
1942 paragraphs.push_back(*it);
1943 Paragraph & tmp = paragraphs.back();
1944 tmp.setInsetOwner(this);
1952 void InsetText::markNew(bool track_changes)
1954 ParagraphList::iterator pit = paragraphs.begin();
1955 ParagraphList::iterator pend = paragraphs.end();
1956 for (; pit != pend; ++pit) {
1957 if (track_changes) {
1958 pit->trackChanges();
1960 // no-op when not tracking
1961 pit->cleanChanges();
1967 void InsetText::setText(string const & data, LyXFont const & font)
1970 for (unsigned int i = 0; i < data.length(); ++i)
1971 paragraphs.begin()->insertChar(i, data[i], font);
1976 void InsetText::setAutoBreakRows(bool flag)
1978 if (flag != autoBreakRows) {
1979 autoBreakRows = flag;
1987 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1989 if (how != drawFrame_) {
1992 updateLocal(bv, DRAW_FRAME, false);
1997 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1999 if (frame_color != col) {
2002 updateLocal(bv, DRAW_FRAME, false);
2007 int InsetText::cx(BufferView * bv) const
2009 // we do nothing dangerous so we use a local cache
2010 LyXText * llt = getLyXText(bv);
2011 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2012 if (the_locking_inset) {
2013 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2015 if (font.isVisibleRightToLeft())
2016 x -= the_locking_inset->width(bv, font);
2022 int InsetText::cix(BufferView * bv) const
2024 // we do nothing dangerous so we use a local cache
2025 LyXText * llt = getLyXText(bv);
2026 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2027 if (the_locking_inset) {
2028 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2030 if (font.isVisibleRightToLeft())
2031 x -= the_locking_inset->width(bv, font);
2037 int InsetText::cy(BufferView * bv) const
2040 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2044 int InsetText::ciy(BufferView * bv) const
2047 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2051 pos_type InsetText::cpos(BufferView * bv) const
2053 return getLyXText(bv)->cursor.pos();
2057 ParagraphList::iterator InsetText::cpar(BufferView * bv) const
2059 return getLyXText(bv)->cursor.par();
2063 bool InsetText::cboundary(BufferView * bv) const
2065 return getLyXText(bv)->cursor.boundary();
2069 RowList::iterator InsetText::crow(BufferView * bv) const
2071 return getLyXText(bv)->cursorRow();
2075 LyXText * InsetText::getLyXText(BufferView const * lbv,
2076 bool const recursive) const
2078 if (cached_bview == lbv) {
2079 if (recursive && the_locking_inset)
2080 return the_locking_inset->getLyXText(lbv, true);
2081 LyXText * lt = cached_text.get();
2082 lyx::Assert(lt && lt->rows().begin()->par() == const_cast<ParagraphList&>(paragraphs).begin());
2085 // Super UGLY! (Lgb)
2086 BufferView * bv = const_cast<BufferView *>(lbv);
2089 Cache::iterator it = cache.find(bv);
2091 if (it != cache.end()) {
2094 } else if (do_resize) {
2095 resizeLyXText(do_resize);
2097 if (lt || !it->second.remove) {
2098 lyx::Assert(it->second.text.get());
2099 cached_text = it->second.text;
2100 if (recursive && the_locking_inset) {
2101 return the_locking_inset->getLyXText(bv, true);
2103 return cached_text.get();
2104 } else if (it->second.remove) {
2106 saveLyXTextState(it->second.text.get());
2108 sstate.cursor.par(const_cast<ParagraphList&>(paragraphs).end());
2112 // when we have to reinit the existing LyXText!
2114 it->second.text->init(bv);
2115 restoreLyXTextState(it->second.text.get());
2116 it->second.remove = false;
2118 cached_text = it->second.text;
2119 if (the_locking_inset && recursive) {
2120 return the_locking_inset->getLyXText(bv);
2122 return cached_text.get();
2125 // we are here only if we don't have a BufferView * in the cache!!!
2127 cached_text.reset(new LyXText(bv, const_cast<InsetText *>(this)));
2128 cached_text->init(bv);
2129 restoreLyXTextState(cached_text.get());
2131 cache.insert(make_pair(bv, cached_text));
2133 if (the_locking_inset && recursive) {
2134 return the_locking_inset->getLyXText(bv);
2136 return cached_text.get();
2140 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2144 Cache::iterator it = cache.find(bv);
2146 if (it == cache.end()) {
2150 lyx::Assert(it->second.text.get());
2152 it->second.remove = true;
2154 /// then remove all LyXText in text-insets
2155 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2156 const_cast<ParagraphList&>(paragraphs).end(),
2157 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2162 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2165 // we cannot resize this because we are in use!
2166 // so do this on the next possible getLyXText()
2171 if (paragraphs.size() == 1 && paragraphs.begin()->empty()) {
2172 // no data, resize not neccessary!
2173 // we have to do this as a fixed width may have changed!
2174 LyXText * t = getLyXText(bv);
2175 saveLyXTextState(t);
2177 restoreLyXTextState(t);
2181 // one endless line, resize normally not necessary
2182 if (!force && getMaxWidth(bv, this) < 0)
2185 Cache::iterator it = cache.find(bv);
2186 if (it == cache.end()) {
2190 lyx::Assert(it->second.text.get());
2192 LyXText * t = it->second.text.get();
2193 saveLyXTextState(t);
2195 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2196 const_cast<ParagraphList&>(paragraphs).end(),
2197 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2200 restoreLyXTextState(t);
2202 if (the_locking_inset) {
2203 inset_x = cix(bv) - top_x + drawTextXOffset;
2204 inset_y = ciy(bv) + drawTextYOffset;
2207 t->top_y(bv->screen().topCursorVisible(t));
2209 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2210 // this will scroll the screen such that the cursor becomes visible
2211 bv->updateScrollbar();
2213 need_update |= FULL;
2218 void InsetText::reinitLyXText() const
2221 // we cannot resize this because we are in use!
2222 // so do this on the next possible getLyXText()
2228 for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2229 lyx::Assert(it->second.text.get());
2231 LyXText * t = it->second.text.get();
2232 BufferView * bv = it->first;
2234 saveLyXTextState(t);
2236 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2237 const_cast<ParagraphList&>(paragraphs).end(),
2238 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2241 restoreLyXTextState(t);
2242 if (the_locking_inset) {
2243 inset_x = cix(bv) - top_x + drawTextXOffset;
2244 inset_y = ciy(bv) + drawTextYOffset;
2246 t->top_y(bv->screen().topCursorVisible(t));
2248 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2249 // this will scroll the screen such that the cursor becomes visible
2250 bv->updateScrollbar();
2258 void InsetText::removeNewlines()
2260 bool changed = false;
2262 ParagraphList::iterator it = paragraphs.begin();
2263 ParagraphList::iterator end = paragraphs.end();
2264 for (; it != end; ++it) {
2265 for (int i = 0; i < it->size(); ++i) {
2266 if (it->isNewline(i)) {
2277 bool InsetText::nodraw() const
2279 if (the_locking_inset)
2280 return the_locking_inset->nodraw();
2281 return UpdatableInset::nodraw();
2285 int InsetText::scroll(bool recursive) const
2287 int sx = UpdatableInset::scroll(false);
2289 if (recursive && the_locking_inset)
2290 sx += the_locking_inset->scroll(recursive);
2296 void InsetText::selectAll(BufferView * bv)
2298 getLyXText(bv)->cursorTop();
2299 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2300 getLyXText(bv)->cursorBottom();
2301 getLyXText(bv)->setSelection();
2305 void InsetText::clearSelection(BufferView * bv)
2307 getLyXText(bv)->clearSelection();
2311 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2313 Painter & pain = bv->painter();
2315 int h = dim_.asc + dim_.des;
2316 int ty = baseline - dim_.asc;
2322 if ((ty + h) > pain.paperHeight())
2323 h = pain.paperHeight();
2324 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2325 w = pain.paperWidth();
2326 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2331 ParagraphList * InsetText::getParagraphs(int i) const
2333 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2337 LyXCursor const & InsetText::cursor(BufferView * bv) const
2339 if (the_locking_inset)
2340 return the_locking_inset->cursor(bv);
2341 return getLyXText(bv)->cursor;
2345 Inset * InsetText::getInsetFromID(int id_arg) const
2348 return const_cast<InsetText *>(this);
2350 ParagraphList::const_iterator pit = paragraphs.begin();
2351 ParagraphList::const_iterator pend = paragraphs.end();
2352 for (; pit != pend; ++pit) {
2353 InsetList::const_iterator it = pit->insetlist.begin();
2354 InsetList::const_iterator end = pit->insetlist.end();
2355 for (; it != end; ++it) {
2356 if (it->inset->id() == id_arg)
2358 Inset * in = it->inset->getInsetFromID(id_arg);
2368 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2369 float & value) const
2375 lt = getLyXText(bv);
2378 if (the_locking_inset) {
2379 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2380 if (!word.word().empty()) {
2386 // we have to go on checking so move cursor to the next char
2387 lt->cursor.pos(lt->cursor.pos() + 1);
2389 word = lt->selectNextWordToSpellcheck(value);
2390 if (word.word().empty())
2391 bv->unlockInset(const_cast<InsetText *>(this));
2400 void InsetText::selectSelectedWord(BufferView * bv)
2402 if (the_locking_inset) {
2403 the_locking_inset->selectSelectedWord(bv);
2406 getLyXText(bv)->selectSelectedWord();
2407 updateLocal(bv, SELECTION, false);
2411 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2413 if (the_locking_inset) {
2414 the_locking_inset->toggleSelection(bv, kill_selection);
2418 lt = getLyXText(bv);
2422 int x = top_x + TEXT_TO_INSET_OFFSET;
2424 RowList::iterator rowit = lt->rows().begin();
2425 RowList::iterator end = lt->rows().end();
2426 int y_offset = top_baseline - rowit->ascent_of_text();
2428 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2429 y += rowit->height();
2435 if (need_update & SELECTION)
2437 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2443 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2447 lt = getLyXText(bv);
2450 if (the_locking_inset) {
2451 if (the_locking_inset->nextChange(bv, length))
2453 lt->cursorRight(true);
2455 lyxfind::SearchResult result =
2456 lyxfind::findNextChange(bv, lt, length);
2458 if (result == lyxfind::SR_FOUND) {
2459 LyXCursor cur = lt->cursor;
2460 bv->unlockInset(bv->theLockingInset());
2461 if (bv->lockInset(this))
2464 lt->setSelectionRange(length);
2465 updateLocal(bv, SELECTION, false);
2469 return result != lyxfind::SR_NOT_FOUND;
2473 bool InsetText::searchForward(BufferView * bv, string const & str,
2478 lt = getLyXText(bv);
2481 if (the_locking_inset) {
2482 if (the_locking_inset->searchForward(bv, str, cs, mw))
2484 lt->cursorRight(true);
2486 lyxfind::SearchResult result =
2487 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2489 if (result == lyxfind::SR_FOUND) {
2490 LyXCursor cur = lt->cursor;
2491 bv->unlockInset(bv->theLockingInset());
2492 if (bv->lockInset(this))
2495 lt->setSelectionRange(str.length());
2496 updateLocal(bv, SELECTION, false);
2500 return (result != lyxfind::SR_NOT_FOUND);
2503 bool InsetText::searchBackward(BufferView * bv, string const & str,
2506 if (the_locking_inset) {
2507 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2512 lt = getLyXText(bv);
2516 ParagraphList::iterator pit = paragraphs.begin();
2517 ParagraphList::iterator pend = paragraphs.end();
2519 while (boost::next(pit) != pend)
2522 lt->setCursor(pit, pit->size());
2524 lyxfind::SearchResult result =
2525 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2527 if (result == lyxfind::SR_FOUND) {
2528 LyXCursor cur = lt->cursor;
2529 bv->unlockInset(bv->theLockingInset());
2530 if (bv->lockInset(this))
2533 lt->setSelectionRange(str.length());
2534 updateLocal(bv, SELECTION, false);
2538 return (result != lyxfind::SR_NOT_FOUND);
2542 bool InsetText::checkInsertChar(LyXFont & font)
2545 return owner()->checkInsertChar(font);
2550 void InsetText::collapseParagraphs(BufferView * bv)
2552 LyXText * llt = getLyXText(bv);
2554 while (paragraphs.size() > 1) {
2555 ParagraphList::iterator first_par = paragraphs.begin();
2556 ParagraphList::iterator next_par = boost::next(first_par);
2557 size_t const first_par_size = first_par->size();
2559 if (!first_par->empty() &&
2560 !next_par->empty() &&
2561 !first_par->isSeparator(first_par_size - 1)) {
2562 first_par->insertChar(first_par_size, ' ');
2565 if (llt->selection.set()) {
2566 if (llt->selection.start.par() == next_par) {
2567 llt->selection.start.par(first_par);
2568 llt->selection.start.pos(
2569 llt->selection.start.pos() + first_par_size);
2571 if (llt->selection.end.par() == next_par) {
2572 llt->selection.end.par(first_par);
2573 llt->selection.end.pos(
2574 llt->selection.end.pos() + first_par_size);
2578 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2584 void InsetText::getDrawFont(LyXFont & font) const
2588 owner()->getDrawFont(font);
2592 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2594 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2595 // And it probably does. You have to take a look at this John. (Lgb)
2596 #warning John, have a look here. (Lgb)
2597 ParagraphList::iterator pit = plist.begin();
2598 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2600 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2602 ParagraphList::iterator pend = plist.end();
2603 for (; pit != pend; ++pit) {
2604 paragraphs.push_back(*pit);
2611 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2613 ParagraphList::const_iterator pit = paragraphs.begin();
2614 ParagraphList::const_iterator pend = paragraphs.end();
2616 for (; pit != pend; ++pit) {
2617 InsetList::const_iterator it = pit->insetlist.begin();
2618 InsetList::const_iterator end = pit->insetlist.end();
2619 for (; it != end; ++it) {
2620 it->inset->addPreview(loader);