3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS
13 #include "insettext.h"
16 #include "BufferView.h"
17 #include "bufferview_funcs.h"
18 #include "CutAndPaste.h"
20 #include "dimension.h"
21 #include "funcrequest.h"
23 #include "errorlist.h"
25 #include "LaTeXFeatures.h"
29 #include "lyxcursor.h"
35 #include "paragraph.h"
36 #include "ParagraphParameters.h"
37 #include "trans_mgr.h"
38 #include "undo_funcs.h"
39 #include "WordLangTuple.h"
40 #include "paragraph_funcs.h"
42 #include "rowpainter.h"
43 #include "insetnewline.h"
44 #include "metricsinfo.h"
46 #include "frontends/Alert.h"
47 #include "frontends/Dialogs.h"
48 #include "frontends/font_metrics.h"
49 #include "frontends/LyXView.h"
50 #include "frontends/Painter.h"
51 #include "frontends/screen.h"
53 #include "support/textutils.h"
54 #include "support/LAssert.h"
55 #include "support/lstrings.h"
56 #include "support/lyxalgo.h" // lyx::count
58 #include <boost/bind.hpp>
74 using namespace lyx::support;
75 using namespace bv_funcs;
78 using lyx::textclass_type;
80 namespace grfx = lyx::graphics;
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 InsetBase * InsetText::clone() const
210 return 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: " << getInsetName()
280 // << " width: " << mi.base.textwidth << "\n";
281 if (mi.base.textwidth)
282 textwidth_ = mi.base.textwidth;
283 BufferView * bv = mi.base.bv;
285 text_.rebuild(mi.base.textwidth);
286 dim.asc = text_.rows().begin()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
287 dim.des = text_.height - dim.asc + TEXT_TO_INSET_OFFSET;
288 dim.wid = max(textWidth(bv), int(text_.width)) + 2 * TEXT_TO_INSET_OFFSET;
289 dim.wid = max(dim.wid, 10);
294 int InsetText::textWidth(BufferView * bv, bool fordraw) const
297 int w = autoBreakRows ? getMaxWidth(bv, this) : -1;
300 return max(w - 2 * TEXT_TO_INSET_OFFSET,
301 (int)getLyXText(bv)->width);
306 return w - 2 * TEXT_TO_INSET_OFFSET;
307 lyxerr << "InsetText::textWidth: " << getInsetName()
308 << " " << textwidth_ << endl;
314 void InsetText::draw(PainterInfo & pi, int x, int baseline) const
319 // update our idea of where we are. Clearly, we should
320 // not have to know this information.
324 int const start_x = x;
326 BufferView * bv = pi.base.bv;
327 Painter & pain = pi.pain;
329 // repaint the background if needed
330 if (backgroundColor() != LColor::background)
331 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
333 // no draw is necessary !!!
334 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
335 top_baseline = baseline;
343 top_baseline = baseline;
344 top_y = baseline - dim_.asc;
346 if (last_drawn_width != dim_.wid) {
348 last_drawn_width = dim_.wid;
351 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
352 inset_x = cix(bv) - int(x) + drawTextXOffset;
353 inset_y = ciy() + drawTextYOffset;
356 x += TEXT_TO_INSET_OFFSET;
358 RowList::iterator rowit = text_.rows().begin();
359 RowList::iterator end = text_.rows().end();
361 int y_offset = baseline - rowit->ascent_of_text();
362 int ph = pain.paperHeight();
365 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
366 y += rowit->height();
367 first += rowit->height();
371 text_.top_y(-y_offset);
379 int yf = y_offset + first;
384 while (rowit != end && yf < ph) {
385 paintRows(*bv, text_, rowit,
386 y + y_offset + first, int(x), y + text_.top_y());
387 y += rowit->height();
388 yf += rowit->height();
394 if ((drawFrame_ == ALWAYS) || (drawFrame_ == LOCKED && locked)) {
395 drawFrame(pain, int(start_x));
398 if (need_update != INIT) {
404 void InsetText::drawFrame(Painter & pain, int x) const
406 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
408 frame_y = top_baseline - dim_.asc + ttoD2;
409 frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
410 frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
411 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
416 void InsetText::setUpdateStatus(int what) const
419 // we will to redraw us full if our LyXText wants it
420 if (text_.needRefresh())
423 // this to not draw a selection when we redraw all of it!
424 if (need_update & CURSOR && !(need_update & SELECTION)) {
425 if (text_.selection.set())
427 text_.clearSelection();
432 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
437 if (!autoBreakRows && paragraphs.size() > 1)
438 collapseParagraphs(bv);
440 text_.partialRebreak();
441 setUpdateStatus(what);
442 bool flag = mark_dirty ||
443 ((need_update != CURSOR && need_update != NONE) ||
444 text_.needRefresh() || text_.selection.set());
445 if (!text_.selection.set())
446 text_.selection.cursor = text_.cursor;
452 bv->updateInset(const_cast<InsetText *>(this));
455 if (need_update == CURSOR)
457 bv->owner()->view_state_changed();
458 bv->owner()->updateMenubar();
459 bv->owner()->updateToolbar();
460 if (old_par != cpar()) {
461 bv->owner()->setLayout(cpar()->layout()->name());
467 string const InsetText::editMessage() const
469 return _("Opened Text Inset");
473 void InsetText::insetUnlock(BufferView * bv)
475 if (the_locking_inset) {
476 the_locking_inset->insetUnlock(bv);
477 the_locking_inset = 0;
478 updateLocal(bv, CURSOR_PAR, false);
484 if (text_.selection.set()) {
485 text_.clearSelection();
487 } else if (owner()) {
488 bv->owner()->setLayout(owner()->getLyXText(bv)
489 ->cursor.par()->layout()->name());
491 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
492 // hack for deleteEmptyParMech
493 ParagraphList::iterator first_par = paragraphs.begin();
494 if (!first_par->empty()) {
495 text_.setCursor(first_par, 0);
496 } else if (paragraphs.size() > 1) {
497 text_.setCursor(boost::next(first_par), 0);
500 updateLocal(bv, code, false);
503 setUpdateStatus(code);
508 void InsetText::lockInset(BufferView * bv)
511 the_locking_inset = 0;
512 inset_pos = inset_x = inset_y = 0;
513 inset_boundary = false;
514 inset_par = paragraphs.end();
515 old_par = paragraphs.end();
516 text_.setCursorIntern(paragraphs.begin(), 0);
517 text_.clearSelection();
519 // If the inset is empty set the language of the current font to the
520 // language to the surronding text (if different).
521 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
522 bv->getParentLanguage(this) != text_.current_font.language()) {
523 LyXFont font(LyXFont::ALL_IGNORE);
524 font.setLanguage(bv->getParentLanguage(this));
525 setFont(bv, font, false);
528 if (drawFrame_ == LOCKED)
529 code = CURSOR|DRAW_FRAME;
530 setUpdateStatus(code);
534 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
536 the_locking_inset = inset;
537 inset_x = cix(bv) - top_x + drawTextXOffset;
538 inset_y = ciy() + drawTextYOffset;
541 inset_boundary = cboundary();
542 //updateLocal(bv, CURSOR, false);
546 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
548 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
552 if (!the_locking_inset) {
553 ParagraphList::iterator pit = paragraphs.begin();
554 ParagraphList::iterator pend = paragraphs.end();
556 int const id = inset->id();
557 for (; pit != pend; ++pit) {
558 InsetList::iterator it = pit->insetlist.begin();
559 InsetList::iterator const end = pit->insetlist.end();
560 for (; it != end; ++it) {
561 if (it->inset == inset) {
562 lyxerr << "InsetText::lockInsetInInset: 1 a\n";
563 text_.setCursorIntern(pit, it->pos);
564 lyxerr << "InsetText::lockInsetInInset: 1 b\n";
565 lyxerr << "bv: " << bv << " inset: " << inset << "\n";
566 lockInset(bv, inset);
567 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
570 if (it->inset->getInsetFromID(id)) {
571 lyxerr << "InsetText::lockInsetInInset: 2\n";
572 text_.setCursorIntern(pit, it->pos);
573 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
574 return the_locking_inset->lockInsetInInset(bv, inset);
578 lyxerr << "InsetText::lockInsetInInset: 3\n";
581 if (inset == cpar()->getInset(cpos())) {
582 lyxerr[Debug::INSETS] << "OK" << endl;
583 lockInset(bv, inset);
587 if (the_locking_inset && the_locking_inset == inset) {
588 if (cpar() == inset_par && cpos() == inset_pos) {
589 lyxerr[Debug::INSETS] << "OK" << endl;
590 inset_x = cix(bv) - top_x + drawTextXOffset;
591 inset_y = ciy() + drawTextYOffset;
593 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
595 } else if (the_locking_inset) {
596 lyxerr[Debug::INSETS] << "MAYBE" << endl;
597 return the_locking_inset->lockInsetInInset(bv, inset);
599 lyxerr[Debug::INSETS] << "NOT OK" << endl;
604 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
607 if (!the_locking_inset)
609 if (the_locking_inset == inset) {
610 the_locking_inset->insetUnlock(bv);
611 getLyXText(bv)->updateInset(inset);
612 the_locking_inset = 0;
614 moveRightIntern(bv, true, false);
615 old_par = paragraphs.end(); // force layout setting
619 updateLocal(bv, CURSOR, false);
622 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
626 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
628 if (!autoBreakRows && paragraphs.size() > 1)
629 collapseParagraphs(bv);
634 if (inset->owner() != this) {
635 int ustat = CURSOR_PAR;
637 UpdatableInset * tl_inset = the_locking_inset;
639 found = tl_inset->updateInsetInInset(bv, inset);
641 tl_inset = static_cast<UpdatableInset *>(inset);
642 while(tl_inset->owner() && tl_inset->owner() != this)
643 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
644 if (!tl_inset->owner())
646 found = tl_inset->updateInsetInInset(bv, inset);
650 text_.updateInset(tl_inset);
652 setUpdateStatus(ustat);
655 bool found = text_.updateInset(inset);
657 setUpdateStatus(CURSOR_PAR);
658 if (the_locking_inset &&
659 cpar() == inset_par && cpos() == inset_pos)
661 inset_x = cix(bv) - top_x + drawTextXOffset;
662 inset_y = ciy() + drawTextYOffset;
669 void InsetText::lfunMousePress(FuncRequest const & cmd)
673 // use this to check mouse motion for selection!
677 BufferView * bv = cmd.view();
678 FuncRequest cmd1 = cmd;
684 int tmp_x = cmd.x - drawTextXOffset;
685 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
686 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
688 if (the_locking_inset) {
689 if (the_locking_inset == inset) {
690 the_locking_inset->localDispatch(cmd1);
693 // otherwise only unlock the_locking_inset
694 the_locking_inset->insetUnlock(bv);
695 the_locking_inset = 0;
698 no_selection = false;
700 if (bv->theLockingInset()) {
701 if (isHighlyEditableInset(inset)) {
702 // We just have to lock the inset before calling a
704 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
705 if (!bv->lockInset(uinset)) {
706 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
708 inset->localDispatch(cmd1);
709 if (the_locking_inset)
710 updateLocal(bv, CURSOR, false);
715 bool paste_internally = false;
716 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
717 localDispatch(FuncRequest(bv, LFUN_COPY));
718 paste_internally = true;
720 int old_top_y = text_.top_y();
722 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
724 // set the selection cursor!
725 text_.selection.cursor = text_.cursor;
726 text_.cursor.x_fix(text_.cursor.x());
728 if (text_.selection.set()) {
729 text_.clearSelection();
730 updateLocal(bv, FULL, false);
732 text_.clearSelection();
733 updateLocal(bv, CURSOR, false);
736 bv->owner()->setLayout(cpar()->layout()->name());
738 // we moved the view we cannot do mouse selection in this case!
739 if (getLyXText(bv)->top_y() != old_top_y)
742 // Insert primary selection with middle mouse
743 // if there is a local selection in the current buffer,
745 if (cmd.button() == mouse_button::button2) {
746 if (paste_internally)
747 localDispatch(FuncRequest(bv, LFUN_PASTE));
749 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
752 getLyXText(bv)->clearSelection();
757 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
759 BufferView * bv = cmd.view();
760 FuncRequest cmd1 = cmd;
765 if (the_locking_inset)
766 return the_locking_inset->localDispatch(cmd1);
768 int tmp_x = cmd.x - drawTextXOffset;
769 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
770 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
773 // This code should probably be removed now. Simple insets
774 // (!highlyEditable) can actually take the localDispatch,
775 // and turn it into edit() if necessary. But we still
776 // need to deal properly with the whole relative vs.
777 // absolute mouse co-ords thing in a realiable, sensible way
779 if (isHighlyEditableInset(inset))
780 ret = inset->localDispatch(cmd1);
782 inset_x = cix(bv) - top_x + drawTextXOffset;
783 inset_y = ciy() + drawTextYOffset;
784 cmd1.x = cmd.x - inset_x;
785 cmd1.y = cmd.x - inset_y;
786 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
790 ret = inset->localDispatch(cmd1);
791 updateLocal(bv, CURSOR_PAR, false);
798 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
800 FuncRequest cmd1 = cmd;
804 if (the_locking_inset) {
805 the_locking_inset->localDispatch(cmd1);
809 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
812 BufferView * bv = cmd.view();
813 LyXCursor cur = text_.cursor;
814 text_.setCursorFromCoordinates
815 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
816 text_.cursor.x_fix(text_.cursor.x());
817 if (cur == text_.cursor)
819 text_.setSelection();
820 bool flag = (text_.toggle_cursor.par() != text_.toggle_end_cursor.par() ||
821 text_.toggle_cursor.pos() != text_.toggle_end_cursor.pos());
823 updateLocal(bv, SELECTION, false);
828 Inset::RESULT InsetText::localDispatch(FuncRequest const & cmd)
830 BufferView * bv = cmd.view();
833 if (cmd.action == LFUN_INSET_EDIT) {
834 UpdatableInset::localDispatch(cmd);
836 if (!bv->lockInset(this)) {
837 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
842 the_locking_inset = 0;
843 inset_pos = inset_x = inset_y = 0;
844 inset_boundary = false;
845 inset_par = paragraphs.end();
846 old_par = paragraphs.end();
849 if (cmd.argument.size()) {
850 if (cmd.argument == "left")
851 text_.setCursorIntern(paragraphs.begin(), 0);
853 ParagraphList::iterator it = paragraphs.begin();
854 ParagraphList::iterator end = paragraphs.end();
855 while (boost::next(it) != end)
857 // int const pos = (p->size() ? p->size()-1 : p->size());
858 text_.setCursor(it, it->size());
861 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
862 // we put here -1 and not button as now the button in the
863 // edit call should not be needed we will fix this in 1.3.x
864 // cycle hopefully (Jug 20020509)
865 // FIXME: GUII I've changed this to none: probably WRONG
866 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
867 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
869 text_.cursor.x_fix(text_.cursor.x());
873 text_.clearSelection();
876 // If the inset is empty set the language of the current font to the
877 // language to the surronding text (if different).
878 if (paragraphs.begin()->empty() &&
879 paragraphs.size() == 1 &&
880 bv->getParentLanguage(this) != text_.current_font.language())
882 LyXFont font(LyXFont::ALL_IGNORE);
883 font.setLanguage(bv->getParentLanguage(this));
884 setFont(bv, font, false);
888 if (drawFrame_ == LOCKED)
889 code = CURSOR | DRAW_FRAME;
891 updateLocal(bv, code, false);
892 // Tell the paragraph dialog that we've entered an insettext.
893 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
898 switch (cmd.action) {
899 case LFUN_MOUSE_PRESS:
902 case LFUN_MOUSE_MOTION:
903 lfunMouseMotion(cmd);
905 case LFUN_MOUSE_RELEASE:
906 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
911 bool was_empty = (paragraphs.begin()->empty() &&
912 paragraphs.size() == 1);
914 no_selection = false;
915 RESULT result = UpdatableInset::localDispatch(cmd);
916 if (result != UNDISPATCHED)
920 if (cmd.action < 0 && cmd.argument.empty())
923 if (the_locking_inset) {
924 result = the_locking_inset->localDispatch(cmd);
925 if (result == DISPATCHED_NOUPDATE)
927 else if (result == DISPATCHED) {
928 updateLocal(bv, CURSOR_PAR, false);
930 } else if (result >= FINISHED) {
933 moveRightIntern(bv, false, false);
937 if ((result = moveUp(bv)) >= FINISHED) {
938 updateLocal(bv, CURSOR, false);
939 bv->unlockInset(this);
943 if ((result = moveDown(bv)) >= FINISHED) {
944 updateLocal(bv, CURSOR, false);
945 bv->unlockInset(this);
952 the_locking_inset = 0;
953 updateLocal(bv, CURSOR, false);
954 // make sure status gets reset immediately
955 bv->owner()->clearMessage();
962 // what type of update to do on a cursor movement
963 int cursor_update = CURSOR;
965 if (text_.selection.set())
966 cursor_update = SELECTION;
968 switch (cmd.action) {
971 case LFUN_SELFINSERT:
972 if (bv->buffer()->isReadonly()) {
973 // setErrorMessage(N_("Document is read only"));
976 if (!cmd.argument.empty()) {
977 /* Automatically delete the currently selected
978 * text and replace it with what is being
979 * typed in now. Depends on lyxrc settings
980 * "auto_region_delete", which defaults to
983 // This should not be needed here and is also WRONG!
984 setUndo(bv, Undo::INSERT, text_.cursor.par());
987 if (lyxrc.auto_region_delete) {
988 if (text_.selection.set()) {
989 text_.cutSelection(false, false);
992 text_.clearSelection();
993 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
994 bv->owner()->getIntl().getTransManager().
995 TranslateAndInsert(cmd.argument[i], &text_);
998 text_.selection.cursor = text_.cursor;
999 updwhat = CURSOR | CURSOR_PAR;
1001 result = DISPATCHED_NOUPDATE;
1004 // cursor movements that need special handling
1007 result = moveRight(bv);
1009 updwhat = cursor_update;
1013 result = moveLeft(bv);
1014 updwhat = cursor_update;
1018 result = moveDown(bv);
1019 updwhat = cursor_update;
1023 result = moveUp(bv);
1024 updwhat = cursor_update;
1028 if (crow() == text_.rows().begin())
1029 result = FINISHED_UP;
1031 text_.cursorPrevious();
1032 text_.clearSelection();
1033 result = DISPATCHED_NOUPDATE;
1035 updwhat = cursor_update;
1039 if (boost::next(crow()) == text_.rows().end())
1040 result = FINISHED_DOWN;
1043 text_.clearSelection();
1044 result = DISPATCHED_NOUPDATE;
1046 updwhat = cursor_update;
1049 case LFUN_BACKSPACE: {
1050 if (text_.selection.set())
1051 text_.cutSelection(true, false);
1054 updwhat = CURSOR_PAR;
1060 if (text_.selection.set()) {
1061 text_.cutSelection(true, false);
1065 updwhat = CURSOR_PAR;
1071 text_.cutSelection(true, true);
1072 updwhat = CURSOR_PAR;
1079 text_.copySelection();
1080 updwhat = CURSOR_PAR;
1083 case LFUN_PASTESELECTION:
1085 string const clip(bv->getClipboard());
1089 if (cmd.argument == "paragraph") {
1090 text_.insertStringAsParagraphs(clip);
1092 text_.insertStringAsLines(clip);
1095 text_.clearSelection();
1097 updwhat = CURSOR_PAR;
1103 if (!autoBreakRows) {
1104 if (CutAndPaste::nrOfParagraphs() > 1) {
1105 #ifdef WITH_WARNINGS
1106 #warning FIXME horrendously bad UI
1108 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1113 replaceSelection(bv->getLyXText());
1114 size_t sel_index = 0;
1115 string const & arg = cmd.argument;
1116 if (isStrUnsignedInt(arg)) {
1117 size_t const paste_arg = strToUnsignedInt(arg);
1118 #warning FIXME Check if the arg is in the domain of available selections.
1119 sel_index = paste_arg;
1121 text_.pasteSelection(sel_index);
1123 text_.clearSelection();
1124 updwhat = CURSOR_PAR;
1129 case LFUN_BREAKPARAGRAPH:
1130 if (!autoBreakRows) {
1131 result = DISPATCHED;
1134 replaceSelection(bv->getLyXText());
1135 text_.breakParagraph(paragraphs, 0);
1136 updwhat = CURSOR | FULL;
1140 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1141 if (!autoBreakRows) {
1142 result = DISPATCHED;
1145 replaceSelection(bv->getLyXText());
1146 text_.breakParagraph(paragraphs, 1);
1147 updwhat = CURSOR | FULL;
1151 case LFUN_BREAKLINE: {
1152 if (!autoBreakRows) {
1153 result = DISPATCHED;
1157 replaceSelection(bv->getLyXText());
1158 text_.insertInset(new InsetNewline);
1159 updwhat = CURSOR | CURSOR_PAR;
1165 // do not set layouts on non breakable textinsets
1166 if (autoBreakRows) {
1167 string cur_layout = cpar()->layout()->name();
1169 // Derive layout number from given argument (string)
1170 // and current buffer's textclass (number). */
1171 LyXTextClass const & tclass =
1172 bv->buffer()->params.getLyXTextClass();
1173 string layout = cmd.argument;
1174 bool hasLayout = tclass.hasLayout(layout);
1176 // If the entry is obsolete, use the new one instead.
1178 string const & obs =
1179 tclass[layout]->obsoleted_by();
1184 // see if we found the layout number:
1186 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1187 bv->owner()->dispatch(lf);
1191 if (cur_layout != layout) {
1192 cur_layout = layout;
1193 text_.setLayout(layout);
1194 bv->owner()->setLayout(cpar()->layout()->name());
1195 updwhat = CURSOR_PAR;
1199 // reset the layout box
1200 bv->owner()->setLayout(cpar()->layout()->name());
1203 case LFUN_PARAGRAPH_SPACING:
1204 // This one is absolutely not working. When fiddling with this
1205 // it also seems to me that the paragraphs inside the insettext
1206 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1207 // FIXME: how old is this comment ? ...
1209 ParagraphList::iterator pit = text_.cursor.par();
1210 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1211 float cur_value = 1.0;
1212 if (cur_spacing == Spacing::Other) {
1213 cur_value = pit->params().spacing().getValue();
1216 istringstream istr(STRCONV(cmd.argument));
1219 Spacing::Space new_spacing = cur_spacing;
1220 float new_value = cur_value;
1222 lyxerr << "Missing argument to `paragraph-spacing'"
1224 } else if (tmp == "single") {
1225 new_spacing = Spacing::Single;
1226 } else if (tmp == "onehalf") {
1227 new_spacing = Spacing::Onehalf;
1228 } else if (tmp == "double") {
1229 new_spacing = Spacing::Double;
1230 } else if (tmp == "other") {
1231 new_spacing = Spacing::Other;
1234 lyxerr << "new_value = " << tmpval << endl;
1237 } else if (tmp == "default") {
1238 new_spacing = Spacing::Default;
1240 lyxerr << _("Unknown spacing argument: ")
1241 << cmd.argument << endl;
1243 if (cur_spacing != new_spacing || cur_value != new_value) {
1244 pit->params().spacing(Spacing(new_spacing, new_value));
1245 updwhat = CURSOR_PAR;
1251 // These need to do repaints but don't require
1252 // special handling otherwise. A *lot* of the
1253 // above could probably be done similarly ...
1258 case LFUN_WORDRIGHT:
1259 // these two are really unhandled ...
1261 case LFUN_BEGINNINGBUF:
1262 updwhat = cursor_update;
1263 if (!bv->dispatch(cmd))
1264 result = UNDISPATCHED;
1273 case LFUN_WORDLEFTSEL:
1274 case LFUN_WORDRIGHTSEL:
1275 updwhat = SELECTION;
1280 if (!bv->dispatch(cmd))
1281 result = UNDISPATCHED;
1286 updateLocal(bv, updwhat, updflag);
1287 /// If the action has deleted all text in the inset, we need to change the
1288 // language to the language of the surronding text.
1289 if (!was_empty && paragraphs.begin()->empty() &&
1290 paragraphs.size() == 1) {
1291 LyXFont font(LyXFont::ALL_IGNORE);
1292 font.setLanguage(bv->getParentLanguage(this));
1293 setFont(bv, font, false);
1296 if (result >= FINISHED)
1297 bv->unlockInset(this);
1299 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1300 result = DISPATCHED;
1305 int InsetText::latex(Buffer const * buf, ostream & os,
1306 LatexRunParams const & runparams) const
1309 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1310 return texrow.rows();
1314 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1316 unsigned int lines = 0;
1318 ParagraphList::const_iterator beg = paragraphs.begin();
1319 ParagraphList::const_iterator end = paragraphs.end();
1320 ParagraphList::const_iterator it = beg;
1321 for (; it != end; ++it) {
1322 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1323 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1330 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1332 unsigned int lines = 0;
1334 vector<string> environment_stack(10);
1335 vector<string> environment_inner(10);
1337 int const command_depth = 0;
1340 Paragraph::depth_type depth = 0; // paragraph depth
1342 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1343 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1345 for (; pit != pend; ++pit) {
1347 int desc_on = 0; // description mode
1349 LyXLayout_ptr const & style = pit->layout();
1351 // environment tag closing
1352 for (; depth > pit->params().depth(); --depth) {
1353 if (environment_inner[depth] != "!-- --") {
1354 item_name = "listitem";
1355 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1356 if (environment_inner[depth] == "varlistentry")
1357 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1359 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1360 environment_stack[depth].erase();
1361 environment_inner[depth].erase();
1364 if (depth == pit->params().depth()
1365 && environment_stack[depth] != style->latexname()
1366 && !environment_stack[depth].empty()) {
1367 if (environment_inner[depth] != "!-- --") {
1368 item_name= "listitem";
1369 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1370 if (environment_inner[depth] == "varlistentry")
1371 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1374 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1376 environment_stack[depth].erase();
1377 environment_inner[depth].erase();
1380 // Write opening SGML tags.
1381 switch (style->latextype) {
1382 case LATEX_PARAGRAPH:
1383 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1387 buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1391 case LATEX_ENVIRONMENT:
1392 case LATEX_ITEM_ENVIRONMENT:
1393 if (depth < pit->params().depth()) {
1394 depth = pit->params().depth();
1395 environment_stack[depth].erase();
1398 if (environment_stack[depth] != style->latexname()) {
1399 if (environment_stack.size() == depth + 1) {
1400 environment_stack.push_back("!-- --");
1401 environment_inner.push_back("!-- --");
1403 environment_stack[depth] = style->latexname();
1404 environment_inner[depth] = "!-- --";
1405 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1407 if (environment_inner[depth] != "!-- --") {
1408 item_name= "listitem";
1409 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1410 if (environment_inner[depth] == "varlistentry")
1411 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1415 if (style->latextype == LATEX_ENVIRONMENT) {
1416 if (!style->latexparam().empty()) {
1417 if (style->latexparam() == "CDATA")
1420 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1425 desc_on = (style->labeltype == LABEL_MANUAL);
1427 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1428 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1430 item_name = desc_on ? "term" : "para";
1431 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1435 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1439 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1442 // write closing SGML tags
1443 switch (style->latextype) {
1444 case LATEX_ENVIRONMENT:
1445 if (!style->latexparam().empty()) {
1446 if (style->latexparam() == "CDATA")
1449 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1452 case LATEX_ITEM_ENVIRONMENT:
1453 if (desc_on == 1) break;
1455 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1457 case LATEX_PARAGRAPH:
1458 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1461 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1467 for (int d = depth; d >= 0; --d) {
1468 if (!environment_stack[depth].empty()) {
1469 if (environment_inner[depth] != "!-- --") {
1470 item_name = "listitem";
1471 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1472 if (environment_inner[depth] == "varlistentry")
1473 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1476 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1484 void InsetText::validate(LaTeXFeatures & features) const
1486 for_each(paragraphs.begin(), paragraphs.end(),
1487 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1491 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1493 if (the_locking_inset) {
1494 the_locking_inset->getCursor(bv, x, y);
1498 y = cy() + InsetText::y();
1502 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1504 if (the_locking_inset) {
1505 the_locking_inset->getCursorPos(bv, x, y);
1508 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1509 y = cy() - TEXT_TO_INSET_OFFSET;
1513 int InsetText::insetInInsetY() const
1515 if (!the_locking_inset)
1518 return (inset_y + the_locking_inset->insetInInsetY());
1522 void InsetText::fitInsetCursor(BufferView * bv) const
1524 if (the_locking_inset) {
1525 the_locking_inset->fitInsetCursor(bv);
1528 LyXFont const font =
1529 getLyXText(bv)->getFont(bv->buffer(), cpar(), cpos());
1531 int const asc = font_metrics::maxAscent(font);
1532 int const desc = font_metrics::maxDescent(font);
1534 if (bv->fitLockedInsetCursor(cx(bv), cy(), asc, desc))
1535 need_update |= FULL;
1540 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1542 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1543 return moveLeftIntern(bv, false, activate_inset, selecting);
1545 return moveRightIntern(bv, true, activate_inset, selecting);
1550 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1552 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1553 return moveRightIntern(bv, true, activate_inset, selecting);
1555 return moveLeftIntern(bv, false, activate_inset, selecting);
1560 InsetText::moveRightIntern(BufferView * bv, bool front,
1561 bool activate_inset, bool selecting)
1563 ParagraphList::iterator c_par = cpar();
1565 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1566 return FINISHED_RIGHT;
1567 if (activate_inset && checkAndActivateInset(bv, front))
1569 text_.cursorRight(bv);
1571 text_.clearSelection();
1572 return DISPATCHED_NOUPDATE;
1577 InsetText::moveLeftIntern(BufferView * bv, bool front,
1578 bool activate_inset, bool selecting)
1580 if (cpar() == paragraphs.begin() && cpos() <= 0)
1582 text_.cursorLeft(bv);
1584 text_.clearSelection();
1585 if (activate_inset && checkAndActivateInset(bv, front))
1587 return DISPATCHED_NOUPDATE;
1591 Inset::RESULT InsetText::moveUp(BufferView * bv)
1593 if (crow() == text_.rows().begin())
1596 text_.clearSelection();
1597 return DISPATCHED_NOUPDATE;
1601 Inset::RESULT InsetText::moveDown(BufferView * bv)
1603 if (boost::next(crow()) == text_.rows().end())
1604 return FINISHED_DOWN;
1605 text_.cursorDown(bv);
1606 text_.clearSelection();
1607 return DISPATCHED_NOUPDATE;
1611 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1613 if (the_locking_inset) {
1614 if (the_locking_inset->insetAllowed(inset))
1615 return the_locking_inset->insertInset(bv, inset);
1618 inset->setOwner(this);
1619 getLyXText(bv)->insertInset(inset);
1621 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1626 bool InsetText::insetAllowed(Inset::Code code) const
1628 // in_insetAllowed is a really gross hack,
1629 // to allow us to call the owner's insetAllowed
1630 // without stack overflow, which can happen
1631 // when the owner uses InsetCollapsable::insetAllowed()
1633 if (in_insetAllowed)
1635 in_insetAllowed = true;
1636 if (the_locking_inset)
1637 ret = the_locking_inset->insetAllowed(code);
1639 ret = owner()->insetAllowed(code);
1640 in_insetAllowed = false;
1645 UpdatableInset * InsetText::getLockingInset() const
1647 return the_locking_inset ? the_locking_inset->getLockingInset() :
1648 const_cast<InsetText *>(this);
1652 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1656 if (the_locking_inset)
1657 return the_locking_inset->getFirstLockingInsetOfType(c);
1662 bool InsetText::showInsetDialog(BufferView * bv) const
1664 if (the_locking_inset)
1665 return the_locking_inset->showInsetDialog(bv);
1670 void InsetText::getLabelList(std::vector<string> & list) const
1672 ParagraphList::const_iterator pit = paragraphs.begin();
1673 ParagraphList::const_iterator pend = paragraphs.end();
1674 for (; pit != pend; ++pit) {
1675 InsetList::const_iterator beg = pit->insetlist.begin();
1676 InsetList::const_iterator end = pit->insetlist.end();
1677 for (; beg != end; ++beg)
1678 beg->inset->getLabelList(list);
1683 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1686 if (the_locking_inset) {
1687 the_locking_inset->setFont(bv, font, toggleall, selectall);
1691 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1692 || cpar()->empty()) {
1693 getLyXText(bv)->setFont(font, toggleall);
1698 if (text_.selection.set())
1699 setUndo(bv, Undo::EDIT, text_.cursor.par());
1703 text_.selection.cursor = text_.cursor;
1704 text_.cursorBottom();
1705 text_.setSelection();
1708 text_.toggleFree(font, toggleall);
1711 text_.clearSelection();
1715 bool flag = (selectall || text_.selection.set());
1718 updateLocal(bv, FULL, true);
1720 updateLocal(bv, CURSOR_PAR, true);
1724 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1726 if (cpar()->isInset(cpos())) {
1728 static_cast<UpdatableInset*>(cpar()->getInset(cpos()));
1729 if (!isHighlyEditableInset(inset))
1731 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1732 inset->localDispatch(cmd);
1733 if (!the_locking_inset)
1735 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 Inset * 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))
1762 inset_x = cix(bv) - top_x + drawTextXOffset;
1763 inset_y = ciy() + drawTextYOffset;
1764 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1765 inset->localDispatch(cmd);
1766 if (!the_locking_inset)
1768 updateLocal(bv, CURSOR, false);
1775 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1778 int w = UpdatableInset::getMaxWidth(bv, inset);
1783 w = w - top_x + owner()->x();
1786 w -= (2 * TEXT_TO_INSET_OFFSET);
1789 return UpdatableInset::getMaxWidth(bv, inset);
1794 void InsetText::setParagraphData(ParagraphList const & plist)
1796 // we have to unlock any locked inset otherwise we're in troubles
1797 the_locking_inset = 0;
1799 // But it it makes no difference that is a lot better.
1801 // See if this can be simplified when std::list is in effect.
1804 ParagraphList::const_iterator it = plist.begin();
1805 ParagraphList::const_iterator end = plist.end();
1806 for (; it != end; ++it) {
1807 paragraphs.push_back(*it);
1808 Paragraph & tmp = paragraphs.back();
1809 tmp.setInsetOwner(this);
1817 void InsetText::markNew(bool track_changes)
1819 ParagraphList::iterator pit = paragraphs.begin();
1820 ParagraphList::iterator pend = paragraphs.end();
1821 for (; pit != pend; ++pit) {
1822 if (track_changes) {
1823 pit->trackChanges();
1825 // no-op when not tracking
1826 pit->cleanChanges();
1832 void InsetText::setText(string const & data, LyXFont const & font)
1835 for (unsigned int i = 0; i < data.length(); ++i)
1836 paragraphs.begin()->insertChar(i, data[i], font);
1841 void InsetText::setAutoBreakRows(bool flag)
1843 if (flag != autoBreakRows) {
1844 autoBreakRows = flag;
1852 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1854 if (how != drawFrame_) {
1857 updateLocal(bv, DRAW_FRAME, false);
1862 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1864 if (frame_color != col) {
1867 updateLocal(bv, DRAW_FRAME, false);
1872 int InsetText::cx(BufferView * bv) const
1874 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1875 if (the_locking_inset) {
1876 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1877 text_.cursor.pos());
1878 if (font.isVisibleRightToLeft())
1879 x -= the_locking_inset->width();
1885 int InsetText::cix(BufferView * bv) const
1887 int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1888 if (the_locking_inset) {
1889 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1890 text_.cursor.pos());
1891 if (font.isVisibleRightToLeft())
1892 x -= the_locking_inset->width();
1898 int InsetText::cy() const
1900 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1904 int InsetText::ciy() const
1906 return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1910 pos_type InsetText::cpos() const
1912 return text_.cursor.pos();
1916 ParagraphList::iterator InsetText::cpar() const
1918 return text_.cursor.par();
1922 bool InsetText::cboundary() const
1924 return text_.cursor.boundary();
1928 RowList::iterator InsetText::crow() const
1930 return text_.cursorRow();
1934 LyXText * InsetText::getLyXText(BufferView const * bv,
1935 bool const recursive) const
1938 if (recursive && the_locking_inset)
1939 return the_locking_inset->getLyXText(bv, true);
1944 void InsetText::setViewCache(BufferView const * bv) const
1947 text_.bv_owner = const_cast<BufferView *>(bv);
1951 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1954 /// then remove all LyXText in text-insets
1955 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1956 const_cast<ParagraphList&>(paragraphs).end(),
1957 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1962 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1964 if (paragraphs.size() == 1 && paragraphs.begin()->empty()) {
1965 // no data, resize not neccessary!
1966 // we have to do this as a fixed width may have changed!
1969 restoreLyXTextState();
1979 // one endless line, resize normally not necessary
1980 if (!force && getMaxWidth(bv, this) < 0)
1985 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1986 const_cast<ParagraphList&>(paragraphs).end(),
1987 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1990 restoreLyXTextState();
1992 if (the_locking_inset) {
1993 inset_x = cix(bv) - top_x + drawTextXOffset;
1994 inset_y = ciy() + drawTextYOffset;
1997 text_.top_y(bv->screen().topCursorVisible(&text_));
1999 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2000 // this will scroll the screen such that the cursor becomes visible
2001 bv->updateScrollbar();
2003 need_update |= FULL;
2008 void InsetText::reinitLyXText() const
2010 BufferView * bv = text_.bv_owner;
2017 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2018 const_cast<ParagraphList&>(paragraphs).end(),
2019 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2022 restoreLyXTextState();
2023 if (the_locking_inset) {
2024 inset_x = cix(bv) - top_x + drawTextXOffset;
2025 inset_y = ciy() + drawTextYOffset;
2027 text_.top_y(bv->screen().topCursorVisible(&text_));
2029 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2030 // this will scroll the screen such that the cursor becomes visible
2031 bv->updateScrollbar();
2038 void InsetText::removeNewlines()
2040 bool changed = false;
2042 ParagraphList::iterator it = paragraphs.begin();
2043 ParagraphList::iterator end = paragraphs.end();
2044 for (; it != end; ++it) {
2045 for (int i = 0; i < it->size(); ++i) {
2046 if (it->isNewline(i)) {
2057 bool InsetText::nodraw() const
2059 if (the_locking_inset)
2060 return the_locking_inset->nodraw();
2061 return UpdatableInset::nodraw();
2065 int InsetText::scroll(bool recursive) const
2067 int sx = UpdatableInset::scroll(false);
2069 if (recursive && the_locking_inset)
2070 sx += the_locking_inset->scroll(recursive);
2076 void InsetText::clearSelection(BufferView * bv)
2078 getLyXText(bv)->clearSelection();
2082 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2084 Painter & pain = bv->painter();
2086 int h = dim_.asc + dim_.des;
2087 int ty = baseline - dim_.asc;
2093 if ((ty + h) > pain.paperHeight())
2094 h = pain.paperHeight();
2095 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2096 w = pain.paperWidth();
2097 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2102 ParagraphList * InsetText::getParagraphs(int i) const
2104 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2108 LyXCursor const & InsetText::cursor(BufferView * bv) const
2110 if (the_locking_inset)
2111 return the_locking_inset->cursor(bv);
2112 return getLyXText(bv)->cursor;
2116 Inset * InsetText::getInsetFromID(int id_arg) const
2119 return const_cast<InsetText *>(this);
2121 ParagraphList::const_iterator pit = paragraphs.begin();
2122 ParagraphList::const_iterator pend = paragraphs.end();
2123 for (; pit != pend; ++pit) {
2124 InsetList::const_iterator it = pit->insetlist.begin();
2125 InsetList::const_iterator end = pit->insetlist.end();
2126 for (; it != end; ++it) {
2127 if (it->inset->id() == id_arg)
2129 Inset * in = it->inset->getInsetFromID(id_arg);
2139 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2142 if (the_locking_inset) {
2143 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2144 if (!word.word().empty()) {
2148 // we have to go on checking so move cursor to the next char
2149 text_.cursor.pos(text_.cursor.pos() + 1);
2151 word = text_.selectNextWordToSpellcheck(value);
2152 if (word.word().empty())
2153 bv->unlockInset(const_cast<InsetText *>(this));
2160 void InsetText::selectSelectedWord(BufferView * bv)
2162 if (the_locking_inset) {
2163 the_locking_inset->selectSelectedWord(bv);
2166 getLyXText(bv)->selectSelectedWord();
2167 updateLocal(bv, SELECTION, false);
2171 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2173 if (the_locking_inset) {
2174 the_locking_inset->toggleSelection(bv, kill_selection);
2177 int x = top_x + TEXT_TO_INSET_OFFSET;
2179 RowList::iterator rowit = text_.rows().begin();
2180 RowList::iterator end = text_.rows().end();
2181 int y_offset = top_baseline - rowit->ascent_of_text();
2183 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2184 y += rowit->height();
2190 if (need_update & SELECTION)
2192 bv->screen().toggleSelection(&text_, bv, kill_selection, y_offset, x);
2196 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2198 if (the_locking_inset) {
2199 if (the_locking_inset->nextChange(bv, length))
2201 text_.cursorRight(true);
2203 lyxfind::SearchResult result =
2204 lyxfind::findNextChange(bv, &text_, length);
2206 if (result == lyxfind::SR_FOUND) {
2207 LyXCursor cur = text_.cursor;
2208 bv->unlockInset(bv->theLockingInset());
2209 if (bv->lockInset(this))
2212 text_.setSelectionRange(length);
2213 updateLocal(bv, SELECTION, false);
2215 return result != lyxfind::SR_NOT_FOUND;
2219 bool InsetText::searchForward(BufferView * bv, string const & str,
2222 if (the_locking_inset) {
2223 if (the_locking_inset->searchForward(bv, str, cs, mw))
2225 text_.cursorRight(true);
2227 lyxfind::SearchResult result =
2228 lyxfind::LyXFind(bv, &text_, str, true, cs, mw);
2230 if (result == lyxfind::SR_FOUND) {
2231 LyXCursor cur = text_.cursor;
2232 bv->unlockInset(bv->theLockingInset());
2233 if (bv->lockInset(this))
2236 text_.setSelectionRange(str.length());
2237 updateLocal(bv, SELECTION, false);
2239 return (result != lyxfind::SR_NOT_FOUND);
2242 bool InsetText::searchBackward(BufferView * bv, string const & str,
2245 if (the_locking_inset) {
2246 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2250 ParagraphList::iterator pit = paragraphs.begin();
2251 ParagraphList::iterator pend = paragraphs.end();
2253 while (boost::next(pit) != pend)
2256 text_.setCursor(pit, pit->size());
2258 lyxfind::SearchResult result =
2259 lyxfind::LyXFind(bv, &text_, str, false, cs, mw);
2261 if (result == lyxfind::SR_FOUND) {
2262 LyXCursor cur = text_.cursor;
2263 bv->unlockInset(bv->theLockingInset());
2264 if (bv->lockInset(this))
2267 text_.setSelectionRange(str.length());
2268 updateLocal(bv, SELECTION, false);
2270 return (result != lyxfind::SR_NOT_FOUND);
2274 bool InsetText::checkInsertChar(LyXFont & font)
2277 return owner()->checkInsertChar(font);
2282 void InsetText::collapseParagraphs(BufferView * bv)
2284 while (paragraphs.size() > 1) {
2285 ParagraphList::iterator first_par = paragraphs.begin();
2286 ParagraphList::iterator next_par = boost::next(first_par);
2287 size_t const first_par_size = first_par->size();
2289 if (!first_par->empty() &&
2290 !next_par->empty() &&
2291 !first_par->isSeparator(first_par_size - 1)) {
2292 first_par->insertChar(first_par_size, ' ');
2295 if (text_.selection.set()) {
2296 if (text_.selection.start.par() == next_par) {
2297 text_.selection.start.par(first_par);
2298 text_.selection.start.pos(
2299 text_.selection.start.pos() + first_par_size);
2301 if (text_.selection.end.par() == next_par) {
2302 text_.selection.end.par(first_par);
2303 text_.selection.end.pos(
2304 text_.selection.end.pos() + first_par_size);
2308 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2314 void InsetText::getDrawFont(LyXFont & font) const
2318 owner()->getDrawFont(font);
2322 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2324 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2325 // And it probably does. You have to take a look at this John. (Lgb)
2326 #warning John, have a look here. (Lgb)
2327 ParagraphList::iterator pit = plist.begin();
2328 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2330 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2332 ParagraphList::iterator pend = plist.end();
2333 for (; pit != pend; ++pit) {
2334 paragraphs.push_back(*pit);
2341 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2343 ParagraphList::const_iterator pit = paragraphs.begin();
2344 ParagraphList::const_iterator pend = paragraphs.end();
2346 for (; pit != pend; ++pit) {
2347 InsetList::const_iterator it = pit->insetlist.begin();
2348 InsetList::const_iterator end = pit->insetlist.end();
2349 for (; it != end; ++it) {
2350 it->inset->addPreview(loader);