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"
40 #include "frontends/Alert.h"
41 #include "frontends/Dialogs.h"
42 #include "frontends/font_metrics.h"
43 #include "frontends/LyXView.h"
44 #include "frontends/Painter.h"
45 #include "frontends/screen.h"
47 #include "support/textutils.h"
48 #include "support/LAssert.h"
49 #include "support/lstrings.h"
50 #include "support/lyxalgo.h" // lyx::count
52 #include <boost/bind.hpp>
70 using lyx::textclass_type;
72 extern unsigned char getCurrentTextClass(Buffer *);
73 extern bool math_insert_greek(BufferView *, char);
74 extern int greek_kb_flag;
77 // These functions should probably go into bufferview_funcs somehow (Jug)
79 void InsetText::saveLyXTextState(LyXText * t) const
81 // check if my paragraphs are still valid
82 ParagraphList::iterator it = paragraphs.begin();
83 ParagraphList::iterator end = paragraphs.end();
84 for (; it != end; ++it) {
85 if (&*it == t->cursor.par())
89 if (it != end && t->cursor.pos() <= it->size()) {
90 sstate.lpar = t->cursor.par();
91 sstate.pos = t->cursor.pos();
92 sstate.boundary = t->cursor.boundary();
93 sstate.selstartpar = t->selection.start.par();
94 sstate.selstartpos = t->selection.start.pos();
95 sstate.selstartboundary = t->selection.start.boundary();
96 sstate.selendpar = t->selection.end.par();
97 sstate.selendpos = t->selection.end.pos();
98 sstate.selendboundary = t->selection.end.boundary();
99 sstate.selection = t->selection.set();
100 sstate.mark_set = t->selection.mark();
101 sstate.refresh = t->refresh_row != 0;
107 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
110 t->selection.set(true);
111 /* at this point just to avoid the Delete-Empty-Paragraph
112 * Mechanism when setting the cursor */
113 t->selection.mark(sstate.mark_set);
114 if (sstate.selection) {
115 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
116 true, sstate.selstartboundary);
117 t->selection.cursor = t->cursor;
118 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
119 true, sstate.selendboundary);
121 t->setCursor(bv, sstate.lpar, sstate.pos);
123 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
124 t->selection.cursor = t->cursor;
125 t->selection.set(false);
127 if (sstate.refresh) {
133 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
140 InsetText::InsetText(BufferParams const & bp)
141 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
144 paragraphs.set(new Paragraph);
145 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
146 if (bp.tracking_changes)
147 paragraphs.begin()->trackChanges();
152 InsetText::InsetText(InsetText const & in, bool same_id)
153 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
160 InsetText & InsetText::operator=(InsetText const & it)
167 void InsetText::init(InsetText const * ins, bool same_id)
170 setParagraphData(&*(ins->paragraphs.begin()), same_id);
171 autoBreakRows = ins->autoBreakRows;
172 drawFrame_ = ins->drawFrame_;
173 frame_color = ins->frame_color;
177 for_each(paragraphs.begin(), paragraphs.end(),
178 boost::bind(&Paragraph::setInsetOwner, _1, this));
180 the_locking_inset = 0;
182 frame_color = LColor::insetframe;
183 autoBreakRows = false;
196 last_drawn_width = -1;
197 frame_is_visible = false;
200 in_insetAllowed = false;
204 InsetText::~InsetText()
210 void InsetText::clear(bool just_mark_erased)
212 if (just_mark_erased) {
213 ParagraphList::iterator it = paragraphs.begin();
214 ParagraphList::iterator end = paragraphs.end();
215 for (; it != end; ++it) {
222 // This is a gross hack...
223 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
226 paragraphs.set(new Paragraph);
227 paragraphs.begin()->setInsetOwner(this);
228 paragraphs.begin()->layout(old_layout);
235 Inset * InsetText::clone(Buffer const &, bool same_id) const
237 return new InsetText(*this, same_id);
241 void InsetText::write(Buffer const * buf, ostream & os) const
244 writeParagraphData(buf, os);
248 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
250 ParagraphList::iterator it = paragraphs.begin();
251 ParagraphList::iterator end = paragraphs.end();
252 Paragraph::depth_type dth = 0;
253 for (; it != end; ++it) {
254 it->write(buf, os, buf->params, dth);
259 void InsetText::read(Buffer const * buf, LyXLex & lex)
263 Paragraph * return_par = 0;
264 Paragraph::depth_type depth = 0;
265 LyXFont font(LyXFont::ALL_INHERIT);
269 if (buf->params.tracking_changes)
270 paragraphs.begin()->trackChanges();
274 token = lex.getString();
277 if (token == "\\end_inset") {
281 Paragraph * tmp = &*(paragraphs.begin());
283 if (const_cast<Buffer*>(buf)->
284 parseSingleLyXformat2Token(lex, tmp, return_par,
285 token, pos, depth, font)) {
286 // the_end read this should NEVER happen
287 lex.printError("\\the_end read in inset! Error in document!");
293 return_par = &*(paragraphs.begin());
294 paragraphs.set(return_par);
296 return_par->setInsetOwner(this);
297 return_par = return_par->next();
300 if (token != "\\end_inset") {
301 lex.printError("Missing \\end_inset at this point. "
308 int InsetText::ascent(BufferView * bv, LyXFont const &) const
310 insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
311 TEXT_TO_INSET_OFFSET;
316 int InsetText::descent(BufferView * bv, LyXFont const &) const
318 LyXText * llt = getLyXText(bv);
319 insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
320 TEXT_TO_INSET_OFFSET;
325 int InsetText::width(BufferView * bv, LyXFont const &) const
327 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
328 (2 * TEXT_TO_INSET_OFFSET);
329 insetWidth = max(insetWidth, 10);
334 int InsetText::textWidth(BufferView * bv, bool fordraw) const
337 if (!autoBreakRows) {
340 w = getMaxWidth(bv, this);
343 return max(w - (2 * TEXT_TO_INSET_OFFSET),
344 (int)getLyXText(bv)->width);
348 return w - (2 * TEXT_TO_INSET_OFFSET);
352 void InsetText::draw(BufferView * bv, LyXFont const & f,
353 int baseline, float & x, bool cleared) const
358 Painter & pain = bv->painter();
360 // this is the first thing we have to ask because if the x pos
361 // changed we have to do a complete rebreak of the text as we
362 // may have few space to draw in. Well we should check on this too
364 if (top_x != int(x)) {
367 int nw = getMaxWidth(bv, this);
368 if (nw > 0 && old_max_width != nw) {
371 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
376 // call these methods so that insetWidth, insetAscent and
377 // insetDescent have the right values.
382 // repaint the background if needed
383 if (cleared && backgroundColor() != LColor::background) {
384 clearInset(bv, baseline, cleared);
387 // no draw is necessary !!!
388 if ((drawFrame_ == LOCKED) && !locked && paragraphs.begin()->empty()) {
389 top_baseline = baseline;
391 if (need_update & CLEAR_FRAME)
392 clearFrame(pain, cleared);
398 x += static_cast<float>(scroll());
400 // if top_x differs we did it already
401 if (!cleared && (old_x == int(x))
402 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
403 ||(last_drawn_width != insetWidth)))
405 // Condition necessary to eliminate bug 59 attachment 37
407 clearInset(bv, baseline, cleared);
411 frame_is_visible = false;
413 if (!cleared && (need_update == NONE)) {
415 drawFrame(pain, cleared);
419 top_baseline = baseline;
420 top_y = baseline - insetAscent;
422 if (last_drawn_width != insetWidth) {
424 clearInset(bv, baseline, cleared);
426 last_drawn_width = insetWidth;
429 if (the_locking_inset && (cpar(bv) == inset_par)
430 && (cpos(bv) == inset_pos)) {
431 inset_x = cix(bv) - top_x + drawTextXOffset;
432 inset_y = ciy(bv) + drawTextYOffset;
434 if (!cleared && (need_update == CURSOR)
435 && !getLyXText(bv)->selection.set()) {
436 drawFrame(pain, cleared);
446 x += TEXT_TO_INSET_OFFSET;
448 Row * row = lt->firstRow();
449 int y_offset = baseline - row->ascent_of_text();
450 int ph = pain.paperHeight();
453 while ((row != 0) && ((y+row->height()) <= 0)) {
455 first += row->height();
459 lt->first_y = -y_offset;
466 if (cleared || (need_update&(INIT|FULL))) {
467 int yf = y_offset + first;
469 while ((row != 0) && (yf < ph)) {
470 lt->getVisibleRow(bv, y + y_offset + first, int(x),
471 row, y + lt->first_y, cleared);
472 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
473 lt->need_break_row = row;
475 lt->setCursor(bv, lt->cursor.par(),
477 if (lt->selection.set()) {
478 lt->setCursor(bv, lt->selection.start,
479 lt->selection.start.par(),
480 lt->selection.start.pos());
481 lt->setCursor(bv, lt->selection.end,
482 lt->selection.end.par(),
483 lt->selection.end.pos());
491 } else if (!locked) {
492 if (need_update & CURSOR) {
493 bv->screen().toggleSelection(lt, bv, true, y_offset,int(x));
494 lt->clearSelection();
495 lt->selection.cursor = lt->cursor;
497 bv->screen().update(lt, bv, y_offset, int(x));
500 if (need_update & SELECTION) {
501 bv->screen().toggleToggle(lt, bv, y_offset, int(x));
502 } else if (need_update & CURSOR) {
503 bv->screen().toggleSelection(lt, bv, true, y_offset,int(x));
504 lt->clearSelection();
505 lt->selection.cursor = lt->cursor;
507 bv->screen().update(lt, bv, y_offset, int(x));
512 lt->status(bv, LyXText::UNCHANGED);
513 if ((drawFrame_ == ALWAYS) ||
514 ((cleared || (need_update != CURSOR_PAR)) &&
515 (drawFrame_ == LOCKED) && locked))
517 drawFrame(pain, cleared);
518 } else if (need_update & CLEAR_FRAME) {
519 clearFrame(pain, cleared);
522 x += insetWidth - TEXT_TO_INSET_OFFSET;
524 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
526 } else if (need_update != INIT) {
534 void InsetText::drawFrame(Painter & pain, bool cleared) const
536 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
537 if (!frame_is_visible || cleared) {
538 frame_x = top_x + ttoD2;
539 frame_y = top_baseline - insetAscent + ttoD2;
540 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
541 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
542 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
544 frame_is_visible = true;
549 void InsetText::clearFrame(Painter & pain, bool cleared) const
551 if (frame_is_visible) {
553 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
556 frame_is_visible = false;
561 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
564 if (reinit && owner()) {
566 owner()->update(bv, font, true);
571 if (reinit || need_update == INIT) {
573 // we should put this call where we set need_update to INIT!
576 owner()->update(bv, font, true);
581 if (!autoBreakRows && paragraphs.begin()->next())
582 collapseParagraphs(bv);
584 if (the_locking_inset) {
585 inset_x = cix(bv) - top_x + drawTextXOffset;
586 inset_y = ciy(bv) + drawTextYOffset;
587 the_locking_inset->update(bv, font, reinit);
595 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
598 lt->updateInset(bv, the_locking_inset);
600 if (lt->status() == LyXText::NEED_MORE_REFRESH)
608 void InsetText::setUpdateStatus(BufferView * bv, int what) const
610 // this does nothing dangerous so use only a localized buffer
611 LyXText * llt = getLyXText(bv);
614 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
615 // if we don't break row so that we only have one row to update!
616 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
618 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
621 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
622 need_update |= CURSOR_PAR;
625 // this to not draw a selection when we redraw all of it!
626 if (need_update & CURSOR && !(need_update & SELECTION)) {
627 if (llt->selection.set())
629 llt->clearSelection();
634 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
636 if (!autoBreakRows && paragraphs.begin()->next())
637 collapseParagraphs(bv);
644 setUpdateStatus(bv, what);
645 bool flag = mark_dirty ||
646 (((need_update != CURSOR) && (need_update != NONE)) ||
647 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
648 if (!lt->selection.set())
649 lt->selection.cursor = lt->cursor;
653 // IMO this is not anymore needed as we do this in fitInsetCursor!
654 // and we always get "true" as returnvalue of this function in the
655 // case of a locking inset (Jug 20020412)
656 if (locked && (need_update & CURSOR) && bv->fitCursor())
662 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
664 if (need_update == CURSOR)
666 bv->owner()->view_state_changed();
667 bv->owner()->updateMenubar();
668 bv->owner()->updateToolbar();
669 if (old_par != cpar(bv)) {
670 bv->owner()->setLayout(cpar(bv)->layout()->name());
676 string const InsetText::editMessage() const
678 return _("Opened Text Inset");
682 void InsetText::edit(BufferView * bv, int x, int y, mouse_button::state button)
684 UpdatableInset::edit(bv, x, y, button);
686 if (!bv->lockInset(this)) {
687 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
691 the_locking_inset = 0;
692 inset_pos = inset_x = inset_y = 0;
693 inset_boundary = false;
696 int tmp_y = (y < 0) ? 0 : y;
702 // we put here -1 and not button as now the button in the
703 // edit call should not be needed we will fix this in 1.3.x
704 // cycle hopefully (Jug 20020509)
705 // FIXME: GUII I've changed this to none: probably WRONG
706 if (!checkAndActivateInset(bv, x, tmp_y, mouse_button::none)) {
707 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
709 lt->cursor.x_fix(lt->cursor.x());
711 lt->clearSelection();
713 // If the inset is empty set the language of the current font to the
714 // language to the surronding text (if different).
715 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
716 bv->getParentLanguage(this) != lt->current_font.language())
718 LyXFont font(LyXFont::ALL_IGNORE);
719 font.setLanguage(bv->getParentLanguage(this));
720 setFont(bv, font, false);
726 if (drawFrame_ == LOCKED)
727 code = CURSOR|DRAW_FRAME;
728 updateLocal(bv, code, false);
731 // Tell the paragraph dialog that we've entered an insettext.
732 bv->owner()->getDialogs().updateParagraph();
736 void InsetText::edit(BufferView * bv, bool front)
738 UpdatableInset::edit(bv, front);
740 if (!bv->lockInset(this)) {
741 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
745 the_locking_inset = 0;
746 inset_pos = inset_x = inset_y = 0;
747 inset_boundary = false;
756 lt->setCursor(bv, &*(paragraphs.begin()), 0);
758 ParagraphList::iterator it = paragraphs.begin();
759 ParagraphList::iterator end = paragraphs.end();
760 while (boost::next(it) != end)
762 // int const pos = (p->size() ? p->size()-1 : p->size());
763 lt->setCursor(bv, &*it, it->size());
765 lt->clearSelection();
767 // If the inset is empty set the language of the current font to the
768 // language to the surronding text (if different).
769 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
770 bv->getParentLanguage(this) != lt->current_font.language()) {
771 LyXFont font(LyXFont::ALL_IGNORE);
772 font.setLanguage(bv->getParentLanguage(this));
773 setFont(bv, font, false);
778 if (drawFrame_ == LOCKED)
779 code = CURSOR|DRAW_FRAME;
780 updateLocal(bv, code, false);
785 void InsetText::insetUnlock(BufferView * bv)
787 if (the_locking_inset) {
788 the_locking_inset->insetUnlock(bv);
789 the_locking_inset = 0;
790 updateLocal(bv, CURSOR_PAR, false);
797 if (drawFrame_ == LOCKED)
798 code = CURSOR|CLEAR_FRAME;
802 if (drawFrame_ == LOCKED)
810 if (lt->selection.set()) {
811 lt->clearSelection();
813 } else if (owner()) {
814 bv->owner()->setLayout(owner()->getLyXText(bv)
815 ->cursor.par()->layout()->name());
817 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
818 // hack for deleteEmptyParMech
819 if (!paragraphs.begin()->empty()) {
820 lt->setCursor(bv, &*(paragraphs.begin()), 0);
821 } else if (paragraphs.begin()->next()) {
822 lt->setCursor(bv, paragraphs.begin()->next(), 0);
827 updateLocal(bv, code, false);
830 setUpdateStatus(bv, code);
835 void InsetText::lockInset(BufferView * bv)
838 the_locking_inset = 0;
839 inset_pos = inset_x = inset_y = 0;
840 inset_boundary = false;
848 lt->setCursor(bv, &*(paragraphs.begin()), 0);
849 lt->clearSelection();
851 // If the inset is empty set the language of the current font to the
852 // language to the surronding text (if different).
853 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
854 bv->getParentLanguage(this) != lt->current_font.language()) {
855 LyXFont font(LyXFont::ALL_IGNORE);
856 font.setLanguage(bv->getParentLanguage(this));
857 setFont(bv, font, false);
862 if (drawFrame_ == LOCKED)
863 code = CURSOR|DRAW_FRAME;
864 setUpdateStatus(bv, code);
868 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
870 the_locking_inset = inset;
871 inset_x = cix(bv) - top_x + drawTextXOffset;
872 inset_y = ciy(bv) + drawTextYOffset;
873 inset_pos = cpos(bv);
874 inset_par = cpar(bv);
875 inset_boundary = cboundary(bv);
876 updateLocal(bv, CURSOR, false);
880 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
882 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
886 if (!the_locking_inset) {
887 Paragraph * p = &*(paragraphs.begin());
888 int const id = inset->id();
890 InsetList::iterator it =
891 p->insetlist.begin();
892 InsetList::iterator const end =
894 for (; it != end; ++it) {
895 if (it.getInset() == inset) {
896 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
897 lockInset(bv, inset);
900 if (it.getInset()->getInsetFromID(id)) {
901 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
902 it.getInset()->edit(bv);
903 return the_locking_inset->lockInsetInInset(bv, inset);
910 if (inset == cpar(bv)->getInset(cpos(bv))) {
911 lyxerr[Debug::INSETS] << "OK" << endl;
912 lockInset(bv, inset);
914 } else if (the_locking_inset && (the_locking_inset == inset)) {
915 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
916 lyxerr[Debug::INSETS] << "OK" << endl;
917 inset_x = cix(bv) - top_x + drawTextXOffset;
918 inset_y = ciy(bv) + drawTextYOffset;
920 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
922 } else if (the_locking_inset) {
923 lyxerr[Debug::INSETS] << "MAYBE" << endl;
924 return the_locking_inset->lockInsetInInset(bv, inset);
926 lyxerr[Debug::INSETS] << "NOT OK" << endl;
931 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
934 if (!the_locking_inset)
936 if (the_locking_inset == inset) {
937 the_locking_inset->insetUnlock(bv);
938 getLyXText(bv)->updateInset(bv, inset);
939 the_locking_inset = 0;
941 moveRightIntern(bv, true, false);
942 old_par = 0; // force layout setting
946 updateLocal(bv, CURSOR, false);
949 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
953 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
955 if (!autoBreakRows && paragraphs.begin()->next())
956 collapseParagraphs(bv);
964 if (inset->owner() != this) {
965 int ustat = CURSOR_PAR;
967 UpdatableInset * tl_inset = the_locking_inset;
969 found = tl_inset->updateInsetInInset(bv, inset);
971 tl_inset = static_cast<UpdatableInset *>(inset);
972 while(tl_inset->owner() && tl_inset->owner() != this)
973 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
974 if (!tl_inset->owner())
976 found = tl_inset->updateInsetInInset(bv, inset);
980 lt->updateInset(bv, tl_inset);
984 setUpdateStatus(bv, ustat);
987 bool found = lt->updateInset(bv, inset);
991 setUpdateStatus(bv, CURSOR_PAR);
992 if (the_locking_inset &&
993 cpar(bv) == inset_par && cpos(bv) == inset_pos)
995 inset_x = cix(bv) - top_x + drawTextXOffset;
996 inset_y = ciy(bv) + drawTextYOffset;
1003 void InsetText::lfunMousePress(FuncRequest const & cmd)
1005 no_selection = true;
1007 // use this to check mouse motion for selection!
1011 BufferView * bv = cmd.view();
1012 FuncRequest cmd1 = cmd;
1018 int tmp_x = cmd.x - drawTextXOffset;
1019 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->first_y;
1020 Inset * inset = getLyXText(bv)->checkInsetHit(bv, tmp_x, tmp_y);
1022 hideInsetCursor(bv);
1023 if (the_locking_inset) {
1024 if (the_locking_inset == inset) {
1025 the_locking_inset->localDispatch(cmd1);
1030 // otherwise unlock the_locking_inset and lock the new inset
1031 the_locking_inset->insetUnlock(bv);
1032 inset_x = cix(bv) - top_x + drawTextXOffset;
1033 inset_y = ciy(bv) + drawTextYOffset;
1034 the_locking_inset = 0;
1035 inset->localDispatch(cmd1);
1036 // inset->edit(bv, x - inset_x, y - inset_y, button);
1037 if (the_locking_inset)
1038 updateLocal(bv, CURSOR, false);
1042 // otherwise only unlock the_locking_inset
1043 the_locking_inset->insetUnlock(bv);
1044 the_locking_inset = 0;
1047 no_selection = false;
1049 if (bv->theLockingInset()) {
1050 if (isHighlyEditableInset(inset)) {
1051 // We just have to lock the inset before calling a
1052 // PressEvent on it!
1053 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
1054 if (!bv->lockInset(uinset)) {
1055 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1057 inset->localDispatch(cmd1);
1058 if (the_locking_inset)
1059 updateLocal(bv, CURSOR, false);
1063 if (!inset) { // && (button == mouse_button::button2)) {
1064 bool paste_internally = false;
1065 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
1066 localDispatch(FuncRequest(bv, LFUN_COPY));
1067 paste_internally = true;
1071 lt = getLyXText(bv);
1074 int old_first_y = lt->first_y;
1076 lt->setCursorFromCoordinates(bv, cmd.x - drawTextXOffset,
1077 cmd.y + insetAscent);
1078 // set the selection cursor!
1079 lt->selection.cursor = lt->cursor;
1080 lt->cursor.x_fix(lt->cursor.x());
1082 if (lt->selection.set()) {
1083 lt->clearSelection();
1086 updateLocal(bv, FULL, false);
1088 lt->clearSelection();
1091 updateLocal(bv, CURSOR, false);
1094 bv->owner()->setLayout(cpar(bv)->layout()->name());
1096 // we moved the view we cannot do mouse selection in this case!
1097 if (getLyXText(bv)->first_y != old_first_y)
1098 no_selection = true;
1100 // Insert primary selection with middle mouse
1101 // if there is a local selection in the current buffer,
1103 if (cmd.button() == mouse_button::button2) {
1104 if (paste_internally)
1105 localDispatch(FuncRequest(bv, LFUN_PASTE));
1107 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
1110 getLyXText(bv)->clearSelection();
1112 showInsetCursor(bv);
1116 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
1118 BufferView * bv = cmd.view();
1119 FuncRequest cmd1 = cmd;
1123 no_selection = true;
1124 if (the_locking_inset)
1125 return the_locking_inset->localDispatch(cmd1);
1127 int tmp_x = cmd.x - drawTextXOffset;
1128 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->first_y;
1129 Inset * inset = getLyXText(bv)->checkInsetHit(bv, tmp_x, tmp_y);
1132 if (isHighlyEditableInset(inset))
1133 ret = inset->localDispatch(cmd1);
1135 inset_x = cix(bv) - top_x + drawTextXOffset;
1136 inset_y = ciy(bv) + drawTextYOffset;
1137 cmd1.x = cmd.x - inset_x;
1138 cmd1.y = cmd.x - inset_y;
1139 // note that we should do ret = inset->localDispatch(cmd1)
1140 // and fix this instead (Alfredo);
1142 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
1144 updateLocal(bv, CURSOR_PAR, false);
1150 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
1152 FuncRequest cmd1 = cmd;
1156 if (the_locking_inset) {
1157 the_locking_inset->localDispatch(cmd1);
1161 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
1164 BufferView * bv = cmd.view();
1167 lt = getLyXText(bv);
1170 hideInsetCursor(bv);
1171 LyXCursor cur = lt->cursor;
1172 lt->setCursorFromCoordinates
1173 (bv, cmd.x - drawTextXOffset, cmd.y + insetAscent);
1174 lt->cursor.x_fix(lt->cursor.x());
1175 if (cur == lt->cursor) {
1180 lt->setSelection(bv);
1181 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1182 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1186 updateLocal(bv, SELECTION, false);
1188 showInsetCursor(bv);
1192 Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
1194 BufferView * bv = ev.view();
1195 switch (ev.action) {
1196 case LFUN_MOUSE_PRESS:
1199 case LFUN_MOUSE_MOTION:
1200 lfunMouseMotion(ev);
1202 case LFUN_MOUSE_RELEASE:
1203 return lfunMouseRelease(ev) ? DISPATCHED : UNDISPATCHED;
1208 bool was_empty = (paragraphs.begin()->empty() && !paragraphs.begin()->next());
1209 no_selection = false;
1210 RESULT result = UpdatableInset::localDispatch(ev);
1211 if (result != UNDISPATCHED)
1214 result = DISPATCHED;
1215 if (ev.action < 0 && ev.argument.empty())
1218 if (the_locking_inset) {
1219 result = the_locking_inset->localDispatch(ev);
1220 if (result == DISPATCHED_NOUPDATE)
1222 else if (result == DISPATCHED) {
1223 updateLocal(bv, CURSOR_PAR, false);
1225 } else if (result >= FINISHED) {
1227 case FINISHED_RIGHT:
1228 moveRightIntern(bv, false, false);
1229 result = DISPATCHED;
1232 if ((result = moveUp(bv)) >= FINISHED) {
1233 updateLocal(bv, CURSOR, false);
1234 bv->unlockInset(this);
1238 if ((result = moveDown(bv)) >= FINISHED) {
1239 updateLocal(bv, CURSOR, false);
1240 bv->unlockInset(this);
1244 result = DISPATCHED;
1247 the_locking_inset = 0;
1248 updateLocal(bv, CURSOR, false);
1252 hideInsetCursor(bv);
1255 lt = getLyXText(bv);
1259 int updflag = false;
1260 switch (ev.action) {
1263 case LFUN_SELFINSERT:
1264 if (bv->buffer()->isReadonly()) {
1265 // setErrorMessage(N_("Document is read only"));
1268 if (!ev.argument.empty()) {
1269 /* Automatically delete the currently selected
1270 * text and replace it with what is being
1271 * typed in now. Depends on lyxrc settings
1272 * "auto_region_delete", which defaults to
1275 // This should not be needed here and is also WRONG!
1276 setUndo(bv, Undo::INSERT,
1277 lt->cursor.par(), lt->cursor.par()->next());
1280 if (lyxrc.auto_region_delete) {
1281 if (lt->selection.set()) {
1282 lt->cutSelection(bv, false, false);
1285 lt->clearSelection();
1286 for (string::size_type i = 0; i < ev.argument.length(); ++i) {
1287 bv->owner()->getIntl().getTransManager().
1288 TranslateAndInsert(ev.argument[i], lt);
1291 lt->selection.cursor = lt->cursor;
1292 updwhat = CURSOR | CURSOR_PAR;
1294 result = DISPATCHED_NOUPDATE;
1296 // --- Cursor Movements -----------------------------------
1299 moveRight(bv, false, true);
1300 lt->setSelection(bv);
1301 updwhat = SELECTION;
1304 result = moveRight(bv);
1310 moveLeft(bv, false, true);
1311 lt->setSelection(bv);
1312 updwhat = SELECTION;
1316 result = moveLeft(bv);
1322 lt->setSelection(bv);
1323 updwhat = SELECTION;
1327 result = moveDown(bv);
1333 lt->setSelection(bv);
1334 updwhat = SELECTION;
1338 result = moveUp(bv);
1351 if (!crow(bv)->previous())
1352 result = FINISHED_UP;
1354 lt->cursorPrevious(bv);
1355 result = DISPATCHED_NOUPDATE;
1360 if (!crow(bv)->next())
1361 result = FINISHED_DOWN;
1364 result = DISPATCHED_NOUPDATE;
1368 case LFUN_BACKSPACE: {
1369 if (lt->selection.set())
1370 lt->cutSelection(bv, true, false);
1373 updwhat = CURSOR_PAR;
1379 if (lt->selection.set()) {
1380 lt->cutSelection(bv, true, false);
1384 updwhat = CURSOR_PAR;
1390 lt->cutSelection(bv);
1391 updwhat = CURSOR_PAR;
1398 lt->copySelection(bv);
1399 updwhat = CURSOR_PAR;
1401 case LFUN_PASTESELECTION:
1403 string const clip(bv->getClipboard());
1407 if (ev.argument == "paragraph") {
1408 lt->insertStringAsParagraphs(bv, clip);
1410 lt->insertStringAsLines(bv, clip);
1413 lt->clearSelection();
1415 updwhat = CURSOR_PAR;
1420 if (!autoBreakRows) {
1422 if (CutAndPaste::nrOfParagraphs() > 1) {
1423 Alert::alert(_("Impossible operation!"),
1424 _("Cannot include more than one paragraph!"),
1430 // This should not be needed here and is also WRONG!
1431 setUndo(bv, Undo::INSERT,
1432 lt->cursor.par(), lt->cursor.par()->next());
1434 lt->pasteSelection(bv);
1436 lt->clearSelection();
1437 updwhat = CURSOR_PAR;
1442 case LFUN_BREAKPARAGRAPH:
1443 if (!autoBreakRows) {
1444 result = DISPATCHED;
1447 lt->breakParagraph(bv, 0);
1448 updwhat = CURSOR | FULL;
1451 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1452 if (!autoBreakRows) {
1453 result = DISPATCHED;
1456 lt->breakParagraph(bv, 1);
1457 updwhat = CURSOR | FULL;
1461 case LFUN_BREAKLINE: {
1462 if (!autoBreakRows) {
1463 result = DISPATCHED;
1467 // This should not be needed here and is also WRONG!
1468 setUndo(bv, Undo::INSERT,
1469 lt->cursor.par(), lt->cursor.par()->next());
1471 lt->insertChar(bv, Paragraph::META_NEWLINE);
1472 updwhat = CURSOR | CURSOR_PAR;
1478 // do not set layouts on non breakable textinsets
1479 if (autoBreakRows) {
1480 string cur_layout = cpar(bv)->layout()->name();
1482 // Derive layout number from given argument (string)
1483 // and current buffer's textclass (number). */
1484 LyXTextClass const & tclass =
1485 bv->buffer()->params.getLyXTextClass();
1486 string layout = ev.argument;
1487 bool hasLayout = tclass.hasLayout(layout);
1489 // If the entry is obsolete, use the new one instead.
1491 string const & obs =
1492 tclass[layout]->obsoleted_by();
1497 // see if we found the layout number:
1499 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + ev.argument + N_(" not known"));
1500 bv->owner()->dispatch(lf);
1504 if (cur_layout != layout) {
1505 cur_layout = layout;
1506 lt->setLayout(bv, layout);
1507 bv->owner()->setLayout(cpar(bv)->layout()->name());
1508 updwhat = CURSOR_PAR;
1512 // reset the layout box
1513 bv->owner()->setLayout(cpar(bv)->layout()->name());
1516 case LFUN_PARAGRAPH_SPACING:
1517 // This one is absolutely not working. When fiddling with this
1518 // it also seems to me that the paragraphs inside the insettext
1519 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1521 Paragraph * par = lt->cursor.par();
1522 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1523 float cur_value = 1.0;
1524 if (cur_spacing == Spacing::Other) {
1525 cur_value = par->params().spacing().getValue();
1528 istringstream istr(ev.argument.c_str());
1531 Spacing::Space new_spacing = cur_spacing;
1532 float new_value = cur_value;
1534 lyxerr << "Missing argument to `paragraph-spacing'"
1536 } else if (tmp == "single") {
1537 new_spacing = Spacing::Single;
1538 } else if (tmp == "onehalf") {
1539 new_spacing = Spacing::Onehalf;
1540 } else if (tmp == "double") {
1541 new_spacing = Spacing::Double;
1542 } else if (tmp == "other") {
1543 new_spacing = Spacing::Other;
1546 lyxerr << "new_value = " << tmpval << endl;
1549 } else if (tmp == "default") {
1550 new_spacing = Spacing::Default;
1552 lyxerr << _("Unknown spacing argument: ")
1553 << ev.argument << endl;
1555 if (cur_spacing != new_spacing || cur_value != new_value) {
1556 par->params().spacing(Spacing(new_spacing, new_value));
1557 updwhat = CURSOR_PAR;
1564 if (!bv->dispatch(ev))
1565 result = UNDISPATCHED;
1572 updateLocal(bv, updwhat, updflag);
1573 /// If the action has deleted all text in the inset, we need to change the
1574 // language to the language of the surronding text.
1575 if (!was_empty && paragraphs.begin()->empty() && !paragraphs.begin()->next()) {
1576 LyXFont font(LyXFont::ALL_IGNORE);
1577 font.setLanguage(bv->getParentLanguage(this));
1578 setFont(bv, font, false);
1581 if (result >= FINISHED)
1582 bv->unlockInset(this);
1584 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1585 result = DISPATCHED;
1590 int InsetText::latex(Buffer const * buf, ostream & os,
1591 bool moving_arg, bool) const
1594 buf->latexParagraphs(os, &*(paragraphs.begin()), 0,
1595 texrow, moving_arg);
1596 return texrow.rows();
1600 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1602 unsigned int lines = 0;
1604 ParagraphList::iterator beg = paragraphs.begin();
1605 ParagraphList::iterator end = paragraphs.end();
1606 ParagraphList::iterator it = beg;
1607 for (; it != end; ++it) {
1608 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1609 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1616 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1618 Paragraph * p = &*(paragraphs.begin());
1619 unsigned int lines = 0;
1621 vector<string> environment_stack(10);
1622 vector<string> environment_inner(10);
1624 int const command_depth = 0;
1627 Paragraph::depth_type depth = 0; // paragraph depth
1631 int desc_on = 0; // description mode
1633 LyXLayout_ptr const & style = p->layout();
1635 // environment tag closing
1636 for (; depth > p->params().depth(); --depth) {
1637 if (environment_inner[depth] != "!-- --") {
1638 item_name = "listitem";
1639 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1640 if (environment_inner[depth] == "varlistentry")
1641 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1643 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1644 environment_stack[depth].erase();
1645 environment_inner[depth].erase();
1648 if (depth == p->params().depth()
1649 && environment_stack[depth] != style->latexname()
1650 && !environment_stack[depth].empty()) {
1651 if (environment_inner[depth] != "!-- --") {
1652 item_name= "listitem";
1653 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1654 if (environment_inner[depth] == "varlistentry")
1655 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1658 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1660 environment_stack[depth].erase();
1661 environment_inner[depth].erase();
1664 // Write opening SGML tags.
1665 switch (style->latextype) {
1666 case LATEX_PARAGRAPH:
1667 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1671 buf->sgmlError(p, 0, _("Error: LatexType Command not allowed here.\n"));
1675 case LATEX_ENVIRONMENT:
1676 case LATEX_ITEM_ENVIRONMENT:
1677 if (depth < p->params().depth()) {
1678 depth = p->params().depth();
1679 environment_stack[depth].erase();
1682 if (environment_stack[depth] != style->latexname()) {
1683 if (environment_stack.size() == depth + 1) {
1684 environment_stack.push_back("!-- --");
1685 environment_inner.push_back("!-- --");
1687 environment_stack[depth] = style->latexname();
1688 environment_inner[depth] = "!-- --";
1689 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1691 if (environment_inner[depth] != "!-- --") {
1692 item_name= "listitem";
1693 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1694 if (environment_inner[depth] == "varlistentry")
1695 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1699 if (style->latextype == LATEX_ENVIRONMENT) {
1700 if (!style->latexparam().empty()) {
1701 if (style->latexparam() == "CDATA")
1704 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1709 desc_on = (style->labeltype == LABEL_MANUAL);
1711 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1712 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1714 item_name = desc_on ? "term" : "para";
1715 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1719 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1723 buf->simpleDocBookOnePar(os, p, desc_on, depth + 1 + command_depth);
1727 // write closing SGML tags
1728 switch (style->latextype) {
1729 case LATEX_ENVIRONMENT:
1730 if (!style->latexparam().empty()) {
1731 if (style->latexparam() == "CDATA")
1734 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1737 case LATEX_ITEM_ENVIRONMENT:
1738 if (desc_on == 1) break;
1740 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1742 case LATEX_PARAGRAPH:
1743 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1746 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1752 for (int d = depth; d >= 0; --d) {
1753 if (!environment_stack[depth].empty()) {
1754 if (environment_inner[depth] != "!-- --") {
1755 item_name = "listitem";
1756 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1757 if (environment_inner[depth] == "varlistentry")
1758 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1761 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1769 void InsetText::validate(LaTeXFeatures & features) const
1771 for_each(paragraphs.begin(), paragraphs.end(),
1772 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1776 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1778 if (the_locking_inset) {
1779 the_locking_inset->getCursorPos(bv, x, y);
1782 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1783 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1787 int InsetText::insetInInsetY() const
1789 if (!the_locking_inset)
1792 return (inset_y + the_locking_inset->insetInInsetY());
1796 void InsetText::toggleInsetCursor(BufferView * bv)
1798 if (the_locking_inset) {
1799 the_locking_inset->toggleInsetCursor(bv);
1803 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1805 int const asc = font_metrics::maxAscent(font);
1806 int const desc = font_metrics::maxDescent(font);
1808 if (isCursorVisible())
1809 bv->hideLockedInsetCursor();
1811 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1812 toggleCursorVisible();
1816 void InsetText::showInsetCursor(BufferView * bv, bool show)
1818 if (the_locking_inset) {
1819 the_locking_inset->showInsetCursor(bv, show);
1822 if (!isCursorVisible()) {
1823 LyXFont const font =
1824 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1826 int const asc = font_metrics::maxAscent(font);
1827 int const desc = font_metrics::maxDescent(font);
1829 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1831 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1832 setCursorVisible(true);
1837 void InsetText::hideInsetCursor(BufferView * bv)
1839 if (isCursorVisible()) {
1840 bv->hideLockedInsetCursor();
1841 setCursorVisible(false);
1843 if (the_locking_inset)
1844 the_locking_inset->hideInsetCursor(bv);
1848 void InsetText::fitInsetCursor(BufferView * bv) const
1850 if (the_locking_inset) {
1851 the_locking_inset->fitInsetCursor(bv);
1854 LyXFont const font =
1855 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1857 int const asc = font_metrics::maxAscent(font);
1858 int const desc = font_metrics::maxDescent(font);
1860 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1861 need_update |= FULL;
1866 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1868 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1869 return moveLeftIntern(bv, false, activate_inset, selecting);
1871 return moveRightIntern(bv, true, activate_inset, selecting);
1876 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1878 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1879 return moveRightIntern(bv, true, activate_inset, selecting);
1881 return moveLeftIntern(bv, false, activate_inset, selecting);
1886 InsetText::moveRightIntern(BufferView * bv, bool front,
1887 bool activate_inset, bool selecting)
1889 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1890 return FINISHED_RIGHT;
1891 if (activate_inset && checkAndActivateInset(bv, front))
1893 getLyXText(bv)->cursorRight(bv);
1895 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1896 return DISPATCHED_NOUPDATE;
1901 InsetText::moveLeftIntern(BufferView * bv, bool front,
1902 bool activate_inset, bool selecting)
1904 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1906 getLyXText(bv)->cursorLeft(bv);
1908 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1909 if (activate_inset && checkAndActivateInset(bv, front))
1911 return DISPATCHED_NOUPDATE;
1915 Inset::RESULT InsetText::moveUp(BufferView * bv)
1917 if (!crow(bv)->previous())
1919 getLyXText(bv)->cursorUp(bv);
1920 return DISPATCHED_NOUPDATE;
1924 Inset::RESULT InsetText::moveDown(BufferView * bv)
1926 if (!crow(bv)->next())
1927 return FINISHED_DOWN;
1928 getLyXText(bv)->cursorDown(bv);
1929 return DISPATCHED_NOUPDATE;
1933 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1935 if (the_locking_inset) {
1936 if (the_locking_inset->insetAllowed(inset))
1937 return the_locking_inset->insertInset(bv, inset);
1940 inset->setOwner(this);
1941 hideInsetCursor(bv);
1942 getLyXText(bv)->insertInset(bv, inset);
1944 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1949 bool InsetText::insetAllowed(Inset::Code code) const
1951 // in_insetAllowed is a really gross hack,
1952 // to allow us to call the owner's insetAllowed
1953 // without stack overflow, which can happen
1954 // when the owner uses InsetCollapsable::insetAllowed()
1956 if (in_insetAllowed)
1958 in_insetAllowed = true;
1959 if (the_locking_inset)
1960 ret = the_locking_inset->insetAllowed(code);
1962 ret = owner()->insetAllowed(code);
1963 in_insetAllowed = false;
1968 UpdatableInset * InsetText::getLockingInset() const
1970 return the_locking_inset ? the_locking_inset->getLockingInset() :
1971 const_cast<InsetText *>(this);
1975 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1979 if (the_locking_inset)
1980 return the_locking_inset->getFirstLockingInsetOfType(c);
1985 bool InsetText::showInsetDialog(BufferView * bv) const
1987 if (the_locking_inset)
1988 return the_locking_inset->showInsetDialog(bv);
1993 vector<string> const InsetText::getLabelList() const
1995 vector<string> label_list;
1997 ParagraphList::iterator pit = paragraphs.begin();
1998 ParagraphList::iterator pend = paragraphs.end();
1999 for (; pit != pend; ++pit) {
2000 InsetList::iterator beg = pit->insetlist.begin();
2001 InsetList::iterator end = pit->insetlist.end();
2002 for (; beg != end; ++beg) {
2003 vector<string> const l = beg.getInset()->getLabelList();
2004 label_list.insert(label_list.end(), l.begin(), l.end());
2011 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
2014 if (the_locking_inset) {
2015 the_locking_inset->setFont(bv, font, toggleall, selectall);
2018 if ((!paragraphs.begin()->next() && paragraphs.begin()->empty()) || cpar(bv)->empty()) {
2019 getLyXText(bv)->setFont(bv, font, toggleall);
2024 lt = getLyXText(bv);
2027 if (lt->selection.set()) {
2028 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
2032 lt->toggleFree(bv, font, toggleall);
2034 lt->clearSelection();
2036 bool flag = (selectall || lt->selection.set());
2040 updateLocal(bv, FULL, true);
2042 updateLocal(bv, CURSOR_PAR, true);
2046 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
2048 if (cpar(bv)->isInset(cpos(bv))) {
2050 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
2051 if (!isHighlyEditableInset(inset))
2053 inset->edit(bv, front);
2054 if (!the_locking_inset)
2056 updateLocal(bv, CURSOR, false);
2063 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2064 mouse_button::state button)
2066 x -= drawTextXOffset;
2068 int dummyy = y + insetAscent;
2069 Inset * inset = getLyXText(bv)->checkInsetHit(bv, dummyx, dummyy);
2070 // we only do the edit() call if the inset was hit by the mouse
2071 // or if it is a highly editable inset. So we should call this
2072 // function from our own edit with button < 0.
2073 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
2075 if (button == mouse_button::none && !isHighlyEditableInset(inset))
2083 inset_x = cix(bv) - top_x + drawTextXOffset;
2084 inset_y = ciy(bv) + drawTextYOffset;
2085 inset->edit(bv, x - inset_x, y - inset_y, button);
2086 if (!the_locking_inset)
2088 updateLocal(bv, CURSOR, false);
2095 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2098 int w = UpdatableInset::getMaxWidth(bv, inset);
2103 w = w - top_x + owner()->x();
2106 w -= (2 * TEXT_TO_INSET_OFFSET);
2109 return UpdatableInset::getMaxWidth(bv, inset);
2114 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2116 // we have to unlock any locked inset otherwise we're in troubles
2117 the_locking_inset = 0;
2120 paragraphs.set(new Paragraph(*p, same_id));
2121 paragraphs.begin()->setInsetOwner(this);
2122 Paragraph * np = &*(paragraphs.begin());
2125 np->next(new Paragraph(*p, same_id));
2126 np->next()->previous(np);
2128 np->setInsetOwner(this);
2135 void InsetText::markNew(bool track_changes)
2137 ParagraphList::iterator pit = paragraphs.begin();
2138 ParagraphList::iterator pend = paragraphs.end();
2139 for (; pit != pend; ++pit) {
2140 if (track_changes) {
2141 pit->trackChanges();
2143 // no-op when not tracking
2144 pit->cleanChanges();
2150 void InsetText::setText(string const & data, LyXFont const & font)
2153 for (unsigned int i = 0; i < data.length(); ++i)
2154 paragraphs.begin()->insertChar(i, data[i], font);
2159 void InsetText::setAutoBreakRows(bool flag)
2161 if (flag != autoBreakRows) {
2162 autoBreakRows = flag;
2170 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2172 if (how != drawFrame_) {
2175 updateLocal(bv, DRAW_FRAME, false);
2180 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2182 if (frame_color != col) {
2185 updateLocal(bv, DRAW_FRAME, false);
2190 int InsetText::cx(BufferView * bv) const
2192 // we do nothing dangerous so we use a local cache
2193 LyXText * llt = getLyXText(bv);
2194 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2195 if (the_locking_inset) {
2196 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2198 if (font.isVisibleRightToLeft())
2199 x -= the_locking_inset->width(bv, font);
2205 int InsetText::cix(BufferView * bv) const
2207 // we do nothing dangerous so we use a local cache
2208 LyXText * llt = getLyXText(bv);
2209 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2210 if (the_locking_inset) {
2211 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2213 if (font.isVisibleRightToLeft())
2214 x -= the_locking_inset->width(bv, font);
2220 int InsetText::cy(BufferView * bv) const
2223 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2227 int InsetText::ciy(BufferView * bv) const
2230 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2234 pos_type InsetText::cpos(BufferView * bv) const
2236 return getLyXText(bv)->cursor.pos();
2240 Paragraph * InsetText::cpar(BufferView * bv) const
2242 return getLyXText(bv)->cursor.par();
2246 bool InsetText::cboundary(BufferView * bv) const
2248 return getLyXText(bv)->cursor.boundary();
2252 Row * InsetText::crow(BufferView * bv) const
2254 return getLyXText(bv)->cursor.row();
2258 LyXText * InsetText::getLyXText(BufferView const * lbv,
2259 bool const recursive) const
2261 if (cached_bview == lbv) {
2262 if (recursive && the_locking_inset)
2263 return the_locking_inset->getLyXText(lbv, true);
2264 LyXText * lt = cached_text.get();
2265 lyx::Assert(lt && lt->firstRow()->par() == &*(paragraphs.begin()));
2268 // Super UGLY! (Lgb)
2269 BufferView * bv = const_cast<BufferView *>(lbv);
2272 Cache::iterator it = cache.find(bv);
2274 if (it != cache.end()) {
2277 } else if (do_resize) {
2278 resizeLyXText(do_resize);
2280 if (lt || !it->second.remove) {
2281 lyx::Assert(it->second.text.get());
2282 cached_text = it->second.text;
2283 if (recursive && the_locking_inset) {
2284 return the_locking_inset->getLyXText(bv, true);
2286 return cached_text.get();
2287 } else if (it->second.remove) {
2289 saveLyXTextState(it->second.text.get());
2295 // when we have to reinit the existing LyXText!
2297 it->second.text->init(bv);
2298 restoreLyXTextState(bv, it->second.text.get());
2299 it->second.remove = false;
2301 cached_text = it->second.text;
2302 if (the_locking_inset && recursive) {
2303 return the_locking_inset->getLyXText(bv);
2305 return cached_text.get();
2308 // we are here only if we don't have a BufferView * in the cache!!!
2310 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2311 cached_text->init(bv);
2312 restoreLyXTextState(bv, cached_text.get());
2314 cache.insert(make_pair(bv, cached_text));
2316 if (the_locking_inset && recursive) {
2317 return the_locking_inset->getLyXText(bv);
2319 return cached_text.get();
2323 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2327 Cache::iterator it = cache.find(bv);
2329 if (it == cache.end()) {
2333 lyx::Assert(it->second.text.get());
2335 it->second.remove = true;
2337 /// then remove all LyXText in text-insets
2338 for_each(paragraphs.begin(), paragraphs.end(),
2339 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2344 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2347 // we cannot resize this because we are in use!
2348 // so do this on the next possible getLyXText()
2353 // lyxerr << "InsetText::resizeLyXText\n";
2354 if (!paragraphs.begin()->next() && paragraphs.begin()->empty()) { // no data, resize not neccessary!
2355 // we have to do this as a fixed width may have changed!
2356 LyXText * t = getLyXText(bv);
2357 saveLyXTextState(t);
2359 restoreLyXTextState(bv, t);
2362 // one endless line, resize normally not necessary
2363 if (!force && getMaxWidth(bv, this) < 0)
2366 Cache::iterator it = cache.find(bv);
2367 if (it == cache.end()) {
2370 lyx::Assert(it->second.text.get());
2372 LyXText * t = it->second.text.get();
2373 saveLyXTextState(t);
2375 for_each(paragraphs.begin(), paragraphs.end(),
2376 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2379 restoreLyXTextState(bv, t);
2380 if (the_locking_inset) {
2381 inset_x = cix(bv) - top_x + drawTextXOffset;
2382 inset_y = ciy(bv) + drawTextYOffset;
2385 t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2387 updateLocal(bv, FULL, false);
2388 // this will scroll the screen such that the cursor becomes visible
2389 bv->updateScrollbar();
2391 need_update |= FULL;
2396 void InsetText::reinitLyXText() const
2399 // we cannot resize this because we are in use!
2400 // so do this on the next possible getLyXText()
2406 // lyxerr << "InsetText::reinitLyXText\n";
2407 for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2408 lyx::Assert(it->second.text.get());
2410 LyXText * t = it->second.text.get();
2411 BufferView * bv = it->first;
2413 saveLyXTextState(t);
2415 for_each(paragraphs.begin(), paragraphs.end(),
2416 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2419 restoreLyXTextState(bv, t);
2420 if (the_locking_inset) {
2421 inset_x = cix(bv) - top_x + drawTextXOffset;
2422 inset_y = ciy(bv) + drawTextYOffset;
2424 t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2426 updateLocal(bv, FULL, false);
2427 // this will scroll the screen such that the cursor becomes visible
2428 bv->updateScrollbar();
2436 void InsetText::removeNewlines()
2438 bool changed = false;
2440 ParagraphList::iterator it = paragraphs.begin();
2441 ParagraphList::iterator end = paragraphs.end();
2442 for (; it != end; ++it) {
2443 for (int i = 0; i < it->size(); ++i) {
2444 if (it->getChar(i) == Paragraph::META_NEWLINE) {
2455 bool InsetText::nodraw() const
2457 if (the_locking_inset)
2458 return the_locking_inset->nodraw();
2459 return UpdatableInset::nodraw();
2463 int InsetText::scroll(bool recursive) const
2465 int sx = UpdatableInset::scroll(false);
2467 if (recursive && the_locking_inset)
2468 sx += the_locking_inset->scroll(recursive);
2474 bool InsetText::doClearArea() const
2476 return !locked || (need_update & (FULL|INIT));
2480 void InsetText::selectAll(BufferView * bv)
2482 getLyXText(bv)->cursorTop(bv);
2483 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2484 getLyXText(bv)->cursorBottom(bv);
2485 getLyXText(bv)->setSelection(bv);
2489 void InsetText::clearSelection(BufferView * bv)
2491 getLyXText(bv)->clearSelection();
2495 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2497 Painter & pain = bv->painter();
2499 int h = insetAscent + insetDescent;
2500 int ty = baseline - insetAscent;
2506 if ((ty + h) > pain.paperHeight())
2507 h = pain.paperHeight();
2508 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2509 w = pain.paperWidth();
2510 // w -= TEXT_TO_INSET_OFFSET;
2511 pain.fillRectangle(top_x + 1, ty + 1, w - 1, h - 1, backgroundColor());
2514 frame_is_visible = false;
2518 Paragraph * InsetText::firstParagraph() const
2521 if (the_locking_inset)
2522 if ((result = the_locking_inset->firstParagraph()))
2524 return &*(paragraphs.begin());
2528 Paragraph * InsetText::getFirstParagraph(int i) const
2530 return (i == 0) ? &*(paragraphs.begin()) : 0;
2534 LyXCursor const & InsetText::cursor(BufferView * bv) const
2536 if (the_locking_inset)
2537 return the_locking_inset->cursor(bv);
2538 return getLyXText(bv)->cursor;
2542 Paragraph * InsetText::paragraph() const
2544 return &*(paragraphs.begin());
2548 void InsetText::paragraph(Paragraph * p)
2550 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2551 // caller of this function has to take care of it. This IS important
2552 // as we could have to insert a paragraph before this one and just
2553 // link the actual to a new ones next and set it with this function
2556 // set ourself as owner for all the paragraphs inserted!
2557 for_each(paragraphs.begin(), paragraphs.end(),
2558 boost::bind(&Paragraph::setInsetOwner, _1, this));
2561 // redraw myself when asked for
2566 Inset * InsetText::getInsetFromID(int id_arg) const
2569 return const_cast<InsetText *>(this);
2571 ParagraphList::iterator pit = paragraphs.begin();
2572 ParagraphList::iterator pend = paragraphs.end();
2573 for (; pit != pend; ++pit) {
2574 InsetList::iterator it = pit->insetlist.begin();
2575 InsetList::iterator end = pit->insetlist.end();
2576 for (; it != end; ++it) {
2577 if (it.getInset()->id() == id_arg)
2578 return it.getInset();
2579 Inset * in = it.getInset()->getInsetFromID(id_arg);
2589 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2590 float & value) const
2596 lt = getLyXText(bv);
2599 if (the_locking_inset) {
2600 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2601 if (!word.word().empty()) {
2607 // we have to go on checking so move cursor to the next char
2608 lt->cursor.pos(lt->cursor.pos() + 1);
2610 word = lt->selectNextWordToSpellcheck(bv, value);
2611 if (word.word().empty())
2612 bv->unlockInset(const_cast<InsetText *>(this));
2621 void InsetText::selectSelectedWord(BufferView * bv)
2623 if (the_locking_inset) {
2624 the_locking_inset->selectSelectedWord(bv);
2627 getLyXText(bv)->selectSelectedWord(bv);
2628 updateLocal(bv, SELECTION, false);
2632 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2634 if (the_locking_inset) {
2635 the_locking_inset->toggleSelection(bv, kill_selection);
2639 lt = getLyXText(bv);
2643 int x = top_x + TEXT_TO_INSET_OFFSET;
2645 Row * row = lt->firstRow();
2646 int y_offset = top_baseline - row->ascent_of_text();
2648 while ((row != 0) && ((y+row->height()) <= 0)) {
2655 if (need_update & SELECTION)
2657 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2663 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2667 lt = getLyXText(bv);
2670 if (the_locking_inset) {
2671 if (the_locking_inset->nextChange(bv, length))
2673 lt->cursorRight(bv, true);
2675 lyxfind::SearchResult result =
2676 lyxfind::findNextChange(bv, lt, length);
2678 if (result == lyxfind::SR_FOUND) {
2679 LyXCursor cur = lt->cursor;
2680 bv->unlockInset(bv->theLockingInset());
2681 if (bv->lockInset(this))
2684 lt->setSelectionRange(bv, length);
2685 updateLocal(bv, SELECTION, false);
2689 return result != lyxfind::SR_NOT_FOUND;
2693 bool InsetText::searchForward(BufferView * bv, string const & str,
2698 lt = getLyXText(bv);
2701 if (the_locking_inset) {
2702 if (the_locking_inset->searchForward(bv, str, cs, mw))
2704 lt->cursorRight(bv, true);
2706 lyxfind::SearchResult result =
2707 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2709 if (result == lyxfind::SR_FOUND) {
2710 LyXCursor cur = lt->cursor;
2711 bv->unlockInset(bv->theLockingInset());
2712 if (bv->lockInset(this))
2715 lt->setSelectionRange(bv, str.length());
2716 updateLocal(bv, SELECTION, false);
2720 return (result != lyxfind::SR_NOT_FOUND);
2723 bool InsetText::searchBackward(BufferView * bv, string const & str,
2726 if (the_locking_inset) {
2727 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2732 lt = getLyXText(bv);
2736 Paragraph * p = &*(paragraphs.begin());
2739 lt->setCursor(bv, p, p->size());
2741 lyxfind::SearchResult result =
2742 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2744 if (result == lyxfind::SR_FOUND) {
2745 LyXCursor cur = lt->cursor;
2746 bv->unlockInset(bv->theLockingInset());
2747 if (bv->lockInset(this))
2750 lt->setSelectionRange(bv, str.length());
2751 updateLocal(bv, SELECTION, false);
2755 return (result != lyxfind::SR_NOT_FOUND);
2759 bool InsetText::checkInsertChar(LyXFont & font)
2762 return owner()->checkInsertChar(font);
2767 void InsetText::collapseParagraphs(BufferView * bv) const
2769 BufferParams const & bparams = bv->buffer()->params;
2770 LyXText * llt = getLyXText(bv);
2772 while (paragraphs.begin()->next()) {
2773 if (!paragraphs.begin()->empty() && !paragraphs.begin()->next()->empty() &&
2774 !paragraphs.begin()->isSeparator(paragraphs.begin()->size() - 1))
2776 paragraphs.begin()->insertChar(paragraphs.begin()->size(), ' ');
2778 if (llt->selection.set()) {
2779 if (llt->selection.start.par() == paragraphs.begin()->next()) {
2780 llt->selection.start.par(&*(paragraphs.begin()));
2781 llt->selection.start.pos(
2782 llt->selection.start.pos() + paragraphs.begin()->size());
2784 if (llt->selection.end.par() == paragraphs.begin()->next()) {
2785 llt->selection.end.par(&*(paragraphs.begin()));
2786 llt->selection.end.pos(
2787 llt->selection.end.pos() + paragraphs.begin()->size());
2790 mergeParagraph(bparams, &*paragraphs.begin());
2796 void InsetText::getDrawFont(LyXFont & font) const
2800 owner()->getDrawFont(font);
2804 void InsetText::appendParagraphs(BufferParams const & bparams,
2808 Paragraph * tmpbuf = newpar;
2809 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2810 if (bparams.tracking_changes)
2811 buf->cleanChanges();
2813 while (tmpbuf->next()) {
2814 tmpbuf = tmpbuf->next();
2815 lastbuffer->next(new Paragraph(*tmpbuf, false));
2816 lastbuffer->next()->previous(lastbuffer);
2817 lastbuffer = lastbuffer->next();
2818 if (bparams.tracking_changes)
2819 lastbuffer->cleanChanges();
2821 lastbuffer = &*(paragraphs.begin());
2822 while (lastbuffer->next())
2823 lastbuffer = lastbuffer->next();
2824 if (!newpar->empty() && !lastbuffer->empty() &&
2825 !lastbuffer->isSeparator(lastbuffer->size() - 1))
2827 lastbuffer->insertChar(lastbuffer->size(), ' ');
2830 // make the buf exactly the same layout than our last paragraph
2831 buf->makeSameLayout(lastbuffer);
2834 lastbuffer->next(buf);
2835 buf->previous(lastbuffer);
2836 mergeParagraph(bparams, lastbuffer);
2842 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2844 Paragraph * par = getFirstParagraph(0);
2846 InsetList::iterator it = par->insetlist.begin();
2847 InsetList::iterator end = par->insetlist.end();
2848 for (; it != end; ++it) {
2849 it.getInset()->addPreview(loader);