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
14 #include "insettext.h"
17 #include "BufferView.h"
18 #include "CutAndPaste.h"
20 #include "funcrequest.h"
23 #include "LaTeXFeatures.h"
26 #include "lyxcursor.h"
32 #include "paragraph.h"
33 #include "ParagraphParameters.h"
34 #include "trans_mgr.h"
35 #include "undo_funcs.h"
36 #include "WordLangTuple.h"
37 #include "paragraph_funcs.h"
39 #include "rowpainter.h"
40 #include "insetnewline.h"
42 #include "frontends/Alert.h"
43 #include "frontends/Dialogs.h"
44 #include "frontends/font_metrics.h"
45 #include "frontends/LyXView.h"
46 #include "frontends/Painter.h"
47 #include "frontends/screen.h"
49 #include "support/textutils.h"
50 #include "support/LAssert.h"
51 #include "support/lstrings.h"
52 #include "support/lyxalgo.h" // lyx::count
54 #include <boost/bind.hpp>
72 using lyx::textclass_type;
74 // These functions should probably go into bufferview_funcs somehow (Jug)
76 void InsetText::saveLyXTextState(LyXText * t) const
78 // check if my paragraphs are still valid
79 ParagraphList::iterator it = paragraphs.begin();
80 ParagraphList::iterator end = paragraphs.end();
81 for (; it != end; ++it) {
82 if (&*it == t->cursor.par())
86 if (it != end && t->cursor.pos() <= it->size()) {
87 sstate.lpar = &*t->cursor.par();
88 sstate.pos = t->cursor.pos();
89 sstate.boundary = t->cursor.boundary();
90 sstate.selstartpar = &*t->selection.start.par();
91 sstate.selstartpos = t->selection.start.pos();
92 sstate.selstartboundary = t->selection.start.boundary();
93 sstate.selendpar = &*t->selection.end.par();
94 sstate.selendpos = t->selection.end.pos();
95 sstate.selendboundary = t->selection.end.boundary();
96 sstate.selection = t->selection.set();
97 sstate.mark_set = t->selection.mark();
104 void InsetText::restoreLyXTextState(LyXText * t) const
109 t->selection.set(true);
110 /* at this point just to avoid the DEPM when setting the cursor */
111 t->selection.mark(sstate.mark_set);
112 if (sstate.selection) {
113 t->setCursor(sstate.selstartpar, sstate.selstartpos,
114 true, sstate.selstartboundary);
115 t->selection.cursor = t->cursor;
116 t->setCursor(sstate.selendpar, sstate.selendpos,
117 true, sstate.selendboundary);
119 t->setCursor(sstate.lpar, sstate.pos);
121 t->setCursor(sstate.lpar, sstate.pos, true, sstate.boundary);
122 t->selection.cursor = t->cursor;
123 t->selection.set(false);
128 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
135 InsetText::InsetText(BufferParams const & bp)
136 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
139 paragraphs.set(new Paragraph);
140 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
141 if (bp.tracking_changes)
142 paragraphs.begin()->trackChanges();
147 InsetText::InsetText(InsetText const & in, bool same_id)
148 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
155 InsetText & InsetText::operator=(InsetText const & it)
162 void InsetText::init(InsetText const * ins, bool same_id)
165 setParagraphData(&*(ins->paragraphs.begin()), same_id);
166 autoBreakRows = ins->autoBreakRows;
167 drawFrame_ = ins->drawFrame_;
168 frame_color = ins->frame_color;
172 for_each(paragraphs.begin(), paragraphs.end(),
173 boost::bind(&Paragraph::setInsetOwner, _1, this));
175 the_locking_inset = 0;
177 frame_color = LColor::insetframe;
178 autoBreakRows = false;
191 last_drawn_width = -1;
194 in_insetAllowed = false;
198 InsetText::~InsetText()
204 void InsetText::clear(bool just_mark_erased)
206 if (just_mark_erased) {
207 ParagraphList::iterator it = paragraphs.begin();
208 ParagraphList::iterator end = paragraphs.end();
209 for (; it != end; ++it) {
216 // This is a gross hack...
217 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
220 paragraphs.set(new Paragraph);
221 paragraphs.begin()->setInsetOwner(this);
222 paragraphs.begin()->layout(old_layout);
229 Inset * InsetText::clone(Buffer const &, bool same_id) const
231 return new InsetText(*this, same_id);
235 void InsetText::write(Buffer const * buf, ostream & os) const
238 writeParagraphData(buf, os);
242 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
244 ParagraphList::iterator it = paragraphs.begin();
245 ParagraphList::iterator end = paragraphs.end();
246 Paragraph::depth_type dth = 0;
247 for (; it != end; ++it) {
248 it->write(buf, os, buf->params, dth);
253 void InsetText::read(Buffer const * buf, LyXLex & lex)
256 Paragraph::depth_type depth = 0;
260 if (buf->params.tracking_changes)
261 paragraphs.begin()->trackChanges();
263 // delete the initial paragraph
265 ParagraphList::iterator pit = paragraphs.begin();
269 token = lex.getString();
272 if (token == "\\end_inset") {
276 if (token == "\\the_end") {
277 lex.printError("\\the_end read in inset! Error in document!");
282 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
285 pit = paragraphs.begin();
286 ParagraphList::iterator const end = paragraphs.end();
287 for (; pit != end; ++pit)
288 pit->setInsetOwner(this);
290 if (token != "\\end_inset") {
291 lex.printError("Missing \\end_inset at this point. "
298 int InsetText::ascent(BufferView * bv, LyXFont const &) const
300 insetAscent = getLyXText(bv)->rows().begin()->ascent_of_text() +
301 TEXT_TO_INSET_OFFSET;
306 int InsetText::descent(BufferView * bv, LyXFont const &) const
308 LyXText * llt = getLyXText(bv);
309 insetDescent = llt->height - llt->rows().begin()->ascent_of_text() +
310 TEXT_TO_INSET_OFFSET;
315 int InsetText::width(BufferView * bv, LyXFont const &) const
317 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
318 (2 * TEXT_TO_INSET_OFFSET);
319 insetWidth = max(insetWidth, 10);
324 int InsetText::textWidth(BufferView * bv, bool fordraw) const
327 if (!autoBreakRows) {
330 w = getMaxWidth(bv, this);
333 return max(w - (2 * TEXT_TO_INSET_OFFSET),
334 (int)getLyXText(bv)->width);
338 return w - (2 * TEXT_TO_INSET_OFFSET);
342 void InsetText::draw(BufferView * bv, LyXFont const & f,
343 int baseline, float & x) const
348 // update our idea of where we are. Clearly, we should
349 // not have to know this information.
353 int const start_x = int(x);
355 Painter & pain = bv->painter();
357 // call these methods so that insetWidth, insetAscent and
358 // insetDescent have the right values.
363 // repaint the background if needed
364 if (backgroundColor() != LColor::background)
365 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
367 // no draw is necessary !!!
368 if ((drawFrame_ == LOCKED) && !locked && paragraphs.begin()->empty()) {
369 top_baseline = baseline;
376 x += static_cast<float>(scroll());
378 top_baseline = baseline;
379 top_y = baseline - insetAscent;
381 if (last_drawn_width != insetWidth) {
383 last_drawn_width = insetWidth;
386 if (the_locking_inset && (cpar(bv) == inset_par)
387 && (cpos(bv) == inset_pos)) {
388 inset_x = cix(bv) - int(x) + drawTextXOffset;
389 inset_y = ciy(bv) + drawTextYOffset;
397 x += TEXT_TO_INSET_OFFSET;
399 RowList::iterator rowit = lt->rows().begin();
400 RowList::iterator end = lt->rows().end();
402 int y_offset = baseline - rowit->ascent_of_text();
403 int ph = pain.paperHeight();
406 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
407 y += rowit->height();
408 first += rowit->height();
412 lt->top_y(-y_offset);
420 int yf = y_offset + first;
422 while ((rowit != end) && (yf < ph)) {
423 RowPainter rp(*bv, *lt, rowit);
424 rp.paint(y + y_offset + first, int(x), y + lt->top_y());
425 y += rowit->height();
426 yf += rowit->height();
432 if ((drawFrame_ == ALWAYS) || (drawFrame_ == LOCKED && locked)) {
433 drawFrame(pain, int(start_x));
436 x += insetWidth - TEXT_TO_INSET_OFFSET;
438 if (need_update != INIT) {
446 void InsetText::drawFrame(Painter & pain, int x) const
448 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
450 frame_y = top_baseline - insetAscent + ttoD2;
451 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
452 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
453 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
458 void InsetText::update(BufferView * bv, bool reinit)
461 if (reinit && owner()) {
463 owner()->update(bv, true);
469 if (reinit || need_update == INIT) {
471 // we should put this call where we set need_update to INIT!
474 owner()->update(bv, true);
477 int nw = getMaxWidth(bv, this);
478 if (nw > 0 && old_max_width != nw) {
485 if (!autoBreakRows &&
486 boost::next(paragraphs.begin()) != paragraphs.end())
487 collapseParagraphs(bv);
489 if (the_locking_inset) {
490 inset_x = cix(bv) - top_x + drawTextXOffset;
491 inset_y = ciy(bv) + drawTextYOffset;
492 the_locking_inset->update(bv, reinit);
500 if ((need_update & CURSOR_PAR) && (lt->refreshStatus() == LyXText::REFRESH_NONE) &&
502 lt->updateInset(the_locking_inset);
505 if (lt->refreshStatus() == LyXText::REFRESH_AREA)
512 int nw = getMaxWidth(bv, this);
513 if (nw > 0 && old_max_width != nw) {
520 void InsetText::setUpdateStatus(BufferView * bv, int what) const
522 // this does nothing dangerous so use only a localized buffer
523 LyXText * llt = getLyXText(bv);
526 // we have to redraw us full if our LyXText REFRESH_AREA or
527 // if we don't break row so that we only have one row to update!
528 if ((llt->refreshStatus() == LyXText::REFRESH_AREA) ||
530 (llt->refreshStatus() == LyXText::REFRESH_ROW)))
533 } else if (llt->refreshStatus() == LyXText::REFRESH_ROW) {
534 need_update |= CURSOR_PAR;
537 // this to not draw a selection when we redraw all of it!
538 if (need_update & CURSOR && !(need_update & SELECTION)) {
539 if (llt->selection.set())
541 llt->clearSelection();
546 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
548 if (!autoBreakRows &&
549 boost::next(paragraphs.begin()) != paragraphs.end())
550 collapseParagraphs(bv);
557 setUpdateStatus(bv, what);
558 bool flag = mark_dirty ||
559 (((need_update != CURSOR) && (need_update != NONE)) ||
560 (lt->refreshStatus() != LyXText::REFRESH_NONE) || lt->selection.set());
561 if (!lt->selection.set())
562 lt->selection.cursor = lt->cursor;
568 bv->updateInset(const_cast<InsetText *>(this));
574 if (need_update == CURSOR)
576 bv->owner()->view_state_changed();
577 bv->owner()->updateMenubar();
578 bv->owner()->updateToolbar();
579 if (old_par != cpar(bv)) {
580 bv->owner()->setLayout(cpar(bv)->layout()->name());
586 string const InsetText::editMessage() const
588 return _("Opened Text Inset");
592 void InsetText::edit(BufferView * bv, int x, int y, mouse_button::state button)
594 UpdatableInset::edit(bv, x, y, button);
596 if (!bv->lockInset(this)) {
597 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
601 the_locking_inset = 0;
602 inset_pos = inset_x = inset_y = 0;
603 inset_boundary = false;
606 int tmp_y = (y < 0) ? 0 : y;
612 // we put here -1 and not button as now the button in the
613 // edit call should not be needed we will fix this in 1.3.x
614 // cycle hopefully (Jug 20020509)
615 // FIXME: GUII I've changed this to none: probably WRONG
616 if (!checkAndActivateInset(bv, x, tmp_y, mouse_button::none)) {
617 lt->setCursorFromCoordinates(x - drawTextXOffset,
619 lt->cursor.x_fix(lt->cursor.x());
621 lt->clearSelection();
623 // If the inset is empty set the language of the current font to the
624 // language to the surronding text (if different).
625 if (paragraphs.begin()->empty() &&
626 boost::next(paragraphs.begin()) == paragraphs.end()&&
627 bv->getParentLanguage(this) != lt->current_font.language())
629 LyXFont font(LyXFont::ALL_IGNORE);
630 font.setLanguage(bv->getParentLanguage(this));
631 setFont(bv, font, false);
637 if (drawFrame_ == LOCKED)
638 code = CURSOR|DRAW_FRAME;
639 updateLocal(bv, code, false);
642 // Tell the paragraph dialog that we've entered an insettext.
643 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
647 void InsetText::edit(BufferView * bv, bool front)
649 UpdatableInset::edit(bv, front);
651 if (!bv->lockInset(this)) {
652 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
656 the_locking_inset = 0;
657 inset_pos = inset_x = inset_y = 0;
658 inset_boundary = false;
667 lt->setCursor(&*(paragraphs.begin()), 0);
669 ParagraphList::iterator it = paragraphs.begin();
670 ParagraphList::iterator end = paragraphs.end();
671 while (boost::next(it) != end)
673 // int const pos = (p->size() ? p->size()-1 : p->size());
674 lt->setCursor(&*it, it->size());
676 lt->clearSelection();
678 // If the inset is empty set the language of the current font to the
679 // language to the surronding text (if different).
680 if (paragraphs.begin()->empty() &&
681 boost::next(paragraphs.begin()) == paragraphs.end() &&
682 bv->getParentLanguage(this) != lt->current_font.language()) {
683 LyXFont font(LyXFont::ALL_IGNORE);
684 font.setLanguage(bv->getParentLanguage(this));
685 setFont(bv, font, false);
690 if (drawFrame_ == LOCKED)
691 code = CURSOR|DRAW_FRAME;
692 updateLocal(bv, code, false);
697 void InsetText::insetUnlock(BufferView * bv)
699 if (the_locking_inset) {
700 the_locking_inset->insetUnlock(bv);
701 the_locking_inset = 0;
702 updateLocal(bv, CURSOR_PAR, false);
714 if (lt->selection.set()) {
715 lt->clearSelection();
717 } else if (owner()) {
718 bv->owner()->setLayout(owner()->getLyXText(bv)
719 ->cursor.par()->layout()->name());
721 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
722 // hack for deleteEmptyParMech
723 if (!paragraphs.begin()->empty()) {
724 lt->setCursor(&*(paragraphs.begin()), 0);
725 } else if (boost::next(paragraphs.begin()) != paragraphs.end()) {
726 lt->setCursor(&*boost::next(paragraphs.begin()), 0);
731 updateLocal(bv, code, false);
734 setUpdateStatus(bv, code);
739 void InsetText::lockInset(BufferView * bv)
742 the_locking_inset = 0;
743 inset_pos = inset_x = inset_y = 0;
744 inset_boundary = false;
752 lt->setCursor(&*(paragraphs.begin()), 0);
753 lt->clearSelection();
755 // If the inset is empty set the language of the current font to the
756 // language to the surronding text (if different).
757 if (paragraphs.begin()->empty() &&
758 boost::next(paragraphs.begin()) == paragraphs.end() &&
759 bv->getParentLanguage(this) != lt->current_font.language()) {
760 LyXFont font(LyXFont::ALL_IGNORE);
761 font.setLanguage(bv->getParentLanguage(this));
762 setFont(bv, font, false);
767 if (drawFrame_ == LOCKED)
768 code = CURSOR|DRAW_FRAME;
769 setUpdateStatus(bv, code);
773 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
775 the_locking_inset = inset;
776 inset_x = cix(bv) - top_x + drawTextXOffset;
777 inset_y = ciy(bv) + drawTextYOffset;
778 inset_pos = cpos(bv);
779 inset_par = cpar(bv);
780 inset_boundary = cboundary(bv);
781 updateLocal(bv, CURSOR, false);
785 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
787 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
791 if (!the_locking_inset) {
792 ParagraphList::iterator pit = paragraphs.begin();
793 ParagraphList::iterator pend = paragraphs.end();
795 int const id = inset->id();
796 for (; pit != pend; ++pit) {
797 InsetList::iterator it =
798 pit->insetlist.begin();
799 InsetList::iterator const end =
800 pit->insetlist.end();
801 for (; it != end; ++it) {
802 if (it.getInset() == inset) {
803 getLyXText(bv)->setCursorIntern(&*pit, it.getPos());
804 lockInset(bv, inset);
807 if (it.getInset()->getInsetFromID(id)) {
808 getLyXText(bv)->setCursorIntern(&*pit, it.getPos());
809 it.getInset()->edit(bv);
810 return the_locking_inset->lockInsetInInset(bv, inset);
816 if (inset == cpar(bv)->getInset(cpos(bv))) {
817 lyxerr[Debug::INSETS] << "OK" << endl;
818 lockInset(bv, inset);
820 } else if (the_locking_inset && (the_locking_inset == inset)) {
821 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
822 lyxerr[Debug::INSETS] << "OK" << endl;
823 inset_x = cix(bv) - top_x + drawTextXOffset;
824 inset_y = ciy(bv) + drawTextYOffset;
826 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
828 } else if (the_locking_inset) {
829 lyxerr[Debug::INSETS] << "MAYBE" << endl;
830 return the_locking_inset->lockInsetInInset(bv, inset);
832 lyxerr[Debug::INSETS] << "NOT OK" << endl;
837 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
840 if (!the_locking_inset)
842 if (the_locking_inset == inset) {
843 the_locking_inset->insetUnlock(bv);
844 getLyXText(bv)->updateInset(inset);
845 the_locking_inset = 0;
847 moveRightIntern(bv, true, false);
848 old_par = 0; // force layout setting
852 updateLocal(bv, CURSOR, false);
855 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
859 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
861 if (!autoBreakRows &&
862 boost::next(paragraphs.begin()) != paragraphs.end())
863 collapseParagraphs(bv);
871 if (inset->owner() != this) {
872 int ustat = CURSOR_PAR;
874 UpdatableInset * tl_inset = the_locking_inset;
876 found = tl_inset->updateInsetInInset(bv, inset);
878 tl_inset = static_cast<UpdatableInset *>(inset);
879 while(tl_inset->owner() && tl_inset->owner() != this)
880 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
881 if (!tl_inset->owner())
883 found = tl_inset->updateInsetInInset(bv, inset);
887 lt->updateInset(tl_inset);
891 setUpdateStatus(bv, ustat);
894 bool found = lt->updateInset(inset);
898 setUpdateStatus(bv, CURSOR_PAR);
899 if (the_locking_inset &&
900 cpar(bv) == inset_par && cpos(bv) == inset_pos)
902 inset_x = cix(bv) - top_x + drawTextXOffset;
903 inset_y = ciy(bv) + drawTextYOffset;
910 void InsetText::lfunMousePress(FuncRequest const & cmd)
914 // use this to check mouse motion for selection!
918 BufferView * bv = cmd.view();
919 FuncRequest cmd1 = cmd;
925 int tmp_x = cmd.x - drawTextXOffset;
926 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->top_y();
927 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
930 if (the_locking_inset) {
931 if (the_locking_inset == inset) {
932 the_locking_inset->localDispatch(cmd1);
935 // otherwise only unlock the_locking_inset
936 the_locking_inset->insetUnlock(bv);
937 the_locking_inset = 0;
940 no_selection = false;
942 if (bv->theLockingInset()) {
943 if (isHighlyEditableInset(inset)) {
944 // We just have to lock the inset before calling a
946 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
947 if (!bv->lockInset(uinset)) {
948 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
950 inset->localDispatch(cmd1);
951 if (the_locking_inset)
952 updateLocal(bv, CURSOR, false);
957 bool paste_internally = false;
958 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
959 localDispatch(FuncRequest(bv, LFUN_COPY));
960 paste_internally = true;
967 int old_top_y = lt->top_y();
969 lt->setCursorFromCoordinates(cmd.x - drawTextXOffset,
970 cmd.y + insetAscent);
971 // set the selection cursor!
972 lt->selection.cursor = lt->cursor;
973 lt->cursor.x_fix(lt->cursor.x());
975 if (lt->selection.set()) {
976 lt->clearSelection();
977 updateLocal(bv, FULL, false);
979 lt->clearSelection();
980 updateLocal(bv, CURSOR, false);
986 bv->owner()->setLayout(cpar(bv)->layout()->name());
988 // we moved the view we cannot do mouse selection in this case!
989 if (getLyXText(bv)->top_y() != old_top_y)
992 // Insert primary selection with middle mouse
993 // if there is a local selection in the current buffer,
995 if (cmd.button() == mouse_button::button2) {
996 if (paste_internally)
997 localDispatch(FuncRequest(bv, LFUN_PASTE));
999 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
1002 getLyXText(bv)->clearSelection();
1004 showInsetCursor(bv);
1008 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
1010 BufferView * bv = cmd.view();
1011 FuncRequest cmd1 = cmd;
1015 no_selection = true;
1016 if (the_locking_inset)
1017 return the_locking_inset->localDispatch(cmd1);
1019 int tmp_x = cmd.x - drawTextXOffset;
1020 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->top_y();
1021 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
1024 // This code should probably be removed now. Simple insets
1025 // (!highlyEditable) can actually take the localDispatch,
1026 // and turn it into edit() if necessary. But we still
1027 // need to deal properly with the whole relative vs.
1028 // absolute mouse co-ords thing in a realiable, sensible way
1030 if (isHighlyEditableInset(inset))
1031 ret = inset->localDispatch(cmd1);
1033 inset_x = cix(bv) - top_x + drawTextXOffset;
1034 inset_y = ciy(bv) + drawTextYOffset;
1035 cmd1.x = cmd.x - inset_x;
1036 cmd1.y = cmd.x - inset_y;
1037 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
1041 ret = inset->localDispatch(cmd1);
1042 updateLocal(bv, CURSOR_PAR, false);
1049 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
1051 FuncRequest cmd1 = cmd;
1055 if (the_locking_inset) {
1056 the_locking_inset->localDispatch(cmd1);
1060 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
1063 BufferView * bv = cmd.view();
1066 lt = getLyXText(bv);
1069 hideInsetCursor(bv);
1070 LyXCursor cur = lt->cursor;
1071 lt->setCursorFromCoordinates
1072 (cmd.x - drawTextXOffset, cmd.y + insetAscent);
1073 lt->cursor.x_fix(lt->cursor.x());
1074 if (cur == lt->cursor) {
1080 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1081 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1085 updateLocal(bv, SELECTION, false);
1087 showInsetCursor(bv);
1091 Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
1093 BufferView * bv = ev.view();
1094 switch (ev.action) {
1095 case LFUN_MOUSE_PRESS:
1098 case LFUN_MOUSE_MOTION:
1099 lfunMouseMotion(ev);
1101 case LFUN_MOUSE_RELEASE:
1102 return lfunMouseRelease(ev) ? DISPATCHED : UNDISPATCHED;
1107 bool was_empty = (paragraphs.begin()->empty() &&
1108 boost::next(paragraphs.begin()) == paragraphs.end());
1109 no_selection = false;
1110 RESULT result = UpdatableInset::localDispatch(ev);
1111 if (result != UNDISPATCHED)
1114 result = DISPATCHED;
1115 if (ev.action < 0 && ev.argument.empty())
1118 if (the_locking_inset) {
1119 result = the_locking_inset->localDispatch(ev);
1120 if (result == DISPATCHED_NOUPDATE)
1122 else if (result == DISPATCHED) {
1123 updateLocal(bv, CURSOR_PAR, false);
1125 } else if (result >= FINISHED) {
1127 case FINISHED_RIGHT:
1128 moveRightIntern(bv, false, false);
1129 result = DISPATCHED;
1132 if ((result = moveUp(bv)) >= FINISHED) {
1133 updateLocal(bv, CURSOR, false);
1134 bv->unlockInset(this);
1138 if ((result = moveDown(bv)) >= FINISHED) {
1139 updateLocal(bv, CURSOR, false);
1140 bv->unlockInset(this);
1144 result = DISPATCHED;
1147 the_locking_inset = 0;
1148 updateLocal(bv, CURSOR, false);
1149 // make sure status gets reset immediately
1150 bv->owner()->clearMessage();
1154 hideInsetCursor(bv);
1157 lt = getLyXText(bv);
1161 int updflag = false;
1163 // what type of update to do on a cursor movement
1164 int cursor_update = CURSOR;
1166 if (lt->selection.set())
1167 cursor_update = SELECTION;
1169 switch (ev.action) {
1172 case LFUN_SELFINSERT:
1173 if (bv->buffer()->isReadonly()) {
1174 // setErrorMessage(N_("Document is read only"));
1177 if (!ev.argument.empty()) {
1178 /* Automatically delete the currently selected
1179 * text and replace it with what is being
1180 * typed in now. Depends on lyxrc settings
1181 * "auto_region_delete", which defaults to
1184 // This should not be needed here and is also WRONG!
1185 setUndo(bv, Undo::INSERT,
1186 lt->cursor.par(), lt->cursor.par()->next());
1189 if (lyxrc.auto_region_delete) {
1190 if (lt->selection.set()) {
1191 lt->cutSelection(false, false);
1194 lt->clearSelection();
1195 for (string::size_type i = 0; i < ev.argument.length(); ++i) {
1196 bv->owner()->getIntl().getTransManager().
1197 TranslateAndInsert(ev.argument[i], lt);
1200 lt->selection.cursor = lt->cursor;
1201 updwhat = CURSOR | CURSOR_PAR;
1203 result = DISPATCHED_NOUPDATE;
1206 // cursor movements that need special handling
1209 result = moveRight(bv);
1211 updwhat = cursor_update;
1215 result = moveLeft(bv);
1216 updwhat = cursor_update;
1220 result = moveDown(bv);
1221 updwhat = cursor_update;
1225 result = moveUp(bv);
1226 updwhat = cursor_update;
1230 if (crow(bv) == lt->rows().begin())
1231 result = FINISHED_UP;
1233 lt->cursorPrevious();
1234 lt->clearSelection();
1235 result = DISPATCHED_NOUPDATE;
1237 updwhat = cursor_update;
1241 if (boost::next(crow(bv)) == lt->rows().end())
1242 result = FINISHED_DOWN;
1245 lt->clearSelection();
1246 result = DISPATCHED_NOUPDATE;
1248 updwhat = cursor_update;
1251 case LFUN_BACKSPACE: {
1252 if (lt->selection.set())
1253 lt->cutSelection(true, false);
1256 updwhat = CURSOR_PAR;
1262 if (lt->selection.set()) {
1263 lt->cutSelection(true, false);
1267 updwhat = CURSOR_PAR;
1273 lt->cutSelection(bv);
1274 updwhat = CURSOR_PAR;
1281 lt->copySelection();
1282 updwhat = CURSOR_PAR;
1285 case LFUN_PASTESELECTION:
1287 string const clip(bv->getClipboard());
1291 if (ev.argument == "paragraph") {
1292 lt->insertStringAsParagraphs(clip);
1294 lt->insertStringAsLines(clip);
1297 lt->clearSelection();
1299 updwhat = CURSOR_PAR;
1305 if (!autoBreakRows) {
1306 if (CutAndPaste::nrOfParagraphs() > 1) {
1307 #ifdef WITH_WARNINGS
1308 #warning FIXME horrendously bad UI
1310 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1315 lt->pasteSelection();
1317 lt->clearSelection();
1318 updwhat = CURSOR_PAR;
1323 case LFUN_BREAKPARAGRAPH:
1324 if (!autoBreakRows) {
1325 result = DISPATCHED;
1328 lt->breakParagraph(paragraphs, 0);
1329 updwhat = CURSOR | FULL;
1333 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1334 if (!autoBreakRows) {
1335 result = DISPATCHED;
1338 lt->breakParagraph(paragraphs, 1);
1339 updwhat = CURSOR | FULL;
1343 case LFUN_BREAKLINE: {
1344 if (!autoBreakRows) {
1345 result = DISPATCHED;
1349 lt->insertInset(new InsetNewline);
1350 updwhat = CURSOR | CURSOR_PAR;
1356 // do not set layouts on non breakable textinsets
1357 if (autoBreakRows) {
1358 string cur_layout = cpar(bv)->layout()->name();
1360 // Derive layout number from given argument (string)
1361 // and current buffer's textclass (number). */
1362 LyXTextClass const & tclass =
1363 bv->buffer()->params.getLyXTextClass();
1364 string layout = ev.argument;
1365 bool hasLayout = tclass.hasLayout(layout);
1367 // If the entry is obsolete, use the new one instead.
1369 string const & obs =
1370 tclass[layout]->obsoleted_by();
1375 // see if we found the layout number:
1377 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + ev.argument + N_(" not known"));
1378 bv->owner()->dispatch(lf);
1382 if (cur_layout != layout) {
1383 cur_layout = layout;
1384 lt->setLayout(layout);
1385 bv->owner()->setLayout(cpar(bv)->layout()->name());
1386 updwhat = CURSOR_PAR;
1390 // reset the layout box
1391 bv->owner()->setLayout(cpar(bv)->layout()->name());
1394 case LFUN_PARAGRAPH_SPACING:
1395 // This one is absolutely not working. When fiddling with this
1396 // it also seems to me that the paragraphs inside the insettext
1397 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1398 // FIXME: how old is this comment ? ...
1400 ParagraphList::iterator pit = lt->cursor.par();
1401 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1402 float cur_value = 1.0;
1403 if (cur_spacing == Spacing::Other) {
1404 cur_value = pit->params().spacing().getValue();
1407 istringstream istr(ev.argument.c_str());
1410 Spacing::Space new_spacing = cur_spacing;
1411 float new_value = cur_value;
1413 lyxerr << "Missing argument to `paragraph-spacing'"
1415 } else if (tmp == "single") {
1416 new_spacing = Spacing::Single;
1417 } else if (tmp == "onehalf") {
1418 new_spacing = Spacing::Onehalf;
1419 } else if (tmp == "double") {
1420 new_spacing = Spacing::Double;
1421 } else if (tmp == "other") {
1422 new_spacing = Spacing::Other;
1425 lyxerr << "new_value = " << tmpval << endl;
1428 } else if (tmp == "default") {
1429 new_spacing = Spacing::Default;
1431 lyxerr << _("Unknown spacing argument: ")
1432 << ev.argument << endl;
1434 if (cur_spacing != new_spacing || cur_value != new_value) {
1435 pit->params().spacing(Spacing(new_spacing, new_value));
1436 updwhat = CURSOR_PAR;
1442 // These need to do repaints but don't require
1443 // special handling otherwise. A *lot* of the
1444 // above could probably be done similarly ...
1449 case LFUN_WORDRIGHT:
1450 // these two are really unhandled ...
1452 case LFUN_BEGINNINGBUF:
1453 updwhat = cursor_update;
1454 if (!bv->dispatch(ev))
1455 result = UNDISPATCHED;
1464 case LFUN_WORDLEFTSEL:
1465 case LFUN_WORDRIGHTSEL:
1466 updwhat = SELECTION;
1471 if (!bv->dispatch(ev))
1472 result = UNDISPATCHED;
1479 updateLocal(bv, updwhat, updflag);
1480 /// If the action has deleted all text in the inset, we need to change the
1481 // language to the language of the surronding text.
1482 if (!was_empty && paragraphs.begin()->empty() &&
1483 boost::next(paragraphs.begin()) == paragraphs.end()) {
1484 LyXFont font(LyXFont::ALL_IGNORE);
1485 font.setLanguage(bv->getParentLanguage(this));
1486 setFont(bv, font, false);
1489 if (result >= FINISHED)
1490 bv->unlockInset(this);
1492 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1493 result = DISPATCHED;
1498 int InsetText::latex(Buffer const * buf, ostream & os,
1499 bool moving_arg, bool) const
1502 latexParagraphs(buf, paragraphs,
1503 paragraphs.begin(), paragraphs.end(),
1504 os, texrow, moving_arg);
1505 return texrow.rows();
1509 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1511 unsigned int lines = 0;
1513 ParagraphList::iterator beg = paragraphs.begin();
1514 ParagraphList::iterator end = paragraphs.end();
1515 ParagraphList::iterator it = beg;
1516 for (; it != end; ++it) {
1517 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1518 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1525 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1527 unsigned int lines = 0;
1529 vector<string> environment_stack(10);
1530 vector<string> environment_inner(10);
1532 int const command_depth = 0;
1535 Paragraph::depth_type depth = 0; // paragraph depth
1537 ParagraphList::iterator pit = paragraphs.begin();
1538 ParagraphList::iterator pend = paragraphs.end();
1540 for (; pit != pend; ++pit) {
1542 int desc_on = 0; // description mode
1544 LyXLayout_ptr const & style = pit->layout();
1546 // environment tag closing
1547 for (; depth > pit->params().depth(); --depth) {
1548 if (environment_inner[depth] != "!-- --") {
1549 item_name = "listitem";
1550 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1551 if (environment_inner[depth] == "varlistentry")
1552 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1554 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1555 environment_stack[depth].erase();
1556 environment_inner[depth].erase();
1559 if (depth == pit->params().depth()
1560 && environment_stack[depth] != style->latexname()
1561 && !environment_stack[depth].empty()) {
1562 if (environment_inner[depth] != "!-- --") {
1563 item_name= "listitem";
1564 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1565 if (environment_inner[depth] == "varlistentry")
1566 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1569 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1571 environment_stack[depth].erase();
1572 environment_inner[depth].erase();
1575 // Write opening SGML tags.
1576 switch (style->latextype) {
1577 case LATEX_PARAGRAPH:
1578 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1582 buf->sgmlError(&*pit, 0, _("Error: LatexType Command not allowed here.\n"));
1586 case LATEX_ENVIRONMENT:
1587 case LATEX_ITEM_ENVIRONMENT:
1588 if (depth < pit->params().depth()) {
1589 depth = pit->params().depth();
1590 environment_stack[depth].erase();
1593 if (environment_stack[depth] != style->latexname()) {
1594 if (environment_stack.size() == depth + 1) {
1595 environment_stack.push_back("!-- --");
1596 environment_inner.push_back("!-- --");
1598 environment_stack[depth] = style->latexname();
1599 environment_inner[depth] = "!-- --";
1600 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1602 if (environment_inner[depth] != "!-- --") {
1603 item_name= "listitem";
1604 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1605 if (environment_inner[depth] == "varlistentry")
1606 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1610 if (style->latextype == LATEX_ENVIRONMENT) {
1611 if (!style->latexparam().empty()) {
1612 if (style->latexparam() == "CDATA")
1615 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1620 desc_on = (style->labeltype == LABEL_MANUAL);
1622 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1623 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1625 item_name = desc_on ? "term" : "para";
1626 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1630 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1634 buf->simpleDocBookOnePar(os, &*pit, desc_on, depth + 1 + command_depth);
1637 // write closing SGML tags
1638 switch (style->latextype) {
1639 case LATEX_ENVIRONMENT:
1640 if (!style->latexparam().empty()) {
1641 if (style->latexparam() == "CDATA")
1644 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1647 case LATEX_ITEM_ENVIRONMENT:
1648 if (desc_on == 1) break;
1650 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1652 case LATEX_PARAGRAPH:
1653 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1656 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1662 for (int d = depth; d >= 0; --d) {
1663 if (!environment_stack[depth].empty()) {
1664 if (environment_inner[depth] != "!-- --") {
1665 item_name = "listitem";
1666 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1667 if (environment_inner[depth] == "varlistentry")
1668 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1671 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1679 void InsetText::validate(LaTeXFeatures & features) const
1681 for_each(paragraphs.begin(), paragraphs.end(),
1682 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1686 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1688 if (the_locking_inset) {
1689 the_locking_inset->getCursorPos(bv, x, y);
1692 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1693 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1697 int InsetText::insetInInsetY() const
1699 if (!the_locking_inset)
1702 return (inset_y + the_locking_inset->insetInInsetY());
1706 void InsetText::toggleInsetCursor(BufferView * bv)
1708 if (the_locking_inset) {
1709 the_locking_inset->toggleInsetCursor(bv);
1713 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1715 int const asc = font_metrics::maxAscent(font);
1716 int const desc = font_metrics::maxDescent(font);
1718 if (isCursorVisible())
1719 bv->hideLockedInsetCursor();
1721 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1722 toggleCursorVisible();
1726 void InsetText::showInsetCursor(BufferView * bv, bool show)
1728 if (the_locking_inset) {
1729 the_locking_inset->showInsetCursor(bv, show);
1732 if (!isCursorVisible()) {
1733 LyXFont const font =
1734 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1736 int const asc = font_metrics::maxAscent(font);
1737 int const desc = font_metrics::maxDescent(font);
1739 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1741 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1742 setCursorVisible(true);
1747 void InsetText::hideInsetCursor(BufferView * bv)
1749 if (isCursorVisible()) {
1750 bv->hideLockedInsetCursor();
1751 setCursorVisible(false);
1753 if (the_locking_inset)
1754 the_locking_inset->hideInsetCursor(bv);
1758 void InsetText::fitInsetCursor(BufferView * bv) const
1760 if (the_locking_inset) {
1761 the_locking_inset->fitInsetCursor(bv);
1764 LyXFont const font =
1765 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1767 int const asc = font_metrics::maxAscent(font);
1768 int const desc = font_metrics::maxDescent(font);
1770 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1771 need_update |= FULL;
1776 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1778 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1779 return moveLeftIntern(bv, false, activate_inset, selecting);
1781 return moveRightIntern(bv, true, activate_inset, selecting);
1786 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1788 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1789 return moveRightIntern(bv, true, activate_inset, selecting);
1791 return moveLeftIntern(bv, false, activate_inset, selecting);
1796 InsetText::moveRightIntern(BufferView * bv, bool front,
1797 bool activate_inset, bool selecting)
1799 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1800 return FINISHED_RIGHT;
1801 if (activate_inset && checkAndActivateInset(bv, front))
1803 getLyXText(bv)->cursorRight(bv);
1805 getLyXText(bv)->clearSelection();
1806 return DISPATCHED_NOUPDATE;
1811 InsetText::moveLeftIntern(BufferView * bv, bool front,
1812 bool activate_inset, bool selecting)
1814 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1816 getLyXText(bv)->cursorLeft(bv);
1818 getLyXText(bv)->clearSelection();
1819 if (activate_inset && checkAndActivateInset(bv, front))
1821 return DISPATCHED_NOUPDATE;
1825 Inset::RESULT InsetText::moveUp(BufferView * bv)
1827 if (crow(bv) == getLyXText(bv)->rows().begin())
1829 getLyXText(bv)->cursorUp(bv);
1830 getLyXText(bv)->clearSelection();
1831 return DISPATCHED_NOUPDATE;
1835 Inset::RESULT InsetText::moveDown(BufferView * bv)
1837 if (boost::next(crow(bv)) == getLyXText(bv)->rows().end())
1838 return FINISHED_DOWN;
1839 getLyXText(bv)->cursorDown(bv);
1840 getLyXText(bv)->clearSelection();
1841 return DISPATCHED_NOUPDATE;
1845 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1847 if (the_locking_inset) {
1848 if (the_locking_inset->insetAllowed(inset))
1849 return the_locking_inset->insertInset(bv, inset);
1852 inset->setOwner(this);
1853 hideInsetCursor(bv);
1854 getLyXText(bv)->insertInset(inset);
1856 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1861 bool InsetText::insetAllowed(Inset::Code code) const
1863 // in_insetAllowed is a really gross hack,
1864 // to allow us to call the owner's insetAllowed
1865 // without stack overflow, which can happen
1866 // when the owner uses InsetCollapsable::insetAllowed()
1868 if (in_insetAllowed)
1870 in_insetAllowed = true;
1871 if (the_locking_inset)
1872 ret = the_locking_inset->insetAllowed(code);
1874 ret = owner()->insetAllowed(code);
1875 in_insetAllowed = false;
1880 UpdatableInset * InsetText::getLockingInset() const
1882 return the_locking_inset ? the_locking_inset->getLockingInset() :
1883 const_cast<InsetText *>(this);
1887 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1891 if (the_locking_inset)
1892 return the_locking_inset->getFirstLockingInsetOfType(c);
1897 bool InsetText::showInsetDialog(BufferView * bv) const
1899 if (the_locking_inset)
1900 return the_locking_inset->showInsetDialog(bv);
1905 vector<string> const InsetText::getLabelList() const
1907 vector<string> label_list;
1909 ParagraphList::iterator pit = paragraphs.begin();
1910 ParagraphList::iterator pend = paragraphs.end();
1911 for (; pit != pend; ++pit) {
1912 InsetList::iterator beg = pit->insetlist.begin();
1913 InsetList::iterator end = pit->insetlist.end();
1914 for (; beg != end; ++beg) {
1915 vector<string> const l = beg.getInset()->getLabelList();
1916 label_list.insert(label_list.end(), l.begin(), l.end());
1923 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1926 if (the_locking_inset) {
1927 the_locking_inset->setFont(bv, font, toggleall, selectall);
1930 if ((boost::next(paragraphs.begin()) == paragraphs.end() &&
1931 paragraphs.begin()->empty()) || cpar(bv)->empty()) {
1932 getLyXText(bv)->setFont(font, toggleall);
1937 lt = getLyXText(bv);
1940 if (lt->selection.set()) {
1941 setUndo(bv, Undo::EDIT, &*lt->cursor.par(), lt->cursor.par()->next());
1945 lt->toggleFree(font, toggleall);
1947 lt->clearSelection();
1949 bool flag = (selectall || lt->selection.set());
1953 updateLocal(bv, FULL, true);
1955 updateLocal(bv, CURSOR_PAR, true);
1959 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1961 if (cpar(bv)->isInset(cpos(bv))) {
1963 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1964 if (!isHighlyEditableInset(inset))
1966 inset->edit(bv, front);
1967 if (!the_locking_inset)
1969 updateLocal(bv, CURSOR, false);
1976 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1977 mouse_button::state button)
1979 x -= drawTextXOffset;
1981 int dummyy = y + insetAscent;
1982 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1983 // we only do the edit() call if the inset was hit by the mouse
1984 // or if it is a highly editable inset. So we should call this
1985 // function from our own edit with button < 0.
1986 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1988 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1996 inset_x = cix(bv) - top_x + drawTextXOffset;
1997 inset_y = ciy(bv) + drawTextYOffset;
1998 inset->edit(bv, x - inset_x, y - inset_y, button);
1999 if (!the_locking_inset)
2001 updateLocal(bv, CURSOR, false);
2008 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2011 int w = UpdatableInset::getMaxWidth(bv, inset);
2016 w = w - top_x + owner()->x();
2019 w -= (2 * TEXT_TO_INSET_OFFSET);
2022 return UpdatableInset::getMaxWidth(bv, inset);
2027 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2029 // we have to unlock any locked inset otherwise we're in troubles
2030 the_locking_inset = 0;
2033 paragraphs.set(new Paragraph(*p, same_id));
2034 paragraphs.begin()->setInsetOwner(this);
2035 Paragraph * np = &*(paragraphs.begin());
2038 np->next(new Paragraph(*p, same_id));
2039 np->next()->previous(np);
2041 np->setInsetOwner(this);
2048 void InsetText::markNew(bool track_changes)
2050 ParagraphList::iterator pit = paragraphs.begin();
2051 ParagraphList::iterator pend = paragraphs.end();
2052 for (; pit != pend; ++pit) {
2053 if (track_changes) {
2054 pit->trackChanges();
2056 // no-op when not tracking
2057 pit->cleanChanges();
2063 void InsetText::setText(string const & data, LyXFont const & font)
2066 for (unsigned int i = 0; i < data.length(); ++i)
2067 paragraphs.begin()->insertChar(i, data[i], font);
2072 void InsetText::setAutoBreakRows(bool flag)
2074 if (flag != autoBreakRows) {
2075 autoBreakRows = flag;
2083 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2085 if (how != drawFrame_) {
2088 updateLocal(bv, DRAW_FRAME, false);
2093 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2095 if (frame_color != col) {
2098 updateLocal(bv, DRAW_FRAME, false);
2103 int InsetText::cx(BufferView * bv) const
2105 // we do nothing dangerous so we use a local cache
2106 LyXText * llt = getLyXText(bv);
2107 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2108 if (the_locking_inset) {
2109 LyXFont font = llt->getFont(bv->buffer(), &*llt->cursor.par(),
2111 if (font.isVisibleRightToLeft())
2112 x -= the_locking_inset->width(bv, font);
2118 int InsetText::cix(BufferView * bv) const
2120 // we do nothing dangerous so we use a local cache
2121 LyXText * llt = getLyXText(bv);
2122 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2123 if (the_locking_inset) {
2124 LyXFont font = llt->getFont(bv->buffer(), &*llt->cursor.par(),
2126 if (font.isVisibleRightToLeft())
2127 x -= the_locking_inset->width(bv, font);
2133 int InsetText::cy(BufferView * bv) const
2136 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2140 int InsetText::ciy(BufferView * bv) const
2143 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2147 pos_type InsetText::cpos(BufferView * bv) const
2149 return getLyXText(bv)->cursor.pos();
2153 Paragraph * InsetText::cpar(BufferView * bv) const
2155 return &*getLyXText(bv)->cursor.par();
2159 bool InsetText::cboundary(BufferView * bv) const
2161 return getLyXText(bv)->cursor.boundary();
2165 RowList::iterator InsetText::crow(BufferView * bv) const
2167 return getLyXText(bv)->cursor.row();
2171 LyXText * InsetText::getLyXText(BufferView const * lbv,
2172 bool const recursive) const
2174 if (cached_bview == lbv) {
2175 if (recursive && the_locking_inset)
2176 return the_locking_inset->getLyXText(lbv, true);
2177 LyXText * lt = cached_text.get();
2178 lyx::Assert(lt && lt->rows().begin()->par() == &*(paragraphs.begin()));
2181 // Super UGLY! (Lgb)
2182 BufferView * bv = const_cast<BufferView *>(lbv);
2185 Cache::iterator it = cache.find(bv);
2187 if (it != cache.end()) {
2190 } else if (do_resize) {
2191 resizeLyXText(do_resize);
2193 if (lt || !it->second.remove) {
2194 lyx::Assert(it->second.text.get());
2195 cached_text = it->second.text;
2196 if (recursive && the_locking_inset) {
2197 return the_locking_inset->getLyXText(bv, true);
2199 return cached_text.get();
2200 } else if (it->second.remove) {
2202 saveLyXTextState(it->second.text.get());
2208 // when we have to reinit the existing LyXText!
2210 it->second.text->init(bv);
2211 restoreLyXTextState(it->second.text.get());
2212 it->second.remove = false;
2214 cached_text = it->second.text;
2215 if (the_locking_inset && recursive) {
2216 return the_locking_inset->getLyXText(bv);
2218 return cached_text.get();
2221 // we are here only if we don't have a BufferView * in the cache!!!
2223 cached_text.reset(new LyXText(bv, const_cast<InsetText *>(this)));
2224 cached_text->init(bv);
2225 restoreLyXTextState(cached_text.get());
2227 cache.insert(make_pair(bv, cached_text));
2229 if (the_locking_inset && recursive) {
2230 return the_locking_inset->getLyXText(bv);
2232 return cached_text.get();
2236 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2240 Cache::iterator it = cache.find(bv);
2242 if (it == cache.end()) {
2246 lyx::Assert(it->second.text.get());
2248 it->second.remove = true;
2250 /// then remove all LyXText in text-insets
2251 for_each(paragraphs.begin(), paragraphs.end(),
2252 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2257 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2260 // we cannot resize this because we are in use!
2261 // so do this on the next possible getLyXText()
2266 if (boost::next(paragraphs.begin()) == paragraphs.end() &&
2267 paragraphs.begin()->empty()) { // no data, resize not neccessary!
2268 // we have to do this as a fixed width may have changed!
2269 LyXText * t = getLyXText(bv);
2270 saveLyXTextState(t);
2272 restoreLyXTextState(t);
2275 // one endless line, resize normally not necessary
2276 if (!force && getMaxWidth(bv, this) < 0)
2279 Cache::iterator it = cache.find(bv);
2280 if (it == cache.end()) {
2283 lyx::Assert(it->second.text.get());
2285 LyXText * t = it->second.text.get();
2286 saveLyXTextState(t);
2288 for_each(paragraphs.begin(), paragraphs.end(),
2289 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2292 restoreLyXTextState(t);
2293 if (the_locking_inset) {
2294 inset_x = cix(bv) - top_x + drawTextXOffset;
2295 inset_y = ciy(bv) + drawTextYOffset;
2298 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2300 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2301 // this will scroll the screen such that the cursor becomes visible
2302 bv->updateScrollbar();
2304 need_update |= FULL;
2309 void InsetText::reinitLyXText() const
2312 // we cannot resize this because we are in use!
2313 // so do this on the next possible getLyXText()
2319 for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2320 lyx::Assert(it->second.text.get());
2322 LyXText * t = it->second.text.get();
2323 BufferView * bv = it->first;
2325 saveLyXTextState(t);
2327 for_each(paragraphs.begin(), paragraphs.end(),
2328 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2331 restoreLyXTextState(t);
2332 if (the_locking_inset) {
2333 inset_x = cix(bv) - top_x + drawTextXOffset;
2334 inset_y = ciy(bv) + drawTextYOffset;
2336 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2338 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2339 // this will scroll the screen such that the cursor becomes visible
2340 bv->updateScrollbar();
2348 void InsetText::removeNewlines()
2350 bool changed = false;
2352 ParagraphList::iterator it = paragraphs.begin();
2353 ParagraphList::iterator end = paragraphs.end();
2354 for (; it != end; ++it) {
2355 for (int i = 0; i < it->size(); ++i) {
2356 if (it->isNewline(i)) {
2367 bool InsetText::nodraw() const
2369 if (the_locking_inset)
2370 return the_locking_inset->nodraw();
2371 return UpdatableInset::nodraw();
2375 int InsetText::scroll(bool recursive) const
2377 int sx = UpdatableInset::scroll(false);
2379 if (recursive && the_locking_inset)
2380 sx += the_locking_inset->scroll(recursive);
2386 void InsetText::selectAll(BufferView * bv)
2388 getLyXText(bv)->cursorTop();
2389 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2390 getLyXText(bv)->cursorBottom();
2391 getLyXText(bv)->setSelection();
2395 void InsetText::clearSelection(BufferView * bv)
2397 getLyXText(bv)->clearSelection();
2401 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2403 Painter & pain = bv->painter();
2405 int h = insetAscent + insetDescent;
2406 int ty = baseline - insetAscent;
2412 if ((ty + h) > pain.paperHeight())
2413 h = pain.paperHeight();
2414 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2415 w = pain.paperWidth();
2416 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2421 Paragraph * InsetText::firstParagraph() const
2424 if (the_locking_inset)
2425 if ((result = the_locking_inset->firstParagraph()))
2427 return &*(paragraphs.begin());
2431 Paragraph * InsetText::getFirstParagraph(int i) const
2433 return (i == 0) ? &*(paragraphs.begin()) : 0;
2437 LyXCursor const & InsetText::cursor(BufferView * bv) const
2439 if (the_locking_inset)
2440 return the_locking_inset->cursor(bv);
2441 return getLyXText(bv)->cursor;
2445 void InsetText::paragraph(Paragraph * p)
2447 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2448 // caller of this function has to take care of it. This IS important
2449 // as we could have to insert a paragraph before this one and just
2450 // link the actual to a new ones next and set it with this function
2453 // set ourself as owner for all the paragraphs inserted!
2454 for_each(paragraphs.begin(), paragraphs.end(),
2455 boost::bind(&Paragraph::setInsetOwner, _1, this));
2458 // redraw myself when asked for
2463 Inset * InsetText::getInsetFromID(int id_arg) const
2466 return const_cast<InsetText *>(this);
2468 ParagraphList::iterator pit = paragraphs.begin();
2469 ParagraphList::iterator pend = paragraphs.end();
2470 for (; pit != pend; ++pit) {
2471 InsetList::iterator it = pit->insetlist.begin();
2472 InsetList::iterator end = pit->insetlist.end();
2473 for (; it != end; ++it) {
2474 if (it.getInset()->id() == id_arg)
2475 return it.getInset();
2476 Inset * in = it.getInset()->getInsetFromID(id_arg);
2486 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2487 float & value) const
2493 lt = getLyXText(bv);
2496 if (the_locking_inset) {
2497 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2498 if (!word.word().empty()) {
2504 // we have to go on checking so move cursor to the next char
2505 lt->cursor.pos(lt->cursor.pos() + 1);
2507 word = lt->selectNextWordToSpellcheck(value);
2508 if (word.word().empty())
2509 bv->unlockInset(const_cast<InsetText *>(this));
2518 void InsetText::selectSelectedWord(BufferView * bv)
2520 if (the_locking_inset) {
2521 the_locking_inset->selectSelectedWord(bv);
2524 getLyXText(bv)->selectSelectedWord();
2525 updateLocal(bv, SELECTION, false);
2529 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2531 if (the_locking_inset) {
2532 the_locking_inset->toggleSelection(bv, kill_selection);
2536 lt = getLyXText(bv);
2540 int x = top_x + TEXT_TO_INSET_OFFSET;
2542 RowList::iterator rowit = lt->rows().begin();
2543 RowList::iterator end = lt->rows().end();
2544 int y_offset = top_baseline - rowit->ascent_of_text();
2546 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2547 y += rowit->height();
2553 if (need_update & SELECTION)
2555 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2561 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2565 lt = getLyXText(bv);
2568 if (the_locking_inset) {
2569 if (the_locking_inset->nextChange(bv, length))
2571 lt->cursorRight(true);
2573 lyxfind::SearchResult result =
2574 lyxfind::findNextChange(bv, lt, length);
2576 if (result == lyxfind::SR_FOUND) {
2577 LyXCursor cur = lt->cursor;
2578 bv->unlockInset(bv->theLockingInset());
2579 if (bv->lockInset(this))
2582 lt->setSelectionRange(length);
2583 updateLocal(bv, SELECTION, false);
2587 return result != lyxfind::SR_NOT_FOUND;
2591 bool InsetText::searchForward(BufferView * bv, string const & str,
2596 lt = getLyXText(bv);
2599 if (the_locking_inset) {
2600 if (the_locking_inset->searchForward(bv, str, cs, mw))
2602 lt->cursorRight(true);
2604 lyxfind::SearchResult result =
2605 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2607 if (result == lyxfind::SR_FOUND) {
2608 LyXCursor cur = lt->cursor;
2609 bv->unlockInset(bv->theLockingInset());
2610 if (bv->lockInset(this))
2613 lt->setSelectionRange(str.length());
2614 updateLocal(bv, SELECTION, false);
2618 return (result != lyxfind::SR_NOT_FOUND);
2621 bool InsetText::searchBackward(BufferView * bv, string const & str,
2624 if (the_locking_inset) {
2625 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2630 lt = getLyXText(bv);
2634 ParagraphList::iterator pit = paragraphs.begin();
2635 ParagraphList::iterator pend = paragraphs.end();
2637 while (boost::next(pit) != pend)
2640 lt->setCursor(&*pit, pit->size());
2642 lyxfind::SearchResult result =
2643 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2645 if (result == lyxfind::SR_FOUND) {
2646 LyXCursor cur = lt->cursor;
2647 bv->unlockInset(bv->theLockingInset());
2648 if (bv->lockInset(this))
2651 lt->setSelectionRange(str.length());
2652 updateLocal(bv, SELECTION, false);
2656 return (result != lyxfind::SR_NOT_FOUND);
2660 bool InsetText::checkInsertChar(LyXFont & font)
2663 return owner()->checkInsertChar(font);
2668 void InsetText::collapseParagraphs(BufferView * bv)
2670 LyXText * llt = getLyXText(bv);
2672 while (boost::next(paragraphs.begin()) != paragraphs.end()) {
2673 if (!paragraphs.begin()->empty() &&
2674 !boost::next(paragraphs.begin())->empty() &&
2675 !paragraphs.begin()->isSeparator(paragraphs.begin()->size() - 1))
2677 paragraphs.begin()->insertChar(paragraphs.begin()->size(), ' ');
2679 if (llt->selection.set()) {
2680 if (llt->selection.start.par() == boost::next(paragraphs.begin())) {
2681 llt->selection.start.par(&*(paragraphs.begin()));
2682 llt->selection.start.pos(
2683 llt->selection.start.pos() + paragraphs.begin()->size());
2685 if (llt->selection.end.par() == boost::next(paragraphs.begin())) {
2686 llt->selection.end.par(&*(paragraphs.begin()));
2687 llt->selection.end.pos(
2688 llt->selection.end.pos() + paragraphs.begin()->size());
2691 mergeParagraph(bv->buffer()->params, paragraphs, paragraphs.begin());
2697 void InsetText::getDrawFont(LyXFont & font) const
2701 owner()->getDrawFont(font);
2705 void InsetText::appendParagraphs(Buffer * buffer,
2708 BufferParams const & bparams = buffer->params;
2710 Paragraph * tmpbuf = newpar;
2711 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2712 if (bparams.tracking_changes)
2713 buf->cleanChanges();
2715 while (tmpbuf->next()) {
2716 tmpbuf = tmpbuf->next();
2717 lastbuffer->next(new Paragraph(*tmpbuf, false));
2718 lastbuffer->next()->previous(lastbuffer);
2719 lastbuffer = lastbuffer->next();
2720 if (bparams.tracking_changes)
2721 lastbuffer->cleanChanges();
2723 lastbuffer = &*(paragraphs.begin());
2724 while (lastbuffer->next())
2725 lastbuffer = lastbuffer->next();
2726 if (!newpar->empty() && !lastbuffer->empty() &&
2727 !lastbuffer->isSeparator(lastbuffer->size() - 1))
2729 lastbuffer->insertChar(lastbuffer->size(), ' ');
2732 // make the buf exactly the same layout than our last paragraph
2733 buf->makeSameLayout(lastbuffer);
2736 lastbuffer->next(buf);
2737 buf->previous(lastbuffer);
2738 mergeParagraph(buffer->params, paragraphs, lastbuffer);
2744 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2746 ParagraphList::iterator pit = paragraphs.begin();
2747 ParagraphList::iterator pend = paragraphs.end();
2749 for (; pit != pend; ++pit) {
2750 InsetList::iterator it = pit->insetlist.begin();
2751 InsetList::iterator end = pit->insetlist.end();
2752 for (; it != end; ++it) {
2753 it.getInset()->addPreview(loader);