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 * return_par = 0;
266 Paragraph::depth_type depth = 0;
267 LyXFont font(LyXFont::ALL_INHERIT);
271 if (buf->params.tracking_changes)
272 paragraphs.begin()->trackChanges();
276 token = lex.getString();
279 if (token == "\\end_inset") {
283 Paragraph * tmp = &*(paragraphs.begin());
285 if (const_cast<Buffer*>(buf)->
286 parseSingleLyXformat2Token(lex, tmp, return_par,
287 token, pos, depth, font)) {
288 // the_end read this should NEVER happen
289 lex.printError("\\the_end read in inset! Error in document!");
295 return_par = &*(paragraphs.begin());
296 paragraphs.set(return_par);
298 return_par->setInsetOwner(this);
299 return_par = return_par->next();
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->first_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->first_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) const
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)->first_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_first_y = lt->first_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)->first_y != old_first_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)->first_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, 0);
1452 updwhat = CURSOR | FULL;
1455 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1456 if (!autoBreakRows) {
1457 result = DISPATCHED;
1460 lt->breakParagraph(bv, 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 buf->latexParagraphs(os, &*(paragraphs.begin()), 0,
1599 texrow, moving_arg);
1600 return texrow.rows();
1604 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1606 unsigned int lines = 0;
1608 ParagraphList::iterator beg = paragraphs.begin();
1609 ParagraphList::iterator end = paragraphs.end();
1610 ParagraphList::iterator it = beg;
1611 for (; it != end; ++it) {
1612 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1613 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1620 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1622 Paragraph * p = &*(paragraphs.begin());
1623 unsigned int lines = 0;
1625 vector<string> environment_stack(10);
1626 vector<string> environment_inner(10);
1628 int const command_depth = 0;
1631 Paragraph::depth_type depth = 0; // paragraph depth
1635 int desc_on = 0; // description mode
1637 LyXLayout_ptr const & style = p->layout();
1639 // environment tag closing
1640 for (; depth > p->params().depth(); --depth) {
1641 if (environment_inner[depth] != "!-- --") {
1642 item_name = "listitem";
1643 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1644 if (environment_inner[depth] == "varlistentry")
1645 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1647 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1648 environment_stack[depth].erase();
1649 environment_inner[depth].erase();
1652 if (depth == p->params().depth()
1653 && environment_stack[depth] != style->latexname()
1654 && !environment_stack[depth].empty()) {
1655 if (environment_inner[depth] != "!-- --") {
1656 item_name= "listitem";
1657 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1658 if (environment_inner[depth] == "varlistentry")
1659 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1662 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1664 environment_stack[depth].erase();
1665 environment_inner[depth].erase();
1668 // Write opening SGML tags.
1669 switch (style->latextype) {
1670 case LATEX_PARAGRAPH:
1671 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1675 buf->sgmlError(p, 0, _("Error: LatexType Command not allowed here.\n"));
1679 case LATEX_ENVIRONMENT:
1680 case LATEX_ITEM_ENVIRONMENT:
1681 if (depth < p->params().depth()) {
1682 depth = p->params().depth();
1683 environment_stack[depth].erase();
1686 if (environment_stack[depth] != style->latexname()) {
1687 if (environment_stack.size() == depth + 1) {
1688 environment_stack.push_back("!-- --");
1689 environment_inner.push_back("!-- --");
1691 environment_stack[depth] = style->latexname();
1692 environment_inner[depth] = "!-- --";
1693 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1695 if (environment_inner[depth] != "!-- --") {
1696 item_name= "listitem";
1697 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1698 if (environment_inner[depth] == "varlistentry")
1699 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1703 if (style->latextype == LATEX_ENVIRONMENT) {
1704 if (!style->latexparam().empty()) {
1705 if (style->latexparam() == "CDATA")
1708 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1713 desc_on = (style->labeltype == LABEL_MANUAL);
1715 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1716 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1718 item_name = desc_on ? "term" : "para";
1719 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1723 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1727 buf->simpleDocBookOnePar(os, p, desc_on, depth + 1 + command_depth);
1731 // write closing SGML tags
1732 switch (style->latextype) {
1733 case LATEX_ENVIRONMENT:
1734 if (!style->latexparam().empty()) {
1735 if (style->latexparam() == "CDATA")
1738 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1741 case LATEX_ITEM_ENVIRONMENT:
1742 if (desc_on == 1) break;
1744 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1746 case LATEX_PARAGRAPH:
1747 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1750 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1756 for (int d = depth; d >= 0; --d) {
1757 if (!environment_stack[depth].empty()) {
1758 if (environment_inner[depth] != "!-- --") {
1759 item_name = "listitem";
1760 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1761 if (environment_inner[depth] == "varlistentry")
1762 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1765 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1773 void InsetText::validate(LaTeXFeatures & features) const
1775 for_each(paragraphs.begin(), paragraphs.end(),
1776 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1780 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1782 if (the_locking_inset) {
1783 the_locking_inset->getCursorPos(bv, x, y);
1786 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1787 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1791 int InsetText::insetInInsetY() const
1793 if (!the_locking_inset)
1796 return (inset_y + the_locking_inset->insetInInsetY());
1800 void InsetText::toggleInsetCursor(BufferView * bv)
1802 if (the_locking_inset) {
1803 the_locking_inset->toggleInsetCursor(bv);
1807 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1809 int const asc = font_metrics::maxAscent(font);
1810 int const desc = font_metrics::maxDescent(font);
1812 if (isCursorVisible())
1813 bv->hideLockedInsetCursor();
1815 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1816 toggleCursorVisible();
1820 void InsetText::showInsetCursor(BufferView * bv, bool show)
1822 if (the_locking_inset) {
1823 the_locking_inset->showInsetCursor(bv, show);
1826 if (!isCursorVisible()) {
1827 LyXFont const font =
1828 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1830 int const asc = font_metrics::maxAscent(font);
1831 int const desc = font_metrics::maxDescent(font);
1833 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1835 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1836 setCursorVisible(true);
1841 void InsetText::hideInsetCursor(BufferView * bv)
1843 if (isCursorVisible()) {
1844 bv->hideLockedInsetCursor();
1845 setCursorVisible(false);
1847 if (the_locking_inset)
1848 the_locking_inset->hideInsetCursor(bv);
1852 void InsetText::fitInsetCursor(BufferView * bv) const
1854 if (the_locking_inset) {
1855 the_locking_inset->fitInsetCursor(bv);
1858 LyXFont const font =
1859 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1861 int const asc = font_metrics::maxAscent(font);
1862 int const desc = font_metrics::maxDescent(font);
1864 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1865 need_update |= FULL;
1870 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1872 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1873 return moveLeftIntern(bv, false, activate_inset, selecting);
1875 return moveRightIntern(bv, true, activate_inset, selecting);
1880 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1882 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1883 return moveRightIntern(bv, true, activate_inset, selecting);
1885 return moveLeftIntern(bv, false, activate_inset, selecting);
1890 InsetText::moveRightIntern(BufferView * bv, bool front,
1891 bool activate_inset, bool selecting)
1893 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1894 return FINISHED_RIGHT;
1895 if (activate_inset && checkAndActivateInset(bv, front))
1897 getLyXText(bv)->cursorRight(bv);
1899 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1900 return DISPATCHED_NOUPDATE;
1905 InsetText::moveLeftIntern(BufferView * bv, bool front,
1906 bool activate_inset, bool selecting)
1908 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1910 getLyXText(bv)->cursorLeft(bv);
1912 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1913 if (activate_inset && checkAndActivateInset(bv, front))
1915 return DISPATCHED_NOUPDATE;
1919 Inset::RESULT InsetText::moveUp(BufferView * bv)
1921 if (!crow(bv)->previous())
1923 getLyXText(bv)->cursorUp(bv);
1924 return DISPATCHED_NOUPDATE;
1928 Inset::RESULT InsetText::moveDown(BufferView * bv)
1930 if (!crow(bv)->next())
1931 return FINISHED_DOWN;
1932 getLyXText(bv)->cursorDown(bv);
1933 return DISPATCHED_NOUPDATE;
1937 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1939 if (the_locking_inset) {
1940 if (the_locking_inset->insetAllowed(inset))
1941 return the_locking_inset->insertInset(bv, inset);
1944 inset->setOwner(this);
1945 hideInsetCursor(bv);
1946 getLyXText(bv)->insertInset(bv, inset);
1948 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1953 bool InsetText::insetAllowed(Inset::Code code) const
1955 // in_insetAllowed is a really gross hack,
1956 // to allow us to call the owner's insetAllowed
1957 // without stack overflow, which can happen
1958 // when the owner uses InsetCollapsable::insetAllowed()
1960 if (in_insetAllowed)
1962 in_insetAllowed = true;
1963 if (the_locking_inset)
1964 ret = the_locking_inset->insetAllowed(code);
1966 ret = owner()->insetAllowed(code);
1967 in_insetAllowed = false;
1972 UpdatableInset * InsetText::getLockingInset() const
1974 return the_locking_inset ? the_locking_inset->getLockingInset() :
1975 const_cast<InsetText *>(this);
1979 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1983 if (the_locking_inset)
1984 return the_locking_inset->getFirstLockingInsetOfType(c);
1989 bool InsetText::showInsetDialog(BufferView * bv) const
1991 if (the_locking_inset)
1992 return the_locking_inset->showInsetDialog(bv);
1997 vector<string> const InsetText::getLabelList() const
1999 vector<string> label_list;
2001 ParagraphList::iterator pit = paragraphs.begin();
2002 ParagraphList::iterator pend = paragraphs.end();
2003 for (; pit != pend; ++pit) {
2004 InsetList::iterator beg = pit->insetlist.begin();
2005 InsetList::iterator end = pit->insetlist.end();
2006 for (; beg != end; ++beg) {
2007 vector<string> const l = beg.getInset()->getLabelList();
2008 label_list.insert(label_list.end(), l.begin(), l.end());
2015 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
2018 if (the_locking_inset) {
2019 the_locking_inset->setFont(bv, font, toggleall, selectall);
2022 if ((!paragraphs.begin()->next() && paragraphs.begin()->empty()) || cpar(bv)->empty()) {
2023 getLyXText(bv)->setFont(bv, font, toggleall);
2028 lt = getLyXText(bv);
2031 if (lt->selection.set()) {
2032 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
2036 lt->toggleFree(bv, font, toggleall);
2038 lt->clearSelection();
2040 bool flag = (selectall || lt->selection.set());
2044 updateLocal(bv, FULL, true);
2046 updateLocal(bv, CURSOR_PAR, true);
2050 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
2052 if (cpar(bv)->isInset(cpos(bv))) {
2054 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
2055 if (!isHighlyEditableInset(inset))
2057 inset->edit(bv, front);
2058 if (!the_locking_inset)
2060 updateLocal(bv, CURSOR, false);
2067 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2068 mouse_button::state button)
2070 x -= drawTextXOffset;
2072 int dummyy = y + insetAscent;
2073 Inset * inset = getLyXText(bv)->checkInsetHit(bv, dummyx, dummyy);
2074 // we only do the edit() call if the inset was hit by the mouse
2075 // or if it is a highly editable inset. So we should call this
2076 // function from our own edit with button < 0.
2077 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
2079 if (button == mouse_button::none && !isHighlyEditableInset(inset))
2087 inset_x = cix(bv) - top_x + drawTextXOffset;
2088 inset_y = ciy(bv) + drawTextYOffset;
2089 inset->edit(bv, x - inset_x, y - inset_y, button);
2090 if (!the_locking_inset)
2092 updateLocal(bv, CURSOR, false);
2099 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2102 int w = UpdatableInset::getMaxWidth(bv, inset);
2107 w = w - top_x + owner()->x();
2110 w -= (2 * TEXT_TO_INSET_OFFSET);
2113 return UpdatableInset::getMaxWidth(bv, inset);
2118 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2120 // we have to unlock any locked inset otherwise we're in troubles
2121 the_locking_inset = 0;
2124 paragraphs.set(new Paragraph(*p, same_id));
2125 paragraphs.begin()->setInsetOwner(this);
2126 Paragraph * np = &*(paragraphs.begin());
2129 np->next(new Paragraph(*p, same_id));
2130 np->next()->previous(np);
2132 np->setInsetOwner(this);
2139 void InsetText::markNew(bool track_changes)
2141 ParagraphList::iterator pit = paragraphs.begin();
2142 ParagraphList::iterator pend = paragraphs.end();
2143 for (; pit != pend; ++pit) {
2144 if (track_changes) {
2145 pit->trackChanges();
2147 // no-op when not tracking
2148 pit->cleanChanges();
2154 void InsetText::setText(string const & data, LyXFont const & font)
2157 for (unsigned int i = 0; i < data.length(); ++i)
2158 paragraphs.begin()->insertChar(i, data[i], font);
2163 void InsetText::setAutoBreakRows(bool flag)
2165 if (flag != autoBreakRows) {
2166 autoBreakRows = flag;
2174 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2176 if (how != drawFrame_) {
2179 updateLocal(bv, DRAW_FRAME, false);
2184 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2186 if (frame_color != col) {
2189 updateLocal(bv, DRAW_FRAME, false);
2194 int InsetText::cx(BufferView * bv) const
2196 // we do nothing dangerous so we use a local cache
2197 LyXText * llt = getLyXText(bv);
2198 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2199 if (the_locking_inset) {
2200 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2202 if (font.isVisibleRightToLeft())
2203 x -= the_locking_inset->width(bv, font);
2209 int InsetText::cix(BufferView * bv) const
2211 // we do nothing dangerous so we use a local cache
2212 LyXText * llt = getLyXText(bv);
2213 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2214 if (the_locking_inset) {
2215 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2217 if (font.isVisibleRightToLeft())
2218 x -= the_locking_inset->width(bv, font);
2224 int InsetText::cy(BufferView * bv) const
2227 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2231 int InsetText::ciy(BufferView * bv) const
2234 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2238 pos_type InsetText::cpos(BufferView * bv) const
2240 return getLyXText(bv)->cursor.pos();
2244 Paragraph * InsetText::cpar(BufferView * bv) const
2246 return getLyXText(bv)->cursor.par();
2250 bool InsetText::cboundary(BufferView * bv) const
2252 return getLyXText(bv)->cursor.boundary();
2256 Row * InsetText::crow(BufferView * bv) const
2258 return getLyXText(bv)->cursor.row();
2262 LyXText * InsetText::getLyXText(BufferView const * lbv,
2263 bool const recursive) const
2265 if (cached_bview == lbv) {
2266 if (recursive && the_locking_inset)
2267 return the_locking_inset->getLyXText(lbv, true);
2268 LyXText * lt = cached_text.get();
2269 lyx::Assert(lt && lt->firstRow()->par() == &*(paragraphs.begin()));
2272 // Super UGLY! (Lgb)
2273 BufferView * bv = const_cast<BufferView *>(lbv);
2276 Cache::iterator it = cache.find(bv);
2278 if (it != cache.end()) {
2281 } else if (do_resize) {
2282 resizeLyXText(do_resize);
2284 if (lt || !it->second.remove) {
2285 lyx::Assert(it->second.text.get());
2286 cached_text = it->second.text;
2287 if (recursive && the_locking_inset) {
2288 return the_locking_inset->getLyXText(bv, true);
2290 return cached_text.get();
2291 } else if (it->second.remove) {
2293 saveLyXTextState(it->second.text.get());
2299 // when we have to reinit the existing LyXText!
2301 it->second.text->init(bv);
2302 restoreLyXTextState(bv, it->second.text.get());
2303 it->second.remove = false;
2305 cached_text = it->second.text;
2306 if (the_locking_inset && recursive) {
2307 return the_locking_inset->getLyXText(bv);
2309 return cached_text.get();
2312 // we are here only if we don't have a BufferView * in the cache!!!
2314 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2315 cached_text->init(bv);
2316 restoreLyXTextState(bv, cached_text.get());
2318 cache.insert(make_pair(bv, cached_text));
2320 if (the_locking_inset && recursive) {
2321 return the_locking_inset->getLyXText(bv);
2323 return cached_text.get();
2327 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2331 Cache::iterator it = cache.find(bv);
2333 if (it == cache.end()) {
2337 lyx::Assert(it->second.text.get());
2339 it->second.remove = true;
2341 /// then remove all LyXText in text-insets
2342 for_each(paragraphs.begin(), paragraphs.end(),
2343 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2348 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2351 // we cannot resize this because we are in use!
2352 // so do this on the next possible getLyXText()
2357 // lyxerr << "InsetText::resizeLyXText\n";
2358 if (!paragraphs.begin()->next() && paragraphs.begin()->empty()) { // no data, resize not neccessary!
2359 // we have to do this as a fixed width may have changed!
2360 LyXText * t = getLyXText(bv);
2361 saveLyXTextState(t);
2363 restoreLyXTextState(bv, t);
2366 // one endless line, resize normally not necessary
2367 if (!force && getMaxWidth(bv, this) < 0)
2370 Cache::iterator it = cache.find(bv);
2371 if (it == cache.end()) {
2374 lyx::Assert(it->second.text.get());
2376 LyXText * t = it->second.text.get();
2377 saveLyXTextState(t);
2379 for_each(paragraphs.begin(), paragraphs.end(),
2380 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2383 restoreLyXTextState(bv, t);
2384 if (the_locking_inset) {
2385 inset_x = cix(bv) - top_x + drawTextXOffset;
2386 inset_y = ciy(bv) + drawTextYOffset;
2389 t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2391 updateLocal(bv, FULL, false);
2392 // this will scroll the screen such that the cursor becomes visible
2393 bv->updateScrollbar();
2395 need_update |= FULL;
2400 void InsetText::reinitLyXText() const
2403 // we cannot resize this because we are in use!
2404 // so do this on the next possible getLyXText()
2410 // lyxerr << "InsetText::reinitLyXText\n";
2411 for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2412 lyx::Assert(it->second.text.get());
2414 LyXText * t = it->second.text.get();
2415 BufferView * bv = it->first;
2417 saveLyXTextState(t);
2419 for_each(paragraphs.begin(), paragraphs.end(),
2420 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2423 restoreLyXTextState(bv, t);
2424 if (the_locking_inset) {
2425 inset_x = cix(bv) - top_x + drawTextXOffset;
2426 inset_y = ciy(bv) + drawTextYOffset;
2428 t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2430 updateLocal(bv, FULL, false);
2431 // this will scroll the screen such that the cursor becomes visible
2432 bv->updateScrollbar();
2440 void InsetText::removeNewlines()
2442 bool changed = false;
2444 ParagraphList::iterator it = paragraphs.begin();
2445 ParagraphList::iterator end = paragraphs.end();
2446 for (; it != end; ++it) {
2447 for (int i = 0; i < it->size(); ++i) {
2448 if (it->getChar(i) == Paragraph::META_NEWLINE) {
2459 bool InsetText::nodraw() const
2461 if (the_locking_inset)
2462 return the_locking_inset->nodraw();
2463 return UpdatableInset::nodraw();
2467 int InsetText::scroll(bool recursive) const
2469 int sx = UpdatableInset::scroll(false);
2471 if (recursive && the_locking_inset)
2472 sx += the_locking_inset->scroll(recursive);
2478 bool InsetText::doClearArea() const
2480 return !locked || (need_update & (FULL|INIT));
2484 void InsetText::selectAll(BufferView * bv)
2486 getLyXText(bv)->cursorTop(bv);
2487 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2488 getLyXText(bv)->cursorBottom(bv);
2489 getLyXText(bv)->setSelection(bv);
2493 void InsetText::clearSelection(BufferView * bv)
2495 getLyXText(bv)->clearSelection();
2499 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2501 Painter & pain = bv->painter();
2503 int h = insetAscent + insetDescent;
2504 int ty = baseline - insetAscent;
2510 if ((ty + h) > pain.paperHeight())
2511 h = pain.paperHeight();
2512 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2513 w = pain.paperWidth();
2514 // w -= TEXT_TO_INSET_OFFSET;
2515 pain.fillRectangle(top_x + 1, ty + 1, w - 1, h - 1, backgroundColor());
2518 frame_is_visible = false;
2522 Paragraph * InsetText::firstParagraph() const
2525 if (the_locking_inset)
2526 if ((result = the_locking_inset->firstParagraph()))
2528 return &*(paragraphs.begin());
2532 Paragraph * InsetText::getFirstParagraph(int i) const
2534 return (i == 0) ? &*(paragraphs.begin()) : 0;
2538 LyXCursor const & InsetText::cursor(BufferView * bv) const
2540 if (the_locking_inset)
2541 return the_locking_inset->cursor(bv);
2542 return getLyXText(bv)->cursor;
2546 Paragraph * InsetText::paragraph() const
2548 return &*(paragraphs.begin());
2552 void InsetText::paragraph(Paragraph * p)
2554 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2555 // caller of this function has to take care of it. This IS important
2556 // as we could have to insert a paragraph before this one and just
2557 // link the actual to a new ones next and set it with this function
2560 // set ourself as owner for all the paragraphs inserted!
2561 for_each(paragraphs.begin(), paragraphs.end(),
2562 boost::bind(&Paragraph::setInsetOwner, _1, this));
2565 // redraw myself when asked for
2570 Inset * InsetText::getInsetFromID(int id_arg) const
2573 return const_cast<InsetText *>(this);
2575 ParagraphList::iterator pit = paragraphs.begin();
2576 ParagraphList::iterator pend = paragraphs.end();
2577 for (; pit != pend; ++pit) {
2578 InsetList::iterator it = pit->insetlist.begin();
2579 InsetList::iterator end = pit->insetlist.end();
2580 for (; it != end; ++it) {
2581 if (it.getInset()->id() == id_arg)
2582 return it.getInset();
2583 Inset * in = it.getInset()->getInsetFromID(id_arg);
2593 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2594 float & value) const
2600 lt = getLyXText(bv);
2603 if (the_locking_inset) {
2604 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2605 if (!word.word().empty()) {
2611 // we have to go on checking so move cursor to the next char
2612 lt->cursor.pos(lt->cursor.pos() + 1);
2614 word = lt->selectNextWordToSpellcheck(bv, value);
2615 if (word.word().empty())
2616 bv->unlockInset(const_cast<InsetText *>(this));
2625 void InsetText::selectSelectedWord(BufferView * bv)
2627 if (the_locking_inset) {
2628 the_locking_inset->selectSelectedWord(bv);
2631 getLyXText(bv)->selectSelectedWord(bv);
2632 updateLocal(bv, SELECTION, false);
2636 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2638 if (the_locking_inset) {
2639 the_locking_inset->toggleSelection(bv, kill_selection);
2643 lt = getLyXText(bv);
2647 int x = top_x + TEXT_TO_INSET_OFFSET;
2649 Row * row = lt->firstRow();
2650 int y_offset = top_baseline - row->ascent_of_text();
2652 while ((row != 0) && ((y+row->height()) <= 0)) {
2659 if (need_update & SELECTION)
2661 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2667 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2671 lt = getLyXText(bv);
2674 if (the_locking_inset) {
2675 if (the_locking_inset->nextChange(bv, length))
2677 lt->cursorRight(bv, true);
2679 lyxfind::SearchResult result =
2680 lyxfind::findNextChange(bv, lt, length);
2682 if (result == lyxfind::SR_FOUND) {
2683 LyXCursor cur = lt->cursor;
2684 bv->unlockInset(bv->theLockingInset());
2685 if (bv->lockInset(this))
2688 lt->setSelectionRange(bv, length);
2689 updateLocal(bv, SELECTION, false);
2693 return result != lyxfind::SR_NOT_FOUND;
2697 bool InsetText::searchForward(BufferView * bv, string const & str,
2702 lt = getLyXText(bv);
2705 if (the_locking_inset) {
2706 if (the_locking_inset->searchForward(bv, str, cs, mw))
2708 lt->cursorRight(bv, true);
2710 lyxfind::SearchResult result =
2711 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2713 if (result == lyxfind::SR_FOUND) {
2714 LyXCursor cur = lt->cursor;
2715 bv->unlockInset(bv->theLockingInset());
2716 if (bv->lockInset(this))
2719 lt->setSelectionRange(bv, str.length());
2720 updateLocal(bv, SELECTION, false);
2724 return (result != lyxfind::SR_NOT_FOUND);
2727 bool InsetText::searchBackward(BufferView * bv, string const & str,
2730 if (the_locking_inset) {
2731 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2736 lt = getLyXText(bv);
2740 Paragraph * p = &*(paragraphs.begin());
2743 lt->setCursor(bv, p, p->size());
2745 lyxfind::SearchResult result =
2746 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2748 if (result == lyxfind::SR_FOUND) {
2749 LyXCursor cur = lt->cursor;
2750 bv->unlockInset(bv->theLockingInset());
2751 if (bv->lockInset(this))
2754 lt->setSelectionRange(bv, str.length());
2755 updateLocal(bv, SELECTION, false);
2759 return (result != lyxfind::SR_NOT_FOUND);
2763 bool InsetText::checkInsertChar(LyXFont & font)
2766 return owner()->checkInsertChar(font);
2771 void InsetText::collapseParagraphs(BufferView * bv) const
2773 BufferParams const & bparams = bv->buffer()->params;
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(bparams, &*paragraphs.begin());
2800 void InsetText::getDrawFont(LyXFont & font) const
2804 owner()->getDrawFont(font);
2808 void InsetText::appendParagraphs(BufferParams const & bparams,
2812 Paragraph * tmpbuf = newpar;
2813 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2814 if (bparams.tracking_changes)
2815 buf->cleanChanges();
2817 while (tmpbuf->next()) {
2818 tmpbuf = tmpbuf->next();
2819 lastbuffer->next(new Paragraph(*tmpbuf, false));
2820 lastbuffer->next()->previous(lastbuffer);
2821 lastbuffer = lastbuffer->next();
2822 if (bparams.tracking_changes)
2823 lastbuffer->cleanChanges();
2825 lastbuffer = &*(paragraphs.begin());
2826 while (lastbuffer->next())
2827 lastbuffer = lastbuffer->next();
2828 if (!newpar->empty() && !lastbuffer->empty() &&
2829 !lastbuffer->isSeparator(lastbuffer->size() - 1))
2831 lastbuffer->insertChar(lastbuffer->size(), ' ');
2834 // make the buf exactly the same layout than our last paragraph
2835 buf->makeSameLayout(lastbuffer);
2838 lastbuffer->next(buf);
2839 buf->previous(lastbuffer);
2840 mergeParagraph(bparams, lastbuffer);
2846 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2848 Paragraph * par = getFirstParagraph(0);
2850 InsetList::iterator it = par->insetlist.begin();
2851 InsetList::iterator end = par->insetlist.end();
2852 for (; it != end; ++it) {
2853 it.getInset()->addPreview(loader);