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"
41 #include "frontends/Alert.h"
42 #include "frontends/Dialogs.h"
43 #include "frontends/font_metrics.h"
44 #include "frontends/LyXView.h"
45 #include "frontends/Painter.h"
46 #include "frontends/screen.h"
48 #include "support/textutils.h"
49 #include "support/LAssert.h"
50 #include "support/lstrings.h"
51 #include "support/lyxalgo.h" // lyx::count
53 #include <boost/bind.hpp>
71 using lyx::textclass_type;
73 extern unsigned char getCurrentTextClass(Buffer *);
74 extern bool math_insert_greek(BufferView *, char);
75 extern int greek_kb_flag;
78 // These functions should probably go into bufferview_funcs somehow (Jug)
80 void InsetText::saveLyXTextState(LyXText * t) const
82 // check if my paragraphs are still valid
83 ParagraphList::iterator it = paragraphs.begin();
84 ParagraphList::iterator end = paragraphs.end();
85 for (; it != end; ++it) {
86 if (&*it == t->cursor.par())
90 if (it != end && t->cursor.pos() <= it->size()) {
91 sstate.lpar = t->cursor.par();
92 sstate.pos = t->cursor.pos();
93 sstate.boundary = t->cursor.boundary();
94 sstate.selstartpar = t->selection.start.par();
95 sstate.selstartpos = t->selection.start.pos();
96 sstate.selstartboundary = t->selection.start.boundary();
97 sstate.selendpar = t->selection.end.par();
98 sstate.selendpos = t->selection.end.pos();
99 sstate.selendboundary = t->selection.end.boundary();
100 sstate.selection = t->selection.set();
101 sstate.mark_set = t->selection.mark();
102 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());
148 if (bp.tracking_changes)
149 paragraphs.begin()->trackChanges();
154 InsetText::InsetText(InsetText const & in, bool same_id)
155 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
162 InsetText & InsetText::operator=(InsetText const & it)
169 void InsetText::init(InsetText const * ins, bool same_id)
172 setParagraphData(&*(ins->paragraphs.begin()), same_id);
173 autoBreakRows = ins->autoBreakRows;
174 drawFrame_ = ins->drawFrame_;
175 frame_color = ins->frame_color;
179 for_each(paragraphs.begin(), paragraphs.end(),
180 boost::bind(&Paragraph::setInsetOwner, _1, this));
182 the_locking_inset = 0;
184 frame_color = LColor::insetframe;
185 autoBreakRows = false;
198 last_drawn_width = -1;
199 frame_is_visible = false;
202 in_insetAllowed = false;
206 InsetText::~InsetText()
212 void InsetText::clear(bool just_mark_erased)
214 if (just_mark_erased) {
215 ParagraphList::iterator it = paragraphs.begin();
216 ParagraphList::iterator end = paragraphs.end();
217 for (; it != end; ++it) {
224 // This is a gross hack...
225 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
228 paragraphs.set(new Paragraph);
229 paragraphs.begin()->setInsetOwner(this);
230 paragraphs.begin()->layout(old_layout);
237 Inset * InsetText::clone(Buffer const &, bool same_id) const
239 return new InsetText(*this, same_id);
243 void InsetText::write(Buffer const * buf, ostream & os) const
246 writeParagraphData(buf, os);
250 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
252 ParagraphList::iterator it = paragraphs.begin();
253 ParagraphList::iterator end = paragraphs.end();
254 Paragraph::depth_type dth = 0;
255 for (; it != end; ++it) {
256 it->write(buf, os, buf->params, dth);
261 void InsetText::read(Buffer const * buf, LyXLex & lex)
265 Paragraph::depth_type depth = 0;
266 LyXFont font(LyXFont::ALL_INHERIT);
270 if (buf->params.tracking_changes)
271 paragraphs.begin()->trackChanges();
273 // delete the initial paragraph
275 ParagraphList::iterator pit = paragraphs.begin();
279 token = lex.getString();
282 if (token == "\\end_inset") {
286 if (token == "\\the_end") {
287 lex.printError("\\the_end read in inset! Error in document!");
293 const_cast<Buffer*>(buf)->readToken(lex, paragraphs, pit,
294 token, pos, depth, font);
297 pit = paragraphs.begin();
298 ParagraphList::iterator const end = paragraphs.end();
299 for (; pit != end; ++pit)
300 pit->setInsetOwner(this);
302 if (token != "\\end_inset") {
303 lex.printError("Missing \\end_inset at this point. "
310 int InsetText::ascent(BufferView * bv, LyXFont const &) const
312 insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
313 TEXT_TO_INSET_OFFSET;
318 int InsetText::descent(BufferView * bv, LyXFont const &) const
320 LyXText * llt = getLyXText(bv);
321 insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
322 TEXT_TO_INSET_OFFSET;
327 int InsetText::width(BufferView * bv, LyXFont const &) const
329 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
330 (2 * TEXT_TO_INSET_OFFSET);
331 insetWidth = max(insetWidth, 10);
336 int InsetText::textWidth(BufferView * bv, bool fordraw) const
339 if (!autoBreakRows) {
342 w = getMaxWidth(bv, this);
345 return max(w - (2 * TEXT_TO_INSET_OFFSET),
346 (int)getLyXText(bv)->width);
350 return w - (2 * TEXT_TO_INSET_OFFSET);
354 void InsetText::draw(BufferView * bv, LyXFont const & f,
355 int baseline, float & x, bool cleared) const
360 Painter & pain = bv->painter();
362 // this is the first thing we have to ask because if the x pos
363 // changed we have to do a complete rebreak of the text as we
364 // may have few space to draw in. Well we should check on this too
366 if (top_x != int(x)) {
369 int nw = getMaxWidth(bv, this);
370 if (nw > 0 && old_max_width != nw) {
373 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
378 // call these methods so that insetWidth, insetAscent and
379 // insetDescent have the right values.
384 // repaint the background if needed
385 if (cleared && backgroundColor() != LColor::background) {
386 clearInset(bv, baseline, cleared);
389 // no draw is necessary !!!
390 if ((drawFrame_ == LOCKED) && !locked && paragraphs.begin()->empty()) {
391 top_baseline = baseline;
393 if (need_update & CLEAR_FRAME)
394 clearFrame(pain, cleared);
400 x += static_cast<float>(scroll());
402 // if top_x differs we did it already
403 if (!cleared && (old_x == int(x))
404 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
405 ||(last_drawn_width != insetWidth)))
407 // Condition necessary to eliminate bug 59 attachment 37
409 clearInset(bv, baseline, cleared);
413 frame_is_visible = false;
415 if (!cleared && (need_update == NONE)) {
417 drawFrame(pain, cleared);
421 top_baseline = baseline;
422 top_y = baseline - insetAscent;
424 if (last_drawn_width != insetWidth) {
426 clearInset(bv, baseline, cleared);
428 last_drawn_width = insetWidth;
431 if (the_locking_inset && (cpar(bv) == inset_par)
432 && (cpos(bv) == inset_pos)) {
433 inset_x = cix(bv) - top_x + drawTextXOffset;
434 inset_y = ciy(bv) + drawTextYOffset;
436 if (!cleared && (need_update == CURSOR)
437 && !getLyXText(bv)->selection.set()) {
438 drawFrame(pain, cleared);
448 x += TEXT_TO_INSET_OFFSET;
450 Row * row = lt->firstRow();
451 int y_offset = baseline - row->ascent_of_text();
452 int ph = pain.paperHeight();
455 while ((row != 0) && ((y+row->height()) <= 0)) {
457 first += row->height();
461 lt->top_y(-y_offset);
468 if (cleared || (need_update&(INIT|FULL))) {
469 int yf = y_offset + first;
471 while ((row != 0) && (yf < ph)) {
472 Row * prev = row->previous();
473 RowPainter rp(*bv, *lt, *row);
474 if (rp.paint(y + y_offset + first, int(x), y + lt->top_y(), cleared))
475 lt->markChangeInDraw(bv, row, prev);
476 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
477 lt->need_break_row = row;
479 lt->setCursor(bv, lt->cursor.par(),
481 if (lt->selection.set()) {
482 lt->setCursor(bv, lt->selection.start,
483 lt->selection.start.par(),
484 lt->selection.start.pos());
485 lt->setCursor(bv, lt->selection.end,
486 lt->selection.end.par(),
487 lt->selection.end.pos());
495 } else if (!locked) {
496 if (need_update & CURSOR) {
497 bv->screen().toggleSelection(lt, bv, true, y_offset,int(x));
498 lt->clearSelection();
499 lt->selection.cursor = lt->cursor;
501 bv->screen().update(lt, bv, y_offset, int(x));
504 if (need_update & SELECTION) {
505 bv->screen().toggleToggle(lt, bv, y_offset, int(x));
506 } else if (need_update & CURSOR) {
507 bv->screen().toggleSelection(lt, bv, true, y_offset,int(x));
508 lt->clearSelection();
509 lt->selection.cursor = lt->cursor;
511 bv->screen().update(lt, bv, y_offset, int(x));
516 lt->status(bv, LyXText::UNCHANGED);
517 if ((drawFrame_ == ALWAYS) ||
518 ((cleared || (need_update != CURSOR_PAR)) &&
519 (drawFrame_ == LOCKED) && locked))
521 drawFrame(pain, cleared);
522 } else if (need_update & CLEAR_FRAME) {
523 clearFrame(pain, cleared);
526 x += insetWidth - TEXT_TO_INSET_OFFSET;
528 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
530 } else if (need_update != INIT) {
538 void InsetText::drawFrame(Painter & pain, bool cleared) const
540 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
541 if (!frame_is_visible || cleared) {
542 frame_x = top_x + ttoD2;
543 frame_y = top_baseline - insetAscent + ttoD2;
544 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
545 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
546 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
548 frame_is_visible = true;
553 void InsetText::clearFrame(Painter & pain, bool cleared) const
555 if (frame_is_visible) {
557 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
560 frame_is_visible = false;
565 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
568 if (reinit && owner()) {
570 owner()->update(bv, font, true);
575 if (reinit || need_update == INIT) {
577 // we should put this call where we set need_update to INIT!
580 owner()->update(bv, font, true);
585 if (!autoBreakRows && paragraphs.begin()->next())
586 collapseParagraphs(bv);
588 if (the_locking_inset) {
589 inset_x = cix(bv) - top_x + drawTextXOffset;
590 inset_y = ciy(bv) + drawTextYOffset;
591 the_locking_inset->update(bv, font, reinit);
599 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
602 lt->updateInset(bv, the_locking_inset);
604 if (lt->status() == LyXText::NEED_MORE_REFRESH)
612 void InsetText::setUpdateStatus(BufferView * bv, int what) const
614 // this does nothing dangerous so use only a localized buffer
615 LyXText * llt = getLyXText(bv);
618 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
619 // if we don't break row so that we only have one row to update!
620 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
622 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
625 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
626 need_update |= CURSOR_PAR;
629 // this to not draw a selection when we redraw all of it!
630 if (need_update & CURSOR && !(need_update & SELECTION)) {
631 if (llt->selection.set())
633 llt->clearSelection();
638 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
640 if (!autoBreakRows && paragraphs.begin()->next())
641 collapseParagraphs(bv);
648 setUpdateStatus(bv, what);
649 bool flag = mark_dirty ||
650 (((need_update != CURSOR) && (need_update != NONE)) ||
651 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
652 if (!lt->selection.set())
653 lt->selection.cursor = lt->cursor;
657 // IMO this is not anymore needed as we do this in fitInsetCursor!
658 // and we always get "true" as returnvalue of this function in the
659 // case of a locking inset (Jug 20020412)
660 if (locked && (need_update & CURSOR) && bv->fitCursor())
666 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
668 if (need_update == CURSOR)
670 bv->owner()->view_state_changed();
671 bv->owner()->updateMenubar();
672 bv->owner()->updateToolbar();
673 if (old_par != cpar(bv)) {
674 bv->owner()->setLayout(cpar(bv)->layout()->name());
680 string const InsetText::editMessage() const
682 return _("Opened Text Inset");
686 void InsetText::edit(BufferView * bv, int x, int y, mouse_button::state button)
688 UpdatableInset::edit(bv, x, y, button);
690 if (!bv->lockInset(this)) {
691 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
695 the_locking_inset = 0;
696 inset_pos = inset_x = inset_y = 0;
697 inset_boundary = false;
700 int tmp_y = (y < 0) ? 0 : y;
706 // we put here -1 and not button as now the button in the
707 // edit call should not be needed we will fix this in 1.3.x
708 // cycle hopefully (Jug 20020509)
709 // FIXME: GUII I've changed this to none: probably WRONG
710 if (!checkAndActivateInset(bv, x, tmp_y, mouse_button::none)) {
711 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
713 lt->cursor.x_fix(lt->cursor.x());
715 lt->clearSelection();
717 // If the inset is empty set the language of the current font to the
718 // language to the surronding text (if different).
719 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
720 bv->getParentLanguage(this) != lt->current_font.language())
722 LyXFont font(LyXFont::ALL_IGNORE);
723 font.setLanguage(bv->getParentLanguage(this));
724 setFont(bv, font, false);
730 if (drawFrame_ == LOCKED)
731 code = CURSOR|DRAW_FRAME;
732 updateLocal(bv, code, false);
735 // Tell the paragraph dialog that we've entered an insettext.
736 bv->owner()->getDialogs().updateParagraph();
740 void InsetText::edit(BufferView * bv, bool front)
742 UpdatableInset::edit(bv, front);
744 if (!bv->lockInset(this)) {
745 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
749 the_locking_inset = 0;
750 inset_pos = inset_x = inset_y = 0;
751 inset_boundary = false;
760 lt->setCursor(bv, &*(paragraphs.begin()), 0);
762 ParagraphList::iterator it = paragraphs.begin();
763 ParagraphList::iterator end = paragraphs.end();
764 while (boost::next(it) != end)
766 // int const pos = (p->size() ? p->size()-1 : p->size());
767 lt->setCursor(bv, &*it, it->size());
769 lt->clearSelection();
771 // If the inset is empty set the language of the current font to the
772 // language to the surronding text (if different).
773 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
774 bv->getParentLanguage(this) != lt->current_font.language()) {
775 LyXFont font(LyXFont::ALL_IGNORE);
776 font.setLanguage(bv->getParentLanguage(this));
777 setFont(bv, font, false);
782 if (drawFrame_ == LOCKED)
783 code = CURSOR|DRAW_FRAME;
784 updateLocal(bv, code, false);
789 void InsetText::insetUnlock(BufferView * bv)
791 if (the_locking_inset) {
792 the_locking_inset->insetUnlock(bv);
793 the_locking_inset = 0;
794 updateLocal(bv, CURSOR_PAR, false);
801 if (drawFrame_ == LOCKED)
802 code = CURSOR|CLEAR_FRAME;
806 if (drawFrame_ == LOCKED)
814 if (lt->selection.set()) {
815 lt->clearSelection();
817 } else if (owner()) {
818 bv->owner()->setLayout(owner()->getLyXText(bv)
819 ->cursor.par()->layout()->name());
821 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
822 // hack for deleteEmptyParMech
823 if (!paragraphs.begin()->empty()) {
824 lt->setCursor(bv, &*(paragraphs.begin()), 0);
825 } else if (paragraphs.begin()->next()) {
826 lt->setCursor(bv, paragraphs.begin()->next(), 0);
831 updateLocal(bv, code, false);
834 setUpdateStatus(bv, code);
839 void InsetText::lockInset(BufferView * bv)
842 the_locking_inset = 0;
843 inset_pos = inset_x = inset_y = 0;
844 inset_boundary = false;
852 lt->setCursor(bv, &*(paragraphs.begin()), 0);
853 lt->clearSelection();
855 // If the inset is empty set the language of the current font to the
856 // language to the surronding text (if different).
857 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
858 bv->getParentLanguage(this) != lt->current_font.language()) {
859 LyXFont font(LyXFont::ALL_IGNORE);
860 font.setLanguage(bv->getParentLanguage(this));
861 setFont(bv, font, false);
866 if (drawFrame_ == LOCKED)
867 code = CURSOR|DRAW_FRAME;
868 setUpdateStatus(bv, code);
872 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
874 the_locking_inset = inset;
875 inset_x = cix(bv) - top_x + drawTextXOffset;
876 inset_y = ciy(bv) + drawTextYOffset;
877 inset_pos = cpos(bv);
878 inset_par = cpar(bv);
879 inset_boundary = cboundary(bv);
880 updateLocal(bv, CURSOR, false);
884 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
886 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
890 if (!the_locking_inset) {
891 Paragraph * p = &*(paragraphs.begin());
892 int const id = inset->id();
894 InsetList::iterator it =
895 p->insetlist.begin();
896 InsetList::iterator const end =
898 for (; it != end; ++it) {
899 if (it.getInset() == inset) {
900 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
901 lockInset(bv, inset);
904 if (it.getInset()->getInsetFromID(id)) {
905 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
906 it.getInset()->edit(bv);
907 return the_locking_inset->lockInsetInInset(bv, inset);
914 if (inset == cpar(bv)->getInset(cpos(bv))) {
915 lyxerr[Debug::INSETS] << "OK" << endl;
916 lockInset(bv, inset);
918 } else if (the_locking_inset && (the_locking_inset == inset)) {
919 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
920 lyxerr[Debug::INSETS] << "OK" << endl;
921 inset_x = cix(bv) - top_x + drawTextXOffset;
922 inset_y = ciy(bv) + drawTextYOffset;
924 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
926 } else if (the_locking_inset) {
927 lyxerr[Debug::INSETS] << "MAYBE" << endl;
928 return the_locking_inset->lockInsetInInset(bv, inset);
930 lyxerr[Debug::INSETS] << "NOT OK" << endl;
935 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
938 if (!the_locking_inset)
940 if (the_locking_inset == inset) {
941 the_locking_inset->insetUnlock(bv);
942 getLyXText(bv)->updateInset(bv, inset);
943 the_locking_inset = 0;
945 moveRightIntern(bv, true, false);
946 old_par = 0; // force layout setting
950 updateLocal(bv, CURSOR, false);
953 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
957 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
959 if (!autoBreakRows && paragraphs.begin()->next())
960 collapseParagraphs(bv);
968 if (inset->owner() != this) {
969 int ustat = CURSOR_PAR;
971 UpdatableInset * tl_inset = the_locking_inset;
973 found = tl_inset->updateInsetInInset(bv, inset);
975 tl_inset = static_cast<UpdatableInset *>(inset);
976 while(tl_inset->owner() && tl_inset->owner() != this)
977 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
978 if (!tl_inset->owner())
980 found = tl_inset->updateInsetInInset(bv, inset);
984 lt->updateInset(bv, tl_inset);
988 setUpdateStatus(bv, ustat);
991 bool found = lt->updateInset(bv, inset);
995 setUpdateStatus(bv, CURSOR_PAR);
996 if (the_locking_inset &&
997 cpar(bv) == inset_par && cpos(bv) == inset_pos)
999 inset_x = cix(bv) - top_x + drawTextXOffset;
1000 inset_y = ciy(bv) + drawTextYOffset;
1007 void InsetText::lfunMousePress(FuncRequest const & cmd)
1009 no_selection = true;
1011 // use this to check mouse motion for selection!
1015 BufferView * bv = cmd.view();
1016 FuncRequest cmd1 = cmd;
1022 int tmp_x = cmd.x - drawTextXOffset;
1023 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->top_y();
1024 Inset * inset = getLyXText(bv)->checkInsetHit(bv, tmp_x, tmp_y);
1026 hideInsetCursor(bv);
1027 if (the_locking_inset) {
1028 if (the_locking_inset == inset) {
1029 the_locking_inset->localDispatch(cmd1);
1034 // otherwise unlock the_locking_inset and lock the new inset
1035 the_locking_inset->insetUnlock(bv);
1036 inset_x = cix(bv) - top_x + drawTextXOffset;
1037 inset_y = ciy(bv) + drawTextYOffset;
1038 the_locking_inset = 0;
1039 inset->localDispatch(cmd1);
1040 // inset->edit(bv, x - inset_x, y - inset_y, button);
1041 if (the_locking_inset)
1042 updateLocal(bv, CURSOR, false);
1046 // otherwise only unlock the_locking_inset
1047 the_locking_inset->insetUnlock(bv);
1048 the_locking_inset = 0;
1051 no_selection = false;
1053 if (bv->theLockingInset()) {
1054 if (isHighlyEditableInset(inset)) {
1055 // We just have to lock the inset before calling a
1056 // PressEvent on it!
1057 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
1058 if (!bv->lockInset(uinset)) {
1059 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1061 inset->localDispatch(cmd1);
1062 if (the_locking_inset)
1063 updateLocal(bv, CURSOR, false);
1067 if (!inset) { // && (button == mouse_button::button2)) {
1068 bool paste_internally = false;
1069 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
1070 localDispatch(FuncRequest(bv, LFUN_COPY));
1071 paste_internally = true;
1075 lt = getLyXText(bv);
1078 int old_top_y = lt->top_y();
1080 lt->setCursorFromCoordinates(bv, cmd.x - drawTextXOffset,
1081 cmd.y + insetAscent);
1082 // set the selection cursor!
1083 lt->selection.cursor = lt->cursor;
1084 lt->cursor.x_fix(lt->cursor.x());
1086 if (lt->selection.set()) {
1087 lt->clearSelection();
1090 updateLocal(bv, FULL, false);
1092 lt->clearSelection();
1095 updateLocal(bv, CURSOR, false);
1098 bv->owner()->setLayout(cpar(bv)->layout()->name());
1100 // we moved the view we cannot do mouse selection in this case!
1101 if (getLyXText(bv)->top_y() != old_top_y)
1102 no_selection = true;
1104 // Insert primary selection with middle mouse
1105 // if there is a local selection in the current buffer,
1107 if (cmd.button() == mouse_button::button2) {
1108 if (paste_internally)
1109 localDispatch(FuncRequest(bv, LFUN_PASTE));
1111 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
1114 getLyXText(bv)->clearSelection();
1116 showInsetCursor(bv);
1120 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
1122 BufferView * bv = cmd.view();
1123 FuncRequest cmd1 = cmd;
1127 no_selection = true;
1128 if (the_locking_inset)
1129 return the_locking_inset->localDispatch(cmd1);
1131 int tmp_x = cmd.x - drawTextXOffset;
1132 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->top_y();
1133 Inset * inset = getLyXText(bv)->checkInsetHit(bv, tmp_x, tmp_y);
1136 if (isHighlyEditableInset(inset))
1137 ret = inset->localDispatch(cmd1);
1139 inset_x = cix(bv) - top_x + drawTextXOffset;
1140 inset_y = ciy(bv) + drawTextYOffset;
1141 cmd1.x = cmd.x - inset_x;
1142 cmd1.y = cmd.x - inset_y;
1143 // note that we should do ret = inset->localDispatch(cmd1)
1144 // and fix this instead (Alfredo);
1146 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
1148 updateLocal(bv, CURSOR_PAR, false);
1154 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
1156 FuncRequest cmd1 = cmd;
1160 if (the_locking_inset) {
1161 the_locking_inset->localDispatch(cmd1);
1165 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
1168 BufferView * bv = cmd.view();
1171 lt = getLyXText(bv);
1174 hideInsetCursor(bv);
1175 LyXCursor cur = lt->cursor;
1176 lt->setCursorFromCoordinates
1177 (bv, cmd.x - drawTextXOffset, cmd.y + insetAscent);
1178 lt->cursor.x_fix(lt->cursor.x());
1179 if (cur == lt->cursor) {
1184 lt->setSelection(bv);
1185 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1186 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1190 updateLocal(bv, SELECTION, false);
1192 showInsetCursor(bv);
1196 Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
1198 BufferView * bv = ev.view();
1199 switch (ev.action) {
1200 case LFUN_MOUSE_PRESS:
1203 case LFUN_MOUSE_MOTION:
1204 lfunMouseMotion(ev);
1206 case LFUN_MOUSE_RELEASE:
1207 return lfunMouseRelease(ev) ? DISPATCHED : UNDISPATCHED;
1212 bool was_empty = (paragraphs.begin()->empty() && !paragraphs.begin()->next());
1213 no_selection = false;
1214 RESULT result = UpdatableInset::localDispatch(ev);
1215 if (result != UNDISPATCHED)
1218 result = DISPATCHED;
1219 if (ev.action < 0 && ev.argument.empty())
1222 if (the_locking_inset) {
1223 result = the_locking_inset->localDispatch(ev);
1224 if (result == DISPATCHED_NOUPDATE)
1226 else if (result == DISPATCHED) {
1227 updateLocal(bv, CURSOR_PAR, false);
1229 } else if (result >= FINISHED) {
1231 case FINISHED_RIGHT:
1232 moveRightIntern(bv, false, false);
1233 result = DISPATCHED;
1236 if ((result = moveUp(bv)) >= FINISHED) {
1237 updateLocal(bv, CURSOR, false);
1238 bv->unlockInset(this);
1242 if ((result = moveDown(bv)) >= FINISHED) {
1243 updateLocal(bv, CURSOR, false);
1244 bv->unlockInset(this);
1248 result = DISPATCHED;
1251 the_locking_inset = 0;
1252 updateLocal(bv, CURSOR, false);
1256 hideInsetCursor(bv);
1259 lt = getLyXText(bv);
1263 int updflag = false;
1264 switch (ev.action) {
1267 case LFUN_SELFINSERT:
1268 if (bv->buffer()->isReadonly()) {
1269 // setErrorMessage(N_("Document is read only"));
1272 if (!ev.argument.empty()) {
1273 /* Automatically delete the currently selected
1274 * text and replace it with what is being
1275 * typed in now. Depends on lyxrc settings
1276 * "auto_region_delete", which defaults to
1279 // This should not be needed here and is also WRONG!
1280 setUndo(bv, Undo::INSERT,
1281 lt->cursor.par(), lt->cursor.par()->next());
1284 if (lyxrc.auto_region_delete) {
1285 if (lt->selection.set()) {
1286 lt->cutSelection(bv, false, false);
1289 lt->clearSelection();
1290 for (string::size_type i = 0; i < ev.argument.length(); ++i) {
1291 bv->owner()->getIntl().getTransManager().
1292 TranslateAndInsert(ev.argument[i], lt);
1295 lt->selection.cursor = lt->cursor;
1296 updwhat = CURSOR | CURSOR_PAR;
1298 result = DISPATCHED_NOUPDATE;
1300 // --- Cursor Movements -----------------------------------
1303 moveRight(bv, false, true);
1304 lt->setSelection(bv);
1305 updwhat = SELECTION;
1308 result = moveRight(bv);
1314 moveLeft(bv, false, true);
1315 lt->setSelection(bv);
1316 updwhat = SELECTION;
1320 result = moveLeft(bv);
1326 lt->setSelection(bv);
1327 updwhat = SELECTION;
1331 result = moveDown(bv);
1337 lt->setSelection(bv);
1338 updwhat = SELECTION;
1342 result = moveUp(bv);
1355 if (!crow(bv)->previous())
1356 result = FINISHED_UP;
1358 lt->cursorPrevious(bv);
1359 result = DISPATCHED_NOUPDATE;
1364 if (!crow(bv)->next())
1365 result = FINISHED_DOWN;
1368 result = DISPATCHED_NOUPDATE;
1372 case LFUN_BACKSPACE: {
1373 if (lt->selection.set())
1374 lt->cutSelection(bv, true, false);
1377 updwhat = CURSOR_PAR;
1383 if (lt->selection.set()) {
1384 lt->cutSelection(bv, true, false);
1388 updwhat = CURSOR_PAR;
1394 lt->cutSelection(bv);
1395 updwhat = CURSOR_PAR;
1402 lt->copySelection(bv);
1403 updwhat = CURSOR_PAR;
1405 case LFUN_PASTESELECTION:
1407 string const clip(bv->getClipboard());
1411 if (ev.argument == "paragraph") {
1412 lt->insertStringAsParagraphs(bv, clip);
1414 lt->insertStringAsLines(bv, clip);
1417 lt->clearSelection();
1419 updwhat = CURSOR_PAR;
1424 if (!autoBreakRows) {
1426 if (CutAndPaste::nrOfParagraphs() > 1) {
1427 Alert::alert(_("Impossible operation!"),
1428 _("Cannot include more than one paragraph!"),
1434 // This should not be needed here and is also WRONG!
1435 setUndo(bv, Undo::INSERT,
1436 lt->cursor.par(), lt->cursor.par()->next());
1438 lt->pasteSelection(bv);
1440 lt->clearSelection();
1441 updwhat = CURSOR_PAR;
1446 case LFUN_BREAKPARAGRAPH:
1447 if (!autoBreakRows) {
1448 result = DISPATCHED;
1451 lt->breakParagraph(bv, paragraphs, 0);
1452 updwhat = CURSOR | FULL;
1455 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1456 if (!autoBreakRows) {
1457 result = DISPATCHED;
1460 lt->breakParagraph(bv, paragraphs, 1);
1461 updwhat = CURSOR | FULL;
1465 case LFUN_BREAKLINE: {
1466 if (!autoBreakRows) {
1467 result = DISPATCHED;
1471 // This should not be needed here and is also WRONG!
1472 setUndo(bv, Undo::INSERT,
1473 lt->cursor.par(), lt->cursor.par()->next());
1475 lt->insertChar(bv, Paragraph::META_NEWLINE);
1476 updwhat = CURSOR | CURSOR_PAR;
1482 // do not set layouts on non breakable textinsets
1483 if (autoBreakRows) {
1484 string cur_layout = cpar(bv)->layout()->name();
1486 // Derive layout number from given argument (string)
1487 // and current buffer's textclass (number). */
1488 LyXTextClass const & tclass =
1489 bv->buffer()->params.getLyXTextClass();
1490 string layout = ev.argument;
1491 bool hasLayout = tclass.hasLayout(layout);
1493 // If the entry is obsolete, use the new one instead.
1495 string const & obs =
1496 tclass[layout]->obsoleted_by();
1501 // see if we found the layout number:
1503 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + ev.argument + N_(" not known"));
1504 bv->owner()->dispatch(lf);
1508 if (cur_layout != layout) {
1509 cur_layout = layout;
1510 lt->setLayout(bv, layout);
1511 bv->owner()->setLayout(cpar(bv)->layout()->name());
1512 updwhat = CURSOR_PAR;
1516 // reset the layout box
1517 bv->owner()->setLayout(cpar(bv)->layout()->name());
1520 case LFUN_PARAGRAPH_SPACING:
1521 // This one is absolutely not working. When fiddling with this
1522 // it also seems to me that the paragraphs inside the insettext
1523 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1525 Paragraph * par = lt->cursor.par();
1526 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1527 float cur_value = 1.0;
1528 if (cur_spacing == Spacing::Other) {
1529 cur_value = par->params().spacing().getValue();
1532 istringstream istr(ev.argument.c_str());
1535 Spacing::Space new_spacing = cur_spacing;
1536 float new_value = cur_value;
1538 lyxerr << "Missing argument to `paragraph-spacing'"
1540 } else if (tmp == "single") {
1541 new_spacing = Spacing::Single;
1542 } else if (tmp == "onehalf") {
1543 new_spacing = Spacing::Onehalf;
1544 } else if (tmp == "double") {
1545 new_spacing = Spacing::Double;
1546 } else if (tmp == "other") {
1547 new_spacing = Spacing::Other;
1550 lyxerr << "new_value = " << tmpval << endl;
1553 } else if (tmp == "default") {
1554 new_spacing = Spacing::Default;
1556 lyxerr << _("Unknown spacing argument: ")
1557 << ev.argument << endl;
1559 if (cur_spacing != new_spacing || cur_value != new_value) {
1560 par->params().spacing(Spacing(new_spacing, new_value));
1561 updwhat = CURSOR_PAR;
1568 if (!bv->dispatch(ev))
1569 result = UNDISPATCHED;
1576 updateLocal(bv, updwhat, updflag);
1577 /// If the action has deleted all text in the inset, we need to change the
1578 // language to the language of the surronding text.
1579 if (!was_empty && paragraphs.begin()->empty() && !paragraphs.begin()->next()) {
1580 LyXFont font(LyXFont::ALL_IGNORE);
1581 font.setLanguage(bv->getParentLanguage(this));
1582 setFont(bv, font, false);
1585 if (result >= FINISHED)
1586 bv->unlockInset(this);
1588 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1589 result = DISPATCHED;
1594 int InsetText::latex(Buffer const * buf, ostream & os,
1595 bool moving_arg, bool) const
1598 latexParagraphs(buf, paragraphs,
1599 paragraphs.begin(), paragraphs.end(),
1600 os, texrow, moving_arg);
1601 return texrow.rows();
1605 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1607 unsigned int lines = 0;
1609 ParagraphList::iterator beg = paragraphs.begin();
1610 ParagraphList::iterator end = paragraphs.end();
1611 ParagraphList::iterator it = beg;
1612 for (; it != end; ++it) {
1613 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1614 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1621 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1623 Paragraph * p = &*(paragraphs.begin());
1624 unsigned int lines = 0;
1626 vector<string> environment_stack(10);
1627 vector<string> environment_inner(10);
1629 int const command_depth = 0;
1632 Paragraph::depth_type depth = 0; // paragraph depth
1636 int desc_on = 0; // description mode
1638 LyXLayout_ptr const & style = p->layout();
1640 // environment tag closing
1641 for (; depth > p->params().depth(); --depth) {
1642 if (environment_inner[depth] != "!-- --") {
1643 item_name = "listitem";
1644 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1645 if (environment_inner[depth] == "varlistentry")
1646 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1648 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1649 environment_stack[depth].erase();
1650 environment_inner[depth].erase();
1653 if (depth == p->params().depth()
1654 && environment_stack[depth] != style->latexname()
1655 && !environment_stack[depth].empty()) {
1656 if (environment_inner[depth] != "!-- --") {
1657 item_name= "listitem";
1658 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1659 if (environment_inner[depth] == "varlistentry")
1660 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1663 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1665 environment_stack[depth].erase();
1666 environment_inner[depth].erase();
1669 // Write opening SGML tags.
1670 switch (style->latextype) {
1671 case LATEX_PARAGRAPH:
1672 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1676 buf->sgmlError(p, 0, _("Error: LatexType Command not allowed here.\n"));
1680 case LATEX_ENVIRONMENT:
1681 case LATEX_ITEM_ENVIRONMENT:
1682 if (depth < p->params().depth()) {
1683 depth = p->params().depth();
1684 environment_stack[depth].erase();
1687 if (environment_stack[depth] != style->latexname()) {
1688 if (environment_stack.size() == depth + 1) {
1689 environment_stack.push_back("!-- --");
1690 environment_inner.push_back("!-- --");
1692 environment_stack[depth] = style->latexname();
1693 environment_inner[depth] = "!-- --";
1694 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1696 if (environment_inner[depth] != "!-- --") {
1697 item_name= "listitem";
1698 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1699 if (environment_inner[depth] == "varlistentry")
1700 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1704 if (style->latextype == LATEX_ENVIRONMENT) {
1705 if (!style->latexparam().empty()) {
1706 if (style->latexparam() == "CDATA")
1709 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1714 desc_on = (style->labeltype == LABEL_MANUAL);
1716 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1717 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1719 item_name = desc_on ? "term" : "para";
1720 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1724 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1728 buf->simpleDocBookOnePar(os, p, desc_on, depth + 1 + command_depth);
1732 // write closing SGML tags
1733 switch (style->latextype) {
1734 case LATEX_ENVIRONMENT:
1735 if (!style->latexparam().empty()) {
1736 if (style->latexparam() == "CDATA")
1739 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1742 case LATEX_ITEM_ENVIRONMENT:
1743 if (desc_on == 1) break;
1745 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1747 case LATEX_PARAGRAPH:
1748 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1751 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1757 for (int d = depth; d >= 0; --d) {
1758 if (!environment_stack[depth].empty()) {
1759 if (environment_inner[depth] != "!-- --") {
1760 item_name = "listitem";
1761 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1762 if (environment_inner[depth] == "varlistentry")
1763 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1766 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1774 void InsetText::validate(LaTeXFeatures & features) const
1776 for_each(paragraphs.begin(), paragraphs.end(),
1777 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1781 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1783 if (the_locking_inset) {
1784 the_locking_inset->getCursorPos(bv, x, y);
1787 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1788 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1792 int InsetText::insetInInsetY() const
1794 if (!the_locking_inset)
1797 return (inset_y + the_locking_inset->insetInInsetY());
1801 void InsetText::toggleInsetCursor(BufferView * bv)
1803 if (the_locking_inset) {
1804 the_locking_inset->toggleInsetCursor(bv);
1808 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1810 int const asc = font_metrics::maxAscent(font);
1811 int const desc = font_metrics::maxDescent(font);
1813 if (isCursorVisible())
1814 bv->hideLockedInsetCursor();
1816 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1817 toggleCursorVisible();
1821 void InsetText::showInsetCursor(BufferView * bv, bool show)
1823 if (the_locking_inset) {
1824 the_locking_inset->showInsetCursor(bv, show);
1827 if (!isCursorVisible()) {
1828 LyXFont const font =
1829 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1831 int const asc = font_metrics::maxAscent(font);
1832 int const desc = font_metrics::maxDescent(font);
1834 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1836 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1837 setCursorVisible(true);
1842 void InsetText::hideInsetCursor(BufferView * bv)
1844 if (isCursorVisible()) {
1845 bv->hideLockedInsetCursor();
1846 setCursorVisible(false);
1848 if (the_locking_inset)
1849 the_locking_inset->hideInsetCursor(bv);
1853 void InsetText::fitInsetCursor(BufferView * bv) const
1855 if (the_locking_inset) {
1856 the_locking_inset->fitInsetCursor(bv);
1859 LyXFont const font =
1860 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1862 int const asc = font_metrics::maxAscent(font);
1863 int const desc = font_metrics::maxDescent(font);
1865 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1866 need_update |= FULL;
1871 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1873 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1874 return moveLeftIntern(bv, false, activate_inset, selecting);
1876 return moveRightIntern(bv, true, activate_inset, selecting);
1881 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1883 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1884 return moveRightIntern(bv, true, activate_inset, selecting);
1886 return moveLeftIntern(bv, false, activate_inset, selecting);
1891 InsetText::moveRightIntern(BufferView * bv, bool front,
1892 bool activate_inset, bool selecting)
1894 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1895 return FINISHED_RIGHT;
1896 if (activate_inset && checkAndActivateInset(bv, front))
1898 getLyXText(bv)->cursorRight(bv);
1900 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1901 return DISPATCHED_NOUPDATE;
1906 InsetText::moveLeftIntern(BufferView * bv, bool front,
1907 bool activate_inset, bool selecting)
1909 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1911 getLyXText(bv)->cursorLeft(bv);
1913 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1914 if (activate_inset && checkAndActivateInset(bv, front))
1916 return DISPATCHED_NOUPDATE;
1920 Inset::RESULT InsetText::moveUp(BufferView * bv)
1922 if (!crow(bv)->previous())
1924 getLyXText(bv)->cursorUp(bv);
1925 return DISPATCHED_NOUPDATE;
1929 Inset::RESULT InsetText::moveDown(BufferView * bv)
1931 if (!crow(bv)->next())
1932 return FINISHED_DOWN;
1933 getLyXText(bv)->cursorDown(bv);
1934 return DISPATCHED_NOUPDATE;
1938 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1940 if (the_locking_inset) {
1941 if (the_locking_inset->insetAllowed(inset))
1942 return the_locking_inset->insertInset(bv, inset);
1945 inset->setOwner(this);
1946 hideInsetCursor(bv);
1947 getLyXText(bv)->insertInset(bv, inset);
1949 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1954 bool InsetText::insetAllowed(Inset::Code code) const
1956 // in_insetAllowed is a really gross hack,
1957 // to allow us to call the owner's insetAllowed
1958 // without stack overflow, which can happen
1959 // when the owner uses InsetCollapsable::insetAllowed()
1961 if (in_insetAllowed)
1963 in_insetAllowed = true;
1964 if (the_locking_inset)
1965 ret = the_locking_inset->insetAllowed(code);
1967 ret = owner()->insetAllowed(code);
1968 in_insetAllowed = false;
1973 UpdatableInset * InsetText::getLockingInset() const
1975 return the_locking_inset ? the_locking_inset->getLockingInset() :
1976 const_cast<InsetText *>(this);
1980 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1984 if (the_locking_inset)
1985 return the_locking_inset->getFirstLockingInsetOfType(c);
1990 bool InsetText::showInsetDialog(BufferView * bv) const
1992 if (the_locking_inset)
1993 return the_locking_inset->showInsetDialog(bv);
1998 vector<string> const InsetText::getLabelList() const
2000 vector<string> label_list;
2002 ParagraphList::iterator pit = paragraphs.begin();
2003 ParagraphList::iterator pend = paragraphs.end();
2004 for (; pit != pend; ++pit) {
2005 InsetList::iterator beg = pit->insetlist.begin();
2006 InsetList::iterator end = pit->insetlist.end();
2007 for (; beg != end; ++beg) {
2008 vector<string> const l = beg.getInset()->getLabelList();
2009 label_list.insert(label_list.end(), l.begin(), l.end());
2016 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
2019 if (the_locking_inset) {
2020 the_locking_inset->setFont(bv, font, toggleall, selectall);
2023 if ((!paragraphs.begin()->next() && paragraphs.begin()->empty()) || cpar(bv)->empty()) {
2024 getLyXText(bv)->setFont(bv, font, toggleall);
2029 lt = getLyXText(bv);
2032 if (lt->selection.set()) {
2033 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
2037 lt->toggleFree(bv, font, toggleall);
2039 lt->clearSelection();
2041 bool flag = (selectall || lt->selection.set());
2045 updateLocal(bv, FULL, true);
2047 updateLocal(bv, CURSOR_PAR, true);
2051 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
2053 if (cpar(bv)->isInset(cpos(bv))) {
2055 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
2056 if (!isHighlyEditableInset(inset))
2058 inset->edit(bv, front);
2059 if (!the_locking_inset)
2061 updateLocal(bv, CURSOR, false);
2068 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2069 mouse_button::state button)
2071 x -= drawTextXOffset;
2073 int dummyy = y + insetAscent;
2074 Inset * inset = getLyXText(bv)->checkInsetHit(bv, dummyx, dummyy);
2075 // we only do the edit() call if the inset was hit by the mouse
2076 // or if it is a highly editable inset. So we should call this
2077 // function from our own edit with button < 0.
2078 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
2080 if (button == mouse_button::none && !isHighlyEditableInset(inset))
2088 inset_x = cix(bv) - top_x + drawTextXOffset;
2089 inset_y = ciy(bv) + drawTextYOffset;
2090 inset->edit(bv, x - inset_x, y - inset_y, button);
2091 if (!the_locking_inset)
2093 updateLocal(bv, CURSOR, false);
2100 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2103 int w = UpdatableInset::getMaxWidth(bv, inset);
2108 w = w - top_x + owner()->x();
2111 w -= (2 * TEXT_TO_INSET_OFFSET);
2114 return UpdatableInset::getMaxWidth(bv, inset);
2119 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2121 // we have to unlock any locked inset otherwise we're in troubles
2122 the_locking_inset = 0;
2125 paragraphs.set(new Paragraph(*p, same_id));
2126 paragraphs.begin()->setInsetOwner(this);
2127 Paragraph * np = &*(paragraphs.begin());
2130 np->next(new Paragraph(*p, same_id));
2131 np->next()->previous(np);
2133 np->setInsetOwner(this);
2140 void InsetText::markNew(bool track_changes)
2142 ParagraphList::iterator pit = paragraphs.begin();
2143 ParagraphList::iterator pend = paragraphs.end();
2144 for (; pit != pend; ++pit) {
2145 if (track_changes) {
2146 pit->trackChanges();
2148 // no-op when not tracking
2149 pit->cleanChanges();
2155 void InsetText::setText(string const & data, LyXFont const & font)
2158 for (unsigned int i = 0; i < data.length(); ++i)
2159 paragraphs.begin()->insertChar(i, data[i], font);
2164 void InsetText::setAutoBreakRows(bool flag)
2166 if (flag != autoBreakRows) {
2167 autoBreakRows = flag;
2175 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2177 if (how != drawFrame_) {
2180 updateLocal(bv, DRAW_FRAME, false);
2185 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2187 if (frame_color != col) {
2190 updateLocal(bv, DRAW_FRAME, false);
2195 int InsetText::cx(BufferView * bv) const
2197 // we do nothing dangerous so we use a local cache
2198 LyXText * llt = getLyXText(bv);
2199 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2200 if (the_locking_inset) {
2201 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2203 if (font.isVisibleRightToLeft())
2204 x -= the_locking_inset->width(bv, font);
2210 int InsetText::cix(BufferView * bv) const
2212 // we do nothing dangerous so we use a local cache
2213 LyXText * llt = getLyXText(bv);
2214 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2215 if (the_locking_inset) {
2216 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2218 if (font.isVisibleRightToLeft())
2219 x -= the_locking_inset->width(bv, font);
2225 int InsetText::cy(BufferView * bv) const
2228 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2232 int InsetText::ciy(BufferView * bv) const
2235 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2239 pos_type InsetText::cpos(BufferView * bv) const
2241 return getLyXText(bv)->cursor.pos();
2245 Paragraph * InsetText::cpar(BufferView * bv) const
2247 return getLyXText(bv)->cursor.par();
2251 bool InsetText::cboundary(BufferView * bv) const
2253 return getLyXText(bv)->cursor.boundary();
2257 Row * InsetText::crow(BufferView * bv) const
2259 return getLyXText(bv)->cursor.row();
2263 LyXText * InsetText::getLyXText(BufferView const * lbv,
2264 bool const recursive) const
2266 if (cached_bview == lbv) {
2267 if (recursive && the_locking_inset)
2268 return the_locking_inset->getLyXText(lbv, true);
2269 LyXText * lt = cached_text.get();
2270 lyx::Assert(lt && lt->firstRow()->par() == &*(paragraphs.begin()));
2273 // Super UGLY! (Lgb)
2274 BufferView * bv = const_cast<BufferView *>(lbv);
2277 Cache::iterator it = cache.find(bv);
2279 if (it != cache.end()) {
2282 } else if (do_resize) {
2283 resizeLyXText(do_resize);
2285 if (lt || !it->second.remove) {
2286 lyx::Assert(it->second.text.get());
2287 cached_text = it->second.text;
2288 if (recursive && the_locking_inset) {
2289 return the_locking_inset->getLyXText(bv, true);
2291 return cached_text.get();
2292 } else if (it->second.remove) {
2294 saveLyXTextState(it->second.text.get());
2300 // when we have to reinit the existing LyXText!
2302 it->second.text->init(bv);
2303 restoreLyXTextState(bv, it->second.text.get());
2304 it->second.remove = false;
2306 cached_text = it->second.text;
2307 if (the_locking_inset && recursive) {
2308 return the_locking_inset->getLyXText(bv);
2310 return cached_text.get();
2313 // we are here only if we don't have a BufferView * in the cache!!!
2315 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2316 cached_text->init(bv);
2317 restoreLyXTextState(bv, cached_text.get());
2319 cache.insert(make_pair(bv, cached_text));
2321 if (the_locking_inset && recursive) {
2322 return the_locking_inset->getLyXText(bv);
2324 return cached_text.get();
2328 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2332 Cache::iterator it = cache.find(bv);
2334 if (it == cache.end()) {
2338 lyx::Assert(it->second.text.get());
2340 it->second.remove = true;
2342 /// then remove all LyXText in text-insets
2343 for_each(paragraphs.begin(), paragraphs.end(),
2344 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2349 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2352 // we cannot resize this because we are in use!
2353 // so do this on the next possible getLyXText()
2358 // lyxerr << "InsetText::resizeLyXText\n";
2359 if (!paragraphs.begin()->next() && paragraphs.begin()->empty()) { // no data, resize not neccessary!
2360 // we have to do this as a fixed width may have changed!
2361 LyXText * t = getLyXText(bv);
2362 saveLyXTextState(t);
2364 restoreLyXTextState(bv, t);
2367 // one endless line, resize normally not necessary
2368 if (!force && getMaxWidth(bv, this) < 0)
2371 Cache::iterator it = cache.find(bv);
2372 if (it == cache.end()) {
2375 lyx::Assert(it->second.text.get());
2377 LyXText * t = it->second.text.get();
2378 saveLyXTextState(t);
2380 for_each(paragraphs.begin(), paragraphs.end(),
2381 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2384 restoreLyXTextState(bv, t);
2385 if (the_locking_inset) {
2386 inset_x = cix(bv) - top_x + drawTextXOffset;
2387 inset_y = ciy(bv) + drawTextYOffset;
2390 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2392 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2393 // this will scroll the screen such that the cursor becomes visible
2394 bv->updateScrollbar();
2396 need_update |= FULL;
2401 void InsetText::reinitLyXText() const
2404 // we cannot resize this because we are in use!
2405 // so do this on the next possible getLyXText()
2411 // lyxerr << "InsetText::reinitLyXText\n";
2412 for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2413 lyx::Assert(it->second.text.get());
2415 LyXText * t = it->second.text.get();
2416 BufferView * bv = it->first;
2418 saveLyXTextState(t);
2420 for_each(paragraphs.begin(), paragraphs.end(),
2421 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2424 restoreLyXTextState(bv, t);
2425 if (the_locking_inset) {
2426 inset_x = cix(bv) - top_x + drawTextXOffset;
2427 inset_y = ciy(bv) + drawTextYOffset;
2429 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2431 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2432 // this will scroll the screen such that the cursor becomes visible
2433 bv->updateScrollbar();
2441 void InsetText::removeNewlines()
2443 bool changed = false;
2445 ParagraphList::iterator it = paragraphs.begin();
2446 ParagraphList::iterator end = paragraphs.end();
2447 for (; it != end; ++it) {
2448 for (int i = 0; i < it->size(); ++i) {
2449 if (it->getChar(i) == Paragraph::META_NEWLINE) {
2460 bool InsetText::nodraw() const
2462 if (the_locking_inset)
2463 return the_locking_inset->nodraw();
2464 return UpdatableInset::nodraw();
2468 int InsetText::scroll(bool recursive) const
2470 int sx = UpdatableInset::scroll(false);
2472 if (recursive && the_locking_inset)
2473 sx += the_locking_inset->scroll(recursive);
2479 bool InsetText::doClearArea() const
2481 return !locked || (need_update & (FULL|INIT));
2485 void InsetText::selectAll(BufferView * bv)
2487 getLyXText(bv)->cursorTop(bv);
2488 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2489 getLyXText(bv)->cursorBottom(bv);
2490 getLyXText(bv)->setSelection(bv);
2494 void InsetText::clearSelection(BufferView * bv)
2496 getLyXText(bv)->clearSelection();
2500 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2502 Painter & pain = bv->painter();
2504 int h = insetAscent + insetDescent;
2505 int ty = baseline - insetAscent;
2511 if ((ty + h) > pain.paperHeight())
2512 h = pain.paperHeight();
2513 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2514 w = pain.paperWidth();
2515 // w -= TEXT_TO_INSET_OFFSET;
2516 pain.fillRectangle(top_x + 1, ty + 1, w - 1, h - 1, backgroundColor());
2519 frame_is_visible = false;
2523 Paragraph * InsetText::firstParagraph() const
2526 if (the_locking_inset)
2527 if ((result = the_locking_inset->firstParagraph()))
2529 return &*(paragraphs.begin());
2533 Paragraph * InsetText::getFirstParagraph(int i) const
2535 return (i == 0) ? &*(paragraphs.begin()) : 0;
2539 LyXCursor const & InsetText::cursor(BufferView * bv) const
2541 if (the_locking_inset)
2542 return the_locking_inset->cursor(bv);
2543 return getLyXText(bv)->cursor;
2547 Paragraph * InsetText::paragraph() const
2549 return &*(paragraphs.begin());
2553 void InsetText::paragraph(Paragraph * p)
2555 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2556 // caller of this function has to take care of it. This IS important
2557 // as we could have to insert a paragraph before this one and just
2558 // link the actual to a new ones next and set it with this function
2561 // set ourself as owner for all the paragraphs inserted!
2562 for_each(paragraphs.begin(), paragraphs.end(),
2563 boost::bind(&Paragraph::setInsetOwner, _1, this));
2566 // redraw myself when asked for
2571 Inset * InsetText::getInsetFromID(int id_arg) const
2574 return const_cast<InsetText *>(this);
2576 ParagraphList::iterator pit = paragraphs.begin();
2577 ParagraphList::iterator pend = paragraphs.end();
2578 for (; pit != pend; ++pit) {
2579 InsetList::iterator it = pit->insetlist.begin();
2580 InsetList::iterator end = pit->insetlist.end();
2581 for (; it != end; ++it) {
2582 if (it.getInset()->id() == id_arg)
2583 return it.getInset();
2584 Inset * in = it.getInset()->getInsetFromID(id_arg);
2594 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2595 float & value) const
2601 lt = getLyXText(bv);
2604 if (the_locking_inset) {
2605 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2606 if (!word.word().empty()) {
2612 // we have to go on checking so move cursor to the next char
2613 lt->cursor.pos(lt->cursor.pos() + 1);
2615 word = lt->selectNextWordToSpellcheck(bv, value);
2616 if (word.word().empty())
2617 bv->unlockInset(const_cast<InsetText *>(this));
2626 void InsetText::selectSelectedWord(BufferView * bv)
2628 if (the_locking_inset) {
2629 the_locking_inset->selectSelectedWord(bv);
2632 getLyXText(bv)->selectSelectedWord(bv);
2633 updateLocal(bv, SELECTION, false);
2637 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2639 if (the_locking_inset) {
2640 the_locking_inset->toggleSelection(bv, kill_selection);
2644 lt = getLyXText(bv);
2648 int x = top_x + TEXT_TO_INSET_OFFSET;
2650 Row * row = lt->firstRow();
2651 int y_offset = top_baseline - row->ascent_of_text();
2653 while ((row != 0) && ((y+row->height()) <= 0)) {
2660 if (need_update & SELECTION)
2662 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2668 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2672 lt = getLyXText(bv);
2675 if (the_locking_inset) {
2676 if (the_locking_inset->nextChange(bv, length))
2678 lt->cursorRight(bv, true);
2680 lyxfind::SearchResult result =
2681 lyxfind::findNextChange(bv, lt, length);
2683 if (result == lyxfind::SR_FOUND) {
2684 LyXCursor cur = lt->cursor;
2685 bv->unlockInset(bv->theLockingInset());
2686 if (bv->lockInset(this))
2689 lt->setSelectionRange(bv, length);
2690 updateLocal(bv, SELECTION, false);
2694 return result != lyxfind::SR_NOT_FOUND;
2698 bool InsetText::searchForward(BufferView * bv, string const & str,
2703 lt = getLyXText(bv);
2706 if (the_locking_inset) {
2707 if (the_locking_inset->searchForward(bv, str, cs, mw))
2709 lt->cursorRight(bv, true);
2711 lyxfind::SearchResult result =
2712 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2714 if (result == lyxfind::SR_FOUND) {
2715 LyXCursor cur = lt->cursor;
2716 bv->unlockInset(bv->theLockingInset());
2717 if (bv->lockInset(this))
2720 lt->setSelectionRange(bv, str.length());
2721 updateLocal(bv, SELECTION, false);
2725 return (result != lyxfind::SR_NOT_FOUND);
2728 bool InsetText::searchBackward(BufferView * bv, string const & str,
2731 if (the_locking_inset) {
2732 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2737 lt = getLyXText(bv);
2741 Paragraph * p = &*(paragraphs.begin());
2744 lt->setCursor(bv, p, p->size());
2746 lyxfind::SearchResult result =
2747 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2749 if (result == lyxfind::SR_FOUND) {
2750 LyXCursor cur = lt->cursor;
2751 bv->unlockInset(bv->theLockingInset());
2752 if (bv->lockInset(this))
2755 lt->setSelectionRange(bv, str.length());
2756 updateLocal(bv, SELECTION, false);
2760 return (result != lyxfind::SR_NOT_FOUND);
2764 bool InsetText::checkInsertChar(LyXFont & font)
2767 return owner()->checkInsertChar(font);
2772 void InsetText::collapseParagraphs(BufferView * bv)
2774 LyXText * llt = getLyXText(bv);
2776 while (paragraphs.begin()->next()) {
2777 if (!paragraphs.begin()->empty() && !paragraphs.begin()->next()->empty() &&
2778 !paragraphs.begin()->isSeparator(paragraphs.begin()->size() - 1))
2780 paragraphs.begin()->insertChar(paragraphs.begin()->size(), ' ');
2782 if (llt->selection.set()) {
2783 if (llt->selection.start.par() == paragraphs.begin()->next()) {
2784 llt->selection.start.par(&*(paragraphs.begin()));
2785 llt->selection.start.pos(
2786 llt->selection.start.pos() + paragraphs.begin()->size());
2788 if (llt->selection.end.par() == paragraphs.begin()->next()) {
2789 llt->selection.end.par(&*(paragraphs.begin()));
2790 llt->selection.end.pos(
2791 llt->selection.end.pos() + paragraphs.begin()->size());
2794 mergeParagraph(bv->buffer()->params, paragraphs, paragraphs.begin());
2800 void InsetText::getDrawFont(LyXFont & font) const
2804 owner()->getDrawFont(font);
2808 void InsetText::appendParagraphs(Buffer * buffer,
2811 BufferParams const & bparams = buffer->params;
2813 Paragraph * tmpbuf = newpar;
2814 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2815 if (bparams.tracking_changes)
2816 buf->cleanChanges();
2818 while (tmpbuf->next()) {
2819 tmpbuf = tmpbuf->next();
2820 lastbuffer->next(new Paragraph(*tmpbuf, false));
2821 lastbuffer->next()->previous(lastbuffer);
2822 lastbuffer = lastbuffer->next();
2823 if (bparams.tracking_changes)
2824 lastbuffer->cleanChanges();
2826 lastbuffer = &*(paragraphs.begin());
2827 while (lastbuffer->next())
2828 lastbuffer = lastbuffer->next();
2829 if (!newpar->empty() && !lastbuffer->empty() &&
2830 !lastbuffer->isSeparator(lastbuffer->size() - 1))
2832 lastbuffer->insertChar(lastbuffer->size(), ' ');
2835 // make the buf exactly the same layout than our last paragraph
2836 buf->makeSameLayout(lastbuffer);
2839 lastbuffer->next(buf);
2840 buf->previous(lastbuffer);
2841 mergeParagraph(buffer->params, paragraphs, lastbuffer);
2847 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2849 Paragraph * par = getFirstParagraph(0);
2851 InsetList::iterator it = par->insetlist.begin();
2852 InsetList::iterator end = par->insetlist.end();
2853 for (; it != end; ++it) {
2854 it.getInset()->addPreview(loader);