3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS
13 #include "insettext.h"
16 #include "BufferView.h"
17 #include "bufferview_funcs.h"
18 #include "CutAndPaste.h"
20 #include "dimension.h"
21 #include "funcrequest.h"
23 #include "errorlist.h"
25 #include "LaTeXFeatures.h"
29 #include "lyxcursor.h"
35 #include "paragraph.h"
36 #include "ParagraphParameters.h"
37 #include "trans_mgr.h"
38 #include "undo_funcs.h"
39 #include "WordLangTuple.h"
40 #include "paragraph_funcs.h"
42 #include "rowpainter.h"
43 #include "insetnewline.h"
44 #include "metricsinfo.h"
46 #include "frontends/Alert.h"
47 #include "frontends/Dialogs.h"
48 #include "frontends/font_metrics.h"
49 #include "frontends/LyXView.h"
50 #include "frontends/Painter.h"
51 #include "frontends/screen.h"
53 #include "support/textutils.h"
54 #include "support/LAssert.h"
55 #include "support/lstrings.h"
56 #include "support/lyxalgo.h" // lyx::count
58 #include <boost/bind.hpp>
75 using namespace lyx::support;
76 using namespace lyx::graphics;
77 using namespace bv_funcs;
80 using lyx::textclass_type;
82 // These functions should probably go into bufferview_funcs somehow (Jug)
84 void InsetText::saveLyXTextState() const
86 // check if my paragraphs are still valid
87 ParagraphList::iterator it = const_cast<ParagraphList&>(paragraphs).begin();
88 ParagraphList::iterator end = const_cast<ParagraphList&>(paragraphs).end();
89 for (; it != end; ++it) {
90 if (it == text_.cursor.par())
94 if (it != end && text_.cursor.pos() <= it->size())
95 sstate = text_; // slicing intended
97 sstate.cursor.par(end);
101 void InsetText::restoreLyXTextState() const
103 if (sstate.cursor.par() == const_cast<ParagraphList&>(paragraphs).end())
106 text_.selection.set(true);
107 // at this point just to avoid the DEPM when setting the cursor
108 text_.selection.mark(sstate.selection.mark());
109 if (sstate.selection.set()) {
110 text_.setCursor(sstate.selection.start.par(),
111 sstate.selection.start.pos(),
112 true, sstate.selection.start.boundary());
113 text_.selection.cursor = text_.cursor;
114 text_.setCursor(sstate.selection.end.par(), sstate.selection.end.pos(),
115 true, sstate.selection.end.boundary());
116 text_.setSelection();
117 text_.setCursor(sstate.cursor.par(), sstate.cursor.pos());
119 text_.setCursor(sstate.cursor.par(), sstate.cursor.pos(),
120 true, sstate.cursor.boundary());
121 text_.selection.cursor = text_.cursor;
122 text_.selection.set(false);
127 InsetText::InsetText(BufferParams const & bp)
128 : UpdatableInset(), text_(0, this)
130 paragraphs.push_back(Paragraph());
131 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
132 if (bp.tracking_changes)
133 paragraphs.begin()->trackChanges();
138 InsetText::InsetText(InsetText const & in)
139 : UpdatableInset(in), text_(0, this)
145 InsetText & InsetText::operator=(InsetText const & it)
152 void InsetText::init(InsetText const * ins)
155 textwidth_ = ins->textwidth_;
156 text_.bv_owner = ins->text_.bv_owner;
157 setParagraphData(ins->paragraphs);
158 autoBreakRows = ins->autoBreakRows;
159 drawFrame_ = ins->drawFrame_;
160 frame_color = ins->frame_color;
162 textwidth_ = 0; // unbounded
164 frame_color = LColor::insetframe;
165 autoBreakRows = false;
167 the_locking_inset = 0;
168 for_each(paragraphs.begin(), paragraphs.end(),
169 boost::bind(&Paragraph::setInsetOwner, _1, this));
176 old_par = paragraphs.end();
177 last_drawn_width = -1;
178 sstate.cursor.par(paragraphs.end());
179 in_insetAllowed = false;
183 void InsetText::clear(bool just_mark_erased)
185 if (just_mark_erased) {
186 ParagraphList::iterator it = paragraphs.begin();
187 ParagraphList::iterator end = paragraphs.end();
188 for (; it != end; ++it) {
195 // This is a gross hack...
196 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
199 paragraphs.push_back(Paragraph());
200 paragraphs.begin()->setInsetOwner(this);
201 paragraphs.begin()->layout(old_layout);
208 auto_ptr<InsetBase> InsetText::clone() const
210 return auto_ptr<InsetBase>(new InsetText(*this));
214 void InsetText::write(Buffer const * buf, ostream & os) const
217 writeParagraphData(buf, os);
221 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
223 ParagraphList::const_iterator it = paragraphs.begin();
224 ParagraphList::const_iterator end = paragraphs.end();
225 Paragraph::depth_type dth = 0;
226 for (; it != end; ++it) {
227 it->write(buf, os, buf->params, dth);
232 void InsetText::read(Buffer const * buf, LyXLex & lex)
235 Paragraph::depth_type depth = 0;
239 if (buf->params.tracking_changes)
240 paragraphs.begin()->trackChanges();
242 // delete the initial paragraph
244 ParagraphList::iterator pit = paragraphs.begin();
248 token = lex.getString();
251 if (token == "\\end_inset") {
255 if (token == "\\the_end") {
256 lex.printError("\\the_end read in inset! Error in document!");
261 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
264 pit = paragraphs.begin();
265 ParagraphList::iterator const end = paragraphs.end();
266 for (; pit != end; ++pit)
267 pit->setInsetOwner(this);
269 if (token != "\\end_inset") {
270 lex.printError("Missing \\end_inset at this point. "
277 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
279 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << "\n";
281 textwidth_ = mi.base.textwidth;
282 BufferView * bv = mi.base.bv;
284 text_.metrics(mi, dim);
285 dim.asc += TEXT_TO_INSET_OFFSET;
286 dim.des += TEXT_TO_INSET_OFFSET;
287 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
288 dim.wid = max(dim.wid, 10);
293 int InsetText::textWidth() const
299 void InsetText::draw(PainterInfo & pi, int x, int baseline) const
304 // update our idea of where we are. Clearly, we should
305 // not have to know this information.
308 int const start_x = x;
310 BufferView * bv = pi.base.bv;
311 Painter & pain = pi.pain;
313 // repaint the background if needed
314 if (backgroundColor() != LColor::background)
315 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
317 // no draw is necessary !!!
318 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
319 top_baseline = baseline;
329 top_baseline = baseline;
330 top_y = baseline - dim_.asc;
332 if (last_drawn_width != dim_.wid) {
334 last_drawn_width = dim_.wid;
337 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
338 inset_x = cix() - x + drawTextXOffset;
339 inset_y = ciy() + drawTextYOffset;
342 x += TEXT_TO_INSET_OFFSET;
344 RowList::iterator rit = text_.rows().begin();
345 RowList::iterator end = text_.rows().end();
347 int y_offset = baseline - rit->ascent_of_text();
348 int y2 = pain.paperHeight();
351 while (rit != end && y + rit->height() <= 0) {
353 first += rit->height();
357 text_.top_y(-y_offset);
365 int yo = y_offset + first;
367 paintRows(*bv, text_, rit, x, 0, yo, y2, yo);
371 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
372 drawFrame(pain, int(start_x));
374 if (need_update != INIT)
379 void InsetText::drawFrame(Painter & pain, int x) const
381 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
383 frame_y = top_baseline - dim_.asc + ttoD2;
384 frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
385 frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
386 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
391 void InsetText::setUpdateStatus(int what) const
394 // we will to redraw us full if our LyXText wants it
395 if (text_.needRefresh())
398 // this to not draw a selection when we redraw all of it!
399 if (need_update & CURSOR && !(need_update & SELECTION)) {
400 if (text_.selection.set())
402 text_.clearSelection();
407 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
412 if (!autoBreakRows && paragraphs.size() > 1)
413 collapseParagraphs(bv);
415 text_.partialRebreak();
416 setUpdateStatus(what);
417 bool flag = mark_dirty ||
418 ((need_update != CURSOR && need_update != NONE) ||
419 text_.needRefresh() || text_.selection.set());
420 if (!text_.selection.set())
421 text_.selection.cursor = text_.cursor;
427 bv->updateInset(const_cast<InsetText *>(this));
430 if (need_update == CURSOR)
432 bv->owner()->view_state_changed();
433 bv->owner()->updateMenubar();
434 bv->owner()->updateToolbar();
435 if (old_par != cpar()) {
436 bv->owner()->setLayout(cpar()->layout()->name());
442 string const InsetText::editMessage() const
444 return _("Opened Text Inset");
448 void InsetText::insetUnlock(BufferView * bv)
450 if (the_locking_inset) {
451 the_locking_inset->insetUnlock(bv);
452 the_locking_inset = 0;
453 updateLocal(bv, CURSOR_PAR, false);
459 if (text_.selection.set()) {
460 text_.clearSelection();
462 } else if (owner()) {
463 bv->owner()->setLayout(owner()->getLyXText(bv)
464 ->cursor.par()->layout()->name());
466 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
467 // hack for deleteEmptyParMech
468 ParagraphList::iterator first_par = paragraphs.begin();
469 if (!first_par->empty()) {
470 text_.setCursor(first_par, 0);
471 } else if (paragraphs.size() > 1) {
472 text_.setCursor(boost::next(first_par), 0);
475 updateLocal(bv, code, false);
478 setUpdateStatus(code);
483 void InsetText::lockInset(BufferView * bv)
486 the_locking_inset = 0;
487 inset_pos = inset_x = inset_y = 0;
488 inset_boundary = false;
489 inset_par = paragraphs.end();
490 old_par = paragraphs.end();
491 text_.setCursorIntern(paragraphs.begin(), 0);
492 text_.clearSelection();
494 // If the inset is empty set the language of the current font to the
495 // language to the surronding text (if different).
496 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
497 bv->getParentLanguage(this) != text_.current_font.language()) {
498 LyXFont font(LyXFont::ALL_IGNORE);
499 font.setLanguage(bv->getParentLanguage(this));
500 setFont(bv, font, false);
503 if (drawFrame_ == LOCKED)
504 code = CURSOR|DRAW_FRAME;
505 setUpdateStatus(code);
509 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
511 the_locking_inset = inset;
512 inset_x = cix() - top_x + drawTextXOffset;
513 inset_y = ciy() + drawTextYOffset;
516 inset_boundary = cboundary();
517 //updateLocal(bv, CURSOR, false);
521 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
523 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
527 if (!the_locking_inset) {
528 ParagraphList::iterator pit = paragraphs.begin();
529 ParagraphList::iterator pend = paragraphs.end();
531 int const id = inset->id();
532 for (; pit != pend; ++pit) {
533 InsetList::iterator it = pit->insetlist.begin();
534 InsetList::iterator const end = pit->insetlist.end();
535 for (; it != end; ++it) {
536 if (it->inset == inset) {
537 lyxerr << "InsetText::lockInsetInInset: 1 a\n";
538 text_.setCursorIntern(pit, it->pos);
539 lyxerr << "InsetText::lockInsetInInset: 1 b\n";
540 lyxerr << "bv: " << bv << " inset: " << inset << "\n";
541 lockInset(bv, inset);
542 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
545 if (it->inset->getInsetFromID(id)) {
546 lyxerr << "InsetText::lockInsetInInset: 2\n";
547 text_.setCursorIntern(pit, it->pos);
548 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
549 return the_locking_inset->lockInsetInInset(bv, inset);
553 lyxerr << "InsetText::lockInsetInInset: 3\n";
556 if (inset == cpar()->getInset(cpos())) {
557 lyxerr[Debug::INSETS] << "OK" << endl;
558 lockInset(bv, inset);
562 if (the_locking_inset && the_locking_inset == inset) {
563 if (cpar() == inset_par && cpos() == inset_pos) {
564 lyxerr[Debug::INSETS] << "OK" << endl;
565 inset_x = cix() - top_x + drawTextXOffset;
566 inset_y = ciy() + drawTextYOffset;
568 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
570 } else if (the_locking_inset) {
571 lyxerr[Debug::INSETS] << "MAYBE" << endl;
572 return the_locking_inset->lockInsetInInset(bv, inset);
574 lyxerr[Debug::INSETS] << "NOT OK" << endl;
579 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
582 if (!the_locking_inset)
584 if (the_locking_inset == inset) {
585 the_locking_inset->insetUnlock(bv);
586 getLyXText(bv)->updateInset(inset);
587 the_locking_inset = 0;
589 moveRightIntern(bv, true, false);
590 old_par = paragraphs.end(); // force layout setting
594 updateLocal(bv, CURSOR, false);
597 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
601 bool InsetText::updateInsetInInset(BufferView * bv, InsetOld * inset)
603 if (!autoBreakRows && paragraphs.size() > 1)
604 collapseParagraphs(bv);
609 if (inset->owner() != this) {
610 int ustat = CURSOR_PAR;
612 UpdatableInset * tl_inset = the_locking_inset;
614 found = tl_inset->updateInsetInInset(bv, inset);
616 tl_inset = static_cast<UpdatableInset *>(inset);
617 while(tl_inset->owner() && tl_inset->owner() != this)
618 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
619 if (!tl_inset->owner())
621 found = tl_inset->updateInsetInInset(bv, inset);
624 text_.updateInset(tl_inset);
625 setUpdateStatus(ustat);
629 bool found = text_.updateInset(inset);
631 setUpdateStatus(CURSOR_PAR);
632 if (the_locking_inset &&
633 cpar() == inset_par && cpos() == inset_pos)
635 inset_x = cix() - top_x + drawTextXOffset;
636 inset_y = ciy() + drawTextYOffset;
643 void InsetText::lfunMousePress(FuncRequest const & cmd)
647 // use this to check mouse motion for selection!
651 BufferView * bv = cmd.view();
652 FuncRequest cmd1 = cmd;
658 int tmp_x = cmd.x - drawTextXOffset;
659 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
660 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
662 if (the_locking_inset) {
663 if (the_locking_inset == inset) {
664 the_locking_inset->localDispatch(cmd1);
667 // otherwise only unlock the_locking_inset
668 the_locking_inset->insetUnlock(bv);
669 the_locking_inset = 0;
672 no_selection = false;
674 if (bv->theLockingInset()) {
675 if (isHighlyEditableInset(inset)) {
676 // We just have to lock the inset before calling a
678 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
679 if (!bv->lockInset(uinset)) {
680 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
682 inset->localDispatch(cmd1);
683 if (the_locking_inset)
684 updateLocal(bv, CURSOR, false);
689 bool paste_internally = false;
690 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
691 localDispatch(FuncRequest(bv, LFUN_COPY));
692 paste_internally = true;
694 int old_top_y = text_.top_y();
696 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
698 // set the selection cursor!
699 text_.selection.cursor = text_.cursor;
700 text_.cursor.x_fix(text_.cursor.x());
702 if (text_.selection.set()) {
703 text_.clearSelection();
704 updateLocal(bv, FULL, false);
706 text_.clearSelection();
707 updateLocal(bv, CURSOR, false);
710 bv->owner()->setLayout(cpar()->layout()->name());
712 // we moved the view we cannot do mouse selection in this case!
713 if (getLyXText(bv)->top_y() != old_top_y)
716 // Insert primary selection with middle mouse
717 // if there is a local selection in the current buffer,
719 if (cmd.button() == mouse_button::button2) {
720 if (paste_internally)
721 localDispatch(FuncRequest(bv, LFUN_PASTE));
723 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
726 getLyXText(bv)->clearSelection();
731 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
733 BufferView * bv = cmd.view();
734 FuncRequest cmd1 = cmd;
739 if (the_locking_inset)
740 return the_locking_inset->localDispatch(cmd1);
742 int tmp_x = cmd.x - drawTextXOffset;
743 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
744 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
747 // This code should probably be removed now. Simple insets
748 // (!highlyEditable) can actually take the localDispatch,
749 // and turn it into edit() if necessary. But we still
750 // need to deal properly with the whole relative vs.
751 // absolute mouse co-ords thing in a realiable, sensible way
753 if (isHighlyEditableInset(inset))
754 ret = inset->localDispatch(cmd1);
756 inset_x = cix(bv) - top_x + drawTextXOffset;
757 inset_y = ciy() + drawTextYOffset;
758 cmd1.x = cmd.x - inset_x;
759 cmd1.y = cmd.x - inset_y;
760 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
764 ret = inset->localDispatch(cmd1);
765 updateLocal(bv, CURSOR_PAR, false);
772 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
774 FuncRequest cmd1 = cmd;
778 if (the_locking_inset) {
779 the_locking_inset->localDispatch(cmd1);
783 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
786 BufferView * bv = cmd.view();
787 LyXCursor cur = text_.cursor;
788 text_.setCursorFromCoordinates
789 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
790 text_.cursor.x_fix(text_.cursor.x());
791 if (cur == text_.cursor)
793 text_.setSelection();
794 bool flag = (text_.toggle_cursor.par() != text_.toggle_end_cursor.par() ||
795 text_.toggle_cursor.pos() != text_.toggle_end_cursor.pos());
797 updateLocal(bv, SELECTION, false);
802 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
804 BufferView * bv = cmd.view();
807 switch (cmd.action) {
808 case LFUN_INSET_EDIT: {
809 UpdatableInset::localDispatch(cmd);
811 if (!bv->lockInset(this)) {
812 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
817 the_locking_inset = 0;
821 inset_boundary = false;
822 inset_par = paragraphs.end();
823 old_par = paragraphs.end();
826 if (cmd.argument.size()) {
827 if (cmd.argument == "left")
828 text_.setCursorIntern(paragraphs.begin(), 0);
830 ParagraphList::iterator it = boost::prior(paragraphs.end());
831 text_.setCursor(it, it->size());
834 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
835 // we put here -1 and not button as now the button in the
836 // edit call should not be needed we will fix this in 1.3.x
837 // cycle hopefully (Jug 20020509)
838 // FIXME: GUII I've changed this to none: probably WRONG
839 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
840 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
842 text_.cursor.x_fix(text_.cursor.x());
846 text_.clearSelection();
849 // If the inset is empty set the language of the current font to the
850 // language to the surronding text (if different).
851 if (paragraphs.begin()->empty() &&
852 paragraphs.size() == 1 &&
853 bv->getParentLanguage(this) != text_.current_font.language())
855 LyXFont font(LyXFont::ALL_IGNORE);
856 font.setLanguage(bv->getParentLanguage(this));
857 setFont(bv, font, false);
861 if (drawFrame_ == LOCKED)
862 code = CURSOR | DRAW_FRAME;
864 updateLocal(bv, code, false);
865 // Tell the paragraph dialog that we've entered an insettext.
866 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
870 case LFUN_MOUSE_PRESS:
874 case LFUN_MOUSE_MOTION:
875 lfunMouseMotion(cmd);
878 case LFUN_MOUSE_RELEASE:
879 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
885 bool was_empty = (paragraphs.begin()->empty() &&
886 paragraphs.size() == 1);
888 no_selection = false;
889 RESULT result = UpdatableInset::localDispatch(cmd);
890 if (result != UNDISPATCHED)
894 if (cmd.action < 0 && cmd.argument.empty())
897 if (the_locking_inset) {
898 result = the_locking_inset->localDispatch(cmd);
899 if (result == DISPATCHED_NOUPDATE)
901 else if (result == DISPATCHED) {
902 updateLocal(bv, CURSOR_PAR, false);
904 } else if (result >= FINISHED) {
907 moveRightIntern(bv, false, false);
911 if ((result = moveUp(bv)) >= FINISHED) {
912 updateLocal(bv, CURSOR, false);
913 bv->unlockInset(this);
917 if ((result = moveDown(bv)) >= FINISHED) {
918 updateLocal(bv, CURSOR, false);
919 bv->unlockInset(this);
926 the_locking_inset = 0;
927 updateLocal(bv, CURSOR, false);
928 // make sure status gets reset immediately
929 bv->owner()->clearMessage();
936 // what type of update to do on a cursor movement
937 int cursor_update = CURSOR;
939 if (text_.selection.set())
940 cursor_update = SELECTION;
942 switch (cmd.action) {
945 case LFUN_SELFINSERT:
946 if (bv->buffer()->isReadonly()) {
947 // setErrorMessage(N_("Document is read only"));
950 if (!cmd.argument.empty()) {
951 /* Automatically delete the currently selected
952 * text and replace it with what is being
953 * typed in now. Depends on lyxrc settings
954 * "auto_region_delete", which defaults to
957 // This should not be needed here and is also WRONG!
958 recordUndo(bv, Undo::INSERT, text_.cursor.par());
961 if (lyxrc.auto_region_delete) {
962 if (text_.selection.set()) {
963 text_.cutSelection(false, false);
966 text_.clearSelection();
967 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
968 bv->owner()->getIntl().getTransManager().
969 TranslateAndInsert(cmd.argument[i], &text_);
972 text_.selection.cursor = text_.cursor;
973 updwhat = CURSOR | CURSOR_PAR;
975 result = DISPATCHED_NOUPDATE;
978 // cursor movements that need special handling
981 result = moveRight(bv);
983 updwhat = cursor_update;
987 result = moveLeft(bv);
988 updwhat = cursor_update;
992 result = moveDown(bv);
993 updwhat = cursor_update;
998 updwhat = cursor_update;
1002 if (crow() == text_.rows().begin())
1003 result = FINISHED_UP;
1005 text_.cursorPrevious();
1006 text_.clearSelection();
1007 result = DISPATCHED_NOUPDATE;
1009 updwhat = cursor_update;
1013 if (boost::next(crow()) == text_.rows().end())
1014 result = FINISHED_DOWN;
1017 text_.clearSelection();
1018 result = DISPATCHED_NOUPDATE;
1020 updwhat = cursor_update;
1023 case LFUN_BACKSPACE: {
1024 if (text_.selection.set())
1025 text_.cutSelection(true, false);
1028 updwhat = CURSOR_PAR;
1034 if (text_.selection.set()) {
1035 text_.cutSelection(true, false);
1039 updwhat = CURSOR_PAR;
1045 text_.cutSelection(true, true);
1046 updwhat = CURSOR_PAR;
1053 text_.copySelection();
1054 updwhat = CURSOR_PAR;
1057 case LFUN_PASTESELECTION:
1059 string const clip(bv->getClipboard());
1063 if (cmd.argument == "paragraph") {
1064 text_.insertStringAsParagraphs(clip);
1066 text_.insertStringAsLines(clip);
1069 text_.clearSelection();
1071 updwhat = CURSOR_PAR;
1077 if (!autoBreakRows) {
1078 if (CutAndPaste::nrOfParagraphs() > 1) {
1079 #ifdef WITH_WARNINGS
1080 #warning FIXME horrendously bad UI
1082 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1087 replaceSelection(bv->getLyXText());
1088 size_t sel_index = 0;
1089 string const & arg = cmd.argument;
1090 if (isStrUnsignedInt(arg)) {
1091 size_t const paste_arg = strToUnsignedInt(arg);
1092 #warning FIXME Check if the arg is in the domain of available selections.
1093 sel_index = paste_arg;
1095 text_.pasteSelection(sel_index);
1097 text_.clearSelection();
1098 updwhat = CURSOR_PAR;
1103 case LFUN_BREAKPARAGRAPH:
1104 if (!autoBreakRows) {
1105 result = DISPATCHED;
1108 replaceSelection(bv->getLyXText());
1109 text_.breakParagraph(paragraphs, 0);
1110 updwhat = CURSOR | FULL;
1114 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1115 if (!autoBreakRows) {
1116 result = DISPATCHED;
1119 replaceSelection(bv->getLyXText());
1120 text_.breakParagraph(paragraphs, 1);
1121 updwhat = CURSOR | FULL;
1125 case LFUN_BREAKLINE: {
1126 if (!autoBreakRows) {
1127 result = DISPATCHED;
1131 replaceSelection(bv->getLyXText());
1132 text_.insertInset(new InsetNewline);
1133 updwhat = CURSOR | CURSOR_PAR;
1139 // do not set layouts on non breakable textinsets
1140 if (autoBreakRows) {
1141 string cur_layout = cpar()->layout()->name();
1143 // Derive layout number from given argument (string)
1144 // and current buffer's textclass (number). */
1145 LyXTextClass const & tclass =
1146 bv->buffer()->params.getLyXTextClass();
1147 string layout = cmd.argument;
1148 bool hasLayout = tclass.hasLayout(layout);
1150 // If the entry is obsolete, use the new one instead.
1152 string const & obs =
1153 tclass[layout]->obsoleted_by();
1158 // see if we found the layout number:
1160 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1161 bv->owner()->dispatch(lf);
1165 if (cur_layout != layout) {
1166 cur_layout = layout;
1167 text_.setLayout(layout);
1168 bv->owner()->setLayout(cpar()->layout()->name());
1169 updwhat = CURSOR_PAR;
1173 // reset the layout box
1174 bv->owner()->setLayout(cpar()->layout()->name());
1177 case LFUN_PARAGRAPH_SPACING:
1178 // This one is absolutely not working. When fiddling with this
1179 // it also seems to me that the paragraphs inside the insettext
1180 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1181 // FIXME: how old is this comment ? ...
1183 ParagraphList::iterator pit = text_.cursor.par();
1184 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1185 float cur_value = 1.0;
1186 if (cur_spacing == Spacing::Other) {
1187 cur_value = pit->params().spacing().getValue();
1190 istringstream istr(STRCONV(cmd.argument));
1193 Spacing::Space new_spacing = cur_spacing;
1194 float new_value = cur_value;
1196 lyxerr << "Missing argument to `paragraph-spacing'"
1198 } else if (tmp == "single") {
1199 new_spacing = Spacing::Single;
1200 } else if (tmp == "onehalf") {
1201 new_spacing = Spacing::Onehalf;
1202 } else if (tmp == "double") {
1203 new_spacing = Spacing::Double;
1204 } else if (tmp == "other") {
1205 new_spacing = Spacing::Other;
1208 lyxerr << "new_value = " << tmpval << endl;
1211 } else if (tmp == "default") {
1212 new_spacing = Spacing::Default;
1214 lyxerr << _("Unknown spacing argument: ")
1215 << cmd.argument << endl;
1217 if (cur_spacing != new_spacing || cur_value != new_value) {
1218 pit->params().spacing(Spacing(new_spacing, new_value));
1219 updwhat = CURSOR_PAR;
1225 // These need to do repaints but don't require
1226 // special handling otherwise. A *lot* of the
1227 // above could probably be done similarly ...
1232 case LFUN_WORDRIGHT:
1233 // these two are really unhandled ...
1235 case LFUN_BEGINNINGBUF:
1236 updwhat = cursor_update;
1237 if (!bv->dispatch(cmd))
1238 result = UNDISPATCHED;
1247 case LFUN_WORDLEFTSEL:
1248 case LFUN_WORDRIGHTSEL:
1249 updwhat = SELECTION;
1254 if (!bv->dispatch(cmd))
1255 result = UNDISPATCHED;
1260 updateLocal(bv, updwhat, updflag);
1261 /// If the action has deleted all text in the inset, we need to change the
1262 // language to the language of the surronding text.
1263 if (!was_empty && paragraphs.begin()->empty() &&
1264 paragraphs.size() == 1) {
1265 LyXFont font(LyXFont::ALL_IGNORE);
1266 font.setLanguage(bv->getParentLanguage(this));
1267 setFont(bv, font, false);
1270 if (result >= FINISHED)
1271 bv->unlockInset(this);
1273 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1274 result = DISPATCHED;
1279 int InsetText::latex(Buffer const * buf, ostream & os,
1280 LatexRunParams const & runparams) const
1283 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1284 return texrow.rows();
1288 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1290 unsigned int lines = 0;
1292 ParagraphList::const_iterator beg = paragraphs.begin();
1293 ParagraphList::const_iterator end = paragraphs.end();
1294 ParagraphList::const_iterator it = beg;
1295 for (; it != end; ++it) {
1296 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1297 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1304 int InsetText::linuxdoc(Buffer const * buf, ostream & os) const
1306 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1307 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1309 // There is a confusion between the empty paragraph and the default paragraph
1310 // The default paragraph is <p></p>, the empty paragraph is *empty*
1311 // Since none of the floats of linuxdoc accepts standard paragraphs
1312 // I disable them. I don't expect problems. (jamatos 2003/07/27)
1313 for (; pit != pend; ++pit) {
1314 const string name = pit->layout()->latexname();
1316 sgml::openTag(os, 1, 0, name);
1317 buf->simpleLinuxDocOnePar(os, pit, 0);
1319 sgml::closeTag(os, 1, 0, name);
1325 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1327 unsigned int lines = 0;
1329 vector<string> environment_stack(10);
1330 vector<string> environment_inner(10);
1332 int const command_depth = 0;
1335 Paragraph::depth_type depth = 0; // paragraph depth
1337 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1338 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1340 for (; pit != pend; ++pit) {
1342 int desc_on = 0; // description mode
1344 LyXLayout_ptr const & style = pit->layout();
1346 // environment tag closing
1347 for (; depth > pit->params().depth(); --depth) {
1348 if (environment_inner[depth] != "!-- --") {
1349 item_name = "listitem";
1350 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1351 if (environment_inner[depth] == "varlistentry")
1352 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1354 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1355 environment_stack[depth].erase();
1356 environment_inner[depth].erase();
1359 if (depth == pit->params().depth()
1360 && environment_stack[depth] != style->latexname()
1361 && !environment_stack[depth].empty()) {
1362 if (environment_inner[depth] != "!-- --") {
1363 item_name= "listitem";
1364 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1365 if (environment_inner[depth] == "varlistentry")
1366 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1369 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1371 environment_stack[depth].erase();
1372 environment_inner[depth].erase();
1375 // Write opening SGML tags.
1376 switch (style->latextype) {
1377 case LATEX_PARAGRAPH:
1378 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1382 buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1386 case LATEX_ENVIRONMENT:
1387 case LATEX_ITEM_ENVIRONMENT:
1388 if (depth < pit->params().depth()) {
1389 depth = pit->params().depth();
1390 environment_stack[depth].erase();
1393 if (environment_stack[depth] != style->latexname()) {
1394 if (environment_stack.size() == depth + 1) {
1395 environment_stack.push_back("!-- --");
1396 environment_inner.push_back("!-- --");
1398 environment_stack[depth] = style->latexname();
1399 environment_inner[depth] = "!-- --";
1400 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1402 if (environment_inner[depth] != "!-- --") {
1403 item_name= "listitem";
1404 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1405 if (environment_inner[depth] == "varlistentry")
1406 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1410 if (style->latextype == LATEX_ENVIRONMENT) {
1411 if (!style->latexparam().empty()) {
1412 if (style->latexparam() == "CDATA")
1415 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1420 desc_on = (style->labeltype == LABEL_MANUAL);
1422 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1423 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1425 item_name = desc_on ? "term" : "para";
1426 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1430 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1434 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1437 // write closing SGML tags
1438 switch (style->latextype) {
1439 case LATEX_ENVIRONMENT:
1440 if (!style->latexparam().empty()) {
1441 if (style->latexparam() == "CDATA")
1444 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1447 case LATEX_ITEM_ENVIRONMENT:
1448 if (desc_on == 1) break;
1450 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1452 case LATEX_PARAGRAPH:
1453 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1456 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1462 for (int d = depth; d >= 0; --d) {
1463 if (!environment_stack[depth].empty()) {
1464 if (environment_inner[depth] != "!-- --") {
1465 item_name = "listitem";
1466 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1467 if (environment_inner[depth] == "varlistentry")
1468 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1471 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1479 void InsetText::validate(LaTeXFeatures & features) const
1481 for_each(paragraphs.begin(), paragraphs.end(),
1482 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1486 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1488 if (the_locking_inset) {
1489 the_locking_inset->getCursor(bv, x, y);
1493 y = cy() + InsetText::y();
1497 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1499 if (the_locking_inset) {
1500 the_locking_inset->getCursorPos(bv, x, y);
1503 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1504 y = cy() - TEXT_TO_INSET_OFFSET;
1508 int InsetText::insetInInsetY() const
1510 if (!the_locking_inset)
1513 return inset_y + the_locking_inset->insetInInsetY();
1517 void InsetText::fitInsetCursor(BufferView * bv) const
1519 if (the_locking_inset) {
1520 the_locking_inset->fitInsetCursor(bv);
1524 LyXFont const font = text_.getFont(cpar(), cpos());
1526 int const asc = font_metrics::maxAscent(font);
1527 int const desc = font_metrics::maxDescent(font);
1529 if (bv->fitLockedInsetCursor(cx(), cy(), asc, desc))
1530 need_update |= FULL;
1535 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1537 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1538 return moveLeftIntern(bv, false, activate_inset, selecting);
1540 return moveRightIntern(bv, true, activate_inset, selecting);
1545 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1547 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1548 return moveRightIntern(bv, true, activate_inset, selecting);
1550 return moveLeftIntern(bv, false, activate_inset, selecting);
1555 InsetText::moveRightIntern(BufferView * bv, bool front,
1556 bool activate_inset, bool selecting)
1558 ParagraphList::iterator c_par = cpar();
1560 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1561 return FINISHED_RIGHT;
1562 if (activate_inset && checkAndActivateInset(bv, front))
1564 text_.cursorRight(bv);
1566 text_.clearSelection();
1567 return DISPATCHED_NOUPDATE;
1572 InsetText::moveLeftIntern(BufferView * bv, bool front,
1573 bool activate_inset, bool selecting)
1575 if (cpar() == paragraphs.begin() && cpos() <= 0)
1577 text_.cursorLeft(bv);
1579 text_.clearSelection();
1580 if (activate_inset && checkAndActivateInset(bv, front))
1582 return DISPATCHED_NOUPDATE;
1586 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1588 if (crow() == text_.rows().begin())
1591 text_.clearSelection();
1592 return DISPATCHED_NOUPDATE;
1596 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1598 if (boost::next(crow()) == text_.rows().end())
1599 return FINISHED_DOWN;
1600 text_.cursorDown(bv);
1601 text_.clearSelection();
1602 return DISPATCHED_NOUPDATE;
1606 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1608 if (the_locking_inset) {
1609 if (the_locking_inset->insetAllowed(inset))
1610 return the_locking_inset->insertInset(bv, inset);
1613 inset->setOwner(this);
1614 text_.insertInset(inset);
1616 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1621 bool InsetText::insetAllowed(InsetOld::Code code) const
1623 // in_insetAllowed is a really gross hack,
1624 // to allow us to call the owner's insetAllowed
1625 // without stack overflow, which can happen
1626 // when the owner uses InsetCollapsable::insetAllowed()
1628 if (in_insetAllowed)
1630 in_insetAllowed = true;
1631 if (the_locking_inset)
1632 ret = the_locking_inset->insetAllowed(code);
1634 ret = owner()->insetAllowed(code);
1635 in_insetAllowed = false;
1640 UpdatableInset * InsetText::getLockingInset() const
1642 return the_locking_inset ? the_locking_inset->getLockingInset() :
1643 const_cast<InsetText *>(this);
1647 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1651 if (the_locking_inset)
1652 return the_locking_inset->getFirstLockingInsetOfType(c);
1657 bool InsetText::showInsetDialog(BufferView * bv) const
1659 if (the_locking_inset)
1660 return the_locking_inset->showInsetDialog(bv);
1665 void InsetText::getLabelList(std::vector<string> & list) const
1667 ParagraphList::const_iterator pit = paragraphs.begin();
1668 ParagraphList::const_iterator pend = paragraphs.end();
1669 for (; pit != pend; ++pit) {
1670 InsetList::const_iterator beg = pit->insetlist.begin();
1671 InsetList::const_iterator end = pit->insetlist.end();
1672 for (; beg != end; ++beg)
1673 beg->inset->getLabelList(list);
1678 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1681 if (the_locking_inset) {
1682 the_locking_inset->setFont(bv, font, toggleall, selectall);
1686 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1687 || cpar()->empty()) {
1688 text_.setFont(font, toggleall);
1693 if (text_.selection.set())
1694 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1698 text_.selection.cursor = text_.cursor;
1699 text_.cursorBottom();
1700 text_.setSelection();
1703 text_.toggleFree(font, toggleall);
1706 text_.clearSelection();
1710 bool flag = (selectall || text_.selection.set());
1713 updateLocal(bv, FULL, true);
1715 updateLocal(bv, CURSOR_PAR, true);
1719 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1721 InsetOld * inset = cpar()->getInset(cpos());
1722 if (!isHighlyEditableInset(inset))
1724 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1725 inset->localDispatch(cmd);
1726 if (!the_locking_inset)
1728 updateLocal(bv, CURSOR, false);
1733 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1734 mouse_button::state button)
1736 x -= drawTextXOffset;
1738 int dummyy = y + dim_.asc;
1739 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1740 // we only do the edit() call if the inset was hit by the mouse
1741 // or if it is a highly editable inset. So we should call this
1742 // function from our own edit with button < 0.
1743 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1745 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1754 inset_x = cix() - top_x + drawTextXOffset;
1755 inset_y = ciy() + drawTextYOffset;
1756 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1757 inset->localDispatch(cmd);
1758 if (!the_locking_inset)
1760 updateLocal(bv, CURSOR, false);
1765 void InsetText::setParagraphData(ParagraphList const & plist)
1767 // we have to unlock any locked inset otherwise we're in troubles
1768 the_locking_inset = 0;
1770 // But it it makes no difference that is a lot better.
1772 // See if this can be simplified when std::list is in effect.
1775 ParagraphList::const_iterator it = plist.begin();
1776 ParagraphList::const_iterator end = plist.end();
1777 for (; it != end; ++it) {
1778 paragraphs.push_back(*it);
1779 paragraphs.back().setInsetOwner(this);
1787 void InsetText::markNew(bool track_changes)
1789 ParagraphList::iterator pit = paragraphs.begin();
1790 ParagraphList::iterator pend = paragraphs.end();
1791 for (; pit != pend; ++pit) {
1792 if (track_changes) {
1793 pit->trackChanges();
1795 // no-op when not tracking
1796 pit->cleanChanges();
1802 void InsetText::setText(string const & data, LyXFont const & font)
1805 for (unsigned int i = 0; i < data.length(); ++i)
1806 paragraphs.begin()->insertChar(i, data[i], font);
1811 void InsetText::setAutoBreakRows(bool flag)
1813 if (flag != autoBreakRows) {
1814 autoBreakRows = flag;
1822 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1824 if (how != drawFrame_) {
1827 updateLocal(bv, DRAW_FRAME, false);
1832 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1834 if (frame_color != col) {
1837 updateLocal(bv, DRAW_FRAME, false);
1842 int InsetText::cx() const
1844 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1845 if (the_locking_inset) {
1846 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1847 if (font.isVisibleRightToLeft())
1848 x -= the_locking_inset->width();
1854 int InsetText::cix() const
1856 int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1857 if (the_locking_inset) {
1858 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1859 if (font.isVisibleRightToLeft())
1860 x -= the_locking_inset->width();
1866 int InsetText::cy() const
1868 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1872 int InsetText::ciy() const
1874 return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1878 pos_type InsetText::cpos() const
1880 return text_.cursor.pos();
1884 ParagraphList::iterator InsetText::cpar() const
1886 return text_.cursor.par();
1890 bool InsetText::cboundary() const
1892 return text_.cursor.boundary();
1896 RowList::iterator InsetText::crow() const
1898 return text_.cursorRow();
1902 LyXText * InsetText::getLyXText(BufferView const * bv,
1903 bool const recursive) const
1906 if (recursive && the_locking_inset)
1907 return the_locking_inset->getLyXText(bv, true);
1912 void InsetText::setViewCache(BufferView const * bv) const
1915 text_.bv_owner = const_cast<BufferView *>(bv);
1919 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1922 /// then remove all LyXText in text-insets
1923 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1924 const_cast<ParagraphList&>(paragraphs).end(),
1925 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1930 void InsetText::resizeLyXText(BufferView * bv, bool /*force*/) const
1932 if (paragraphs.size() == 1 && paragraphs.begin()->empty()) {
1933 // no data, resize not neccessary!
1934 // we have to do this as a fixed width may have changed!
1937 restoreLyXTextState();
1949 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1950 const_cast<ParagraphList&>(paragraphs).end(),
1951 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1954 restoreLyXTextState();
1956 // seems to be unneeded
1958 if (the_locking_inset) {
1959 inset_x = cix() - top_x + drawTextXOffset;
1960 inset_y = ciy() + drawTextYOffset;
1963 text_.top_y(bv->screen().topCursorVisible(&text_));
1965 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
1966 // this will scroll the screen such that the cursor becomes visible
1967 bv->updateScrollbar();
1969 need_update |= FULL;
1975 void InsetText::reinitLyXText() const
1977 BufferView * bv = text_.bv_owner;
1984 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1985 const_cast<ParagraphList&>(paragraphs).end(),
1986 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1989 restoreLyXTextState();
1990 if (the_locking_inset) {
1991 inset_x = cix() - top_x + drawTextXOffset;
1992 inset_y = ciy() + drawTextYOffset;
1994 text_.top_y(bv->screen().topCursorVisible(&text_));
1996 // this will scroll the screen such that the cursor becomes visible
1997 bv->updateScrollbar();
2004 void InsetText::removeNewlines()
2006 bool changed = false;
2008 ParagraphList::iterator it = paragraphs.begin();
2009 ParagraphList::iterator end = paragraphs.end();
2010 for (; it != end; ++it) {
2011 for (int i = 0; i < it->size(); ++i) {
2012 if (it->isNewline(i)) {
2023 bool InsetText::nodraw() const
2025 if (the_locking_inset)
2026 return the_locking_inset->nodraw();
2027 return UpdatableInset::nodraw();
2031 int InsetText::scroll(bool recursive) const
2033 int sx = UpdatableInset::scroll(false);
2035 if (recursive && the_locking_inset)
2036 sx += the_locking_inset->scroll(recursive);
2042 void InsetText::clearSelection(BufferView * bv)
2044 getLyXText(bv)->clearSelection();
2048 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2050 Painter & pain = bv->painter();
2052 int h = dim_.asc + dim_.des;
2053 int ty = baseline - dim_.asc;
2059 if ((ty + h) > pain.paperHeight())
2060 h = pain.paperHeight();
2061 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2062 w = pain.paperWidth();
2063 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2068 ParagraphList * InsetText::getParagraphs(int i) const
2070 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2074 LyXCursor const & InsetText::cursor(BufferView * bv) const
2076 if (the_locking_inset)
2077 return the_locking_inset->cursor(bv);
2078 return getLyXText(bv)->cursor;
2082 InsetOld * InsetText::getInsetFromID(int id_arg) const
2085 return const_cast<InsetText *>(this);
2087 ParagraphList::const_iterator pit = paragraphs.begin();
2088 ParagraphList::const_iterator pend = paragraphs.end();
2089 for (; pit != pend; ++pit) {
2090 InsetList::const_iterator it = pit->insetlist.begin();
2091 InsetList::const_iterator end = pit->insetlist.end();
2092 for (; it != end; ++it) {
2093 if (it->inset->id() == id_arg)
2095 InsetOld * in = it->inset->getInsetFromID(id_arg);
2105 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2108 if (the_locking_inset) {
2109 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2110 if (!word.word().empty()) {
2114 // we have to go on checking so move cursor to the next char
2115 text_.cursor.pos(text_.cursor.pos() + 1);
2117 word = text_.selectNextWordToSpellcheck(value);
2118 if (word.word().empty())
2119 bv->unlockInset(const_cast<InsetText *>(this));
2126 void InsetText::selectSelectedWord(BufferView * bv)
2128 if (the_locking_inset) {
2129 the_locking_inset->selectSelectedWord(bv);
2132 getLyXText(bv)->selectSelectedWord();
2133 updateLocal(bv, SELECTION, false);
2137 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2139 if (the_locking_inset)
2140 the_locking_inset->toggleSelection(bv, kill_selection);
2142 int x = top_x + TEXT_TO_INSET_OFFSET;
2144 RowList::iterator rit = text_.rows().begin();
2145 RowList::iterator end = text_.rows().end();
2146 int y_offset = top_baseline - rit->ascent_of_text();
2148 while (rit != end && y + rit->height() <= 0) {
2155 if (need_update & SELECTION)
2157 bv->screen().toggleSelection(&text_, bv, kill_selection, y_offset, x);
2161 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2163 if (the_locking_inset) {
2164 if (the_locking_inset->nextChange(bv, length))
2166 text_.cursorRight(true);
2168 lyx::find::SearchResult result =
2169 lyx::find::findNextChange(bv, &text_, length);
2171 if (result == lyx::find::SR_FOUND) {
2172 LyXCursor cur = text_.cursor;
2173 bv->unlockInset(bv->theLockingInset());
2174 if (bv->lockInset(this))
2177 text_.setSelectionRange(length);
2178 updateLocal(bv, SELECTION, false);
2180 return result != lyx::find::SR_NOT_FOUND;
2184 bool InsetText::searchForward(BufferView * bv, string const & str,
2187 if (the_locking_inset) {
2188 if (the_locking_inset->searchForward(bv, str, cs, mw))
2190 text_.cursorRight(true);
2192 lyx::find::SearchResult result =
2193 lyx::find::find(bv, &text_, str, true, cs, mw);
2195 if (result == lyx::find::SR_FOUND) {
2196 LyXCursor cur = text_.cursor;
2197 bv->unlockInset(bv->theLockingInset());
2198 if (bv->lockInset(this))
2201 text_.setSelectionRange(str.length());
2202 updateLocal(bv, SELECTION, false);
2204 return result != lyx::find::SR_NOT_FOUND;
2208 bool InsetText::searchBackward(BufferView * bv, string const & str,
2211 if (the_locking_inset) {
2212 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2216 ParagraphList::iterator pit = paragraphs.begin();
2217 ParagraphList::iterator pend = paragraphs.end();
2219 while (boost::next(pit) != pend)
2222 text_.setCursor(pit, pit->size());
2224 lyx::find::SearchResult result =
2225 lyx::find::find(bv, &text_, str, false, cs, mw);
2227 if (result == lyx::find::SR_FOUND) {
2228 LyXCursor cur = text_.cursor;
2229 bv->unlockInset(bv->theLockingInset());
2230 if (bv->lockInset(this))
2233 text_.setSelectionRange(str.length());
2234 updateLocal(bv, SELECTION, false);
2236 return result != lyx::find::SR_NOT_FOUND;
2240 bool InsetText::checkInsertChar(LyXFont & font)
2243 return owner()->checkInsertChar(font);
2248 void InsetText::collapseParagraphs(BufferView * bv)
2250 while (paragraphs.size() > 1) {
2251 ParagraphList::iterator first_par = paragraphs.begin();
2252 ParagraphList::iterator next_par = boost::next(first_par);
2253 size_t const first_par_size = first_par->size();
2255 if (!first_par->empty() &&
2256 !next_par->empty() &&
2257 !first_par->isSeparator(first_par_size - 1)) {
2258 first_par->insertChar(first_par_size, ' ');
2261 if (text_.selection.set()) {
2262 if (text_.selection.start.par() == next_par) {
2263 text_.selection.start.par(first_par);
2264 text_.selection.start.pos(
2265 text_.selection.start.pos() + first_par_size);
2267 if (text_.selection.end.par() == next_par) {
2268 text_.selection.end.par(first_par);
2269 text_.selection.end.pos(
2270 text_.selection.end.pos() + first_par_size);
2274 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2280 void InsetText::getDrawFont(LyXFont & font) const
2284 owner()->getDrawFont(font);
2288 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2290 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2291 // And it probably does. You have to take a look at this John. (Lgb)
2292 #warning John, have a look here. (Lgb)
2293 ParagraphList::iterator pit = plist.begin();
2294 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2296 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2298 ParagraphList::iterator pend = plist.end();
2299 for (; pit != pend; ++pit) {
2300 paragraphs.push_back(*pit);
2307 void InsetText::addPreview(PreviewLoader & loader) const
2309 ParagraphList::const_iterator pit = paragraphs.begin();
2310 ParagraphList::const_iterator pend = paragraphs.end();
2312 for (; pit != pend; ++pit) {
2313 InsetList::const_iterator it = pit->insetlist.begin();
2314 InsetList::const_iterator end = pit->insetlist.end();
2315 for (; it != end; ++it) {
2316 it->inset->addPreview(loader);