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"
36 #include "paragraph.h"
37 #include "ParagraphParameters.h"
38 #include "trans_mgr.h"
39 #include "undo_funcs.h"
40 #include "WordLangTuple.h"
42 #include "frontends/Alert.h"
43 #include "frontends/Dialogs.h"
44 #include "frontends/font_metrics.h"
45 #include "frontends/LyXView.h"
46 #include "frontends/Painter.h"
47 #include "frontends/screen.h"
49 #include "support/textutils.h"
50 #include "support/LAssert.h"
51 #include "support/lstrings.h"
52 #include "support/lyxalgo.h" // lyx::count
69 using lyx::textclass_type;
71 extern unsigned char getCurrentTextClass(Buffer *);
72 extern bool math_insert_greek(BufferView *, char);
73 extern int greek_kb_flag;
76 // These functions should probably go into bufferview_funcs somehow (Jug)
78 void InsetText::saveLyXTextState(LyXText * t) const
80 // check if my paragraphs are still valid
81 Paragraph * p = &*(paragraphs.begin());
83 if (p == t->cursor.par())
88 if (p && t->cursor.pos() <= p->size()) {
89 sstate.lpar = t->cursor.par();
90 sstate.pos = t->cursor.pos();
91 sstate.boundary = t->cursor.boundary();
92 sstate.selstartpar = t->selection.start.par();
93 sstate.selstartpos = t->selection.start.pos();
94 sstate.selstartboundary = t->selection.start.boundary();
95 sstate.selendpar = t->selection.end.par();
96 sstate.selendpos = t->selection.end.pos();
97 sstate.selendboundary = t->selection.end.boundary();
98 sstate.selection = t->selection.set();
99 sstate.mark_set = t->selection.mark();
100 sstate.refresh = t->refresh_row != 0;
106 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
109 t->selection.set(true);
110 /* at this point just to avoid the Delete-Empty-Paragraph
111 * Mechanism when setting the cursor */
112 t->selection.mark(sstate.mark_set);
113 if (sstate.selection) {
114 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
115 true, sstate.selstartboundary);
116 t->selection.cursor = t->cursor;
117 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
118 true, sstate.selendboundary);
120 t->setCursor(bv, sstate.lpar, sstate.pos);
122 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
123 t->selection.cursor = t->cursor;
124 t->selection.set(false);
126 if (sstate.refresh) {
132 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
139 InsetText::InsetText(BufferParams const & bp)
140 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
143 paragraphs.set(new Paragraph);
144 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
149 InsetText::InsetText(InsetText const & in, bool same_id)
150 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
157 InsetText & InsetText::operator=(InsetText const & it)
164 void InsetText::init(InsetText const * ins, bool same_id)
167 setParagraphData(&*(ins->paragraphs.begin()), same_id);
168 autoBreakRows = ins->autoBreakRows;
169 drawFrame_ = ins->drawFrame_;
170 frame_color = ins->frame_color;
174 Paragraph * p = &*(paragraphs.begin());
176 p->setInsetOwner(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()
213 void InsetText::clear()
215 // This is a gross hack...
216 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
219 paragraphs.set(new Paragraph);
220 paragraphs.begin()->setInsetOwner(this);
221 paragraphs.begin()->layout(old_layout);
228 Inset * InsetText::clone(Buffer const &, bool same_id) const
230 return new InsetText(*this, same_id);
234 void InsetText::write(Buffer const * buf, ostream & os) const
237 writeParagraphData(buf, os);
241 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
243 paragraphs.begin()->writeFile(buf, os, buf->params, 0);
247 void InsetText::read(Buffer const * buf, LyXLex & lex)
251 Paragraph * return_par = 0;
252 Paragraph::depth_type depth = 0;
253 LyXFont font(LyXFont::ALL_INHERIT);
259 token = lex.getString();
262 if (token == "\\end_inset") {
263 #ifndef NO_COMPABILITY
264 const_cast<Buffer*>(buf)->insertErtContents(par, pos, false);
269 Paragraph * tmp = &*(paragraphs.begin());
271 if (const_cast<Buffer*>(buf)->
272 parseSingleLyXformat2Token(lex, tmp, return_par,
273 token, pos, depth, font)) {
274 // the_end read this should NEVER happen
275 lex.printError("\\the_end read in inset! Error in document!");
280 return_par = &*(paragraphs.begin());
281 paragraphs.set(return_par);
283 return_par->setInsetOwner(this);
284 return_par = return_par->next();
287 if (token != "\\end_inset") {
288 lex.printError("Missing \\end_inset at this point. "
295 int InsetText::ascent(BufferView * bv, LyXFont const &) const
297 insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
298 TEXT_TO_INSET_OFFSET;
303 int InsetText::descent(BufferView * bv, LyXFont const &) const
305 LyXText * llt = getLyXText(bv);
306 insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
307 TEXT_TO_INSET_OFFSET;
312 int InsetText::width(BufferView * bv, LyXFont const &) const
314 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
315 (2 * TEXT_TO_INSET_OFFSET);
316 insetWidth = max(insetWidth, 10);
321 int InsetText::textWidth(BufferView * bv, bool fordraw) const
324 if (!autoBreakRows) {
327 w = getMaxWidth(bv, this);
330 return max(w - (2 * TEXT_TO_INSET_OFFSET),
331 (int)getLyXText(bv)->width);
335 return w - (2 * TEXT_TO_INSET_OFFSET);
339 void InsetText::draw(BufferView * bv, LyXFont const & f,
340 int baseline, float & x, bool cleared) const
345 Painter & pain = bv->painter();
347 // this is the first thing we have to ask because if the x pos
348 // changed we have to do a complete rebreak of the text as we
349 // may have few space to draw in. Well we should check on this too
351 if (top_x != int(x)) {
354 int nw = getMaxWidth(bv, this);
355 if (nw > 0 && old_max_width != nw) {
358 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
363 // call these methods so that insetWidth, insetAscent and
364 // insetDescent have the right values.
369 // repaint the background if needed
370 if (cleared && backgroundColor() != LColor::background) {
371 clearInset(bv, baseline, cleared);
374 // no draw is necessary !!!
375 if ((drawFrame_ == LOCKED) && !locked && paragraphs.begin()->empty()) {
376 top_baseline = baseline;
378 if (need_update & CLEAR_FRAME)
379 clearFrame(pain, cleared);
385 x += static_cast<float>(scroll());
387 // if top_x differs we did it already
388 if (!cleared && (old_x == int(x))
389 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
390 ||(last_drawn_width != insetWidth)))
392 // Condition necessary to eliminate bug 59 attachment 37
394 clearInset(bv, baseline, cleared);
398 frame_is_visible = false;
400 if (!cleared && (need_update == NONE)) {
402 drawFrame(pain, cleared);
406 top_baseline = baseline;
407 top_y = baseline - insetAscent;
409 if (last_drawn_width != insetWidth) {
411 clearInset(bv, baseline, cleared);
413 last_drawn_width = insetWidth;
416 if (the_locking_inset && (cpar(bv) == inset_par)
417 && (cpos(bv) == inset_pos)) {
418 inset_x = cix(bv) - top_x + drawTextXOffset;
419 inset_y = ciy(bv) + drawTextYOffset;
421 if (!cleared && (need_update == CURSOR)
422 && !getLyXText(bv)->selection.set()) {
423 drawFrame(pain, cleared);
433 x += TEXT_TO_INSET_OFFSET;
435 Row * row = lt->firstRow();
436 int y_offset = baseline - row->ascent_of_text();
437 int ph = pain.paperHeight();
440 while ((row != 0) && ((y+row->height()) <= 0)) {
442 first += row->height();
446 lt->first_y = -y_offset;
453 if (cleared || (need_update&(INIT|FULL))) {
454 int yf = y_offset + first;
456 while ((row != 0) && (yf < ph)) {
457 lt->getVisibleRow(bv, y + y_offset + first, int(x),
458 row, y + lt->first_y, cleared);
459 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
460 lt->need_break_row = row;
462 lt->setCursor(bv, lt->cursor.par(),
464 if (lt->selection.set()) {
465 lt->setCursor(bv, lt->selection.start,
466 lt->selection.start.par(),
467 lt->selection.start.pos());
468 lt->setCursor(bv, lt->selection.end,
469 lt->selection.end.par(),
470 lt->selection.end.pos());
478 } else if (!locked) {
479 if (need_update & CURSOR) {
480 bv->screen().toggleSelection(lt, bv, true, y_offset,int(x));
481 lt->clearSelection();
482 lt->selection.cursor = lt->cursor;
484 bv->screen().update(lt, bv, y_offset, int(x));
487 if (need_update & SELECTION) {
488 bv->screen().toggleToggle(lt, bv, y_offset, int(x));
489 } else if (need_update & CURSOR) {
490 bv->screen().toggleSelection(lt, bv, true, y_offset,int(x));
491 lt->clearSelection();
492 lt->selection.cursor = lt->cursor;
494 bv->screen().update(lt, bv, y_offset, int(x));
499 lt->status(bv, LyXText::UNCHANGED);
500 if ((drawFrame_ == ALWAYS) ||
501 ((cleared || (need_update != CURSOR_PAR)) &&
502 (drawFrame_ == LOCKED) && locked))
504 drawFrame(pain, cleared);
505 } else if (need_update & CLEAR_FRAME) {
506 clearFrame(pain, cleared);
509 x += insetWidth - TEXT_TO_INSET_OFFSET;
511 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
513 } else if (need_update != INIT) {
521 void InsetText::drawFrame(Painter & pain, bool cleared) const
523 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
524 if (!frame_is_visible || cleared) {
525 frame_x = top_x + ttoD2;
526 frame_y = top_baseline - insetAscent + ttoD2;
527 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
528 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
529 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
531 frame_is_visible = true;
536 void InsetText::clearFrame(Painter & pain, bool cleared) const
538 if (frame_is_visible) {
540 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
543 frame_is_visible = false;
548 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
551 if (reinit && owner()) {
553 owner()->update(bv, font, true);
558 if (reinit || need_update == INIT) {
560 // we should put this call where we set need_update to INIT!
563 owner()->update(bv, font, true);
568 if (!autoBreakRows && paragraphs.begin()->next())
569 collapseParagraphs(bv);
571 if (the_locking_inset) {
572 inset_x = cix(bv) - top_x + drawTextXOffset;
573 inset_y = ciy(bv) + drawTextYOffset;
574 the_locking_inset->update(bv, font, reinit);
582 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
585 lt->updateInset(bv, the_locking_inset);
587 if (lt->status() == LyXText::NEED_MORE_REFRESH)
595 void InsetText::setUpdateStatus(BufferView * bv, int what) const
597 // this does nothing dangerous so use only a localized buffer
598 LyXText * llt = getLyXText(bv);
601 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
602 // if we don't break row so that we only have one row to update!
603 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
605 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
608 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
609 need_update |= CURSOR_PAR;
612 // this to not draw a selection when we redraw all of it!
613 if (need_update & CURSOR && !(need_update & SELECTION)) {
614 if (llt->selection.set())
616 llt->clearSelection();
621 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
623 if (!autoBreakRows && paragraphs.begin()->next())
624 collapseParagraphs(bv);
631 setUpdateStatus(bv, what);
632 bool flag = mark_dirty ||
633 (((need_update != CURSOR) && (need_update != NONE)) ||
634 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
635 if (!lt->selection.set())
636 lt->selection.cursor = lt->cursor;
640 // IMO this is not anymore needed as we do this in fitInsetCursor!
641 // and we always get "true" as returnvalue of this function in the
642 // case of a locking inset (Jug 20020412)
643 if (locked && (need_update & CURSOR) && bv->fitCursor())
649 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
651 if (need_update == CURSOR)
653 bv->owner()->view_state_changed();
654 bv->owner()->updateMenubar();
655 bv->owner()->updateToolbar();
656 if (old_par != cpar(bv)) {
657 bv->owner()->setLayout(cpar(bv)->layout()->name());
663 string const InsetText::editMessage() const
665 return _("Opened Text Inset");
669 void InsetText::edit(BufferView * bv, int x, int y, mouse_button::state button)
671 UpdatableInset::edit(bv, x, y, button);
673 if (!bv->lockInset(this)) {
674 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
678 the_locking_inset = 0;
679 inset_pos = inset_x = inset_y = 0;
680 inset_boundary = false;
683 int tmp_y = (y < 0) ? 0 : y;
689 // we put here -1 and not button as now the button in the
690 // edit call should not be needed we will fix this in 1.3.x
691 // cycle hopefully (Jug 20020509)
692 // FIXME: GUII I've changed this to none: probably WRONG
693 if (!checkAndActivateInset(bv, x, tmp_y, mouse_button::none)) {
694 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
696 lt->cursor.x_fix(lt->cursor.x());
698 lt->clearSelection();
700 // If the inset is empty set the language of the current font to the
701 // language to the surronding text (if different).
702 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
703 bv->getParentLanguage(this) != lt->current_font.language())
705 LyXFont font(LyXFont::ALL_IGNORE);
706 font.setLanguage(bv->getParentLanguage(this));
707 setFont(bv, font, false);
713 if (drawFrame_ == LOCKED)
714 code = CURSOR|DRAW_FRAME;
715 updateLocal(bv, code, false);
718 // Tell the paragraph dialog that we've entered an insettext.
719 bv->owner()->getDialogs().updateParagraph();
723 void InsetText::edit(BufferView * bv, bool front)
725 UpdatableInset::edit(bv, front);
727 if (!bv->lockInset(this)) {
728 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
732 the_locking_inset = 0;
733 inset_pos = inset_x = inset_y = 0;
734 inset_boundary = false;
743 lt->setCursor(bv, &*(paragraphs.begin()), 0);
745 Paragraph * p = &*(paragraphs.begin());
748 // int const pos = (p->size() ? p->size()-1 : p->size());
749 lt->setCursor(bv, p, p->size());
751 lt->clearSelection();
753 // If the inset is empty set the language of the current font to the
754 // language to the surronding text (if different).
755 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
756 bv->getParentLanguage(this) != lt->current_font.language()) {
757 LyXFont font(LyXFont::ALL_IGNORE);
758 font.setLanguage(bv->getParentLanguage(this));
759 setFont(bv, font, false);
764 if (drawFrame_ == LOCKED)
765 code = CURSOR|DRAW_FRAME;
766 updateLocal(bv, code, false);
771 void InsetText::insetUnlock(BufferView * bv)
773 if (the_locking_inset) {
774 the_locking_inset->insetUnlock(bv);
775 the_locking_inset = 0;
776 updateLocal(bv, CURSOR_PAR, false);
783 if (drawFrame_ == LOCKED)
784 code = CURSOR|CLEAR_FRAME;
788 if (drawFrame_ == LOCKED)
796 if (lt->selection.set()) {
797 lt->clearSelection();
799 } else if (owner()) {
800 bv->owner()->setLayout(owner()->getLyXText(bv)
801 ->cursor.par()->layout()->name());
803 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
804 // hack for deleteEmptyParMech
805 if (!paragraphs.begin()->empty()) {
806 lt->setCursor(bv, &*(paragraphs.begin()), 0);
807 } else if (paragraphs.begin()->next()) {
808 lt->setCursor(bv, paragraphs.begin()->next(), 0);
813 updateLocal(bv, code, false);
816 setUpdateStatus(bv, code);
821 void InsetText::lockInset(BufferView * bv)
824 the_locking_inset = 0;
825 inset_pos = inset_x = inset_y = 0;
826 inset_boundary = false;
834 lt->setCursor(bv, &*(paragraphs.begin()), 0);
835 lt->clearSelection();
837 // If the inset is empty set the language of the current font to the
838 // language to the surronding text (if different).
839 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
840 bv->getParentLanguage(this) != lt->current_font.language()) {
841 LyXFont font(LyXFont::ALL_IGNORE);
842 font.setLanguage(bv->getParentLanguage(this));
843 setFont(bv, font, false);
848 if (drawFrame_ == LOCKED)
849 code = CURSOR|DRAW_FRAME;
850 setUpdateStatus(bv, code);
854 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
856 the_locking_inset = inset;
857 inset_x = cix(bv) - top_x + drawTextXOffset;
858 inset_y = ciy(bv) + drawTextYOffset;
859 inset_pos = cpos(bv);
860 inset_par = cpar(bv);
861 inset_boundary = cboundary(bv);
862 updateLocal(bv, CURSOR, false);
866 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
868 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
872 if (!the_locking_inset) {
873 Paragraph * p = &*(paragraphs.begin());
874 int const id = inset->id();
876 InsetList::iterator it =
877 p->insetlist.begin();
878 InsetList::iterator const end =
880 for (; it != end; ++it) {
881 if (it.getInset() == inset) {
882 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
883 lockInset(bv, inset);
886 if (it.getInset()->getInsetFromID(id)) {
887 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
888 it.getInset()->edit(bv);
889 return the_locking_inset->lockInsetInInset(bv, inset);
896 if (inset == cpar(bv)->getInset(cpos(bv))) {
897 lyxerr[Debug::INSETS] << "OK" << endl;
898 lockInset(bv, inset);
900 } else if (the_locking_inset && (the_locking_inset == inset)) {
901 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
902 lyxerr[Debug::INSETS] << "OK" << endl;
903 inset_x = cix(bv) - top_x + drawTextXOffset;
904 inset_y = ciy(bv) + drawTextYOffset;
906 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
908 } else if (the_locking_inset) {
909 lyxerr[Debug::INSETS] << "MAYBE" << endl;
910 return the_locking_inset->lockInsetInInset(bv, inset);
912 lyxerr[Debug::INSETS] << "NOT OK" << endl;
917 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
920 if (!the_locking_inset)
922 if (the_locking_inset == inset) {
923 the_locking_inset->insetUnlock(bv);
924 getLyXText(bv)->updateInset(bv, inset);
925 the_locking_inset = 0;
927 moveRightIntern(bv, true, false);
928 old_par = 0; // force layout setting
932 updateLocal(bv, CURSOR, false);
935 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
939 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
941 if (!autoBreakRows && paragraphs.begin()->next())
942 collapseParagraphs(bv);
950 if (inset->owner() != this) {
951 int ustat = CURSOR_PAR;
953 UpdatableInset * tl_inset = the_locking_inset;
955 found = tl_inset->updateInsetInInset(bv, inset);
957 tl_inset = static_cast<UpdatableInset *>(inset);
958 while(tl_inset->owner() && tl_inset->owner() != this)
959 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
960 if (!tl_inset->owner())
962 found = tl_inset->updateInsetInInset(bv, inset);
966 lt->updateInset(bv, tl_inset);
970 setUpdateStatus(bv, ustat);
973 bool found = lt->updateInset(bv, inset);
977 setUpdateStatus(bv, CURSOR_PAR);
978 if (the_locking_inset &&
979 cpar(bv) == inset_par && cpos(bv) == inset_pos)
981 inset_x = cix(bv) - top_x + drawTextXOffset;
982 inset_y = ciy(bv) + drawTextYOffset;
989 void InsetText::insetButtonPress(BufferView * bv,
990 int x, int y, mouse_button::state button)
994 // use this to check mouse motion for selection!
1001 int tmp_x = x - drawTextXOffset;
1002 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1003 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1005 hideInsetCursor(bv);
1006 if (the_locking_inset) {
1007 if (the_locking_inset == inset) {
1008 the_locking_inset->insetButtonPress(bv,
1016 // otherwise unlock the_locking_inset and lock the new inset
1017 the_locking_inset->insetUnlock(bv);
1018 inset_x = cix(bv) - top_x + drawTextXOffset;
1019 inset_y = ciy(bv) + drawTextYOffset;
1020 the_locking_inset = 0;
1021 inset->insetButtonPress(bv, x - inset_x,
1022 y - inset_y, button);
1023 // inset->edit(bv, x - inset_x, y - inset_y, button);
1024 if (the_locking_inset)
1025 updateLocal(bv, CURSOR, false);
1029 // otherwise only unlock the_locking_inset
1030 the_locking_inset->insetUnlock(bv);
1031 the_locking_inset = 0;
1034 no_selection = false;
1036 if (bv->theLockingInset()) {
1037 if (isHighlyEditableInset(inset)) {
1038 // We just have to lock the inset before calling a
1039 // PressFuncRequest on it!
1040 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
1041 if (!bv->lockInset(uinset)) {
1042 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1044 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
1045 if (the_locking_inset)
1046 updateLocal(bv, CURSOR, false);
1050 if (!inset) { // && (button == mouse_button::button2)) {
1051 bool paste_internally = false;
1052 if ((button == mouse_button::button2) && getLyXText(bv)->selection.set()) {
1053 localDispatch(bv, FuncRequest(LFUN_COPY));
1054 paste_internally = true;
1058 lt = getLyXText(bv);
1061 int old_first_y = lt->first_y;
1063 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
1065 // set the selection cursor!
1066 lt->selection.cursor = lt->cursor;
1067 lt->cursor.x_fix(lt->cursor.x());
1069 if (lt->selection.set()) {
1070 lt->clearSelection();
1073 updateLocal(bv, FULL, false);
1075 lt->clearSelection();
1078 updateLocal(bv, CURSOR, false);
1081 bv->owner()->setLayout(cpar(bv)->layout()->name());
1083 // we moved the view we cannot do mouse selection in this case!
1084 if (getLyXText(bv)->first_y != old_first_y)
1085 no_selection = true;
1087 // Insert primary selection with middle mouse
1088 // if there is a local selection in the current buffer,
1090 if (button == mouse_button::button2) {
1091 if (paste_internally)
1092 localDispatch(bv, FuncRequest(LFUN_PASTE));
1094 localDispatch(bv, FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1097 getLyXText(bv)->clearSelection();
1099 showInsetCursor(bv);
1103 bool InsetText::insetButtonRelease(BufferView * bv,
1104 int x, int y, mouse_button::state button)
1106 no_selection = true;
1107 if (the_locking_inset) {
1108 return the_locking_inset->insetButtonRelease(bv,
1109 x - inset_x, y - inset_y,
1112 int tmp_x = x - drawTextXOffset;
1113 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1114 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1117 if (isHighlyEditableInset(inset)) {
1118 ret = inset->insetButtonRelease(bv, x - inset_x,
1119 y - inset_y, button);
1121 inset_x = cix(bv) - top_x + drawTextXOffset;
1122 inset_y = ciy(bv) + drawTextYOffset;
1123 ret = inset->insetButtonRelease(bv, x - inset_x,
1124 y - inset_y, button);
1125 inset->edit(bv, x - inset_x,
1126 y - inset_y, button);
1128 updateLocal(bv, CURSOR_PAR, false);
1134 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, mouse_button::state state)
1136 if (the_locking_inset) {
1137 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1142 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1147 lt = getLyXText(bv);
1150 hideInsetCursor(bv);
1151 LyXCursor cur = lt->cursor;
1152 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1153 lt->cursor.x_fix(lt->cursor.x());
1154 if (cur == lt->cursor) {
1159 lt->setSelection(bv);
1160 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1161 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1165 updateLocal(bv, SELECTION, false);
1167 showInsetCursor(bv);
1171 UpdatableInset::RESULT
1172 InsetText::localDispatch(BufferView * bv, FuncRequest const & ev)
1174 bool was_empty = (paragraphs.begin()->empty() && !paragraphs.begin()->next());
1175 no_selection = false;
1176 UpdatableInset::RESULT
1177 result= UpdatableInset::localDispatch(bv, ev);
1178 if (result != UNDISPATCHED)
1181 result = DISPATCHED;
1182 if (ev.action < 0 && ev.argument.empty())
1185 if (the_locking_inset) {
1186 result = the_locking_inset->localDispatch(bv, ev);
1187 if (result == DISPATCHED_NOUPDATE)
1189 else if (result == DISPATCHED) {
1190 updateLocal(bv, CURSOR_PAR, false);
1192 } else if (result >= FINISHED) {
1194 case FINISHED_RIGHT:
1195 moveRightIntern(bv, false, false);
1196 result = DISPATCHED;
1199 if ((result = moveUp(bv)) >= FINISHED) {
1200 updateLocal(bv, CURSOR, false);
1201 bv->unlockInset(this);
1206 LyXText *lt = getLyXText(bv);
1207 if (lt->cursor.irow()->next()) {
1208 lt->setCursorFromCoordinates(
1209 bv, lt->cursor.ix() + inset_x,
1211 lt->cursor.irow()->baseline() +
1212 lt->cursor.irow()->height() + 1);
1213 lt->cursor.x_fix(lt->cursor.x());
1214 updateLocal(bv, CURSOR, false);
1216 bv->unlockInset(this);
1221 result = DISPATCHED;
1224 the_locking_inset = 0;
1225 updateLocal(bv, CURSOR, false);
1229 hideInsetCursor(bv);
1232 lt = getLyXText(bv);
1236 int updflag = false;
1237 switch (ev.action) {
1239 case LFUN_SELFINSERT:
1240 if (bv->buffer()->isReadonly()) {
1241 // setErrorMessage(N_("Document is read only"));
1244 if (!ev.argument.empty()) {
1245 /* Automatically delete the currently selected
1246 * text and replace it with what is being
1247 * typed in now. Depends on lyxrc settings
1248 * "auto_region_delete", which defaults to
1251 setUndo(bv, Undo::INSERT,
1252 lt->cursor.par(), lt->cursor.par()->next());
1254 if (lyxrc.auto_region_delete) {
1255 if (lt->selection.set()) {
1256 lt->cutSelection(bv, false, false);
1259 lt->clearSelection();
1260 for (string::size_type i = 0; i < ev.argument.length(); ++i) {
1261 bv->owner()->getIntl().getTransManager().
1262 TranslateAndInsert(ev.argument[i], lt);
1265 lt->selection.cursor = lt->cursor;
1266 updwhat = CURSOR | CURSOR_PAR;
1268 result = DISPATCHED_NOUPDATE;
1270 // --- Cursor Movements -----------------------------------
1273 moveRight(bv, false, true);
1274 lt->setSelection(bv);
1275 updwhat = SELECTION;
1278 result = moveRight(bv);
1284 moveLeft(bv, false, true);
1285 lt->setSelection(bv);
1286 updwhat = SELECTION;
1290 result = moveLeft(bv);
1296 lt->setSelection(bv);
1297 updwhat = SELECTION;
1301 result = moveDown(bv);
1307 lt->setSelection(bv);
1308 updwhat = SELECTION;
1312 result = moveUp(bv);
1324 case LFUN_BACKSPACE: {
1325 setUndo(bv, Undo::DELETE,
1326 lt->cursor.par(), lt->cursor.par()->next());
1327 if (lt->selection.set())
1328 lt->cutSelection(bv, true, false);
1331 updwhat = CURSOR_PAR;
1337 setUndo(bv, Undo::DELETE,
1338 lt->cursor.par(), lt->cursor.par()->next());
1339 if (lt->selection.set()) {
1340 lt->cutSelection(bv, true, false);
1344 updwhat = CURSOR_PAR;
1350 setUndo(bv, Undo::DELETE,
1351 lt->cursor.par(), lt->cursor.par()->next());
1352 lt->cutSelection(bv);
1353 updwhat = CURSOR_PAR;
1360 lt->copySelection(bv);
1361 updwhat = CURSOR_PAR;
1363 case LFUN_PASTESELECTION:
1365 string const clip(bv->getClipboard());
1369 if (ev.argument == "paragraph") {
1370 lt->insertStringAsParagraphs(bv, clip);
1372 lt->insertStringAsLines(bv, clip);
1375 lt->clearSelection();
1377 updwhat = CURSOR_PAR;
1382 if (!autoBreakRows) {
1384 if (CutAndPaste::nrOfParagraphs() > 1) {
1385 Alert::alert(_("Impossible operation"),
1386 _("Cannot include more than one paragraph!"),
1391 setUndo(bv, Undo::INSERT,
1392 lt->cursor.par(), lt->cursor.par()->next());
1393 lt->pasteSelection(bv);
1395 lt->clearSelection();
1396 updwhat = CURSOR_PAR;
1401 case LFUN_BREAKPARAGRAPH:
1402 if (!autoBreakRows) {
1403 result = DISPATCHED;
1406 lt->breakParagraph(bv, 0);
1407 updwhat = CURSOR | FULL;
1410 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1411 if (!autoBreakRows) {
1412 result = DISPATCHED;
1415 lt->breakParagraph(bv, 1);
1416 updwhat = CURSOR | FULL;
1420 case LFUN_BREAKLINE: {
1421 if (!autoBreakRows) {
1422 result = DISPATCHED;
1425 setUndo(bv, Undo::INSERT,
1426 lt->cursor.par(), lt->cursor.par()->next());
1427 lt->insertChar(bv, Paragraph::META_NEWLINE);
1428 updwhat = CURSOR | CURSOR_PAR;
1434 // do not set layouts on non breakable textinsets
1435 if (autoBreakRows) {
1436 string cur_layout = cpar(bv)->layout()->name();
1438 // Derive layout number from given argument (string)
1439 // and current buffer's textclass (number). */
1440 LyXTextClass const & tclass =
1441 bv->buffer()->params.getLyXTextClass();
1442 string layout = ev.argument;
1443 bool hasLayout = tclass.hasLayout(layout);
1445 // If the entry is obsolete, use the new one instead.
1447 string const & obs =
1448 tclass[layout]->obsoleted_by();
1453 // see if we found the layout number:
1455 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + ev.argument + N_(" not known"));
1456 bv->owner()->getLyXFunc().dispatch(lf);
1460 if (cur_layout != layout) {
1461 cur_layout = layout;
1462 lt->setLayout(bv, layout);
1463 bv->owner()->setLayout(cpar(bv)->layout()->name());
1464 updwhat = CURSOR_PAR;
1468 // reset the layout box
1469 bv->owner()->setLayout(cpar(bv)->layout()->name());
1472 case LFUN_PARAGRAPH_SPACING:
1473 // This one is absolutely not working. When fiddling with this
1474 // it also seems to me that the paragraphs inside the insettext
1475 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1477 Paragraph * par = lt->cursor.par();
1478 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1479 float cur_value = 1.0;
1480 if (cur_spacing == Spacing::Other) {
1481 cur_value = par->params().spacing().getValue();
1484 istringstream istr(ev.argument.c_str());
1487 Spacing::Space new_spacing = cur_spacing;
1488 float new_value = cur_value;
1490 lyxerr << "Missing argument to `paragraph-spacing'"
1492 } else if (tmp == "single") {
1493 new_spacing = Spacing::Single;
1494 } else if (tmp == "onehalf") {
1495 new_spacing = Spacing::Onehalf;
1496 } else if (tmp == "double") {
1497 new_spacing = Spacing::Double;
1498 } else if (tmp == "other") {
1499 new_spacing = Spacing::Other;
1502 lyxerr << "new_value = " << tmpval << endl;
1505 } else if (tmp == "default") {
1506 new_spacing = Spacing::Default;
1508 lyxerr << _("Unknown spacing argument: ")
1509 << ev.argument << endl;
1511 if (cur_spacing != new_spacing || cur_value != new_value) {
1512 par->params().spacing(Spacing(new_spacing, new_value));
1513 updwhat = CURSOR_PAR;
1520 if (!bv->dispatch(ev))
1521 result = UNDISPATCHED;
1528 updateLocal(bv, updwhat, updflag);
1529 /// If the action has deleted all text in the inset, we need to change the
1530 // language to the language of the surronding text.
1531 if (!was_empty && paragraphs.begin()->empty() && !paragraphs.begin()->next()) {
1532 LyXFont font(LyXFont::ALL_IGNORE);
1533 font.setLanguage(bv->getParentLanguage(this));
1534 setFont(bv, font, false);
1537 if (result >= FINISHED)
1538 bv->unlockInset(this);
1540 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1541 result = DISPATCHED;
1546 int InsetText::latex(Buffer const * buf, ostream & os, bool moving_arg, bool) const
1549 buf->latexParagraphs(os, &*(paragraphs.begin()), 0, texrow, moving_arg);
1550 return texrow.rows();
1554 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1556 Paragraph * p = &*(paragraphs.begin());
1557 unsigned int lines = 0;
1560 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1561 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1569 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1571 Paragraph * p = &*(paragraphs.begin());
1572 unsigned int lines = 0;
1574 vector<string> environment_stack(10);
1575 vector<string> environment_inner(10);
1577 int const command_depth = 0;
1580 Paragraph::depth_type depth = 0; // paragraph depth
1584 int desc_on = 0; // description mode
1586 LyXLayout_ptr const & style = p->layout();
1588 // environment tag closing
1589 for (; depth > p->params().depth(); --depth) {
1590 if (environment_inner[depth] != "!-- --") {
1591 item_name = "listitem";
1592 lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
1593 if (environment_inner[depth] == "varlistentry")
1594 lines += buf->sgmlCloseTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1596 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1597 environment_stack[depth].erase();
1598 environment_inner[depth].erase();
1601 if (depth == p->params().depth()
1602 && environment_stack[depth] != style->latexname()
1603 && !environment_stack[depth].empty()) {
1604 if (environment_inner[depth] != "!-- --") {
1605 item_name= "listitem";
1606 lines += buf->sgmlCloseTag(os, command_depth+depth, mixcont, item_name);
1607 if (environment_inner[depth] == "varlistentry")
1608 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1611 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1613 environment_stack[depth].erase();
1614 environment_inner[depth].erase();
1617 // Write opening SGML tags.
1618 switch (style->latextype) {
1619 case LATEX_PARAGRAPH:
1620 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style->latexname());
1624 buf->sgmlError(p, 0, _("Error : LatexType Command not allowed here.\n"));
1628 case LATEX_ENVIRONMENT:
1629 case LATEX_ITEM_ENVIRONMENT:
1630 if (depth < p->params().depth()) {
1631 depth = p->params().depth();
1632 environment_stack[depth].erase();
1635 if (environment_stack[depth] != style->latexname()) {
1636 if (environment_stack.size() == depth + 1) {
1637 environment_stack.push_back("!-- --");
1638 environment_inner.push_back("!-- --");
1640 environment_stack[depth] = style->latexname();
1641 environment_inner[depth] = "!-- --";
1642 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1644 if (environment_inner[depth] != "!-- --") {
1645 item_name= "listitem";
1646 lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
1647 if (environment_inner[depth] == "varlistentry")
1648 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1652 if (style->latextype == LATEX_ENVIRONMENT) {
1653 if (!style->latexparam().empty()) {
1654 if (style->latexparam() == "CDATA")
1657 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style->latexparam());
1662 desc_on = (style->labeltype == LABEL_MANUAL);
1664 environment_inner[depth] = desc_on?"varlistentry":"listitem";
1665 lines += buf->sgmlOpenTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1667 item_name = desc_on?"term":"para";
1668 lines += buf->sgmlOpenTag(os, depth + 1 + command_depth, mixcont, item_name);
1672 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style->latexname());
1676 buf->simpleDocBookOnePar(os, p, desc_on, depth + 1 + command_depth);
1680 // write closing SGML tags
1681 switch (style->latextype) {
1682 case LATEX_ENVIRONMENT:
1683 if (!style->latexparam().empty()) {
1684 if (style->latexparam() == "CDATA")
1687 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style->latexparam());
1690 case LATEX_ITEM_ENVIRONMENT:
1691 if (desc_on == 1) break;
1693 lines += buf->sgmlCloseTag(os, depth + 1 + command_depth, mixcont, end_tag);
1695 case LATEX_PARAGRAPH:
1696 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style->latexname());
1699 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style->latexname());
1705 for (int d = depth; d >= 0; --d) {
1706 if (!environment_stack[depth].empty()) {
1707 if (environment_inner[depth] != "!-- --") {
1708 item_name = "listitem";
1709 lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
1710 if (environment_inner[depth] == "varlistentry")
1711 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1714 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1722 void InsetText::validate(LaTeXFeatures & features) const
1724 Paragraph * p = &*(paragraphs.begin());
1726 p->validate(features);
1732 int InsetText::beginningOfMainBody(Paragraph * p) const
1734 if (p->layout()->labeltype != LABEL_MANUAL)
1737 return p->beginningOfMainBody();
1741 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1743 if (the_locking_inset) {
1744 the_locking_inset->getCursorPos(bv, x, y);
1747 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1748 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1752 int InsetText::insetInInsetY() const
1754 if (!the_locking_inset)
1757 return (inset_y + the_locking_inset->insetInInsetY());
1761 void InsetText::toggleInsetCursor(BufferView * bv)
1763 if (the_locking_inset) {
1764 the_locking_inset->toggleInsetCursor(bv);
1768 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1770 int const asc = font_metrics::maxAscent(font);
1771 int const desc = font_metrics::maxDescent(font);
1773 if (isCursorVisible())
1774 bv->hideLockedInsetCursor();
1776 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1777 toggleCursorVisible();
1781 void InsetText::showInsetCursor(BufferView * bv, bool show)
1783 if (the_locking_inset) {
1784 the_locking_inset->showInsetCursor(bv, show);
1787 if (!isCursorVisible()) {
1788 LyXFont const font =
1789 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1791 int const asc = font_metrics::maxAscent(font);
1792 int const desc = font_metrics::maxDescent(font);
1794 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1796 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1797 setCursorVisible(true);
1802 void InsetText::hideInsetCursor(BufferView * bv)
1804 if (isCursorVisible()) {
1805 bv->hideLockedInsetCursor();
1806 setCursorVisible(false);
1808 if (the_locking_inset)
1809 the_locking_inset->hideInsetCursor(bv);
1813 void InsetText::fitInsetCursor(BufferView * bv) const
1815 if (the_locking_inset) {
1816 the_locking_inset->fitInsetCursor(bv);
1819 LyXFont const font =
1820 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1822 int const asc = font_metrics::maxAscent(font);
1823 int const desc = font_metrics::maxDescent(font);
1825 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1826 need_update |= FULL;
1830 UpdatableInset::RESULT
1831 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1833 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1834 return moveLeftIntern(bv, false, activate_inset, selecting);
1836 return moveRightIntern(bv, true, activate_inset, selecting);
1840 UpdatableInset::RESULT
1841 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1843 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1844 return moveRightIntern(bv, true, activate_inset, selecting);
1846 return moveLeftIntern(bv, false, activate_inset, selecting);
1850 UpdatableInset::RESULT
1851 InsetText::moveRightIntern(BufferView * bv, bool front,
1852 bool activate_inset, bool selecting)
1854 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1855 return FINISHED_RIGHT;
1856 if (activate_inset && checkAndActivateInset(bv, front))
1858 getLyXText(bv)->cursorRight(bv);
1860 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1861 return DISPATCHED_NOUPDATE;
1865 UpdatableInset::RESULT
1866 InsetText::moveLeftIntern(BufferView * bv, bool front,
1867 bool activate_inset, bool selecting)
1869 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1871 getLyXText(bv)->cursorLeft(bv);
1873 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1874 if (activate_inset && checkAndActivateInset(bv, front))
1876 return DISPATCHED_NOUPDATE;
1880 UpdatableInset::RESULT
1881 InsetText::moveUp(BufferView * bv)
1883 if (!crow(bv)->previous())
1885 getLyXText(bv)->cursorUp(bv);
1886 return DISPATCHED_NOUPDATE;
1890 UpdatableInset::RESULT
1891 InsetText::moveDown(BufferView * bv)
1893 if (!crow(bv)->next())
1894 return FINISHED_DOWN;
1895 getLyXText(bv)->cursorDown(bv);
1896 return DISPATCHED_NOUPDATE;
1900 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1902 if (the_locking_inset) {
1903 if (the_locking_inset->insetAllowed(inset))
1904 return the_locking_inset->insertInset(bv, inset);
1907 inset->setOwner(this);
1908 hideInsetCursor(bv);
1909 getLyXText(bv)->insertInset(bv, inset);
1911 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1916 bool InsetText::insetAllowed(Inset::Code code) const
1918 // in_insetAllowed is a really gross hack,
1919 // to allow us to call the owner's insetAllowed
1920 // without stack overflow, which can happen
1921 // when the owner uses InsetCollapsable::insetAllowed()
1923 if (in_insetAllowed)
1925 in_insetAllowed = true;
1926 if (the_locking_inset)
1927 ret = the_locking_inset->insetAllowed(code);
1929 ret = owner()->insetAllowed(code);
1930 in_insetAllowed = false;
1935 UpdatableInset * InsetText::getLockingInset() const
1937 return the_locking_inset ? the_locking_inset->getLockingInset() :
1938 const_cast<InsetText *>(this);
1942 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1946 if (the_locking_inset)
1947 return the_locking_inset->getFirstLockingInsetOfType(c);
1952 bool InsetText::showInsetDialog(BufferView * bv) const
1954 if (the_locking_inset)
1955 return the_locking_inset->showInsetDialog(bv);
1960 vector<string> const InsetText::getLabelList() const
1962 vector<string> label_list;
1964 Paragraph * tpar = &*(paragraphs.begin());
1966 InsetList::iterator beg = tpar->insetlist.begin();
1967 InsetList::iterator end = tpar->insetlist.end();
1968 for (; beg != end; ++beg) {
1969 vector<string> const l = beg.getInset()->getLabelList();
1970 label_list.insert(label_list.end(), l.begin(), l.end());
1972 tpar = tpar->next();
1978 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1981 if (the_locking_inset) {
1982 the_locking_inset->setFont(bv, font, toggleall, selectall);
1985 if ((!paragraphs.begin()->next() && paragraphs.begin()->empty()) || cpar(bv)->empty()) {
1986 getLyXText(bv)->setFont(bv, font, toggleall);
1991 lt = getLyXText(bv);
1994 if (lt->selection.set()) {
1995 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1999 lt->toggleFree(bv, font, toggleall);
2001 lt->clearSelection();
2003 bool flag = (selectall || lt->selection.set());
2007 updateLocal(bv, FULL, true);
2009 updateLocal(bv, CURSOR_PAR, true);
2013 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
2015 if (cpar(bv)->isInset(cpos(bv))) {
2017 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
2018 if (!isHighlyEditableInset(inset))
2020 inset->edit(bv, front);
2021 if (!the_locking_inset)
2023 updateLocal(bv, CURSOR, false);
2030 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2031 mouse_button::state button)
2033 x -= drawTextXOffset;
2035 int dummyy = y + insetAscent;
2036 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
2037 // we only do the edit() call if the inset was hit by the mouse
2038 // or if it is a highly editable inset. So we should call this
2039 // function from our own edit with button < 0.
2040 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
2042 if (button == mouse_button::none && !isHighlyEditableInset(inset))
2050 inset_x = cix(bv) - top_x + drawTextXOffset;
2051 inset_y = ciy(bv) + drawTextYOffset;
2052 inset->edit(bv, x - inset_x, y - inset_y, button);
2053 if (!the_locking_inset)
2055 updateLocal(bv, CURSOR, false);
2062 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2065 int w = UpdatableInset::getMaxWidth(bv, inset);
2070 w = w - top_x + owner()->x();
2073 w -= (2 * TEXT_TO_INSET_OFFSET);
2076 return UpdatableInset::getMaxWidth(bv, inset);
2081 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2083 // we have to unlock any locked inset otherwise we're in troubles
2084 the_locking_inset = 0;
2087 paragraphs.set(new Paragraph(*p, same_id));
2088 paragraphs.begin()->setInsetOwner(this);
2089 Paragraph * np = &*(paragraphs.begin());
2092 np->next(new Paragraph(*p, same_id));
2093 np->next()->previous(np);
2095 np->setInsetOwner(this);
2102 void InsetText::setText(string const & data, LyXFont const & font)
2105 for (unsigned int i = 0; i < data.length(); ++i)
2106 paragraphs.begin()->insertChar(i, data[i], font);
2111 void InsetText::setAutoBreakRows(bool flag)
2113 if (flag != autoBreakRows) {
2114 autoBreakRows = flag;
2122 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2124 if (how != drawFrame_) {
2127 updateLocal(bv, DRAW_FRAME, false);
2132 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2134 if (frame_color != col) {
2137 updateLocal(bv, DRAW_FRAME, false);
2142 int InsetText::cx(BufferView * bv) const
2144 // we do nothing dangerous so we use a local cache
2145 LyXText * llt = getLyXText(bv);
2146 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2147 if (the_locking_inset) {
2148 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2150 if (font.isVisibleRightToLeft())
2151 x -= the_locking_inset->width(bv, font);
2157 int InsetText::cix(BufferView * bv) const
2159 // we do nothing dangerous so we use a local cache
2160 LyXText * llt = getLyXText(bv);
2161 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2162 if (the_locking_inset) {
2163 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2165 if (font.isVisibleRightToLeft())
2166 x -= the_locking_inset->width(bv, font);
2172 int InsetText::cy(BufferView * bv) const
2175 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2179 int InsetText::ciy(BufferView * bv) const
2182 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2186 pos_type InsetText::cpos(BufferView * bv) const
2188 return getLyXText(bv)->cursor.pos();
2192 Paragraph * InsetText::cpar(BufferView * bv) const
2194 return getLyXText(bv)->cursor.par();
2198 bool InsetText::cboundary(BufferView * bv) const
2200 return getLyXText(bv)->cursor.boundary();
2204 Row * InsetText::crow(BufferView * bv) const
2206 return getLyXText(bv)->cursor.row();
2210 LyXText * InsetText::getLyXText(BufferView const * lbv,
2211 bool const recursive) const
2213 if (cached_bview == lbv) {
2214 if (recursive && the_locking_inset)
2215 return the_locking_inset->getLyXText(lbv, true);
2216 LyXText * lt = cached_text.get();
2217 lyx::Assert(lt && lt->firstRow()->par() == &*(paragraphs.begin()));
2220 // Super UGLY! (Lgb)
2221 BufferView * bv = const_cast<BufferView *>(lbv);
2224 Cache::iterator it = cache.find(bv);
2226 if (it != cache.end()) {
2229 } else if (do_resize) {
2230 resizeLyXText(do_resize);
2232 if (lt || !it->second.remove) {
2233 lyx::Assert(it->second.text.get());
2234 cached_text = it->second.text;
2235 if (recursive && the_locking_inset) {
2236 return the_locking_inset->getLyXText(bv, true);
2238 return cached_text.get();
2239 } else if (it->second.remove) {
2241 saveLyXTextState(it->second.text.get());
2247 // when we have to reinit the existing LyXText!
2249 it->second.text->init(bv);
2250 restoreLyXTextState(bv, it->second.text.get());
2251 it->second.remove = false;
2253 cached_text = it->second.text;
2254 if (the_locking_inset && recursive) {
2255 return the_locking_inset->getLyXText(bv);
2257 return cached_text.get();
2260 // we are here only if we don't have a BufferView * in the cache!!!
2262 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2263 cached_text->init(bv);
2264 restoreLyXTextState(bv, cached_text.get());
2266 cache.insert(make_pair(bv, cached_text));
2268 if (the_locking_inset && recursive) {
2269 return the_locking_inset->getLyXText(bv);
2271 return cached_text.get();
2275 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2279 Cache::iterator it = cache.find(bv);
2281 if (it == cache.end()) {
2285 lyx::Assert(it->second.text.get());
2287 it->second.remove = true;
2289 /// then remove all LyXText in text-insets
2290 Paragraph * p = &*(paragraphs.begin());
2291 for (; p; p = p->next()) {
2292 p->deleteInsetsLyXText(bv);
2298 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2301 // we cannot resize this because we are in use!
2302 // so do this on the next possible getLyXText()
2307 // lyxerr << "InsetText::resizeLyXText\n";
2308 if (!paragraphs.begin()->next() && paragraphs.begin()->empty()) { // no data, resize not neccessary!
2309 // we have to do this as a fixed width may have changed!
2310 LyXText * t = getLyXText(bv);
2311 saveLyXTextState(t);
2313 restoreLyXTextState(bv, t);
2316 // one endless line, resize normally not necessary
2317 if (!force && getMaxWidth(bv, this) < 0)
2320 Cache::iterator it = cache.find(bv);
2321 if (it == cache.end()) {
2324 lyx::Assert(it->second.text.get());
2326 LyXText * t = it->second.text.get();
2327 saveLyXTextState(t);
2328 for (Paragraph * p = &*(paragraphs.begin()); p; p = p->next()) {
2329 p->resizeInsetsLyXText(bv);
2332 restoreLyXTextState(bv, t);
2333 if (the_locking_inset) {
2334 inset_x = cix(bv) - top_x + drawTextXOffset;
2335 inset_y = ciy(bv) + drawTextYOffset;
2338 t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2340 updateLocal(bv, FULL, false);
2341 // this will scroll the screen such that the cursor becomes visible
2342 bv->updateScrollbar();
2344 need_update |= FULL;
2349 void InsetText::reinitLyXText() const
2352 // we cannot resize this because we are in use!
2353 // so do this on the next possible getLyXText()
2359 // lyxerr << "InsetText::reinitLyXText\n";
2360 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2361 lyx::Assert(it->second.text.get());
2363 LyXText * t = it->second.text.get();
2364 BufferView * bv = it->first;
2366 saveLyXTextState(t);
2367 for (Paragraph * p = &*(paragraphs.begin());
2369 p->resizeInsetsLyXText(bv);
2372 restoreLyXTextState(bv, t);
2373 if (the_locking_inset) {
2374 inset_x = cix(bv) - top_x + drawTextXOffset;
2375 inset_y = ciy(bv) + drawTextYOffset;
2377 t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2379 updateLocal(bv, FULL, false);
2380 // this will scroll the screen such that the cursor becomes visible
2381 bv->updateScrollbar();
2389 void InsetText::removeNewlines()
2391 bool changed = false;
2393 for (Paragraph * p = &*(paragraphs.begin()); p; p = p->next()) {
2394 for (int i = 0; i < p->size(); ++i) {
2395 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2406 bool InsetText::nodraw() const
2408 if (the_locking_inset)
2409 return the_locking_inset->nodraw();
2410 return UpdatableInset::nodraw();
2414 int InsetText::scroll(bool recursive) const
2416 int sx = UpdatableInset::scroll(false);
2418 if (recursive && the_locking_inset)
2419 sx += the_locking_inset->scroll(recursive);
2425 bool InsetText::doClearArea() const
2427 return !locked || (need_update & (FULL|INIT));
2431 void InsetText::selectAll(BufferView * bv)
2433 getLyXText(bv)->cursorTop(bv);
2434 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2435 getLyXText(bv)->cursorBottom(bv);
2436 getLyXText(bv)->setSelection(bv);
2440 void InsetText::clearSelection(BufferView * bv)
2442 getLyXText(bv)->clearSelection();
2446 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2448 Painter & pain = bv->painter();
2450 int h = insetAscent + insetDescent;
2451 int ty = baseline - insetAscent;
2457 if ((ty + h) > pain.paperHeight())
2458 h = pain.paperHeight();
2459 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2460 w = pain.paperWidth();
2461 // w -= TEXT_TO_INSET_OFFSET;
2462 pain.fillRectangle(top_x + 1, ty + 1, w - 1, h - 1, backgroundColor());
2465 frame_is_visible = false;
2469 Paragraph * InsetText::getParFromID(int id) const
2472 Paragraph * result = par;
2473 Paragraph * ires = 0;
2474 while (result && result->id() != id) {
2475 if ((ires = result->getParFromID(id)))
2477 result = result->next();
2481 Paragraph * tmp = &*(paragraphs.begin());
2483 if (tmp->id() == id) {
2486 Paragraph * tmp2 = tmp->getParFromID(id);
2497 Paragraph * InsetText::firstParagraph() const
2500 if (the_locking_inset)
2501 if ((result = the_locking_inset->firstParagraph()))
2503 return &*(paragraphs.begin());
2507 Paragraph * InsetText::getFirstParagraph(int i) const
2509 return (i == 0) ? &*(paragraphs.begin()) : 0;
2513 LyXCursor const & InsetText::cursor(BufferView * bv) const
2515 if (the_locking_inset)
2516 return the_locking_inset->cursor(bv);
2517 return getLyXText(bv)->cursor;
2521 Paragraph * InsetText::paragraph() const
2523 return &*(paragraphs.begin());
2527 void InsetText::paragraph(Paragraph * p)
2529 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2530 // caller of this function has to take care of it. This IS important
2531 // as we could have to insert a paragraph before this one and just
2532 // link the actual to a new ones next and set it with this function
2535 // set ourself as owner for all the paragraphs inserted!
2536 Paragraph * np = &*(paragraphs.begin());
2538 np->setInsetOwner(this);
2542 // redraw myself when asked for
2547 Inset * InsetText::getInsetFromID(int id_arg) const
2550 return const_cast<InsetText *>(this);
2552 Paragraph * lp = &*(paragraphs.begin());
2555 for (InsetList::iterator it = lp->insetlist.begin(),
2556 en = lp->insetlist.end();
2559 if (it.getInset()->id() == id_arg)
2560 return it.getInset();
2561 Inset * in = it.getInset()->getInsetFromID(id_arg);
2572 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2573 float & value) const
2579 lt = getLyXText(bv);
2582 if (the_locking_inset) {
2583 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2584 if (!word.word().empty()) {
2590 // we have to go on checking so move cursor to the next char
2591 lt->cursor.pos(lt->cursor.pos() + 1);
2593 word = lt->selectNextWordToSpellcheck(bv, value);
2594 if (word.word().empty())
2595 bv->unlockInset(const_cast<InsetText *>(this));
2604 void InsetText::selectSelectedWord(BufferView * bv)
2606 if (the_locking_inset) {
2607 the_locking_inset->selectSelectedWord(bv);
2610 getLyXText(bv)->selectSelectedWord(bv);
2611 updateLocal(bv, SELECTION, false);
2615 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2617 if (the_locking_inset) {
2618 the_locking_inset->toggleSelection(bv, kill_selection);
2622 lt = getLyXText(bv);
2626 int x = top_x + TEXT_TO_INSET_OFFSET;
2628 Row * row = lt->firstRow();
2629 int y_offset = top_baseline - row->ascent_of_text();
2631 while ((row != 0) && ((y+row->height()) <= 0)) {
2638 if (need_update & SELECTION)
2640 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2646 bool InsetText::searchForward(BufferView * bv, string const & str,
2651 lt = getLyXText(bv);
2654 if (the_locking_inset) {
2655 if (the_locking_inset->searchForward(bv, str, cs, mw))
2657 lt->cursorRight(bv, true);
2659 lyxfind::SearchResult result =
2660 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2662 if (result == lyxfind::SR_FOUND) {
2663 LyXCursor cur = lt->cursor;
2664 bv->unlockInset(bv->theLockingInset());
2665 if (bv->lockInset(this))
2668 lt->setSelectionOverString(bv, str);
2669 updateLocal(bv, SELECTION, false);
2673 return (result != lyxfind::SR_NOT_FOUND);
2676 bool InsetText::searchBackward(BufferView * bv, string const & str,
2679 if (the_locking_inset) {
2680 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2685 lt = getLyXText(bv);
2689 Paragraph * p = &*(paragraphs.begin());
2692 lt->setCursor(bv, p, p->size());
2694 lyxfind::SearchResult result =
2695 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2697 if (result == lyxfind::SR_FOUND) {
2698 LyXCursor cur = lt->cursor;
2699 bv->unlockInset(bv->theLockingInset());
2700 if (bv->lockInset(this))
2703 lt->setSelectionOverString(bv, str);
2704 updateLocal(bv, SELECTION, false);
2708 return (result != lyxfind::SR_NOT_FOUND);
2712 bool InsetText::checkInsertChar(LyXFont & font)
2715 return owner()->checkInsertChar(font);
2720 void InsetText::collapseParagraphs(BufferView * bv) const
2722 BufferParams const & bparams = bv->buffer()->params;
2723 LyXText * llt = getLyXText(bv);
2725 while (paragraphs.begin()->next()) {
2726 if (!paragraphs.begin()->empty() && !paragraphs.begin()->next()->empty() &&
2727 !paragraphs.begin()->isSeparator(paragraphs.begin()->size() - 1))
2729 paragraphs.begin()->insertChar(paragraphs.begin()->size(), ' ');
2731 if (llt->selection.set()) {
2732 if (llt->selection.start.par() == paragraphs.begin()->next()) {
2733 llt->selection.start.par(&*(paragraphs.begin()));
2734 llt->selection.start.pos(
2735 llt->selection.start.pos() + paragraphs.begin()->size());
2737 if (llt->selection.end.par() == paragraphs.begin()->next()) {
2738 llt->selection.end.par(&*(paragraphs.begin()));
2739 llt->selection.end.pos(
2740 llt->selection.end.pos() + paragraphs.begin()->size());
2743 paragraphs.begin()->pasteParagraph(bparams);
2749 void InsetText::getDrawFont(LyXFont & font) const
2753 owner()->getDrawFont(font);
2757 void InsetText::appendParagraphs(BufferParams const & bparams,
2761 Paragraph * tmpbuf = newpar;
2762 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2764 while (tmpbuf->next()) {
2765 tmpbuf = tmpbuf->next();
2766 lastbuffer->next(new Paragraph(*tmpbuf, false));
2767 lastbuffer->next()->previous(lastbuffer);
2768 lastbuffer = lastbuffer->next();
2770 lastbuffer = &*(paragraphs.begin());
2771 while (lastbuffer->next())
2772 lastbuffer = lastbuffer->next();
2773 if (!newpar->empty() && !lastbuffer->empty() &&
2774 !lastbuffer->isSeparator(lastbuffer->size() - 1))
2776 lastbuffer->insertChar(lastbuffer->size(), ' ');
2779 // make the buf exactly the same layout than our last paragraph
2780 buf->makeSameLayout(lastbuffer);
2783 lastbuffer->next(buf);
2784 buf->previous(lastbuffer);
2785 lastbuffer->pasteParagraph(bparams);
2791 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2793 Paragraph * par = getFirstParagraph(0);
2795 InsetList::iterator it = par->insetlist.begin();
2796 InsetList::iterator end = par->insetlist.end();
2797 for (; it != end; ++it) {
2798 it.getInset()->addPreview(loader);