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 "bufferview_funcs.h"
18 #include "CutAndPaste.h"
20 #include "dimension.h"
21 #include "funcrequest.h"
23 #include "errorlist.h"
25 #include "LaTeXFeatures.h"
29 #include "lyxcursor.h"
35 #include "paragraph.h"
36 #include "ParagraphParameters.h"
37 #include "trans_mgr.h"
38 #include "undo_funcs.h"
39 #include "WordLangTuple.h"
40 #include "paragraph_funcs.h"
42 #include "rowpainter.h"
43 #include "insetnewline.h"
44 #include "metricsinfo.h"
46 #include "frontends/Alert.h"
47 #include "frontends/Dialogs.h"
48 #include "frontends/font_metrics.h"
49 #include "frontends/LyXView.h"
50 #include "frontends/Painter.h"
51 #include "frontends/screen.h"
53 #include "support/textutils.h"
54 #include "support/LAssert.h"
55 #include "support/lstrings.h"
56 #include "support/lyxalgo.h" // lyx::count
58 #include <boost/bind.hpp>
74 using namespace lyx::support;
75 using namespace bv_funcs;
78 using lyx::textclass_type;
80 namespace grfx = lyx::graphics;
82 // These functions should probably go into bufferview_funcs somehow (Jug)
84 void InsetText::saveLyXTextState() const
86 // check if my paragraphs are still valid
87 ParagraphList::iterator it = const_cast<ParagraphList&>(paragraphs).begin();
88 ParagraphList::iterator end = const_cast<ParagraphList&>(paragraphs).end();
89 for (; it != end; ++it) {
90 if (it == text_.cursor.par())
94 if (it != end && text_.cursor.pos() <= it->size())
95 sstate = text_; // slicing intended
97 sstate.cursor.par(end);
101 void InsetText::restoreLyXTextState() const
103 if (sstate.cursor.par() == const_cast<ParagraphList&>(paragraphs).end())
106 text_.selection.set(true);
107 // at this point just to avoid the DEPM when setting the cursor
108 text_.selection.mark(sstate.selection.mark());
109 if (sstate.selection.set()) {
110 text_.setCursor(sstate.selection.start.par(),
111 sstate.selection.start.pos(),
112 true, sstate.selection.start.boundary());
113 text_.selection.cursor = text_.cursor;
114 text_.setCursor(sstate.selection.end.par(), sstate.selection.end.pos(),
115 true, sstate.selection.end.boundary());
116 text_.setSelection();
117 text_.setCursor(sstate.cursor.par(), sstate.cursor.pos());
119 text_.setCursor(sstate.cursor.par(), sstate.cursor.pos(),
120 true, sstate.cursor.boundary());
121 text_.selection.cursor = text_.cursor;
122 text_.selection.set(false);
127 InsetText::InsetText(BufferParams const & bp)
128 : UpdatableInset(), in_update(false), text_(0, this)
130 paragraphs.push_back(Paragraph());
131 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
132 if (bp.tracking_changes)
133 paragraphs.begin()->trackChanges();
138 InsetText::InsetText(InsetText const & in)
139 : UpdatableInset(in), in_update(false), text_(0, this)
145 InsetText & InsetText::operator=(InsetText const & it)
152 void InsetText::init(InsetText const * ins)
155 text_.bv_owner = ins->text_.bv_owner;
156 setParagraphData(ins->paragraphs);
157 autoBreakRows = ins->autoBreakRows;
158 drawFrame_ = ins->drawFrame_;
159 frame_color = ins->frame_color;
162 frame_color = LColor::insetframe;
163 autoBreakRows = false;
165 the_locking_inset = 0;
166 for_each(paragraphs.begin(), paragraphs.end(),
167 boost::bind(&Paragraph::setInsetOwner, _1, this));
175 old_par = paragraphs.end();
176 last_drawn_width = -1;
177 sstate.cursor.par(paragraphs.end());
178 in_insetAllowed = false;
182 void InsetText::clear(bool just_mark_erased)
184 if (just_mark_erased) {
185 ParagraphList::iterator it = paragraphs.begin();
186 ParagraphList::iterator end = paragraphs.end();
187 for (; it != end; ++it) {
194 // This is a gross hack...
195 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
198 paragraphs.push_back(Paragraph());
199 paragraphs.begin()->setInsetOwner(this);
200 paragraphs.begin()->layout(old_layout);
207 InsetBase * InsetText::clone() const
209 return new InsetText(*this);
213 void InsetText::write(Buffer const * buf, ostream & os) const
216 writeParagraphData(buf, os);
220 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
222 ParagraphList::const_iterator it = paragraphs.begin();
223 ParagraphList::const_iterator end = paragraphs.end();
224 Paragraph::depth_type dth = 0;
225 for (; it != end; ++it) {
226 it->write(buf, os, buf->params, dth);
231 void InsetText::read(Buffer const * buf, LyXLex & lex)
234 Paragraph::depth_type depth = 0;
238 if (buf->params.tracking_changes)
239 paragraphs.begin()->trackChanges();
241 // delete the initial paragraph
243 ParagraphList::iterator pit = paragraphs.begin();
247 token = lex.getString();
250 if (token == "\\end_inset") {
254 if (token == "\\the_end") {
255 lex.printError("\\the_end read in inset! Error in document!");
260 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
263 pit = paragraphs.begin();
264 ParagraphList::iterator const end = paragraphs.end();
265 for (; pit != end; ++pit)
266 pit->setInsetOwner(this);
268 if (token != "\\end_inset") {
269 lex.printError("Missing \\end_inset at this point. "
276 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
278 BufferView * bv = mi.base.bv;
280 dim.asc = text_.rows().begin()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
281 dim.des = text_.height - dim.asc + TEXT_TO_INSET_OFFSET;
282 dim.wid = max(textWidth(bv), int(text_.width)) + 2 * TEXT_TO_INSET_OFFSET;
283 dim.wid = max(dim.wid, 10);
288 int InsetText::textWidth(BufferView * bv, bool fordraw) const
290 int w = autoBreakRows ? getMaxWidth(bv, this) : -1;
293 return max(w - 2 * TEXT_TO_INSET_OFFSET,
294 (int)getLyXText(bv)->width);
299 return w - 2 * TEXT_TO_INSET_OFFSET;
303 void InsetText::draw(PainterInfo & pi, int x, int baseline) const
308 // update our idea of where we are. Clearly, we should
309 // not have to know this information.
313 int const start_x = x;
315 BufferView * bv = pi.base.bv;
316 Painter & pain = pi.pain;
318 // repaint the background if needed
319 if (backgroundColor() != LColor::background)
320 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
322 // no draw is necessary !!!
323 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
324 top_baseline = baseline;
332 top_baseline = baseline;
333 top_y = baseline - dim_.asc;
335 if (last_drawn_width != dim_.wid) {
337 last_drawn_width = dim_.wid;
340 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
341 inset_x = cix(bv) - int(x) + drawTextXOffset;
342 inset_y = ciy() + drawTextYOffset;
345 x += TEXT_TO_INSET_OFFSET;
347 RowList::iterator rowit = text_.rows().begin();
348 RowList::iterator end = text_.rows().end();
350 int y_offset = baseline - rowit->ascent_of_text();
351 int ph = pain.paperHeight();
354 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
355 y += rowit->height();
356 first += rowit->height();
360 text_.top_y(-y_offset);
368 int yf = y_offset + first;
373 while (rowit != end && yf < ph) {
374 paintRows(*bv, text_, rowit,
375 y + y_offset + first, int(x), y + text_.top_y());
376 y += rowit->height();
377 yf += rowit->height();
383 if ((drawFrame_ == ALWAYS) || (drawFrame_ == LOCKED && locked)) {
384 drawFrame(pain, int(start_x));
387 if (need_update != INIT) {
393 void InsetText::drawFrame(Painter & pain, int x) const
395 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
397 frame_y = top_baseline - dim_.asc + ttoD2;
398 frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
399 frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
400 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
405 void InsetText::update(BufferView * bv, bool reinit)
410 if (reinit && owner()) {
412 owner()->update(bv, true);
418 if (reinit || need_update == INIT) {
420 // we should put this call where we set need_update to INIT!
423 owner()->update(bv, true);
426 int nw = getMaxWidth(bv, this);
427 if (nw > 0 && old_max_width != nw) {
434 if (!autoBreakRows && paragraphs.size() > 1)
435 collapseParagraphs(bv);
437 if (the_locking_inset) {
438 inset_x = cix(bv) - top_x + drawTextXOffset;
439 inset_y = ciy() + drawTextYOffset;
440 the_locking_inset->update(bv, reinit);
443 if ((need_update & CURSOR_PAR) && !text_.needRefresh() &&
445 text_.updateInset(the_locking_inset);
448 if (text_.needRefresh())
453 int nw = getMaxWidth(bv, this);
454 if (nw > 0 && old_max_width != nw) {
461 void InsetText::setUpdateStatus(int what) const
464 // we will to redraw us full if our LyXText wants it
465 if (text_.needRefresh())
468 // this to not draw a selection when we redraw all of it!
469 if (need_update & CURSOR && !(need_update & SELECTION)) {
470 if (text_.selection.set())
472 text_.clearSelection();
477 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
479 if (!autoBreakRows && paragraphs.size() > 1)
480 collapseParagraphs(bv);
482 text_.partialRebreak();
483 setUpdateStatus(what);
484 bool flag = mark_dirty ||
485 ((need_update != CURSOR && need_update != NONE) ||
486 text_.needRefresh() || text_.selection.set());
487 if (!text_.selection.set())
488 text_.selection.cursor = text_.cursor;
494 bv->updateInset(const_cast<InsetText *>(this));
497 if (need_update == CURSOR)
499 bv->owner()->view_state_changed();
500 bv->owner()->updateMenubar();
501 bv->owner()->updateToolbar();
502 if (old_par != cpar()) {
503 bv->owner()->setLayout(cpar()->layout()->name());
509 string const InsetText::editMessage() const
511 return _("Opened Text Inset");
515 void InsetText::insetUnlock(BufferView * bv)
517 if (the_locking_inset) {
518 the_locking_inset->insetUnlock(bv);
519 the_locking_inset = 0;
520 updateLocal(bv, CURSOR_PAR, false);
526 if (text_.selection.set()) {
527 text_.clearSelection();
529 } else if (owner()) {
530 bv->owner()->setLayout(owner()->getLyXText(bv)
531 ->cursor.par()->layout()->name());
533 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
534 // hack for deleteEmptyParMech
535 ParagraphList::iterator first_par = paragraphs.begin();
536 if (!first_par->empty()) {
537 text_.setCursor(first_par, 0);
538 } else if (paragraphs.size() > 1) {
539 text_.setCursor(boost::next(first_par), 0);
542 updateLocal(bv, code, false);
545 setUpdateStatus(code);
550 void InsetText::lockInset(BufferView * bv)
553 the_locking_inset = 0;
554 inset_pos = inset_x = inset_y = 0;
555 inset_boundary = false;
556 inset_par = paragraphs.end();
557 old_par = paragraphs.end();
558 text_.setCursor(paragraphs.begin(), 0);
559 text_.clearSelection();
561 // If the inset is empty set the language of the current font to the
562 // language to the surronding text (if different).
563 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
564 bv->getParentLanguage(this) != text_.current_font.language()) {
565 LyXFont font(LyXFont::ALL_IGNORE);
566 font.setLanguage(bv->getParentLanguage(this));
567 setFont(bv, font, false);
570 if (drawFrame_ == LOCKED)
571 code = CURSOR|DRAW_FRAME;
572 setUpdateStatus(code);
576 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
578 the_locking_inset = inset;
579 inset_x = cix(bv) - top_x + drawTextXOffset;
580 inset_y = ciy() + drawTextYOffset;
583 inset_boundary = cboundary();
584 updateLocal(bv, CURSOR, false);
588 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
590 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
594 if (!the_locking_inset) {
595 ParagraphList::iterator pit = paragraphs.begin();
596 ParagraphList::iterator pend = paragraphs.end();
598 int const id = inset->id();
599 for (; pit != pend; ++pit) {
600 InsetList::iterator it =
601 pit->insetlist.begin();
602 InsetList::iterator const end =
603 pit->insetlist.end();
604 for (; it != end; ++it) {
605 if (it->inset == inset) {
606 getLyXText(bv)->setCursorIntern(pit, it->pos);
607 lockInset(bv, inset);
610 if (it->inset->getInsetFromID(id)) {
611 getLyXText(bv)->setCursorIntern(pit, it->pos);
612 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
613 return the_locking_inset->lockInsetInInset(bv, inset);
619 if (inset == cpar()->getInset(cpos())) {
620 lyxerr[Debug::INSETS] << "OK" << endl;
621 lockInset(bv, inset);
623 } else if (the_locking_inset && (the_locking_inset == inset)) {
624 if (cpar() == inset_par && cpos() == inset_pos) {
625 lyxerr[Debug::INSETS] << "OK" << endl;
626 inset_x = cix(bv) - top_x + drawTextXOffset;
627 inset_y = ciy() + drawTextYOffset;
629 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
631 } else if (the_locking_inset) {
632 lyxerr[Debug::INSETS] << "MAYBE" << endl;
633 return the_locking_inset->lockInsetInInset(bv, inset);
635 lyxerr[Debug::INSETS] << "NOT OK" << endl;
640 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
643 if (!the_locking_inset)
645 if (the_locking_inset == inset) {
646 the_locking_inset->insetUnlock(bv);
647 getLyXText(bv)->updateInset(inset);
648 the_locking_inset = 0;
650 moveRightIntern(bv, true, false);
651 old_par = paragraphs.end(); // force layout setting
655 updateLocal(bv, CURSOR, false);
658 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
662 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
664 if (!autoBreakRows && paragraphs.size() > 1)
665 collapseParagraphs(bv);
670 if (inset->owner() != this) {
671 int ustat = CURSOR_PAR;
673 UpdatableInset * tl_inset = the_locking_inset;
675 found = tl_inset->updateInsetInInset(bv, inset);
677 tl_inset = static_cast<UpdatableInset *>(inset);
678 while(tl_inset->owner() && tl_inset->owner() != this)
679 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
680 if (!tl_inset->owner())
682 found = tl_inset->updateInsetInInset(bv, inset);
686 text_.updateInset(tl_inset);
688 setUpdateStatus(ustat);
691 bool found = text_.updateInset(inset);
693 setUpdateStatus(CURSOR_PAR);
694 if (the_locking_inset &&
695 cpar() == inset_par && cpos() == inset_pos)
697 inset_x = cix(bv) - top_x + drawTextXOffset;
698 inset_y = ciy() + drawTextYOffset;
705 void InsetText::lfunMousePress(FuncRequest const & cmd)
709 // use this to check mouse motion for selection!
713 BufferView * bv = cmd.view();
714 FuncRequest cmd1 = cmd;
720 int tmp_x = cmd.x - drawTextXOffset;
721 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
722 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
724 if (the_locking_inset) {
725 if (the_locking_inset == inset) {
726 the_locking_inset->localDispatch(cmd1);
729 // otherwise only unlock the_locking_inset
730 the_locking_inset->insetUnlock(bv);
731 the_locking_inset = 0;
734 no_selection = false;
736 if (bv->theLockingInset()) {
737 if (isHighlyEditableInset(inset)) {
738 // We just have to lock the inset before calling a
740 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
741 if (!bv->lockInset(uinset)) {
742 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
744 inset->localDispatch(cmd1);
745 if (the_locking_inset)
746 updateLocal(bv, CURSOR, false);
751 bool paste_internally = false;
752 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
753 localDispatch(FuncRequest(bv, LFUN_COPY));
754 paste_internally = true;
756 int old_top_y = text_.top_y();
758 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
760 // set the selection cursor!
761 text_.selection.cursor = text_.cursor;
762 text_.cursor.x_fix(text_.cursor.x());
764 if (text_.selection.set()) {
765 text_.clearSelection();
766 updateLocal(bv, FULL, false);
768 text_.clearSelection();
769 updateLocal(bv, CURSOR, false);
772 bv->owner()->setLayout(cpar()->layout()->name());
774 // we moved the view we cannot do mouse selection in this case!
775 if (getLyXText(bv)->top_y() != old_top_y)
778 // Insert primary selection with middle mouse
779 // if there is a local selection in the current buffer,
781 if (cmd.button() == mouse_button::button2) {
782 if (paste_internally)
783 localDispatch(FuncRequest(bv, LFUN_PASTE));
785 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
788 getLyXText(bv)->clearSelection();
793 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
795 BufferView * bv = cmd.view();
796 FuncRequest cmd1 = cmd;
801 if (the_locking_inset)
802 return the_locking_inset->localDispatch(cmd1);
804 int tmp_x = cmd.x - drawTextXOffset;
805 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
806 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
809 // This code should probably be removed now. Simple insets
810 // (!highlyEditable) can actually take the localDispatch,
811 // and turn it into edit() if necessary. But we still
812 // need to deal properly with the whole relative vs.
813 // absolute mouse co-ords thing in a realiable, sensible way
815 if (isHighlyEditableInset(inset))
816 ret = inset->localDispatch(cmd1);
818 inset_x = cix(bv) - top_x + drawTextXOffset;
819 inset_y = ciy() + drawTextYOffset;
820 cmd1.x = cmd.x - inset_x;
821 cmd1.y = cmd.x - inset_y;
822 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
826 ret = inset->localDispatch(cmd1);
827 updateLocal(bv, CURSOR_PAR, false);
834 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
836 FuncRequest cmd1 = cmd;
840 if (the_locking_inset) {
841 the_locking_inset->localDispatch(cmd1);
845 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
848 BufferView * bv = cmd.view();
849 LyXCursor cur = text_.cursor;
850 text_.setCursorFromCoordinates
851 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
852 text_.cursor.x_fix(text_.cursor.x());
853 if (cur == text_.cursor)
855 text_.setSelection();
856 bool flag = (text_.toggle_cursor.par() != text_.toggle_end_cursor.par() ||
857 text_.toggle_cursor.pos() != text_.toggle_end_cursor.pos());
859 updateLocal(bv, SELECTION, false);
864 Inset::RESULT InsetText::localDispatch(FuncRequest const & cmd)
866 BufferView * bv = cmd.view();
869 if (cmd.action == LFUN_INSET_EDIT) {
870 UpdatableInset::localDispatch(cmd);
872 if (!bv->lockInset(this)) {
873 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
878 the_locking_inset = 0;
879 inset_pos = inset_x = inset_y = 0;
880 inset_boundary = false;
881 inset_par = paragraphs.end();
882 old_par = paragraphs.end();
885 if (cmd.argument.size()) {
886 if (cmd.argument == "left")
887 text_.setCursor(paragraphs.begin(), 0);
889 ParagraphList::iterator it = paragraphs.begin();
890 ParagraphList::iterator end = paragraphs.end();
891 while (boost::next(it) != end)
893 // int const pos = (p->size() ? p->size()-1 : p->size());
894 text_.setCursor(it, it->size());
897 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
898 // we put here -1 and not button as now the button in the
899 // edit call should not be needed we will fix this in 1.3.x
900 // cycle hopefully (Jug 20020509)
901 // FIXME: GUII I've changed this to none: probably WRONG
902 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
903 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
905 text_.cursor.x_fix(text_.cursor.x());
909 text_.clearSelection();
912 // If the inset is empty set the language of the current font to the
913 // language to the surronding text (if different).
914 if (paragraphs.begin()->empty() &&
915 paragraphs.size() == 1 &&
916 bv->getParentLanguage(this) != text_.current_font.language())
918 LyXFont font(LyXFont::ALL_IGNORE);
919 font.setLanguage(bv->getParentLanguage(this));
920 setFont(bv, font, false);
924 if (drawFrame_ == LOCKED)
925 code = CURSOR | DRAW_FRAME;
927 updateLocal(bv, code, false);
928 // Tell the paragraph dialog that we've entered an insettext.
929 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
934 switch (cmd.action) {
935 case LFUN_MOUSE_PRESS:
938 case LFUN_MOUSE_MOTION:
939 lfunMouseMotion(cmd);
941 case LFUN_MOUSE_RELEASE:
942 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
947 bool was_empty = (paragraphs.begin()->empty() &&
948 paragraphs.size() == 1);
950 no_selection = false;
951 RESULT result = UpdatableInset::localDispatch(cmd);
952 if (result != UNDISPATCHED)
956 if (cmd.action < 0 && cmd.argument.empty())
959 if (the_locking_inset) {
960 result = the_locking_inset->localDispatch(cmd);
961 if (result == DISPATCHED_NOUPDATE)
963 else if (result == DISPATCHED) {
964 updateLocal(bv, CURSOR_PAR, false);
966 } else if (result >= FINISHED) {
969 moveRightIntern(bv, false, false);
973 if ((result = moveUp(bv)) >= FINISHED) {
974 updateLocal(bv, CURSOR, false);
975 bv->unlockInset(this);
979 if ((result = moveDown(bv)) >= FINISHED) {
980 updateLocal(bv, CURSOR, false);
981 bv->unlockInset(this);
988 the_locking_inset = 0;
989 updateLocal(bv, CURSOR, false);
990 // make sure status gets reset immediately
991 bv->owner()->clearMessage();
998 // what type of update to do on a cursor movement
999 int cursor_update = CURSOR;
1001 if (text_.selection.set())
1002 cursor_update = SELECTION;
1004 switch (cmd.action) {
1007 case LFUN_SELFINSERT:
1008 if (bv->buffer()->isReadonly()) {
1009 // setErrorMessage(N_("Document is read only"));
1012 if (!cmd.argument.empty()) {
1013 /* Automatically delete the currently selected
1014 * text and replace it with what is being
1015 * typed in now. Depends on lyxrc settings
1016 * "auto_region_delete", which defaults to
1019 // This should not be needed here and is also WRONG!
1020 setUndo(bv, Undo::INSERT, text_.cursor.par());
1023 if (lyxrc.auto_region_delete) {
1024 if (text_.selection.set()) {
1025 text_.cutSelection(false, false);
1028 text_.clearSelection();
1029 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
1030 bv->owner()->getIntl().getTransManager().
1031 TranslateAndInsert(cmd.argument[i], &text_);
1034 text_.selection.cursor = text_.cursor;
1035 updwhat = CURSOR | CURSOR_PAR;
1037 result = DISPATCHED_NOUPDATE;
1040 // cursor movements that need special handling
1043 result = moveRight(bv);
1045 updwhat = cursor_update;
1049 result = moveLeft(bv);
1050 updwhat = cursor_update;
1054 result = moveDown(bv);
1055 updwhat = cursor_update;
1059 result = moveUp(bv);
1060 updwhat = cursor_update;
1064 if (crow() == text_.rows().begin())
1065 result = FINISHED_UP;
1067 text_.cursorPrevious();
1068 text_.clearSelection();
1069 result = DISPATCHED_NOUPDATE;
1071 updwhat = cursor_update;
1075 if (boost::next(crow()) == text_.rows().end())
1076 result = FINISHED_DOWN;
1079 text_.clearSelection();
1080 result = DISPATCHED_NOUPDATE;
1082 updwhat = cursor_update;
1085 case LFUN_BACKSPACE: {
1086 if (text_.selection.set())
1087 text_.cutSelection(true, false);
1090 updwhat = CURSOR_PAR;
1096 if (text_.selection.set()) {
1097 text_.cutSelection(true, false);
1101 updwhat = CURSOR_PAR;
1107 text_.cutSelection(true, true);
1108 updwhat = CURSOR_PAR;
1115 text_.copySelection();
1116 updwhat = CURSOR_PAR;
1119 case LFUN_PASTESELECTION:
1121 string const clip(bv->getClipboard());
1125 if (cmd.argument == "paragraph") {
1126 text_.insertStringAsParagraphs(clip);
1128 text_.insertStringAsLines(clip);
1131 text_.clearSelection();
1133 updwhat = CURSOR_PAR;
1139 if (!autoBreakRows) {
1140 if (CutAndPaste::nrOfParagraphs() > 1) {
1141 #ifdef WITH_WARNINGS
1142 #warning FIXME horrendously bad UI
1144 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1149 replaceSelection(bv->getLyXText());
1150 size_t sel_index = 0;
1151 string const & arg = cmd.argument;
1152 if (isStrUnsignedInt(arg)) {
1153 size_t const paste_arg = strToUnsignedInt(arg);
1154 #warning FIXME Check if the arg is in the domain of available selections.
1155 sel_index = paste_arg;
1157 text_.pasteSelection(sel_index);
1159 text_.clearSelection();
1160 updwhat = CURSOR_PAR;
1165 case LFUN_BREAKPARAGRAPH:
1166 if (!autoBreakRows) {
1167 result = DISPATCHED;
1170 replaceSelection(bv->getLyXText());
1171 text_.breakParagraph(paragraphs, 0);
1172 updwhat = CURSOR | FULL;
1176 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1177 if (!autoBreakRows) {
1178 result = DISPATCHED;
1181 replaceSelection(bv->getLyXText());
1182 text_.breakParagraph(paragraphs, 1);
1183 updwhat = CURSOR | FULL;
1187 case LFUN_BREAKLINE: {
1188 if (!autoBreakRows) {
1189 result = DISPATCHED;
1193 replaceSelection(bv->getLyXText());
1194 text_.insertInset(new InsetNewline);
1195 updwhat = CURSOR | CURSOR_PAR;
1201 // do not set layouts on non breakable textinsets
1202 if (autoBreakRows) {
1203 string cur_layout = cpar()->layout()->name();
1205 // Derive layout number from given argument (string)
1206 // and current buffer's textclass (number). */
1207 LyXTextClass const & tclass =
1208 bv->buffer()->params.getLyXTextClass();
1209 string layout = cmd.argument;
1210 bool hasLayout = tclass.hasLayout(layout);
1212 // If the entry is obsolete, use the new one instead.
1214 string const & obs =
1215 tclass[layout]->obsoleted_by();
1220 // see if we found the layout number:
1222 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1223 bv->owner()->dispatch(lf);
1227 if (cur_layout != layout) {
1228 cur_layout = layout;
1229 text_.setLayout(layout);
1230 bv->owner()->setLayout(cpar()->layout()->name());
1231 updwhat = CURSOR_PAR;
1235 // reset the layout box
1236 bv->owner()->setLayout(cpar()->layout()->name());
1239 case LFUN_PARAGRAPH_SPACING:
1240 // This one is absolutely not working. When fiddling with this
1241 // it also seems to me that the paragraphs inside the insettext
1242 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1243 // FIXME: how old is this comment ? ...
1245 ParagraphList::iterator pit = text_.cursor.par();
1246 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1247 float cur_value = 1.0;
1248 if (cur_spacing == Spacing::Other) {
1249 cur_value = pit->params().spacing().getValue();
1252 istringstream istr(STRCONV(cmd.argument));
1255 Spacing::Space new_spacing = cur_spacing;
1256 float new_value = cur_value;
1258 lyxerr << "Missing argument to `paragraph-spacing'"
1260 } else if (tmp == "single") {
1261 new_spacing = Spacing::Single;
1262 } else if (tmp == "onehalf") {
1263 new_spacing = Spacing::Onehalf;
1264 } else if (tmp == "double") {
1265 new_spacing = Spacing::Double;
1266 } else if (tmp == "other") {
1267 new_spacing = Spacing::Other;
1270 lyxerr << "new_value = " << tmpval << endl;
1273 } else if (tmp == "default") {
1274 new_spacing = Spacing::Default;
1276 lyxerr << _("Unknown spacing argument: ")
1277 << cmd.argument << endl;
1279 if (cur_spacing != new_spacing || cur_value != new_value) {
1280 pit->params().spacing(Spacing(new_spacing, new_value));
1281 updwhat = CURSOR_PAR;
1287 // These need to do repaints but don't require
1288 // special handling otherwise. A *lot* of the
1289 // above could probably be done similarly ...
1294 case LFUN_WORDRIGHT:
1295 // these two are really unhandled ...
1297 case LFUN_BEGINNINGBUF:
1298 updwhat = cursor_update;
1299 if (!bv->dispatch(cmd))
1300 result = UNDISPATCHED;
1309 case LFUN_WORDLEFTSEL:
1310 case LFUN_WORDRIGHTSEL:
1311 updwhat = SELECTION;
1316 if (!bv->dispatch(cmd))
1317 result = UNDISPATCHED;
1322 updateLocal(bv, updwhat, updflag);
1323 /// If the action has deleted all text in the inset, we need to change the
1324 // language to the language of the surronding text.
1325 if (!was_empty && paragraphs.begin()->empty() &&
1326 paragraphs.size() == 1) {
1327 LyXFont font(LyXFont::ALL_IGNORE);
1328 font.setLanguage(bv->getParentLanguage(this));
1329 setFont(bv, font, false);
1332 if (result >= FINISHED)
1333 bv->unlockInset(this);
1335 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1336 result = DISPATCHED;
1341 int InsetText::latex(Buffer const * buf, ostream & os,
1342 LatexRunParams const & runparams) const
1345 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1346 return texrow.rows();
1350 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1352 unsigned int lines = 0;
1354 ParagraphList::const_iterator beg = paragraphs.begin();
1355 ParagraphList::const_iterator end = paragraphs.end();
1356 ParagraphList::const_iterator it = beg;
1357 for (; it != end; ++it) {
1358 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1359 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1366 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1368 unsigned int lines = 0;
1370 vector<string> environment_stack(10);
1371 vector<string> environment_inner(10);
1373 int const command_depth = 0;
1376 Paragraph::depth_type depth = 0; // paragraph depth
1378 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1379 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1381 for (; pit != pend; ++pit) {
1383 int desc_on = 0; // description mode
1385 LyXLayout_ptr const & style = pit->layout();
1387 // environment tag closing
1388 for (; depth > pit->params().depth(); --depth) {
1389 if (environment_inner[depth] != "!-- --") {
1390 item_name = "listitem";
1391 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1392 if (environment_inner[depth] == "varlistentry")
1393 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1395 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1396 environment_stack[depth].erase();
1397 environment_inner[depth].erase();
1400 if (depth == pit->params().depth()
1401 && environment_stack[depth] != style->latexname()
1402 && !environment_stack[depth].empty()) {
1403 if (environment_inner[depth] != "!-- --") {
1404 item_name= "listitem";
1405 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1406 if (environment_inner[depth] == "varlistentry")
1407 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1410 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1412 environment_stack[depth].erase();
1413 environment_inner[depth].erase();
1416 // Write opening SGML tags.
1417 switch (style->latextype) {
1418 case LATEX_PARAGRAPH:
1419 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1423 buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1427 case LATEX_ENVIRONMENT:
1428 case LATEX_ITEM_ENVIRONMENT:
1429 if (depth < pit->params().depth()) {
1430 depth = pit->params().depth();
1431 environment_stack[depth].erase();
1434 if (environment_stack[depth] != style->latexname()) {
1435 if (environment_stack.size() == depth + 1) {
1436 environment_stack.push_back("!-- --");
1437 environment_inner.push_back("!-- --");
1439 environment_stack[depth] = style->latexname();
1440 environment_inner[depth] = "!-- --";
1441 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1443 if (environment_inner[depth] != "!-- --") {
1444 item_name= "listitem";
1445 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1446 if (environment_inner[depth] == "varlistentry")
1447 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1451 if (style->latextype == LATEX_ENVIRONMENT) {
1452 if (!style->latexparam().empty()) {
1453 if (style->latexparam() == "CDATA")
1456 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1461 desc_on = (style->labeltype == LABEL_MANUAL);
1463 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1464 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1466 item_name = desc_on ? "term" : "para";
1467 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1471 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1475 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1478 // write closing SGML tags
1479 switch (style->latextype) {
1480 case LATEX_ENVIRONMENT:
1481 if (!style->latexparam().empty()) {
1482 if (style->latexparam() == "CDATA")
1485 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1488 case LATEX_ITEM_ENVIRONMENT:
1489 if (desc_on == 1) break;
1491 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1493 case LATEX_PARAGRAPH:
1494 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1497 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1503 for (int d = depth; d >= 0; --d) {
1504 if (!environment_stack[depth].empty()) {
1505 if (environment_inner[depth] != "!-- --") {
1506 item_name = "listitem";
1507 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1508 if (environment_inner[depth] == "varlistentry")
1509 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1512 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1520 void InsetText::validate(LaTeXFeatures & features) const
1522 for_each(paragraphs.begin(), paragraphs.end(),
1523 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1527 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1529 if (the_locking_inset) {
1530 the_locking_inset->getCursor(bv, x, y);
1534 y = cy() + InsetText::y();
1538 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1540 if (the_locking_inset) {
1541 the_locking_inset->getCursorPos(bv, x, y);
1544 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1545 y = cy() - TEXT_TO_INSET_OFFSET;
1549 int InsetText::insetInInsetY() const
1551 if (!the_locking_inset)
1554 return (inset_y + the_locking_inset->insetInInsetY());
1558 void InsetText::fitInsetCursor(BufferView * bv) const
1560 if (the_locking_inset) {
1561 the_locking_inset->fitInsetCursor(bv);
1564 LyXFont const font =
1565 getLyXText(bv)->getFont(bv->buffer(), cpar(), cpos());
1567 int const asc = font_metrics::maxAscent(font);
1568 int const desc = font_metrics::maxDescent(font);
1570 if (bv->fitLockedInsetCursor(cx(bv), cy(), asc, desc))
1571 need_update |= FULL;
1576 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1578 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1579 return moveLeftIntern(bv, false, activate_inset, selecting);
1581 return moveRightIntern(bv, true, activate_inset, selecting);
1586 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1588 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1589 return moveRightIntern(bv, true, activate_inset, selecting);
1591 return moveLeftIntern(bv, false, activate_inset, selecting);
1596 InsetText::moveRightIntern(BufferView * bv, bool front,
1597 bool activate_inset, bool selecting)
1599 ParagraphList::iterator c_par = cpar();
1601 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1602 return FINISHED_RIGHT;
1603 if (activate_inset && checkAndActivateInset(bv, front))
1605 text_.cursorRight(bv);
1607 text_.clearSelection();
1608 return DISPATCHED_NOUPDATE;
1613 InsetText::moveLeftIntern(BufferView * bv, bool front,
1614 bool activate_inset, bool selecting)
1616 if (cpar() == paragraphs.begin() && cpos() <= 0)
1618 text_.cursorLeft(bv);
1620 text_.clearSelection();
1621 if (activate_inset && checkAndActivateInset(bv, front))
1623 return DISPATCHED_NOUPDATE;
1627 Inset::RESULT InsetText::moveUp(BufferView * bv)
1629 if (crow() == text_.rows().begin())
1632 text_.clearSelection();
1633 return DISPATCHED_NOUPDATE;
1637 Inset::RESULT InsetText::moveDown(BufferView * bv)
1639 if (boost::next(crow()) == text_.rows().end())
1640 return FINISHED_DOWN;
1641 text_.cursorDown(bv);
1642 text_.clearSelection();
1643 return DISPATCHED_NOUPDATE;
1647 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1649 if (the_locking_inset) {
1650 if (the_locking_inset->insetAllowed(inset))
1651 return the_locking_inset->insertInset(bv, inset);
1654 inset->setOwner(this);
1655 getLyXText(bv)->insertInset(inset);
1657 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1662 bool InsetText::insetAllowed(Inset::Code code) const
1664 // in_insetAllowed is a really gross hack,
1665 // to allow us to call the owner's insetAllowed
1666 // without stack overflow, which can happen
1667 // when the owner uses InsetCollapsable::insetAllowed()
1669 if (in_insetAllowed)
1671 in_insetAllowed = true;
1672 if (the_locking_inset)
1673 ret = the_locking_inset->insetAllowed(code);
1675 ret = owner()->insetAllowed(code);
1676 in_insetAllowed = false;
1681 UpdatableInset * InsetText::getLockingInset() const
1683 return the_locking_inset ? the_locking_inset->getLockingInset() :
1684 const_cast<InsetText *>(this);
1688 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1692 if (the_locking_inset)
1693 return the_locking_inset->getFirstLockingInsetOfType(c);
1698 bool InsetText::showInsetDialog(BufferView * bv) const
1700 if (the_locking_inset)
1701 return the_locking_inset->showInsetDialog(bv);
1706 void InsetText::getLabelList(std::vector<string> & list) const
1708 ParagraphList::const_iterator pit = paragraphs.begin();
1709 ParagraphList::const_iterator pend = paragraphs.end();
1710 for (; pit != pend; ++pit) {
1711 InsetList::const_iterator beg = pit->insetlist.begin();
1712 InsetList::const_iterator end = pit->insetlist.end();
1713 for (; beg != end; ++beg)
1714 beg->inset->getLabelList(list);
1719 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1722 if (the_locking_inset) {
1723 the_locking_inset->setFont(bv, font, toggleall, selectall);
1727 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1728 || cpar()->empty()) {
1729 getLyXText(bv)->setFont(font, toggleall);
1734 if (text_.selection.set()) {
1735 setUndo(bv, Undo::EDIT, text_.cursor.par());
1741 text_.toggleFree(font, toggleall);
1744 text_.clearSelection();
1748 bool flag = (selectall || text_.selection.set());
1751 updateLocal(bv, FULL, true);
1753 updateLocal(bv, CURSOR_PAR, true);
1757 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1759 if (cpar()->isInset(cpos())) {
1761 static_cast<UpdatableInset*>(cpar()->getInset(cpos()));
1762 if (!isHighlyEditableInset(inset))
1764 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1765 inset->localDispatch(cmd);
1766 if (!the_locking_inset)
1768 updateLocal(bv, CURSOR, false);
1775 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1776 mouse_button::state button)
1778 x -= drawTextXOffset;
1780 int dummyy = y + dim_.asc;
1781 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1782 // we only do the edit() call if the inset was hit by the mouse
1783 // or if it is a highly editable inset. So we should call this
1784 // function from our own edit with button < 0.
1785 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1787 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1795 inset_x = cix(bv) - top_x + drawTextXOffset;
1796 inset_y = ciy() + drawTextYOffset;
1797 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1798 inset->localDispatch(cmd);
1799 if (!the_locking_inset)
1801 updateLocal(bv, CURSOR, false);
1808 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1811 int w = UpdatableInset::getMaxWidth(bv, inset);
1816 w = w - top_x + owner()->x();
1819 w -= (2 * TEXT_TO_INSET_OFFSET);
1822 return UpdatableInset::getMaxWidth(bv, inset);
1827 void InsetText::setParagraphData(ParagraphList const & plist)
1829 // we have to unlock any locked inset otherwise we're in troubles
1830 the_locking_inset = 0;
1832 // But it it makes no difference that is a lot better.
1834 // See if this can be simplified when std::list is in effect.
1837 ParagraphList::const_iterator it = plist.begin();
1838 ParagraphList::const_iterator end = plist.end();
1839 for (; it != end; ++it) {
1840 paragraphs.push_back(*it);
1841 Paragraph & tmp = paragraphs.back();
1842 tmp.setInsetOwner(this);
1850 void InsetText::markNew(bool track_changes)
1852 ParagraphList::iterator pit = paragraphs.begin();
1853 ParagraphList::iterator pend = paragraphs.end();
1854 for (; pit != pend; ++pit) {
1855 if (track_changes) {
1856 pit->trackChanges();
1858 // no-op when not tracking
1859 pit->cleanChanges();
1865 void InsetText::setText(string const & data, LyXFont const & font)
1868 for (unsigned int i = 0; i < data.length(); ++i)
1869 paragraphs.begin()->insertChar(i, data[i], font);
1874 void InsetText::setAutoBreakRows(bool flag)
1876 if (flag != autoBreakRows) {
1877 autoBreakRows = flag;
1885 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1887 if (how != drawFrame_) {
1890 updateLocal(bv, DRAW_FRAME, false);
1895 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1897 if (frame_color != col) {
1900 updateLocal(bv, DRAW_FRAME, false);
1905 int InsetText::cx(BufferView * bv) const
1907 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1908 if (the_locking_inset) {
1909 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1910 text_.cursor.pos());
1911 if (font.isVisibleRightToLeft())
1912 x -= the_locking_inset->width(bv, font);
1918 int InsetText::cix(BufferView * bv) const
1920 int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1921 if (the_locking_inset) {
1922 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1923 text_.cursor.pos());
1924 if (font.isVisibleRightToLeft())
1925 x -= the_locking_inset->width(bv, font);
1931 int InsetText::cy() const
1933 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1937 int InsetText::ciy() const
1939 return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1943 pos_type InsetText::cpos() const
1945 return text_.cursor.pos();
1949 ParagraphList::iterator InsetText::cpar() const
1951 return text_.cursor.par();
1955 bool InsetText::cboundary() const
1957 return text_.cursor.boundary();
1961 RowList::iterator InsetText::crow() const
1963 return text_.cursorRow();
1967 LyXText * InsetText::getLyXText(BufferView const * bv,
1968 bool const recursive) const
1971 if (recursive && the_locking_inset)
1972 return the_locking_inset->getLyXText(bv, true);
1977 void InsetText::setViewCache(BufferView const * bv) const
1980 text_.bv_owner = const_cast<BufferView *>(bv);
1984 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1987 /// then remove all LyXText in text-insets
1988 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1989 const_cast<ParagraphList&>(paragraphs).end(),
1990 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1995 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1997 if (paragraphs.size() == 1 && paragraphs.begin()->empty()) {
1998 // no data, resize not neccessary!
1999 // we have to do this as a fixed width may have changed!
2002 restoreLyXTextState();
2012 // one endless line, resize normally not necessary
2013 if (!force && getMaxWidth(bv, this) < 0)
2018 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2019 const_cast<ParagraphList&>(paragraphs).end(),
2020 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2023 restoreLyXTextState();
2025 if (the_locking_inset) {
2026 inset_x = cix(bv) - top_x + drawTextXOffset;
2027 inset_y = ciy() + drawTextYOffset;
2030 text_.top_y(bv->screen().topCursorVisible(&text_));
2032 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2033 // this will scroll the screen such that the cursor becomes visible
2034 bv->updateScrollbar();
2036 need_update |= FULL;
2041 void InsetText::reinitLyXText() const
2043 BufferView * bv = text_.bv_owner;
2050 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2051 const_cast<ParagraphList&>(paragraphs).end(),
2052 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2055 restoreLyXTextState();
2056 if (the_locking_inset) {
2057 inset_x = cix(bv) - top_x + drawTextXOffset;
2058 inset_y = ciy() + drawTextYOffset;
2060 text_.top_y(bv->screen().topCursorVisible(&text_));
2062 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2063 // this will scroll the screen such that the cursor becomes visible
2064 bv->updateScrollbar();
2071 void InsetText::removeNewlines()
2073 bool changed = false;
2075 ParagraphList::iterator it = paragraphs.begin();
2076 ParagraphList::iterator end = paragraphs.end();
2077 for (; it != end; ++it) {
2078 for (int i = 0; i < it->size(); ++i) {
2079 if (it->isNewline(i)) {
2090 bool InsetText::nodraw() const
2092 if (the_locking_inset)
2093 return the_locking_inset->nodraw();
2094 return UpdatableInset::nodraw();
2098 int InsetText::scroll(bool recursive) const
2100 int sx = UpdatableInset::scroll(false);
2102 if (recursive && the_locking_inset)
2103 sx += the_locking_inset->scroll(recursive);
2109 void InsetText::selectAll(BufferView * bv)
2111 getLyXText(bv)->cursorTop();
2112 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2113 getLyXText(bv)->cursorBottom();
2114 getLyXText(bv)->setSelection();
2118 void InsetText::clearSelection(BufferView * bv)
2120 getLyXText(bv)->clearSelection();
2124 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2126 Painter & pain = bv->painter();
2128 int h = dim_.asc + dim_.des;
2129 int ty = baseline - dim_.asc;
2135 if ((ty + h) > pain.paperHeight())
2136 h = pain.paperHeight();
2137 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2138 w = pain.paperWidth();
2139 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2144 ParagraphList * InsetText::getParagraphs(int i) const
2146 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2150 LyXCursor const & InsetText::cursor(BufferView * bv) const
2152 if (the_locking_inset)
2153 return the_locking_inset->cursor(bv);
2154 return getLyXText(bv)->cursor;
2158 Inset * InsetText::getInsetFromID(int id_arg) const
2161 return const_cast<InsetText *>(this);
2163 ParagraphList::const_iterator pit = paragraphs.begin();
2164 ParagraphList::const_iterator pend = paragraphs.end();
2165 for (; pit != pend; ++pit) {
2166 InsetList::const_iterator it = pit->insetlist.begin();
2167 InsetList::const_iterator end = pit->insetlist.end();
2168 for (; it != end; ++it) {
2169 if (it->inset->id() == id_arg)
2171 Inset * in = it->inset->getInsetFromID(id_arg);
2181 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2184 if (the_locking_inset) {
2185 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2186 if (!word.word().empty()) {
2190 // we have to go on checking so move cursor to the next char
2191 text_.cursor.pos(text_.cursor.pos() + 1);
2193 word = text_.selectNextWordToSpellcheck(value);
2194 if (word.word().empty())
2195 bv->unlockInset(const_cast<InsetText *>(this));
2202 void InsetText::selectSelectedWord(BufferView * bv)
2204 if (the_locking_inset) {
2205 the_locking_inset->selectSelectedWord(bv);
2208 getLyXText(bv)->selectSelectedWord();
2209 updateLocal(bv, SELECTION, false);
2213 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2215 if (the_locking_inset) {
2216 the_locking_inset->toggleSelection(bv, kill_selection);
2219 int x = top_x + TEXT_TO_INSET_OFFSET;
2221 RowList::iterator rowit = text_.rows().begin();
2222 RowList::iterator end = text_.rows().end();
2223 int y_offset = top_baseline - rowit->ascent_of_text();
2225 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2226 y += rowit->height();
2232 if (need_update & SELECTION)
2234 bv->screen().toggleSelection(&text_, bv, kill_selection, y_offset, x);
2238 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2240 if (the_locking_inset) {
2241 if (the_locking_inset->nextChange(bv, length))
2243 text_.cursorRight(true);
2245 lyxfind::SearchResult result =
2246 lyxfind::findNextChange(bv, &text_, length);
2248 if (result == lyxfind::SR_FOUND) {
2249 LyXCursor cur = text_.cursor;
2250 bv->unlockInset(bv->theLockingInset());
2251 if (bv->lockInset(this))
2254 text_.setSelectionRange(length);
2255 updateLocal(bv, SELECTION, false);
2257 return result != lyxfind::SR_NOT_FOUND;
2261 bool InsetText::searchForward(BufferView * bv, string const & str,
2264 if (the_locking_inset) {
2265 if (the_locking_inset->searchForward(bv, str, cs, mw))
2267 text_.cursorRight(true);
2269 lyxfind::SearchResult result =
2270 lyxfind::LyXFind(bv, &text_, str, true, cs, mw);
2272 if (result == lyxfind::SR_FOUND) {
2273 LyXCursor cur = text_.cursor;
2274 bv->unlockInset(bv->theLockingInset());
2275 if (bv->lockInset(this))
2278 text_.setSelectionRange(str.length());
2279 updateLocal(bv, SELECTION, false);
2281 return (result != lyxfind::SR_NOT_FOUND);
2284 bool InsetText::searchBackward(BufferView * bv, string const & str,
2287 if (the_locking_inset) {
2288 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2292 ParagraphList::iterator pit = paragraphs.begin();
2293 ParagraphList::iterator pend = paragraphs.end();
2295 while (boost::next(pit) != pend)
2298 text_.setCursor(pit, pit->size());
2300 lyxfind::SearchResult result =
2301 lyxfind::LyXFind(bv, &text_, str, false, cs, mw);
2303 if (result == lyxfind::SR_FOUND) {
2304 LyXCursor cur = text_.cursor;
2305 bv->unlockInset(bv->theLockingInset());
2306 if (bv->lockInset(this))
2309 text_.setSelectionRange(str.length());
2310 updateLocal(bv, SELECTION, false);
2312 return (result != lyxfind::SR_NOT_FOUND);
2316 bool InsetText::checkInsertChar(LyXFont & font)
2319 return owner()->checkInsertChar(font);
2324 void InsetText::collapseParagraphs(BufferView * bv)
2326 while (paragraphs.size() > 1) {
2327 ParagraphList::iterator first_par = paragraphs.begin();
2328 ParagraphList::iterator next_par = boost::next(first_par);
2329 size_t const first_par_size = first_par->size();
2331 if (!first_par->empty() &&
2332 !next_par->empty() &&
2333 !first_par->isSeparator(first_par_size - 1)) {
2334 first_par->insertChar(first_par_size, ' ');
2337 if (text_.selection.set()) {
2338 if (text_.selection.start.par() == next_par) {
2339 text_.selection.start.par(first_par);
2340 text_.selection.start.pos(
2341 text_.selection.start.pos() + first_par_size);
2343 if (text_.selection.end.par() == next_par) {
2344 text_.selection.end.par(first_par);
2345 text_.selection.end.pos(
2346 text_.selection.end.pos() + first_par_size);
2350 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2356 void InsetText::getDrawFont(LyXFont & font) const
2360 owner()->getDrawFont(font);
2364 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2366 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2367 // And it probably does. You have to take a look at this John. (Lgb)
2368 #warning John, have a look here. (Lgb)
2369 ParagraphList::iterator pit = plist.begin();
2370 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2372 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2374 ParagraphList::iterator pend = plist.end();
2375 for (; pit != pend; ++pit) {
2376 paragraphs.push_back(*pit);
2383 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2385 ParagraphList::const_iterator pit = paragraphs.begin();
2386 ParagraphList::const_iterator pend = paragraphs.end();
2388 for (; pit != pend; ++pit) {
2389 InsetList::const_iterator it = pit->insetlist.begin();
2390 InsetList::const_iterator end = pit->insetlist.end();
2391 for (; it != end; ++it) {
2392 it->inset->addPreview(loader);