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 << "\n";
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, int what, bool mark_dirty)
403 if (!autoBreakRows && paragraphs.size() > 1)
404 collapseParagraphs(bv);
406 text_.partialRebreak();
407 setUpdateStatus(what);
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, CURSOR_PAR, 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 updateLocal(bv, code, false);
467 setUpdateStatus(code);
472 void InsetText::lockInset(BufferView * bv)
475 the_locking_inset = 0;
476 inset_pos = inset_x = inset_y = 0;
477 inset_boundary = false;
478 inset_par = paragraphs.end();
479 old_par = paragraphs.end();
480 text_.setCursorIntern(paragraphs.begin(), 0);
481 text_.clearSelection();
483 // If the inset is empty set the language of the current font to the
484 // language to the surronding text (if different).
485 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
486 bv->getParentLanguage(this) != text_.current_font.language()) {
487 LyXFont font(LyXFont::ALL_IGNORE);
488 font.setLanguage(bv->getParentLanguage(this));
489 setFont(bv, font, false);
492 if (drawFrame_ == LOCKED)
493 code = CURSOR|DRAW_FRAME;
494 setUpdateStatus(code);
498 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
500 the_locking_inset = inset;
501 inset_x = cix() - top_x + drawTextXOffset;
502 inset_y = ciy() + drawTextYOffset;
505 inset_boundary = cboundary();
506 //updateLocal(bv, CURSOR, false);
510 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
512 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
516 if (!the_locking_inset) {
517 ParagraphList::iterator pit = paragraphs.begin();
518 ParagraphList::iterator pend = paragraphs.end();
520 int const id = inset->id();
521 for (; pit != pend; ++pit) {
522 InsetList::iterator it = pit->insetlist.begin();
523 InsetList::iterator const end = pit->insetlist.end();
524 for (; it != end; ++it) {
525 if (it->inset == inset) {
526 lyxerr << "InsetText::lockInsetInInset: 1 a\n";
527 text_.setCursorIntern(pit, it->pos);
528 lyxerr << "InsetText::lockInsetInInset: 1 b\n";
529 lyxerr << "bv: " << bv << " inset: " << inset << "\n";
530 lockInset(bv, inset);
531 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
534 if (it->inset->getInsetFromID(id)) {
535 lyxerr << "InsetText::lockInsetInInset: 2\n";
536 text_.setCursorIntern(pit, it->pos);
537 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
538 return the_locking_inset->lockInsetInInset(bv, inset);
542 lyxerr << "InsetText::lockInsetInInset: 3\n";
545 if (inset == cpar()->getInset(cpos())) {
546 lyxerr[Debug::INSETS] << "OK" << endl;
547 lockInset(bv, inset);
551 if (the_locking_inset && the_locking_inset == inset) {
552 if (cpar() == inset_par && cpos() == inset_pos) {
553 lyxerr[Debug::INSETS] << "OK" << endl;
554 inset_x = cix() - top_x + drawTextXOffset;
555 inset_y = ciy() + drawTextYOffset;
557 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
559 } else if (the_locking_inset) {
560 lyxerr[Debug::INSETS] << "MAYBE" << endl;
561 return the_locking_inset->lockInsetInInset(bv, inset);
563 lyxerr[Debug::INSETS] << "NOT OK" << endl;
568 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
571 if (!the_locking_inset)
573 if (the_locking_inset == inset) {
574 the_locking_inset->insetUnlock(bv);
575 getLyXText(bv)->updateInset(inset);
576 the_locking_inset = 0;
578 moveRightIntern(bv, true, false);
579 old_par = paragraphs.end(); // force layout setting
583 updateLocal(bv, CURSOR, false);
586 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
590 bool InsetText::updateInsetInInset(BufferView * bv, InsetOld * inset)
592 if (!autoBreakRows && paragraphs.size() > 1)
593 collapseParagraphs(bv);
598 if (inset->owner() != this) {
599 int ustat = CURSOR_PAR;
601 UpdatableInset * tl_inset = the_locking_inset;
603 found = tl_inset->updateInsetInInset(bv, inset);
605 tl_inset = static_cast<UpdatableInset *>(inset);
606 while(tl_inset->owner() && tl_inset->owner() != this)
607 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
608 if (!tl_inset->owner())
610 found = tl_inset->updateInsetInInset(bv, inset);
613 text_.updateInset(tl_inset);
614 setUpdateStatus(ustat);
618 bool found = text_.updateInset(inset);
620 setUpdateStatus(CURSOR_PAR);
621 if (the_locking_inset &&
622 cpar() == inset_par && cpos() == inset_pos)
624 inset_x = cix() - top_x + drawTextXOffset;
625 inset_y = ciy() + drawTextYOffset;
632 void InsetText::lfunMousePress(FuncRequest const & cmd)
636 // use this to check mouse motion for selection!
640 BufferView * bv = cmd.view();
641 FuncRequest cmd1 = cmd;
647 int tmp_x = cmd.x - drawTextXOffset;
648 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
649 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
651 if (the_locking_inset) {
652 if (the_locking_inset == inset) {
653 the_locking_inset->localDispatch(cmd1);
656 // otherwise only unlock the_locking_inset
657 the_locking_inset->insetUnlock(bv);
658 the_locking_inset = 0;
661 no_selection = false;
663 if (bv->theLockingInset()) {
664 if (isHighlyEditableInset(inset)) {
665 // We just have to lock the inset before calling a
667 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
668 if (!bv->lockInset(uinset)) {
669 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
671 inset->localDispatch(cmd1);
672 if (the_locking_inset)
673 updateLocal(bv, CURSOR, false);
678 bool paste_internally = false;
679 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
680 localDispatch(FuncRequest(bv, LFUN_COPY));
681 paste_internally = true;
683 int old_top_y = text_.top_y();
685 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
687 // set the selection cursor!
688 text_.selection.cursor = text_.cursor;
689 text_.cursor.x_fix(text_.cursor.x());
691 if (text_.selection.set()) {
692 text_.clearSelection();
693 updateLocal(bv, FULL, false);
695 text_.clearSelection();
696 updateLocal(bv, CURSOR, false);
699 bv->owner()->setLayout(cpar()->layout()->name());
701 // we moved the view we cannot do mouse selection in this case!
702 if (getLyXText(bv)->top_y() != old_top_y)
705 // Insert primary selection with middle mouse
706 // if there is a local selection in the current buffer,
708 if (cmd.button() == mouse_button::button2) {
709 if (paste_internally)
710 localDispatch(FuncRequest(bv, LFUN_PASTE));
712 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
715 getLyXText(bv)->clearSelection();
720 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
722 BufferView * bv = cmd.view();
723 FuncRequest cmd1 = cmd;
728 if (the_locking_inset)
729 return the_locking_inset->localDispatch(cmd1);
731 int tmp_x = cmd.x - drawTextXOffset;
732 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
733 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
736 // This code should probably be removed now. Simple insets
737 // (!highlyEditable) can actually take the localDispatch,
738 // and turn it into edit() if necessary. But we still
739 // need to deal properly with the whole relative vs.
740 // absolute mouse co-ords thing in a realiable, sensible way
742 if (isHighlyEditableInset(inset))
743 ret = inset->localDispatch(cmd1);
745 inset_x = cix(bv) - top_x + drawTextXOffset;
746 inset_y = ciy() + drawTextYOffset;
747 cmd1.x = cmd.x - inset_x;
748 cmd1.y = cmd.x - inset_y;
749 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
753 ret = inset->localDispatch(cmd1);
754 updateLocal(bv, CURSOR_PAR, false);
761 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
763 FuncRequest cmd1 = cmd;
767 if (the_locking_inset) {
768 the_locking_inset->localDispatch(cmd1);
772 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
775 BufferView * bv = cmd.view();
776 LyXCursor cur = text_.cursor;
777 text_.setCursorFromCoordinates
778 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
779 text_.cursor.x_fix(text_.cursor.x());
780 if (cur == text_.cursor)
782 text_.setSelection();
783 updateLocal(bv, SELECTION, false);
787 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
789 BufferView * bv = cmd.view();
792 switch (cmd.action) {
793 case LFUN_INSET_EDIT: {
794 UpdatableInset::localDispatch(cmd);
796 if (!bv->lockInset(this)) {
797 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
802 the_locking_inset = 0;
806 inset_boundary = false;
807 inset_par = paragraphs.end();
808 old_par = paragraphs.end();
811 if (cmd.argument.size()) {
812 if (cmd.argument == "left")
813 text_.setCursorIntern(paragraphs.begin(), 0);
815 ParagraphList::iterator it = boost::prior(paragraphs.end());
816 text_.setCursor(it, it->size());
819 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
820 // we put here -1 and not button as now the button in the
821 // edit call should not be needed we will fix this in 1.3.x
822 // cycle hopefully (Jug 20020509)
823 // FIXME: GUII I've changed this to none: probably WRONG
824 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
825 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
827 text_.cursor.x_fix(text_.cursor.x());
831 text_.clearSelection();
834 // If the inset is empty set the language of the current font to the
835 // language to the surronding text (if different).
836 if (paragraphs.begin()->empty() &&
837 paragraphs.size() == 1 &&
838 bv->getParentLanguage(this) != text_.current_font.language())
840 LyXFont font(LyXFont::ALL_IGNORE);
841 font.setLanguage(bv->getParentLanguage(this));
842 setFont(bv, font, false);
846 if (drawFrame_ == LOCKED)
847 code = CURSOR | DRAW_FRAME;
849 updateLocal(bv, code, false);
850 // Tell the paragraph dialog that we've entered an insettext.
851 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
855 case LFUN_MOUSE_PRESS:
859 case LFUN_MOUSE_MOTION:
860 lfunMouseMotion(cmd);
863 case LFUN_MOUSE_RELEASE:
864 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
870 bool was_empty = (paragraphs.begin()->empty() &&
871 paragraphs.size() == 1);
873 no_selection = false;
874 RESULT result = UpdatableInset::localDispatch(cmd);
875 if (result != UNDISPATCHED)
879 if (cmd.action < 0 && cmd.argument.empty())
882 if (the_locking_inset) {
883 result = the_locking_inset->localDispatch(cmd);
884 if (result == DISPATCHED_NOUPDATE)
886 else if (result == DISPATCHED) {
887 updateLocal(bv, CURSOR_PAR, false);
889 } else if (result >= FINISHED) {
892 moveRightIntern(bv, false, false);
896 if ((result = moveUp(bv)) >= FINISHED) {
897 updateLocal(bv, CURSOR, false);
898 bv->unlockInset(this);
902 if ((result = moveDown(bv)) >= FINISHED) {
903 updateLocal(bv, CURSOR, false);
904 bv->unlockInset(this);
911 the_locking_inset = 0;
912 updateLocal(bv, CURSOR, false);
913 // make sure status gets reset immediately
914 bv->owner()->clearMessage();
921 // what type of update to do on a cursor movement
922 int cursor_update = CURSOR;
924 if (text_.selection.set())
925 cursor_update = SELECTION;
927 switch (cmd.action) {
930 case LFUN_SELFINSERT:
931 if (bv->buffer()->isReadonly()) {
932 // setErrorMessage(N_("Document is read only"));
935 if (!cmd.argument.empty()) {
936 /* Automatically delete the currently selected
937 * text and replace it with what is being
938 * typed in now. Depends on lyxrc settings
939 * "auto_region_delete", which defaults to
942 // This should not be needed here and is also WRONG!
943 recordUndo(bv, Undo::INSERT, text_.cursor.par());
946 if (lyxrc.auto_region_delete) {
947 if (text_.selection.set()) {
948 text_.cutSelection(false, false);
951 text_.clearSelection();
952 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
953 bv->owner()->getIntl().getTransManager().
954 TranslateAndInsert(cmd.argument[i], &text_);
957 text_.selection.cursor = text_.cursor;
958 updwhat = CURSOR | CURSOR_PAR;
960 result = DISPATCHED_NOUPDATE;
963 // cursor movements that need special handling
966 result = moveRight(bv);
968 updwhat = cursor_update;
972 result = moveLeft(bv);
973 updwhat = cursor_update;
977 result = moveDown(bv);
978 updwhat = cursor_update;
983 updwhat = cursor_update;
987 if (crow() == text_.rows().begin())
988 result = FINISHED_UP;
990 text_.cursorPrevious();
991 text_.clearSelection();
992 result = DISPATCHED_NOUPDATE;
994 updwhat = cursor_update;
998 if (boost::next(crow()) == text_.rows().end())
999 result = FINISHED_DOWN;
1002 text_.clearSelection();
1003 result = DISPATCHED_NOUPDATE;
1005 updwhat = cursor_update;
1008 case LFUN_BACKSPACE: {
1009 if (text_.selection.set())
1010 text_.cutSelection(true, false);
1013 updwhat = CURSOR_PAR;
1019 if (text_.selection.set()) {
1020 text_.cutSelection(true, false);
1024 updwhat = CURSOR_PAR;
1030 text_.cutSelection(true, true);
1031 updwhat = CURSOR_PAR;
1038 text_.copySelection();
1039 updwhat = CURSOR_PAR;
1042 case LFUN_PASTESELECTION:
1044 string const clip(bv->getClipboard());
1048 if (cmd.argument == "paragraph") {
1049 text_.insertStringAsParagraphs(clip);
1051 text_.insertStringAsLines(clip);
1054 text_.clearSelection();
1056 updwhat = CURSOR_PAR;
1062 if (!autoBreakRows) {
1063 if (CutAndPaste::nrOfParagraphs() > 1) {
1064 #ifdef WITH_WARNINGS
1065 #warning FIXME horrendously bad UI
1067 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1072 replaceSelection(bv->getLyXText());
1073 size_t sel_index = 0;
1074 string const & arg = cmd.argument;
1075 if (isStrUnsignedInt(arg)) {
1076 size_t const paste_arg = strToUnsignedInt(arg);
1077 #warning FIXME Check if the arg is in the domain of available selections.
1078 sel_index = paste_arg;
1080 text_.pasteSelection(sel_index);
1082 text_.clearSelection();
1083 updwhat = CURSOR_PAR;
1088 case LFUN_BREAKPARAGRAPH:
1089 if (!autoBreakRows) {
1090 result = DISPATCHED;
1093 replaceSelection(bv->getLyXText());
1094 text_.breakParagraph(paragraphs, 0);
1095 updwhat = CURSOR | FULL;
1099 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1100 if (!autoBreakRows) {
1101 result = DISPATCHED;
1104 replaceSelection(bv->getLyXText());
1105 text_.breakParagraph(paragraphs, 1);
1106 updwhat = CURSOR | FULL;
1110 case LFUN_BREAKLINE: {
1111 if (!autoBreakRows) {
1112 result = DISPATCHED;
1116 replaceSelection(bv->getLyXText());
1117 text_.insertInset(new InsetNewline);
1118 updwhat = CURSOR | CURSOR_PAR;
1124 // do not set layouts on non breakable textinsets
1125 if (autoBreakRows) {
1126 string cur_layout = cpar()->layout()->name();
1128 // Derive layout number from given argument (string)
1129 // and current buffer's textclass (number). */
1130 LyXTextClass const & tclass =
1131 bv->buffer()->params.getLyXTextClass();
1132 string layout = cmd.argument;
1133 bool hasLayout = tclass.hasLayout(layout);
1135 // If the entry is obsolete, use the new one instead.
1137 string const & obs =
1138 tclass[layout]->obsoleted_by();
1143 // see if we found the layout number:
1145 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1146 bv->owner()->dispatch(lf);
1150 if (cur_layout != layout) {
1151 cur_layout = layout;
1152 text_.setLayout(layout);
1153 bv->owner()->setLayout(cpar()->layout()->name());
1154 updwhat = CURSOR_PAR;
1158 // reset the layout box
1159 bv->owner()->setLayout(cpar()->layout()->name());
1162 case LFUN_PARAGRAPH_SPACING:
1163 // This one is absolutely not working. When fiddling with this
1164 // it also seems to me that the paragraphs inside the insettext
1165 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1166 // FIXME: how old is this comment ? ...
1168 ParagraphList::iterator pit = text_.cursor.par();
1169 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1170 float cur_value = 1.0;
1171 if (cur_spacing == Spacing::Other) {
1172 cur_value = pit->params().spacing().getValue();
1175 istringstream istr(STRCONV(cmd.argument));
1178 Spacing::Space new_spacing = cur_spacing;
1179 float new_value = cur_value;
1181 lyxerr << "Missing argument to `paragraph-spacing'"
1183 } else if (tmp == "single") {
1184 new_spacing = Spacing::Single;
1185 } else if (tmp == "onehalf") {
1186 new_spacing = Spacing::Onehalf;
1187 } else if (tmp == "double") {
1188 new_spacing = Spacing::Double;
1189 } else if (tmp == "other") {
1190 new_spacing = Spacing::Other;
1193 lyxerr << "new_value = " << tmpval << endl;
1196 } else if (tmp == "default") {
1197 new_spacing = Spacing::Default;
1199 lyxerr << _("Unknown spacing argument: ")
1200 << cmd.argument << endl;
1202 if (cur_spacing != new_spacing || cur_value != new_value) {
1203 pit->params().spacing(Spacing(new_spacing, new_value));
1204 updwhat = CURSOR_PAR;
1210 // These need to do repaints but don't require
1211 // special handling otherwise. A *lot* of the
1212 // above could probably be done similarly ...
1217 case LFUN_WORDRIGHT:
1218 // these two are really unhandled ...
1220 case LFUN_BEGINNINGBUF:
1221 updwhat = cursor_update;
1222 if (!bv->dispatch(cmd))
1223 result = UNDISPATCHED;
1232 case LFUN_WORDLEFTSEL:
1233 case LFUN_WORDRIGHTSEL:
1234 updwhat = SELECTION;
1239 if (!bv->dispatch(cmd))
1240 result = UNDISPATCHED;
1245 updateLocal(bv, updwhat, updflag);
1246 /// If the action has deleted all text in the inset, we need to change the
1247 // language to the language of the surronding text.
1248 if (!was_empty && paragraphs.begin()->empty() &&
1249 paragraphs.size() == 1) {
1250 LyXFont font(LyXFont::ALL_IGNORE);
1251 font.setLanguage(bv->getParentLanguage(this));
1252 setFont(bv, font, false);
1255 if (result >= FINISHED)
1256 bv->unlockInset(this);
1258 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1259 result = DISPATCHED;
1264 int InsetText::latex(Buffer const * buf, ostream & os,
1265 LatexRunParams const & runparams) const
1268 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1269 return texrow.rows();
1273 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1275 unsigned int lines = 0;
1277 ParagraphList::const_iterator beg = paragraphs.begin();
1278 ParagraphList::const_iterator end = paragraphs.end();
1279 ParagraphList::const_iterator it = beg;
1280 for (; it != end; ++it) {
1281 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1282 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1289 int InsetText::linuxdoc(Buffer const * buf, ostream & os) const
1291 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1292 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1294 // There is a confusion between the empty paragraph and the default paragraph
1295 // The default paragraph is <p></p>, the empty paragraph is *empty*
1296 // Since none of the floats of linuxdoc accepts standard paragraphs
1297 // I disable them. I don't expect problems. (jamatos 2003/07/27)
1298 for (; pit != pend; ++pit) {
1299 const string name = pit->layout()->latexname();
1301 sgml::openTag(os, 1, 0, name);
1302 buf->simpleLinuxDocOnePar(os, pit, 0);
1304 sgml::closeTag(os, 1, 0, name);
1310 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1312 unsigned int lines = 0;
1314 vector<string> environment_stack(10);
1315 vector<string> environment_inner(10);
1317 int const command_depth = 0;
1320 Paragraph::depth_type depth = 0; // paragraph depth
1322 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1323 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1325 for (; pit != pend; ++pit) {
1327 int desc_on = 0; // description mode
1329 LyXLayout_ptr const & style = pit->layout();
1331 // environment tag closing
1332 for (; depth > pit->params().depth(); --depth) {
1333 if (environment_inner[depth] != "!-- --") {
1334 item_name = "listitem";
1335 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1336 if (environment_inner[depth] == "varlistentry")
1337 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1339 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1340 environment_stack[depth].erase();
1341 environment_inner[depth].erase();
1344 if (depth == pit->params().depth()
1345 && environment_stack[depth] != style->latexname()
1346 && !environment_stack[depth].empty()) {
1347 if (environment_inner[depth] != "!-- --") {
1348 item_name= "listitem";
1349 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1350 if (environment_inner[depth] == "varlistentry")
1351 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1354 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1356 environment_stack[depth].erase();
1357 environment_inner[depth].erase();
1360 // Write opening SGML tags.
1361 switch (style->latextype) {
1362 case LATEX_PARAGRAPH:
1363 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1367 buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1371 case LATEX_ENVIRONMENT:
1372 case LATEX_ITEM_ENVIRONMENT:
1373 if (depth < pit->params().depth()) {
1374 depth = pit->params().depth();
1375 environment_stack[depth].erase();
1378 if (environment_stack[depth] != style->latexname()) {
1379 if (environment_stack.size() == depth + 1) {
1380 environment_stack.push_back("!-- --");
1381 environment_inner.push_back("!-- --");
1383 environment_stack[depth] = style->latexname();
1384 environment_inner[depth] = "!-- --";
1385 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1387 if (environment_inner[depth] != "!-- --") {
1388 item_name= "listitem";
1389 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1390 if (environment_inner[depth] == "varlistentry")
1391 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1395 if (style->latextype == LATEX_ENVIRONMENT) {
1396 if (!style->latexparam().empty()) {
1397 if (style->latexparam() == "CDATA")
1400 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1405 desc_on = (style->labeltype == LABEL_MANUAL);
1407 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1408 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1410 item_name = desc_on ? "term" : "para";
1411 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1415 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1419 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1422 // write closing SGML tags
1423 switch (style->latextype) {
1424 case LATEX_ENVIRONMENT:
1425 if (!style->latexparam().empty()) {
1426 if (style->latexparam() == "CDATA")
1429 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1432 case LATEX_ITEM_ENVIRONMENT:
1433 if (desc_on == 1) break;
1435 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1437 case LATEX_PARAGRAPH:
1438 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1441 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1447 for (int d = depth; d >= 0; --d) {
1448 if (!environment_stack[depth].empty()) {
1449 if (environment_inner[depth] != "!-- --") {
1450 item_name = "listitem";
1451 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1452 if (environment_inner[depth] == "varlistentry")
1453 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1456 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1464 void InsetText::validate(LaTeXFeatures & features) const
1466 for_each(paragraphs.begin(), paragraphs.end(),
1467 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1471 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1473 if (the_locking_inset) {
1474 the_locking_inset->getCursor(bv, x, y);
1478 y = cy() + InsetText::y();
1482 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1484 if (the_locking_inset) {
1485 the_locking_inset->getCursorPos(bv, x, y);
1488 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1489 y = cy() - TEXT_TO_INSET_OFFSET;
1493 int InsetText::insetInInsetY() const
1495 if (!the_locking_inset)
1498 return inset_y + the_locking_inset->insetInInsetY();
1502 void InsetText::fitInsetCursor(BufferView * bv) const
1504 if (the_locking_inset) {
1505 the_locking_inset->fitInsetCursor(bv);
1509 LyXFont const font = text_.getFont(cpar(), cpos());
1511 int const asc = font_metrics::maxAscent(font);
1512 int const desc = font_metrics::maxDescent(font);
1514 if (bv->fitLockedInsetCursor(cx(), cy(), asc, desc))
1515 need_update |= FULL;
1520 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1522 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1523 return moveLeftIntern(bv, false, activate_inset, selecting);
1525 return moveRightIntern(bv, true, activate_inset, selecting);
1530 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1532 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1533 return moveRightIntern(bv, true, activate_inset, selecting);
1535 return moveLeftIntern(bv, false, activate_inset, selecting);
1540 InsetText::moveRightIntern(BufferView * bv, bool front,
1541 bool activate_inset, bool selecting)
1543 ParagraphList::iterator c_par = cpar();
1545 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1546 return FINISHED_RIGHT;
1547 if (activate_inset && checkAndActivateInset(bv, front))
1549 text_.cursorRight(bv);
1551 text_.clearSelection();
1552 return DISPATCHED_NOUPDATE;
1557 InsetText::moveLeftIntern(BufferView * bv, bool front,
1558 bool activate_inset, bool selecting)
1560 if (cpar() == paragraphs.begin() && cpos() <= 0)
1562 text_.cursorLeft(bv);
1564 text_.clearSelection();
1565 if (activate_inset && checkAndActivateInset(bv, front))
1567 return DISPATCHED_NOUPDATE;
1571 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1573 if (crow() == text_.rows().begin())
1576 text_.clearSelection();
1577 return DISPATCHED_NOUPDATE;
1581 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1583 if (boost::next(crow()) == text_.rows().end())
1584 return FINISHED_DOWN;
1585 text_.cursorDown(bv);
1586 text_.clearSelection();
1587 return DISPATCHED_NOUPDATE;
1591 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1593 if (the_locking_inset) {
1594 if (the_locking_inset->insetAllowed(inset))
1595 return the_locking_inset->insertInset(bv, inset);
1598 inset->setOwner(this);
1599 text_.insertInset(inset);
1601 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1606 bool InsetText::insetAllowed(InsetOld::Code code) const
1608 // in_insetAllowed is a really gross hack,
1609 // to allow us to call the owner's insetAllowed
1610 // without stack overflow, which can happen
1611 // when the owner uses InsetCollapsable::insetAllowed()
1613 if (in_insetAllowed)
1615 in_insetAllowed = true;
1616 if (the_locking_inset)
1617 ret = the_locking_inset->insetAllowed(code);
1619 ret = owner()->insetAllowed(code);
1620 in_insetAllowed = false;
1625 UpdatableInset * InsetText::getLockingInset() const
1627 return the_locking_inset ? the_locking_inset->getLockingInset() :
1628 const_cast<InsetText *>(this);
1632 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1636 if (the_locking_inset)
1637 return the_locking_inset->getFirstLockingInsetOfType(c);
1642 bool InsetText::showInsetDialog(BufferView * bv) const
1644 if (the_locking_inset)
1645 return the_locking_inset->showInsetDialog(bv);
1650 void InsetText::getLabelList(std::vector<string> & list) const
1652 ParagraphList::const_iterator pit = paragraphs.begin();
1653 ParagraphList::const_iterator pend = paragraphs.end();
1654 for (; pit != pend; ++pit) {
1655 InsetList::const_iterator beg = pit->insetlist.begin();
1656 InsetList::const_iterator end = pit->insetlist.end();
1657 for (; beg != end; ++beg)
1658 beg->inset->getLabelList(list);
1663 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1666 if (the_locking_inset) {
1667 the_locking_inset->setFont(bv, font, toggleall, selectall);
1671 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1672 || cpar()->empty()) {
1673 text_.setFont(font, toggleall);
1678 if (text_.selection.set())
1679 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1683 text_.selection.cursor = text_.cursor;
1684 text_.cursorBottom();
1685 text_.setSelection();
1688 text_.toggleFree(font, toggleall);
1691 text_.clearSelection();
1695 bool flag = (selectall || text_.selection.set());
1698 updateLocal(bv, FULL, true);
1700 updateLocal(bv, CURSOR_PAR, true);
1704 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1706 InsetOld * inset = cpar()->getInset(cpos());
1707 if (!isHighlyEditableInset(inset))
1709 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1710 inset->localDispatch(cmd);
1711 if (!the_locking_inset)
1713 updateLocal(bv, CURSOR, false);
1718 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1719 mouse_button::state button)
1721 x -= drawTextXOffset;
1723 int dummyy = y + dim_.asc;
1724 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1725 // we only do the edit() call if the inset was hit by the mouse
1726 // or if it is a highly editable inset. So we should call this
1727 // function from our own edit with button < 0.
1728 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1730 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1739 inset_x = cix() - top_x + drawTextXOffset;
1740 inset_y = ciy() + drawTextYOffset;
1741 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1742 inset->localDispatch(cmd);
1743 if (!the_locking_inset)
1745 updateLocal(bv, CURSOR, false);
1750 void InsetText::setParagraphData(ParagraphList const & plist)
1752 // we have to unlock any locked inset otherwise we're in troubles
1753 the_locking_inset = 0;
1755 // But it it makes no difference that is a lot better.
1757 // See if this can be simplified when std::list is in effect.
1760 ParagraphList::const_iterator it = plist.begin();
1761 ParagraphList::const_iterator end = plist.end();
1762 for (; it != end; ++it) {
1763 paragraphs.push_back(*it);
1764 paragraphs.back().setInsetOwner(this);
1772 void InsetText::markNew(bool track_changes)
1774 ParagraphList::iterator pit = paragraphs.begin();
1775 ParagraphList::iterator pend = paragraphs.end();
1776 for (; pit != pend; ++pit) {
1777 if (track_changes) {
1778 pit->trackChanges();
1780 // no-op when not tracking
1781 pit->cleanChanges();
1787 void InsetText::setText(string const & data, LyXFont const & font)
1790 for (unsigned int i = 0; i < data.length(); ++i)
1791 paragraphs.begin()->insertChar(i, data[i], font);
1796 void InsetText::setAutoBreakRows(bool flag)
1798 if (flag != autoBreakRows) {
1799 autoBreakRows = flag;
1807 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1809 if (how != drawFrame_) {
1812 updateLocal(bv, DRAW_FRAME, false);
1817 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1819 if (frame_color != col) {
1822 updateLocal(bv, DRAW_FRAME, false);
1827 int InsetText::cx() const
1829 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1830 if (the_locking_inset) {
1831 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1832 if (font.isVisibleRightToLeft())
1833 x -= the_locking_inset->width();
1839 int InsetText::cix() const
1841 int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1842 if (the_locking_inset) {
1843 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1844 if (font.isVisibleRightToLeft())
1845 x -= the_locking_inset->width();
1851 int InsetText::cy() const
1853 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1857 int InsetText::ciy() const
1859 return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1863 pos_type InsetText::cpos() const
1865 return text_.cursor.pos();
1869 ParagraphList::iterator InsetText::cpar() const
1871 return text_.cursor.par();
1875 bool InsetText::cboundary() const
1877 return text_.cursor.boundary();
1881 RowList::iterator InsetText::crow() const
1883 return text_.cursorRow();
1887 LyXText * InsetText::getLyXText(BufferView const * bv,
1888 bool const recursive) const
1891 if (recursive && the_locking_inset)
1892 return the_locking_inset->getLyXText(bv, true);
1897 void InsetText::setViewCache(BufferView const * bv) const
1900 text_.bv_owner = const_cast<BufferView *>(bv);
1904 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1907 /// then remove all LyXText in text-insets
1908 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1909 const_cast<ParagraphList&>(paragraphs).end(),
1910 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1915 void InsetText::resizeLyXText(BufferView * bv, bool /*force*/) const
1917 if (paragraphs.size() == 1 && paragraphs.begin()->empty())
1926 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1927 const_cast<ParagraphList&>(paragraphs).end(),
1928 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1932 restoreLyXTextState();
1934 // seems to be unneeded
1936 if (the_locking_inset) {
1937 inset_x = cix() - top_x + drawTextXOffset;
1938 inset_y = ciy() + drawTextYOffset;
1941 text_.top_y(bv->screen().topCursorVisible(&text_));
1943 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
1944 // this will scroll the screen such that the cursor becomes visible
1945 bv->updateScrollbar();
1947 need_update |= FULL;
1953 void InsetText::reinitLyXText() const
1955 BufferView * bv = text_.bv_owner;
1962 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1963 const_cast<ParagraphList&>(paragraphs).end(),
1964 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1967 restoreLyXTextState();
1968 if (the_locking_inset) {
1969 inset_x = cix() - top_x + drawTextXOffset;
1970 inset_y = ciy() + drawTextYOffset;
1972 text_.top_y(bv->screen().topCursorVisible(&text_));
1974 // this will scroll the screen such that the cursor becomes visible
1975 bv->updateScrollbar();
1982 void InsetText::removeNewlines()
1984 bool changed = false;
1986 ParagraphList::iterator it = paragraphs.begin();
1987 ParagraphList::iterator end = paragraphs.end();
1988 for (; it != end; ++it) {
1989 for (int i = 0; i < it->size(); ++i) {
1990 if (it->isNewline(i)) {
2001 int InsetText::scroll(bool recursive) const
2003 int sx = UpdatableInset::scroll(false);
2005 if (recursive && the_locking_inset)
2006 sx += the_locking_inset->scroll(recursive);
2012 void InsetText::clearSelection(BufferView * bv)
2014 getLyXText(bv)->clearSelection();
2018 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2020 Painter & pain = bv->painter();
2022 int h = dim_.asc + dim_.des;
2023 int ty = baseline - dim_.asc;
2029 if (ty + h > pain.paperHeight())
2030 h = pain.paperHeight();
2031 if (top_x + drawTextXOffset + w > pain.paperWidth())
2032 w = pain.paperWidth();
2033 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2038 ParagraphList * InsetText::getParagraphs(int i) const
2040 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2044 LyXCursor const & InsetText::cursor(BufferView * bv) const
2046 if (the_locking_inset)
2047 return the_locking_inset->cursor(bv);
2048 return getLyXText(bv)->cursor;
2052 InsetOld * InsetText::getInsetFromID(int id_arg) const
2055 return const_cast<InsetText *>(this);
2057 ParagraphList::const_iterator pit = paragraphs.begin();
2058 ParagraphList::const_iterator pend = paragraphs.end();
2059 for (; pit != pend; ++pit) {
2060 InsetList::const_iterator it = pit->insetlist.begin();
2061 InsetList::const_iterator end = pit->insetlist.end();
2062 for (; it != end; ++it) {
2063 if (it->inset->id() == id_arg)
2065 InsetOld * in = it->inset->getInsetFromID(id_arg);
2075 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2078 if (the_locking_inset) {
2079 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2080 if (!word.word().empty()) {
2084 // we have to go on checking so move cursor to the next char
2085 text_.cursor.pos(text_.cursor.pos() + 1);
2087 word = text_.selectNextWordToSpellcheck(value);
2088 if (word.word().empty())
2089 bv->unlockInset(const_cast<InsetText *>(this));
2096 void InsetText::selectSelectedWord(BufferView * bv)
2098 if (the_locking_inset) {
2099 the_locking_inset->selectSelectedWord(bv);
2102 getLyXText(bv)->selectSelectedWord();
2103 updateLocal(bv, SELECTION, false);
2107 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2109 if (the_locking_inset) {
2110 if (the_locking_inset->nextChange(bv, length))
2112 text_.cursorRight(true);
2114 lyx::find::SearchResult result =
2115 lyx::find::findNextChange(bv, &text_, length);
2117 if (result == lyx::find::SR_FOUND) {
2118 LyXCursor cur = text_.cursor;
2119 bv->unlockInset(bv->theLockingInset());
2120 if (bv->lockInset(this))
2123 text_.setSelectionRange(length);
2124 updateLocal(bv, SELECTION, false);
2126 return result != lyx::find::SR_NOT_FOUND;
2130 bool InsetText::searchForward(BufferView * bv, string const & str,
2133 if (the_locking_inset) {
2134 if (the_locking_inset->searchForward(bv, str, cs, mw))
2136 text_.cursorRight(true);
2138 lyx::find::SearchResult result =
2139 lyx::find::find(bv, &text_, str, true, cs, mw);
2141 if (result == lyx::find::SR_FOUND) {
2142 LyXCursor cur = text_.cursor;
2143 bv->unlockInset(bv->theLockingInset());
2144 if (bv->lockInset(this))
2147 text_.setSelectionRange(str.length());
2148 updateLocal(bv, SELECTION, false);
2150 return result != lyx::find::SR_NOT_FOUND;
2154 bool InsetText::searchBackward(BufferView * bv, string const & str,
2157 if (the_locking_inset) {
2158 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2162 ParagraphList::iterator pit = paragraphs.begin();
2163 ParagraphList::iterator pend = paragraphs.end();
2165 while (boost::next(pit) != pend)
2168 text_.setCursor(pit, pit->size());
2170 lyx::find::SearchResult result =
2171 lyx::find::find(bv, &text_, str, false, cs, mw);
2173 if (result == lyx::find::SR_FOUND) {
2174 LyXCursor cur = text_.cursor;
2175 bv->unlockInset(bv->theLockingInset());
2176 if (bv->lockInset(this))
2179 text_.setSelectionRange(str.length());
2180 updateLocal(bv, SELECTION, false);
2182 return result != lyx::find::SR_NOT_FOUND;
2186 bool InsetText::checkInsertChar(LyXFont & font)
2189 return owner()->checkInsertChar(font);
2194 void InsetText::collapseParagraphs(BufferView * bv)
2196 while (paragraphs.size() > 1) {
2197 ParagraphList::iterator first_par = paragraphs.begin();
2198 ParagraphList::iterator next_par = boost::next(first_par);
2199 size_t const first_par_size = first_par->size();
2201 if (!first_par->empty() &&
2202 !next_par->empty() &&
2203 !first_par->isSeparator(first_par_size - 1)) {
2204 first_par->insertChar(first_par_size, ' ');
2207 if (text_.selection.set()) {
2208 if (text_.selection.start.par() == next_par) {
2209 text_.selection.start.par(first_par);
2210 text_.selection.start.pos(
2211 text_.selection.start.pos() + first_par_size);
2213 if (text_.selection.end.par() == next_par) {
2214 text_.selection.end.par(first_par);
2215 text_.selection.end.pos(
2216 text_.selection.end.pos() + first_par_size);
2220 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2226 void InsetText::getDrawFont(LyXFont & font) const
2230 owner()->getDrawFont(font);
2234 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2236 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2237 // And it probably does. You have to take a look at this John. (Lgb)
2238 #warning John, have a look here. (Lgb)
2239 ParagraphList::iterator pit = plist.begin();
2240 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2242 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2244 ParagraphList::iterator pend = plist.end();
2245 for (; pit != pend; ++pit) {
2246 paragraphs.push_back(*pit);
2253 void InsetText::addPreview(PreviewLoader & loader) const
2255 ParagraphList::const_iterator pit = paragraphs.begin();
2256 ParagraphList::const_iterator pend = paragraphs.end();
2258 for (; pit != pend; ++pit) {
2259 InsetList::const_iterator it = pit->insetlist.begin();
2260 InsetList::const_iterator end = pit->insetlist.end();
2261 for (; it != end; ++it) {
2262 it->inset->addPreview(loader);