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: width: " << mi.base.textwidth << "\n";
279 if (mi.base.textwidth)
280 textwidth_ = mi.base.textwidth;
281 BufferView * bv = mi.base.bv;
283 text_.metrics(mi, dim);
284 dim.asc += TEXT_TO_INSET_OFFSET;
285 dim.des += TEXT_TO_INSET_OFFSET;
286 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
287 dim.wid = max(dim.wid, 10);
292 int InsetText::textWidth() const
298 void InsetText::draw(PainterInfo & pi, int x, int baseline) const
303 // update our idea of where we are. Clearly, we should
304 // not have to know this information.
307 int const start_x = x;
309 BufferView * bv = pi.base.bv;
310 Painter & pain = pi.pain;
312 // repaint the background if needed
313 if (backgroundColor() != LColor::background)
314 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
316 // no draw is necessary !!!
317 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
318 top_baseline = baseline;
326 top_baseline = baseline;
327 top_y = baseline - dim_.asc;
329 if (last_drawn_width != dim_.wid) {
331 last_drawn_width = dim_.wid;
334 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
335 inset_x = cix(bv) - x + drawTextXOffset;
336 inset_y = ciy() + drawTextYOffset;
339 x += TEXT_TO_INSET_OFFSET;
341 RowList::iterator rowit = text_.rows().begin();
342 RowList::iterator end = text_.rows().end();
344 int y_offset = baseline - rowit->ascent_of_text();
345 int ph = pain.paperHeight();
348 while (rowit != end && y + rowit->height() <= 0) {
349 y += rowit->height();
350 first += rowit->height();
354 text_.top_y(-y_offset);
362 int yf = y_offset + first;
367 while (rowit != end && yf < ph) {
368 paintRows(*bv, text_, rowit,
369 y + y_offset + first, int(x), y + text_.top_y());
370 y += rowit->height();
371 yf += rowit->height();
377 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked)) {
378 drawFrame(pain, int(start_x));
381 if (need_update != INIT) {
387 void InsetText::drawFrame(Painter & pain, int x) const
389 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
391 frame_y = top_baseline - dim_.asc + ttoD2;
392 frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
393 frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
394 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
399 void InsetText::setUpdateStatus(int what) const
402 // we will to redraw us full if our LyXText wants it
403 if (text_.needRefresh())
406 // this to not draw a selection when we redraw all of it!
407 if (need_update & CURSOR && !(need_update & SELECTION)) {
408 if (text_.selection.set())
410 text_.clearSelection();
415 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
420 if (!autoBreakRows && paragraphs.size() > 1)
421 collapseParagraphs(bv);
423 text_.partialRebreak();
424 setUpdateStatus(what);
425 bool flag = mark_dirty ||
426 ((need_update != CURSOR && need_update != NONE) ||
427 text_.needRefresh() || text_.selection.set());
428 if (!text_.selection.set())
429 text_.selection.cursor = text_.cursor;
435 bv->updateInset(const_cast<InsetText *>(this));
438 if (need_update == CURSOR)
440 bv->owner()->view_state_changed();
441 bv->owner()->updateMenubar();
442 bv->owner()->updateToolbar();
443 if (old_par != cpar()) {
444 bv->owner()->setLayout(cpar()->layout()->name());
450 string const InsetText::editMessage() const
452 return _("Opened Text Inset");
456 void InsetText::insetUnlock(BufferView * bv)
458 if (the_locking_inset) {
459 the_locking_inset->insetUnlock(bv);
460 the_locking_inset = 0;
461 updateLocal(bv, CURSOR_PAR, false);
467 if (text_.selection.set()) {
468 text_.clearSelection();
470 } else if (owner()) {
471 bv->owner()->setLayout(owner()->getLyXText(bv)
472 ->cursor.par()->layout()->name());
474 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
475 // hack for deleteEmptyParMech
476 ParagraphList::iterator first_par = paragraphs.begin();
477 if (!first_par->empty()) {
478 text_.setCursor(first_par, 0);
479 } else if (paragraphs.size() > 1) {
480 text_.setCursor(boost::next(first_par), 0);
483 updateLocal(bv, code, false);
486 setUpdateStatus(code);
491 void InsetText::lockInset(BufferView * bv)
494 the_locking_inset = 0;
495 inset_pos = inset_x = inset_y = 0;
496 inset_boundary = false;
497 inset_par = paragraphs.end();
498 old_par = paragraphs.end();
499 text_.setCursorIntern(paragraphs.begin(), 0);
500 text_.clearSelection();
502 // If the inset is empty set the language of the current font to the
503 // language to the surronding text (if different).
504 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
505 bv->getParentLanguage(this) != text_.current_font.language()) {
506 LyXFont font(LyXFont::ALL_IGNORE);
507 font.setLanguage(bv->getParentLanguage(this));
508 setFont(bv, font, false);
511 if (drawFrame_ == LOCKED)
512 code = CURSOR|DRAW_FRAME;
513 setUpdateStatus(code);
517 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
519 the_locking_inset = inset;
520 inset_x = cix(bv) - top_x + drawTextXOffset;
521 inset_y = ciy() + drawTextYOffset;
524 inset_boundary = cboundary();
525 //updateLocal(bv, CURSOR, false);
529 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
531 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
535 if (!the_locking_inset) {
536 ParagraphList::iterator pit = paragraphs.begin();
537 ParagraphList::iterator pend = paragraphs.end();
539 int const id = inset->id();
540 for (; pit != pend; ++pit) {
541 InsetList::iterator it = pit->insetlist.begin();
542 InsetList::iterator const end = pit->insetlist.end();
543 for (; it != end; ++it) {
544 if (it->inset == inset) {
545 lyxerr << "InsetText::lockInsetInInset: 1 a\n";
546 text_.setCursorIntern(pit, it->pos);
547 lyxerr << "InsetText::lockInsetInInset: 1 b\n";
548 lyxerr << "bv: " << bv << " inset: " << inset << "\n";
549 lockInset(bv, inset);
550 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
553 if (it->inset->getInsetFromID(id)) {
554 lyxerr << "InsetText::lockInsetInInset: 2\n";
555 text_.setCursorIntern(pit, it->pos);
556 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
557 return the_locking_inset->lockInsetInInset(bv, inset);
561 lyxerr << "InsetText::lockInsetInInset: 3\n";
564 if (inset == cpar()->getInset(cpos())) {
565 lyxerr[Debug::INSETS] << "OK" << endl;
566 lockInset(bv, inset);
570 if (the_locking_inset && the_locking_inset == inset) {
571 if (cpar() == inset_par && cpos() == inset_pos) {
572 lyxerr[Debug::INSETS] << "OK" << endl;
573 inset_x = cix(bv) - top_x + drawTextXOffset;
574 inset_y = ciy() + drawTextYOffset;
576 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
578 } else if (the_locking_inset) {
579 lyxerr[Debug::INSETS] << "MAYBE" << endl;
580 return the_locking_inset->lockInsetInInset(bv, inset);
582 lyxerr[Debug::INSETS] << "NOT OK" << endl;
587 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
590 if (!the_locking_inset)
592 if (the_locking_inset == inset) {
593 the_locking_inset->insetUnlock(bv);
594 getLyXText(bv)->updateInset(inset);
595 the_locking_inset = 0;
597 moveRightIntern(bv, true, false);
598 old_par = paragraphs.end(); // force layout setting
602 updateLocal(bv, CURSOR, false);
605 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
609 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
611 if (!autoBreakRows && paragraphs.size() > 1)
612 collapseParagraphs(bv);
617 if (inset->owner() != this) {
618 int ustat = CURSOR_PAR;
620 UpdatableInset * tl_inset = the_locking_inset;
622 found = tl_inset->updateInsetInInset(bv, inset);
624 tl_inset = static_cast<UpdatableInset *>(inset);
625 while(tl_inset->owner() && tl_inset->owner() != this)
626 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
627 if (!tl_inset->owner())
629 found = tl_inset->updateInsetInInset(bv, inset);
633 text_.updateInset(tl_inset);
635 setUpdateStatus(ustat);
638 bool found = text_.updateInset(inset);
640 setUpdateStatus(CURSOR_PAR);
641 if (the_locking_inset &&
642 cpar() == inset_par && cpos() == inset_pos)
644 inset_x = cix(bv) - top_x + drawTextXOffset;
645 inset_y = ciy() + drawTextYOffset;
652 void InsetText::lfunMousePress(FuncRequest const & cmd)
656 // use this to check mouse motion for selection!
660 BufferView * bv = cmd.view();
661 FuncRequest cmd1 = cmd;
667 int tmp_x = cmd.x - drawTextXOffset;
668 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
669 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
671 if (the_locking_inset) {
672 if (the_locking_inset == inset) {
673 the_locking_inset->localDispatch(cmd1);
676 // otherwise only unlock the_locking_inset
677 the_locking_inset->insetUnlock(bv);
678 the_locking_inset = 0;
681 no_selection = false;
683 if (bv->theLockingInset()) {
684 if (isHighlyEditableInset(inset)) {
685 // We just have to lock the inset before calling a
687 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
688 if (!bv->lockInset(uinset)) {
689 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
691 inset->localDispatch(cmd1);
692 if (the_locking_inset)
693 updateLocal(bv, CURSOR, false);
698 bool paste_internally = false;
699 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
700 localDispatch(FuncRequest(bv, LFUN_COPY));
701 paste_internally = true;
703 int old_top_y = text_.top_y();
705 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
707 // set the selection cursor!
708 text_.selection.cursor = text_.cursor;
709 text_.cursor.x_fix(text_.cursor.x());
711 if (text_.selection.set()) {
712 text_.clearSelection();
713 updateLocal(bv, FULL, false);
715 text_.clearSelection();
716 updateLocal(bv, CURSOR, false);
719 bv->owner()->setLayout(cpar()->layout()->name());
721 // we moved the view we cannot do mouse selection in this case!
722 if (getLyXText(bv)->top_y() != old_top_y)
725 // Insert primary selection with middle mouse
726 // if there is a local selection in the current buffer,
728 if (cmd.button() == mouse_button::button2) {
729 if (paste_internally)
730 localDispatch(FuncRequest(bv, LFUN_PASTE));
732 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
735 getLyXText(bv)->clearSelection();
740 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
742 BufferView * bv = cmd.view();
743 FuncRequest cmd1 = cmd;
748 if (the_locking_inset)
749 return the_locking_inset->localDispatch(cmd1);
751 int tmp_x = cmd.x - drawTextXOffset;
752 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
753 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
756 // This code should probably be removed now. Simple insets
757 // (!highlyEditable) can actually take the localDispatch,
758 // and turn it into edit() if necessary. But we still
759 // need to deal properly with the whole relative vs.
760 // absolute mouse co-ords thing in a realiable, sensible way
762 if (isHighlyEditableInset(inset))
763 ret = inset->localDispatch(cmd1);
765 inset_x = cix(bv) - top_x + drawTextXOffset;
766 inset_y = ciy() + drawTextYOffset;
767 cmd1.x = cmd.x - inset_x;
768 cmd1.y = cmd.x - inset_y;
769 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
773 ret = inset->localDispatch(cmd1);
774 updateLocal(bv, CURSOR_PAR, false);
781 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
783 FuncRequest cmd1 = cmd;
787 if (the_locking_inset) {
788 the_locking_inset->localDispatch(cmd1);
792 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
795 BufferView * bv = cmd.view();
796 LyXCursor cur = text_.cursor;
797 text_.setCursorFromCoordinates
798 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
799 text_.cursor.x_fix(text_.cursor.x());
800 if (cur == text_.cursor)
802 text_.setSelection();
803 bool flag = (text_.toggle_cursor.par() != text_.toggle_end_cursor.par() ||
804 text_.toggle_cursor.pos() != text_.toggle_end_cursor.pos());
806 updateLocal(bv, SELECTION, false);
811 Inset::RESULT InsetText::localDispatch(FuncRequest const & cmd)
813 BufferView * bv = cmd.view();
816 switch (cmd.action) {
817 case 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));
881 case LFUN_MOUSE_PRESS:
885 case LFUN_MOUSE_MOTION:
886 lfunMouseMotion(cmd);
889 case LFUN_MOUSE_RELEASE:
890 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
896 bool was_empty = (paragraphs.begin()->empty() &&
897 paragraphs.size() == 1);
899 no_selection = false;
900 RESULT result = UpdatableInset::localDispatch(cmd);
901 if (result != UNDISPATCHED)
905 if (cmd.action < 0 && cmd.argument.empty())
908 if (the_locking_inset) {
909 result = the_locking_inset->localDispatch(cmd);
910 if (result == DISPATCHED_NOUPDATE)
912 else if (result == DISPATCHED) {
913 updateLocal(bv, CURSOR_PAR, false);
915 } else if (result >= FINISHED) {
918 moveRightIntern(bv, false, false);
922 if ((result = moveUp(bv)) >= FINISHED) {
923 updateLocal(bv, CURSOR, false);
924 bv->unlockInset(this);
928 if ((result = moveDown(bv)) >= FINISHED) {
929 updateLocal(bv, CURSOR, false);
930 bv->unlockInset(this);
937 the_locking_inset = 0;
938 updateLocal(bv, CURSOR, false);
939 // make sure status gets reset immediately
940 bv->owner()->clearMessage();
947 // what type of update to do on a cursor movement
948 int cursor_update = CURSOR;
950 if (text_.selection.set())
951 cursor_update = SELECTION;
953 switch (cmd.action) {
956 case LFUN_SELFINSERT:
957 if (bv->buffer()->isReadonly()) {
958 // setErrorMessage(N_("Document is read only"));
961 if (!cmd.argument.empty()) {
962 /* Automatically delete the currently selected
963 * text and replace it with what is being
964 * typed in now. Depends on lyxrc settings
965 * "auto_region_delete", which defaults to
968 // This should not be needed here and is also WRONG!
969 setUndo(bv, Undo::INSERT, text_.cursor.par());
972 if (lyxrc.auto_region_delete) {
973 if (text_.selection.set()) {
974 text_.cutSelection(false, false);
977 text_.clearSelection();
978 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
979 bv->owner()->getIntl().getTransManager().
980 TranslateAndInsert(cmd.argument[i], &text_);
983 text_.selection.cursor = text_.cursor;
984 updwhat = CURSOR | CURSOR_PAR;
986 result = DISPATCHED_NOUPDATE;
989 // cursor movements that need special handling
992 result = moveRight(bv);
994 updwhat = cursor_update;
998 result = moveLeft(bv);
999 updwhat = cursor_update;
1003 result = moveDown(bv);
1004 updwhat = cursor_update;
1008 result = moveUp(bv);
1009 updwhat = cursor_update;
1013 if (crow() == text_.rows().begin())
1014 result = FINISHED_UP;
1016 text_.cursorPrevious();
1017 text_.clearSelection();
1018 result = DISPATCHED_NOUPDATE;
1020 updwhat = cursor_update;
1024 if (boost::next(crow()) == text_.rows().end())
1025 result = FINISHED_DOWN;
1028 text_.clearSelection();
1029 result = DISPATCHED_NOUPDATE;
1031 updwhat = cursor_update;
1034 case LFUN_BACKSPACE: {
1035 if (text_.selection.set())
1036 text_.cutSelection(true, false);
1039 updwhat = CURSOR_PAR;
1045 if (text_.selection.set()) {
1046 text_.cutSelection(true, false);
1050 updwhat = CURSOR_PAR;
1056 text_.cutSelection(true, true);
1057 updwhat = CURSOR_PAR;
1064 text_.copySelection();
1065 updwhat = CURSOR_PAR;
1068 case LFUN_PASTESELECTION:
1070 string const clip(bv->getClipboard());
1074 if (cmd.argument == "paragraph") {
1075 text_.insertStringAsParagraphs(clip);
1077 text_.insertStringAsLines(clip);
1080 text_.clearSelection();
1082 updwhat = CURSOR_PAR;
1088 if (!autoBreakRows) {
1089 if (CutAndPaste::nrOfParagraphs() > 1) {
1090 #ifdef WITH_WARNINGS
1091 #warning FIXME horrendously bad UI
1093 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1098 replaceSelection(bv->getLyXText());
1099 size_t sel_index = 0;
1100 string const & arg = cmd.argument;
1101 if (isStrUnsignedInt(arg)) {
1102 size_t const paste_arg = strToUnsignedInt(arg);
1103 #warning FIXME Check if the arg is in the domain of available selections.
1104 sel_index = paste_arg;
1106 text_.pasteSelection(sel_index);
1108 text_.clearSelection();
1109 updwhat = CURSOR_PAR;
1114 case LFUN_BREAKPARAGRAPH:
1115 if (!autoBreakRows) {
1116 result = DISPATCHED;
1119 replaceSelection(bv->getLyXText());
1120 text_.breakParagraph(paragraphs, 0);
1121 updwhat = CURSOR | FULL;
1125 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1126 if (!autoBreakRows) {
1127 result = DISPATCHED;
1130 replaceSelection(bv->getLyXText());
1131 text_.breakParagraph(paragraphs, 1);
1132 updwhat = CURSOR | FULL;
1136 case LFUN_BREAKLINE: {
1137 if (!autoBreakRows) {
1138 result = DISPATCHED;
1142 replaceSelection(bv->getLyXText());
1143 text_.insertInset(new InsetNewline);
1144 updwhat = CURSOR | CURSOR_PAR;
1150 // do not set layouts on non breakable textinsets
1151 if (autoBreakRows) {
1152 string cur_layout = cpar()->layout()->name();
1154 // Derive layout number from given argument (string)
1155 // and current buffer's textclass (number). */
1156 LyXTextClass const & tclass =
1157 bv->buffer()->params.getLyXTextClass();
1158 string layout = cmd.argument;
1159 bool hasLayout = tclass.hasLayout(layout);
1161 // If the entry is obsolete, use the new one instead.
1163 string const & obs =
1164 tclass[layout]->obsoleted_by();
1169 // see if we found the layout number:
1171 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1172 bv->owner()->dispatch(lf);
1176 if (cur_layout != layout) {
1177 cur_layout = layout;
1178 text_.setLayout(layout);
1179 bv->owner()->setLayout(cpar()->layout()->name());
1180 updwhat = CURSOR_PAR;
1184 // reset the layout box
1185 bv->owner()->setLayout(cpar()->layout()->name());
1188 case LFUN_PARAGRAPH_SPACING:
1189 // This one is absolutely not working. When fiddling with this
1190 // it also seems to me that the paragraphs inside the insettext
1191 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1192 // FIXME: how old is this comment ? ...
1194 ParagraphList::iterator pit = text_.cursor.par();
1195 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1196 float cur_value = 1.0;
1197 if (cur_spacing == Spacing::Other) {
1198 cur_value = pit->params().spacing().getValue();
1201 istringstream istr(STRCONV(cmd.argument));
1204 Spacing::Space new_spacing = cur_spacing;
1205 float new_value = cur_value;
1207 lyxerr << "Missing argument to `paragraph-spacing'"
1209 } else if (tmp == "single") {
1210 new_spacing = Spacing::Single;
1211 } else if (tmp == "onehalf") {
1212 new_spacing = Spacing::Onehalf;
1213 } else if (tmp == "double") {
1214 new_spacing = Spacing::Double;
1215 } else if (tmp == "other") {
1216 new_spacing = Spacing::Other;
1219 lyxerr << "new_value = " << tmpval << endl;
1222 } else if (tmp == "default") {
1223 new_spacing = Spacing::Default;
1225 lyxerr << _("Unknown spacing argument: ")
1226 << cmd.argument << endl;
1228 if (cur_spacing != new_spacing || cur_value != new_value) {
1229 pit->params().spacing(Spacing(new_spacing, new_value));
1230 updwhat = CURSOR_PAR;
1236 // These need to do repaints but don't require
1237 // special handling otherwise. A *lot* of the
1238 // above could probably be done similarly ...
1243 case LFUN_WORDRIGHT:
1244 // these two are really unhandled ...
1246 case LFUN_BEGINNINGBUF:
1247 updwhat = cursor_update;
1248 if (!bv->dispatch(cmd))
1249 result = UNDISPATCHED;
1258 case LFUN_WORDLEFTSEL:
1259 case LFUN_WORDRIGHTSEL:
1260 updwhat = SELECTION;
1265 if (!bv->dispatch(cmd))
1266 result = UNDISPATCHED;
1271 updateLocal(bv, updwhat, updflag);
1272 /// If the action has deleted all text in the inset, we need to change the
1273 // language to the language of the surronding text.
1274 if (!was_empty && paragraphs.begin()->empty() &&
1275 paragraphs.size() == 1) {
1276 LyXFont font(LyXFont::ALL_IGNORE);
1277 font.setLanguage(bv->getParentLanguage(this));
1278 setFont(bv, font, false);
1281 if (result >= FINISHED)
1282 bv->unlockInset(this);
1284 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1285 result = DISPATCHED;
1290 int InsetText::latex(Buffer const * buf, ostream & os,
1291 LatexRunParams const & runparams) const
1294 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1295 return texrow.rows();
1299 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1301 unsigned int lines = 0;
1303 ParagraphList::const_iterator beg = paragraphs.begin();
1304 ParagraphList::const_iterator end = paragraphs.end();
1305 ParagraphList::const_iterator it = beg;
1306 for (; it != end; ++it) {
1307 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1308 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1315 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1317 unsigned int lines = 0;
1319 vector<string> environment_stack(10);
1320 vector<string> environment_inner(10);
1322 int const command_depth = 0;
1325 Paragraph::depth_type depth = 0; // paragraph depth
1327 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1328 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1330 for (; pit != pend; ++pit) {
1332 int desc_on = 0; // description mode
1334 LyXLayout_ptr const & style = pit->layout();
1336 // environment tag closing
1337 for (; depth > pit->params().depth(); --depth) {
1338 if (environment_inner[depth] != "!-- --") {
1339 item_name = "listitem";
1340 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1341 if (environment_inner[depth] == "varlistentry")
1342 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1344 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1345 environment_stack[depth].erase();
1346 environment_inner[depth].erase();
1349 if (depth == pit->params().depth()
1350 && environment_stack[depth] != style->latexname()
1351 && !environment_stack[depth].empty()) {
1352 if (environment_inner[depth] != "!-- --") {
1353 item_name= "listitem";
1354 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1355 if (environment_inner[depth] == "varlistentry")
1356 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1359 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1361 environment_stack[depth].erase();
1362 environment_inner[depth].erase();
1365 // Write opening SGML tags.
1366 switch (style->latextype) {
1367 case LATEX_PARAGRAPH:
1368 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1372 buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1376 case LATEX_ENVIRONMENT:
1377 case LATEX_ITEM_ENVIRONMENT:
1378 if (depth < pit->params().depth()) {
1379 depth = pit->params().depth();
1380 environment_stack[depth].erase();
1383 if (environment_stack[depth] != style->latexname()) {
1384 if (environment_stack.size() == depth + 1) {
1385 environment_stack.push_back("!-- --");
1386 environment_inner.push_back("!-- --");
1388 environment_stack[depth] = style->latexname();
1389 environment_inner[depth] = "!-- --";
1390 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1392 if (environment_inner[depth] != "!-- --") {
1393 item_name= "listitem";
1394 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1395 if (environment_inner[depth] == "varlistentry")
1396 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1400 if (style->latextype == LATEX_ENVIRONMENT) {
1401 if (!style->latexparam().empty()) {
1402 if (style->latexparam() == "CDATA")
1405 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1410 desc_on = (style->labeltype == LABEL_MANUAL);
1412 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1413 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1415 item_name = desc_on ? "term" : "para";
1416 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1420 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1424 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1427 // write closing SGML tags
1428 switch (style->latextype) {
1429 case LATEX_ENVIRONMENT:
1430 if (!style->latexparam().empty()) {
1431 if (style->latexparam() == "CDATA")
1434 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1437 case LATEX_ITEM_ENVIRONMENT:
1438 if (desc_on == 1) break;
1440 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1442 case LATEX_PARAGRAPH:
1443 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1446 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1452 for (int d = depth; d >= 0; --d) {
1453 if (!environment_stack[depth].empty()) {
1454 if (environment_inner[depth] != "!-- --") {
1455 item_name = "listitem";
1456 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1457 if (environment_inner[depth] == "varlistentry")
1458 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1461 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1469 void InsetText::validate(LaTeXFeatures & features) const
1471 for_each(paragraphs.begin(), paragraphs.end(),
1472 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1476 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1478 if (the_locking_inset) {
1479 the_locking_inset->getCursor(bv, x, y);
1483 y = cy() + InsetText::y();
1487 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1489 if (the_locking_inset) {
1490 the_locking_inset->getCursorPos(bv, x, y);
1493 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1494 y = cy() - TEXT_TO_INSET_OFFSET;
1498 int InsetText::insetInInsetY() const
1500 if (!the_locking_inset)
1503 return (inset_y + the_locking_inset->insetInInsetY());
1507 void InsetText::fitInsetCursor(BufferView * bv) const
1509 if (the_locking_inset) {
1510 the_locking_inset->fitInsetCursor(bv);
1513 LyXFont const font = text_.getFont(bv->buffer(), cpar(), cpos());
1515 int const asc = font_metrics::maxAscent(font);
1516 int const desc = font_metrics::maxDescent(font);
1518 if (bv->fitLockedInsetCursor(cx(bv), cy(), asc, desc))
1519 need_update |= FULL;
1524 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1526 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1527 return moveLeftIntern(bv, false, activate_inset, selecting);
1529 return moveRightIntern(bv, true, activate_inset, selecting);
1534 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1536 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1537 return moveRightIntern(bv, true, activate_inset, selecting);
1539 return moveLeftIntern(bv, false, activate_inset, selecting);
1544 InsetText::moveRightIntern(BufferView * bv, bool front,
1545 bool activate_inset, bool selecting)
1547 ParagraphList::iterator c_par = cpar();
1549 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1550 return FINISHED_RIGHT;
1551 if (activate_inset && checkAndActivateInset(bv, front))
1553 text_.cursorRight(bv);
1555 text_.clearSelection();
1556 return DISPATCHED_NOUPDATE;
1561 InsetText::moveLeftIntern(BufferView * bv, bool front,
1562 bool activate_inset, bool selecting)
1564 if (cpar() == paragraphs.begin() && cpos() <= 0)
1566 text_.cursorLeft(bv);
1568 text_.clearSelection();
1569 if (activate_inset && checkAndActivateInset(bv, front))
1571 return DISPATCHED_NOUPDATE;
1575 Inset::RESULT InsetText::moveUp(BufferView * bv)
1577 if (crow() == text_.rows().begin())
1580 text_.clearSelection();
1581 return DISPATCHED_NOUPDATE;
1585 Inset::RESULT InsetText::moveDown(BufferView * bv)
1587 if (boost::next(crow()) == text_.rows().end())
1588 return FINISHED_DOWN;
1589 text_.cursorDown(bv);
1590 text_.clearSelection();
1591 return DISPATCHED_NOUPDATE;
1595 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1597 if (the_locking_inset) {
1598 if (the_locking_inset->insetAllowed(inset))
1599 return the_locking_inset->insertInset(bv, inset);
1602 inset->setOwner(this);
1603 text_.insertInset(inset);
1605 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1610 bool InsetText::insetAllowed(Inset::Code code) const
1612 // in_insetAllowed is a really gross hack,
1613 // to allow us to call the owner's insetAllowed
1614 // without stack overflow, which can happen
1615 // when the owner uses InsetCollapsable::insetAllowed()
1617 if (in_insetAllowed)
1619 in_insetAllowed = true;
1620 if (the_locking_inset)
1621 ret = the_locking_inset->insetAllowed(code);
1623 ret = owner()->insetAllowed(code);
1624 in_insetAllowed = false;
1629 UpdatableInset * InsetText::getLockingInset() const
1631 return the_locking_inset ? the_locking_inset->getLockingInset() :
1632 const_cast<InsetText *>(this);
1636 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1640 if (the_locking_inset)
1641 return the_locking_inset->getFirstLockingInsetOfType(c);
1646 bool InsetText::showInsetDialog(BufferView * bv) const
1648 if (the_locking_inset)
1649 return the_locking_inset->showInsetDialog(bv);
1654 void InsetText::getLabelList(std::vector<string> & list) const
1656 ParagraphList::const_iterator pit = paragraphs.begin();
1657 ParagraphList::const_iterator pend = paragraphs.end();
1658 for (; pit != pend; ++pit) {
1659 InsetList::const_iterator beg = pit->insetlist.begin();
1660 InsetList::const_iterator end = pit->insetlist.end();
1661 for (; beg != end; ++beg)
1662 beg->inset->getLabelList(list);
1667 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1670 if (the_locking_inset) {
1671 the_locking_inset->setFont(bv, font, toggleall, selectall);
1675 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1676 || cpar()->empty()) {
1677 text_.setFont(font, toggleall);
1682 if (text_.selection.set())
1683 setUndo(bv, Undo::EDIT, text_.cursor.par());
1687 text_.selection.cursor = text_.cursor;
1688 text_.cursorBottom();
1689 text_.setSelection();
1692 text_.toggleFree(font, toggleall);
1695 text_.clearSelection();
1699 bool flag = (selectall || text_.selection.set());
1702 updateLocal(bv, FULL, true);
1704 updateLocal(bv, CURSOR_PAR, true);
1708 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1710 if (cpar()->isInset(cpos())) {
1712 static_cast<UpdatableInset*>(cpar()->getInset(cpos()));
1713 if (!isHighlyEditableInset(inset))
1715 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1716 inset->localDispatch(cmd);
1717 if (!the_locking_inset)
1719 updateLocal(bv, CURSOR, false);
1726 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1727 mouse_button::state button)
1729 x -= drawTextXOffset;
1731 int dummyy = y + dim_.asc;
1732 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1733 // we only do the edit() call if the inset was hit by the mouse
1734 // or if it is a highly editable inset. So we should call this
1735 // function from our own edit with button < 0.
1736 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1738 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1746 inset_x = cix(bv) - top_x + drawTextXOffset;
1747 inset_y = ciy() + drawTextYOffset;
1748 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1749 inset->localDispatch(cmd);
1750 if (!the_locking_inset)
1752 updateLocal(bv, CURSOR, false);
1759 void InsetText::setParagraphData(ParagraphList const & plist)
1761 // we have to unlock any locked inset otherwise we're in troubles
1762 the_locking_inset = 0;
1764 // But it it makes no difference that is a lot better.
1766 // See if this can be simplified when std::list is in effect.
1769 ParagraphList::const_iterator it = plist.begin();
1770 ParagraphList::const_iterator end = plist.end();
1771 for (; it != end; ++it) {
1772 paragraphs.push_back(*it);
1773 Paragraph & tmp = paragraphs.back();
1774 tmp.setInsetOwner(this);
1782 void InsetText::markNew(bool track_changes)
1784 ParagraphList::iterator pit = paragraphs.begin();
1785 ParagraphList::iterator pend = paragraphs.end();
1786 for (; pit != pend; ++pit) {
1787 if (track_changes) {
1788 pit->trackChanges();
1790 // no-op when not tracking
1791 pit->cleanChanges();
1797 void InsetText::setText(string const & data, LyXFont const & font)
1800 for (unsigned int i = 0; i < data.length(); ++i)
1801 paragraphs.begin()->insertChar(i, data[i], font);
1806 void InsetText::setAutoBreakRows(bool flag)
1808 if (flag != autoBreakRows) {
1809 autoBreakRows = flag;
1817 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1819 if (how != drawFrame_) {
1822 updateLocal(bv, DRAW_FRAME, false);
1827 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1829 if (frame_color != col) {
1832 updateLocal(bv, DRAW_FRAME, false);
1837 int InsetText::cx(BufferView * bv) const
1839 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1840 if (the_locking_inset) {
1841 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1842 text_.cursor.pos());
1843 if (font.isVisibleRightToLeft())
1844 x -= the_locking_inset->width();
1850 int InsetText::cix(BufferView * bv) const
1852 int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1853 if (the_locking_inset) {
1854 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1855 text_.cursor.pos());
1856 if (font.isVisibleRightToLeft())
1857 x -= the_locking_inset->width();
1863 int InsetText::cy() const
1865 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1869 int InsetText::ciy() const
1871 return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1875 pos_type InsetText::cpos() const
1877 return text_.cursor.pos();
1881 ParagraphList::iterator InsetText::cpar() const
1883 return text_.cursor.par();
1887 bool InsetText::cboundary() const
1889 return text_.cursor.boundary();
1893 RowList::iterator InsetText::crow() const
1895 return text_.cursorRow();
1899 LyXText * InsetText::getLyXText(BufferView const * bv,
1900 bool const recursive) const
1903 if (recursive && the_locking_inset)
1904 return the_locking_inset->getLyXText(bv, true);
1909 void InsetText::setViewCache(BufferView const * bv) const
1912 text_.bv_owner = const_cast<BufferView *>(bv);
1916 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1919 /// then remove all LyXText in text-insets
1920 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1921 const_cast<ParagraphList&>(paragraphs).end(),
1922 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1927 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1929 if (paragraphs.size() == 1 && paragraphs.begin()->empty()) {
1930 // no data, resize not neccessary!
1931 // we have to do this as a fixed width may have changed!
1934 restoreLyXTextState();
1946 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1947 const_cast<ParagraphList&>(paragraphs).end(),
1948 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1951 restoreLyXTextState();
1953 // seems to be unneeded
1955 if (the_locking_inset) {
1956 inset_x = cix(bv) - top_x + drawTextXOffset;
1957 inset_y = ciy() + drawTextYOffset;
1960 text_.top_y(bv->screen().topCursorVisible(&text_));
1962 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
1963 // this will scroll the screen such that the cursor becomes visible
1964 bv->updateScrollbar();
1966 need_update |= FULL;
1972 void InsetText::reinitLyXText() const
1974 BufferView * bv = text_.bv_owner;
1981 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1982 const_cast<ParagraphList&>(paragraphs).end(),
1983 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1986 restoreLyXTextState();
1987 if (the_locking_inset) {
1988 inset_x = cix(bv) - top_x + drawTextXOffset;
1989 inset_y = ciy() + drawTextYOffset;
1991 text_.top_y(bv->screen().topCursorVisible(&text_));
1993 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
1994 // this will scroll the screen such that the cursor becomes visible
1995 bv->updateScrollbar();
2002 void InsetText::removeNewlines()
2004 bool changed = false;
2006 ParagraphList::iterator it = paragraphs.begin();
2007 ParagraphList::iterator end = paragraphs.end();
2008 for (; it != end; ++it) {
2009 for (int i = 0; i < it->size(); ++i) {
2010 if (it->isNewline(i)) {
2021 bool InsetText::nodraw() const
2023 if (the_locking_inset)
2024 return the_locking_inset->nodraw();
2025 return UpdatableInset::nodraw();
2029 int InsetText::scroll(bool recursive) const
2031 int sx = UpdatableInset::scroll(false);
2033 if (recursive && the_locking_inset)
2034 sx += the_locking_inset->scroll(recursive);
2040 void InsetText::clearSelection(BufferView * bv)
2042 getLyXText(bv)->clearSelection();
2046 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2048 Painter & pain = bv->painter();
2050 int h = dim_.asc + dim_.des;
2051 int ty = baseline - dim_.asc;
2057 if ((ty + h) > pain.paperHeight())
2058 h = pain.paperHeight();
2059 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2060 w = pain.paperWidth();
2061 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2066 ParagraphList * InsetText::getParagraphs(int i) const
2068 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2072 LyXCursor const & InsetText::cursor(BufferView * bv) const
2074 if (the_locking_inset)
2075 return the_locking_inset->cursor(bv);
2076 return getLyXText(bv)->cursor;
2080 Inset * InsetText::getInsetFromID(int id_arg) const
2083 return const_cast<InsetText *>(this);
2085 ParagraphList::const_iterator pit = paragraphs.begin();
2086 ParagraphList::const_iterator pend = paragraphs.end();
2087 for (; pit != pend; ++pit) {
2088 InsetList::const_iterator it = pit->insetlist.begin();
2089 InsetList::const_iterator end = pit->insetlist.end();
2090 for (; it != end; ++it) {
2091 if (it->inset->id() == id_arg)
2093 Inset * in = it->inset->getInsetFromID(id_arg);
2103 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2106 if (the_locking_inset) {
2107 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2108 if (!word.word().empty()) {
2112 // we have to go on checking so move cursor to the next char
2113 text_.cursor.pos(text_.cursor.pos() + 1);
2115 word = text_.selectNextWordToSpellcheck(value);
2116 if (word.word().empty())
2117 bv->unlockInset(const_cast<InsetText *>(this));
2124 void InsetText::selectSelectedWord(BufferView * bv)
2126 if (the_locking_inset) {
2127 the_locking_inset->selectSelectedWord(bv);
2130 getLyXText(bv)->selectSelectedWord();
2131 updateLocal(bv, SELECTION, false);
2135 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2137 if (the_locking_inset) {
2138 the_locking_inset->toggleSelection(bv, kill_selection);
2141 int x = top_x + TEXT_TO_INSET_OFFSET;
2143 RowList::iterator rowit = text_.rows().begin();
2144 RowList::iterator end = text_.rows().end();
2145 int y_offset = top_baseline - rowit->ascent_of_text();
2147 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2148 y += rowit->height();
2154 if (need_update & SELECTION)
2156 bv->screen().toggleSelection(&text_, bv, kill_selection, y_offset, x);
2160 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2162 if (the_locking_inset) {
2163 if (the_locking_inset->nextChange(bv, length))
2165 text_.cursorRight(true);
2167 lyxfind::SearchResult result =
2168 lyxfind::findNextChange(bv, &text_, length);
2170 if (result == lyxfind::SR_FOUND) {
2171 LyXCursor cur = text_.cursor;
2172 bv->unlockInset(bv->theLockingInset());
2173 if (bv->lockInset(this))
2176 text_.setSelectionRange(length);
2177 updateLocal(bv, SELECTION, false);
2179 return result != lyxfind::SR_NOT_FOUND;
2183 bool InsetText::searchForward(BufferView * bv, string const & str,
2186 if (the_locking_inset) {
2187 if (the_locking_inset->searchForward(bv, str, cs, mw))
2189 text_.cursorRight(true);
2191 lyxfind::SearchResult result =
2192 lyxfind::LyXFind(bv, &text_, str, true, cs, mw);
2194 if (result == lyxfind::SR_FOUND) {
2195 LyXCursor cur = text_.cursor;
2196 bv->unlockInset(bv->theLockingInset());
2197 if (bv->lockInset(this))
2200 text_.setSelectionRange(str.length());
2201 updateLocal(bv, SELECTION, false);
2203 return (result != lyxfind::SR_NOT_FOUND);
2206 bool InsetText::searchBackward(BufferView * bv, string const & str,
2209 if (the_locking_inset) {
2210 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2214 ParagraphList::iterator pit = paragraphs.begin();
2215 ParagraphList::iterator pend = paragraphs.end();
2217 while (boost::next(pit) != pend)
2220 text_.setCursor(pit, pit->size());
2222 lyxfind::SearchResult result =
2223 lyxfind::LyXFind(bv, &text_, str, false, cs, mw);
2225 if (result == lyxfind::SR_FOUND) {
2226 LyXCursor cur = text_.cursor;
2227 bv->unlockInset(bv->theLockingInset());
2228 if (bv->lockInset(this))
2231 text_.setSelectionRange(str.length());
2232 updateLocal(bv, SELECTION, false);
2234 return (result != lyxfind::SR_NOT_FOUND);
2238 bool InsetText::checkInsertChar(LyXFont & font)
2241 return owner()->checkInsertChar(font);
2246 void InsetText::collapseParagraphs(BufferView * bv)
2248 while (paragraphs.size() > 1) {
2249 ParagraphList::iterator first_par = paragraphs.begin();
2250 ParagraphList::iterator next_par = boost::next(first_par);
2251 size_t const first_par_size = first_par->size();
2253 if (!first_par->empty() &&
2254 !next_par->empty() &&
2255 !first_par->isSeparator(first_par_size - 1)) {
2256 first_par->insertChar(first_par_size, ' ');
2259 if (text_.selection.set()) {
2260 if (text_.selection.start.par() == next_par) {
2261 text_.selection.start.par(first_par);
2262 text_.selection.start.pos(
2263 text_.selection.start.pos() + first_par_size);
2265 if (text_.selection.end.par() == next_par) {
2266 text_.selection.end.par(first_par);
2267 text_.selection.end.pos(
2268 text_.selection.end.pos() + first_par_size);
2272 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2278 void InsetText::getDrawFont(LyXFont & font) const
2282 owner()->getDrawFont(font);
2286 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2288 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2289 // And it probably does. You have to take a look at this John. (Lgb)
2290 #warning John, have a look here. (Lgb)
2291 ParagraphList::iterator pit = plist.begin();
2292 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2294 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2296 ParagraphList::iterator pend = plist.end();
2297 for (; pit != pend; ++pit) {
2298 paragraphs.push_back(*pit);
2305 void InsetText::addPreview(PreviewLoader & loader) const
2307 ParagraphList::const_iterator pit = paragraphs.begin();
2308 ParagraphList::const_iterator pend = paragraphs.end();
2310 for (; pit != pend; ++pit) {
2311 InsetList::const_iterator it = pit->insetlist.begin();
2312 InsetList::const_iterator end = pit->insetlist.end();
2313 for (; it != end; ++it) {
2314 it->inset->addPreview(loader);