3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS
14 #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"
40 #include "paragraph_funcs.h"
43 #include "frontends/Alert.h"
44 #include "frontends/Dialogs.h"
45 #include "frontends/font_metrics.h"
46 #include "frontends/LyXView.h"
47 #include "frontends/Painter.h"
48 #include "frontends/screen.h"
50 #include "support/textutils.h"
51 #include "support/LAssert.h"
52 #include "support/lstrings.h"
53 #include "support/lyxalgo.h" // lyx::count
55 #include <boost/bind.hpp>
73 using lyx::textclass_type;
75 extern unsigned char getCurrentTextClass(Buffer *);
76 extern bool math_insert_greek(BufferView *, char);
77 extern int greek_kb_flag;
80 // These functions should probably go into bufferview_funcs somehow (Jug)
82 void InsetText::saveLyXTextState(LyXText * t) const
84 // check if my paragraphs are still valid
85 ParagraphList::iterator it = paragraphs.begin();
86 ParagraphList::iterator end = paragraphs.end();
87 for (; it != end; ++it) {
88 if (&*it == t->cursor.par())
92 if (it != end && t->cursor.pos() <= it->size()) {
93 sstate.lpar = t->cursor.par();
94 sstate.pos = t->cursor.pos();
95 sstate.boundary = t->cursor.boundary();
96 sstate.selstartpar = t->selection.start.par();
97 sstate.selstartpos = t->selection.start.pos();
98 sstate.selstartboundary = t->selection.start.boundary();
99 sstate.selendpar = t->selection.end.par();
100 sstate.selendpos = t->selection.end.pos();
101 sstate.selendboundary = t->selection.end.boundary();
102 sstate.selection = t->selection.set();
103 sstate.mark_set = t->selection.mark();
104 sstate.refresh = t->refresh_row != 0;
110 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
113 t->selection.set(true);
114 /* at this point just to avoid the Delete-Empty-Paragraph
115 * Mechanism when setting the cursor */
116 t->selection.mark(sstate.mark_set);
117 if (sstate.selection) {
118 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
119 true, sstate.selstartboundary);
120 t->selection.cursor = t->cursor;
121 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
122 true, sstate.selendboundary);
124 t->setCursor(bv, sstate.lpar, sstate.pos);
126 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
127 t->selection.cursor = t->cursor;
128 t->selection.set(false);
130 if (sstate.refresh) {
136 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
143 InsetText::InsetText(BufferParams const & bp)
144 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
147 paragraphs.set(new Paragraph);
148 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
153 InsetText::InsetText(InsetText const & in, bool same_id)
154 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
161 InsetText & InsetText::operator=(InsetText const & it)
168 void InsetText::init(InsetText const * ins, bool same_id)
171 setParagraphData(&*(ins->paragraphs.begin()), same_id);
172 autoBreakRows = ins->autoBreakRows;
173 drawFrame_ = ins->drawFrame_;
174 frame_color = ins->frame_color;
178 for_each(paragraphs.begin(), paragraphs.end(),
179 boost::bind(&Paragraph::setInsetOwner, _1, this));
181 the_locking_inset = 0;
183 frame_color = LColor::insetframe;
184 autoBreakRows = false;
197 last_drawn_width = -1;
198 frame_is_visible = false;
201 in_insetAllowed = false;
205 InsetText::~InsetText()
211 void InsetText::clear()
213 // This is a gross hack...
214 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
217 paragraphs.set(new Paragraph);
218 paragraphs.begin()->setInsetOwner(this);
219 paragraphs.begin()->layout(old_layout);
226 Inset * InsetText::clone(Buffer const &, bool same_id) const
228 return new InsetText(*this, same_id);
232 void InsetText::write(Buffer const * buf, ostream & os) const
235 writeParagraphData(buf, os);
239 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
241 ParagraphList::iterator it = paragraphs.begin();
242 ParagraphList::iterator end = paragraphs.end();
243 Paragraph::depth_type dth = 0;
244 for (; it != end; ++it) {
245 it->write(buf, os, buf->params, dth);
250 void InsetText::read(Buffer const * buf, LyXLex & lex)
254 Paragraph * return_par = 0;
255 Paragraph::depth_type depth = 0;
256 LyXFont font(LyXFont::ALL_INHERIT);
262 token = lex.getString();
265 if (token == "\\end_inset") {
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!");
281 return_par = &*(paragraphs.begin());
282 paragraphs.set(return_par);
284 return_par->setInsetOwner(this);
285 return_par = return_par->next();
288 if (token != "\\end_inset") {
289 lex.printError("Missing \\end_inset at this point. "
296 int InsetText::ascent(BufferView * bv, LyXFont const &) const
298 insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
299 TEXT_TO_INSET_OFFSET;
304 int InsetText::descent(BufferView * bv, LyXFont const &) const
306 LyXText * llt = getLyXText(bv);
307 insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
308 TEXT_TO_INSET_OFFSET;
313 int InsetText::width(BufferView * bv, LyXFont const &) const
315 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
316 (2 * TEXT_TO_INSET_OFFSET);
317 insetWidth = max(insetWidth, 10);
322 int InsetText::textWidth(BufferView * bv, bool fordraw) const
325 if (!autoBreakRows) {
328 w = getMaxWidth(bv, this);
331 return max(w - (2 * TEXT_TO_INSET_OFFSET),
332 (int)getLyXText(bv)->width);
336 return w - (2 * TEXT_TO_INSET_OFFSET);
340 void InsetText::draw(BufferView * bv, LyXFont const & f,
341 int baseline, float & x, bool cleared) const
346 Painter & pain = bv->painter();
348 // this is the first thing we have to ask because if the x pos
349 // changed we have to do a complete rebreak of the text as we
350 // may have few space to draw in. Well we should check on this too
352 if (top_x != int(x)) {
355 int nw = getMaxWidth(bv, this);
356 if (nw > 0 && old_max_width != nw) {
359 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
364 // call these methods so that insetWidth, insetAscent and
365 // insetDescent have the right values.
370 // repaint the background if needed
371 if (cleared && backgroundColor() != LColor::background) {
372 clearInset(bv, baseline, cleared);
375 // no draw is necessary !!!
376 if ((drawFrame_ == LOCKED) && !locked && paragraphs.begin()->empty()) {
377 top_baseline = baseline;
379 if (need_update & CLEAR_FRAME)
380 clearFrame(pain, cleared);
386 x += static_cast<float>(scroll());
388 // if top_x differs we did it already
389 if (!cleared && (old_x == int(x))
390 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
391 ||(last_drawn_width != insetWidth)))
393 // Condition necessary to eliminate bug 59 attachment 37
395 clearInset(bv, baseline, cleared);
399 frame_is_visible = false;
401 if (!cleared && (need_update == NONE)) {
403 drawFrame(pain, cleared);
407 top_baseline = baseline;
408 top_y = baseline - insetAscent;
410 if (last_drawn_width != insetWidth) {
412 clearInset(bv, baseline, cleared);
414 last_drawn_width = insetWidth;
417 if (the_locking_inset && (cpar(bv) == inset_par)
418 && (cpos(bv) == inset_pos)) {
419 inset_x = cix(bv) - top_x + drawTextXOffset;
420 inset_y = ciy(bv) + drawTextYOffset;
422 if (!cleared && (need_update == CURSOR)
423 && !getLyXText(bv)->selection.set()) {
424 drawFrame(pain, cleared);
434 x += TEXT_TO_INSET_OFFSET;
436 Row * row = lt->firstRow();
437 int y_offset = baseline - row->ascent_of_text();
438 int ph = pain.paperHeight();
441 while ((row != 0) && ((y+row->height()) <= 0)) {
443 first += row->height();
447 lt->first_y = -y_offset;
454 if (cleared || (need_update&(INIT|FULL))) {
455 int yf = y_offset + first;
457 while ((row != 0) && (yf < ph)) {
458 lt->getVisibleRow(bv, y + y_offset + first, int(x),
459 row, y + lt->first_y, cleared);
460 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
461 lt->need_break_row = row;
463 lt->setCursor(bv, lt->cursor.par(),
465 if (lt->selection.set()) {
466 lt->setCursor(bv, lt->selection.start,
467 lt->selection.start.par(),
468 lt->selection.start.pos());
469 lt->setCursor(bv, lt->selection.end,
470 lt->selection.end.par(),
471 lt->selection.end.pos());
479 } else if (!locked) {
480 if (need_update & CURSOR) {
481 bv->screen().toggleSelection(lt, bv, true, y_offset,int(x));
482 lt->clearSelection();
483 lt->selection.cursor = lt->cursor;
485 bv->screen().update(lt, bv, y_offset, int(x));
488 if (need_update & SELECTION) {
489 bv->screen().toggleToggle(lt, bv, y_offset, int(x));
490 } else if (need_update & CURSOR) {
491 bv->screen().toggleSelection(lt, bv, true, y_offset,int(x));
492 lt->clearSelection();
493 lt->selection.cursor = lt->cursor;
495 bv->screen().update(lt, bv, y_offset, int(x));
500 lt->status(bv, LyXText::UNCHANGED);
501 if ((drawFrame_ == ALWAYS) ||
502 ((cleared || (need_update != CURSOR_PAR)) &&
503 (drawFrame_ == LOCKED) && locked))
505 drawFrame(pain, cleared);
506 } else if (need_update & CLEAR_FRAME) {
507 clearFrame(pain, cleared);
510 x += insetWidth - TEXT_TO_INSET_OFFSET;
512 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
514 } else if (need_update != INIT) {
522 void InsetText::drawFrame(Painter & pain, bool cleared) const
524 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
525 if (!frame_is_visible || cleared) {
526 frame_x = top_x + ttoD2;
527 frame_y = top_baseline - insetAscent + ttoD2;
528 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
529 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
530 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
532 frame_is_visible = true;
537 void InsetText::clearFrame(Painter & pain, bool cleared) const
539 if (frame_is_visible) {
541 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
544 frame_is_visible = false;
549 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
552 if (reinit && owner()) {
554 owner()->update(bv, font, true);
559 if (reinit || need_update == INIT) {
561 // we should put this call where we set need_update to INIT!
564 owner()->update(bv, font, true);
569 if (!autoBreakRows && paragraphs.begin()->next())
570 collapseParagraphs(bv);
572 if (the_locking_inset) {
573 inset_x = cix(bv) - top_x + drawTextXOffset;
574 inset_y = ciy(bv) + drawTextYOffset;
575 the_locking_inset->update(bv, font, reinit);
583 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
586 lt->updateInset(bv, the_locking_inset);
588 if (lt->status() == LyXText::NEED_MORE_REFRESH)
596 void InsetText::setUpdateStatus(BufferView * bv, int what) const
598 // this does nothing dangerous so use only a localized buffer
599 LyXText * llt = getLyXText(bv);
602 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
603 // if we don't break row so that we only have one row to update!
604 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
606 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
609 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
610 need_update |= CURSOR_PAR;
613 // this to not draw a selection when we redraw all of it!
614 if (need_update & CURSOR && !(need_update & SELECTION)) {
615 if (llt->selection.set())
617 llt->clearSelection();
622 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
624 if (!autoBreakRows && paragraphs.begin()->next())
625 collapseParagraphs(bv);
632 setUpdateStatus(bv, what);
633 bool flag = mark_dirty ||
634 (((need_update != CURSOR) && (need_update != NONE)) ||
635 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
636 if (!lt->selection.set())
637 lt->selection.cursor = lt->cursor;
641 // IMO this is not anymore needed as we do this in fitInsetCursor!
642 // and we always get "true" as returnvalue of this function in the
643 // case of a locking inset (Jug 20020412)
644 if (locked && (need_update & CURSOR) && bv->fitCursor())
650 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
652 if (need_update == CURSOR)
654 bv->owner()->view_state_changed();
655 bv->owner()->updateMenubar();
656 bv->owner()->updateToolbar();
657 if (old_par != cpar(bv)) {
658 bv->owner()->setLayout(cpar(bv)->layout()->name());
664 string const InsetText::editMessage() const
666 return _("Opened Text Inset");
670 void InsetText::edit(BufferView * bv, int x, int y, mouse_button::state button)
672 UpdatableInset::edit(bv, x, y, button);
674 if (!bv->lockInset(this)) {
675 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
679 the_locking_inset = 0;
680 inset_pos = inset_x = inset_y = 0;
681 inset_boundary = false;
684 int tmp_y = (y < 0) ? 0 : y;
690 // we put here -1 and not button as now the button in the
691 // edit call should not be needed we will fix this in 1.3.x
692 // cycle hopefully (Jug 20020509)
693 // FIXME: GUII I've changed this to none: probably WRONG
694 if (!checkAndActivateInset(bv, x, tmp_y, mouse_button::none)) {
695 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
697 lt->cursor.x_fix(lt->cursor.x());
699 lt->clearSelection();
701 // If the inset is empty set the language of the current font to the
702 // language to the surronding text (if different).
703 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
704 bv->getParentLanguage(this) != lt->current_font.language())
706 LyXFont font(LyXFont::ALL_IGNORE);
707 font.setLanguage(bv->getParentLanguage(this));
708 setFont(bv, font, false);
714 if (drawFrame_ == LOCKED)
715 code = CURSOR|DRAW_FRAME;
716 updateLocal(bv, code, false);
719 // Tell the paragraph dialog that we've entered an insettext.
720 bv->owner()->getDialogs().updateParagraph();
724 void InsetText::edit(BufferView * bv, bool front)
726 UpdatableInset::edit(bv, front);
728 if (!bv->lockInset(this)) {
729 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
733 the_locking_inset = 0;
734 inset_pos = inset_x = inset_y = 0;
735 inset_boundary = false;
744 lt->setCursor(bv, &*(paragraphs.begin()), 0);
746 ParagraphList::iterator it = paragraphs.begin();
747 ParagraphList::iterator end = paragraphs.end();
748 while (boost::next(it) != end)
750 // int const pos = (p->size() ? p->size()-1 : p->size());
751 lt->setCursor(bv, &*it, it->size());
753 lt->clearSelection();
755 // If the inset is empty set the language of the current font to the
756 // language to the surronding text (if different).
757 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
758 bv->getParentLanguage(this) != lt->current_font.language()) {
759 LyXFont font(LyXFont::ALL_IGNORE);
760 font.setLanguage(bv->getParentLanguage(this));
761 setFont(bv, font, false);
766 if (drawFrame_ == LOCKED)
767 code = CURSOR|DRAW_FRAME;
768 updateLocal(bv, code, false);
773 void InsetText::insetUnlock(BufferView * bv)
775 if (the_locking_inset) {
776 the_locking_inset->insetUnlock(bv);
777 the_locking_inset = 0;
778 updateLocal(bv, CURSOR_PAR, false);
785 if (drawFrame_ == LOCKED)
786 code = CURSOR|CLEAR_FRAME;
790 if (drawFrame_ == LOCKED)
798 if (lt->selection.set()) {
799 lt->clearSelection();
801 } else if (owner()) {
802 bv->owner()->setLayout(owner()->getLyXText(bv)
803 ->cursor.par()->layout()->name());
805 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
806 // hack for deleteEmptyParMech
807 if (!paragraphs.begin()->empty()) {
808 lt->setCursor(bv, &*(paragraphs.begin()), 0);
809 } else if (paragraphs.begin()->next()) {
810 lt->setCursor(bv, paragraphs.begin()->next(), 0);
815 updateLocal(bv, code, false);
818 setUpdateStatus(bv, code);
823 void InsetText::lockInset(BufferView * bv)
826 the_locking_inset = 0;
827 inset_pos = inset_x = inset_y = 0;
828 inset_boundary = false;
836 lt->setCursor(bv, &*(paragraphs.begin()), 0);
837 lt->clearSelection();
839 // If the inset is empty set the language of the current font to the
840 // language to the surronding text (if different).
841 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
842 bv->getParentLanguage(this) != lt->current_font.language()) {
843 LyXFont font(LyXFont::ALL_IGNORE);
844 font.setLanguage(bv->getParentLanguage(this));
845 setFont(bv, font, false);
850 if (drawFrame_ == LOCKED)
851 code = CURSOR|DRAW_FRAME;
852 setUpdateStatus(bv, code);
856 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
858 the_locking_inset = inset;
859 inset_x = cix(bv) - top_x + drawTextXOffset;
860 inset_y = ciy(bv) + drawTextYOffset;
861 inset_pos = cpos(bv);
862 inset_par = cpar(bv);
863 inset_boundary = cboundary(bv);
864 updateLocal(bv, CURSOR, false);
868 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
870 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
874 if (!the_locking_inset) {
875 Paragraph * p = &*(paragraphs.begin());
876 int const id = inset->id();
878 InsetList::iterator it =
879 p->insetlist.begin();
880 InsetList::iterator const end =
882 for (; it != end; ++it) {
883 if (it.getInset() == inset) {
884 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
885 lockInset(bv, inset);
888 if (it.getInset()->getInsetFromID(id)) {
889 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
890 it.getInset()->edit(bv);
891 return the_locking_inset->lockInsetInInset(bv, inset);
898 if (inset == cpar(bv)->getInset(cpos(bv))) {
899 lyxerr[Debug::INSETS] << "OK" << endl;
900 lockInset(bv, inset);
902 } else if (the_locking_inset && (the_locking_inset == inset)) {
903 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
904 lyxerr[Debug::INSETS] << "OK" << endl;
905 inset_x = cix(bv) - top_x + drawTextXOffset;
906 inset_y = ciy(bv) + drawTextYOffset;
908 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
910 } else if (the_locking_inset) {
911 lyxerr[Debug::INSETS] << "MAYBE" << endl;
912 return the_locking_inset->lockInsetInInset(bv, inset);
914 lyxerr[Debug::INSETS] << "NOT OK" << endl;
919 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
922 if (!the_locking_inset)
924 if (the_locking_inset == inset) {
925 the_locking_inset->insetUnlock(bv);
926 getLyXText(bv)->updateInset(bv, inset);
927 the_locking_inset = 0;
929 moveRightIntern(bv, true, false);
930 old_par = 0; // force layout setting
934 updateLocal(bv, CURSOR, false);
937 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
941 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
943 if (!autoBreakRows && paragraphs.begin()->next())
944 collapseParagraphs(bv);
952 if (inset->owner() != this) {
953 int ustat = CURSOR_PAR;
955 UpdatableInset * tl_inset = the_locking_inset;
957 found = tl_inset->updateInsetInInset(bv, inset);
959 tl_inset = static_cast<UpdatableInset *>(inset);
960 while(tl_inset->owner() && tl_inset->owner() != this)
961 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
962 if (!tl_inset->owner())
964 found = tl_inset->updateInsetInInset(bv, inset);
968 lt->updateInset(bv, tl_inset);
972 setUpdateStatus(bv, ustat);
975 bool found = lt->updateInset(bv, inset);
979 setUpdateStatus(bv, CURSOR_PAR);
980 if (the_locking_inset &&
981 cpar(bv) == inset_par && cpos(bv) == inset_pos)
983 inset_x = cix(bv) - top_x + drawTextXOffset;
984 inset_y = ciy(bv) + drawTextYOffset;
991 void InsetText::lfunMousePress(FuncRequest const & cmd)
995 // use this to check mouse motion for selection!
999 BufferView * bv = cmd.view();
1000 FuncRequest cmd1 = cmd;
1006 int tmp_x = cmd.x - drawTextXOffset;
1007 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->first_y;
1008 Inset * inset = getLyXText(bv)->checkInsetHit(bv, tmp_x, tmp_y);
1010 hideInsetCursor(bv);
1011 if (the_locking_inset) {
1012 if (the_locking_inset == inset) {
1013 the_locking_inset->localDispatch(cmd1);
1018 // otherwise unlock the_locking_inset and lock the new inset
1019 the_locking_inset->insetUnlock(bv);
1020 inset_x = cix(bv) - top_x + drawTextXOffset;
1021 inset_y = ciy(bv) + drawTextYOffset;
1022 the_locking_inset = 0;
1023 inset->localDispatch(cmd1);
1024 // inset->edit(bv, x - inset_x, y - inset_y, button);
1025 if (the_locking_inset)
1026 updateLocal(bv, CURSOR, false);
1030 // otherwise only unlock the_locking_inset
1031 the_locking_inset->insetUnlock(bv);
1032 the_locking_inset = 0;
1035 no_selection = false;
1037 if (bv->theLockingInset()) {
1038 if (isHighlyEditableInset(inset)) {
1039 // We just have to lock the inset before calling a
1040 // PressEvent on it!
1041 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
1042 if (!bv->lockInset(uinset)) {
1043 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1045 inset->localDispatch(cmd1);
1046 if (the_locking_inset)
1047 updateLocal(bv, CURSOR, false);
1051 if (!inset) { // && (button == mouse_button::button2)) {
1052 bool paste_internally = false;
1053 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
1054 localDispatch(FuncRequest(bv, LFUN_COPY));
1055 paste_internally = true;
1059 lt = getLyXText(bv);
1062 int old_first_y = lt->first_y;
1064 lt->setCursorFromCoordinates(bv, cmd.x - drawTextXOffset,
1065 cmd.y + insetAscent);
1066 // set the selection cursor!
1067 lt->selection.cursor = lt->cursor;
1068 lt->cursor.x_fix(lt->cursor.x());
1070 if (lt->selection.set()) {
1071 lt->clearSelection();
1074 updateLocal(bv, FULL, false);
1076 lt->clearSelection();
1079 updateLocal(bv, CURSOR, false);
1082 bv->owner()->setLayout(cpar(bv)->layout()->name());
1084 // we moved the view we cannot do mouse selection in this case!
1085 if (getLyXText(bv)->first_y != old_first_y)
1086 no_selection = true;
1088 // Insert primary selection with middle mouse
1089 // if there is a local selection in the current buffer,
1091 if (cmd.button() == mouse_button::button2) {
1092 if (paste_internally)
1093 localDispatch(FuncRequest(bv, LFUN_PASTE));
1095 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
1098 getLyXText(bv)->clearSelection();
1100 showInsetCursor(bv);
1104 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
1106 BufferView * bv = cmd.view();
1107 FuncRequest cmd1 = cmd;
1111 no_selection = true;
1112 if (the_locking_inset)
1113 return the_locking_inset->localDispatch(cmd1);
1115 int tmp_x = cmd.x - drawTextXOffset;
1116 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->first_y;
1117 Inset * inset = getLyXText(bv)->checkInsetHit(bv, tmp_x, tmp_y);
1120 if (isHighlyEditableInset(inset))
1121 ret = inset->localDispatch(cmd1);
1123 inset_x = cix(bv) - top_x + drawTextXOffset;
1124 inset_y = ciy(bv) + drawTextYOffset;
1125 cmd1.x = cmd.x - inset_x;
1126 cmd1.y = cmd.x - inset_y;
1127 // note that we should do ret = inset->localDispatch(cmd1)
1128 // and fix this instead (Alfredo);
1130 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
1132 updateLocal(bv, CURSOR_PAR, false);
1138 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
1140 FuncRequest cmd1 = cmd;
1144 if (the_locking_inset) {
1145 the_locking_inset->localDispatch(cmd1);
1149 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
1152 BufferView * bv = cmd.view();
1155 lt = getLyXText(bv);
1158 hideInsetCursor(bv);
1159 LyXCursor cur = lt->cursor;
1160 lt->setCursorFromCoordinates
1161 (bv, cmd.x - drawTextXOffset, cmd.y + insetAscent);
1162 lt->cursor.x_fix(lt->cursor.x());
1163 if (cur == lt->cursor) {
1168 lt->setSelection(bv);
1169 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1170 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1174 updateLocal(bv, SELECTION, false);
1176 showInsetCursor(bv);
1180 Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
1182 BufferView * bv = ev.view();
1183 switch (ev.action) {
1184 case LFUN_MOUSE_PRESS:
1187 case LFUN_MOUSE_MOTION:
1188 lfunMouseMotion(ev);
1190 case LFUN_MOUSE_RELEASE:
1191 return lfunMouseRelease(ev) ? DISPATCHED : UNDISPATCHED;
1196 bool was_empty = (paragraphs.begin()->empty() && !paragraphs.begin()->next());
1197 no_selection = false;
1198 RESULT result = UpdatableInset::localDispatch(ev);
1199 if (result != UNDISPATCHED)
1202 result = DISPATCHED;
1203 if (ev.action < 0 && ev.argument.empty())
1206 if (the_locking_inset) {
1207 result = the_locking_inset->localDispatch(ev);
1208 if (result == DISPATCHED_NOUPDATE)
1210 else if (result == DISPATCHED) {
1211 updateLocal(bv, CURSOR_PAR, false);
1213 } else if (result >= FINISHED) {
1215 case FINISHED_RIGHT:
1216 moveRightIntern(bv, false, false);
1217 result = DISPATCHED;
1220 if ((result = moveUp(bv)) >= FINISHED) {
1221 updateLocal(bv, CURSOR, false);
1222 bv->unlockInset(this);
1226 if ((result = moveDown(bv)) >= FINISHED) {
1227 updateLocal(bv, CURSOR, false);
1228 bv->unlockInset(this);
1232 result = DISPATCHED;
1235 the_locking_inset = 0;
1236 updateLocal(bv, CURSOR, false);
1240 hideInsetCursor(bv);
1243 lt = getLyXText(bv);
1247 int updflag = false;
1248 switch (ev.action) {
1251 case LFUN_SELFINSERT:
1252 if (bv->buffer()->isReadonly()) {
1253 // setErrorMessage(N_("Document is read only"));
1256 if (!ev.argument.empty()) {
1257 /* Automatically delete the currently selected
1258 * text and replace it with what is being
1259 * typed in now. Depends on lyxrc settings
1260 * "auto_region_delete", which defaults to
1263 // This should not be needed here and is also WRONG!
1264 setUndo(bv, Undo::INSERT,
1265 lt->cursor.par(), lt->cursor.par()->next());
1268 if (lyxrc.auto_region_delete) {
1269 if (lt->selection.set()) {
1270 lt->cutSelection(bv, false, false);
1273 lt->clearSelection();
1274 for (string::size_type i = 0; i < ev.argument.length(); ++i) {
1275 bv->owner()->getIntl().getTransManager().
1276 TranslateAndInsert(ev.argument[i], lt);
1279 lt->selection.cursor = lt->cursor;
1280 updwhat = CURSOR | CURSOR_PAR;
1282 result = DISPATCHED_NOUPDATE;
1284 // --- Cursor Movements -----------------------------------
1287 moveRight(bv, false, true);
1288 lt->setSelection(bv);
1289 updwhat = SELECTION;
1292 result = moveRight(bv);
1298 moveLeft(bv, false, true);
1299 lt->setSelection(bv);
1300 updwhat = SELECTION;
1304 result = moveLeft(bv);
1310 lt->setSelection(bv);
1311 updwhat = SELECTION;
1315 result = moveDown(bv);
1321 lt->setSelection(bv);
1322 updwhat = SELECTION;
1326 result = moveUp(bv);
1339 if (!crow(bv)->previous())
1340 result = FINISHED_UP;
1342 lt->cursorPrevious(bv);
1343 result = DISPATCHED_NOUPDATE;
1348 if (!crow(bv)->next())
1349 result = FINISHED_DOWN;
1352 result = DISPATCHED_NOUPDATE;
1356 case LFUN_BACKSPACE: {
1357 if (lt->selection.set())
1358 lt->cutSelection(bv, true, false);
1361 updwhat = CURSOR_PAR;
1367 if (lt->selection.set()) {
1368 lt->cutSelection(bv, true, false);
1372 updwhat = CURSOR_PAR;
1378 lt->cutSelection(bv);
1379 updwhat = CURSOR_PAR;
1386 lt->copySelection(bv);
1387 updwhat = CURSOR_PAR;
1389 case LFUN_PASTESELECTION:
1391 string const clip(bv->getClipboard());
1395 if (ev.argument == "paragraph") {
1396 lt->insertStringAsParagraphs(bv, clip);
1398 lt->insertStringAsLines(bv, clip);
1401 lt->clearSelection();
1403 updwhat = CURSOR_PAR;
1408 if (!autoBreakRows) {
1410 if (CutAndPaste::nrOfParagraphs() > 1) {
1411 Alert::alert(_("Impossible operation!"),
1412 _("Cannot include more than one paragraph!"),
1418 // This should not be needed here and is also WRONG!
1419 setUndo(bv, Undo::INSERT,
1420 lt->cursor.par(), lt->cursor.par()->next());
1422 lt->pasteSelection(bv);
1424 lt->clearSelection();
1425 updwhat = CURSOR_PAR;
1430 case LFUN_BREAKPARAGRAPH:
1431 if (!autoBreakRows) {
1432 result = DISPATCHED;
1435 lt->breakParagraph(bv, 0);
1436 updwhat = CURSOR | FULL;
1439 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1440 if (!autoBreakRows) {
1441 result = DISPATCHED;
1444 lt->breakParagraph(bv, 1);
1445 updwhat = CURSOR | FULL;
1449 case LFUN_BREAKLINE: {
1450 if (!autoBreakRows) {
1451 result = DISPATCHED;
1455 // This should not be needed here and is also WRONG!
1456 setUndo(bv, Undo::INSERT,
1457 lt->cursor.par(), lt->cursor.par()->next());
1459 lt->insertChar(bv, Paragraph::META_NEWLINE);
1460 updwhat = CURSOR | CURSOR_PAR;
1466 // do not set layouts on non breakable textinsets
1467 if (autoBreakRows) {
1468 string cur_layout = cpar(bv)->layout()->name();
1470 // Derive layout number from given argument (string)
1471 // and current buffer's textclass (number). */
1472 LyXTextClass const & tclass =
1473 bv->buffer()->params.getLyXTextClass();
1474 string layout = ev.argument;
1475 bool hasLayout = tclass.hasLayout(layout);
1477 // If the entry is obsolete, use the new one instead.
1479 string const & obs =
1480 tclass[layout]->obsoleted_by();
1485 // see if we found the layout number:
1487 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + ev.argument + N_(" not known"));
1488 bv->owner()->dispatch(lf);
1492 if (cur_layout != layout) {
1493 cur_layout = layout;
1494 lt->setLayout(bv, layout);
1495 bv->owner()->setLayout(cpar(bv)->layout()->name());
1496 updwhat = CURSOR_PAR;
1500 // reset the layout box
1501 bv->owner()->setLayout(cpar(bv)->layout()->name());
1504 case LFUN_PARAGRAPH_SPACING:
1505 // This one is absolutely not working. When fiddling with this
1506 // it also seems to me that the paragraphs inside the insettext
1507 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1509 Paragraph * par = lt->cursor.par();
1510 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1511 float cur_value = 1.0;
1512 if (cur_spacing == Spacing::Other) {
1513 cur_value = par->params().spacing().getValue();
1516 istringstream istr(ev.argument.c_str());
1519 Spacing::Space new_spacing = cur_spacing;
1520 float new_value = cur_value;
1522 lyxerr << "Missing argument to `paragraph-spacing'"
1524 } else if (tmp == "single") {
1525 new_spacing = Spacing::Single;
1526 } else if (tmp == "onehalf") {
1527 new_spacing = Spacing::Onehalf;
1528 } else if (tmp == "double") {
1529 new_spacing = Spacing::Double;
1530 } else if (tmp == "other") {
1531 new_spacing = Spacing::Other;
1534 lyxerr << "new_value = " << tmpval << endl;
1537 } else if (tmp == "default") {
1538 new_spacing = Spacing::Default;
1540 lyxerr << _("Unknown spacing argument: ")
1541 << ev.argument << endl;
1543 if (cur_spacing != new_spacing || cur_value != new_value) {
1544 par->params().spacing(Spacing(new_spacing, new_value));
1545 updwhat = CURSOR_PAR;
1552 if (!bv->dispatch(ev))
1553 result = UNDISPATCHED;
1560 updateLocal(bv, updwhat, updflag);
1561 /// If the action has deleted all text in the inset, we need to change the
1562 // language to the language of the surronding text.
1563 if (!was_empty && paragraphs.begin()->empty() && !paragraphs.begin()->next()) {
1564 LyXFont font(LyXFont::ALL_IGNORE);
1565 font.setLanguage(bv->getParentLanguage(this));
1566 setFont(bv, font, false);
1569 if (result >= FINISHED)
1570 bv->unlockInset(this);
1572 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1573 result = DISPATCHED;
1578 int InsetText::latex(Buffer const * buf, ostream & os,
1579 bool moving_arg, bool) const
1582 buf->latexParagraphs(os, &*(paragraphs.begin()), 0,
1583 texrow, moving_arg);
1584 return texrow.rows();
1588 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1590 unsigned int lines = 0;
1592 ParagraphList::iterator beg = paragraphs.begin();
1593 ParagraphList::iterator end = paragraphs.end();
1594 ParagraphList::iterator it = beg;
1595 for (; it != end; ++it) {
1596 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1597 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1604 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1606 Paragraph * p = &*(paragraphs.begin());
1607 unsigned int lines = 0;
1609 vector<string> environment_stack(10);
1610 vector<string> environment_inner(10);
1612 int const command_depth = 0;
1615 Paragraph::depth_type depth = 0; // paragraph depth
1619 int desc_on = 0; // description mode
1621 LyXLayout_ptr const & style = p->layout();
1623 // environment tag closing
1624 for (; depth > p->params().depth(); --depth) {
1625 if (environment_inner[depth] != "!-- --") {
1626 item_name = "listitem";
1627 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1628 if (environment_inner[depth] == "varlistentry")
1629 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1631 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1632 environment_stack[depth].erase();
1633 environment_inner[depth].erase();
1636 if (depth == p->params().depth()
1637 && environment_stack[depth] != style->latexname()
1638 && !environment_stack[depth].empty()) {
1639 if (environment_inner[depth] != "!-- --") {
1640 item_name= "listitem";
1641 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1642 if (environment_inner[depth] == "varlistentry")
1643 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1646 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1648 environment_stack[depth].erase();
1649 environment_inner[depth].erase();
1652 // Write opening SGML tags.
1653 switch (style->latextype) {
1654 case LATEX_PARAGRAPH:
1655 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1659 buf->sgmlError(p, 0, _("Error: LatexType Command not allowed here.\n"));
1663 case LATEX_ENVIRONMENT:
1664 case LATEX_ITEM_ENVIRONMENT:
1665 if (depth < p->params().depth()) {
1666 depth = p->params().depth();
1667 environment_stack[depth].erase();
1670 if (environment_stack[depth] != style->latexname()) {
1671 if (environment_stack.size() == depth + 1) {
1672 environment_stack.push_back("!-- --");
1673 environment_inner.push_back("!-- --");
1675 environment_stack[depth] = style->latexname();
1676 environment_inner[depth] = "!-- --";
1677 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1679 if (environment_inner[depth] != "!-- --") {
1680 item_name= "listitem";
1681 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1682 if (environment_inner[depth] == "varlistentry")
1683 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1687 if (style->latextype == LATEX_ENVIRONMENT) {
1688 if (!style->latexparam().empty()) {
1689 if (style->latexparam() == "CDATA")
1692 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1697 desc_on = (style->labeltype == LABEL_MANUAL);
1699 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1700 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1702 item_name = desc_on ? "term" : "para";
1703 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1707 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1711 buf->simpleDocBookOnePar(os, p, desc_on, depth + 1 + command_depth);
1715 // write closing SGML tags
1716 switch (style->latextype) {
1717 case LATEX_ENVIRONMENT:
1718 if (!style->latexparam().empty()) {
1719 if (style->latexparam() == "CDATA")
1722 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1725 case LATEX_ITEM_ENVIRONMENT:
1726 if (desc_on == 1) break;
1728 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1730 case LATEX_PARAGRAPH:
1731 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1734 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1740 for (int d = depth; d >= 0; --d) {
1741 if (!environment_stack[depth].empty()) {
1742 if (environment_inner[depth] != "!-- --") {
1743 item_name = "listitem";
1744 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1745 if (environment_inner[depth] == "varlistentry")
1746 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1749 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1757 void InsetText::validate(LaTeXFeatures & features) const
1759 for_each(paragraphs.begin(), paragraphs.end(),
1760 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1764 int InsetText::beginningOfMainBody(Paragraph * p) const
1766 if (p->layout()->labeltype != LABEL_MANUAL)
1769 return p->beginningOfMainBody();
1773 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1775 if (the_locking_inset) {
1776 the_locking_inset->getCursorPos(bv, x, y);
1779 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1780 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1784 int InsetText::insetInInsetY() const
1786 if (!the_locking_inset)
1789 return (inset_y + the_locking_inset->insetInInsetY());
1793 void InsetText::toggleInsetCursor(BufferView * bv)
1795 if (the_locking_inset) {
1796 the_locking_inset->toggleInsetCursor(bv);
1800 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1802 int const asc = font_metrics::maxAscent(font);
1803 int const desc = font_metrics::maxDescent(font);
1805 if (isCursorVisible())
1806 bv->hideLockedInsetCursor();
1808 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1809 toggleCursorVisible();
1813 void InsetText::showInsetCursor(BufferView * bv, bool show)
1815 if (the_locking_inset) {
1816 the_locking_inset->showInsetCursor(bv, show);
1819 if (!isCursorVisible()) {
1820 LyXFont const font =
1821 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1823 int const asc = font_metrics::maxAscent(font);
1824 int const desc = font_metrics::maxDescent(font);
1826 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1828 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1829 setCursorVisible(true);
1834 void InsetText::hideInsetCursor(BufferView * bv)
1836 if (isCursorVisible()) {
1837 bv->hideLockedInsetCursor();
1838 setCursorVisible(false);
1840 if (the_locking_inset)
1841 the_locking_inset->hideInsetCursor(bv);
1845 void InsetText::fitInsetCursor(BufferView * bv) const
1847 if (the_locking_inset) {
1848 the_locking_inset->fitInsetCursor(bv);
1851 LyXFont const font =
1852 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1854 int const asc = font_metrics::maxAscent(font);
1855 int const desc = font_metrics::maxDescent(font);
1857 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1858 need_update |= FULL;
1863 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1865 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1866 return moveLeftIntern(bv, false, activate_inset, selecting);
1868 return moveRightIntern(bv, true, activate_inset, selecting);
1873 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1875 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1876 return moveRightIntern(bv, true, activate_inset, selecting);
1878 return moveLeftIntern(bv, false, activate_inset, selecting);
1883 InsetText::moveRightIntern(BufferView * bv, bool front,
1884 bool activate_inset, bool selecting)
1886 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1887 return FINISHED_RIGHT;
1888 if (activate_inset && checkAndActivateInset(bv, front))
1890 getLyXText(bv)->cursorRight(bv);
1892 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1893 return DISPATCHED_NOUPDATE;
1898 InsetText::moveLeftIntern(BufferView * bv, bool front,
1899 bool activate_inset, bool selecting)
1901 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1903 getLyXText(bv)->cursorLeft(bv);
1905 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1906 if (activate_inset && checkAndActivateInset(bv, front))
1908 return DISPATCHED_NOUPDATE;
1912 Inset::RESULT InsetText::moveUp(BufferView * bv)
1914 if (!crow(bv)->previous())
1916 getLyXText(bv)->cursorUp(bv);
1917 return DISPATCHED_NOUPDATE;
1921 Inset::RESULT InsetText::moveDown(BufferView * bv)
1923 if (!crow(bv)->next())
1924 return FINISHED_DOWN;
1925 getLyXText(bv)->cursorDown(bv);
1926 return DISPATCHED_NOUPDATE;
1930 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1932 if (the_locking_inset) {
1933 if (the_locking_inset->insetAllowed(inset))
1934 return the_locking_inset->insertInset(bv, inset);
1937 inset->setOwner(this);
1938 hideInsetCursor(bv);
1939 getLyXText(bv)->insertInset(bv, inset);
1941 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1946 bool InsetText::insetAllowed(Inset::Code code) const
1948 // in_insetAllowed is a really gross hack,
1949 // to allow us to call the owner's insetAllowed
1950 // without stack overflow, which can happen
1951 // when the owner uses InsetCollapsable::insetAllowed()
1953 if (in_insetAllowed)
1955 in_insetAllowed = true;
1956 if (the_locking_inset)
1957 ret = the_locking_inset->insetAllowed(code);
1959 ret = owner()->insetAllowed(code);
1960 in_insetAllowed = false;
1965 UpdatableInset * InsetText::getLockingInset() const
1967 return the_locking_inset ? the_locking_inset->getLockingInset() :
1968 const_cast<InsetText *>(this);
1972 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1976 if (the_locking_inset)
1977 return the_locking_inset->getFirstLockingInsetOfType(c);
1982 bool InsetText::showInsetDialog(BufferView * bv) const
1984 if (the_locking_inset)
1985 return the_locking_inset->showInsetDialog(bv);
1990 vector<string> const InsetText::getLabelList() const
1992 vector<string> label_list;
1994 ParagraphList::iterator pit = paragraphs.begin();
1995 ParagraphList::iterator pend = paragraphs.end();
1996 for (; pit != pend; ++pit) {
1997 InsetList::iterator beg = pit->insetlist.begin();
1998 InsetList::iterator end = pit->insetlist.end();
1999 for (; beg != end; ++beg) {
2000 vector<string> const l = beg.getInset()->getLabelList();
2001 label_list.insert(label_list.end(), l.begin(), l.end());
2008 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
2011 if (the_locking_inset) {
2012 the_locking_inset->setFont(bv, font, toggleall, selectall);
2015 if ((!paragraphs.begin()->next() && paragraphs.begin()->empty()) || cpar(bv)->empty()) {
2016 getLyXText(bv)->setFont(bv, font, toggleall);
2021 lt = getLyXText(bv);
2024 if (lt->selection.set()) {
2025 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
2029 lt->toggleFree(bv, font, toggleall);
2031 lt->clearSelection();
2033 bool flag = (selectall || lt->selection.set());
2037 updateLocal(bv, FULL, true);
2039 updateLocal(bv, CURSOR_PAR, true);
2043 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
2045 if (cpar(bv)->isInset(cpos(bv))) {
2047 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
2048 if (!isHighlyEditableInset(inset))
2050 inset->edit(bv, front);
2051 if (!the_locking_inset)
2053 updateLocal(bv, CURSOR, false);
2060 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2061 mouse_button::state button)
2063 x -= drawTextXOffset;
2065 int dummyy = y + insetAscent;
2066 Inset * inset = getLyXText(bv)->checkInsetHit(bv, dummyx, dummyy);
2067 // we only do the edit() call if the inset was hit by the mouse
2068 // or if it is a highly editable inset. So we should call this
2069 // function from our own edit with button < 0.
2070 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
2072 if (button == mouse_button::none && !isHighlyEditableInset(inset))
2080 inset_x = cix(bv) - top_x + drawTextXOffset;
2081 inset_y = ciy(bv) + drawTextYOffset;
2082 inset->edit(bv, x - inset_x, y - inset_y, button);
2083 if (!the_locking_inset)
2085 updateLocal(bv, CURSOR, false);
2092 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2095 int w = UpdatableInset::getMaxWidth(bv, inset);
2100 w = w - top_x + owner()->x();
2103 w -= (2 * TEXT_TO_INSET_OFFSET);
2106 return UpdatableInset::getMaxWidth(bv, inset);
2111 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2113 // we have to unlock any locked inset otherwise we're in troubles
2114 the_locking_inset = 0;
2117 paragraphs.set(new Paragraph(*p, same_id));
2118 paragraphs.begin()->setInsetOwner(this);
2119 Paragraph * np = &*(paragraphs.begin());
2122 np->next(new Paragraph(*p, same_id));
2123 np->next()->previous(np);
2125 np->setInsetOwner(this);
2132 void InsetText::setText(string const & data, LyXFont const & font)
2135 for (unsigned int i = 0; i < data.length(); ++i)
2136 paragraphs.begin()->insertChar(i, data[i], font);
2141 void InsetText::setAutoBreakRows(bool flag)
2143 if (flag != autoBreakRows) {
2144 autoBreakRows = flag;
2152 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2154 if (how != drawFrame_) {
2157 updateLocal(bv, DRAW_FRAME, false);
2162 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2164 if (frame_color != col) {
2167 updateLocal(bv, DRAW_FRAME, false);
2172 int InsetText::cx(BufferView * bv) const
2174 // we do nothing dangerous so we use a local cache
2175 LyXText * llt = getLyXText(bv);
2176 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2177 if (the_locking_inset) {
2178 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2180 if (font.isVisibleRightToLeft())
2181 x -= the_locking_inset->width(bv, font);
2187 int InsetText::cix(BufferView * bv) const
2189 // we do nothing dangerous so we use a local cache
2190 LyXText * llt = getLyXText(bv);
2191 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2192 if (the_locking_inset) {
2193 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2195 if (font.isVisibleRightToLeft())
2196 x -= the_locking_inset->width(bv, font);
2202 int InsetText::cy(BufferView * bv) const
2205 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2209 int InsetText::ciy(BufferView * bv) const
2212 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2216 pos_type InsetText::cpos(BufferView * bv) const
2218 return getLyXText(bv)->cursor.pos();
2222 Paragraph * InsetText::cpar(BufferView * bv) const
2224 return getLyXText(bv)->cursor.par();
2228 bool InsetText::cboundary(BufferView * bv) const
2230 return getLyXText(bv)->cursor.boundary();
2234 Row * InsetText::crow(BufferView * bv) const
2236 return getLyXText(bv)->cursor.row();
2240 LyXText * InsetText::getLyXText(BufferView const * lbv,
2241 bool const recursive) const
2243 if (cached_bview == lbv) {
2244 if (recursive && the_locking_inset)
2245 return the_locking_inset->getLyXText(lbv, true);
2246 LyXText * lt = cached_text.get();
2247 lyx::Assert(lt && lt->firstRow()->par() == &*(paragraphs.begin()));
2250 // Super UGLY! (Lgb)
2251 BufferView * bv = const_cast<BufferView *>(lbv);
2254 Cache::iterator it = cache.find(bv);
2256 if (it != cache.end()) {
2259 } else if (do_resize) {
2260 resizeLyXText(do_resize);
2262 if (lt || !it->second.remove) {
2263 lyx::Assert(it->second.text.get());
2264 cached_text = it->second.text;
2265 if (recursive && the_locking_inset) {
2266 return the_locking_inset->getLyXText(bv, true);
2268 return cached_text.get();
2269 } else if (it->second.remove) {
2271 saveLyXTextState(it->second.text.get());
2277 // when we have to reinit the existing LyXText!
2279 it->second.text->init(bv);
2280 restoreLyXTextState(bv, it->second.text.get());
2281 it->second.remove = false;
2283 cached_text = it->second.text;
2284 if (the_locking_inset && recursive) {
2285 return the_locking_inset->getLyXText(bv);
2287 return cached_text.get();
2290 // we are here only if we don't have a BufferView * in the cache!!!
2292 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2293 cached_text->init(bv);
2294 restoreLyXTextState(bv, cached_text.get());
2296 cache.insert(make_pair(bv, cached_text));
2298 if (the_locking_inset && recursive) {
2299 return the_locking_inset->getLyXText(bv);
2301 return cached_text.get();
2305 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2309 Cache::iterator it = cache.find(bv);
2311 if (it == cache.end()) {
2315 lyx::Assert(it->second.text.get());
2317 it->second.remove = true;
2319 /// then remove all LyXText in text-insets
2320 for_each(paragraphs.begin(), paragraphs.end(),
2321 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2326 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2329 // we cannot resize this because we are in use!
2330 // so do this on the next possible getLyXText()
2335 // lyxerr << "InsetText::resizeLyXText\n";
2336 if (!paragraphs.begin()->next() && paragraphs.begin()->empty()) { // no data, resize not neccessary!
2337 // we have to do this as a fixed width may have changed!
2338 LyXText * t = getLyXText(bv);
2339 saveLyXTextState(t);
2341 restoreLyXTextState(bv, t);
2344 // one endless line, resize normally not necessary
2345 if (!force && getMaxWidth(bv, this) < 0)
2348 Cache::iterator it = cache.find(bv);
2349 if (it == cache.end()) {
2352 lyx::Assert(it->second.text.get());
2354 LyXText * t = it->second.text.get();
2355 saveLyXTextState(t);
2357 for_each(paragraphs.begin(), paragraphs.end(),
2358 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2361 restoreLyXTextState(bv, t);
2362 if (the_locking_inset) {
2363 inset_x = cix(bv) - top_x + drawTextXOffset;
2364 inset_y = ciy(bv) + drawTextYOffset;
2367 t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2369 updateLocal(bv, FULL, false);
2370 // this will scroll the screen such that the cursor becomes visible
2371 bv->updateScrollbar();
2373 need_update |= FULL;
2378 void InsetText::reinitLyXText() const
2381 // we cannot resize this because we are in use!
2382 // so do this on the next possible getLyXText()
2388 // lyxerr << "InsetText::reinitLyXText\n";
2389 for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2390 lyx::Assert(it->second.text.get());
2392 LyXText * t = it->second.text.get();
2393 BufferView * bv = it->first;
2395 saveLyXTextState(t);
2397 for_each(paragraphs.begin(), paragraphs.end(),
2398 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2401 restoreLyXTextState(bv, t);
2402 if (the_locking_inset) {
2403 inset_x = cix(bv) - top_x + drawTextXOffset;
2404 inset_y = ciy(bv) + drawTextYOffset;
2406 t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2408 updateLocal(bv, FULL, false);
2409 // this will scroll the screen such that the cursor becomes visible
2410 bv->updateScrollbar();
2418 void InsetText::removeNewlines()
2420 bool changed = false;
2422 ParagraphList::iterator it = paragraphs.begin();
2423 ParagraphList::iterator end = paragraphs.end();
2424 for (; it != end; ++it) {
2425 for (int i = 0; i < it->size(); ++i) {
2426 if (it->getChar(i) == Paragraph::META_NEWLINE) {
2437 bool InsetText::nodraw() const
2439 if (the_locking_inset)
2440 return the_locking_inset->nodraw();
2441 return UpdatableInset::nodraw();
2445 int InsetText::scroll(bool recursive) const
2447 int sx = UpdatableInset::scroll(false);
2449 if (recursive && the_locking_inset)
2450 sx += the_locking_inset->scroll(recursive);
2456 bool InsetText::doClearArea() const
2458 return !locked || (need_update & (FULL|INIT));
2462 void InsetText::selectAll(BufferView * bv)
2464 getLyXText(bv)->cursorTop(bv);
2465 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2466 getLyXText(bv)->cursorBottom(bv);
2467 getLyXText(bv)->setSelection(bv);
2471 void InsetText::clearSelection(BufferView * bv)
2473 getLyXText(bv)->clearSelection();
2477 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2479 Painter & pain = bv->painter();
2481 int h = insetAscent + insetDescent;
2482 int ty = baseline - insetAscent;
2488 if ((ty + h) > pain.paperHeight())
2489 h = pain.paperHeight();
2490 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2491 w = pain.paperWidth();
2492 // w -= TEXT_TO_INSET_OFFSET;
2493 pain.fillRectangle(top_x + 1, ty + 1, w - 1, h - 1, backgroundColor());
2496 frame_is_visible = false;
2500 Paragraph * InsetText::getParFromID(int id) const
2502 ParagraphList::iterator it = paragraphs.begin();
2503 ParagraphList::iterator end = paragraphs.end();
2504 for (; it != end; ++it) {
2505 if (it->id() == id) {
2508 Paragraph * tmp2 = it->getParFromID(id);
2517 Paragraph * InsetText::firstParagraph() const
2520 if (the_locking_inset)
2521 if ((result = the_locking_inset->firstParagraph()))
2523 return &*(paragraphs.begin());
2527 Paragraph * InsetText::getFirstParagraph(int i) const
2529 return (i == 0) ? &*(paragraphs.begin()) : 0;
2533 LyXCursor const & InsetText::cursor(BufferView * bv) const
2535 if (the_locking_inset)
2536 return the_locking_inset->cursor(bv);
2537 return getLyXText(bv)->cursor;
2541 Paragraph * InsetText::paragraph() const
2543 return &*(paragraphs.begin());
2547 void InsetText::paragraph(Paragraph * p)
2549 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2550 // caller of this function has to take care of it. This IS important
2551 // as we could have to insert a paragraph before this one and just
2552 // link the actual to a new ones next and set it with this function
2555 // set ourself as owner for all the paragraphs inserted!
2556 for_each(paragraphs.begin(), paragraphs.end(),
2557 boost::bind(&Paragraph::setInsetOwner, _1, this));
2560 // redraw myself when asked for
2565 Inset * InsetText::getInsetFromID(int id_arg) const
2568 return const_cast<InsetText *>(this);
2570 ParagraphList::iterator pit = paragraphs.begin();
2571 ParagraphList::iterator pend = paragraphs.end();
2572 for (; pit != pend; ++pit) {
2573 InsetList::iterator it = pit->insetlist.begin();
2574 InsetList::iterator end = pit->insetlist.end();
2575 for (; it != end; ++it) {
2576 if (it.getInset()->id() == id_arg)
2577 return it.getInset();
2578 Inset * in = it.getInset()->getInsetFromID(id_arg);
2588 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2589 float & value) const
2595 lt = getLyXText(bv);
2598 if (the_locking_inset) {
2599 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2600 if (!word.word().empty()) {
2606 // we have to go on checking so move cursor to the next char
2607 lt->cursor.pos(lt->cursor.pos() + 1);
2609 word = lt->selectNextWordToSpellcheck(bv, value);
2610 if (word.word().empty())
2611 bv->unlockInset(const_cast<InsetText *>(this));
2620 void InsetText::selectSelectedWord(BufferView * bv)
2622 if (the_locking_inset) {
2623 the_locking_inset->selectSelectedWord(bv);
2626 getLyXText(bv)->selectSelectedWord(bv);
2627 updateLocal(bv, SELECTION, false);
2631 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2633 if (the_locking_inset) {
2634 the_locking_inset->toggleSelection(bv, kill_selection);
2638 lt = getLyXText(bv);
2642 int x = top_x + TEXT_TO_INSET_OFFSET;
2644 Row * row = lt->firstRow();
2645 int y_offset = top_baseline - row->ascent_of_text();
2647 while ((row != 0) && ((y+row->height()) <= 0)) {
2654 if (need_update & SELECTION)
2656 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2662 bool InsetText::searchForward(BufferView * bv, string const & str,
2667 lt = getLyXText(bv);
2670 if (the_locking_inset) {
2671 if (the_locking_inset->searchForward(bv, str, cs, mw))
2673 lt->cursorRight(bv, true);
2675 lyxfind::SearchResult result =
2676 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2678 if (result == lyxfind::SR_FOUND) {
2679 LyXCursor cur = lt->cursor;
2680 bv->unlockInset(bv->theLockingInset());
2681 if (bv->lockInset(this))
2684 lt->setSelectionOverString(bv, str);
2685 updateLocal(bv, SELECTION, false);
2689 return (result != lyxfind::SR_NOT_FOUND);
2692 bool InsetText::searchBackward(BufferView * bv, string const & str,
2695 if (the_locking_inset) {
2696 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2701 lt = getLyXText(bv);
2705 Paragraph * p = &*(paragraphs.begin());
2708 lt->setCursor(bv, p, p->size());
2710 lyxfind::SearchResult result =
2711 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2713 if (result == lyxfind::SR_FOUND) {
2714 LyXCursor cur = lt->cursor;
2715 bv->unlockInset(bv->theLockingInset());
2716 if (bv->lockInset(this))
2719 lt->setSelectionOverString(bv, str);
2720 updateLocal(bv, SELECTION, false);
2724 return (result != lyxfind::SR_NOT_FOUND);
2728 bool InsetText::checkInsertChar(LyXFont & font)
2731 return owner()->checkInsertChar(font);
2736 void InsetText::collapseParagraphs(BufferView * bv) const
2738 BufferParams const & bparams = bv->buffer()->params;
2739 LyXText * llt = getLyXText(bv);
2741 while (paragraphs.begin()->next()) {
2742 if (!paragraphs.begin()->empty() && !paragraphs.begin()->next()->empty() &&
2743 !paragraphs.begin()->isSeparator(paragraphs.begin()->size() - 1))
2745 paragraphs.begin()->insertChar(paragraphs.begin()->size(), ' ');
2747 if (llt->selection.set()) {
2748 if (llt->selection.start.par() == paragraphs.begin()->next()) {
2749 llt->selection.start.par(&*(paragraphs.begin()));
2750 llt->selection.start.pos(
2751 llt->selection.start.pos() + paragraphs.begin()->size());
2753 if (llt->selection.end.par() == paragraphs.begin()->next()) {
2754 llt->selection.end.par(&*(paragraphs.begin()));
2755 llt->selection.end.pos(
2756 llt->selection.end.pos() + paragraphs.begin()->size());
2759 mergeParagraph(bparams, &*paragraphs.begin());
2765 void InsetText::getDrawFont(LyXFont & font) const
2769 owner()->getDrawFont(font);
2773 void InsetText::appendParagraphs(BufferParams const & bparams,
2777 Paragraph * tmpbuf = newpar;
2778 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2780 while (tmpbuf->next()) {
2781 tmpbuf = tmpbuf->next();
2782 lastbuffer->next(new Paragraph(*tmpbuf, false));
2783 lastbuffer->next()->previous(lastbuffer);
2784 lastbuffer = lastbuffer->next();
2786 lastbuffer = &*(paragraphs.begin());
2787 while (lastbuffer->next())
2788 lastbuffer = lastbuffer->next();
2789 if (!newpar->empty() && !lastbuffer->empty() &&
2790 !lastbuffer->isSeparator(lastbuffer->size() - 1))
2792 lastbuffer->insertChar(lastbuffer->size(), ' ');
2795 // make the buf exactly the same layout than our last paragraph
2796 buf->makeSameLayout(lastbuffer);
2799 lastbuffer->next(buf);
2800 buf->previous(lastbuffer);
2801 mergeParagraph(bparams, lastbuffer);
2807 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2809 Paragraph * par = getFirstParagraph(0);
2811 InsetList::iterator it = par->insetlist.begin();
2812 InsetList::iterator end = par->insetlist.end();
2813 for (; it != end; ++it) {
2814 it.getInset()->addPreview(loader);