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(), 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), 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));
174 old_par = paragraphs.end();
175 last_drawn_width = -1;
176 sstate.cursor.par(paragraphs.end());
177 in_insetAllowed = false;
181 void InsetText::clear(bool just_mark_erased)
183 if (just_mark_erased) {
184 ParagraphList::iterator it = paragraphs.begin();
185 ParagraphList::iterator end = paragraphs.end();
186 for (; it != end; ++it) {
193 // This is a gross hack...
194 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
197 paragraphs.push_back(Paragraph());
198 paragraphs.begin()->setInsetOwner(this);
199 paragraphs.begin()->layout(old_layout);
206 InsetBase * InsetText::clone() const
208 return new InsetText(*this);
212 void InsetText::write(Buffer const * buf, ostream & os) const
215 writeParagraphData(buf, os);
219 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
221 ParagraphList::const_iterator it = paragraphs.begin();
222 ParagraphList::const_iterator end = paragraphs.end();
223 Paragraph::depth_type dth = 0;
224 for (; it != end; ++it) {
225 it->write(buf, os, buf->params, dth);
230 void InsetText::read(Buffer const * buf, LyXLex & lex)
233 Paragraph::depth_type depth = 0;
237 if (buf->params.tracking_changes)
238 paragraphs.begin()->trackChanges();
240 // delete the initial paragraph
242 ParagraphList::iterator pit = paragraphs.begin();
246 token = lex.getString();
249 if (token == "\\end_inset") {
253 if (token == "\\the_end") {
254 lex.printError("\\the_end read in inset! Error in document!");
259 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
262 pit = paragraphs.begin();
263 ParagraphList::iterator const end = paragraphs.end();
264 for (; pit != end; ++pit)
265 pit->setInsetOwner(this);
267 if (token != "\\end_inset") {
268 lex.printError("Missing \\end_inset at this point. "
275 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
277 BufferView * bv = mi.base.bv;
280 dim.asc = text_.rows().begin()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
281 dim.des = text_.height - dim.asc + TEXT_TO_INSET_OFFSET;
282 dim.wid = max(textWidth(bv), int(text_.width)) + 2 * TEXT_TO_INSET_OFFSET;
283 dim.wid = max(dim.wid, 10);
288 int InsetText::textWidth(BufferView * bv, bool fordraw) const
290 int w = autoBreakRows ? getMaxWidth(bv, this) : -1;
293 return max(w - 2 * TEXT_TO_INSET_OFFSET,
294 (int)getLyXText(bv)->width);
299 return w - 2 * TEXT_TO_INSET_OFFSET;
303 void InsetText::draw(PainterInfo & pi, int x, int baseline) const
308 // update our idea of where we are. Clearly, we should
309 // not have to know this information.
313 int const start_x = x;
315 BufferView * bv = pi.base.bv;
316 Painter & pain = pi.pain;
318 // repaint the background if needed
319 if (backgroundColor() != LColor::background)
320 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
322 // no draw is necessary !!!
323 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
324 top_baseline = baseline;
332 top_baseline = baseline;
333 top_y = baseline - dim_.asc;
335 if (last_drawn_width != dim_.wid) {
337 last_drawn_width = dim_.wid;
340 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
341 inset_x = cix(bv) - int(x) + drawTextXOffset;
342 inset_y = ciy() + drawTextYOffset;
345 x += TEXT_TO_INSET_OFFSET;
347 RowList::iterator rowit = text_.rows().begin();
348 RowList::iterator end = text_.rows().end();
350 int y_offset = baseline - rowit->ascent_of_text();
351 int ph = pain.paperHeight();
354 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
355 y += rowit->height();
356 first += rowit->height();
360 text_.top_y(-y_offset);
368 int yf = y_offset + first;
373 while (rowit != end && yf < ph) {
374 paintRows(*bv, text_, rowit,
375 y + y_offset + first, int(x), y + text_.top_y());
376 y += rowit->height();
377 yf += rowit->height();
383 if ((drawFrame_ == ALWAYS) || (drawFrame_ == LOCKED && locked)) {
384 drawFrame(pain, int(start_x));
387 if (need_update != INIT) {
393 void InsetText::drawFrame(Painter & pain, int x) const
395 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
397 frame_y = top_baseline - dim_.asc + ttoD2;
398 frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
399 frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
400 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
405 void InsetText::setUpdateStatus(int what) const
408 // we will to redraw us full if our LyXText wants it
409 if (text_.needRefresh())
412 // this to not draw a selection when we redraw all of it!
413 if (need_update & CURSOR && !(need_update & SELECTION)) {
414 if (text_.selection.set())
416 text_.clearSelection();
421 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
426 if (!autoBreakRows && paragraphs.size() > 1)
427 collapseParagraphs(bv);
429 text_.partialRebreak();
430 setUpdateStatus(what);
431 bool flag = mark_dirty ||
432 ((need_update != CURSOR && need_update != NONE) ||
433 text_.needRefresh() || text_.selection.set());
434 if (!text_.selection.set())
435 text_.selection.cursor = text_.cursor;
441 bv->updateInset(const_cast<InsetText *>(this));
444 if (need_update == CURSOR)
446 bv->owner()->view_state_changed();
447 bv->owner()->updateMenubar();
448 bv->owner()->updateToolbar();
449 if (old_par != cpar()) {
450 bv->owner()->setLayout(cpar()->layout()->name());
456 string const InsetText::editMessage() const
458 return _("Opened Text Inset");
462 void InsetText::insetUnlock(BufferView * bv)
464 if (the_locking_inset) {
465 the_locking_inset->insetUnlock(bv);
466 the_locking_inset = 0;
467 updateLocal(bv, CURSOR_PAR, false);
473 if (text_.selection.set()) {
474 text_.clearSelection();
476 } else if (owner()) {
477 bv->owner()->setLayout(owner()->getLyXText(bv)
478 ->cursor.par()->layout()->name());
480 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
481 // hack for deleteEmptyParMech
482 ParagraphList::iterator first_par = paragraphs.begin();
483 if (!first_par->empty()) {
484 text_.setCursor(first_par, 0);
485 } else if (paragraphs.size() > 1) {
486 text_.setCursor(boost::next(first_par), 0);
489 updateLocal(bv, code, false);
492 setUpdateStatus(code);
497 void InsetText::lockInset(BufferView * bv)
500 the_locking_inset = 0;
501 inset_pos = inset_x = inset_y = 0;
502 inset_boundary = false;
503 inset_par = paragraphs.end();
504 old_par = paragraphs.end();
505 text_.setCursorIntern(paragraphs.begin(), 0);
506 text_.clearSelection();
508 // If the inset is empty set the language of the current font to the
509 // language to the surronding text (if different).
510 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
511 bv->getParentLanguage(this) != text_.current_font.language()) {
512 LyXFont font(LyXFont::ALL_IGNORE);
513 font.setLanguage(bv->getParentLanguage(this));
514 setFont(bv, font, false);
517 if (drawFrame_ == LOCKED)
518 code = CURSOR|DRAW_FRAME;
519 setUpdateStatus(code);
523 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
525 the_locking_inset = inset;
526 inset_x = cix(bv) - top_x + drawTextXOffset;
527 inset_y = ciy() + drawTextYOffset;
530 inset_boundary = cboundary();
531 //updateLocal(bv, CURSOR, false);
535 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
537 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
541 if (!the_locking_inset) {
542 ParagraphList::iterator pit = paragraphs.begin();
543 ParagraphList::iterator pend = paragraphs.end();
545 int const id = inset->id();
546 for (; pit != pend; ++pit) {
547 InsetList::iterator it = pit->insetlist.begin();
548 InsetList::iterator const end = pit->insetlist.end();
549 for (; it != end; ++it) {
550 if (it->inset == inset) {
551 lyxerr << "InsetText::lockInsetInInset: 1 a\n";
552 text_.setCursorIntern(pit, it->pos);
553 lyxerr << "InsetText::lockInsetInInset: 1 b\n";
554 lyxerr << "bv: " << bv << " inset: " << inset << "\n";
555 lockInset(bv, inset);
556 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
559 if (it->inset->getInsetFromID(id)) {
560 lyxerr << "InsetText::lockInsetInInset: 2\n";
561 text_.setCursorIntern(pit, it->pos);
562 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
563 return the_locking_inset->lockInsetInInset(bv, inset);
567 lyxerr << "InsetText::lockInsetInInset: 3\n";
570 if (inset == cpar()->getInset(cpos())) {
571 lyxerr[Debug::INSETS] << "OK" << endl;
572 lockInset(bv, inset);
576 if (the_locking_inset && the_locking_inset == inset) {
577 if (cpar() == inset_par && cpos() == inset_pos) {
578 lyxerr[Debug::INSETS] << "OK" << endl;
579 inset_x = cix(bv) - top_x + drawTextXOffset;
580 inset_y = ciy() + drawTextYOffset;
582 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
584 } else if (the_locking_inset) {
585 lyxerr[Debug::INSETS] << "MAYBE" << endl;
586 return the_locking_inset->lockInsetInInset(bv, inset);
588 lyxerr[Debug::INSETS] << "NOT OK" << endl;
593 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
596 if (!the_locking_inset)
598 if (the_locking_inset == inset) {
599 the_locking_inset->insetUnlock(bv);
600 getLyXText(bv)->updateInset(inset);
601 the_locking_inset = 0;
603 moveRightIntern(bv, true, false);
604 old_par = paragraphs.end(); // force layout setting
608 updateLocal(bv, CURSOR, false);
611 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
615 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
617 if (!autoBreakRows && paragraphs.size() > 1)
618 collapseParagraphs(bv);
623 if (inset->owner() != this) {
624 int ustat = CURSOR_PAR;
626 UpdatableInset * tl_inset = the_locking_inset;
628 found = tl_inset->updateInsetInInset(bv, inset);
630 tl_inset = static_cast<UpdatableInset *>(inset);
631 while(tl_inset->owner() && tl_inset->owner() != this)
632 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
633 if (!tl_inset->owner())
635 found = tl_inset->updateInsetInInset(bv, inset);
639 text_.updateInset(tl_inset);
641 setUpdateStatus(ustat);
644 bool found = text_.updateInset(inset);
646 setUpdateStatus(CURSOR_PAR);
647 if (the_locking_inset &&
648 cpar() == inset_par && cpos() == inset_pos)
650 inset_x = cix(bv) - top_x + drawTextXOffset;
651 inset_y = ciy() + drawTextYOffset;
658 void InsetText::lfunMousePress(FuncRequest const & cmd)
662 // use this to check mouse motion for selection!
666 BufferView * bv = cmd.view();
667 FuncRequest cmd1 = cmd;
673 int tmp_x = cmd.x - drawTextXOffset;
674 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
675 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
677 if (the_locking_inset) {
678 if (the_locking_inset == inset) {
679 the_locking_inset->localDispatch(cmd1);
682 // otherwise only unlock the_locking_inset
683 the_locking_inset->insetUnlock(bv);
684 the_locking_inset = 0;
687 no_selection = false;
689 if (bv->theLockingInset()) {
690 if (isHighlyEditableInset(inset)) {
691 // We just have to lock the inset before calling a
693 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
694 if (!bv->lockInset(uinset)) {
695 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
697 inset->localDispatch(cmd1);
698 if (the_locking_inset)
699 updateLocal(bv, CURSOR, false);
704 bool paste_internally = false;
705 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
706 localDispatch(FuncRequest(bv, LFUN_COPY));
707 paste_internally = true;
709 int old_top_y = text_.top_y();
711 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
713 // set the selection cursor!
714 text_.selection.cursor = text_.cursor;
715 text_.cursor.x_fix(text_.cursor.x());
717 if (text_.selection.set()) {
718 text_.clearSelection();
719 updateLocal(bv, FULL, false);
721 text_.clearSelection();
722 updateLocal(bv, CURSOR, false);
725 bv->owner()->setLayout(cpar()->layout()->name());
727 // we moved the view we cannot do mouse selection in this case!
728 if (getLyXText(bv)->top_y() != old_top_y)
731 // Insert primary selection with middle mouse
732 // if there is a local selection in the current buffer,
734 if (cmd.button() == mouse_button::button2) {
735 if (paste_internally)
736 localDispatch(FuncRequest(bv, LFUN_PASTE));
738 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
741 getLyXText(bv)->clearSelection();
746 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
748 BufferView * bv = cmd.view();
749 FuncRequest cmd1 = cmd;
754 if (the_locking_inset)
755 return the_locking_inset->localDispatch(cmd1);
757 int tmp_x = cmd.x - drawTextXOffset;
758 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
759 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
762 // This code should probably be removed now. Simple insets
763 // (!highlyEditable) can actually take the localDispatch,
764 // and turn it into edit() if necessary. But we still
765 // need to deal properly with the whole relative vs.
766 // absolute mouse co-ords thing in a realiable, sensible way
768 if (isHighlyEditableInset(inset))
769 ret = inset->localDispatch(cmd1);
771 inset_x = cix(bv) - top_x + drawTextXOffset;
772 inset_y = ciy() + drawTextYOffset;
773 cmd1.x = cmd.x - inset_x;
774 cmd1.y = cmd.x - inset_y;
775 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
779 ret = inset->localDispatch(cmd1);
780 updateLocal(bv, CURSOR_PAR, false);
787 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
789 FuncRequest cmd1 = cmd;
793 if (the_locking_inset) {
794 the_locking_inset->localDispatch(cmd1);
798 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
801 BufferView * bv = cmd.view();
802 LyXCursor cur = text_.cursor;
803 text_.setCursorFromCoordinates
804 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
805 text_.cursor.x_fix(text_.cursor.x());
806 if (cur == text_.cursor)
808 text_.setSelection();
809 bool flag = (text_.toggle_cursor.par() != text_.toggle_end_cursor.par() ||
810 text_.toggle_cursor.pos() != text_.toggle_end_cursor.pos());
812 updateLocal(bv, SELECTION, false);
817 Inset::RESULT InsetText::localDispatch(FuncRequest const & cmd)
819 BufferView * bv = cmd.view();
822 if (cmd.action == LFUN_INSET_EDIT) {
823 UpdatableInset::localDispatch(cmd);
825 if (!bv->lockInset(this)) {
826 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
831 the_locking_inset = 0;
832 inset_pos = inset_x = inset_y = 0;
833 inset_boundary = false;
834 inset_par = paragraphs.end();
835 old_par = paragraphs.end();
838 if (cmd.argument.size()) {
839 if (cmd.argument == "left")
840 text_.setCursorIntern(paragraphs.begin(), 0);
842 ParagraphList::iterator it = paragraphs.begin();
843 ParagraphList::iterator end = paragraphs.end();
844 while (boost::next(it) != end)
846 // int const pos = (p->size() ? p->size()-1 : p->size());
847 text_.setCursor(it, it->size());
850 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
851 // we put here -1 and not button as now the button in the
852 // edit call should not be needed we will fix this in 1.3.x
853 // cycle hopefully (Jug 20020509)
854 // FIXME: GUII I've changed this to none: probably WRONG
855 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
856 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
858 text_.cursor.x_fix(text_.cursor.x());
862 text_.clearSelection();
865 // If the inset is empty set the language of the current font to the
866 // language to the surronding text (if different).
867 if (paragraphs.begin()->empty() &&
868 paragraphs.size() == 1 &&
869 bv->getParentLanguage(this) != text_.current_font.language())
871 LyXFont font(LyXFont::ALL_IGNORE);
872 font.setLanguage(bv->getParentLanguage(this));
873 setFont(bv, font, false);
877 if (drawFrame_ == LOCKED)
878 code = CURSOR | DRAW_FRAME;
880 updateLocal(bv, code, false);
881 // Tell the paragraph dialog that we've entered an insettext.
882 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
887 switch (cmd.action) {
888 case LFUN_MOUSE_PRESS:
891 case LFUN_MOUSE_MOTION:
892 lfunMouseMotion(cmd);
894 case LFUN_MOUSE_RELEASE:
895 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
900 bool was_empty = (paragraphs.begin()->empty() &&
901 paragraphs.size() == 1);
903 no_selection = false;
904 RESULT result = UpdatableInset::localDispatch(cmd);
905 if (result != UNDISPATCHED)
909 if (cmd.action < 0 && cmd.argument.empty())
912 if (the_locking_inset) {
913 result = the_locking_inset->localDispatch(cmd);
914 if (result == DISPATCHED_NOUPDATE)
916 else if (result == DISPATCHED) {
917 updateLocal(bv, CURSOR_PAR, false);
919 } else if (result >= FINISHED) {
922 moveRightIntern(bv, false, false);
926 if ((result = moveUp(bv)) >= FINISHED) {
927 updateLocal(bv, CURSOR, false);
928 bv->unlockInset(this);
932 if ((result = moveDown(bv)) >= FINISHED) {
933 updateLocal(bv, CURSOR, false);
934 bv->unlockInset(this);
941 the_locking_inset = 0;
942 updateLocal(bv, CURSOR, false);
943 // make sure status gets reset immediately
944 bv->owner()->clearMessage();
951 // what type of update to do on a cursor movement
952 int cursor_update = CURSOR;
954 if (text_.selection.set())
955 cursor_update = SELECTION;
957 switch (cmd.action) {
960 case LFUN_SELFINSERT:
961 if (bv->buffer()->isReadonly()) {
962 // setErrorMessage(N_("Document is read only"));
965 if (!cmd.argument.empty()) {
966 /* Automatically delete the currently selected
967 * text and replace it with what is being
968 * typed in now. Depends on lyxrc settings
969 * "auto_region_delete", which defaults to
972 // This should not be needed here and is also WRONG!
973 setUndo(bv, Undo::INSERT, text_.cursor.par());
976 if (lyxrc.auto_region_delete) {
977 if (text_.selection.set()) {
978 text_.cutSelection(false, false);
981 text_.clearSelection();
982 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
983 bv->owner()->getIntl().getTransManager().
984 TranslateAndInsert(cmd.argument[i], &text_);
987 text_.selection.cursor = text_.cursor;
988 updwhat = CURSOR | CURSOR_PAR;
990 result = DISPATCHED_NOUPDATE;
993 // cursor movements that need special handling
996 result = moveRight(bv);
998 updwhat = cursor_update;
1002 result = moveLeft(bv);
1003 updwhat = cursor_update;
1007 result = moveDown(bv);
1008 updwhat = cursor_update;
1012 result = moveUp(bv);
1013 updwhat = cursor_update;
1017 if (crow() == text_.rows().begin())
1018 result = FINISHED_UP;
1020 text_.cursorPrevious();
1021 text_.clearSelection();
1022 result = DISPATCHED_NOUPDATE;
1024 updwhat = cursor_update;
1028 if (boost::next(crow()) == text_.rows().end())
1029 result = FINISHED_DOWN;
1032 text_.clearSelection();
1033 result = DISPATCHED_NOUPDATE;
1035 updwhat = cursor_update;
1038 case LFUN_BACKSPACE: {
1039 if (text_.selection.set())
1040 text_.cutSelection(true, false);
1043 updwhat = CURSOR_PAR;
1049 if (text_.selection.set()) {
1050 text_.cutSelection(true, false);
1054 updwhat = CURSOR_PAR;
1060 text_.cutSelection(true, true);
1061 updwhat = CURSOR_PAR;
1068 text_.copySelection();
1069 updwhat = CURSOR_PAR;
1072 case LFUN_PASTESELECTION:
1074 string const clip(bv->getClipboard());
1078 if (cmd.argument == "paragraph") {
1079 text_.insertStringAsParagraphs(clip);
1081 text_.insertStringAsLines(clip);
1084 text_.clearSelection();
1086 updwhat = CURSOR_PAR;
1092 if (!autoBreakRows) {
1093 if (CutAndPaste::nrOfParagraphs() > 1) {
1094 #ifdef WITH_WARNINGS
1095 #warning FIXME horrendously bad UI
1097 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1102 replaceSelection(bv->getLyXText());
1103 size_t sel_index = 0;
1104 string const & arg = cmd.argument;
1105 if (isStrUnsignedInt(arg)) {
1106 size_t const paste_arg = strToUnsignedInt(arg);
1107 #warning FIXME Check if the arg is in the domain of available selections.
1108 sel_index = paste_arg;
1110 text_.pasteSelection(sel_index);
1112 text_.clearSelection();
1113 updwhat = CURSOR_PAR;
1118 case LFUN_BREAKPARAGRAPH:
1119 if (!autoBreakRows) {
1120 result = DISPATCHED;
1123 replaceSelection(bv->getLyXText());
1124 text_.breakParagraph(paragraphs, 0);
1125 updwhat = CURSOR | FULL;
1129 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1130 if (!autoBreakRows) {
1131 result = DISPATCHED;
1134 replaceSelection(bv->getLyXText());
1135 text_.breakParagraph(paragraphs, 1);
1136 updwhat = CURSOR | FULL;
1140 case LFUN_BREAKLINE: {
1141 if (!autoBreakRows) {
1142 result = DISPATCHED;
1146 replaceSelection(bv->getLyXText());
1147 text_.insertInset(new InsetNewline);
1148 updwhat = CURSOR | CURSOR_PAR;
1154 // do not set layouts on non breakable textinsets
1155 if (autoBreakRows) {
1156 string cur_layout = cpar()->layout()->name();
1158 // Derive layout number from given argument (string)
1159 // and current buffer's textclass (number). */
1160 LyXTextClass const & tclass =
1161 bv->buffer()->params.getLyXTextClass();
1162 string layout = cmd.argument;
1163 bool hasLayout = tclass.hasLayout(layout);
1165 // If the entry is obsolete, use the new one instead.
1167 string const & obs =
1168 tclass[layout]->obsoleted_by();
1173 // see if we found the layout number:
1175 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1176 bv->owner()->dispatch(lf);
1180 if (cur_layout != layout) {
1181 cur_layout = layout;
1182 text_.setLayout(layout);
1183 bv->owner()->setLayout(cpar()->layout()->name());
1184 updwhat = CURSOR_PAR;
1188 // reset the layout box
1189 bv->owner()->setLayout(cpar()->layout()->name());
1192 case LFUN_PARAGRAPH_SPACING:
1193 // This one is absolutely not working. When fiddling with this
1194 // it also seems to me that the paragraphs inside the insettext
1195 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1196 // FIXME: how old is this comment ? ...
1198 ParagraphList::iterator pit = text_.cursor.par();
1199 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1200 float cur_value = 1.0;
1201 if (cur_spacing == Spacing::Other) {
1202 cur_value = pit->params().spacing().getValue();
1205 istringstream istr(STRCONV(cmd.argument));
1208 Spacing::Space new_spacing = cur_spacing;
1209 float new_value = cur_value;
1211 lyxerr << "Missing argument to `paragraph-spacing'"
1213 } else if (tmp == "single") {
1214 new_spacing = Spacing::Single;
1215 } else if (tmp == "onehalf") {
1216 new_spacing = Spacing::Onehalf;
1217 } else if (tmp == "double") {
1218 new_spacing = Spacing::Double;
1219 } else if (tmp == "other") {
1220 new_spacing = Spacing::Other;
1223 lyxerr << "new_value = " << tmpval << endl;
1226 } else if (tmp == "default") {
1227 new_spacing = Spacing::Default;
1229 lyxerr << _("Unknown spacing argument: ")
1230 << cmd.argument << endl;
1232 if (cur_spacing != new_spacing || cur_value != new_value) {
1233 pit->params().spacing(Spacing(new_spacing, new_value));
1234 updwhat = CURSOR_PAR;
1240 // These need to do repaints but don't require
1241 // special handling otherwise. A *lot* of the
1242 // above could probably be done similarly ...
1247 case LFUN_WORDRIGHT:
1248 // these two are really unhandled ...
1250 case LFUN_BEGINNINGBUF:
1251 updwhat = cursor_update;
1252 if (!bv->dispatch(cmd))
1253 result = UNDISPATCHED;
1262 case LFUN_WORDLEFTSEL:
1263 case LFUN_WORDRIGHTSEL:
1264 updwhat = SELECTION;
1269 if (!bv->dispatch(cmd))
1270 result = UNDISPATCHED;
1275 updateLocal(bv, updwhat, updflag);
1276 /// If the action has deleted all text in the inset, we need to change the
1277 // language to the language of the surronding text.
1278 if (!was_empty && paragraphs.begin()->empty() &&
1279 paragraphs.size() == 1) {
1280 LyXFont font(LyXFont::ALL_IGNORE);
1281 font.setLanguage(bv->getParentLanguage(this));
1282 setFont(bv, font, false);
1285 if (result >= FINISHED)
1286 bv->unlockInset(this);
1288 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1289 result = DISPATCHED;
1294 int InsetText::latex(Buffer const * buf, ostream & os,
1295 LatexRunParams const & runparams) const
1298 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1299 return texrow.rows();
1303 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1305 unsigned int lines = 0;
1307 ParagraphList::const_iterator beg = paragraphs.begin();
1308 ParagraphList::const_iterator end = paragraphs.end();
1309 ParagraphList::const_iterator it = beg;
1310 for (; it != end; ++it) {
1311 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1312 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1319 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1321 unsigned int lines = 0;
1323 vector<string> environment_stack(10);
1324 vector<string> environment_inner(10);
1326 int const command_depth = 0;
1329 Paragraph::depth_type depth = 0; // paragraph depth
1331 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1332 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1334 for (; pit != pend; ++pit) {
1336 int desc_on = 0; // description mode
1338 LyXLayout_ptr const & style = pit->layout();
1340 // environment tag closing
1341 for (; depth > pit->params().depth(); --depth) {
1342 if (environment_inner[depth] != "!-- --") {
1343 item_name = "listitem";
1344 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1345 if (environment_inner[depth] == "varlistentry")
1346 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1348 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1349 environment_stack[depth].erase();
1350 environment_inner[depth].erase();
1353 if (depth == pit->params().depth()
1354 && environment_stack[depth] != style->latexname()
1355 && !environment_stack[depth].empty()) {
1356 if (environment_inner[depth] != "!-- --") {
1357 item_name= "listitem";
1358 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1359 if (environment_inner[depth] == "varlistentry")
1360 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1363 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1365 environment_stack[depth].erase();
1366 environment_inner[depth].erase();
1369 // Write opening SGML tags.
1370 switch (style->latextype) {
1371 case LATEX_PARAGRAPH:
1372 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1376 buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1380 case LATEX_ENVIRONMENT:
1381 case LATEX_ITEM_ENVIRONMENT:
1382 if (depth < pit->params().depth()) {
1383 depth = pit->params().depth();
1384 environment_stack[depth].erase();
1387 if (environment_stack[depth] != style->latexname()) {
1388 if (environment_stack.size() == depth + 1) {
1389 environment_stack.push_back("!-- --");
1390 environment_inner.push_back("!-- --");
1392 environment_stack[depth] = style->latexname();
1393 environment_inner[depth] = "!-- --";
1394 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1396 if (environment_inner[depth] != "!-- --") {
1397 item_name= "listitem";
1398 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1399 if (environment_inner[depth] == "varlistentry")
1400 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1404 if (style->latextype == LATEX_ENVIRONMENT) {
1405 if (!style->latexparam().empty()) {
1406 if (style->latexparam() == "CDATA")
1409 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1414 desc_on = (style->labeltype == LABEL_MANUAL);
1416 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1417 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1419 item_name = desc_on ? "term" : "para";
1420 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1424 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1428 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1431 // write closing SGML tags
1432 switch (style->latextype) {
1433 case LATEX_ENVIRONMENT:
1434 if (!style->latexparam().empty()) {
1435 if (style->latexparam() == "CDATA")
1438 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1441 case LATEX_ITEM_ENVIRONMENT:
1442 if (desc_on == 1) break;
1444 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1446 case LATEX_PARAGRAPH:
1447 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1450 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1456 for (int d = depth; d >= 0; --d) {
1457 if (!environment_stack[depth].empty()) {
1458 if (environment_inner[depth] != "!-- --") {
1459 item_name = "listitem";
1460 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1461 if (environment_inner[depth] == "varlistentry")
1462 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1465 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1473 void InsetText::validate(LaTeXFeatures & features) const
1475 for_each(paragraphs.begin(), paragraphs.end(),
1476 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1480 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1482 if (the_locking_inset) {
1483 the_locking_inset->getCursor(bv, x, y);
1487 y = cy() + InsetText::y();
1491 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1493 if (the_locking_inset) {
1494 the_locking_inset->getCursorPos(bv, x, y);
1497 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1498 y = cy() - TEXT_TO_INSET_OFFSET;
1502 int InsetText::insetInInsetY() const
1504 if (!the_locking_inset)
1507 return (inset_y + the_locking_inset->insetInInsetY());
1511 void InsetText::fitInsetCursor(BufferView * bv) const
1513 if (the_locking_inset) {
1514 the_locking_inset->fitInsetCursor(bv);
1517 LyXFont const font =
1518 getLyXText(bv)->getFont(bv->buffer(), cpar(), cpos());
1520 int const asc = font_metrics::maxAscent(font);
1521 int const desc = font_metrics::maxDescent(font);
1523 if (bv->fitLockedInsetCursor(cx(bv), cy(), asc, desc))
1524 need_update |= FULL;
1529 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1531 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1532 return moveLeftIntern(bv, false, activate_inset, selecting);
1534 return moveRightIntern(bv, true, activate_inset, selecting);
1539 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1541 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1542 return moveRightIntern(bv, true, activate_inset, selecting);
1544 return moveLeftIntern(bv, false, activate_inset, selecting);
1549 InsetText::moveRightIntern(BufferView * bv, bool front,
1550 bool activate_inset, bool selecting)
1552 ParagraphList::iterator c_par = cpar();
1554 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1555 return FINISHED_RIGHT;
1556 if (activate_inset && checkAndActivateInset(bv, front))
1558 text_.cursorRight(bv);
1560 text_.clearSelection();
1561 return DISPATCHED_NOUPDATE;
1566 InsetText::moveLeftIntern(BufferView * bv, bool front,
1567 bool activate_inset, bool selecting)
1569 if (cpar() == paragraphs.begin() && cpos() <= 0)
1571 text_.cursorLeft(bv);
1573 text_.clearSelection();
1574 if (activate_inset && checkAndActivateInset(bv, front))
1576 return DISPATCHED_NOUPDATE;
1580 Inset::RESULT InsetText::moveUp(BufferView * bv)
1582 if (crow() == text_.rows().begin())
1585 text_.clearSelection();
1586 return DISPATCHED_NOUPDATE;
1590 Inset::RESULT InsetText::moveDown(BufferView * bv)
1592 if (boost::next(crow()) == text_.rows().end())
1593 return FINISHED_DOWN;
1594 text_.cursorDown(bv);
1595 text_.clearSelection();
1596 return DISPATCHED_NOUPDATE;
1600 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1602 if (the_locking_inset) {
1603 if (the_locking_inset->insetAllowed(inset))
1604 return the_locking_inset->insertInset(bv, inset);
1607 inset->setOwner(this);
1608 getLyXText(bv)->insertInset(inset);
1610 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1615 bool InsetText::insetAllowed(Inset::Code code) const
1617 // in_insetAllowed is a really gross hack,
1618 // to allow us to call the owner's insetAllowed
1619 // without stack overflow, which can happen
1620 // when the owner uses InsetCollapsable::insetAllowed()
1622 if (in_insetAllowed)
1624 in_insetAllowed = true;
1625 if (the_locking_inset)
1626 ret = the_locking_inset->insetAllowed(code);
1628 ret = owner()->insetAllowed(code);
1629 in_insetAllowed = false;
1634 UpdatableInset * InsetText::getLockingInset() const
1636 return the_locking_inset ? the_locking_inset->getLockingInset() :
1637 const_cast<InsetText *>(this);
1641 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1645 if (the_locking_inset)
1646 return the_locking_inset->getFirstLockingInsetOfType(c);
1651 bool InsetText::showInsetDialog(BufferView * bv) const
1653 if (the_locking_inset)
1654 return the_locking_inset->showInsetDialog(bv);
1659 void InsetText::getLabelList(std::vector<string> & list) const
1661 ParagraphList::const_iterator pit = paragraphs.begin();
1662 ParagraphList::const_iterator pend = paragraphs.end();
1663 for (; pit != pend; ++pit) {
1664 InsetList::const_iterator beg = pit->insetlist.begin();
1665 InsetList::const_iterator end = pit->insetlist.end();
1666 for (; beg != end; ++beg)
1667 beg->inset->getLabelList(list);
1672 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1675 if (the_locking_inset) {
1676 the_locking_inset->setFont(bv, font, toggleall, selectall);
1680 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1681 || cpar()->empty()) {
1682 getLyXText(bv)->setFont(font, toggleall);
1687 if (text_.selection.set())
1688 setUndo(bv, Undo::EDIT, text_.cursor.par());
1692 text_.selection.cursor = text_.cursor;
1693 text_.cursorBottom();
1694 text_.setSelection();
1697 text_.toggleFree(font, toggleall);
1700 text_.clearSelection();
1704 bool flag = (selectall || text_.selection.set());
1707 updateLocal(bv, FULL, true);
1709 updateLocal(bv, CURSOR_PAR, true);
1713 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1715 if (cpar()->isInset(cpos())) {
1717 static_cast<UpdatableInset*>(cpar()->getInset(cpos()));
1718 if (!isHighlyEditableInset(inset))
1720 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1721 inset->localDispatch(cmd);
1722 if (!the_locking_inset)
1724 updateLocal(bv, CURSOR, false);
1731 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1732 mouse_button::state button)
1734 x -= drawTextXOffset;
1736 int dummyy = y + dim_.asc;
1737 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1738 // we only do the edit() call if the inset was hit by the mouse
1739 // or if it is a highly editable inset. So we should call this
1740 // function from our own edit with button < 0.
1741 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1743 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1751 inset_x = cix(bv) - top_x + drawTextXOffset;
1752 inset_y = ciy() + drawTextYOffset;
1753 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1754 inset->localDispatch(cmd);
1755 if (!the_locking_inset)
1757 updateLocal(bv, CURSOR, false);
1764 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1767 int w = UpdatableInset::getMaxWidth(bv, inset);
1772 w = w - top_x + owner()->x();
1775 w -= (2 * TEXT_TO_INSET_OFFSET);
1778 return UpdatableInset::getMaxWidth(bv, inset);
1783 void InsetText::setParagraphData(ParagraphList const & plist)
1785 // we have to unlock any locked inset otherwise we're in troubles
1786 the_locking_inset = 0;
1788 // But it it makes no difference that is a lot better.
1790 // See if this can be simplified when std::list is in effect.
1793 ParagraphList::const_iterator it = plist.begin();
1794 ParagraphList::const_iterator end = plist.end();
1795 for (; it != end; ++it) {
1796 paragraphs.push_back(*it);
1797 Paragraph & tmp = paragraphs.back();
1798 tmp.setInsetOwner(this);
1806 void InsetText::markNew(bool track_changes)
1808 ParagraphList::iterator pit = paragraphs.begin();
1809 ParagraphList::iterator pend = paragraphs.end();
1810 for (; pit != pend; ++pit) {
1811 if (track_changes) {
1812 pit->trackChanges();
1814 // no-op when not tracking
1815 pit->cleanChanges();
1821 void InsetText::setText(string const & data, LyXFont const & font)
1824 for (unsigned int i = 0; i < data.length(); ++i)
1825 paragraphs.begin()->insertChar(i, data[i], font);
1830 void InsetText::setAutoBreakRows(bool flag)
1832 if (flag != autoBreakRows) {
1833 autoBreakRows = flag;
1841 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1843 if (how != drawFrame_) {
1846 updateLocal(bv, DRAW_FRAME, false);
1851 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1853 if (frame_color != col) {
1856 updateLocal(bv, DRAW_FRAME, false);
1861 int InsetText::cx(BufferView * bv) const
1863 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1864 if (the_locking_inset) {
1865 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1866 text_.cursor.pos());
1867 if (font.isVisibleRightToLeft())
1868 x -= the_locking_inset->width(bv, font);
1874 int InsetText::cix(BufferView * bv) const
1876 int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1877 if (the_locking_inset) {
1878 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1879 text_.cursor.pos());
1880 if (font.isVisibleRightToLeft())
1881 x -= the_locking_inset->width(bv, font);
1887 int InsetText::cy() const
1889 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1893 int InsetText::ciy() const
1895 return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1899 pos_type InsetText::cpos() const
1901 return text_.cursor.pos();
1905 ParagraphList::iterator InsetText::cpar() const
1907 return text_.cursor.par();
1911 bool InsetText::cboundary() const
1913 return text_.cursor.boundary();
1917 RowList::iterator InsetText::crow() const
1919 return text_.cursorRow();
1923 LyXText * InsetText::getLyXText(BufferView const * bv,
1924 bool const recursive) const
1927 if (recursive && the_locking_inset)
1928 return the_locking_inset->getLyXText(bv, true);
1933 void InsetText::setViewCache(BufferView const * bv) const
1936 text_.bv_owner = const_cast<BufferView *>(bv);
1940 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1943 /// then remove all LyXText in text-insets
1944 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1945 const_cast<ParagraphList&>(paragraphs).end(),
1946 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1951 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1953 if (paragraphs.size() == 1 && paragraphs.begin()->empty()) {
1954 // no data, resize not neccessary!
1955 // we have to do this as a fixed width may have changed!
1958 restoreLyXTextState();
1968 // one endless line, resize normally not necessary
1969 if (!force && getMaxWidth(bv, this) < 0)
1974 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1975 const_cast<ParagraphList&>(paragraphs).end(),
1976 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1979 restoreLyXTextState();
1981 if (the_locking_inset) {
1982 inset_x = cix(bv) - top_x + drawTextXOffset;
1983 inset_y = ciy() + drawTextYOffset;
1986 text_.top_y(bv->screen().topCursorVisible(&text_));
1988 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
1989 // this will scroll the screen such that the cursor becomes visible
1990 bv->updateScrollbar();
1992 need_update |= FULL;
1997 void InsetText::reinitLyXText() const
1999 BufferView * bv = text_.bv_owner;
2006 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2007 const_cast<ParagraphList&>(paragraphs).end(),
2008 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2011 restoreLyXTextState();
2012 if (the_locking_inset) {
2013 inset_x = cix(bv) - top_x + drawTextXOffset;
2014 inset_y = ciy() + drawTextYOffset;
2016 text_.top_y(bv->screen().topCursorVisible(&text_));
2018 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2019 // this will scroll the screen such that the cursor becomes visible
2020 bv->updateScrollbar();
2027 void InsetText::removeNewlines()
2029 bool changed = false;
2031 ParagraphList::iterator it = paragraphs.begin();
2032 ParagraphList::iterator end = paragraphs.end();
2033 for (; it != end; ++it) {
2034 for (int i = 0; i < it->size(); ++i) {
2035 if (it->isNewline(i)) {
2046 bool InsetText::nodraw() const
2048 if (the_locking_inset)
2049 return the_locking_inset->nodraw();
2050 return UpdatableInset::nodraw();
2054 int InsetText::scroll(bool recursive) const
2056 int sx = UpdatableInset::scroll(false);
2058 if (recursive && the_locking_inset)
2059 sx += the_locking_inset->scroll(recursive);
2065 void InsetText::clearSelection(BufferView * bv)
2067 getLyXText(bv)->clearSelection();
2071 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2073 Painter & pain = bv->painter();
2075 int h = dim_.asc + dim_.des;
2076 int ty = baseline - dim_.asc;
2082 if ((ty + h) > pain.paperHeight())
2083 h = pain.paperHeight();
2084 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2085 w = pain.paperWidth();
2086 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2091 ParagraphList * InsetText::getParagraphs(int i) const
2093 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2097 LyXCursor const & InsetText::cursor(BufferView * bv) const
2099 if (the_locking_inset)
2100 return the_locking_inset->cursor(bv);
2101 return getLyXText(bv)->cursor;
2105 Inset * InsetText::getInsetFromID(int id_arg) const
2108 return const_cast<InsetText *>(this);
2110 ParagraphList::const_iterator pit = paragraphs.begin();
2111 ParagraphList::const_iterator pend = paragraphs.end();
2112 for (; pit != pend; ++pit) {
2113 InsetList::const_iterator it = pit->insetlist.begin();
2114 InsetList::const_iterator end = pit->insetlist.end();
2115 for (; it != end; ++it) {
2116 if (it->inset->id() == id_arg)
2118 Inset * in = it->inset->getInsetFromID(id_arg);
2128 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2131 if (the_locking_inset) {
2132 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2133 if (!word.word().empty()) {
2137 // we have to go on checking so move cursor to the next char
2138 text_.cursor.pos(text_.cursor.pos() + 1);
2140 word = text_.selectNextWordToSpellcheck(value);
2141 if (word.word().empty())
2142 bv->unlockInset(const_cast<InsetText *>(this));
2149 void InsetText::selectSelectedWord(BufferView * bv)
2151 if (the_locking_inset) {
2152 the_locking_inset->selectSelectedWord(bv);
2155 getLyXText(bv)->selectSelectedWord();
2156 updateLocal(bv, SELECTION, false);
2160 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2162 if (the_locking_inset) {
2163 the_locking_inset->toggleSelection(bv, kill_selection);
2166 int x = top_x + TEXT_TO_INSET_OFFSET;
2168 RowList::iterator rowit = text_.rows().begin();
2169 RowList::iterator end = text_.rows().end();
2170 int y_offset = top_baseline - rowit->ascent_of_text();
2172 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2173 y += rowit->height();
2179 if (need_update & SELECTION)
2181 bv->screen().toggleSelection(&text_, bv, kill_selection, y_offset, x);
2185 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2187 if (the_locking_inset) {
2188 if (the_locking_inset->nextChange(bv, length))
2190 text_.cursorRight(true);
2192 lyxfind::SearchResult result =
2193 lyxfind::findNextChange(bv, &text_, length);
2195 if (result == lyxfind::SR_FOUND) {
2196 LyXCursor cur = text_.cursor;
2197 bv->unlockInset(bv->theLockingInset());
2198 if (bv->lockInset(this))
2201 text_.setSelectionRange(length);
2202 updateLocal(bv, SELECTION, false);
2204 return result != lyxfind::SR_NOT_FOUND;
2208 bool InsetText::searchForward(BufferView * bv, string const & str,
2211 if (the_locking_inset) {
2212 if (the_locking_inset->searchForward(bv, str, cs, mw))
2214 text_.cursorRight(true);
2216 lyxfind::SearchResult result =
2217 lyxfind::LyXFind(bv, &text_, str, true, cs, mw);
2219 if (result == lyxfind::SR_FOUND) {
2220 LyXCursor cur = text_.cursor;
2221 bv->unlockInset(bv->theLockingInset());
2222 if (bv->lockInset(this))
2225 text_.setSelectionRange(str.length());
2226 updateLocal(bv, SELECTION, false);
2228 return (result != lyxfind::SR_NOT_FOUND);
2231 bool InsetText::searchBackward(BufferView * bv, string const & str,
2234 if (the_locking_inset) {
2235 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2239 ParagraphList::iterator pit = paragraphs.begin();
2240 ParagraphList::iterator pend = paragraphs.end();
2242 while (boost::next(pit) != pend)
2245 text_.setCursor(pit, pit->size());
2247 lyxfind::SearchResult result =
2248 lyxfind::LyXFind(bv, &text_, str, false, cs, mw);
2250 if (result == lyxfind::SR_FOUND) {
2251 LyXCursor cur = text_.cursor;
2252 bv->unlockInset(bv->theLockingInset());
2253 if (bv->lockInset(this))
2256 text_.setSelectionRange(str.length());
2257 updateLocal(bv, SELECTION, false);
2259 return (result != lyxfind::SR_NOT_FOUND);
2263 bool InsetText::checkInsertChar(LyXFont & font)
2266 return owner()->checkInsertChar(font);
2271 void InsetText::collapseParagraphs(BufferView * bv)
2273 while (paragraphs.size() > 1) {
2274 ParagraphList::iterator first_par = paragraphs.begin();
2275 ParagraphList::iterator next_par = boost::next(first_par);
2276 size_t const first_par_size = first_par->size();
2278 if (!first_par->empty() &&
2279 !next_par->empty() &&
2280 !first_par->isSeparator(first_par_size - 1)) {
2281 first_par->insertChar(first_par_size, ' ');
2284 if (text_.selection.set()) {
2285 if (text_.selection.start.par() == next_par) {
2286 text_.selection.start.par(first_par);
2287 text_.selection.start.pos(
2288 text_.selection.start.pos() + first_par_size);
2290 if (text_.selection.end.par() == next_par) {
2291 text_.selection.end.par(first_par);
2292 text_.selection.end.pos(
2293 text_.selection.end.pos() + first_par_size);
2297 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2303 void InsetText::getDrawFont(LyXFont & font) const
2307 owner()->getDrawFont(font);
2311 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2313 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2314 // And it probably does. You have to take a look at this John. (Lgb)
2315 #warning John, have a look here. (Lgb)
2316 ParagraphList::iterator pit = plist.begin();
2317 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2319 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2321 ParagraphList::iterator pend = plist.end();
2322 for (; pit != pend; ++pit) {
2323 paragraphs.push_back(*pit);
2330 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2332 ParagraphList::const_iterator pit = paragraphs.begin();
2333 ParagraphList::const_iterator pend = paragraphs.end();
2335 for (; pit != pend; ++pit) {
2336 InsetList::const_iterator it = pit->insetlist.begin();
2337 InsetList::const_iterator end = pit->insetlist.end();
2338 for (; it != end; ++it) {
2339 it->inset->addPreview(loader);