1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team.
8 * ======================================================
14 #pragma implementation
17 #include "insettext.h"
20 #include "BufferView.h"
21 #include "CutAndPaste.h"
23 #include "funcrequest.h"
26 #include "LaTeXFeatures.h"
29 #include "lyxcursor.h"
35 #include "paragraph.h"
36 #include "ParagraphParameters.h"
37 #include "trans_mgr.h"
38 #include "undo_funcs.h"
39 #include "WordLangTuple.h"
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;
108 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
111 t->selection.set(true);
112 /* at this point just to avoid the Delete-Empty-Paragraph
113 * Mechanism when setting the cursor */
114 t->selection.mark(sstate.mark_set);
115 if (sstate.selection) {
116 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
117 true, sstate.selstartboundary);
118 t->selection.cursor = t->cursor;
119 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
120 true, sstate.selendboundary);
122 t->setCursor(bv, sstate.lpar, sstate.pos);
124 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
125 t->selection.cursor = t->cursor;
126 t->selection.set(false);
128 if (sstate.refresh) {
134 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
141 InsetText::InsetText(BufferParams const & bp)
142 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
145 paragraphs.set(new Paragraph);
146 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
151 InsetText::InsetText(InsetText const & in, bool same_id)
152 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
159 InsetText & InsetText::operator=(InsetText const & it)
166 void InsetText::init(InsetText const * ins, bool same_id)
169 setParagraphData(&*(ins->paragraphs.begin()), same_id);
170 autoBreakRows = ins->autoBreakRows;
171 drawFrame_ = ins->drawFrame_;
172 frame_color = ins->frame_color;
176 for_each(paragraphs.begin(), paragraphs.end(),
177 boost::bind(&Paragraph::setInsetOwner, _1, this));
179 the_locking_inset = 0;
181 frame_color = LColor::insetframe;
182 autoBreakRows = false;
195 last_drawn_width = -1;
196 frame_is_visible = false;
199 in_insetAllowed = false;
203 InsetText::~InsetText()
209 void InsetText::clear()
211 // This is a gross hack...
212 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
215 paragraphs.set(new Paragraph);
216 paragraphs.begin()->setInsetOwner(this);
217 paragraphs.begin()->layout(old_layout);
224 Inset * InsetText::clone(Buffer const &, bool same_id) const
226 return new InsetText(*this, same_id);
230 void InsetText::write(Buffer const * buf, ostream & os) const
233 writeParagraphData(buf, os);
237 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
239 paragraphs.begin()->writeFile(buf, os, buf->params, 0);
243 void InsetText::read(Buffer const * buf, LyXLex & lex)
247 Paragraph * return_par = 0;
248 Paragraph::depth_type depth = 0;
249 LyXFont font(LyXFont::ALL_INHERIT);
255 token = lex.getString();
258 if (token == "\\end_inset") {
259 #ifndef NO_COMPABILITY
260 const_cast<Buffer*>(buf)->insertErtContents(par, pos, false);
265 Paragraph * tmp = &*(paragraphs.begin());
267 if (const_cast<Buffer*>(buf)->
268 parseSingleLyXformat2Token(lex, tmp, return_par,
269 token, pos, depth, font)) {
270 // the_end read this should NEVER happen
271 lex.printError("\\the_end read in inset! Error in document!");
277 return_par = &*(paragraphs.begin());
278 paragraphs.set(return_par);
280 return_par->setInsetOwner(this);
281 return_par = return_par->next();
284 if (token != "\\end_inset") {
285 lex.printError("Missing \\end_inset at this point. "
292 int InsetText::ascent(BufferView * bv, LyXFont const &) const
294 insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
295 TEXT_TO_INSET_OFFSET;
300 int InsetText::descent(BufferView * bv, LyXFont const &) const
302 LyXText * llt = getLyXText(bv);
303 insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
304 TEXT_TO_INSET_OFFSET;
309 int InsetText::width(BufferView * bv, LyXFont const &) const
311 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
312 (2 * TEXT_TO_INSET_OFFSET);
313 insetWidth = max(insetWidth, 10);
318 int InsetText::textWidth(BufferView * bv, bool fordraw) const
321 if (!autoBreakRows) {
324 w = getMaxWidth(bv, this);
327 return max(w - (2 * TEXT_TO_INSET_OFFSET),
328 (int)getLyXText(bv)->width);
332 return w - (2 * TEXT_TO_INSET_OFFSET);
336 void InsetText::draw(BufferView * bv, LyXFont const & f,
337 int baseline, float & x, bool cleared) const
342 Painter & pain = bv->painter();
344 // this is the first thing we have to ask because if the x pos
345 // changed we have to do a complete rebreak of the text as we
346 // may have few space to draw in. Well we should check on this too
348 if (top_x != int(x)) {
351 int nw = getMaxWidth(bv, this);
352 if (nw > 0 && old_max_width != nw) {
355 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
360 // call these methods so that insetWidth, insetAscent and
361 // insetDescent have the right values.
366 // repaint the background if needed
367 if (cleared && backgroundColor() != LColor::background) {
368 clearInset(bv, baseline, cleared);
371 // no draw is necessary !!!
372 if ((drawFrame_ == LOCKED) && !locked && paragraphs.begin()->empty()) {
373 top_baseline = baseline;
375 if (need_update & CLEAR_FRAME)
376 clearFrame(pain, cleared);
382 x += static_cast<float>(scroll());
384 // if top_x differs we did it already
385 if (!cleared && (old_x == int(x))
386 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
387 ||(last_drawn_width != insetWidth)))
389 // Condition necessary to eliminate bug 59 attachment 37
391 clearInset(bv, baseline, cleared);
395 frame_is_visible = false;
397 if (!cleared && (need_update == NONE)) {
399 drawFrame(pain, cleared);
403 top_baseline = baseline;
404 top_y = baseline - insetAscent;
406 if (last_drawn_width != insetWidth) {
408 clearInset(bv, baseline, cleared);
410 last_drawn_width = insetWidth;
413 if (the_locking_inset && (cpar(bv) == inset_par)
414 && (cpos(bv) == inset_pos)) {
415 inset_x = cix(bv) - top_x + drawTextXOffset;
416 inset_y = ciy(bv) + drawTextYOffset;
418 if (!cleared && (need_update == CURSOR)
419 && !getLyXText(bv)->selection.set()) {
420 drawFrame(pain, cleared);
430 x += TEXT_TO_INSET_OFFSET;
432 Row * row = lt->firstRow();
433 int y_offset = baseline - row->ascent_of_text();
434 int ph = pain.paperHeight();
437 while ((row != 0) && ((y+row->height()) <= 0)) {
439 first += row->height();
443 lt->first_y = -y_offset;
450 if (cleared || (need_update&(INIT|FULL))) {
451 int yf = y_offset + first;
453 while ((row != 0) && (yf < ph)) {
454 lt->getVisibleRow(bv, y + y_offset + first, int(x),
455 row, y + lt->first_y, cleared);
456 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
457 lt->need_break_row = row;
459 lt->setCursor(bv, lt->cursor.par(),
461 if (lt->selection.set()) {
462 lt->setCursor(bv, lt->selection.start,
463 lt->selection.start.par(),
464 lt->selection.start.pos());
465 lt->setCursor(bv, lt->selection.end,
466 lt->selection.end.par(),
467 lt->selection.end.pos());
475 } else if (!locked) {
476 if (need_update & CURSOR) {
477 bv->screen().toggleSelection(lt, bv, true, y_offset,int(x));
478 lt->clearSelection();
479 lt->selection.cursor = lt->cursor;
481 bv->screen().update(lt, bv, y_offset, int(x));
484 if (need_update & SELECTION) {
485 bv->screen().toggleToggle(lt, bv, y_offset, int(x));
486 } else if (need_update & CURSOR) {
487 bv->screen().toggleSelection(lt, bv, true, y_offset,int(x));
488 lt->clearSelection();
489 lt->selection.cursor = lt->cursor;
491 bv->screen().update(lt, bv, y_offset, int(x));
496 lt->status(bv, LyXText::UNCHANGED);
497 if ((drawFrame_ == ALWAYS) ||
498 ((cleared || (need_update != CURSOR_PAR)) &&
499 (drawFrame_ == LOCKED) && locked))
501 drawFrame(pain, cleared);
502 } else if (need_update & CLEAR_FRAME) {
503 clearFrame(pain, cleared);
506 x += insetWidth - TEXT_TO_INSET_OFFSET;
508 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
510 } else if (need_update != INIT) {
518 void InsetText::drawFrame(Painter & pain, bool cleared) const
520 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
521 if (!frame_is_visible || cleared) {
522 frame_x = top_x + ttoD2;
523 frame_y = top_baseline - insetAscent + ttoD2;
524 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
525 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
526 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
528 frame_is_visible = true;
533 void InsetText::clearFrame(Painter & pain, bool cleared) const
535 if (frame_is_visible) {
537 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
540 frame_is_visible = false;
545 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
548 if (reinit && owner()) {
550 owner()->update(bv, font, true);
555 if (reinit || need_update == INIT) {
557 // we should put this call where we set need_update to INIT!
560 owner()->update(bv, font, true);
565 if (!autoBreakRows && paragraphs.begin()->next())
566 collapseParagraphs(bv);
568 if (the_locking_inset) {
569 inset_x = cix(bv) - top_x + drawTextXOffset;
570 inset_y = ciy(bv) + drawTextYOffset;
571 the_locking_inset->update(bv, font, reinit);
579 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
582 lt->updateInset(bv, the_locking_inset);
584 if (lt->status() == LyXText::NEED_MORE_REFRESH)
592 void InsetText::setUpdateStatus(BufferView * bv, int what) const
594 // this does nothing dangerous so use only a localized buffer
595 LyXText * llt = getLyXText(bv);
598 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
599 // if we don't break row so that we only have one row to update!
600 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
602 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
605 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
606 need_update |= CURSOR_PAR;
609 // this to not draw a selection when we redraw all of it!
610 if (need_update & CURSOR && !(need_update & SELECTION)) {
611 if (llt->selection.set())
613 llt->clearSelection();
618 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
620 if (!autoBreakRows && paragraphs.begin()->next())
621 collapseParagraphs(bv);
628 setUpdateStatus(bv, what);
629 bool flag = mark_dirty ||
630 (((need_update != CURSOR) && (need_update != NONE)) ||
631 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
632 if (!lt->selection.set())
633 lt->selection.cursor = lt->cursor;
637 // IMO this is not anymore needed as we do this in fitInsetCursor!
638 // and we always get "true" as returnvalue of this function in the
639 // case of a locking inset (Jug 20020412)
640 if (locked && (need_update & CURSOR) && bv->fitCursor())
646 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
648 if (need_update == CURSOR)
650 bv->owner()->view_state_changed();
651 bv->owner()->updateMenubar();
652 bv->owner()->updateToolbar();
653 if (old_par != cpar(bv)) {
654 bv->owner()->setLayout(cpar(bv)->layout()->name());
660 string const InsetText::editMessage() const
662 return _("Opened Text Inset");
666 void InsetText::edit(BufferView * bv, int x, int y, mouse_button::state button)
668 UpdatableInset::edit(bv, x, y, button);
670 if (!bv->lockInset(this)) {
671 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
675 the_locking_inset = 0;
676 inset_pos = inset_x = inset_y = 0;
677 inset_boundary = false;
680 int tmp_y = (y < 0) ? 0 : y;
686 // we put here -1 and not button as now the button in the
687 // edit call should not be needed we will fix this in 1.3.x
688 // cycle hopefully (Jug 20020509)
689 // FIXME: GUII I've changed this to none: probably WRONG
690 if (!checkAndActivateInset(bv, x, tmp_y, mouse_button::none)) {
691 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
693 lt->cursor.x_fix(lt->cursor.x());
695 lt->clearSelection();
697 // If the inset is empty set the language of the current font to the
698 // language to the surronding text (if different).
699 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
700 bv->getParentLanguage(this) != lt->current_font.language())
702 LyXFont font(LyXFont::ALL_IGNORE);
703 font.setLanguage(bv->getParentLanguage(this));
704 setFont(bv, font, false);
710 if (drawFrame_ == LOCKED)
711 code = CURSOR|DRAW_FRAME;
712 updateLocal(bv, code, false);
715 // Tell the paragraph dialog that we've entered an insettext.
716 bv->owner()->getDialogs().updateParagraph();
720 void InsetText::edit(BufferView * bv, bool front)
722 UpdatableInset::edit(bv, front);
724 if (!bv->lockInset(this)) {
725 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
729 the_locking_inset = 0;
730 inset_pos = inset_x = inset_y = 0;
731 inset_boundary = false;
740 lt->setCursor(bv, &*(paragraphs.begin()), 0);
742 ParagraphList::iterator it = paragraphs.begin();
743 ParagraphList::iterator end = paragraphs.end();
744 while (boost::next(it) != end)
746 // int const pos = (p->size() ? p->size()-1 : p->size());
747 lt->setCursor(bv, &*it, it->size());
749 lt->clearSelection();
751 // If the inset is empty set the language of the current font to the
752 // language to the surronding text (if different).
753 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
754 bv->getParentLanguage(this) != lt->current_font.language()) {
755 LyXFont font(LyXFont::ALL_IGNORE);
756 font.setLanguage(bv->getParentLanguage(this));
757 setFont(bv, font, false);
762 if (drawFrame_ == LOCKED)
763 code = CURSOR|DRAW_FRAME;
764 updateLocal(bv, code, false);
769 void InsetText::insetUnlock(BufferView * bv)
771 if (the_locking_inset) {
772 the_locking_inset->insetUnlock(bv);
773 the_locking_inset = 0;
774 updateLocal(bv, CURSOR_PAR, false);
781 if (drawFrame_ == LOCKED)
782 code = CURSOR|CLEAR_FRAME;
786 if (drawFrame_ == LOCKED)
794 if (lt->selection.set()) {
795 lt->clearSelection();
797 } else if (owner()) {
798 bv->owner()->setLayout(owner()->getLyXText(bv)
799 ->cursor.par()->layout()->name());
801 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
802 // hack for deleteEmptyParMech
803 if (!paragraphs.begin()->empty()) {
804 lt->setCursor(bv, &*(paragraphs.begin()), 0);
805 } else if (paragraphs.begin()->next()) {
806 lt->setCursor(bv, paragraphs.begin()->next(), 0);
811 updateLocal(bv, code, false);
814 setUpdateStatus(bv, code);
819 void InsetText::lockInset(BufferView * bv)
822 the_locking_inset = 0;
823 inset_pos = inset_x = inset_y = 0;
824 inset_boundary = false;
832 lt->setCursor(bv, &*(paragraphs.begin()), 0);
833 lt->clearSelection();
835 // If the inset is empty set the language of the current font to the
836 // language to the surronding text (if different).
837 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
838 bv->getParentLanguage(this) != lt->current_font.language()) {
839 LyXFont font(LyXFont::ALL_IGNORE);
840 font.setLanguage(bv->getParentLanguage(this));
841 setFont(bv, font, false);
846 if (drawFrame_ == LOCKED)
847 code = CURSOR|DRAW_FRAME;
848 setUpdateStatus(bv, code);
852 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
854 the_locking_inset = inset;
855 inset_x = cix(bv) - top_x + drawTextXOffset;
856 inset_y = ciy(bv) + drawTextYOffset;
857 inset_pos = cpos(bv);
858 inset_par = cpar(bv);
859 inset_boundary = cboundary(bv);
860 updateLocal(bv, CURSOR, false);
864 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
866 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
870 if (!the_locking_inset) {
871 Paragraph * p = &*(paragraphs.begin());
872 int const id = inset->id();
874 InsetList::iterator it =
875 p->insetlist.begin();
876 InsetList::iterator const end =
878 for (; it != end; ++it) {
879 if (it.getInset() == inset) {
880 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
881 lockInset(bv, inset);
884 if (it.getInset()->getInsetFromID(id)) {
885 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
886 it.getInset()->edit(bv);
887 return the_locking_inset->lockInsetInInset(bv, inset);
894 if (inset == cpar(bv)->getInset(cpos(bv))) {
895 lyxerr[Debug::INSETS] << "OK" << endl;
896 lockInset(bv, inset);
898 } else if (the_locking_inset && (the_locking_inset == inset)) {
899 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
900 lyxerr[Debug::INSETS] << "OK" << endl;
901 inset_x = cix(bv) - top_x + drawTextXOffset;
902 inset_y = ciy(bv) + drawTextYOffset;
904 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
906 } else if (the_locking_inset) {
907 lyxerr[Debug::INSETS] << "MAYBE" << endl;
908 return the_locking_inset->lockInsetInInset(bv, inset);
910 lyxerr[Debug::INSETS] << "NOT OK" << endl;
915 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
918 if (!the_locking_inset)
920 if (the_locking_inset == inset) {
921 the_locking_inset->insetUnlock(bv);
922 getLyXText(bv)->updateInset(bv, inset);
923 the_locking_inset = 0;
925 moveRightIntern(bv, true, false);
926 old_par = 0; // force layout setting
930 updateLocal(bv, CURSOR, false);
933 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
937 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
939 if (!autoBreakRows && paragraphs.begin()->next())
940 collapseParagraphs(bv);
948 if (inset->owner() != this) {
949 int ustat = CURSOR_PAR;
951 UpdatableInset * tl_inset = the_locking_inset;
953 found = tl_inset->updateInsetInInset(bv, inset);
955 tl_inset = static_cast<UpdatableInset *>(inset);
956 while(tl_inset->owner() && tl_inset->owner() != this)
957 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
958 if (!tl_inset->owner())
960 found = tl_inset->updateInsetInInset(bv, inset);
964 lt->updateInset(bv, tl_inset);
968 setUpdateStatus(bv, ustat);
971 bool found = lt->updateInset(bv, inset);
975 setUpdateStatus(bv, CURSOR_PAR);
976 if (the_locking_inset &&
977 cpar(bv) == inset_par && cpos(bv) == inset_pos)
979 inset_x = cix(bv) - top_x + drawTextXOffset;
980 inset_y = ciy(bv) + drawTextYOffset;
987 void InsetText::insetButtonPress(BufferView * bv,
988 int x, int y, mouse_button::state button)
992 // use this to check mouse motion for selection!
999 int tmp_x = x - drawTextXOffset;
1000 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1001 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1003 hideInsetCursor(bv);
1004 if (the_locking_inset) {
1005 if (the_locking_inset == inset) {
1006 the_locking_inset->insetButtonPress(bv,
1014 // otherwise unlock the_locking_inset and lock the new inset
1015 the_locking_inset->insetUnlock(bv);
1016 inset_x = cix(bv) - top_x + drawTextXOffset;
1017 inset_y = ciy(bv) + drawTextYOffset;
1018 the_locking_inset = 0;
1019 inset->insetButtonPress(bv, x - inset_x,
1020 y - inset_y, button);
1021 // inset->edit(bv, x - inset_x, y - inset_y, button);
1022 if (the_locking_inset)
1023 updateLocal(bv, CURSOR, false);
1027 // otherwise only unlock the_locking_inset
1028 the_locking_inset->insetUnlock(bv);
1029 the_locking_inset = 0;
1032 no_selection = false;
1034 if (bv->theLockingInset()) {
1035 if (isHighlyEditableInset(inset)) {
1036 // We just have to lock the inset before calling a
1037 // PressEvent on it!
1038 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
1039 if (!bv->lockInset(uinset)) {
1040 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1042 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
1043 if (the_locking_inset)
1044 updateLocal(bv, CURSOR, false);
1048 if (!inset) { // && (button == mouse_button::button2)) {
1049 bool paste_internally = false;
1050 if ((button == mouse_button::button2) && getLyXText(bv)->selection.set()) {
1051 localDispatch(FuncRequest(bv, LFUN_COPY));
1052 paste_internally = true;
1056 lt = getLyXText(bv);
1059 int old_first_y = lt->first_y;
1061 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
1063 // set the selection cursor!
1064 lt->selection.cursor = lt->cursor;
1065 lt->cursor.x_fix(lt->cursor.x());
1067 if (lt->selection.set()) {
1068 lt->clearSelection();
1071 updateLocal(bv, FULL, false);
1073 lt->clearSelection();
1076 updateLocal(bv, CURSOR, false);
1079 bv->owner()->setLayout(cpar(bv)->layout()->name());
1081 // we moved the view we cannot do mouse selection in this case!
1082 if (getLyXText(bv)->first_y != old_first_y)
1083 no_selection = true;
1085 // Insert primary selection with middle mouse
1086 // if there is a local selection in the current buffer,
1088 if (button == mouse_button::button2) {
1089 if (paste_internally)
1090 localDispatch(FuncRequest(bv, LFUN_PASTE));
1092 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
1095 getLyXText(bv)->clearSelection();
1097 showInsetCursor(bv);
1101 bool InsetText::insetButtonRelease(BufferView * bv,
1102 int x, int y, mouse_button::state button)
1104 no_selection = true;
1105 if (the_locking_inset) {
1106 return the_locking_inset->insetButtonRelease(bv,
1107 x - inset_x, y - inset_y,
1110 int tmp_x = x - drawTextXOffset;
1111 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1112 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1115 if (isHighlyEditableInset(inset)) {
1116 ret = inset->insetButtonRelease(bv, x - inset_x,
1117 y - inset_y, button);
1119 inset_x = cix(bv) - top_x + drawTextXOffset;
1120 inset_y = ciy(bv) + drawTextYOffset;
1121 ret = inset->insetButtonRelease(bv, x - inset_x,
1122 y - inset_y, button);
1123 inset->edit(bv, x - inset_x,
1124 y - inset_y, button);
1126 updateLocal(bv, CURSOR_PAR, false);
1132 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, mouse_button::state state)
1134 if (the_locking_inset) {
1135 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1140 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1145 lt = getLyXText(bv);
1148 hideInsetCursor(bv);
1149 LyXCursor cur = lt->cursor;
1150 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1151 lt->cursor.x_fix(lt->cursor.x());
1152 if (cur == lt->cursor) {
1157 lt->setSelection(bv);
1158 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1159 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1163 updateLocal(bv, SELECTION, false);
1165 showInsetCursor(bv);
1169 UpdatableInset::RESULT
1170 InsetText::localDispatch(FuncRequest const & ev)
1172 BufferView * bv = ev.view();
1173 bool was_empty = (paragraphs.begin()->empty() && !paragraphs.begin()->next());
1174 no_selection = false;
1175 RESULT result= UpdatableInset::localDispatch(ev);
1176 if (result != UNDISPATCHED)
1179 result = DISPATCHED;
1180 if (ev.action < 0 && ev.argument.empty())
1183 if (the_locking_inset) {
1184 result = the_locking_inset->localDispatch(ev);
1185 if (result == DISPATCHED_NOUPDATE)
1187 else if (result == DISPATCHED) {
1188 updateLocal(bv, CURSOR_PAR, false);
1190 } else if (result >= FINISHED) {
1192 case FINISHED_RIGHT:
1193 moveRightIntern(bv, false, false);
1194 result = DISPATCHED;
1197 if ((result = moveUp(bv)) >= FINISHED) {
1198 updateLocal(bv, CURSOR, false);
1199 bv->unlockInset(this);
1204 LyXText *lt = getLyXText(bv);
1205 if (lt->cursor.irow()->next()) {
1206 lt->setCursorFromCoordinates(
1207 bv, lt->cursor.ix() + inset_x,
1209 lt->cursor.irow()->baseline() +
1210 lt->cursor.irow()->height() + 1);
1211 lt->cursor.x_fix(lt->cursor.x());
1212 updateLocal(bv, CURSOR, false);
1214 bv->unlockInset(this);
1219 result = DISPATCHED;
1222 the_locking_inset = 0;
1223 updateLocal(bv, CURSOR, false);
1227 hideInsetCursor(bv);
1230 lt = getLyXText(bv);
1234 int updflag = false;
1235 switch (ev.action) {
1237 case LFUN_SELFINSERT:
1238 if (bv->buffer()->isReadonly()) {
1239 // setErrorMessage(N_("Document is read only"));
1242 if (!ev.argument.empty()) {
1243 /* Automatically delete the currently selected
1244 * text and replace it with what is being
1245 * typed in now. Depends on lyxrc settings
1246 * "auto_region_delete", which defaults to
1249 setUndo(bv, Undo::INSERT,
1250 lt->cursor.par(), lt->cursor.par()->next());
1252 if (lyxrc.auto_region_delete) {
1253 if (lt->selection.set()) {
1254 lt->cutSelection(bv, false, false);
1257 lt->clearSelection();
1258 for (string::size_type i = 0; i < ev.argument.length(); ++i) {
1259 bv->owner()->getIntl().getTransManager().
1260 TranslateAndInsert(ev.argument[i], lt);
1263 lt->selection.cursor = lt->cursor;
1264 updwhat = CURSOR | CURSOR_PAR;
1266 result = DISPATCHED_NOUPDATE;
1268 // --- Cursor Movements -----------------------------------
1271 moveRight(bv, false, true);
1272 lt->setSelection(bv);
1273 updwhat = SELECTION;
1276 result = moveRight(bv);
1282 moveLeft(bv, false, true);
1283 lt->setSelection(bv);
1284 updwhat = SELECTION;
1288 result = moveLeft(bv);
1294 lt->setSelection(bv);
1295 updwhat = SELECTION;
1299 result = moveDown(bv);
1305 lt->setSelection(bv);
1306 updwhat = SELECTION;
1310 result = moveUp(bv);
1322 case LFUN_BACKSPACE: {
1323 setUndo(bv, Undo::DELETE,
1324 lt->cursor.par(), lt->cursor.par()->next());
1325 if (lt->selection.set())
1326 lt->cutSelection(bv, true, false);
1329 updwhat = CURSOR_PAR;
1335 setUndo(bv, Undo::DELETE,
1336 lt->cursor.par(), lt->cursor.par()->next());
1337 if (lt->selection.set()) {
1338 lt->cutSelection(bv, true, false);
1342 updwhat = CURSOR_PAR;
1348 setUndo(bv, Undo::DELETE,
1349 lt->cursor.par(), lt->cursor.par()->next());
1350 lt->cutSelection(bv);
1351 updwhat = CURSOR_PAR;
1358 lt->copySelection(bv);
1359 updwhat = CURSOR_PAR;
1361 case LFUN_PASTESELECTION:
1363 string const clip(bv->getClipboard());
1367 if (ev.argument == "paragraph") {
1368 lt->insertStringAsParagraphs(bv, clip);
1370 lt->insertStringAsLines(bv, clip);
1373 lt->clearSelection();
1375 updwhat = CURSOR_PAR;
1380 if (!autoBreakRows) {
1382 if (CutAndPaste::nrOfParagraphs() > 1) {
1383 Alert::alert(_("Impossible operation"),
1384 _("Cannot include more than one paragraph!"),
1389 setUndo(bv, Undo::INSERT,
1390 lt->cursor.par(), lt->cursor.par()->next());
1391 lt->pasteSelection(bv);
1393 lt->clearSelection();
1394 updwhat = CURSOR_PAR;
1399 case LFUN_BREAKPARAGRAPH:
1400 if (!autoBreakRows) {
1401 result = DISPATCHED;
1404 lt->breakParagraph(bv, 0);
1405 updwhat = CURSOR | FULL;
1408 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1409 if (!autoBreakRows) {
1410 result = DISPATCHED;
1413 lt->breakParagraph(bv, 1);
1414 updwhat = CURSOR | FULL;
1418 case LFUN_BREAKLINE: {
1419 if (!autoBreakRows) {
1420 result = DISPATCHED;
1423 setUndo(bv, Undo::INSERT,
1424 lt->cursor.par(), lt->cursor.par()->next());
1425 lt->insertChar(bv, Paragraph::META_NEWLINE);
1426 updwhat = CURSOR | CURSOR_PAR;
1432 // do not set layouts on non breakable textinsets
1433 if (autoBreakRows) {
1434 string cur_layout = cpar(bv)->layout()->name();
1436 // Derive layout number from given argument (string)
1437 // and current buffer's textclass (number). */
1438 LyXTextClass const & tclass =
1439 bv->buffer()->params.getLyXTextClass();
1440 string layout = ev.argument;
1441 bool hasLayout = tclass.hasLayout(layout);
1443 // If the entry is obsolete, use the new one instead.
1445 string const & obs =
1446 tclass[layout]->obsoleted_by();
1451 // see if we found the layout number:
1453 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + ev.argument + N_(" not known"));
1454 bv->owner()->dispatch(lf);
1458 if (cur_layout != layout) {
1459 cur_layout = layout;
1460 lt->setLayout(bv, layout);
1461 bv->owner()->setLayout(cpar(bv)->layout()->name());
1462 updwhat = CURSOR_PAR;
1466 // reset the layout box
1467 bv->owner()->setLayout(cpar(bv)->layout()->name());
1470 case LFUN_PARAGRAPH_SPACING:
1471 // This one is absolutely not working. When fiddling with this
1472 // it also seems to me that the paragraphs inside the insettext
1473 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1475 Paragraph * par = lt->cursor.par();
1476 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1477 float cur_value = 1.0;
1478 if (cur_spacing == Spacing::Other) {
1479 cur_value = par->params().spacing().getValue();
1482 istringstream istr(ev.argument.c_str());
1485 Spacing::Space new_spacing = cur_spacing;
1486 float new_value = cur_value;
1488 lyxerr << "Missing argument to `paragraph-spacing'"
1490 } else if (tmp == "single") {
1491 new_spacing = Spacing::Single;
1492 } else if (tmp == "onehalf") {
1493 new_spacing = Spacing::Onehalf;
1494 } else if (tmp == "double") {
1495 new_spacing = Spacing::Double;
1496 } else if (tmp == "other") {
1497 new_spacing = Spacing::Other;
1500 lyxerr << "new_value = " << tmpval << endl;
1503 } else if (tmp == "default") {
1504 new_spacing = Spacing::Default;
1506 lyxerr << _("Unknown spacing argument: ")
1507 << ev.argument << endl;
1509 if (cur_spacing != new_spacing || cur_value != new_value) {
1510 par->params().spacing(Spacing(new_spacing, new_value));
1511 updwhat = CURSOR_PAR;
1518 if (!bv->dispatch(ev))
1519 result = UNDISPATCHED;
1526 updateLocal(bv, updwhat, updflag);
1527 /// If the action has deleted all text in the inset, we need to change the
1528 // language to the language of the surronding text.
1529 if (!was_empty && paragraphs.begin()->empty() && !paragraphs.begin()->next()) {
1530 LyXFont font(LyXFont::ALL_IGNORE);
1531 font.setLanguage(bv->getParentLanguage(this));
1532 setFont(bv, font, false);
1535 if (result >= FINISHED)
1536 bv->unlockInset(this);
1538 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1539 result = DISPATCHED;
1544 int InsetText::latex(Buffer const * buf, ostream & os,
1545 bool moving_arg, bool) const
1548 buf->latexParagraphs(os, &*(paragraphs.begin()), 0,
1549 texrow, moving_arg);
1550 return texrow.rows();
1554 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1556 unsigned int lines = 0;
1558 ParagraphList::iterator beg = paragraphs.begin();
1559 ParagraphList::iterator end = paragraphs.end();
1560 ParagraphList::iterator it = beg;
1561 for (; it != end; ++it) {
1562 string const tmp = buf->asciiParagraph(&*it, linelen, it == beg);
1563 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1570 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1572 Paragraph * p = &*(paragraphs.begin());
1573 unsigned int lines = 0;
1575 vector<string> environment_stack(10);
1576 vector<string> environment_inner(10);
1578 int const command_depth = 0;
1581 Paragraph::depth_type depth = 0; // paragraph depth
1585 int desc_on = 0; // description mode
1587 LyXLayout_ptr const & style = p->layout();
1589 // environment tag closing
1590 for (; depth > p->params().depth(); --depth) {
1591 if (environment_inner[depth] != "!-- --") {
1592 item_name = "listitem";
1593 lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
1594 if (environment_inner[depth] == "varlistentry")
1595 lines += buf->sgmlCloseTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1597 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1598 environment_stack[depth].erase();
1599 environment_inner[depth].erase();
1602 if (depth == p->params().depth()
1603 && environment_stack[depth] != style->latexname()
1604 && !environment_stack[depth].empty()) {
1605 if (environment_inner[depth] != "!-- --") {
1606 item_name= "listitem";
1607 lines += buf->sgmlCloseTag(os, command_depth+depth, mixcont, item_name);
1608 if (environment_inner[depth] == "varlistentry")
1609 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1612 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1614 environment_stack[depth].erase();
1615 environment_inner[depth].erase();
1618 // Write opening SGML tags.
1619 switch (style->latextype) {
1620 case LATEX_PARAGRAPH:
1621 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style->latexname());
1625 buf->sgmlError(p, 0, _("Error : LatexType Command not allowed here.\n"));
1629 case LATEX_ENVIRONMENT:
1630 case LATEX_ITEM_ENVIRONMENT:
1631 if (depth < p->params().depth()) {
1632 depth = p->params().depth();
1633 environment_stack[depth].erase();
1636 if (environment_stack[depth] != style->latexname()) {
1637 if (environment_stack.size() == depth + 1) {
1638 environment_stack.push_back("!-- --");
1639 environment_inner.push_back("!-- --");
1641 environment_stack[depth] = style->latexname();
1642 environment_inner[depth] = "!-- --";
1643 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1645 if (environment_inner[depth] != "!-- --") {
1646 item_name= "listitem";
1647 lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
1648 if (environment_inner[depth] == "varlistentry")
1649 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1653 if (style->latextype == LATEX_ENVIRONMENT) {
1654 if (!style->latexparam().empty()) {
1655 if (style->latexparam() == "CDATA")
1658 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style->latexparam());
1663 desc_on = (style->labeltype == LABEL_MANUAL);
1665 environment_inner[depth] = desc_on?"varlistentry":"listitem";
1666 lines += buf->sgmlOpenTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1668 item_name = desc_on?"term":"para";
1669 lines += buf->sgmlOpenTag(os, depth + 1 + command_depth, mixcont, item_name);
1673 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style->latexname());
1677 buf->simpleDocBookOnePar(os, p, desc_on, depth + 1 + command_depth);
1681 // write closing SGML tags
1682 switch (style->latextype) {
1683 case LATEX_ENVIRONMENT:
1684 if (!style->latexparam().empty()) {
1685 if (style->latexparam() == "CDATA")
1688 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style->latexparam());
1691 case LATEX_ITEM_ENVIRONMENT:
1692 if (desc_on == 1) break;
1694 lines += buf->sgmlCloseTag(os, depth + 1 + command_depth, mixcont, end_tag);
1696 case LATEX_PARAGRAPH:
1697 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style->latexname());
1700 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style->latexname());
1706 for (int d = depth; d >= 0; --d) {
1707 if (!environment_stack[depth].empty()) {
1708 if (environment_inner[depth] != "!-- --") {
1709 item_name = "listitem";
1710 lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
1711 if (environment_inner[depth] == "varlistentry")
1712 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1715 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1723 void InsetText::validate(LaTeXFeatures & features) const
1725 for_each(paragraphs.begin(), paragraphs.end(),
1726 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1730 int InsetText::beginningOfMainBody(Paragraph * p) const
1732 if (p->layout()->labeltype != LABEL_MANUAL)
1735 return p->beginningOfMainBody();
1739 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1741 if (the_locking_inset) {
1742 the_locking_inset->getCursorPos(bv, x, y);
1745 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1746 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1750 int InsetText::insetInInsetY() const
1752 if (!the_locking_inset)
1755 return (inset_y + the_locking_inset->insetInInsetY());
1759 void InsetText::toggleInsetCursor(BufferView * bv)
1761 if (the_locking_inset) {
1762 the_locking_inset->toggleInsetCursor(bv);
1766 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1768 int const asc = font_metrics::maxAscent(font);
1769 int const desc = font_metrics::maxDescent(font);
1771 if (isCursorVisible())
1772 bv->hideLockedInsetCursor();
1774 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1775 toggleCursorVisible();
1779 void InsetText::showInsetCursor(BufferView * bv, bool show)
1781 if (the_locking_inset) {
1782 the_locking_inset->showInsetCursor(bv, show);
1785 if (!isCursorVisible()) {
1786 LyXFont const font =
1787 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1789 int const asc = font_metrics::maxAscent(font);
1790 int const desc = font_metrics::maxDescent(font);
1792 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1794 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1795 setCursorVisible(true);
1800 void InsetText::hideInsetCursor(BufferView * bv)
1802 if (isCursorVisible()) {
1803 bv->hideLockedInsetCursor();
1804 setCursorVisible(false);
1806 if (the_locking_inset)
1807 the_locking_inset->hideInsetCursor(bv);
1811 void InsetText::fitInsetCursor(BufferView * bv) const
1813 if (the_locking_inset) {
1814 the_locking_inset->fitInsetCursor(bv);
1817 LyXFont const font =
1818 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1820 int const asc = font_metrics::maxAscent(font);
1821 int const desc = font_metrics::maxDescent(font);
1823 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1824 need_update |= FULL;
1828 UpdatableInset::RESULT
1829 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1831 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1832 return moveLeftIntern(bv, false, activate_inset, selecting);
1834 return moveRightIntern(bv, true, activate_inset, selecting);
1838 UpdatableInset::RESULT
1839 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1841 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1842 return moveRightIntern(bv, true, activate_inset, selecting);
1844 return moveLeftIntern(bv, false, activate_inset, selecting);
1848 UpdatableInset::RESULT
1849 InsetText::moveRightIntern(BufferView * bv, bool front,
1850 bool activate_inset, bool selecting)
1852 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1853 return FINISHED_RIGHT;
1854 if (activate_inset && checkAndActivateInset(bv, front))
1856 getLyXText(bv)->cursorRight(bv);
1858 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1859 return DISPATCHED_NOUPDATE;
1863 UpdatableInset::RESULT
1864 InsetText::moveLeftIntern(BufferView * bv, bool front,
1865 bool activate_inset, bool selecting)
1867 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1869 getLyXText(bv)->cursorLeft(bv);
1871 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1872 if (activate_inset && checkAndActivateInset(bv, front))
1874 return DISPATCHED_NOUPDATE;
1878 UpdatableInset::RESULT
1879 InsetText::moveUp(BufferView * bv)
1881 if (!crow(bv)->previous())
1883 getLyXText(bv)->cursorUp(bv);
1884 return DISPATCHED_NOUPDATE;
1888 UpdatableInset::RESULT
1889 InsetText::moveDown(BufferView * bv)
1891 if (!crow(bv)->next())
1892 return FINISHED_DOWN;
1893 getLyXText(bv)->cursorDown(bv);
1894 return DISPATCHED_NOUPDATE;
1898 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1900 if (the_locking_inset) {
1901 if (the_locking_inset->insetAllowed(inset))
1902 return the_locking_inset->insertInset(bv, inset);
1905 inset->setOwner(this);
1906 hideInsetCursor(bv);
1907 getLyXText(bv)->insertInset(bv, inset);
1909 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1914 bool InsetText::insetAllowed(Inset::Code code) const
1916 // in_insetAllowed is a really gross hack,
1917 // to allow us to call the owner's insetAllowed
1918 // without stack overflow, which can happen
1919 // when the owner uses InsetCollapsable::insetAllowed()
1921 if (in_insetAllowed)
1923 in_insetAllowed = true;
1924 if (the_locking_inset)
1925 ret = the_locking_inset->insetAllowed(code);
1927 ret = owner()->insetAllowed(code);
1928 in_insetAllowed = false;
1933 UpdatableInset * InsetText::getLockingInset() const
1935 return the_locking_inset ? the_locking_inset->getLockingInset() :
1936 const_cast<InsetText *>(this);
1940 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1944 if (the_locking_inset)
1945 return the_locking_inset->getFirstLockingInsetOfType(c);
1950 bool InsetText::showInsetDialog(BufferView * bv) const
1952 if (the_locking_inset)
1953 return the_locking_inset->showInsetDialog(bv);
1958 vector<string> const InsetText::getLabelList() const
1960 vector<string> label_list;
1962 ParagraphList::iterator pit = paragraphs.begin();
1963 ParagraphList::iterator pend = paragraphs.end();
1964 for (; pit != pend; ++pit) {
1965 InsetList::iterator beg = pit->insetlist.begin();
1966 InsetList::iterator end = pit->insetlist.end();
1967 for (; beg != end; ++beg) {
1968 vector<string> const l = beg.getInset()->getLabelList();
1969 label_list.insert(label_list.end(), l.begin(), l.end());
1976 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1979 if (the_locking_inset) {
1980 the_locking_inset->setFont(bv, font, toggleall, selectall);
1983 if ((!paragraphs.begin()->next() && paragraphs.begin()->empty()) || cpar(bv)->empty()) {
1984 getLyXText(bv)->setFont(bv, font, toggleall);
1989 lt = getLyXText(bv);
1992 if (lt->selection.set()) {
1993 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1997 lt->toggleFree(bv, font, toggleall);
1999 lt->clearSelection();
2001 bool flag = (selectall || lt->selection.set());
2005 updateLocal(bv, FULL, true);
2007 updateLocal(bv, CURSOR_PAR, true);
2011 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
2013 if (cpar(bv)->isInset(cpos(bv))) {
2015 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
2016 if (!isHighlyEditableInset(inset))
2018 inset->edit(bv, front);
2019 if (!the_locking_inset)
2021 updateLocal(bv, CURSOR, false);
2028 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2029 mouse_button::state button)
2031 x -= drawTextXOffset;
2033 int dummyy = y + insetAscent;
2034 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
2035 // we only do the edit() call if the inset was hit by the mouse
2036 // or if it is a highly editable inset. So we should call this
2037 // function from our own edit with button < 0.
2038 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
2040 if (button == mouse_button::none && !isHighlyEditableInset(inset))
2048 inset_x = cix(bv) - top_x + drawTextXOffset;
2049 inset_y = ciy(bv) + drawTextYOffset;
2050 inset->edit(bv, x - inset_x, y - inset_y, button);
2051 if (!the_locking_inset)
2053 updateLocal(bv, CURSOR, false);
2060 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2063 int w = UpdatableInset::getMaxWidth(bv, inset);
2068 w = w - top_x + owner()->x();
2071 w -= (2 * TEXT_TO_INSET_OFFSET);
2074 return UpdatableInset::getMaxWidth(bv, inset);
2079 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2081 // we have to unlock any locked inset otherwise we're in troubles
2082 the_locking_inset = 0;
2085 paragraphs.set(new Paragraph(*p, same_id));
2086 paragraphs.begin()->setInsetOwner(this);
2087 Paragraph * np = &*(paragraphs.begin());
2090 np->next(new Paragraph(*p, same_id));
2091 np->next()->previous(np);
2093 np->setInsetOwner(this);
2100 void InsetText::setText(string const & data, LyXFont const & font)
2103 for (unsigned int i = 0; i < data.length(); ++i)
2104 paragraphs.begin()->insertChar(i, data[i], font);
2109 void InsetText::setAutoBreakRows(bool flag)
2111 if (flag != autoBreakRows) {
2112 autoBreakRows = flag;
2120 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2122 if (how != drawFrame_) {
2125 updateLocal(bv, DRAW_FRAME, false);
2130 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2132 if (frame_color != col) {
2135 updateLocal(bv, DRAW_FRAME, false);
2140 int InsetText::cx(BufferView * bv) const
2142 // we do nothing dangerous so we use a local cache
2143 LyXText * llt = getLyXText(bv);
2144 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2145 if (the_locking_inset) {
2146 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2148 if (font.isVisibleRightToLeft())
2149 x -= the_locking_inset->width(bv, font);
2155 int InsetText::cix(BufferView * bv) const
2157 // we do nothing dangerous so we use a local cache
2158 LyXText * llt = getLyXText(bv);
2159 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2160 if (the_locking_inset) {
2161 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2163 if (font.isVisibleRightToLeft())
2164 x -= the_locking_inset->width(bv, font);
2170 int InsetText::cy(BufferView * bv) const
2173 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2177 int InsetText::ciy(BufferView * bv) const
2180 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2184 pos_type InsetText::cpos(BufferView * bv) const
2186 return getLyXText(bv)->cursor.pos();
2190 Paragraph * InsetText::cpar(BufferView * bv) const
2192 return getLyXText(bv)->cursor.par();
2196 bool InsetText::cboundary(BufferView * bv) const
2198 return getLyXText(bv)->cursor.boundary();
2202 Row * InsetText::crow(BufferView * bv) const
2204 return getLyXText(bv)->cursor.row();
2208 LyXText * InsetText::getLyXText(BufferView const * lbv,
2209 bool const recursive) const
2211 if (cached_bview == lbv) {
2212 if (recursive && the_locking_inset)
2213 return the_locking_inset->getLyXText(lbv, true);
2214 LyXText * lt = cached_text.get();
2215 lyx::Assert(lt && lt->firstRow()->par() == &*(paragraphs.begin()));
2218 // Super UGLY! (Lgb)
2219 BufferView * bv = const_cast<BufferView *>(lbv);
2222 Cache::iterator it = cache.find(bv);
2224 if (it != cache.end()) {
2227 } else if (do_resize) {
2228 resizeLyXText(do_resize);
2230 if (lt || !it->second.remove) {
2231 lyx::Assert(it->second.text.get());
2232 cached_text = it->second.text;
2233 if (recursive && the_locking_inset) {
2234 return the_locking_inset->getLyXText(bv, true);
2236 return cached_text.get();
2237 } else if (it->second.remove) {
2239 saveLyXTextState(it->second.text.get());
2245 // when we have to reinit the existing LyXText!
2247 it->second.text->init(bv);
2248 restoreLyXTextState(bv, it->second.text.get());
2249 it->second.remove = false;
2251 cached_text = it->second.text;
2252 if (the_locking_inset && recursive) {
2253 return the_locking_inset->getLyXText(bv);
2255 return cached_text.get();
2258 // we are here only if we don't have a BufferView * in the cache!!!
2260 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2261 cached_text->init(bv);
2262 restoreLyXTextState(bv, cached_text.get());
2264 cache.insert(make_pair(bv, cached_text));
2266 if (the_locking_inset && recursive) {
2267 return the_locking_inset->getLyXText(bv);
2269 return cached_text.get();
2273 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2277 Cache::iterator it = cache.find(bv);
2279 if (it == cache.end()) {
2283 lyx::Assert(it->second.text.get());
2285 it->second.remove = true;
2287 /// then remove all LyXText in text-insets
2288 for_each(paragraphs.begin(), paragraphs.end(),
2289 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2294 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2297 // we cannot resize this because we are in use!
2298 // so do this on the next possible getLyXText()
2303 // lyxerr << "InsetText::resizeLyXText\n";
2304 if (!paragraphs.begin()->next() && paragraphs.begin()->empty()) { // no data, resize not neccessary!
2305 // we have to do this as a fixed width may have changed!
2306 LyXText * t = getLyXText(bv);
2307 saveLyXTextState(t);
2309 restoreLyXTextState(bv, t);
2312 // one endless line, resize normally not necessary
2313 if (!force && getMaxWidth(bv, this) < 0)
2316 Cache::iterator it = cache.find(bv);
2317 if (it == cache.end()) {
2320 lyx::Assert(it->second.text.get());
2322 LyXText * t = it->second.text.get();
2323 saveLyXTextState(t);
2325 for_each(paragraphs.begin(), paragraphs.end(),
2326 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2329 restoreLyXTextState(bv, t);
2330 if (the_locking_inset) {
2331 inset_x = cix(bv) - top_x + drawTextXOffset;
2332 inset_y = ciy(bv) + drawTextYOffset;
2335 t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2337 updateLocal(bv, FULL, false);
2338 // this will scroll the screen such that the cursor becomes visible
2339 bv->updateScrollbar();
2341 need_update |= FULL;
2346 void InsetText::reinitLyXText() const
2349 // we cannot resize this because we are in use!
2350 // so do this on the next possible getLyXText()
2356 // lyxerr << "InsetText::reinitLyXText\n";
2357 for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2358 lyx::Assert(it->second.text.get());
2360 LyXText * t = it->second.text.get();
2361 BufferView * bv = it->first;
2363 saveLyXTextState(t);
2365 for_each(paragraphs.begin(), paragraphs.end(),
2366 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2369 restoreLyXTextState(bv, t);
2370 if (the_locking_inset) {
2371 inset_x = cix(bv) - top_x + drawTextXOffset;
2372 inset_y = ciy(bv) + drawTextYOffset;
2374 t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2376 updateLocal(bv, FULL, false);
2377 // this will scroll the screen such that the cursor becomes visible
2378 bv->updateScrollbar();
2386 void InsetText::removeNewlines()
2388 bool changed = false;
2390 ParagraphList::iterator it = paragraphs.begin();
2391 ParagraphList::iterator end = paragraphs.end();
2392 for (; it != end; ++it) {
2393 for (int i = 0; i < it->size(); ++i) {
2394 if (it->getChar(i) == Paragraph::META_NEWLINE) {
2405 bool InsetText::nodraw() const
2407 if (the_locking_inset)
2408 return the_locking_inset->nodraw();
2409 return UpdatableInset::nodraw();
2413 int InsetText::scroll(bool recursive) const
2415 int sx = UpdatableInset::scroll(false);
2417 if (recursive && the_locking_inset)
2418 sx += the_locking_inset->scroll(recursive);
2424 bool InsetText::doClearArea() const
2426 return !locked || (need_update & (FULL|INIT));
2430 void InsetText::selectAll(BufferView * bv)
2432 getLyXText(bv)->cursorTop(bv);
2433 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2434 getLyXText(bv)->cursorBottom(bv);
2435 getLyXText(bv)->setSelection(bv);
2439 void InsetText::clearSelection(BufferView * bv)
2441 getLyXText(bv)->clearSelection();
2445 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2447 Painter & pain = bv->painter();
2449 int h = insetAscent + insetDescent;
2450 int ty = baseline - insetAscent;
2456 if ((ty + h) > pain.paperHeight())
2457 h = pain.paperHeight();
2458 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2459 w = pain.paperWidth();
2460 // w -= TEXT_TO_INSET_OFFSET;
2461 pain.fillRectangle(top_x + 1, ty + 1, w - 1, h - 1, backgroundColor());
2464 frame_is_visible = false;
2468 Paragraph * InsetText::getParFromID(int id) const
2470 ParagraphList::iterator it = paragraphs.begin();
2471 ParagraphList::iterator end = paragraphs.end();
2472 for (; it != end; ++it) {
2473 if (it->id() == id) {
2476 Paragraph * tmp2 = it->getParFromID(id);
2485 Paragraph * InsetText::firstParagraph() const
2488 if (the_locking_inset)
2489 if ((result = the_locking_inset->firstParagraph()))
2491 return &*(paragraphs.begin());
2495 Paragraph * InsetText::getFirstParagraph(int i) const
2497 return (i == 0) ? &*(paragraphs.begin()) : 0;
2501 LyXCursor const & InsetText::cursor(BufferView * bv) const
2503 if (the_locking_inset)
2504 return the_locking_inset->cursor(bv);
2505 return getLyXText(bv)->cursor;
2509 Paragraph * InsetText::paragraph() const
2511 return &*(paragraphs.begin());
2515 void InsetText::paragraph(Paragraph * p)
2517 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2518 // caller of this function has to take care of it. This IS important
2519 // as we could have to insert a paragraph before this one and just
2520 // link the actual to a new ones next and set it with this function
2523 // set ourself as owner for all the paragraphs inserted!
2524 for_each(paragraphs.begin(), paragraphs.end(),
2525 boost::bind(&Paragraph::setInsetOwner, _1, this));
2528 // redraw myself when asked for
2533 Inset * InsetText::getInsetFromID(int id_arg) const
2536 return const_cast<InsetText *>(this);
2538 ParagraphList::iterator pit = paragraphs.begin();
2539 ParagraphList::iterator pend = paragraphs.end();
2540 for (; pit != pend; ++pit) {
2541 InsetList::iterator it = pit->insetlist.begin();
2542 InsetList::iterator end = pit->insetlist.end();
2543 for (; it != end; ++it) {
2544 if (it.getInset()->id() == id_arg)
2545 return it.getInset();
2546 Inset * in = it.getInset()->getInsetFromID(id_arg);
2556 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2557 float & value) const
2563 lt = getLyXText(bv);
2566 if (the_locking_inset) {
2567 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2568 if (!word.word().empty()) {
2574 // we have to go on checking so move cursor to the next char
2575 lt->cursor.pos(lt->cursor.pos() + 1);
2577 word = lt->selectNextWordToSpellcheck(bv, value);
2578 if (word.word().empty())
2579 bv->unlockInset(const_cast<InsetText *>(this));
2588 void InsetText::selectSelectedWord(BufferView * bv)
2590 if (the_locking_inset) {
2591 the_locking_inset->selectSelectedWord(bv);
2594 getLyXText(bv)->selectSelectedWord(bv);
2595 updateLocal(bv, SELECTION, false);
2599 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2601 if (the_locking_inset) {
2602 the_locking_inset->toggleSelection(bv, kill_selection);
2606 lt = getLyXText(bv);
2610 int x = top_x + TEXT_TO_INSET_OFFSET;
2612 Row * row = lt->firstRow();
2613 int y_offset = top_baseline - row->ascent_of_text();
2615 while ((row != 0) && ((y+row->height()) <= 0)) {
2622 if (need_update & SELECTION)
2624 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2630 bool InsetText::searchForward(BufferView * bv, string const & str,
2635 lt = getLyXText(bv);
2638 if (the_locking_inset) {
2639 if (the_locking_inset->searchForward(bv, str, cs, mw))
2641 lt->cursorRight(bv, true);
2643 lyxfind::SearchResult result =
2644 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2646 if (result == lyxfind::SR_FOUND) {
2647 LyXCursor cur = lt->cursor;
2648 bv->unlockInset(bv->theLockingInset());
2649 if (bv->lockInset(this))
2652 lt->setSelectionOverString(bv, str);
2653 updateLocal(bv, SELECTION, false);
2657 return (result != lyxfind::SR_NOT_FOUND);
2660 bool InsetText::searchBackward(BufferView * bv, string const & str,
2663 if (the_locking_inset) {
2664 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2669 lt = getLyXText(bv);
2673 Paragraph * p = &*(paragraphs.begin());
2676 lt->setCursor(bv, p, p->size());
2678 lyxfind::SearchResult result =
2679 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2681 if (result == lyxfind::SR_FOUND) {
2682 LyXCursor cur = lt->cursor;
2683 bv->unlockInset(bv->theLockingInset());
2684 if (bv->lockInset(this))
2687 lt->setSelectionOverString(bv, str);
2688 updateLocal(bv, SELECTION, false);
2692 return (result != lyxfind::SR_NOT_FOUND);
2696 bool InsetText::checkInsertChar(LyXFont & font)
2699 return owner()->checkInsertChar(font);
2704 void InsetText::collapseParagraphs(BufferView * bv) const
2706 BufferParams const & bparams = bv->buffer()->params;
2707 LyXText * llt = getLyXText(bv);
2709 while (paragraphs.begin()->next()) {
2710 if (!paragraphs.begin()->empty() && !paragraphs.begin()->next()->empty() &&
2711 !paragraphs.begin()->isSeparator(paragraphs.begin()->size() - 1))
2713 paragraphs.begin()->insertChar(paragraphs.begin()->size(), ' ');
2715 if (llt->selection.set()) {
2716 if (llt->selection.start.par() == paragraphs.begin()->next()) {
2717 llt->selection.start.par(&*(paragraphs.begin()));
2718 llt->selection.start.pos(
2719 llt->selection.start.pos() + paragraphs.begin()->size());
2721 if (llt->selection.end.par() == paragraphs.begin()->next()) {
2722 llt->selection.end.par(&*(paragraphs.begin()));
2723 llt->selection.end.pos(
2724 llt->selection.end.pos() + paragraphs.begin()->size());
2727 paragraphs.begin()->pasteParagraph(bparams);
2733 void InsetText::getDrawFont(LyXFont & font) const
2737 owner()->getDrawFont(font);
2741 void InsetText::appendParagraphs(BufferParams const & bparams,
2745 Paragraph * tmpbuf = newpar;
2746 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2748 while (tmpbuf->next()) {
2749 tmpbuf = tmpbuf->next();
2750 lastbuffer->next(new Paragraph(*tmpbuf, false));
2751 lastbuffer->next()->previous(lastbuffer);
2752 lastbuffer = lastbuffer->next();
2754 lastbuffer = &*(paragraphs.begin());
2755 while (lastbuffer->next())
2756 lastbuffer = lastbuffer->next();
2757 if (!newpar->empty() && !lastbuffer->empty() &&
2758 !lastbuffer->isSeparator(lastbuffer->size() - 1))
2760 lastbuffer->insertChar(lastbuffer->size(), ' ');
2763 // make the buf exactly the same layout than our last paragraph
2764 buf->makeSameLayout(lastbuffer);
2767 lastbuffer->next(buf);
2768 buf->previous(lastbuffer);
2769 lastbuffer->pasteParagraph(bparams);
2775 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2777 Paragraph * par = getFirstParagraph(0);
2779 InsetList::iterator it = par->insetlist.begin();
2780 InsetList::iterator end = par->insetlist.end();
2781 for (; it != end; ++it) {
2782 it.getInset()->addPreview(loader);