3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS
13 #include "insettext.h"
16 #include "BufferView.h"
17 #include "bufferview_funcs.h"
18 #include "CutAndPaste.h"
20 #include "dimension.h"
21 #include "funcrequest.h"
23 #include "errorlist.h"
25 #include "LaTeXFeatures.h"
29 #include "lyxcursor.h"
35 #include "paragraph.h"
36 #include "ParagraphParameters.h"
37 #include "trans_mgr.h"
38 #include "undo_funcs.h"
39 #include "WordLangTuple.h"
40 #include "paragraph_funcs.h"
42 #include "rowpainter.h"
43 #include "insetnewline.h"
44 #include "metricsinfo.h"
46 #include "frontends/Alert.h"
47 #include "frontends/Dialogs.h"
48 #include "frontends/font_metrics.h"
49 #include "frontends/LyXView.h"
50 #include "frontends/Painter.h"
51 #include "frontends/screen.h"
53 #include "support/textutils.h"
54 #include "support/LAssert.h"
55 #include "support/lstrings.h"
56 #include "support/lyxalgo.h" // lyx::count
58 #include <boost/bind.hpp>
74 using namespace lyx::support;
75 using namespace lyx::graphics;
76 using namespace bv_funcs;
79 using lyx::textclass_type;
81 // These functions should probably go into bufferview_funcs somehow (Jug)
83 void InsetText::saveLyXTextState() const
85 // check if my paragraphs are still valid
86 ParagraphList::iterator it = const_cast<ParagraphList&>(paragraphs).begin();
87 ParagraphList::iterator end = const_cast<ParagraphList&>(paragraphs).end();
88 for (; it != end; ++it) {
89 if (it == text_.cursor.par())
93 if (it != end && text_.cursor.pos() <= it->size())
94 sstate = text_; // slicing intended
96 sstate.cursor.par(end);
100 void InsetText::restoreLyXTextState() const
102 if (sstate.cursor.par() == const_cast<ParagraphList&>(paragraphs).end())
105 text_.selection.set(true);
106 // at this point just to avoid the DEPM when setting the cursor
107 text_.selection.mark(sstate.selection.mark());
108 if (sstate.selection.set()) {
109 text_.setCursor(sstate.selection.start.par(),
110 sstate.selection.start.pos(),
111 true, sstate.selection.start.boundary());
112 text_.selection.cursor = text_.cursor;
113 text_.setCursor(sstate.selection.end.par(), sstate.selection.end.pos(),
114 true, sstate.selection.end.boundary());
115 text_.setSelection();
116 text_.setCursor(sstate.cursor.par(), sstate.cursor.pos());
118 text_.setCursor(sstate.cursor.par(), sstate.cursor.pos(),
119 true, sstate.cursor.boundary());
120 text_.selection.cursor = text_.cursor;
121 text_.selection.set(false);
126 InsetText::InsetText(BufferParams const & bp)
127 : UpdatableInset(), text_(0, this)
129 paragraphs.push_back(Paragraph());
130 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
131 if (bp.tracking_changes)
132 paragraphs.begin()->trackChanges();
137 InsetText::InsetText(InsetText const & in)
138 : UpdatableInset(in), text_(0, this)
144 InsetText & InsetText::operator=(InsetText const & it)
151 void InsetText::init(InsetText const * ins)
154 textwidth_ = ins->textwidth_;
155 text_.bv_owner = ins->text_.bv_owner;
156 setParagraphData(ins->paragraphs);
157 autoBreakRows = ins->autoBreakRows;
158 drawFrame_ = ins->drawFrame_;
159 frame_color = ins->frame_color;
161 textwidth_ = 0; // unbounded
163 frame_color = LColor::insetframe;
164 autoBreakRows = false;
166 the_locking_inset = 0;
167 for_each(paragraphs.begin(), paragraphs.end(),
168 boost::bind(&Paragraph::setInsetOwner, _1, this));
175 old_par = paragraphs.end();
176 last_drawn_width = -1;
177 sstate.cursor.par(paragraphs.end());
178 in_insetAllowed = false;
182 void InsetText::clear(bool just_mark_erased)
184 if (just_mark_erased) {
185 ParagraphList::iterator it = paragraphs.begin();
186 ParagraphList::iterator end = paragraphs.end();
187 for (; it != end; ++it) {
194 // This is a gross hack...
195 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
198 paragraphs.push_back(Paragraph());
199 paragraphs.begin()->setInsetOwner(this);
200 paragraphs.begin()->layout(old_layout);
207 InsetBase * InsetText::clone() const
209 return new InsetText(*this);
213 void InsetText::write(Buffer const * buf, ostream & os) const
216 writeParagraphData(buf, os);
220 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
222 ParagraphList::const_iterator it = paragraphs.begin();
223 ParagraphList::const_iterator end = paragraphs.end();
224 Paragraph::depth_type dth = 0;
225 for (; it != end; ++it) {
226 it->write(buf, os, buf->params, dth);
231 void InsetText::read(Buffer const * buf, LyXLex & lex)
234 Paragraph::depth_type depth = 0;
238 if (buf->params.tracking_changes)
239 paragraphs.begin()->trackChanges();
241 // delete the initial paragraph
243 ParagraphList::iterator pit = paragraphs.begin();
247 token = lex.getString();
250 if (token == "\\end_inset") {
254 if (token == "\\the_end") {
255 lex.printError("\\the_end read in inset! Error in document!");
260 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
263 pit = paragraphs.begin();
264 ParagraphList::iterator const end = paragraphs.end();
265 for (; pit != end; ++pit)
266 pit->setInsetOwner(this);
268 if (token != "\\end_inset") {
269 lex.printError("Missing \\end_inset at this point. "
276 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
278 //lyxerr << "InsetText::metrics: " << getInsetName()
279 // << " width: " << mi.base.textwidth << "\n";
280 if (mi.base.textwidth)
281 textwidth_ = mi.base.textwidth;
282 BufferView * bv = mi.base.bv;
284 text_.rebuild(mi.base.textwidth);
285 dim.asc = text_.rows().begin()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
286 dim.des = text_.height - dim.asc + TEXT_TO_INSET_OFFSET;
287 dim.wid = max(textwidth_, int(text_.width)) + 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;
327 top_baseline = baseline;
328 top_y = baseline - dim_.asc;
330 if (last_drawn_width != dim_.wid) {
332 last_drawn_width = dim_.wid;
335 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
336 inset_x = cix(bv) - x + drawTextXOffset;
337 inset_y = ciy() + drawTextYOffset;
340 x += TEXT_TO_INSET_OFFSET;
342 RowList::iterator rowit = text_.rows().begin();
343 RowList::iterator end = text_.rows().end();
345 int y_offset = baseline - rowit->ascent_of_text();
346 int ph = pain.paperHeight();
349 while (rowit != end && y + rowit->height() <= 0) {
350 y += rowit->height();
351 first += rowit->height();
355 text_.top_y(-y_offset);
363 int yf = y_offset + first;
368 while (rowit != end && yf < ph) {
369 paintRows(*bv, text_, rowit,
370 y + y_offset + first, int(x), y + text_.top_y());
371 y += rowit->height();
372 yf += rowit->height();
378 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked)) {
379 drawFrame(pain, int(start_x));
382 if (need_update != INIT) {
388 void InsetText::drawFrame(Painter & pain, int x) const
390 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
392 frame_y = top_baseline - dim_.asc + ttoD2;
393 frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
394 frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
395 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
400 void InsetText::setUpdateStatus(int what) const
403 // we will to redraw us full if our LyXText wants it
404 if (text_.needRefresh())
407 // this to not draw a selection when we redraw all of it!
408 if (need_update & CURSOR && !(need_update & SELECTION)) {
409 if (text_.selection.set())
411 text_.clearSelection();
416 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
421 if (!autoBreakRows && paragraphs.size() > 1)
422 collapseParagraphs(bv);
424 text_.partialRebreak();
425 setUpdateStatus(what);
426 bool flag = mark_dirty ||
427 ((need_update != CURSOR && need_update != NONE) ||
428 text_.needRefresh() || text_.selection.set());
429 if (!text_.selection.set())
430 text_.selection.cursor = text_.cursor;
436 bv->updateInset(const_cast<InsetText *>(this));
439 if (need_update == CURSOR)
441 bv->owner()->view_state_changed();
442 bv->owner()->updateMenubar();
443 bv->owner()->updateToolbar();
444 if (old_par != cpar()) {
445 bv->owner()->setLayout(cpar()->layout()->name());
451 string const InsetText::editMessage() const
453 return _("Opened Text Inset");
457 void InsetText::insetUnlock(BufferView * bv)
459 if (the_locking_inset) {
460 the_locking_inset->insetUnlock(bv);
461 the_locking_inset = 0;
462 updateLocal(bv, CURSOR_PAR, false);
468 if (text_.selection.set()) {
469 text_.clearSelection();
471 } else if (owner()) {
472 bv->owner()->setLayout(owner()->getLyXText(bv)
473 ->cursor.par()->layout()->name());
475 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
476 // hack for deleteEmptyParMech
477 ParagraphList::iterator first_par = paragraphs.begin();
478 if (!first_par->empty()) {
479 text_.setCursor(first_par, 0);
480 } else if (paragraphs.size() > 1) {
481 text_.setCursor(boost::next(first_par), 0);
484 updateLocal(bv, code, false);
487 setUpdateStatus(code);
492 void InsetText::lockInset(BufferView * bv)
495 the_locking_inset = 0;
496 inset_pos = inset_x = inset_y = 0;
497 inset_boundary = false;
498 inset_par = paragraphs.end();
499 old_par = paragraphs.end();
500 text_.setCursorIntern(paragraphs.begin(), 0);
501 text_.clearSelection();
503 // If the inset is empty set the language of the current font to the
504 // language to the surronding text (if different).
505 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
506 bv->getParentLanguage(this) != text_.current_font.language()) {
507 LyXFont font(LyXFont::ALL_IGNORE);
508 font.setLanguage(bv->getParentLanguage(this));
509 setFont(bv, font, false);
512 if (drawFrame_ == LOCKED)
513 code = CURSOR|DRAW_FRAME;
514 setUpdateStatus(code);
518 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
520 the_locking_inset = inset;
521 inset_x = cix(bv) - top_x + drawTextXOffset;
522 inset_y = ciy() + drawTextYOffset;
525 inset_boundary = cboundary();
526 //updateLocal(bv, CURSOR, false);
530 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
532 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
536 if (!the_locking_inset) {
537 ParagraphList::iterator pit = paragraphs.begin();
538 ParagraphList::iterator pend = paragraphs.end();
540 int const id = inset->id();
541 for (; pit != pend; ++pit) {
542 InsetList::iterator it = pit->insetlist.begin();
543 InsetList::iterator const end = pit->insetlist.end();
544 for (; it != end; ++it) {
545 if (it->inset == inset) {
546 lyxerr << "InsetText::lockInsetInInset: 1 a\n";
547 text_.setCursorIntern(pit, it->pos);
548 lyxerr << "InsetText::lockInsetInInset: 1 b\n";
549 lyxerr << "bv: " << bv << " inset: " << inset << "\n";
550 lockInset(bv, inset);
551 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
554 if (it->inset->getInsetFromID(id)) {
555 lyxerr << "InsetText::lockInsetInInset: 2\n";
556 text_.setCursorIntern(pit, it->pos);
557 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
558 return the_locking_inset->lockInsetInInset(bv, inset);
562 lyxerr << "InsetText::lockInsetInInset: 3\n";
565 if (inset == cpar()->getInset(cpos())) {
566 lyxerr[Debug::INSETS] << "OK" << endl;
567 lockInset(bv, inset);
571 if (the_locking_inset && the_locking_inset == inset) {
572 if (cpar() == inset_par && cpos() == inset_pos) {
573 lyxerr[Debug::INSETS] << "OK" << endl;
574 inset_x = cix(bv) - top_x + drawTextXOffset;
575 inset_y = ciy() + drawTextYOffset;
577 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
579 } else if (the_locking_inset) {
580 lyxerr[Debug::INSETS] << "MAYBE" << endl;
581 return the_locking_inset->lockInsetInInset(bv, inset);
583 lyxerr[Debug::INSETS] << "NOT OK" << endl;
588 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
591 if (!the_locking_inset)
593 if (the_locking_inset == inset) {
594 the_locking_inset->insetUnlock(bv);
595 getLyXText(bv)->updateInset(inset);
596 the_locking_inset = 0;
598 moveRightIntern(bv, true, false);
599 old_par = paragraphs.end(); // force layout setting
603 updateLocal(bv, CURSOR, false);
606 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
610 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
612 if (!autoBreakRows && paragraphs.size() > 1)
613 collapseParagraphs(bv);
618 if (inset->owner() != this) {
619 int ustat = CURSOR_PAR;
621 UpdatableInset * tl_inset = the_locking_inset;
623 found = tl_inset->updateInsetInInset(bv, inset);
625 tl_inset = static_cast<UpdatableInset *>(inset);
626 while(tl_inset->owner() && tl_inset->owner() != this)
627 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
628 if (!tl_inset->owner())
630 found = tl_inset->updateInsetInInset(bv, inset);
634 text_.updateInset(tl_inset);
636 setUpdateStatus(ustat);
639 bool found = text_.updateInset(inset);
641 setUpdateStatus(CURSOR_PAR);
642 if (the_locking_inset &&
643 cpar() == inset_par && cpos() == inset_pos)
645 inset_x = cix(bv) - top_x + drawTextXOffset;
646 inset_y = ciy() + drawTextYOffset;
653 void InsetText::lfunMousePress(FuncRequest const & cmd)
657 // use this to check mouse motion for selection!
661 BufferView * bv = cmd.view();
662 FuncRequest cmd1 = cmd;
668 int tmp_x = cmd.x - drawTextXOffset;
669 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
670 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
672 if (the_locking_inset) {
673 if (the_locking_inset == inset) {
674 the_locking_inset->localDispatch(cmd1);
677 // otherwise only unlock the_locking_inset
678 the_locking_inset->insetUnlock(bv);
679 the_locking_inset = 0;
682 no_selection = false;
684 if (bv->theLockingInset()) {
685 if (isHighlyEditableInset(inset)) {
686 // We just have to lock the inset before calling a
688 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
689 if (!bv->lockInset(uinset)) {
690 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
692 inset->localDispatch(cmd1);
693 if (the_locking_inset)
694 updateLocal(bv, CURSOR, false);
699 bool paste_internally = false;
700 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
701 localDispatch(FuncRequest(bv, LFUN_COPY));
702 paste_internally = true;
704 int old_top_y = text_.top_y();
706 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
708 // set the selection cursor!
709 text_.selection.cursor = text_.cursor;
710 text_.cursor.x_fix(text_.cursor.x());
712 if (text_.selection.set()) {
713 text_.clearSelection();
714 updateLocal(bv, FULL, false);
716 text_.clearSelection();
717 updateLocal(bv, CURSOR, false);
720 bv->owner()->setLayout(cpar()->layout()->name());
722 // we moved the view we cannot do mouse selection in this case!
723 if (getLyXText(bv)->top_y() != old_top_y)
726 // Insert primary selection with middle mouse
727 // if there is a local selection in the current buffer,
729 if (cmd.button() == mouse_button::button2) {
730 if (paste_internally)
731 localDispatch(FuncRequest(bv, LFUN_PASTE));
733 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
736 getLyXText(bv)->clearSelection();
741 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
743 BufferView * bv = cmd.view();
744 FuncRequest cmd1 = cmd;
749 if (the_locking_inset)
750 return the_locking_inset->localDispatch(cmd1);
752 int tmp_x = cmd.x - drawTextXOffset;
753 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
754 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
757 // This code should probably be removed now. Simple insets
758 // (!highlyEditable) can actually take the localDispatch,
759 // and turn it into edit() if necessary. But we still
760 // need to deal properly with the whole relative vs.
761 // absolute mouse co-ords thing in a realiable, sensible way
763 if (isHighlyEditableInset(inset))
764 ret = inset->localDispatch(cmd1);
766 inset_x = cix(bv) - top_x + drawTextXOffset;
767 inset_y = ciy() + drawTextYOffset;
768 cmd1.x = cmd.x - inset_x;
769 cmd1.y = cmd.x - inset_y;
770 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
774 ret = inset->localDispatch(cmd1);
775 updateLocal(bv, CURSOR_PAR, false);
782 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
784 FuncRequest cmd1 = cmd;
788 if (the_locking_inset) {
789 the_locking_inset->localDispatch(cmd1);
793 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
796 BufferView * bv = cmd.view();
797 LyXCursor cur = text_.cursor;
798 text_.setCursorFromCoordinates
799 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
800 text_.cursor.x_fix(text_.cursor.x());
801 if (cur == text_.cursor)
803 text_.setSelection();
804 bool flag = (text_.toggle_cursor.par() != text_.toggle_end_cursor.par() ||
805 text_.toggle_cursor.pos() != text_.toggle_end_cursor.pos());
807 updateLocal(bv, SELECTION, false);
812 Inset::RESULT InsetText::localDispatch(FuncRequest const & cmd)
814 BufferView * bv = cmd.view();
817 if (cmd.action == LFUN_INSET_EDIT) {
818 UpdatableInset::localDispatch(cmd);
820 if (!bv->lockInset(this)) {
821 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
826 the_locking_inset = 0;
827 inset_pos = inset_x = inset_y = 0;
828 inset_boundary = false;
829 inset_par = paragraphs.end();
830 old_par = paragraphs.end();
833 if (cmd.argument.size()) {
834 if (cmd.argument == "left")
835 text_.setCursorIntern(paragraphs.begin(), 0);
837 ParagraphList::iterator it = paragraphs.begin();
838 ParagraphList::iterator end = paragraphs.end();
839 while (boost::next(it) != end)
841 // int const pos = (p->size() ? p->size()-1 : p->size());
842 text_.setCursor(it, it->size());
845 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
846 // we put here -1 and not button as now the button in the
847 // edit call should not be needed we will fix this in 1.3.x
848 // cycle hopefully (Jug 20020509)
849 // FIXME: GUII I've changed this to none: probably WRONG
850 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
851 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
853 text_.cursor.x_fix(text_.cursor.x());
857 text_.clearSelection();
860 // If the inset is empty set the language of the current font to the
861 // language to the surronding text (if different).
862 if (paragraphs.begin()->empty() &&
863 paragraphs.size() == 1 &&
864 bv->getParentLanguage(this) != text_.current_font.language())
866 LyXFont font(LyXFont::ALL_IGNORE);
867 font.setLanguage(bv->getParentLanguage(this));
868 setFont(bv, font, false);
872 if (drawFrame_ == LOCKED)
873 code = CURSOR | DRAW_FRAME;
875 updateLocal(bv, code, false);
876 // Tell the paragraph dialog that we've entered an insettext.
877 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
882 switch (cmd.action) {
883 case LFUN_MOUSE_PRESS:
886 case LFUN_MOUSE_MOTION:
887 lfunMouseMotion(cmd);
889 case LFUN_MOUSE_RELEASE:
890 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
895 bool was_empty = (paragraphs.begin()->empty() &&
896 paragraphs.size() == 1);
898 no_selection = false;
899 RESULT result = UpdatableInset::localDispatch(cmd);
900 if (result != UNDISPATCHED)
904 if (cmd.action < 0 && cmd.argument.empty())
907 if (the_locking_inset) {
908 result = the_locking_inset->localDispatch(cmd);
909 if (result == DISPATCHED_NOUPDATE)
911 else if (result == DISPATCHED) {
912 updateLocal(bv, CURSOR_PAR, false);
914 } else if (result >= FINISHED) {
917 moveRightIntern(bv, false, false);
921 if ((result = moveUp(bv)) >= FINISHED) {
922 updateLocal(bv, CURSOR, false);
923 bv->unlockInset(this);
927 if ((result = moveDown(bv)) >= FINISHED) {
928 updateLocal(bv, CURSOR, false);
929 bv->unlockInset(this);
936 the_locking_inset = 0;
937 updateLocal(bv, CURSOR, false);
938 // make sure status gets reset immediately
939 bv->owner()->clearMessage();
946 // what type of update to do on a cursor movement
947 int cursor_update = CURSOR;
949 if (text_.selection.set())
950 cursor_update = SELECTION;
952 switch (cmd.action) {
955 case LFUN_SELFINSERT:
956 if (bv->buffer()->isReadonly()) {
957 // setErrorMessage(N_("Document is read only"));
960 if (!cmd.argument.empty()) {
961 /* Automatically delete the currently selected
962 * text and replace it with what is being
963 * typed in now. Depends on lyxrc settings
964 * "auto_region_delete", which defaults to
967 // This should not be needed here and is also WRONG!
968 setUndo(bv, Undo::INSERT, text_.cursor.par());
971 if (lyxrc.auto_region_delete) {
972 if (text_.selection.set()) {
973 text_.cutSelection(false, false);
976 text_.clearSelection();
977 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
978 bv->owner()->getIntl().getTransManager().
979 TranslateAndInsert(cmd.argument[i], &text_);
982 text_.selection.cursor = text_.cursor;
983 updwhat = CURSOR | CURSOR_PAR;
985 result = DISPATCHED_NOUPDATE;
988 // cursor movements that need special handling
991 result = moveRight(bv);
993 updwhat = cursor_update;
997 result = moveLeft(bv);
998 updwhat = cursor_update;
1002 result = moveDown(bv);
1003 updwhat = cursor_update;
1007 result = moveUp(bv);
1008 updwhat = cursor_update;
1012 if (crow() == text_.rows().begin())
1013 result = FINISHED_UP;
1015 text_.cursorPrevious();
1016 text_.clearSelection();
1017 result = DISPATCHED_NOUPDATE;
1019 updwhat = cursor_update;
1023 if (boost::next(crow()) == text_.rows().end())
1024 result = FINISHED_DOWN;
1027 text_.clearSelection();
1028 result = DISPATCHED_NOUPDATE;
1030 updwhat = cursor_update;
1033 case LFUN_BACKSPACE: {
1034 if (text_.selection.set())
1035 text_.cutSelection(true, false);
1038 updwhat = CURSOR_PAR;
1044 if (text_.selection.set()) {
1045 text_.cutSelection(true, false);
1049 updwhat = CURSOR_PAR;
1055 text_.cutSelection(true, true);
1056 updwhat = CURSOR_PAR;
1063 text_.copySelection();
1064 updwhat = CURSOR_PAR;
1067 case LFUN_PASTESELECTION:
1069 string const clip(bv->getClipboard());
1073 if (cmd.argument == "paragraph") {
1074 text_.insertStringAsParagraphs(clip);
1076 text_.insertStringAsLines(clip);
1079 text_.clearSelection();
1081 updwhat = CURSOR_PAR;
1087 if (!autoBreakRows) {
1088 if (CutAndPaste::nrOfParagraphs() > 1) {
1089 #ifdef WITH_WARNINGS
1090 #warning FIXME horrendously bad UI
1092 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1097 replaceSelection(bv->getLyXText());
1098 size_t sel_index = 0;
1099 string const & arg = cmd.argument;
1100 if (isStrUnsignedInt(arg)) {
1101 size_t const paste_arg = strToUnsignedInt(arg);
1102 #warning FIXME Check if the arg is in the domain of available selections.
1103 sel_index = paste_arg;
1105 text_.pasteSelection(sel_index);
1107 text_.clearSelection();
1108 updwhat = CURSOR_PAR;
1113 case LFUN_BREAKPARAGRAPH:
1114 if (!autoBreakRows) {
1115 result = DISPATCHED;
1118 replaceSelection(bv->getLyXText());
1119 text_.breakParagraph(paragraphs, 0);
1120 updwhat = CURSOR | FULL;
1124 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1125 if (!autoBreakRows) {
1126 result = DISPATCHED;
1129 replaceSelection(bv->getLyXText());
1130 text_.breakParagraph(paragraphs, 1);
1131 updwhat = CURSOR | FULL;
1135 case LFUN_BREAKLINE: {
1136 if (!autoBreakRows) {
1137 result = DISPATCHED;
1141 replaceSelection(bv->getLyXText());
1142 text_.insertInset(new InsetNewline);
1143 updwhat = CURSOR | CURSOR_PAR;
1149 // do not set layouts on non breakable textinsets
1150 if (autoBreakRows) {
1151 string cur_layout = cpar()->layout()->name();
1153 // Derive layout number from given argument (string)
1154 // and current buffer's textclass (number). */
1155 LyXTextClass const & tclass =
1156 bv->buffer()->params.getLyXTextClass();
1157 string layout = cmd.argument;
1158 bool hasLayout = tclass.hasLayout(layout);
1160 // If the entry is obsolete, use the new one instead.
1162 string const & obs =
1163 tclass[layout]->obsoleted_by();
1168 // see if we found the layout number:
1170 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1171 bv->owner()->dispatch(lf);
1175 if (cur_layout != layout) {
1176 cur_layout = layout;
1177 text_.setLayout(layout);
1178 bv->owner()->setLayout(cpar()->layout()->name());
1179 updwhat = CURSOR_PAR;
1183 // reset the layout box
1184 bv->owner()->setLayout(cpar()->layout()->name());
1187 case LFUN_PARAGRAPH_SPACING:
1188 // This one is absolutely not working. When fiddling with this
1189 // it also seems to me that the paragraphs inside the insettext
1190 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1191 // FIXME: how old is this comment ? ...
1193 ParagraphList::iterator pit = text_.cursor.par();
1194 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1195 float cur_value = 1.0;
1196 if (cur_spacing == Spacing::Other) {
1197 cur_value = pit->params().spacing().getValue();
1200 istringstream istr(STRCONV(cmd.argument));
1203 Spacing::Space new_spacing = cur_spacing;
1204 float new_value = cur_value;
1206 lyxerr << "Missing argument to `paragraph-spacing'"
1208 } else if (tmp == "single") {
1209 new_spacing = Spacing::Single;
1210 } else if (tmp == "onehalf") {
1211 new_spacing = Spacing::Onehalf;
1212 } else if (tmp == "double") {
1213 new_spacing = Spacing::Double;
1214 } else if (tmp == "other") {
1215 new_spacing = Spacing::Other;
1218 lyxerr << "new_value = " << tmpval << endl;
1221 } else if (tmp == "default") {
1222 new_spacing = Spacing::Default;
1224 lyxerr << _("Unknown spacing argument: ")
1225 << cmd.argument << endl;
1227 if (cur_spacing != new_spacing || cur_value != new_value) {
1228 pit->params().spacing(Spacing(new_spacing, new_value));
1229 updwhat = CURSOR_PAR;
1235 // These need to do repaints but don't require
1236 // special handling otherwise. A *lot* of the
1237 // above could probably be done similarly ...
1242 case LFUN_WORDRIGHT:
1243 // these two are really unhandled ...
1245 case LFUN_BEGINNINGBUF:
1246 updwhat = cursor_update;
1247 if (!bv->dispatch(cmd))
1248 result = UNDISPATCHED;
1257 case LFUN_WORDLEFTSEL:
1258 case LFUN_WORDRIGHTSEL:
1259 updwhat = SELECTION;
1264 if (!bv->dispatch(cmd))
1265 result = UNDISPATCHED;
1270 updateLocal(bv, updwhat, updflag);
1271 /// If the action has deleted all text in the inset, we need to change the
1272 // language to the language of the surronding text.
1273 if (!was_empty && paragraphs.begin()->empty() &&
1274 paragraphs.size() == 1) {
1275 LyXFont font(LyXFont::ALL_IGNORE);
1276 font.setLanguage(bv->getParentLanguage(this));
1277 setFont(bv, font, false);
1280 if (result >= FINISHED)
1281 bv->unlockInset(this);
1283 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1284 result = DISPATCHED;
1289 int InsetText::latex(Buffer const * buf, ostream & os,
1290 LatexRunParams const & runparams) const
1293 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1294 return texrow.rows();
1298 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1300 unsigned int lines = 0;
1302 ParagraphList::const_iterator beg = paragraphs.begin();
1303 ParagraphList::const_iterator end = paragraphs.end();
1304 ParagraphList::const_iterator it = beg;
1305 for (; it != end; ++it) {
1306 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1307 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1314 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1316 unsigned int lines = 0;
1318 vector<string> environment_stack(10);
1319 vector<string> environment_inner(10);
1321 int const command_depth = 0;
1324 Paragraph::depth_type depth = 0; // paragraph depth
1326 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1327 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1329 for (; pit != pend; ++pit) {
1331 int desc_on = 0; // description mode
1333 LyXLayout_ptr const & style = pit->layout();
1335 // environment tag closing
1336 for (; depth > pit->params().depth(); --depth) {
1337 if (environment_inner[depth] != "!-- --") {
1338 item_name = "listitem";
1339 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1340 if (environment_inner[depth] == "varlistentry")
1341 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1343 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1344 environment_stack[depth].erase();
1345 environment_inner[depth].erase();
1348 if (depth == pit->params().depth()
1349 && environment_stack[depth] != style->latexname()
1350 && !environment_stack[depth].empty()) {
1351 if (environment_inner[depth] != "!-- --") {
1352 item_name= "listitem";
1353 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1354 if (environment_inner[depth] == "varlistentry")
1355 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1358 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1360 environment_stack[depth].erase();
1361 environment_inner[depth].erase();
1364 // Write opening SGML tags.
1365 switch (style->latextype) {
1366 case LATEX_PARAGRAPH:
1367 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1371 buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1375 case LATEX_ENVIRONMENT:
1376 case LATEX_ITEM_ENVIRONMENT:
1377 if (depth < pit->params().depth()) {
1378 depth = pit->params().depth();
1379 environment_stack[depth].erase();
1382 if (environment_stack[depth] != style->latexname()) {
1383 if (environment_stack.size() == depth + 1) {
1384 environment_stack.push_back("!-- --");
1385 environment_inner.push_back("!-- --");
1387 environment_stack[depth] = style->latexname();
1388 environment_inner[depth] = "!-- --";
1389 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1391 if (environment_inner[depth] != "!-- --") {
1392 item_name= "listitem";
1393 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1394 if (environment_inner[depth] == "varlistentry")
1395 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1399 if (style->latextype == LATEX_ENVIRONMENT) {
1400 if (!style->latexparam().empty()) {
1401 if (style->latexparam() == "CDATA")
1404 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1409 desc_on = (style->labeltype == LABEL_MANUAL);
1411 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1412 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1414 item_name = desc_on ? "term" : "para";
1415 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1419 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1423 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1426 // write closing SGML tags
1427 switch (style->latextype) {
1428 case LATEX_ENVIRONMENT:
1429 if (!style->latexparam().empty()) {
1430 if (style->latexparam() == "CDATA")
1433 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1436 case LATEX_ITEM_ENVIRONMENT:
1437 if (desc_on == 1) break;
1439 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1441 case LATEX_PARAGRAPH:
1442 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1445 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1451 for (int d = depth; d >= 0; --d) {
1452 if (!environment_stack[depth].empty()) {
1453 if (environment_inner[depth] != "!-- --") {
1454 item_name = "listitem";
1455 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1456 if (environment_inner[depth] == "varlistentry")
1457 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1460 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1468 void InsetText::validate(LaTeXFeatures & features) const
1470 for_each(paragraphs.begin(), paragraphs.end(),
1471 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1475 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1477 if (the_locking_inset) {
1478 the_locking_inset->getCursor(bv, x, y);
1482 y = cy() + InsetText::y();
1486 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1488 if (the_locking_inset) {
1489 the_locking_inset->getCursorPos(bv, x, y);
1492 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1493 y = cy() - TEXT_TO_INSET_OFFSET;
1497 int InsetText::insetInInsetY() const
1499 if (!the_locking_inset)
1502 return (inset_y + the_locking_inset->insetInInsetY());
1506 void InsetText::fitInsetCursor(BufferView * bv) const
1508 if (the_locking_inset) {
1509 the_locking_inset->fitInsetCursor(bv);
1512 LyXFont const font = text_.getFont(bv->buffer(), cpar(), cpos());
1514 int const asc = font_metrics::maxAscent(font);
1515 int const desc = font_metrics::maxDescent(font);
1517 if (bv->fitLockedInsetCursor(cx(bv), cy(), asc, desc))
1518 need_update |= FULL;
1523 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1525 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1526 return moveLeftIntern(bv, false, activate_inset, selecting);
1528 return moveRightIntern(bv, true, activate_inset, selecting);
1533 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1535 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1536 return moveRightIntern(bv, true, activate_inset, selecting);
1538 return moveLeftIntern(bv, false, activate_inset, selecting);
1543 InsetText::moveRightIntern(BufferView * bv, bool front,
1544 bool activate_inset, bool selecting)
1546 ParagraphList::iterator c_par = cpar();
1548 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1549 return FINISHED_RIGHT;
1550 if (activate_inset && checkAndActivateInset(bv, front))
1552 text_.cursorRight(bv);
1554 text_.clearSelection();
1555 return DISPATCHED_NOUPDATE;
1560 InsetText::moveLeftIntern(BufferView * bv, bool front,
1561 bool activate_inset, bool selecting)
1563 if (cpar() == paragraphs.begin() && cpos() <= 0)
1565 text_.cursorLeft(bv);
1567 text_.clearSelection();
1568 if (activate_inset && checkAndActivateInset(bv, front))
1570 return DISPATCHED_NOUPDATE;
1574 Inset::RESULT InsetText::moveUp(BufferView * bv)
1576 if (crow() == text_.rows().begin())
1579 text_.clearSelection();
1580 return DISPATCHED_NOUPDATE;
1584 Inset::RESULT InsetText::moveDown(BufferView * bv)
1586 if (boost::next(crow()) == text_.rows().end())
1587 return FINISHED_DOWN;
1588 text_.cursorDown(bv);
1589 text_.clearSelection();
1590 return DISPATCHED_NOUPDATE;
1594 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1596 if (the_locking_inset) {
1597 if (the_locking_inset->insetAllowed(inset))
1598 return the_locking_inset->insertInset(bv, inset);
1601 inset->setOwner(this);
1602 text_.insertInset(inset);
1604 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1609 bool InsetText::insetAllowed(Inset::Code code) const
1611 // in_insetAllowed is a really gross hack,
1612 // to allow us to call the owner's insetAllowed
1613 // without stack overflow, which can happen
1614 // when the owner uses InsetCollapsable::insetAllowed()
1616 if (in_insetAllowed)
1618 in_insetAllowed = true;
1619 if (the_locking_inset)
1620 ret = the_locking_inset->insetAllowed(code);
1622 ret = owner()->insetAllowed(code);
1623 in_insetAllowed = false;
1628 UpdatableInset * InsetText::getLockingInset() const
1630 return the_locking_inset ? the_locking_inset->getLockingInset() :
1631 const_cast<InsetText *>(this);
1635 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1639 if (the_locking_inset)
1640 return the_locking_inset->getFirstLockingInsetOfType(c);
1645 bool InsetText::showInsetDialog(BufferView * bv) const
1647 if (the_locking_inset)
1648 return the_locking_inset->showInsetDialog(bv);
1653 void InsetText::getLabelList(std::vector<string> & list) const
1655 ParagraphList::const_iterator pit = paragraphs.begin();
1656 ParagraphList::const_iterator pend = paragraphs.end();
1657 for (; pit != pend; ++pit) {
1658 InsetList::const_iterator beg = pit->insetlist.begin();
1659 InsetList::const_iterator end = pit->insetlist.end();
1660 for (; beg != end; ++beg)
1661 beg->inset->getLabelList(list);
1666 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1669 if (the_locking_inset) {
1670 the_locking_inset->setFont(bv, font, toggleall, selectall);
1674 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1675 || cpar()->empty()) {
1676 text_.setFont(font, toggleall);
1681 if (text_.selection.set())
1682 setUndo(bv, Undo::EDIT, text_.cursor.par());
1686 text_.selection.cursor = text_.cursor;
1687 text_.cursorBottom();
1688 text_.setSelection();
1691 text_.toggleFree(font, toggleall);
1694 text_.clearSelection();
1698 bool flag = (selectall || text_.selection.set());
1701 updateLocal(bv, FULL, true);
1703 updateLocal(bv, CURSOR_PAR, true);
1707 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1709 if (cpar()->isInset(cpos())) {
1711 static_cast<UpdatableInset*>(cpar()->getInset(cpos()));
1712 if (!isHighlyEditableInset(inset))
1714 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1715 inset->localDispatch(cmd);
1716 if (!the_locking_inset)
1718 updateLocal(bv, CURSOR, false);
1725 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1726 mouse_button::state button)
1728 x -= drawTextXOffset;
1730 int dummyy = y + dim_.asc;
1731 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1732 // we only do the edit() call if the inset was hit by the mouse
1733 // or if it is a highly editable inset. So we should call this
1734 // function from our own edit with button < 0.
1735 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1737 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1745 inset_x = cix(bv) - top_x + drawTextXOffset;
1746 inset_y = ciy() + drawTextYOffset;
1747 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1748 inset->localDispatch(cmd);
1749 if (!the_locking_inset)
1751 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 Paragraph & tmp = paragraphs.back();
1773 tmp.setInsetOwner(this);
1781 void InsetText::markNew(bool track_changes)
1783 ParagraphList::iterator pit = paragraphs.begin();
1784 ParagraphList::iterator pend = paragraphs.end();
1785 for (; pit != pend; ++pit) {
1786 if (track_changes) {
1787 pit->trackChanges();
1789 // no-op when not tracking
1790 pit->cleanChanges();
1796 void InsetText::setText(string const & data, LyXFont const & font)
1799 for (unsigned int i = 0; i < data.length(); ++i)
1800 paragraphs.begin()->insertChar(i, data[i], font);
1805 void InsetText::setAutoBreakRows(bool flag)
1807 if (flag != autoBreakRows) {
1808 autoBreakRows = flag;
1816 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1818 if (how != drawFrame_) {
1821 updateLocal(bv, DRAW_FRAME, false);
1826 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1828 if (frame_color != col) {
1831 updateLocal(bv, DRAW_FRAME, false);
1836 int InsetText::cx(BufferView * bv) const
1838 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1839 if (the_locking_inset) {
1840 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1841 text_.cursor.pos());
1842 if (font.isVisibleRightToLeft())
1843 x -= the_locking_inset->width();
1849 int InsetText::cix(BufferView * bv) const
1851 int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1852 if (the_locking_inset) {
1853 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1854 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 if (the_locking_inset) {
1953 inset_x = cix(bv) - top_x + drawTextXOffset;
1954 inset_y = ciy() + drawTextYOffset;
1957 text_.top_y(bv->screen().topCursorVisible(&text_));
1959 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
1960 // this will scroll the screen such that the cursor becomes visible
1961 bv->updateScrollbar();
1963 need_update |= FULL;
1968 void InsetText::reinitLyXText() const
1970 BufferView * bv = text_.bv_owner;
1977 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1978 const_cast<ParagraphList&>(paragraphs).end(),
1979 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1982 restoreLyXTextState();
1983 if (the_locking_inset) {
1984 inset_x = cix(bv) - top_x + drawTextXOffset;
1985 inset_y = ciy() + drawTextYOffset;
1987 text_.top_y(bv->screen().topCursorVisible(&text_));
1989 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
1990 // this will scroll the screen such that the cursor becomes visible
1991 bv->updateScrollbar();
1998 void InsetText::removeNewlines()
2000 bool changed = false;
2002 ParagraphList::iterator it = paragraphs.begin();
2003 ParagraphList::iterator end = paragraphs.end();
2004 for (; it != end; ++it) {
2005 for (int i = 0; i < it->size(); ++i) {
2006 if (it->isNewline(i)) {
2017 bool InsetText::nodraw() const
2019 if (the_locking_inset)
2020 return the_locking_inset->nodraw();
2021 return UpdatableInset::nodraw();
2025 int InsetText::scroll(bool recursive) const
2027 int sx = UpdatableInset::scroll(false);
2029 if (recursive && the_locking_inset)
2030 sx += the_locking_inset->scroll(recursive);
2036 void InsetText::clearSelection(BufferView * bv)
2038 getLyXText(bv)->clearSelection();
2042 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2044 Painter & pain = bv->painter();
2046 int h = dim_.asc + dim_.des;
2047 int ty = baseline - dim_.asc;
2053 if ((ty + h) > pain.paperHeight())
2054 h = pain.paperHeight();
2055 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2056 w = pain.paperWidth();
2057 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2062 ParagraphList * InsetText::getParagraphs(int i) const
2064 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2068 LyXCursor const & InsetText::cursor(BufferView * bv) const
2070 if (the_locking_inset)
2071 return the_locking_inset->cursor(bv);
2072 return getLyXText(bv)->cursor;
2076 Inset * InsetText::getInsetFromID(int id_arg) const
2079 return const_cast<InsetText *>(this);
2081 ParagraphList::const_iterator pit = paragraphs.begin();
2082 ParagraphList::const_iterator pend = paragraphs.end();
2083 for (; pit != pend; ++pit) {
2084 InsetList::const_iterator it = pit->insetlist.begin();
2085 InsetList::const_iterator end = pit->insetlist.end();
2086 for (; it != end; ++it) {
2087 if (it->inset->id() == id_arg)
2089 Inset * in = it->inset->getInsetFromID(id_arg);
2099 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2102 if (the_locking_inset) {
2103 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2104 if (!word.word().empty()) {
2108 // we have to go on checking so move cursor to the next char
2109 text_.cursor.pos(text_.cursor.pos() + 1);
2111 word = text_.selectNextWordToSpellcheck(value);
2112 if (word.word().empty())
2113 bv->unlockInset(const_cast<InsetText *>(this));
2120 void InsetText::selectSelectedWord(BufferView * bv)
2122 if (the_locking_inset) {
2123 the_locking_inset->selectSelectedWord(bv);
2126 getLyXText(bv)->selectSelectedWord();
2127 updateLocal(bv, SELECTION, false);
2131 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2133 if (the_locking_inset) {
2134 the_locking_inset->toggleSelection(bv, kill_selection);
2137 int x = top_x + TEXT_TO_INSET_OFFSET;
2139 RowList::iterator rowit = text_.rows().begin();
2140 RowList::iterator end = text_.rows().end();
2141 int y_offset = top_baseline - rowit->ascent_of_text();
2143 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2144 y += rowit->height();
2150 if (need_update & SELECTION)
2152 bv->screen().toggleSelection(&text_, bv, kill_selection, y_offset, x);
2156 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2158 if (the_locking_inset) {
2159 if (the_locking_inset->nextChange(bv, length))
2161 text_.cursorRight(true);
2163 lyxfind::SearchResult result =
2164 lyxfind::findNextChange(bv, &text_, length);
2166 if (result == lyxfind::SR_FOUND) {
2167 LyXCursor cur = text_.cursor;
2168 bv->unlockInset(bv->theLockingInset());
2169 if (bv->lockInset(this))
2172 text_.setSelectionRange(length);
2173 updateLocal(bv, SELECTION, false);
2175 return result != lyxfind::SR_NOT_FOUND;
2179 bool InsetText::searchForward(BufferView * bv, string const & str,
2182 if (the_locking_inset) {
2183 if (the_locking_inset->searchForward(bv, str, cs, mw))
2185 text_.cursorRight(true);
2187 lyxfind::SearchResult result =
2188 lyxfind::LyXFind(bv, &text_, str, true, cs, mw);
2190 if (result == lyxfind::SR_FOUND) {
2191 LyXCursor cur = text_.cursor;
2192 bv->unlockInset(bv->theLockingInset());
2193 if (bv->lockInset(this))
2196 text_.setSelectionRange(str.length());
2197 updateLocal(bv, SELECTION, false);
2199 return (result != lyxfind::SR_NOT_FOUND);
2202 bool InsetText::searchBackward(BufferView * bv, string const & str,
2205 if (the_locking_inset) {
2206 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2210 ParagraphList::iterator pit = paragraphs.begin();
2211 ParagraphList::iterator pend = paragraphs.end();
2213 while (boost::next(pit) != pend)
2216 text_.setCursor(pit, pit->size());
2218 lyxfind::SearchResult result =
2219 lyxfind::LyXFind(bv, &text_, str, false, cs, mw);
2221 if (result == lyxfind::SR_FOUND) {
2222 LyXCursor cur = text_.cursor;
2223 bv->unlockInset(bv->theLockingInset());
2224 if (bv->lockInset(this))
2227 text_.setSelectionRange(str.length());
2228 updateLocal(bv, SELECTION, false);
2230 return (result != lyxfind::SR_NOT_FOUND);
2234 bool InsetText::checkInsertChar(LyXFont & font)
2237 return owner()->checkInsertChar(font);
2242 void InsetText::collapseParagraphs(BufferView * bv)
2244 while (paragraphs.size() > 1) {
2245 ParagraphList::iterator first_par = paragraphs.begin();
2246 ParagraphList::iterator next_par = boost::next(first_par);
2247 size_t const first_par_size = first_par->size();
2249 if (!first_par->empty() &&
2250 !next_par->empty() &&
2251 !first_par->isSeparator(first_par_size - 1)) {
2252 first_par->insertChar(first_par_size, ' ');
2255 if (text_.selection.set()) {
2256 if (text_.selection.start.par() == next_par) {
2257 text_.selection.start.par(first_par);
2258 text_.selection.start.pos(
2259 text_.selection.start.pos() + first_par_size);
2261 if (text_.selection.end.par() == next_par) {
2262 text_.selection.end.par(first_par);
2263 text_.selection.end.pos(
2264 text_.selection.end.pos() + first_par_size);
2268 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2274 void InsetText::getDrawFont(LyXFont & font) const
2278 owner()->getDrawFont(font);
2282 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2284 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2285 // And it probably does. You have to take a look at this John. (Lgb)
2286 #warning John, have a look here. (Lgb)
2287 ParagraphList::iterator pit = plist.begin();
2288 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2290 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2292 ParagraphList::iterator pend = plist.end();
2293 for (; pit != pend; ++pit) {
2294 paragraphs.push_back(*pit);
2301 void InsetText::addPreview(PreviewLoader & loader) const
2303 ParagraphList::const_iterator pit = paragraphs.begin();
2304 ParagraphList::const_iterator pend = paragraphs.end();
2306 for (; pit != pend; ++pit) {
2307 InsetList::const_iterator it = pit->insetlist.begin();
2308 InsetList::const_iterator end = pit->insetlist.end();
2309 for (; it != end; ++it) {
2310 it->inset->addPreview(loader);