3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS
14 #include "insettext.h"
17 #include "BufferView.h"
18 #include "CutAndPaste.h"
20 #include "funcrequest.h"
23 #include "LaTeXFeatures.h"
26 #include "lyxcursor.h"
32 #include "paragraph.h"
33 #include "ParagraphParameters.h"
34 #include "trans_mgr.h"
35 #include "undo_funcs.h"
36 #include "WordLangTuple.h"
37 #include "paragraph_funcs.h"
39 #include "rowpainter.h"
40 #include "insetnewline.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
54 #include <boost/bind.hpp>
72 using lyx::textclass_type;
74 // These functions should probably go into bufferview_funcs somehow (Jug)
76 void InsetText::saveLyXTextState(LyXText * t) const
78 // check if my paragraphs are still valid
79 ParagraphList::iterator it = paragraphs.begin();
80 ParagraphList::iterator end = paragraphs.end();
81 for (; it != end; ++it) {
82 if (&*it == t->cursor.par())
86 if (it != end && t->cursor.pos() <= it->size()) {
87 sstate.lpar = t->cursor.par();
88 sstate.pos = t->cursor.pos();
89 sstate.boundary = t->cursor.boundary();
90 sstate.selstartpar = t->selection.start.par();
91 sstate.selstartpos = t->selection.start.pos();
92 sstate.selstartboundary = t->selection.start.boundary();
93 sstate.selendpar = t->selection.end.par();
94 sstate.selendpos = t->selection.end.pos();
95 sstate.selendboundary = t->selection.end.boundary();
96 sstate.selection = t->selection.set();
97 sstate.mark_set = t->selection.mark();
104 void InsetText::restoreLyXTextState(LyXText * t) const
109 t->selection.set(true);
110 /* at this point just to avoid the DEPM when setting the cursor */
111 t->selection.mark(sstate.mark_set);
112 if (sstate.selection) {
113 t->setCursor(sstate.selstartpar, sstate.selstartpos,
114 true, sstate.selstartboundary);
115 t->selection.cursor = t->cursor;
116 t->setCursor(sstate.selendpar, sstate.selendpos,
117 true, sstate.selendboundary);
119 t->setCursor(sstate.lpar, sstate.pos);
121 t->setCursor(sstate.lpar, sstate.pos, true, sstate.boundary);
122 t->selection.cursor = t->cursor;
123 t->selection.set(false);
128 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
135 InsetText::InsetText(BufferParams const & bp)
136 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
139 paragraphs.set(new Paragraph);
140 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
141 if (bp.tracking_changes)
142 paragraphs.begin()->trackChanges();
147 InsetText::InsetText(InsetText const & in, bool same_id)
148 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
155 InsetText & InsetText::operator=(InsetText const & it)
162 void InsetText::init(InsetText const * ins, bool same_id)
165 setParagraphData(&*(ins->paragraphs.begin()), same_id);
166 autoBreakRows = ins->autoBreakRows;
167 drawFrame_ = ins->drawFrame_;
168 frame_color = ins->frame_color;
172 for_each(paragraphs.begin(), paragraphs.end(),
173 boost::bind(&Paragraph::setInsetOwner, _1, this));
175 the_locking_inset = 0;
177 frame_color = LColor::insetframe;
178 autoBreakRows = false;
191 last_drawn_width = -1;
194 in_insetAllowed = false;
198 InsetText::~InsetText()
204 void InsetText::clear(bool just_mark_erased)
206 if (just_mark_erased) {
207 ParagraphList::iterator it = paragraphs.begin();
208 ParagraphList::iterator end = paragraphs.end();
209 for (; it != end; ++it) {
216 // This is a gross hack...
217 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
220 paragraphs.set(new Paragraph);
221 paragraphs.begin()->setInsetOwner(this);
222 paragraphs.begin()->layout(old_layout);
229 Inset * InsetText::clone(Buffer const &, bool same_id) const
231 return new InsetText(*this, same_id);
235 void InsetText::write(Buffer const * buf, ostream & os) const
238 writeParagraphData(buf, os);
242 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
244 ParagraphList::iterator it = paragraphs.begin();
245 ParagraphList::iterator end = paragraphs.end();
246 Paragraph::depth_type dth = 0;
247 for (; it != end; ++it) {
248 it->write(buf, os, buf->params, dth);
253 void InsetText::read(Buffer const * buf, LyXLex & lex)
256 Paragraph::depth_type depth = 0;
260 if (buf->params.tracking_changes)
261 paragraphs.begin()->trackChanges();
263 // delete the initial paragraph
265 ParagraphList::iterator pit = paragraphs.begin();
269 token = lex.getString();
272 if (token == "\\end_inset") {
276 if (token == "\\the_end") {
277 lex.printError("\\the_end read in inset! Error in document!");
282 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
285 pit = paragraphs.begin();
286 ParagraphList::iterator const end = paragraphs.end();
287 for (; pit != end; ++pit)
288 pit->setInsetOwner(this);
290 if (token != "\\end_inset") {
291 lex.printError("Missing \\end_inset at this point. "
298 int InsetText::ascent(BufferView * bv, LyXFont const &) const
300 insetAscent = getLyXText(bv)->rows().begin()->ascent_of_text() +
301 TEXT_TO_INSET_OFFSET;
306 int InsetText::descent(BufferView * bv, LyXFont const &) const
308 LyXText * llt = getLyXText(bv);
309 insetDescent = llt->height - llt->rows().begin()->ascent_of_text() +
310 TEXT_TO_INSET_OFFSET;
315 int InsetText::width(BufferView * bv, LyXFont const &) const
317 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
318 (2 * TEXT_TO_INSET_OFFSET);
319 insetWidth = max(insetWidth, 10);
324 int InsetText::textWidth(BufferView * bv, bool fordraw) const
327 if (!autoBreakRows) {
330 w = getMaxWidth(bv, this);
333 return max(w - (2 * TEXT_TO_INSET_OFFSET),
334 (int)getLyXText(bv)->width);
338 return w - (2 * TEXT_TO_INSET_OFFSET);
342 void InsetText::draw(BufferView * bv, LyXFont const & f,
343 int baseline, float & x) const
348 // update our idea of where we are. Clearly, we should
349 // not have to know this information.
353 int const start_x = int(x);
355 Painter & pain = bv->painter();
357 // call these methods so that insetWidth, insetAscent and
358 // insetDescent have the right values.
363 // repaint the background if needed
364 if (backgroundColor() != LColor::background)
365 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
367 // no draw is necessary !!!
368 if ((drawFrame_ == LOCKED) && !locked && paragraphs.begin()->empty()) {
369 top_baseline = baseline;
376 x += static_cast<float>(scroll());
378 top_baseline = baseline;
379 top_y = baseline - insetAscent;
381 if (last_drawn_width != insetWidth) {
383 last_drawn_width = insetWidth;
386 if (the_locking_inset && (cpar(bv) == inset_par)
387 && (cpos(bv) == inset_pos)) {
388 inset_x = cix(bv) - int(x) + drawTextXOffset;
389 inset_y = ciy(bv) + drawTextYOffset;
397 x += TEXT_TO_INSET_OFFSET;
399 RowList::iterator rowit = lt->rows().begin();
400 RowList::iterator end = lt->rows().end();
402 int y_offset = baseline - rowit->ascent_of_text();
403 int ph = pain.paperHeight();
406 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
407 y += rowit->height();
408 first += rowit->height();
412 lt->top_y(-y_offset);
420 int yf = y_offset + first;
422 while ((rowit != end) && (yf < ph)) {
423 RowPainter rp(*bv, *lt, *rowit);
424 rp.paint(y + y_offset + first, int(x), y + lt->top_y());
425 y += rowit->height();
426 yf += rowit->height();
432 if ((drawFrame_ == ALWAYS) || (drawFrame_ == LOCKED && locked)) {
433 drawFrame(pain, int(start_x));
436 x += insetWidth - TEXT_TO_INSET_OFFSET;
438 if (need_update != INIT) {
446 void InsetText::drawFrame(Painter & pain, int x) const
448 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
450 frame_y = top_baseline - insetAscent + ttoD2;
451 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
452 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
453 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
458 void InsetText::update(BufferView * bv, bool reinit)
461 if (reinit && owner()) {
463 owner()->update(bv, true);
469 if (reinit || need_update == INIT) {
471 // we should put this call where we set need_update to INIT!
474 owner()->update(bv, true);
477 int nw = getMaxWidth(bv, this);
478 if (nw > 0 && old_max_width != nw) {
485 if (!autoBreakRows && paragraphs.begin()->next())
486 collapseParagraphs(bv);
488 if (the_locking_inset) {
489 inset_x = cix(bv) - top_x + drawTextXOffset;
490 inset_y = ciy(bv) + drawTextYOffset;
491 the_locking_inset->update(bv, reinit);
499 if ((need_update & CURSOR_PAR) && (lt->refreshStatus() == LyXText::REFRESH_NONE) &&
501 lt->updateInset(the_locking_inset);
504 if (lt->refreshStatus() == LyXText::REFRESH_AREA)
511 int nw = getMaxWidth(bv, this);
512 if (nw > 0 && old_max_width != nw) {
519 void InsetText::setUpdateStatus(BufferView * bv, int what) const
521 // this does nothing dangerous so use only a localized buffer
522 LyXText * llt = getLyXText(bv);
525 // we have to redraw us full if our LyXText REFRESH_AREA or
526 // if we don't break row so that we only have one row to update!
527 if ((llt->refreshStatus() == LyXText::REFRESH_AREA) ||
529 (llt->refreshStatus() == LyXText::REFRESH_ROW)))
532 } else if (llt->refreshStatus() == LyXText::REFRESH_ROW) {
533 need_update |= CURSOR_PAR;
536 // this to not draw a selection when we redraw all of it!
537 if (need_update & CURSOR && !(need_update & SELECTION)) {
538 if (llt->selection.set())
540 llt->clearSelection();
545 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
547 if (!autoBreakRows && paragraphs.begin()->next())
548 collapseParagraphs(bv);
555 setUpdateStatus(bv, what);
556 bool flag = mark_dirty ||
557 (((need_update != CURSOR) && (need_update != NONE)) ||
558 (lt->refreshStatus() != LyXText::REFRESH_NONE) || lt->selection.set());
559 if (!lt->selection.set())
560 lt->selection.cursor = lt->cursor;
566 bv->updateInset(const_cast<InsetText *>(this));
572 if (need_update == CURSOR)
574 bv->owner()->view_state_changed();
575 bv->owner()->updateMenubar();
576 bv->owner()->updateToolbar();
577 if (old_par != cpar(bv)) {
578 bv->owner()->setLayout(cpar(bv)->layout()->name());
584 string const InsetText::editMessage() const
586 return _("Opened Text Inset");
590 void InsetText::edit(BufferView * bv, int x, int y, mouse_button::state button)
592 UpdatableInset::edit(bv, x, y, button);
594 if (!bv->lockInset(this)) {
595 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
599 the_locking_inset = 0;
600 inset_pos = inset_x = inset_y = 0;
601 inset_boundary = false;
604 int tmp_y = (y < 0) ? 0 : y;
610 // we put here -1 and not button as now the button in the
611 // edit call should not be needed we will fix this in 1.3.x
612 // cycle hopefully (Jug 20020509)
613 // FIXME: GUII I've changed this to none: probably WRONG
614 if (!checkAndActivateInset(bv, x, tmp_y, mouse_button::none)) {
615 lt->setCursorFromCoordinates(x - drawTextXOffset,
617 lt->cursor.x_fix(lt->cursor.x());
619 lt->clearSelection();
621 // If the inset is empty set the language of the current font to the
622 // language to the surronding text (if different).
623 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
624 bv->getParentLanguage(this) != lt->current_font.language())
626 LyXFont font(LyXFont::ALL_IGNORE);
627 font.setLanguage(bv->getParentLanguage(this));
628 setFont(bv, font, false);
634 if (drawFrame_ == LOCKED)
635 code = CURSOR|DRAW_FRAME;
636 updateLocal(bv, code, false);
639 // Tell the paragraph dialog that we've entered an insettext.
640 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
644 void InsetText::edit(BufferView * bv, bool front)
646 UpdatableInset::edit(bv, front);
648 if (!bv->lockInset(this)) {
649 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
653 the_locking_inset = 0;
654 inset_pos = inset_x = inset_y = 0;
655 inset_boundary = false;
664 lt->setCursor(&*(paragraphs.begin()), 0);
666 ParagraphList::iterator it = paragraphs.begin();
667 ParagraphList::iterator end = paragraphs.end();
668 while (boost::next(it) != end)
670 // int const pos = (p->size() ? p->size()-1 : p->size());
671 lt->setCursor(&*it, it->size());
673 lt->clearSelection();
675 // If the inset is empty set the language of the current font to the
676 // language to the surronding text (if different).
677 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
678 bv->getParentLanguage(this) != lt->current_font.language()) {
679 LyXFont font(LyXFont::ALL_IGNORE);
680 font.setLanguage(bv->getParentLanguage(this));
681 setFont(bv, font, false);
686 if (drawFrame_ == LOCKED)
687 code = CURSOR|DRAW_FRAME;
688 updateLocal(bv, code, false);
693 void InsetText::insetUnlock(BufferView * bv)
695 if (the_locking_inset) {
696 the_locking_inset->insetUnlock(bv);
697 the_locking_inset = 0;
698 updateLocal(bv, CURSOR_PAR, false);
710 if (lt->selection.set()) {
711 lt->clearSelection();
713 } else if (owner()) {
714 bv->owner()->setLayout(owner()->getLyXText(bv)
715 ->cursor.par()->layout()->name());
717 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
718 // hack for deleteEmptyParMech
719 if (!paragraphs.begin()->empty()) {
720 lt->setCursor(&*(paragraphs.begin()), 0);
721 } else if (paragraphs.begin()->next()) {
722 lt->setCursor(paragraphs.begin()->next(), 0);
727 updateLocal(bv, code, false);
730 setUpdateStatus(bv, code);
735 void InsetText::lockInset(BufferView * bv)
738 the_locking_inset = 0;
739 inset_pos = inset_x = inset_y = 0;
740 inset_boundary = false;
748 lt->setCursor(&*(paragraphs.begin()), 0);
749 lt->clearSelection();
751 // If the inset is empty set the language of the current font to the
752 // language to the surronding text (if different).
753 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
754 bv->getParentLanguage(this) != lt->current_font.language()) {
755 LyXFont font(LyXFont::ALL_IGNORE);
756 font.setLanguage(bv->getParentLanguage(this));
757 setFont(bv, font, false);
762 if (drawFrame_ == LOCKED)
763 code = CURSOR|DRAW_FRAME;
764 setUpdateStatus(bv, code);
768 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
770 the_locking_inset = inset;
771 inset_x = cix(bv) - top_x + drawTextXOffset;
772 inset_y = ciy(bv) + drawTextYOffset;
773 inset_pos = cpos(bv);
774 inset_par = cpar(bv);
775 inset_boundary = cboundary(bv);
776 updateLocal(bv, CURSOR, false);
780 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
782 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
786 if (!the_locking_inset) {
787 Paragraph * p = &*(paragraphs.begin());
788 int const id = inset->id();
790 InsetList::iterator it =
791 p->insetlist.begin();
792 InsetList::iterator const end =
794 for (; it != end; ++it) {
795 if (it.getInset() == inset) {
796 getLyXText(bv)->setCursorIntern(p, it.getPos());
797 lockInset(bv, inset);
800 if (it.getInset()->getInsetFromID(id)) {
801 getLyXText(bv)->setCursorIntern(p, it.getPos());
802 it.getInset()->edit(bv);
803 return the_locking_inset->lockInsetInInset(bv, inset);
810 if (inset == cpar(bv)->getInset(cpos(bv))) {
811 lyxerr[Debug::INSETS] << "OK" << endl;
812 lockInset(bv, inset);
814 } else if (the_locking_inset && (the_locking_inset == inset)) {
815 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
816 lyxerr[Debug::INSETS] << "OK" << endl;
817 inset_x = cix(bv) - top_x + drawTextXOffset;
818 inset_y = ciy(bv) + drawTextYOffset;
820 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
822 } else if (the_locking_inset) {
823 lyxerr[Debug::INSETS] << "MAYBE" << endl;
824 return the_locking_inset->lockInsetInInset(bv, inset);
826 lyxerr[Debug::INSETS] << "NOT OK" << endl;
831 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
834 if (!the_locking_inset)
836 if (the_locking_inset == inset) {
837 the_locking_inset->insetUnlock(bv);
838 getLyXText(bv)->updateInset(inset);
839 the_locking_inset = 0;
841 moveRightIntern(bv, true, false);
842 old_par = 0; // force layout setting
846 updateLocal(bv, CURSOR, false);
849 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
853 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
855 if (!autoBreakRows && paragraphs.begin()->next())
856 collapseParagraphs(bv);
864 if (inset->owner() != this) {
865 int ustat = CURSOR_PAR;
867 UpdatableInset * tl_inset = the_locking_inset;
869 found = tl_inset->updateInsetInInset(bv, inset);
871 tl_inset = static_cast<UpdatableInset *>(inset);
872 while(tl_inset->owner() && tl_inset->owner() != this)
873 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
874 if (!tl_inset->owner())
876 found = tl_inset->updateInsetInInset(bv, inset);
880 lt->updateInset(tl_inset);
884 setUpdateStatus(bv, ustat);
887 bool found = lt->updateInset(inset);
891 setUpdateStatus(bv, CURSOR_PAR);
892 if (the_locking_inset &&
893 cpar(bv) == inset_par && cpos(bv) == inset_pos)
895 inset_x = cix(bv) - top_x + drawTextXOffset;
896 inset_y = ciy(bv) + drawTextYOffset;
903 void InsetText::lfunMousePress(FuncRequest const & cmd)
907 // use this to check mouse motion for selection!
911 BufferView * bv = cmd.view();
912 FuncRequest cmd1 = cmd;
918 int tmp_x = cmd.x - drawTextXOffset;
919 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->top_y();
920 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
923 if (the_locking_inset) {
924 if (the_locking_inset == inset) {
925 the_locking_inset->localDispatch(cmd1);
928 // otherwise only unlock the_locking_inset
929 the_locking_inset->insetUnlock(bv);
930 the_locking_inset = 0;
933 no_selection = false;
935 if (bv->theLockingInset()) {
936 if (isHighlyEditableInset(inset)) {
937 // We just have to lock the inset before calling a
939 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
940 if (!bv->lockInset(uinset)) {
941 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
943 inset->localDispatch(cmd1);
944 if (the_locking_inset)
945 updateLocal(bv, CURSOR, false);
950 bool paste_internally = false;
951 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
952 localDispatch(FuncRequest(bv, LFUN_COPY));
953 paste_internally = true;
960 int old_top_y = lt->top_y();
962 lt->setCursorFromCoordinates(cmd.x - drawTextXOffset,
963 cmd.y + insetAscent);
964 // set the selection cursor!
965 lt->selection.cursor = lt->cursor;
966 lt->cursor.x_fix(lt->cursor.x());
968 if (lt->selection.set()) {
969 lt->clearSelection();
970 updateLocal(bv, FULL, false);
972 lt->clearSelection();
973 updateLocal(bv, CURSOR, false);
979 bv->owner()->setLayout(cpar(bv)->layout()->name());
981 // we moved the view we cannot do mouse selection in this case!
982 if (getLyXText(bv)->top_y() != old_top_y)
985 // Insert primary selection with middle mouse
986 // if there is a local selection in the current buffer,
988 if (cmd.button() == mouse_button::button2) {
989 if (paste_internally)
990 localDispatch(FuncRequest(bv, LFUN_PASTE));
992 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
995 getLyXText(bv)->clearSelection();
1001 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
1003 BufferView * bv = cmd.view();
1004 FuncRequest cmd1 = cmd;
1008 no_selection = true;
1009 if (the_locking_inset)
1010 return the_locking_inset->localDispatch(cmd1);
1012 int tmp_x = cmd.x - drawTextXOffset;
1013 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->top_y();
1014 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
1017 if (isHighlyEditableInset(inset))
1018 ret = inset->localDispatch(cmd1);
1020 inset_x = cix(bv) - top_x + drawTextXOffset;
1021 inset_y = ciy(bv) + drawTextYOffset;
1022 cmd1.x = cmd.x - inset_x;
1023 cmd1.y = cmd.x - inset_y;
1024 // note that we should do ret = inset->localDispatch(cmd1)
1025 // and fix this instead (Alfredo);
1027 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
1029 updateLocal(bv, CURSOR_PAR, false);
1035 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
1037 FuncRequest cmd1 = cmd;
1041 if (the_locking_inset) {
1042 the_locking_inset->localDispatch(cmd1);
1046 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
1049 BufferView * bv = cmd.view();
1052 lt = getLyXText(bv);
1055 hideInsetCursor(bv);
1056 LyXCursor cur = lt->cursor;
1057 lt->setCursorFromCoordinates
1058 (cmd.x - drawTextXOffset, cmd.y + insetAscent);
1059 lt->cursor.x_fix(lt->cursor.x());
1060 if (cur == lt->cursor) {
1066 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1067 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1071 updateLocal(bv, SELECTION, false);
1073 showInsetCursor(bv);
1077 Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
1079 BufferView * bv = ev.view();
1080 switch (ev.action) {
1081 case LFUN_MOUSE_PRESS:
1084 case LFUN_MOUSE_MOTION:
1085 lfunMouseMotion(ev);
1087 case LFUN_MOUSE_RELEASE:
1088 return lfunMouseRelease(ev) ? DISPATCHED : UNDISPATCHED;
1093 bool was_empty = (paragraphs.begin()->empty() && !paragraphs.begin()->next());
1094 no_selection = false;
1095 RESULT result = UpdatableInset::localDispatch(ev);
1096 if (result != UNDISPATCHED)
1099 result = DISPATCHED;
1100 if (ev.action < 0 && ev.argument.empty())
1103 if (the_locking_inset) {
1104 result = the_locking_inset->localDispatch(ev);
1105 if (result == DISPATCHED_NOUPDATE)
1107 else if (result == DISPATCHED) {
1108 updateLocal(bv, CURSOR_PAR, false);
1110 } else if (result >= FINISHED) {
1112 case FINISHED_RIGHT:
1113 moveRightIntern(bv, false, false);
1114 result = DISPATCHED;
1117 if ((result = moveUp(bv)) >= FINISHED) {
1118 updateLocal(bv, CURSOR, false);
1119 bv->unlockInset(this);
1123 if ((result = moveDown(bv)) >= FINISHED) {
1124 updateLocal(bv, CURSOR, false);
1125 bv->unlockInset(this);
1129 result = DISPATCHED;
1132 the_locking_inset = 0;
1133 updateLocal(bv, CURSOR, false);
1137 hideInsetCursor(bv);
1140 lt = getLyXText(bv);
1144 int updflag = false;
1146 // what type of update to do on a cursor movement
1147 int cursor_update = CURSOR;
1149 if (lt->selection.set())
1150 cursor_update = SELECTION;
1152 switch (ev.action) {
1155 case LFUN_SELFINSERT:
1156 if (bv->buffer()->isReadonly()) {
1157 // setErrorMessage(N_("Document is read only"));
1160 if (!ev.argument.empty()) {
1161 /* Automatically delete the currently selected
1162 * text and replace it with what is being
1163 * typed in now. Depends on lyxrc settings
1164 * "auto_region_delete", which defaults to
1167 // This should not be needed here and is also WRONG!
1168 setUndo(bv, Undo::INSERT,
1169 lt->cursor.par(), lt->cursor.par()->next());
1172 if (lyxrc.auto_region_delete) {
1173 if (lt->selection.set()) {
1174 lt->cutSelection(false, false);
1177 lt->clearSelection();
1178 for (string::size_type i = 0; i < ev.argument.length(); ++i) {
1179 bv->owner()->getIntl().getTransManager().
1180 TranslateAndInsert(ev.argument[i], lt);
1183 lt->selection.cursor = lt->cursor;
1184 updwhat = CURSOR | CURSOR_PAR;
1186 result = DISPATCHED_NOUPDATE;
1189 // cursor movements that need special handling
1192 result = moveRight(bv);
1194 updwhat = cursor_update;
1198 result = moveLeft(bv);
1199 updwhat = cursor_update;
1203 result = moveDown(bv);
1204 updwhat = cursor_update;
1208 result = moveUp(bv);
1209 updwhat = cursor_update;
1213 if (!crow(bv)->previous())
1214 result = FINISHED_UP;
1216 lt->cursorPrevious();
1217 lt->clearSelection();
1218 result = DISPATCHED_NOUPDATE;
1220 updwhat = cursor_update;
1224 if (!crow(bv)->next())
1225 result = FINISHED_DOWN;
1228 lt->clearSelection();
1229 result = DISPATCHED_NOUPDATE;
1231 updwhat = cursor_update;
1234 case LFUN_BACKSPACE: {
1235 if (lt->selection.set())
1236 lt->cutSelection(true, false);
1239 updwhat = CURSOR_PAR;
1245 if (lt->selection.set()) {
1246 lt->cutSelection(true, false);
1250 updwhat = CURSOR_PAR;
1256 lt->cutSelection(bv);
1257 updwhat = CURSOR_PAR;
1264 lt->copySelection();
1265 updwhat = CURSOR_PAR;
1268 case LFUN_PASTESELECTION:
1270 string const clip(bv->getClipboard());
1274 if (ev.argument == "paragraph") {
1275 lt->insertStringAsParagraphs(clip);
1277 lt->insertStringAsLines(clip);
1280 lt->clearSelection();
1282 updwhat = CURSOR_PAR;
1288 if (!autoBreakRows) {
1289 if (CutAndPaste::nrOfParagraphs() > 1) {
1290 #ifdef WITH_WARNINGS
1291 #warning FIXME horrendously bad UI
1293 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1298 lt->pasteSelection();
1300 lt->clearSelection();
1301 updwhat = CURSOR_PAR;
1306 case LFUN_BREAKPARAGRAPH:
1307 if (!autoBreakRows) {
1308 result = DISPATCHED;
1311 lt->breakParagraph(paragraphs, 0);
1312 updwhat = CURSOR | FULL;
1316 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1317 if (!autoBreakRows) {
1318 result = DISPATCHED;
1321 lt->breakParagraph(paragraphs, 1);
1322 updwhat = CURSOR | FULL;
1326 case LFUN_BREAKLINE: {
1327 if (!autoBreakRows) {
1328 result = DISPATCHED;
1332 lt->insertInset(new InsetNewline);
1333 updwhat = CURSOR | CURSOR_PAR;
1339 // do not set layouts on non breakable textinsets
1340 if (autoBreakRows) {
1341 string cur_layout = cpar(bv)->layout()->name();
1343 // Derive layout number from given argument (string)
1344 // and current buffer's textclass (number). */
1345 LyXTextClass const & tclass =
1346 bv->buffer()->params.getLyXTextClass();
1347 string layout = ev.argument;
1348 bool hasLayout = tclass.hasLayout(layout);
1350 // If the entry is obsolete, use the new one instead.
1352 string const & obs =
1353 tclass[layout]->obsoleted_by();
1358 // see if we found the layout number:
1360 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + ev.argument + N_(" not known"));
1361 bv->owner()->dispatch(lf);
1365 if (cur_layout != layout) {
1366 cur_layout = layout;
1367 lt->setLayout(layout);
1368 bv->owner()->setLayout(cpar(bv)->layout()->name());
1369 updwhat = CURSOR_PAR;
1373 // reset the layout box
1374 bv->owner()->setLayout(cpar(bv)->layout()->name());
1377 case LFUN_PARAGRAPH_SPACING:
1378 // This one is absolutely not working. When fiddling with this
1379 // it also seems to me that the paragraphs inside the insettext
1380 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1381 // FIXME: how old is this comment ? ...
1383 Paragraph * par = lt->cursor.par();
1384 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1385 float cur_value = 1.0;
1386 if (cur_spacing == Spacing::Other) {
1387 cur_value = par->params().spacing().getValue();
1390 istringstream istr(ev.argument.c_str());
1393 Spacing::Space new_spacing = cur_spacing;
1394 float new_value = cur_value;
1396 lyxerr << "Missing argument to `paragraph-spacing'"
1398 } else if (tmp == "single") {
1399 new_spacing = Spacing::Single;
1400 } else if (tmp == "onehalf") {
1401 new_spacing = Spacing::Onehalf;
1402 } else if (tmp == "double") {
1403 new_spacing = Spacing::Double;
1404 } else if (tmp == "other") {
1405 new_spacing = Spacing::Other;
1408 lyxerr << "new_value = " << tmpval << endl;
1411 } else if (tmp == "default") {
1412 new_spacing = Spacing::Default;
1414 lyxerr << _("Unknown spacing argument: ")
1415 << ev.argument << endl;
1417 if (cur_spacing != new_spacing || cur_value != new_value) {
1418 par->params().spacing(Spacing(new_spacing, new_value));
1419 updwhat = CURSOR_PAR;
1425 // These need to do repaints but don't require
1426 // special handling otherwise. A *lot* of the
1427 // above could probably be done similarly ...
1432 case LFUN_WORDRIGHT:
1433 // these two are really unhandled ...
1435 case LFUN_BEGINNINGBUF:
1436 updwhat = cursor_update;
1437 if (!bv->dispatch(ev))
1438 result = UNDISPATCHED;
1447 case LFUN_WORDLEFTSEL:
1448 case LFUN_WORDRIGHTSEL:
1449 updwhat = SELECTION;
1454 if (!bv->dispatch(ev))
1455 result = UNDISPATCHED;
1462 updateLocal(bv, updwhat, updflag);
1463 /// If the action has deleted all text in the inset, we need to change the
1464 // language to the language of the surronding text.
1465 if (!was_empty && paragraphs.begin()->empty() && !paragraphs.begin()->next()) {
1466 LyXFont font(LyXFont::ALL_IGNORE);
1467 font.setLanguage(bv->getParentLanguage(this));
1468 setFont(bv, font, false);
1471 if (result >= FINISHED)
1472 bv->unlockInset(this);
1474 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1475 result = DISPATCHED;
1480 int InsetText::latex(Buffer const * buf, ostream & os,
1481 bool moving_arg, bool) const
1484 latexParagraphs(buf, paragraphs,
1485 paragraphs.begin(), paragraphs.end(),
1486 os, texrow, moving_arg);
1487 return texrow.rows();
1491 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1493 unsigned int lines = 0;
1495 ParagraphList::iterator beg = paragraphs.begin();
1496 ParagraphList::iterator end = paragraphs.end();
1497 ParagraphList::iterator it = beg;
1498 for (; it != end; ++it) {
1499 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1500 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1507 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1509 Paragraph * p = &*(paragraphs.begin());
1510 unsigned int lines = 0;
1512 vector<string> environment_stack(10);
1513 vector<string> environment_inner(10);
1515 int const command_depth = 0;
1518 Paragraph::depth_type depth = 0; // paragraph depth
1522 int desc_on = 0; // description mode
1524 LyXLayout_ptr const & style = p->layout();
1526 // environment tag closing
1527 for (; depth > p->params().depth(); --depth) {
1528 if (environment_inner[depth] != "!-- --") {
1529 item_name = "listitem";
1530 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1531 if (environment_inner[depth] == "varlistentry")
1532 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1534 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1535 environment_stack[depth].erase();
1536 environment_inner[depth].erase();
1539 if (depth == p->params().depth()
1540 && environment_stack[depth] != style->latexname()
1541 && !environment_stack[depth].empty()) {
1542 if (environment_inner[depth] != "!-- --") {
1543 item_name= "listitem";
1544 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1545 if (environment_inner[depth] == "varlistentry")
1546 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1549 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1551 environment_stack[depth].erase();
1552 environment_inner[depth].erase();
1555 // Write opening SGML tags.
1556 switch (style->latextype) {
1557 case LATEX_PARAGRAPH:
1558 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1562 buf->sgmlError(p, 0, _("Error: LatexType Command not allowed here.\n"));
1566 case LATEX_ENVIRONMENT:
1567 case LATEX_ITEM_ENVIRONMENT:
1568 if (depth < p->params().depth()) {
1569 depth = p->params().depth();
1570 environment_stack[depth].erase();
1573 if (environment_stack[depth] != style->latexname()) {
1574 if (environment_stack.size() == depth + 1) {
1575 environment_stack.push_back("!-- --");
1576 environment_inner.push_back("!-- --");
1578 environment_stack[depth] = style->latexname();
1579 environment_inner[depth] = "!-- --";
1580 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1582 if (environment_inner[depth] != "!-- --") {
1583 item_name= "listitem";
1584 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1585 if (environment_inner[depth] == "varlistentry")
1586 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1590 if (style->latextype == LATEX_ENVIRONMENT) {
1591 if (!style->latexparam().empty()) {
1592 if (style->latexparam() == "CDATA")
1595 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1600 desc_on = (style->labeltype == LABEL_MANUAL);
1602 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1603 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1605 item_name = desc_on ? "term" : "para";
1606 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1610 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1614 buf->simpleDocBookOnePar(os, p, desc_on, depth + 1 + command_depth);
1618 // write closing SGML tags
1619 switch (style->latextype) {
1620 case LATEX_ENVIRONMENT:
1621 if (!style->latexparam().empty()) {
1622 if (style->latexparam() == "CDATA")
1625 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1628 case LATEX_ITEM_ENVIRONMENT:
1629 if (desc_on == 1) break;
1631 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1633 case LATEX_PARAGRAPH:
1634 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1637 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1643 for (int d = depth; d >= 0; --d) {
1644 if (!environment_stack[depth].empty()) {
1645 if (environment_inner[depth] != "!-- --") {
1646 item_name = "listitem";
1647 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1648 if (environment_inner[depth] == "varlistentry")
1649 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1652 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1660 void InsetText::validate(LaTeXFeatures & features) const
1662 for_each(paragraphs.begin(), paragraphs.end(),
1663 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1667 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1669 if (the_locking_inset) {
1670 the_locking_inset->getCursorPos(bv, x, y);
1673 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1674 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1678 int InsetText::insetInInsetY() const
1680 if (!the_locking_inset)
1683 return (inset_y + the_locking_inset->insetInInsetY());
1687 void InsetText::toggleInsetCursor(BufferView * bv)
1689 if (the_locking_inset) {
1690 the_locking_inset->toggleInsetCursor(bv);
1694 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1696 int const asc = font_metrics::maxAscent(font);
1697 int const desc = font_metrics::maxDescent(font);
1699 if (isCursorVisible())
1700 bv->hideLockedInsetCursor();
1702 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1703 toggleCursorVisible();
1707 void InsetText::showInsetCursor(BufferView * bv, bool show)
1709 if (the_locking_inset) {
1710 the_locking_inset->showInsetCursor(bv, show);
1713 if (!isCursorVisible()) {
1714 LyXFont const font =
1715 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1717 int const asc = font_metrics::maxAscent(font);
1718 int const desc = font_metrics::maxDescent(font);
1720 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1722 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1723 setCursorVisible(true);
1728 void InsetText::hideInsetCursor(BufferView * bv)
1730 if (isCursorVisible()) {
1731 bv->hideLockedInsetCursor();
1732 setCursorVisible(false);
1734 if (the_locking_inset)
1735 the_locking_inset->hideInsetCursor(bv);
1739 void InsetText::fitInsetCursor(BufferView * bv) const
1741 if (the_locking_inset) {
1742 the_locking_inset->fitInsetCursor(bv);
1745 LyXFont const font =
1746 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1748 int const asc = font_metrics::maxAscent(font);
1749 int const desc = font_metrics::maxDescent(font);
1751 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1752 need_update |= FULL;
1757 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1759 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1760 return moveLeftIntern(bv, false, activate_inset, selecting);
1762 return moveRightIntern(bv, true, activate_inset, selecting);
1767 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1769 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1770 return moveRightIntern(bv, true, activate_inset, selecting);
1772 return moveLeftIntern(bv, false, activate_inset, selecting);
1777 InsetText::moveRightIntern(BufferView * bv, bool front,
1778 bool activate_inset, bool selecting)
1780 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1781 return FINISHED_RIGHT;
1782 if (activate_inset && checkAndActivateInset(bv, front))
1784 getLyXText(bv)->cursorRight(bv);
1786 getLyXText(bv)->clearSelection();
1787 return DISPATCHED_NOUPDATE;
1792 InsetText::moveLeftIntern(BufferView * bv, bool front,
1793 bool activate_inset, bool selecting)
1795 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1797 getLyXText(bv)->cursorLeft(bv);
1799 getLyXText(bv)->clearSelection();
1800 if (activate_inset && checkAndActivateInset(bv, front))
1802 return DISPATCHED_NOUPDATE;
1806 Inset::RESULT InsetText::moveUp(BufferView * bv)
1808 if (!crow(bv)->previous())
1810 getLyXText(bv)->cursorUp(bv);
1811 getLyXText(bv)->clearSelection();
1812 return DISPATCHED_NOUPDATE;
1816 Inset::RESULT InsetText::moveDown(BufferView * bv)
1818 if (!crow(bv)->next())
1819 return FINISHED_DOWN;
1820 getLyXText(bv)->cursorDown(bv);
1821 getLyXText(bv)->clearSelection();
1822 return DISPATCHED_NOUPDATE;
1826 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1828 if (the_locking_inset) {
1829 if (the_locking_inset->insetAllowed(inset))
1830 return the_locking_inset->insertInset(bv, inset);
1833 inset->setOwner(this);
1834 hideInsetCursor(bv);
1835 getLyXText(bv)->insertInset(inset);
1837 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1842 bool InsetText::insetAllowed(Inset::Code code) const
1844 // in_insetAllowed is a really gross hack,
1845 // to allow us to call the owner's insetAllowed
1846 // without stack overflow, which can happen
1847 // when the owner uses InsetCollapsable::insetAllowed()
1849 if (in_insetAllowed)
1851 in_insetAllowed = true;
1852 if (the_locking_inset)
1853 ret = the_locking_inset->insetAllowed(code);
1855 ret = owner()->insetAllowed(code);
1856 in_insetAllowed = false;
1861 UpdatableInset * InsetText::getLockingInset() const
1863 return the_locking_inset ? the_locking_inset->getLockingInset() :
1864 const_cast<InsetText *>(this);
1868 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1872 if (the_locking_inset)
1873 return the_locking_inset->getFirstLockingInsetOfType(c);
1878 bool InsetText::showInsetDialog(BufferView * bv) const
1880 if (the_locking_inset)
1881 return the_locking_inset->showInsetDialog(bv);
1886 vector<string> const InsetText::getLabelList() const
1888 vector<string> label_list;
1890 ParagraphList::iterator pit = paragraphs.begin();
1891 ParagraphList::iterator pend = paragraphs.end();
1892 for (; pit != pend; ++pit) {
1893 InsetList::iterator beg = pit->insetlist.begin();
1894 InsetList::iterator end = pit->insetlist.end();
1895 for (; beg != end; ++beg) {
1896 vector<string> const l = beg.getInset()->getLabelList();
1897 label_list.insert(label_list.end(), l.begin(), l.end());
1904 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1907 if (the_locking_inset) {
1908 the_locking_inset->setFont(bv, font, toggleall, selectall);
1911 if ((!paragraphs.begin()->next() && paragraphs.begin()->empty()) || cpar(bv)->empty()) {
1912 getLyXText(bv)->setFont(font, toggleall);
1917 lt = getLyXText(bv);
1920 if (lt->selection.set()) {
1921 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1925 lt->toggleFree(font, toggleall);
1927 lt->clearSelection();
1929 bool flag = (selectall || lt->selection.set());
1933 updateLocal(bv, FULL, true);
1935 updateLocal(bv, CURSOR_PAR, true);
1939 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1941 if (cpar(bv)->isInset(cpos(bv))) {
1943 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1944 if (!isHighlyEditableInset(inset))
1946 inset->edit(bv, front);
1947 if (!the_locking_inset)
1949 updateLocal(bv, CURSOR, false);
1956 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1957 mouse_button::state button)
1959 x -= drawTextXOffset;
1961 int dummyy = y + insetAscent;
1962 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1963 // we only do the edit() call if the inset was hit by the mouse
1964 // or if it is a highly editable inset. So we should call this
1965 // function from our own edit with button < 0.
1966 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1968 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1976 inset_x = cix(bv) - top_x + drawTextXOffset;
1977 inset_y = ciy(bv) + drawTextYOffset;
1978 inset->edit(bv, x - inset_x, y - inset_y, button);
1979 if (!the_locking_inset)
1981 updateLocal(bv, CURSOR, false);
1988 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1991 int w = UpdatableInset::getMaxWidth(bv, inset);
1996 w = w - top_x + owner()->x();
1999 w -= (2 * TEXT_TO_INSET_OFFSET);
2002 return UpdatableInset::getMaxWidth(bv, inset);
2007 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2009 // we have to unlock any locked inset otherwise we're in troubles
2010 the_locking_inset = 0;
2013 paragraphs.set(new Paragraph(*p, same_id));
2014 paragraphs.begin()->setInsetOwner(this);
2015 Paragraph * np = &*(paragraphs.begin());
2018 np->next(new Paragraph(*p, same_id));
2019 np->next()->previous(np);
2021 np->setInsetOwner(this);
2028 void InsetText::markNew(bool track_changes)
2030 ParagraphList::iterator pit = paragraphs.begin();
2031 ParagraphList::iterator pend = paragraphs.end();
2032 for (; pit != pend; ++pit) {
2033 if (track_changes) {
2034 pit->trackChanges();
2036 // no-op when not tracking
2037 pit->cleanChanges();
2043 void InsetText::setText(string const & data, LyXFont const & font)
2046 for (unsigned int i = 0; i < data.length(); ++i)
2047 paragraphs.begin()->insertChar(i, data[i], font);
2052 void InsetText::setAutoBreakRows(bool flag)
2054 if (flag != autoBreakRows) {
2055 autoBreakRows = flag;
2063 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2065 if (how != drawFrame_) {
2068 updateLocal(bv, DRAW_FRAME, false);
2073 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2075 if (frame_color != col) {
2078 updateLocal(bv, DRAW_FRAME, false);
2083 int InsetText::cx(BufferView * bv) const
2085 // we do nothing dangerous so we use a local cache
2086 LyXText * llt = getLyXText(bv);
2087 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2088 if (the_locking_inset) {
2089 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2091 if (font.isVisibleRightToLeft())
2092 x -= the_locking_inset->width(bv, font);
2098 int InsetText::cix(BufferView * bv) const
2100 // we do nothing dangerous so we use a local cache
2101 LyXText * llt = getLyXText(bv);
2102 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2103 if (the_locking_inset) {
2104 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2106 if (font.isVisibleRightToLeft())
2107 x -= the_locking_inset->width(bv, font);
2113 int InsetText::cy(BufferView * bv) const
2116 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2120 int InsetText::ciy(BufferView * bv) const
2123 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2127 pos_type InsetText::cpos(BufferView * bv) const
2129 return getLyXText(bv)->cursor.pos();
2133 Paragraph * InsetText::cpar(BufferView * bv) const
2135 return getLyXText(bv)->cursor.par();
2139 bool InsetText::cboundary(BufferView * bv) const
2141 return getLyXText(bv)->cursor.boundary();
2145 Row * InsetText::crow(BufferView * bv) const
2147 return getLyXText(bv)->cursor.row();
2151 LyXText * InsetText::getLyXText(BufferView const * lbv,
2152 bool const recursive) const
2154 if (cached_bview == lbv) {
2155 if (recursive && the_locking_inset)
2156 return the_locking_inset->getLyXText(lbv, true);
2157 LyXText * lt = cached_text.get();
2158 lyx::Assert(lt && lt->rows().begin()->par() == &*(paragraphs.begin()));
2161 // Super UGLY! (Lgb)
2162 BufferView * bv = const_cast<BufferView *>(lbv);
2165 Cache::iterator it = cache.find(bv);
2167 if (it != cache.end()) {
2170 } else if (do_resize) {
2171 resizeLyXText(do_resize);
2173 if (lt || !it->second.remove) {
2174 lyx::Assert(it->second.text.get());
2175 cached_text = it->second.text;
2176 if (recursive && the_locking_inset) {
2177 return the_locking_inset->getLyXText(bv, true);
2179 return cached_text.get();
2180 } else if (it->second.remove) {
2182 saveLyXTextState(it->second.text.get());
2188 // when we have to reinit the existing LyXText!
2190 it->second.text->init(bv);
2191 restoreLyXTextState(it->second.text.get());
2192 it->second.remove = false;
2194 cached_text = it->second.text;
2195 if (the_locking_inset && recursive) {
2196 return the_locking_inset->getLyXText(bv);
2198 return cached_text.get();
2201 // we are here only if we don't have a BufferView * in the cache!!!
2203 cached_text.reset(new LyXText(bv, const_cast<InsetText *>(this)));
2204 cached_text->init(bv);
2205 restoreLyXTextState(cached_text.get());
2207 cache.insert(make_pair(bv, cached_text));
2209 if (the_locking_inset && recursive) {
2210 return the_locking_inset->getLyXText(bv);
2212 return cached_text.get();
2216 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2220 Cache::iterator it = cache.find(bv);
2222 if (it == cache.end()) {
2226 lyx::Assert(it->second.text.get());
2228 it->second.remove = true;
2230 /// then remove all LyXText in text-insets
2231 for_each(paragraphs.begin(), paragraphs.end(),
2232 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2237 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2240 // we cannot resize this because we are in use!
2241 // so do this on the next possible getLyXText()
2246 if (!paragraphs.begin()->next() && paragraphs.begin()->empty()) { // no data, resize not neccessary!
2247 // we have to do this as a fixed width may have changed!
2248 LyXText * t = getLyXText(bv);
2249 saveLyXTextState(t);
2251 restoreLyXTextState(t);
2254 // one endless line, resize normally not necessary
2255 if (!force && getMaxWidth(bv, this) < 0)
2258 Cache::iterator it = cache.find(bv);
2259 if (it == cache.end()) {
2262 lyx::Assert(it->second.text.get());
2264 LyXText * t = it->second.text.get();
2265 saveLyXTextState(t);
2267 for_each(paragraphs.begin(), paragraphs.end(),
2268 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2271 restoreLyXTextState(t);
2272 if (the_locking_inset) {
2273 inset_x = cix(bv) - top_x + drawTextXOffset;
2274 inset_y = ciy(bv) + drawTextYOffset;
2277 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2279 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2280 // this will scroll the screen such that the cursor becomes visible
2281 bv->updateScrollbar();
2283 need_update |= FULL;
2288 void InsetText::reinitLyXText() const
2291 // we cannot resize this because we are in use!
2292 // so do this on the next possible getLyXText()
2298 for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2299 lyx::Assert(it->second.text.get());
2301 LyXText * t = it->second.text.get();
2302 BufferView * bv = it->first;
2304 saveLyXTextState(t);
2306 for_each(paragraphs.begin(), paragraphs.end(),
2307 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2310 restoreLyXTextState(t);
2311 if (the_locking_inset) {
2312 inset_x = cix(bv) - top_x + drawTextXOffset;
2313 inset_y = ciy(bv) + drawTextYOffset;
2315 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2317 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2318 // this will scroll the screen such that the cursor becomes visible
2319 bv->updateScrollbar();
2327 void InsetText::removeNewlines()
2329 bool changed = false;
2331 ParagraphList::iterator it = paragraphs.begin();
2332 ParagraphList::iterator end = paragraphs.end();
2333 for (; it != end; ++it) {
2334 for (int i = 0; i < it->size(); ++i) {
2335 if (it->isNewline(i)) {
2346 bool InsetText::nodraw() const
2348 if (the_locking_inset)
2349 return the_locking_inset->nodraw();
2350 return UpdatableInset::nodraw();
2354 int InsetText::scroll(bool recursive) const
2356 int sx = UpdatableInset::scroll(false);
2358 if (recursive && the_locking_inset)
2359 sx += the_locking_inset->scroll(recursive);
2365 void InsetText::selectAll(BufferView * bv)
2367 getLyXText(bv)->cursorTop();
2368 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2369 getLyXText(bv)->cursorBottom();
2370 getLyXText(bv)->setSelection();
2374 void InsetText::clearSelection(BufferView * bv)
2376 getLyXText(bv)->clearSelection();
2380 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2382 Painter & pain = bv->painter();
2384 int h = insetAscent + insetDescent;
2385 int ty = baseline - insetAscent;
2391 if ((ty + h) > pain.paperHeight())
2392 h = pain.paperHeight();
2393 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2394 w = pain.paperWidth();
2395 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2400 Paragraph * InsetText::firstParagraph() const
2403 if (the_locking_inset)
2404 if ((result = the_locking_inset->firstParagraph()))
2406 return &*(paragraphs.begin());
2410 Paragraph * InsetText::getFirstParagraph(int i) const
2412 return (i == 0) ? &*(paragraphs.begin()) : 0;
2416 LyXCursor const & InsetText::cursor(BufferView * bv) const
2418 if (the_locking_inset)
2419 return the_locking_inset->cursor(bv);
2420 return getLyXText(bv)->cursor;
2424 Paragraph * InsetText::paragraph() const
2426 return &*(paragraphs.begin());
2430 void InsetText::paragraph(Paragraph * p)
2432 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2433 // caller of this function has to take care of it. This IS important
2434 // as we could have to insert a paragraph before this one and just
2435 // link the actual to a new ones next and set it with this function
2438 // set ourself as owner for all the paragraphs inserted!
2439 for_each(paragraphs.begin(), paragraphs.end(),
2440 boost::bind(&Paragraph::setInsetOwner, _1, this));
2443 // redraw myself when asked for
2448 Inset * InsetText::getInsetFromID(int id_arg) const
2451 return const_cast<InsetText *>(this);
2453 ParagraphList::iterator pit = paragraphs.begin();
2454 ParagraphList::iterator pend = paragraphs.end();
2455 for (; pit != pend; ++pit) {
2456 InsetList::iterator it = pit->insetlist.begin();
2457 InsetList::iterator end = pit->insetlist.end();
2458 for (; it != end; ++it) {
2459 if (it.getInset()->id() == id_arg)
2460 return it.getInset();
2461 Inset * in = it.getInset()->getInsetFromID(id_arg);
2471 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2472 float & value) const
2478 lt = getLyXText(bv);
2481 if (the_locking_inset) {
2482 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2483 if (!word.word().empty()) {
2489 // we have to go on checking so move cursor to the next char
2490 lt->cursor.pos(lt->cursor.pos() + 1);
2492 word = lt->selectNextWordToSpellcheck(value);
2493 if (word.word().empty())
2494 bv->unlockInset(const_cast<InsetText *>(this));
2503 void InsetText::selectSelectedWord(BufferView * bv)
2505 if (the_locking_inset) {
2506 the_locking_inset->selectSelectedWord(bv);
2509 getLyXText(bv)->selectSelectedWord();
2510 updateLocal(bv, SELECTION, false);
2514 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2516 if (the_locking_inset) {
2517 the_locking_inset->toggleSelection(bv, kill_selection);
2521 lt = getLyXText(bv);
2525 int x = top_x + TEXT_TO_INSET_OFFSET;
2527 RowList::iterator rowit = lt->rows().begin();
2528 RowList::iterator end = lt->rows().end();
2529 int y_offset = top_baseline - rowit->ascent_of_text();
2531 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2532 y += rowit->height();
2538 if (need_update & SELECTION)
2540 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2546 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2550 lt = getLyXText(bv);
2553 if (the_locking_inset) {
2554 if (the_locking_inset->nextChange(bv, length))
2556 lt->cursorRight(true);
2558 lyxfind::SearchResult result =
2559 lyxfind::findNextChange(bv, lt, length);
2561 if (result == lyxfind::SR_FOUND) {
2562 LyXCursor cur = lt->cursor;
2563 bv->unlockInset(bv->theLockingInset());
2564 if (bv->lockInset(this))
2567 lt->setSelectionRange(length);
2568 updateLocal(bv, SELECTION, false);
2572 return result != lyxfind::SR_NOT_FOUND;
2576 bool InsetText::searchForward(BufferView * bv, string const & str,
2581 lt = getLyXText(bv);
2584 if (the_locking_inset) {
2585 if (the_locking_inset->searchForward(bv, str, cs, mw))
2587 lt->cursorRight(true);
2589 lyxfind::SearchResult result =
2590 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2592 if (result == lyxfind::SR_FOUND) {
2593 LyXCursor cur = lt->cursor;
2594 bv->unlockInset(bv->theLockingInset());
2595 if (bv->lockInset(this))
2598 lt->setSelectionRange(str.length());
2599 updateLocal(bv, SELECTION, false);
2603 return (result != lyxfind::SR_NOT_FOUND);
2606 bool InsetText::searchBackward(BufferView * bv, string const & str,
2609 if (the_locking_inset) {
2610 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2615 lt = getLyXText(bv);
2619 Paragraph * p = &*(paragraphs.begin());
2622 lt->setCursor(p, p->size());
2624 lyxfind::SearchResult result =
2625 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2627 if (result == lyxfind::SR_FOUND) {
2628 LyXCursor cur = lt->cursor;
2629 bv->unlockInset(bv->theLockingInset());
2630 if (bv->lockInset(this))
2633 lt->setSelectionRange(str.length());
2634 updateLocal(bv, SELECTION, false);
2638 return (result != lyxfind::SR_NOT_FOUND);
2642 bool InsetText::checkInsertChar(LyXFont & font)
2645 return owner()->checkInsertChar(font);
2650 void InsetText::collapseParagraphs(BufferView * bv)
2652 LyXText * llt = getLyXText(bv);
2654 while (paragraphs.begin()->next()) {
2655 if (!paragraphs.begin()->empty() && !paragraphs.begin()->next()->empty() &&
2656 !paragraphs.begin()->isSeparator(paragraphs.begin()->size() - 1))
2658 paragraphs.begin()->insertChar(paragraphs.begin()->size(), ' ');
2660 if (llt->selection.set()) {
2661 if (llt->selection.start.par() == paragraphs.begin()->next()) {
2662 llt->selection.start.par(&*(paragraphs.begin()));
2663 llt->selection.start.pos(
2664 llt->selection.start.pos() + paragraphs.begin()->size());
2666 if (llt->selection.end.par() == paragraphs.begin()->next()) {
2667 llt->selection.end.par(&*(paragraphs.begin()));
2668 llt->selection.end.pos(
2669 llt->selection.end.pos() + paragraphs.begin()->size());
2672 mergeParagraph(bv->buffer()->params, paragraphs, paragraphs.begin());
2678 void InsetText::getDrawFont(LyXFont & font) const
2682 owner()->getDrawFont(font);
2686 void InsetText::appendParagraphs(Buffer * buffer,
2689 BufferParams const & bparams = buffer->params;
2691 Paragraph * tmpbuf = newpar;
2692 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2693 if (bparams.tracking_changes)
2694 buf->cleanChanges();
2696 while (tmpbuf->next()) {
2697 tmpbuf = tmpbuf->next();
2698 lastbuffer->next(new Paragraph(*tmpbuf, false));
2699 lastbuffer->next()->previous(lastbuffer);
2700 lastbuffer = lastbuffer->next();
2701 if (bparams.tracking_changes)
2702 lastbuffer->cleanChanges();
2704 lastbuffer = &*(paragraphs.begin());
2705 while (lastbuffer->next())
2706 lastbuffer = lastbuffer->next();
2707 if (!newpar->empty() && !lastbuffer->empty() &&
2708 !lastbuffer->isSeparator(lastbuffer->size() - 1))
2710 lastbuffer->insertChar(lastbuffer->size(), ' ');
2713 // make the buf exactly the same layout than our last paragraph
2714 buf->makeSameLayout(lastbuffer);
2717 lastbuffer->next(buf);
2718 buf->previous(lastbuffer);
2719 mergeParagraph(buffer->params, paragraphs, lastbuffer);
2725 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2727 Paragraph * par = getFirstParagraph(0);
2729 InsetList::iterator it = par->insetlist.begin();
2730 InsetList::iterator end = par->insetlist.end();
2731 for (; it != end; ++it) {
2732 it.getInset()->addPreview(loader);