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
13 #include "insettext.h"
16 #include "BufferView.h"
17 #include "CutAndPaste.h"
19 #include "dimension.h"
20 #include "funcrequest.h"
23 #include "LaTeXFeatures.h"
27 #include "lyxcursor.h"
33 #include "paragraph.h"
34 #include "ParagraphParameters.h"
35 #include "trans_mgr.h"
36 #include "undo_funcs.h"
37 #include "WordLangTuple.h"
38 #include "paragraph_funcs.h"
40 #include "rowpainter.h"
41 #include "insetnewline.h"
44 #include "frontends/Alert.h"
45 #include "frontends/Dialogs.h"
46 #include "frontends/font_metrics.h"
47 #include "frontends/LyXView.h"
48 #include "frontends/Painter.h"
49 #include "frontends/screen.h"
51 #include "support/textutils.h"
52 #include "support/LAssert.h"
53 #include "support/lstrings.h"
54 #include "support/lyxalgo.h" // lyx::count
56 #include <boost/bind.hpp>
74 using lyx::textclass_type;
77 // These functions should probably go into bufferview_funcs somehow (Jug)
79 void InsetText::saveLyXTextState(LyXText * t) const
81 // check if my paragraphs are still valid
82 ParagraphList::iterator it = paragraphs.begin();
83 ParagraphList::iterator end = paragraphs.end();
84 for (; it != end; ++it) {
85 if (&*it == t->cursor.par())
89 if (it != end && t->cursor.pos() <= it->size()) {
90 sstate.lpar = &*t->cursor.par();
91 sstate.pos = t->cursor.pos();
92 sstate.boundary = t->cursor.boundary();
93 sstate.selstartpar = &*t->selection.start.par();
94 sstate.selstartpos = t->selection.start.pos();
95 sstate.selstartboundary = t->selection.start.boundary();
96 sstate.selendpar = &*t->selection.end.par();
97 sstate.selendpos = t->selection.end.pos();
98 sstate.selendboundary = t->selection.end.boundary();
99 sstate.selection = t->selection.set();
100 sstate.mark_set = t->selection.mark();
107 void InsetText::restoreLyXTextState(LyXText * t) const
112 t->selection.set(true);
113 /* at this point just to avoid the DEPM when setting the cursor */
114 t->selection.mark(sstate.mark_set);
115 if (sstate.selection) {
116 t->setCursor(sstate.selstartpar, sstate.selstartpos,
117 true, sstate.selstartboundary);
118 t->selection.cursor = t->cursor;
119 t->setCursor(sstate.selendpar, sstate.selendpos,
120 true, sstate.selendboundary);
122 t->setCursor(sstate.lpar, sstate.pos);
124 t->setCursor(sstate.lpar, sstate.pos, true, sstate.boundary);
125 t->selection.cursor = t->cursor;
126 t->selection.set(false);
131 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
138 InsetText::InsetText(BufferParams const & bp)
139 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
142 paragraphs.push_back(new Paragraph);
143 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
144 if (bp.tracking_changes)
145 paragraphs.begin()->trackChanges();
150 InsetText::InsetText(InsetText const & in, bool same_id)
151 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
158 InsetText & InsetText::operator=(InsetText const & it)
165 void InsetText::init(InsetText const * ins, bool same_id)
168 setParagraphData(ins->paragraphs, same_id);
169 autoBreakRows = ins->autoBreakRows;
170 drawFrame_ = ins->drawFrame_;
171 frame_color = ins->frame_color;
175 for_each(paragraphs.begin(), paragraphs.end(),
176 boost::bind(&Paragraph::setInsetOwner, _1, this));
178 the_locking_inset = 0;
180 frame_color = LColor::insetframe;
181 autoBreakRows = false;
194 last_drawn_width = -1;
197 in_insetAllowed = false;
201 InsetText::~InsetText()
207 void InsetText::clear(bool just_mark_erased)
209 if (just_mark_erased) {
210 ParagraphList::iterator it = paragraphs.begin();
211 ParagraphList::iterator end = paragraphs.end();
212 for (; it != end; ++it) {
219 // This is a gross hack...
220 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
223 paragraphs.push_back(new Paragraph);
224 paragraphs.begin()->setInsetOwner(this);
225 paragraphs.begin()->layout(old_layout);
232 Inset * InsetText::clone(Buffer const &, bool same_id) const
234 return new InsetText(*this, same_id);
238 void InsetText::write(Buffer const * buf, ostream & os) const
241 writeParagraphData(buf, os);
245 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
247 ParagraphList::iterator it = paragraphs.begin();
248 ParagraphList::iterator end = paragraphs.end();
249 Paragraph::depth_type dth = 0;
250 for (; it != end; ++it) {
251 it->write(buf, os, buf->params, dth);
256 void InsetText::read(Buffer const * buf, LyXLex & lex)
259 Paragraph::depth_type depth = 0;
263 if (buf->params.tracking_changes)
264 paragraphs.begin()->trackChanges();
266 // delete the initial paragraph
268 ParagraphList::iterator pit = paragraphs.begin();
272 token = lex.getString();
275 if (token == "\\end_inset") {
279 if (token == "\\the_end") {
280 lex.printError("\\the_end read in inset! Error in document!");
285 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
288 pit = paragraphs.begin();
289 ParagraphList::iterator const end = paragraphs.end();
290 for (; pit != end; ++pit)
291 pit->setInsetOwner(this);
293 if (token != "\\end_inset") {
294 lex.printError("Missing \\end_inset at this point. "
301 void InsetText::dimension(BufferView * bv, LyXFont const &,
302 Dimension & dim) const
304 LyXText * text = getLyXText(bv);
305 dim.a = text->rows().begin()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
306 dim.d = text->height - dim.a + TEXT_TO_INSET_OFFSET;
307 dim.w = max(textWidth(bv), int(text->width)) + 2 * TEXT_TO_INSET_OFFSET;
308 dim.w = max(dim.w, 10);
312 int InsetText::textWidth(BufferView * bv, bool fordraw) const
314 int w = autoBreakRows ? getMaxWidth(bv, this) : -1;
317 return max(w - 2 * TEXT_TO_INSET_OFFSET,
318 (int)getLyXText(bv)->width);
323 return w - 2 * TEXT_TO_INSET_OFFSET;
327 void InsetText::draw(BufferView * bv, LyXFont const & f,
328 int baseline, float & x) const
333 // update our idea of where we are. Clearly, we should
334 // not have to know this information.
338 int const start_x = int(x);
340 Painter & pain = bv->painter();
342 // call these methods so that insetWidth, insetAscent and
343 // insetDescent have the right values.
348 // repaint the background if needed
349 if (backgroundColor() != LColor::background)
350 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
352 // no draw is necessary !!!
353 if ((drawFrame_ == LOCKED) && !locked && paragraphs.begin()->empty()) {
354 top_baseline = baseline;
361 x += static_cast<float>(scroll());
363 top_baseline = baseline;
364 top_y = baseline - insetAscent;
366 if (last_drawn_width != insetWidth) {
368 last_drawn_width = insetWidth;
371 if (the_locking_inset && (cpar(bv) == inset_par)
372 && (cpos(bv) == inset_pos)) {
373 inset_x = cix(bv) - int(x) + drawTextXOffset;
374 inset_y = ciy(bv) + drawTextYOffset;
382 x += TEXT_TO_INSET_OFFSET;
384 RowList::iterator rowit = lt->rows().begin();
385 RowList::iterator end = lt->rows().end();
387 int y_offset = baseline - rowit->ascent_of_text();
388 int ph = pain.paperHeight();
391 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
392 y += rowit->height();
393 first += rowit->height();
397 lt->top_y(-y_offset);
405 int yf = y_offset + first;
410 while ((rowit != end) && (yf < ph)) {
411 RowPainter rp(*bv, *lt, rowit);
412 rp.paint(y + y_offset + first, int(x), y + lt->top_y());
413 y += rowit->height();
414 yf += rowit->height();
420 if ((drawFrame_ == ALWAYS) || (drawFrame_ == LOCKED && locked)) {
421 drawFrame(pain, int(start_x));
424 x += insetWidth - TEXT_TO_INSET_OFFSET;
426 if (need_update != INIT) {
434 void InsetText::drawFrame(Painter & pain, int x) const
436 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
438 frame_y = top_baseline - insetAscent + ttoD2;
439 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
440 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
441 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
446 void InsetText::update(BufferView * bv, bool reinit)
449 if (reinit && owner()) {
451 owner()->update(bv, true);
457 if (reinit || need_update == INIT) {
459 // we should put this call where we set need_update to INIT!
462 owner()->update(bv, true);
465 int nw = getMaxWidth(bv, this);
466 if (nw > 0 && old_max_width != nw) {
473 if (!autoBreakRows &&
474 boost::next(paragraphs.begin()) != paragraphs.end())
475 collapseParagraphs(bv);
477 if (the_locking_inset) {
478 inset_x = cix(bv) - top_x + drawTextXOffset;
479 inset_y = ciy(bv) + drawTextYOffset;
480 the_locking_inset->update(bv, reinit);
488 if ((need_update & CURSOR_PAR) && (lt->refreshStatus() == LyXText::REFRESH_NONE) &&
490 lt->updateInset(the_locking_inset);
493 if (lt->refreshStatus() == LyXText::REFRESH_AREA)
500 int nw = getMaxWidth(bv, this);
501 if (nw > 0 && old_max_width != nw) {
508 void InsetText::setUpdateStatus(BufferView * bv, int what) const
510 // this does nothing dangerous so use only a localized buffer
511 LyXText * llt = getLyXText(bv);
514 // we have to redraw us full if our LyXText REFRESH_AREA or
515 // if we don't break row so that we only have one row to update!
516 if ((llt->refreshStatus() == LyXText::REFRESH_AREA) ||
518 (llt->refreshStatus() == LyXText::REFRESH_ROW)))
521 } else if (llt->refreshStatus() == LyXText::REFRESH_ROW) {
522 need_update |= CURSOR_PAR;
525 // this to not draw a selection when we redraw all of it!
526 if (need_update & CURSOR && !(need_update & SELECTION)) {
527 if (llt->selection.set())
529 llt->clearSelection();
534 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
537 if (!autoBreakRows &&
538 boost::next(paragraphs.begin()) != paragraphs.end())
539 collapseParagraphs(bv);
541 if (!autoBreakRows && paragraphs.size() > 1)
542 collapseParagraphs(bv);
551 setUpdateStatus(bv, what);
552 bool flag = mark_dirty ||
553 (((need_update != CURSOR) && (need_update != NONE)) ||
554 (lt->refreshStatus() != LyXText::REFRESH_NONE) || lt->selection.set());
555 if (!lt->selection.set())
556 lt->selection.cursor = lt->cursor;
562 bv->updateInset(const_cast<InsetText *>(this));
568 if (need_update == CURSOR)
570 bv->owner()->view_state_changed();
571 bv->owner()->updateMenubar();
572 bv->owner()->updateToolbar();
573 if (old_par != cpar(bv)) {
574 bv->owner()->setLayout(cpar(bv)->layout()->name());
580 string const InsetText::editMessage() const
582 return _("Opened Text Inset");
586 void InsetText::insetUnlock(BufferView * bv)
588 if (the_locking_inset) {
589 the_locking_inset->insetUnlock(bv);
590 the_locking_inset = 0;
591 updateLocal(bv, CURSOR_PAR, false);
602 if (lt->selection.set()) {
603 lt->clearSelection();
605 } else if (owner()) {
606 bv->owner()->setLayout(owner()->getLyXText(bv)
607 ->cursor.par()->layout()->name());
609 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
610 // hack for deleteEmptyParMech
611 if (!paragraphs.begin()->empty()) {
612 lt->setCursor(paragraphs.begin(), 0);
613 } else if (boost::next(paragraphs.begin()) != paragraphs.end()) {
614 lt->setCursor(boost::next(paragraphs.begin()), 0);
619 updateLocal(bv, code, false);
622 setUpdateStatus(bv, code);
627 void InsetText::lockInset(BufferView * bv)
630 the_locking_inset = 0;
631 inset_pos = inset_x = inset_y = 0;
632 inset_boundary = false;
640 lt->setCursor(paragraphs.begin(), 0);
641 lt->clearSelection();
643 // If the inset is empty set the language of the current font to the
644 // language to the surronding text (if different).
645 if (paragraphs.begin()->empty() &&
646 boost::next(paragraphs.begin()) == paragraphs.end() &&
647 bv->getParentLanguage(this) != lt->current_font.language()) {
648 LyXFont font(LyXFont::ALL_IGNORE);
649 font.setLanguage(bv->getParentLanguage(this));
650 setFont(bv, font, false);
655 if (drawFrame_ == LOCKED)
656 code = CURSOR|DRAW_FRAME;
657 setUpdateStatus(bv, code);
661 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
663 the_locking_inset = inset;
664 inset_x = cix(bv) - top_x + drawTextXOffset;
665 inset_y = ciy(bv) + drawTextYOffset;
666 inset_pos = cpos(bv);
667 inset_par = cpar(bv);
668 inset_boundary = cboundary(bv);
669 updateLocal(bv, CURSOR, false);
673 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
675 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
679 if (!the_locking_inset) {
680 ParagraphList::iterator pit = paragraphs.begin();
681 ParagraphList::iterator pend = paragraphs.end();
683 int const id = inset->id();
684 for (; pit != pend; ++pit) {
685 InsetList::iterator it =
686 pit->insetlist.begin();
687 InsetList::iterator const end =
688 pit->insetlist.end();
689 for (; it != end; ++it) {
690 if (it.getInset() == inset) {
691 getLyXText(bv)->setCursorIntern(pit, it.getPos());
692 lockInset(bv, inset);
695 if (it.getInset()->getInsetFromID(id)) {
696 getLyXText(bv)->setCursorIntern(pit, it.getPos());
697 it.getInset()->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
698 return the_locking_inset->lockInsetInInset(bv, inset);
704 if (inset == cpar(bv)->getInset(cpos(bv))) {
705 lyxerr[Debug::INSETS] << "OK" << endl;
706 lockInset(bv, inset);
708 } else if (the_locking_inset && (the_locking_inset == inset)) {
709 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
710 lyxerr[Debug::INSETS] << "OK" << endl;
711 inset_x = cix(bv) - top_x + drawTextXOffset;
712 inset_y = ciy(bv) + drawTextYOffset;
714 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
716 } else if (the_locking_inset) {
717 lyxerr[Debug::INSETS] << "MAYBE" << endl;
718 return the_locking_inset->lockInsetInInset(bv, inset);
720 lyxerr[Debug::INSETS] << "NOT OK" << endl;
725 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
728 if (!the_locking_inset)
730 if (the_locking_inset == inset) {
731 the_locking_inset->insetUnlock(bv);
732 getLyXText(bv)->updateInset(inset);
733 the_locking_inset = 0;
735 moveRightIntern(bv, true, false);
736 old_par = 0; // force layout setting
740 updateLocal(bv, CURSOR, false);
743 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
747 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
749 if (!autoBreakRows &&
750 boost::next(paragraphs.begin()) != paragraphs.end())
751 collapseParagraphs(bv);
759 if (inset->owner() != this) {
760 int ustat = CURSOR_PAR;
762 UpdatableInset * tl_inset = the_locking_inset;
764 found = tl_inset->updateInsetInInset(bv, inset);
766 tl_inset = static_cast<UpdatableInset *>(inset);
767 while(tl_inset->owner() && tl_inset->owner() != this)
768 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
769 if (!tl_inset->owner())
771 found = tl_inset->updateInsetInInset(bv, inset);
775 lt->updateInset(tl_inset);
779 setUpdateStatus(bv, ustat);
782 bool found = lt->updateInset(inset);
786 setUpdateStatus(bv, CURSOR_PAR);
787 if (the_locking_inset &&
788 cpar(bv) == inset_par && cpos(bv) == inset_pos)
790 inset_x = cix(bv) - top_x + drawTextXOffset;
791 inset_y = ciy(bv) + drawTextYOffset;
798 void InsetText::lfunMousePress(FuncRequest const & cmd)
802 // use this to check mouse motion for selection!
806 BufferView * bv = cmd.view();
807 FuncRequest cmd1 = cmd;
813 int tmp_x = cmd.x - drawTextXOffset;
814 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->top_y();
815 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
817 if (the_locking_inset) {
818 if (the_locking_inset == inset) {
819 the_locking_inset->localDispatch(cmd1);
822 // otherwise only unlock the_locking_inset
823 the_locking_inset->insetUnlock(bv);
824 the_locking_inset = 0;
827 no_selection = false;
829 if (bv->theLockingInset()) {
830 if (isHighlyEditableInset(inset)) {
831 // We just have to lock the inset before calling a
833 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
834 if (!bv->lockInset(uinset)) {
835 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
837 inset->localDispatch(cmd1);
838 if (the_locking_inset)
839 updateLocal(bv, CURSOR, false);
844 bool paste_internally = false;
845 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
846 localDispatch(FuncRequest(bv, LFUN_COPY));
847 paste_internally = true;
854 int old_top_y = lt->top_y();
856 lt->setCursorFromCoordinates(cmd.x - drawTextXOffset,
857 cmd.y + insetAscent);
858 // set the selection cursor!
859 lt->selection.cursor = lt->cursor;
860 lt->cursor.x_fix(lt->cursor.x());
862 if (lt->selection.set()) {
863 lt->clearSelection();
864 updateLocal(bv, FULL, false);
866 lt->clearSelection();
867 updateLocal(bv, CURSOR, false);
873 bv->owner()->setLayout(cpar(bv)->layout()->name());
875 // we moved the view we cannot do mouse selection in this case!
876 if (getLyXText(bv)->top_y() != old_top_y)
879 // Insert primary selection with middle mouse
880 // if there is a local selection in the current buffer,
882 if (cmd.button() == mouse_button::button2) {
883 if (paste_internally)
884 localDispatch(FuncRequest(bv, LFUN_PASTE));
886 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
889 getLyXText(bv)->clearSelection();
894 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
896 BufferView * bv = cmd.view();
897 FuncRequest cmd1 = cmd;
902 if (the_locking_inset)
903 return the_locking_inset->localDispatch(cmd1);
905 int tmp_x = cmd.x - drawTextXOffset;
906 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->top_y();
907 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
910 // This code should probably be removed now. Simple insets
911 // (!highlyEditable) can actually take the localDispatch,
912 // and turn it into edit() if necessary. But we still
913 // need to deal properly with the whole relative vs.
914 // absolute mouse co-ords thing in a realiable, sensible way
916 if (isHighlyEditableInset(inset))
917 ret = inset->localDispatch(cmd1);
919 inset_x = cix(bv) - top_x + drawTextXOffset;
920 inset_y = ciy(bv) + drawTextYOffset;
921 cmd1.x = cmd.x - inset_x;
922 cmd1.y = cmd.x - inset_y;
923 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
927 ret = inset->localDispatch(cmd1);
928 updateLocal(bv, CURSOR_PAR, false);
935 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
937 FuncRequest cmd1 = cmd;
941 if (the_locking_inset) {
942 the_locking_inset->localDispatch(cmd1);
946 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
949 BufferView * bv = cmd.view();
955 LyXCursor cur = lt->cursor;
956 lt->setCursorFromCoordinates
957 (cmd.x - drawTextXOffset, cmd.y + insetAscent);
958 lt->cursor.x_fix(lt->cursor.x());
959 if (cur == lt->cursor) {
965 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
966 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
970 updateLocal(bv, SELECTION, false);
975 Inset::RESULT InsetText::localDispatch(FuncRequest const & cmd)
977 BufferView * bv = cmd.view();
979 if (cmd.action == LFUN_INSET_EDIT) {
980 UpdatableInset::localDispatch(cmd);
982 if (!bv->lockInset(this)) {
983 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
988 the_locking_inset = 0;
989 inset_pos = inset_x = inset_y = 0;
990 inset_boundary = false;
1000 if (cmd.argument.size()) {
1001 if (cmd.argument == "left")
1002 lt->setCursor(paragraphs.begin(), 0);
1004 ParagraphList::iterator it = paragraphs.begin();
1005 ParagraphList::iterator end = paragraphs.end();
1006 while (boost::next(it) != end)
1008 // int const pos = (p->size() ? p->size()-1 : p->size());
1009 lt->setCursor(it, it->size());
1012 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
1013 // we put here -1 and not button as now the button in the
1014 // edit call should not be needed we will fix this in 1.3.x
1015 // cycle hopefully (Jug 20020509)
1016 // FIXME: GUII I've changed this to none: probably WRONG
1017 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
1018 lt->setCursorFromCoordinates(cmd.x - drawTextXOffset,
1019 cmd.y + insetAscent);
1020 lt->cursor.x_fix(lt->cursor.x());
1024 lt->clearSelection();
1027 // If the inset is empty set the language of the current font to the
1028 // language to the surronding text (if different).
1029 if (paragraphs.begin()->empty() &&
1030 boost::next(paragraphs.begin()) == paragraphs.end()&&
1031 bv->getParentLanguage(this) != lt->current_font.language())
1033 LyXFont font(LyXFont::ALL_IGNORE);
1034 font.setLanguage(bv->getParentLanguage(this));
1035 setFont(bv, font, false);
1041 if (drawFrame_ == LOCKED)
1042 code = CURSOR | DRAW_FRAME;
1044 updateLocal(bv, code, false);
1045 // Tell the paragraph dialog that we've entered an insettext.
1046 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1051 switch (cmd.action) {
1052 case LFUN_MOUSE_PRESS:
1053 lfunMousePress(cmd);
1055 case LFUN_MOUSE_MOTION:
1056 lfunMouseMotion(cmd);
1058 case LFUN_MOUSE_RELEASE:
1059 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
1064 bool was_empty = (paragraphs.begin()->empty() &&
1065 boost::next(paragraphs.begin()) == paragraphs.end());
1066 no_selection = false;
1067 RESULT result = UpdatableInset::localDispatch(cmd);
1068 if (result != UNDISPATCHED)
1071 result = DISPATCHED;
1072 if (cmd.action < 0 && cmd.argument.empty())
1075 if (the_locking_inset) {
1076 result = the_locking_inset->localDispatch(cmd);
1077 if (result == DISPATCHED_NOUPDATE)
1079 else if (result == DISPATCHED) {
1080 updateLocal(bv, CURSOR_PAR, false);
1082 } else if (result >= FINISHED) {
1084 case FINISHED_RIGHT:
1085 moveRightIntern(bv, false, false);
1086 result = DISPATCHED;
1089 if ((result = moveUp(bv)) >= FINISHED) {
1090 updateLocal(bv, CURSOR, false);
1091 bv->unlockInset(this);
1095 if ((result = moveDown(bv)) >= FINISHED) {
1096 updateLocal(bv, CURSOR, false);
1097 bv->unlockInset(this);
1101 result = DISPATCHED;
1104 the_locking_inset = 0;
1105 updateLocal(bv, CURSOR, false);
1106 // make sure status gets reset immediately
1107 bv->owner()->clearMessage();
1113 lt = getLyXText(bv);
1117 int updflag = false;
1119 // what type of update to do on a cursor movement
1120 int cursor_update = CURSOR;
1122 if (lt->selection.set())
1123 cursor_update = SELECTION;
1125 switch (cmd.action) {
1128 case LFUN_SELFINSERT:
1129 if (bv->buffer()->isReadonly()) {
1130 // setErrorMessage(N_("Document is read only"));
1133 if (!cmd.argument.empty()) {
1134 /* Automatically delete the currently selected
1135 * text and replace it with what is being
1136 * typed in now. Depends on lyxrc settings
1137 * "auto_region_delete", which defaults to
1140 // This should not be needed here and is also WRONG!
1141 setUndo(bv, Undo::INSERT,
1142 lt->cursor.par(), boost::next(lt->cursor.par()));
1145 if (lyxrc.auto_region_delete) {
1146 if (lt->selection.set()) {
1147 lt->cutSelection(false, false);
1150 lt->clearSelection();
1151 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
1152 bv->owner()->getIntl().getTransManager().
1153 TranslateAndInsert(cmd.argument[i], lt);
1156 lt->selection.cursor = lt->cursor;
1157 updwhat = CURSOR | CURSOR_PAR;
1159 result = DISPATCHED_NOUPDATE;
1162 // cursor movements that need special handling
1165 result = moveRight(bv);
1167 updwhat = cursor_update;
1171 result = moveLeft(bv);
1172 updwhat = cursor_update;
1176 result = moveDown(bv);
1177 updwhat = cursor_update;
1181 result = moveUp(bv);
1182 updwhat = cursor_update;
1186 if (crow(bv) == lt->rows().begin())
1187 result = FINISHED_UP;
1189 lt->cursorPrevious();
1190 lt->clearSelection();
1191 result = DISPATCHED_NOUPDATE;
1193 updwhat = cursor_update;
1197 if (boost::next(crow(bv)) == lt->rows().end())
1198 result = FINISHED_DOWN;
1201 lt->clearSelection();
1202 result = DISPATCHED_NOUPDATE;
1204 updwhat = cursor_update;
1207 case LFUN_BACKSPACE: {
1208 if (lt->selection.set())
1209 lt->cutSelection(true, false);
1212 updwhat = CURSOR_PAR;
1218 if (lt->selection.set()) {
1219 lt->cutSelection(true, false);
1223 updwhat = CURSOR_PAR;
1229 lt->cutSelection(bv);
1230 updwhat = CURSOR_PAR;
1237 lt->copySelection();
1238 updwhat = CURSOR_PAR;
1241 case LFUN_PASTESELECTION:
1243 string const clip(bv->getClipboard());
1247 if (cmd.argument == "paragraph") {
1248 lt->insertStringAsParagraphs(clip);
1250 lt->insertStringAsLines(clip);
1253 lt->clearSelection();
1255 updwhat = CURSOR_PAR;
1261 if (!autoBreakRows) {
1262 if (CutAndPaste::nrOfParagraphs() > 1) {
1263 #ifdef WITH_WARNINGS
1264 #warning FIXME horrendously bad UI
1266 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1271 lt->pasteSelection();
1273 lt->clearSelection();
1274 updwhat = CURSOR_PAR;
1279 case LFUN_BREAKPARAGRAPH:
1280 if (!autoBreakRows) {
1281 result = DISPATCHED;
1284 lt->breakParagraph(paragraphs, 0);
1285 updwhat = CURSOR | FULL;
1289 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1290 if (!autoBreakRows) {
1291 result = DISPATCHED;
1294 lt->breakParagraph(paragraphs, 1);
1295 updwhat = CURSOR | FULL;
1299 case LFUN_BREAKLINE: {
1300 if (!autoBreakRows) {
1301 result = DISPATCHED;
1305 lt->insertInset(new InsetNewline);
1306 updwhat = CURSOR | CURSOR_PAR;
1312 // do not set layouts on non breakable textinsets
1313 if (autoBreakRows) {
1314 string cur_layout = cpar(bv)->layout()->name();
1316 // Derive layout number from given argument (string)
1317 // and current buffer's textclass (number). */
1318 LyXTextClass const & tclass =
1319 bv->buffer()->params.getLyXTextClass();
1320 string layout = cmd.argument;
1321 bool hasLayout = tclass.hasLayout(layout);
1323 // If the entry is obsolete, use the new one instead.
1325 string const & obs =
1326 tclass[layout]->obsoleted_by();
1331 // see if we found the layout number:
1333 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1334 bv->owner()->dispatch(lf);
1338 if (cur_layout != layout) {
1339 cur_layout = layout;
1340 lt->setLayout(layout);
1341 bv->owner()->setLayout(cpar(bv)->layout()->name());
1342 updwhat = CURSOR_PAR;
1346 // reset the layout box
1347 bv->owner()->setLayout(cpar(bv)->layout()->name());
1350 case LFUN_PARAGRAPH_SPACING:
1351 // This one is absolutely not working. When fiddling with this
1352 // it also seems to me that the paragraphs inside the insettext
1353 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1354 // FIXME: how old is this comment ? ...
1356 ParagraphList::iterator pit = lt->cursor.par();
1357 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1358 float cur_value = 1.0;
1359 if (cur_spacing == Spacing::Other) {
1360 cur_value = pit->params().spacing().getValue();
1363 istringstream istr(STRCONV(cmd.argument));
1366 Spacing::Space new_spacing = cur_spacing;
1367 float new_value = cur_value;
1369 lyxerr << "Missing argument to `paragraph-spacing'"
1371 } else if (tmp == "single") {
1372 new_spacing = Spacing::Single;
1373 } else if (tmp == "onehalf") {
1374 new_spacing = Spacing::Onehalf;
1375 } else if (tmp == "double") {
1376 new_spacing = Spacing::Double;
1377 } else if (tmp == "other") {
1378 new_spacing = Spacing::Other;
1381 lyxerr << "new_value = " << tmpval << endl;
1384 } else if (tmp == "default") {
1385 new_spacing = Spacing::Default;
1387 lyxerr << _("Unknown spacing argument: ")
1388 << cmd.argument << endl;
1390 if (cur_spacing != new_spacing || cur_value != new_value) {
1391 pit->params().spacing(Spacing(new_spacing, new_value));
1392 updwhat = CURSOR_PAR;
1398 // These need to do repaints but don't require
1399 // special handling otherwise. A *lot* of the
1400 // above could probably be done similarly ...
1405 case LFUN_WORDRIGHT:
1406 // these two are really unhandled ...
1408 case LFUN_BEGINNINGBUF:
1409 updwhat = cursor_update;
1410 if (!bv->dispatch(cmd))
1411 result = UNDISPATCHED;
1420 case LFUN_WORDLEFTSEL:
1421 case LFUN_WORDRIGHTSEL:
1422 updwhat = SELECTION;
1427 if (!bv->dispatch(cmd))
1428 result = UNDISPATCHED;
1435 updateLocal(bv, updwhat, updflag);
1436 /// If the action has deleted all text in the inset, we need to change the
1437 // language to the language of the surronding text.
1438 if (!was_empty && paragraphs.begin()->empty() &&
1439 boost::next(paragraphs.begin()) == paragraphs.end()) {
1440 LyXFont font(LyXFont::ALL_IGNORE);
1441 font.setLanguage(bv->getParentLanguage(this));
1442 setFont(bv, font, false);
1445 if (result >= FINISHED)
1446 bv->unlockInset(this);
1448 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1449 result = DISPATCHED;
1454 int InsetText::latex(Buffer const * buf, ostream & os, bool fragile, bool) const
1457 latexParagraphs(buf, paragraphs, os, texrow, fragile);
1458 return texrow.rows();
1462 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1464 unsigned int lines = 0;
1466 ParagraphList::iterator beg = paragraphs.begin();
1467 ParagraphList::iterator end = paragraphs.end();
1468 ParagraphList::iterator it = beg;
1469 for (; it != end; ++it) {
1470 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1471 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1478 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1480 unsigned int lines = 0;
1482 vector<string> environment_stack(10);
1483 vector<string> environment_inner(10);
1485 int const command_depth = 0;
1488 Paragraph::depth_type depth = 0; // paragraph depth
1490 ParagraphList::iterator pit = paragraphs.begin();
1491 ParagraphList::iterator pend = paragraphs.end();
1493 for (; pit != pend; ++pit) {
1495 int desc_on = 0; // description mode
1497 LyXLayout_ptr const & style = pit->layout();
1499 // environment tag closing
1500 for (; depth > pit->params().depth(); --depth) {
1501 if (environment_inner[depth] != "!-- --") {
1502 item_name = "listitem";
1503 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1504 if (environment_inner[depth] == "varlistentry")
1505 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1507 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1508 environment_stack[depth].erase();
1509 environment_inner[depth].erase();
1512 if (depth == pit->params().depth()
1513 && environment_stack[depth] != style->latexname()
1514 && !environment_stack[depth].empty()) {
1515 if (environment_inner[depth] != "!-- --") {
1516 item_name= "listitem";
1517 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1518 if (environment_inner[depth] == "varlistentry")
1519 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1522 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1524 environment_stack[depth].erase();
1525 environment_inner[depth].erase();
1528 // Write opening SGML tags.
1529 switch (style->latextype) {
1530 case LATEX_PARAGRAPH:
1531 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1535 buf->sgmlError(&*pit, 0, _("Error: LatexType Command not allowed here.\n"));
1539 case LATEX_ENVIRONMENT:
1540 case LATEX_ITEM_ENVIRONMENT:
1541 if (depth < pit->params().depth()) {
1542 depth = pit->params().depth();
1543 environment_stack[depth].erase();
1546 if (environment_stack[depth] != style->latexname()) {
1547 if (environment_stack.size() == depth + 1) {
1548 environment_stack.push_back("!-- --");
1549 environment_inner.push_back("!-- --");
1551 environment_stack[depth] = style->latexname();
1552 environment_inner[depth] = "!-- --";
1553 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1555 if (environment_inner[depth] != "!-- --") {
1556 item_name= "listitem";
1557 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1558 if (environment_inner[depth] == "varlistentry")
1559 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1563 if (style->latextype == LATEX_ENVIRONMENT) {
1564 if (!style->latexparam().empty()) {
1565 if (style->latexparam() == "CDATA")
1568 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1573 desc_on = (style->labeltype == LABEL_MANUAL);
1575 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1576 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1578 item_name = desc_on ? "term" : "para";
1579 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1583 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1587 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1590 // write closing SGML tags
1591 switch (style->latextype) {
1592 case LATEX_ENVIRONMENT:
1593 if (!style->latexparam().empty()) {
1594 if (style->latexparam() == "CDATA")
1597 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1600 case LATEX_ITEM_ENVIRONMENT:
1601 if (desc_on == 1) break;
1603 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1605 case LATEX_PARAGRAPH:
1606 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1609 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1615 for (int d = depth; d >= 0; --d) {
1616 if (!environment_stack[depth].empty()) {
1617 if (environment_inner[depth] != "!-- --") {
1618 item_name = "listitem";
1619 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1620 if (environment_inner[depth] == "varlistentry")
1621 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1624 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1632 void InsetText::validate(LaTeXFeatures & features) const
1634 for_each(paragraphs.begin(), paragraphs.end(),
1635 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1639 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1641 if (the_locking_inset) {
1642 the_locking_inset->getCursor(bv, x, y);
1646 y = cy(&bv) + InsetText::y();
1650 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1652 if (the_locking_inset) {
1653 the_locking_inset->getCursorPos(bv, x, y);
1656 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1657 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1661 int InsetText::insetInInsetY() const
1663 if (!the_locking_inset)
1666 return (inset_y + the_locking_inset->insetInInsetY());
1670 void InsetText::fitInsetCursor(BufferView * bv) const
1672 if (the_locking_inset) {
1673 the_locking_inset->fitInsetCursor(bv);
1676 LyXFont const font =
1677 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1679 int const asc = font_metrics::maxAscent(font);
1680 int const desc = font_metrics::maxDescent(font);
1682 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1683 need_update |= FULL;
1688 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1690 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1691 return moveLeftIntern(bv, false, activate_inset, selecting);
1693 return moveRightIntern(bv, true, activate_inset, selecting);
1698 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1700 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1701 return moveRightIntern(bv, true, activate_inset, selecting);
1703 return moveLeftIntern(bv, false, activate_inset, selecting);
1708 InsetText::moveRightIntern(BufferView * bv, bool front,
1709 bool activate_inset, bool selecting)
1711 LyXText * text = getLyXText(bv);
1713 ParagraphList::iterator c_par = cpar(bv);
1715 if (boost::next(c_par) == paragraphs.end() &&
1716 (cpos(bv) >= c_par->size()))
1717 return FINISHED_RIGHT;
1718 if (activate_inset && checkAndActivateInset(bv, front))
1720 text->cursorRight(bv);
1722 text->clearSelection();
1723 return DISPATCHED_NOUPDATE;
1728 InsetText::moveLeftIntern(BufferView * bv, bool front,
1729 bool activate_inset, bool selecting)
1731 LyXText * text = getLyXText(bv);
1733 if (cpar(bv) == paragraphs.begin() && (cpos(bv) <= 0))
1735 text->cursorLeft(bv);
1737 text->clearSelection();
1738 if (activate_inset && checkAndActivateInset(bv, front))
1740 return DISPATCHED_NOUPDATE;
1744 Inset::RESULT InsetText::moveUp(BufferView * bv)
1746 if (crow(bv) == getLyXText(bv)->rows().begin())
1748 getLyXText(bv)->cursorUp(bv);
1749 getLyXText(bv)->clearSelection();
1750 return DISPATCHED_NOUPDATE;
1754 Inset::RESULT InsetText::moveDown(BufferView * bv)
1756 if (boost::next(crow(bv)) == getLyXText(bv)->rows().end())
1757 return FINISHED_DOWN;
1758 getLyXText(bv)->cursorDown(bv);
1759 getLyXText(bv)->clearSelection();
1760 return DISPATCHED_NOUPDATE;
1764 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1766 if (the_locking_inset) {
1767 if (the_locking_inset->insetAllowed(inset))
1768 return the_locking_inset->insertInset(bv, inset);
1771 inset->setOwner(this);
1772 getLyXText(bv)->insertInset(inset);
1774 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1779 bool InsetText::insetAllowed(Inset::Code code) const
1781 // in_insetAllowed is a really gross hack,
1782 // to allow us to call the owner's insetAllowed
1783 // without stack overflow, which can happen
1784 // when the owner uses InsetCollapsable::insetAllowed()
1786 if (in_insetAllowed)
1788 in_insetAllowed = true;
1789 if (the_locking_inset)
1790 ret = the_locking_inset->insetAllowed(code);
1792 ret = owner()->insetAllowed(code);
1793 in_insetAllowed = false;
1798 UpdatableInset * InsetText::getLockingInset() const
1800 return the_locking_inset ? the_locking_inset->getLockingInset() :
1801 const_cast<InsetText *>(this);
1805 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1809 if (the_locking_inset)
1810 return the_locking_inset->getFirstLockingInsetOfType(c);
1815 bool InsetText::showInsetDialog(BufferView * bv) const
1817 if (the_locking_inset)
1818 return the_locking_inset->showInsetDialog(bv);
1823 vector<string> const InsetText::getLabelList() const
1825 vector<string> label_list;
1827 ParagraphList::iterator pit = paragraphs.begin();
1828 ParagraphList::iterator pend = paragraphs.end();
1829 for (; pit != pend; ++pit) {
1830 InsetList::iterator beg = pit->insetlist.begin();
1831 InsetList::iterator end = pit->insetlist.end();
1832 for (; beg != end; ++beg) {
1833 vector<string> const l = beg.getInset()->getLabelList();
1834 label_list.insert(label_list.end(), l.begin(), l.end());
1841 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1844 if (the_locking_inset) {
1845 the_locking_inset->setFont(bv, font, toggleall, selectall);
1848 if ((boost::next(paragraphs.begin()) == paragraphs.end() &&
1849 paragraphs.begin()->empty()) || cpar(bv)->empty()) {
1850 getLyXText(bv)->setFont(font, toggleall);
1855 lt = getLyXText(bv);
1858 if (lt->selection.set()) {
1859 setUndo(bv, Undo::EDIT, lt->cursor.par(), boost::next(lt->cursor.par()));
1863 lt->toggleFree(font, toggleall);
1865 lt->clearSelection();
1867 bool flag = (selectall || lt->selection.set());
1871 updateLocal(bv, FULL, true);
1873 updateLocal(bv, CURSOR_PAR, true);
1877 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1879 if (cpar(bv)->isInset(cpos(bv))) {
1881 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1882 if (!isHighlyEditableInset(inset))
1884 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1885 inset->localDispatch(cmd);
1886 if (!the_locking_inset)
1888 updateLocal(bv, CURSOR, false);
1895 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1896 mouse_button::state button)
1898 x -= drawTextXOffset;
1900 int dummyy = y + insetAscent;
1901 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1902 // we only do the edit() call if the inset was hit by the mouse
1903 // or if it is a highly editable inset. So we should call this
1904 // function from our own edit with button < 0.
1905 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1907 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1915 inset_x = cix(bv) - top_x + drawTextXOffset;
1916 inset_y = ciy(bv) + drawTextYOffset;
1917 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1918 inset->localDispatch(cmd);
1919 if (!the_locking_inset)
1921 updateLocal(bv, CURSOR, false);
1928 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1931 int w = UpdatableInset::getMaxWidth(bv, inset);
1936 w = w - top_x + owner()->x();
1939 w -= (2 * TEXT_TO_INSET_OFFSET);
1942 return UpdatableInset::getMaxWidth(bv, inset);
1947 void InsetText::setParagraphData(ParagraphList const & plist, bool same_id)
1949 // we have to unlock any locked inset otherwise we're in troubles
1950 the_locking_inset = 0;
1953 ParagraphList::iterator pit = plist.begin();
1954 ParagraphList::iterator pend = plist.end();
1955 for (; pit != pend; ++pit) {
1956 Paragraph * new_par = new Paragraph(*pit, same_id);
1957 new_par->setInsetOwner(this);
1958 paragraphs.push_back(new_par);
1966 void InsetText::markNew(bool track_changes)
1968 ParagraphList::iterator pit = paragraphs.begin();
1969 ParagraphList::iterator pend = paragraphs.end();
1970 for (; pit != pend; ++pit) {
1971 if (track_changes) {
1972 pit->trackChanges();
1974 // no-op when not tracking
1975 pit->cleanChanges();
1981 void InsetText::setText(string const & data, LyXFont const & font)
1984 for (unsigned int i = 0; i < data.length(); ++i)
1985 paragraphs.begin()->insertChar(i, data[i], font);
1990 void InsetText::setAutoBreakRows(bool flag)
1992 if (flag != autoBreakRows) {
1993 autoBreakRows = flag;
2001 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2003 if (how != drawFrame_) {
2006 updateLocal(bv, DRAW_FRAME, false);
2011 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2013 if (frame_color != col) {
2016 updateLocal(bv, DRAW_FRAME, false);
2021 int InsetText::cx(BufferView * bv) const
2023 // we do nothing dangerous so we use a local cache
2024 LyXText * llt = getLyXText(bv);
2025 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2026 if (the_locking_inset) {
2027 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2029 if (font.isVisibleRightToLeft())
2030 x -= the_locking_inset->width(bv, font);
2036 int InsetText::cix(BufferView * bv) const
2038 // we do nothing dangerous so we use a local cache
2039 LyXText * llt = getLyXText(bv);
2040 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2041 if (the_locking_inset) {
2042 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2044 if (font.isVisibleRightToLeft())
2045 x -= the_locking_inset->width(bv, font);
2051 int InsetText::cy(BufferView * bv) const
2054 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2058 int InsetText::ciy(BufferView * bv) const
2061 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2065 pos_type InsetText::cpos(BufferView * bv) const
2067 return getLyXText(bv)->cursor.pos();
2071 ParagraphList::iterator InsetText::cpar(BufferView * bv) const
2073 return getLyXText(bv)->cursor.par();
2077 bool InsetText::cboundary(BufferView * bv) const
2079 return getLyXText(bv)->cursor.boundary();
2083 RowList::iterator InsetText::crow(BufferView * bv) const
2085 return getLyXText(bv)->cursor.row();
2089 LyXText * InsetText::getLyXText(BufferView const * lbv,
2090 bool const recursive) const
2092 if (cached_bview == lbv) {
2093 if (recursive && the_locking_inset)
2094 return the_locking_inset->getLyXText(lbv, true);
2095 LyXText * lt = cached_text.get();
2096 lyx::Assert(lt && lt->rows().begin()->par() == paragraphs.begin());
2099 // Super UGLY! (Lgb)
2100 BufferView * bv = const_cast<BufferView *>(lbv);
2103 Cache::iterator it = cache.find(bv);
2105 if (it != cache.end()) {
2108 } else if (do_resize) {
2109 resizeLyXText(do_resize);
2111 if (lt || !it->second.remove) {
2112 lyx::Assert(it->second.text.get());
2113 cached_text = it->second.text;
2114 if (recursive && the_locking_inset) {
2115 return the_locking_inset->getLyXText(bv, true);
2117 return cached_text.get();
2118 } else if (it->second.remove) {
2120 saveLyXTextState(it->second.text.get());
2126 // when we have to reinit the existing LyXText!
2128 it->second.text->init(bv);
2129 restoreLyXTextState(it->second.text.get());
2130 it->second.remove = false;
2132 cached_text = it->second.text;
2133 if (the_locking_inset && recursive) {
2134 return the_locking_inset->getLyXText(bv);
2136 return cached_text.get();
2139 // we are here only if we don't have a BufferView * in the cache!!!
2141 cached_text.reset(new LyXText(bv, const_cast<InsetText *>(this)));
2142 cached_text->init(bv);
2143 restoreLyXTextState(cached_text.get());
2145 cache.insert(make_pair(bv, cached_text));
2147 if (the_locking_inset && recursive) {
2148 return the_locking_inset->getLyXText(bv);
2150 return cached_text.get();
2154 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2158 Cache::iterator it = cache.find(bv);
2160 if (it == cache.end()) {
2164 lyx::Assert(it->second.text.get());
2166 it->second.remove = true;
2168 /// then remove all LyXText in text-insets
2169 for_each(paragraphs.begin(), paragraphs.end(),
2170 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2175 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2178 // we cannot resize this because we are in use!
2179 // so do this on the next possible getLyXText()
2184 if (boost::next(paragraphs.begin()) == paragraphs.end() &&
2185 paragraphs.begin()->empty()) { // no data, resize not neccessary!
2186 // we have to do this as a fixed width may have changed!
2187 LyXText * t = getLyXText(bv);
2188 saveLyXTextState(t);
2190 restoreLyXTextState(t);
2193 // one endless line, resize normally not necessary
2194 if (!force && getMaxWidth(bv, this) < 0)
2197 Cache::iterator it = cache.find(bv);
2198 if (it == cache.end()) {
2201 lyx::Assert(it->second.text.get());
2203 LyXText * t = it->second.text.get();
2204 saveLyXTextState(t);
2206 for_each(paragraphs.begin(), paragraphs.end(),
2207 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2210 restoreLyXTextState(t);
2211 if (the_locking_inset) {
2212 inset_x = cix(bv) - top_x + drawTextXOffset;
2213 inset_y = ciy(bv) + drawTextYOffset;
2216 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2218 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2219 // this will scroll the screen such that the cursor becomes visible
2220 bv->updateScrollbar();
2222 need_update |= FULL;
2227 void InsetText::reinitLyXText() const
2230 // we cannot resize this because we are in use!
2231 // so do this on the next possible getLyXText()
2237 for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2238 lyx::Assert(it->second.text.get());
2240 LyXText * t = it->second.text.get();
2241 BufferView * bv = it->first;
2243 saveLyXTextState(t);
2245 for_each(paragraphs.begin(), paragraphs.end(),
2246 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2249 restoreLyXTextState(t);
2250 if (the_locking_inset) {
2251 inset_x = cix(bv) - top_x + drawTextXOffset;
2252 inset_y = ciy(bv) + drawTextYOffset;
2254 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2256 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2257 // this will scroll the screen such that the cursor becomes visible
2258 bv->updateScrollbar();
2266 void InsetText::removeNewlines()
2268 bool changed = false;
2270 ParagraphList::iterator it = paragraphs.begin();
2271 ParagraphList::iterator end = paragraphs.end();
2272 for (; it != end; ++it) {
2273 for (int i = 0; i < it->size(); ++i) {
2274 if (it->isNewline(i)) {
2285 bool InsetText::nodraw() const
2287 if (the_locking_inset)
2288 return the_locking_inset->nodraw();
2289 return UpdatableInset::nodraw();
2293 int InsetText::scroll(bool recursive) const
2295 int sx = UpdatableInset::scroll(false);
2297 if (recursive && the_locking_inset)
2298 sx += the_locking_inset->scroll(recursive);
2304 void InsetText::selectAll(BufferView * bv)
2306 getLyXText(bv)->cursorTop();
2307 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2308 getLyXText(bv)->cursorBottom();
2309 getLyXText(bv)->setSelection();
2313 void InsetText::clearSelection(BufferView * bv)
2315 getLyXText(bv)->clearSelection();
2319 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2321 Painter & pain = bv->painter();
2323 int h = insetAscent + insetDescent;
2324 int ty = baseline - insetAscent;
2330 if ((ty + h) > pain.paperHeight())
2331 h = pain.paperHeight();
2332 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2333 w = pain.paperWidth();
2334 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2339 ParagraphList * InsetText::getParagraphs(int i) const
2341 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2345 LyXCursor const & InsetText::cursor(BufferView * bv) const
2347 if (the_locking_inset)
2348 return the_locking_inset->cursor(bv);
2349 return getLyXText(bv)->cursor;
2353 void InsetText::paragraph(Paragraph * p)
2355 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2356 // caller of this function has to take care of it. This IS important
2357 // as we could have to insert a paragraph before this one and just
2358 // link the actual to a new ones next and set it with this function
2361 // set ourself as owner for all the paragraphs inserted!
2362 for_each(paragraphs.begin(), paragraphs.end(),
2363 boost::bind(&Paragraph::setInsetOwner, _1, this));
2366 // redraw myself when asked for
2371 Inset * InsetText::getInsetFromID(int id_arg) const
2374 return const_cast<InsetText *>(this);
2376 ParagraphList::iterator pit = paragraphs.begin();
2377 ParagraphList::iterator pend = paragraphs.end();
2378 for (; pit != pend; ++pit) {
2379 InsetList::iterator it = pit->insetlist.begin();
2380 InsetList::iterator end = pit->insetlist.end();
2381 for (; it != end; ++it) {
2382 if (it.getInset()->id() == id_arg)
2383 return it.getInset();
2384 Inset * in = it.getInset()->getInsetFromID(id_arg);
2394 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2395 float & value) const
2401 lt = getLyXText(bv);
2404 if (the_locking_inset) {
2405 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2406 if (!word.word().empty()) {
2412 // we have to go on checking so move cursor to the next char
2413 lt->cursor.pos(lt->cursor.pos() + 1);
2415 word = lt->selectNextWordToSpellcheck(value);
2416 if (word.word().empty())
2417 bv->unlockInset(const_cast<InsetText *>(this));
2426 void InsetText::selectSelectedWord(BufferView * bv)
2428 if (the_locking_inset) {
2429 the_locking_inset->selectSelectedWord(bv);
2432 getLyXText(bv)->selectSelectedWord();
2433 updateLocal(bv, SELECTION, false);
2437 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2439 if (the_locking_inset) {
2440 the_locking_inset->toggleSelection(bv, kill_selection);
2444 lt = getLyXText(bv);
2448 int x = top_x + TEXT_TO_INSET_OFFSET;
2450 RowList::iterator rowit = lt->rows().begin();
2451 RowList::iterator end = lt->rows().end();
2452 int y_offset = top_baseline - rowit->ascent_of_text();
2454 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2455 y += rowit->height();
2461 if (need_update & SELECTION)
2463 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2469 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2473 lt = getLyXText(bv);
2476 if (the_locking_inset) {
2477 if (the_locking_inset->nextChange(bv, length))
2479 lt->cursorRight(true);
2481 lyxfind::SearchResult result =
2482 lyxfind::findNextChange(bv, lt, length);
2484 if (result == lyxfind::SR_FOUND) {
2485 LyXCursor cur = lt->cursor;
2486 bv->unlockInset(bv->theLockingInset());
2487 if (bv->lockInset(this))
2490 lt->setSelectionRange(length);
2491 updateLocal(bv, SELECTION, false);
2495 return result != lyxfind::SR_NOT_FOUND;
2499 bool InsetText::searchForward(BufferView * bv, string const & str,
2504 lt = getLyXText(bv);
2507 if (the_locking_inset) {
2508 if (the_locking_inset->searchForward(bv, str, cs, mw))
2510 lt->cursorRight(true);
2512 lyxfind::SearchResult result =
2513 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2515 if (result == lyxfind::SR_FOUND) {
2516 LyXCursor cur = lt->cursor;
2517 bv->unlockInset(bv->theLockingInset());
2518 if (bv->lockInset(this))
2521 lt->setSelectionRange(str.length());
2522 updateLocal(bv, SELECTION, false);
2526 return (result != lyxfind::SR_NOT_FOUND);
2529 bool InsetText::searchBackward(BufferView * bv, string const & str,
2532 if (the_locking_inset) {
2533 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2538 lt = getLyXText(bv);
2542 ParagraphList::iterator pit = paragraphs.begin();
2543 ParagraphList::iterator pend = paragraphs.end();
2545 while (boost::next(pit) != pend)
2548 lt->setCursor(pit, pit->size());
2550 lyxfind::SearchResult result =
2551 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2553 if (result == lyxfind::SR_FOUND) {
2554 LyXCursor cur = lt->cursor;
2555 bv->unlockInset(bv->theLockingInset());
2556 if (bv->lockInset(this))
2559 lt->setSelectionRange(str.length());
2560 updateLocal(bv, SELECTION, false);
2564 return (result != lyxfind::SR_NOT_FOUND);
2568 bool InsetText::checkInsertChar(LyXFont & font)
2571 return owner()->checkInsertChar(font);
2576 void InsetText::collapseParagraphs(BufferView * bv)
2578 LyXText * llt = getLyXText(bv);
2580 while (boost::next(paragraphs.begin()) != paragraphs.end()) {
2581 if (!paragraphs.begin()->empty() &&
2582 !boost::next(paragraphs.begin())->empty() &&
2583 !paragraphs.begin()->isSeparator(paragraphs.begin()->size() - 1))
2585 paragraphs.begin()->insertChar(paragraphs.begin()->size(), ' ');
2587 if (llt->selection.set()) {
2588 if (llt->selection.start.par() == boost::next(paragraphs.begin())) {
2589 llt->selection.start.par(paragraphs.begin());
2590 llt->selection.start.pos(
2591 llt->selection.start.pos() + paragraphs.begin()->size());
2593 if (llt->selection.end.par() == boost::next(paragraphs.begin())) {
2594 llt->selection.end.par(paragraphs.begin());
2595 llt->selection.end.pos(
2596 llt->selection.end.pos() + paragraphs.begin()->size());
2599 mergeParagraph(bv->buffer()->params, paragraphs, paragraphs.begin());
2605 void InsetText::getDrawFont(LyXFont & font) const
2609 owner()->getDrawFont(font);
2613 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2615 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2616 // And it probably does. You have to take a look at this John. (Lgb)
2617 #warning John, have a look here. (Lgb)
2619 BufferParams const & bparams = buffer->params;
2621 Paragraph * tmpbuf = newpar;
2622 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2623 if (bparams.tracking_changes)
2624 buf->cleanChanges();
2626 while (tmpbuf->next()) {
2627 tmpbuf = tmpbuf->next();
2628 lastbuffer->next(new Paragraph(*tmpbuf, false));
2629 lastbuffer->next()->previous(lastbuffer);
2630 lastbuffer = lastbuffer->next();
2631 if (bparams.tracking_changes)
2632 lastbuffer->cleanChanges();
2635 lastbuffer = &*(paragraphs.begin());
2636 while (lastbuffer->next())
2637 lastbuffer = lastbuffer->next();
2638 if (!newpar->empty() && !lastbuffer->empty() &&
2639 !lastbuffer->isSeparator(lastbuffer->size() - 1))
2641 lastbuffer->insertChar(lastbuffer->size(), ' ');
2644 // make the buf exactly the same layout than our last paragraph
2645 buf->makeSameLayout(lastbuffer);
2648 lastbuffer->next(buf);
2649 buf->previous(lastbuffer);
2650 mergeParagraph(buffer->params, paragraphs, lastbuffer);
2652 ParagraphList::iterator pit = plist.begin();
2653 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(),
2654 new Paragraph(*pit, false));
2656 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2658 ParagraphList::iterator pend = plist.end();
2659 for (; pit != pend; ++pit) {
2660 paragraphs.push_back(new Paragraph(*pit, false));
2669 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2671 ParagraphList::iterator pit = paragraphs.begin();
2672 ParagraphList::iterator pend = paragraphs.end();
2674 for (; pit != pend; ++pit) {
2675 InsetList::iterator it = pit->insetlist.begin();
2676 InsetList::iterator end = pit->insetlist.end();
2677 for (; it != end; ++it) {
2678 it.getInset()->addPreview(loader);