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>
75 using namespace lyx::support;
76 using namespace lyx::graphics;
77 using namespace bv_funcs;
80 using lyx::textclass_type;
83 // we should get rid of these (Andre')
85 void InsetText::saveLyXTextState() const
87 // check if my paragraphs are still valid
88 ParagraphList::iterator it = const_cast<ParagraphList&>(paragraphs).begin();
89 ParagraphList::iterator end = const_cast<ParagraphList&>(paragraphs).end();
90 for (; it != end; ++it)
91 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 textwidth_ = ins->textwidth_;
156 text_.bv_owner = ins->text_.bv_owner;
157 setParagraphData(ins->paragraphs);
158 autoBreakRows = ins->autoBreakRows;
159 drawFrame_ = ins->drawFrame_;
160 frame_color = ins->frame_color;
162 textwidth_ = 0; // broken
164 frame_color = LColor::insetframe;
165 autoBreakRows = false;
167 the_locking_inset = 0;
168 for_each(paragraphs.begin(), paragraphs.end(),
169 boost::bind(&Paragraph::setInsetOwner, _1, this));
176 old_par = paragraphs.end();
177 last_drawn_width = -1;
178 sstate.cursor.par(paragraphs.end());
179 in_insetAllowed = false;
183 void InsetText::clear(bool just_mark_erased)
185 if (just_mark_erased) {
186 ParagraphList::iterator it = paragraphs.begin();
187 ParagraphList::iterator end = paragraphs.end();
188 for (; it != end; ++it) {
195 // This is a gross hack...
196 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
199 paragraphs.push_back(Paragraph());
200 paragraphs.begin()->setInsetOwner(this);
201 paragraphs.begin()->layout(old_layout);
208 auto_ptr<InsetBase> InsetText::clone() const
210 return auto_ptr<InsetBase>(new InsetText(*this));
214 void InsetText::write(Buffer const * buf, ostream & os) const
217 writeParagraphData(buf, os);
221 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
223 ParagraphList::const_iterator it = paragraphs.begin();
224 ParagraphList::const_iterator end = paragraphs.end();
225 Paragraph::depth_type dth = 0;
226 for (; it != end; ++it) {
227 it->write(buf, os, buf->params, dth);
232 void InsetText::read(Buffer const * buf, LyXLex & lex)
235 Paragraph::depth_type depth = 0;
239 if (buf->params.tracking_changes)
240 paragraphs.begin()->trackChanges();
242 // delete the initial paragraph
244 ParagraphList::iterator pit = paragraphs.begin();
248 token = lex.getString();
251 if (token == "\\end_inset") {
255 if (token == "\\end_document") {
256 lex.printError("\\end_document read in inset! Error in document!");
261 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
264 pit = paragraphs.begin();
265 ParagraphList::iterator const end = paragraphs.end();
266 for (; pit != end; ++pit)
267 pit->setInsetOwner(this);
269 if (token != "\\end_inset") {
270 lex.printError("Missing \\end_inset at this point. "
277 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
279 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
281 textwidth_ = mi.base.textwidth;
282 BufferView * bv = mi.base.bv;
284 text_.metrics(mi, dim);
285 dim.asc += TEXT_TO_INSET_OFFSET;
286 dim.des += TEXT_TO_INSET_OFFSET;
287 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
288 dim.wid = max(dim.wid, 10);
293 int InsetText::textWidth() const
299 void InsetText::draw(PainterInfo & pi, int x, int baseline) const
301 // update our idea of where we are. Clearly, we should
302 // not have to know this information.
305 int const start_x = x;
307 BufferView * bv = pi.base.bv;
308 Painter & pain = pi.pain;
310 // repaint the background if needed
311 if (backgroundColor() != LColor::background)
312 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
314 // no draw is necessary !!!
315 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
316 top_baseline = baseline;
326 top_baseline = baseline;
327 top_y = baseline - dim_.asc;
329 if (last_drawn_width != dim_.wid) {
331 last_drawn_width = dim_.wid;
334 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
335 inset_x = cix() - x + drawTextXOffset;
336 inset_y = ciy() + drawTextYOffset;
339 x += TEXT_TO_INSET_OFFSET;
341 RowList::iterator rit = text_.rows().begin();
342 RowList::iterator end = text_.rows().end();
344 int y_offset = baseline - rit->ascent_of_text();
345 int y2 = pain.paperHeight();
348 while (rit != end && y + rit->height() <= 0) {
350 first += rit->height();
354 text_.top_y(-y_offset);
362 int yo = y_offset + first;
364 paintRows(*bv, text_, rit, x, 0, yo, y2, yo);
366 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
367 drawFrame(pain, int(start_x));
369 if (need_update != INIT)
374 void InsetText::drawFrame(Painter & pain, int x) const
376 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
378 frame_y = top_baseline - dim_.asc + ttoD2;
379 frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
380 frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
381 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
386 void InsetText::setUpdateStatus(int what) const
389 // this to not draw a selection when we redraw all of it!
390 if (need_update & CURSOR && !(need_update & SELECTION)) {
391 if (text_.selection.set())
393 text_.clearSelection();
398 void InsetText::updateLocal(BufferView * bv, bool mark_dirty)
403 if (!autoBreakRows && paragraphs.size() > 1)
404 collapseParagraphs(bv);
406 text_.partialRebreak();
407 setUpdateStatus(FULL);
408 bool flag = mark_dirty ||
409 (need_update != CURSOR && need_update != NONE) ||
410 text_.selection.set();
411 if (!text_.selection.set())
412 text_.selection.cursor = text_.cursor;
417 bv->updateInset(const_cast<InsetText *>(this));
419 if (need_update == CURSOR)
421 bv->owner()->view_state_changed();
422 bv->owner()->updateMenubar();
423 bv->owner()->updateToolbar();
424 if (old_par != cpar()) {
425 bv->owner()->setLayout(cpar()->layout()->name());
431 string const InsetText::editMessage() const
433 return _("Opened Text Inset");
437 void InsetText::insetUnlock(BufferView * bv)
439 if (the_locking_inset) {
440 the_locking_inset->insetUnlock(bv);
441 the_locking_inset = 0;
442 updateLocal(bv, false);
448 if (text_.selection.set()) {
449 text_.clearSelection();
451 } else if (owner()) {
452 bv->owner()->setLayout(owner()->getLyXText(bv)
453 ->cursor.par()->layout()->name());
455 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
456 // hack for deleteEmptyParMech
457 ParagraphList::iterator first_par = paragraphs.begin();
458 if (!first_par->empty()) {
459 text_.setCursor(first_par, 0);
460 } else if (paragraphs.size() > 1) {
461 text_.setCursor(boost::next(first_par), 0);
464 setUpdateStatus(code);
468 void InsetText::lockInset(BufferView * bv)
471 the_locking_inset = 0;
472 inset_pos = inset_x = inset_y = 0;
473 inset_boundary = false;
474 inset_par = paragraphs.end();
475 old_par = paragraphs.end();
476 text_.setCursorIntern(paragraphs.begin(), 0);
477 text_.clearSelection();
479 // If the inset is empty set the language of the current font to the
480 // language to the surronding text (if different).
481 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
482 bv->getParentLanguage(this) != text_.current_font.language()) {
483 LyXFont font(LyXFont::ALL_IGNORE);
484 font.setLanguage(bv->getParentLanguage(this));
485 setFont(bv, font, false);
488 if (drawFrame_ == LOCKED)
489 code = CURSOR|DRAW_FRAME;
490 setUpdateStatus(code);
494 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
496 the_locking_inset = inset;
497 inset_x = cix() - top_x + drawTextXOffset;
498 inset_y = ciy() + drawTextYOffset;
501 inset_boundary = cboundary();
505 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
507 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
508 << inset << "): " << endl;
511 if (!the_locking_inset) {
512 ParagraphList::iterator pit = paragraphs.begin();
513 ParagraphList::iterator pend = paragraphs.end();
515 int const id = inset->id();
516 for (; pit != pend; ++pit) {
517 InsetList::iterator it = pit->insetlist.begin();
518 InsetList::iterator const end = pit->insetlist.end();
519 for (; it != end; ++it) {
520 if (it->inset == inset) {
521 lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
522 text_.setCursorIntern(pit, it->pos);
523 lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
524 lyxerr << "bv: " << bv << " inset: " << inset << endl;
525 lockInset(bv, inset);
526 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
529 if (it->inset->getInsetFromID(id)) {
530 lyxerr << "InsetText::lockInsetInInset: 2" << endl;
531 text_.setCursorIntern(pit, it->pos);
532 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
533 return the_locking_inset->lockInsetInInset(bv, inset);
537 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
540 if (inset == cpar()->getInset(cpos())) {
541 lyxerr[Debug::INSETS] << "OK" << endl;
542 lockInset(bv, inset);
546 if (the_locking_inset && the_locking_inset == inset) {
547 if (cpar() == inset_par && cpos() == inset_pos) {
548 lyxerr[Debug::INSETS] << "OK" << endl;
549 inset_x = cix() - top_x + drawTextXOffset;
550 inset_y = ciy() + drawTextYOffset;
552 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
554 } else if (the_locking_inset) {
555 lyxerr[Debug::INSETS] << "MAYBE" << endl;
556 return the_locking_inset->lockInsetInInset(bv, inset);
558 lyxerr[Debug::INSETS] << "NOT OK" << endl;
563 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
566 if (!the_locking_inset)
568 if (the_locking_inset == inset) {
569 the_locking_inset->insetUnlock(bv);
570 getLyXText(bv)->updateInset(inset);
571 the_locking_inset = 0;
573 moveRightIntern(bv, true, false);
574 old_par = paragraphs.end(); // force layout setting
578 updateLocal(bv, false);
581 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
585 bool InsetText::updateInsetInInset(BufferView * bv, InsetOld * inset)
587 if (!autoBreakRows && paragraphs.size() > 1)
588 collapseParagraphs(bv);
593 if (inset->owner() != this) {
594 int ustat = CURSOR_PAR;
596 UpdatableInset * tl_inset = the_locking_inset;
598 found = tl_inset->updateInsetInInset(bv, inset);
600 tl_inset = static_cast<UpdatableInset *>(inset);
601 while(tl_inset->owner() && tl_inset->owner() != this)
602 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
603 if (!tl_inset->owner())
605 found = tl_inset->updateInsetInInset(bv, inset);
608 text_.updateInset(tl_inset);
609 setUpdateStatus(ustat);
613 bool found = text_.updateInset(inset);
615 setUpdateStatus(CURSOR_PAR);
616 if (the_locking_inset &&
617 cpar() == inset_par && cpos() == inset_pos)
619 inset_x = cix() - top_x + drawTextXOffset;
620 inset_y = ciy() + drawTextYOffset;
627 void InsetText::lfunMousePress(FuncRequest const & cmd)
631 // use this to check mouse motion for selection!
635 BufferView * bv = cmd.view();
636 FuncRequest cmd1 = cmd;
642 int tmp_x = cmd.x - drawTextXOffset;
643 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
644 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
646 if (the_locking_inset) {
647 if (the_locking_inset == inset) {
648 the_locking_inset->localDispatch(cmd1);
651 // otherwise only unlock the_locking_inset
652 the_locking_inset->insetUnlock(bv);
653 the_locking_inset = 0;
656 no_selection = false;
658 if (bv->theLockingInset()) {
659 if (isHighlyEditableInset(inset)) {
660 // We just have to lock the inset before calling a
662 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
663 if (!bv->lockInset(uinset)) {
664 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
666 inset->localDispatch(cmd1);
667 if (the_locking_inset)
668 updateLocal(bv, false);
673 bool paste_internally = false;
674 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
675 localDispatch(FuncRequest(bv, LFUN_COPY));
676 paste_internally = true;
678 int old_top_y = text_.top_y();
680 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
682 // set the selection cursor!
683 text_.selection.cursor = text_.cursor;
684 text_.cursor.x_fix(text_.cursor.x());
686 text_.clearSelection();
687 updateLocal(bv, false);
689 bv->owner()->setLayout(cpar()->layout()->name());
691 // we moved the view we cannot do mouse selection in this case!
692 if (getLyXText(bv)->top_y() != old_top_y)
695 // Insert primary selection with middle mouse
696 // if there is a local selection in the current buffer,
698 if (cmd.button() == mouse_button::button2) {
699 if (paste_internally)
700 localDispatch(FuncRequest(bv, LFUN_PASTE));
702 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
705 getLyXText(bv)->clearSelection();
710 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
712 BufferView * bv = cmd.view();
713 FuncRequest cmd1 = cmd;
718 if (the_locking_inset)
719 return the_locking_inset->localDispatch(cmd1);
721 int tmp_x = cmd.x - drawTextXOffset;
722 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
723 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
726 // This code should probably be removed now. Simple insets
727 // (!highlyEditable) can actually take the localDispatch,
728 // and turn it into edit() if necessary. But we still
729 // need to deal properly with the whole relative vs.
730 // absolute mouse co-ords thing in a realiable, sensible way
732 if (isHighlyEditableInset(inset))
733 ret = inset->localDispatch(cmd1);
735 inset_x = cix(bv) - top_x + drawTextXOffset;
736 inset_y = ciy() + drawTextYOffset;
737 cmd1.x = cmd.x - inset_x;
738 cmd1.y = cmd.x - inset_y;
739 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
743 ret = inset->localDispatch(cmd1);
744 updateLocal(bv, false);
751 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
753 FuncRequest cmd1 = cmd;
757 if (the_locking_inset) {
758 the_locking_inset->localDispatch(cmd1);
762 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
765 BufferView * bv = cmd.view();
766 LyXCursor cur = text_.cursor;
767 text_.setCursorFromCoordinates
768 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
769 text_.cursor.x_fix(text_.cursor.x());
770 if (cur == text_.cursor)
772 text_.setSelection();
773 updateLocal(bv, false);
777 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
779 BufferView * bv = cmd.view();
782 switch (cmd.action) {
783 case LFUN_INSET_EDIT: {
784 UpdatableInset::localDispatch(cmd);
786 if (!bv->lockInset(this)) {
787 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
792 the_locking_inset = 0;
796 inset_boundary = false;
797 inset_par = paragraphs.end();
798 old_par = paragraphs.end();
801 if (cmd.argument.size()) {
802 if (cmd.argument == "left")
803 text_.setCursorIntern(paragraphs.begin(), 0);
805 ParagraphList::iterator it = boost::prior(paragraphs.end());
806 text_.setCursor(it, it->size());
809 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
810 // we put here -1 and not button as now the button in the
811 // edit call should not be needed we will fix this in 1.3.x
812 // cycle hopefully (Jug 20020509)
813 // FIXME: GUII I've changed this to none: probably WRONG
814 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
815 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
817 text_.cursor.x_fix(text_.cursor.x());
821 text_.clearSelection();
824 // If the inset is empty set the language of the current font to the
825 // language to the surronding text (if different).
826 if (paragraphs.begin()->empty() &&
827 paragraphs.size() == 1 &&
828 bv->getParentLanguage(this) != text_.current_font.language())
830 LyXFont font(LyXFont::ALL_IGNORE);
831 font.setLanguage(bv->getParentLanguage(this));
832 setFont(bv, font, false);
836 if (drawFrame_ == LOCKED)
837 code = CURSOR | DRAW_FRAME;
839 updateLocal(bv, false);
840 // Tell the paragraph dialog that we've entered an insettext.
841 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
845 case LFUN_MOUSE_PRESS:
849 case LFUN_MOUSE_MOTION:
850 lfunMouseMotion(cmd);
853 case LFUN_MOUSE_RELEASE:
854 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
860 bool was_empty = (paragraphs.begin()->empty() &&
861 paragraphs.size() == 1);
863 no_selection = false;
864 RESULT result = UpdatableInset::localDispatch(cmd);
865 if (result != UNDISPATCHED)
869 if (cmd.action < 0 && cmd.argument.empty())
872 if (the_locking_inset) {
873 result = the_locking_inset->localDispatch(cmd);
874 if (result == DISPATCHED_NOUPDATE)
876 else if (result == DISPATCHED) {
877 updateLocal(bv, false);
879 } else if (result >= FINISHED) {
882 moveRightIntern(bv, false, false);
886 if ((result = moveUp(bv)) >= FINISHED) {
887 updateLocal(bv, false);
888 bv->unlockInset(this);
892 if ((result = moveDown(bv)) >= FINISHED) {
893 updateLocal(bv, false);
894 bv->unlockInset(this);
901 the_locking_inset = 0;
902 updateLocal(bv, false);
903 // make sure status gets reset immediately
904 bv->owner()->clearMessage();
911 // what type of update to do on a cursor movement
912 int cursor_update = CURSOR;
914 if (text_.selection.set())
915 cursor_update = SELECTION;
917 switch (cmd.action) {
920 case LFUN_SELFINSERT:
921 if (bv->buffer()->isReadonly()) {
922 // setErrorMessage(N_("Document is read only"));
925 if (!cmd.argument.empty()) {
926 /* Automatically delete the currently selected
927 * text and replace it with what is being
928 * typed in now. Depends on lyxrc settings
929 * "auto_region_delete", which defaults to
932 // This should not be needed here and is also WRONG!
933 recordUndo(bv, Undo::INSERT, text_.cursor.par());
936 if (lyxrc.auto_region_delete) {
937 if (text_.selection.set()) {
938 text_.cutSelection(false, false);
941 text_.clearSelection();
942 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
943 bv->owner()->getIntl().getTransManager().
944 TranslateAndInsert(cmd.argument[i], &text_);
947 text_.selection.cursor = text_.cursor;
948 updwhat = CURSOR | CURSOR_PAR;
950 result = DISPATCHED_NOUPDATE;
953 // cursor movements that need special handling
956 result = moveRight(bv);
958 updwhat = cursor_update;
962 result = moveLeft(bv);
963 updwhat = cursor_update;
967 result = moveDown(bv);
968 updwhat = cursor_update;
973 updwhat = cursor_update;
977 if (crow() == text_.rows().begin())
978 result = FINISHED_UP;
980 text_.cursorPrevious();
981 text_.clearSelection();
982 result = DISPATCHED_NOUPDATE;
984 updwhat = cursor_update;
988 if (boost::next(crow()) == text_.rows().end())
989 result = FINISHED_DOWN;
992 text_.clearSelection();
993 result = DISPATCHED_NOUPDATE;
995 updwhat = cursor_update;
998 case LFUN_BACKSPACE: {
999 if (text_.selection.set())
1000 text_.cutSelection(true, false);
1003 updwhat = CURSOR_PAR;
1009 if (text_.selection.set()) {
1010 text_.cutSelection(true, false);
1014 updwhat = CURSOR_PAR;
1020 text_.cutSelection(true, true);
1021 updwhat = CURSOR_PAR;
1028 text_.copySelection();
1029 updwhat = CURSOR_PAR;
1032 case LFUN_PASTESELECTION:
1034 string const clip(bv->getClipboard());
1038 if (cmd.argument == "paragraph") {
1039 text_.insertStringAsParagraphs(clip);
1041 text_.insertStringAsLines(clip);
1044 text_.clearSelection();
1046 updwhat = CURSOR_PAR;
1052 if (!autoBreakRows) {
1053 if (CutAndPaste::nrOfParagraphs() > 1) {
1054 #ifdef WITH_WARNINGS
1055 #warning FIXME horrendously bad UI
1057 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1062 replaceSelection(bv->getLyXText());
1063 size_t sel_index = 0;
1064 string const & arg = cmd.argument;
1065 if (isStrUnsignedInt(arg)) {
1066 size_t const paste_arg = strToUnsignedInt(arg);
1067 #warning FIXME Check if the arg is in the domain of available selections.
1068 sel_index = paste_arg;
1070 text_.pasteSelection(sel_index);
1072 text_.clearSelection();
1073 updwhat = CURSOR_PAR;
1078 case LFUN_BREAKPARAGRAPH:
1079 if (!autoBreakRows) {
1080 result = DISPATCHED;
1083 replaceSelection(bv->getLyXText());
1084 text_.breakParagraph(paragraphs, 0);
1085 updwhat = CURSOR | FULL;
1089 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1090 if (!autoBreakRows) {
1091 result = DISPATCHED;
1094 replaceSelection(bv->getLyXText());
1095 text_.breakParagraph(paragraphs, 1);
1096 updwhat = CURSOR | FULL;
1100 case LFUN_BREAKLINE: {
1101 if (!autoBreakRows) {
1102 result = DISPATCHED;
1106 replaceSelection(bv->getLyXText());
1107 text_.insertInset(new InsetNewline);
1108 updwhat = CURSOR | CURSOR_PAR;
1114 // do not set layouts on non breakable textinsets
1115 if (autoBreakRows) {
1116 string cur_layout = cpar()->layout()->name();
1118 // Derive layout number from given argument (string)
1119 // and current buffer's textclass (number). */
1120 LyXTextClass const & tclass =
1121 bv->buffer()->params.getLyXTextClass();
1122 string layout = cmd.argument;
1123 bool hasLayout = tclass.hasLayout(layout);
1125 // If the entry is obsolete, use the new one instead.
1127 string const & obs =
1128 tclass[layout]->obsoleted_by();
1133 // see if we found the layout number:
1135 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1136 bv->owner()->dispatch(lf);
1140 if (cur_layout != layout) {
1141 cur_layout = layout;
1142 text_.setLayout(layout);
1143 bv->owner()->setLayout(cpar()->layout()->name());
1144 updwhat = CURSOR_PAR;
1148 // reset the layout box
1149 bv->owner()->setLayout(cpar()->layout()->name());
1152 case LFUN_PARAGRAPH_SPACING:
1153 // This one is absolutely not working. When fiddling with this
1154 // it also seems to me that the paragraphs inside the insettext
1155 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1156 // FIXME: how old is this comment ? ...
1158 ParagraphList::iterator pit = text_.cursor.par();
1159 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1160 float cur_value = 1.0;
1161 if (cur_spacing == Spacing::Other) {
1162 cur_value = pit->params().spacing().getValue();
1165 istringstream istr(STRCONV(cmd.argument));
1168 Spacing::Space new_spacing = cur_spacing;
1169 float new_value = cur_value;
1171 lyxerr << "Missing argument to `paragraph-spacing'"
1173 } else if (tmp == "single") {
1174 new_spacing = Spacing::Single;
1175 } else if (tmp == "onehalf") {
1176 new_spacing = Spacing::Onehalf;
1177 } else if (tmp == "double") {
1178 new_spacing = Spacing::Double;
1179 } else if (tmp == "other") {
1180 new_spacing = Spacing::Other;
1183 lyxerr << "new_value = " << tmpval << endl;
1186 } else if (tmp == "default") {
1187 new_spacing = Spacing::Default;
1189 lyxerr << _("Unknown spacing argument: ")
1190 << cmd.argument << endl;
1192 if (cur_spacing != new_spacing || cur_value != new_value) {
1193 pit->params().spacing(Spacing(new_spacing, new_value));
1194 updwhat = CURSOR_PAR;
1200 // These need to do repaints but don't require
1201 // special handling otherwise. A *lot* of the
1202 // above could probably be done similarly ...
1207 case LFUN_WORDRIGHT:
1208 // these two are really unhandled ...
1210 case LFUN_BEGINNINGBUF:
1211 updwhat = cursor_update;
1212 if (!bv->dispatch(cmd))
1213 result = UNDISPATCHED;
1222 case LFUN_WORDLEFTSEL:
1223 case LFUN_WORDRIGHTSEL:
1224 updwhat = SELECTION;
1229 if (!bv->dispatch(cmd))
1230 result = UNDISPATCHED;
1235 updateLocal(bv, updflag);
1236 /// If the action has deleted all text in the inset, we need to change the
1237 // language to the language of the surronding text.
1238 if (!was_empty && paragraphs.begin()->empty() &&
1239 paragraphs.size() == 1) {
1240 LyXFont font(LyXFont::ALL_IGNORE);
1241 font.setLanguage(bv->getParentLanguage(this));
1242 setFont(bv, font, false);
1245 if (result >= FINISHED)
1246 bv->unlockInset(this);
1248 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1249 result = DISPATCHED;
1254 int InsetText::latex(Buffer const * buf, ostream & os,
1255 LatexRunParams const & runparams) const
1258 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1259 return texrow.rows();
1263 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1265 unsigned int lines = 0;
1267 ParagraphList::const_iterator beg = paragraphs.begin();
1268 ParagraphList::const_iterator end = paragraphs.end();
1269 ParagraphList::const_iterator it = beg;
1270 for (; it != end; ++it) {
1271 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1272 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1279 int InsetText::linuxdoc(Buffer const * buf, ostream & os) const
1281 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1282 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1284 // There is a confusion between the empty paragraph and the default paragraph
1285 // The default paragraph is <p></p>, the empty paragraph is *empty*
1286 // Since none of the floats of linuxdoc accepts standard paragraphs
1287 // I disable them. I don't expect problems. (jamatos 2003/07/27)
1288 for (; pit != pend; ++pit) {
1289 const string name = pit->layout()->latexname();
1291 sgml::openTag(os, 1, 0, name);
1292 buf->simpleLinuxDocOnePar(os, pit, 0);
1294 sgml::closeTag(os, 1, 0, name);
1300 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1302 unsigned int lines = 0;
1304 vector<string> environment_stack(10);
1305 vector<string> environment_inner(10);
1307 int const command_depth = 0;
1310 Paragraph::depth_type depth = 0; // paragraph depth
1312 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1313 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1315 for (; pit != pend; ++pit) {
1317 int desc_on = 0; // description mode
1319 LyXLayout_ptr const & style = pit->layout();
1321 // environment tag closing
1322 for (; depth > pit->params().depth(); --depth) {
1323 if (environment_inner[depth] != "!-- --") {
1324 item_name = "listitem";
1325 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1326 if (environment_inner[depth] == "varlistentry")
1327 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1329 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1330 environment_stack[depth].erase();
1331 environment_inner[depth].erase();
1334 if (depth == pit->params().depth()
1335 && environment_stack[depth] != style->latexname()
1336 && !environment_stack[depth].empty()) {
1337 if (environment_inner[depth] != "!-- --") {
1338 item_name= "listitem";
1339 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1340 if (environment_inner[depth] == "varlistentry")
1341 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1344 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1346 environment_stack[depth].erase();
1347 environment_inner[depth].erase();
1350 // Write opening SGML tags.
1351 switch (style->latextype) {
1352 case LATEX_PARAGRAPH:
1353 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1357 buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1361 case LATEX_ENVIRONMENT:
1362 case LATEX_ITEM_ENVIRONMENT:
1363 if (depth < pit->params().depth()) {
1364 depth = pit->params().depth();
1365 environment_stack[depth].erase();
1368 if (environment_stack[depth] != style->latexname()) {
1369 if (environment_stack.size() == depth + 1) {
1370 environment_stack.push_back("!-- --");
1371 environment_inner.push_back("!-- --");
1373 environment_stack[depth] = style->latexname();
1374 environment_inner[depth] = "!-- --";
1375 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1377 if (environment_inner[depth] != "!-- --") {
1378 item_name= "listitem";
1379 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1380 if (environment_inner[depth] == "varlistentry")
1381 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1385 if (style->latextype == LATEX_ENVIRONMENT) {
1386 if (!style->latexparam().empty()) {
1387 if (style->latexparam() == "CDATA")
1390 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1395 desc_on = (style->labeltype == LABEL_MANUAL);
1397 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1398 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1400 item_name = desc_on ? "term" : "para";
1401 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1405 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1409 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1412 // write closing SGML tags
1413 switch (style->latextype) {
1414 case LATEX_ENVIRONMENT:
1415 if (!style->latexparam().empty()) {
1416 if (style->latexparam() == "CDATA")
1419 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1422 case LATEX_ITEM_ENVIRONMENT:
1423 if (desc_on == 1) break;
1425 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1427 case LATEX_PARAGRAPH:
1428 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1431 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1437 for (int d = depth; d >= 0; --d) {
1438 if (!environment_stack[depth].empty()) {
1439 if (environment_inner[depth] != "!-- --") {
1440 item_name = "listitem";
1441 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1442 if (environment_inner[depth] == "varlistentry")
1443 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1446 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1454 void InsetText::validate(LaTeXFeatures & features) const
1456 for_each(paragraphs.begin(), paragraphs.end(),
1457 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1461 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1463 if (the_locking_inset) {
1464 the_locking_inset->getCursor(bv, x, y);
1468 y = cy() + InsetText::y();
1472 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1474 if (the_locking_inset) {
1475 the_locking_inset->getCursorPos(bv, x, y);
1478 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1479 y = cy() - TEXT_TO_INSET_OFFSET;
1483 int InsetText::insetInInsetY() const
1485 if (!the_locking_inset)
1488 return inset_y + the_locking_inset->insetInInsetY();
1492 void InsetText::fitInsetCursor(BufferView * bv) const
1494 if (the_locking_inset) {
1495 the_locking_inset->fitInsetCursor(bv);
1499 LyXFont const font = text_.getFont(cpar(), cpos());
1501 int const asc = font_metrics::maxAscent(font);
1502 int const desc = font_metrics::maxDescent(font);
1504 if (bv->fitLockedInsetCursor(cx(), cy(), asc, desc))
1505 need_update |= FULL;
1510 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1512 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1513 return moveLeftIntern(bv, false, activate_inset, selecting);
1515 return moveRightIntern(bv, true, activate_inset, selecting);
1520 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1522 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1523 return moveRightIntern(bv, true, activate_inset, selecting);
1525 return moveLeftIntern(bv, false, activate_inset, selecting);
1530 InsetText::moveRightIntern(BufferView * bv, bool front,
1531 bool activate_inset, bool selecting)
1533 ParagraphList::iterator c_par = cpar();
1535 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1536 return FINISHED_RIGHT;
1537 if (activate_inset && checkAndActivateInset(bv, front))
1539 text_.cursorRight(bv);
1541 text_.clearSelection();
1542 return DISPATCHED_NOUPDATE;
1547 InsetText::moveLeftIntern(BufferView * bv, bool front,
1548 bool activate_inset, bool selecting)
1550 if (cpar() == paragraphs.begin() && cpos() <= 0)
1552 text_.cursorLeft(bv);
1554 text_.clearSelection();
1555 if (activate_inset && checkAndActivateInset(bv, front))
1557 return DISPATCHED_NOUPDATE;
1561 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1563 if (crow() == text_.rows().begin())
1566 text_.clearSelection();
1567 return DISPATCHED_NOUPDATE;
1571 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1573 if (boost::next(crow()) == text_.rows().end())
1574 return FINISHED_DOWN;
1575 text_.cursorDown(bv);
1576 text_.clearSelection();
1577 return DISPATCHED_NOUPDATE;
1581 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1583 if (the_locking_inset) {
1584 if (the_locking_inset->insetAllowed(inset))
1585 return the_locking_inset->insertInset(bv, inset);
1588 inset->setOwner(this);
1589 text_.insertInset(inset);
1591 updateLocal(bv, true);
1596 bool InsetText::insetAllowed(InsetOld::Code code) const
1598 // in_insetAllowed is a really gross hack,
1599 // to allow us to call the owner's insetAllowed
1600 // without stack overflow, which can happen
1601 // when the owner uses InsetCollapsable::insetAllowed()
1603 if (in_insetAllowed)
1605 in_insetAllowed = true;
1606 if (the_locking_inset)
1607 ret = the_locking_inset->insetAllowed(code);
1609 ret = owner()->insetAllowed(code);
1610 in_insetAllowed = false;
1615 UpdatableInset * InsetText::getLockingInset() const
1617 return the_locking_inset ? the_locking_inset->getLockingInset() :
1618 const_cast<InsetText *>(this);
1622 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1626 if (the_locking_inset)
1627 return the_locking_inset->getFirstLockingInsetOfType(c);
1632 bool InsetText::showInsetDialog(BufferView * bv) const
1634 if (the_locking_inset)
1635 return the_locking_inset->showInsetDialog(bv);
1640 void InsetText::getLabelList(std::vector<string> & list) const
1642 ParagraphList::const_iterator pit = paragraphs.begin();
1643 ParagraphList::const_iterator pend = paragraphs.end();
1644 for (; pit != pend; ++pit) {
1645 InsetList::const_iterator beg = pit->insetlist.begin();
1646 InsetList::const_iterator end = pit->insetlist.end();
1647 for (; beg != end; ++beg)
1648 beg->inset->getLabelList(list);
1653 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1656 if (the_locking_inset) {
1657 the_locking_inset->setFont(bv, font, toggleall, selectall);
1661 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1662 || cpar()->empty()) {
1663 text_.setFont(font, toggleall);
1668 if (text_.selection.set())
1669 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1673 text_.selection.cursor = text_.cursor;
1674 text_.cursorBottom();
1675 text_.setSelection();
1678 text_.toggleFree(font, toggleall);
1681 text_.clearSelection();
1684 updateLocal(bv, true);
1688 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1690 InsetOld * inset = cpar()->getInset(cpos());
1691 if (!isHighlyEditableInset(inset))
1693 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1694 inset->localDispatch(cmd);
1695 if (!the_locking_inset)
1697 updateLocal(bv, false);
1702 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1703 mouse_button::state button)
1705 x -= drawTextXOffset;
1707 int dummyy = y + dim_.asc;
1708 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1709 // we only do the edit() call if the inset was hit by the mouse
1710 // or if it is a highly editable inset. So we should call this
1711 // function from our own edit with button < 0.
1712 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1714 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1723 inset_x = cix() - top_x + drawTextXOffset;
1724 inset_y = ciy() + drawTextYOffset;
1725 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1726 inset->localDispatch(cmd);
1727 if (!the_locking_inset)
1729 updateLocal(bv, false);
1734 void InsetText::setParagraphData(ParagraphList const & plist)
1736 // we have to unlock any locked inset otherwise we're in troubles
1737 the_locking_inset = 0;
1739 // But it it makes no difference that is a lot better.
1741 // See if this can be simplified when std::list is in effect.
1744 ParagraphList::const_iterator it = plist.begin();
1745 ParagraphList::const_iterator end = plist.end();
1746 for (; it != end; ++it) {
1747 paragraphs.push_back(*it);
1748 paragraphs.back().setInsetOwner(this);
1756 void InsetText::markNew(bool track_changes)
1758 ParagraphList::iterator pit = paragraphs.begin();
1759 ParagraphList::iterator pend = paragraphs.end();
1760 for (; pit != pend; ++pit) {
1761 if (track_changes) {
1762 pit->trackChanges();
1764 // no-op when not tracking
1765 pit->cleanChanges();
1771 void InsetText::setText(string const & data, LyXFont const & font)
1774 for (unsigned int i = 0; i < data.length(); ++i)
1775 paragraphs.begin()->insertChar(i, data[i], font);
1780 void InsetText::setAutoBreakRows(bool flag)
1782 if (flag != autoBreakRows) {
1783 autoBreakRows = flag;
1791 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1793 if (how != drawFrame_) {
1796 updateLocal(bv, false);
1801 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1803 if (frame_color != col) {
1806 updateLocal(bv, false);
1811 int InsetText::cx() const
1813 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1814 if (the_locking_inset) {
1815 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1816 if (font.isVisibleRightToLeft())
1817 x -= the_locking_inset->width();
1823 int InsetText::cix() const
1825 int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1826 if (the_locking_inset) {
1827 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1828 if (font.isVisibleRightToLeft())
1829 x -= the_locking_inset->width();
1835 int InsetText::cy() const
1837 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1841 int InsetText::ciy() const
1843 return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1847 pos_type InsetText::cpos() const
1849 return text_.cursor.pos();
1853 ParagraphList::iterator InsetText::cpar() const
1855 return text_.cursor.par();
1859 bool InsetText::cboundary() const
1861 return text_.cursor.boundary();
1865 RowList::iterator InsetText::crow() const
1867 return text_.cursorRow();
1871 LyXText * InsetText::getLyXText(BufferView const * bv,
1872 bool const recursive) const
1875 if (recursive && the_locking_inset)
1876 return the_locking_inset->getLyXText(bv, true);
1881 void InsetText::setViewCache(BufferView const * bv) const
1884 text_.bv_owner = const_cast<BufferView *>(bv);
1888 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1891 /// then remove all LyXText in text-insets
1892 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1893 const_cast<ParagraphList&>(paragraphs).end(),
1894 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1899 void InsetText::resizeLyXText(BufferView * bv, bool /*force*/) const
1901 if (paragraphs.size() == 1 && paragraphs.begin()->empty())
1910 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1911 const_cast<ParagraphList&>(paragraphs).end(),
1912 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1916 restoreLyXTextState();
1918 // seems to be unneeded
1920 if (the_locking_inset) {
1921 inset_x = cix() - top_x + drawTextXOffset;
1922 inset_y = ciy() + drawTextYOffset;
1925 text_.top_y(bv->screen().topCursorVisible(&text_));
1927 const_cast<InsetText*>(this)->updateLocal(bv, false);
1928 // this will scroll the screen such that the cursor becomes visible
1929 bv->updateScrollbar();
1931 need_update |= FULL;
1937 void InsetText::reinitLyXText() const
1939 BufferView * bv = text_.bv_owner;
1946 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1947 const_cast<ParagraphList&>(paragraphs).end(),
1948 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1951 restoreLyXTextState();
1952 if (the_locking_inset) {
1953 inset_x = cix() - top_x + drawTextXOffset;
1954 inset_y = ciy() + drawTextYOffset;
1956 text_.top_y(bv->screen().topCursorVisible(&text_));
1958 // this will scroll the screen such that the cursor becomes visible
1959 bv->updateScrollbar();
1966 void InsetText::removeNewlines()
1968 bool changed = false;
1970 ParagraphList::iterator it = paragraphs.begin();
1971 ParagraphList::iterator end = paragraphs.end();
1972 for (; it != end; ++it) {
1973 for (int i = 0; i < it->size(); ++i) {
1974 if (it->isNewline(i)) {
1985 int InsetText::scroll(bool recursive) const
1987 int sx = UpdatableInset::scroll(false);
1989 if (recursive && the_locking_inset)
1990 sx += the_locking_inset->scroll(recursive);
1996 void InsetText::clearSelection(BufferView * bv)
1998 getLyXText(bv)->clearSelection();
2002 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2004 Painter & pain = bv->painter();
2006 int h = dim_.asc + dim_.des;
2007 int ty = baseline - dim_.asc;
2013 if (ty + h > pain.paperHeight())
2014 h = pain.paperHeight();
2015 if (top_x + drawTextXOffset + w > pain.paperWidth())
2016 w = pain.paperWidth();
2017 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2022 ParagraphList * InsetText::getParagraphs(int i) const
2024 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2028 LyXCursor const & InsetText::cursor(BufferView * bv) const
2030 if (the_locking_inset)
2031 return the_locking_inset->cursor(bv);
2032 return getLyXText(bv)->cursor;
2036 InsetOld * InsetText::getInsetFromID(int id_arg) const
2039 return const_cast<InsetText *>(this);
2041 ParagraphList::const_iterator pit = paragraphs.begin();
2042 ParagraphList::const_iterator pend = paragraphs.end();
2043 for (; pit != pend; ++pit) {
2044 InsetList::const_iterator it = pit->insetlist.begin();
2045 InsetList::const_iterator end = pit->insetlist.end();
2046 for (; it != end; ++it) {
2047 if (it->inset->id() == id_arg)
2049 InsetOld * in = it->inset->getInsetFromID(id_arg);
2059 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2062 if (the_locking_inset) {
2063 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2064 if (!word.word().empty()) {
2068 // we have to go on checking so move cursor to the next char
2069 text_.cursor.pos(text_.cursor.pos() + 1);
2071 word = text_.selectNextWordToSpellcheck(value);
2072 if (word.word().empty())
2073 bv->unlockInset(const_cast<InsetText *>(this));
2080 void InsetText::selectSelectedWord(BufferView * bv)
2082 if (the_locking_inset) {
2083 the_locking_inset->selectSelectedWord(bv);
2086 getLyXText(bv)->selectSelectedWord();
2087 updateLocal(bv, false);
2091 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2093 if (the_locking_inset) {
2094 if (the_locking_inset->nextChange(bv, length))
2096 text_.cursorRight(true);
2098 lyx::find::SearchResult result =
2099 lyx::find::findNextChange(bv, &text_, length);
2101 if (result == lyx::find::SR_FOUND) {
2102 LyXCursor cur = text_.cursor;
2103 bv->unlockInset(bv->theLockingInset());
2104 if (bv->lockInset(this))
2107 text_.setSelectionRange(length);
2108 updateLocal(bv, false);
2110 return result != lyx::find::SR_NOT_FOUND;
2114 bool InsetText::searchForward(BufferView * bv, string const & str,
2117 if (the_locking_inset) {
2118 if (the_locking_inset->searchForward(bv, str, cs, mw))
2120 text_.cursorRight(true);
2122 lyx::find::SearchResult result =
2123 lyx::find::find(bv, &text_, str, true, cs, mw);
2125 if (result == lyx::find::SR_FOUND) {
2126 LyXCursor cur = text_.cursor;
2127 bv->unlockInset(bv->theLockingInset());
2128 if (bv->lockInset(this))
2131 text_.setSelectionRange(str.length());
2132 updateLocal(bv, false);
2134 return result != lyx::find::SR_NOT_FOUND;
2138 bool InsetText::searchBackward(BufferView * bv, string const & str,
2141 if (the_locking_inset) {
2142 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2146 ParagraphList::iterator pit = paragraphs.begin();
2147 ParagraphList::iterator pend = paragraphs.end();
2149 while (boost::next(pit) != pend)
2152 text_.setCursor(pit, pit->size());
2154 lyx::find::SearchResult result =
2155 lyx::find::find(bv, &text_, str, false, cs, mw);
2157 if (result == lyx::find::SR_FOUND) {
2158 LyXCursor cur = text_.cursor;
2159 bv->unlockInset(bv->theLockingInset());
2160 if (bv->lockInset(this))
2163 text_.setSelectionRange(str.length());
2164 updateLocal(bv, false);
2166 return result != lyx::find::SR_NOT_FOUND;
2170 bool InsetText::checkInsertChar(LyXFont & font)
2173 return owner()->checkInsertChar(font);
2178 void InsetText::collapseParagraphs(BufferView * bv)
2180 while (paragraphs.size() > 1) {
2181 ParagraphList::iterator first_par = paragraphs.begin();
2182 ParagraphList::iterator next_par = boost::next(first_par);
2183 size_t const first_par_size = first_par->size();
2185 if (!first_par->empty() &&
2186 !next_par->empty() &&
2187 !first_par->isSeparator(first_par_size - 1)) {
2188 first_par->insertChar(first_par_size, ' ');
2191 if (text_.selection.set()) {
2192 if (text_.selection.start.par() == next_par) {
2193 text_.selection.start.par(first_par);
2194 text_.selection.start.pos(
2195 text_.selection.start.pos() + first_par_size);
2197 if (text_.selection.end.par() == next_par) {
2198 text_.selection.end.par(first_par);
2199 text_.selection.end.pos(
2200 text_.selection.end.pos() + first_par_size);
2204 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2210 void InsetText::getDrawFont(LyXFont & font) const
2214 owner()->getDrawFont(font);
2218 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2220 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2221 // And it probably does. You have to take a look at this John. (Lgb)
2222 #warning John, have a look here. (Lgb)
2223 ParagraphList::iterator pit = plist.begin();
2224 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2226 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2228 ParagraphList::iterator pend = plist.end();
2229 for (; pit != pend; ++pit) {
2230 paragraphs.push_back(*pit);
2237 void InsetText::addPreview(PreviewLoader & loader) const
2239 ParagraphList::const_iterator pit = paragraphs.begin();
2240 ParagraphList::const_iterator pend = paragraphs.end();
2242 for (; pit != pend; ++pit) {
2243 InsetList::const_iterator it = pit->insetlist.begin();
2244 InsetList::const_iterator end = pit->insetlist.end();
2245 for (; it != end; ++it) {
2246 it->inset->addPreview(loader);