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;
281 dim.asc = text_.rows().begin()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
282 dim.des = text_.height - dim.asc + TEXT_TO_INSET_OFFSET;
283 dim.wid = max(textWidth(bv), int(text_.width)) + 2 * TEXT_TO_INSET_OFFSET;
284 dim.wid = max(dim.wid, 10);
289 int InsetText::textWidth(BufferView * bv, bool fordraw) const
291 int w = autoBreakRows ? getMaxWidth(bv, this) : -1;
294 return max(w - 2 * TEXT_TO_INSET_OFFSET,
295 (int)getLyXText(bv)->width);
300 return w - 2 * TEXT_TO_INSET_OFFSET;
304 void InsetText::draw(PainterInfo & pi, int x, int baseline) const
309 // update our idea of where we are. Clearly, we should
310 // not have to know this information.
314 int const start_x = x;
316 BufferView * bv = pi.base.bv;
317 Painter & pain = pi.pain;
319 // repaint the background if needed
320 if (backgroundColor() != LColor::background)
321 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
323 // no draw is necessary !!!
324 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
325 top_baseline = baseline;
333 top_baseline = baseline;
334 top_y = baseline - dim_.asc;
336 if (last_drawn_width != dim_.wid) {
338 last_drawn_width = dim_.wid;
341 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
342 inset_x = cix(bv) - int(x) + drawTextXOffset;
343 inset_y = ciy() + drawTextYOffset;
346 x += TEXT_TO_INSET_OFFSET;
348 RowList::iterator rowit = text_.rows().begin();
349 RowList::iterator end = text_.rows().end();
351 int y_offset = baseline - rowit->ascent_of_text();
352 int ph = pain.paperHeight();
355 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
356 y += rowit->height();
357 first += rowit->height();
361 text_.top_y(-y_offset);
369 int yf = y_offset + first;
374 while (rowit != end && yf < ph) {
375 paintRows(*bv, text_, rowit,
376 y + y_offset + first, int(x), y + text_.top_y());
377 y += rowit->height();
378 yf += rowit->height();
384 if ((drawFrame_ == ALWAYS) || (drawFrame_ == LOCKED && locked)) {
385 drawFrame(pain, int(start_x));
388 if (need_update != INIT) {
394 void InsetText::drawFrame(Painter & pain, int x) const
396 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
398 frame_y = top_baseline - dim_.asc + ttoD2;
399 frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
400 frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
401 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
406 void InsetText::update(BufferView * bv, bool reinit)
412 void InsetText::setUpdateStatus(int what) const
415 // we will to redraw us full if our LyXText wants it
416 if (text_.needRefresh())
419 // this to not draw a selection when we redraw all of it!
420 if (need_update & CURSOR && !(need_update & SELECTION)) {
421 if (text_.selection.set())
423 text_.clearSelection();
428 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
433 if (!autoBreakRows && paragraphs.size() > 1)
434 collapseParagraphs(bv);
436 text_.partialRebreak();
437 setUpdateStatus(what);
438 bool flag = mark_dirty ||
439 ((need_update != CURSOR && need_update != NONE) ||
440 text_.needRefresh() || text_.selection.set());
441 if (!text_.selection.set())
442 text_.selection.cursor = text_.cursor;
448 bv->updateInset(const_cast<InsetText *>(this));
451 if (need_update == CURSOR)
453 bv->owner()->view_state_changed();
454 bv->owner()->updateMenubar();
455 bv->owner()->updateToolbar();
456 if (old_par != cpar()) {
457 bv->owner()->setLayout(cpar()->layout()->name());
463 string const InsetText::editMessage() const
465 return _("Opened Text Inset");
469 void InsetText::insetUnlock(BufferView * bv)
471 if (the_locking_inset) {
472 the_locking_inset->insetUnlock(bv);
473 the_locking_inset = 0;
474 updateLocal(bv, CURSOR_PAR, false);
480 if (text_.selection.set()) {
481 text_.clearSelection();
483 } else if (owner()) {
484 bv->owner()->setLayout(owner()->getLyXText(bv)
485 ->cursor.par()->layout()->name());
487 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
488 // hack for deleteEmptyParMech
489 ParagraphList::iterator first_par = paragraphs.begin();
490 if (!first_par->empty()) {
491 text_.setCursor(first_par, 0);
492 } else if (paragraphs.size() > 1) {
493 text_.setCursor(boost::next(first_par), 0);
496 updateLocal(bv, code, false);
499 setUpdateStatus(code);
504 void InsetText::lockInset(BufferView * bv)
507 the_locking_inset = 0;
508 inset_pos = inset_x = inset_y = 0;
509 inset_boundary = false;
510 inset_par = paragraphs.end();
511 old_par = paragraphs.end();
512 text_.setCursorIntern(paragraphs.begin(), 0);
513 text_.clearSelection();
515 // If the inset is empty set the language of the current font to the
516 // language to the surronding text (if different).
517 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
518 bv->getParentLanguage(this) != text_.current_font.language()) {
519 LyXFont font(LyXFont::ALL_IGNORE);
520 font.setLanguage(bv->getParentLanguage(this));
521 setFont(bv, font, false);
524 if (drawFrame_ == LOCKED)
525 code = CURSOR|DRAW_FRAME;
526 setUpdateStatus(code);
530 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
532 the_locking_inset = inset;
533 inset_x = cix(bv) - top_x + drawTextXOffset;
534 inset_y = ciy() + drawTextYOffset;
537 inset_boundary = cboundary();
538 //updateLocal(bv, CURSOR, false);
542 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
544 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
548 if (!the_locking_inset) {
549 ParagraphList::iterator pit = paragraphs.begin();
550 ParagraphList::iterator pend = paragraphs.end();
552 int const id = inset->id();
553 for (; pit != pend; ++pit) {
554 InsetList::iterator it = pit->insetlist.begin();
555 InsetList::iterator const end = pit->insetlist.end();
556 for (; it != end; ++it) {
557 if (it->inset == inset) {
558 lyxerr << "InsetText::lockInsetInInset: 1 a\n";
559 text_.setCursorIntern(pit, it->pos);
560 lyxerr << "InsetText::lockInsetInInset: 1 b\n";
561 lyxerr << "bv: " << bv << " inset: " << inset << "\n";
562 lockInset(bv, inset);
563 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
566 if (it->inset->getInsetFromID(id)) {
567 lyxerr << "InsetText::lockInsetInInset: 2\n";
568 text_.setCursorIntern(pit, it->pos);
569 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
570 return the_locking_inset->lockInsetInInset(bv, inset);
574 lyxerr << "InsetText::lockInsetInInset: 3\n";
577 if (inset == cpar()->getInset(cpos())) {
578 lyxerr[Debug::INSETS] << "OK" << endl;
579 lockInset(bv, inset);
583 if (the_locking_inset && the_locking_inset == inset) {
584 if (cpar() == inset_par && cpos() == inset_pos) {
585 lyxerr[Debug::INSETS] << "OK" << endl;
586 inset_x = cix(bv) - top_x + drawTextXOffset;
587 inset_y = ciy() + drawTextYOffset;
589 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
591 } else if (the_locking_inset) {
592 lyxerr[Debug::INSETS] << "MAYBE" << endl;
593 return the_locking_inset->lockInsetInInset(bv, inset);
595 lyxerr[Debug::INSETS] << "NOT OK" << endl;
600 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
603 if (!the_locking_inset)
605 if (the_locking_inset == inset) {
606 the_locking_inset->insetUnlock(bv);
607 getLyXText(bv)->updateInset(inset);
608 the_locking_inset = 0;
610 moveRightIntern(bv, true, false);
611 old_par = paragraphs.end(); // force layout setting
615 updateLocal(bv, CURSOR, false);
618 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
622 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
624 if (!autoBreakRows && paragraphs.size() > 1)
625 collapseParagraphs(bv);
630 if (inset->owner() != this) {
631 int ustat = CURSOR_PAR;
633 UpdatableInset * tl_inset = the_locking_inset;
635 found = tl_inset->updateInsetInInset(bv, inset);
637 tl_inset = static_cast<UpdatableInset *>(inset);
638 while(tl_inset->owner() && tl_inset->owner() != this)
639 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
640 if (!tl_inset->owner())
642 found = tl_inset->updateInsetInInset(bv, inset);
646 text_.updateInset(tl_inset);
648 setUpdateStatus(ustat);
651 bool found = text_.updateInset(inset);
653 setUpdateStatus(CURSOR_PAR);
654 if (the_locking_inset &&
655 cpar() == inset_par && cpos() == inset_pos)
657 inset_x = cix(bv) - top_x + drawTextXOffset;
658 inset_y = ciy() + drawTextYOffset;
665 void InsetText::lfunMousePress(FuncRequest const & cmd)
669 // use this to check mouse motion for selection!
673 BufferView * bv = cmd.view();
674 FuncRequest cmd1 = cmd;
680 int tmp_x = cmd.x - drawTextXOffset;
681 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
682 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
684 if (the_locking_inset) {
685 if (the_locking_inset == inset) {
686 the_locking_inset->localDispatch(cmd1);
689 // otherwise only unlock the_locking_inset
690 the_locking_inset->insetUnlock(bv);
691 the_locking_inset = 0;
694 no_selection = false;
696 if (bv->theLockingInset()) {
697 if (isHighlyEditableInset(inset)) {
698 // We just have to lock the inset before calling a
700 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
701 if (!bv->lockInset(uinset)) {
702 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
704 inset->localDispatch(cmd1);
705 if (the_locking_inset)
706 updateLocal(bv, CURSOR, false);
711 bool paste_internally = false;
712 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
713 localDispatch(FuncRequest(bv, LFUN_COPY));
714 paste_internally = true;
716 int old_top_y = text_.top_y();
718 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
720 // set the selection cursor!
721 text_.selection.cursor = text_.cursor;
722 text_.cursor.x_fix(text_.cursor.x());
724 if (text_.selection.set()) {
725 text_.clearSelection();
726 updateLocal(bv, FULL, false);
728 text_.clearSelection();
729 updateLocal(bv, CURSOR, false);
732 bv->owner()->setLayout(cpar()->layout()->name());
734 // we moved the view we cannot do mouse selection in this case!
735 if (getLyXText(bv)->top_y() != old_top_y)
738 // Insert primary selection with middle mouse
739 // if there is a local selection in the current buffer,
741 if (cmd.button() == mouse_button::button2) {
742 if (paste_internally)
743 localDispatch(FuncRequest(bv, LFUN_PASTE));
745 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
748 getLyXText(bv)->clearSelection();
753 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
755 BufferView * bv = cmd.view();
756 FuncRequest cmd1 = cmd;
761 if (the_locking_inset)
762 return the_locking_inset->localDispatch(cmd1);
764 int tmp_x = cmd.x - drawTextXOffset;
765 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
766 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
769 // This code should probably be removed now. Simple insets
770 // (!highlyEditable) can actually take the localDispatch,
771 // and turn it into edit() if necessary. But we still
772 // need to deal properly with the whole relative vs.
773 // absolute mouse co-ords thing in a realiable, sensible way
775 if (isHighlyEditableInset(inset))
776 ret = inset->localDispatch(cmd1);
778 inset_x = cix(bv) - top_x + drawTextXOffset;
779 inset_y = ciy() + drawTextYOffset;
780 cmd1.x = cmd.x - inset_x;
781 cmd1.y = cmd.x - inset_y;
782 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
786 ret = inset->localDispatch(cmd1);
787 updateLocal(bv, CURSOR_PAR, false);
794 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
796 FuncRequest cmd1 = cmd;
800 if (the_locking_inset) {
801 the_locking_inset->localDispatch(cmd1);
805 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
808 BufferView * bv = cmd.view();
809 LyXCursor cur = text_.cursor;
810 text_.setCursorFromCoordinates
811 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
812 text_.cursor.x_fix(text_.cursor.x());
813 if (cur == text_.cursor)
815 text_.setSelection();
816 bool flag = (text_.toggle_cursor.par() != text_.toggle_end_cursor.par() ||
817 text_.toggle_cursor.pos() != text_.toggle_end_cursor.pos());
819 updateLocal(bv, SELECTION, false);
824 Inset::RESULT InsetText::localDispatch(FuncRequest const & cmd)
826 BufferView * bv = cmd.view();
829 if (cmd.action == LFUN_INSET_EDIT) {
830 UpdatableInset::localDispatch(cmd);
832 if (!bv->lockInset(this)) {
833 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
838 the_locking_inset = 0;
839 inset_pos = inset_x = inset_y = 0;
840 inset_boundary = false;
841 inset_par = paragraphs.end();
842 old_par = paragraphs.end();
845 if (cmd.argument.size()) {
846 if (cmd.argument == "left")
847 text_.setCursorIntern(paragraphs.begin(), 0);
849 ParagraphList::iterator it = paragraphs.begin();
850 ParagraphList::iterator end = paragraphs.end();
851 while (boost::next(it) != end)
853 // int const pos = (p->size() ? p->size()-1 : p->size());
854 text_.setCursor(it, it->size());
857 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
858 // we put here -1 and not button as now the button in the
859 // edit call should not be needed we will fix this in 1.3.x
860 // cycle hopefully (Jug 20020509)
861 // FIXME: GUII I've changed this to none: probably WRONG
862 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
863 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
865 text_.cursor.x_fix(text_.cursor.x());
869 text_.clearSelection();
872 // If the inset is empty set the language of the current font to the
873 // language to the surronding text (if different).
874 if (paragraphs.begin()->empty() &&
875 paragraphs.size() == 1 &&
876 bv->getParentLanguage(this) != text_.current_font.language())
878 LyXFont font(LyXFont::ALL_IGNORE);
879 font.setLanguage(bv->getParentLanguage(this));
880 setFont(bv, font, false);
884 if (drawFrame_ == LOCKED)
885 code = CURSOR | DRAW_FRAME;
887 updateLocal(bv, code, false);
888 // Tell the paragraph dialog that we've entered an insettext.
889 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
894 switch (cmd.action) {
895 case LFUN_MOUSE_PRESS:
898 case LFUN_MOUSE_MOTION:
899 lfunMouseMotion(cmd);
901 case LFUN_MOUSE_RELEASE:
902 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
907 bool was_empty = (paragraphs.begin()->empty() &&
908 paragraphs.size() == 1);
910 no_selection = false;
911 RESULT result = UpdatableInset::localDispatch(cmd);
912 if (result != UNDISPATCHED)
916 if (cmd.action < 0 && cmd.argument.empty())
919 if (the_locking_inset) {
920 result = the_locking_inset->localDispatch(cmd);
921 if (result == DISPATCHED_NOUPDATE)
923 else if (result == DISPATCHED) {
924 updateLocal(bv, CURSOR_PAR, false);
926 } else if (result >= FINISHED) {
929 moveRightIntern(bv, false, false);
933 if ((result = moveUp(bv)) >= FINISHED) {
934 updateLocal(bv, CURSOR, false);
935 bv->unlockInset(this);
939 if ((result = moveDown(bv)) >= FINISHED) {
940 updateLocal(bv, CURSOR, false);
941 bv->unlockInset(this);
948 the_locking_inset = 0;
949 updateLocal(bv, CURSOR, false);
950 // make sure status gets reset immediately
951 bv->owner()->clearMessage();
958 // what type of update to do on a cursor movement
959 int cursor_update = CURSOR;
961 if (text_.selection.set())
962 cursor_update = SELECTION;
964 switch (cmd.action) {
967 case LFUN_SELFINSERT:
968 if (bv->buffer()->isReadonly()) {
969 // setErrorMessage(N_("Document is read only"));
972 if (!cmd.argument.empty()) {
973 /* Automatically delete the currently selected
974 * text and replace it with what is being
975 * typed in now. Depends on lyxrc settings
976 * "auto_region_delete", which defaults to
979 // This should not be needed here and is also WRONG!
980 setUndo(bv, Undo::INSERT, text_.cursor.par());
983 if (lyxrc.auto_region_delete) {
984 if (text_.selection.set()) {
985 text_.cutSelection(false, false);
988 text_.clearSelection();
989 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
990 bv->owner()->getIntl().getTransManager().
991 TranslateAndInsert(cmd.argument[i], &text_);
994 text_.selection.cursor = text_.cursor;
995 updwhat = CURSOR | CURSOR_PAR;
997 result = DISPATCHED_NOUPDATE;
1000 // cursor movements that need special handling
1003 result = moveRight(bv);
1005 updwhat = cursor_update;
1009 result = moveLeft(bv);
1010 updwhat = cursor_update;
1014 result = moveDown(bv);
1015 updwhat = cursor_update;
1019 result = moveUp(bv);
1020 updwhat = cursor_update;
1024 if (crow() == text_.rows().begin())
1025 result = FINISHED_UP;
1027 text_.cursorPrevious();
1028 text_.clearSelection();
1029 result = DISPATCHED_NOUPDATE;
1031 updwhat = cursor_update;
1035 if (boost::next(crow()) == text_.rows().end())
1036 result = FINISHED_DOWN;
1039 text_.clearSelection();
1040 result = DISPATCHED_NOUPDATE;
1042 updwhat = cursor_update;
1045 case LFUN_BACKSPACE: {
1046 if (text_.selection.set())
1047 text_.cutSelection(true, false);
1050 updwhat = CURSOR_PAR;
1056 if (text_.selection.set()) {
1057 text_.cutSelection(true, false);
1061 updwhat = CURSOR_PAR;
1067 text_.cutSelection(true, true);
1068 updwhat = CURSOR_PAR;
1075 text_.copySelection();
1076 updwhat = CURSOR_PAR;
1079 case LFUN_PASTESELECTION:
1081 string const clip(bv->getClipboard());
1085 if (cmd.argument == "paragraph") {
1086 text_.insertStringAsParagraphs(clip);
1088 text_.insertStringAsLines(clip);
1091 text_.clearSelection();
1093 updwhat = CURSOR_PAR;
1099 if (!autoBreakRows) {
1100 if (CutAndPaste::nrOfParagraphs() > 1) {
1101 #ifdef WITH_WARNINGS
1102 #warning FIXME horrendously bad UI
1104 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1109 replaceSelection(bv->getLyXText());
1110 size_t sel_index = 0;
1111 string const & arg = cmd.argument;
1112 if (isStrUnsignedInt(arg)) {
1113 size_t const paste_arg = strToUnsignedInt(arg);
1114 #warning FIXME Check if the arg is in the domain of available selections.
1115 sel_index = paste_arg;
1117 text_.pasteSelection(sel_index);
1119 text_.clearSelection();
1120 updwhat = CURSOR_PAR;
1125 case LFUN_BREAKPARAGRAPH:
1126 if (!autoBreakRows) {
1127 result = DISPATCHED;
1130 replaceSelection(bv->getLyXText());
1131 text_.breakParagraph(paragraphs, 0);
1132 updwhat = CURSOR | FULL;
1136 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1137 if (!autoBreakRows) {
1138 result = DISPATCHED;
1141 replaceSelection(bv->getLyXText());
1142 text_.breakParagraph(paragraphs, 1);
1143 updwhat = CURSOR | FULL;
1147 case LFUN_BREAKLINE: {
1148 if (!autoBreakRows) {
1149 result = DISPATCHED;
1153 replaceSelection(bv->getLyXText());
1154 text_.insertInset(new InsetNewline);
1155 updwhat = CURSOR | CURSOR_PAR;
1161 // do not set layouts on non breakable textinsets
1162 if (autoBreakRows) {
1163 string cur_layout = cpar()->layout()->name();
1165 // Derive layout number from given argument (string)
1166 // and current buffer's textclass (number). */
1167 LyXTextClass const & tclass =
1168 bv->buffer()->params.getLyXTextClass();
1169 string layout = cmd.argument;
1170 bool hasLayout = tclass.hasLayout(layout);
1172 // If the entry is obsolete, use the new one instead.
1174 string const & obs =
1175 tclass[layout]->obsoleted_by();
1180 // see if we found the layout number:
1182 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1183 bv->owner()->dispatch(lf);
1187 if (cur_layout != layout) {
1188 cur_layout = layout;
1189 text_.setLayout(layout);
1190 bv->owner()->setLayout(cpar()->layout()->name());
1191 updwhat = CURSOR_PAR;
1195 // reset the layout box
1196 bv->owner()->setLayout(cpar()->layout()->name());
1199 case LFUN_PARAGRAPH_SPACING:
1200 // This one is absolutely not working. When fiddling with this
1201 // it also seems to me that the paragraphs inside the insettext
1202 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1203 // FIXME: how old is this comment ? ...
1205 ParagraphList::iterator pit = text_.cursor.par();
1206 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1207 float cur_value = 1.0;
1208 if (cur_spacing == Spacing::Other) {
1209 cur_value = pit->params().spacing().getValue();
1212 istringstream istr(STRCONV(cmd.argument));
1215 Spacing::Space new_spacing = cur_spacing;
1216 float new_value = cur_value;
1218 lyxerr << "Missing argument to `paragraph-spacing'"
1220 } else if (tmp == "single") {
1221 new_spacing = Spacing::Single;
1222 } else if (tmp == "onehalf") {
1223 new_spacing = Spacing::Onehalf;
1224 } else if (tmp == "double") {
1225 new_spacing = Spacing::Double;
1226 } else if (tmp == "other") {
1227 new_spacing = Spacing::Other;
1230 lyxerr << "new_value = " << tmpval << endl;
1233 } else if (tmp == "default") {
1234 new_spacing = Spacing::Default;
1236 lyxerr << _("Unknown spacing argument: ")
1237 << cmd.argument << endl;
1239 if (cur_spacing != new_spacing || cur_value != new_value) {
1240 pit->params().spacing(Spacing(new_spacing, new_value));
1241 updwhat = CURSOR_PAR;
1247 // These need to do repaints but don't require
1248 // special handling otherwise. A *lot* of the
1249 // above could probably be done similarly ...
1254 case LFUN_WORDRIGHT:
1255 // these two are really unhandled ...
1257 case LFUN_BEGINNINGBUF:
1258 updwhat = cursor_update;
1259 if (!bv->dispatch(cmd))
1260 result = UNDISPATCHED;
1269 case LFUN_WORDLEFTSEL:
1270 case LFUN_WORDRIGHTSEL:
1271 updwhat = SELECTION;
1276 if (!bv->dispatch(cmd))
1277 result = UNDISPATCHED;
1282 updateLocal(bv, updwhat, updflag);
1283 /// If the action has deleted all text in the inset, we need to change the
1284 // language to the language of the surronding text.
1285 if (!was_empty && paragraphs.begin()->empty() &&
1286 paragraphs.size() == 1) {
1287 LyXFont font(LyXFont::ALL_IGNORE);
1288 font.setLanguage(bv->getParentLanguage(this));
1289 setFont(bv, font, false);
1292 if (result >= FINISHED)
1293 bv->unlockInset(this);
1295 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1296 result = DISPATCHED;
1301 int InsetText::latex(Buffer const * buf, ostream & os,
1302 LatexRunParams const & runparams) const
1305 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1306 return texrow.rows();
1310 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1312 unsigned int lines = 0;
1314 ParagraphList::const_iterator beg = paragraphs.begin();
1315 ParagraphList::const_iterator end = paragraphs.end();
1316 ParagraphList::const_iterator it = beg;
1317 for (; it != end; ++it) {
1318 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1319 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1326 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1328 unsigned int lines = 0;
1330 vector<string> environment_stack(10);
1331 vector<string> environment_inner(10);
1333 int const command_depth = 0;
1336 Paragraph::depth_type depth = 0; // paragraph depth
1338 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1339 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1341 for (; pit != pend; ++pit) {
1343 int desc_on = 0; // description mode
1345 LyXLayout_ptr const & style = pit->layout();
1347 // environment tag closing
1348 for (; depth > pit->params().depth(); --depth) {
1349 if (environment_inner[depth] != "!-- --") {
1350 item_name = "listitem";
1351 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1352 if (environment_inner[depth] == "varlistentry")
1353 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1355 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1356 environment_stack[depth].erase();
1357 environment_inner[depth].erase();
1360 if (depth == pit->params().depth()
1361 && environment_stack[depth] != style->latexname()
1362 && !environment_stack[depth].empty()) {
1363 if (environment_inner[depth] != "!-- --") {
1364 item_name= "listitem";
1365 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1366 if (environment_inner[depth] == "varlistentry")
1367 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1370 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1372 environment_stack[depth].erase();
1373 environment_inner[depth].erase();
1376 // Write opening SGML tags.
1377 switch (style->latextype) {
1378 case LATEX_PARAGRAPH:
1379 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1383 buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1387 case LATEX_ENVIRONMENT:
1388 case LATEX_ITEM_ENVIRONMENT:
1389 if (depth < pit->params().depth()) {
1390 depth = pit->params().depth();
1391 environment_stack[depth].erase();
1394 if (environment_stack[depth] != style->latexname()) {
1395 if (environment_stack.size() == depth + 1) {
1396 environment_stack.push_back("!-- --");
1397 environment_inner.push_back("!-- --");
1399 environment_stack[depth] = style->latexname();
1400 environment_inner[depth] = "!-- --";
1401 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
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]);
1411 if (style->latextype == LATEX_ENVIRONMENT) {
1412 if (!style->latexparam().empty()) {
1413 if (style->latexparam() == "CDATA")
1416 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1421 desc_on = (style->labeltype == LABEL_MANUAL);
1423 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1424 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1426 item_name = desc_on ? "term" : "para";
1427 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1431 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1435 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1438 // write closing SGML tags
1439 switch (style->latextype) {
1440 case LATEX_ENVIRONMENT:
1441 if (!style->latexparam().empty()) {
1442 if (style->latexparam() == "CDATA")
1445 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1448 case LATEX_ITEM_ENVIRONMENT:
1449 if (desc_on == 1) break;
1451 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1453 case LATEX_PARAGRAPH:
1454 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1457 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1463 for (int d = depth; d >= 0; --d) {
1464 if (!environment_stack[depth].empty()) {
1465 if (environment_inner[depth] != "!-- --") {
1466 item_name = "listitem";
1467 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1468 if (environment_inner[depth] == "varlistentry")
1469 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1472 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1480 void InsetText::validate(LaTeXFeatures & features) const
1482 for_each(paragraphs.begin(), paragraphs.end(),
1483 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1487 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1489 if (the_locking_inset) {
1490 the_locking_inset->getCursor(bv, x, y);
1494 y = cy() + InsetText::y();
1498 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1500 if (the_locking_inset) {
1501 the_locking_inset->getCursorPos(bv, x, y);
1504 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1505 y = cy() - TEXT_TO_INSET_OFFSET;
1509 int InsetText::insetInInsetY() const
1511 if (!the_locking_inset)
1514 return (inset_y + the_locking_inset->insetInInsetY());
1518 void InsetText::fitInsetCursor(BufferView * bv) const
1520 if (the_locking_inset) {
1521 the_locking_inset->fitInsetCursor(bv);
1524 LyXFont const font =
1525 getLyXText(bv)->getFont(bv->buffer(), cpar(), cpos());
1527 int const asc = font_metrics::maxAscent(font);
1528 int const desc = font_metrics::maxDescent(font);
1530 if (bv->fitLockedInsetCursor(cx(bv), cy(), asc, desc))
1531 need_update |= FULL;
1536 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1538 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1539 return moveLeftIntern(bv, false, activate_inset, selecting);
1541 return moveRightIntern(bv, true, activate_inset, selecting);
1546 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1548 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1549 return moveRightIntern(bv, true, activate_inset, selecting);
1551 return moveLeftIntern(bv, false, activate_inset, selecting);
1556 InsetText::moveRightIntern(BufferView * bv, bool front,
1557 bool activate_inset, bool selecting)
1559 ParagraphList::iterator c_par = cpar();
1561 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1562 return FINISHED_RIGHT;
1563 if (activate_inset && checkAndActivateInset(bv, front))
1565 text_.cursorRight(bv);
1567 text_.clearSelection();
1568 return DISPATCHED_NOUPDATE;
1573 InsetText::moveLeftIntern(BufferView * bv, bool front,
1574 bool activate_inset, bool selecting)
1576 if (cpar() == paragraphs.begin() && cpos() <= 0)
1578 text_.cursorLeft(bv);
1580 text_.clearSelection();
1581 if (activate_inset && checkAndActivateInset(bv, front))
1583 return DISPATCHED_NOUPDATE;
1587 Inset::RESULT InsetText::moveUp(BufferView * bv)
1589 if (crow() == text_.rows().begin())
1592 text_.clearSelection();
1593 return DISPATCHED_NOUPDATE;
1597 Inset::RESULT InsetText::moveDown(BufferView * bv)
1599 if (boost::next(crow()) == text_.rows().end())
1600 return FINISHED_DOWN;
1601 text_.cursorDown(bv);
1602 text_.clearSelection();
1603 return DISPATCHED_NOUPDATE;
1607 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1609 if (the_locking_inset) {
1610 if (the_locking_inset->insetAllowed(inset))
1611 return the_locking_inset->insertInset(bv, inset);
1614 inset->setOwner(this);
1615 getLyXText(bv)->insertInset(inset);
1617 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1622 bool InsetText::insetAllowed(Inset::Code code) const
1624 // in_insetAllowed is a really gross hack,
1625 // to allow us to call the owner's insetAllowed
1626 // without stack overflow, which can happen
1627 // when the owner uses InsetCollapsable::insetAllowed()
1629 if (in_insetAllowed)
1631 in_insetAllowed = true;
1632 if (the_locking_inset)
1633 ret = the_locking_inset->insetAllowed(code);
1635 ret = owner()->insetAllowed(code);
1636 in_insetAllowed = false;
1641 UpdatableInset * InsetText::getLockingInset() const
1643 return the_locking_inset ? the_locking_inset->getLockingInset() :
1644 const_cast<InsetText *>(this);
1648 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1652 if (the_locking_inset)
1653 return the_locking_inset->getFirstLockingInsetOfType(c);
1658 bool InsetText::showInsetDialog(BufferView * bv) const
1660 if (the_locking_inset)
1661 return the_locking_inset->showInsetDialog(bv);
1666 void InsetText::getLabelList(std::vector<string> & list) const
1668 ParagraphList::const_iterator pit = paragraphs.begin();
1669 ParagraphList::const_iterator pend = paragraphs.end();
1670 for (; pit != pend; ++pit) {
1671 InsetList::const_iterator beg = pit->insetlist.begin();
1672 InsetList::const_iterator end = pit->insetlist.end();
1673 for (; beg != end; ++beg)
1674 beg->inset->getLabelList(list);
1679 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1682 if (the_locking_inset) {
1683 the_locking_inset->setFont(bv, font, toggleall, selectall);
1687 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1688 || cpar()->empty()) {
1689 getLyXText(bv)->setFont(font, toggleall);
1694 if (text_.selection.set())
1695 setUndo(bv, Undo::EDIT, text_.cursor.par());
1699 text_.selection.cursor = text_.cursor;
1700 text_.cursorBottom();
1701 text_.setSelection();
1704 text_.toggleFree(font, toggleall);
1707 text_.clearSelection();
1711 bool flag = (selectall || text_.selection.set());
1714 updateLocal(bv, FULL, true);
1716 updateLocal(bv, CURSOR_PAR, true);
1720 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1722 if (cpar()->isInset(cpos())) {
1724 static_cast<UpdatableInset*>(cpar()->getInset(cpos()));
1725 if (!isHighlyEditableInset(inset))
1727 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1728 inset->localDispatch(cmd);
1729 if (!the_locking_inset)
1731 updateLocal(bv, CURSOR, false);
1738 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1739 mouse_button::state button)
1741 x -= drawTextXOffset;
1743 int dummyy = y + dim_.asc;
1744 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1745 // we only do the edit() call if the inset was hit by the mouse
1746 // or if it is a highly editable inset. So we should call this
1747 // function from our own edit with button < 0.
1748 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1750 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1758 inset_x = cix(bv) - top_x + drawTextXOffset;
1759 inset_y = ciy() + drawTextYOffset;
1760 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1761 inset->localDispatch(cmd);
1762 if (!the_locking_inset)
1764 updateLocal(bv, CURSOR, false);
1771 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1774 int w = UpdatableInset::getMaxWidth(bv, inset);
1779 w = w - top_x + owner()->x();
1782 w -= (2 * TEXT_TO_INSET_OFFSET);
1785 return UpdatableInset::getMaxWidth(bv, inset);
1790 void InsetText::setParagraphData(ParagraphList const & plist)
1792 // we have to unlock any locked inset otherwise we're in troubles
1793 the_locking_inset = 0;
1795 // But it it makes no difference that is a lot better.
1797 // See if this can be simplified when std::list is in effect.
1800 ParagraphList::const_iterator it = plist.begin();
1801 ParagraphList::const_iterator end = plist.end();
1802 for (; it != end; ++it) {
1803 paragraphs.push_back(*it);
1804 Paragraph & tmp = paragraphs.back();
1805 tmp.setInsetOwner(this);
1813 void InsetText::markNew(bool track_changes)
1815 ParagraphList::iterator pit = paragraphs.begin();
1816 ParagraphList::iterator pend = paragraphs.end();
1817 for (; pit != pend; ++pit) {
1818 if (track_changes) {
1819 pit->trackChanges();
1821 // no-op when not tracking
1822 pit->cleanChanges();
1828 void InsetText::setText(string const & data, LyXFont const & font)
1831 for (unsigned int i = 0; i < data.length(); ++i)
1832 paragraphs.begin()->insertChar(i, data[i], font);
1837 void InsetText::setAutoBreakRows(bool flag)
1839 if (flag != autoBreakRows) {
1840 autoBreakRows = flag;
1848 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1850 if (how != drawFrame_) {
1853 updateLocal(bv, DRAW_FRAME, false);
1858 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1860 if (frame_color != col) {
1863 updateLocal(bv, DRAW_FRAME, false);
1868 int InsetText::cx(BufferView * bv) const
1870 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1871 if (the_locking_inset) {
1872 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1873 text_.cursor.pos());
1874 if (font.isVisibleRightToLeft())
1875 x -= the_locking_inset->width(bv, font);
1881 int InsetText::cix(BufferView * bv) const
1883 int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1884 if (the_locking_inset) {
1885 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1886 text_.cursor.pos());
1887 if (font.isVisibleRightToLeft())
1888 x -= the_locking_inset->width(bv, font);
1894 int InsetText::cy() const
1896 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1900 int InsetText::ciy() const
1902 return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1906 pos_type InsetText::cpos() const
1908 return text_.cursor.pos();
1912 ParagraphList::iterator InsetText::cpar() const
1914 return text_.cursor.par();
1918 bool InsetText::cboundary() const
1920 return text_.cursor.boundary();
1924 RowList::iterator InsetText::crow() const
1926 return text_.cursorRow();
1930 LyXText * InsetText::getLyXText(BufferView const * bv,
1931 bool const recursive) const
1934 if (recursive && the_locking_inset)
1935 return the_locking_inset->getLyXText(bv, true);
1940 void InsetText::setViewCache(BufferView const * bv) const
1943 text_.bv_owner = const_cast<BufferView *>(bv);
1947 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1950 /// then remove all LyXText in text-insets
1951 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1952 const_cast<ParagraphList&>(paragraphs).end(),
1953 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1958 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1960 if (paragraphs.size() == 1 && paragraphs.begin()->empty()) {
1961 // no data, resize not neccessary!
1962 // we have to do this as a fixed width may have changed!
1965 restoreLyXTextState();
1975 // one endless line, resize normally not necessary
1976 if (!force && getMaxWidth(bv, this) < 0)
1981 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1982 const_cast<ParagraphList&>(paragraphs).end(),
1983 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1986 restoreLyXTextState();
1988 if (the_locking_inset) {
1989 inset_x = cix(bv) - top_x + drawTextXOffset;
1990 inset_y = ciy() + drawTextYOffset;
1993 text_.top_y(bv->screen().topCursorVisible(&text_));
1995 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
1996 // this will scroll the screen such that the cursor becomes visible
1997 bv->updateScrollbar();
1999 need_update |= FULL;
2004 void InsetText::reinitLyXText() const
2006 BufferView * bv = text_.bv_owner;
2013 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2014 const_cast<ParagraphList&>(paragraphs).end(),
2015 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2018 restoreLyXTextState();
2019 if (the_locking_inset) {
2020 inset_x = cix(bv) - top_x + drawTextXOffset;
2021 inset_y = ciy() + drawTextYOffset;
2023 text_.top_y(bv->screen().topCursorVisible(&text_));
2025 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2026 // this will scroll the screen such that the cursor becomes visible
2027 bv->updateScrollbar();
2034 void InsetText::removeNewlines()
2036 bool changed = false;
2038 ParagraphList::iterator it = paragraphs.begin();
2039 ParagraphList::iterator end = paragraphs.end();
2040 for (; it != end; ++it) {
2041 for (int i = 0; i < it->size(); ++i) {
2042 if (it->isNewline(i)) {
2053 bool InsetText::nodraw() const
2055 if (the_locking_inset)
2056 return the_locking_inset->nodraw();
2057 return UpdatableInset::nodraw();
2061 int InsetText::scroll(bool recursive) const
2063 int sx = UpdatableInset::scroll(false);
2065 if (recursive && the_locking_inset)
2066 sx += the_locking_inset->scroll(recursive);
2072 void InsetText::clearSelection(BufferView * bv)
2074 getLyXText(bv)->clearSelection();
2078 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2080 Painter & pain = bv->painter();
2082 int h = dim_.asc + dim_.des;
2083 int ty = baseline - dim_.asc;
2089 if ((ty + h) > pain.paperHeight())
2090 h = pain.paperHeight();
2091 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2092 w = pain.paperWidth();
2093 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2098 ParagraphList * InsetText::getParagraphs(int i) const
2100 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2104 LyXCursor const & InsetText::cursor(BufferView * bv) const
2106 if (the_locking_inset)
2107 return the_locking_inset->cursor(bv);
2108 return getLyXText(bv)->cursor;
2112 Inset * InsetText::getInsetFromID(int id_arg) const
2115 return const_cast<InsetText *>(this);
2117 ParagraphList::const_iterator pit = paragraphs.begin();
2118 ParagraphList::const_iterator pend = paragraphs.end();
2119 for (; pit != pend; ++pit) {
2120 InsetList::const_iterator it = pit->insetlist.begin();
2121 InsetList::const_iterator end = pit->insetlist.end();
2122 for (; it != end; ++it) {
2123 if (it->inset->id() == id_arg)
2125 Inset * in = it->inset->getInsetFromID(id_arg);
2135 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2138 if (the_locking_inset) {
2139 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2140 if (!word.word().empty()) {
2144 // we have to go on checking so move cursor to the next char
2145 text_.cursor.pos(text_.cursor.pos() + 1);
2147 word = text_.selectNextWordToSpellcheck(value);
2148 if (word.word().empty())
2149 bv->unlockInset(const_cast<InsetText *>(this));
2156 void InsetText::selectSelectedWord(BufferView * bv)
2158 if (the_locking_inset) {
2159 the_locking_inset->selectSelectedWord(bv);
2162 getLyXText(bv)->selectSelectedWord();
2163 updateLocal(bv, SELECTION, false);
2167 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2169 if (the_locking_inset) {
2170 the_locking_inset->toggleSelection(bv, kill_selection);
2173 int x = top_x + TEXT_TO_INSET_OFFSET;
2175 RowList::iterator rowit = text_.rows().begin();
2176 RowList::iterator end = text_.rows().end();
2177 int y_offset = top_baseline - rowit->ascent_of_text();
2179 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2180 y += rowit->height();
2186 if (need_update & SELECTION)
2188 bv->screen().toggleSelection(&text_, bv, kill_selection, y_offset, x);
2192 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2194 if (the_locking_inset) {
2195 if (the_locking_inset->nextChange(bv, length))
2197 text_.cursorRight(true);
2199 lyxfind::SearchResult result =
2200 lyxfind::findNextChange(bv, &text_, length);
2202 if (result == lyxfind::SR_FOUND) {
2203 LyXCursor cur = text_.cursor;
2204 bv->unlockInset(bv->theLockingInset());
2205 if (bv->lockInset(this))
2208 text_.setSelectionRange(length);
2209 updateLocal(bv, SELECTION, false);
2211 return result != lyxfind::SR_NOT_FOUND;
2215 bool InsetText::searchForward(BufferView * bv, string const & str,
2218 if (the_locking_inset) {
2219 if (the_locking_inset->searchForward(bv, str, cs, mw))
2221 text_.cursorRight(true);
2223 lyxfind::SearchResult result =
2224 lyxfind::LyXFind(bv, &text_, str, true, cs, mw);
2226 if (result == lyxfind::SR_FOUND) {
2227 LyXCursor cur = text_.cursor;
2228 bv->unlockInset(bv->theLockingInset());
2229 if (bv->lockInset(this))
2232 text_.setSelectionRange(str.length());
2233 updateLocal(bv, SELECTION, false);
2235 return (result != lyxfind::SR_NOT_FOUND);
2238 bool InsetText::searchBackward(BufferView * bv, string const & str,
2241 if (the_locking_inset) {
2242 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2246 ParagraphList::iterator pit = paragraphs.begin();
2247 ParagraphList::iterator pend = paragraphs.end();
2249 while (boost::next(pit) != pend)
2252 text_.setCursor(pit, pit->size());
2254 lyxfind::SearchResult result =
2255 lyxfind::LyXFind(bv, &text_, str, false, cs, mw);
2257 if (result == lyxfind::SR_FOUND) {
2258 LyXCursor cur = text_.cursor;
2259 bv->unlockInset(bv->theLockingInset());
2260 if (bv->lockInset(this))
2263 text_.setSelectionRange(str.length());
2264 updateLocal(bv, SELECTION, false);
2266 return (result != lyxfind::SR_NOT_FOUND);
2270 bool InsetText::checkInsertChar(LyXFont & font)
2273 return owner()->checkInsertChar(font);
2278 void InsetText::collapseParagraphs(BufferView * bv)
2280 while (paragraphs.size() > 1) {
2281 ParagraphList::iterator first_par = paragraphs.begin();
2282 ParagraphList::iterator next_par = boost::next(first_par);
2283 size_t const first_par_size = first_par->size();
2285 if (!first_par->empty() &&
2286 !next_par->empty() &&
2287 !first_par->isSeparator(first_par_size - 1)) {
2288 first_par->insertChar(first_par_size, ' ');
2291 if (text_.selection.set()) {
2292 if (text_.selection.start.par() == next_par) {
2293 text_.selection.start.par(first_par);
2294 text_.selection.start.pos(
2295 text_.selection.start.pos() + first_par_size);
2297 if (text_.selection.end.par() == next_par) {
2298 text_.selection.end.par(first_par);
2299 text_.selection.end.pos(
2300 text_.selection.end.pos() + first_par_size);
2304 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2310 void InsetText::getDrawFont(LyXFont & font) const
2314 owner()->getDrawFont(font);
2318 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2320 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2321 // And it probably does. You have to take a look at this John. (Lgb)
2322 #warning John, have a look here. (Lgb)
2323 ParagraphList::iterator pit = plist.begin();
2324 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2326 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2328 ParagraphList::iterator pend = plist.end();
2329 for (; pit != pend; ++pit) {
2330 paragraphs.push_back(*pit);
2337 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2339 ParagraphList::const_iterator pit = paragraphs.begin();
2340 ParagraphList::const_iterator pend = paragraphs.end();
2342 for (; pit != pend; ++pit) {
2343 InsetList::const_iterator it = pit->insetlist.begin();
2344 InsetList::const_iterator end = pit->insetlist.end();
2345 for (; it != end; ++it) {
2346 it->inset->addPreview(loader);