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 switch (cmd.action) {
818 case LFUN_INSET_EDIT: {
819 UpdatableInset::localDispatch(cmd);
821 if (!bv->lockInset(this)) {
822 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
827 the_locking_inset = 0;
828 inset_pos = inset_x = inset_y = 0;
829 inset_boundary = false;
830 inset_par = paragraphs.end();
831 old_par = paragraphs.end();
834 if (cmd.argument.size()) {
835 if (cmd.argument == "left")
836 text_.setCursorIntern(paragraphs.begin(), 0);
838 ParagraphList::iterator it = paragraphs.begin();
839 ParagraphList::iterator end = paragraphs.end();
840 while (boost::next(it) != end)
842 // int const pos = (p->size() ? p->size()-1 : p->size());
843 text_.setCursor(it, it->size());
846 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
847 // we put here -1 and not button as now the button in the
848 // edit call should not be needed we will fix this in 1.3.x
849 // cycle hopefully (Jug 20020509)
850 // FIXME: GUII I've changed this to none: probably WRONG
851 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
852 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
854 text_.cursor.x_fix(text_.cursor.x());
858 text_.clearSelection();
861 // If the inset is empty set the language of the current font to the
862 // language to the surronding text (if different).
863 if (paragraphs.begin()->empty() &&
864 paragraphs.size() == 1 &&
865 bv->getParentLanguage(this) != text_.current_font.language())
867 LyXFont font(LyXFont::ALL_IGNORE);
868 font.setLanguage(bv->getParentLanguage(this));
869 setFont(bv, font, false);
873 if (drawFrame_ == LOCKED)
874 code = CURSOR | DRAW_FRAME;
876 updateLocal(bv, code, false);
877 // Tell the paragraph dialog that we've entered an insettext.
878 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
882 case LFUN_MOUSE_PRESS:
886 case LFUN_MOUSE_MOTION:
887 lfunMouseMotion(cmd);
890 case LFUN_MOUSE_RELEASE:
891 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
897 bool was_empty = (paragraphs.begin()->empty() &&
898 paragraphs.size() == 1);
900 no_selection = false;
901 RESULT result = UpdatableInset::localDispatch(cmd);
902 if (result != UNDISPATCHED)
906 if (cmd.action < 0 && cmd.argument.empty())
909 if (the_locking_inset) {
910 result = the_locking_inset->localDispatch(cmd);
911 if (result == DISPATCHED_NOUPDATE)
913 else if (result == DISPATCHED) {
914 updateLocal(bv, CURSOR_PAR, false);
916 } else if (result >= FINISHED) {
919 moveRightIntern(bv, false, false);
923 if ((result = moveUp(bv)) >= FINISHED) {
924 updateLocal(bv, CURSOR, false);
925 bv->unlockInset(this);
929 if ((result = moveDown(bv)) >= FINISHED) {
930 updateLocal(bv, CURSOR, false);
931 bv->unlockInset(this);
938 the_locking_inset = 0;
939 updateLocal(bv, CURSOR, false);
940 // make sure status gets reset immediately
941 bv->owner()->clearMessage();
948 // what type of update to do on a cursor movement
949 int cursor_update = CURSOR;
951 if (text_.selection.set())
952 cursor_update = SELECTION;
954 switch (cmd.action) {
957 case LFUN_SELFINSERT:
958 if (bv->buffer()->isReadonly()) {
959 // setErrorMessage(N_("Document is read only"));
962 if (!cmd.argument.empty()) {
963 /* Automatically delete the currently selected
964 * text and replace it with what is being
965 * typed in now. Depends on lyxrc settings
966 * "auto_region_delete", which defaults to
969 // This should not be needed here and is also WRONG!
970 setUndo(bv, Undo::INSERT, text_.cursor.par());
973 if (lyxrc.auto_region_delete) {
974 if (text_.selection.set()) {
975 text_.cutSelection(false, false);
978 text_.clearSelection();
979 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
980 bv->owner()->getIntl().getTransManager().
981 TranslateAndInsert(cmd.argument[i], &text_);
984 text_.selection.cursor = text_.cursor;
985 updwhat = CURSOR | CURSOR_PAR;
987 result = DISPATCHED_NOUPDATE;
990 // cursor movements that need special handling
993 result = moveRight(bv);
995 updwhat = cursor_update;
999 result = moveLeft(bv);
1000 updwhat = cursor_update;
1004 result = moveDown(bv);
1005 updwhat = cursor_update;
1009 result = moveUp(bv);
1010 updwhat = cursor_update;
1014 if (crow() == text_.rows().begin())
1015 result = FINISHED_UP;
1017 text_.cursorPrevious();
1018 text_.clearSelection();
1019 result = DISPATCHED_NOUPDATE;
1021 updwhat = cursor_update;
1025 if (boost::next(crow()) == text_.rows().end())
1026 result = FINISHED_DOWN;
1029 text_.clearSelection();
1030 result = DISPATCHED_NOUPDATE;
1032 updwhat = cursor_update;
1035 case LFUN_BACKSPACE: {
1036 if (text_.selection.set())
1037 text_.cutSelection(true, false);
1040 updwhat = CURSOR_PAR;
1046 if (text_.selection.set()) {
1047 text_.cutSelection(true, false);
1051 updwhat = CURSOR_PAR;
1057 text_.cutSelection(true, true);
1058 updwhat = CURSOR_PAR;
1065 text_.copySelection();
1066 updwhat = CURSOR_PAR;
1069 case LFUN_PASTESELECTION:
1071 string const clip(bv->getClipboard());
1075 if (cmd.argument == "paragraph") {
1076 text_.insertStringAsParagraphs(clip);
1078 text_.insertStringAsLines(clip);
1081 text_.clearSelection();
1083 updwhat = CURSOR_PAR;
1089 if (!autoBreakRows) {
1090 if (CutAndPaste::nrOfParagraphs() > 1) {
1091 #ifdef WITH_WARNINGS
1092 #warning FIXME horrendously bad UI
1094 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1099 replaceSelection(bv->getLyXText());
1100 size_t sel_index = 0;
1101 string const & arg = cmd.argument;
1102 if (isStrUnsignedInt(arg)) {
1103 size_t const paste_arg = strToUnsignedInt(arg);
1104 #warning FIXME Check if the arg is in the domain of available selections.
1105 sel_index = paste_arg;
1107 text_.pasteSelection(sel_index);
1109 text_.clearSelection();
1110 updwhat = CURSOR_PAR;
1115 case LFUN_BREAKPARAGRAPH:
1116 if (!autoBreakRows) {
1117 result = DISPATCHED;
1120 replaceSelection(bv->getLyXText());
1121 text_.breakParagraph(paragraphs, 0);
1122 updwhat = CURSOR | FULL;
1126 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1127 if (!autoBreakRows) {
1128 result = DISPATCHED;
1131 replaceSelection(bv->getLyXText());
1132 text_.breakParagraph(paragraphs, 1);
1133 updwhat = CURSOR | FULL;
1137 case LFUN_BREAKLINE: {
1138 if (!autoBreakRows) {
1139 result = DISPATCHED;
1143 replaceSelection(bv->getLyXText());
1144 text_.insertInset(new InsetNewline);
1145 updwhat = CURSOR | CURSOR_PAR;
1151 // do not set layouts on non breakable textinsets
1152 if (autoBreakRows) {
1153 string cur_layout = cpar()->layout()->name();
1155 // Derive layout number from given argument (string)
1156 // and current buffer's textclass (number). */
1157 LyXTextClass const & tclass =
1158 bv->buffer()->params.getLyXTextClass();
1159 string layout = cmd.argument;
1160 bool hasLayout = tclass.hasLayout(layout);
1162 // If the entry is obsolete, use the new one instead.
1164 string const & obs =
1165 tclass[layout]->obsoleted_by();
1170 // see if we found the layout number:
1172 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1173 bv->owner()->dispatch(lf);
1177 if (cur_layout != layout) {
1178 cur_layout = layout;
1179 text_.setLayout(layout);
1180 bv->owner()->setLayout(cpar()->layout()->name());
1181 updwhat = CURSOR_PAR;
1185 // reset the layout box
1186 bv->owner()->setLayout(cpar()->layout()->name());
1189 case LFUN_PARAGRAPH_SPACING:
1190 // This one is absolutely not working. When fiddling with this
1191 // it also seems to me that the paragraphs inside the insettext
1192 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1193 // FIXME: how old is this comment ? ...
1195 ParagraphList::iterator pit = text_.cursor.par();
1196 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1197 float cur_value = 1.0;
1198 if (cur_spacing == Spacing::Other) {
1199 cur_value = pit->params().spacing().getValue();
1202 istringstream istr(STRCONV(cmd.argument));
1205 Spacing::Space new_spacing = cur_spacing;
1206 float new_value = cur_value;
1208 lyxerr << "Missing argument to `paragraph-spacing'"
1210 } else if (tmp == "single") {
1211 new_spacing = Spacing::Single;
1212 } else if (tmp == "onehalf") {
1213 new_spacing = Spacing::Onehalf;
1214 } else if (tmp == "double") {
1215 new_spacing = Spacing::Double;
1216 } else if (tmp == "other") {
1217 new_spacing = Spacing::Other;
1220 lyxerr << "new_value = " << tmpval << endl;
1223 } else if (tmp == "default") {
1224 new_spacing = Spacing::Default;
1226 lyxerr << _("Unknown spacing argument: ")
1227 << cmd.argument << endl;
1229 if (cur_spacing != new_spacing || cur_value != new_value) {
1230 pit->params().spacing(Spacing(new_spacing, new_value));
1231 updwhat = CURSOR_PAR;
1237 // These need to do repaints but don't require
1238 // special handling otherwise. A *lot* of the
1239 // above could probably be done similarly ...
1244 case LFUN_WORDRIGHT:
1245 // these two are really unhandled ...
1247 case LFUN_BEGINNINGBUF:
1248 updwhat = cursor_update;
1249 if (!bv->dispatch(cmd))
1250 result = UNDISPATCHED;
1259 case LFUN_WORDLEFTSEL:
1260 case LFUN_WORDRIGHTSEL:
1261 updwhat = SELECTION;
1266 if (!bv->dispatch(cmd))
1267 result = UNDISPATCHED;
1272 updateLocal(bv, updwhat, updflag);
1273 /// If the action has deleted all text in the inset, we need to change the
1274 // language to the language of the surronding text.
1275 if (!was_empty && paragraphs.begin()->empty() &&
1276 paragraphs.size() == 1) {
1277 LyXFont font(LyXFont::ALL_IGNORE);
1278 font.setLanguage(bv->getParentLanguage(this));
1279 setFont(bv, font, false);
1282 if (result >= FINISHED)
1283 bv->unlockInset(this);
1285 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1286 result = DISPATCHED;
1291 int InsetText::latex(Buffer const * buf, ostream & os,
1292 LatexRunParams const & runparams) const
1295 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1296 return texrow.rows();
1300 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1302 unsigned int lines = 0;
1304 ParagraphList::const_iterator beg = paragraphs.begin();
1305 ParagraphList::const_iterator end = paragraphs.end();
1306 ParagraphList::const_iterator it = beg;
1307 for (; it != end; ++it) {
1308 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1309 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1316 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1318 unsigned int lines = 0;
1320 vector<string> environment_stack(10);
1321 vector<string> environment_inner(10);
1323 int const command_depth = 0;
1326 Paragraph::depth_type depth = 0; // paragraph depth
1328 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1329 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1331 for (; pit != pend; ++pit) {
1333 int desc_on = 0; // description mode
1335 LyXLayout_ptr const & style = pit->layout();
1337 // environment tag closing
1338 for (; depth > pit->params().depth(); --depth) {
1339 if (environment_inner[depth] != "!-- --") {
1340 item_name = "listitem";
1341 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1342 if (environment_inner[depth] == "varlistentry")
1343 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1345 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1346 environment_stack[depth].erase();
1347 environment_inner[depth].erase();
1350 if (depth == pit->params().depth()
1351 && environment_stack[depth] != style->latexname()
1352 && !environment_stack[depth].empty()) {
1353 if (environment_inner[depth] != "!-- --") {
1354 item_name= "listitem";
1355 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1356 if (environment_inner[depth] == "varlistentry")
1357 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1360 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1362 environment_stack[depth].erase();
1363 environment_inner[depth].erase();
1366 // Write opening SGML tags.
1367 switch (style->latextype) {
1368 case LATEX_PARAGRAPH:
1369 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1373 buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1377 case LATEX_ENVIRONMENT:
1378 case LATEX_ITEM_ENVIRONMENT:
1379 if (depth < pit->params().depth()) {
1380 depth = pit->params().depth();
1381 environment_stack[depth].erase();
1384 if (environment_stack[depth] != style->latexname()) {
1385 if (environment_stack.size() == depth + 1) {
1386 environment_stack.push_back("!-- --");
1387 environment_inner.push_back("!-- --");
1389 environment_stack[depth] = style->latexname();
1390 environment_inner[depth] = "!-- --";
1391 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1393 if (environment_inner[depth] != "!-- --") {
1394 item_name= "listitem";
1395 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1396 if (environment_inner[depth] == "varlistentry")
1397 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1401 if (style->latextype == LATEX_ENVIRONMENT) {
1402 if (!style->latexparam().empty()) {
1403 if (style->latexparam() == "CDATA")
1406 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1411 desc_on = (style->labeltype == LABEL_MANUAL);
1413 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1414 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1416 item_name = desc_on ? "term" : "para";
1417 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1421 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1425 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1428 // write closing SGML tags
1429 switch (style->latextype) {
1430 case LATEX_ENVIRONMENT:
1431 if (!style->latexparam().empty()) {
1432 if (style->latexparam() == "CDATA")
1435 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1438 case LATEX_ITEM_ENVIRONMENT:
1439 if (desc_on == 1) break;
1441 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1443 case LATEX_PARAGRAPH:
1444 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1447 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1453 for (int d = depth; d >= 0; --d) {
1454 if (!environment_stack[depth].empty()) {
1455 if (environment_inner[depth] != "!-- --") {
1456 item_name = "listitem";
1457 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1458 if (environment_inner[depth] == "varlistentry")
1459 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1462 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1470 void InsetText::validate(LaTeXFeatures & features) const
1472 for_each(paragraphs.begin(), paragraphs.end(),
1473 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1477 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1479 if (the_locking_inset) {
1480 the_locking_inset->getCursor(bv, x, y);
1484 y = cy() + InsetText::y();
1488 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1490 if (the_locking_inset) {
1491 the_locking_inset->getCursorPos(bv, x, y);
1494 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1495 y = cy() - TEXT_TO_INSET_OFFSET;
1499 int InsetText::insetInInsetY() const
1501 if (!the_locking_inset)
1504 return (inset_y + the_locking_inset->insetInInsetY());
1508 void InsetText::fitInsetCursor(BufferView * bv) const
1510 if (the_locking_inset) {
1511 the_locking_inset->fitInsetCursor(bv);
1514 LyXFont const font = text_.getFont(bv->buffer(), cpar(), cpos());
1516 int const asc = font_metrics::maxAscent(font);
1517 int const desc = font_metrics::maxDescent(font);
1519 if (bv->fitLockedInsetCursor(cx(bv), cy(), asc, desc))
1520 need_update |= FULL;
1525 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1527 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1528 return moveLeftIntern(bv, false, activate_inset, selecting);
1530 return moveRightIntern(bv, true, activate_inset, selecting);
1535 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1537 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1538 return moveRightIntern(bv, true, activate_inset, selecting);
1540 return moveLeftIntern(bv, false, activate_inset, selecting);
1545 InsetText::moveRightIntern(BufferView * bv, bool front,
1546 bool activate_inset, bool selecting)
1548 ParagraphList::iterator c_par = cpar();
1550 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1551 return FINISHED_RIGHT;
1552 if (activate_inset && checkAndActivateInset(bv, front))
1554 text_.cursorRight(bv);
1556 text_.clearSelection();
1557 return DISPATCHED_NOUPDATE;
1562 InsetText::moveLeftIntern(BufferView * bv, bool front,
1563 bool activate_inset, bool selecting)
1565 if (cpar() == paragraphs.begin() && cpos() <= 0)
1567 text_.cursorLeft(bv);
1569 text_.clearSelection();
1570 if (activate_inset && checkAndActivateInset(bv, front))
1572 return DISPATCHED_NOUPDATE;
1576 Inset::RESULT InsetText::moveUp(BufferView * bv)
1578 if (crow() == text_.rows().begin())
1581 text_.clearSelection();
1582 return DISPATCHED_NOUPDATE;
1586 Inset::RESULT InsetText::moveDown(BufferView * bv)
1588 if (boost::next(crow()) == text_.rows().end())
1589 return FINISHED_DOWN;
1590 text_.cursorDown(bv);
1591 text_.clearSelection();
1592 return DISPATCHED_NOUPDATE;
1596 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1598 if (the_locking_inset) {
1599 if (the_locking_inset->insetAllowed(inset))
1600 return the_locking_inset->insertInset(bv, inset);
1603 inset->setOwner(this);
1604 text_.insertInset(inset);
1606 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1611 bool InsetText::insetAllowed(Inset::Code code) const
1613 // in_insetAllowed is a really gross hack,
1614 // to allow us to call the owner's insetAllowed
1615 // without stack overflow, which can happen
1616 // when the owner uses InsetCollapsable::insetAllowed()
1618 if (in_insetAllowed)
1620 in_insetAllowed = true;
1621 if (the_locking_inset)
1622 ret = the_locking_inset->insetAllowed(code);
1624 ret = owner()->insetAllowed(code);
1625 in_insetAllowed = false;
1630 UpdatableInset * InsetText::getLockingInset() const
1632 return the_locking_inset ? the_locking_inset->getLockingInset() :
1633 const_cast<InsetText *>(this);
1637 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1641 if (the_locking_inset)
1642 return the_locking_inset->getFirstLockingInsetOfType(c);
1647 bool InsetText::showInsetDialog(BufferView * bv) const
1649 if (the_locking_inset)
1650 return the_locking_inset->showInsetDialog(bv);
1655 void InsetText::getLabelList(std::vector<string> & list) const
1657 ParagraphList::const_iterator pit = paragraphs.begin();
1658 ParagraphList::const_iterator pend = paragraphs.end();
1659 for (; pit != pend; ++pit) {
1660 InsetList::const_iterator beg = pit->insetlist.begin();
1661 InsetList::const_iterator end = pit->insetlist.end();
1662 for (; beg != end; ++beg)
1663 beg->inset->getLabelList(list);
1668 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1671 if (the_locking_inset) {
1672 the_locking_inset->setFont(bv, font, toggleall, selectall);
1676 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1677 || cpar()->empty()) {
1678 text_.setFont(font, toggleall);
1683 if (text_.selection.set())
1684 setUndo(bv, Undo::EDIT, text_.cursor.par());
1688 text_.selection.cursor = text_.cursor;
1689 text_.cursorBottom();
1690 text_.setSelection();
1693 text_.toggleFree(font, toggleall);
1696 text_.clearSelection();
1700 bool flag = (selectall || text_.selection.set());
1703 updateLocal(bv, FULL, true);
1705 updateLocal(bv, CURSOR_PAR, true);
1709 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1711 if (cpar()->isInset(cpos())) {
1713 static_cast<UpdatableInset*>(cpar()->getInset(cpos()));
1714 if (!isHighlyEditableInset(inset))
1716 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1717 inset->localDispatch(cmd);
1718 if (!the_locking_inset)
1720 updateLocal(bv, CURSOR, false);
1727 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1728 mouse_button::state button)
1730 x -= drawTextXOffset;
1732 int dummyy = y + dim_.asc;
1733 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1734 // we only do the edit() call if the inset was hit by the mouse
1735 // or if it is a highly editable inset. So we should call this
1736 // function from our own edit with button < 0.
1737 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1739 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1747 inset_x = cix(bv) - 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);
1760 void InsetText::setParagraphData(ParagraphList const & plist)
1762 // we have to unlock any locked inset otherwise we're in troubles
1763 the_locking_inset = 0;
1765 // But it it makes no difference that is a lot better.
1767 // See if this can be simplified when std::list is in effect.
1770 ParagraphList::const_iterator it = plist.begin();
1771 ParagraphList::const_iterator end = plist.end();
1772 for (; it != end; ++it) {
1773 paragraphs.push_back(*it);
1774 Paragraph & tmp = paragraphs.back();
1775 tmp.setInsetOwner(this);
1783 void InsetText::markNew(bool track_changes)
1785 ParagraphList::iterator pit = paragraphs.begin();
1786 ParagraphList::iterator pend = paragraphs.end();
1787 for (; pit != pend; ++pit) {
1788 if (track_changes) {
1789 pit->trackChanges();
1791 // no-op when not tracking
1792 pit->cleanChanges();
1798 void InsetText::setText(string const & data, LyXFont const & font)
1801 for (unsigned int i = 0; i < data.length(); ++i)
1802 paragraphs.begin()->insertChar(i, data[i], font);
1807 void InsetText::setAutoBreakRows(bool flag)
1809 if (flag != autoBreakRows) {
1810 autoBreakRows = flag;
1818 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1820 if (how != drawFrame_) {
1823 updateLocal(bv, DRAW_FRAME, false);
1828 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1830 if (frame_color != col) {
1833 updateLocal(bv, DRAW_FRAME, false);
1838 int InsetText::cx(BufferView * bv) const
1840 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1841 if (the_locking_inset) {
1842 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1843 text_.cursor.pos());
1844 if (font.isVisibleRightToLeft())
1845 x -= the_locking_inset->width();
1851 int InsetText::cix(BufferView * bv) const
1853 int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1854 if (the_locking_inset) {
1855 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1856 text_.cursor.pos());
1857 if (font.isVisibleRightToLeft())
1858 x -= the_locking_inset->width();
1864 int InsetText::cy() const
1866 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1870 int InsetText::ciy() const
1872 return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1876 pos_type InsetText::cpos() const
1878 return text_.cursor.pos();
1882 ParagraphList::iterator InsetText::cpar() const
1884 return text_.cursor.par();
1888 bool InsetText::cboundary() const
1890 return text_.cursor.boundary();
1894 RowList::iterator InsetText::crow() const
1896 return text_.cursorRow();
1900 LyXText * InsetText::getLyXText(BufferView const * bv,
1901 bool const recursive) const
1904 if (recursive && the_locking_inset)
1905 return the_locking_inset->getLyXText(bv, true);
1910 void InsetText::setViewCache(BufferView const * bv) const
1913 text_.bv_owner = const_cast<BufferView *>(bv);
1917 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1920 /// then remove all LyXText in text-insets
1921 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1922 const_cast<ParagraphList&>(paragraphs).end(),
1923 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1928 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1930 if (paragraphs.size() == 1 && paragraphs.begin()->empty()) {
1931 // no data, resize not neccessary!
1932 // we have to do this as a fixed width may have changed!
1935 restoreLyXTextState();
1947 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1948 const_cast<ParagraphList&>(paragraphs).end(),
1949 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1952 restoreLyXTextState();
1954 if (the_locking_inset) {
1955 inset_x = cix(bv) - top_x + drawTextXOffset;
1956 inset_y = ciy() + drawTextYOffset;
1959 text_.top_y(bv->screen().topCursorVisible(&text_));
1961 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
1962 // this will scroll the screen such that the cursor becomes visible
1963 bv->updateScrollbar();
1965 need_update |= FULL;
1970 void InsetText::reinitLyXText() const
1972 BufferView * bv = text_.bv_owner;
1979 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1980 const_cast<ParagraphList&>(paragraphs).end(),
1981 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1984 restoreLyXTextState();
1985 if (the_locking_inset) {
1986 inset_x = cix(bv) - top_x + drawTextXOffset;
1987 inset_y = ciy() + drawTextYOffset;
1989 text_.top_y(bv->screen().topCursorVisible(&text_));
1991 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
1992 // this will scroll the screen such that the cursor becomes visible
1993 bv->updateScrollbar();
2000 void InsetText::removeNewlines()
2002 bool changed = false;
2004 ParagraphList::iterator it = paragraphs.begin();
2005 ParagraphList::iterator end = paragraphs.end();
2006 for (; it != end; ++it) {
2007 for (int i = 0; i < it->size(); ++i) {
2008 if (it->isNewline(i)) {
2019 bool InsetText::nodraw() const
2021 if (the_locking_inset)
2022 return the_locking_inset->nodraw();
2023 return UpdatableInset::nodraw();
2027 int InsetText::scroll(bool recursive) const
2029 int sx = UpdatableInset::scroll(false);
2031 if (recursive && the_locking_inset)
2032 sx += the_locking_inset->scroll(recursive);
2038 void InsetText::clearSelection(BufferView * bv)
2040 getLyXText(bv)->clearSelection();
2044 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2046 Painter & pain = bv->painter();
2048 int h = dim_.asc + dim_.des;
2049 int ty = baseline - dim_.asc;
2055 if ((ty + h) > pain.paperHeight())
2056 h = pain.paperHeight();
2057 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2058 w = pain.paperWidth();
2059 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2064 ParagraphList * InsetText::getParagraphs(int i) const
2066 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2070 LyXCursor const & InsetText::cursor(BufferView * bv) const
2072 if (the_locking_inset)
2073 return the_locking_inset->cursor(bv);
2074 return getLyXText(bv)->cursor;
2078 Inset * InsetText::getInsetFromID(int id_arg) const
2081 return const_cast<InsetText *>(this);
2083 ParagraphList::const_iterator pit = paragraphs.begin();
2084 ParagraphList::const_iterator pend = paragraphs.end();
2085 for (; pit != pend; ++pit) {
2086 InsetList::const_iterator it = pit->insetlist.begin();
2087 InsetList::const_iterator end = pit->insetlist.end();
2088 for (; it != end; ++it) {
2089 if (it->inset->id() == id_arg)
2091 Inset * in = it->inset->getInsetFromID(id_arg);
2101 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2104 if (the_locking_inset) {
2105 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2106 if (!word.word().empty()) {
2110 // we have to go on checking so move cursor to the next char
2111 text_.cursor.pos(text_.cursor.pos() + 1);
2113 word = text_.selectNextWordToSpellcheck(value);
2114 if (word.word().empty())
2115 bv->unlockInset(const_cast<InsetText *>(this));
2122 void InsetText::selectSelectedWord(BufferView * bv)
2124 if (the_locking_inset) {
2125 the_locking_inset->selectSelectedWord(bv);
2128 getLyXText(bv)->selectSelectedWord();
2129 updateLocal(bv, SELECTION, false);
2133 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2135 if (the_locking_inset) {
2136 the_locking_inset->toggleSelection(bv, kill_selection);
2139 int x = top_x + TEXT_TO_INSET_OFFSET;
2141 RowList::iterator rowit = text_.rows().begin();
2142 RowList::iterator end = text_.rows().end();
2143 int y_offset = top_baseline - rowit->ascent_of_text();
2145 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2146 y += rowit->height();
2152 if (need_update & SELECTION)
2154 bv->screen().toggleSelection(&text_, bv, kill_selection, y_offset, x);
2158 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2160 if (the_locking_inset) {
2161 if (the_locking_inset->nextChange(bv, length))
2163 text_.cursorRight(true);
2165 lyxfind::SearchResult result =
2166 lyxfind::findNextChange(bv, &text_, length);
2168 if (result == lyxfind::SR_FOUND) {
2169 LyXCursor cur = text_.cursor;
2170 bv->unlockInset(bv->theLockingInset());
2171 if (bv->lockInset(this))
2174 text_.setSelectionRange(length);
2175 updateLocal(bv, SELECTION, false);
2177 return result != lyxfind::SR_NOT_FOUND;
2181 bool InsetText::searchForward(BufferView * bv, string const & str,
2184 if (the_locking_inset) {
2185 if (the_locking_inset->searchForward(bv, str, cs, mw))
2187 text_.cursorRight(true);
2189 lyxfind::SearchResult result =
2190 lyxfind::LyXFind(bv, &text_, str, true, cs, mw);
2192 if (result == lyxfind::SR_FOUND) {
2193 LyXCursor cur = text_.cursor;
2194 bv->unlockInset(bv->theLockingInset());
2195 if (bv->lockInset(this))
2198 text_.setSelectionRange(str.length());
2199 updateLocal(bv, SELECTION, false);
2201 return (result != lyxfind::SR_NOT_FOUND);
2204 bool InsetText::searchBackward(BufferView * bv, string const & str,
2207 if (the_locking_inset) {
2208 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2212 ParagraphList::iterator pit = paragraphs.begin();
2213 ParagraphList::iterator pend = paragraphs.end();
2215 while (boost::next(pit) != pend)
2218 text_.setCursor(pit, pit->size());
2220 lyxfind::SearchResult result =
2221 lyxfind::LyXFind(bv, &text_, str, false, cs, mw);
2223 if (result == lyxfind::SR_FOUND) {
2224 LyXCursor cur = text_.cursor;
2225 bv->unlockInset(bv->theLockingInset());
2226 if (bv->lockInset(this))
2229 text_.setSelectionRange(str.length());
2230 updateLocal(bv, SELECTION, false);
2232 return (result != lyxfind::SR_NOT_FOUND);
2236 bool InsetText::checkInsertChar(LyXFont & font)
2239 return owner()->checkInsertChar(font);
2244 void InsetText::collapseParagraphs(BufferView * bv)
2246 while (paragraphs.size() > 1) {
2247 ParagraphList::iterator first_par = paragraphs.begin();
2248 ParagraphList::iterator next_par = boost::next(first_par);
2249 size_t const first_par_size = first_par->size();
2251 if (!first_par->empty() &&
2252 !next_par->empty() &&
2253 !first_par->isSeparator(first_par_size - 1)) {
2254 first_par->insertChar(first_par_size, ' ');
2257 if (text_.selection.set()) {
2258 if (text_.selection.start.par() == next_par) {
2259 text_.selection.start.par(first_par);
2260 text_.selection.start.pos(
2261 text_.selection.start.pos() + first_par_size);
2263 if (text_.selection.end.par() == next_par) {
2264 text_.selection.end.par(first_par);
2265 text_.selection.end.pos(
2266 text_.selection.end.pos() + first_par_size);
2270 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2276 void InsetText::getDrawFont(LyXFont & font) const
2280 owner()->getDrawFont(font);
2284 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2286 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2287 // And it probably does. You have to take a look at this John. (Lgb)
2288 #warning John, have a look here. (Lgb)
2289 ParagraphList::iterator pit = plist.begin();
2290 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2292 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2294 ParagraphList::iterator pend = plist.end();
2295 for (; pit != pend; ++pit) {
2296 paragraphs.push_back(*pit);
2303 void InsetText::addPreview(PreviewLoader & loader) const
2305 ParagraphList::const_iterator pit = paragraphs.begin();
2306 ParagraphList::const_iterator pend = paragraphs.end();
2308 for (; pit != pend; ++pit) {
2309 InsetList::const_iterator it = pit->insetlist.begin();
2310 InsetList::const_iterator end = pit->insetlist.end();
2311 for (; it != end; ++it) {
2312 it->inset->addPreview(loader);