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);
1152 hideInsetCursor(bv);
1155 lt = getLyXText(bv);
1159 int updflag = false;
1161 // what type of update to do on a cursor movement
1162 int cursor_update = CURSOR;
1164 if (lt->selection.set())
1165 cursor_update = SELECTION;
1167 switch (ev.action) {
1170 case LFUN_SELFINSERT:
1171 if (bv->buffer()->isReadonly()) {
1172 // setErrorMessage(N_("Document is read only"));
1175 if (!ev.argument.empty()) {
1176 /* Automatically delete the currently selected
1177 * text and replace it with what is being
1178 * typed in now. Depends on lyxrc settings
1179 * "auto_region_delete", which defaults to
1182 // This should not be needed here and is also WRONG!
1183 setUndo(bv, Undo::INSERT,
1184 lt->cursor.par(), lt->cursor.par()->next());
1187 if (lyxrc.auto_region_delete) {
1188 if (lt->selection.set()) {
1189 lt->cutSelection(false, false);
1192 lt->clearSelection();
1193 for (string::size_type i = 0; i < ev.argument.length(); ++i) {
1194 bv->owner()->getIntl().getTransManager().
1195 TranslateAndInsert(ev.argument[i], lt);
1198 lt->selection.cursor = lt->cursor;
1199 updwhat = CURSOR | CURSOR_PAR;
1201 result = DISPATCHED_NOUPDATE;
1204 // cursor movements that need special handling
1207 result = moveRight(bv);
1209 updwhat = cursor_update;
1213 result = moveLeft(bv);
1214 updwhat = cursor_update;
1218 result = moveDown(bv);
1219 updwhat = cursor_update;
1223 result = moveUp(bv);
1224 updwhat = cursor_update;
1228 if (crow(bv) == lt->rows().begin())
1229 result = FINISHED_UP;
1231 lt->cursorPrevious();
1232 lt->clearSelection();
1233 result = DISPATCHED_NOUPDATE;
1235 updwhat = cursor_update;
1239 if (boost::next(crow(bv)) == lt->rows().end())
1240 result = FINISHED_DOWN;
1243 lt->clearSelection();
1244 result = DISPATCHED_NOUPDATE;
1246 updwhat = cursor_update;
1249 case LFUN_BACKSPACE: {
1250 if (lt->selection.set())
1251 lt->cutSelection(true, false);
1254 updwhat = CURSOR_PAR;
1260 if (lt->selection.set()) {
1261 lt->cutSelection(true, false);
1265 updwhat = CURSOR_PAR;
1271 lt->cutSelection(bv);
1272 updwhat = CURSOR_PAR;
1279 lt->copySelection();
1280 updwhat = CURSOR_PAR;
1283 case LFUN_PASTESELECTION:
1285 string const clip(bv->getClipboard());
1289 if (ev.argument == "paragraph") {
1290 lt->insertStringAsParagraphs(clip);
1292 lt->insertStringAsLines(clip);
1295 lt->clearSelection();
1297 updwhat = CURSOR_PAR;
1303 if (!autoBreakRows) {
1304 if (CutAndPaste::nrOfParagraphs() > 1) {
1305 #ifdef WITH_WARNINGS
1306 #warning FIXME horrendously bad UI
1308 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1313 lt->pasteSelection();
1315 lt->clearSelection();
1316 updwhat = CURSOR_PAR;
1321 case LFUN_BREAKPARAGRAPH:
1322 if (!autoBreakRows) {
1323 result = DISPATCHED;
1326 lt->breakParagraph(paragraphs, 0);
1327 updwhat = CURSOR | FULL;
1331 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1332 if (!autoBreakRows) {
1333 result = DISPATCHED;
1336 lt->breakParagraph(paragraphs, 1);
1337 updwhat = CURSOR | FULL;
1341 case LFUN_BREAKLINE: {
1342 if (!autoBreakRows) {
1343 result = DISPATCHED;
1347 lt->insertInset(new InsetNewline);
1348 updwhat = CURSOR | CURSOR_PAR;
1354 // do not set layouts on non breakable textinsets
1355 if (autoBreakRows) {
1356 string cur_layout = cpar(bv)->layout()->name();
1358 // Derive layout number from given argument (string)
1359 // and current buffer's textclass (number). */
1360 LyXTextClass const & tclass =
1361 bv->buffer()->params.getLyXTextClass();
1362 string layout = ev.argument;
1363 bool hasLayout = tclass.hasLayout(layout);
1365 // If the entry is obsolete, use the new one instead.
1367 string const & obs =
1368 tclass[layout]->obsoleted_by();
1373 // see if we found the layout number:
1375 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + ev.argument + N_(" not known"));
1376 bv->owner()->dispatch(lf);
1380 if (cur_layout != layout) {
1381 cur_layout = layout;
1382 lt->setLayout(layout);
1383 bv->owner()->setLayout(cpar(bv)->layout()->name());
1384 updwhat = CURSOR_PAR;
1388 // reset the layout box
1389 bv->owner()->setLayout(cpar(bv)->layout()->name());
1392 case LFUN_PARAGRAPH_SPACING:
1393 // This one is absolutely not working. When fiddling with this
1394 // it also seems to me that the paragraphs inside the insettext
1395 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1396 // FIXME: how old is this comment ? ...
1398 Paragraph * par = lt->cursor.par();
1399 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1400 float cur_value = 1.0;
1401 if (cur_spacing == Spacing::Other) {
1402 cur_value = par->params().spacing().getValue();
1405 istringstream istr(ev.argument.c_str());
1408 Spacing::Space new_spacing = cur_spacing;
1409 float new_value = cur_value;
1411 lyxerr << "Missing argument to `paragraph-spacing'"
1413 } else if (tmp == "single") {
1414 new_spacing = Spacing::Single;
1415 } else if (tmp == "onehalf") {
1416 new_spacing = Spacing::Onehalf;
1417 } else if (tmp == "double") {
1418 new_spacing = Spacing::Double;
1419 } else if (tmp == "other") {
1420 new_spacing = Spacing::Other;
1423 lyxerr << "new_value = " << tmpval << endl;
1426 } else if (tmp == "default") {
1427 new_spacing = Spacing::Default;
1429 lyxerr << _("Unknown spacing argument: ")
1430 << ev.argument << endl;
1432 if (cur_spacing != new_spacing || cur_value != new_value) {
1433 par->params().spacing(Spacing(new_spacing, new_value));
1434 updwhat = CURSOR_PAR;
1440 // These need to do repaints but don't require
1441 // special handling otherwise. A *lot* of the
1442 // above could probably be done similarly ...
1447 case LFUN_WORDRIGHT:
1448 // these two are really unhandled ...
1450 case LFUN_BEGINNINGBUF:
1451 updwhat = cursor_update;
1452 if (!bv->dispatch(ev))
1453 result = UNDISPATCHED;
1462 case LFUN_WORDLEFTSEL:
1463 case LFUN_WORDRIGHTSEL:
1464 updwhat = SELECTION;
1469 if (!bv->dispatch(ev))
1470 result = UNDISPATCHED;
1477 updateLocal(bv, updwhat, updflag);
1478 /// If the action has deleted all text in the inset, we need to change the
1479 // language to the language of the surronding text.
1480 if (!was_empty && paragraphs.begin()->empty() &&
1481 boost::next(paragraphs.begin()) == paragraphs.end()) {
1482 LyXFont font(LyXFont::ALL_IGNORE);
1483 font.setLanguage(bv->getParentLanguage(this));
1484 setFont(bv, font, false);
1487 if (result >= FINISHED)
1488 bv->unlockInset(this);
1490 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1491 result = DISPATCHED;
1496 int InsetText::latex(Buffer const * buf, ostream & os,
1497 bool moving_arg, bool) const
1500 latexParagraphs(buf, paragraphs,
1501 paragraphs.begin(), paragraphs.end(),
1502 os, texrow, moving_arg);
1503 return texrow.rows();
1507 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1509 unsigned int lines = 0;
1511 ParagraphList::iterator beg = paragraphs.begin();
1512 ParagraphList::iterator end = paragraphs.end();
1513 ParagraphList::iterator it = beg;
1514 for (; it != end; ++it) {
1515 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1516 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1523 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1525 unsigned int lines = 0;
1527 vector<string> environment_stack(10);
1528 vector<string> environment_inner(10);
1530 int const command_depth = 0;
1533 Paragraph::depth_type depth = 0; // paragraph depth
1535 ParagraphList::iterator pit = paragraphs.begin();
1536 ParagraphList::iterator pend = paragraphs.end();
1538 for (; pit != pend; ++pit) {
1540 int desc_on = 0; // description mode
1542 LyXLayout_ptr const & style = pit->layout();
1544 // environment tag closing
1545 for (; depth > pit->params().depth(); --depth) {
1546 if (environment_inner[depth] != "!-- --") {
1547 item_name = "listitem";
1548 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1549 if (environment_inner[depth] == "varlistentry")
1550 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1552 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1553 environment_stack[depth].erase();
1554 environment_inner[depth].erase();
1557 if (depth == pit->params().depth()
1558 && environment_stack[depth] != style->latexname()
1559 && !environment_stack[depth].empty()) {
1560 if (environment_inner[depth] != "!-- --") {
1561 item_name= "listitem";
1562 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1563 if (environment_inner[depth] == "varlistentry")
1564 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1567 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1569 environment_stack[depth].erase();
1570 environment_inner[depth].erase();
1573 // Write opening SGML tags.
1574 switch (style->latextype) {
1575 case LATEX_PARAGRAPH:
1576 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1580 buf->sgmlError(&*pit, 0, _("Error: LatexType Command not allowed here.\n"));
1584 case LATEX_ENVIRONMENT:
1585 case LATEX_ITEM_ENVIRONMENT:
1586 if (depth < pit->params().depth()) {
1587 depth = pit->params().depth();
1588 environment_stack[depth].erase();
1591 if (environment_stack[depth] != style->latexname()) {
1592 if (environment_stack.size() == depth + 1) {
1593 environment_stack.push_back("!-- --");
1594 environment_inner.push_back("!-- --");
1596 environment_stack[depth] = style->latexname();
1597 environment_inner[depth] = "!-- --";
1598 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1600 if (environment_inner[depth] != "!-- --") {
1601 item_name= "listitem";
1602 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1603 if (environment_inner[depth] == "varlistentry")
1604 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1608 if (style->latextype == LATEX_ENVIRONMENT) {
1609 if (!style->latexparam().empty()) {
1610 if (style->latexparam() == "CDATA")
1613 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1618 desc_on = (style->labeltype == LABEL_MANUAL);
1620 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1621 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1623 item_name = desc_on ? "term" : "para";
1624 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1628 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1632 buf->simpleDocBookOnePar(os, &*pit, desc_on, depth + 1 + command_depth);
1635 // write closing SGML tags
1636 switch (style->latextype) {
1637 case LATEX_ENVIRONMENT:
1638 if (!style->latexparam().empty()) {
1639 if (style->latexparam() == "CDATA")
1642 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1645 case LATEX_ITEM_ENVIRONMENT:
1646 if (desc_on == 1) break;
1648 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1650 case LATEX_PARAGRAPH:
1651 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1654 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1660 for (int d = depth; d >= 0; --d) {
1661 if (!environment_stack[depth].empty()) {
1662 if (environment_inner[depth] != "!-- --") {
1663 item_name = "listitem";
1664 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1665 if (environment_inner[depth] == "varlistentry")
1666 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1669 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1677 void InsetText::validate(LaTeXFeatures & features) const
1679 for_each(paragraphs.begin(), paragraphs.end(),
1680 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1684 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1686 if (the_locking_inset) {
1687 the_locking_inset->getCursorPos(bv, x, y);
1690 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1691 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1695 int InsetText::insetInInsetY() const
1697 if (!the_locking_inset)
1700 return (inset_y + the_locking_inset->insetInInsetY());
1704 void InsetText::toggleInsetCursor(BufferView * bv)
1706 if (the_locking_inset) {
1707 the_locking_inset->toggleInsetCursor(bv);
1711 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1713 int const asc = font_metrics::maxAscent(font);
1714 int const desc = font_metrics::maxDescent(font);
1716 if (isCursorVisible())
1717 bv->hideLockedInsetCursor();
1719 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1720 toggleCursorVisible();
1724 void InsetText::showInsetCursor(BufferView * bv, bool show)
1726 if (the_locking_inset) {
1727 the_locking_inset->showInsetCursor(bv, show);
1730 if (!isCursorVisible()) {
1731 LyXFont const font =
1732 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1734 int const asc = font_metrics::maxAscent(font);
1735 int const desc = font_metrics::maxDescent(font);
1737 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1739 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1740 setCursorVisible(true);
1745 void InsetText::hideInsetCursor(BufferView * bv)
1747 if (isCursorVisible()) {
1748 bv->hideLockedInsetCursor();
1749 setCursorVisible(false);
1751 if (the_locking_inset)
1752 the_locking_inset->hideInsetCursor(bv);
1756 void InsetText::fitInsetCursor(BufferView * bv) const
1758 if (the_locking_inset) {
1759 the_locking_inset->fitInsetCursor(bv);
1762 LyXFont const font =
1763 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1765 int const asc = font_metrics::maxAscent(font);
1766 int const desc = font_metrics::maxDescent(font);
1768 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1769 need_update |= FULL;
1774 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1776 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1777 return moveLeftIntern(bv, false, activate_inset, selecting);
1779 return moveRightIntern(bv, true, activate_inset, selecting);
1784 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1786 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1787 return moveRightIntern(bv, true, activate_inset, selecting);
1789 return moveLeftIntern(bv, false, activate_inset, selecting);
1794 InsetText::moveRightIntern(BufferView * bv, bool front,
1795 bool activate_inset, bool selecting)
1797 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1798 return FINISHED_RIGHT;
1799 if (activate_inset && checkAndActivateInset(bv, front))
1801 getLyXText(bv)->cursorRight(bv);
1803 getLyXText(bv)->clearSelection();
1804 return DISPATCHED_NOUPDATE;
1809 InsetText::moveLeftIntern(BufferView * bv, bool front,
1810 bool activate_inset, bool selecting)
1812 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1814 getLyXText(bv)->cursorLeft(bv);
1816 getLyXText(bv)->clearSelection();
1817 if (activate_inset && checkAndActivateInset(bv, front))
1819 return DISPATCHED_NOUPDATE;
1823 Inset::RESULT InsetText::moveUp(BufferView * bv)
1825 if (crow(bv) == getLyXText(bv)->rows().begin())
1827 getLyXText(bv)->cursorUp(bv);
1828 getLyXText(bv)->clearSelection();
1829 return DISPATCHED_NOUPDATE;
1833 Inset::RESULT InsetText::moveDown(BufferView * bv)
1835 if (boost::next(crow(bv)) == getLyXText(bv)->rows().end())
1836 return FINISHED_DOWN;
1837 getLyXText(bv)->cursorDown(bv);
1838 getLyXText(bv)->clearSelection();
1839 return DISPATCHED_NOUPDATE;
1843 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1845 if (the_locking_inset) {
1846 if (the_locking_inset->insetAllowed(inset))
1847 return the_locking_inset->insertInset(bv, inset);
1850 inset->setOwner(this);
1851 hideInsetCursor(bv);
1852 getLyXText(bv)->insertInset(inset);
1854 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1859 bool InsetText::insetAllowed(Inset::Code code) const
1861 // in_insetAllowed is a really gross hack,
1862 // to allow us to call the owner's insetAllowed
1863 // without stack overflow, which can happen
1864 // when the owner uses InsetCollapsable::insetAllowed()
1866 if (in_insetAllowed)
1868 in_insetAllowed = true;
1869 if (the_locking_inset)
1870 ret = the_locking_inset->insetAllowed(code);
1872 ret = owner()->insetAllowed(code);
1873 in_insetAllowed = false;
1878 UpdatableInset * InsetText::getLockingInset() const
1880 return the_locking_inset ? the_locking_inset->getLockingInset() :
1881 const_cast<InsetText *>(this);
1885 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1889 if (the_locking_inset)
1890 return the_locking_inset->getFirstLockingInsetOfType(c);
1895 bool InsetText::showInsetDialog(BufferView * bv) const
1897 if (the_locking_inset)
1898 return the_locking_inset->showInsetDialog(bv);
1903 vector<string> const InsetText::getLabelList() const
1905 vector<string> label_list;
1907 ParagraphList::iterator pit = paragraphs.begin();
1908 ParagraphList::iterator pend = paragraphs.end();
1909 for (; pit != pend; ++pit) {
1910 InsetList::iterator beg = pit->insetlist.begin();
1911 InsetList::iterator end = pit->insetlist.end();
1912 for (; beg != end; ++beg) {
1913 vector<string> const l = beg.getInset()->getLabelList();
1914 label_list.insert(label_list.end(), l.begin(), l.end());
1921 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1924 if (the_locking_inset) {
1925 the_locking_inset->setFont(bv, font, toggleall, selectall);
1928 if ((boost::next(paragraphs.begin()) == paragraphs.end() &&
1929 paragraphs.begin()->empty()) || cpar(bv)->empty()) {
1930 getLyXText(bv)->setFont(font, toggleall);
1935 lt = getLyXText(bv);
1938 if (lt->selection.set()) {
1939 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1943 lt->toggleFree(font, toggleall);
1945 lt->clearSelection();
1947 bool flag = (selectall || lt->selection.set());
1951 updateLocal(bv, FULL, true);
1953 updateLocal(bv, CURSOR_PAR, true);
1957 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1959 if (cpar(bv)->isInset(cpos(bv))) {
1961 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1962 if (!isHighlyEditableInset(inset))
1964 inset->edit(bv, front);
1965 if (!the_locking_inset)
1967 updateLocal(bv, CURSOR, false);
1974 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1975 mouse_button::state button)
1977 x -= drawTextXOffset;
1979 int dummyy = y + insetAscent;
1980 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1981 // we only do the edit() call if the inset was hit by the mouse
1982 // or if it is a highly editable inset. So we should call this
1983 // function from our own edit with button < 0.
1984 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1986 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1994 inset_x = cix(bv) - top_x + drawTextXOffset;
1995 inset_y = ciy(bv) + drawTextYOffset;
1996 inset->edit(bv, x - inset_x, y - inset_y, button);
1997 if (!the_locking_inset)
1999 updateLocal(bv, CURSOR, false);
2006 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2009 int w = UpdatableInset::getMaxWidth(bv, inset);
2014 w = w - top_x + owner()->x();
2017 w -= (2 * TEXT_TO_INSET_OFFSET);
2020 return UpdatableInset::getMaxWidth(bv, inset);
2025 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2027 // we have to unlock any locked inset otherwise we're in troubles
2028 the_locking_inset = 0;
2031 paragraphs.set(new Paragraph(*p, same_id));
2032 paragraphs.begin()->setInsetOwner(this);
2033 Paragraph * np = &*(paragraphs.begin());
2036 np->next(new Paragraph(*p, same_id));
2037 np->next()->previous(np);
2039 np->setInsetOwner(this);
2046 void InsetText::markNew(bool track_changes)
2048 ParagraphList::iterator pit = paragraphs.begin();
2049 ParagraphList::iterator pend = paragraphs.end();
2050 for (; pit != pend; ++pit) {
2051 if (track_changes) {
2052 pit->trackChanges();
2054 // no-op when not tracking
2055 pit->cleanChanges();
2061 void InsetText::setText(string const & data, LyXFont const & font)
2064 for (unsigned int i = 0; i < data.length(); ++i)
2065 paragraphs.begin()->insertChar(i, data[i], font);
2070 void InsetText::setAutoBreakRows(bool flag)
2072 if (flag != autoBreakRows) {
2073 autoBreakRows = flag;
2081 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2083 if (how != drawFrame_) {
2086 updateLocal(bv, DRAW_FRAME, false);
2091 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2093 if (frame_color != col) {
2096 updateLocal(bv, DRAW_FRAME, false);
2101 int InsetText::cx(BufferView * bv) const
2103 // we do nothing dangerous so we use a local cache
2104 LyXText * llt = getLyXText(bv);
2105 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2106 if (the_locking_inset) {
2107 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2109 if (font.isVisibleRightToLeft())
2110 x -= the_locking_inset->width(bv, font);
2116 int InsetText::cix(BufferView * bv) const
2118 // we do nothing dangerous so we use a local cache
2119 LyXText * llt = getLyXText(bv);
2120 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2121 if (the_locking_inset) {
2122 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2124 if (font.isVisibleRightToLeft())
2125 x -= the_locking_inset->width(bv, font);
2131 int InsetText::cy(BufferView * bv) const
2134 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2138 int InsetText::ciy(BufferView * bv) const
2141 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2145 pos_type InsetText::cpos(BufferView * bv) const
2147 return getLyXText(bv)->cursor.pos();
2151 Paragraph * InsetText::cpar(BufferView * bv) const
2153 return getLyXText(bv)->cursor.par();
2157 bool InsetText::cboundary(BufferView * bv) const
2159 return getLyXText(bv)->cursor.boundary();
2163 RowList::iterator InsetText::crow(BufferView * bv) const
2165 return getLyXText(bv)->cursor.row();
2169 LyXText * InsetText::getLyXText(BufferView const * lbv,
2170 bool const recursive) const
2172 if (cached_bview == lbv) {
2173 if (recursive && the_locking_inset)
2174 return the_locking_inset->getLyXText(lbv, true);
2175 LyXText * lt = cached_text.get();
2176 lyx::Assert(lt && lt->rows().begin()->par() == &*(paragraphs.begin()));
2179 // Super UGLY! (Lgb)
2180 BufferView * bv = const_cast<BufferView *>(lbv);
2183 Cache::iterator it = cache.find(bv);
2185 if (it != cache.end()) {
2188 } else if (do_resize) {
2189 resizeLyXText(do_resize);
2191 if (lt || !it->second.remove) {
2192 lyx::Assert(it->second.text.get());
2193 cached_text = it->second.text;
2194 if (recursive && the_locking_inset) {
2195 return the_locking_inset->getLyXText(bv, true);
2197 return cached_text.get();
2198 } else if (it->second.remove) {
2200 saveLyXTextState(it->second.text.get());
2206 // when we have to reinit the existing LyXText!
2208 it->second.text->init(bv);
2209 restoreLyXTextState(it->second.text.get());
2210 it->second.remove = false;
2212 cached_text = it->second.text;
2213 if (the_locking_inset && recursive) {
2214 return the_locking_inset->getLyXText(bv);
2216 return cached_text.get();
2219 // we are here only if we don't have a BufferView * in the cache!!!
2221 cached_text.reset(new LyXText(bv, const_cast<InsetText *>(this)));
2222 cached_text->init(bv);
2223 restoreLyXTextState(cached_text.get());
2225 cache.insert(make_pair(bv, cached_text));
2227 if (the_locking_inset && recursive) {
2228 return the_locking_inset->getLyXText(bv);
2230 return cached_text.get();
2234 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2238 Cache::iterator it = cache.find(bv);
2240 if (it == cache.end()) {
2244 lyx::Assert(it->second.text.get());
2246 it->second.remove = true;
2248 /// then remove all LyXText in text-insets
2249 for_each(paragraphs.begin(), paragraphs.end(),
2250 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2255 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2258 // we cannot resize this because we are in use!
2259 // so do this on the next possible getLyXText()
2264 if (boost::next(paragraphs.begin()) == paragraphs.end() &&
2265 paragraphs.begin()->empty()) { // no data, resize not neccessary!
2266 // we have to do this as a fixed width may have changed!
2267 LyXText * t = getLyXText(bv);
2268 saveLyXTextState(t);
2270 restoreLyXTextState(t);
2273 // one endless line, resize normally not necessary
2274 if (!force && getMaxWidth(bv, this) < 0)
2277 Cache::iterator it = cache.find(bv);
2278 if (it == cache.end()) {
2281 lyx::Assert(it->second.text.get());
2283 LyXText * t = it->second.text.get();
2284 saveLyXTextState(t);
2286 for_each(paragraphs.begin(), paragraphs.end(),
2287 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2290 restoreLyXTextState(t);
2291 if (the_locking_inset) {
2292 inset_x = cix(bv) - top_x + drawTextXOffset;
2293 inset_y = ciy(bv) + drawTextYOffset;
2296 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2298 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2299 // this will scroll the screen such that the cursor becomes visible
2300 bv->updateScrollbar();
2302 need_update |= FULL;
2307 void InsetText::reinitLyXText() const
2310 // we cannot resize this because we are in use!
2311 // so do this on the next possible getLyXText()
2317 for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2318 lyx::Assert(it->second.text.get());
2320 LyXText * t = it->second.text.get();
2321 BufferView * bv = it->first;
2323 saveLyXTextState(t);
2325 for_each(paragraphs.begin(), paragraphs.end(),
2326 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2329 restoreLyXTextState(t);
2330 if (the_locking_inset) {
2331 inset_x = cix(bv) - top_x + drawTextXOffset;
2332 inset_y = ciy(bv) + drawTextYOffset;
2334 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2336 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2337 // this will scroll the screen such that the cursor becomes visible
2338 bv->updateScrollbar();
2346 void InsetText::removeNewlines()
2348 bool changed = false;
2350 ParagraphList::iterator it = paragraphs.begin();
2351 ParagraphList::iterator end = paragraphs.end();
2352 for (; it != end; ++it) {
2353 for (int i = 0; i < it->size(); ++i) {
2354 if (it->isNewline(i)) {
2365 bool InsetText::nodraw() const
2367 if (the_locking_inset)
2368 return the_locking_inset->nodraw();
2369 return UpdatableInset::nodraw();
2373 int InsetText::scroll(bool recursive) const
2375 int sx = UpdatableInset::scroll(false);
2377 if (recursive && the_locking_inset)
2378 sx += the_locking_inset->scroll(recursive);
2384 void InsetText::selectAll(BufferView * bv)
2386 getLyXText(bv)->cursorTop();
2387 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2388 getLyXText(bv)->cursorBottom();
2389 getLyXText(bv)->setSelection();
2393 void InsetText::clearSelection(BufferView * bv)
2395 getLyXText(bv)->clearSelection();
2399 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2401 Painter & pain = bv->painter();
2403 int h = insetAscent + insetDescent;
2404 int ty = baseline - insetAscent;
2410 if ((ty + h) > pain.paperHeight())
2411 h = pain.paperHeight();
2412 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2413 w = pain.paperWidth();
2414 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2419 Paragraph * InsetText::firstParagraph() const
2422 if (the_locking_inset)
2423 if ((result = the_locking_inset->firstParagraph()))
2425 return &*(paragraphs.begin());
2429 Paragraph * InsetText::getFirstParagraph(int i) const
2431 return (i == 0) ? &*(paragraphs.begin()) : 0;
2435 LyXCursor const & InsetText::cursor(BufferView * bv) const
2437 if (the_locking_inset)
2438 return the_locking_inset->cursor(bv);
2439 return getLyXText(bv)->cursor;
2443 void InsetText::paragraph(Paragraph * p)
2445 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2446 // caller of this function has to take care of it. This IS important
2447 // as we could have to insert a paragraph before this one and just
2448 // link the actual to a new ones next and set it with this function
2451 // set ourself as owner for all the paragraphs inserted!
2452 for_each(paragraphs.begin(), paragraphs.end(),
2453 boost::bind(&Paragraph::setInsetOwner, _1, this));
2456 // redraw myself when asked for
2461 Inset * InsetText::getInsetFromID(int id_arg) const
2464 return const_cast<InsetText *>(this);
2466 ParagraphList::iterator pit = paragraphs.begin();
2467 ParagraphList::iterator pend = paragraphs.end();
2468 for (; pit != pend; ++pit) {
2469 InsetList::iterator it = pit->insetlist.begin();
2470 InsetList::iterator end = pit->insetlist.end();
2471 for (; it != end; ++it) {
2472 if (it.getInset()->id() == id_arg)
2473 return it.getInset();
2474 Inset * in = it.getInset()->getInsetFromID(id_arg);
2484 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2485 float & value) const
2491 lt = getLyXText(bv);
2494 if (the_locking_inset) {
2495 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2496 if (!word.word().empty()) {
2502 // we have to go on checking so move cursor to the next char
2503 lt->cursor.pos(lt->cursor.pos() + 1);
2505 word = lt->selectNextWordToSpellcheck(value);
2506 if (word.word().empty())
2507 bv->unlockInset(const_cast<InsetText *>(this));
2516 void InsetText::selectSelectedWord(BufferView * bv)
2518 if (the_locking_inset) {
2519 the_locking_inset->selectSelectedWord(bv);
2522 getLyXText(bv)->selectSelectedWord();
2523 updateLocal(bv, SELECTION, false);
2527 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2529 if (the_locking_inset) {
2530 the_locking_inset->toggleSelection(bv, kill_selection);
2534 lt = getLyXText(bv);
2538 int x = top_x + TEXT_TO_INSET_OFFSET;
2540 RowList::iterator rowit = lt->rows().begin();
2541 RowList::iterator end = lt->rows().end();
2542 int y_offset = top_baseline - rowit->ascent_of_text();
2544 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2545 y += rowit->height();
2551 if (need_update & SELECTION)
2553 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2559 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2563 lt = getLyXText(bv);
2566 if (the_locking_inset) {
2567 if (the_locking_inset->nextChange(bv, length))
2569 lt->cursorRight(true);
2571 lyxfind::SearchResult result =
2572 lyxfind::findNextChange(bv, lt, length);
2574 if (result == lyxfind::SR_FOUND) {
2575 LyXCursor cur = lt->cursor;
2576 bv->unlockInset(bv->theLockingInset());
2577 if (bv->lockInset(this))
2580 lt->setSelectionRange(length);
2581 updateLocal(bv, SELECTION, false);
2585 return result != lyxfind::SR_NOT_FOUND;
2589 bool InsetText::searchForward(BufferView * bv, string const & str,
2594 lt = getLyXText(bv);
2597 if (the_locking_inset) {
2598 if (the_locking_inset->searchForward(bv, str, cs, mw))
2600 lt->cursorRight(true);
2602 lyxfind::SearchResult result =
2603 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2605 if (result == lyxfind::SR_FOUND) {
2606 LyXCursor cur = lt->cursor;
2607 bv->unlockInset(bv->theLockingInset());
2608 if (bv->lockInset(this))
2611 lt->setSelectionRange(str.length());
2612 updateLocal(bv, SELECTION, false);
2616 return (result != lyxfind::SR_NOT_FOUND);
2619 bool InsetText::searchBackward(BufferView * bv, string const & str,
2622 if (the_locking_inset) {
2623 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2628 lt = getLyXText(bv);
2632 ParagraphList::iterator pit = paragraphs.begin();
2633 ParagraphList::iterator pend = paragraphs.end();
2635 while (boost::next(pit) != pend)
2638 lt->setCursor(&*pit, pit->size());
2640 lyxfind::SearchResult result =
2641 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2643 if (result == lyxfind::SR_FOUND) {
2644 LyXCursor cur = lt->cursor;
2645 bv->unlockInset(bv->theLockingInset());
2646 if (bv->lockInset(this))
2649 lt->setSelectionRange(str.length());
2650 updateLocal(bv, SELECTION, false);
2654 return (result != lyxfind::SR_NOT_FOUND);
2658 bool InsetText::checkInsertChar(LyXFont & font)
2661 return owner()->checkInsertChar(font);
2666 void InsetText::collapseParagraphs(BufferView * bv)
2668 LyXText * llt = getLyXText(bv);
2670 while (boost::next(paragraphs.begin()) != paragraphs.end()) {
2671 if (!paragraphs.begin()->empty() &&
2672 !boost::next(paragraphs.begin())->empty() &&
2673 !paragraphs.begin()->isSeparator(paragraphs.begin()->size() - 1))
2675 paragraphs.begin()->insertChar(paragraphs.begin()->size(), ' ');
2677 if (llt->selection.set()) {
2678 if (llt->selection.start.par() == boost::next(paragraphs.begin())) {
2679 llt->selection.start.par(&*(paragraphs.begin()));
2680 llt->selection.start.pos(
2681 llt->selection.start.pos() + paragraphs.begin()->size());
2683 if (llt->selection.end.par() == boost::next(paragraphs.begin())) {
2684 llt->selection.end.par(&*(paragraphs.begin()));
2685 llt->selection.end.pos(
2686 llt->selection.end.pos() + paragraphs.begin()->size());
2689 mergeParagraph(bv->buffer()->params, paragraphs, paragraphs.begin());
2695 void InsetText::getDrawFont(LyXFont & font) const
2699 owner()->getDrawFont(font);
2703 void InsetText::appendParagraphs(Buffer * buffer,
2706 BufferParams const & bparams = buffer->params;
2708 Paragraph * tmpbuf = newpar;
2709 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2710 if (bparams.tracking_changes)
2711 buf->cleanChanges();
2713 while (tmpbuf->next()) {
2714 tmpbuf = tmpbuf->next();
2715 lastbuffer->next(new Paragraph(*tmpbuf, false));
2716 lastbuffer->next()->previous(lastbuffer);
2717 lastbuffer = lastbuffer->next();
2718 if (bparams.tracking_changes)
2719 lastbuffer->cleanChanges();
2721 lastbuffer = &*(paragraphs.begin());
2722 while (lastbuffer->next())
2723 lastbuffer = lastbuffer->next();
2724 if (!newpar->empty() && !lastbuffer->empty() &&
2725 !lastbuffer->isSeparator(lastbuffer->size() - 1))
2727 lastbuffer->insertChar(lastbuffer->size(), ' ');
2730 // make the buf exactly the same layout than our last paragraph
2731 buf->makeSameLayout(lastbuffer);
2734 lastbuffer->next(buf);
2735 buf->previous(lastbuffer);
2736 mergeParagraph(buffer->params, paragraphs, lastbuffer);
2742 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2744 ParagraphList::iterator pit = paragraphs.begin();
2745 ParagraphList::iterator pend = paragraphs.end();
2747 for (; pit != pend; ++pit) {
2748 InsetList::iterator it = pit->insetlist.begin();
2749 InsetList::iterator end = pit->insetlist.end();
2750 for (; it != end; ++it) {
2751 it.getInset()->addPreview(loader);