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, 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)
549 if (!autoBreakRows &&
550 boost::next(paragraphs.begin()) != paragraphs.end())
551 collapseParagraphs(bv);
553 if (!autoBreakRows && paragraphs.size() > 1)
554 collapseParagraphs(bv);
563 setUpdateStatus(bv, what);
564 bool flag = mark_dirty ||
565 (((need_update != CURSOR) && (need_update != NONE)) ||
566 (lt->refreshStatus() != LyXText::REFRESH_NONE) || lt->selection.set());
567 if (!lt->selection.set())
568 lt->selection.cursor = lt->cursor;
574 bv->updateInset(const_cast<InsetText *>(this));
580 if (need_update == CURSOR)
582 bv->owner()->view_state_changed();
583 bv->owner()->updateMenubar();
584 bv->owner()->updateToolbar();
585 if (old_par != cpar(bv)) {
586 bv->owner()->setLayout(cpar(bv)->layout()->name());
592 string const InsetText::editMessage() const
594 return _("Opened Text Inset");
598 void InsetText::edit(BufferView * bv, int x, int y, mouse_button::state button)
600 UpdatableInset::edit(bv, x, y, button);
602 if (!bv->lockInset(this)) {
603 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
607 the_locking_inset = 0;
608 inset_pos = inset_x = inset_y = 0;
609 inset_boundary = false;
612 int tmp_y = (y < 0) ? 0 : y;
618 // we put here -1 and not button as now the button in the
619 // edit call should not be needed we will fix this in 1.3.x
620 // cycle hopefully (Jug 20020509)
621 // FIXME: GUII I've changed this to none: probably WRONG
622 if (!checkAndActivateInset(bv, x, tmp_y, mouse_button::none)) {
623 lt->setCursorFromCoordinates(x - drawTextXOffset,
625 lt->cursor.x_fix(lt->cursor.x());
627 lt->clearSelection();
629 // If the inset is empty set the language of the current font to the
630 // language to the surronding text (if different).
631 if (paragraphs.begin()->empty() &&
632 boost::next(paragraphs.begin()) == paragraphs.end()&&
633 bv->getParentLanguage(this) != lt->current_font.language())
635 LyXFont font(LyXFont::ALL_IGNORE);
636 font.setLanguage(bv->getParentLanguage(this));
637 setFont(bv, font, false);
643 if (drawFrame_ == LOCKED)
644 code = CURSOR|DRAW_FRAME;
645 updateLocal(bv, code, false);
648 // Tell the paragraph dialog that we've entered an insettext.
649 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
653 void InsetText::edit(BufferView * bv, bool front)
655 UpdatableInset::edit(bv, front);
657 if (!bv->lockInset(this)) {
658 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
662 the_locking_inset = 0;
663 inset_pos = inset_x = inset_y = 0;
664 inset_boundary = false;
673 lt->setCursor(&*(paragraphs.begin()), 0);
675 ParagraphList::iterator it = paragraphs.begin();
676 ParagraphList::iterator end = paragraphs.end();
677 while (boost::next(it) != end)
679 // int const pos = (p->size() ? p->size()-1 : p->size());
680 lt->setCursor(&*it, it->size());
682 lt->clearSelection();
684 // If the inset is empty set the language of the current font to the
685 // language to the surronding text (if different).
686 if (paragraphs.begin()->empty() &&
687 boost::next(paragraphs.begin()) == paragraphs.end() &&
688 bv->getParentLanguage(this) != lt->current_font.language()) {
689 LyXFont font(LyXFont::ALL_IGNORE);
690 font.setLanguage(bv->getParentLanguage(this));
691 setFont(bv, font, false);
696 if (drawFrame_ == LOCKED)
697 code = CURSOR|DRAW_FRAME;
698 updateLocal(bv, code, false);
703 void InsetText::insetUnlock(BufferView * bv)
705 if (the_locking_inset) {
706 the_locking_inset->insetUnlock(bv);
707 the_locking_inset = 0;
708 updateLocal(bv, CURSOR_PAR, false);
720 if (lt->selection.set()) {
721 lt->clearSelection();
723 } else if (owner()) {
724 bv->owner()->setLayout(owner()->getLyXText(bv)
725 ->cursor.par()->layout()->name());
727 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
728 // hack for deleteEmptyParMech
729 if (!paragraphs.begin()->empty()) {
730 lt->setCursor(&*(paragraphs.begin()), 0);
731 } else if (boost::next(paragraphs.begin()) != paragraphs.end()) {
732 lt->setCursor(&*boost::next(paragraphs.begin()), 0);
737 updateLocal(bv, code, false);
740 setUpdateStatus(bv, code);
745 void InsetText::lockInset(BufferView * bv)
748 the_locking_inset = 0;
749 inset_pos = inset_x = inset_y = 0;
750 inset_boundary = false;
758 lt->setCursor(&*(paragraphs.begin()), 0);
759 lt->clearSelection();
761 // If the inset is empty set the language of the current font to the
762 // language to the surronding text (if different).
763 if (paragraphs.begin()->empty() &&
764 boost::next(paragraphs.begin()) == paragraphs.end() &&
765 bv->getParentLanguage(this) != lt->current_font.language()) {
766 LyXFont font(LyXFont::ALL_IGNORE);
767 font.setLanguage(bv->getParentLanguage(this));
768 setFont(bv, font, false);
773 if (drawFrame_ == LOCKED)
774 code = CURSOR|DRAW_FRAME;
775 setUpdateStatus(bv, code);
779 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
781 the_locking_inset = inset;
782 inset_x = cix(bv) - top_x + drawTextXOffset;
783 inset_y = ciy(bv) + drawTextYOffset;
784 inset_pos = cpos(bv);
785 inset_par = cpar(bv);
786 inset_boundary = cboundary(bv);
787 updateLocal(bv, CURSOR, false);
791 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
793 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
797 if (!the_locking_inset) {
798 ParagraphList::iterator pit = paragraphs.begin();
799 ParagraphList::iterator pend = paragraphs.end();
801 int const id = inset->id();
802 for (; pit != pend; ++pit) {
803 InsetList::iterator it =
804 pit->insetlist.begin();
805 InsetList::iterator const end =
806 pit->insetlist.end();
807 for (; it != end; ++it) {
808 if (it.getInset() == inset) {
809 getLyXText(bv)->setCursorIntern(&*pit, it.getPos());
810 lockInset(bv, inset);
813 if (it.getInset()->getInsetFromID(id)) {
814 getLyXText(bv)->setCursorIntern(&*pit, it.getPos());
815 it.getInset()->edit(bv);
816 return the_locking_inset->lockInsetInInset(bv, inset);
822 if (inset == cpar(bv)->getInset(cpos(bv))) {
823 lyxerr[Debug::INSETS] << "OK" << endl;
824 lockInset(bv, inset);
826 } else if (the_locking_inset && (the_locking_inset == inset)) {
827 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
828 lyxerr[Debug::INSETS] << "OK" << endl;
829 inset_x = cix(bv) - top_x + drawTextXOffset;
830 inset_y = ciy(bv) + drawTextYOffset;
832 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
834 } else if (the_locking_inset) {
835 lyxerr[Debug::INSETS] << "MAYBE" << endl;
836 return the_locking_inset->lockInsetInInset(bv, inset);
838 lyxerr[Debug::INSETS] << "NOT OK" << endl;
843 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
846 if (!the_locking_inset)
848 if (the_locking_inset == inset) {
849 the_locking_inset->insetUnlock(bv);
850 getLyXText(bv)->updateInset(inset);
851 the_locking_inset = 0;
853 moveRightIntern(bv, true, false);
854 old_par = 0; // force layout setting
858 updateLocal(bv, CURSOR, false);
861 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
865 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
867 if (!autoBreakRows &&
868 boost::next(paragraphs.begin()) != paragraphs.end())
869 collapseParagraphs(bv);
877 if (inset->owner() != this) {
878 int ustat = CURSOR_PAR;
880 UpdatableInset * tl_inset = the_locking_inset;
882 found = tl_inset->updateInsetInInset(bv, inset);
884 tl_inset = static_cast<UpdatableInset *>(inset);
885 while(tl_inset->owner() && tl_inset->owner() != this)
886 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
887 if (!tl_inset->owner())
889 found = tl_inset->updateInsetInInset(bv, inset);
893 lt->updateInset(tl_inset);
897 setUpdateStatus(bv, ustat);
900 bool found = lt->updateInset(inset);
904 setUpdateStatus(bv, CURSOR_PAR);
905 if (the_locking_inset &&
906 cpar(bv) == inset_par && cpos(bv) == inset_pos)
908 inset_x = cix(bv) - top_x + drawTextXOffset;
909 inset_y = ciy(bv) + drawTextYOffset;
916 void InsetText::lfunMousePress(FuncRequest const & cmd)
920 // use this to check mouse motion for selection!
924 BufferView * bv = cmd.view();
925 FuncRequest cmd1 = cmd;
931 int tmp_x = cmd.x - drawTextXOffset;
932 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->top_y();
933 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
936 if (the_locking_inset) {
937 if (the_locking_inset == inset) {
938 the_locking_inset->localDispatch(cmd1);
941 // otherwise only unlock the_locking_inset
942 the_locking_inset->insetUnlock(bv);
943 the_locking_inset = 0;
946 no_selection = false;
948 if (bv->theLockingInset()) {
949 if (isHighlyEditableInset(inset)) {
950 // We just have to lock the inset before calling a
952 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
953 if (!bv->lockInset(uinset)) {
954 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
956 inset->localDispatch(cmd1);
957 if (the_locking_inset)
958 updateLocal(bv, CURSOR, false);
963 bool paste_internally = false;
964 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
965 localDispatch(FuncRequest(bv, LFUN_COPY));
966 paste_internally = true;
973 int old_top_y = lt->top_y();
975 lt->setCursorFromCoordinates(cmd.x - drawTextXOffset,
976 cmd.y + insetAscent);
977 // set the selection cursor!
978 lt->selection.cursor = lt->cursor;
979 lt->cursor.x_fix(lt->cursor.x());
981 if (lt->selection.set()) {
982 lt->clearSelection();
983 updateLocal(bv, FULL, false);
985 lt->clearSelection();
986 updateLocal(bv, CURSOR, false);
992 bv->owner()->setLayout(cpar(bv)->layout()->name());
994 // we moved the view we cannot do mouse selection in this case!
995 if (getLyXText(bv)->top_y() != old_top_y)
998 // Insert primary selection with middle mouse
999 // if there is a local selection in the current buffer,
1001 if (cmd.button() == mouse_button::button2) {
1002 if (paste_internally)
1003 localDispatch(FuncRequest(bv, LFUN_PASTE));
1005 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
1008 getLyXText(bv)->clearSelection();
1010 showInsetCursor(bv);
1014 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
1016 BufferView * bv = cmd.view();
1017 FuncRequest cmd1 = cmd;
1021 no_selection = true;
1022 if (the_locking_inset)
1023 return the_locking_inset->localDispatch(cmd1);
1025 int tmp_x = cmd.x - drawTextXOffset;
1026 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->top_y();
1027 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
1030 // This code should probably be removed now. Simple insets
1031 // (!highlyEditable) can actually take the localDispatch,
1032 // and turn it into edit() if necessary. But we still
1033 // need to deal properly with the whole relative vs.
1034 // absolute mouse co-ords thing in a realiable, sensible way
1036 if (isHighlyEditableInset(inset))
1037 ret = inset->localDispatch(cmd1);
1039 inset_x = cix(bv) - top_x + drawTextXOffset;
1040 inset_y = ciy(bv) + drawTextYOffset;
1041 cmd1.x = cmd.x - inset_x;
1042 cmd1.y = cmd.x - inset_y;
1043 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
1047 ret = inset->localDispatch(cmd1);
1048 updateLocal(bv, CURSOR_PAR, false);
1055 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
1057 FuncRequest cmd1 = cmd;
1061 if (the_locking_inset) {
1062 the_locking_inset->localDispatch(cmd1);
1066 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
1069 BufferView * bv = cmd.view();
1072 lt = getLyXText(bv);
1075 hideInsetCursor(bv);
1076 LyXCursor cur = lt->cursor;
1077 lt->setCursorFromCoordinates
1078 (cmd.x - drawTextXOffset, cmd.y + insetAscent);
1079 lt->cursor.x_fix(lt->cursor.x());
1080 if (cur == lt->cursor) {
1086 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1087 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1091 updateLocal(bv, SELECTION, false);
1093 showInsetCursor(bv);
1097 Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
1099 BufferView * bv = ev.view();
1100 switch (ev.action) {
1101 case LFUN_MOUSE_PRESS:
1104 case LFUN_MOUSE_MOTION:
1105 lfunMouseMotion(ev);
1107 case LFUN_MOUSE_RELEASE:
1108 return lfunMouseRelease(ev) ? DISPATCHED : UNDISPATCHED;
1113 bool was_empty = (paragraphs.begin()->empty() &&
1114 boost::next(paragraphs.begin()) == paragraphs.end());
1115 no_selection = false;
1116 RESULT result = UpdatableInset::localDispatch(ev);
1117 if (result != UNDISPATCHED)
1120 result = DISPATCHED;
1121 if (ev.action < 0 && ev.argument.empty())
1124 if (the_locking_inset) {
1125 result = the_locking_inset->localDispatch(ev);
1126 if (result == DISPATCHED_NOUPDATE)
1128 else if (result == DISPATCHED) {
1129 updateLocal(bv, CURSOR_PAR, false);
1131 } else if (result >= FINISHED) {
1133 case FINISHED_RIGHT:
1134 moveRightIntern(bv, false, false);
1135 result = DISPATCHED;
1138 if ((result = moveUp(bv)) >= FINISHED) {
1139 updateLocal(bv, CURSOR, false);
1140 bv->unlockInset(this);
1144 if ((result = moveDown(bv)) >= FINISHED) {
1145 updateLocal(bv, CURSOR, false);
1146 bv->unlockInset(this);
1150 result = DISPATCHED;
1153 the_locking_inset = 0;
1154 updateLocal(bv, CURSOR, false);
1155 // make sure status gets reset immediately
1156 bv->owner()->clearMessage();
1160 hideInsetCursor(bv);
1163 lt = getLyXText(bv);
1167 int updflag = false;
1169 // what type of update to do on a cursor movement
1170 int cursor_update = CURSOR;
1172 if (lt->selection.set())
1173 cursor_update = SELECTION;
1175 switch (ev.action) {
1178 case LFUN_SELFINSERT:
1179 if (bv->buffer()->isReadonly()) {
1180 // setErrorMessage(N_("Document is read only"));
1183 if (!ev.argument.empty()) {
1184 /* Automatically delete the currently selected
1185 * text and replace it with what is being
1186 * typed in now. Depends on lyxrc settings
1187 * "auto_region_delete", which defaults to
1190 // This should not be needed here and is also WRONG!
1191 setUndo(bv, Undo::INSERT,
1192 lt->cursor.par(), boost::next(lt->cursor.par()));
1195 if (lyxrc.auto_region_delete) {
1196 if (lt->selection.set()) {
1197 lt->cutSelection(false, false);
1200 lt->clearSelection();
1201 for (string::size_type i = 0; i < ev.argument.length(); ++i) {
1202 bv->owner()->getIntl().getTransManager().
1203 TranslateAndInsert(ev.argument[i], lt);
1206 lt->selection.cursor = lt->cursor;
1207 updwhat = CURSOR | CURSOR_PAR;
1209 result = DISPATCHED_NOUPDATE;
1212 // cursor movements that need special handling
1215 result = moveRight(bv);
1217 updwhat = cursor_update;
1221 result = moveLeft(bv);
1222 updwhat = cursor_update;
1226 result = moveDown(bv);
1227 updwhat = cursor_update;
1231 result = moveUp(bv);
1232 updwhat = cursor_update;
1236 if (crow(bv) == lt->rows().begin())
1237 result = FINISHED_UP;
1239 lt->cursorPrevious();
1240 lt->clearSelection();
1241 result = DISPATCHED_NOUPDATE;
1243 updwhat = cursor_update;
1247 if (boost::next(crow(bv)) == lt->rows().end())
1248 result = FINISHED_DOWN;
1251 lt->clearSelection();
1252 result = DISPATCHED_NOUPDATE;
1254 updwhat = cursor_update;
1257 case LFUN_BACKSPACE: {
1258 if (lt->selection.set())
1259 lt->cutSelection(true, false);
1262 updwhat = CURSOR_PAR;
1268 if (lt->selection.set()) {
1269 lt->cutSelection(true, false);
1273 updwhat = CURSOR_PAR;
1279 lt->cutSelection(bv);
1280 updwhat = CURSOR_PAR;
1287 lt->copySelection();
1288 updwhat = CURSOR_PAR;
1291 case LFUN_PASTESELECTION:
1293 string const clip(bv->getClipboard());
1297 if (ev.argument == "paragraph") {
1298 lt->insertStringAsParagraphs(clip);
1300 lt->insertStringAsLines(clip);
1303 lt->clearSelection();
1305 updwhat = CURSOR_PAR;
1311 if (!autoBreakRows) {
1312 if (CutAndPaste::nrOfParagraphs() > 1) {
1313 #ifdef WITH_WARNINGS
1314 #warning FIXME horrendously bad UI
1316 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1321 lt->pasteSelection();
1323 lt->clearSelection();
1324 updwhat = CURSOR_PAR;
1329 case LFUN_BREAKPARAGRAPH:
1330 if (!autoBreakRows) {
1331 result = DISPATCHED;
1334 lt->breakParagraph(paragraphs, 0);
1335 updwhat = CURSOR | FULL;
1339 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1340 if (!autoBreakRows) {
1341 result = DISPATCHED;
1344 lt->breakParagraph(paragraphs, 1);
1345 updwhat = CURSOR | FULL;
1349 case LFUN_BREAKLINE: {
1350 if (!autoBreakRows) {
1351 result = DISPATCHED;
1355 lt->insertInset(new InsetNewline);
1356 updwhat = CURSOR | CURSOR_PAR;
1362 // do not set layouts on non breakable textinsets
1363 if (autoBreakRows) {
1364 string cur_layout = cpar(bv)->layout()->name();
1366 // Derive layout number from given argument (string)
1367 // and current buffer's textclass (number). */
1368 LyXTextClass const & tclass =
1369 bv->buffer()->params.getLyXTextClass();
1370 string layout = ev.argument;
1371 bool hasLayout = tclass.hasLayout(layout);
1373 // If the entry is obsolete, use the new one instead.
1375 string const & obs =
1376 tclass[layout]->obsoleted_by();
1381 // see if we found the layout number:
1383 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + ev.argument + N_(" not known"));
1384 bv->owner()->dispatch(lf);
1388 if (cur_layout != layout) {
1389 cur_layout = layout;
1390 lt->setLayout(layout);
1391 bv->owner()->setLayout(cpar(bv)->layout()->name());
1392 updwhat = CURSOR_PAR;
1396 // reset the layout box
1397 bv->owner()->setLayout(cpar(bv)->layout()->name());
1400 case LFUN_PARAGRAPH_SPACING:
1401 // This one is absolutely not working. When fiddling with this
1402 // it also seems to me that the paragraphs inside the insettext
1403 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1404 // FIXME: how old is this comment ? ...
1406 ParagraphList::iterator pit = lt->cursor.par();
1407 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1408 float cur_value = 1.0;
1409 if (cur_spacing == Spacing::Other) {
1410 cur_value = pit->params().spacing().getValue();
1413 istringstream istr(ev.argument.c_str());
1416 Spacing::Space new_spacing = cur_spacing;
1417 float new_value = cur_value;
1419 lyxerr << "Missing argument to `paragraph-spacing'"
1421 } else if (tmp == "single") {
1422 new_spacing = Spacing::Single;
1423 } else if (tmp == "onehalf") {
1424 new_spacing = Spacing::Onehalf;
1425 } else if (tmp == "double") {
1426 new_spacing = Spacing::Double;
1427 } else if (tmp == "other") {
1428 new_spacing = Spacing::Other;
1431 lyxerr << "new_value = " << tmpval << endl;
1434 } else if (tmp == "default") {
1435 new_spacing = Spacing::Default;
1437 lyxerr << _("Unknown spacing argument: ")
1438 << ev.argument << endl;
1440 if (cur_spacing != new_spacing || cur_value != new_value) {
1441 pit->params().spacing(Spacing(new_spacing, new_value));
1442 updwhat = CURSOR_PAR;
1448 // These need to do repaints but don't require
1449 // special handling otherwise. A *lot* of the
1450 // above could probably be done similarly ...
1455 case LFUN_WORDRIGHT:
1456 // these two are really unhandled ...
1458 case LFUN_BEGINNINGBUF:
1459 updwhat = cursor_update;
1460 if (!bv->dispatch(ev))
1461 result = UNDISPATCHED;
1470 case LFUN_WORDLEFTSEL:
1471 case LFUN_WORDRIGHTSEL:
1472 updwhat = SELECTION;
1477 if (!bv->dispatch(ev))
1478 result = UNDISPATCHED;
1485 updateLocal(bv, updwhat, updflag);
1486 /// If the action has deleted all text in the inset, we need to change the
1487 // language to the language of the surronding text.
1488 if (!was_empty && paragraphs.begin()->empty() &&
1489 boost::next(paragraphs.begin()) == paragraphs.end()) {
1490 LyXFont font(LyXFont::ALL_IGNORE);
1491 font.setLanguage(bv->getParentLanguage(this));
1492 setFont(bv, font, false);
1495 if (result >= FINISHED)
1496 bv->unlockInset(this);
1498 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1499 result = DISPATCHED;
1504 int InsetText::latex(Buffer const * buf, ostream & os,
1505 bool moving_arg, bool) const
1508 latexParagraphs(buf, paragraphs,
1509 paragraphs.begin(), paragraphs.end(),
1510 os, texrow, moving_arg);
1511 return texrow.rows();
1515 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1517 unsigned int lines = 0;
1519 ParagraphList::iterator beg = paragraphs.begin();
1520 ParagraphList::iterator end = paragraphs.end();
1521 ParagraphList::iterator it = beg;
1522 for (; it != end; ++it) {
1523 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1524 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1531 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1533 unsigned int lines = 0;
1535 vector<string> environment_stack(10);
1536 vector<string> environment_inner(10);
1538 int const command_depth = 0;
1541 Paragraph::depth_type depth = 0; // paragraph depth
1543 ParagraphList::iterator pit = paragraphs.begin();
1544 ParagraphList::iterator pend = paragraphs.end();
1546 for (; pit != pend; ++pit) {
1548 int desc_on = 0; // description mode
1550 LyXLayout_ptr const & style = pit->layout();
1552 // environment tag closing
1553 for (; depth > pit->params().depth(); --depth) {
1554 if (environment_inner[depth] != "!-- --") {
1555 item_name = "listitem";
1556 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1557 if (environment_inner[depth] == "varlistentry")
1558 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1560 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1561 environment_stack[depth].erase();
1562 environment_inner[depth].erase();
1565 if (depth == pit->params().depth()
1566 && environment_stack[depth] != style->latexname()
1567 && !environment_stack[depth].empty()) {
1568 if (environment_inner[depth] != "!-- --") {
1569 item_name= "listitem";
1570 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1571 if (environment_inner[depth] == "varlistentry")
1572 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1575 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1577 environment_stack[depth].erase();
1578 environment_inner[depth].erase();
1581 // Write opening SGML tags.
1582 switch (style->latextype) {
1583 case LATEX_PARAGRAPH:
1584 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1588 buf->sgmlError(&*pit, 0, _("Error: LatexType Command not allowed here.\n"));
1592 case LATEX_ENVIRONMENT:
1593 case LATEX_ITEM_ENVIRONMENT:
1594 if (depth < pit->params().depth()) {
1595 depth = pit->params().depth();
1596 environment_stack[depth].erase();
1599 if (environment_stack[depth] != style->latexname()) {
1600 if (environment_stack.size() == depth + 1) {
1601 environment_stack.push_back("!-- --");
1602 environment_inner.push_back("!-- --");
1604 environment_stack[depth] = style->latexname();
1605 environment_inner[depth] = "!-- --";
1606 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1608 if (environment_inner[depth] != "!-- --") {
1609 item_name= "listitem";
1610 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1611 if (environment_inner[depth] == "varlistentry")
1612 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1616 if (style->latextype == LATEX_ENVIRONMENT) {
1617 if (!style->latexparam().empty()) {
1618 if (style->latexparam() == "CDATA")
1621 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1626 desc_on = (style->labeltype == LABEL_MANUAL);
1628 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1629 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1631 item_name = desc_on ? "term" : "para";
1632 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1636 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1640 buf->simpleDocBookOnePar(os, &*pit, desc_on, depth + 1 + command_depth);
1643 // write closing SGML tags
1644 switch (style->latextype) {
1645 case LATEX_ENVIRONMENT:
1646 if (!style->latexparam().empty()) {
1647 if (style->latexparam() == "CDATA")
1650 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1653 case LATEX_ITEM_ENVIRONMENT:
1654 if (desc_on == 1) break;
1656 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1658 case LATEX_PARAGRAPH:
1659 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1662 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1668 for (int d = depth; d >= 0; --d) {
1669 if (!environment_stack[depth].empty()) {
1670 if (environment_inner[depth] != "!-- --") {
1671 item_name = "listitem";
1672 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1673 if (environment_inner[depth] == "varlistentry")
1674 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1677 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1685 void InsetText::validate(LaTeXFeatures & features) const
1687 for_each(paragraphs.begin(), paragraphs.end(),
1688 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1692 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1694 if (the_locking_inset) {
1695 the_locking_inset->getCursorPos(bv, x, y);
1698 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1699 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1703 int InsetText::insetInInsetY() const
1705 if (!the_locking_inset)
1708 return (inset_y + the_locking_inset->insetInInsetY());
1712 void InsetText::toggleInsetCursor(BufferView * bv)
1714 if (the_locking_inset) {
1715 the_locking_inset->toggleInsetCursor(bv);
1719 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1721 int const asc = font_metrics::maxAscent(font);
1722 int const desc = font_metrics::maxDescent(font);
1724 if (isCursorVisible())
1725 bv->hideLockedInsetCursor();
1727 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1728 toggleCursorVisible();
1732 void InsetText::showInsetCursor(BufferView * bv, bool show)
1734 if (the_locking_inset) {
1735 the_locking_inset->showInsetCursor(bv, show);
1738 if (!isCursorVisible()) {
1739 LyXFont const font =
1740 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1742 int const asc = font_metrics::maxAscent(font);
1743 int const desc = font_metrics::maxDescent(font);
1745 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1747 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1748 setCursorVisible(true);
1753 void InsetText::hideInsetCursor(BufferView * bv)
1755 if (isCursorVisible()) {
1756 bv->hideLockedInsetCursor();
1757 setCursorVisible(false);
1759 if (the_locking_inset)
1760 the_locking_inset->hideInsetCursor(bv);
1764 void InsetText::fitInsetCursor(BufferView * bv) const
1766 if (the_locking_inset) {
1767 the_locking_inset->fitInsetCursor(bv);
1770 LyXFont const font =
1771 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1773 int const asc = font_metrics::maxAscent(font);
1774 int const desc = font_metrics::maxDescent(font);
1776 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1777 need_update |= FULL;
1782 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1784 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1785 return moveLeftIntern(bv, false, activate_inset, selecting);
1787 return moveRightIntern(bv, true, activate_inset, selecting);
1792 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1794 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1795 return moveRightIntern(bv, true, activate_inset, selecting);
1797 return moveLeftIntern(bv, false, activate_inset, selecting);
1802 InsetText::moveRightIntern(BufferView * bv, bool front,
1803 bool activate_inset, bool selecting)
1805 LyXText * text = getLyXText(bv);
1807 ParagraphList::iterator c_par = cpar(bv);
1809 if (boost::next(c_par) == paragraphs.end() &&
1810 (cpos(bv) >= c_par->size()))
1811 return FINISHED_RIGHT;
1812 if (activate_inset && checkAndActivateInset(bv, front))
1814 text->cursorRight(bv);
1816 text->clearSelection();
1817 return DISPATCHED_NOUPDATE;
1822 InsetText::moveLeftIntern(BufferView * bv, bool front,
1823 bool activate_inset, bool selecting)
1825 LyXText * text = getLyXText(bv);
1827 if (cpar(bv) == paragraphs.begin() && (cpos(bv) <= 0))
1829 text->cursorLeft(bv);
1831 text->clearSelection();
1832 if (activate_inset && checkAndActivateInset(bv, front))
1834 return DISPATCHED_NOUPDATE;
1838 Inset::RESULT InsetText::moveUp(BufferView * bv)
1840 if (crow(bv) == getLyXText(bv)->rows().begin())
1842 getLyXText(bv)->cursorUp(bv);
1843 getLyXText(bv)->clearSelection();
1844 return DISPATCHED_NOUPDATE;
1848 Inset::RESULT InsetText::moveDown(BufferView * bv)
1850 if (boost::next(crow(bv)) == getLyXText(bv)->rows().end())
1851 return FINISHED_DOWN;
1852 getLyXText(bv)->cursorDown(bv);
1853 getLyXText(bv)->clearSelection();
1854 return DISPATCHED_NOUPDATE;
1858 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1860 if (the_locking_inset) {
1861 if (the_locking_inset->insetAllowed(inset))
1862 return the_locking_inset->insertInset(bv, inset);
1865 inset->setOwner(this);
1866 hideInsetCursor(bv);
1867 getLyXText(bv)->insertInset(inset);
1869 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1874 bool InsetText::insetAllowed(Inset::Code code) const
1876 // in_insetAllowed is a really gross hack,
1877 // to allow us to call the owner's insetAllowed
1878 // without stack overflow, which can happen
1879 // when the owner uses InsetCollapsable::insetAllowed()
1881 if (in_insetAllowed)
1883 in_insetAllowed = true;
1884 if (the_locking_inset)
1885 ret = the_locking_inset->insetAllowed(code);
1887 ret = owner()->insetAllowed(code);
1888 in_insetAllowed = false;
1893 UpdatableInset * InsetText::getLockingInset() const
1895 return the_locking_inset ? the_locking_inset->getLockingInset() :
1896 const_cast<InsetText *>(this);
1900 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1904 if (the_locking_inset)
1905 return the_locking_inset->getFirstLockingInsetOfType(c);
1910 bool InsetText::showInsetDialog(BufferView * bv) const
1912 if (the_locking_inset)
1913 return the_locking_inset->showInsetDialog(bv);
1918 vector<string> const InsetText::getLabelList() const
1920 vector<string> label_list;
1922 ParagraphList::iterator pit = paragraphs.begin();
1923 ParagraphList::iterator pend = paragraphs.end();
1924 for (; pit != pend; ++pit) {
1925 InsetList::iterator beg = pit->insetlist.begin();
1926 InsetList::iterator end = pit->insetlist.end();
1927 for (; beg != end; ++beg) {
1928 vector<string> const l = beg.getInset()->getLabelList();
1929 label_list.insert(label_list.end(), l.begin(), l.end());
1936 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1939 if (the_locking_inset) {
1940 the_locking_inset->setFont(bv, font, toggleall, selectall);
1943 if ((boost::next(paragraphs.begin()) == paragraphs.end() &&
1944 paragraphs.begin()->empty()) || cpar(bv)->empty()) {
1945 getLyXText(bv)->setFont(font, toggleall);
1950 lt = getLyXText(bv);
1953 if (lt->selection.set()) {
1954 setUndo(bv, Undo::EDIT, lt->cursor.par(), boost::next(lt->cursor.par()));
1958 lt->toggleFree(font, toggleall);
1960 lt->clearSelection();
1962 bool flag = (selectall || lt->selection.set());
1966 updateLocal(bv, FULL, true);
1968 updateLocal(bv, CURSOR_PAR, true);
1972 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1974 if (cpar(bv)->isInset(cpos(bv))) {
1976 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1977 if (!isHighlyEditableInset(inset))
1979 inset->edit(bv, front);
1980 if (!the_locking_inset)
1982 updateLocal(bv, CURSOR, false);
1989 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1990 mouse_button::state button)
1992 x -= drawTextXOffset;
1994 int dummyy = y + insetAscent;
1995 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1996 // we only do the edit() call if the inset was hit by the mouse
1997 // or if it is a highly editable inset. So we should call this
1998 // function from our own edit with button < 0.
1999 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
2001 if (button == mouse_button::none && !isHighlyEditableInset(inset))
2009 inset_x = cix(bv) - top_x + drawTextXOffset;
2010 inset_y = ciy(bv) + drawTextYOffset;
2011 inset->edit(bv, x - inset_x, y - inset_y, button);
2012 if (!the_locking_inset)
2014 updateLocal(bv, CURSOR, false);
2021 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2024 int w = UpdatableInset::getMaxWidth(bv, inset);
2029 w = w - top_x + owner()->x();
2032 w -= (2 * TEXT_TO_INSET_OFFSET);
2035 return UpdatableInset::getMaxWidth(bv, inset);
2040 void InsetText::setParagraphData(ParagraphList const & plist, bool same_id)
2042 // we have to unlock any locked inset otherwise we're in troubles
2043 the_locking_inset = 0;
2046 ParagraphList::iterator pit = plist.begin();
2047 ParagraphList::iterator pend = plist.end();
2048 for (; pit != pend; ++pit) {
2049 Paragraph * new_par = new Paragraph(*pit, same_id);
2050 new_par->setInsetOwner(this);
2051 paragraphs.push_back(new_par);
2059 void InsetText::markNew(bool track_changes)
2061 ParagraphList::iterator pit = paragraphs.begin();
2062 ParagraphList::iterator pend = paragraphs.end();
2063 for (; pit != pend; ++pit) {
2064 if (track_changes) {
2065 pit->trackChanges();
2067 // no-op when not tracking
2068 pit->cleanChanges();
2074 void InsetText::setText(string const & data, LyXFont const & font)
2077 for (unsigned int i = 0; i < data.length(); ++i)
2078 paragraphs.begin()->insertChar(i, data[i], font);
2083 void InsetText::setAutoBreakRows(bool flag)
2085 if (flag != autoBreakRows) {
2086 autoBreakRows = flag;
2094 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2096 if (how != drawFrame_) {
2099 updateLocal(bv, DRAW_FRAME, false);
2104 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2106 if (frame_color != col) {
2109 updateLocal(bv, DRAW_FRAME, false);
2114 int InsetText::cx(BufferView * bv) const
2116 // we do nothing dangerous so we use a local cache
2117 LyXText * llt = getLyXText(bv);
2118 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2119 if (the_locking_inset) {
2120 LyXFont font = llt->getFont(bv->buffer(), &*llt->cursor.par(),
2122 if (font.isVisibleRightToLeft())
2123 x -= the_locking_inset->width(bv, font);
2129 int InsetText::cix(BufferView * bv) const
2131 // we do nothing dangerous so we use a local cache
2132 LyXText * llt = getLyXText(bv);
2133 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2134 if (the_locking_inset) {
2135 LyXFont font = llt->getFont(bv->buffer(), &*llt->cursor.par(),
2137 if (font.isVisibleRightToLeft())
2138 x -= the_locking_inset->width(bv, font);
2144 int InsetText::cy(BufferView * bv) const
2147 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2151 int InsetText::ciy(BufferView * bv) const
2154 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2158 pos_type InsetText::cpos(BufferView * bv) const
2160 return getLyXText(bv)->cursor.pos();
2164 ParagraphList::iterator InsetText::cpar(BufferView * bv) const
2166 return getLyXText(bv)->cursor.par();
2170 bool InsetText::cboundary(BufferView * bv) const
2172 return getLyXText(bv)->cursor.boundary();
2176 RowList::iterator InsetText::crow(BufferView * bv) const
2178 return getLyXText(bv)->cursor.row();
2182 LyXText * InsetText::getLyXText(BufferView const * lbv,
2183 bool const recursive) const
2185 if (cached_bview == lbv) {
2186 if (recursive && the_locking_inset)
2187 return the_locking_inset->getLyXText(lbv, true);
2188 LyXText * lt = cached_text.get();
2189 lyx::Assert(lt && lt->rows().begin()->par() == &*(paragraphs.begin()));
2192 // Super UGLY! (Lgb)
2193 BufferView * bv = const_cast<BufferView *>(lbv);
2196 Cache::iterator it = cache.find(bv);
2198 if (it != cache.end()) {
2201 } else if (do_resize) {
2202 resizeLyXText(do_resize);
2204 if (lt || !it->second.remove) {
2205 lyx::Assert(it->second.text.get());
2206 cached_text = it->second.text;
2207 if (recursive && the_locking_inset) {
2208 return the_locking_inset->getLyXText(bv, true);
2210 return cached_text.get();
2211 } else if (it->second.remove) {
2213 saveLyXTextState(it->second.text.get());
2219 // when we have to reinit the existing LyXText!
2221 it->second.text->init(bv);
2222 restoreLyXTextState(it->second.text.get());
2223 it->second.remove = false;
2225 cached_text = it->second.text;
2226 if (the_locking_inset && recursive) {
2227 return the_locking_inset->getLyXText(bv);
2229 return cached_text.get();
2232 // we are here only if we don't have a BufferView * in the cache!!!
2234 cached_text.reset(new LyXText(bv, const_cast<InsetText *>(this)));
2235 cached_text->init(bv);
2236 restoreLyXTextState(cached_text.get());
2238 cache.insert(make_pair(bv, cached_text));
2240 if (the_locking_inset && recursive) {
2241 return the_locking_inset->getLyXText(bv);
2243 return cached_text.get();
2247 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2251 Cache::iterator it = cache.find(bv);
2253 if (it == cache.end()) {
2257 lyx::Assert(it->second.text.get());
2259 it->second.remove = true;
2261 /// then remove all LyXText in text-insets
2262 for_each(paragraphs.begin(), paragraphs.end(),
2263 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2268 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2271 // we cannot resize this because we are in use!
2272 // so do this on the next possible getLyXText()
2277 if (boost::next(paragraphs.begin()) == paragraphs.end() &&
2278 paragraphs.begin()->empty()) { // no data, resize not neccessary!
2279 // we have to do this as a fixed width may have changed!
2280 LyXText * t = getLyXText(bv);
2281 saveLyXTextState(t);
2283 restoreLyXTextState(t);
2286 // one endless line, resize normally not necessary
2287 if (!force && getMaxWidth(bv, this) < 0)
2290 Cache::iterator it = cache.find(bv);
2291 if (it == cache.end()) {
2294 lyx::Assert(it->second.text.get());
2296 LyXText * t = it->second.text.get();
2297 saveLyXTextState(t);
2299 for_each(paragraphs.begin(), paragraphs.end(),
2300 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2303 restoreLyXTextState(t);
2304 if (the_locking_inset) {
2305 inset_x = cix(bv) - top_x + drawTextXOffset;
2306 inset_y = ciy(bv) + drawTextYOffset;
2309 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2311 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2312 // this will scroll the screen such that the cursor becomes visible
2313 bv->updateScrollbar();
2315 need_update |= FULL;
2320 void InsetText::reinitLyXText() const
2323 // we cannot resize this because we are in use!
2324 // so do this on the next possible getLyXText()
2330 for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2331 lyx::Assert(it->second.text.get());
2333 LyXText * t = it->second.text.get();
2334 BufferView * bv = it->first;
2336 saveLyXTextState(t);
2338 for_each(paragraphs.begin(), paragraphs.end(),
2339 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2342 restoreLyXTextState(t);
2343 if (the_locking_inset) {
2344 inset_x = cix(bv) - top_x + drawTextXOffset;
2345 inset_y = ciy(bv) + drawTextYOffset;
2347 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2349 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2350 // this will scroll the screen such that the cursor becomes visible
2351 bv->updateScrollbar();
2359 void InsetText::removeNewlines()
2361 bool changed = false;
2363 ParagraphList::iterator it = paragraphs.begin();
2364 ParagraphList::iterator end = paragraphs.end();
2365 for (; it != end; ++it) {
2366 for (int i = 0; i < it->size(); ++i) {
2367 if (it->isNewline(i)) {
2378 bool InsetText::nodraw() const
2380 if (the_locking_inset)
2381 return the_locking_inset->nodraw();
2382 return UpdatableInset::nodraw();
2386 int InsetText::scroll(bool recursive) const
2388 int sx = UpdatableInset::scroll(false);
2390 if (recursive && the_locking_inset)
2391 sx += the_locking_inset->scroll(recursive);
2397 void InsetText::selectAll(BufferView * bv)
2399 getLyXText(bv)->cursorTop();
2400 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2401 getLyXText(bv)->cursorBottom();
2402 getLyXText(bv)->setSelection();
2406 void InsetText::clearSelection(BufferView * bv)
2408 getLyXText(bv)->clearSelection();
2412 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2414 Painter & pain = bv->painter();
2416 int h = insetAscent + insetDescent;
2417 int ty = baseline - insetAscent;
2423 if ((ty + h) > pain.paperHeight())
2424 h = pain.paperHeight();
2425 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2426 w = pain.paperWidth();
2427 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2432 Paragraph * InsetText::firstParagraph() const
2435 if (the_locking_inset)
2436 if ((result = the_locking_inset->firstParagraph()))
2438 return &*(paragraphs.begin());
2442 Paragraph * InsetText::getFirstParagraph(int i) const
2444 return (i == 0) ? &*(paragraphs.begin()) : 0;
2448 LyXCursor const & InsetText::cursor(BufferView * bv) const
2450 if (the_locking_inset)
2451 return the_locking_inset->cursor(bv);
2452 return getLyXText(bv)->cursor;
2456 void InsetText::paragraph(Paragraph * p)
2458 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2459 // caller of this function has to take care of it. This IS important
2460 // as we could have to insert a paragraph before this one and just
2461 // link the actual to a new ones next and set it with this function
2464 // set ourself as owner for all the paragraphs inserted!
2465 for_each(paragraphs.begin(), paragraphs.end(),
2466 boost::bind(&Paragraph::setInsetOwner, _1, this));
2469 // redraw myself when asked for
2474 Inset * InsetText::getInsetFromID(int id_arg) const
2477 return const_cast<InsetText *>(this);
2479 ParagraphList::iterator pit = paragraphs.begin();
2480 ParagraphList::iterator pend = paragraphs.end();
2481 for (; pit != pend; ++pit) {
2482 InsetList::iterator it = pit->insetlist.begin();
2483 InsetList::iterator end = pit->insetlist.end();
2484 for (; it != end; ++it) {
2485 if (it.getInset()->id() == id_arg)
2486 return it.getInset();
2487 Inset * in = it.getInset()->getInsetFromID(id_arg);
2497 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2498 float & value) const
2504 lt = getLyXText(bv);
2507 if (the_locking_inset) {
2508 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2509 if (!word.word().empty()) {
2515 // we have to go on checking so move cursor to the next char
2516 lt->cursor.pos(lt->cursor.pos() + 1);
2518 word = lt->selectNextWordToSpellcheck(value);
2519 if (word.word().empty())
2520 bv->unlockInset(const_cast<InsetText *>(this));
2529 void InsetText::selectSelectedWord(BufferView * bv)
2531 if (the_locking_inset) {
2532 the_locking_inset->selectSelectedWord(bv);
2535 getLyXText(bv)->selectSelectedWord();
2536 updateLocal(bv, SELECTION, false);
2540 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2542 if (the_locking_inset) {
2543 the_locking_inset->toggleSelection(bv, kill_selection);
2547 lt = getLyXText(bv);
2551 int x = top_x + TEXT_TO_INSET_OFFSET;
2553 RowList::iterator rowit = lt->rows().begin();
2554 RowList::iterator end = lt->rows().end();
2555 int y_offset = top_baseline - rowit->ascent_of_text();
2557 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2558 y += rowit->height();
2564 if (need_update & SELECTION)
2566 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2572 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2576 lt = getLyXText(bv);
2579 if (the_locking_inset) {
2580 if (the_locking_inset->nextChange(bv, length))
2582 lt->cursorRight(true);
2584 lyxfind::SearchResult result =
2585 lyxfind::findNextChange(bv, lt, length);
2587 if (result == lyxfind::SR_FOUND) {
2588 LyXCursor cur = lt->cursor;
2589 bv->unlockInset(bv->theLockingInset());
2590 if (bv->lockInset(this))
2593 lt->setSelectionRange(length);
2594 updateLocal(bv, SELECTION, false);
2598 return result != lyxfind::SR_NOT_FOUND;
2602 bool InsetText::searchForward(BufferView * bv, string const & str,
2607 lt = getLyXText(bv);
2610 if (the_locking_inset) {
2611 if (the_locking_inset->searchForward(bv, str, cs, mw))
2613 lt->cursorRight(true);
2615 lyxfind::SearchResult result =
2616 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2618 if (result == lyxfind::SR_FOUND) {
2619 LyXCursor cur = lt->cursor;
2620 bv->unlockInset(bv->theLockingInset());
2621 if (bv->lockInset(this))
2624 lt->setSelectionRange(str.length());
2625 updateLocal(bv, SELECTION, false);
2629 return (result != lyxfind::SR_NOT_FOUND);
2632 bool InsetText::searchBackward(BufferView * bv, string const & str,
2635 if (the_locking_inset) {
2636 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2641 lt = getLyXText(bv);
2645 ParagraphList::iterator pit = paragraphs.begin();
2646 ParagraphList::iterator pend = paragraphs.end();
2648 while (boost::next(pit) != pend)
2651 lt->setCursor(&*pit, pit->size());
2653 lyxfind::SearchResult result =
2654 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2656 if (result == lyxfind::SR_FOUND) {
2657 LyXCursor cur = lt->cursor;
2658 bv->unlockInset(bv->theLockingInset());
2659 if (bv->lockInset(this))
2662 lt->setSelectionRange(str.length());
2663 updateLocal(bv, SELECTION, false);
2667 return (result != lyxfind::SR_NOT_FOUND);
2671 bool InsetText::checkInsertChar(LyXFont & font)
2674 return owner()->checkInsertChar(font);
2679 void InsetText::collapseParagraphs(BufferView * bv)
2681 LyXText * llt = getLyXText(bv);
2683 while (boost::next(paragraphs.begin()) != paragraphs.end()) {
2684 if (!paragraphs.begin()->empty() &&
2685 !boost::next(paragraphs.begin())->empty() &&
2686 !paragraphs.begin()->isSeparator(paragraphs.begin()->size() - 1))
2688 paragraphs.begin()->insertChar(paragraphs.begin()->size(), ' ');
2690 if (llt->selection.set()) {
2691 if (llt->selection.start.par() == boost::next(paragraphs.begin())) {
2692 llt->selection.start.par(&*(paragraphs.begin()));
2693 llt->selection.start.pos(
2694 llt->selection.start.pos() + paragraphs.begin()->size());
2696 if (llt->selection.end.par() == boost::next(paragraphs.begin())) {
2697 llt->selection.end.par(&*(paragraphs.begin()));
2698 llt->selection.end.pos(
2699 llt->selection.end.pos() + paragraphs.begin()->size());
2702 mergeParagraph(bv->buffer()->params, paragraphs, paragraphs.begin());
2708 void InsetText::getDrawFont(LyXFont & font) const
2712 owner()->getDrawFont(font);
2716 void InsetText::appendParagraphs(Buffer * buffer,
2717 ParagraphList & plist)
2719 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2720 // And it probably does. You have to take a look at this John. (Lgb)
2721 #warning John, have a look here. (Lgb)
2722 BufferParams const & bparams = buffer->params;
2725 Paragraph * tmpbuf = newpar;
2726 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2727 if (bparams.tracking_changes)
2728 buf->cleanChanges();
2730 while (tmpbuf->next()) {
2731 tmpbuf = tmpbuf->next();
2732 lastbuffer->next(new Paragraph(*tmpbuf, false));
2733 lastbuffer->next()->previous(lastbuffer);
2734 lastbuffer = lastbuffer->next();
2735 if (bparams.tracking_changes)
2736 lastbuffer->cleanChanges();
2739 lastbuffer = &*(paragraphs.begin());
2740 while (lastbuffer->next())
2741 lastbuffer = lastbuffer->next();
2742 if (!newpar->empty() && !lastbuffer->empty() &&
2743 !lastbuffer->isSeparator(lastbuffer->size() - 1))
2745 lastbuffer->insertChar(lastbuffer->size(), ' ');
2748 // make the buf exactly the same layout than our last paragraph
2749 buf->makeSameLayout(lastbuffer);
2752 lastbuffer->next(buf);
2753 buf->previous(lastbuffer);
2754 mergeParagraph(buffer->params, paragraphs, lastbuffer);
2756 ParagraphList::iterator pit = plist.begin();
2757 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(),
2758 new Paragraph(*pit, false));
2760 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2762 ParagraphList::iterator pend = plist.end();
2763 for (; pit != pend; ++pit) {
2764 paragraphs.push_back(new Paragraph(*pit, false));
2773 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2775 ParagraphList::iterator pit = paragraphs.begin();
2776 ParagraphList::iterator pend = paragraphs.end();
2778 for (; pit != pend; ++pit) {
2779 InsetList::iterator it = pit->insetlist.begin();
2780 InsetList::iterator end = pit->insetlist.end();
2781 for (; it != end; ++it) {
2782 it.getInset()->addPreview(loader);