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 extern unsigned char getCurrentTextClass(Buffer *);
75 extern bool math_insert_greek(BufferView *, char);
76 extern int greek_kb_flag;
79 // These functions should probably go into bufferview_funcs somehow (Jug)
81 void InsetText::saveLyXTextState(LyXText * t) const
83 // check if my paragraphs are still valid
84 ParagraphList::iterator it = paragraphs.begin();
85 ParagraphList::iterator end = paragraphs.end();
86 for (; it != end; ++it) {
87 if (&*it == t->cursor.par())
91 if (it != end && t->cursor.pos() <= it->size()) {
92 sstate.lpar = t->cursor.par();
93 sstate.pos = t->cursor.pos();
94 sstate.boundary = t->cursor.boundary();
95 sstate.selstartpar = t->selection.start.par();
96 sstate.selstartpos = t->selection.start.pos();
97 sstate.selstartboundary = t->selection.start.boundary();
98 sstate.selendpar = t->selection.end.par();
99 sstate.selendpos = t->selection.end.pos();
100 sstate.selendboundary = t->selection.end.boundary();
101 sstate.selection = t->selection.set();
102 sstate.mark_set = t->selection.mark();
109 void InsetText::restoreLyXTextState(LyXText * t) const
114 t->selection.set(true);
115 /* at this point just to avoid the DEPM when setting the cursor */
116 t->selection.mark(sstate.mark_set);
117 if (sstate.selection) {
118 t->setCursor(sstate.selstartpar, sstate.selstartpos,
119 true, sstate.selstartboundary);
120 t->selection.cursor = t->cursor;
121 t->setCursor(sstate.selendpar, sstate.selendpos,
122 true, sstate.selendboundary);
124 t->setCursor(sstate.lpar, sstate.pos);
126 t->setCursor(sstate.lpar, sstate.pos, true, sstate.boundary);
127 t->selection.cursor = t->cursor;
128 t->selection.set(false);
133 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
140 InsetText::InsetText(BufferParams const & bp)
141 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
144 paragraphs.set(new Paragraph);
145 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
146 if (bp.tracking_changes)
147 paragraphs.begin()->trackChanges();
152 InsetText::InsetText(InsetText const & in, bool same_id)
153 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
160 InsetText & InsetText::operator=(InsetText const & it)
167 void InsetText::init(InsetText const * ins, bool same_id)
170 setParagraphData(&*(ins->paragraphs.begin()), same_id);
171 autoBreakRows = ins->autoBreakRows;
172 drawFrame_ = ins->drawFrame_;
173 frame_color = ins->frame_color;
177 for_each(paragraphs.begin(), paragraphs.end(),
178 boost::bind(&Paragraph::setInsetOwner, _1, this));
180 the_locking_inset = 0;
182 frame_color = LColor::insetframe;
183 autoBreakRows = false;
196 last_drawn_width = -1;
199 in_insetAllowed = false;
203 InsetText::~InsetText()
209 void InsetText::clear(bool just_mark_erased)
211 if (just_mark_erased) {
212 ParagraphList::iterator it = paragraphs.begin();
213 ParagraphList::iterator end = paragraphs.end();
214 for (; it != end; ++it) {
221 // This is a gross hack...
222 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
225 paragraphs.set(new Paragraph);
226 paragraphs.begin()->setInsetOwner(this);
227 paragraphs.begin()->layout(old_layout);
234 Inset * InsetText::clone(Buffer const &, bool same_id) const
236 return new InsetText(*this, same_id);
240 void InsetText::write(Buffer const * buf, ostream & os) const
243 writeParagraphData(buf, os);
247 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
249 ParagraphList::iterator it = paragraphs.begin();
250 ParagraphList::iterator end = paragraphs.end();
251 Paragraph::depth_type dth = 0;
252 for (; it != end; ++it) {
253 it->write(buf, os, buf->params, dth);
258 void InsetText::read(Buffer const * buf, LyXLex & lex)
261 Paragraph::depth_type depth = 0;
265 if (buf->params.tracking_changes)
266 paragraphs.begin()->trackChanges();
268 // delete the initial paragraph
270 ParagraphList::iterator pit = paragraphs.begin();
274 token = lex.getString();
277 if (token == "\\end_inset") {
281 if (token == "\\the_end") {
282 lex.printError("\\the_end read in inset! Error in document!");
287 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
290 pit = paragraphs.begin();
291 ParagraphList::iterator const end = paragraphs.end();
292 for (; pit != end; ++pit)
293 pit->setInsetOwner(this);
295 if (token != "\\end_inset") {
296 lex.printError("Missing \\end_inset at this point. "
303 int InsetText::ascent(BufferView * bv, LyXFont const &) const
305 insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
306 TEXT_TO_INSET_OFFSET;
311 int InsetText::descent(BufferView * bv, LyXFont const &) const
313 LyXText * llt = getLyXText(bv);
314 insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
315 TEXT_TO_INSET_OFFSET;
320 int InsetText::width(BufferView * bv, LyXFont const &) const
322 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
323 (2 * TEXT_TO_INSET_OFFSET);
324 insetWidth = max(insetWidth, 10);
329 int InsetText::textWidth(BufferView * bv, bool fordraw) const
332 if (!autoBreakRows) {
335 w = getMaxWidth(bv, this);
338 return max(w - (2 * TEXT_TO_INSET_OFFSET),
339 (int)getLyXText(bv)->width);
343 return w - (2 * TEXT_TO_INSET_OFFSET);
347 void InsetText::draw(BufferView * bv, LyXFont const & f,
348 int baseline, float & x) const
353 // update our idea of where we are. Clearly, we should
354 // not have to know this information.
358 int const start_x = int(x);
360 Painter & pain = bv->painter();
362 // call these methods so that insetWidth, insetAscent and
363 // insetDescent have the right values.
368 // repaint the background if needed
369 if (backgroundColor() != LColor::background)
370 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
372 // no draw is necessary !!!
373 if ((drawFrame_ == LOCKED) && !locked && paragraphs.begin()->empty()) {
374 top_baseline = baseline;
381 x += static_cast<float>(scroll());
383 top_baseline = baseline;
384 top_y = baseline - insetAscent;
386 if (last_drawn_width != insetWidth) {
388 last_drawn_width = insetWidth;
391 if (the_locking_inset && (cpar(bv) == inset_par)
392 && (cpos(bv) == inset_pos)) {
393 inset_x = cix(bv) - int(x) + drawTextXOffset;
394 inset_y = ciy(bv) + drawTextYOffset;
402 x += TEXT_TO_INSET_OFFSET;
404 Row * row = lt->firstRow();
405 int y_offset = baseline - row->ascent_of_text();
406 int ph = pain.paperHeight();
409 while ((row != 0) && ((y+row->height()) <= 0)) {
411 first += row->height();
415 lt->top_y(-y_offset);
423 int yf = y_offset + first;
425 while ((row != 0) && (yf < ph)) {
426 RowPainter rp(*bv, *lt, *row);
427 rp.paint(y + y_offset + first, int(x), y + lt->top_y());
435 if ((drawFrame_ == ALWAYS) || (drawFrame_ == LOCKED && locked)) {
436 drawFrame(pain, int(start_x));
439 x += insetWidth - TEXT_TO_INSET_OFFSET;
441 if (need_update != INIT) {
449 void InsetText::drawFrame(Painter & pain, int x) const
451 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
453 frame_y = top_baseline - insetAscent + ttoD2;
454 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
455 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
456 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
461 void InsetText::update(BufferView * bv, bool reinit)
464 if (reinit && owner()) {
466 owner()->update(bv, true);
472 if (reinit || need_update == INIT) {
474 // we should put this call where we set need_update to INIT!
477 owner()->update(bv, true);
480 int nw = getMaxWidth(bv, this);
481 if (nw > 0 && old_max_width != nw) {
488 if (!autoBreakRows && paragraphs.begin()->next())
489 collapseParagraphs(bv);
491 if (the_locking_inset) {
492 inset_x = cix(bv) - top_x + drawTextXOffset;
493 inset_y = ciy(bv) + drawTextYOffset;
494 the_locking_inset->update(bv, reinit);
502 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
504 lt->updateInset(the_locking_inset);
507 if (lt->status() == LyXText::NEED_MORE_REFRESH)
514 int nw = getMaxWidth(bv, this);
515 if (nw > 0 && old_max_width != nw) {
522 void InsetText::setUpdateStatus(BufferView * bv, int what) const
524 // this does nothing dangerous so use only a localized buffer
525 LyXText * llt = getLyXText(bv);
528 // we have to redraw us full if our LyXText NEED_MORE_REFRESH or
529 // if we don't break row so that we only have one row to update!
530 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
532 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
535 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
536 need_update |= CURSOR_PAR;
539 // this to not draw a selection when we redraw all of it!
540 if (need_update & CURSOR && !(need_update & SELECTION)) {
541 if (llt->selection.set())
543 llt->clearSelection();
548 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
550 if (!autoBreakRows && paragraphs.begin()->next())
551 collapseParagraphs(bv);
558 setUpdateStatus(bv, what);
559 bool flag = mark_dirty ||
560 (((need_update != CURSOR) && (need_update != NONE)) ||
561 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
562 if (!lt->selection.set())
563 lt->selection.cursor = lt->cursor;
569 bv->updateInset(const_cast<InsetText *>(this));
575 if (need_update == CURSOR)
577 bv->owner()->view_state_changed();
578 bv->owner()->updateMenubar();
579 bv->owner()->updateToolbar();
580 if (old_par != cpar(bv)) {
581 bv->owner()->setLayout(cpar(bv)->layout()->name());
587 string const InsetText::editMessage() const
589 return _("Opened Text Inset");
593 void InsetText::edit(BufferView * bv, int x, int y, mouse_button::state button)
595 UpdatableInset::edit(bv, x, y, button);
597 if (!bv->lockInset(this)) {
598 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
602 the_locking_inset = 0;
603 inset_pos = inset_x = inset_y = 0;
604 inset_boundary = false;
607 int tmp_y = (y < 0) ? 0 : y;
613 // we put here -1 and not button as now the button in the
614 // edit call should not be needed we will fix this in 1.3.x
615 // cycle hopefully (Jug 20020509)
616 // FIXME: GUII I've changed this to none: probably WRONG
617 if (!checkAndActivateInset(bv, x, tmp_y, mouse_button::none)) {
618 lt->setCursorFromCoordinates(x - drawTextXOffset,
620 lt->cursor.x_fix(lt->cursor.x());
622 lt->clearSelection();
624 // If the inset is empty set the language of the current font to the
625 // language to the surronding text (if different).
626 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
627 bv->getParentLanguage(this) != lt->current_font.language())
629 LyXFont font(LyXFont::ALL_IGNORE);
630 font.setLanguage(bv->getParentLanguage(this));
631 setFont(bv, font, false);
637 if (drawFrame_ == LOCKED)
638 code = CURSOR|DRAW_FRAME;
639 updateLocal(bv, code, false);
642 // Tell the paragraph dialog that we've entered an insettext.
643 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
647 void InsetText::edit(BufferView * bv, bool front)
649 UpdatableInset::edit(bv, front);
651 if (!bv->lockInset(this)) {
652 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
656 the_locking_inset = 0;
657 inset_pos = inset_x = inset_y = 0;
658 inset_boundary = false;
667 lt->setCursor(&*(paragraphs.begin()), 0);
669 ParagraphList::iterator it = paragraphs.begin();
670 ParagraphList::iterator end = paragraphs.end();
671 while (boost::next(it) != end)
673 // int const pos = (p->size() ? p->size()-1 : p->size());
674 lt->setCursor(&*it, it->size());
676 lt->clearSelection();
678 // If the inset is empty set the language of the current font to the
679 // language to the surronding text (if different).
680 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
681 bv->getParentLanguage(this) != lt->current_font.language()) {
682 LyXFont font(LyXFont::ALL_IGNORE);
683 font.setLanguage(bv->getParentLanguage(this));
684 setFont(bv, font, false);
689 if (drawFrame_ == LOCKED)
690 code = CURSOR|DRAW_FRAME;
691 updateLocal(bv, code, false);
696 void InsetText::insetUnlock(BufferView * bv)
698 if (the_locking_inset) {
699 the_locking_inset->insetUnlock(bv);
700 the_locking_inset = 0;
701 updateLocal(bv, CURSOR_PAR, false);
713 if (lt->selection.set()) {
714 lt->clearSelection();
716 } else if (owner()) {
717 bv->owner()->setLayout(owner()->getLyXText(bv)
718 ->cursor.par()->layout()->name());
720 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
721 // hack for deleteEmptyParMech
722 if (!paragraphs.begin()->empty()) {
723 lt->setCursor(&*(paragraphs.begin()), 0);
724 } else if (paragraphs.begin()->next()) {
725 lt->setCursor(paragraphs.begin()->next(), 0);
730 updateLocal(bv, code, false);
733 setUpdateStatus(bv, code);
738 void InsetText::lockInset(BufferView * bv)
741 the_locking_inset = 0;
742 inset_pos = inset_x = inset_y = 0;
743 inset_boundary = false;
751 lt->setCursor(&*(paragraphs.begin()), 0);
752 lt->clearSelection();
754 // If the inset is empty set the language of the current font to the
755 // language to the surronding text (if different).
756 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
757 bv->getParentLanguage(this) != lt->current_font.language()) {
758 LyXFont font(LyXFont::ALL_IGNORE);
759 font.setLanguage(bv->getParentLanguage(this));
760 setFont(bv, font, false);
765 if (drawFrame_ == LOCKED)
766 code = CURSOR|DRAW_FRAME;
767 setUpdateStatus(bv, code);
771 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
773 the_locking_inset = inset;
774 inset_x = cix(bv) - top_x + drawTextXOffset;
775 inset_y = ciy(bv) + drawTextYOffset;
776 inset_pos = cpos(bv);
777 inset_par = cpar(bv);
778 inset_boundary = cboundary(bv);
779 updateLocal(bv, CURSOR, false);
783 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
785 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
789 if (!the_locking_inset) {
790 Paragraph * p = &*(paragraphs.begin());
791 int const id = inset->id();
793 InsetList::iterator it =
794 p->insetlist.begin();
795 InsetList::iterator const end =
797 for (; it != end; ++it) {
798 if (it.getInset() == inset) {
799 getLyXText(bv)->setCursorIntern(p, it.getPos());
800 lockInset(bv, inset);
803 if (it.getInset()->getInsetFromID(id)) {
804 getLyXText(bv)->setCursorIntern(p, it.getPos());
805 it.getInset()->edit(bv);
806 return the_locking_inset->lockInsetInInset(bv, inset);
813 if (inset == cpar(bv)->getInset(cpos(bv))) {
814 lyxerr[Debug::INSETS] << "OK" << endl;
815 lockInset(bv, inset);
817 } else if (the_locking_inset && (the_locking_inset == inset)) {
818 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
819 lyxerr[Debug::INSETS] << "OK" << endl;
820 inset_x = cix(bv) - top_x + drawTextXOffset;
821 inset_y = ciy(bv) + drawTextYOffset;
823 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
825 } else if (the_locking_inset) {
826 lyxerr[Debug::INSETS] << "MAYBE" << endl;
827 return the_locking_inset->lockInsetInInset(bv, inset);
829 lyxerr[Debug::INSETS] << "NOT OK" << endl;
834 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
837 if (!the_locking_inset)
839 if (the_locking_inset == inset) {
840 the_locking_inset->insetUnlock(bv);
841 getLyXText(bv)->updateInset(inset);
842 the_locking_inset = 0;
844 moveRightIntern(bv, true, false);
845 old_par = 0; // force layout setting
849 updateLocal(bv, CURSOR, false);
852 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
856 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
858 if (!autoBreakRows && paragraphs.begin()->next())
859 collapseParagraphs(bv);
867 if (inset->owner() != this) {
868 int ustat = CURSOR_PAR;
870 UpdatableInset * tl_inset = the_locking_inset;
872 found = tl_inset->updateInsetInInset(bv, inset);
874 tl_inset = static_cast<UpdatableInset *>(inset);
875 while(tl_inset->owner() && tl_inset->owner() != this)
876 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
877 if (!tl_inset->owner())
879 found = tl_inset->updateInsetInInset(bv, inset);
883 lt->updateInset(tl_inset);
887 setUpdateStatus(bv, ustat);
890 bool found = lt->updateInset(inset);
894 setUpdateStatus(bv, CURSOR_PAR);
895 if (the_locking_inset &&
896 cpar(bv) == inset_par && cpos(bv) == inset_pos)
898 inset_x = cix(bv) - top_x + drawTextXOffset;
899 inset_y = ciy(bv) + drawTextYOffset;
906 void InsetText::lfunMousePress(FuncRequest const & cmd)
910 // use this to check mouse motion for selection!
914 BufferView * bv = cmd.view();
915 FuncRequest cmd1 = cmd;
921 int tmp_x = cmd.x - drawTextXOffset;
922 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->top_y();
923 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
926 if (the_locking_inset) {
927 if (the_locking_inset == inset) {
928 the_locking_inset->localDispatch(cmd1);
931 // otherwise only unlock the_locking_inset
932 the_locking_inset->insetUnlock(bv);
933 the_locking_inset = 0;
936 no_selection = false;
938 if (bv->theLockingInset()) {
939 if (isHighlyEditableInset(inset)) {
940 // We just have to lock the inset before calling a
942 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
943 if (!bv->lockInset(uinset)) {
944 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
946 inset->localDispatch(cmd1);
947 if (the_locking_inset)
948 updateLocal(bv, CURSOR, false);
953 bool paste_internally = false;
954 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
955 localDispatch(FuncRequest(bv, LFUN_COPY));
956 paste_internally = true;
963 int old_top_y = lt->top_y();
965 lt->setCursorFromCoordinates(cmd.x - drawTextXOffset,
966 cmd.y + insetAscent);
967 // set the selection cursor!
968 lt->selection.cursor = lt->cursor;
969 lt->cursor.x_fix(lt->cursor.x());
971 if (lt->selection.set()) {
972 lt->clearSelection();
973 updateLocal(bv, FULL, false);
975 lt->clearSelection();
976 updateLocal(bv, CURSOR, false);
982 bv->owner()->setLayout(cpar(bv)->layout()->name());
984 // we moved the view we cannot do mouse selection in this case!
985 if (getLyXText(bv)->top_y() != old_top_y)
988 // Insert primary selection with middle mouse
989 // if there is a local selection in the current buffer,
991 if (cmd.button() == mouse_button::button2) {
992 if (paste_internally)
993 localDispatch(FuncRequest(bv, LFUN_PASTE));
995 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
998 getLyXText(bv)->clearSelection();
1000 showInsetCursor(bv);
1004 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
1006 BufferView * bv = cmd.view();
1007 FuncRequest cmd1 = cmd;
1011 no_selection = true;
1012 if (the_locking_inset)
1013 return the_locking_inset->localDispatch(cmd1);
1015 int tmp_x = cmd.x - drawTextXOffset;
1016 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->top_y();
1017 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
1020 if (isHighlyEditableInset(inset))
1021 ret = inset->localDispatch(cmd1);
1023 inset_x = cix(bv) - top_x + drawTextXOffset;
1024 inset_y = ciy(bv) + drawTextYOffset;
1025 cmd1.x = cmd.x - inset_x;
1026 cmd1.y = cmd.x - inset_y;
1027 // note that we should do ret = inset->localDispatch(cmd1)
1028 // and fix this instead (Alfredo);
1030 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
1032 updateLocal(bv, CURSOR_PAR, false);
1038 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
1040 FuncRequest cmd1 = cmd;
1044 if (the_locking_inset) {
1045 the_locking_inset->localDispatch(cmd1);
1049 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
1052 BufferView * bv = cmd.view();
1055 lt = getLyXText(bv);
1058 hideInsetCursor(bv);
1059 LyXCursor cur = lt->cursor;
1060 lt->setCursorFromCoordinates
1061 (cmd.x - drawTextXOffset, cmd.y + insetAscent);
1062 lt->cursor.x_fix(lt->cursor.x());
1063 if (cur == lt->cursor) {
1069 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1070 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1074 updateLocal(bv, SELECTION, false);
1076 showInsetCursor(bv);
1080 Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
1082 BufferView * bv = ev.view();
1083 switch (ev.action) {
1084 case LFUN_MOUSE_PRESS:
1087 case LFUN_MOUSE_MOTION:
1088 lfunMouseMotion(ev);
1090 case LFUN_MOUSE_RELEASE:
1091 return lfunMouseRelease(ev) ? DISPATCHED : UNDISPATCHED;
1096 bool was_empty = (paragraphs.begin()->empty() && !paragraphs.begin()->next());
1097 no_selection = false;
1098 RESULT result = UpdatableInset::localDispatch(ev);
1099 if (result != UNDISPATCHED)
1102 result = DISPATCHED;
1103 if (ev.action < 0 && ev.argument.empty())
1106 if (the_locking_inset) {
1107 result = the_locking_inset->localDispatch(ev);
1108 if (result == DISPATCHED_NOUPDATE)
1110 else if (result == DISPATCHED) {
1111 updateLocal(bv, CURSOR_PAR, false);
1113 } else if (result >= FINISHED) {
1115 case FINISHED_RIGHT:
1116 moveRightIntern(bv, false, false);
1117 result = DISPATCHED;
1120 if ((result = moveUp(bv)) >= FINISHED) {
1121 updateLocal(bv, CURSOR, false);
1122 bv->unlockInset(this);
1126 if ((result = moveDown(bv)) >= FINISHED) {
1127 updateLocal(bv, CURSOR, false);
1128 bv->unlockInset(this);
1132 result = DISPATCHED;
1135 the_locking_inset = 0;
1136 updateLocal(bv, CURSOR, false);
1140 hideInsetCursor(bv);
1143 lt = getLyXText(bv);
1147 int updflag = false;
1149 // what type of update to do on a cursor movement
1150 int cursor_update = CURSOR;
1152 if (lt->selection.set())
1153 cursor_update = SELECTION;
1155 switch (ev.action) {
1158 case LFUN_SELFINSERT:
1159 if (bv->buffer()->isReadonly()) {
1160 // setErrorMessage(N_("Document is read only"));
1163 if (!ev.argument.empty()) {
1164 /* Automatically delete the currently selected
1165 * text and replace it with what is being
1166 * typed in now. Depends on lyxrc settings
1167 * "auto_region_delete", which defaults to
1170 // This should not be needed here and is also WRONG!
1171 setUndo(bv, Undo::INSERT,
1172 lt->cursor.par(), lt->cursor.par()->next());
1175 if (lyxrc.auto_region_delete) {
1176 if (lt->selection.set()) {
1177 lt->cutSelection(false, false);
1180 lt->clearSelection();
1181 for (string::size_type i = 0; i < ev.argument.length(); ++i) {
1182 bv->owner()->getIntl().getTransManager().
1183 TranslateAndInsert(ev.argument[i], lt);
1186 lt->selection.cursor = lt->cursor;
1187 updwhat = CURSOR | CURSOR_PAR;
1189 result = DISPATCHED_NOUPDATE;
1192 // cursor movements that need special handling
1195 result = moveRight(bv);
1197 updwhat = cursor_update;
1201 result = moveLeft(bv);
1202 updwhat = cursor_update;
1206 result = moveDown(bv);
1207 updwhat = cursor_update;
1211 result = moveUp(bv);
1212 updwhat = cursor_update;
1216 if (!crow(bv)->previous())
1217 result = FINISHED_UP;
1219 lt->cursorPrevious();
1220 lt->clearSelection();
1221 result = DISPATCHED_NOUPDATE;
1223 updwhat = cursor_update;
1227 if (!crow(bv)->next())
1228 result = FINISHED_DOWN;
1231 lt->clearSelection();
1232 result = DISPATCHED_NOUPDATE;
1234 updwhat = cursor_update;
1237 case LFUN_BACKSPACE: {
1238 if (lt->selection.set())
1239 lt->cutSelection(true, false);
1242 updwhat = CURSOR_PAR;
1248 if (lt->selection.set()) {
1249 lt->cutSelection(true, false);
1253 updwhat = CURSOR_PAR;
1259 lt->cutSelection(bv);
1260 updwhat = CURSOR_PAR;
1267 lt->copySelection();
1268 updwhat = CURSOR_PAR;
1271 case LFUN_PASTESELECTION:
1273 string const clip(bv->getClipboard());
1277 if (ev.argument == "paragraph") {
1278 lt->insertStringAsParagraphs(clip);
1280 lt->insertStringAsLines(clip);
1283 lt->clearSelection();
1285 updwhat = CURSOR_PAR;
1291 if (!autoBreakRows) {
1292 if (CutAndPaste::nrOfParagraphs() > 1) {
1293 #ifdef WITH_WARNINGS
1294 #warning FIXME horrendously bad UI
1296 Alert::alert(_("Impossible operation!"),
1297 _("Cannot include more than one paragraph!"),
1303 lt->pasteSelection();
1305 lt->clearSelection();
1306 updwhat = CURSOR_PAR;
1311 case LFUN_BREAKPARAGRAPH:
1312 if (!autoBreakRows) {
1313 result = DISPATCHED;
1316 lt->breakParagraph(paragraphs, 0);
1317 updwhat = CURSOR | FULL;
1321 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1322 if (!autoBreakRows) {
1323 result = DISPATCHED;
1326 lt->breakParagraph(paragraphs, 1);
1327 updwhat = CURSOR | FULL;
1331 case LFUN_BREAKLINE: {
1332 if (!autoBreakRows) {
1333 result = DISPATCHED;
1337 lt->insertInset(new InsetNewline);
1338 updwhat = CURSOR | CURSOR_PAR;
1344 // do not set layouts on non breakable textinsets
1345 if (autoBreakRows) {
1346 string cur_layout = cpar(bv)->layout()->name();
1348 // Derive layout number from given argument (string)
1349 // and current buffer's textclass (number). */
1350 LyXTextClass const & tclass =
1351 bv->buffer()->params.getLyXTextClass();
1352 string layout = ev.argument;
1353 bool hasLayout = tclass.hasLayout(layout);
1355 // If the entry is obsolete, use the new one instead.
1357 string const & obs =
1358 tclass[layout]->obsoleted_by();
1363 // see if we found the layout number:
1365 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + ev.argument + N_(" not known"));
1366 bv->owner()->dispatch(lf);
1370 if (cur_layout != layout) {
1371 cur_layout = layout;
1372 lt->setLayout(layout);
1373 bv->owner()->setLayout(cpar(bv)->layout()->name());
1374 updwhat = CURSOR_PAR;
1378 // reset the layout box
1379 bv->owner()->setLayout(cpar(bv)->layout()->name());
1382 case LFUN_PARAGRAPH_SPACING:
1383 // This one is absolutely not working. When fiddling with this
1384 // it also seems to me that the paragraphs inside the insettext
1385 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1386 // FIXME: how old is this comment ? ...
1388 Paragraph * par = lt->cursor.par();
1389 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1390 float cur_value = 1.0;
1391 if (cur_spacing == Spacing::Other) {
1392 cur_value = par->params().spacing().getValue();
1395 istringstream istr(ev.argument.c_str());
1398 Spacing::Space new_spacing = cur_spacing;
1399 float new_value = cur_value;
1401 lyxerr << "Missing argument to `paragraph-spacing'"
1403 } else if (tmp == "single") {
1404 new_spacing = Spacing::Single;
1405 } else if (tmp == "onehalf") {
1406 new_spacing = Spacing::Onehalf;
1407 } else if (tmp == "double") {
1408 new_spacing = Spacing::Double;
1409 } else if (tmp == "other") {
1410 new_spacing = Spacing::Other;
1413 lyxerr << "new_value = " << tmpval << endl;
1416 } else if (tmp == "default") {
1417 new_spacing = Spacing::Default;
1419 lyxerr << _("Unknown spacing argument: ")
1420 << ev.argument << endl;
1422 if (cur_spacing != new_spacing || cur_value != new_value) {
1423 par->params().spacing(Spacing(new_spacing, new_value));
1424 updwhat = CURSOR_PAR;
1430 // These need to do repaints but don't require
1431 // special handling otherwise. A *lot* of the
1432 // above could probably be done similarly ...
1437 case LFUN_WORDRIGHT:
1438 // these two are really unhandled ...
1440 case LFUN_BEGINNINGBUF:
1441 updwhat = cursor_update;
1442 if (!bv->dispatch(ev))
1443 result = UNDISPATCHED;
1452 case LFUN_WORDLEFTSEL:
1453 case LFUN_WORDRIGHTSEL:
1454 updwhat = SELECTION;
1459 if (!bv->dispatch(ev))
1460 result = UNDISPATCHED;
1467 updateLocal(bv, updwhat, updflag);
1468 /// If the action has deleted all text in the inset, we need to change the
1469 // language to the language of the surronding text.
1470 if (!was_empty && paragraphs.begin()->empty() && !paragraphs.begin()->next()) {
1471 LyXFont font(LyXFont::ALL_IGNORE);
1472 font.setLanguage(bv->getParentLanguage(this));
1473 setFont(bv, font, false);
1476 if (result >= FINISHED)
1477 bv->unlockInset(this);
1479 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1480 result = DISPATCHED;
1485 int InsetText::latex(Buffer const * buf, ostream & os,
1486 bool moving_arg, bool) const
1489 latexParagraphs(buf, paragraphs,
1490 paragraphs.begin(), paragraphs.end(),
1491 os, texrow, moving_arg);
1492 return texrow.rows();
1496 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1498 unsigned int lines = 0;
1500 ParagraphList::iterator beg = paragraphs.begin();
1501 ParagraphList::iterator end = paragraphs.end();
1502 ParagraphList::iterator it = beg;
1503 for (; it != end; ++it) {
1504 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1505 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1512 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1514 Paragraph * p = &*(paragraphs.begin());
1515 unsigned int lines = 0;
1517 vector<string> environment_stack(10);
1518 vector<string> environment_inner(10);
1520 int const command_depth = 0;
1523 Paragraph::depth_type depth = 0; // paragraph depth
1527 int desc_on = 0; // description mode
1529 LyXLayout_ptr const & style = p->layout();
1531 // environment tag closing
1532 for (; depth > p->params().depth(); --depth) {
1533 if (environment_inner[depth] != "!-- --") {
1534 item_name = "listitem";
1535 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1536 if (environment_inner[depth] == "varlistentry")
1537 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1539 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1540 environment_stack[depth].erase();
1541 environment_inner[depth].erase();
1544 if (depth == p->params().depth()
1545 && environment_stack[depth] != style->latexname()
1546 && !environment_stack[depth].empty()) {
1547 if (environment_inner[depth] != "!-- --") {
1548 item_name= "listitem";
1549 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1550 if (environment_inner[depth] == "varlistentry")
1551 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1554 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1556 environment_stack[depth].erase();
1557 environment_inner[depth].erase();
1560 // Write opening SGML tags.
1561 switch (style->latextype) {
1562 case LATEX_PARAGRAPH:
1563 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1567 buf->sgmlError(p, 0, _("Error: LatexType Command not allowed here.\n"));
1571 case LATEX_ENVIRONMENT:
1572 case LATEX_ITEM_ENVIRONMENT:
1573 if (depth < p->params().depth()) {
1574 depth = p->params().depth();
1575 environment_stack[depth].erase();
1578 if (environment_stack[depth] != style->latexname()) {
1579 if (environment_stack.size() == depth + 1) {
1580 environment_stack.push_back("!-- --");
1581 environment_inner.push_back("!-- --");
1583 environment_stack[depth] = style->latexname();
1584 environment_inner[depth] = "!-- --";
1585 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1587 if (environment_inner[depth] != "!-- --") {
1588 item_name= "listitem";
1589 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1590 if (environment_inner[depth] == "varlistentry")
1591 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1595 if (style->latextype == LATEX_ENVIRONMENT) {
1596 if (!style->latexparam().empty()) {
1597 if (style->latexparam() == "CDATA")
1600 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1605 desc_on = (style->labeltype == LABEL_MANUAL);
1607 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1608 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1610 item_name = desc_on ? "term" : "para";
1611 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1615 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1619 buf->simpleDocBookOnePar(os, p, desc_on, depth + 1 + command_depth);
1623 // write closing SGML tags
1624 switch (style->latextype) {
1625 case LATEX_ENVIRONMENT:
1626 if (!style->latexparam().empty()) {
1627 if (style->latexparam() == "CDATA")
1630 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1633 case LATEX_ITEM_ENVIRONMENT:
1634 if (desc_on == 1) break;
1636 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1638 case LATEX_PARAGRAPH:
1639 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1642 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1648 for (int d = depth; d >= 0; --d) {
1649 if (!environment_stack[depth].empty()) {
1650 if (environment_inner[depth] != "!-- --") {
1651 item_name = "listitem";
1652 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1653 if (environment_inner[depth] == "varlistentry")
1654 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1657 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1665 void InsetText::validate(LaTeXFeatures & features) const
1667 for_each(paragraphs.begin(), paragraphs.end(),
1668 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1672 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1674 if (the_locking_inset) {
1675 the_locking_inset->getCursorPos(bv, x, y);
1678 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1679 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1683 int InsetText::insetInInsetY() const
1685 if (!the_locking_inset)
1688 return (inset_y + the_locking_inset->insetInInsetY());
1692 void InsetText::toggleInsetCursor(BufferView * bv)
1694 if (the_locking_inset) {
1695 the_locking_inset->toggleInsetCursor(bv);
1699 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1701 int const asc = font_metrics::maxAscent(font);
1702 int const desc = font_metrics::maxDescent(font);
1704 if (isCursorVisible())
1705 bv->hideLockedInsetCursor();
1707 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1708 toggleCursorVisible();
1712 void InsetText::showInsetCursor(BufferView * bv, bool show)
1714 if (the_locking_inset) {
1715 the_locking_inset->showInsetCursor(bv, show);
1718 if (!isCursorVisible()) {
1719 LyXFont const font =
1720 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1722 int const asc = font_metrics::maxAscent(font);
1723 int const desc = font_metrics::maxDescent(font);
1725 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1727 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1728 setCursorVisible(true);
1733 void InsetText::hideInsetCursor(BufferView * bv)
1735 if (isCursorVisible()) {
1736 bv->hideLockedInsetCursor();
1737 setCursorVisible(false);
1739 if (the_locking_inset)
1740 the_locking_inset->hideInsetCursor(bv);
1744 void InsetText::fitInsetCursor(BufferView * bv) const
1746 if (the_locking_inset) {
1747 the_locking_inset->fitInsetCursor(bv);
1750 LyXFont const font =
1751 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1753 int const asc = font_metrics::maxAscent(font);
1754 int const desc = font_metrics::maxDescent(font);
1756 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1757 need_update |= FULL;
1762 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1764 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1765 return moveLeftIntern(bv, false, activate_inset, selecting);
1767 return moveRightIntern(bv, true, activate_inset, selecting);
1772 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1774 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1775 return moveRightIntern(bv, true, activate_inset, selecting);
1777 return moveLeftIntern(bv, false, activate_inset, selecting);
1782 InsetText::moveRightIntern(BufferView * bv, bool front,
1783 bool activate_inset, bool selecting)
1785 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1786 return FINISHED_RIGHT;
1787 if (activate_inset && checkAndActivateInset(bv, front))
1789 getLyXText(bv)->cursorRight(bv);
1791 getLyXText(bv)->clearSelection();
1792 return DISPATCHED_NOUPDATE;
1797 InsetText::moveLeftIntern(BufferView * bv, bool front,
1798 bool activate_inset, bool selecting)
1800 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1802 getLyXText(bv)->cursorLeft(bv);
1804 getLyXText(bv)->clearSelection();
1805 if (activate_inset && checkAndActivateInset(bv, front))
1807 return DISPATCHED_NOUPDATE;
1811 Inset::RESULT InsetText::moveUp(BufferView * bv)
1813 if (!crow(bv)->previous())
1815 getLyXText(bv)->cursorUp(bv);
1816 getLyXText(bv)->clearSelection();
1817 return DISPATCHED_NOUPDATE;
1821 Inset::RESULT InsetText::moveDown(BufferView * bv)
1823 if (!crow(bv)->next())
1824 return FINISHED_DOWN;
1825 getLyXText(bv)->cursorDown(bv);
1826 getLyXText(bv)->clearSelection();
1827 return DISPATCHED_NOUPDATE;
1831 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1833 if (the_locking_inset) {
1834 if (the_locking_inset->insetAllowed(inset))
1835 return the_locking_inset->insertInset(bv, inset);
1838 inset->setOwner(this);
1839 hideInsetCursor(bv);
1840 getLyXText(bv)->insertInset(inset);
1842 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1847 bool InsetText::insetAllowed(Inset::Code code) const
1849 // in_insetAllowed is a really gross hack,
1850 // to allow us to call the owner's insetAllowed
1851 // without stack overflow, which can happen
1852 // when the owner uses InsetCollapsable::insetAllowed()
1854 if (in_insetAllowed)
1856 in_insetAllowed = true;
1857 if (the_locking_inset)
1858 ret = the_locking_inset->insetAllowed(code);
1860 ret = owner()->insetAllowed(code);
1861 in_insetAllowed = false;
1866 UpdatableInset * InsetText::getLockingInset() const
1868 return the_locking_inset ? the_locking_inset->getLockingInset() :
1869 const_cast<InsetText *>(this);
1873 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1877 if (the_locking_inset)
1878 return the_locking_inset->getFirstLockingInsetOfType(c);
1883 bool InsetText::showInsetDialog(BufferView * bv) const
1885 if (the_locking_inset)
1886 return the_locking_inset->showInsetDialog(bv);
1891 vector<string> const InsetText::getLabelList() const
1893 vector<string> label_list;
1895 ParagraphList::iterator pit = paragraphs.begin();
1896 ParagraphList::iterator pend = paragraphs.end();
1897 for (; pit != pend; ++pit) {
1898 InsetList::iterator beg = pit->insetlist.begin();
1899 InsetList::iterator end = pit->insetlist.end();
1900 for (; beg != end; ++beg) {
1901 vector<string> const l = beg.getInset()->getLabelList();
1902 label_list.insert(label_list.end(), l.begin(), l.end());
1909 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1912 if (the_locking_inset) {
1913 the_locking_inset->setFont(bv, font, toggleall, selectall);
1916 if ((!paragraphs.begin()->next() && paragraphs.begin()->empty()) || cpar(bv)->empty()) {
1917 getLyXText(bv)->setFont(font, toggleall);
1922 lt = getLyXText(bv);
1925 if (lt->selection.set()) {
1926 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1930 lt->toggleFree(font, toggleall);
1932 lt->clearSelection();
1934 bool flag = (selectall || lt->selection.set());
1938 updateLocal(bv, FULL, true);
1940 updateLocal(bv, CURSOR_PAR, true);
1944 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1946 if (cpar(bv)->isInset(cpos(bv))) {
1948 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1949 if (!isHighlyEditableInset(inset))
1951 inset->edit(bv, front);
1952 if (!the_locking_inset)
1954 updateLocal(bv, CURSOR, false);
1961 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1962 mouse_button::state button)
1964 x -= drawTextXOffset;
1966 int dummyy = y + insetAscent;
1967 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1968 // we only do the edit() call if the inset was hit by the mouse
1969 // or if it is a highly editable inset. So we should call this
1970 // function from our own edit with button < 0.
1971 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1973 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1981 inset_x = cix(bv) - top_x + drawTextXOffset;
1982 inset_y = ciy(bv) + drawTextYOffset;
1983 inset->edit(bv, x - inset_x, y - inset_y, button);
1984 if (!the_locking_inset)
1986 updateLocal(bv, CURSOR, false);
1993 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1996 int w = UpdatableInset::getMaxWidth(bv, inset);
2001 w = w - top_x + owner()->x();
2004 w -= (2 * TEXT_TO_INSET_OFFSET);
2007 return UpdatableInset::getMaxWidth(bv, inset);
2012 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2014 // we have to unlock any locked inset otherwise we're in troubles
2015 the_locking_inset = 0;
2018 paragraphs.set(new Paragraph(*p, same_id));
2019 paragraphs.begin()->setInsetOwner(this);
2020 Paragraph * np = &*(paragraphs.begin());
2023 np->next(new Paragraph(*p, same_id));
2024 np->next()->previous(np);
2026 np->setInsetOwner(this);
2033 void InsetText::markNew(bool track_changes)
2035 ParagraphList::iterator pit = paragraphs.begin();
2036 ParagraphList::iterator pend = paragraphs.end();
2037 for (; pit != pend; ++pit) {
2038 if (track_changes) {
2039 pit->trackChanges();
2041 // no-op when not tracking
2042 pit->cleanChanges();
2048 void InsetText::setText(string const & data, LyXFont const & font)
2051 for (unsigned int i = 0; i < data.length(); ++i)
2052 paragraphs.begin()->insertChar(i, data[i], font);
2057 void InsetText::setAutoBreakRows(bool flag)
2059 if (flag != autoBreakRows) {
2060 autoBreakRows = flag;
2068 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2070 if (how != drawFrame_) {
2073 updateLocal(bv, DRAW_FRAME, false);
2078 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2080 if (frame_color != col) {
2083 updateLocal(bv, DRAW_FRAME, false);
2088 int InsetText::cx(BufferView * bv) const
2090 // we do nothing dangerous so we use a local cache
2091 LyXText * llt = getLyXText(bv);
2092 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2093 if (the_locking_inset) {
2094 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2096 if (font.isVisibleRightToLeft())
2097 x -= the_locking_inset->width(bv, font);
2103 int InsetText::cix(BufferView * bv) const
2105 // we do nothing dangerous so we use a local cache
2106 LyXText * llt = getLyXText(bv);
2107 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2108 if (the_locking_inset) {
2109 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2111 if (font.isVisibleRightToLeft())
2112 x -= the_locking_inset->width(bv, font);
2118 int InsetText::cy(BufferView * bv) const
2121 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2125 int InsetText::ciy(BufferView * bv) const
2128 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2132 pos_type InsetText::cpos(BufferView * bv) const
2134 return getLyXText(bv)->cursor.pos();
2138 Paragraph * InsetText::cpar(BufferView * bv) const
2140 return getLyXText(bv)->cursor.par();
2144 bool InsetText::cboundary(BufferView * bv) const
2146 return getLyXText(bv)->cursor.boundary();
2150 Row * InsetText::crow(BufferView * bv) const
2152 return getLyXText(bv)->cursor.row();
2156 LyXText * InsetText::getLyXText(BufferView const * lbv,
2157 bool const recursive) const
2159 if (cached_bview == lbv) {
2160 if (recursive && the_locking_inset)
2161 return the_locking_inset->getLyXText(lbv, true);
2162 LyXText * lt = cached_text.get();
2163 lyx::Assert(lt && lt->firstRow()->par() == &*(paragraphs.begin()));
2166 // Super UGLY! (Lgb)
2167 BufferView * bv = const_cast<BufferView *>(lbv);
2170 Cache::iterator it = cache.find(bv);
2172 if (it != cache.end()) {
2175 } else if (do_resize) {
2176 resizeLyXText(do_resize);
2178 if (lt || !it->second.remove) {
2179 lyx::Assert(it->second.text.get());
2180 cached_text = it->second.text;
2181 if (recursive && the_locking_inset) {
2182 return the_locking_inset->getLyXText(bv, true);
2184 return cached_text.get();
2185 } else if (it->second.remove) {
2187 saveLyXTextState(it->second.text.get());
2193 // when we have to reinit the existing LyXText!
2195 it->second.text->init(bv);
2196 restoreLyXTextState(it->second.text.get());
2197 it->second.remove = false;
2199 cached_text = it->second.text;
2200 if (the_locking_inset && recursive) {
2201 return the_locking_inset->getLyXText(bv);
2203 return cached_text.get();
2206 // we are here only if we don't have a BufferView * in the cache!!!
2208 cached_text.reset(new LyXText(bv, const_cast<InsetText *>(this)));
2209 cached_text->init(bv);
2210 restoreLyXTextState(cached_text.get());
2212 cache.insert(make_pair(bv, cached_text));
2214 if (the_locking_inset && recursive) {
2215 return the_locking_inset->getLyXText(bv);
2217 return cached_text.get();
2221 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2225 Cache::iterator it = cache.find(bv);
2227 if (it == cache.end()) {
2231 lyx::Assert(it->second.text.get());
2233 it->second.remove = true;
2235 /// then remove all LyXText in text-insets
2236 for_each(paragraphs.begin(), paragraphs.end(),
2237 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2242 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2245 // we cannot resize this because we are in use!
2246 // so do this on the next possible getLyXText()
2251 if (!paragraphs.begin()->next() && paragraphs.begin()->empty()) { // no data, resize not neccessary!
2252 // we have to do this as a fixed width may have changed!
2253 LyXText * t = getLyXText(bv);
2254 saveLyXTextState(t);
2256 restoreLyXTextState(t);
2259 // one endless line, resize normally not necessary
2260 if (!force && getMaxWidth(bv, this) < 0)
2263 Cache::iterator it = cache.find(bv);
2264 if (it == cache.end()) {
2267 lyx::Assert(it->second.text.get());
2269 LyXText * t = it->second.text.get();
2270 saveLyXTextState(t);
2272 for_each(paragraphs.begin(), paragraphs.end(),
2273 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2276 restoreLyXTextState(t);
2277 if (the_locking_inset) {
2278 inset_x = cix(bv) - top_x + drawTextXOffset;
2279 inset_y = ciy(bv) + drawTextYOffset;
2282 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2284 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2285 // this will scroll the screen such that the cursor becomes visible
2286 bv->updateScrollbar();
2288 need_update |= FULL;
2293 void InsetText::reinitLyXText() const
2296 // we cannot resize this because we are in use!
2297 // so do this on the next possible getLyXText()
2303 for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2304 lyx::Assert(it->second.text.get());
2306 LyXText * t = it->second.text.get();
2307 BufferView * bv = it->first;
2309 saveLyXTextState(t);
2311 for_each(paragraphs.begin(), paragraphs.end(),
2312 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2315 restoreLyXTextState(t);
2316 if (the_locking_inset) {
2317 inset_x = cix(bv) - top_x + drawTextXOffset;
2318 inset_y = ciy(bv) + drawTextYOffset;
2320 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2322 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2323 // this will scroll the screen such that the cursor becomes visible
2324 bv->updateScrollbar();
2332 void InsetText::removeNewlines()
2334 bool changed = false;
2336 ParagraphList::iterator it = paragraphs.begin();
2337 ParagraphList::iterator end = paragraphs.end();
2338 for (; it != end; ++it) {
2339 for (int i = 0; i < it->size(); ++i) {
2340 if (it->isNewline(i)) {
2351 bool InsetText::nodraw() const
2353 if (the_locking_inset)
2354 return the_locking_inset->nodraw();
2355 return UpdatableInset::nodraw();
2359 int InsetText::scroll(bool recursive) const
2361 int sx = UpdatableInset::scroll(false);
2363 if (recursive && the_locking_inset)
2364 sx += the_locking_inset->scroll(recursive);
2370 void InsetText::selectAll(BufferView * bv)
2372 getLyXText(bv)->cursorTop();
2373 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2374 getLyXText(bv)->cursorBottom();
2375 getLyXText(bv)->setSelection();
2379 void InsetText::clearSelection(BufferView * bv)
2381 getLyXText(bv)->clearSelection();
2385 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2387 Painter & pain = bv->painter();
2389 int h = insetAscent + insetDescent;
2390 int ty = baseline - insetAscent;
2396 if ((ty + h) > pain.paperHeight())
2397 h = pain.paperHeight();
2398 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2399 w = pain.paperWidth();
2400 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2405 Paragraph * InsetText::firstParagraph() const
2408 if (the_locking_inset)
2409 if ((result = the_locking_inset->firstParagraph()))
2411 return &*(paragraphs.begin());
2415 Paragraph * InsetText::getFirstParagraph(int i) const
2417 return (i == 0) ? &*(paragraphs.begin()) : 0;
2421 LyXCursor const & InsetText::cursor(BufferView * bv) const
2423 if (the_locking_inset)
2424 return the_locking_inset->cursor(bv);
2425 return getLyXText(bv)->cursor;
2429 Paragraph * InsetText::paragraph() const
2431 return &*(paragraphs.begin());
2435 void InsetText::paragraph(Paragraph * p)
2437 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2438 // caller of this function has to take care of it. This IS important
2439 // as we could have to insert a paragraph before this one and just
2440 // link the actual to a new ones next and set it with this function
2443 // set ourself as owner for all the paragraphs inserted!
2444 for_each(paragraphs.begin(), paragraphs.end(),
2445 boost::bind(&Paragraph::setInsetOwner, _1, this));
2448 // redraw myself when asked for
2453 Inset * InsetText::getInsetFromID(int id_arg) const
2456 return const_cast<InsetText *>(this);
2458 ParagraphList::iterator pit = paragraphs.begin();
2459 ParagraphList::iterator pend = paragraphs.end();
2460 for (; pit != pend; ++pit) {
2461 InsetList::iterator it = pit->insetlist.begin();
2462 InsetList::iterator end = pit->insetlist.end();
2463 for (; it != end; ++it) {
2464 if (it.getInset()->id() == id_arg)
2465 return it.getInset();
2466 Inset * in = it.getInset()->getInsetFromID(id_arg);
2476 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2477 float & value) const
2483 lt = getLyXText(bv);
2486 if (the_locking_inset) {
2487 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2488 if (!word.word().empty()) {
2494 // we have to go on checking so move cursor to the next char
2495 lt->cursor.pos(lt->cursor.pos() + 1);
2497 word = lt->selectNextWordToSpellcheck(value);
2498 if (word.word().empty())
2499 bv->unlockInset(const_cast<InsetText *>(this));
2508 void InsetText::selectSelectedWord(BufferView * bv)
2510 if (the_locking_inset) {
2511 the_locking_inset->selectSelectedWord(bv);
2514 getLyXText(bv)->selectSelectedWord();
2515 updateLocal(bv, SELECTION, false);
2519 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2521 if (the_locking_inset) {
2522 the_locking_inset->toggleSelection(bv, kill_selection);
2526 lt = getLyXText(bv);
2530 int x = top_x + TEXT_TO_INSET_OFFSET;
2532 Row * row = lt->firstRow();
2533 int y_offset = top_baseline - row->ascent_of_text();
2535 while ((row != 0) && ((y+row->height()) <= 0)) {
2542 if (need_update & SELECTION)
2544 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2550 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2554 lt = getLyXText(bv);
2557 if (the_locking_inset) {
2558 if (the_locking_inset->nextChange(bv, length))
2560 lt->cursorRight(true);
2562 lyxfind::SearchResult result =
2563 lyxfind::findNextChange(bv, lt, length);
2565 if (result == lyxfind::SR_FOUND) {
2566 LyXCursor cur = lt->cursor;
2567 bv->unlockInset(bv->theLockingInset());
2568 if (bv->lockInset(this))
2571 lt->setSelectionRange(length);
2572 updateLocal(bv, SELECTION, false);
2576 return result != lyxfind::SR_NOT_FOUND;
2580 bool InsetText::searchForward(BufferView * bv, string const & str,
2585 lt = getLyXText(bv);
2588 if (the_locking_inset) {
2589 if (the_locking_inset->searchForward(bv, str, cs, mw))
2591 lt->cursorRight(true);
2593 lyxfind::SearchResult result =
2594 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2596 if (result == lyxfind::SR_FOUND) {
2597 LyXCursor cur = lt->cursor;
2598 bv->unlockInset(bv->theLockingInset());
2599 if (bv->lockInset(this))
2602 lt->setSelectionRange(str.length());
2603 updateLocal(bv, SELECTION, false);
2607 return (result != lyxfind::SR_NOT_FOUND);
2610 bool InsetText::searchBackward(BufferView * bv, string const & str,
2613 if (the_locking_inset) {
2614 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2619 lt = getLyXText(bv);
2623 Paragraph * p = &*(paragraphs.begin());
2626 lt->setCursor(p, p->size());
2628 lyxfind::SearchResult result =
2629 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2631 if (result == lyxfind::SR_FOUND) {
2632 LyXCursor cur = lt->cursor;
2633 bv->unlockInset(bv->theLockingInset());
2634 if (bv->lockInset(this))
2637 lt->setSelectionRange(str.length());
2638 updateLocal(bv, SELECTION, false);
2642 return (result != lyxfind::SR_NOT_FOUND);
2646 bool InsetText::checkInsertChar(LyXFont & font)
2649 return owner()->checkInsertChar(font);
2654 void InsetText::collapseParagraphs(BufferView * bv)
2656 LyXText * llt = getLyXText(bv);
2658 while (paragraphs.begin()->next()) {
2659 if (!paragraphs.begin()->empty() && !paragraphs.begin()->next()->empty() &&
2660 !paragraphs.begin()->isSeparator(paragraphs.begin()->size() - 1))
2662 paragraphs.begin()->insertChar(paragraphs.begin()->size(), ' ');
2664 if (llt->selection.set()) {
2665 if (llt->selection.start.par() == paragraphs.begin()->next()) {
2666 llt->selection.start.par(&*(paragraphs.begin()));
2667 llt->selection.start.pos(
2668 llt->selection.start.pos() + paragraphs.begin()->size());
2670 if (llt->selection.end.par() == paragraphs.begin()->next()) {
2671 llt->selection.end.par(&*(paragraphs.begin()));
2672 llt->selection.end.pos(
2673 llt->selection.end.pos() + paragraphs.begin()->size());
2676 mergeParagraph(bv->buffer()->params, paragraphs, paragraphs.begin());
2682 void InsetText::getDrawFont(LyXFont & font) const
2686 owner()->getDrawFont(font);
2690 void InsetText::appendParagraphs(Buffer * buffer,
2693 BufferParams const & bparams = buffer->params;
2695 Paragraph * tmpbuf = newpar;
2696 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2697 if (bparams.tracking_changes)
2698 buf->cleanChanges();
2700 while (tmpbuf->next()) {
2701 tmpbuf = tmpbuf->next();
2702 lastbuffer->next(new Paragraph(*tmpbuf, false));
2703 lastbuffer->next()->previous(lastbuffer);
2704 lastbuffer = lastbuffer->next();
2705 if (bparams.tracking_changes)
2706 lastbuffer->cleanChanges();
2708 lastbuffer = &*(paragraphs.begin());
2709 while (lastbuffer->next())
2710 lastbuffer = lastbuffer->next();
2711 if (!newpar->empty() && !lastbuffer->empty() &&
2712 !lastbuffer->isSeparator(lastbuffer->size() - 1))
2714 lastbuffer->insertChar(lastbuffer->size(), ' ');
2717 // make the buf exactly the same layout than our last paragraph
2718 buf->makeSameLayout(lastbuffer);
2721 lastbuffer->next(buf);
2722 buf->previous(lastbuffer);
2723 mergeParagraph(buffer->params, paragraphs, lastbuffer);
2729 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2731 Paragraph * par = getFirstParagraph(0);
2733 InsetList::iterator it = par->insetlist.begin();
2734 InsetList::iterator end = par->insetlist.end();
2735 for (; it != end; ++it) {
2736 it.getInset()->addPreview(loader);