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>
73 using namespace lyx::support;
76 using lyx::textclass_type;
78 namespace grfx = lyx::graphics;
80 // These functions should probably go into bufferview_funcs somehow (Jug)
82 void InsetText::saveLyXTextState() const
84 // check if my paragraphs are still valid
85 ParagraphList::iterator it = const_cast<ParagraphList&>(paragraphs).begin();
86 ParagraphList::iterator end = const_cast<ParagraphList&>(paragraphs).end();
87 for (; it != end; ++it) {
88 if (it == text_.cursor.par())
92 if (it != end && text_.cursor.pos() <= it->size())
93 sstate = text_; // slicing intended
95 sstate.cursor.par(end);
99 void InsetText::restoreLyXTextState() const
101 if (sstate.cursor.par() == const_cast<ParagraphList&>(paragraphs).end())
104 text_.selection.set(true);
105 // at this point just to avoid the DEPM when setting the cursor
106 text_.selection.mark(sstate.selection.mark());
107 if (sstate.selection.set()) {
108 text_.setCursor(sstate.selection.start.par(),
109 sstate.selection.start.pos(),
110 true, sstate.selection.start.boundary());
111 text_.selection.cursor = text_.cursor;
112 text_.setCursor(sstate.selection.end.par(), sstate.selection.end.pos(),
113 true, sstate.selection.end.boundary());
114 text_.setSelection();
115 text_.setCursor(sstate.cursor.par(), sstate.cursor.pos());
117 text_.setCursor(sstate.cursor.par(), sstate.cursor.pos(),
118 true, sstate.cursor.boundary());
119 text_.selection.cursor = text_.cursor;
120 text_.selection.set(false);
125 InsetText::InsetText(BufferParams const & bp)
126 : UpdatableInset(), in_update(false), text_(0, this)
128 paragraphs.push_back(Paragraph());
129 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
130 if (bp.tracking_changes)
131 paragraphs.begin()->trackChanges();
136 InsetText::InsetText(InsetText const & in)
137 : UpdatableInset(in), in_update(false), text_(0, this)
143 InsetText & InsetText::operator=(InsetText const & it)
150 void InsetText::init(InsetText const * ins)
153 text_.bv_owner = ins->text_.bv_owner;
154 setParagraphData(ins->paragraphs);
155 autoBreakRows = ins->autoBreakRows;
156 drawFrame_ = ins->drawFrame_;
157 frame_color = ins->frame_color;
160 frame_color = LColor::insetframe;
161 autoBreakRows = false;
163 the_locking_inset = 0;
164 for_each(paragraphs.begin(), paragraphs.end(),
165 boost::bind(&Paragraph::setInsetOwner, _1, this));
173 old_par = paragraphs.end();
174 last_drawn_width = -1;
175 sstate.cursor.par(paragraphs.end());
176 in_insetAllowed = false;
180 void InsetText::clear(bool just_mark_erased)
182 if (just_mark_erased) {
183 ParagraphList::iterator it = paragraphs.begin();
184 ParagraphList::iterator end = paragraphs.end();
185 for (; it != end; ++it) {
192 // This is a gross hack...
193 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
196 paragraphs.push_back(Paragraph());
197 paragraphs.begin()->setInsetOwner(this);
198 paragraphs.begin()->layout(old_layout);
205 InsetBase * InsetText::clone() const
207 return new InsetText(*this);
211 void InsetText::write(Buffer const * buf, ostream & os) const
214 writeParagraphData(buf, os);
218 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
220 ParagraphList::const_iterator it = paragraphs.begin();
221 ParagraphList::const_iterator end = paragraphs.end();
222 Paragraph::depth_type dth = 0;
223 for (; it != end; ++it) {
224 it->write(buf, os, buf->params, dth);
229 void InsetText::read(Buffer const * buf, LyXLex & lex)
232 Paragraph::depth_type depth = 0;
236 if (buf->params.tracking_changes)
237 paragraphs.begin()->trackChanges();
239 // delete the initial paragraph
241 ParagraphList::iterator pit = paragraphs.begin();
245 token = lex.getString();
248 if (token == "\\end_inset") {
252 if (token == "\\the_end") {
253 lex.printError("\\the_end read in inset! Error in document!");
258 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
261 pit = paragraphs.begin();
262 ParagraphList::iterator const end = paragraphs.end();
263 for (; pit != end; ++pit)
264 pit->setInsetOwner(this);
266 if (token != "\\end_inset") {
267 lex.printError("Missing \\end_inset at this point. "
274 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
276 BufferView * bv = mi.base.bv;
278 dim.asc = text_.rows().begin()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
279 dim.des = text_.height - dim.asc + TEXT_TO_INSET_OFFSET;
280 dim.wid = max(textWidth(bv), int(text_.width)) + 2 * TEXT_TO_INSET_OFFSET;
281 dim.wid = max(dim.wid, 10);
286 int InsetText::textWidth(BufferView * bv, bool fordraw) const
288 int w = autoBreakRows ? getMaxWidth(bv, this) : -1;
291 return max(w - 2 * TEXT_TO_INSET_OFFSET,
292 (int)getLyXText(bv)->width);
297 return w - 2 * TEXT_TO_INSET_OFFSET;
301 void InsetText::draw(PainterInfo & pi, int x, int baseline) const
306 // update our idea of where we are. Clearly, we should
307 // not have to know this information.
311 int const start_x = x;
313 BufferView * bv = pi.base.bv;
314 Painter & pain = pi.pain;
316 // repaint the background if needed
317 if (backgroundColor() != LColor::background)
318 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
320 // no draw is necessary !!!
321 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
322 top_baseline = baseline;
330 top_baseline = baseline;
331 top_y = baseline - dim_.asc;
333 if (last_drawn_width != dim_.wid) {
335 last_drawn_width = dim_.wid;
338 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
339 inset_x = cix(bv) - int(x) + drawTextXOffset;
340 inset_y = ciy() + drawTextYOffset;
343 x += TEXT_TO_INSET_OFFSET;
345 RowList::iterator rowit = text_.rows().begin();
346 RowList::iterator end = text_.rows().end();
348 int y_offset = baseline - rowit->ascent_of_text();
349 int ph = pain.paperHeight();
352 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
353 y += rowit->height();
354 first += rowit->height();
358 text_.top_y(-y_offset);
366 int yf = y_offset + first;
371 while ((rowit != end) && (yf < ph)) {
372 RowPainter rp(*bv, text_, rowit);
373 rp.paint(y + y_offset + first, int(x), y + text_.top_y());
374 y += rowit->height();
375 yf += rowit->height();
381 if ((drawFrame_ == ALWAYS) || (drawFrame_ == LOCKED && locked)) {
382 drawFrame(pain, int(start_x));
385 if (need_update != INIT) {
391 void InsetText::drawFrame(Painter & pain, int x) const
393 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
395 frame_y = top_baseline - dim_.asc + ttoD2;
396 frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
397 frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
398 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
403 void InsetText::update(BufferView * bv, bool reinit)
408 if (reinit && owner()) {
410 owner()->update(bv, true);
416 if (reinit || need_update == INIT) {
418 // we should put this call where we set need_update to INIT!
421 owner()->update(bv, true);
424 int nw = getMaxWidth(bv, this);
425 if (nw > 0 && old_max_width != nw) {
432 if (!autoBreakRows && paragraphs.size() > 1)
433 collapseParagraphs(bv);
435 if (the_locking_inset) {
436 inset_x = cix(bv) - top_x + drawTextXOffset;
437 inset_y = ciy() + drawTextYOffset;
438 the_locking_inset->update(bv, reinit);
441 if ((need_update & CURSOR_PAR) && (text_.refreshStatus() == LyXText::REFRESH_NONE) &&
443 text_.updateInset(the_locking_inset);
446 if (text_.refreshStatus() == LyXText::REFRESH_AREA)
451 int nw = getMaxWidth(bv, this);
452 if (nw > 0 && old_max_width != nw) {
459 void InsetText::setUpdateStatus(int what) const
462 // we have to redraw us full if our LyXText REFRESH_AREA or
463 // if we don't break row so that we only have one row to update!
464 if ((text_.refreshStatus() == LyXText::REFRESH_AREA) ||
466 (text_.refreshStatus() == LyXText::REFRESH_ROW)))
469 } else if (text_.refreshStatus() == LyXText::REFRESH_ROW) {
470 need_update |= CURSOR_PAR;
473 // this to not draw a selection when we redraw all of it!
474 if (need_update & CURSOR && !(need_update & SELECTION)) {
475 if (text_.selection.set())
477 text_.clearSelection();
482 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
484 if (!autoBreakRows && paragraphs.size() > 1)
485 collapseParagraphs(bv);
487 text_.partialRebreak();
488 setUpdateStatus(what);
489 bool flag = mark_dirty ||
490 (((need_update != CURSOR) && (need_update != NONE)) ||
491 (text_.refreshStatus() != LyXText::REFRESH_NONE) || text_.selection.set());
492 if (!text_.selection.set())
493 text_.selection.cursor = text_.cursor;
499 bv->updateInset(const_cast<InsetText *>(this));
502 if (need_update == CURSOR)
504 bv->owner()->view_state_changed();
505 bv->owner()->updateMenubar();
506 bv->owner()->updateToolbar();
507 if (old_par != cpar()) {
508 bv->owner()->setLayout(cpar()->layout()->name());
514 string const InsetText::editMessage() const
516 return _("Opened Text Inset");
520 void InsetText::insetUnlock(BufferView * bv)
522 if (the_locking_inset) {
523 the_locking_inset->insetUnlock(bv);
524 the_locking_inset = 0;
525 updateLocal(bv, CURSOR_PAR, false);
531 if (text_.selection.set()) {
532 text_.clearSelection();
534 } else if (owner()) {
535 bv->owner()->setLayout(owner()->getLyXText(bv)
536 ->cursor.par()->layout()->name());
538 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
539 // hack for deleteEmptyParMech
540 ParagraphList::iterator first_par = paragraphs.begin();
541 if (!first_par->empty()) {
542 text_.setCursor(first_par, 0);
543 } else if (paragraphs.size() > 1) {
544 text_.setCursor(boost::next(first_par), 0);
547 updateLocal(bv, code, false);
550 setUpdateStatus(code);
555 void InsetText::lockInset(BufferView * bv)
558 the_locking_inset = 0;
559 inset_pos = inset_x = inset_y = 0;
560 inset_boundary = false;
561 inset_par = paragraphs.end();
562 old_par = paragraphs.end();
563 text_.setCursor(paragraphs.begin(), 0);
564 text_.clearSelection();
566 // If the inset is empty set the language of the current font to the
567 // language to the surronding text (if different).
568 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
569 bv->getParentLanguage(this) != text_.current_font.language()) {
570 LyXFont font(LyXFont::ALL_IGNORE);
571 font.setLanguage(bv->getParentLanguage(this));
572 setFont(bv, font, false);
575 if (drawFrame_ == LOCKED)
576 code = CURSOR|DRAW_FRAME;
577 setUpdateStatus(code);
581 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
583 the_locking_inset = inset;
584 inset_x = cix(bv) - top_x + drawTextXOffset;
585 inset_y = ciy() + drawTextYOffset;
588 inset_boundary = cboundary();
589 updateLocal(bv, CURSOR, false);
593 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
595 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
599 if (!the_locking_inset) {
600 ParagraphList::iterator pit = paragraphs.begin();
601 ParagraphList::iterator pend = paragraphs.end();
603 int const id = inset->id();
604 for (; pit != pend; ++pit) {
605 InsetList::iterator it =
606 pit->insetlist.begin();
607 InsetList::iterator const end =
608 pit->insetlist.end();
609 for (; it != end; ++it) {
610 if (it->inset == inset) {
611 getLyXText(bv)->setCursorIntern(pit, it->pos);
612 lockInset(bv, inset);
615 if (it->inset->getInsetFromID(id)) {
616 getLyXText(bv)->setCursorIntern(pit, it->pos);
617 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
618 return the_locking_inset->lockInsetInInset(bv, inset);
624 if (inset == cpar()->getInset(cpos())) {
625 lyxerr[Debug::INSETS] << "OK" << endl;
626 lockInset(bv, inset);
628 } else if (the_locking_inset && (the_locking_inset == inset)) {
629 if (cpar() == inset_par && cpos() == inset_pos) {
630 lyxerr[Debug::INSETS] << "OK" << endl;
631 inset_x = cix(bv) - top_x + drawTextXOffset;
632 inset_y = ciy() + drawTextYOffset;
634 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
636 } else if (the_locking_inset) {
637 lyxerr[Debug::INSETS] << "MAYBE" << endl;
638 return the_locking_inset->lockInsetInInset(bv, inset);
640 lyxerr[Debug::INSETS] << "NOT OK" << endl;
645 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
648 if (!the_locking_inset)
650 if (the_locking_inset == inset) {
651 the_locking_inset->insetUnlock(bv);
652 getLyXText(bv)->updateInset(inset);
653 the_locking_inset = 0;
655 moveRightIntern(bv, true, false);
656 old_par = paragraphs.end(); // force layout setting
660 updateLocal(bv, CURSOR, false);
663 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
667 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
669 if (!autoBreakRows && paragraphs.size() > 1)
670 collapseParagraphs(bv);
675 if (inset->owner() != this) {
676 int ustat = CURSOR_PAR;
678 UpdatableInset * tl_inset = the_locking_inset;
680 found = tl_inset->updateInsetInInset(bv, inset);
682 tl_inset = static_cast<UpdatableInset *>(inset);
683 while(tl_inset->owner() && tl_inset->owner() != this)
684 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
685 if (!tl_inset->owner())
687 found = tl_inset->updateInsetInInset(bv, inset);
691 text_.updateInset(tl_inset);
693 setUpdateStatus(ustat);
696 bool found = text_.updateInset(inset);
698 setUpdateStatus(CURSOR_PAR);
699 if (the_locking_inset &&
700 cpar() == inset_par && cpos() == inset_pos)
702 inset_x = cix(bv) - top_x + drawTextXOffset;
703 inset_y = ciy() + drawTextYOffset;
710 void InsetText::lfunMousePress(FuncRequest const & cmd)
714 // use this to check mouse motion for selection!
718 BufferView * bv = cmd.view();
719 FuncRequest cmd1 = cmd;
725 int tmp_x = cmd.x - drawTextXOffset;
726 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
727 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
729 if (the_locking_inset) {
730 if (the_locking_inset == inset) {
731 the_locking_inset->localDispatch(cmd1);
734 // otherwise only unlock the_locking_inset
735 the_locking_inset->insetUnlock(bv);
736 the_locking_inset = 0;
739 no_selection = false;
741 if (bv->theLockingInset()) {
742 if (isHighlyEditableInset(inset)) {
743 // We just have to lock the inset before calling a
745 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
746 if (!bv->lockInset(uinset)) {
747 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
749 inset->localDispatch(cmd1);
750 if (the_locking_inset)
751 updateLocal(bv, CURSOR, false);
756 bool paste_internally = false;
757 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
758 localDispatch(FuncRequest(bv, LFUN_COPY));
759 paste_internally = true;
761 int old_top_y = text_.top_y();
763 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
765 // set the selection cursor!
766 text_.selection.cursor = text_.cursor;
767 text_.cursor.x_fix(text_.cursor.x());
769 if (text_.selection.set()) {
770 text_.clearSelection();
771 updateLocal(bv, FULL, false);
773 text_.clearSelection();
774 updateLocal(bv, CURSOR, false);
777 bv->owner()->setLayout(cpar()->layout()->name());
779 // we moved the view we cannot do mouse selection in this case!
780 if (getLyXText(bv)->top_y() != old_top_y)
783 // Insert primary selection with middle mouse
784 // if there is a local selection in the current buffer,
786 if (cmd.button() == mouse_button::button2) {
787 if (paste_internally)
788 localDispatch(FuncRequest(bv, LFUN_PASTE));
790 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
793 getLyXText(bv)->clearSelection();
798 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
800 BufferView * bv = cmd.view();
801 FuncRequest cmd1 = cmd;
806 if (the_locking_inset)
807 return the_locking_inset->localDispatch(cmd1);
809 int tmp_x = cmd.x - drawTextXOffset;
810 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
811 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
814 // This code should probably be removed now. Simple insets
815 // (!highlyEditable) can actually take the localDispatch,
816 // and turn it into edit() if necessary. But we still
817 // need to deal properly with the whole relative vs.
818 // absolute mouse co-ords thing in a realiable, sensible way
820 if (isHighlyEditableInset(inset))
821 ret = inset->localDispatch(cmd1);
823 inset_x = cix(bv) - top_x + drawTextXOffset;
824 inset_y = ciy() + drawTextYOffset;
825 cmd1.x = cmd.x - inset_x;
826 cmd1.y = cmd.x - inset_y;
827 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
831 ret = inset->localDispatch(cmd1);
832 updateLocal(bv, CURSOR_PAR, false);
839 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
841 FuncRequest cmd1 = cmd;
845 if (the_locking_inset) {
846 the_locking_inset->localDispatch(cmd1);
850 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
853 BufferView * bv = cmd.view();
854 LyXCursor cur = text_.cursor;
855 text_.setCursorFromCoordinates
856 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
857 text_.cursor.x_fix(text_.cursor.x());
858 if (cur == text_.cursor)
860 text_.setSelection();
861 bool flag = (text_.toggle_cursor.par() != text_.toggle_end_cursor.par() ||
862 text_.toggle_cursor.pos() != text_.toggle_end_cursor.pos());
864 updateLocal(bv, SELECTION, false);
869 Inset::RESULT InsetText::localDispatch(FuncRequest const & cmd)
871 BufferView * bv = cmd.view();
874 if (cmd.action == LFUN_INSET_EDIT) {
875 UpdatableInset::localDispatch(cmd);
877 if (!bv->lockInset(this)) {
878 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
883 the_locking_inset = 0;
884 inset_pos = inset_x = inset_y = 0;
885 inset_boundary = false;
886 inset_par = paragraphs.end();
887 old_par = paragraphs.end();
890 if (cmd.argument.size()) {
891 if (cmd.argument == "left")
892 text_.setCursor(paragraphs.begin(), 0);
894 ParagraphList::iterator it = paragraphs.begin();
895 ParagraphList::iterator end = paragraphs.end();
896 while (boost::next(it) != end)
898 // int const pos = (p->size() ? p->size()-1 : p->size());
899 text_.setCursor(it, it->size());
902 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
903 // we put here -1 and not button as now the button in the
904 // edit call should not be needed we will fix this in 1.3.x
905 // cycle hopefully (Jug 20020509)
906 // FIXME: GUII I've changed this to none: probably WRONG
907 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
908 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
910 text_.cursor.x_fix(text_.cursor.x());
914 text_.clearSelection();
917 // If the inset is empty set the language of the current font to the
918 // language to the surronding text (if different).
919 if (paragraphs.begin()->empty() &&
920 paragraphs.size() == 1 &&
921 bv->getParentLanguage(this) != text_.current_font.language())
923 LyXFont font(LyXFont::ALL_IGNORE);
924 font.setLanguage(bv->getParentLanguage(this));
925 setFont(bv, font, false);
929 if (drawFrame_ == LOCKED)
930 code = CURSOR | DRAW_FRAME;
932 updateLocal(bv, code, false);
933 // Tell the paragraph dialog that we've entered an insettext.
934 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
939 switch (cmd.action) {
940 case LFUN_MOUSE_PRESS:
943 case LFUN_MOUSE_MOTION:
944 lfunMouseMotion(cmd);
946 case LFUN_MOUSE_RELEASE:
947 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
952 bool was_empty = (paragraphs.begin()->empty() &&
953 paragraphs.size() == 1);
955 no_selection = false;
956 RESULT result = UpdatableInset::localDispatch(cmd);
957 if (result != UNDISPATCHED)
961 if (cmd.action < 0 && cmd.argument.empty())
964 if (the_locking_inset) {
965 result = the_locking_inset->localDispatch(cmd);
966 if (result == DISPATCHED_NOUPDATE)
968 else if (result == DISPATCHED) {
969 updateLocal(bv, CURSOR_PAR, false);
971 } else if (result >= FINISHED) {
974 moveRightIntern(bv, false, false);
978 if ((result = moveUp(bv)) >= FINISHED) {
979 updateLocal(bv, CURSOR, false);
980 bv->unlockInset(this);
984 if ((result = moveDown(bv)) >= FINISHED) {
985 updateLocal(bv, CURSOR, false);
986 bv->unlockInset(this);
993 the_locking_inset = 0;
994 updateLocal(bv, CURSOR, false);
995 // make sure status gets reset immediately
996 bv->owner()->clearMessage();
1001 int updflag = false;
1003 // what type of update to do on a cursor movement
1004 int cursor_update = CURSOR;
1006 if (text_.selection.set())
1007 cursor_update = SELECTION;
1009 switch (cmd.action) {
1012 case LFUN_SELFINSERT:
1013 if (bv->buffer()->isReadonly()) {
1014 // setErrorMessage(N_("Document is read only"));
1017 if (!cmd.argument.empty()) {
1018 /* Automatically delete the currently selected
1019 * text and replace it with what is being
1020 * typed in now. Depends on lyxrc settings
1021 * "auto_region_delete", which defaults to
1024 // This should not be needed here and is also WRONG!
1025 setUndo(bv, Undo::INSERT, text_.cursor.par());
1028 if (lyxrc.auto_region_delete) {
1029 if (text_.selection.set()) {
1030 text_.cutSelection(false, false);
1033 text_.clearSelection();
1034 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
1035 bv->owner()->getIntl().getTransManager().
1036 TranslateAndInsert(cmd.argument[i], &text_);
1039 text_.selection.cursor = text_.cursor;
1040 updwhat = CURSOR | CURSOR_PAR;
1042 result = DISPATCHED_NOUPDATE;
1045 // cursor movements that need special handling
1048 result = moveRight(bv);
1050 updwhat = cursor_update;
1054 result = moveLeft(bv);
1055 updwhat = cursor_update;
1059 result = moveDown(bv);
1060 updwhat = cursor_update;
1064 result = moveUp(bv);
1065 updwhat = cursor_update;
1069 if (crow() == text_.rows().begin())
1070 result = FINISHED_UP;
1072 text_.cursorPrevious();
1073 text_.clearSelection();
1074 result = DISPATCHED_NOUPDATE;
1076 updwhat = cursor_update;
1080 if (boost::next(crow()) == text_.rows().end())
1081 result = FINISHED_DOWN;
1084 text_.clearSelection();
1085 result = DISPATCHED_NOUPDATE;
1087 updwhat = cursor_update;
1090 case LFUN_BACKSPACE: {
1091 if (text_.selection.set())
1092 text_.cutSelection(true, false);
1095 updwhat = CURSOR_PAR;
1101 if (text_.selection.set()) {
1102 text_.cutSelection(true, false);
1106 updwhat = CURSOR_PAR;
1112 text_.cutSelection(true, true);
1113 updwhat = CURSOR_PAR;
1120 text_.copySelection();
1121 updwhat = CURSOR_PAR;
1124 case LFUN_PASTESELECTION:
1126 string const clip(bv->getClipboard());
1130 if (cmd.argument == "paragraph") {
1131 text_.insertStringAsParagraphs(clip);
1133 text_.insertStringAsLines(clip);
1136 text_.clearSelection();
1138 updwhat = CURSOR_PAR;
1144 if (!autoBreakRows) {
1145 if (CutAndPaste::nrOfParagraphs() > 1) {
1146 #ifdef WITH_WARNINGS
1147 #warning FIXME horrendously bad UI
1149 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1154 size_t sel_index = 0;
1155 string const & arg = cmd.argument;
1156 if (isStrUnsignedInt(arg)) {
1157 size_t const paste_arg = strToUnsignedInt(arg);
1158 #warning FIXME Check if the arg is in the domain of available selections.
1159 sel_index = paste_arg;
1161 text_.pasteSelection(sel_index);
1163 text_.clearSelection();
1164 updwhat = CURSOR_PAR;
1169 case LFUN_BREAKPARAGRAPH:
1170 if (!autoBreakRows) {
1171 result = DISPATCHED;
1174 text_.breakParagraph(paragraphs, 0);
1175 updwhat = CURSOR | FULL;
1179 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1180 if (!autoBreakRows) {
1181 result = DISPATCHED;
1184 text_.breakParagraph(paragraphs, 1);
1185 updwhat = CURSOR | FULL;
1189 case LFUN_BREAKLINE: {
1190 if (!autoBreakRows) {
1191 result = DISPATCHED;
1195 text_.insertInset(new InsetNewline);
1196 updwhat = CURSOR | CURSOR_PAR;
1202 // do not set layouts on non breakable textinsets
1203 if (autoBreakRows) {
1204 string cur_layout = cpar()->layout()->name();
1206 // Derive layout number from given argument (string)
1207 // and current buffer's textclass (number). */
1208 LyXTextClass const & tclass =
1209 bv->buffer()->params.getLyXTextClass();
1210 string layout = cmd.argument;
1211 bool hasLayout = tclass.hasLayout(layout);
1213 // If the entry is obsolete, use the new one instead.
1215 string const & obs =
1216 tclass[layout]->obsoleted_by();
1221 // see if we found the layout number:
1223 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1224 bv->owner()->dispatch(lf);
1228 if (cur_layout != layout) {
1229 cur_layout = layout;
1230 text_.setLayout(layout);
1231 bv->owner()->setLayout(cpar()->layout()->name());
1232 updwhat = CURSOR_PAR;
1236 // reset the layout box
1237 bv->owner()->setLayout(cpar()->layout()->name());
1240 case LFUN_PARAGRAPH_SPACING:
1241 // This one is absolutely not working. When fiddling with this
1242 // it also seems to me that the paragraphs inside the insettext
1243 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1244 // FIXME: how old is this comment ? ...
1246 ParagraphList::iterator pit = text_.cursor.par();
1247 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1248 float cur_value = 1.0;
1249 if (cur_spacing == Spacing::Other) {
1250 cur_value = pit->params().spacing().getValue();
1253 istringstream istr(STRCONV(cmd.argument));
1256 Spacing::Space new_spacing = cur_spacing;
1257 float new_value = cur_value;
1259 lyxerr << "Missing argument to `paragraph-spacing'"
1261 } else if (tmp == "single") {
1262 new_spacing = Spacing::Single;
1263 } else if (tmp == "onehalf") {
1264 new_spacing = Spacing::Onehalf;
1265 } else if (tmp == "double") {
1266 new_spacing = Spacing::Double;
1267 } else if (tmp == "other") {
1268 new_spacing = Spacing::Other;
1271 lyxerr << "new_value = " << tmpval << endl;
1274 } else if (tmp == "default") {
1275 new_spacing = Spacing::Default;
1277 lyxerr << _("Unknown spacing argument: ")
1278 << cmd.argument << endl;
1280 if (cur_spacing != new_spacing || cur_value != new_value) {
1281 pit->params().spacing(Spacing(new_spacing, new_value));
1282 updwhat = CURSOR_PAR;
1288 // These need to do repaints but don't require
1289 // special handling otherwise. A *lot* of the
1290 // above could probably be done similarly ...
1295 case LFUN_WORDRIGHT:
1296 // these two are really unhandled ...
1298 case LFUN_BEGINNINGBUF:
1299 updwhat = cursor_update;
1300 if (!bv->dispatch(cmd))
1301 result = UNDISPATCHED;
1310 case LFUN_WORDLEFTSEL:
1311 case LFUN_WORDRIGHTSEL:
1312 updwhat = SELECTION;
1317 if (!bv->dispatch(cmd))
1318 result = UNDISPATCHED;
1323 updateLocal(bv, updwhat, updflag);
1324 /// If the action has deleted all text in the inset, we need to change the
1325 // language to the language of the surronding text.
1326 if (!was_empty && paragraphs.begin()->empty() &&
1327 paragraphs.size() == 1) {
1328 LyXFont font(LyXFont::ALL_IGNORE);
1329 font.setLanguage(bv->getParentLanguage(this));
1330 setFont(bv, font, false);
1333 if (result >= FINISHED)
1334 bv->unlockInset(this);
1336 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1337 result = DISPATCHED;
1342 int InsetText::latex(Buffer const * buf, ostream & os,
1343 LatexRunParams const & runparams) const
1346 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1347 return texrow.rows();
1351 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1353 unsigned int lines = 0;
1355 ParagraphList::const_iterator beg = paragraphs.begin();
1356 ParagraphList::const_iterator end = paragraphs.end();
1357 ParagraphList::const_iterator it = beg;
1358 for (; it != end; ++it) {
1359 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1360 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1367 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1369 unsigned int lines = 0;
1371 vector<string> environment_stack(10);
1372 vector<string> environment_inner(10);
1374 int const command_depth = 0;
1377 Paragraph::depth_type depth = 0; // paragraph depth
1379 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1380 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1382 for (; pit != pend; ++pit) {
1384 int desc_on = 0; // description mode
1386 LyXLayout_ptr const & style = pit->layout();
1388 // environment tag closing
1389 for (; depth > pit->params().depth(); --depth) {
1390 if (environment_inner[depth] != "!-- --") {
1391 item_name = "listitem";
1392 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1393 if (environment_inner[depth] == "varlistentry")
1394 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1396 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1397 environment_stack[depth].erase();
1398 environment_inner[depth].erase();
1401 if (depth == pit->params().depth()
1402 && environment_stack[depth] != style->latexname()
1403 && !environment_stack[depth].empty()) {
1404 if (environment_inner[depth] != "!-- --") {
1405 item_name= "listitem";
1406 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1407 if (environment_inner[depth] == "varlistentry")
1408 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1411 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1413 environment_stack[depth].erase();
1414 environment_inner[depth].erase();
1417 // Write opening SGML tags.
1418 switch (style->latextype) {
1419 case LATEX_PARAGRAPH:
1420 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1424 buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1428 case LATEX_ENVIRONMENT:
1429 case LATEX_ITEM_ENVIRONMENT:
1430 if (depth < pit->params().depth()) {
1431 depth = pit->params().depth();
1432 environment_stack[depth].erase();
1435 if (environment_stack[depth] != style->latexname()) {
1436 if (environment_stack.size() == depth + 1) {
1437 environment_stack.push_back("!-- --");
1438 environment_inner.push_back("!-- --");
1440 environment_stack[depth] = style->latexname();
1441 environment_inner[depth] = "!-- --";
1442 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1444 if (environment_inner[depth] != "!-- --") {
1445 item_name= "listitem";
1446 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1447 if (environment_inner[depth] == "varlistentry")
1448 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1452 if (style->latextype == LATEX_ENVIRONMENT) {
1453 if (!style->latexparam().empty()) {
1454 if (style->latexparam() == "CDATA")
1457 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1462 desc_on = (style->labeltype == LABEL_MANUAL);
1464 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1465 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1467 item_name = desc_on ? "term" : "para";
1468 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1472 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1476 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1479 // write closing SGML tags
1480 switch (style->latextype) {
1481 case LATEX_ENVIRONMENT:
1482 if (!style->latexparam().empty()) {
1483 if (style->latexparam() == "CDATA")
1486 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1489 case LATEX_ITEM_ENVIRONMENT:
1490 if (desc_on == 1) break;
1492 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1494 case LATEX_PARAGRAPH:
1495 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1498 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1504 for (int d = depth; d >= 0; --d) {
1505 if (!environment_stack[depth].empty()) {
1506 if (environment_inner[depth] != "!-- --") {
1507 item_name = "listitem";
1508 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1509 if (environment_inner[depth] == "varlistentry")
1510 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1513 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1521 void InsetText::validate(LaTeXFeatures & features) const
1523 for_each(paragraphs.begin(), paragraphs.end(),
1524 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1528 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1530 if (the_locking_inset) {
1531 the_locking_inset->getCursor(bv, x, y);
1535 y = cy() + InsetText::y();
1539 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1541 if (the_locking_inset) {
1542 the_locking_inset->getCursorPos(bv, x, y);
1545 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1546 y = cy() - TEXT_TO_INSET_OFFSET;
1550 int InsetText::insetInInsetY() const
1552 if (!the_locking_inset)
1555 return (inset_y + the_locking_inset->insetInInsetY());
1559 void InsetText::fitInsetCursor(BufferView * bv) const
1561 if (the_locking_inset) {
1562 the_locking_inset->fitInsetCursor(bv);
1565 LyXFont const font =
1566 getLyXText(bv)->getFont(bv->buffer(), cpar(), cpos());
1568 int const asc = font_metrics::maxAscent(font);
1569 int const desc = font_metrics::maxDescent(font);
1571 if (bv->fitLockedInsetCursor(cx(bv), cy(), asc, desc))
1572 need_update |= FULL;
1577 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1579 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1580 return moveLeftIntern(bv, false, activate_inset, selecting);
1582 return moveRightIntern(bv, true, activate_inset, selecting);
1587 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1589 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1590 return moveRightIntern(bv, true, activate_inset, selecting);
1592 return moveLeftIntern(bv, false, activate_inset, selecting);
1597 InsetText::moveRightIntern(BufferView * bv, bool front,
1598 bool activate_inset, bool selecting)
1600 ParagraphList::iterator c_par = cpar();
1602 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1603 return FINISHED_RIGHT;
1604 if (activate_inset && checkAndActivateInset(bv, front))
1606 text_.cursorRight(bv);
1608 text_.clearSelection();
1609 return DISPATCHED_NOUPDATE;
1614 InsetText::moveLeftIntern(BufferView * bv, bool front,
1615 bool activate_inset, bool selecting)
1617 if (cpar() == paragraphs.begin() && cpos() <= 0)
1619 text_.cursorLeft(bv);
1621 text_.clearSelection();
1622 if (activate_inset && checkAndActivateInset(bv, front))
1624 return DISPATCHED_NOUPDATE;
1628 Inset::RESULT InsetText::moveUp(BufferView * bv)
1630 if (crow() == text_.rows().begin())
1633 text_.clearSelection();
1634 return DISPATCHED_NOUPDATE;
1638 Inset::RESULT InsetText::moveDown(BufferView * bv)
1640 if (boost::next(crow()) == text_.rows().end())
1641 return FINISHED_DOWN;
1642 text_.cursorDown(bv);
1643 text_.clearSelection();
1644 return DISPATCHED_NOUPDATE;
1648 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1650 if (the_locking_inset) {
1651 if (the_locking_inset->insetAllowed(inset))
1652 return the_locking_inset->insertInset(bv, inset);
1655 inset->setOwner(this);
1656 getLyXText(bv)->insertInset(inset);
1658 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1663 bool InsetText::insetAllowed(Inset::Code code) const
1665 // in_insetAllowed is a really gross hack,
1666 // to allow us to call the owner's insetAllowed
1667 // without stack overflow, which can happen
1668 // when the owner uses InsetCollapsable::insetAllowed()
1670 if (in_insetAllowed)
1672 in_insetAllowed = true;
1673 if (the_locking_inset)
1674 ret = the_locking_inset->insetAllowed(code);
1676 ret = owner()->insetAllowed(code);
1677 in_insetAllowed = false;
1682 UpdatableInset * InsetText::getLockingInset() const
1684 return the_locking_inset ? the_locking_inset->getLockingInset() :
1685 const_cast<InsetText *>(this);
1689 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1693 if (the_locking_inset)
1694 return the_locking_inset->getFirstLockingInsetOfType(c);
1699 bool InsetText::showInsetDialog(BufferView * bv) const
1701 if (the_locking_inset)
1702 return the_locking_inset->showInsetDialog(bv);
1707 void InsetText::getLabelList(std::vector<string> & list) const
1709 ParagraphList::const_iterator pit = paragraphs.begin();
1710 ParagraphList::const_iterator pend = paragraphs.end();
1711 for (; pit != pend; ++pit) {
1712 InsetList::const_iterator beg = pit->insetlist.begin();
1713 InsetList::const_iterator end = pit->insetlist.end();
1714 for (; beg != end; ++beg)
1715 beg->inset->getLabelList(list);
1720 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1723 if (the_locking_inset) {
1724 the_locking_inset->setFont(bv, font, toggleall, selectall);
1728 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1729 || cpar()->empty()) {
1730 getLyXText(bv)->setFont(font, toggleall);
1735 if (text_.selection.set()) {
1736 setUndo(bv, Undo::EDIT, text_.cursor.par());
1742 text_.toggleFree(font, toggleall);
1745 text_.clearSelection();
1749 bool flag = (selectall || text_.selection.set());
1752 updateLocal(bv, FULL, true);
1754 updateLocal(bv, CURSOR_PAR, true);
1758 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1760 if (cpar()->isInset(cpos())) {
1762 static_cast<UpdatableInset*>(cpar()->getInset(cpos()));
1763 if (!isHighlyEditableInset(inset))
1765 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1766 inset->localDispatch(cmd);
1767 if (!the_locking_inset)
1769 updateLocal(bv, CURSOR, false);
1776 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1777 mouse_button::state button)
1779 x -= drawTextXOffset;
1781 int dummyy = y + dim_.asc;
1782 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1783 // we only do the edit() call if the inset was hit by the mouse
1784 // or if it is a highly editable inset. So we should call this
1785 // function from our own edit with button < 0.
1786 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1788 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1796 inset_x = cix(bv) - top_x + drawTextXOffset;
1797 inset_y = ciy() + drawTextYOffset;
1798 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1799 inset->localDispatch(cmd);
1800 if (!the_locking_inset)
1802 updateLocal(bv, CURSOR, false);
1809 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1812 int w = UpdatableInset::getMaxWidth(bv, inset);
1817 w = w - top_x + owner()->x();
1820 w -= (2 * TEXT_TO_INSET_OFFSET);
1823 return UpdatableInset::getMaxWidth(bv, inset);
1828 void InsetText::setParagraphData(ParagraphList const & plist)
1830 // we have to unlock any locked inset otherwise we're in troubles
1831 the_locking_inset = 0;
1833 // But it it makes no difference that is a lot better.
1835 // See if this can be simplified when std::list is in effect.
1838 ParagraphList::const_iterator it = plist.begin();
1839 ParagraphList::const_iterator end = plist.end();
1840 for (; it != end; ++it) {
1841 paragraphs.push_back(*it);
1842 Paragraph & tmp = paragraphs.back();
1843 tmp.setInsetOwner(this);
1851 void InsetText::markNew(bool track_changes)
1853 ParagraphList::iterator pit = paragraphs.begin();
1854 ParagraphList::iterator pend = paragraphs.end();
1855 for (; pit != pend; ++pit) {
1856 if (track_changes) {
1857 pit->trackChanges();
1859 // no-op when not tracking
1860 pit->cleanChanges();
1866 void InsetText::setText(string const & data, LyXFont const & font)
1869 for (unsigned int i = 0; i < data.length(); ++i)
1870 paragraphs.begin()->insertChar(i, data[i], font);
1875 void InsetText::setAutoBreakRows(bool flag)
1877 if (flag != autoBreakRows) {
1878 autoBreakRows = flag;
1886 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1888 if (how != drawFrame_) {
1891 updateLocal(bv, DRAW_FRAME, false);
1896 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1898 if (frame_color != col) {
1901 updateLocal(bv, DRAW_FRAME, false);
1906 int InsetText::cx(BufferView * bv) const
1908 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1909 if (the_locking_inset) {
1910 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1911 text_.cursor.pos());
1912 if (font.isVisibleRightToLeft())
1913 x -= the_locking_inset->width(bv, font);
1919 int InsetText::cix(BufferView * bv) const
1921 int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1922 if (the_locking_inset) {
1923 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1924 text_.cursor.pos());
1925 if (font.isVisibleRightToLeft())
1926 x -= the_locking_inset->width(bv, font);
1932 int InsetText::cy() const
1934 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1938 int InsetText::ciy() const
1940 return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1944 pos_type InsetText::cpos() const
1946 return text_.cursor.pos();
1950 ParagraphList::iterator InsetText::cpar() const
1952 return text_.cursor.par();
1956 bool InsetText::cboundary() const
1958 return text_.cursor.boundary();
1962 RowList::iterator InsetText::crow() const
1964 return text_.cursorRow();
1968 LyXText * InsetText::getLyXText(BufferView const * bv,
1969 bool const recursive) const
1972 if (recursive && the_locking_inset)
1973 return the_locking_inset->getLyXText(bv, true);
1978 void InsetText::setViewCache(BufferView const * bv) const
1981 text_.bv_owner = const_cast<BufferView *>(bv);
1985 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1988 /// then remove all LyXText in text-insets
1989 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1990 const_cast<ParagraphList&>(paragraphs).end(),
1991 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1996 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1998 if (paragraphs.size() == 1 && paragraphs.begin()->empty()) {
1999 // no data, resize not neccessary!
2000 // we have to do this as a fixed width may have changed!
2002 text_.init(bv, true);
2003 restoreLyXTextState();
2013 // one endless line, resize normally not necessary
2014 if (!force && getMaxWidth(bv, this) < 0)
2019 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2020 const_cast<ParagraphList&>(paragraphs).end(),
2021 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2023 text_.init(bv, true);
2024 restoreLyXTextState();
2026 if (the_locking_inset) {
2027 inset_x = cix(bv) - top_x + drawTextXOffset;
2028 inset_y = ciy() + drawTextYOffset;
2031 text_.top_y(bv->screen().topCursorVisible(&text_));
2033 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2034 // this will scroll the screen such that the cursor becomes visible
2035 bv->updateScrollbar();
2037 need_update |= FULL;
2042 void InsetText::reinitLyXText() const
2044 BufferView * bv = text_.bv_owner;
2051 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2052 const_cast<ParagraphList&>(paragraphs).end(),
2053 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2055 text_.init(bv, true);
2056 restoreLyXTextState();
2057 if (the_locking_inset) {
2058 inset_x = cix(bv) - top_x + drawTextXOffset;
2059 inset_y = ciy() + drawTextYOffset;
2061 text_.top_y(bv->screen().topCursorVisible(&text_));
2063 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2064 // this will scroll the screen such that the cursor becomes visible
2065 bv->updateScrollbar();
2072 void InsetText::removeNewlines()
2074 bool changed = false;
2076 ParagraphList::iterator it = paragraphs.begin();
2077 ParagraphList::iterator end = paragraphs.end();
2078 for (; it != end; ++it) {
2079 for (int i = 0; i < it->size(); ++i) {
2080 if (it->isNewline(i)) {
2091 bool InsetText::nodraw() const
2093 if (the_locking_inset)
2094 return the_locking_inset->nodraw();
2095 return UpdatableInset::nodraw();
2099 int InsetText::scroll(bool recursive) const
2101 int sx = UpdatableInset::scroll(false);
2103 if (recursive && the_locking_inset)
2104 sx += the_locking_inset->scroll(recursive);
2110 void InsetText::selectAll(BufferView * bv)
2112 getLyXText(bv)->cursorTop();
2113 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2114 getLyXText(bv)->cursorBottom();
2115 getLyXText(bv)->setSelection();
2119 void InsetText::clearSelection(BufferView * bv)
2121 getLyXText(bv)->clearSelection();
2125 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2127 Painter & pain = bv->painter();
2129 int h = dim_.asc + dim_.des;
2130 int ty = baseline - dim_.asc;
2136 if ((ty + h) > pain.paperHeight())
2137 h = pain.paperHeight();
2138 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2139 w = pain.paperWidth();
2140 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2145 ParagraphList * InsetText::getParagraphs(int i) const
2147 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2151 LyXCursor const & InsetText::cursor(BufferView * bv) const
2153 if (the_locking_inset)
2154 return the_locking_inset->cursor(bv);
2155 return getLyXText(bv)->cursor;
2159 Inset * InsetText::getInsetFromID(int id_arg) const
2162 return const_cast<InsetText *>(this);
2164 ParagraphList::const_iterator pit = paragraphs.begin();
2165 ParagraphList::const_iterator pend = paragraphs.end();
2166 for (; pit != pend; ++pit) {
2167 InsetList::const_iterator it = pit->insetlist.begin();
2168 InsetList::const_iterator end = pit->insetlist.end();
2169 for (; it != end; ++it) {
2170 if (it->inset->id() == id_arg)
2172 Inset * in = it->inset->getInsetFromID(id_arg);
2182 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2185 if (the_locking_inset) {
2186 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2187 if (!word.word().empty()) {
2191 // we have to go on checking so move cursor to the next char
2192 text_.cursor.pos(text_.cursor.pos() + 1);
2194 word = text_.selectNextWordToSpellcheck(value);
2195 if (word.word().empty())
2196 bv->unlockInset(const_cast<InsetText *>(this));
2203 void InsetText::selectSelectedWord(BufferView * bv)
2205 if (the_locking_inset) {
2206 the_locking_inset->selectSelectedWord(bv);
2209 getLyXText(bv)->selectSelectedWord();
2210 updateLocal(bv, SELECTION, false);
2214 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2216 if (the_locking_inset) {
2217 the_locking_inset->toggleSelection(bv, kill_selection);
2220 int x = top_x + TEXT_TO_INSET_OFFSET;
2222 RowList::iterator rowit = text_.rows().begin();
2223 RowList::iterator end = text_.rows().end();
2224 int y_offset = top_baseline - rowit->ascent_of_text();
2226 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2227 y += rowit->height();
2233 if (need_update & SELECTION)
2235 bv->screen().toggleSelection(&text_, bv, kill_selection, y_offset, x);
2239 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2241 if (the_locking_inset) {
2242 if (the_locking_inset->nextChange(bv, length))
2244 text_.cursorRight(true);
2246 lyxfind::SearchResult result =
2247 lyxfind::findNextChange(bv, &text_, length);
2249 if (result == lyxfind::SR_FOUND) {
2250 LyXCursor cur = text_.cursor;
2251 bv->unlockInset(bv->theLockingInset());
2252 if (bv->lockInset(this))
2255 text_.setSelectionRange(length);
2256 updateLocal(bv, SELECTION, false);
2258 return result != lyxfind::SR_NOT_FOUND;
2262 bool InsetText::searchForward(BufferView * bv, string const & str,
2265 if (the_locking_inset) {
2266 if (the_locking_inset->searchForward(bv, str, cs, mw))
2268 text_.cursorRight(true);
2270 lyxfind::SearchResult result =
2271 lyxfind::LyXFind(bv, &text_, str, true, cs, mw);
2273 if (result == lyxfind::SR_FOUND) {
2274 LyXCursor cur = text_.cursor;
2275 bv->unlockInset(bv->theLockingInset());
2276 if (bv->lockInset(this))
2279 text_.setSelectionRange(str.length());
2280 updateLocal(bv, SELECTION, false);
2282 return (result != lyxfind::SR_NOT_FOUND);
2285 bool InsetText::searchBackward(BufferView * bv, string const & str,
2288 if (the_locking_inset) {
2289 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2293 ParagraphList::iterator pit = paragraphs.begin();
2294 ParagraphList::iterator pend = paragraphs.end();
2296 while (boost::next(pit) != pend)
2299 text_.setCursor(pit, pit->size());
2301 lyxfind::SearchResult result =
2302 lyxfind::LyXFind(bv, &text_, str, false, cs, mw);
2304 if (result == lyxfind::SR_FOUND) {
2305 LyXCursor cur = text_.cursor;
2306 bv->unlockInset(bv->theLockingInset());
2307 if (bv->lockInset(this))
2310 text_.setSelectionRange(str.length());
2311 updateLocal(bv, SELECTION, false);
2313 return (result != lyxfind::SR_NOT_FOUND);
2317 bool InsetText::checkInsertChar(LyXFont & font)
2320 return owner()->checkInsertChar(font);
2325 void InsetText::collapseParagraphs(BufferView * bv)
2327 while (paragraphs.size() > 1) {
2328 ParagraphList::iterator first_par = paragraphs.begin();
2329 ParagraphList::iterator next_par = boost::next(first_par);
2330 size_t const first_par_size = first_par->size();
2332 if (!first_par->empty() &&
2333 !next_par->empty() &&
2334 !first_par->isSeparator(first_par_size - 1)) {
2335 first_par->insertChar(first_par_size, ' ');
2338 if (text_.selection.set()) {
2339 if (text_.selection.start.par() == next_par) {
2340 text_.selection.start.par(first_par);
2341 text_.selection.start.pos(
2342 text_.selection.start.pos() + first_par_size);
2344 if (text_.selection.end.par() == next_par) {
2345 text_.selection.end.par(first_par);
2346 text_.selection.end.pos(
2347 text_.selection.end.pos() + first_par_size);
2351 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2357 void InsetText::getDrawFont(LyXFont & font) const
2361 owner()->getDrawFont(font);
2365 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2367 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2368 // And it probably does. You have to take a look at this John. (Lgb)
2369 #warning John, have a look here. (Lgb)
2370 ParagraphList::iterator pit = plist.begin();
2371 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2373 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2375 ParagraphList::iterator pend = plist.end();
2376 for (; pit != pend; ++pit) {
2377 paragraphs.push_back(*pit);
2384 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2386 ParagraphList::const_iterator pit = paragraphs.begin();
2387 ParagraphList::const_iterator pend = paragraphs.end();
2389 for (; pit != pend; ++pit) {
2390 InsetList::const_iterator it = pit->insetlist.begin();
2391 InsetList::const_iterator end = pit->insetlist.end();
2392 for (; it != end; ++it) {
2393 it->inset->addPreview(loader);