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::setUpdateStatus(int what) const
409 // we will to redraw us full if our LyXText wants it
410 if (text_.needRefresh())
413 // this to not draw a selection when we redraw all of it!
414 if (need_update & CURSOR && !(need_update & SELECTION)) {
415 if (text_.selection.set())
417 text_.clearSelection();
422 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
427 if (!autoBreakRows && paragraphs.size() > 1)
428 collapseParagraphs(bv);
430 text_.partialRebreak();
431 setUpdateStatus(what);
432 bool flag = mark_dirty ||
433 ((need_update != CURSOR && need_update != NONE) ||
434 text_.needRefresh() || text_.selection.set());
435 if (!text_.selection.set())
436 text_.selection.cursor = text_.cursor;
442 bv->updateInset(const_cast<InsetText *>(this));
445 if (need_update == CURSOR)
447 bv->owner()->view_state_changed();
448 bv->owner()->updateMenubar();
449 bv->owner()->updateToolbar();
450 if (old_par != cpar()) {
451 bv->owner()->setLayout(cpar()->layout()->name());
457 string const InsetText::editMessage() const
459 return _("Opened Text Inset");
463 void InsetText::insetUnlock(BufferView * bv)
465 if (the_locking_inset) {
466 the_locking_inset->insetUnlock(bv);
467 the_locking_inset = 0;
468 updateLocal(bv, CURSOR_PAR, false);
474 if (text_.selection.set()) {
475 text_.clearSelection();
477 } else if (owner()) {
478 bv->owner()->setLayout(owner()->getLyXText(bv)
479 ->cursor.par()->layout()->name());
481 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
482 // hack for deleteEmptyParMech
483 ParagraphList::iterator first_par = paragraphs.begin();
484 if (!first_par->empty()) {
485 text_.setCursor(first_par, 0);
486 } else if (paragraphs.size() > 1) {
487 text_.setCursor(boost::next(first_par), 0);
490 updateLocal(bv, code, false);
493 setUpdateStatus(code);
498 void InsetText::lockInset(BufferView * bv)
501 the_locking_inset = 0;
502 inset_pos = inset_x = inset_y = 0;
503 inset_boundary = false;
504 inset_par = paragraphs.end();
505 old_par = paragraphs.end();
506 text_.setCursorIntern(paragraphs.begin(), 0);
507 text_.clearSelection();
509 // If the inset is empty set the language of the current font to the
510 // language to the surronding text (if different).
511 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
512 bv->getParentLanguage(this) != text_.current_font.language()) {
513 LyXFont font(LyXFont::ALL_IGNORE);
514 font.setLanguage(bv->getParentLanguage(this));
515 setFont(bv, font, false);
518 if (drawFrame_ == LOCKED)
519 code = CURSOR|DRAW_FRAME;
520 setUpdateStatus(code);
524 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
526 the_locking_inset = inset;
527 inset_x = cix(bv) - top_x + drawTextXOffset;
528 inset_y = ciy() + drawTextYOffset;
531 inset_boundary = cboundary();
532 //updateLocal(bv, CURSOR, false);
536 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
538 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
542 if (!the_locking_inset) {
543 ParagraphList::iterator pit = paragraphs.begin();
544 ParagraphList::iterator pend = paragraphs.end();
546 int const id = inset->id();
547 for (; pit != pend; ++pit) {
548 InsetList::iterator it = pit->insetlist.begin();
549 InsetList::iterator const end = pit->insetlist.end();
550 for (; it != end; ++it) {
551 if (it->inset == inset) {
552 lyxerr << "InsetText::lockInsetInInset: 1 a\n";
553 text_.setCursorIntern(pit, it->pos);
554 lyxerr << "InsetText::lockInsetInInset: 1 b\n";
555 lyxerr << "bv: " << bv << " inset: " << inset << "\n";
556 lockInset(bv, inset);
557 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
560 if (it->inset->getInsetFromID(id)) {
561 lyxerr << "InsetText::lockInsetInInset: 2\n";
562 text_.setCursorIntern(pit, it->pos);
563 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
564 return the_locking_inset->lockInsetInInset(bv, inset);
568 lyxerr << "InsetText::lockInsetInInset: 3\n";
571 if (inset == cpar()->getInset(cpos())) {
572 lyxerr[Debug::INSETS] << "OK" << endl;
573 lockInset(bv, inset);
577 if (the_locking_inset && the_locking_inset == inset) {
578 if (cpar() == inset_par && cpos() == inset_pos) {
579 lyxerr[Debug::INSETS] << "OK" << endl;
580 inset_x = cix(bv) - top_x + drawTextXOffset;
581 inset_y = ciy() + drawTextYOffset;
583 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
585 } else if (the_locking_inset) {
586 lyxerr[Debug::INSETS] << "MAYBE" << endl;
587 return the_locking_inset->lockInsetInInset(bv, inset);
589 lyxerr[Debug::INSETS] << "NOT OK" << endl;
594 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
597 if (!the_locking_inset)
599 if (the_locking_inset == inset) {
600 the_locking_inset->insetUnlock(bv);
601 getLyXText(bv)->updateInset(inset);
602 the_locking_inset = 0;
604 moveRightIntern(bv, true, false);
605 old_par = paragraphs.end(); // force layout setting
609 updateLocal(bv, CURSOR, false);
612 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
616 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
618 if (!autoBreakRows && paragraphs.size() > 1)
619 collapseParagraphs(bv);
624 if (inset->owner() != this) {
625 int ustat = CURSOR_PAR;
627 UpdatableInset * tl_inset = the_locking_inset;
629 found = tl_inset->updateInsetInInset(bv, inset);
631 tl_inset = static_cast<UpdatableInset *>(inset);
632 while(tl_inset->owner() && tl_inset->owner() != this)
633 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
634 if (!tl_inset->owner())
636 found = tl_inset->updateInsetInInset(bv, inset);
640 text_.updateInset(tl_inset);
642 setUpdateStatus(ustat);
645 bool found = text_.updateInset(inset);
647 setUpdateStatus(CURSOR_PAR);
648 if (the_locking_inset &&
649 cpar() == inset_par && cpos() == inset_pos)
651 inset_x = cix(bv) - top_x + drawTextXOffset;
652 inset_y = ciy() + drawTextYOffset;
659 void InsetText::lfunMousePress(FuncRequest const & cmd)
663 // use this to check mouse motion for selection!
667 BufferView * bv = cmd.view();
668 FuncRequest cmd1 = cmd;
674 int tmp_x = cmd.x - drawTextXOffset;
675 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
676 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
678 if (the_locking_inset) {
679 if (the_locking_inset == inset) {
680 the_locking_inset->localDispatch(cmd1);
683 // otherwise only unlock the_locking_inset
684 the_locking_inset->insetUnlock(bv);
685 the_locking_inset = 0;
688 no_selection = false;
690 if (bv->theLockingInset()) {
691 if (isHighlyEditableInset(inset)) {
692 // We just have to lock the inset before calling a
694 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
695 if (!bv->lockInset(uinset)) {
696 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
698 inset->localDispatch(cmd1);
699 if (the_locking_inset)
700 updateLocal(bv, CURSOR, false);
705 bool paste_internally = false;
706 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
707 localDispatch(FuncRequest(bv, LFUN_COPY));
708 paste_internally = true;
710 int old_top_y = text_.top_y();
712 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
714 // set the selection cursor!
715 text_.selection.cursor = text_.cursor;
716 text_.cursor.x_fix(text_.cursor.x());
718 if (text_.selection.set()) {
719 text_.clearSelection();
720 updateLocal(bv, FULL, false);
722 text_.clearSelection();
723 updateLocal(bv, CURSOR, false);
726 bv->owner()->setLayout(cpar()->layout()->name());
728 // we moved the view we cannot do mouse selection in this case!
729 if (getLyXText(bv)->top_y() != old_top_y)
732 // Insert primary selection with middle mouse
733 // if there is a local selection in the current buffer,
735 if (cmd.button() == mouse_button::button2) {
736 if (paste_internally)
737 localDispatch(FuncRequest(bv, LFUN_PASTE));
739 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
742 getLyXText(bv)->clearSelection();
747 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
749 BufferView * bv = cmd.view();
750 FuncRequest cmd1 = cmd;
755 if (the_locking_inset)
756 return the_locking_inset->localDispatch(cmd1);
758 int tmp_x = cmd.x - drawTextXOffset;
759 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
760 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
763 // This code should probably be removed now. Simple insets
764 // (!highlyEditable) can actually take the localDispatch,
765 // and turn it into edit() if necessary. But we still
766 // need to deal properly with the whole relative vs.
767 // absolute mouse co-ords thing in a realiable, sensible way
769 if (isHighlyEditableInset(inset))
770 ret = inset->localDispatch(cmd1);
772 inset_x = cix(bv) - top_x + drawTextXOffset;
773 inset_y = ciy() + drawTextYOffset;
774 cmd1.x = cmd.x - inset_x;
775 cmd1.y = cmd.x - inset_y;
776 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
780 ret = inset->localDispatch(cmd1);
781 updateLocal(bv, CURSOR_PAR, false);
788 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
790 FuncRequest cmd1 = cmd;
794 if (the_locking_inset) {
795 the_locking_inset->localDispatch(cmd1);
799 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
802 BufferView * bv = cmd.view();
803 LyXCursor cur = text_.cursor;
804 text_.setCursorFromCoordinates
805 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
806 text_.cursor.x_fix(text_.cursor.x());
807 if (cur == text_.cursor)
809 text_.setSelection();
810 bool flag = (text_.toggle_cursor.par() != text_.toggle_end_cursor.par() ||
811 text_.toggle_cursor.pos() != text_.toggle_end_cursor.pos());
813 updateLocal(bv, SELECTION, false);
818 Inset::RESULT InsetText::localDispatch(FuncRequest const & cmd)
820 BufferView * bv = cmd.view();
823 if (cmd.action == LFUN_INSET_EDIT) {
824 UpdatableInset::localDispatch(cmd);
826 if (!bv->lockInset(this)) {
827 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
832 the_locking_inset = 0;
833 inset_pos = inset_x = inset_y = 0;
834 inset_boundary = false;
835 inset_par = paragraphs.end();
836 old_par = paragraphs.end();
839 if (cmd.argument.size()) {
840 if (cmd.argument == "left")
841 text_.setCursorIntern(paragraphs.begin(), 0);
843 ParagraphList::iterator it = paragraphs.begin();
844 ParagraphList::iterator end = paragraphs.end();
845 while (boost::next(it) != end)
847 // int const pos = (p->size() ? p->size()-1 : p->size());
848 text_.setCursor(it, it->size());
851 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
852 // we put here -1 and not button as now the button in the
853 // edit call should not be needed we will fix this in 1.3.x
854 // cycle hopefully (Jug 20020509)
855 // FIXME: GUII I've changed this to none: probably WRONG
856 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
857 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
859 text_.cursor.x_fix(text_.cursor.x());
863 text_.clearSelection();
866 // If the inset is empty set the language of the current font to the
867 // language to the surronding text (if different).
868 if (paragraphs.begin()->empty() &&
869 paragraphs.size() == 1 &&
870 bv->getParentLanguage(this) != text_.current_font.language())
872 LyXFont font(LyXFont::ALL_IGNORE);
873 font.setLanguage(bv->getParentLanguage(this));
874 setFont(bv, font, false);
878 if (drawFrame_ == LOCKED)
879 code = CURSOR | DRAW_FRAME;
881 updateLocal(bv, code, false);
882 // Tell the paragraph dialog that we've entered an insettext.
883 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
888 switch (cmd.action) {
889 case LFUN_MOUSE_PRESS:
892 case LFUN_MOUSE_MOTION:
893 lfunMouseMotion(cmd);
895 case LFUN_MOUSE_RELEASE:
896 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
901 bool was_empty = (paragraphs.begin()->empty() &&
902 paragraphs.size() == 1);
904 no_selection = false;
905 RESULT result = UpdatableInset::localDispatch(cmd);
906 if (result != UNDISPATCHED)
910 if (cmd.action < 0 && cmd.argument.empty())
913 if (the_locking_inset) {
914 result = the_locking_inset->localDispatch(cmd);
915 if (result == DISPATCHED_NOUPDATE)
917 else if (result == DISPATCHED) {
918 updateLocal(bv, CURSOR_PAR, false);
920 } else if (result >= FINISHED) {
923 moveRightIntern(bv, false, false);
927 if ((result = moveUp(bv)) >= FINISHED) {
928 updateLocal(bv, CURSOR, false);
929 bv->unlockInset(this);
933 if ((result = moveDown(bv)) >= FINISHED) {
934 updateLocal(bv, CURSOR, false);
935 bv->unlockInset(this);
942 the_locking_inset = 0;
943 updateLocal(bv, CURSOR, false);
944 // make sure status gets reset immediately
945 bv->owner()->clearMessage();
952 // what type of update to do on a cursor movement
953 int cursor_update = CURSOR;
955 if (text_.selection.set())
956 cursor_update = SELECTION;
958 switch (cmd.action) {
961 case LFUN_SELFINSERT:
962 if (bv->buffer()->isReadonly()) {
963 // setErrorMessage(N_("Document is read only"));
966 if (!cmd.argument.empty()) {
967 /* Automatically delete the currently selected
968 * text and replace it with what is being
969 * typed in now. Depends on lyxrc settings
970 * "auto_region_delete", which defaults to
973 // This should not be needed here and is also WRONG!
974 setUndo(bv, Undo::INSERT, text_.cursor.par());
977 if (lyxrc.auto_region_delete) {
978 if (text_.selection.set()) {
979 text_.cutSelection(false, false);
982 text_.clearSelection();
983 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
984 bv->owner()->getIntl().getTransManager().
985 TranslateAndInsert(cmd.argument[i], &text_);
988 text_.selection.cursor = text_.cursor;
989 updwhat = CURSOR | CURSOR_PAR;
991 result = DISPATCHED_NOUPDATE;
994 // cursor movements that need special handling
997 result = moveRight(bv);
999 updwhat = cursor_update;
1003 result = moveLeft(bv);
1004 updwhat = cursor_update;
1008 result = moveDown(bv);
1009 updwhat = cursor_update;
1013 result = moveUp(bv);
1014 updwhat = cursor_update;
1018 if (crow() == text_.rows().begin())
1019 result = FINISHED_UP;
1021 text_.cursorPrevious();
1022 text_.clearSelection();
1023 result = DISPATCHED_NOUPDATE;
1025 updwhat = cursor_update;
1029 if (boost::next(crow()) == text_.rows().end())
1030 result = FINISHED_DOWN;
1033 text_.clearSelection();
1034 result = DISPATCHED_NOUPDATE;
1036 updwhat = cursor_update;
1039 case LFUN_BACKSPACE: {
1040 if (text_.selection.set())
1041 text_.cutSelection(true, false);
1044 updwhat = CURSOR_PAR;
1050 if (text_.selection.set()) {
1051 text_.cutSelection(true, false);
1055 updwhat = CURSOR_PAR;
1061 text_.cutSelection(true, true);
1062 updwhat = CURSOR_PAR;
1069 text_.copySelection();
1070 updwhat = CURSOR_PAR;
1073 case LFUN_PASTESELECTION:
1075 string const clip(bv->getClipboard());
1079 if (cmd.argument == "paragraph") {
1080 text_.insertStringAsParagraphs(clip);
1082 text_.insertStringAsLines(clip);
1085 text_.clearSelection();
1087 updwhat = CURSOR_PAR;
1093 if (!autoBreakRows) {
1094 if (CutAndPaste::nrOfParagraphs() > 1) {
1095 #ifdef WITH_WARNINGS
1096 #warning FIXME horrendously bad UI
1098 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1103 replaceSelection(bv->getLyXText());
1104 size_t sel_index = 0;
1105 string const & arg = cmd.argument;
1106 if (isStrUnsignedInt(arg)) {
1107 size_t const paste_arg = strToUnsignedInt(arg);
1108 #warning FIXME Check if the arg is in the domain of available selections.
1109 sel_index = paste_arg;
1111 text_.pasteSelection(sel_index);
1113 text_.clearSelection();
1114 updwhat = CURSOR_PAR;
1119 case LFUN_BREAKPARAGRAPH:
1120 if (!autoBreakRows) {
1121 result = DISPATCHED;
1124 replaceSelection(bv->getLyXText());
1125 text_.breakParagraph(paragraphs, 0);
1126 updwhat = CURSOR | FULL;
1130 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1131 if (!autoBreakRows) {
1132 result = DISPATCHED;
1135 replaceSelection(bv->getLyXText());
1136 text_.breakParagraph(paragraphs, 1);
1137 updwhat = CURSOR | FULL;
1141 case LFUN_BREAKLINE: {
1142 if (!autoBreakRows) {
1143 result = DISPATCHED;
1147 replaceSelection(bv->getLyXText());
1148 text_.insertInset(new InsetNewline);
1149 updwhat = CURSOR | CURSOR_PAR;
1155 // do not set layouts on non breakable textinsets
1156 if (autoBreakRows) {
1157 string cur_layout = cpar()->layout()->name();
1159 // Derive layout number from given argument (string)
1160 // and current buffer's textclass (number). */
1161 LyXTextClass const & tclass =
1162 bv->buffer()->params.getLyXTextClass();
1163 string layout = cmd.argument;
1164 bool hasLayout = tclass.hasLayout(layout);
1166 // If the entry is obsolete, use the new one instead.
1168 string const & obs =
1169 tclass[layout]->obsoleted_by();
1174 // see if we found the layout number:
1176 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1177 bv->owner()->dispatch(lf);
1181 if (cur_layout != layout) {
1182 cur_layout = layout;
1183 text_.setLayout(layout);
1184 bv->owner()->setLayout(cpar()->layout()->name());
1185 updwhat = CURSOR_PAR;
1189 // reset the layout box
1190 bv->owner()->setLayout(cpar()->layout()->name());
1193 case LFUN_PARAGRAPH_SPACING:
1194 // This one is absolutely not working. When fiddling with this
1195 // it also seems to me that the paragraphs inside the insettext
1196 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1197 // FIXME: how old is this comment ? ...
1199 ParagraphList::iterator pit = text_.cursor.par();
1200 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1201 float cur_value = 1.0;
1202 if (cur_spacing == Spacing::Other) {
1203 cur_value = pit->params().spacing().getValue();
1206 istringstream istr(STRCONV(cmd.argument));
1209 Spacing::Space new_spacing = cur_spacing;
1210 float new_value = cur_value;
1212 lyxerr << "Missing argument to `paragraph-spacing'"
1214 } else if (tmp == "single") {
1215 new_spacing = Spacing::Single;
1216 } else if (tmp == "onehalf") {
1217 new_spacing = Spacing::Onehalf;
1218 } else if (tmp == "double") {
1219 new_spacing = Spacing::Double;
1220 } else if (tmp == "other") {
1221 new_spacing = Spacing::Other;
1224 lyxerr << "new_value = " << tmpval << endl;
1227 } else if (tmp == "default") {
1228 new_spacing = Spacing::Default;
1230 lyxerr << _("Unknown spacing argument: ")
1231 << cmd.argument << endl;
1233 if (cur_spacing != new_spacing || cur_value != new_value) {
1234 pit->params().spacing(Spacing(new_spacing, new_value));
1235 updwhat = CURSOR_PAR;
1241 // These need to do repaints but don't require
1242 // special handling otherwise. A *lot* of the
1243 // above could probably be done similarly ...
1248 case LFUN_WORDRIGHT:
1249 // these two are really unhandled ...
1251 case LFUN_BEGINNINGBUF:
1252 updwhat = cursor_update;
1253 if (!bv->dispatch(cmd))
1254 result = UNDISPATCHED;
1263 case LFUN_WORDLEFTSEL:
1264 case LFUN_WORDRIGHTSEL:
1265 updwhat = SELECTION;
1270 if (!bv->dispatch(cmd))
1271 result = UNDISPATCHED;
1276 updateLocal(bv, updwhat, updflag);
1277 /// If the action has deleted all text in the inset, we need to change the
1278 // language to the language of the surronding text.
1279 if (!was_empty && paragraphs.begin()->empty() &&
1280 paragraphs.size() == 1) {
1281 LyXFont font(LyXFont::ALL_IGNORE);
1282 font.setLanguage(bv->getParentLanguage(this));
1283 setFont(bv, font, false);
1286 if (result >= FINISHED)
1287 bv->unlockInset(this);
1289 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1290 result = DISPATCHED;
1295 int InsetText::latex(Buffer const * buf, ostream & os,
1296 LatexRunParams const & runparams) const
1299 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1300 return texrow.rows();
1304 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1306 unsigned int lines = 0;
1308 ParagraphList::const_iterator beg = paragraphs.begin();
1309 ParagraphList::const_iterator end = paragraphs.end();
1310 ParagraphList::const_iterator it = beg;
1311 for (; it != end; ++it) {
1312 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1313 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1320 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1322 unsigned int lines = 0;
1324 vector<string> environment_stack(10);
1325 vector<string> environment_inner(10);
1327 int const command_depth = 0;
1330 Paragraph::depth_type depth = 0; // paragraph depth
1332 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1333 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1335 for (; pit != pend; ++pit) {
1337 int desc_on = 0; // description mode
1339 LyXLayout_ptr const & style = pit->layout();
1341 // environment tag closing
1342 for (; depth > pit->params().depth(); --depth) {
1343 if (environment_inner[depth] != "!-- --") {
1344 item_name = "listitem";
1345 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1346 if (environment_inner[depth] == "varlistentry")
1347 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1349 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1350 environment_stack[depth].erase();
1351 environment_inner[depth].erase();
1354 if (depth == pit->params().depth()
1355 && environment_stack[depth] != style->latexname()
1356 && !environment_stack[depth].empty()) {
1357 if (environment_inner[depth] != "!-- --") {
1358 item_name= "listitem";
1359 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1360 if (environment_inner[depth] == "varlistentry")
1361 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1364 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1366 environment_stack[depth].erase();
1367 environment_inner[depth].erase();
1370 // Write opening SGML tags.
1371 switch (style->latextype) {
1372 case LATEX_PARAGRAPH:
1373 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1377 buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1381 case LATEX_ENVIRONMENT:
1382 case LATEX_ITEM_ENVIRONMENT:
1383 if (depth < pit->params().depth()) {
1384 depth = pit->params().depth();
1385 environment_stack[depth].erase();
1388 if (environment_stack[depth] != style->latexname()) {
1389 if (environment_stack.size() == depth + 1) {
1390 environment_stack.push_back("!-- --");
1391 environment_inner.push_back("!-- --");
1393 environment_stack[depth] = style->latexname();
1394 environment_inner[depth] = "!-- --";
1395 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1397 if (environment_inner[depth] != "!-- --") {
1398 item_name= "listitem";
1399 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1400 if (environment_inner[depth] == "varlistentry")
1401 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1405 if (style->latextype == LATEX_ENVIRONMENT) {
1406 if (!style->latexparam().empty()) {
1407 if (style->latexparam() == "CDATA")
1410 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1415 desc_on = (style->labeltype == LABEL_MANUAL);
1417 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1418 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1420 item_name = desc_on ? "term" : "para";
1421 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1425 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1429 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1432 // write closing SGML tags
1433 switch (style->latextype) {
1434 case LATEX_ENVIRONMENT:
1435 if (!style->latexparam().empty()) {
1436 if (style->latexparam() == "CDATA")
1439 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1442 case LATEX_ITEM_ENVIRONMENT:
1443 if (desc_on == 1) break;
1445 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1447 case LATEX_PARAGRAPH:
1448 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1451 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1457 for (int d = depth; d >= 0; --d) {
1458 if (!environment_stack[depth].empty()) {
1459 if (environment_inner[depth] != "!-- --") {
1460 item_name = "listitem";
1461 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1462 if (environment_inner[depth] == "varlistentry")
1463 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1466 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1474 void InsetText::validate(LaTeXFeatures & features) const
1476 for_each(paragraphs.begin(), paragraphs.end(),
1477 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1481 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1483 if (the_locking_inset) {
1484 the_locking_inset->getCursor(bv, x, y);
1488 y = cy() + InsetText::y();
1492 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1494 if (the_locking_inset) {
1495 the_locking_inset->getCursorPos(bv, x, y);
1498 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1499 y = cy() - TEXT_TO_INSET_OFFSET;
1503 int InsetText::insetInInsetY() const
1505 if (!the_locking_inset)
1508 return (inset_y + the_locking_inset->insetInInsetY());
1512 void InsetText::fitInsetCursor(BufferView * bv) const
1514 if (the_locking_inset) {
1515 the_locking_inset->fitInsetCursor(bv);
1518 LyXFont const font =
1519 getLyXText(bv)->getFont(bv->buffer(), cpar(), cpos());
1521 int const asc = font_metrics::maxAscent(font);
1522 int const desc = font_metrics::maxDescent(font);
1524 if (bv->fitLockedInsetCursor(cx(bv), cy(), asc, desc))
1525 need_update |= FULL;
1530 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1532 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1533 return moveLeftIntern(bv, false, activate_inset, selecting);
1535 return moveRightIntern(bv, true, activate_inset, selecting);
1540 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1542 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1543 return moveRightIntern(bv, true, activate_inset, selecting);
1545 return moveLeftIntern(bv, false, activate_inset, selecting);
1550 InsetText::moveRightIntern(BufferView * bv, bool front,
1551 bool activate_inset, bool selecting)
1553 ParagraphList::iterator c_par = cpar();
1555 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1556 return FINISHED_RIGHT;
1557 if (activate_inset && checkAndActivateInset(bv, front))
1559 text_.cursorRight(bv);
1561 text_.clearSelection();
1562 return DISPATCHED_NOUPDATE;
1567 InsetText::moveLeftIntern(BufferView * bv, bool front,
1568 bool activate_inset, bool selecting)
1570 if (cpar() == paragraphs.begin() && cpos() <= 0)
1572 text_.cursorLeft(bv);
1574 text_.clearSelection();
1575 if (activate_inset && checkAndActivateInset(bv, front))
1577 return DISPATCHED_NOUPDATE;
1581 Inset::RESULT InsetText::moveUp(BufferView * bv)
1583 if (crow() == text_.rows().begin())
1586 text_.clearSelection();
1587 return DISPATCHED_NOUPDATE;
1591 Inset::RESULT InsetText::moveDown(BufferView * bv)
1593 if (boost::next(crow()) == text_.rows().end())
1594 return FINISHED_DOWN;
1595 text_.cursorDown(bv);
1596 text_.clearSelection();
1597 return DISPATCHED_NOUPDATE;
1601 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1603 if (the_locking_inset) {
1604 if (the_locking_inset->insetAllowed(inset))
1605 return the_locking_inset->insertInset(bv, inset);
1608 inset->setOwner(this);
1609 getLyXText(bv)->insertInset(inset);
1611 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1616 bool InsetText::insetAllowed(Inset::Code code) const
1618 // in_insetAllowed is a really gross hack,
1619 // to allow us to call the owner's insetAllowed
1620 // without stack overflow, which can happen
1621 // when the owner uses InsetCollapsable::insetAllowed()
1623 if (in_insetAllowed)
1625 in_insetAllowed = true;
1626 if (the_locking_inset)
1627 ret = the_locking_inset->insetAllowed(code);
1629 ret = owner()->insetAllowed(code);
1630 in_insetAllowed = false;
1635 UpdatableInset * InsetText::getLockingInset() const
1637 return the_locking_inset ? the_locking_inset->getLockingInset() :
1638 const_cast<InsetText *>(this);
1642 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1646 if (the_locking_inset)
1647 return the_locking_inset->getFirstLockingInsetOfType(c);
1652 bool InsetText::showInsetDialog(BufferView * bv) const
1654 if (the_locking_inset)
1655 return the_locking_inset->showInsetDialog(bv);
1660 void InsetText::getLabelList(std::vector<string> & list) const
1662 ParagraphList::const_iterator pit = paragraphs.begin();
1663 ParagraphList::const_iterator pend = paragraphs.end();
1664 for (; pit != pend; ++pit) {
1665 InsetList::const_iterator beg = pit->insetlist.begin();
1666 InsetList::const_iterator end = pit->insetlist.end();
1667 for (; beg != end; ++beg)
1668 beg->inset->getLabelList(list);
1673 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1676 if (the_locking_inset) {
1677 the_locking_inset->setFont(bv, font, toggleall, selectall);
1681 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1682 || cpar()->empty()) {
1683 getLyXText(bv)->setFont(font, toggleall);
1688 if (text_.selection.set())
1689 setUndo(bv, Undo::EDIT, text_.cursor.par());
1693 text_.selection.cursor = text_.cursor;
1694 text_.cursorBottom();
1695 text_.setSelection();
1698 text_.toggleFree(font, toggleall);
1701 text_.clearSelection();
1705 bool flag = (selectall || text_.selection.set());
1708 updateLocal(bv, FULL, true);
1710 updateLocal(bv, CURSOR_PAR, true);
1714 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1716 if (cpar()->isInset(cpos())) {
1718 static_cast<UpdatableInset*>(cpar()->getInset(cpos()));
1719 if (!isHighlyEditableInset(inset))
1721 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1722 inset->localDispatch(cmd);
1723 if (!the_locking_inset)
1725 updateLocal(bv, CURSOR, false);
1732 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1733 mouse_button::state button)
1735 x -= drawTextXOffset;
1737 int dummyy = y + dim_.asc;
1738 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1739 // we only do the edit() call if the inset was hit by the mouse
1740 // or if it is a highly editable inset. So we should call this
1741 // function from our own edit with button < 0.
1742 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1744 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1752 inset_x = cix(bv) - top_x + drawTextXOffset;
1753 inset_y = ciy() + drawTextYOffset;
1754 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1755 inset->localDispatch(cmd);
1756 if (!the_locking_inset)
1758 updateLocal(bv, CURSOR, false);
1765 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1768 int w = UpdatableInset::getMaxWidth(bv, inset);
1773 w = w - top_x + owner()->x();
1776 w -= (2 * TEXT_TO_INSET_OFFSET);
1779 return UpdatableInset::getMaxWidth(bv, inset);
1784 void InsetText::setParagraphData(ParagraphList const & plist)
1786 // we have to unlock any locked inset otherwise we're in troubles
1787 the_locking_inset = 0;
1789 // But it it makes no difference that is a lot better.
1791 // See if this can be simplified when std::list is in effect.
1794 ParagraphList::const_iterator it = plist.begin();
1795 ParagraphList::const_iterator end = plist.end();
1796 for (; it != end; ++it) {
1797 paragraphs.push_back(*it);
1798 Paragraph & tmp = paragraphs.back();
1799 tmp.setInsetOwner(this);
1807 void InsetText::markNew(bool track_changes)
1809 ParagraphList::iterator pit = paragraphs.begin();
1810 ParagraphList::iterator pend = paragraphs.end();
1811 for (; pit != pend; ++pit) {
1812 if (track_changes) {
1813 pit->trackChanges();
1815 // no-op when not tracking
1816 pit->cleanChanges();
1822 void InsetText::setText(string const & data, LyXFont const & font)
1825 for (unsigned int i = 0; i < data.length(); ++i)
1826 paragraphs.begin()->insertChar(i, data[i], font);
1831 void InsetText::setAutoBreakRows(bool flag)
1833 if (flag != autoBreakRows) {
1834 autoBreakRows = flag;
1842 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1844 if (how != drawFrame_) {
1847 updateLocal(bv, DRAW_FRAME, false);
1852 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1854 if (frame_color != col) {
1857 updateLocal(bv, DRAW_FRAME, false);
1862 int InsetText::cx(BufferView * bv) const
1864 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1865 if (the_locking_inset) {
1866 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1867 text_.cursor.pos());
1868 if (font.isVisibleRightToLeft())
1869 x -= the_locking_inset->width(bv, font);
1875 int InsetText::cix(BufferView * bv) const
1877 int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1878 if (the_locking_inset) {
1879 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1880 text_.cursor.pos());
1881 if (font.isVisibleRightToLeft())
1882 x -= the_locking_inset->width(bv, font);
1888 int InsetText::cy() const
1890 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1894 int InsetText::ciy() const
1896 return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1900 pos_type InsetText::cpos() const
1902 return text_.cursor.pos();
1906 ParagraphList::iterator InsetText::cpar() const
1908 return text_.cursor.par();
1912 bool InsetText::cboundary() const
1914 return text_.cursor.boundary();
1918 RowList::iterator InsetText::crow() const
1920 return text_.cursorRow();
1924 LyXText * InsetText::getLyXText(BufferView const * bv,
1925 bool const recursive) const
1928 if (recursive && the_locking_inset)
1929 return the_locking_inset->getLyXText(bv, true);
1934 void InsetText::setViewCache(BufferView const * bv) const
1937 text_.bv_owner = const_cast<BufferView *>(bv);
1941 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1944 /// then remove all LyXText in text-insets
1945 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1946 const_cast<ParagraphList&>(paragraphs).end(),
1947 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1952 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1954 if (paragraphs.size() == 1 && paragraphs.begin()->empty()) {
1955 // no data, resize not neccessary!
1956 // we have to do this as a fixed width may have changed!
1959 restoreLyXTextState();
1969 // one endless line, resize normally not necessary
1970 if (!force && getMaxWidth(bv, this) < 0)
1975 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1976 const_cast<ParagraphList&>(paragraphs).end(),
1977 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1980 restoreLyXTextState();
1982 if (the_locking_inset) {
1983 inset_x = cix(bv) - top_x + drawTextXOffset;
1984 inset_y = ciy() + drawTextYOffset;
1987 text_.top_y(bv->screen().topCursorVisible(&text_));
1989 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
1990 // this will scroll the screen such that the cursor becomes visible
1991 bv->updateScrollbar();
1993 need_update |= FULL;
1998 void InsetText::reinitLyXText() const
2000 BufferView * bv = text_.bv_owner;
2007 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2008 const_cast<ParagraphList&>(paragraphs).end(),
2009 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2012 restoreLyXTextState();
2013 if (the_locking_inset) {
2014 inset_x = cix(bv) - top_x + drawTextXOffset;
2015 inset_y = ciy() + drawTextYOffset;
2017 text_.top_y(bv->screen().topCursorVisible(&text_));
2019 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2020 // this will scroll the screen such that the cursor becomes visible
2021 bv->updateScrollbar();
2028 void InsetText::removeNewlines()
2030 bool changed = false;
2032 ParagraphList::iterator it = paragraphs.begin();
2033 ParagraphList::iterator end = paragraphs.end();
2034 for (; it != end; ++it) {
2035 for (int i = 0; i < it->size(); ++i) {
2036 if (it->isNewline(i)) {
2047 bool InsetText::nodraw() const
2049 if (the_locking_inset)
2050 return the_locking_inset->nodraw();
2051 return UpdatableInset::nodraw();
2055 int InsetText::scroll(bool recursive) const
2057 int sx = UpdatableInset::scroll(false);
2059 if (recursive && the_locking_inset)
2060 sx += the_locking_inset->scroll(recursive);
2066 void InsetText::clearSelection(BufferView * bv)
2068 getLyXText(bv)->clearSelection();
2072 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2074 Painter & pain = bv->painter();
2076 int h = dim_.asc + dim_.des;
2077 int ty = baseline - dim_.asc;
2083 if ((ty + h) > pain.paperHeight())
2084 h = pain.paperHeight();
2085 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2086 w = pain.paperWidth();
2087 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2092 ParagraphList * InsetText::getParagraphs(int i) const
2094 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2098 LyXCursor const & InsetText::cursor(BufferView * bv) const
2100 if (the_locking_inset)
2101 return the_locking_inset->cursor(bv);
2102 return getLyXText(bv)->cursor;
2106 Inset * InsetText::getInsetFromID(int id_arg) const
2109 return const_cast<InsetText *>(this);
2111 ParagraphList::const_iterator pit = paragraphs.begin();
2112 ParagraphList::const_iterator pend = paragraphs.end();
2113 for (; pit != pend; ++pit) {
2114 InsetList::const_iterator it = pit->insetlist.begin();
2115 InsetList::const_iterator end = pit->insetlist.end();
2116 for (; it != end; ++it) {
2117 if (it->inset->id() == id_arg)
2119 Inset * in = it->inset->getInsetFromID(id_arg);
2129 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2132 if (the_locking_inset) {
2133 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2134 if (!word.word().empty()) {
2138 // we have to go on checking so move cursor to the next char
2139 text_.cursor.pos(text_.cursor.pos() + 1);
2141 word = text_.selectNextWordToSpellcheck(value);
2142 if (word.word().empty())
2143 bv->unlockInset(const_cast<InsetText *>(this));
2150 void InsetText::selectSelectedWord(BufferView * bv)
2152 if (the_locking_inset) {
2153 the_locking_inset->selectSelectedWord(bv);
2156 getLyXText(bv)->selectSelectedWord();
2157 updateLocal(bv, SELECTION, false);
2161 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2163 if (the_locking_inset) {
2164 the_locking_inset->toggleSelection(bv, kill_selection);
2167 int x = top_x + TEXT_TO_INSET_OFFSET;
2169 RowList::iterator rowit = text_.rows().begin();
2170 RowList::iterator end = text_.rows().end();
2171 int y_offset = top_baseline - rowit->ascent_of_text();
2173 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2174 y += rowit->height();
2180 if (need_update & SELECTION)
2182 bv->screen().toggleSelection(&text_, bv, kill_selection, y_offset, x);
2186 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2188 if (the_locking_inset) {
2189 if (the_locking_inset->nextChange(bv, length))
2191 text_.cursorRight(true);
2193 lyxfind::SearchResult result =
2194 lyxfind::findNextChange(bv, &text_, length);
2196 if (result == lyxfind::SR_FOUND) {
2197 LyXCursor cur = text_.cursor;
2198 bv->unlockInset(bv->theLockingInset());
2199 if (bv->lockInset(this))
2202 text_.setSelectionRange(length);
2203 updateLocal(bv, SELECTION, false);
2205 return result != lyxfind::SR_NOT_FOUND;
2209 bool InsetText::searchForward(BufferView * bv, string const & str,
2212 if (the_locking_inset) {
2213 if (the_locking_inset->searchForward(bv, str, cs, mw))
2215 text_.cursorRight(true);
2217 lyxfind::SearchResult result =
2218 lyxfind::LyXFind(bv, &text_, str, true, cs, mw);
2220 if (result == lyxfind::SR_FOUND) {
2221 LyXCursor cur = text_.cursor;
2222 bv->unlockInset(bv->theLockingInset());
2223 if (bv->lockInset(this))
2226 text_.setSelectionRange(str.length());
2227 updateLocal(bv, SELECTION, false);
2229 return (result != lyxfind::SR_NOT_FOUND);
2232 bool InsetText::searchBackward(BufferView * bv, string const & str,
2235 if (the_locking_inset) {
2236 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2240 ParagraphList::iterator pit = paragraphs.begin();
2241 ParagraphList::iterator pend = paragraphs.end();
2243 while (boost::next(pit) != pend)
2246 text_.setCursor(pit, pit->size());
2248 lyxfind::SearchResult result =
2249 lyxfind::LyXFind(bv, &text_, str, false, cs, mw);
2251 if (result == lyxfind::SR_FOUND) {
2252 LyXCursor cur = text_.cursor;
2253 bv->unlockInset(bv->theLockingInset());
2254 if (bv->lockInset(this))
2257 text_.setSelectionRange(str.length());
2258 updateLocal(bv, SELECTION, false);
2260 return (result != lyxfind::SR_NOT_FOUND);
2264 bool InsetText::checkInsertChar(LyXFont & font)
2267 return owner()->checkInsertChar(font);
2272 void InsetText::collapseParagraphs(BufferView * bv)
2274 while (paragraphs.size() > 1) {
2275 ParagraphList::iterator first_par = paragraphs.begin();
2276 ParagraphList::iterator next_par = boost::next(first_par);
2277 size_t const first_par_size = first_par->size();
2279 if (!first_par->empty() &&
2280 !next_par->empty() &&
2281 !first_par->isSeparator(first_par_size - 1)) {
2282 first_par->insertChar(first_par_size, ' ');
2285 if (text_.selection.set()) {
2286 if (text_.selection.start.par() == next_par) {
2287 text_.selection.start.par(first_par);
2288 text_.selection.start.pos(
2289 text_.selection.start.pos() + first_par_size);
2291 if (text_.selection.end.par() == next_par) {
2292 text_.selection.end.par(first_par);
2293 text_.selection.end.pos(
2294 text_.selection.end.pos() + first_par_size);
2298 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2304 void InsetText::getDrawFont(LyXFont & font) const
2308 owner()->getDrawFont(font);
2312 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2314 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2315 // And it probably does. You have to take a look at this John. (Lgb)
2316 #warning John, have a look here. (Lgb)
2317 ParagraphList::iterator pit = plist.begin();
2318 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2320 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2322 ParagraphList::iterator pend = plist.end();
2323 for (; pit != pend; ++pit) {
2324 paragraphs.push_back(*pit);
2331 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2333 ParagraphList::const_iterator pit = paragraphs.begin();
2334 ParagraphList::const_iterator pend = paragraphs.end();
2336 for (; pit != pend; ++pit) {
2337 InsetList::const_iterator it = pit->insetlist.begin();
2338 InsetList::const_iterator end = pit->insetlist.end();
2339 for (; it != end; ++it) {
2340 it->inset->addPreview(loader);