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;
367 int yo = y_offset + first;
369 paintRows2(*bv, text_, rit, end, x, y, yf, y2, yo);
373 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked)) {
374 drawFrame(pain, int(start_x));
377 if (need_update != INIT) {
383 void InsetText::drawFrame(Painter & pain, int x) const
385 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
387 frame_y = top_baseline - dim_.asc + ttoD2;
388 frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
389 frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
390 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
395 void InsetText::setUpdateStatus(int what) const
398 // we will to redraw us full if our LyXText wants it
399 if (text_.needRefresh())
402 // this to not draw a selection when we redraw all of it!
403 if (need_update & CURSOR && !(need_update & SELECTION)) {
404 if (text_.selection.set())
406 text_.clearSelection();
411 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
416 if (!autoBreakRows && paragraphs.size() > 1)
417 collapseParagraphs(bv);
419 text_.partialRebreak();
420 setUpdateStatus(what);
421 bool flag = mark_dirty ||
422 ((need_update != CURSOR && need_update != NONE) ||
423 text_.needRefresh() || text_.selection.set());
424 if (!text_.selection.set())
425 text_.selection.cursor = text_.cursor;
431 bv->updateInset(const_cast<InsetText *>(this));
434 if (need_update == CURSOR)
436 bv->owner()->view_state_changed();
437 bv->owner()->updateMenubar();
438 bv->owner()->updateToolbar();
439 if (old_par != cpar()) {
440 bv->owner()->setLayout(cpar()->layout()->name());
446 string const InsetText::editMessage() const
448 return _("Opened Text Inset");
452 void InsetText::insetUnlock(BufferView * bv)
454 if (the_locking_inset) {
455 the_locking_inset->insetUnlock(bv);
456 the_locking_inset = 0;
457 updateLocal(bv, CURSOR_PAR, false);
463 if (text_.selection.set()) {
464 text_.clearSelection();
466 } else if (owner()) {
467 bv->owner()->setLayout(owner()->getLyXText(bv)
468 ->cursor.par()->layout()->name());
470 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
471 // hack for deleteEmptyParMech
472 ParagraphList::iterator first_par = paragraphs.begin();
473 if (!first_par->empty()) {
474 text_.setCursor(first_par, 0);
475 } else if (paragraphs.size() > 1) {
476 text_.setCursor(boost::next(first_par), 0);
479 updateLocal(bv, code, false);
482 setUpdateStatus(code);
487 void InsetText::lockInset(BufferView * bv)
490 the_locking_inset = 0;
491 inset_pos = inset_x = inset_y = 0;
492 inset_boundary = false;
493 inset_par = paragraphs.end();
494 old_par = paragraphs.end();
495 text_.setCursorIntern(paragraphs.begin(), 0);
496 text_.clearSelection();
498 // If the inset is empty set the language of the current font to the
499 // language to the surronding text (if different).
500 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
501 bv->getParentLanguage(this) != text_.current_font.language()) {
502 LyXFont font(LyXFont::ALL_IGNORE);
503 font.setLanguage(bv->getParentLanguage(this));
504 setFont(bv, font, false);
507 if (drawFrame_ == LOCKED)
508 code = CURSOR|DRAW_FRAME;
509 setUpdateStatus(code);
513 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
515 the_locking_inset = inset;
516 inset_x = cix() - top_x + drawTextXOffset;
517 inset_y = ciy() + drawTextYOffset;
520 inset_boundary = cboundary();
521 //updateLocal(bv, CURSOR, false);
525 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
527 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
531 if (!the_locking_inset) {
532 ParagraphList::iterator pit = paragraphs.begin();
533 ParagraphList::iterator pend = paragraphs.end();
535 int const id = inset->id();
536 for (; pit != pend; ++pit) {
537 InsetList::iterator it = pit->insetlist.begin();
538 InsetList::iterator const end = pit->insetlist.end();
539 for (; it != end; ++it) {
540 if (it->inset == inset) {
541 lyxerr << "InsetText::lockInsetInInset: 1 a\n";
542 text_.setCursorIntern(pit, it->pos);
543 lyxerr << "InsetText::lockInsetInInset: 1 b\n";
544 lyxerr << "bv: " << bv << " inset: " << inset << "\n";
545 lockInset(bv, inset);
546 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
549 if (it->inset->getInsetFromID(id)) {
550 lyxerr << "InsetText::lockInsetInInset: 2\n";
551 text_.setCursorIntern(pit, it->pos);
552 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
553 return the_locking_inset->lockInsetInInset(bv, inset);
557 lyxerr << "InsetText::lockInsetInInset: 3\n";
560 if (inset == cpar()->getInset(cpos())) {
561 lyxerr[Debug::INSETS] << "OK" << endl;
562 lockInset(bv, inset);
566 if (the_locking_inset && the_locking_inset == inset) {
567 if (cpar() == inset_par && cpos() == inset_pos) {
568 lyxerr[Debug::INSETS] << "OK" << endl;
569 inset_x = cix() - top_x + drawTextXOffset;
570 inset_y = ciy() + drawTextYOffset;
572 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
574 } else if (the_locking_inset) {
575 lyxerr[Debug::INSETS] << "MAYBE" << endl;
576 return the_locking_inset->lockInsetInInset(bv, inset);
578 lyxerr[Debug::INSETS] << "NOT OK" << endl;
583 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
586 if (!the_locking_inset)
588 if (the_locking_inset == inset) {
589 the_locking_inset->insetUnlock(bv);
590 getLyXText(bv)->updateInset(inset);
591 the_locking_inset = 0;
593 moveRightIntern(bv, true, false);
594 old_par = paragraphs.end(); // force layout setting
598 updateLocal(bv, CURSOR, false);
601 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
605 bool InsetText::updateInsetInInset(BufferView * bv, InsetOld * inset)
607 if (!autoBreakRows && paragraphs.size() > 1)
608 collapseParagraphs(bv);
613 if (inset->owner() != this) {
614 int ustat = CURSOR_PAR;
616 UpdatableInset * tl_inset = the_locking_inset;
618 found = tl_inset->updateInsetInInset(bv, inset);
620 tl_inset = static_cast<UpdatableInset *>(inset);
621 while(tl_inset->owner() && tl_inset->owner() != this)
622 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
623 if (!tl_inset->owner())
625 found = tl_inset->updateInsetInInset(bv, inset);
628 text_.updateInset(tl_inset);
629 setUpdateStatus(ustat);
633 bool found = text_.updateInset(inset);
635 setUpdateStatus(CURSOR_PAR);
636 if (the_locking_inset &&
637 cpar() == inset_par && cpos() == inset_pos)
639 inset_x = cix() - top_x + drawTextXOffset;
640 inset_y = ciy() + drawTextYOffset;
647 void InsetText::lfunMousePress(FuncRequest const & cmd)
651 // use this to check mouse motion for selection!
655 BufferView * bv = cmd.view();
656 FuncRequest cmd1 = cmd;
662 int tmp_x = cmd.x - drawTextXOffset;
663 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
664 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
666 if (the_locking_inset) {
667 if (the_locking_inset == inset) {
668 the_locking_inset->localDispatch(cmd1);
671 // otherwise only unlock the_locking_inset
672 the_locking_inset->insetUnlock(bv);
673 the_locking_inset = 0;
676 no_selection = false;
678 if (bv->theLockingInset()) {
679 if (isHighlyEditableInset(inset)) {
680 // We just have to lock the inset before calling a
682 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
683 if (!bv->lockInset(uinset)) {
684 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
686 inset->localDispatch(cmd1);
687 if (the_locking_inset)
688 updateLocal(bv, CURSOR, false);
693 bool paste_internally = false;
694 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
695 localDispatch(FuncRequest(bv, LFUN_COPY));
696 paste_internally = true;
698 int old_top_y = text_.top_y();
700 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
702 // set the selection cursor!
703 text_.selection.cursor = text_.cursor;
704 text_.cursor.x_fix(text_.cursor.x());
706 if (text_.selection.set()) {
707 text_.clearSelection();
708 updateLocal(bv, FULL, false);
710 text_.clearSelection();
711 updateLocal(bv, CURSOR, false);
714 bv->owner()->setLayout(cpar()->layout()->name());
716 // we moved the view we cannot do mouse selection in this case!
717 if (getLyXText(bv)->top_y() != old_top_y)
720 // Insert primary selection with middle mouse
721 // if there is a local selection in the current buffer,
723 if (cmd.button() == mouse_button::button2) {
724 if (paste_internally)
725 localDispatch(FuncRequest(bv, LFUN_PASTE));
727 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
730 getLyXText(bv)->clearSelection();
735 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
737 BufferView * bv = cmd.view();
738 FuncRequest cmd1 = cmd;
743 if (the_locking_inset)
744 return the_locking_inset->localDispatch(cmd1);
746 int tmp_x = cmd.x - drawTextXOffset;
747 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
748 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
751 // This code should probably be removed now. Simple insets
752 // (!highlyEditable) can actually take the localDispatch,
753 // and turn it into edit() if necessary. But we still
754 // need to deal properly with the whole relative vs.
755 // absolute mouse co-ords thing in a realiable, sensible way
757 if (isHighlyEditableInset(inset))
758 ret = inset->localDispatch(cmd1);
760 inset_x = cix(bv) - top_x + drawTextXOffset;
761 inset_y = ciy() + drawTextYOffset;
762 cmd1.x = cmd.x - inset_x;
763 cmd1.y = cmd.x - inset_y;
764 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
768 ret = inset->localDispatch(cmd1);
769 updateLocal(bv, CURSOR_PAR, false);
776 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
778 FuncRequest cmd1 = cmd;
782 if (the_locking_inset) {
783 the_locking_inset->localDispatch(cmd1);
787 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
790 BufferView * bv = cmd.view();
791 LyXCursor cur = text_.cursor;
792 text_.setCursorFromCoordinates
793 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
794 text_.cursor.x_fix(text_.cursor.x());
795 if (cur == text_.cursor)
797 text_.setSelection();
798 bool flag = (text_.toggle_cursor.par() != text_.toggle_end_cursor.par() ||
799 text_.toggle_cursor.pos() != text_.toggle_end_cursor.pos());
801 updateLocal(bv, SELECTION, false);
806 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
808 BufferView * bv = cmd.view();
811 switch (cmd.action) {
812 case LFUN_INSET_EDIT: {
813 UpdatableInset::localDispatch(cmd);
815 if (!bv->lockInset(this)) {
816 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
821 the_locking_inset = 0;
825 inset_boundary = false;
826 inset_par = paragraphs.end();
827 old_par = paragraphs.end();
830 if (cmd.argument.size()) {
831 if (cmd.argument == "left")
832 text_.setCursorIntern(paragraphs.begin(), 0);
834 ParagraphList::iterator it = boost::prior(paragraphs.end());
835 text_.setCursor(it, it->size());
838 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
839 // we put here -1 and not button as now the button in the
840 // edit call should not be needed we will fix this in 1.3.x
841 // cycle hopefully (Jug 20020509)
842 // FIXME: GUII I've changed this to none: probably WRONG
843 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
844 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
846 text_.cursor.x_fix(text_.cursor.x());
850 text_.clearSelection();
853 // If the inset is empty set the language of the current font to the
854 // language to the surronding text (if different).
855 if (paragraphs.begin()->empty() &&
856 paragraphs.size() == 1 &&
857 bv->getParentLanguage(this) != text_.current_font.language())
859 LyXFont font(LyXFont::ALL_IGNORE);
860 font.setLanguage(bv->getParentLanguage(this));
861 setFont(bv, font, false);
865 if (drawFrame_ == LOCKED)
866 code = CURSOR | DRAW_FRAME;
868 updateLocal(bv, code, false);
869 // Tell the paragraph dialog that we've entered an insettext.
870 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
874 case LFUN_MOUSE_PRESS:
878 case LFUN_MOUSE_MOTION:
879 lfunMouseMotion(cmd);
882 case LFUN_MOUSE_RELEASE:
883 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
889 bool was_empty = (paragraphs.begin()->empty() &&
890 paragraphs.size() == 1);
892 no_selection = false;
893 RESULT result = UpdatableInset::localDispatch(cmd);
894 if (result != UNDISPATCHED)
898 if (cmd.action < 0 && cmd.argument.empty())
901 if (the_locking_inset) {
902 result = the_locking_inset->localDispatch(cmd);
903 if (result == DISPATCHED_NOUPDATE)
905 else if (result == DISPATCHED) {
906 updateLocal(bv, CURSOR_PAR, false);
908 } else if (result >= FINISHED) {
911 moveRightIntern(bv, false, false);
915 if ((result = moveUp(bv)) >= FINISHED) {
916 updateLocal(bv, CURSOR, false);
917 bv->unlockInset(this);
921 if ((result = moveDown(bv)) >= FINISHED) {
922 updateLocal(bv, CURSOR, false);
923 bv->unlockInset(this);
930 the_locking_inset = 0;
931 updateLocal(bv, CURSOR, false);
932 // make sure status gets reset immediately
933 bv->owner()->clearMessage();
940 // what type of update to do on a cursor movement
941 int cursor_update = CURSOR;
943 if (text_.selection.set())
944 cursor_update = SELECTION;
946 switch (cmd.action) {
949 case LFUN_SELFINSERT:
950 if (bv->buffer()->isReadonly()) {
951 // setErrorMessage(N_("Document is read only"));
954 if (!cmd.argument.empty()) {
955 /* Automatically delete the currently selected
956 * text and replace it with what is being
957 * typed in now. Depends on lyxrc settings
958 * "auto_region_delete", which defaults to
961 // This should not be needed here and is also WRONG!
962 recordUndo(bv, Undo::INSERT, text_.cursor.par());
965 if (lyxrc.auto_region_delete) {
966 if (text_.selection.set()) {
967 text_.cutSelection(false, false);
970 text_.clearSelection();
971 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
972 bv->owner()->getIntl().getTransManager().
973 TranslateAndInsert(cmd.argument[i], &text_);
976 text_.selection.cursor = text_.cursor;
977 updwhat = CURSOR | CURSOR_PAR;
979 result = DISPATCHED_NOUPDATE;
982 // cursor movements that need special handling
985 result = moveRight(bv);
987 updwhat = cursor_update;
991 result = moveLeft(bv);
992 updwhat = cursor_update;
996 result = moveDown(bv);
997 updwhat = cursor_update;
1001 result = moveUp(bv);
1002 updwhat = cursor_update;
1006 if (crow() == text_.rows().begin())
1007 result = FINISHED_UP;
1009 text_.cursorPrevious();
1010 text_.clearSelection();
1011 result = DISPATCHED_NOUPDATE;
1013 updwhat = cursor_update;
1017 if (boost::next(crow()) == text_.rows().end())
1018 result = FINISHED_DOWN;
1021 text_.clearSelection();
1022 result = DISPATCHED_NOUPDATE;
1024 updwhat = cursor_update;
1027 case LFUN_BACKSPACE: {
1028 if (text_.selection.set())
1029 text_.cutSelection(true, false);
1032 updwhat = CURSOR_PAR;
1038 if (text_.selection.set()) {
1039 text_.cutSelection(true, false);
1043 updwhat = CURSOR_PAR;
1049 text_.cutSelection(true, true);
1050 updwhat = CURSOR_PAR;
1057 text_.copySelection();
1058 updwhat = CURSOR_PAR;
1061 case LFUN_PASTESELECTION:
1063 string const clip(bv->getClipboard());
1067 if (cmd.argument == "paragraph") {
1068 text_.insertStringAsParagraphs(clip);
1070 text_.insertStringAsLines(clip);
1073 text_.clearSelection();
1075 updwhat = CURSOR_PAR;
1081 if (!autoBreakRows) {
1082 if (CutAndPaste::nrOfParagraphs() > 1) {
1083 #ifdef WITH_WARNINGS
1084 #warning FIXME horrendously bad UI
1086 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1091 replaceSelection(bv->getLyXText());
1092 size_t sel_index = 0;
1093 string const & arg = cmd.argument;
1094 if (isStrUnsignedInt(arg)) {
1095 size_t const paste_arg = strToUnsignedInt(arg);
1096 #warning FIXME Check if the arg is in the domain of available selections.
1097 sel_index = paste_arg;
1099 text_.pasteSelection(sel_index);
1101 text_.clearSelection();
1102 updwhat = CURSOR_PAR;
1107 case LFUN_BREAKPARAGRAPH:
1108 if (!autoBreakRows) {
1109 result = DISPATCHED;
1112 replaceSelection(bv->getLyXText());
1113 text_.breakParagraph(paragraphs, 0);
1114 updwhat = CURSOR | FULL;
1118 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1119 if (!autoBreakRows) {
1120 result = DISPATCHED;
1123 replaceSelection(bv->getLyXText());
1124 text_.breakParagraph(paragraphs, 1);
1125 updwhat = CURSOR | FULL;
1129 case LFUN_BREAKLINE: {
1130 if (!autoBreakRows) {
1131 result = DISPATCHED;
1135 replaceSelection(bv->getLyXText());
1136 text_.insertInset(new InsetNewline);
1137 updwhat = CURSOR | CURSOR_PAR;
1143 // do not set layouts on non breakable textinsets
1144 if (autoBreakRows) {
1145 string cur_layout = cpar()->layout()->name();
1147 // Derive layout number from given argument (string)
1148 // and current buffer's textclass (number). */
1149 LyXTextClass const & tclass =
1150 bv->buffer()->params.getLyXTextClass();
1151 string layout = cmd.argument;
1152 bool hasLayout = tclass.hasLayout(layout);
1154 // If the entry is obsolete, use the new one instead.
1156 string const & obs =
1157 tclass[layout]->obsoleted_by();
1162 // see if we found the layout number:
1164 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1165 bv->owner()->dispatch(lf);
1169 if (cur_layout != layout) {
1170 cur_layout = layout;
1171 text_.setLayout(layout);
1172 bv->owner()->setLayout(cpar()->layout()->name());
1173 updwhat = CURSOR_PAR;
1177 // reset the layout box
1178 bv->owner()->setLayout(cpar()->layout()->name());
1181 case LFUN_PARAGRAPH_SPACING:
1182 // This one is absolutely not working. When fiddling with this
1183 // it also seems to me that the paragraphs inside the insettext
1184 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1185 // FIXME: how old is this comment ? ...
1187 ParagraphList::iterator pit = text_.cursor.par();
1188 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1189 float cur_value = 1.0;
1190 if (cur_spacing == Spacing::Other) {
1191 cur_value = pit->params().spacing().getValue();
1194 istringstream istr(STRCONV(cmd.argument));
1197 Spacing::Space new_spacing = cur_spacing;
1198 float new_value = cur_value;
1200 lyxerr << "Missing argument to `paragraph-spacing'"
1202 } else if (tmp == "single") {
1203 new_spacing = Spacing::Single;
1204 } else if (tmp == "onehalf") {
1205 new_spacing = Spacing::Onehalf;
1206 } else if (tmp == "double") {
1207 new_spacing = Spacing::Double;
1208 } else if (tmp == "other") {
1209 new_spacing = Spacing::Other;
1212 lyxerr << "new_value = " << tmpval << endl;
1215 } else if (tmp == "default") {
1216 new_spacing = Spacing::Default;
1218 lyxerr << _("Unknown spacing argument: ")
1219 << cmd.argument << endl;
1221 if (cur_spacing != new_spacing || cur_value != new_value) {
1222 pit->params().spacing(Spacing(new_spacing, new_value));
1223 updwhat = CURSOR_PAR;
1229 // These need to do repaints but don't require
1230 // special handling otherwise. A *lot* of the
1231 // above could probably be done similarly ...
1236 case LFUN_WORDRIGHT:
1237 // these two are really unhandled ...
1239 case LFUN_BEGINNINGBUF:
1240 updwhat = cursor_update;
1241 if (!bv->dispatch(cmd))
1242 result = UNDISPATCHED;
1251 case LFUN_WORDLEFTSEL:
1252 case LFUN_WORDRIGHTSEL:
1253 updwhat = SELECTION;
1258 if (!bv->dispatch(cmd))
1259 result = UNDISPATCHED;
1264 updateLocal(bv, updwhat, updflag);
1265 /// If the action has deleted all text in the inset, we need to change the
1266 // language to the language of the surronding text.
1267 if (!was_empty && paragraphs.begin()->empty() &&
1268 paragraphs.size() == 1) {
1269 LyXFont font(LyXFont::ALL_IGNORE);
1270 font.setLanguage(bv->getParentLanguage(this));
1271 setFont(bv, font, false);
1274 if (result >= FINISHED)
1275 bv->unlockInset(this);
1277 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1278 result = DISPATCHED;
1283 int InsetText::latex(Buffer const * buf, ostream & os,
1284 LatexRunParams const & runparams) const
1287 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1288 return texrow.rows();
1292 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1294 unsigned int lines = 0;
1296 ParagraphList::const_iterator beg = paragraphs.begin();
1297 ParagraphList::const_iterator end = paragraphs.end();
1298 ParagraphList::const_iterator it = beg;
1299 for (; it != end; ++it) {
1300 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1301 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1308 int InsetText::linuxdoc(Buffer const * buf, ostream & os) const
1310 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1311 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1313 // There is a confusion between the empty paragraph and the default paragraph
1314 // The default paragraph is <p></p>, the empty paragraph is *empty*
1315 // Since none of the floats of linuxdoc accepts standard paragraphs
1316 // I disable them. I don't expect problems. (jamatos 2003/07/27)
1317 for (; pit != pend; ++pit) {
1318 const string name = pit->layout()->latexname();
1320 sgml::openTag(os, 1, 0, name);
1321 buf->simpleLinuxDocOnePar(os, pit, 0);
1323 sgml::closeTag(os, 1, 0, name);
1329 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1331 unsigned int lines = 0;
1333 vector<string> environment_stack(10);
1334 vector<string> environment_inner(10);
1336 int const command_depth = 0;
1339 Paragraph::depth_type depth = 0; // paragraph depth
1341 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1342 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1344 for (; pit != pend; ++pit) {
1346 int desc_on = 0; // description mode
1348 LyXLayout_ptr const & style = pit->layout();
1350 // environment tag closing
1351 for (; depth > pit->params().depth(); --depth) {
1352 if (environment_inner[depth] != "!-- --") {
1353 item_name = "listitem";
1354 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1355 if (environment_inner[depth] == "varlistentry")
1356 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1358 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1359 environment_stack[depth].erase();
1360 environment_inner[depth].erase();
1363 if (depth == pit->params().depth()
1364 && environment_stack[depth] != style->latexname()
1365 && !environment_stack[depth].empty()) {
1366 if (environment_inner[depth] != "!-- --") {
1367 item_name= "listitem";
1368 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1369 if (environment_inner[depth] == "varlistentry")
1370 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1373 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1375 environment_stack[depth].erase();
1376 environment_inner[depth].erase();
1379 // Write opening SGML tags.
1380 switch (style->latextype) {
1381 case LATEX_PARAGRAPH:
1382 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1386 buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1390 case LATEX_ENVIRONMENT:
1391 case LATEX_ITEM_ENVIRONMENT:
1392 if (depth < pit->params().depth()) {
1393 depth = pit->params().depth();
1394 environment_stack[depth].erase();
1397 if (environment_stack[depth] != style->latexname()) {
1398 if (environment_stack.size() == depth + 1) {
1399 environment_stack.push_back("!-- --");
1400 environment_inner.push_back("!-- --");
1402 environment_stack[depth] = style->latexname();
1403 environment_inner[depth] = "!-- --";
1404 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1406 if (environment_inner[depth] != "!-- --") {
1407 item_name= "listitem";
1408 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1409 if (environment_inner[depth] == "varlistentry")
1410 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1414 if (style->latextype == LATEX_ENVIRONMENT) {
1415 if (!style->latexparam().empty()) {
1416 if (style->latexparam() == "CDATA")
1419 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1424 desc_on = (style->labeltype == LABEL_MANUAL);
1426 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1427 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1429 item_name = desc_on ? "term" : "para";
1430 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1434 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1438 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1441 // write closing SGML tags
1442 switch (style->latextype) {
1443 case LATEX_ENVIRONMENT:
1444 if (!style->latexparam().empty()) {
1445 if (style->latexparam() == "CDATA")
1448 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1451 case LATEX_ITEM_ENVIRONMENT:
1452 if (desc_on == 1) break;
1454 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1456 case LATEX_PARAGRAPH:
1457 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1460 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1466 for (int d = depth; d >= 0; --d) {
1467 if (!environment_stack[depth].empty()) {
1468 if (environment_inner[depth] != "!-- --") {
1469 item_name = "listitem";
1470 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1471 if (environment_inner[depth] == "varlistentry")
1472 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1475 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1483 void InsetText::validate(LaTeXFeatures & features) const
1485 for_each(paragraphs.begin(), paragraphs.end(),
1486 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1490 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1492 if (the_locking_inset) {
1493 the_locking_inset->getCursor(bv, x, y);
1497 y = cy() + InsetText::y();
1501 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1503 if (the_locking_inset) {
1504 the_locking_inset->getCursorPos(bv, x, y);
1507 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1508 y = cy() - TEXT_TO_INSET_OFFSET;
1512 int InsetText::insetInInsetY() const
1514 if (!the_locking_inset)
1517 return inset_y + the_locking_inset->insetInInsetY();
1521 void InsetText::fitInsetCursor(BufferView * bv) const
1523 if (the_locking_inset) {
1524 the_locking_inset->fitInsetCursor(bv);
1528 LyXFont const font = text_.getFont(cpar(), cpos());
1530 int const asc = font_metrics::maxAscent(font);
1531 int const desc = font_metrics::maxDescent(font);
1533 if (bv->fitLockedInsetCursor(cx(), cy(), asc, desc))
1534 need_update |= FULL;
1539 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1541 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1542 return moveLeftIntern(bv, false, activate_inset, selecting);
1544 return moveRightIntern(bv, true, activate_inset, selecting);
1549 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1551 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1552 return moveRightIntern(bv, true, activate_inset, selecting);
1554 return moveLeftIntern(bv, false, activate_inset, selecting);
1559 InsetText::moveRightIntern(BufferView * bv, bool front,
1560 bool activate_inset, bool selecting)
1562 ParagraphList::iterator c_par = cpar();
1564 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1565 return FINISHED_RIGHT;
1566 if (activate_inset && checkAndActivateInset(bv, front))
1568 text_.cursorRight(bv);
1570 text_.clearSelection();
1571 return DISPATCHED_NOUPDATE;
1576 InsetText::moveLeftIntern(BufferView * bv, bool front,
1577 bool activate_inset, bool selecting)
1579 if (cpar() == paragraphs.begin() && cpos() <= 0)
1581 text_.cursorLeft(bv);
1583 text_.clearSelection();
1584 if (activate_inset && checkAndActivateInset(bv, front))
1586 return DISPATCHED_NOUPDATE;
1590 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1592 if (crow() == text_.rows().begin())
1595 text_.clearSelection();
1596 return DISPATCHED_NOUPDATE;
1600 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1602 if (boost::next(crow()) == text_.rows().end())
1603 return FINISHED_DOWN;
1604 text_.cursorDown(bv);
1605 text_.clearSelection();
1606 return DISPATCHED_NOUPDATE;
1610 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1612 if (the_locking_inset) {
1613 if (the_locking_inset->insetAllowed(inset))
1614 return the_locking_inset->insertInset(bv, inset);
1617 inset->setOwner(this);
1618 text_.insertInset(inset);
1620 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1625 bool InsetText::insetAllowed(InsetOld::Code code) const
1627 // in_insetAllowed is a really gross hack,
1628 // to allow us to call the owner's insetAllowed
1629 // without stack overflow, which can happen
1630 // when the owner uses InsetCollapsable::insetAllowed()
1632 if (in_insetAllowed)
1634 in_insetAllowed = true;
1635 if (the_locking_inset)
1636 ret = the_locking_inset->insetAllowed(code);
1638 ret = owner()->insetAllowed(code);
1639 in_insetAllowed = false;
1644 UpdatableInset * InsetText::getLockingInset() const
1646 return the_locking_inset ? the_locking_inset->getLockingInset() :
1647 const_cast<InsetText *>(this);
1651 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1655 if (the_locking_inset)
1656 return the_locking_inset->getFirstLockingInsetOfType(c);
1661 bool InsetText::showInsetDialog(BufferView * bv) const
1663 if (the_locking_inset)
1664 return the_locking_inset->showInsetDialog(bv);
1669 void InsetText::getLabelList(std::vector<string> & list) const
1671 ParagraphList::const_iterator pit = paragraphs.begin();
1672 ParagraphList::const_iterator pend = paragraphs.end();
1673 for (; pit != pend; ++pit) {
1674 InsetList::const_iterator beg = pit->insetlist.begin();
1675 InsetList::const_iterator end = pit->insetlist.end();
1676 for (; beg != end; ++beg)
1677 beg->inset->getLabelList(list);
1682 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1685 if (the_locking_inset) {
1686 the_locking_inset->setFont(bv, font, toggleall, selectall);
1690 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1691 || cpar()->empty()) {
1692 text_.setFont(font, toggleall);
1697 if (text_.selection.set())
1698 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1702 text_.selection.cursor = text_.cursor;
1703 text_.cursorBottom();
1704 text_.setSelection();
1707 text_.toggleFree(font, toggleall);
1710 text_.clearSelection();
1714 bool flag = (selectall || text_.selection.set());
1717 updateLocal(bv, FULL, true);
1719 updateLocal(bv, CURSOR_PAR, true);
1723 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1725 InsetOld * inset = cpar()->getInset(cpos());
1726 if (!isHighlyEditableInset(inset))
1728 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1729 inset->localDispatch(cmd);
1730 if (!the_locking_inset)
1732 updateLocal(bv, CURSOR, false);
1737 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1738 mouse_button::state button)
1740 x -= drawTextXOffset;
1742 int dummyy = y + dim_.asc;
1743 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1744 // we only do the edit() call if the inset was hit by the mouse
1745 // or if it is a highly editable inset. So we should call this
1746 // function from our own edit with button < 0.
1747 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1749 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1758 inset_x = cix() - top_x + drawTextXOffset;
1759 inset_y = ciy() + drawTextYOffset;
1760 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1761 inset->localDispatch(cmd);
1762 if (!the_locking_inset)
1764 updateLocal(bv, CURSOR, false);
1769 void InsetText::setParagraphData(ParagraphList const & plist)
1771 // we have to unlock any locked inset otherwise we're in troubles
1772 the_locking_inset = 0;
1774 // But it it makes no difference that is a lot better.
1776 // See if this can be simplified when std::list is in effect.
1779 ParagraphList::const_iterator it = plist.begin();
1780 ParagraphList::const_iterator end = plist.end();
1781 for (; it != end; ++it) {
1782 paragraphs.push_back(*it);
1783 paragraphs.back().setInsetOwner(this);
1791 void InsetText::markNew(bool track_changes)
1793 ParagraphList::iterator pit = paragraphs.begin();
1794 ParagraphList::iterator pend = paragraphs.end();
1795 for (; pit != pend; ++pit) {
1796 if (track_changes) {
1797 pit->trackChanges();
1799 // no-op when not tracking
1800 pit->cleanChanges();
1806 void InsetText::setText(string const & data, LyXFont const & font)
1809 for (unsigned int i = 0; i < data.length(); ++i)
1810 paragraphs.begin()->insertChar(i, data[i], font);
1815 void InsetText::setAutoBreakRows(bool flag)
1817 if (flag != autoBreakRows) {
1818 autoBreakRows = flag;
1826 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1828 if (how != drawFrame_) {
1831 updateLocal(bv, DRAW_FRAME, false);
1836 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1838 if (frame_color != col) {
1841 updateLocal(bv, DRAW_FRAME, false);
1846 int InsetText::cx() const
1848 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1849 if (the_locking_inset) {
1850 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1851 if (font.isVisibleRightToLeft())
1852 x -= the_locking_inset->width();
1858 int InsetText::cix() const
1860 int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1861 if (the_locking_inset) {
1862 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1863 if (font.isVisibleRightToLeft())
1864 x -= the_locking_inset->width();
1870 int InsetText::cy() const
1872 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1876 int InsetText::ciy() const
1878 return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1882 pos_type InsetText::cpos() const
1884 return text_.cursor.pos();
1888 ParagraphList::iterator InsetText::cpar() const
1890 return text_.cursor.par();
1894 bool InsetText::cboundary() const
1896 return text_.cursor.boundary();
1900 RowList::iterator InsetText::crow() const
1902 return text_.cursorRow();
1906 LyXText * InsetText::getLyXText(BufferView const * bv,
1907 bool const recursive) const
1910 if (recursive && the_locking_inset)
1911 return the_locking_inset->getLyXText(bv, true);
1916 void InsetText::setViewCache(BufferView const * bv) const
1919 text_.bv_owner = const_cast<BufferView *>(bv);
1923 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1926 /// then remove all LyXText in text-insets
1927 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1928 const_cast<ParagraphList&>(paragraphs).end(),
1929 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1934 void InsetText::resizeLyXText(BufferView * bv, bool /*force*/) const
1936 if (paragraphs.size() == 1 && paragraphs.begin()->empty()) {
1937 // no data, resize not neccessary!
1938 // we have to do this as a fixed width may have changed!
1941 restoreLyXTextState();
1953 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1954 const_cast<ParagraphList&>(paragraphs).end(),
1955 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1958 restoreLyXTextState();
1960 // seems to be unneeded
1962 if (the_locking_inset) {
1963 inset_x = cix() - top_x + drawTextXOffset;
1964 inset_y = ciy() + drawTextYOffset;
1967 text_.top_y(bv->screen().topCursorVisible(&text_));
1969 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
1970 // this will scroll the screen such that the cursor becomes visible
1971 bv->updateScrollbar();
1973 need_update |= FULL;
1979 void InsetText::reinitLyXText() const
1981 BufferView * bv = text_.bv_owner;
1988 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1989 const_cast<ParagraphList&>(paragraphs).end(),
1990 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1993 restoreLyXTextState();
1994 if (the_locking_inset) {
1995 inset_x = cix() - top_x + drawTextXOffset;
1996 inset_y = ciy() + drawTextYOffset;
1998 text_.top_y(bv->screen().topCursorVisible(&text_));
2000 // this will scroll the screen such that the cursor becomes visible
2001 bv->updateScrollbar();
2008 void InsetText::removeNewlines()
2010 bool changed = false;
2012 ParagraphList::iterator it = paragraphs.begin();
2013 ParagraphList::iterator end = paragraphs.end();
2014 for (; it != end; ++it) {
2015 for (int i = 0; i < it->size(); ++i) {
2016 if (it->isNewline(i)) {
2027 bool InsetText::nodraw() const
2029 if (the_locking_inset)
2030 return the_locking_inset->nodraw();
2031 return UpdatableInset::nodraw();
2035 int InsetText::scroll(bool recursive) const
2037 int sx = UpdatableInset::scroll(false);
2039 if (recursive && the_locking_inset)
2040 sx += the_locking_inset->scroll(recursive);
2046 void InsetText::clearSelection(BufferView * bv)
2048 getLyXText(bv)->clearSelection();
2052 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2054 Painter & pain = bv->painter();
2056 int h = dim_.asc + dim_.des;
2057 int ty = baseline - dim_.asc;
2063 if ((ty + h) > pain.paperHeight())
2064 h = pain.paperHeight();
2065 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2066 w = pain.paperWidth();
2067 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2072 ParagraphList * InsetText::getParagraphs(int i) const
2074 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2078 LyXCursor const & InsetText::cursor(BufferView * bv) const
2080 if (the_locking_inset)
2081 return the_locking_inset->cursor(bv);
2082 return getLyXText(bv)->cursor;
2086 InsetOld * InsetText::getInsetFromID(int id_arg) const
2089 return const_cast<InsetText *>(this);
2091 ParagraphList::const_iterator pit = paragraphs.begin();
2092 ParagraphList::const_iterator pend = paragraphs.end();
2093 for (; pit != pend; ++pit) {
2094 InsetList::const_iterator it = pit->insetlist.begin();
2095 InsetList::const_iterator end = pit->insetlist.end();
2096 for (; it != end; ++it) {
2097 if (it->inset->id() == id_arg)
2099 InsetOld * in = it->inset->getInsetFromID(id_arg);
2109 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2112 if (the_locking_inset) {
2113 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2114 if (!word.word().empty()) {
2118 // we have to go on checking so move cursor to the next char
2119 text_.cursor.pos(text_.cursor.pos() + 1);
2121 word = text_.selectNextWordToSpellcheck(value);
2122 if (word.word().empty())
2123 bv->unlockInset(const_cast<InsetText *>(this));
2130 void InsetText::selectSelectedWord(BufferView * bv)
2132 if (the_locking_inset) {
2133 the_locking_inset->selectSelectedWord(bv);
2136 getLyXText(bv)->selectSelectedWord();
2137 updateLocal(bv, SELECTION, false);
2141 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2143 if (the_locking_inset)
2144 the_locking_inset->toggleSelection(bv, kill_selection);
2146 int x = top_x + TEXT_TO_INSET_OFFSET;
2148 RowList::iterator rit = text_.rows().begin();
2149 RowList::iterator end = text_.rows().end();
2150 int y_offset = top_baseline - rit->ascent_of_text();
2152 while (rit != end && y + rit->height() <= 0) {
2159 if (need_update & SELECTION)
2161 bv->screen().toggleSelection(&text_, bv, kill_selection, y_offset, x);
2165 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2167 if (the_locking_inset) {
2168 if (the_locking_inset->nextChange(bv, length))
2170 text_.cursorRight(true);
2172 lyx::find::SearchResult result =
2173 lyx::find::findNextChange(bv, &text_, length);
2175 if (result == lyx::find::SR_FOUND) {
2176 LyXCursor cur = text_.cursor;
2177 bv->unlockInset(bv->theLockingInset());
2178 if (bv->lockInset(this))
2181 text_.setSelectionRange(length);
2182 updateLocal(bv, SELECTION, false);
2184 return result != lyx::find::SR_NOT_FOUND;
2188 bool InsetText::searchForward(BufferView * bv, string const & str,
2191 if (the_locking_inset) {
2192 if (the_locking_inset->searchForward(bv, str, cs, mw))
2194 text_.cursorRight(true);
2196 lyx::find::SearchResult result =
2197 lyx::find::find(bv, &text_, str, true, cs, mw);
2199 if (result == lyx::find::SR_FOUND) {
2200 LyXCursor cur = text_.cursor;
2201 bv->unlockInset(bv->theLockingInset());
2202 if (bv->lockInset(this))
2205 text_.setSelectionRange(str.length());
2206 updateLocal(bv, SELECTION, false);
2208 return result != lyx::find::SR_NOT_FOUND;
2212 bool InsetText::searchBackward(BufferView * bv, string const & str,
2215 if (the_locking_inset) {
2216 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2220 ParagraphList::iterator pit = paragraphs.begin();
2221 ParagraphList::iterator pend = paragraphs.end();
2223 while (boost::next(pit) != pend)
2226 text_.setCursor(pit, pit->size());
2228 lyx::find::SearchResult result =
2229 lyx::find::find(bv, &text_, str, false, cs, mw);
2231 if (result == lyx::find::SR_FOUND) {
2232 LyXCursor cur = text_.cursor;
2233 bv->unlockInset(bv->theLockingInset());
2234 if (bv->lockInset(this))
2237 text_.setSelectionRange(str.length());
2238 updateLocal(bv, SELECTION, false);
2240 return result != lyx::find::SR_NOT_FOUND;
2244 bool InsetText::checkInsertChar(LyXFont & font)
2247 return owner()->checkInsertChar(font);
2252 void InsetText::collapseParagraphs(BufferView * bv)
2254 while (paragraphs.size() > 1) {
2255 ParagraphList::iterator first_par = paragraphs.begin();
2256 ParagraphList::iterator next_par = boost::next(first_par);
2257 size_t const first_par_size = first_par->size();
2259 if (!first_par->empty() &&
2260 !next_par->empty() &&
2261 !first_par->isSeparator(first_par_size - 1)) {
2262 first_par->insertChar(first_par_size, ' ');
2265 if (text_.selection.set()) {
2266 if (text_.selection.start.par() == next_par) {
2267 text_.selection.start.par(first_par);
2268 text_.selection.start.pos(
2269 text_.selection.start.pos() + first_par_size);
2271 if (text_.selection.end.par() == next_par) {
2272 text_.selection.end.par(first_par);
2273 text_.selection.end.pos(
2274 text_.selection.end.pos() + first_par_size);
2278 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2284 void InsetText::getDrawFont(LyXFont & font) const
2288 owner()->getDrawFont(font);
2292 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2294 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2295 // And it probably does. You have to take a look at this John. (Lgb)
2296 #warning John, have a look here. (Lgb)
2297 ParagraphList::iterator pit = plist.begin();
2298 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2300 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2302 ParagraphList::iterator pend = plist.end();
2303 for (; pit != pend; ++pit) {
2304 paragraphs.push_back(*pit);
2311 void InsetText::addPreview(PreviewLoader & loader) const
2313 ParagraphList::const_iterator pit = paragraphs.begin();
2314 ParagraphList::const_iterator pend = paragraphs.end();
2316 for (; pit != pend; ++pit) {
2317 InsetList::const_iterator it = pit->insetlist.begin();
2318 InsetList::const_iterator end = pit->insetlist.end();
2319 for (; it != end; ++it) {
2320 it->inset->addPreview(loader);