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 yf = y_offset + first;
368 while (rit != end && yf < y2) {
369 paintRows(*bv, text_, rit,
370 y + y_offset + first, x, y + text_.top_y());
378 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked)) {
379 drawFrame(pain, int(start_x));
382 if (need_update != INIT) {
388 void InsetText::drawFrame(Painter & pain, int x) const
390 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
392 frame_y = top_baseline - dim_.asc + ttoD2;
393 frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
394 frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
395 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
400 void InsetText::setUpdateStatus(int what) const
403 // we will to redraw us full if our LyXText wants it
404 if (text_.needRefresh())
407 // this to not draw a selection when we redraw all of it!
408 if (need_update & CURSOR && !(need_update & SELECTION)) {
409 if (text_.selection.set())
411 text_.clearSelection();
416 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
421 if (!autoBreakRows && paragraphs.size() > 1)
422 collapseParagraphs(bv);
424 text_.partialRebreak();
425 setUpdateStatus(what);
426 bool flag = mark_dirty ||
427 ((need_update != CURSOR && need_update != NONE) ||
428 text_.needRefresh() || text_.selection.set());
429 if (!text_.selection.set())
430 text_.selection.cursor = text_.cursor;
436 bv->updateInset(const_cast<InsetText *>(this));
439 if (need_update == CURSOR)
441 bv->owner()->view_state_changed();
442 bv->owner()->updateMenubar();
443 bv->owner()->updateToolbar();
444 if (old_par != cpar()) {
445 bv->owner()->setLayout(cpar()->layout()->name());
451 string const InsetText::editMessage() const
453 return _("Opened Text Inset");
457 void InsetText::insetUnlock(BufferView * bv)
459 if (the_locking_inset) {
460 the_locking_inset->insetUnlock(bv);
461 the_locking_inset = 0;
462 updateLocal(bv, CURSOR_PAR, false);
468 if (text_.selection.set()) {
469 text_.clearSelection();
471 } else if (owner()) {
472 bv->owner()->setLayout(owner()->getLyXText(bv)
473 ->cursor.par()->layout()->name());
475 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
476 // hack for deleteEmptyParMech
477 ParagraphList::iterator first_par = paragraphs.begin();
478 if (!first_par->empty()) {
479 text_.setCursor(first_par, 0);
480 } else if (paragraphs.size() > 1) {
481 text_.setCursor(boost::next(first_par), 0);
484 updateLocal(bv, code, false);
487 setUpdateStatus(code);
492 void InsetText::lockInset(BufferView * bv)
495 the_locking_inset = 0;
496 inset_pos = inset_x = inset_y = 0;
497 inset_boundary = false;
498 inset_par = paragraphs.end();
499 old_par = paragraphs.end();
500 text_.setCursorIntern(paragraphs.begin(), 0);
501 text_.clearSelection();
503 // If the inset is empty set the language of the current font to the
504 // language to the surronding text (if different).
505 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
506 bv->getParentLanguage(this) != text_.current_font.language()) {
507 LyXFont font(LyXFont::ALL_IGNORE);
508 font.setLanguage(bv->getParentLanguage(this));
509 setFont(bv, font, false);
512 if (drawFrame_ == LOCKED)
513 code = CURSOR|DRAW_FRAME;
514 setUpdateStatus(code);
518 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
520 the_locking_inset = inset;
521 inset_x = cix() - top_x + drawTextXOffset;
522 inset_y = ciy() + drawTextYOffset;
525 inset_boundary = cboundary();
526 //updateLocal(bv, CURSOR, false);
530 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
532 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
536 if (!the_locking_inset) {
537 ParagraphList::iterator pit = paragraphs.begin();
538 ParagraphList::iterator pend = paragraphs.end();
540 int const id = inset->id();
541 for (; pit != pend; ++pit) {
542 InsetList::iterator it = pit->insetlist.begin();
543 InsetList::iterator const end = pit->insetlist.end();
544 for (; it != end; ++it) {
545 if (it->inset == inset) {
546 lyxerr << "InsetText::lockInsetInInset: 1 a\n";
547 text_.setCursorIntern(pit, it->pos);
548 lyxerr << "InsetText::lockInsetInInset: 1 b\n";
549 lyxerr << "bv: " << bv << " inset: " << inset << "\n";
550 lockInset(bv, inset);
551 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
554 if (it->inset->getInsetFromID(id)) {
555 lyxerr << "InsetText::lockInsetInInset: 2\n";
556 text_.setCursorIntern(pit, it->pos);
557 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
558 return the_locking_inset->lockInsetInInset(bv, inset);
562 lyxerr << "InsetText::lockInsetInInset: 3\n";
565 if (inset == cpar()->getInset(cpos())) {
566 lyxerr[Debug::INSETS] << "OK" << endl;
567 lockInset(bv, inset);
571 if (the_locking_inset && the_locking_inset == inset) {
572 if (cpar() == inset_par && cpos() == inset_pos) {
573 lyxerr[Debug::INSETS] << "OK" << endl;
574 inset_x = cix() - top_x + drawTextXOffset;
575 inset_y = ciy() + drawTextYOffset;
577 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
579 } else if (the_locking_inset) {
580 lyxerr[Debug::INSETS] << "MAYBE" << endl;
581 return the_locking_inset->lockInsetInInset(bv, inset);
583 lyxerr[Debug::INSETS] << "NOT OK" << endl;
588 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
591 if (!the_locking_inset)
593 if (the_locking_inset == inset) {
594 the_locking_inset->insetUnlock(bv);
595 getLyXText(bv)->updateInset(inset);
596 the_locking_inset = 0;
598 moveRightIntern(bv, true, false);
599 old_par = paragraphs.end(); // force layout setting
603 updateLocal(bv, CURSOR, false);
606 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
610 bool InsetText::updateInsetInInset(BufferView * bv, InsetOld * inset)
612 if (!autoBreakRows && paragraphs.size() > 1)
613 collapseParagraphs(bv);
618 if (inset->owner() != this) {
619 int ustat = CURSOR_PAR;
621 UpdatableInset * tl_inset = the_locking_inset;
623 found = tl_inset->updateInsetInInset(bv, inset);
625 tl_inset = static_cast<UpdatableInset *>(inset);
626 while(tl_inset->owner() && tl_inset->owner() != this)
627 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
628 if (!tl_inset->owner())
630 found = tl_inset->updateInsetInInset(bv, inset);
633 text_.updateInset(tl_inset);
634 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() - 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 InsetOld * 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 InsetOld * 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 InsetOld::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;
830 inset_boundary = false;
831 inset_par = paragraphs.end();
832 old_par = paragraphs.end();
835 if (cmd.argument.size()) {
836 if (cmd.argument == "left")
837 text_.setCursorIntern(paragraphs.begin(), 0);
839 ParagraphList::iterator it = boost::prior(paragraphs.end());
840 text_.setCursor(it, it->size());
843 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
844 // we put here -1 and not button as now the button in the
845 // edit call should not be needed we will fix this in 1.3.x
846 // cycle hopefully (Jug 20020509)
847 // FIXME: GUII I've changed this to none: probably WRONG
848 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
849 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
851 text_.cursor.x_fix(text_.cursor.x());
855 text_.clearSelection();
858 // If the inset is empty set the language of the current font to the
859 // language to the surronding text (if different).
860 if (paragraphs.begin()->empty() &&
861 paragraphs.size() == 1 &&
862 bv->getParentLanguage(this) != text_.current_font.language())
864 LyXFont font(LyXFont::ALL_IGNORE);
865 font.setLanguage(bv->getParentLanguage(this));
866 setFont(bv, font, false);
870 if (drawFrame_ == LOCKED)
871 code = CURSOR | DRAW_FRAME;
873 updateLocal(bv, code, false);
874 // Tell the paragraph dialog that we've entered an insettext.
875 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
879 case LFUN_MOUSE_PRESS:
883 case LFUN_MOUSE_MOTION:
884 lfunMouseMotion(cmd);
887 case LFUN_MOUSE_RELEASE:
888 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
894 bool was_empty = (paragraphs.begin()->empty() &&
895 paragraphs.size() == 1);
897 no_selection = false;
898 RESULT result = UpdatableInset::localDispatch(cmd);
899 if (result != UNDISPATCHED)
903 if (cmd.action < 0 && cmd.argument.empty())
906 if (the_locking_inset) {
907 result = the_locking_inset->localDispatch(cmd);
908 if (result == DISPATCHED_NOUPDATE)
910 else if (result == DISPATCHED) {
911 updateLocal(bv, CURSOR_PAR, false);
913 } else if (result >= FINISHED) {
916 moveRightIntern(bv, false, false);
920 if ((result = moveUp(bv)) >= FINISHED) {
921 updateLocal(bv, CURSOR, false);
922 bv->unlockInset(this);
926 if ((result = moveDown(bv)) >= FINISHED) {
927 updateLocal(bv, CURSOR, false);
928 bv->unlockInset(this);
935 the_locking_inset = 0;
936 updateLocal(bv, CURSOR, false);
937 // make sure status gets reset immediately
938 bv->owner()->clearMessage();
945 // what type of update to do on a cursor movement
946 int cursor_update = CURSOR;
948 if (text_.selection.set())
949 cursor_update = SELECTION;
951 switch (cmd.action) {
954 case LFUN_SELFINSERT:
955 if (bv->buffer()->isReadonly()) {
956 // setErrorMessage(N_("Document is read only"));
959 if (!cmd.argument.empty()) {
960 /* Automatically delete the currently selected
961 * text and replace it with what is being
962 * typed in now. Depends on lyxrc settings
963 * "auto_region_delete", which defaults to
966 // This should not be needed here and is also WRONG!
967 recordUndo(bv, Undo::INSERT, text_.cursor.par());
970 if (lyxrc.auto_region_delete) {
971 if (text_.selection.set()) {
972 text_.cutSelection(false, false);
975 text_.clearSelection();
976 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
977 bv->owner()->getIntl().getTransManager().
978 TranslateAndInsert(cmd.argument[i], &text_);
981 text_.selection.cursor = text_.cursor;
982 updwhat = CURSOR | CURSOR_PAR;
984 result = DISPATCHED_NOUPDATE;
987 // cursor movements that need special handling
990 result = moveRight(bv);
992 updwhat = cursor_update;
996 result = moveLeft(bv);
997 updwhat = cursor_update;
1001 result = moveDown(bv);
1002 updwhat = cursor_update;
1006 result = moveUp(bv);
1007 updwhat = cursor_update;
1011 if (crow() == text_.rows().begin())
1012 result = FINISHED_UP;
1014 text_.cursorPrevious();
1015 text_.clearSelection();
1016 result = DISPATCHED_NOUPDATE;
1018 updwhat = cursor_update;
1022 if (boost::next(crow()) == text_.rows().end())
1023 result = FINISHED_DOWN;
1026 text_.clearSelection();
1027 result = DISPATCHED_NOUPDATE;
1029 updwhat = cursor_update;
1032 case LFUN_BACKSPACE: {
1033 if (text_.selection.set())
1034 text_.cutSelection(true, false);
1037 updwhat = CURSOR_PAR;
1043 if (text_.selection.set()) {
1044 text_.cutSelection(true, false);
1048 updwhat = CURSOR_PAR;
1054 text_.cutSelection(true, true);
1055 updwhat = CURSOR_PAR;
1062 text_.copySelection();
1063 updwhat = CURSOR_PAR;
1066 case LFUN_PASTESELECTION:
1068 string const clip(bv->getClipboard());
1072 if (cmd.argument == "paragraph") {
1073 text_.insertStringAsParagraphs(clip);
1075 text_.insertStringAsLines(clip);
1078 text_.clearSelection();
1080 updwhat = CURSOR_PAR;
1086 if (!autoBreakRows) {
1087 if (CutAndPaste::nrOfParagraphs() > 1) {
1088 #ifdef WITH_WARNINGS
1089 #warning FIXME horrendously bad UI
1091 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1096 replaceSelection(bv->getLyXText());
1097 size_t sel_index = 0;
1098 string const & arg = cmd.argument;
1099 if (isStrUnsignedInt(arg)) {
1100 size_t const paste_arg = strToUnsignedInt(arg);
1101 #warning FIXME Check if the arg is in the domain of available selections.
1102 sel_index = paste_arg;
1104 text_.pasteSelection(sel_index);
1106 text_.clearSelection();
1107 updwhat = CURSOR_PAR;
1112 case LFUN_BREAKPARAGRAPH:
1113 if (!autoBreakRows) {
1114 result = DISPATCHED;
1117 replaceSelection(bv->getLyXText());
1118 text_.breakParagraph(paragraphs, 0);
1119 updwhat = CURSOR | FULL;
1123 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1124 if (!autoBreakRows) {
1125 result = DISPATCHED;
1128 replaceSelection(bv->getLyXText());
1129 text_.breakParagraph(paragraphs, 1);
1130 updwhat = CURSOR | FULL;
1134 case LFUN_BREAKLINE: {
1135 if (!autoBreakRows) {
1136 result = DISPATCHED;
1140 replaceSelection(bv->getLyXText());
1141 text_.insertInset(new InsetNewline);
1142 updwhat = CURSOR | CURSOR_PAR;
1148 // do not set layouts on non breakable textinsets
1149 if (autoBreakRows) {
1150 string cur_layout = cpar()->layout()->name();
1152 // Derive layout number from given argument (string)
1153 // and current buffer's textclass (number). */
1154 LyXTextClass const & tclass =
1155 bv->buffer()->params.getLyXTextClass();
1156 string layout = cmd.argument;
1157 bool hasLayout = tclass.hasLayout(layout);
1159 // If the entry is obsolete, use the new one instead.
1161 string const & obs =
1162 tclass[layout]->obsoleted_by();
1167 // see if we found the layout number:
1169 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1170 bv->owner()->dispatch(lf);
1174 if (cur_layout != layout) {
1175 cur_layout = layout;
1176 text_.setLayout(layout);
1177 bv->owner()->setLayout(cpar()->layout()->name());
1178 updwhat = CURSOR_PAR;
1182 // reset the layout box
1183 bv->owner()->setLayout(cpar()->layout()->name());
1186 case LFUN_PARAGRAPH_SPACING:
1187 // This one is absolutely not working. When fiddling with this
1188 // it also seems to me that the paragraphs inside the insettext
1189 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1190 // FIXME: how old is this comment ? ...
1192 ParagraphList::iterator pit = text_.cursor.par();
1193 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1194 float cur_value = 1.0;
1195 if (cur_spacing == Spacing::Other) {
1196 cur_value = pit->params().spacing().getValue();
1199 istringstream istr(STRCONV(cmd.argument));
1202 Spacing::Space new_spacing = cur_spacing;
1203 float new_value = cur_value;
1205 lyxerr << "Missing argument to `paragraph-spacing'"
1207 } else if (tmp == "single") {
1208 new_spacing = Spacing::Single;
1209 } else if (tmp == "onehalf") {
1210 new_spacing = Spacing::Onehalf;
1211 } else if (tmp == "double") {
1212 new_spacing = Spacing::Double;
1213 } else if (tmp == "other") {
1214 new_spacing = Spacing::Other;
1217 lyxerr << "new_value = " << tmpval << endl;
1220 } else if (tmp == "default") {
1221 new_spacing = Spacing::Default;
1223 lyxerr << _("Unknown spacing argument: ")
1224 << cmd.argument << endl;
1226 if (cur_spacing != new_spacing || cur_value != new_value) {
1227 pit->params().spacing(Spacing(new_spacing, new_value));
1228 updwhat = CURSOR_PAR;
1234 // These need to do repaints but don't require
1235 // special handling otherwise. A *lot* of the
1236 // above could probably be done similarly ...
1241 case LFUN_WORDRIGHT:
1242 // these two are really unhandled ...
1244 case LFUN_BEGINNINGBUF:
1245 updwhat = cursor_update;
1246 if (!bv->dispatch(cmd))
1247 result = UNDISPATCHED;
1256 case LFUN_WORDLEFTSEL:
1257 case LFUN_WORDRIGHTSEL:
1258 updwhat = SELECTION;
1263 if (!bv->dispatch(cmd))
1264 result = UNDISPATCHED;
1269 updateLocal(bv, updwhat, updflag);
1270 /// If the action has deleted all text in the inset, we need to change the
1271 // language to the language of the surronding text.
1272 if (!was_empty && paragraphs.begin()->empty() &&
1273 paragraphs.size() == 1) {
1274 LyXFont font(LyXFont::ALL_IGNORE);
1275 font.setLanguage(bv->getParentLanguage(this));
1276 setFont(bv, font, false);
1279 if (result >= FINISHED)
1280 bv->unlockInset(this);
1282 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1283 result = DISPATCHED;
1288 int InsetText::latex(Buffer const * buf, ostream & os,
1289 LatexRunParams const & runparams) const
1292 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1293 return texrow.rows();
1297 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1299 unsigned int lines = 0;
1301 ParagraphList::const_iterator beg = paragraphs.begin();
1302 ParagraphList::const_iterator end = paragraphs.end();
1303 ParagraphList::const_iterator it = beg;
1304 for (; it != end; ++it) {
1305 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1306 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1313 int InsetText::linuxdoc(Buffer const * buf, ostream & os) const
1315 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1316 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1318 // There is a confusion between the empty paragraph and the default paragraph
1319 // The default paragraph is <p></p>, the empty paragraph is *empty*
1320 // Since none of the floats of linuxdoc accepts standard paragraphs
1321 // I disable them. I don't expect problems. (jamatos 2003/07/27)
1322 for (; pit != pend; ++pit) {
1323 const string name = pit->layout()->latexname();
1325 sgml::openTag(os, 1, 0, name);
1326 buf->simpleLinuxDocOnePar(os, pit, 0);
1328 sgml::closeTag(os, 1, 0, name);
1334 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1336 unsigned int lines = 0;
1338 vector<string> environment_stack(10);
1339 vector<string> environment_inner(10);
1341 int const command_depth = 0;
1344 Paragraph::depth_type depth = 0; // paragraph depth
1346 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1347 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1349 for (; pit != pend; ++pit) {
1351 int desc_on = 0; // description mode
1353 LyXLayout_ptr const & style = pit->layout();
1355 // environment tag closing
1356 for (; depth > pit->params().depth(); --depth) {
1357 if (environment_inner[depth] != "!-- --") {
1358 item_name = "listitem";
1359 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1360 if (environment_inner[depth] == "varlistentry")
1361 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1363 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1364 environment_stack[depth].erase();
1365 environment_inner[depth].erase();
1368 if (depth == pit->params().depth()
1369 && environment_stack[depth] != style->latexname()
1370 && !environment_stack[depth].empty()) {
1371 if (environment_inner[depth] != "!-- --") {
1372 item_name= "listitem";
1373 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1374 if (environment_inner[depth] == "varlistentry")
1375 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1378 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1380 environment_stack[depth].erase();
1381 environment_inner[depth].erase();
1384 // Write opening SGML tags.
1385 switch (style->latextype) {
1386 case LATEX_PARAGRAPH:
1387 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1391 buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1395 case LATEX_ENVIRONMENT:
1396 case LATEX_ITEM_ENVIRONMENT:
1397 if (depth < pit->params().depth()) {
1398 depth = pit->params().depth();
1399 environment_stack[depth].erase();
1402 if (environment_stack[depth] != style->latexname()) {
1403 if (environment_stack.size() == depth + 1) {
1404 environment_stack.push_back("!-- --");
1405 environment_inner.push_back("!-- --");
1407 environment_stack[depth] = style->latexname();
1408 environment_inner[depth] = "!-- --";
1409 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1411 if (environment_inner[depth] != "!-- --") {
1412 item_name= "listitem";
1413 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1414 if (environment_inner[depth] == "varlistentry")
1415 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1419 if (style->latextype == LATEX_ENVIRONMENT) {
1420 if (!style->latexparam().empty()) {
1421 if (style->latexparam() == "CDATA")
1424 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1429 desc_on = (style->labeltype == LABEL_MANUAL);
1431 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1432 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1434 item_name = desc_on ? "term" : "para";
1435 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1439 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1443 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1446 // write closing SGML tags
1447 switch (style->latextype) {
1448 case LATEX_ENVIRONMENT:
1449 if (!style->latexparam().empty()) {
1450 if (style->latexparam() == "CDATA")
1453 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1456 case LATEX_ITEM_ENVIRONMENT:
1457 if (desc_on == 1) break;
1459 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1461 case LATEX_PARAGRAPH:
1462 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1465 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1471 for (int d = depth; d >= 0; --d) {
1472 if (!environment_stack[depth].empty()) {
1473 if (environment_inner[depth] != "!-- --") {
1474 item_name = "listitem";
1475 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1476 if (environment_inner[depth] == "varlistentry")
1477 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1480 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1488 void InsetText::validate(LaTeXFeatures & features) const
1490 for_each(paragraphs.begin(), paragraphs.end(),
1491 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1495 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1497 if (the_locking_inset) {
1498 the_locking_inset->getCursor(bv, x, y);
1502 y = cy() + InsetText::y();
1506 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1508 if (the_locking_inset) {
1509 the_locking_inset->getCursorPos(bv, x, y);
1512 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1513 y = cy() - TEXT_TO_INSET_OFFSET;
1517 int InsetText::insetInInsetY() const
1519 if (!the_locking_inset)
1522 return inset_y + the_locking_inset->insetInInsetY();
1526 void InsetText::fitInsetCursor(BufferView * bv) const
1528 if (the_locking_inset) {
1529 the_locking_inset->fitInsetCursor(bv);
1533 LyXFont const font = text_.getFont(cpar(), cpos());
1535 int const asc = font_metrics::maxAscent(font);
1536 int const desc = font_metrics::maxDescent(font);
1538 if (bv->fitLockedInsetCursor(cx(), cy(), asc, desc))
1539 need_update |= FULL;
1544 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1546 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1547 return moveLeftIntern(bv, false, activate_inset, selecting);
1549 return moveRightIntern(bv, true, activate_inset, selecting);
1554 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1556 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1557 return moveRightIntern(bv, true, activate_inset, selecting);
1559 return moveLeftIntern(bv, false, activate_inset, selecting);
1564 InsetText::moveRightIntern(BufferView * bv, bool front,
1565 bool activate_inset, bool selecting)
1567 ParagraphList::iterator c_par = cpar();
1569 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1570 return FINISHED_RIGHT;
1571 if (activate_inset && checkAndActivateInset(bv, front))
1573 text_.cursorRight(bv);
1575 text_.clearSelection();
1576 return DISPATCHED_NOUPDATE;
1581 InsetText::moveLeftIntern(BufferView * bv, bool front,
1582 bool activate_inset, bool selecting)
1584 if (cpar() == paragraphs.begin() && cpos() <= 0)
1586 text_.cursorLeft(bv);
1588 text_.clearSelection();
1589 if (activate_inset && checkAndActivateInset(bv, front))
1591 return DISPATCHED_NOUPDATE;
1595 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1597 if (crow() == text_.rows().begin())
1600 text_.clearSelection();
1601 return DISPATCHED_NOUPDATE;
1605 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1607 if (boost::next(crow()) == text_.rows().end())
1608 return FINISHED_DOWN;
1609 text_.cursorDown(bv);
1610 text_.clearSelection();
1611 return DISPATCHED_NOUPDATE;
1615 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1617 if (the_locking_inset) {
1618 if (the_locking_inset->insetAllowed(inset))
1619 return the_locking_inset->insertInset(bv, inset);
1622 inset->setOwner(this);
1623 text_.insertInset(inset);
1625 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1630 bool InsetText::insetAllowed(InsetOld::Code code) const
1632 // in_insetAllowed is a really gross hack,
1633 // to allow us to call the owner's insetAllowed
1634 // without stack overflow, which can happen
1635 // when the owner uses InsetCollapsable::insetAllowed()
1637 if (in_insetAllowed)
1639 in_insetAllowed = true;
1640 if (the_locking_inset)
1641 ret = the_locking_inset->insetAllowed(code);
1643 ret = owner()->insetAllowed(code);
1644 in_insetAllowed = false;
1649 UpdatableInset * InsetText::getLockingInset() const
1651 return the_locking_inset ? the_locking_inset->getLockingInset() :
1652 const_cast<InsetText *>(this);
1656 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1660 if (the_locking_inset)
1661 return the_locking_inset->getFirstLockingInsetOfType(c);
1666 bool InsetText::showInsetDialog(BufferView * bv) const
1668 if (the_locking_inset)
1669 return the_locking_inset->showInsetDialog(bv);
1674 void InsetText::getLabelList(std::vector<string> & list) const
1676 ParagraphList::const_iterator pit = paragraphs.begin();
1677 ParagraphList::const_iterator pend = paragraphs.end();
1678 for (; pit != pend; ++pit) {
1679 InsetList::const_iterator beg = pit->insetlist.begin();
1680 InsetList::const_iterator end = pit->insetlist.end();
1681 for (; beg != end; ++beg)
1682 beg->inset->getLabelList(list);
1687 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1690 if (the_locking_inset) {
1691 the_locking_inset->setFont(bv, font, toggleall, selectall);
1695 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1696 || cpar()->empty()) {
1697 text_.setFont(font, toggleall);
1702 if (text_.selection.set())
1703 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1707 text_.selection.cursor = text_.cursor;
1708 text_.cursorBottom();
1709 text_.setSelection();
1712 text_.toggleFree(font, toggleall);
1715 text_.clearSelection();
1719 bool flag = (selectall || text_.selection.set());
1722 updateLocal(bv, FULL, true);
1724 updateLocal(bv, CURSOR_PAR, true);
1728 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1730 InsetOld * inset = cpar()->getInset(cpos());
1731 if (!isHighlyEditableInset(inset))
1733 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1734 inset->localDispatch(cmd);
1735 if (!the_locking_inset)
1737 updateLocal(bv, CURSOR, false);
1742 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1743 mouse_button::state button)
1745 x -= drawTextXOffset;
1747 int dummyy = y + dim_.asc;
1748 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1749 // we only do the edit() call if the inset was hit by the mouse
1750 // or if it is a highly editable inset. So we should call this
1751 // function from our own edit with button < 0.
1752 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1754 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1763 inset_x = cix() - top_x + drawTextXOffset;
1764 inset_y = ciy() + drawTextYOffset;
1765 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1766 inset->localDispatch(cmd);
1767 if (!the_locking_inset)
1769 updateLocal(bv, CURSOR, false);
1774 void InsetText::setParagraphData(ParagraphList const & plist)
1776 // we have to unlock any locked inset otherwise we're in troubles
1777 the_locking_inset = 0;
1779 // But it it makes no difference that is a lot better.
1781 // See if this can be simplified when std::list is in effect.
1784 ParagraphList::const_iterator it = plist.begin();
1785 ParagraphList::const_iterator end = plist.end();
1786 for (; it != end; ++it) {
1787 paragraphs.push_back(*it);
1788 paragraphs.back().setInsetOwner(this);
1796 void InsetText::markNew(bool track_changes)
1798 ParagraphList::iterator pit = paragraphs.begin();
1799 ParagraphList::iterator pend = paragraphs.end();
1800 for (; pit != pend; ++pit) {
1801 if (track_changes) {
1802 pit->trackChanges();
1804 // no-op when not tracking
1805 pit->cleanChanges();
1811 void InsetText::setText(string const & data, LyXFont const & font)
1814 for (unsigned int i = 0; i < data.length(); ++i)
1815 paragraphs.begin()->insertChar(i, data[i], font);
1820 void InsetText::setAutoBreakRows(bool flag)
1822 if (flag != autoBreakRows) {
1823 autoBreakRows = flag;
1831 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1833 if (how != drawFrame_) {
1836 updateLocal(bv, DRAW_FRAME, false);
1841 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1843 if (frame_color != col) {
1846 updateLocal(bv, DRAW_FRAME, false);
1851 int InsetText::cx() const
1853 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1854 if (the_locking_inset) {
1855 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1856 if (font.isVisibleRightToLeft())
1857 x -= the_locking_inset->width();
1863 int InsetText::cix() const
1865 int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1866 if (the_locking_inset) {
1867 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1868 if (font.isVisibleRightToLeft())
1869 x -= the_locking_inset->width();
1875 int InsetText::cy() const
1877 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1881 int InsetText::ciy() const
1883 return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1887 pos_type InsetText::cpos() const
1889 return text_.cursor.pos();
1893 ParagraphList::iterator InsetText::cpar() const
1895 return text_.cursor.par();
1899 bool InsetText::cboundary() const
1901 return text_.cursor.boundary();
1905 RowList::iterator InsetText::crow() const
1907 return text_.cursorRow();
1911 LyXText * InsetText::getLyXText(BufferView const * bv,
1912 bool const recursive) const
1915 if (recursive && the_locking_inset)
1916 return the_locking_inset->getLyXText(bv, true);
1921 void InsetText::setViewCache(BufferView const * bv) const
1924 text_.bv_owner = const_cast<BufferView *>(bv);
1928 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1931 /// then remove all LyXText in text-insets
1932 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1933 const_cast<ParagraphList&>(paragraphs).end(),
1934 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1939 void InsetText::resizeLyXText(BufferView * bv, bool /*force*/) const
1941 if (paragraphs.size() == 1 && paragraphs.begin()->empty()) {
1942 // no data, resize not neccessary!
1943 // we have to do this as a fixed width may have changed!
1946 restoreLyXTextState();
1958 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1959 const_cast<ParagraphList&>(paragraphs).end(),
1960 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1963 restoreLyXTextState();
1965 // seems to be unneeded
1967 if (the_locking_inset) {
1968 inset_x = cix() - top_x + drawTextXOffset;
1969 inset_y = ciy() + drawTextYOffset;
1972 text_.top_y(bv->screen().topCursorVisible(&text_));
1974 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
1975 // this will scroll the screen such that the cursor becomes visible
1976 bv->updateScrollbar();
1978 need_update |= FULL;
1984 void InsetText::reinitLyXText() const
1986 BufferView * bv = text_.bv_owner;
1993 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1994 const_cast<ParagraphList&>(paragraphs).end(),
1995 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1998 restoreLyXTextState();
1999 if (the_locking_inset) {
2000 inset_x = cix() - top_x + drawTextXOffset;
2001 inset_y = ciy() + drawTextYOffset;
2003 text_.top_y(bv->screen().topCursorVisible(&text_));
2005 // this will scroll the screen such that the cursor becomes visible
2006 bv->updateScrollbar();
2013 void InsetText::removeNewlines()
2015 bool changed = false;
2017 ParagraphList::iterator it = paragraphs.begin();
2018 ParagraphList::iterator end = paragraphs.end();
2019 for (; it != end; ++it) {
2020 for (int i = 0; i < it->size(); ++i) {
2021 if (it->isNewline(i)) {
2032 bool InsetText::nodraw() const
2034 if (the_locking_inset)
2035 return the_locking_inset->nodraw();
2036 return UpdatableInset::nodraw();
2040 int InsetText::scroll(bool recursive) const
2042 int sx = UpdatableInset::scroll(false);
2044 if (recursive && the_locking_inset)
2045 sx += the_locking_inset->scroll(recursive);
2051 void InsetText::clearSelection(BufferView * bv)
2053 getLyXText(bv)->clearSelection();
2057 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2059 Painter & pain = bv->painter();
2061 int h = dim_.asc + dim_.des;
2062 int ty = baseline - dim_.asc;
2068 if ((ty + h) > pain.paperHeight())
2069 h = pain.paperHeight();
2070 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2071 w = pain.paperWidth();
2072 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2077 ParagraphList * InsetText::getParagraphs(int i) const
2079 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2083 LyXCursor const & InsetText::cursor(BufferView * bv) const
2085 if (the_locking_inset)
2086 return the_locking_inset->cursor(bv);
2087 return getLyXText(bv)->cursor;
2091 InsetOld * InsetText::getInsetFromID(int id_arg) const
2094 return const_cast<InsetText *>(this);
2096 ParagraphList::const_iterator pit = paragraphs.begin();
2097 ParagraphList::const_iterator pend = paragraphs.end();
2098 for (; pit != pend; ++pit) {
2099 InsetList::const_iterator it = pit->insetlist.begin();
2100 InsetList::const_iterator end = pit->insetlist.end();
2101 for (; it != end; ++it) {
2102 if (it->inset->id() == id_arg)
2104 InsetOld * in = it->inset->getInsetFromID(id_arg);
2114 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2117 if (the_locking_inset) {
2118 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2119 if (!word.word().empty()) {
2123 // we have to go on checking so move cursor to the next char
2124 text_.cursor.pos(text_.cursor.pos() + 1);
2126 word = text_.selectNextWordToSpellcheck(value);
2127 if (word.word().empty())
2128 bv->unlockInset(const_cast<InsetText *>(this));
2135 void InsetText::selectSelectedWord(BufferView * bv)
2137 if (the_locking_inset) {
2138 the_locking_inset->selectSelectedWord(bv);
2141 getLyXText(bv)->selectSelectedWord();
2142 updateLocal(bv, SELECTION, false);
2146 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2148 if (the_locking_inset)
2149 the_locking_inset->toggleSelection(bv, kill_selection);
2151 int x = top_x + TEXT_TO_INSET_OFFSET;
2153 RowList::iterator rit = text_.rows().begin();
2154 RowList::iterator end = text_.rows().end();
2155 int y_offset = top_baseline - rit->ascent_of_text();
2157 while (rit != end && y + rit->height() <= 0) {
2164 if (need_update & SELECTION)
2166 bv->screen().toggleSelection(&text_, bv, kill_selection, y_offset, x);
2170 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2172 if (the_locking_inset) {
2173 if (the_locking_inset->nextChange(bv, length))
2175 text_.cursorRight(true);
2177 lyx::find::SearchResult result =
2178 lyx::find::findNextChange(bv, &text_, length);
2180 if (result == lyx::find::SR_FOUND) {
2181 LyXCursor cur = text_.cursor;
2182 bv->unlockInset(bv->theLockingInset());
2183 if (bv->lockInset(this))
2186 text_.setSelectionRange(length);
2187 updateLocal(bv, SELECTION, false);
2189 return result != lyx::find::SR_NOT_FOUND;
2193 bool InsetText::searchForward(BufferView * bv, string const & str,
2196 if (the_locking_inset) {
2197 if (the_locking_inset->searchForward(bv, str, cs, mw))
2199 text_.cursorRight(true);
2201 lyx::find::SearchResult result =
2202 lyx::find::find(bv, &text_, str, true, cs, mw);
2204 if (result == lyx::find::SR_FOUND) {
2205 LyXCursor cur = text_.cursor;
2206 bv->unlockInset(bv->theLockingInset());
2207 if (bv->lockInset(this))
2210 text_.setSelectionRange(str.length());
2211 updateLocal(bv, SELECTION, false);
2213 return result != lyx::find::SR_NOT_FOUND;
2217 bool InsetText::searchBackward(BufferView * bv, string const & str,
2220 if (the_locking_inset) {
2221 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2225 ParagraphList::iterator pit = paragraphs.begin();
2226 ParagraphList::iterator pend = paragraphs.end();
2228 while (boost::next(pit) != pend)
2231 text_.setCursor(pit, pit->size());
2233 lyx::find::SearchResult result =
2234 lyx::find::find(bv, &text_, str, false, cs, mw);
2236 if (result == lyx::find::SR_FOUND) {
2237 LyXCursor cur = text_.cursor;
2238 bv->unlockInset(bv->theLockingInset());
2239 if (bv->lockInset(this))
2242 text_.setSelectionRange(str.length());
2243 updateLocal(bv, SELECTION, false);
2245 return result != lyx::find::SR_NOT_FOUND;
2249 bool InsetText::checkInsertChar(LyXFont & font)
2252 return owner()->checkInsertChar(font);
2257 void InsetText::collapseParagraphs(BufferView * bv)
2259 while (paragraphs.size() > 1) {
2260 ParagraphList::iterator first_par = paragraphs.begin();
2261 ParagraphList::iterator next_par = boost::next(first_par);
2262 size_t const first_par_size = first_par->size();
2264 if (!first_par->empty() &&
2265 !next_par->empty() &&
2266 !first_par->isSeparator(first_par_size - 1)) {
2267 first_par->insertChar(first_par_size, ' ');
2270 if (text_.selection.set()) {
2271 if (text_.selection.start.par() == next_par) {
2272 text_.selection.start.par(first_par);
2273 text_.selection.start.pos(
2274 text_.selection.start.pos() + first_par_size);
2276 if (text_.selection.end.par() == next_par) {
2277 text_.selection.end.par(first_par);
2278 text_.selection.end.pos(
2279 text_.selection.end.pos() + first_par_size);
2283 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2289 void InsetText::getDrawFont(LyXFont & font) const
2293 owner()->getDrawFont(font);
2297 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2299 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2300 // And it probably does. You have to take a look at this John. (Lgb)
2301 #warning John, have a look here. (Lgb)
2302 ParagraphList::iterator pit = plist.begin();
2303 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2305 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2307 ParagraphList::iterator pend = plist.end();
2308 for (; pit != pend; ++pit) {
2309 paragraphs.push_back(*pit);
2316 void InsetText::addPreview(PreviewLoader & loader) const
2318 ParagraphList::const_iterator pit = paragraphs.begin();
2319 ParagraphList::const_iterator pend = paragraphs.end();
2321 for (; pit != pend; ++pit) {
2322 InsetList::const_iterator it = pit->insetlist.begin();
2323 InsetList::const_iterator end = pit->insetlist.end();
2324 for (; it != end; ++it) {
2325 it->inset->addPreview(loader);