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 Painter & pain = bv->painter();
355 // this is the first thing we have to ask because if the x pos
356 // changed we have to do a complete rebreak of the text as we
357 // may have few space to draw in. Well we should check on this too
358 if (top_x != int(x)) {
361 int nw = getMaxWidth(bv, this);
362 if (nw > 0 && old_max_width != nw) {
365 bv->text->postChangedInDraw();
370 // call these methods so that insetWidth, insetAscent and
371 // insetDescent have the right values.
376 // repaint the background if needed
377 if (backgroundColor() != LColor::background)
378 clearInset(bv, baseline);
380 // no draw is necessary !!!
381 if ((drawFrame_ == LOCKED) && !locked && paragraphs.begin()->empty()) {
382 top_baseline = baseline;
389 x += static_cast<float>(scroll());
391 top_baseline = baseline;
392 top_y = baseline - insetAscent;
394 if (last_drawn_width != insetWidth) {
396 last_drawn_width = insetWidth;
399 if (the_locking_inset && (cpar(bv) == inset_par)
400 && (cpos(bv) == inset_pos)) {
401 inset_x = cix(bv) - top_x + drawTextXOffset;
402 inset_y = ciy(bv) + drawTextYOffset;
410 x += TEXT_TO_INSET_OFFSET;
412 Row * row = lt->firstRow();
413 int y_offset = baseline - row->ascent_of_text();
414 int ph = pain.paperHeight();
417 while ((row != 0) && ((y+row->height()) <= 0)) {
419 first += row->height();
423 lt->top_y(-y_offset);
431 int yf = y_offset + first;
433 while ((row != 0) && (yf < ph)) {
434 Row * prev = row->previous();
435 RowPainter rp(*bv, *lt, *row);
436 if (rp.paint(y + y_offset + first, int(x), y + lt->top_y()))
437 lt->markChangeInDraw(row, prev);
438 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
439 lt->need_break_row = row;
441 lt->setCursor(lt->cursor.par(),
443 if (lt->selection.set()) {
444 lt->setCursor(lt->selection.start,
445 lt->selection.start.par(),
446 lt->selection.start.pos());
447 lt->setCursor(lt->selection.end,
448 lt->selection.end.par(),
449 lt->selection.end.pos());
460 if ((drawFrame_ == ALWAYS) || (drawFrame_ == LOCKED && locked)) {
464 x += insetWidth - TEXT_TO_INSET_OFFSET;
466 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
468 } else if (need_update != INIT) {
476 void InsetText::drawFrame(Painter & pain) const
478 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
479 frame_x = top_x + ttoD2;
480 frame_y = top_baseline - insetAscent + ttoD2;
481 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
482 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
483 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
488 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
491 if (reinit && owner()) {
493 owner()->update(bv, font, true);
498 if (reinit || need_update == INIT) {
500 // we should put this call where we set need_update to INIT!
503 owner()->update(bv, font, true);
508 if (!autoBreakRows && paragraphs.begin()->next())
509 collapseParagraphs(bv);
511 if (the_locking_inset) {
512 inset_x = cix(bv) - top_x + drawTextXOffset;
513 inset_y = ciy(bv) + drawTextYOffset;
514 the_locking_inset->update(bv, font, reinit);
522 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
525 lt->updateInset(the_locking_inset);
527 if (lt->status() == LyXText::NEED_MORE_REFRESH)
535 void InsetText::setUpdateStatus(BufferView * bv, int what) const
537 // this does nothing dangerous so use only a localized buffer
538 LyXText * llt = getLyXText(bv);
541 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
542 // if we don't break row so that we only have one row to update!
543 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
545 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
548 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
549 need_update |= CURSOR_PAR;
552 // this to not draw a selection when we redraw all of it!
553 if (need_update & CURSOR && !(need_update & SELECTION)) {
554 if (llt->selection.set())
556 llt->clearSelection();
561 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
563 if (!autoBreakRows && paragraphs.begin()->next())
564 collapseParagraphs(bv);
571 setUpdateStatus(bv, what);
572 bool flag = mark_dirty ||
573 (((need_update != CURSOR) && (need_update != NONE)) ||
574 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
575 if (!lt->selection.set())
576 lt->selection.cursor = lt->cursor;
580 // IMO this is not anymore needed as we do this in fitInsetCursor!
581 // and we always get "true" as returnvalue of this function in the
582 // case of a locking inset (Jug 20020412)
583 if (locked && (need_update & CURSOR) && bv->fitCursor())
589 bv->updateInset(const_cast<InsetText *>(this));
591 if (need_update == CURSOR)
593 bv->owner()->view_state_changed();
594 bv->owner()->updateMenubar();
595 bv->owner()->updateToolbar();
596 if (old_par != cpar(bv)) {
597 bv->owner()->setLayout(cpar(bv)->layout()->name());
603 string const InsetText::editMessage() const
605 return _("Opened Text Inset");
609 void InsetText::edit(BufferView * bv, int x, int y, mouse_button::state button)
611 UpdatableInset::edit(bv, x, y, button);
613 if (!bv->lockInset(this)) {
614 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
618 the_locking_inset = 0;
619 inset_pos = inset_x = inset_y = 0;
620 inset_boundary = false;
623 int tmp_y = (y < 0) ? 0 : y;
629 // we put here -1 and not button as now the button in the
630 // edit call should not be needed we will fix this in 1.3.x
631 // cycle hopefully (Jug 20020509)
632 // FIXME: GUII I've changed this to none: probably WRONG
633 if (!checkAndActivateInset(bv, x, tmp_y, mouse_button::none)) {
634 lt->setCursorFromCoordinates(x - drawTextXOffset,
636 lt->cursor.x_fix(lt->cursor.x());
638 lt->clearSelection();
640 // If the inset is empty set the language of the current font to the
641 // language to the surronding text (if different).
642 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
643 bv->getParentLanguage(this) != lt->current_font.language())
645 LyXFont font(LyXFont::ALL_IGNORE);
646 font.setLanguage(bv->getParentLanguage(this));
647 setFont(bv, font, false);
653 if (drawFrame_ == LOCKED)
654 code = CURSOR|DRAW_FRAME;
655 updateLocal(bv, code, false);
658 // Tell the paragraph dialog that we've entered an insettext.
659 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
663 void InsetText::edit(BufferView * bv, bool front)
665 UpdatableInset::edit(bv, front);
667 if (!bv->lockInset(this)) {
668 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
672 the_locking_inset = 0;
673 inset_pos = inset_x = inset_y = 0;
674 inset_boundary = false;
683 lt->setCursor(&*(paragraphs.begin()), 0);
685 ParagraphList::iterator it = paragraphs.begin();
686 ParagraphList::iterator end = paragraphs.end();
687 while (boost::next(it) != end)
689 // int const pos = (p->size() ? p->size()-1 : p->size());
690 lt->setCursor(&*it, it->size());
692 lt->clearSelection();
694 // If the inset is empty set the language of the current font to the
695 // language to the surronding text (if different).
696 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
697 bv->getParentLanguage(this) != lt->current_font.language()) {
698 LyXFont font(LyXFont::ALL_IGNORE);
699 font.setLanguage(bv->getParentLanguage(this));
700 setFont(bv, font, false);
705 if (drawFrame_ == LOCKED)
706 code = CURSOR|DRAW_FRAME;
707 updateLocal(bv, code, false);
712 void InsetText::insetUnlock(BufferView * bv)
714 if (the_locking_inset) {
715 the_locking_inset->insetUnlock(bv);
716 the_locking_inset = 0;
717 updateLocal(bv, CURSOR_PAR, false);
729 if (lt->selection.set()) {
730 lt->clearSelection();
732 } else if (owner()) {
733 bv->owner()->setLayout(owner()->getLyXText(bv)
734 ->cursor.par()->layout()->name());
736 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
737 // hack for deleteEmptyParMech
738 if (!paragraphs.begin()->empty()) {
739 lt->setCursor(&*(paragraphs.begin()), 0);
740 } else if (paragraphs.begin()->next()) {
741 lt->setCursor(paragraphs.begin()->next(), 0);
746 updateLocal(bv, code, false);
749 setUpdateStatus(bv, code);
754 void InsetText::lockInset(BufferView * bv)
757 the_locking_inset = 0;
758 inset_pos = inset_x = inset_y = 0;
759 inset_boundary = false;
767 lt->setCursor(&*(paragraphs.begin()), 0);
768 lt->clearSelection();
770 // If the inset is empty set the language of the current font to the
771 // language to the surronding text (if different).
772 if (paragraphs.begin()->empty() && !paragraphs.begin()->next() &&
773 bv->getParentLanguage(this) != lt->current_font.language()) {
774 LyXFont font(LyXFont::ALL_IGNORE);
775 font.setLanguage(bv->getParentLanguage(this));
776 setFont(bv, font, false);
781 if (drawFrame_ == LOCKED)
782 code = CURSOR|DRAW_FRAME;
783 setUpdateStatus(bv, code);
787 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
789 the_locking_inset = inset;
790 inset_x = cix(bv) - top_x + drawTextXOffset;
791 inset_y = ciy(bv) + drawTextYOffset;
792 inset_pos = cpos(bv);
793 inset_par = cpar(bv);
794 inset_boundary = cboundary(bv);
795 updateLocal(bv, CURSOR, false);
799 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
801 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
805 if (!the_locking_inset) {
806 Paragraph * p = &*(paragraphs.begin());
807 int const id = inset->id();
809 InsetList::iterator it =
810 p->insetlist.begin();
811 InsetList::iterator const end =
813 for (; it != end; ++it) {
814 if (it.getInset() == inset) {
815 getLyXText(bv)->setCursorIntern(p, it.getPos());
816 lockInset(bv, inset);
819 if (it.getInset()->getInsetFromID(id)) {
820 getLyXText(bv)->setCursorIntern(p, it.getPos());
821 it.getInset()->edit(bv);
822 return the_locking_inset->lockInsetInInset(bv, inset);
829 if (inset == cpar(bv)->getInset(cpos(bv))) {
830 lyxerr[Debug::INSETS] << "OK" << endl;
831 lockInset(bv, inset);
833 } else if (the_locking_inset && (the_locking_inset == inset)) {
834 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
835 lyxerr[Debug::INSETS] << "OK" << endl;
836 inset_x = cix(bv) - top_x + drawTextXOffset;
837 inset_y = ciy(bv) + drawTextYOffset;
839 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
841 } else if (the_locking_inset) {
842 lyxerr[Debug::INSETS] << "MAYBE" << endl;
843 return the_locking_inset->lockInsetInInset(bv, inset);
845 lyxerr[Debug::INSETS] << "NOT OK" << endl;
850 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
853 if (!the_locking_inset)
855 if (the_locking_inset == inset) {
856 the_locking_inset->insetUnlock(bv);
857 getLyXText(bv)->updateInset(inset);
858 the_locking_inset = 0;
860 moveRightIntern(bv, true, false);
861 old_par = 0; // force layout setting
865 updateLocal(bv, CURSOR, false);
868 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
872 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
874 if (!autoBreakRows && paragraphs.begin()->next())
875 collapseParagraphs(bv);
883 if (inset->owner() != this) {
884 int ustat = CURSOR_PAR;
886 UpdatableInset * tl_inset = the_locking_inset;
888 found = tl_inset->updateInsetInInset(bv, inset);
890 tl_inset = static_cast<UpdatableInset *>(inset);
891 while(tl_inset->owner() && tl_inset->owner() != this)
892 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
893 if (!tl_inset->owner())
895 found = tl_inset->updateInsetInInset(bv, inset);
899 lt->updateInset(tl_inset);
903 setUpdateStatus(bv, ustat);
906 bool found = lt->updateInset(inset);
910 setUpdateStatus(bv, CURSOR_PAR);
911 if (the_locking_inset &&
912 cpar(bv) == inset_par && cpos(bv) == inset_pos)
914 inset_x = cix(bv) - top_x + drawTextXOffset;
915 inset_y = ciy(bv) + drawTextYOffset;
922 void InsetText::lfunMousePress(FuncRequest const & cmd)
926 // use this to check mouse motion for selection!
930 BufferView * bv = cmd.view();
931 FuncRequest cmd1 = cmd;
937 int tmp_x = cmd.x - drawTextXOffset;
938 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->top_y();
939 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
942 if (the_locking_inset) {
943 if (the_locking_inset == inset) {
944 the_locking_inset->localDispatch(cmd1);
949 // otherwise unlock the_locking_inset and lock the new inset
950 the_locking_inset->insetUnlock(bv);
951 inset_x = cix(bv) - top_x + drawTextXOffset;
952 inset_y = ciy(bv) + drawTextYOffset;
953 the_locking_inset = 0;
954 inset->localDispatch(cmd1);
955 // inset->edit(bv, x - inset_x, y - inset_y, button);
956 if (the_locking_inset)
957 updateLocal(bv, CURSOR, false);
961 // otherwise only unlock the_locking_inset
962 the_locking_inset->insetUnlock(bv);
963 the_locking_inset = 0;
966 no_selection = false;
968 if (bv->theLockingInset()) {
969 if (isHighlyEditableInset(inset)) {
970 // We just have to lock the inset before calling a
972 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
973 if (!bv->lockInset(uinset)) {
974 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
976 inset->localDispatch(cmd1);
977 if (the_locking_inset)
978 updateLocal(bv, CURSOR, false);
982 if (!inset) { // && (button == mouse_button::button2)) {
983 bool paste_internally = false;
984 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
985 localDispatch(FuncRequest(bv, LFUN_COPY));
986 paste_internally = true;
993 int old_top_y = lt->top_y();
995 lt->setCursorFromCoordinates(cmd.x - drawTextXOffset,
996 cmd.y + insetAscent);
997 // set the selection cursor!
998 lt->selection.cursor = lt->cursor;
999 lt->cursor.x_fix(lt->cursor.x());
1001 if (lt->selection.set()) {
1002 lt->clearSelection();
1005 updateLocal(bv, FULL, false);
1007 lt->clearSelection();
1010 updateLocal(bv, CURSOR, false);
1013 bv->owner()->setLayout(cpar(bv)->layout()->name());
1015 // we moved the view we cannot do mouse selection in this case!
1016 if (getLyXText(bv)->top_y() != old_top_y)
1017 no_selection = true;
1019 // Insert primary selection with middle mouse
1020 // if there is a local selection in the current buffer,
1022 if (cmd.button() == mouse_button::button2) {
1023 if (paste_internally)
1024 localDispatch(FuncRequest(bv, LFUN_PASTE));
1026 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
1029 getLyXText(bv)->clearSelection();
1031 showInsetCursor(bv);
1035 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
1037 BufferView * bv = cmd.view();
1038 FuncRequest cmd1 = cmd;
1042 no_selection = true;
1043 if (the_locking_inset)
1044 return the_locking_inset->localDispatch(cmd1);
1046 int tmp_x = cmd.x - drawTextXOffset;
1047 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->top_y();
1048 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
1051 if (isHighlyEditableInset(inset))
1052 ret = inset->localDispatch(cmd1);
1054 inset_x = cix(bv) - top_x + drawTextXOffset;
1055 inset_y = ciy(bv) + drawTextYOffset;
1056 cmd1.x = cmd.x - inset_x;
1057 cmd1.y = cmd.x - inset_y;
1058 // note that we should do ret = inset->localDispatch(cmd1)
1059 // and fix this instead (Alfredo);
1061 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
1063 updateLocal(bv, CURSOR_PAR, false);
1069 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
1071 FuncRequest cmd1 = cmd;
1075 if (the_locking_inset) {
1076 the_locking_inset->localDispatch(cmd1);
1080 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
1083 BufferView * bv = cmd.view();
1086 lt = getLyXText(bv);
1089 hideInsetCursor(bv);
1090 LyXCursor cur = lt->cursor;
1091 lt->setCursorFromCoordinates
1092 (cmd.x - drawTextXOffset, cmd.y + insetAscent);
1093 lt->cursor.x_fix(lt->cursor.x());
1094 if (cur == lt->cursor) {
1100 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1101 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1105 updateLocal(bv, SELECTION, false);
1107 showInsetCursor(bv);
1111 Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
1113 BufferView * bv = ev.view();
1114 switch (ev.action) {
1115 case LFUN_MOUSE_PRESS:
1118 case LFUN_MOUSE_MOTION:
1119 lfunMouseMotion(ev);
1121 case LFUN_MOUSE_RELEASE:
1122 return lfunMouseRelease(ev) ? DISPATCHED : UNDISPATCHED;
1127 bool was_empty = (paragraphs.begin()->empty() && !paragraphs.begin()->next());
1128 no_selection = false;
1129 RESULT result = UpdatableInset::localDispatch(ev);
1130 if (result != UNDISPATCHED)
1133 result = DISPATCHED;
1134 if (ev.action < 0 && ev.argument.empty())
1137 if (the_locking_inset) {
1138 result = the_locking_inset->localDispatch(ev);
1139 if (result == DISPATCHED_NOUPDATE)
1141 else if (result == DISPATCHED) {
1142 updateLocal(bv, CURSOR_PAR, false);
1144 } else if (result >= FINISHED) {
1146 case FINISHED_RIGHT:
1147 moveRightIntern(bv, false, false);
1148 result = DISPATCHED;
1151 if ((result = moveUp(bv)) >= FINISHED) {
1152 updateLocal(bv, CURSOR, false);
1153 bv->unlockInset(this);
1157 if ((result = moveDown(bv)) >= FINISHED) {
1158 updateLocal(bv, CURSOR, false);
1159 bv->unlockInset(this);
1163 result = DISPATCHED;
1166 the_locking_inset = 0;
1167 updateLocal(bv, CURSOR, false);
1171 hideInsetCursor(bv);
1174 lt = getLyXText(bv);
1178 int updflag = false;
1179 switch (ev.action) {
1182 case LFUN_SELFINSERT:
1183 if (bv->buffer()->isReadonly()) {
1184 // setErrorMessage(N_("Document is read only"));
1187 if (!ev.argument.empty()) {
1188 /* Automatically delete the currently selected
1189 * text and replace it with what is being
1190 * typed in now. Depends on lyxrc settings
1191 * "auto_region_delete", which defaults to
1194 // This should not be needed here and is also WRONG!
1195 setUndo(bv, Undo::INSERT,
1196 lt->cursor.par(), lt->cursor.par()->next());
1199 if (lyxrc.auto_region_delete) {
1200 if (lt->selection.set()) {
1201 lt->cutSelection(false, false);
1204 lt->clearSelection();
1205 for (string::size_type i = 0; i < ev.argument.length(); ++i) {
1206 bv->owner()->getIntl().getTransManager().
1207 TranslateAndInsert(ev.argument[i], lt);
1210 lt->selection.cursor = lt->cursor;
1211 updwhat = CURSOR | CURSOR_PAR;
1213 result = DISPATCHED_NOUPDATE;
1215 // --- Cursor Movements -----------------------------------
1218 moveRight(bv, false, true);
1220 updwhat = SELECTION;
1223 result = moveRight(bv);
1229 moveLeft(bv, false, true);
1231 updwhat = SELECTION;
1235 result = moveLeft(bv);
1242 updwhat = SELECTION;
1246 result = moveDown(bv);
1253 updwhat = SELECTION;
1257 result = moveUp(bv);
1270 if (!crow(bv)->previous())
1271 result = FINISHED_UP;
1273 lt->cursorPrevious();
1274 result = DISPATCHED_NOUPDATE;
1279 if (!crow(bv)->next())
1280 result = FINISHED_DOWN;
1283 result = DISPATCHED_NOUPDATE;
1287 case LFUN_BACKSPACE: {
1288 if (lt->selection.set())
1289 lt->cutSelection(true, false);
1292 updwhat = CURSOR_PAR;
1298 if (lt->selection.set()) {
1299 lt->cutSelection(true, false);
1303 updwhat = CURSOR_PAR;
1309 lt->cutSelection(bv);
1310 updwhat = CURSOR_PAR;
1317 lt->copySelection();
1318 updwhat = CURSOR_PAR;
1320 case LFUN_PASTESELECTION:
1322 string const clip(bv->getClipboard());
1326 if (ev.argument == "paragraph") {
1327 lt->insertStringAsParagraphs(clip);
1329 lt->insertStringAsLines(clip);
1332 lt->clearSelection();
1334 updwhat = CURSOR_PAR;
1339 if (!autoBreakRows) {
1341 if (CutAndPaste::nrOfParagraphs() > 1) {
1342 Alert::alert(_("Impossible operation!"),
1343 _("Cannot include more than one paragraph!"),
1349 // This should not be needed here and is also WRONG!
1350 setUndo(bv, Undo::INSERT,
1351 lt->cursor.par(), lt->cursor.par()->next());
1353 lt->pasteSelection();
1355 lt->clearSelection();
1356 updwhat = CURSOR_PAR;
1361 case LFUN_BREAKPARAGRAPH:
1362 if (!autoBreakRows) {
1363 result = DISPATCHED;
1366 lt->breakParagraph(paragraphs, 0);
1367 updwhat = CURSOR | FULL;
1370 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1371 if (!autoBreakRows) {
1372 result = DISPATCHED;
1375 lt->breakParagraph(paragraphs, 1);
1376 updwhat = CURSOR | FULL;
1380 case LFUN_BREAKLINE: {
1381 if (!autoBreakRows) {
1382 result = DISPATCHED;
1386 // This should not be needed here and is also WRONG!
1387 setUndo(bv, Undo::INSERT,
1388 lt->cursor.par(), lt->cursor.par()->next());
1390 lt->insertInset(new InsetNewline);
1391 updwhat = CURSOR | CURSOR_PAR;
1397 // do not set layouts on non breakable textinsets
1398 if (autoBreakRows) {
1399 string cur_layout = cpar(bv)->layout()->name();
1401 // Derive layout number from given argument (string)
1402 // and current buffer's textclass (number). */
1403 LyXTextClass const & tclass =
1404 bv->buffer()->params.getLyXTextClass();
1405 string layout = ev.argument;
1406 bool hasLayout = tclass.hasLayout(layout);
1408 // If the entry is obsolete, use the new one instead.
1410 string const & obs =
1411 tclass[layout]->obsoleted_by();
1416 // see if we found the layout number:
1418 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + ev.argument + N_(" not known"));
1419 bv->owner()->dispatch(lf);
1423 if (cur_layout != layout) {
1424 cur_layout = layout;
1425 lt->setLayout(layout);
1426 bv->owner()->setLayout(cpar(bv)->layout()->name());
1427 updwhat = CURSOR_PAR;
1431 // reset the layout box
1432 bv->owner()->setLayout(cpar(bv)->layout()->name());
1435 case LFUN_PARAGRAPH_SPACING:
1436 // This one is absolutely not working. When fiddling with this
1437 // it also seems to me that the paragraphs inside the insettext
1438 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1440 Paragraph * par = lt->cursor.par();
1441 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1442 float cur_value = 1.0;
1443 if (cur_spacing == Spacing::Other) {
1444 cur_value = par->params().spacing().getValue();
1447 istringstream istr(ev.argument.c_str());
1450 Spacing::Space new_spacing = cur_spacing;
1451 float new_value = cur_value;
1453 lyxerr << "Missing argument to `paragraph-spacing'"
1455 } else if (tmp == "single") {
1456 new_spacing = Spacing::Single;
1457 } else if (tmp == "onehalf") {
1458 new_spacing = Spacing::Onehalf;
1459 } else if (tmp == "double") {
1460 new_spacing = Spacing::Double;
1461 } else if (tmp == "other") {
1462 new_spacing = Spacing::Other;
1465 lyxerr << "new_value = " << tmpval << endl;
1468 } else if (tmp == "default") {
1469 new_spacing = Spacing::Default;
1471 lyxerr << _("Unknown spacing argument: ")
1472 << ev.argument << endl;
1474 if (cur_spacing != new_spacing || cur_value != new_value) {
1475 par->params().spacing(Spacing(new_spacing, new_value));
1476 updwhat = CURSOR_PAR;
1483 if (!bv->dispatch(ev))
1484 result = UNDISPATCHED;
1491 updateLocal(bv, updwhat, updflag);
1492 /// If the action has deleted all text in the inset, we need to change the
1493 // language to the language of the surronding text.
1494 if (!was_empty && paragraphs.begin()->empty() && !paragraphs.begin()->next()) {
1495 LyXFont font(LyXFont::ALL_IGNORE);
1496 font.setLanguage(bv->getParentLanguage(this));
1497 setFont(bv, font, false);
1500 if (result >= FINISHED)
1501 bv->unlockInset(this);
1503 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1504 result = DISPATCHED;
1509 int InsetText::latex(Buffer const * buf, ostream & os,
1510 bool moving_arg, bool) const
1513 latexParagraphs(buf, paragraphs,
1514 paragraphs.begin(), paragraphs.end(),
1515 os, texrow, moving_arg);
1516 return texrow.rows();
1520 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1522 unsigned int lines = 0;
1524 ParagraphList::iterator beg = paragraphs.begin();
1525 ParagraphList::iterator end = paragraphs.end();
1526 ParagraphList::iterator it = beg;
1527 for (; it != end; ++it) {
1528 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1529 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1536 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1538 Paragraph * p = &*(paragraphs.begin());
1539 unsigned int lines = 0;
1541 vector<string> environment_stack(10);
1542 vector<string> environment_inner(10);
1544 int const command_depth = 0;
1547 Paragraph::depth_type depth = 0; // paragraph depth
1551 int desc_on = 0; // description mode
1553 LyXLayout_ptr const & style = p->layout();
1555 // environment tag closing
1556 for (; depth > p->params().depth(); --depth) {
1557 if (environment_inner[depth] != "!-- --") {
1558 item_name = "listitem";
1559 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1560 if (environment_inner[depth] == "varlistentry")
1561 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1563 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1564 environment_stack[depth].erase();
1565 environment_inner[depth].erase();
1568 if (depth == p->params().depth()
1569 && environment_stack[depth] != style->latexname()
1570 && !environment_stack[depth].empty()) {
1571 if (environment_inner[depth] != "!-- --") {
1572 item_name= "listitem";
1573 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1574 if (environment_inner[depth] == "varlistentry")
1575 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1578 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1580 environment_stack[depth].erase();
1581 environment_inner[depth].erase();
1584 // Write opening SGML tags.
1585 switch (style->latextype) {
1586 case LATEX_PARAGRAPH:
1587 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1591 buf->sgmlError(p, 0, _("Error: LatexType Command not allowed here.\n"));
1595 case LATEX_ENVIRONMENT:
1596 case LATEX_ITEM_ENVIRONMENT:
1597 if (depth < p->params().depth()) {
1598 depth = p->params().depth();
1599 environment_stack[depth].erase();
1602 if (environment_stack[depth] != style->latexname()) {
1603 if (environment_stack.size() == depth + 1) {
1604 environment_stack.push_back("!-- --");
1605 environment_inner.push_back("!-- --");
1607 environment_stack[depth] = style->latexname();
1608 environment_inner[depth] = "!-- --";
1609 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1611 if (environment_inner[depth] != "!-- --") {
1612 item_name= "listitem";
1613 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1614 if (environment_inner[depth] == "varlistentry")
1615 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1619 if (style->latextype == LATEX_ENVIRONMENT) {
1620 if (!style->latexparam().empty()) {
1621 if (style->latexparam() == "CDATA")
1624 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1629 desc_on = (style->labeltype == LABEL_MANUAL);
1631 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1632 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1634 item_name = desc_on ? "term" : "para";
1635 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1639 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1643 buf->simpleDocBookOnePar(os, p, desc_on, depth + 1 + command_depth);
1647 // write closing SGML tags
1648 switch (style->latextype) {
1649 case LATEX_ENVIRONMENT:
1650 if (!style->latexparam().empty()) {
1651 if (style->latexparam() == "CDATA")
1654 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1657 case LATEX_ITEM_ENVIRONMENT:
1658 if (desc_on == 1) break;
1660 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1662 case LATEX_PARAGRAPH:
1663 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1666 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1672 for (int d = depth; d >= 0; --d) {
1673 if (!environment_stack[depth].empty()) {
1674 if (environment_inner[depth] != "!-- --") {
1675 item_name = "listitem";
1676 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1677 if (environment_inner[depth] == "varlistentry")
1678 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1681 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1689 void InsetText::validate(LaTeXFeatures & features) const
1691 for_each(paragraphs.begin(), paragraphs.end(),
1692 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1696 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1698 if (the_locking_inset) {
1699 the_locking_inset->getCursorPos(bv, x, y);
1702 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1703 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1707 int InsetText::insetInInsetY() const
1709 if (!the_locking_inset)
1712 return (inset_y + the_locking_inset->insetInInsetY());
1716 void InsetText::toggleInsetCursor(BufferView * bv)
1718 if (the_locking_inset) {
1719 the_locking_inset->toggleInsetCursor(bv);
1723 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1725 int const asc = font_metrics::maxAscent(font);
1726 int const desc = font_metrics::maxDescent(font);
1728 if (isCursorVisible())
1729 bv->hideLockedInsetCursor();
1731 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1732 toggleCursorVisible();
1736 void InsetText::showInsetCursor(BufferView * bv, bool show)
1738 if (the_locking_inset) {
1739 the_locking_inset->showInsetCursor(bv, show);
1742 if (!isCursorVisible()) {
1743 LyXFont const font =
1744 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1746 int const asc = font_metrics::maxAscent(font);
1747 int const desc = font_metrics::maxDescent(font);
1749 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1751 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1752 setCursorVisible(true);
1757 void InsetText::hideInsetCursor(BufferView * bv)
1759 if (isCursorVisible()) {
1760 bv->hideLockedInsetCursor();
1761 setCursorVisible(false);
1763 if (the_locking_inset)
1764 the_locking_inset->hideInsetCursor(bv);
1768 void InsetText::fitInsetCursor(BufferView * bv) const
1770 if (the_locking_inset) {
1771 the_locking_inset->fitInsetCursor(bv);
1774 LyXFont const font =
1775 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1777 int const asc = font_metrics::maxAscent(font);
1778 int const desc = font_metrics::maxDescent(font);
1780 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1781 need_update |= FULL;
1786 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1788 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1789 return moveLeftIntern(bv, false, activate_inset, selecting);
1791 return moveRightIntern(bv, true, activate_inset, selecting);
1796 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1798 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1799 return moveRightIntern(bv, true, activate_inset, selecting);
1801 return moveLeftIntern(bv, false, activate_inset, selecting);
1806 InsetText::moveRightIntern(BufferView * bv, bool front,
1807 bool activate_inset, bool selecting)
1809 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1810 return FINISHED_RIGHT;
1811 if (activate_inset && checkAndActivateInset(bv, front))
1813 getLyXText(bv)->cursorRight(bv);
1815 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1816 return DISPATCHED_NOUPDATE;
1821 InsetText::moveLeftIntern(BufferView * bv, bool front,
1822 bool activate_inset, bool selecting)
1824 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1826 getLyXText(bv)->cursorLeft(bv);
1828 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1829 if (activate_inset && checkAndActivateInset(bv, front))
1831 return DISPATCHED_NOUPDATE;
1835 Inset::RESULT InsetText::moveUp(BufferView * bv)
1837 if (!crow(bv)->previous())
1839 getLyXText(bv)->cursorUp(bv);
1840 return DISPATCHED_NOUPDATE;
1844 Inset::RESULT InsetText::moveDown(BufferView * bv)
1846 if (!crow(bv)->next())
1847 return FINISHED_DOWN;
1848 getLyXText(bv)->cursorDown(bv);
1849 return DISPATCHED_NOUPDATE;
1853 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1855 if (the_locking_inset) {
1856 if (the_locking_inset->insetAllowed(inset))
1857 return the_locking_inset->insertInset(bv, inset);
1860 inset->setOwner(this);
1861 hideInsetCursor(bv);
1862 getLyXText(bv)->insertInset(inset);
1864 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1869 bool InsetText::insetAllowed(Inset::Code code) const
1871 // in_insetAllowed is a really gross hack,
1872 // to allow us to call the owner's insetAllowed
1873 // without stack overflow, which can happen
1874 // when the owner uses InsetCollapsable::insetAllowed()
1876 if (in_insetAllowed)
1878 in_insetAllowed = true;
1879 if (the_locking_inset)
1880 ret = the_locking_inset->insetAllowed(code);
1882 ret = owner()->insetAllowed(code);
1883 in_insetAllowed = false;
1888 UpdatableInset * InsetText::getLockingInset() const
1890 return the_locking_inset ? the_locking_inset->getLockingInset() :
1891 const_cast<InsetText *>(this);
1895 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1899 if (the_locking_inset)
1900 return the_locking_inset->getFirstLockingInsetOfType(c);
1905 bool InsetText::showInsetDialog(BufferView * bv) const
1907 if (the_locking_inset)
1908 return the_locking_inset->showInsetDialog(bv);
1913 vector<string> const InsetText::getLabelList() const
1915 vector<string> label_list;
1917 ParagraphList::iterator pit = paragraphs.begin();
1918 ParagraphList::iterator pend = paragraphs.end();
1919 for (; pit != pend; ++pit) {
1920 InsetList::iterator beg = pit->insetlist.begin();
1921 InsetList::iterator end = pit->insetlist.end();
1922 for (; beg != end; ++beg) {
1923 vector<string> const l = beg.getInset()->getLabelList();
1924 label_list.insert(label_list.end(), l.begin(), l.end());
1931 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1934 if (the_locking_inset) {
1935 the_locking_inset->setFont(bv, font, toggleall, selectall);
1938 if ((!paragraphs.begin()->next() && paragraphs.begin()->empty()) || cpar(bv)->empty()) {
1939 getLyXText(bv)->setFont(font, toggleall);
1944 lt = getLyXText(bv);
1947 if (lt->selection.set()) {
1948 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1952 lt->toggleFree(font, toggleall);
1954 lt->clearSelection();
1956 bool flag = (selectall || lt->selection.set());
1960 updateLocal(bv, FULL, true);
1962 updateLocal(bv, CURSOR_PAR, true);
1966 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1968 if (cpar(bv)->isInset(cpos(bv))) {
1970 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1971 if (!isHighlyEditableInset(inset))
1973 inset->edit(bv, front);
1974 if (!the_locking_inset)
1976 updateLocal(bv, CURSOR, false);
1983 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1984 mouse_button::state button)
1986 x -= drawTextXOffset;
1988 int dummyy = y + insetAscent;
1989 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1990 // we only do the edit() call if the inset was hit by the mouse
1991 // or if it is a highly editable inset. So we should call this
1992 // function from our own edit with button < 0.
1993 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1995 if (button == mouse_button::none && !isHighlyEditableInset(inset))
2003 inset_x = cix(bv) - top_x + drawTextXOffset;
2004 inset_y = ciy(bv) + drawTextYOffset;
2005 inset->edit(bv, x - inset_x, y - inset_y, button);
2006 if (!the_locking_inset)
2008 updateLocal(bv, CURSOR, false);
2015 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2018 int w = UpdatableInset::getMaxWidth(bv, inset);
2023 w = w - top_x + owner()->x();
2026 w -= (2 * TEXT_TO_INSET_OFFSET);
2029 return UpdatableInset::getMaxWidth(bv, inset);
2034 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2036 // we have to unlock any locked inset otherwise we're in troubles
2037 the_locking_inset = 0;
2040 paragraphs.set(new Paragraph(*p, same_id));
2041 paragraphs.begin()->setInsetOwner(this);
2042 Paragraph * np = &*(paragraphs.begin());
2045 np->next(new Paragraph(*p, same_id));
2046 np->next()->previous(np);
2048 np->setInsetOwner(this);
2055 void InsetText::markNew(bool track_changes)
2057 ParagraphList::iterator pit = paragraphs.begin();
2058 ParagraphList::iterator pend = paragraphs.end();
2059 for (; pit != pend; ++pit) {
2060 if (track_changes) {
2061 pit->trackChanges();
2063 // no-op when not tracking
2064 pit->cleanChanges();
2070 void InsetText::setText(string const & data, LyXFont const & font)
2073 for (unsigned int i = 0; i < data.length(); ++i)
2074 paragraphs.begin()->insertChar(i, data[i], font);
2079 void InsetText::setAutoBreakRows(bool flag)
2081 if (flag != autoBreakRows) {
2082 autoBreakRows = flag;
2090 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2092 if (how != drawFrame_) {
2095 updateLocal(bv, DRAW_FRAME, false);
2100 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2102 if (frame_color != col) {
2105 updateLocal(bv, DRAW_FRAME, false);
2110 int InsetText::cx(BufferView * bv) const
2112 // we do nothing dangerous so we use a local cache
2113 LyXText * llt = getLyXText(bv);
2114 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2115 if (the_locking_inset) {
2116 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2118 if (font.isVisibleRightToLeft())
2119 x -= the_locking_inset->width(bv, font);
2125 int InsetText::cix(BufferView * bv) const
2127 // we do nothing dangerous so we use a local cache
2128 LyXText * llt = getLyXText(bv);
2129 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2130 if (the_locking_inset) {
2131 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2133 if (font.isVisibleRightToLeft())
2134 x -= the_locking_inset->width(bv, font);
2140 int InsetText::cy(BufferView * bv) const
2143 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2147 int InsetText::ciy(BufferView * bv) const
2150 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2154 pos_type InsetText::cpos(BufferView * bv) const
2156 return getLyXText(bv)->cursor.pos();
2160 Paragraph * InsetText::cpar(BufferView * bv) const
2162 return getLyXText(bv)->cursor.par();
2166 bool InsetText::cboundary(BufferView * bv) const
2168 return getLyXText(bv)->cursor.boundary();
2172 Row * InsetText::crow(BufferView * bv) const
2174 return getLyXText(bv)->cursor.row();
2178 LyXText * InsetText::getLyXText(BufferView const * lbv,
2179 bool const recursive) const
2181 if (cached_bview == lbv) {
2182 if (recursive && the_locking_inset)
2183 return the_locking_inset->getLyXText(lbv, true);
2184 LyXText * lt = cached_text.get();
2185 lyx::Assert(lt && lt->firstRow()->par() == &*(paragraphs.begin()));
2188 // Super UGLY! (Lgb)
2189 BufferView * bv = const_cast<BufferView *>(lbv);
2192 Cache::iterator it = cache.find(bv);
2194 if (it != cache.end()) {
2197 } else if (do_resize) {
2198 resizeLyXText(do_resize);
2200 if (lt || !it->second.remove) {
2201 lyx::Assert(it->second.text.get());
2202 cached_text = it->second.text;
2203 if (recursive && the_locking_inset) {
2204 return the_locking_inset->getLyXText(bv, true);
2206 return cached_text.get();
2207 } else if (it->second.remove) {
2209 saveLyXTextState(it->second.text.get());
2215 // when we have to reinit the existing LyXText!
2217 it->second.text->init(bv);
2218 restoreLyXTextState(it->second.text.get());
2219 it->second.remove = false;
2221 cached_text = it->second.text;
2222 if (the_locking_inset && recursive) {
2223 return the_locking_inset->getLyXText(bv);
2225 return cached_text.get();
2228 // we are here only if we don't have a BufferView * in the cache!!!
2230 cached_text.reset(new LyXText(bv, const_cast<InsetText *>(this)));
2231 cached_text->init(bv);
2232 restoreLyXTextState(cached_text.get());
2234 cache.insert(make_pair(bv, cached_text));
2236 if (the_locking_inset && recursive) {
2237 return the_locking_inset->getLyXText(bv);
2239 return cached_text.get();
2243 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2247 Cache::iterator it = cache.find(bv);
2249 if (it == cache.end()) {
2253 lyx::Assert(it->second.text.get());
2255 it->second.remove = true;
2257 /// then remove all LyXText in text-insets
2258 for_each(paragraphs.begin(), paragraphs.end(),
2259 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2264 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2267 // we cannot resize this because we are in use!
2268 // so do this on the next possible getLyXText()
2273 // lyxerr << "InsetText::resizeLyXText\n";
2274 if (!paragraphs.begin()->next() && paragraphs.begin()->empty()) { // no data, resize not neccessary!
2275 // we have to do this as a fixed width may have changed!
2276 LyXText * t = getLyXText(bv);
2277 saveLyXTextState(t);
2279 restoreLyXTextState(t);
2282 // one endless line, resize normally not necessary
2283 if (!force && getMaxWidth(bv, this) < 0)
2286 Cache::iterator it = cache.find(bv);
2287 if (it == cache.end()) {
2290 lyx::Assert(it->second.text.get());
2292 LyXText * t = it->second.text.get();
2293 saveLyXTextState(t);
2295 for_each(paragraphs.begin(), paragraphs.end(),
2296 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2299 restoreLyXTextState(t);
2300 if (the_locking_inset) {
2301 inset_x = cix(bv) - top_x + drawTextXOffset;
2302 inset_y = ciy(bv) + drawTextYOffset;
2305 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2307 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2308 // this will scroll the screen such that the cursor becomes visible
2309 bv->updateScrollbar();
2311 need_update |= FULL;
2316 void InsetText::reinitLyXText() const
2319 // we cannot resize this because we are in use!
2320 // so do this on the next possible getLyXText()
2326 // lyxerr << "InsetText::reinitLyXText\n";
2327 for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2328 lyx::Assert(it->second.text.get());
2330 LyXText * t = it->second.text.get();
2331 BufferView * bv = it->first;
2333 saveLyXTextState(t);
2335 for_each(paragraphs.begin(), paragraphs.end(),
2336 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2339 restoreLyXTextState(t);
2340 if (the_locking_inset) {
2341 inset_x = cix(bv) - top_x + drawTextXOffset;
2342 inset_y = ciy(bv) + drawTextYOffset;
2344 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2346 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2347 // this will scroll the screen such that the cursor becomes visible
2348 bv->updateScrollbar();
2356 void InsetText::removeNewlines()
2358 bool changed = false;
2360 ParagraphList::iterator it = paragraphs.begin();
2361 ParagraphList::iterator end = paragraphs.end();
2362 for (; it != end; ++it) {
2363 for (int i = 0; i < it->size(); ++i) {
2364 if (it->isNewline(i)) {
2375 bool InsetText::nodraw() const
2377 if (the_locking_inset)
2378 return the_locking_inset->nodraw();
2379 return UpdatableInset::nodraw();
2383 int InsetText::scroll(bool recursive) const
2385 int sx = UpdatableInset::scroll(false);
2387 if (recursive && the_locking_inset)
2388 sx += the_locking_inset->scroll(recursive);
2394 void InsetText::selectAll(BufferView * bv)
2396 getLyXText(bv)->cursorTop();
2397 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2398 getLyXText(bv)->cursorBottom();
2399 getLyXText(bv)->setSelection();
2403 void InsetText::clearSelection(BufferView * bv)
2405 getLyXText(bv)->clearSelection();
2409 void InsetText::clearInset(BufferView * bv, int baseline) const
2411 Painter & pain = bv->painter();
2413 int h = insetAscent + insetDescent;
2414 int ty = baseline - insetAscent;
2420 if ((ty + h) > pain.paperHeight())
2421 h = pain.paperHeight();
2422 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2423 w = pain.paperWidth();
2424 // w -= TEXT_TO_INSET_OFFSET;
2425 pain.fillRectangle(top_x + 1, ty + 1, w - 1, h - 1, backgroundColor());
2430 Paragraph * InsetText::firstParagraph() const
2433 if (the_locking_inset)
2434 if ((result = the_locking_inset->firstParagraph()))
2436 return &*(paragraphs.begin());
2440 Paragraph * InsetText::getFirstParagraph(int i) const
2442 return (i == 0) ? &*(paragraphs.begin()) : 0;
2446 LyXCursor const & InsetText::cursor(BufferView * bv) const
2448 if (the_locking_inset)
2449 return the_locking_inset->cursor(bv);
2450 return getLyXText(bv)->cursor;
2454 Paragraph * InsetText::paragraph() const
2456 return &*(paragraphs.begin());
2460 void InsetText::paragraph(Paragraph * p)
2462 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2463 // caller of this function has to take care of it. This IS important
2464 // as we could have to insert a paragraph before this one and just
2465 // link the actual to a new ones next and set it with this function
2468 // set ourself as owner for all the paragraphs inserted!
2469 for_each(paragraphs.begin(), paragraphs.end(),
2470 boost::bind(&Paragraph::setInsetOwner, _1, this));
2473 // redraw myself when asked for
2478 Inset * InsetText::getInsetFromID(int id_arg) const
2481 return const_cast<InsetText *>(this);
2483 ParagraphList::iterator pit = paragraphs.begin();
2484 ParagraphList::iterator pend = paragraphs.end();
2485 for (; pit != pend; ++pit) {
2486 InsetList::iterator it = pit->insetlist.begin();
2487 InsetList::iterator end = pit->insetlist.end();
2488 for (; it != end; ++it) {
2489 if (it.getInset()->id() == id_arg)
2490 return it.getInset();
2491 Inset * in = it.getInset()->getInsetFromID(id_arg);
2501 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2502 float & value) const
2508 lt = getLyXText(bv);
2511 if (the_locking_inset) {
2512 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2513 if (!word.word().empty()) {
2519 // we have to go on checking so move cursor to the next char
2520 lt->cursor.pos(lt->cursor.pos() + 1);
2522 word = lt->selectNextWordToSpellcheck(value);
2523 if (word.word().empty())
2524 bv->unlockInset(const_cast<InsetText *>(this));
2533 void InsetText::selectSelectedWord(BufferView * bv)
2535 if (the_locking_inset) {
2536 the_locking_inset->selectSelectedWord(bv);
2539 getLyXText(bv)->selectSelectedWord();
2540 updateLocal(bv, SELECTION, false);
2544 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2546 if (the_locking_inset) {
2547 the_locking_inset->toggleSelection(bv, kill_selection);
2551 lt = getLyXText(bv);
2555 int x = top_x + TEXT_TO_INSET_OFFSET;
2557 Row * row = lt->firstRow();
2558 int y_offset = top_baseline - row->ascent_of_text();
2560 while ((row != 0) && ((y+row->height()) <= 0)) {
2567 if (need_update & SELECTION)
2569 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2575 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2579 lt = getLyXText(bv);
2582 if (the_locking_inset) {
2583 if (the_locking_inset->nextChange(bv, length))
2585 lt->cursorRight(true);
2587 lyxfind::SearchResult result =
2588 lyxfind::findNextChange(bv, lt, length);
2590 if (result == lyxfind::SR_FOUND) {
2591 LyXCursor cur = lt->cursor;
2592 bv->unlockInset(bv->theLockingInset());
2593 if (bv->lockInset(this))
2596 lt->setSelectionRange(length);
2597 updateLocal(bv, SELECTION, false);
2601 return result != lyxfind::SR_NOT_FOUND;
2605 bool InsetText::searchForward(BufferView * bv, string const & str,
2610 lt = getLyXText(bv);
2613 if (the_locking_inset) {
2614 if (the_locking_inset->searchForward(bv, str, cs, mw))
2616 lt->cursorRight(true);
2618 lyxfind::SearchResult result =
2619 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2621 if (result == lyxfind::SR_FOUND) {
2622 LyXCursor cur = lt->cursor;
2623 bv->unlockInset(bv->theLockingInset());
2624 if (bv->lockInset(this))
2627 lt->setSelectionRange(str.length());
2628 updateLocal(bv, SELECTION, false);
2632 return (result != lyxfind::SR_NOT_FOUND);
2635 bool InsetText::searchBackward(BufferView * bv, string const & str,
2638 if (the_locking_inset) {
2639 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2644 lt = getLyXText(bv);
2648 Paragraph * p = &*(paragraphs.begin());
2651 lt->setCursor(p, p->size());
2653 lyxfind::SearchResult result =
2654 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2656 if (result == lyxfind::SR_FOUND) {
2657 LyXCursor cur = lt->cursor;
2658 bv->unlockInset(bv->theLockingInset());
2659 if (bv->lockInset(this))
2662 lt->setSelectionRange(str.length());
2663 updateLocal(bv, SELECTION, false);
2667 return (result != lyxfind::SR_NOT_FOUND);
2671 bool InsetText::checkInsertChar(LyXFont & font)
2674 return owner()->checkInsertChar(font);
2679 void InsetText::collapseParagraphs(BufferView * bv)
2681 LyXText * llt = getLyXText(bv);
2683 while (paragraphs.begin()->next()) {
2684 if (!paragraphs.begin()->empty() && !paragraphs.begin()->next()->empty() &&
2685 !paragraphs.begin()->isSeparator(paragraphs.begin()->size() - 1))
2687 paragraphs.begin()->insertChar(paragraphs.begin()->size(), ' ');
2689 if (llt->selection.set()) {
2690 if (llt->selection.start.par() == paragraphs.begin()->next()) {
2691 llt->selection.start.par(&*(paragraphs.begin()));
2692 llt->selection.start.pos(
2693 llt->selection.start.pos() + paragraphs.begin()->size());
2695 if (llt->selection.end.par() == paragraphs.begin()->next()) {
2696 llt->selection.end.par(&*(paragraphs.begin()));
2697 llt->selection.end.pos(
2698 llt->selection.end.pos() + paragraphs.begin()->size());
2701 mergeParagraph(bv->buffer()->params, paragraphs, paragraphs.begin());
2707 void InsetText::getDrawFont(LyXFont & font) const
2711 owner()->getDrawFont(font);
2715 void InsetText::appendParagraphs(Buffer * buffer,
2718 BufferParams const & bparams = buffer->params;
2720 Paragraph * tmpbuf = newpar;
2721 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2722 if (bparams.tracking_changes)
2723 buf->cleanChanges();
2725 while (tmpbuf->next()) {
2726 tmpbuf = tmpbuf->next();
2727 lastbuffer->next(new Paragraph(*tmpbuf, false));
2728 lastbuffer->next()->previous(lastbuffer);
2729 lastbuffer = lastbuffer->next();
2730 if (bparams.tracking_changes)
2731 lastbuffer->cleanChanges();
2733 lastbuffer = &*(paragraphs.begin());
2734 while (lastbuffer->next())
2735 lastbuffer = lastbuffer->next();
2736 if (!newpar->empty() && !lastbuffer->empty() &&
2737 !lastbuffer->isSeparator(lastbuffer->size() - 1))
2739 lastbuffer->insertChar(lastbuffer->size(), ' ');
2742 // make the buf exactly the same layout than our last paragraph
2743 buf->makeSameLayout(lastbuffer);
2746 lastbuffer->next(buf);
2747 buf->previous(lastbuffer);
2748 mergeParagraph(buffer->params, paragraphs, lastbuffer);
2754 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2756 Paragraph * par = getFirstParagraph(0);
2758 InsetList::iterator it = par->insetlist.begin();
2759 InsetList::iterator end = par->insetlist.end();
2760 for (; it != end; ++it) {
2761 it.getInset()->addPreview(loader);