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);
368 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
369 drawFrame(pain, int(start_x));
371 if (need_update != INIT)
376 void InsetText::drawFrame(Painter & pain, int x) const
378 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
380 frame_y = top_baseline - dim_.asc + ttoD2;
381 frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
382 frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
383 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
388 void InsetText::setUpdateStatus(int what) const
391 // we will to redraw us full if our LyXText wants it
392 if (text_.needRefresh())
395 // this to not draw a selection when we redraw all of it!
396 if (need_update & CURSOR && !(need_update & SELECTION)) {
397 if (text_.selection.set())
399 text_.clearSelection();
404 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
409 if (!autoBreakRows && paragraphs.size() > 1)
410 collapseParagraphs(bv);
412 text_.partialRebreak();
413 setUpdateStatus(what);
414 bool flag = mark_dirty ||
415 ((need_update != CURSOR && need_update != NONE) ||
416 text_.needRefresh() || text_.selection.set());
417 if (!text_.selection.set())
418 text_.selection.cursor = text_.cursor;
424 bv->updateInset(const_cast<InsetText *>(this));
427 if (need_update == CURSOR)
429 bv->owner()->view_state_changed();
430 bv->owner()->updateMenubar();
431 bv->owner()->updateToolbar();
432 if (old_par != cpar()) {
433 bv->owner()->setLayout(cpar()->layout()->name());
439 string const InsetText::editMessage() const
441 return _("Opened Text Inset");
445 void InsetText::insetUnlock(BufferView * bv)
447 if (the_locking_inset) {
448 the_locking_inset->insetUnlock(bv);
449 the_locking_inset = 0;
450 updateLocal(bv, CURSOR_PAR, false);
456 if (text_.selection.set()) {
457 text_.clearSelection();
459 } else if (owner()) {
460 bv->owner()->setLayout(owner()->getLyXText(bv)
461 ->cursor.par()->layout()->name());
463 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
464 // hack for deleteEmptyParMech
465 ParagraphList::iterator first_par = paragraphs.begin();
466 if (!first_par->empty()) {
467 text_.setCursor(first_par, 0);
468 } else if (paragraphs.size() > 1) {
469 text_.setCursor(boost::next(first_par), 0);
472 updateLocal(bv, code, false);
475 setUpdateStatus(code);
480 void InsetText::lockInset(BufferView * bv)
483 the_locking_inset = 0;
484 inset_pos = inset_x = inset_y = 0;
485 inset_boundary = false;
486 inset_par = paragraphs.end();
487 old_par = paragraphs.end();
488 text_.setCursorIntern(paragraphs.begin(), 0);
489 text_.clearSelection();
491 // If the inset is empty set the language of the current font to the
492 // language to the surronding text (if different).
493 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
494 bv->getParentLanguage(this) != text_.current_font.language()) {
495 LyXFont font(LyXFont::ALL_IGNORE);
496 font.setLanguage(bv->getParentLanguage(this));
497 setFont(bv, font, false);
500 if (drawFrame_ == LOCKED)
501 code = CURSOR|DRAW_FRAME;
502 setUpdateStatus(code);
506 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
508 the_locking_inset = inset;
509 inset_x = cix() - top_x + drawTextXOffset;
510 inset_y = ciy() + drawTextYOffset;
513 inset_boundary = cboundary();
514 //updateLocal(bv, CURSOR, false);
518 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
520 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
524 if (!the_locking_inset) {
525 ParagraphList::iterator pit = paragraphs.begin();
526 ParagraphList::iterator pend = paragraphs.end();
528 int const id = inset->id();
529 for (; pit != pend; ++pit) {
530 InsetList::iterator it = pit->insetlist.begin();
531 InsetList::iterator const end = pit->insetlist.end();
532 for (; it != end; ++it) {
533 if (it->inset == inset) {
534 lyxerr << "InsetText::lockInsetInInset: 1 a\n";
535 text_.setCursorIntern(pit, it->pos);
536 lyxerr << "InsetText::lockInsetInInset: 1 b\n";
537 lyxerr << "bv: " << bv << " inset: " << inset << "\n";
538 lockInset(bv, inset);
539 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
542 if (it->inset->getInsetFromID(id)) {
543 lyxerr << "InsetText::lockInsetInInset: 2\n";
544 text_.setCursorIntern(pit, it->pos);
545 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
546 return the_locking_inset->lockInsetInInset(bv, inset);
550 lyxerr << "InsetText::lockInsetInInset: 3\n";
553 if (inset == cpar()->getInset(cpos())) {
554 lyxerr[Debug::INSETS] << "OK" << endl;
555 lockInset(bv, inset);
559 if (the_locking_inset && the_locking_inset == inset) {
560 if (cpar() == inset_par && cpos() == inset_pos) {
561 lyxerr[Debug::INSETS] << "OK" << endl;
562 inset_x = cix() - top_x + drawTextXOffset;
563 inset_y = ciy() + drawTextYOffset;
565 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
567 } else if (the_locking_inset) {
568 lyxerr[Debug::INSETS] << "MAYBE" << endl;
569 return the_locking_inset->lockInsetInInset(bv, inset);
571 lyxerr[Debug::INSETS] << "NOT OK" << endl;
576 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
579 if (!the_locking_inset)
581 if (the_locking_inset == inset) {
582 the_locking_inset->insetUnlock(bv);
583 getLyXText(bv)->updateInset(inset);
584 the_locking_inset = 0;
586 moveRightIntern(bv, true, false);
587 old_par = paragraphs.end(); // force layout setting
591 updateLocal(bv, CURSOR, false);
594 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
598 bool InsetText::updateInsetInInset(BufferView * bv, InsetOld * inset)
600 if (!autoBreakRows && paragraphs.size() > 1)
601 collapseParagraphs(bv);
606 if (inset->owner() != this) {
607 int ustat = CURSOR_PAR;
609 UpdatableInset * tl_inset = the_locking_inset;
611 found = tl_inset->updateInsetInInset(bv, inset);
613 tl_inset = static_cast<UpdatableInset *>(inset);
614 while(tl_inset->owner() && tl_inset->owner() != this)
615 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
616 if (!tl_inset->owner())
618 found = tl_inset->updateInsetInInset(bv, inset);
621 text_.updateInset(tl_inset);
622 setUpdateStatus(ustat);
626 bool found = text_.updateInset(inset);
628 setUpdateStatus(CURSOR_PAR);
629 if (the_locking_inset &&
630 cpar() == inset_par && cpos() == inset_pos)
632 inset_x = cix() - top_x + drawTextXOffset;
633 inset_y = ciy() + drawTextYOffset;
640 void InsetText::lfunMousePress(FuncRequest const & cmd)
644 // use this to check mouse motion for selection!
648 BufferView * bv = cmd.view();
649 FuncRequest cmd1 = cmd;
655 int tmp_x = cmd.x - drawTextXOffset;
656 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
657 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
659 if (the_locking_inset) {
660 if (the_locking_inset == inset) {
661 the_locking_inset->localDispatch(cmd1);
664 // otherwise only unlock the_locking_inset
665 the_locking_inset->insetUnlock(bv);
666 the_locking_inset = 0;
669 no_selection = false;
671 if (bv->theLockingInset()) {
672 if (isHighlyEditableInset(inset)) {
673 // We just have to lock the inset before calling a
675 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
676 if (!bv->lockInset(uinset)) {
677 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
679 inset->localDispatch(cmd1);
680 if (the_locking_inset)
681 updateLocal(bv, CURSOR, false);
686 bool paste_internally = false;
687 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
688 localDispatch(FuncRequest(bv, LFUN_COPY));
689 paste_internally = true;
691 int old_top_y = text_.top_y();
693 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
695 // set the selection cursor!
696 text_.selection.cursor = text_.cursor;
697 text_.cursor.x_fix(text_.cursor.x());
699 if (text_.selection.set()) {
700 text_.clearSelection();
701 updateLocal(bv, FULL, false);
703 text_.clearSelection();
704 updateLocal(bv, CURSOR, false);
707 bv->owner()->setLayout(cpar()->layout()->name());
709 // we moved the view we cannot do mouse selection in this case!
710 if (getLyXText(bv)->top_y() != old_top_y)
713 // Insert primary selection with middle mouse
714 // if there is a local selection in the current buffer,
716 if (cmd.button() == mouse_button::button2) {
717 if (paste_internally)
718 localDispatch(FuncRequest(bv, LFUN_PASTE));
720 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
723 getLyXText(bv)->clearSelection();
728 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
730 BufferView * bv = cmd.view();
731 FuncRequest cmd1 = cmd;
736 if (the_locking_inset)
737 return the_locking_inset->localDispatch(cmd1);
739 int tmp_x = cmd.x - drawTextXOffset;
740 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
741 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
744 // This code should probably be removed now. Simple insets
745 // (!highlyEditable) can actually take the localDispatch,
746 // and turn it into edit() if necessary. But we still
747 // need to deal properly with the whole relative vs.
748 // absolute mouse co-ords thing in a realiable, sensible way
750 if (isHighlyEditableInset(inset))
751 ret = inset->localDispatch(cmd1);
753 inset_x = cix(bv) - top_x + drawTextXOffset;
754 inset_y = ciy() + drawTextYOffset;
755 cmd1.x = cmd.x - inset_x;
756 cmd1.y = cmd.x - inset_y;
757 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
761 ret = inset->localDispatch(cmd1);
762 updateLocal(bv, CURSOR_PAR, false);
769 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
771 FuncRequest cmd1 = cmd;
775 if (the_locking_inset) {
776 the_locking_inset->localDispatch(cmd1);
780 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
783 BufferView * bv = cmd.view();
784 LyXCursor cur = text_.cursor;
785 text_.setCursorFromCoordinates
786 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
787 text_.cursor.x_fix(text_.cursor.x());
788 if (cur == text_.cursor)
790 text_.setSelection();
791 updateLocal(bv, SELECTION, false);
795 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
797 BufferView * bv = cmd.view();
800 switch (cmd.action) {
801 case LFUN_INSET_EDIT: {
802 UpdatableInset::localDispatch(cmd);
804 if (!bv->lockInset(this)) {
805 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
810 the_locking_inset = 0;
814 inset_boundary = false;
815 inset_par = paragraphs.end();
816 old_par = paragraphs.end();
819 if (cmd.argument.size()) {
820 if (cmd.argument == "left")
821 text_.setCursorIntern(paragraphs.begin(), 0);
823 ParagraphList::iterator it = boost::prior(paragraphs.end());
824 text_.setCursor(it, it->size());
827 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
828 // we put here -1 and not button as now the button in the
829 // edit call should not be needed we will fix this in 1.3.x
830 // cycle hopefully (Jug 20020509)
831 // FIXME: GUII I've changed this to none: probably WRONG
832 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
833 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
835 text_.cursor.x_fix(text_.cursor.x());
839 text_.clearSelection();
842 // If the inset is empty set the language of the current font to the
843 // language to the surronding text (if different).
844 if (paragraphs.begin()->empty() &&
845 paragraphs.size() == 1 &&
846 bv->getParentLanguage(this) != text_.current_font.language())
848 LyXFont font(LyXFont::ALL_IGNORE);
849 font.setLanguage(bv->getParentLanguage(this));
850 setFont(bv, font, false);
854 if (drawFrame_ == LOCKED)
855 code = CURSOR | DRAW_FRAME;
857 updateLocal(bv, code, false);
858 // Tell the paragraph dialog that we've entered an insettext.
859 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
863 case LFUN_MOUSE_PRESS:
867 case LFUN_MOUSE_MOTION:
868 lfunMouseMotion(cmd);
871 case LFUN_MOUSE_RELEASE:
872 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
878 bool was_empty = (paragraphs.begin()->empty() &&
879 paragraphs.size() == 1);
881 no_selection = false;
882 RESULT result = UpdatableInset::localDispatch(cmd);
883 if (result != UNDISPATCHED)
887 if (cmd.action < 0 && cmd.argument.empty())
890 if (the_locking_inset) {
891 result = the_locking_inset->localDispatch(cmd);
892 if (result == DISPATCHED_NOUPDATE)
894 else if (result == DISPATCHED) {
895 updateLocal(bv, CURSOR_PAR, false);
897 } else if (result >= FINISHED) {
900 moveRightIntern(bv, false, false);
904 if ((result = moveUp(bv)) >= FINISHED) {
905 updateLocal(bv, CURSOR, false);
906 bv->unlockInset(this);
910 if ((result = moveDown(bv)) >= FINISHED) {
911 updateLocal(bv, CURSOR, false);
912 bv->unlockInset(this);
919 the_locking_inset = 0;
920 updateLocal(bv, CURSOR, false);
921 // make sure status gets reset immediately
922 bv->owner()->clearMessage();
929 // what type of update to do on a cursor movement
930 int cursor_update = CURSOR;
932 if (text_.selection.set())
933 cursor_update = SELECTION;
935 switch (cmd.action) {
938 case LFUN_SELFINSERT:
939 if (bv->buffer()->isReadonly()) {
940 // setErrorMessage(N_("Document is read only"));
943 if (!cmd.argument.empty()) {
944 /* Automatically delete the currently selected
945 * text and replace it with what is being
946 * typed in now. Depends on lyxrc settings
947 * "auto_region_delete", which defaults to
950 // This should not be needed here and is also WRONG!
951 recordUndo(bv, Undo::INSERT, text_.cursor.par());
954 if (lyxrc.auto_region_delete) {
955 if (text_.selection.set()) {
956 text_.cutSelection(false, false);
959 text_.clearSelection();
960 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
961 bv->owner()->getIntl().getTransManager().
962 TranslateAndInsert(cmd.argument[i], &text_);
965 text_.selection.cursor = text_.cursor;
966 updwhat = CURSOR | CURSOR_PAR;
968 result = DISPATCHED_NOUPDATE;
971 // cursor movements that need special handling
974 result = moveRight(bv);
976 updwhat = cursor_update;
980 result = moveLeft(bv);
981 updwhat = cursor_update;
985 result = moveDown(bv);
986 updwhat = cursor_update;
991 updwhat = cursor_update;
995 if (crow() == text_.rows().begin())
996 result = FINISHED_UP;
998 text_.cursorPrevious();
999 text_.clearSelection();
1000 result = DISPATCHED_NOUPDATE;
1002 updwhat = cursor_update;
1006 if (boost::next(crow()) == text_.rows().end())
1007 result = FINISHED_DOWN;
1010 text_.clearSelection();
1011 result = DISPATCHED_NOUPDATE;
1013 updwhat = cursor_update;
1016 case LFUN_BACKSPACE: {
1017 if (text_.selection.set())
1018 text_.cutSelection(true, false);
1021 updwhat = CURSOR_PAR;
1027 if (text_.selection.set()) {
1028 text_.cutSelection(true, false);
1032 updwhat = CURSOR_PAR;
1038 text_.cutSelection(true, true);
1039 updwhat = CURSOR_PAR;
1046 text_.copySelection();
1047 updwhat = CURSOR_PAR;
1050 case LFUN_PASTESELECTION:
1052 string const clip(bv->getClipboard());
1056 if (cmd.argument == "paragraph") {
1057 text_.insertStringAsParagraphs(clip);
1059 text_.insertStringAsLines(clip);
1062 text_.clearSelection();
1064 updwhat = CURSOR_PAR;
1070 if (!autoBreakRows) {
1071 if (CutAndPaste::nrOfParagraphs() > 1) {
1072 #ifdef WITH_WARNINGS
1073 #warning FIXME horrendously bad UI
1075 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1080 replaceSelection(bv->getLyXText());
1081 size_t sel_index = 0;
1082 string const & arg = cmd.argument;
1083 if (isStrUnsignedInt(arg)) {
1084 size_t const paste_arg = strToUnsignedInt(arg);
1085 #warning FIXME Check if the arg is in the domain of available selections.
1086 sel_index = paste_arg;
1088 text_.pasteSelection(sel_index);
1090 text_.clearSelection();
1091 updwhat = CURSOR_PAR;
1096 case LFUN_BREAKPARAGRAPH:
1097 if (!autoBreakRows) {
1098 result = DISPATCHED;
1101 replaceSelection(bv->getLyXText());
1102 text_.breakParagraph(paragraphs, 0);
1103 updwhat = CURSOR | FULL;
1107 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1108 if (!autoBreakRows) {
1109 result = DISPATCHED;
1112 replaceSelection(bv->getLyXText());
1113 text_.breakParagraph(paragraphs, 1);
1114 updwhat = CURSOR | FULL;
1118 case LFUN_BREAKLINE: {
1119 if (!autoBreakRows) {
1120 result = DISPATCHED;
1124 replaceSelection(bv->getLyXText());
1125 text_.insertInset(new InsetNewline);
1126 updwhat = CURSOR | CURSOR_PAR;
1132 // do not set layouts on non breakable textinsets
1133 if (autoBreakRows) {
1134 string cur_layout = cpar()->layout()->name();
1136 // Derive layout number from given argument (string)
1137 // and current buffer's textclass (number). */
1138 LyXTextClass const & tclass =
1139 bv->buffer()->params.getLyXTextClass();
1140 string layout = cmd.argument;
1141 bool hasLayout = tclass.hasLayout(layout);
1143 // If the entry is obsolete, use the new one instead.
1145 string const & obs =
1146 tclass[layout]->obsoleted_by();
1151 // see if we found the layout number:
1153 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1154 bv->owner()->dispatch(lf);
1158 if (cur_layout != layout) {
1159 cur_layout = layout;
1160 text_.setLayout(layout);
1161 bv->owner()->setLayout(cpar()->layout()->name());
1162 updwhat = CURSOR_PAR;
1166 // reset the layout box
1167 bv->owner()->setLayout(cpar()->layout()->name());
1170 case LFUN_PARAGRAPH_SPACING:
1171 // This one is absolutely not working. When fiddling with this
1172 // it also seems to me that the paragraphs inside the insettext
1173 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1174 // FIXME: how old is this comment ? ...
1176 ParagraphList::iterator pit = text_.cursor.par();
1177 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1178 float cur_value = 1.0;
1179 if (cur_spacing == Spacing::Other) {
1180 cur_value = pit->params().spacing().getValue();
1183 istringstream istr(STRCONV(cmd.argument));
1186 Spacing::Space new_spacing = cur_spacing;
1187 float new_value = cur_value;
1189 lyxerr << "Missing argument to `paragraph-spacing'"
1191 } else if (tmp == "single") {
1192 new_spacing = Spacing::Single;
1193 } else if (tmp == "onehalf") {
1194 new_spacing = Spacing::Onehalf;
1195 } else if (tmp == "double") {
1196 new_spacing = Spacing::Double;
1197 } else if (tmp == "other") {
1198 new_spacing = Spacing::Other;
1201 lyxerr << "new_value = " << tmpval << endl;
1204 } else if (tmp == "default") {
1205 new_spacing = Spacing::Default;
1207 lyxerr << _("Unknown spacing argument: ")
1208 << cmd.argument << endl;
1210 if (cur_spacing != new_spacing || cur_value != new_value) {
1211 pit->params().spacing(Spacing(new_spacing, new_value));
1212 updwhat = CURSOR_PAR;
1218 // These need to do repaints but don't require
1219 // special handling otherwise. A *lot* of the
1220 // above could probably be done similarly ...
1225 case LFUN_WORDRIGHT:
1226 // these two are really unhandled ...
1228 case LFUN_BEGINNINGBUF:
1229 updwhat = cursor_update;
1230 if (!bv->dispatch(cmd))
1231 result = UNDISPATCHED;
1240 case LFUN_WORDLEFTSEL:
1241 case LFUN_WORDRIGHTSEL:
1242 updwhat = SELECTION;
1247 if (!bv->dispatch(cmd))
1248 result = UNDISPATCHED;
1253 updateLocal(bv, updwhat, updflag);
1254 /// If the action has deleted all text in the inset, we need to change the
1255 // language to the language of the surronding text.
1256 if (!was_empty && paragraphs.begin()->empty() &&
1257 paragraphs.size() == 1) {
1258 LyXFont font(LyXFont::ALL_IGNORE);
1259 font.setLanguage(bv->getParentLanguage(this));
1260 setFont(bv, font, false);
1263 if (result >= FINISHED)
1264 bv->unlockInset(this);
1266 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1267 result = DISPATCHED;
1272 int InsetText::latex(Buffer const * buf, ostream & os,
1273 LatexRunParams const & runparams) const
1276 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1277 return texrow.rows();
1281 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1283 unsigned int lines = 0;
1285 ParagraphList::const_iterator beg = paragraphs.begin();
1286 ParagraphList::const_iterator end = paragraphs.end();
1287 ParagraphList::const_iterator it = beg;
1288 for (; it != end; ++it) {
1289 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1290 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1297 int InsetText::linuxdoc(Buffer const * buf, ostream & os) const
1299 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1300 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1302 // There is a confusion between the empty paragraph and the default paragraph
1303 // The default paragraph is <p></p>, the empty paragraph is *empty*
1304 // Since none of the floats of linuxdoc accepts standard paragraphs
1305 // I disable them. I don't expect problems. (jamatos 2003/07/27)
1306 for (; pit != pend; ++pit) {
1307 const string name = pit->layout()->latexname();
1309 sgml::openTag(os, 1, 0, name);
1310 buf->simpleLinuxDocOnePar(os, pit, 0);
1312 sgml::closeTag(os, 1, 0, name);
1318 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1320 unsigned int lines = 0;
1322 vector<string> environment_stack(10);
1323 vector<string> environment_inner(10);
1325 int const command_depth = 0;
1328 Paragraph::depth_type depth = 0; // paragraph depth
1330 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1331 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1333 for (; pit != pend; ++pit) {
1335 int desc_on = 0; // description mode
1337 LyXLayout_ptr const & style = pit->layout();
1339 // environment tag closing
1340 for (; depth > pit->params().depth(); --depth) {
1341 if (environment_inner[depth] != "!-- --") {
1342 item_name = "listitem";
1343 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1344 if (environment_inner[depth] == "varlistentry")
1345 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1347 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1348 environment_stack[depth].erase();
1349 environment_inner[depth].erase();
1352 if (depth == pit->params().depth()
1353 && environment_stack[depth] != style->latexname()
1354 && !environment_stack[depth].empty()) {
1355 if (environment_inner[depth] != "!-- --") {
1356 item_name= "listitem";
1357 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1358 if (environment_inner[depth] == "varlistentry")
1359 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1362 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1364 environment_stack[depth].erase();
1365 environment_inner[depth].erase();
1368 // Write opening SGML tags.
1369 switch (style->latextype) {
1370 case LATEX_PARAGRAPH:
1371 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1375 buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1379 case LATEX_ENVIRONMENT:
1380 case LATEX_ITEM_ENVIRONMENT:
1381 if (depth < pit->params().depth()) {
1382 depth = pit->params().depth();
1383 environment_stack[depth].erase();
1386 if (environment_stack[depth] != style->latexname()) {
1387 if (environment_stack.size() == depth + 1) {
1388 environment_stack.push_back("!-- --");
1389 environment_inner.push_back("!-- --");
1391 environment_stack[depth] = style->latexname();
1392 environment_inner[depth] = "!-- --";
1393 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1395 if (environment_inner[depth] != "!-- --") {
1396 item_name= "listitem";
1397 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1398 if (environment_inner[depth] == "varlistentry")
1399 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1403 if (style->latextype == LATEX_ENVIRONMENT) {
1404 if (!style->latexparam().empty()) {
1405 if (style->latexparam() == "CDATA")
1408 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1413 desc_on = (style->labeltype == LABEL_MANUAL);
1415 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1416 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1418 item_name = desc_on ? "term" : "para";
1419 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1423 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1427 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1430 // write closing SGML tags
1431 switch (style->latextype) {
1432 case LATEX_ENVIRONMENT:
1433 if (!style->latexparam().empty()) {
1434 if (style->latexparam() == "CDATA")
1437 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1440 case LATEX_ITEM_ENVIRONMENT:
1441 if (desc_on == 1) break;
1443 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1445 case LATEX_PARAGRAPH:
1446 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1449 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1455 for (int d = depth; d >= 0; --d) {
1456 if (!environment_stack[depth].empty()) {
1457 if (environment_inner[depth] != "!-- --") {
1458 item_name = "listitem";
1459 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1460 if (environment_inner[depth] == "varlistentry")
1461 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1464 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1472 void InsetText::validate(LaTeXFeatures & features) const
1474 for_each(paragraphs.begin(), paragraphs.end(),
1475 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1479 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1481 if (the_locking_inset) {
1482 the_locking_inset->getCursor(bv, x, y);
1486 y = cy() + InsetText::y();
1490 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1492 if (the_locking_inset) {
1493 the_locking_inset->getCursorPos(bv, x, y);
1496 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1497 y = cy() - TEXT_TO_INSET_OFFSET;
1501 int InsetText::insetInInsetY() const
1503 if (!the_locking_inset)
1506 return inset_y + the_locking_inset->insetInInsetY();
1510 void InsetText::fitInsetCursor(BufferView * bv) const
1512 if (the_locking_inset) {
1513 the_locking_inset->fitInsetCursor(bv);
1517 LyXFont const font = text_.getFont(cpar(), cpos());
1519 int const asc = font_metrics::maxAscent(font);
1520 int const desc = font_metrics::maxDescent(font);
1522 if (bv->fitLockedInsetCursor(cx(), cy(), asc, desc))
1523 need_update |= FULL;
1528 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1530 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1531 return moveLeftIntern(bv, false, activate_inset, selecting);
1533 return moveRightIntern(bv, true, activate_inset, selecting);
1538 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1540 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1541 return moveRightIntern(bv, true, activate_inset, selecting);
1543 return moveLeftIntern(bv, false, activate_inset, selecting);
1548 InsetText::moveRightIntern(BufferView * bv, bool front,
1549 bool activate_inset, bool selecting)
1551 ParagraphList::iterator c_par = cpar();
1553 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1554 return FINISHED_RIGHT;
1555 if (activate_inset && checkAndActivateInset(bv, front))
1557 text_.cursorRight(bv);
1559 text_.clearSelection();
1560 return DISPATCHED_NOUPDATE;
1565 InsetText::moveLeftIntern(BufferView * bv, bool front,
1566 bool activate_inset, bool selecting)
1568 if (cpar() == paragraphs.begin() && cpos() <= 0)
1570 text_.cursorLeft(bv);
1572 text_.clearSelection();
1573 if (activate_inset && checkAndActivateInset(bv, front))
1575 return DISPATCHED_NOUPDATE;
1579 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1581 if (crow() == text_.rows().begin())
1584 text_.clearSelection();
1585 return DISPATCHED_NOUPDATE;
1589 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1591 if (boost::next(crow()) == text_.rows().end())
1592 return FINISHED_DOWN;
1593 text_.cursorDown(bv);
1594 text_.clearSelection();
1595 return DISPATCHED_NOUPDATE;
1599 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1601 if (the_locking_inset) {
1602 if (the_locking_inset->insetAllowed(inset))
1603 return the_locking_inset->insertInset(bv, inset);
1606 inset->setOwner(this);
1607 text_.insertInset(inset);
1609 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1614 bool InsetText::insetAllowed(InsetOld::Code code) const
1616 // in_insetAllowed is a really gross hack,
1617 // to allow us to call the owner's insetAllowed
1618 // without stack overflow, which can happen
1619 // when the owner uses InsetCollapsable::insetAllowed()
1621 if (in_insetAllowed)
1623 in_insetAllowed = true;
1624 if (the_locking_inset)
1625 ret = the_locking_inset->insetAllowed(code);
1627 ret = owner()->insetAllowed(code);
1628 in_insetAllowed = false;
1633 UpdatableInset * InsetText::getLockingInset() const
1635 return the_locking_inset ? the_locking_inset->getLockingInset() :
1636 const_cast<InsetText *>(this);
1640 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1644 if (the_locking_inset)
1645 return the_locking_inset->getFirstLockingInsetOfType(c);
1650 bool InsetText::showInsetDialog(BufferView * bv) const
1652 if (the_locking_inset)
1653 return the_locking_inset->showInsetDialog(bv);
1658 void InsetText::getLabelList(std::vector<string> & list) const
1660 ParagraphList::const_iterator pit = paragraphs.begin();
1661 ParagraphList::const_iterator pend = paragraphs.end();
1662 for (; pit != pend; ++pit) {
1663 InsetList::const_iterator beg = pit->insetlist.begin();
1664 InsetList::const_iterator end = pit->insetlist.end();
1665 for (; beg != end; ++beg)
1666 beg->inset->getLabelList(list);
1671 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1674 if (the_locking_inset) {
1675 the_locking_inset->setFont(bv, font, toggleall, selectall);
1679 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1680 || cpar()->empty()) {
1681 text_.setFont(font, toggleall);
1686 if (text_.selection.set())
1687 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1691 text_.selection.cursor = text_.cursor;
1692 text_.cursorBottom();
1693 text_.setSelection();
1696 text_.toggleFree(font, toggleall);
1699 text_.clearSelection();
1703 bool flag = (selectall || text_.selection.set());
1706 updateLocal(bv, FULL, true);
1708 updateLocal(bv, CURSOR_PAR, true);
1712 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1714 InsetOld * inset = cpar()->getInset(cpos());
1715 if (!isHighlyEditableInset(inset))
1717 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1718 inset->localDispatch(cmd);
1719 if (!the_locking_inset)
1721 updateLocal(bv, CURSOR, false);
1726 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1727 mouse_button::state button)
1729 x -= drawTextXOffset;
1731 int dummyy = y + dim_.asc;
1732 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1733 // we only do the edit() call if the inset was hit by the mouse
1734 // or if it is a highly editable inset. So we should call this
1735 // function from our own edit with button < 0.
1736 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1738 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1747 inset_x = cix() - top_x + drawTextXOffset;
1748 inset_y = ciy() + drawTextYOffset;
1749 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1750 inset->localDispatch(cmd);
1751 if (!the_locking_inset)
1753 updateLocal(bv, CURSOR, false);
1758 void InsetText::setParagraphData(ParagraphList const & plist)
1760 // we have to unlock any locked inset otherwise we're in troubles
1761 the_locking_inset = 0;
1763 // But it it makes no difference that is a lot better.
1765 // See if this can be simplified when std::list is in effect.
1768 ParagraphList::const_iterator it = plist.begin();
1769 ParagraphList::const_iterator end = plist.end();
1770 for (; it != end; ++it) {
1771 paragraphs.push_back(*it);
1772 paragraphs.back().setInsetOwner(this);
1780 void InsetText::markNew(bool track_changes)
1782 ParagraphList::iterator pit = paragraphs.begin();
1783 ParagraphList::iterator pend = paragraphs.end();
1784 for (; pit != pend; ++pit) {
1785 if (track_changes) {
1786 pit->trackChanges();
1788 // no-op when not tracking
1789 pit->cleanChanges();
1795 void InsetText::setText(string const & data, LyXFont const & font)
1798 for (unsigned int i = 0; i < data.length(); ++i)
1799 paragraphs.begin()->insertChar(i, data[i], font);
1804 void InsetText::setAutoBreakRows(bool flag)
1806 if (flag != autoBreakRows) {
1807 autoBreakRows = flag;
1815 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1817 if (how != drawFrame_) {
1820 updateLocal(bv, DRAW_FRAME, false);
1825 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1827 if (frame_color != col) {
1830 updateLocal(bv, DRAW_FRAME, false);
1835 int InsetText::cx() const
1837 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1838 if (the_locking_inset) {
1839 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1840 if (font.isVisibleRightToLeft())
1841 x -= the_locking_inset->width();
1847 int InsetText::cix() const
1849 int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1850 if (the_locking_inset) {
1851 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1852 if (font.isVisibleRightToLeft())
1853 x -= the_locking_inset->width();
1859 int InsetText::cy() const
1861 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1865 int InsetText::ciy() const
1867 return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1871 pos_type InsetText::cpos() const
1873 return text_.cursor.pos();
1877 ParagraphList::iterator InsetText::cpar() const
1879 return text_.cursor.par();
1883 bool InsetText::cboundary() const
1885 return text_.cursor.boundary();
1889 RowList::iterator InsetText::crow() const
1891 return text_.cursorRow();
1895 LyXText * InsetText::getLyXText(BufferView const * bv,
1896 bool const recursive) const
1899 if (recursive && the_locking_inset)
1900 return the_locking_inset->getLyXText(bv, true);
1905 void InsetText::setViewCache(BufferView const * bv) const
1908 text_.bv_owner = const_cast<BufferView *>(bv);
1912 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1915 /// then remove all LyXText in text-insets
1916 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1917 const_cast<ParagraphList&>(paragraphs).end(),
1918 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1923 void InsetText::resizeLyXText(BufferView * bv, bool /*force*/) const
1925 if (paragraphs.size() == 1 && paragraphs.begin()->empty())
1934 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1935 const_cast<ParagraphList&>(paragraphs).end(),
1936 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1940 restoreLyXTextState();
1942 // seems to be unneeded
1944 if (the_locking_inset) {
1945 inset_x = cix() - top_x + drawTextXOffset;
1946 inset_y = ciy() + drawTextYOffset;
1949 text_.top_y(bv->screen().topCursorVisible(&text_));
1951 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
1952 // this will scroll the screen such that the cursor becomes visible
1953 bv->updateScrollbar();
1955 need_update |= FULL;
1961 void InsetText::reinitLyXText() const
1963 BufferView * bv = text_.bv_owner;
1970 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1971 const_cast<ParagraphList&>(paragraphs).end(),
1972 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1975 restoreLyXTextState();
1976 if (the_locking_inset) {
1977 inset_x = cix() - top_x + drawTextXOffset;
1978 inset_y = ciy() + drawTextYOffset;
1980 text_.top_y(bv->screen().topCursorVisible(&text_));
1982 // this will scroll the screen such that the cursor becomes visible
1983 bv->updateScrollbar();
1990 void InsetText::removeNewlines()
1992 bool changed = false;
1994 ParagraphList::iterator it = paragraphs.begin();
1995 ParagraphList::iterator end = paragraphs.end();
1996 for (; it != end; ++it) {
1997 for (int i = 0; i < it->size(); ++i) {
1998 if (it->isNewline(i)) {
2009 int InsetText::scroll(bool recursive) const
2011 int sx = UpdatableInset::scroll(false);
2013 if (recursive && the_locking_inset)
2014 sx += the_locking_inset->scroll(recursive);
2020 void InsetText::clearSelection(BufferView * bv)
2022 getLyXText(bv)->clearSelection();
2026 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2028 Painter & pain = bv->painter();
2030 int h = dim_.asc + dim_.des;
2031 int ty = baseline - dim_.asc;
2037 if (ty + h > pain.paperHeight())
2038 h = pain.paperHeight();
2039 if (top_x + drawTextXOffset + w > pain.paperWidth())
2040 w = pain.paperWidth();
2041 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2046 ParagraphList * InsetText::getParagraphs(int i) const
2048 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2052 LyXCursor const & InsetText::cursor(BufferView * bv) const
2054 if (the_locking_inset)
2055 return the_locking_inset->cursor(bv);
2056 return getLyXText(bv)->cursor;
2060 InsetOld * InsetText::getInsetFromID(int id_arg) const
2063 return const_cast<InsetText *>(this);
2065 ParagraphList::const_iterator pit = paragraphs.begin();
2066 ParagraphList::const_iterator pend = paragraphs.end();
2067 for (; pit != pend; ++pit) {
2068 InsetList::const_iterator it = pit->insetlist.begin();
2069 InsetList::const_iterator end = pit->insetlist.end();
2070 for (; it != end; ++it) {
2071 if (it->inset->id() == id_arg)
2073 InsetOld * in = it->inset->getInsetFromID(id_arg);
2083 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2086 if (the_locking_inset) {
2087 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2088 if (!word.word().empty()) {
2092 // we have to go on checking so move cursor to the next char
2093 text_.cursor.pos(text_.cursor.pos() + 1);
2095 word = text_.selectNextWordToSpellcheck(value);
2096 if (word.word().empty())
2097 bv->unlockInset(const_cast<InsetText *>(this));
2104 void InsetText::selectSelectedWord(BufferView * bv)
2106 if (the_locking_inset) {
2107 the_locking_inset->selectSelectedWord(bv);
2110 getLyXText(bv)->selectSelectedWord();
2111 updateLocal(bv, SELECTION, false);
2115 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2117 if (the_locking_inset) {
2118 if (the_locking_inset->nextChange(bv, length))
2120 text_.cursorRight(true);
2122 lyx::find::SearchResult result =
2123 lyx::find::findNextChange(bv, &text_, length);
2125 if (result == lyx::find::SR_FOUND) {
2126 LyXCursor cur = text_.cursor;
2127 bv->unlockInset(bv->theLockingInset());
2128 if (bv->lockInset(this))
2131 text_.setSelectionRange(length);
2132 updateLocal(bv, SELECTION, false);
2134 return result != lyx::find::SR_NOT_FOUND;
2138 bool InsetText::searchForward(BufferView * bv, string const & str,
2141 if (the_locking_inset) {
2142 if (the_locking_inset->searchForward(bv, str, cs, mw))
2144 text_.cursorRight(true);
2146 lyx::find::SearchResult result =
2147 lyx::find::find(bv, &text_, str, true, cs, mw);
2149 if (result == lyx::find::SR_FOUND) {
2150 LyXCursor cur = text_.cursor;
2151 bv->unlockInset(bv->theLockingInset());
2152 if (bv->lockInset(this))
2155 text_.setSelectionRange(str.length());
2156 updateLocal(bv, SELECTION, false);
2158 return result != lyx::find::SR_NOT_FOUND;
2162 bool InsetText::searchBackward(BufferView * bv, string const & str,
2165 if (the_locking_inset) {
2166 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2170 ParagraphList::iterator pit = paragraphs.begin();
2171 ParagraphList::iterator pend = paragraphs.end();
2173 while (boost::next(pit) != pend)
2176 text_.setCursor(pit, pit->size());
2178 lyx::find::SearchResult result =
2179 lyx::find::find(bv, &text_, str, false, cs, mw);
2181 if (result == lyx::find::SR_FOUND) {
2182 LyXCursor cur = text_.cursor;
2183 bv->unlockInset(bv->theLockingInset());
2184 if (bv->lockInset(this))
2187 text_.setSelectionRange(str.length());
2188 updateLocal(bv, SELECTION, false);
2190 return result != lyx::find::SR_NOT_FOUND;
2194 bool InsetText::checkInsertChar(LyXFont & font)
2197 return owner()->checkInsertChar(font);
2202 void InsetText::collapseParagraphs(BufferView * bv)
2204 while (paragraphs.size() > 1) {
2205 ParagraphList::iterator first_par = paragraphs.begin();
2206 ParagraphList::iterator next_par = boost::next(first_par);
2207 size_t const first_par_size = first_par->size();
2209 if (!first_par->empty() &&
2210 !next_par->empty() &&
2211 !first_par->isSeparator(first_par_size - 1)) {
2212 first_par->insertChar(first_par_size, ' ');
2215 if (text_.selection.set()) {
2216 if (text_.selection.start.par() == next_par) {
2217 text_.selection.start.par(first_par);
2218 text_.selection.start.pos(
2219 text_.selection.start.pos() + first_par_size);
2221 if (text_.selection.end.par() == next_par) {
2222 text_.selection.end.par(first_par);
2223 text_.selection.end.pos(
2224 text_.selection.end.pos() + first_par_size);
2228 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2234 void InsetText::getDrawFont(LyXFont & font) const
2238 owner()->getDrawFont(font);
2242 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2244 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2245 // And it probably does. You have to take a look at this John. (Lgb)
2246 #warning John, have a look here. (Lgb)
2247 ParagraphList::iterator pit = plist.begin();
2248 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2250 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2252 ParagraphList::iterator pend = plist.end();
2253 for (; pit != pend; ++pit) {
2254 paragraphs.push_back(*pit);
2261 void InsetText::addPreview(PreviewLoader & loader) const
2263 ParagraphList::const_iterator pit = paragraphs.begin();
2264 ParagraphList::const_iterator pend = paragraphs.end();
2266 for (; pit != pend; ++pit) {
2267 InsetList::const_iterator it = pit->insetlist.begin();
2268 InsetList::const_iterator end = pit->insetlist.end();
2269 for (; it != end; ++it) {
2270 it->inset->addPreview(loader);