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;
82 // These functions should probably go into bufferview_funcs somehow (Jug)
84 void InsetText::saveLyXTextState() const
86 // check if my paragraphs are still valid
87 ParagraphList::iterator it = const_cast<ParagraphList&>(paragraphs).begin();
88 ParagraphList::iterator end = const_cast<ParagraphList&>(paragraphs).end();
89 for (; it != end; ++it) {
90 if (it == text_.cursor.par())
94 if (it != end && text_.cursor.pos() <= it->size())
95 sstate = text_; // slicing intended
97 sstate.cursor.par(end);
101 void InsetText::restoreLyXTextState() const
103 if (sstate.cursor.par() == const_cast<ParagraphList&>(paragraphs).end())
106 text_.selection.set(true);
107 // at this point just to avoid the DEPM when setting the cursor
108 text_.selection.mark(sstate.selection.mark());
109 if (sstate.selection.set()) {
110 text_.setCursor(sstate.selection.start.par(),
111 sstate.selection.start.pos(),
112 true, sstate.selection.start.boundary());
113 text_.selection.cursor = text_.cursor;
114 text_.setCursor(sstate.selection.end.par(), sstate.selection.end.pos(),
115 true, sstate.selection.end.boundary());
116 text_.setSelection();
117 text_.setCursor(sstate.cursor.par(), sstate.cursor.pos());
119 text_.setCursor(sstate.cursor.par(), sstate.cursor.pos(),
120 true, sstate.cursor.boundary());
121 text_.selection.cursor = text_.cursor;
122 text_.selection.set(false);
127 InsetText::InsetText(BufferParams const & bp)
128 : UpdatableInset(), text_(0, this)
130 paragraphs.push_back(Paragraph());
131 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
132 if (bp.tracking_changes)
133 paragraphs.begin()->trackChanges();
138 InsetText::InsetText(InsetText const & in)
139 : UpdatableInset(in), text_(0, this)
145 InsetText & InsetText::operator=(InsetText const & it)
152 void InsetText::init(InsetText const * ins)
155 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; // unbounded
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
304 // update our idea of where we are. Clearly, we should
305 // not have to know this information.
308 int const start_x = x;
310 BufferView * bv = pi.base.bv;
311 Painter & pain = pi.pain;
313 // repaint the background if needed
314 if (backgroundColor() != LColor::background)
315 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
317 // no draw is necessary !!!
318 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
319 top_baseline = baseline;
329 top_baseline = baseline;
330 top_y = baseline - dim_.asc;
332 if (last_drawn_width != dim_.wid) {
334 last_drawn_width = dim_.wid;
337 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
338 inset_x = cix() - x + drawTextXOffset;
339 inset_y = ciy() + drawTextYOffset;
342 x += TEXT_TO_INSET_OFFSET;
344 RowList::iterator rit = text_.rows().begin();
345 RowList::iterator end = text_.rows().end();
347 int y_offset = baseline - rit->ascent_of_text();
348 int y2 = pain.paperHeight();
351 while (rit != end && y + rit->height() <= 0) {
353 first += rit->height();
357 text_.top_y(-y_offset);
365 int yo = y_offset + first;
367 paintRows(*bv, text_, rit, x, 0, yo, y2, yo);
371 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
372 drawFrame(pain, int(start_x));
374 if (need_update != INIT)
379 void InsetText::drawFrame(Painter & pain, int x) const
381 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
383 frame_y = top_baseline - dim_.asc + ttoD2;
384 frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
385 frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
386 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
391 void InsetText::setUpdateStatus(int what) const
394 // we will to redraw us full if our LyXText wants it
395 if (text_.needRefresh())
398 // this to not draw a selection when we redraw all of it!
399 if (need_update & CURSOR && !(need_update & SELECTION)) {
400 if (text_.selection.set())
402 text_.clearSelection();
407 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
412 if (!autoBreakRows && paragraphs.size() > 1)
413 collapseParagraphs(bv);
415 text_.partialRebreak();
416 setUpdateStatus(what);
417 bool flag = mark_dirty ||
418 ((need_update != CURSOR && need_update != NONE) ||
419 text_.needRefresh() || text_.selection.set());
420 if (!text_.selection.set())
421 text_.selection.cursor = text_.cursor;
427 bv->updateInset(const_cast<InsetText *>(this));
430 if (need_update == CURSOR)
432 bv->owner()->view_state_changed();
433 bv->owner()->updateMenubar();
434 bv->owner()->updateToolbar();
435 if (old_par != cpar()) {
436 bv->owner()->setLayout(cpar()->layout()->name());
442 string const InsetText::editMessage() const
444 return _("Opened Text Inset");
448 void InsetText::insetUnlock(BufferView * bv)
450 if (the_locking_inset) {
451 the_locking_inset->insetUnlock(bv);
452 the_locking_inset = 0;
453 updateLocal(bv, CURSOR_PAR, false);
459 if (text_.selection.set()) {
460 text_.clearSelection();
462 } else if (owner()) {
463 bv->owner()->setLayout(owner()->getLyXText(bv)
464 ->cursor.par()->layout()->name());
466 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
467 // hack for deleteEmptyParMech
468 ParagraphList::iterator first_par = paragraphs.begin();
469 if (!first_par->empty()) {
470 text_.setCursor(first_par, 0);
471 } else if (paragraphs.size() > 1) {
472 text_.setCursor(boost::next(first_par), 0);
475 updateLocal(bv, code, false);
478 setUpdateStatus(code);
483 void InsetText::lockInset(BufferView * bv)
486 the_locking_inset = 0;
487 inset_pos = inset_x = inset_y = 0;
488 inset_boundary = false;
489 inset_par = paragraphs.end();
490 old_par = paragraphs.end();
491 text_.setCursorIntern(paragraphs.begin(), 0);
492 text_.clearSelection();
494 // If the inset is empty set the language of the current font to the
495 // language to the surronding text (if different).
496 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
497 bv->getParentLanguage(this) != text_.current_font.language()) {
498 LyXFont font(LyXFont::ALL_IGNORE);
499 font.setLanguage(bv->getParentLanguage(this));
500 setFont(bv, font, false);
503 if (drawFrame_ == LOCKED)
504 code = CURSOR|DRAW_FRAME;
505 setUpdateStatus(code);
509 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
511 the_locking_inset = inset;
512 inset_x = cix() - top_x + drawTextXOffset;
513 inset_y = ciy() + drawTextYOffset;
516 inset_boundary = cboundary();
517 //updateLocal(bv, CURSOR, false);
521 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
523 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
527 if (!the_locking_inset) {
528 ParagraphList::iterator pit = paragraphs.begin();
529 ParagraphList::iterator pend = paragraphs.end();
531 int const id = inset->id();
532 for (; pit != pend; ++pit) {
533 InsetList::iterator it = pit->insetlist.begin();
534 InsetList::iterator const end = pit->insetlist.end();
535 for (; it != end; ++it) {
536 if (it->inset == inset) {
537 lyxerr << "InsetText::lockInsetInInset: 1 a\n";
538 text_.setCursorIntern(pit, it->pos);
539 lyxerr << "InsetText::lockInsetInInset: 1 b\n";
540 lyxerr << "bv: " << bv << " inset: " << inset << "\n";
541 lockInset(bv, inset);
542 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
545 if (it->inset->getInsetFromID(id)) {
546 lyxerr << "InsetText::lockInsetInInset: 2\n";
547 text_.setCursorIntern(pit, it->pos);
548 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
549 return the_locking_inset->lockInsetInInset(bv, inset);
553 lyxerr << "InsetText::lockInsetInInset: 3\n";
556 if (inset == cpar()->getInset(cpos())) {
557 lyxerr[Debug::INSETS] << "OK" << endl;
558 lockInset(bv, inset);
562 if (the_locking_inset && the_locking_inset == inset) {
563 if (cpar() == inset_par && cpos() == inset_pos) {
564 lyxerr[Debug::INSETS] << "OK" << endl;
565 inset_x = cix() - top_x + drawTextXOffset;
566 inset_y = ciy() + drawTextYOffset;
568 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
570 } else if (the_locking_inset) {
571 lyxerr[Debug::INSETS] << "MAYBE" << endl;
572 return the_locking_inset->lockInsetInInset(bv, inset);
574 lyxerr[Debug::INSETS] << "NOT OK" << endl;
579 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
582 if (!the_locking_inset)
584 if (the_locking_inset == inset) {
585 the_locking_inset->insetUnlock(bv);
586 getLyXText(bv)->updateInset(inset);
587 the_locking_inset = 0;
589 moveRightIntern(bv, true, false);
590 old_par = paragraphs.end(); // force layout setting
594 updateLocal(bv, CURSOR, false);
597 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
601 bool InsetText::updateInsetInInset(BufferView * bv, InsetOld * inset)
603 if (!autoBreakRows && paragraphs.size() > 1)
604 collapseParagraphs(bv);
609 if (inset->owner() != this) {
610 int ustat = CURSOR_PAR;
612 UpdatableInset * tl_inset = the_locking_inset;
614 found = tl_inset->updateInsetInInset(bv, inset);
616 tl_inset = static_cast<UpdatableInset *>(inset);
617 while(tl_inset->owner() && tl_inset->owner() != this)
618 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
619 if (!tl_inset->owner())
621 found = tl_inset->updateInsetInInset(bv, inset);
624 text_.updateInset(tl_inset);
625 setUpdateStatus(ustat);
629 bool found = text_.updateInset(inset);
631 setUpdateStatus(CURSOR_PAR);
632 if (the_locking_inset &&
633 cpar() == inset_par && cpos() == inset_pos)
635 inset_x = cix() - top_x + drawTextXOffset;
636 inset_y = ciy() + drawTextYOffset;
643 void InsetText::lfunMousePress(FuncRequest const & cmd)
647 // use this to check mouse motion for selection!
651 BufferView * bv = cmd.view();
652 FuncRequest cmd1 = cmd;
658 int tmp_x = cmd.x - drawTextXOffset;
659 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
660 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
662 if (the_locking_inset) {
663 if (the_locking_inset == inset) {
664 the_locking_inset->localDispatch(cmd1);
667 // otherwise only unlock the_locking_inset
668 the_locking_inset->insetUnlock(bv);
669 the_locking_inset = 0;
672 no_selection = false;
674 if (bv->theLockingInset()) {
675 if (isHighlyEditableInset(inset)) {
676 // We just have to lock the inset before calling a
678 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
679 if (!bv->lockInset(uinset)) {
680 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
682 inset->localDispatch(cmd1);
683 if (the_locking_inset)
684 updateLocal(bv, CURSOR, false);
689 bool paste_internally = false;
690 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
691 localDispatch(FuncRequest(bv, LFUN_COPY));
692 paste_internally = true;
694 int old_top_y = text_.top_y();
696 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
698 // set the selection cursor!
699 text_.selection.cursor = text_.cursor;
700 text_.cursor.x_fix(text_.cursor.x());
702 if (text_.selection.set()) {
703 text_.clearSelection();
704 updateLocal(bv, FULL, false);
706 text_.clearSelection();
707 updateLocal(bv, CURSOR, false);
710 bv->owner()->setLayout(cpar()->layout()->name());
712 // we moved the view we cannot do mouse selection in this case!
713 if (getLyXText(bv)->top_y() != old_top_y)
716 // Insert primary selection with middle mouse
717 // if there is a local selection in the current buffer,
719 if (cmd.button() == mouse_button::button2) {
720 if (paste_internally)
721 localDispatch(FuncRequest(bv, LFUN_PASTE));
723 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
726 getLyXText(bv)->clearSelection();
731 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
733 BufferView * bv = cmd.view();
734 FuncRequest cmd1 = cmd;
739 if (the_locking_inset)
740 return the_locking_inset->localDispatch(cmd1);
742 int tmp_x = cmd.x - drawTextXOffset;
743 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
744 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
747 // This code should probably be removed now. Simple insets
748 // (!highlyEditable) can actually take the localDispatch,
749 // and turn it into edit() if necessary. But we still
750 // need to deal properly with the whole relative vs.
751 // absolute mouse co-ords thing in a realiable, sensible way
753 if (isHighlyEditableInset(inset))
754 ret = inset->localDispatch(cmd1);
756 inset_x = cix(bv) - top_x + drawTextXOffset;
757 inset_y = ciy() + drawTextYOffset;
758 cmd1.x = cmd.x - inset_x;
759 cmd1.y = cmd.x - inset_y;
760 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
764 ret = inset->localDispatch(cmd1);
765 updateLocal(bv, CURSOR_PAR, false);
772 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
774 FuncRequest cmd1 = cmd;
778 if (the_locking_inset) {
779 the_locking_inset->localDispatch(cmd1);
783 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
786 BufferView * bv = cmd.view();
787 LyXCursor cur = text_.cursor;
788 text_.setCursorFromCoordinates
789 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
790 text_.cursor.x_fix(text_.cursor.x());
791 if (cur == text_.cursor)
793 text_.setSelection();
794 updateLocal(bv, SELECTION, false);
798 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
800 BufferView * bv = cmd.view();
803 switch (cmd.action) {
804 case LFUN_INSET_EDIT: {
805 UpdatableInset::localDispatch(cmd);
807 if (!bv->lockInset(this)) {
808 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
813 the_locking_inset = 0;
817 inset_boundary = false;
818 inset_par = paragraphs.end();
819 old_par = paragraphs.end();
822 if (cmd.argument.size()) {
823 if (cmd.argument == "left")
824 text_.setCursorIntern(paragraphs.begin(), 0);
826 ParagraphList::iterator it = boost::prior(paragraphs.end());
827 text_.setCursor(it, it->size());
830 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
831 // we put here -1 and not button as now the button in the
832 // edit call should not be needed we will fix this in 1.3.x
833 // cycle hopefully (Jug 20020509)
834 // FIXME: GUII I've changed this to none: probably WRONG
835 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
836 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
838 text_.cursor.x_fix(text_.cursor.x());
842 text_.clearSelection();
845 // If the inset is empty set the language of the current font to the
846 // language to the surronding text (if different).
847 if (paragraphs.begin()->empty() &&
848 paragraphs.size() == 1 &&
849 bv->getParentLanguage(this) != text_.current_font.language())
851 LyXFont font(LyXFont::ALL_IGNORE);
852 font.setLanguage(bv->getParentLanguage(this));
853 setFont(bv, font, false);
857 if (drawFrame_ == LOCKED)
858 code = CURSOR | DRAW_FRAME;
860 updateLocal(bv, code, false);
861 // Tell the paragraph dialog that we've entered an insettext.
862 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
866 case LFUN_MOUSE_PRESS:
870 case LFUN_MOUSE_MOTION:
871 lfunMouseMotion(cmd);
874 case LFUN_MOUSE_RELEASE:
875 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
881 bool was_empty = (paragraphs.begin()->empty() &&
882 paragraphs.size() == 1);
884 no_selection = false;
885 RESULT result = UpdatableInset::localDispatch(cmd);
886 if (result != UNDISPATCHED)
890 if (cmd.action < 0 && cmd.argument.empty())
893 if (the_locking_inset) {
894 result = the_locking_inset->localDispatch(cmd);
895 if (result == DISPATCHED_NOUPDATE)
897 else if (result == DISPATCHED) {
898 updateLocal(bv, CURSOR_PAR, false);
900 } else if (result >= FINISHED) {
903 moveRightIntern(bv, false, false);
907 if ((result = moveUp(bv)) >= FINISHED) {
908 updateLocal(bv, CURSOR, false);
909 bv->unlockInset(this);
913 if ((result = moveDown(bv)) >= FINISHED) {
914 updateLocal(bv, CURSOR, false);
915 bv->unlockInset(this);
922 the_locking_inset = 0;
923 updateLocal(bv, CURSOR, false);
924 // make sure status gets reset immediately
925 bv->owner()->clearMessage();
932 // what type of update to do on a cursor movement
933 int cursor_update = CURSOR;
935 if (text_.selection.set())
936 cursor_update = SELECTION;
938 switch (cmd.action) {
941 case LFUN_SELFINSERT:
942 if (bv->buffer()->isReadonly()) {
943 // setErrorMessage(N_("Document is read only"));
946 if (!cmd.argument.empty()) {
947 /* Automatically delete the currently selected
948 * text and replace it with what is being
949 * typed in now. Depends on lyxrc settings
950 * "auto_region_delete", which defaults to
953 // This should not be needed here and is also WRONG!
954 recordUndo(bv, Undo::INSERT, text_.cursor.par());
957 if (lyxrc.auto_region_delete) {
958 if (text_.selection.set()) {
959 text_.cutSelection(false, false);
962 text_.clearSelection();
963 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
964 bv->owner()->getIntl().getTransManager().
965 TranslateAndInsert(cmd.argument[i], &text_);
968 text_.selection.cursor = text_.cursor;
969 updwhat = CURSOR | CURSOR_PAR;
971 result = DISPATCHED_NOUPDATE;
974 // cursor movements that need special handling
977 result = moveRight(bv);
979 updwhat = cursor_update;
983 result = moveLeft(bv);
984 updwhat = cursor_update;
988 result = moveDown(bv);
989 updwhat = cursor_update;
994 updwhat = cursor_update;
998 if (crow() == text_.rows().begin())
999 result = FINISHED_UP;
1001 text_.cursorPrevious();
1002 text_.clearSelection();
1003 result = DISPATCHED_NOUPDATE;
1005 updwhat = cursor_update;
1009 if (boost::next(crow()) == text_.rows().end())
1010 result = FINISHED_DOWN;
1013 text_.clearSelection();
1014 result = DISPATCHED_NOUPDATE;
1016 updwhat = cursor_update;
1019 case LFUN_BACKSPACE: {
1020 if (text_.selection.set())
1021 text_.cutSelection(true, false);
1024 updwhat = CURSOR_PAR;
1030 if (text_.selection.set()) {
1031 text_.cutSelection(true, false);
1035 updwhat = CURSOR_PAR;
1041 text_.cutSelection(true, true);
1042 updwhat = CURSOR_PAR;
1049 text_.copySelection();
1050 updwhat = CURSOR_PAR;
1053 case LFUN_PASTESELECTION:
1055 string const clip(bv->getClipboard());
1059 if (cmd.argument == "paragraph") {
1060 text_.insertStringAsParagraphs(clip);
1062 text_.insertStringAsLines(clip);
1065 text_.clearSelection();
1067 updwhat = CURSOR_PAR;
1073 if (!autoBreakRows) {
1074 if (CutAndPaste::nrOfParagraphs() > 1) {
1075 #ifdef WITH_WARNINGS
1076 #warning FIXME horrendously bad UI
1078 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1083 replaceSelection(bv->getLyXText());
1084 size_t sel_index = 0;
1085 string const & arg = cmd.argument;
1086 if (isStrUnsignedInt(arg)) {
1087 size_t const paste_arg = strToUnsignedInt(arg);
1088 #warning FIXME Check if the arg is in the domain of available selections.
1089 sel_index = paste_arg;
1091 text_.pasteSelection(sel_index);
1093 text_.clearSelection();
1094 updwhat = CURSOR_PAR;
1099 case LFUN_BREAKPARAGRAPH:
1100 if (!autoBreakRows) {
1101 result = DISPATCHED;
1104 replaceSelection(bv->getLyXText());
1105 text_.breakParagraph(paragraphs, 0);
1106 updwhat = CURSOR | FULL;
1110 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1111 if (!autoBreakRows) {
1112 result = DISPATCHED;
1115 replaceSelection(bv->getLyXText());
1116 text_.breakParagraph(paragraphs, 1);
1117 updwhat = CURSOR | FULL;
1121 case LFUN_BREAKLINE: {
1122 if (!autoBreakRows) {
1123 result = DISPATCHED;
1127 replaceSelection(bv->getLyXText());
1128 text_.insertInset(new InsetNewline);
1129 updwhat = CURSOR | CURSOR_PAR;
1135 // do not set layouts on non breakable textinsets
1136 if (autoBreakRows) {
1137 string cur_layout = cpar()->layout()->name();
1139 // Derive layout number from given argument (string)
1140 // and current buffer's textclass (number). */
1141 LyXTextClass const & tclass =
1142 bv->buffer()->params.getLyXTextClass();
1143 string layout = cmd.argument;
1144 bool hasLayout = tclass.hasLayout(layout);
1146 // If the entry is obsolete, use the new one instead.
1148 string const & obs =
1149 tclass[layout]->obsoleted_by();
1154 // see if we found the layout number:
1156 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1157 bv->owner()->dispatch(lf);
1161 if (cur_layout != layout) {
1162 cur_layout = layout;
1163 text_.setLayout(layout);
1164 bv->owner()->setLayout(cpar()->layout()->name());
1165 updwhat = CURSOR_PAR;
1169 // reset the layout box
1170 bv->owner()->setLayout(cpar()->layout()->name());
1173 case LFUN_PARAGRAPH_SPACING:
1174 // This one is absolutely not working. When fiddling with this
1175 // it also seems to me that the paragraphs inside the insettext
1176 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1177 // FIXME: how old is this comment ? ...
1179 ParagraphList::iterator pit = text_.cursor.par();
1180 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1181 float cur_value = 1.0;
1182 if (cur_spacing == Spacing::Other) {
1183 cur_value = pit->params().spacing().getValue();
1186 istringstream istr(STRCONV(cmd.argument));
1189 Spacing::Space new_spacing = cur_spacing;
1190 float new_value = cur_value;
1192 lyxerr << "Missing argument to `paragraph-spacing'"
1194 } else if (tmp == "single") {
1195 new_spacing = Spacing::Single;
1196 } else if (tmp == "onehalf") {
1197 new_spacing = Spacing::Onehalf;
1198 } else if (tmp == "double") {
1199 new_spacing = Spacing::Double;
1200 } else if (tmp == "other") {
1201 new_spacing = Spacing::Other;
1204 lyxerr << "new_value = " << tmpval << endl;
1207 } else if (tmp == "default") {
1208 new_spacing = Spacing::Default;
1210 lyxerr << _("Unknown spacing argument: ")
1211 << cmd.argument << endl;
1213 if (cur_spacing != new_spacing || cur_value != new_value) {
1214 pit->params().spacing(Spacing(new_spacing, new_value));
1215 updwhat = CURSOR_PAR;
1221 // These need to do repaints but don't require
1222 // special handling otherwise. A *lot* of the
1223 // above could probably be done similarly ...
1228 case LFUN_WORDRIGHT:
1229 // these two are really unhandled ...
1231 case LFUN_BEGINNINGBUF:
1232 updwhat = cursor_update;
1233 if (!bv->dispatch(cmd))
1234 result = UNDISPATCHED;
1243 case LFUN_WORDLEFTSEL:
1244 case LFUN_WORDRIGHTSEL:
1245 updwhat = SELECTION;
1250 if (!bv->dispatch(cmd))
1251 result = UNDISPATCHED;
1256 updateLocal(bv, updwhat, updflag);
1257 /// If the action has deleted all text in the inset, we need to change the
1258 // language to the language of the surronding text.
1259 if (!was_empty && paragraphs.begin()->empty() &&
1260 paragraphs.size() == 1) {
1261 LyXFont font(LyXFont::ALL_IGNORE);
1262 font.setLanguage(bv->getParentLanguage(this));
1263 setFont(bv, font, false);
1266 if (result >= FINISHED)
1267 bv->unlockInset(this);
1269 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1270 result = DISPATCHED;
1275 int InsetText::latex(Buffer const * buf, ostream & os,
1276 LatexRunParams const & runparams) const
1279 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1280 return texrow.rows();
1284 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1286 unsigned int lines = 0;
1288 ParagraphList::const_iterator beg = paragraphs.begin();
1289 ParagraphList::const_iterator end = paragraphs.end();
1290 ParagraphList::const_iterator it = beg;
1291 for (; it != end; ++it) {
1292 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1293 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1300 int InsetText::linuxdoc(Buffer const * buf, ostream & os) const
1302 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1303 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1305 // There is a confusion between the empty paragraph and the default paragraph
1306 // The default paragraph is <p></p>, the empty paragraph is *empty*
1307 // Since none of the floats of linuxdoc accepts standard paragraphs
1308 // I disable them. I don't expect problems. (jamatos 2003/07/27)
1309 for (; pit != pend; ++pit) {
1310 const string name = pit->layout()->latexname();
1312 sgml::openTag(os, 1, 0, name);
1313 buf->simpleLinuxDocOnePar(os, pit, 0);
1315 sgml::closeTag(os, 1, 0, name);
1321 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1323 unsigned int lines = 0;
1325 vector<string> environment_stack(10);
1326 vector<string> environment_inner(10);
1328 int const command_depth = 0;
1331 Paragraph::depth_type depth = 0; // paragraph depth
1333 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1334 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1336 for (; pit != pend; ++pit) {
1338 int desc_on = 0; // description mode
1340 LyXLayout_ptr const & style = pit->layout();
1342 // environment tag closing
1343 for (; depth > pit->params().depth(); --depth) {
1344 if (environment_inner[depth] != "!-- --") {
1345 item_name = "listitem";
1346 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1347 if (environment_inner[depth] == "varlistentry")
1348 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1350 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1351 environment_stack[depth].erase();
1352 environment_inner[depth].erase();
1355 if (depth == pit->params().depth()
1356 && environment_stack[depth] != style->latexname()
1357 && !environment_stack[depth].empty()) {
1358 if (environment_inner[depth] != "!-- --") {
1359 item_name= "listitem";
1360 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1361 if (environment_inner[depth] == "varlistentry")
1362 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1365 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1367 environment_stack[depth].erase();
1368 environment_inner[depth].erase();
1371 // Write opening SGML tags.
1372 switch (style->latextype) {
1373 case LATEX_PARAGRAPH:
1374 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1378 buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1382 case LATEX_ENVIRONMENT:
1383 case LATEX_ITEM_ENVIRONMENT:
1384 if (depth < pit->params().depth()) {
1385 depth = pit->params().depth();
1386 environment_stack[depth].erase();
1389 if (environment_stack[depth] != style->latexname()) {
1390 if (environment_stack.size() == depth + 1) {
1391 environment_stack.push_back("!-- --");
1392 environment_inner.push_back("!-- --");
1394 environment_stack[depth] = style->latexname();
1395 environment_inner[depth] = "!-- --";
1396 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1398 if (environment_inner[depth] != "!-- --") {
1399 item_name= "listitem";
1400 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1401 if (environment_inner[depth] == "varlistentry")
1402 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1406 if (style->latextype == LATEX_ENVIRONMENT) {
1407 if (!style->latexparam().empty()) {
1408 if (style->latexparam() == "CDATA")
1411 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1416 desc_on = (style->labeltype == LABEL_MANUAL);
1418 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1419 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1421 item_name = desc_on ? "term" : "para";
1422 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1426 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1430 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1433 // write closing SGML tags
1434 switch (style->latextype) {
1435 case LATEX_ENVIRONMENT:
1436 if (!style->latexparam().empty()) {
1437 if (style->latexparam() == "CDATA")
1440 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1443 case LATEX_ITEM_ENVIRONMENT:
1444 if (desc_on == 1) break;
1446 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1448 case LATEX_PARAGRAPH:
1449 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1452 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1458 for (int d = depth; d >= 0; --d) {
1459 if (!environment_stack[depth].empty()) {
1460 if (environment_inner[depth] != "!-- --") {
1461 item_name = "listitem";
1462 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1463 if (environment_inner[depth] == "varlistentry")
1464 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1467 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1475 void InsetText::validate(LaTeXFeatures & features) const
1477 for_each(paragraphs.begin(), paragraphs.end(),
1478 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1482 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1484 if (the_locking_inset) {
1485 the_locking_inset->getCursor(bv, x, y);
1489 y = cy() + InsetText::y();
1493 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1495 if (the_locking_inset) {
1496 the_locking_inset->getCursorPos(bv, x, y);
1499 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1500 y = cy() - TEXT_TO_INSET_OFFSET;
1504 int InsetText::insetInInsetY() const
1506 if (!the_locking_inset)
1509 return inset_y + the_locking_inset->insetInInsetY();
1513 void InsetText::fitInsetCursor(BufferView * bv) const
1515 if (the_locking_inset) {
1516 the_locking_inset->fitInsetCursor(bv);
1520 LyXFont const font = text_.getFont(cpar(), cpos());
1522 int const asc = font_metrics::maxAscent(font);
1523 int const desc = font_metrics::maxDescent(font);
1525 if (bv->fitLockedInsetCursor(cx(), cy(), asc, desc))
1526 need_update |= FULL;
1531 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1533 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1534 return moveLeftIntern(bv, false, activate_inset, selecting);
1536 return moveRightIntern(bv, true, activate_inset, selecting);
1541 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1543 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1544 return moveRightIntern(bv, true, activate_inset, selecting);
1546 return moveLeftIntern(bv, false, activate_inset, selecting);
1551 InsetText::moveRightIntern(BufferView * bv, bool front,
1552 bool activate_inset, bool selecting)
1554 ParagraphList::iterator c_par = cpar();
1556 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1557 return FINISHED_RIGHT;
1558 if (activate_inset && checkAndActivateInset(bv, front))
1560 text_.cursorRight(bv);
1562 text_.clearSelection();
1563 return DISPATCHED_NOUPDATE;
1568 InsetText::moveLeftIntern(BufferView * bv, bool front,
1569 bool activate_inset, bool selecting)
1571 if (cpar() == paragraphs.begin() && cpos() <= 0)
1573 text_.cursorLeft(bv);
1575 text_.clearSelection();
1576 if (activate_inset && checkAndActivateInset(bv, front))
1578 return DISPATCHED_NOUPDATE;
1582 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1584 if (crow() == text_.rows().begin())
1587 text_.clearSelection();
1588 return DISPATCHED_NOUPDATE;
1592 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1594 if (boost::next(crow()) == text_.rows().end())
1595 return FINISHED_DOWN;
1596 text_.cursorDown(bv);
1597 text_.clearSelection();
1598 return DISPATCHED_NOUPDATE;
1602 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1604 if (the_locking_inset) {
1605 if (the_locking_inset->insetAllowed(inset))
1606 return the_locking_inset->insertInset(bv, inset);
1609 inset->setOwner(this);
1610 text_.insertInset(inset);
1612 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1617 bool InsetText::insetAllowed(InsetOld::Code code) const
1619 // in_insetAllowed is a really gross hack,
1620 // to allow us to call the owner's insetAllowed
1621 // without stack overflow, which can happen
1622 // when the owner uses InsetCollapsable::insetAllowed()
1624 if (in_insetAllowed)
1626 in_insetAllowed = true;
1627 if (the_locking_inset)
1628 ret = the_locking_inset->insetAllowed(code);
1630 ret = owner()->insetAllowed(code);
1631 in_insetAllowed = false;
1636 UpdatableInset * InsetText::getLockingInset() const
1638 return the_locking_inset ? the_locking_inset->getLockingInset() :
1639 const_cast<InsetText *>(this);
1643 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1647 if (the_locking_inset)
1648 return the_locking_inset->getFirstLockingInsetOfType(c);
1653 bool InsetText::showInsetDialog(BufferView * bv) const
1655 if (the_locking_inset)
1656 return the_locking_inset->showInsetDialog(bv);
1661 void InsetText::getLabelList(std::vector<string> & list) const
1663 ParagraphList::const_iterator pit = paragraphs.begin();
1664 ParagraphList::const_iterator pend = paragraphs.end();
1665 for (; pit != pend; ++pit) {
1666 InsetList::const_iterator beg = pit->insetlist.begin();
1667 InsetList::const_iterator end = pit->insetlist.end();
1668 for (; beg != end; ++beg)
1669 beg->inset->getLabelList(list);
1674 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1677 if (the_locking_inset) {
1678 the_locking_inset->setFont(bv, font, toggleall, selectall);
1682 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1683 || cpar()->empty()) {
1684 text_.setFont(font, toggleall);
1689 if (text_.selection.set())
1690 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1694 text_.selection.cursor = text_.cursor;
1695 text_.cursorBottom();
1696 text_.setSelection();
1699 text_.toggleFree(font, toggleall);
1702 text_.clearSelection();
1706 bool flag = (selectall || text_.selection.set());
1709 updateLocal(bv, FULL, true);
1711 updateLocal(bv, CURSOR_PAR, true);
1715 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1717 InsetOld * inset = cpar()->getInset(cpos());
1718 if (!isHighlyEditableInset(inset))
1720 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1721 inset->localDispatch(cmd);
1722 if (!the_locking_inset)
1724 updateLocal(bv, CURSOR, false);
1729 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1730 mouse_button::state button)
1732 x -= drawTextXOffset;
1734 int dummyy = y + dim_.asc;
1735 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1736 // we only do the edit() call if the inset was hit by the mouse
1737 // or if it is a highly editable inset. So we should call this
1738 // function from our own edit with button < 0.
1739 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1741 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1750 inset_x = cix() - top_x + drawTextXOffset;
1751 inset_y = ciy() + drawTextYOffset;
1752 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1753 inset->localDispatch(cmd);
1754 if (!the_locking_inset)
1756 updateLocal(bv, CURSOR, false);
1761 void InsetText::setParagraphData(ParagraphList const & plist)
1763 // we have to unlock any locked inset otherwise we're in troubles
1764 the_locking_inset = 0;
1766 // But it it makes no difference that is a lot better.
1768 // See if this can be simplified when std::list is in effect.
1771 ParagraphList::const_iterator it = plist.begin();
1772 ParagraphList::const_iterator end = plist.end();
1773 for (; it != end; ++it) {
1774 paragraphs.push_back(*it);
1775 paragraphs.back().setInsetOwner(this);
1783 void InsetText::markNew(bool track_changes)
1785 ParagraphList::iterator pit = paragraphs.begin();
1786 ParagraphList::iterator pend = paragraphs.end();
1787 for (; pit != pend; ++pit) {
1788 if (track_changes) {
1789 pit->trackChanges();
1791 // no-op when not tracking
1792 pit->cleanChanges();
1798 void InsetText::setText(string const & data, LyXFont const & font)
1801 for (unsigned int i = 0; i < data.length(); ++i)
1802 paragraphs.begin()->insertChar(i, data[i], font);
1807 void InsetText::setAutoBreakRows(bool flag)
1809 if (flag != autoBreakRows) {
1810 autoBreakRows = flag;
1818 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1820 if (how != drawFrame_) {
1823 updateLocal(bv, DRAW_FRAME, false);
1828 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1830 if (frame_color != col) {
1833 updateLocal(bv, DRAW_FRAME, false);
1838 int InsetText::cx() const
1840 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1841 if (the_locking_inset) {
1842 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1843 if (font.isVisibleRightToLeft())
1844 x -= the_locking_inset->width();
1850 int InsetText::cix() const
1852 int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1853 if (the_locking_inset) {
1854 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1855 if (font.isVisibleRightToLeft())
1856 x -= the_locking_inset->width();
1862 int InsetText::cy() const
1864 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1868 int InsetText::ciy() const
1870 return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1874 pos_type InsetText::cpos() const
1876 return text_.cursor.pos();
1880 ParagraphList::iterator InsetText::cpar() const
1882 return text_.cursor.par();
1886 bool InsetText::cboundary() const
1888 return text_.cursor.boundary();
1892 RowList::iterator InsetText::crow() const
1894 return text_.cursorRow();
1898 LyXText * InsetText::getLyXText(BufferView const * bv,
1899 bool const recursive) const
1902 if (recursive && the_locking_inset)
1903 return the_locking_inset->getLyXText(bv, true);
1908 void InsetText::setViewCache(BufferView const * bv) const
1911 text_.bv_owner = const_cast<BufferView *>(bv);
1915 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1918 /// then remove all LyXText in text-insets
1919 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1920 const_cast<ParagraphList&>(paragraphs).end(),
1921 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1926 void InsetText::resizeLyXText(BufferView * bv, bool /*force*/) const
1928 if (paragraphs.size() == 1 && paragraphs.begin()->empty()) {
1929 // no data, resize not neccessary!
1930 // we have to do this as a fixed width may have changed!
1933 restoreLyXTextState();
1945 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1946 const_cast<ParagraphList&>(paragraphs).end(),
1947 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1950 restoreLyXTextState();
1952 // seems to be unneeded
1954 if (the_locking_inset) {
1955 inset_x = cix() - top_x + drawTextXOffset;
1956 inset_y = ciy() + drawTextYOffset;
1959 text_.top_y(bv->screen().topCursorVisible(&text_));
1961 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
1962 // this will scroll the screen such that the cursor becomes visible
1963 bv->updateScrollbar();
1965 need_update |= FULL;
1971 void InsetText::reinitLyXText() const
1973 BufferView * bv = text_.bv_owner;
1980 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1981 const_cast<ParagraphList&>(paragraphs).end(),
1982 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1985 restoreLyXTextState();
1986 if (the_locking_inset) {
1987 inset_x = cix() - top_x + drawTextXOffset;
1988 inset_y = ciy() + drawTextYOffset;
1990 text_.top_y(bv->screen().topCursorVisible(&text_));
1992 // this will scroll the screen such that the cursor becomes visible
1993 bv->updateScrollbar();
2000 void InsetText::removeNewlines()
2002 bool changed = false;
2004 ParagraphList::iterator it = paragraphs.begin();
2005 ParagraphList::iterator end = paragraphs.end();
2006 for (; it != end; ++it) {
2007 for (int i = 0; i < it->size(); ++i) {
2008 if (it->isNewline(i)) {
2019 bool InsetText::nodraw() const
2021 if (the_locking_inset)
2022 return the_locking_inset->nodraw();
2023 return UpdatableInset::nodraw();
2027 int InsetText::scroll(bool recursive) const
2029 int sx = UpdatableInset::scroll(false);
2031 if (recursive && the_locking_inset)
2032 sx += the_locking_inset->scroll(recursive);
2038 void InsetText::clearSelection(BufferView * bv)
2040 getLyXText(bv)->clearSelection();
2044 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2046 Painter & pain = bv->painter();
2048 int h = dim_.asc + dim_.des;
2049 int ty = baseline - dim_.asc;
2055 if ((ty + h) > pain.paperHeight())
2056 h = pain.paperHeight();
2057 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2058 w = pain.paperWidth();
2059 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2064 ParagraphList * InsetText::getParagraphs(int i) const
2066 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2070 LyXCursor const & InsetText::cursor(BufferView * bv) const
2072 if (the_locking_inset)
2073 return the_locking_inset->cursor(bv);
2074 return getLyXText(bv)->cursor;
2078 InsetOld * InsetText::getInsetFromID(int id_arg) const
2081 return const_cast<InsetText *>(this);
2083 ParagraphList::const_iterator pit = paragraphs.begin();
2084 ParagraphList::const_iterator pend = paragraphs.end();
2085 for (; pit != pend; ++pit) {
2086 InsetList::const_iterator it = pit->insetlist.begin();
2087 InsetList::const_iterator end = pit->insetlist.end();
2088 for (; it != end; ++it) {
2089 if (it->inset->id() == id_arg)
2091 InsetOld * in = it->inset->getInsetFromID(id_arg);
2101 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2104 if (the_locking_inset) {
2105 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2106 if (!word.word().empty()) {
2110 // we have to go on checking so move cursor to the next char
2111 text_.cursor.pos(text_.cursor.pos() + 1);
2113 word = text_.selectNextWordToSpellcheck(value);
2114 if (word.word().empty())
2115 bv->unlockInset(const_cast<InsetText *>(this));
2122 void InsetText::selectSelectedWord(BufferView * bv)
2124 if (the_locking_inset) {
2125 the_locking_inset->selectSelectedWord(bv);
2128 getLyXText(bv)->selectSelectedWord();
2129 updateLocal(bv, SELECTION, false);
2133 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2135 if (the_locking_inset)
2136 the_locking_inset->toggleSelection(bv, kill_selection);
2138 int x = top_x + TEXT_TO_INSET_OFFSET;
2140 RowList::iterator rit = text_.rows().begin();
2141 RowList::iterator end = text_.rows().end();
2142 int y_offset = top_baseline - rit->ascent_of_text();
2144 while (rit != end && y + rit->height() <= 0) {
2151 if (need_update & SELECTION)
2153 bv->screen().toggleSelection(&text_, bv, kill_selection, y_offset, x);
2157 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2159 if (the_locking_inset) {
2160 if (the_locking_inset->nextChange(bv, length))
2162 text_.cursorRight(true);
2164 lyx::find::SearchResult result =
2165 lyx::find::findNextChange(bv, &text_, length);
2167 if (result == lyx::find::SR_FOUND) {
2168 LyXCursor cur = text_.cursor;
2169 bv->unlockInset(bv->theLockingInset());
2170 if (bv->lockInset(this))
2173 text_.setSelectionRange(length);
2174 updateLocal(bv, SELECTION, false);
2176 return result != lyx::find::SR_NOT_FOUND;
2180 bool InsetText::searchForward(BufferView * bv, string const & str,
2183 if (the_locking_inset) {
2184 if (the_locking_inset->searchForward(bv, str, cs, mw))
2186 text_.cursorRight(true);
2188 lyx::find::SearchResult result =
2189 lyx::find::find(bv, &text_, str, true, cs, mw);
2191 if (result == lyx::find::SR_FOUND) {
2192 LyXCursor cur = text_.cursor;
2193 bv->unlockInset(bv->theLockingInset());
2194 if (bv->lockInset(this))
2197 text_.setSelectionRange(str.length());
2198 updateLocal(bv, SELECTION, false);
2200 return result != lyx::find::SR_NOT_FOUND;
2204 bool InsetText::searchBackward(BufferView * bv, string const & str,
2207 if (the_locking_inset) {
2208 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2212 ParagraphList::iterator pit = paragraphs.begin();
2213 ParagraphList::iterator pend = paragraphs.end();
2215 while (boost::next(pit) != pend)
2218 text_.setCursor(pit, pit->size());
2220 lyx::find::SearchResult result =
2221 lyx::find::find(bv, &text_, str, false, cs, mw);
2223 if (result == lyx::find::SR_FOUND) {
2224 LyXCursor cur = text_.cursor;
2225 bv->unlockInset(bv->theLockingInset());
2226 if (bv->lockInset(this))
2229 text_.setSelectionRange(str.length());
2230 updateLocal(bv, SELECTION, false);
2232 return result != lyx::find::SR_NOT_FOUND;
2236 bool InsetText::checkInsertChar(LyXFont & font)
2239 return owner()->checkInsertChar(font);
2244 void InsetText::collapseParagraphs(BufferView * bv)
2246 while (paragraphs.size() > 1) {
2247 ParagraphList::iterator first_par = paragraphs.begin();
2248 ParagraphList::iterator next_par = boost::next(first_par);
2249 size_t const first_par_size = first_par->size();
2251 if (!first_par->empty() &&
2252 !next_par->empty() &&
2253 !first_par->isSeparator(first_par_size - 1)) {
2254 first_par->insertChar(first_par_size, ' ');
2257 if (text_.selection.set()) {
2258 if (text_.selection.start.par() == next_par) {
2259 text_.selection.start.par(first_par);
2260 text_.selection.start.pos(
2261 text_.selection.start.pos() + first_par_size);
2263 if (text_.selection.end.par() == next_par) {
2264 text_.selection.end.par(first_par);
2265 text_.selection.end.pos(
2266 text_.selection.end.pos() + first_par_size);
2270 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2276 void InsetText::getDrawFont(LyXFont & font) const
2280 owner()->getDrawFont(font);
2284 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2286 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2287 // And it probably does. You have to take a look at this John. (Lgb)
2288 #warning John, have a look here. (Lgb)
2289 ParagraphList::iterator pit = plist.begin();
2290 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2292 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2294 ParagraphList::iterator pend = plist.end();
2295 for (; pit != pend; ++pit) {
2296 paragraphs.push_back(*pit);
2303 void InsetText::addPreview(PreviewLoader & loader) const
2305 ParagraphList::const_iterator pit = paragraphs.begin();
2306 ParagraphList::const_iterator pend = paragraphs.end();
2308 for (; pit != pend; ++pit) {
2309 InsetList::const_iterator it = pit->insetlist.begin();
2310 InsetList::const_iterator end = pit->insetlist.end();
2311 for (; it != end; ++it) {
2312 it->inset->addPreview(loader);