1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team.
8 * ======================================================
14 #pragma implementation
17 #include "insettext.h"
20 #include "BufferView.h"
21 #include "CutAndPaste.h"
23 #include "funcrequest.h"
26 #include "LaTeXFeatures.h"
29 #include "lyxcursor.h"
35 #include "paragraph.h"
36 #include "ParagraphParameters.h"
37 #include "trans_mgr.h"
38 #include "undo_funcs.h"
39 #include "WordLangTuple.h"
40 #include "paragraph_funcs.h"
42 #include "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 extern unsigned char getCurrentTextClass(Buffer *);
75 extern bool math_insert_greek(BufferView *, char);
76 extern int greek_kb_flag;
79 // These functions should probably go into bufferview_funcs somehow (Jug)
81 void InsetText::saveLyXTextState(LyXText * t) const
83 // check if my paragraphs are still valid
84 ParagraphList::iterator it = paragraphs.begin();
85 ParagraphList::iterator end = paragraphs.end();
86 for (; it != end; ++it) {
87 if (&*it == t->cursor.par())
91 if (it != end && t->cursor.pos() <= it->size()) {
92 sstate.lpar = t->cursor.par();
93 sstate.pos = t->cursor.pos();
94 sstate.boundary = t->cursor.boundary();
95 sstate.selstartpar = t->selection.start.par();
96 sstate.selstartpos = t->selection.start.pos();
97 sstate.selstartboundary = t->selection.start.boundary();
98 sstate.selendpar = t->selection.end.par();
99 sstate.selendpos = t->selection.end.pos();
100 sstate.selendboundary = t->selection.end.boundary();
101 sstate.selection = t->selection.set();
102 sstate.mark_set = t->selection.mark();
103 sstate.refresh = t->refresh_row != 0;
109 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
112 t->selection.set(true);
113 /* at this point just to avoid the Delete-Empty-Paragraph
114 * Mechanism when setting the cursor */
115 t->selection.mark(sstate.mark_set);
116 if (sstate.selection) {
117 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
118 true, sstate.selstartboundary);
119 t->selection.cursor = t->cursor;
120 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
121 true, sstate.selendboundary);
123 t->setCursor(bv, sstate.lpar, sstate.pos);
125 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
126 t->selection.cursor = t->cursor;
127 t->selection.set(false);
129 if (sstate.refresh) {
135 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
142 InsetText::InsetText(BufferParams const & bp)
143 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
146 paragraphs.set(new Paragraph);
147 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
152 InsetText::InsetText(InsetText const & in, bool same_id)
153 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
160 InsetText & InsetText::operator=(InsetText const & it)
167 void InsetText::init(InsetText const * ins, bool same_id)
170 setParagraphData(&*(ins->paragraphs.begin()), same_id);
171 autoBreakRows = ins->autoBreakRows;
172 drawFrame_ = ins->drawFrame_;
173 frame_color = ins->frame_color;
177 for_each(paragraphs.begin(), paragraphs.end(),
178 boost::bind(&Paragraph::setInsetOwner, _1, this));
180 the_locking_inset = 0;
182 frame_color = LColor::insetframe;
183 autoBreakRows = false;
196 last_drawn_width = -1;
197 frame_is_visible = false;
200 in_insetAllowed = false;
204 InsetText::~InsetText()
210 void InsetText::clear()
212 // This is a gross hack...
213 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
216 paragraphs.set(new Paragraph);
217 paragraphs.begin()->setInsetOwner(this);
218 paragraphs.begin()->layout(old_layout);
225 Inset * InsetText::clone(Buffer const &, bool same_id) const
227 return new InsetText(*this, same_id);
231 void InsetText::write(Buffer const * buf, ostream & os) const
234 writeParagraphData(buf, os);
238 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
240 ParagraphList::iterator it = paragraphs.begin();
241 ParagraphList::iterator end = paragraphs.end();
242 Paragraph::depth_type dth = 0;
243 for (; it != end; ++it) {
244 it->write(buf, os, buf->params, dth);
249 void InsetText::read(Buffer const * buf, LyXLex & lex)
253 Paragraph * return_par = 0;
254 Paragraph::depth_type depth = 0;
255 LyXFont font(LyXFont::ALL_INHERIT);
261 token = lex.getString();
264 if (token == "\\end_inset") {
268 Paragraph * tmp = &*(paragraphs.begin());
270 if (const_cast<Buffer*>(buf)->
271 parseSingleLyXformat2Token(lex, tmp, return_par,
272 token, pos, depth, font)) {
273 // the_end read this should NEVER happen
274 lex.printError("\\the_end read in inset! Error in document!");
280 return_par = &*(paragraphs.begin());
281 paragraphs.set(return_par);
283 return_par->setInsetOwner(this);
284 return_par = return_par->next();
287 if (token != "\\end_inset") {
288 lex.printError("Missing \\end_inset at this point. "
295 int InsetText::ascent(BufferView * bv, LyXFont const &) const
297 insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
298 TEXT_TO_INSET_OFFSET;
303 int InsetText::descent(BufferView * bv, LyXFont const &) const
305 LyXText * llt = getLyXText(bv);
306 insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
307 TEXT_TO_INSET_OFFSET;
312 int InsetText::width(BufferView * bv, LyXFont const &) const
314 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
315 (2 * TEXT_TO_INSET_OFFSET);
316 insetWidth = max(insetWidth, 10);
321 int InsetText::textWidth(BufferView * bv, bool fordraw) const
324 if (!autoBreakRows) {
327 w = getMaxWidth(bv, this);
330 return max(w - (2 * TEXT_TO_INSET_OFFSET),
331 (int)getLyXText(bv)->width);
335 return w - (2 * TEXT_TO_INSET_OFFSET);
339 void InsetText::draw(BufferView * bv, LyXFont const & f,
340 int baseline, float & x, bool cleared) const
345 Painter & pain = bv->painter();
347 // this is the first thing we have to ask because if the x pos
348 // changed we have to do a complete rebreak of the text as we
349 // may have few space to draw in. Well we should check on this too
351 if (top_x != int(x)) {
354 int nw = getMaxWidth(bv, this);
355 if (nw > 0 && old_max_width != nw) {
358 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
363 // call these methods so that insetWidth, insetAscent and
364 // insetDescent have the right values.
369 // repaint the background if needed
370 if (cleared && backgroundColor() != LColor::background) {
371 clearInset(bv, baseline, cleared);
374 // no draw is necessary !!!
375 if ((drawFrame_ == LOCKED) && !locked && paragraphs.begin()->empty()) {
376 top_baseline = baseline;
378 if (need_update & CLEAR_FRAME)
379 clearFrame(pain, cleared);
385 x += static_cast<float>(scroll());
387 // if top_x differs we did it already
388 if (!cleared && (old_x == int(x))
389 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
390 ||(last_drawn_width != insetWidth)))
392 // Condition necessary to eliminate bug 59 attachment 37
394 clearInset(bv, baseline, cleared);
398 frame_is_visible = false;
400 if (!cleared && (need_update == NONE)) {
402 drawFrame(pain, cleared);
406 top_baseline = baseline;
407 top_y = baseline - insetAscent;
409 if (last_drawn_width != insetWidth) {
411 clearInset(bv, baseline, cleared);
413 last_drawn_width = insetWidth;
416 if (the_locking_inset && (cpar(bv) == inset_par)
417 && (cpos(bv) == inset_pos)) {
418 inset_x = cix(bv) - top_x + drawTextXOffset;
419 inset_y = ciy(bv) + drawTextYOffset;
421 if (!cleared && (need_update == CURSOR)
422 && !getLyXText(bv)->selection.set()) {
423 drawFrame(pain, cleared);
433 x += TEXT_TO_INSET_OFFSET;
435 Row * row = lt->firstRow();
436 int y_offset = baseline - row->ascent_of_text();
437 int ph = pain.paperHeight();
440 while ((row != 0) && ((y+row->height()) <= 0)) {
442 first += row->height();
446 lt->first_y = -y_offset;
453 if (cleared || (need_update&(INIT|FULL))) {
454 int yf = y_offset + first;
456 while ((row != 0) && (yf < ph)) {
457 lt->getVisibleRow(bv, y + y_offset + first, int(x),
458 row, y + lt->first_y, cleared);
459 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
460 lt->need_break_row = row;
462 lt->setCursor(bv, lt->cursor.par(),
464 if (lt->selection.set()) {
465 lt->setCursor(bv, lt->selection.start,
466 lt->selection.start.par(),
467 lt->selection.start.pos());
468 lt->setCursor(bv, lt->selection.end,
469 lt->selection.end.par(),
470 lt->selection.end.pos());
478 } else if (!locked) {
479 if (need_update & CURSOR) {
480 bv->screen().toggleSelection(lt, bv, true, y_offset,int(x));
481 lt->clearSelection();
482 lt->selection.cursor = lt->cursor;
484 bv->screen().update(lt, bv, y_offset, int(x));
487 if (need_update & SELECTION) {
488 bv->screen().toggleToggle(lt, bv, y_offset, int(x));
489 } else if (need_update & CURSOR) {
490 bv->screen().toggleSelection(lt, bv, true, y_offset,int(x));
491 lt->clearSelection();
492 lt->selection.cursor = lt->cursor;
494 bv->screen().update(lt, bv, y_offset, int(x));
499 lt->status(bv, LyXText::UNCHANGED);
500 if ((drawFrame_ == ALWAYS) ||
501 ((cleared || (need_update != CURSOR_PAR)) &&
502 (drawFrame_ == LOCKED) && locked))
504 drawFrame(pain, cleared);
505 } else if (need_update & CLEAR_FRAME) {
506 clearFrame(pain, cleared);
509 x += insetWidth - TEXT_TO_INSET_OFFSET;
511 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
513 } else if (need_update != INIT) {
521 void InsetText::drawFrame(Painter & pain, bool cleared) const
523 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
524 if (!frame_is_visible || cleared) {
525 frame_x = top_x + ttoD2;
526 frame_y = top_baseline - insetAscent + ttoD2;
527 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
528 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
529 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
531 frame_is_visible = true;
536 void InsetText::clearFrame(Painter & pain, bool cleared) const
538 if (frame_is_visible) {
540 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
543 frame_is_visible = false;
548 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
551 if (reinit && owner()) {
553 owner()->update(bv, font, true);
558 if (reinit || need_update == INIT) {
560 // we should put this call where we set need_update to INIT!
563 owner()->update(bv, font, true);
568 if (!autoBreakRows && paragraphs.begin()->next())
569 collapseParagraphs(bv);
571 if (the_locking_inset) {
572 inset_x = cix(bv) - top_x + drawTextXOffset;
573 inset_y = ciy(bv) + drawTextYOffset;
574 the_locking_inset->update(bv, font, reinit);
582 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
585 lt->updateInset(bv, the_locking_inset);
587 if (lt->status() == LyXText::NEED_MORE_REFRESH)
595 void InsetText::setUpdateStatus(BufferView * bv, int what) const
597 // this does nothing dangerous so use only a localized buffer
598 LyXText * llt = getLyXText(bv);
601 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
602 // if we don't break row so that we only have one row to update!
603 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
605 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
608 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
609 need_update |= CURSOR_PAR;
612 // this to not draw a selection when we redraw all of it!
613 if (need_update & CURSOR && !(need_update & SELECTION)) {
614 if (llt->selection.set())
616 llt->clearSelection();
621 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
623 if (!autoBreakRows && paragraphs.begin()->next())
624 collapseParagraphs(bv);
631 setUpdateStatus(bv, what);
632 bool flag = mark_dirty ||
633 (((need_update != CURSOR) && (need_update != NONE)) ||
634 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
635 if (!lt->selection.set())
636 lt->selection.cursor = lt->cursor;
640 // IMO this is not anymore needed as we do this in fitInsetCursor!
641 // and we always get "true" as returnvalue of this function in the
642 // case of a locking inset (Jug 20020412)
643 if (locked && (need_update & CURSOR) && bv->fitCursor())
649 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
651 if (need_update == CURSOR)
653 bv->owner()->view_state_changed();
654 bv->owner()->updateMenubar();
655 bv->owner()->updateToolbar();
656 if (old_par != cpar(bv)) {
657 bv->owner()->setLayout(cpar(bv)->layout()->name());
663 string const InsetText::editMessage() const
665 return _("Opened Text Inset");
669 void InsetText::edit(BufferView * bv, int x, int y, mouse_button::state button)
671 UpdatableInset::edit(bv, x, y, button);
673 if (!bv->lockInset(this)) {
674 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
678 the_locking_inset = 0;
679 inset_pos = inset_x = inset_y = 0;
680 inset_boundary = false;
683 int tmp_y = (y < 0) ? 0 : y;
689 // we put here -1 and not button as now the button in the
690 // edit call should not be needed we will fix this in 1.3.x
691 // cycle hopefully (Jug 20020509)
692 // FIXME: GUII I've changed this to none: probably WRONG
693 if (!checkAndActivateInset(bv, x, tmp_y, mouse_button::none)) {
694 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
696 lt->cursor.x_fix(lt->cursor.x());
698 lt->clearSelection();
700 // If the inset is empty set the language of the current font to the
701 // language to the surronding text (if different).
702 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
703 bv->getParentLanguage(this) != lt->current_font.language())
705 LyXFont font(LyXFont::ALL_IGNORE);
706 font.setLanguage(bv->getParentLanguage(this));
707 setFont(bv, font, false);
713 if (drawFrame_ == LOCKED)
714 code = CURSOR|DRAW_FRAME;
715 updateLocal(bv, code, false);
718 // Tell the paragraph dialog that we've entered an insettext.
719 bv->owner()->getDialogs().updateParagraph();
723 void InsetText::edit(BufferView * bv, bool front)
725 UpdatableInset::edit(bv, front);
727 if (!bv->lockInset(this)) {
728 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
732 the_locking_inset = 0;
733 inset_pos = inset_x = inset_y = 0;
734 inset_boundary = false;
743 lt->setCursor(bv, &*(paragraphs.begin()), 0);
745 ParagraphList::iterator it = paragraphs.begin();
746 ParagraphList::iterator end = paragraphs.end();
747 while (boost::next(it) != end)
749 // int const pos = (p->size() ? p->size()-1 : p->size());
750 lt->setCursor(bv, &*it, it->size());
752 lt->clearSelection();
754 // If the inset is empty set the language of the current font to the
755 // language to the surronding text (if different).
756 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
757 bv->getParentLanguage(this) != lt->current_font.language()) {
758 LyXFont font(LyXFont::ALL_IGNORE);
759 font.setLanguage(bv->getParentLanguage(this));
760 setFont(bv, font, false);
765 if (drawFrame_ == LOCKED)
766 code = CURSOR|DRAW_FRAME;
767 updateLocal(bv, code, false);
772 void InsetText::insetUnlock(BufferView * bv)
774 if (the_locking_inset) {
775 the_locking_inset->insetUnlock(bv);
776 the_locking_inset = 0;
777 updateLocal(bv, CURSOR_PAR, false);
784 if (drawFrame_ == LOCKED)
785 code = CURSOR|CLEAR_FRAME;
789 if (drawFrame_ == LOCKED)
797 if (lt->selection.set()) {
798 lt->clearSelection();
800 } else if (owner()) {
801 bv->owner()->setLayout(owner()->getLyXText(bv)
802 ->cursor.par()->layout()->name());
804 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
805 // hack for deleteEmptyParMech
806 if (!paragraphs.begin()->empty()) {
807 lt->setCursor(bv, &*(paragraphs.begin()), 0);
808 } else if (paragraphs.begin()->next()) {
809 lt->setCursor(bv, paragraphs.begin()->next(), 0);
814 updateLocal(bv, code, false);
817 setUpdateStatus(bv, code);
822 void InsetText::lockInset(BufferView * bv)
825 the_locking_inset = 0;
826 inset_pos = inset_x = inset_y = 0;
827 inset_boundary = false;
835 lt->setCursor(bv, &*(paragraphs.begin()), 0);
836 lt->clearSelection();
838 // If the inset is empty set the language of the current font to the
839 // language to the surronding text (if different).
840 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
841 bv->getParentLanguage(this) != lt->current_font.language()) {
842 LyXFont font(LyXFont::ALL_IGNORE);
843 font.setLanguage(bv->getParentLanguage(this));
844 setFont(bv, font, false);
849 if (drawFrame_ == LOCKED)
850 code = CURSOR|DRAW_FRAME;
851 setUpdateStatus(bv, code);
855 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
857 the_locking_inset = inset;
858 inset_x = cix(bv) - top_x + drawTextXOffset;
859 inset_y = ciy(bv) + drawTextYOffset;
860 inset_pos = cpos(bv);
861 inset_par = cpar(bv);
862 inset_boundary = cboundary(bv);
863 updateLocal(bv, CURSOR, false);
867 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
869 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
873 if (!the_locking_inset) {
874 Paragraph * p = &*(paragraphs.begin());
875 int const id = inset->id();
877 InsetList::iterator it =
878 p->insetlist.begin();
879 InsetList::iterator const end =
881 for (; it != end; ++it) {
882 if (it.getInset() == inset) {
883 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
884 lockInset(bv, inset);
887 if (it.getInset()->getInsetFromID(id)) {
888 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
889 it.getInset()->edit(bv);
890 return the_locking_inset->lockInsetInInset(bv, inset);
897 if (inset == cpar(bv)->getInset(cpos(bv))) {
898 lyxerr[Debug::INSETS] << "OK" << endl;
899 lockInset(bv, inset);
901 } else if (the_locking_inset && (the_locking_inset == inset)) {
902 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
903 lyxerr[Debug::INSETS] << "OK" << endl;
904 inset_x = cix(bv) - top_x + drawTextXOffset;
905 inset_y = ciy(bv) + drawTextYOffset;
907 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
909 } else if (the_locking_inset) {
910 lyxerr[Debug::INSETS] << "MAYBE" << endl;
911 return the_locking_inset->lockInsetInInset(bv, inset);
913 lyxerr[Debug::INSETS] << "NOT OK" << endl;
918 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
921 if (!the_locking_inset)
923 if (the_locking_inset == inset) {
924 the_locking_inset->insetUnlock(bv);
925 getLyXText(bv)->updateInset(bv, inset);
926 the_locking_inset = 0;
928 moveRightIntern(bv, true, false);
929 old_par = 0; // force layout setting
933 updateLocal(bv, CURSOR, false);
936 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
940 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
942 if (!autoBreakRows && paragraphs.begin()->next())
943 collapseParagraphs(bv);
951 if (inset->owner() != this) {
952 int ustat = CURSOR_PAR;
954 UpdatableInset * tl_inset = the_locking_inset;
956 found = tl_inset->updateInsetInInset(bv, inset);
958 tl_inset = static_cast<UpdatableInset *>(inset);
959 while(tl_inset->owner() && tl_inset->owner() != this)
960 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
961 if (!tl_inset->owner())
963 found = tl_inset->updateInsetInInset(bv, inset);
967 lt->updateInset(bv, tl_inset);
971 setUpdateStatus(bv, ustat);
974 bool found = lt->updateInset(bv, inset);
978 setUpdateStatus(bv, CURSOR_PAR);
979 if (the_locking_inset &&
980 cpar(bv) == inset_par && cpos(bv) == inset_pos)
982 inset_x = cix(bv) - top_x + drawTextXOffset;
983 inset_y = ciy(bv) + drawTextYOffset;
990 void InsetText::lfunMousePress(FuncRequest const & cmd)
994 // use this to check mouse motion for selection!
998 BufferView * bv = cmd.view();
999 FuncRequest cmd1 = cmd;
1005 int tmp_x = cmd.x - drawTextXOffset;
1006 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->first_y;
1007 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1009 hideInsetCursor(bv);
1010 if (the_locking_inset) {
1011 if (the_locking_inset == inset) {
1012 the_locking_inset->localDispatch(cmd1);
1017 // otherwise unlock the_locking_inset and lock the new inset
1018 the_locking_inset->insetUnlock(bv);
1019 inset_x = cix(bv) - top_x + drawTextXOffset;
1020 inset_y = ciy(bv) + drawTextYOffset;
1021 the_locking_inset = 0;
1022 inset->localDispatch(cmd1);
1023 // inset->edit(bv, x - inset_x, y - inset_y, button);
1024 if (the_locking_inset)
1025 updateLocal(bv, CURSOR, false);
1029 // otherwise only unlock the_locking_inset
1030 the_locking_inset->insetUnlock(bv);
1031 the_locking_inset = 0;
1034 no_selection = false;
1036 if (bv->theLockingInset()) {
1037 if (isHighlyEditableInset(inset)) {
1038 // We just have to lock the inset before calling a
1039 // PressEvent on it!
1040 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
1041 if (!bv->lockInset(uinset)) {
1042 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1044 inset->localDispatch(cmd1);
1045 if (the_locking_inset)
1046 updateLocal(bv, CURSOR, false);
1050 if (!inset) { // && (button == mouse_button::button2)) {
1051 bool paste_internally = false;
1052 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
1053 localDispatch(FuncRequest(bv, LFUN_COPY));
1054 paste_internally = true;
1058 lt = getLyXText(bv);
1061 int old_first_y = lt->first_y;
1063 lt->setCursorFromCoordinates(bv, cmd.x - drawTextXOffset,
1064 cmd.y + insetAscent);
1065 // set the selection cursor!
1066 lt->selection.cursor = lt->cursor;
1067 lt->cursor.x_fix(lt->cursor.x());
1069 if (lt->selection.set()) {
1070 lt->clearSelection();
1073 updateLocal(bv, FULL, false);
1075 lt->clearSelection();
1078 updateLocal(bv, CURSOR, false);
1081 bv->owner()->setLayout(cpar(bv)->layout()->name());
1083 // we moved the view we cannot do mouse selection in this case!
1084 if (getLyXText(bv)->first_y != old_first_y)
1085 no_selection = true;
1087 // Insert primary selection with middle mouse
1088 // if there is a local selection in the current buffer,
1090 if (cmd.button() == mouse_button::button2) {
1091 if (paste_internally)
1092 localDispatch(FuncRequest(bv, LFUN_PASTE));
1094 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
1097 getLyXText(bv)->clearSelection();
1099 showInsetCursor(bv);
1103 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
1105 BufferView * bv = cmd.view();
1106 FuncRequest cmd1 = cmd;
1110 no_selection = true;
1111 if (the_locking_inset)
1112 return the_locking_inset->localDispatch(cmd1);
1114 int tmp_x = cmd.x - drawTextXOffset;
1115 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->first_y;
1116 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1119 if (isHighlyEditableInset(inset))
1120 ret = inset->localDispatch(cmd1);
1122 inset_x = cix(bv) - top_x + drawTextXOffset;
1123 inset_y = ciy(bv) + drawTextYOffset;
1124 cmd1.x = cmd.x - inset_x;
1125 cmd1.y = cmd.x - inset_y;
1126 ret = inset->localDispatch(cmd1);
1127 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
1129 updateLocal(bv, CURSOR_PAR, false);
1135 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
1137 FuncRequest cmd1 = cmd;
1141 if (the_locking_inset) {
1142 the_locking_inset->localDispatch(cmd1);
1146 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
1149 BufferView * bv = cmd.view();
1152 lt = getLyXText(bv);
1155 hideInsetCursor(bv);
1156 LyXCursor cur = lt->cursor;
1157 lt->setCursorFromCoordinates
1158 (bv, cmd.x - drawTextXOffset, cmd.y + insetAscent);
1159 lt->cursor.x_fix(lt->cursor.x());
1160 if (cur == lt->cursor) {
1165 lt->setSelection(bv);
1166 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1167 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1171 updateLocal(bv, SELECTION, false);
1173 showInsetCursor(bv);
1177 Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
1179 BufferView * bv = ev.view();
1180 bool was_empty = (paragraphs.begin()->empty() && !paragraphs.begin()->next());
1181 no_selection = false;
1182 RESULT result = UpdatableInset::localDispatch(ev);
1183 if (result != UNDISPATCHED)
1186 result = DISPATCHED;
1187 if (ev.action < 0 && ev.argument.empty())
1190 if (the_locking_inset) {
1191 result = the_locking_inset->localDispatch(ev);
1192 if (result == DISPATCHED_NOUPDATE)
1194 else if (result == DISPATCHED) {
1195 updateLocal(bv, CURSOR_PAR, false);
1197 } else if (result >= FINISHED) {
1199 case FINISHED_RIGHT:
1200 moveRightIntern(bv, false, false);
1201 result = DISPATCHED;
1204 if ((result = moveUp(bv)) >= FINISHED) {
1205 updateLocal(bv, CURSOR, false);
1206 bv->unlockInset(this);
1211 LyXText *lt = getLyXText(bv);
1212 if (lt->cursor.irow()->next()) {
1213 lt->setCursorFromCoordinates(
1214 bv, lt->cursor.ix() + inset_x,
1216 lt->cursor.irow()->baseline() +
1217 lt->cursor.irow()->height() + 1);
1218 lt->cursor.x_fix(lt->cursor.x());
1219 updateLocal(bv, CURSOR, false);
1221 bv->unlockInset(this);
1226 result = DISPATCHED;
1229 the_locking_inset = 0;
1230 updateLocal(bv, CURSOR, false);
1234 hideInsetCursor(bv);
1237 lt = getLyXText(bv);
1241 int updflag = false;
1242 switch (ev.action) {
1244 case LFUN_MOUSE_PRESS:
1248 case LFUN_MOUSE_MOTION:
1249 lfunMouseMotion(ev);
1252 case LFUN_MOUSE_RELEASE:
1253 return lfunMouseRelease(ev) ? DISPATCHED : UNDISPATCHED;
1256 case LFUN_SELFINSERT:
1257 if (bv->buffer()->isReadonly()) {
1258 // setErrorMessage(N_("Document is read only"));
1261 if (!ev.argument.empty()) {
1262 /* Automatically delete the currently selected
1263 * text and replace it with what is being
1264 * typed in now. Depends on lyxrc settings
1265 * "auto_region_delete", which defaults to
1268 setUndo(bv, Undo::INSERT,
1269 lt->cursor.par(), lt->cursor.par()->next());
1271 if (lyxrc.auto_region_delete) {
1272 if (lt->selection.set()) {
1273 lt->cutSelection(bv, false, false);
1276 lt->clearSelection();
1277 for (string::size_type i = 0; i < ev.argument.length(); ++i) {
1278 bv->owner()->getIntl().getTransManager().
1279 TranslateAndInsert(ev.argument[i], lt);
1282 lt->selection.cursor = lt->cursor;
1283 updwhat = CURSOR | CURSOR_PAR;
1285 result = DISPATCHED_NOUPDATE;
1287 // --- Cursor Movements -----------------------------------
1290 moveRight(bv, false, true);
1291 lt->setSelection(bv);
1292 updwhat = SELECTION;
1295 result = moveRight(bv);
1301 moveLeft(bv, false, true);
1302 lt->setSelection(bv);
1303 updwhat = SELECTION;
1307 result = moveLeft(bv);
1313 lt->setSelection(bv);
1314 updwhat = SELECTION;
1318 result = moveDown(bv);
1324 lt->setSelection(bv);
1325 updwhat = SELECTION;
1329 result = moveUp(bv);
1341 case LFUN_BACKSPACE: {
1342 setUndo(bv, Undo::DELETE,
1343 lt->cursor.par(), lt->cursor.par()->next());
1344 if (lt->selection.set())
1345 lt->cutSelection(bv, true, false);
1348 updwhat = CURSOR_PAR;
1354 setUndo(bv, Undo::DELETE,
1355 lt->cursor.par(), lt->cursor.par()->next());
1356 if (lt->selection.set()) {
1357 lt->cutSelection(bv, true, false);
1361 updwhat = CURSOR_PAR;
1367 setUndo(bv, Undo::DELETE,
1368 lt->cursor.par(), lt->cursor.par()->next());
1369 lt->cutSelection(bv);
1370 updwhat = CURSOR_PAR;
1377 lt->copySelection(bv);
1378 updwhat = CURSOR_PAR;
1380 case LFUN_PASTESELECTION:
1382 string const clip(bv->getClipboard());
1386 if (ev.argument == "paragraph") {
1387 lt->insertStringAsParagraphs(bv, clip);
1389 lt->insertStringAsLines(bv, clip);
1392 lt->clearSelection();
1394 updwhat = CURSOR_PAR;
1399 if (!autoBreakRows) {
1401 if (CutAndPaste::nrOfParagraphs() > 1) {
1402 Alert::alert(_("Impossible operation"),
1403 _("Cannot include more than one paragraph!"),
1408 setUndo(bv, Undo::INSERT,
1409 lt->cursor.par(), lt->cursor.par()->next());
1410 lt->pasteSelection(bv);
1412 lt->clearSelection();
1413 updwhat = CURSOR_PAR;
1418 case LFUN_BREAKPARAGRAPH:
1419 if (!autoBreakRows) {
1420 result = DISPATCHED;
1423 lt->breakParagraph(bv, 0);
1424 updwhat = CURSOR | FULL;
1427 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1428 if (!autoBreakRows) {
1429 result = DISPATCHED;
1432 lt->breakParagraph(bv, 1);
1433 updwhat = CURSOR | FULL;
1437 case LFUN_BREAKLINE: {
1438 if (!autoBreakRows) {
1439 result = DISPATCHED;
1442 setUndo(bv, Undo::INSERT,
1443 lt->cursor.par(), lt->cursor.par()->next());
1444 lt->insertChar(bv, Paragraph::META_NEWLINE);
1445 updwhat = CURSOR | CURSOR_PAR;
1451 // do not set layouts on non breakable textinsets
1452 if (autoBreakRows) {
1453 string cur_layout = cpar(bv)->layout()->name();
1455 // Derive layout number from given argument (string)
1456 // and current buffer's textclass (number). */
1457 LyXTextClass const & tclass =
1458 bv->buffer()->params.getLyXTextClass();
1459 string layout = ev.argument;
1460 bool hasLayout = tclass.hasLayout(layout);
1462 // If the entry is obsolete, use the new one instead.
1464 string const & obs =
1465 tclass[layout]->obsoleted_by();
1470 // see if we found the layout number:
1472 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + ev.argument + N_(" not known"));
1473 bv->owner()->dispatch(lf);
1477 if (cur_layout != layout) {
1478 cur_layout = layout;
1479 lt->setLayout(bv, layout);
1480 bv->owner()->setLayout(cpar(bv)->layout()->name());
1481 updwhat = CURSOR_PAR;
1485 // reset the layout box
1486 bv->owner()->setLayout(cpar(bv)->layout()->name());
1489 case LFUN_PARAGRAPH_SPACING:
1490 // This one is absolutely not working. When fiddling with this
1491 // it also seems to me that the paragraphs inside the insettext
1492 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1494 Paragraph * par = lt->cursor.par();
1495 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1496 float cur_value = 1.0;
1497 if (cur_spacing == Spacing::Other) {
1498 cur_value = par->params().spacing().getValue();
1501 istringstream istr(ev.argument.c_str());
1504 Spacing::Space new_spacing = cur_spacing;
1505 float new_value = cur_value;
1507 lyxerr << "Missing argument to `paragraph-spacing'"
1509 } else if (tmp == "single") {
1510 new_spacing = Spacing::Single;
1511 } else if (tmp == "onehalf") {
1512 new_spacing = Spacing::Onehalf;
1513 } else if (tmp == "double") {
1514 new_spacing = Spacing::Double;
1515 } else if (tmp == "other") {
1516 new_spacing = Spacing::Other;
1519 lyxerr << "new_value = " << tmpval << endl;
1522 } else if (tmp == "default") {
1523 new_spacing = Spacing::Default;
1525 lyxerr << _("Unknown spacing argument: ")
1526 << ev.argument << endl;
1528 if (cur_spacing != new_spacing || cur_value != new_value) {
1529 par->params().spacing(Spacing(new_spacing, new_value));
1530 updwhat = CURSOR_PAR;
1537 if (!bv->dispatch(ev))
1538 result = UNDISPATCHED;
1545 updateLocal(bv, updwhat, updflag);
1546 /// If the action has deleted all text in the inset, we need to change the
1547 // language to the language of the surronding text.
1548 if (!was_empty && paragraphs.begin()->empty() && !paragraphs.begin()->next()) {
1549 LyXFont font(LyXFont::ALL_IGNORE);
1550 font.setLanguage(bv->getParentLanguage(this));
1551 setFont(bv, font, false);
1554 if (result >= FINISHED)
1555 bv->unlockInset(this);
1557 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1558 result = DISPATCHED;
1563 int InsetText::latex(Buffer const * buf, ostream & os,
1564 bool moving_arg, bool) const
1567 buf->latexParagraphs(os, &*(paragraphs.begin()), 0,
1568 texrow, moving_arg);
1569 return texrow.rows();
1573 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1575 unsigned int lines = 0;
1577 ParagraphList::iterator beg = paragraphs.begin();
1578 ParagraphList::iterator end = paragraphs.end();
1579 ParagraphList::iterator it = beg;
1580 for (; it != end; ++it) {
1581 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1582 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1589 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1591 Paragraph * p = &*(paragraphs.begin());
1592 unsigned int lines = 0;
1594 vector<string> environment_stack(10);
1595 vector<string> environment_inner(10);
1597 int const command_depth = 0;
1600 Paragraph::depth_type depth = 0; // paragraph depth
1604 int desc_on = 0; // description mode
1606 LyXLayout_ptr const & style = p->layout();
1608 // environment tag closing
1609 for (; depth > p->params().depth(); --depth) {
1610 if (environment_inner[depth] != "!-- --") {
1611 item_name = "listitem";
1612 lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
1613 if (environment_inner[depth] == "varlistentry")
1614 lines += buf->sgmlCloseTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1616 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1617 environment_stack[depth].erase();
1618 environment_inner[depth].erase();
1621 if (depth == p->params().depth()
1622 && environment_stack[depth] != style->latexname()
1623 && !environment_stack[depth].empty()) {
1624 if (environment_inner[depth] != "!-- --") {
1625 item_name= "listitem";
1626 lines += buf->sgmlCloseTag(os, command_depth+depth, mixcont, item_name);
1627 if (environment_inner[depth] == "varlistentry")
1628 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1631 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1633 environment_stack[depth].erase();
1634 environment_inner[depth].erase();
1637 // Write opening SGML tags.
1638 switch (style->latextype) {
1639 case LATEX_PARAGRAPH:
1640 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style->latexname());
1644 buf->sgmlError(p, 0, _("Error : LatexType Command not allowed here.\n"));
1648 case LATEX_ENVIRONMENT:
1649 case LATEX_ITEM_ENVIRONMENT:
1650 if (depth < p->params().depth()) {
1651 depth = p->params().depth();
1652 environment_stack[depth].erase();
1655 if (environment_stack[depth] != style->latexname()) {
1656 if (environment_stack.size() == depth + 1) {
1657 environment_stack.push_back("!-- --");
1658 environment_inner.push_back("!-- --");
1660 environment_stack[depth] = style->latexname();
1661 environment_inner[depth] = "!-- --";
1662 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1664 if (environment_inner[depth] != "!-- --") {
1665 item_name= "listitem";
1666 lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
1667 if (environment_inner[depth] == "varlistentry")
1668 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1672 if (style->latextype == LATEX_ENVIRONMENT) {
1673 if (!style->latexparam().empty()) {
1674 if (style->latexparam() == "CDATA")
1677 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style->latexparam());
1682 desc_on = (style->labeltype == LABEL_MANUAL);
1684 environment_inner[depth] = desc_on?"varlistentry":"listitem";
1685 lines += buf->sgmlOpenTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1687 item_name = desc_on?"term":"para";
1688 lines += buf->sgmlOpenTag(os, depth + 1 + command_depth, mixcont, item_name);
1692 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style->latexname());
1696 buf->simpleDocBookOnePar(os, p, desc_on, depth + 1 + command_depth);
1700 // write closing SGML tags
1701 switch (style->latextype) {
1702 case LATEX_ENVIRONMENT:
1703 if (!style->latexparam().empty()) {
1704 if (style->latexparam() == "CDATA")
1707 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style->latexparam());
1710 case LATEX_ITEM_ENVIRONMENT:
1711 if (desc_on == 1) break;
1713 lines += buf->sgmlCloseTag(os, depth + 1 + command_depth, mixcont, end_tag);
1715 case LATEX_PARAGRAPH:
1716 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style->latexname());
1719 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style->latexname());
1725 for (int d = depth; d >= 0; --d) {
1726 if (!environment_stack[depth].empty()) {
1727 if (environment_inner[depth] != "!-- --") {
1728 item_name = "listitem";
1729 lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
1730 if (environment_inner[depth] == "varlistentry")
1731 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1734 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1742 void InsetText::validate(LaTeXFeatures & features) const
1744 for_each(paragraphs.begin(), paragraphs.end(),
1745 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1749 int InsetText::beginningOfMainBody(Paragraph * p) const
1751 if (p->layout()->labeltype != LABEL_MANUAL)
1754 return p->beginningOfMainBody();
1758 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1760 if (the_locking_inset) {
1761 the_locking_inset->getCursorPos(bv, x, y);
1764 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1765 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1769 int InsetText::insetInInsetY() const
1771 if (!the_locking_inset)
1774 return (inset_y + the_locking_inset->insetInInsetY());
1778 void InsetText::toggleInsetCursor(BufferView * bv)
1780 if (the_locking_inset) {
1781 the_locking_inset->toggleInsetCursor(bv);
1785 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1787 int const asc = font_metrics::maxAscent(font);
1788 int const desc = font_metrics::maxDescent(font);
1790 if (isCursorVisible())
1791 bv->hideLockedInsetCursor();
1793 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1794 toggleCursorVisible();
1798 void InsetText::showInsetCursor(BufferView * bv, bool show)
1800 if (the_locking_inset) {
1801 the_locking_inset->showInsetCursor(bv, show);
1804 if (!isCursorVisible()) {
1805 LyXFont const font =
1806 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1808 int const asc = font_metrics::maxAscent(font);
1809 int const desc = font_metrics::maxDescent(font);
1811 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1813 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1814 setCursorVisible(true);
1819 void InsetText::hideInsetCursor(BufferView * bv)
1821 if (isCursorVisible()) {
1822 bv->hideLockedInsetCursor();
1823 setCursorVisible(false);
1825 if (the_locking_inset)
1826 the_locking_inset->hideInsetCursor(bv);
1830 void InsetText::fitInsetCursor(BufferView * bv) const
1832 if (the_locking_inset) {
1833 the_locking_inset->fitInsetCursor(bv);
1836 LyXFont const font =
1837 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1839 int const asc = font_metrics::maxAscent(font);
1840 int const desc = font_metrics::maxDescent(font);
1842 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1843 need_update |= FULL;
1848 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1850 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1851 return moveLeftIntern(bv, false, activate_inset, selecting);
1853 return moveRightIntern(bv, true, activate_inset, selecting);
1858 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1860 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1861 return moveRightIntern(bv, true, activate_inset, selecting);
1863 return moveLeftIntern(bv, false, activate_inset, selecting);
1868 InsetText::moveRightIntern(BufferView * bv, bool front,
1869 bool activate_inset, bool selecting)
1871 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1872 return FINISHED_RIGHT;
1873 if (activate_inset && checkAndActivateInset(bv, front))
1875 getLyXText(bv)->cursorRight(bv);
1877 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1878 return DISPATCHED_NOUPDATE;
1883 InsetText::moveLeftIntern(BufferView * bv, bool front,
1884 bool activate_inset, bool selecting)
1886 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1888 getLyXText(bv)->cursorLeft(bv);
1890 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1891 if (activate_inset && checkAndActivateInset(bv, front))
1893 return DISPATCHED_NOUPDATE;
1898 InsetText::moveUp(BufferView * bv)
1900 if (!crow(bv)->previous())
1902 getLyXText(bv)->cursorUp(bv);
1903 return DISPATCHED_NOUPDATE;
1908 InsetText::moveDown(BufferView * bv)
1910 if (!crow(bv)->next())
1911 return FINISHED_DOWN;
1912 getLyXText(bv)->cursorDown(bv);
1913 return DISPATCHED_NOUPDATE;
1917 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1919 if (the_locking_inset) {
1920 if (the_locking_inset->insetAllowed(inset))
1921 return the_locking_inset->insertInset(bv, inset);
1924 inset->setOwner(this);
1925 hideInsetCursor(bv);
1926 getLyXText(bv)->insertInset(bv, inset);
1928 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1933 bool InsetText::insetAllowed(Inset::Code code) const
1935 // in_insetAllowed is a really gross hack,
1936 // to allow us to call the owner's insetAllowed
1937 // without stack overflow, which can happen
1938 // when the owner uses InsetCollapsable::insetAllowed()
1940 if (in_insetAllowed)
1942 in_insetAllowed = true;
1943 if (the_locking_inset)
1944 ret = the_locking_inset->insetAllowed(code);
1946 ret = owner()->insetAllowed(code);
1947 in_insetAllowed = false;
1952 UpdatableInset * InsetText::getLockingInset() const
1954 return the_locking_inset ? the_locking_inset->getLockingInset() :
1955 const_cast<InsetText *>(this);
1959 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1963 if (the_locking_inset)
1964 return the_locking_inset->getFirstLockingInsetOfType(c);
1969 bool InsetText::showInsetDialog(BufferView * bv) const
1971 if (the_locking_inset)
1972 return the_locking_inset->showInsetDialog(bv);
1977 vector<string> const InsetText::getLabelList() const
1979 vector<string> label_list;
1981 ParagraphList::iterator pit = paragraphs.begin();
1982 ParagraphList::iterator pend = paragraphs.end();
1983 for (; pit != pend; ++pit) {
1984 InsetList::iterator beg = pit->insetlist.begin();
1985 InsetList::iterator end = pit->insetlist.end();
1986 for (; beg != end; ++beg) {
1987 vector<string> const l = beg.getInset()->getLabelList();
1988 label_list.insert(label_list.end(), l.begin(), l.end());
1995 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1998 if (the_locking_inset) {
1999 the_locking_inset->setFont(bv, font, toggleall, selectall);
2002 if ((!paragraphs.begin()->next() && paragraphs.begin()->empty()) || cpar(bv)->empty()) {
2003 getLyXText(bv)->setFont(bv, font, toggleall);
2008 lt = getLyXText(bv);
2011 if (lt->selection.set()) {
2012 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
2016 lt->toggleFree(bv, font, toggleall);
2018 lt->clearSelection();
2020 bool flag = (selectall || lt->selection.set());
2024 updateLocal(bv, FULL, true);
2026 updateLocal(bv, CURSOR_PAR, true);
2030 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
2032 if (cpar(bv)->isInset(cpos(bv))) {
2034 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
2035 if (!isHighlyEditableInset(inset))
2037 inset->edit(bv, front);
2038 if (!the_locking_inset)
2040 updateLocal(bv, CURSOR, false);
2047 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2048 mouse_button::state button)
2050 x -= drawTextXOffset;
2052 int dummyy = y + insetAscent;
2053 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
2054 // we only do the edit() call if the inset was hit by the mouse
2055 // or if it is a highly editable inset. So we should call this
2056 // function from our own edit with button < 0.
2057 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
2059 if (button == mouse_button::none && !isHighlyEditableInset(inset))
2067 inset_x = cix(bv) - top_x + drawTextXOffset;
2068 inset_y = ciy(bv) + drawTextYOffset;
2069 inset->edit(bv, x - inset_x, y - inset_y, button);
2070 if (!the_locking_inset)
2072 updateLocal(bv, CURSOR, false);
2079 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2082 int w = UpdatableInset::getMaxWidth(bv, inset);
2087 w = w - top_x + owner()->x();
2090 w -= (2 * TEXT_TO_INSET_OFFSET);
2093 return UpdatableInset::getMaxWidth(bv, inset);
2098 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2100 // we have to unlock any locked inset otherwise we're in troubles
2101 the_locking_inset = 0;
2104 paragraphs.set(new Paragraph(*p, same_id));
2105 paragraphs.begin()->setInsetOwner(this);
2106 Paragraph * np = &*(paragraphs.begin());
2109 np->next(new Paragraph(*p, same_id));
2110 np->next()->previous(np);
2112 np->setInsetOwner(this);
2119 void InsetText::setText(string const & data, LyXFont const & font)
2122 for (unsigned int i = 0; i < data.length(); ++i)
2123 paragraphs.begin()->insertChar(i, data[i], font);
2128 void InsetText::setAutoBreakRows(bool flag)
2130 if (flag != autoBreakRows) {
2131 autoBreakRows = flag;
2139 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2141 if (how != drawFrame_) {
2144 updateLocal(bv, DRAW_FRAME, false);
2149 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2151 if (frame_color != col) {
2154 updateLocal(bv, DRAW_FRAME, false);
2159 int InsetText::cx(BufferView * bv) const
2161 // we do nothing dangerous so we use a local cache
2162 LyXText * llt = getLyXText(bv);
2163 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2164 if (the_locking_inset) {
2165 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2167 if (font.isVisibleRightToLeft())
2168 x -= the_locking_inset->width(bv, font);
2174 int InsetText::cix(BufferView * bv) const
2176 // we do nothing dangerous so we use a local cache
2177 LyXText * llt = getLyXText(bv);
2178 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2179 if (the_locking_inset) {
2180 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2182 if (font.isVisibleRightToLeft())
2183 x -= the_locking_inset->width(bv, font);
2189 int InsetText::cy(BufferView * bv) const
2192 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2196 int InsetText::ciy(BufferView * bv) const
2199 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2203 pos_type InsetText::cpos(BufferView * bv) const
2205 return getLyXText(bv)->cursor.pos();
2209 Paragraph * InsetText::cpar(BufferView * bv) const
2211 return getLyXText(bv)->cursor.par();
2215 bool InsetText::cboundary(BufferView * bv) const
2217 return getLyXText(bv)->cursor.boundary();
2221 Row * InsetText::crow(BufferView * bv) const
2223 return getLyXText(bv)->cursor.row();
2227 LyXText * InsetText::getLyXText(BufferView const * lbv,
2228 bool const recursive) const
2230 if (cached_bview == lbv) {
2231 if (recursive && the_locking_inset)
2232 return the_locking_inset->getLyXText(lbv, true);
2233 LyXText * lt = cached_text.get();
2234 lyx::Assert(lt && lt->firstRow()->par() == &*(paragraphs.begin()));
2237 // Super UGLY! (Lgb)
2238 BufferView * bv = const_cast<BufferView *>(lbv);
2241 Cache::iterator it = cache.find(bv);
2243 if (it != cache.end()) {
2246 } else if (do_resize) {
2247 resizeLyXText(do_resize);
2249 if (lt || !it->second.remove) {
2250 lyx::Assert(it->second.text.get());
2251 cached_text = it->second.text;
2252 if (recursive && the_locking_inset) {
2253 return the_locking_inset->getLyXText(bv, true);
2255 return cached_text.get();
2256 } else if (it->second.remove) {
2258 saveLyXTextState(it->second.text.get());
2264 // when we have to reinit the existing LyXText!
2266 it->second.text->init(bv);
2267 restoreLyXTextState(bv, it->second.text.get());
2268 it->second.remove = false;
2270 cached_text = it->second.text;
2271 if (the_locking_inset && recursive) {
2272 return the_locking_inset->getLyXText(bv);
2274 return cached_text.get();
2277 // we are here only if we don't have a BufferView * in the cache!!!
2279 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2280 cached_text->init(bv);
2281 restoreLyXTextState(bv, cached_text.get());
2283 cache.insert(make_pair(bv, cached_text));
2285 if (the_locking_inset && recursive) {
2286 return the_locking_inset->getLyXText(bv);
2288 return cached_text.get();
2292 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2296 Cache::iterator it = cache.find(bv);
2298 if (it == cache.end()) {
2302 lyx::Assert(it->second.text.get());
2304 it->second.remove = true;
2306 /// then remove all LyXText in text-insets
2307 for_each(paragraphs.begin(), paragraphs.end(),
2308 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2313 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2316 // we cannot resize this because we are in use!
2317 // so do this on the next possible getLyXText()
2322 // lyxerr << "InsetText::resizeLyXText\n";
2323 if (!paragraphs.begin()->next() && paragraphs.begin()->empty()) { // no data, resize not neccessary!
2324 // we have to do this as a fixed width may have changed!
2325 LyXText * t = getLyXText(bv);
2326 saveLyXTextState(t);
2328 restoreLyXTextState(bv, t);
2331 // one endless line, resize normally not necessary
2332 if (!force && getMaxWidth(bv, this) < 0)
2335 Cache::iterator it = cache.find(bv);
2336 if (it == cache.end()) {
2339 lyx::Assert(it->second.text.get());
2341 LyXText * t = it->second.text.get();
2342 saveLyXTextState(t);
2344 for_each(paragraphs.begin(), paragraphs.end(),
2345 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2348 restoreLyXTextState(bv, t);
2349 if (the_locking_inset) {
2350 inset_x = cix(bv) - top_x + drawTextXOffset;
2351 inset_y = ciy(bv) + drawTextYOffset;
2354 t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2356 updateLocal(bv, FULL, false);
2357 // this will scroll the screen such that the cursor becomes visible
2358 bv->updateScrollbar();
2360 need_update |= FULL;
2365 void InsetText::reinitLyXText() const
2368 // we cannot resize this because we are in use!
2369 // so do this on the next possible getLyXText()
2375 // lyxerr << "InsetText::reinitLyXText\n";
2376 for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2377 lyx::Assert(it->second.text.get());
2379 LyXText * t = it->second.text.get();
2380 BufferView * bv = it->first;
2382 saveLyXTextState(t);
2384 for_each(paragraphs.begin(), paragraphs.end(),
2385 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2388 restoreLyXTextState(bv, t);
2389 if (the_locking_inset) {
2390 inset_x = cix(bv) - top_x + drawTextXOffset;
2391 inset_y = ciy(bv) + drawTextYOffset;
2393 t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2395 updateLocal(bv, FULL, false);
2396 // this will scroll the screen such that the cursor becomes visible
2397 bv->updateScrollbar();
2405 void InsetText::removeNewlines()
2407 bool changed = false;
2409 ParagraphList::iterator it = paragraphs.begin();
2410 ParagraphList::iterator end = paragraphs.end();
2411 for (; it != end; ++it) {
2412 for (int i = 0; i < it->size(); ++i) {
2413 if (it->getChar(i) == Paragraph::META_NEWLINE) {
2424 bool InsetText::nodraw() const
2426 if (the_locking_inset)
2427 return the_locking_inset->nodraw();
2428 return UpdatableInset::nodraw();
2432 int InsetText::scroll(bool recursive) const
2434 int sx = UpdatableInset::scroll(false);
2436 if (recursive && the_locking_inset)
2437 sx += the_locking_inset->scroll(recursive);
2443 bool InsetText::doClearArea() const
2445 return !locked || (need_update & (FULL|INIT));
2449 void InsetText::selectAll(BufferView * bv)
2451 getLyXText(bv)->cursorTop(bv);
2452 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2453 getLyXText(bv)->cursorBottom(bv);
2454 getLyXText(bv)->setSelection(bv);
2458 void InsetText::clearSelection(BufferView * bv)
2460 getLyXText(bv)->clearSelection();
2464 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2466 Painter & pain = bv->painter();
2468 int h = insetAscent + insetDescent;
2469 int ty = baseline - insetAscent;
2475 if ((ty + h) > pain.paperHeight())
2476 h = pain.paperHeight();
2477 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2478 w = pain.paperWidth();
2479 // w -= TEXT_TO_INSET_OFFSET;
2480 pain.fillRectangle(top_x + 1, ty + 1, w - 1, h - 1, backgroundColor());
2483 frame_is_visible = false;
2487 Paragraph * InsetText::getParFromID(int id) const
2489 ParagraphList::iterator it = paragraphs.begin();
2490 ParagraphList::iterator end = paragraphs.end();
2491 for (; it != end; ++it) {
2492 if (it->id() == id) {
2495 Paragraph * tmp2 = it->getParFromID(id);
2504 Paragraph * InsetText::firstParagraph() const
2507 if (the_locking_inset)
2508 if ((result = the_locking_inset->firstParagraph()))
2510 return &*(paragraphs.begin());
2514 Paragraph * InsetText::getFirstParagraph(int i) const
2516 return (i == 0) ? &*(paragraphs.begin()) : 0;
2520 LyXCursor const & InsetText::cursor(BufferView * bv) const
2522 if (the_locking_inset)
2523 return the_locking_inset->cursor(bv);
2524 return getLyXText(bv)->cursor;
2528 Paragraph * InsetText::paragraph() const
2530 return &*(paragraphs.begin());
2534 void InsetText::paragraph(Paragraph * p)
2536 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2537 // caller of this function has to take care of it. This IS important
2538 // as we could have to insert a paragraph before this one and just
2539 // link the actual to a new ones next and set it with this function
2542 // set ourself as owner for all the paragraphs inserted!
2543 for_each(paragraphs.begin(), paragraphs.end(),
2544 boost::bind(&Paragraph::setInsetOwner, _1, this));
2547 // redraw myself when asked for
2552 Inset * InsetText::getInsetFromID(int id_arg) const
2555 return const_cast<InsetText *>(this);
2557 ParagraphList::iterator pit = paragraphs.begin();
2558 ParagraphList::iterator pend = paragraphs.end();
2559 for (; pit != pend; ++pit) {
2560 InsetList::iterator it = pit->insetlist.begin();
2561 InsetList::iterator end = pit->insetlist.end();
2562 for (; it != end; ++it) {
2563 if (it.getInset()->id() == id_arg)
2564 return it.getInset();
2565 Inset * in = it.getInset()->getInsetFromID(id_arg);
2575 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2576 float & value) const
2582 lt = getLyXText(bv);
2585 if (the_locking_inset) {
2586 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2587 if (!word.word().empty()) {
2593 // we have to go on checking so move cursor to the next char
2594 lt->cursor.pos(lt->cursor.pos() + 1);
2596 word = lt->selectNextWordToSpellcheck(bv, value);
2597 if (word.word().empty())
2598 bv->unlockInset(const_cast<InsetText *>(this));
2607 void InsetText::selectSelectedWord(BufferView * bv)
2609 if (the_locking_inset) {
2610 the_locking_inset->selectSelectedWord(bv);
2613 getLyXText(bv)->selectSelectedWord(bv);
2614 updateLocal(bv, SELECTION, false);
2618 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2620 if (the_locking_inset) {
2621 the_locking_inset->toggleSelection(bv, kill_selection);
2625 lt = getLyXText(bv);
2629 int x = top_x + TEXT_TO_INSET_OFFSET;
2631 Row * row = lt->firstRow();
2632 int y_offset = top_baseline - row->ascent_of_text();
2634 while ((row != 0) && ((y+row->height()) <= 0)) {
2641 if (need_update & SELECTION)
2643 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2649 bool InsetText::searchForward(BufferView * bv, string const & str,
2654 lt = getLyXText(bv);
2657 if (the_locking_inset) {
2658 if (the_locking_inset->searchForward(bv, str, cs, mw))
2660 lt->cursorRight(bv, true);
2662 lyxfind::SearchResult result =
2663 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2665 if (result == lyxfind::SR_FOUND) {
2666 LyXCursor cur = lt->cursor;
2667 bv->unlockInset(bv->theLockingInset());
2668 if (bv->lockInset(this))
2671 lt->setSelectionOverString(bv, str);
2672 updateLocal(bv, SELECTION, false);
2676 return (result != lyxfind::SR_NOT_FOUND);
2679 bool InsetText::searchBackward(BufferView * bv, string const & str,
2682 if (the_locking_inset) {
2683 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2688 lt = getLyXText(bv);
2692 Paragraph * p = &*(paragraphs.begin());
2695 lt->setCursor(bv, p, p->size());
2697 lyxfind::SearchResult result =
2698 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2700 if (result == lyxfind::SR_FOUND) {
2701 LyXCursor cur = lt->cursor;
2702 bv->unlockInset(bv->theLockingInset());
2703 if (bv->lockInset(this))
2706 lt->setSelectionOverString(bv, str);
2707 updateLocal(bv, SELECTION, false);
2711 return (result != lyxfind::SR_NOT_FOUND);
2715 bool InsetText::checkInsertChar(LyXFont & font)
2718 return owner()->checkInsertChar(font);
2723 void InsetText::collapseParagraphs(BufferView * bv) const
2725 BufferParams const & bparams = bv->buffer()->params;
2726 LyXText * llt = getLyXText(bv);
2728 while (paragraphs.begin()->next()) {
2729 if (!paragraphs.begin()->empty() && !paragraphs.begin()->next()->empty() &&
2730 !paragraphs.begin()->isSeparator(paragraphs.begin()->size() - 1))
2732 paragraphs.begin()->insertChar(paragraphs.begin()->size(), ' ');
2734 if (llt->selection.set()) {
2735 if (llt->selection.start.par() == paragraphs.begin()->next()) {
2736 llt->selection.start.par(&*(paragraphs.begin()));
2737 llt->selection.start.pos(
2738 llt->selection.start.pos() + paragraphs.begin()->size());
2740 if (llt->selection.end.par() == paragraphs.begin()->next()) {
2741 llt->selection.end.par(&*(paragraphs.begin()));
2742 llt->selection.end.pos(
2743 llt->selection.end.pos() + paragraphs.begin()->size());
2746 pasteParagraph(bparams, &*paragraphs.begin());
2752 void InsetText::getDrawFont(LyXFont & font) const
2756 owner()->getDrawFont(font);
2760 void InsetText::appendParagraphs(BufferParams const & bparams,
2764 Paragraph * tmpbuf = newpar;
2765 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2767 while (tmpbuf->next()) {
2768 tmpbuf = tmpbuf->next();
2769 lastbuffer->next(new Paragraph(*tmpbuf, false));
2770 lastbuffer->next()->previous(lastbuffer);
2771 lastbuffer = lastbuffer->next();
2773 lastbuffer = &*(paragraphs.begin());
2774 while (lastbuffer->next())
2775 lastbuffer = lastbuffer->next();
2776 if (!newpar->empty() && !lastbuffer->empty() &&
2777 !lastbuffer->isSeparator(lastbuffer->size() - 1))
2779 lastbuffer->insertChar(lastbuffer->size(), ' ');
2782 // make the buf exactly the same layout than our last paragraph
2783 buf->makeSameLayout(lastbuffer);
2786 lastbuffer->next(buf);
2787 buf->previous(lastbuffer);
2788 pasteParagraph(bparams, lastbuffer);
2794 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2796 Paragraph * par = getFirstParagraph(0);
2798 InsetList::iterator it = par->insetlist.begin();
2799 InsetList::iterator end = par->insetlist.end();
2800 for (; it != end; ++it) {
2801 it.getInset()->addPreview(loader);