3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS
14 #include "insettext.h"
17 #include "BufferView.h"
18 #include "CutAndPaste.h"
20 #include "funcrequest.h"
23 #include "LaTeXFeatures.h"
26 #include "lyxcursor.h"
32 #include "paragraph.h"
33 #include "ParagraphParameters.h"
34 #include "trans_mgr.h"
35 #include "undo_funcs.h"
36 #include "WordLangTuple.h"
37 #include "paragraph_funcs.h"
39 #include "rowpainter.h"
40 #include "insetnewline.h"
42 #include "frontends/Alert.h"
43 #include "frontends/Dialogs.h"
44 #include "frontends/font_metrics.h"
45 #include "frontends/LyXView.h"
46 #include "frontends/Painter.h"
47 #include "frontends/screen.h"
49 #include "support/textutils.h"
50 #include "support/LAssert.h"
51 #include "support/lstrings.h"
52 #include "support/lyxalgo.h" // lyx::count
54 #include <boost/bind.hpp>
72 using lyx::textclass_type;
74 // These functions should probably go into bufferview_funcs somehow (Jug)
76 void InsetText::saveLyXTextState(LyXText * t) const
78 // check if my paragraphs are still valid
79 ParagraphList::iterator it = paragraphs.begin();
80 ParagraphList::iterator end = paragraphs.end();
81 for (; it != end; ++it) {
82 if (&*it == t->cursor.par())
86 if (it != end && t->cursor.pos() <= it->size()) {
87 sstate.lpar = &*t->cursor.par();
88 sstate.pos = t->cursor.pos();
89 sstate.boundary = t->cursor.boundary();
90 sstate.selstartpar = &*t->selection.start.par();
91 sstate.selstartpos = t->selection.start.pos();
92 sstate.selstartboundary = t->selection.start.boundary();
93 sstate.selendpar = &*t->selection.end.par();
94 sstate.selendpos = t->selection.end.pos();
95 sstate.selendboundary = t->selection.end.boundary();
96 sstate.selection = t->selection.set();
97 sstate.mark_set = t->selection.mark();
104 void InsetText::restoreLyXTextState(LyXText * t) const
109 t->selection.set(true);
110 /* at this point just to avoid the DEPM when setting the cursor */
111 t->selection.mark(sstate.mark_set);
112 if (sstate.selection) {
113 t->setCursor(sstate.selstartpar, sstate.selstartpos,
114 true, sstate.selstartboundary);
115 t->selection.cursor = t->cursor;
116 t->setCursor(sstate.selendpar, sstate.selendpos,
117 true, sstate.selendboundary);
119 t->setCursor(sstate.lpar, sstate.pos);
121 t->setCursor(sstate.lpar, sstate.pos, true, sstate.boundary);
122 t->selection.cursor = t->cursor;
123 t->selection.set(false);
128 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
135 InsetText::InsetText(BufferParams const & bp)
136 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
139 paragraphs.set(new Paragraph);
140 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
141 if (bp.tracking_changes)
142 paragraphs.begin()->trackChanges();
147 InsetText::InsetText(InsetText const & in, bool same_id)
148 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
155 InsetText & InsetText::operator=(InsetText const & it)
162 void InsetText::init(InsetText const * ins, bool same_id)
165 setParagraphData(ins->paragraphs, same_id);
166 autoBreakRows = ins->autoBreakRows;
167 drawFrame_ = ins->drawFrame_;
168 frame_color = ins->frame_color;
172 for_each(paragraphs.begin(), paragraphs.end(),
173 boost::bind(&Paragraph::setInsetOwner, _1, this));
175 the_locking_inset = 0;
177 frame_color = LColor::insetframe;
178 autoBreakRows = false;
191 last_drawn_width = -1;
194 in_insetAllowed = false;
198 InsetText::~InsetText()
204 void InsetText::clear(bool just_mark_erased)
206 if (just_mark_erased) {
207 ParagraphList::iterator it = paragraphs.begin();
208 ParagraphList::iterator end = paragraphs.end();
209 for (; it != end; ++it) {
216 // This is a gross hack...
217 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
220 paragraphs.set(new Paragraph);
221 paragraphs.begin()->setInsetOwner(this);
222 paragraphs.begin()->layout(old_layout);
229 Inset * InsetText::clone(Buffer const &, bool same_id) const
231 return new InsetText(*this, same_id);
235 void InsetText::write(Buffer const * buf, ostream & os) const
238 writeParagraphData(buf, os);
242 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
244 ParagraphList::iterator it = paragraphs.begin();
245 ParagraphList::iterator end = paragraphs.end();
246 Paragraph::depth_type dth = 0;
247 for (; it != end; ++it) {
248 it->write(buf, os, buf->params, dth);
253 void InsetText::read(Buffer const * buf, LyXLex & lex)
256 Paragraph::depth_type depth = 0;
260 if (buf->params.tracking_changes)
261 paragraphs.begin()->trackChanges();
263 // delete the initial paragraph
265 ParagraphList::iterator pit = paragraphs.begin();
269 token = lex.getString();
272 if (token == "\\end_inset") {
276 if (token == "\\the_end") {
277 lex.printError("\\the_end read in inset! Error in document!");
282 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
285 pit = paragraphs.begin();
286 ParagraphList::iterator const end = paragraphs.end();
287 for (; pit != end; ++pit)
288 pit->setInsetOwner(this);
290 if (token != "\\end_inset") {
291 lex.printError("Missing \\end_inset at this point. "
298 int InsetText::ascent(BufferView * bv, LyXFont const &) const
300 insetAscent = getLyXText(bv)->rows().begin()->ascent_of_text() +
301 TEXT_TO_INSET_OFFSET;
306 int InsetText::descent(BufferView * bv, LyXFont const &) const
308 LyXText * llt = getLyXText(bv);
309 insetDescent = llt->height - llt->rows().begin()->ascent_of_text() +
310 TEXT_TO_INSET_OFFSET;
315 int InsetText::width(BufferView * bv, LyXFont const &) const
317 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
318 (2 * TEXT_TO_INSET_OFFSET);
319 insetWidth = max(insetWidth, 10);
324 int InsetText::textWidth(BufferView * bv, bool fordraw) const
327 if (!autoBreakRows) {
330 w = getMaxWidth(bv, this);
333 return max(w - (2 * TEXT_TO_INSET_OFFSET),
334 (int)getLyXText(bv)->width);
338 return w - (2 * TEXT_TO_INSET_OFFSET);
342 void InsetText::draw(BufferView * bv, LyXFont const & f,
343 int baseline, float & x) const
348 // update our idea of where we are. Clearly, we should
349 // not have to know this information.
353 int const start_x = int(x);
355 Painter & pain = bv->painter();
357 // call these methods so that insetWidth, insetAscent and
358 // insetDescent have the right values.
363 // repaint the background if needed
364 if (backgroundColor() != LColor::background)
365 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
367 // no draw is necessary !!!
368 if ((drawFrame_ == LOCKED) && !locked && paragraphs.begin()->empty()) {
369 top_baseline = baseline;
376 x += static_cast<float>(scroll());
378 top_baseline = baseline;
379 top_y = baseline - insetAscent;
381 if (last_drawn_width != insetWidth) {
383 last_drawn_width = insetWidth;
386 if (the_locking_inset && (cpar(bv) == inset_par)
387 && (cpos(bv) == inset_pos)) {
388 inset_x = cix(bv) - int(x) + drawTextXOffset;
389 inset_y = ciy(bv) + drawTextYOffset;
397 x += TEXT_TO_INSET_OFFSET;
399 RowList::iterator rowit = lt->rows().begin();
400 RowList::iterator end = lt->rows().end();
402 int y_offset = baseline - rowit->ascent_of_text();
403 int ph = pain.paperHeight();
406 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
407 y += rowit->height();
408 first += rowit->height();
412 lt->top_y(-y_offset);
420 int yf = y_offset + first;
425 while ((rowit != end) && (yf < ph)) {
426 RowPainter rp(*bv, *lt, rowit);
427 rp.paint(y + y_offset + first, int(x), y + lt->top_y());
428 y += rowit->height();
429 yf += rowit->height();
435 if ((drawFrame_ == ALWAYS) || (drawFrame_ == LOCKED && locked)) {
436 drawFrame(pain, int(start_x));
439 x += insetWidth - TEXT_TO_INSET_OFFSET;
441 if (need_update != INIT) {
449 void InsetText::drawFrame(Painter & pain, int x) const
451 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
453 frame_y = top_baseline - insetAscent + ttoD2;
454 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
455 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
456 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
461 void InsetText::update(BufferView * bv, bool reinit)
464 if (reinit && owner()) {
466 owner()->update(bv, true);
472 if (reinit || need_update == INIT) {
474 // we should put this call where we set need_update to INIT!
477 owner()->update(bv, true);
480 int nw = getMaxWidth(bv, this);
481 if (nw > 0 && old_max_width != nw) {
488 if (!autoBreakRows &&
489 boost::next(paragraphs.begin()) != paragraphs.end())
490 collapseParagraphs(bv);
492 if (the_locking_inset) {
493 inset_x = cix(bv) - top_x + drawTextXOffset;
494 inset_y = ciy(bv) + drawTextYOffset;
495 the_locking_inset->update(bv, reinit);
503 if ((need_update & CURSOR_PAR) && (lt->refreshStatus() == LyXText::REFRESH_NONE) &&
505 lt->updateInset(the_locking_inset);
508 if (lt->refreshStatus() == LyXText::REFRESH_AREA)
515 int nw = getMaxWidth(bv, this);
516 if (nw > 0 && old_max_width != nw) {
523 void InsetText::setUpdateStatus(BufferView * bv, int what) const
525 // this does nothing dangerous so use only a localized buffer
526 LyXText * llt = getLyXText(bv);
529 // we have to redraw us full if our LyXText REFRESH_AREA or
530 // if we don't break row so that we only have one row to update!
531 if ((llt->refreshStatus() == LyXText::REFRESH_AREA) ||
533 (llt->refreshStatus() == LyXText::REFRESH_ROW)))
536 } else if (llt->refreshStatus() == LyXText::REFRESH_ROW) {
537 need_update |= CURSOR_PAR;
540 // this to not draw a selection when we redraw all of it!
541 if (need_update & CURSOR && !(need_update & SELECTION)) {
542 if (llt->selection.set())
544 llt->clearSelection();
549 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
552 if (!autoBreakRows &&
553 boost::next(paragraphs.begin()) != paragraphs.end())
554 collapseParagraphs(bv);
556 if (!autoBreakRows && paragraphs.size() > 1)
557 collapseParagraphs(bv);
566 setUpdateStatus(bv, what);
567 bool flag = mark_dirty ||
568 (((need_update != CURSOR) && (need_update != NONE)) ||
569 (lt->refreshStatus() != LyXText::REFRESH_NONE) || lt->selection.set());
570 if (!lt->selection.set())
571 lt->selection.cursor = lt->cursor;
577 bv->updateInset(const_cast<InsetText *>(this));
583 if (need_update == CURSOR)
585 bv->owner()->view_state_changed();
586 bv->owner()->updateMenubar();
587 bv->owner()->updateToolbar();
588 if (old_par != cpar(bv)) {
589 bv->owner()->setLayout(cpar(bv)->layout()->name());
595 string const InsetText::editMessage() const
597 return _("Opened Text Inset");
601 void InsetText::edit(BufferView * bv, int x, int y, mouse_button::state button)
603 UpdatableInset::edit(bv, x, y, button);
605 if (!bv->lockInset(this)) {
606 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
610 the_locking_inset = 0;
611 inset_pos = inset_x = inset_y = 0;
612 inset_boundary = false;
615 int tmp_y = (y < 0) ? 0 : y;
621 // we put here -1 and not button as now the button in the
622 // edit call should not be needed we will fix this in 1.3.x
623 // cycle hopefully (Jug 20020509)
624 // FIXME: GUII I've changed this to none: probably WRONG
625 if (!checkAndActivateInset(bv, x, tmp_y, mouse_button::none)) {
626 lt->setCursorFromCoordinates(x - drawTextXOffset,
628 lt->cursor.x_fix(lt->cursor.x());
630 lt->clearSelection();
632 // If the inset is empty set the language of the current font to the
633 // language to the surronding text (if different).
634 if (paragraphs.begin()->empty() &&
635 boost::next(paragraphs.begin()) == paragraphs.end()&&
636 bv->getParentLanguage(this) != lt->current_font.language())
638 LyXFont font(LyXFont::ALL_IGNORE);
639 font.setLanguage(bv->getParentLanguage(this));
640 setFont(bv, font, false);
646 if (drawFrame_ == LOCKED)
647 code = CURSOR|DRAW_FRAME;
648 updateLocal(bv, code, false);
650 // Tell the paragraph dialog that we've entered an insettext.
651 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
655 void InsetText::edit(BufferView * bv, bool front)
657 UpdatableInset::edit(bv, front);
659 if (!bv->lockInset(this)) {
660 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
664 the_locking_inset = 0;
665 inset_pos = inset_x = inset_y = 0;
666 inset_boundary = false;
675 lt->setCursor(&*(paragraphs.begin()), 0);
677 ParagraphList::iterator it = paragraphs.begin();
678 ParagraphList::iterator end = paragraphs.end();
679 while (boost::next(it) != end)
681 // int const pos = (p->size() ? p->size()-1 : p->size());
682 lt->setCursor(&*it, it->size());
684 lt->clearSelection();
686 // If the inset is empty set the language of the current font to the
687 // language to the surronding text (if different).
688 if (paragraphs.begin()->empty() &&
689 boost::next(paragraphs.begin()) == paragraphs.end() &&
690 bv->getParentLanguage(this) != lt->current_font.language()) {
691 LyXFont font(LyXFont::ALL_IGNORE);
692 font.setLanguage(bv->getParentLanguage(this));
693 setFont(bv, font, false);
698 if (drawFrame_ == LOCKED)
699 code = CURSOR|DRAW_FRAME;
700 updateLocal(bv, code, false);
704 void InsetText::insetUnlock(BufferView * bv)
706 if (the_locking_inset) {
707 the_locking_inset->insetUnlock(bv);
708 the_locking_inset = 0;
709 updateLocal(bv, CURSOR_PAR, false);
720 if (lt->selection.set()) {
721 lt->clearSelection();
723 } else if (owner()) {
724 bv->owner()->setLayout(owner()->getLyXText(bv)
725 ->cursor.par()->layout()->name());
727 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
728 // hack for deleteEmptyParMech
729 if (!paragraphs.begin()->empty()) {
730 lt->setCursor(&*(paragraphs.begin()), 0);
731 } else if (boost::next(paragraphs.begin()) != paragraphs.end()) {
732 lt->setCursor(&*boost::next(paragraphs.begin()), 0);
737 updateLocal(bv, code, false);
740 setUpdateStatus(bv, code);
745 void InsetText::lockInset(BufferView * bv)
748 the_locking_inset = 0;
749 inset_pos = inset_x = inset_y = 0;
750 inset_boundary = false;
758 lt->setCursor(&*(paragraphs.begin()), 0);
759 lt->clearSelection();
761 // If the inset is empty set the language of the current font to the
762 // language to the surronding text (if different).
763 if (paragraphs.begin()->empty() &&
764 boost::next(paragraphs.begin()) == paragraphs.end() &&
765 bv->getParentLanguage(this) != lt->current_font.language()) {
766 LyXFont font(LyXFont::ALL_IGNORE);
767 font.setLanguage(bv->getParentLanguage(this));
768 setFont(bv, font, false);
773 if (drawFrame_ == LOCKED)
774 code = CURSOR|DRAW_FRAME;
775 setUpdateStatus(bv, code);
779 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
781 the_locking_inset = inset;
782 inset_x = cix(bv) - top_x + drawTextXOffset;
783 inset_y = ciy(bv) + drawTextYOffset;
784 inset_pos = cpos(bv);
785 inset_par = cpar(bv);
786 inset_boundary = cboundary(bv);
787 updateLocal(bv, CURSOR, false);
791 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
793 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
797 if (!the_locking_inset) {
798 ParagraphList::iterator pit = paragraphs.begin();
799 ParagraphList::iterator pend = paragraphs.end();
801 int const id = inset->id();
802 for (; pit != pend; ++pit) {
803 InsetList::iterator it =
804 pit->insetlist.begin();
805 InsetList::iterator const end =
806 pit->insetlist.end();
807 for (; it != end; ++it) {
808 if (it.getInset() == inset) {
809 getLyXText(bv)->setCursorIntern(&*pit, it.getPos());
810 lockInset(bv, inset);
813 if (it.getInset()->getInsetFromID(id)) {
814 getLyXText(bv)->setCursorIntern(&*pit, it.getPos());
815 it.getInset()->edit(bv);
816 return the_locking_inset->lockInsetInInset(bv, inset);
822 if (inset == cpar(bv)->getInset(cpos(bv))) {
823 lyxerr[Debug::INSETS] << "OK" << endl;
824 lockInset(bv, inset);
826 } else if (the_locking_inset && (the_locking_inset == inset)) {
827 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
828 lyxerr[Debug::INSETS] << "OK" << endl;
829 inset_x = cix(bv) - top_x + drawTextXOffset;
830 inset_y = ciy(bv) + drawTextYOffset;
832 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
834 } else if (the_locking_inset) {
835 lyxerr[Debug::INSETS] << "MAYBE" << endl;
836 return the_locking_inset->lockInsetInInset(bv, inset);
838 lyxerr[Debug::INSETS] << "NOT OK" << endl;
843 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
846 if (!the_locking_inset)
848 if (the_locking_inset == inset) {
849 the_locking_inset->insetUnlock(bv);
850 getLyXText(bv)->updateInset(inset);
851 the_locking_inset = 0;
853 moveRightIntern(bv, true, false);
854 old_par = 0; // force layout setting
858 updateLocal(bv, CURSOR, false);
861 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
865 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
867 if (!autoBreakRows &&
868 boost::next(paragraphs.begin()) != paragraphs.end())
869 collapseParagraphs(bv);
877 if (inset->owner() != this) {
878 int ustat = CURSOR_PAR;
880 UpdatableInset * tl_inset = the_locking_inset;
882 found = tl_inset->updateInsetInInset(bv, inset);
884 tl_inset = static_cast<UpdatableInset *>(inset);
885 while(tl_inset->owner() && tl_inset->owner() != this)
886 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
887 if (!tl_inset->owner())
889 found = tl_inset->updateInsetInInset(bv, inset);
893 lt->updateInset(tl_inset);
897 setUpdateStatus(bv, ustat);
900 bool found = lt->updateInset(inset);
904 setUpdateStatus(bv, CURSOR_PAR);
905 if (the_locking_inset &&
906 cpar(bv) == inset_par && cpos(bv) == inset_pos)
908 inset_x = cix(bv) - top_x + drawTextXOffset;
909 inset_y = ciy(bv) + drawTextYOffset;
916 void InsetText::lfunMousePress(FuncRequest const & cmd)
920 // use this to check mouse motion for selection!
924 BufferView * bv = cmd.view();
925 FuncRequest cmd1 = cmd;
931 int tmp_x = cmd.x - drawTextXOffset;
932 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->top_y();
933 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
935 if (the_locking_inset) {
936 if (the_locking_inset == inset) {
937 the_locking_inset->localDispatch(cmd1);
940 // otherwise only unlock the_locking_inset
941 the_locking_inset->insetUnlock(bv);
942 the_locking_inset = 0;
945 no_selection = false;
947 if (bv->theLockingInset()) {
948 if (isHighlyEditableInset(inset)) {
949 // We just have to lock the inset before calling a
951 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
952 if (!bv->lockInset(uinset)) {
953 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
955 inset->localDispatch(cmd1);
956 if (the_locking_inset)
957 updateLocal(bv, CURSOR, false);
962 bool paste_internally = false;
963 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
964 localDispatch(FuncRequest(bv, LFUN_COPY));
965 paste_internally = true;
972 int old_top_y = lt->top_y();
974 lt->setCursorFromCoordinates(cmd.x - drawTextXOffset,
975 cmd.y + insetAscent);
976 // set the selection cursor!
977 lt->selection.cursor = lt->cursor;
978 lt->cursor.x_fix(lt->cursor.x());
980 if (lt->selection.set()) {
981 lt->clearSelection();
982 updateLocal(bv, FULL, false);
984 lt->clearSelection();
985 updateLocal(bv, CURSOR, false);
991 bv->owner()->setLayout(cpar(bv)->layout()->name());
993 // we moved the view we cannot do mouse selection in this case!
994 if (getLyXText(bv)->top_y() != old_top_y)
997 // Insert primary selection with middle mouse
998 // if there is a local selection in the current buffer,
1000 if (cmd.button() == mouse_button::button2) {
1001 if (paste_internally)
1002 localDispatch(FuncRequest(bv, LFUN_PASTE));
1004 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
1007 getLyXText(bv)->clearSelection();
1012 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
1014 BufferView * bv = cmd.view();
1015 FuncRequest cmd1 = cmd;
1019 no_selection = true;
1020 if (the_locking_inset)
1021 return the_locking_inset->localDispatch(cmd1);
1023 int tmp_x = cmd.x - drawTextXOffset;
1024 int tmp_y = cmd.y + insetAscent - getLyXText(bv)->top_y();
1025 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
1028 // This code should probably be removed now. Simple insets
1029 // (!highlyEditable) can actually take the localDispatch,
1030 // and turn it into edit() if necessary. But we still
1031 // need to deal properly with the whole relative vs.
1032 // absolute mouse co-ords thing in a realiable, sensible way
1034 if (isHighlyEditableInset(inset))
1035 ret = inset->localDispatch(cmd1);
1037 inset_x = cix(bv) - top_x + drawTextXOffset;
1038 inset_y = ciy(bv) + drawTextYOffset;
1039 cmd1.x = cmd.x - inset_x;
1040 cmd1.y = cmd.x - inset_y;
1041 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
1045 ret = inset->localDispatch(cmd1);
1046 updateLocal(bv, CURSOR_PAR, false);
1053 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
1055 FuncRequest cmd1 = cmd;
1059 if (the_locking_inset) {
1060 the_locking_inset->localDispatch(cmd1);
1064 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
1067 BufferView * bv = cmd.view();
1070 lt = getLyXText(bv);
1073 LyXCursor cur = lt->cursor;
1074 lt->setCursorFromCoordinates
1075 (cmd.x - drawTextXOffset, cmd.y + insetAscent);
1076 lt->cursor.x_fix(lt->cursor.x());
1077 if (cur == lt->cursor) {
1083 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1084 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1088 updateLocal(bv, SELECTION, false);
1093 Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
1095 BufferView * bv = ev.view();
1096 switch (ev.action) {
1097 case LFUN_MOUSE_PRESS:
1100 case LFUN_MOUSE_MOTION:
1101 lfunMouseMotion(ev);
1103 case LFUN_MOUSE_RELEASE:
1104 return lfunMouseRelease(ev) ? DISPATCHED : UNDISPATCHED;
1109 bool was_empty = (paragraphs.begin()->empty() &&
1110 boost::next(paragraphs.begin()) == paragraphs.end());
1111 no_selection = false;
1112 RESULT result = UpdatableInset::localDispatch(ev);
1113 if (result != UNDISPATCHED)
1116 result = DISPATCHED;
1117 if (ev.action < 0 && ev.argument.empty())
1120 if (the_locking_inset) {
1121 result = the_locking_inset->localDispatch(ev);
1122 if (result == DISPATCHED_NOUPDATE)
1124 else if (result == DISPATCHED) {
1125 updateLocal(bv, CURSOR_PAR, false);
1127 } else if (result >= FINISHED) {
1129 case FINISHED_RIGHT:
1130 moveRightIntern(bv, false, false);
1131 result = DISPATCHED;
1134 if ((result = moveUp(bv)) >= FINISHED) {
1135 updateLocal(bv, CURSOR, false);
1136 bv->unlockInset(this);
1140 if ((result = moveDown(bv)) >= FINISHED) {
1141 updateLocal(bv, CURSOR, false);
1142 bv->unlockInset(this);
1146 result = DISPATCHED;
1149 the_locking_inset = 0;
1150 updateLocal(bv, CURSOR, false);
1151 // make sure status gets reset immediately
1152 bv->owner()->clearMessage();
1158 lt = getLyXText(bv);
1162 int updflag = false;
1164 // what type of update to do on a cursor movement
1165 int cursor_update = CURSOR;
1167 if (lt->selection.set())
1168 cursor_update = SELECTION;
1170 switch (ev.action) {
1173 case LFUN_SELFINSERT:
1174 if (bv->buffer()->isReadonly()) {
1175 // setErrorMessage(N_("Document is read only"));
1178 if (!ev.argument.empty()) {
1179 /* Automatically delete the currently selected
1180 * text and replace it with what is being
1181 * typed in now. Depends on lyxrc settings
1182 * "auto_region_delete", which defaults to
1185 // This should not be needed here and is also WRONG!
1186 setUndo(bv, Undo::INSERT,
1187 lt->cursor.par(), boost::next(lt->cursor.par()));
1190 if (lyxrc.auto_region_delete) {
1191 if (lt->selection.set()) {
1192 lt->cutSelection(false, false);
1195 lt->clearSelection();
1196 for (string::size_type i = 0; i < ev.argument.length(); ++i) {
1197 bv->owner()->getIntl().getTransManager().
1198 TranslateAndInsert(ev.argument[i], lt);
1201 lt->selection.cursor = lt->cursor;
1202 updwhat = CURSOR | CURSOR_PAR;
1204 result = DISPATCHED_NOUPDATE;
1207 // cursor movements that need special handling
1210 result = moveRight(bv);
1212 updwhat = cursor_update;
1216 result = moveLeft(bv);
1217 updwhat = cursor_update;
1221 result = moveDown(bv);
1222 updwhat = cursor_update;
1226 result = moveUp(bv);
1227 updwhat = cursor_update;
1231 if (crow(bv) == lt->rows().begin())
1232 result = FINISHED_UP;
1234 lt->cursorPrevious();
1235 lt->clearSelection();
1236 result = DISPATCHED_NOUPDATE;
1238 updwhat = cursor_update;
1242 if (boost::next(crow(bv)) == lt->rows().end())
1243 result = FINISHED_DOWN;
1246 lt->clearSelection();
1247 result = DISPATCHED_NOUPDATE;
1249 updwhat = cursor_update;
1252 case LFUN_BACKSPACE: {
1253 if (lt->selection.set())
1254 lt->cutSelection(true, false);
1257 updwhat = CURSOR_PAR;
1263 if (lt->selection.set()) {
1264 lt->cutSelection(true, false);
1268 updwhat = CURSOR_PAR;
1274 lt->cutSelection(bv);
1275 updwhat = CURSOR_PAR;
1282 lt->copySelection();
1283 updwhat = CURSOR_PAR;
1286 case LFUN_PASTESELECTION:
1288 string const clip(bv->getClipboard());
1292 if (ev.argument == "paragraph") {
1293 lt->insertStringAsParagraphs(clip);
1295 lt->insertStringAsLines(clip);
1298 lt->clearSelection();
1300 updwhat = CURSOR_PAR;
1306 if (!autoBreakRows) {
1307 if (CutAndPaste::nrOfParagraphs() > 1) {
1308 #ifdef WITH_WARNINGS
1309 #warning FIXME horrendously bad UI
1311 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1316 lt->pasteSelection();
1318 lt->clearSelection();
1319 updwhat = CURSOR_PAR;
1324 case LFUN_BREAKPARAGRAPH:
1325 if (!autoBreakRows) {
1326 result = DISPATCHED;
1329 lt->breakParagraph(paragraphs, 0);
1330 updwhat = CURSOR | FULL;
1334 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1335 if (!autoBreakRows) {
1336 result = DISPATCHED;
1339 lt->breakParagraph(paragraphs, 1);
1340 updwhat = CURSOR | FULL;
1344 case LFUN_BREAKLINE: {
1345 if (!autoBreakRows) {
1346 result = DISPATCHED;
1350 lt->insertInset(new InsetNewline);
1351 updwhat = CURSOR | CURSOR_PAR;
1357 // do not set layouts on non breakable textinsets
1358 if (autoBreakRows) {
1359 string cur_layout = cpar(bv)->layout()->name();
1361 // Derive layout number from given argument (string)
1362 // and current buffer's textclass (number). */
1363 LyXTextClass const & tclass =
1364 bv->buffer()->params.getLyXTextClass();
1365 string layout = ev.argument;
1366 bool hasLayout = tclass.hasLayout(layout);
1368 // If the entry is obsolete, use the new one instead.
1370 string const & obs =
1371 tclass[layout]->obsoleted_by();
1376 // see if we found the layout number:
1378 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + ev.argument + N_(" not known"));
1379 bv->owner()->dispatch(lf);
1383 if (cur_layout != layout) {
1384 cur_layout = layout;
1385 lt->setLayout(layout);
1386 bv->owner()->setLayout(cpar(bv)->layout()->name());
1387 updwhat = CURSOR_PAR;
1391 // reset the layout box
1392 bv->owner()->setLayout(cpar(bv)->layout()->name());
1395 case LFUN_PARAGRAPH_SPACING:
1396 // This one is absolutely not working. When fiddling with this
1397 // it also seems to me that the paragraphs inside the insettext
1398 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1399 // FIXME: how old is this comment ? ...
1401 ParagraphList::iterator pit = lt->cursor.par();
1402 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1403 float cur_value = 1.0;
1404 if (cur_spacing == Spacing::Other) {
1405 cur_value = pit->params().spacing().getValue();
1408 istringstream istr(ev.argument.c_str());
1411 Spacing::Space new_spacing = cur_spacing;
1412 float new_value = cur_value;
1414 lyxerr << "Missing argument to `paragraph-spacing'"
1416 } else if (tmp == "single") {
1417 new_spacing = Spacing::Single;
1418 } else if (tmp == "onehalf") {
1419 new_spacing = Spacing::Onehalf;
1420 } else if (tmp == "double") {
1421 new_spacing = Spacing::Double;
1422 } else if (tmp == "other") {
1423 new_spacing = Spacing::Other;
1426 lyxerr << "new_value = " << tmpval << endl;
1429 } else if (tmp == "default") {
1430 new_spacing = Spacing::Default;
1432 lyxerr << _("Unknown spacing argument: ")
1433 << ev.argument << endl;
1435 if (cur_spacing != new_spacing || cur_value != new_value) {
1436 pit->params().spacing(Spacing(new_spacing, new_value));
1437 updwhat = CURSOR_PAR;
1443 // These need to do repaints but don't require
1444 // special handling otherwise. A *lot* of the
1445 // above could probably be done similarly ...
1450 case LFUN_WORDRIGHT:
1451 // these two are really unhandled ...
1453 case LFUN_BEGINNINGBUF:
1454 updwhat = cursor_update;
1455 if (!bv->dispatch(ev))
1456 result = UNDISPATCHED;
1465 case LFUN_WORDLEFTSEL:
1466 case LFUN_WORDRIGHTSEL:
1467 updwhat = SELECTION;
1472 if (!bv->dispatch(ev))
1473 result = UNDISPATCHED;
1480 updateLocal(bv, updwhat, updflag);
1481 /// If the action has deleted all text in the inset, we need to change the
1482 // language to the language of the surronding text.
1483 if (!was_empty && paragraphs.begin()->empty() &&
1484 boost::next(paragraphs.begin()) == paragraphs.end()) {
1485 LyXFont font(LyXFont::ALL_IGNORE);
1486 font.setLanguage(bv->getParentLanguage(this));
1487 setFont(bv, font, false);
1490 if (result >= FINISHED)
1491 bv->unlockInset(this);
1493 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1494 result = DISPATCHED;
1499 int InsetText::latex(Buffer const * buf, ostream & os,
1500 bool moving_arg, bool) const
1503 latexParagraphs(buf, paragraphs,
1504 paragraphs.begin(), paragraphs.end(),
1505 os, texrow, moving_arg);
1506 return texrow.rows();
1510 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1512 unsigned int lines = 0;
1514 ParagraphList::iterator beg = paragraphs.begin();
1515 ParagraphList::iterator end = paragraphs.end();
1516 ParagraphList::iterator it = beg;
1517 for (; it != end; ++it) {
1518 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1519 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1526 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1528 unsigned int lines = 0;
1530 vector<string> environment_stack(10);
1531 vector<string> environment_inner(10);
1533 int const command_depth = 0;
1536 Paragraph::depth_type depth = 0; // paragraph depth
1538 ParagraphList::iterator pit = paragraphs.begin();
1539 ParagraphList::iterator pend = paragraphs.end();
1541 for (; pit != pend; ++pit) {
1543 int desc_on = 0; // description mode
1545 LyXLayout_ptr const & style = pit->layout();
1547 // environment tag closing
1548 for (; depth > pit->params().depth(); --depth) {
1549 if (environment_inner[depth] != "!-- --") {
1550 item_name = "listitem";
1551 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1552 if (environment_inner[depth] == "varlistentry")
1553 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1555 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1556 environment_stack[depth].erase();
1557 environment_inner[depth].erase();
1560 if (depth == pit->params().depth()
1561 && environment_stack[depth] != style->latexname()
1562 && !environment_stack[depth].empty()) {
1563 if (environment_inner[depth] != "!-- --") {
1564 item_name= "listitem";
1565 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1566 if (environment_inner[depth] == "varlistentry")
1567 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1570 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1572 environment_stack[depth].erase();
1573 environment_inner[depth].erase();
1576 // Write opening SGML tags.
1577 switch (style->latextype) {
1578 case LATEX_PARAGRAPH:
1579 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1583 buf->sgmlError(&*pit, 0, _("Error: LatexType Command not allowed here.\n"));
1587 case LATEX_ENVIRONMENT:
1588 case LATEX_ITEM_ENVIRONMENT:
1589 if (depth < pit->params().depth()) {
1590 depth = pit->params().depth();
1591 environment_stack[depth].erase();
1594 if (environment_stack[depth] != style->latexname()) {
1595 if (environment_stack.size() == depth + 1) {
1596 environment_stack.push_back("!-- --");
1597 environment_inner.push_back("!-- --");
1599 environment_stack[depth] = style->latexname();
1600 environment_inner[depth] = "!-- --";
1601 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1603 if (environment_inner[depth] != "!-- --") {
1604 item_name= "listitem";
1605 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1606 if (environment_inner[depth] == "varlistentry")
1607 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1611 if (style->latextype == LATEX_ENVIRONMENT) {
1612 if (!style->latexparam().empty()) {
1613 if (style->latexparam() == "CDATA")
1616 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1621 desc_on = (style->labeltype == LABEL_MANUAL);
1623 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1624 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1626 item_name = desc_on ? "term" : "para";
1627 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1631 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1635 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1638 // write closing SGML tags
1639 switch (style->latextype) {
1640 case LATEX_ENVIRONMENT:
1641 if (!style->latexparam().empty()) {
1642 if (style->latexparam() == "CDATA")
1645 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1648 case LATEX_ITEM_ENVIRONMENT:
1649 if (desc_on == 1) break;
1651 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1653 case LATEX_PARAGRAPH:
1654 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1657 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1663 for (int d = depth; d >= 0; --d) {
1664 if (!environment_stack[depth].empty()) {
1665 if (environment_inner[depth] != "!-- --") {
1666 item_name = "listitem";
1667 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1668 if (environment_inner[depth] == "varlistentry")
1669 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1672 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1680 void InsetText::validate(LaTeXFeatures & features) const
1682 for_each(paragraphs.begin(), paragraphs.end(),
1683 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1687 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1689 if (the_locking_inset) {
1690 the_locking_inset->getCursor(bv, x, y);
1694 y = cy(&bv) + InsetText::y();
1698 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1700 if (the_locking_inset) {
1701 the_locking_inset->getCursorPos(bv, x, y);
1704 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1705 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1709 int InsetText::insetInInsetY() const
1711 if (!the_locking_inset)
1714 return (inset_y + the_locking_inset->insetInInsetY());
1718 void InsetText::fitInsetCursor(BufferView * bv) const
1720 if (the_locking_inset) {
1721 the_locking_inset->fitInsetCursor(bv);
1724 LyXFont const font =
1725 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1727 int const asc = font_metrics::maxAscent(font);
1728 int const desc = font_metrics::maxDescent(font);
1730 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1731 need_update |= FULL;
1736 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1738 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1739 return moveLeftIntern(bv, false, activate_inset, selecting);
1741 return moveRightIntern(bv, true, activate_inset, selecting);
1746 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1748 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1749 return moveRightIntern(bv, true, activate_inset, selecting);
1751 return moveLeftIntern(bv, false, activate_inset, selecting);
1756 InsetText::moveRightIntern(BufferView * bv, bool front,
1757 bool activate_inset, bool selecting)
1759 LyXText * text = getLyXText(bv);
1761 ParagraphList::iterator c_par = cpar(bv);
1763 if (boost::next(c_par) == paragraphs.end() &&
1764 (cpos(bv) >= c_par->size()))
1765 return FINISHED_RIGHT;
1766 if (activate_inset && checkAndActivateInset(bv, front))
1768 text->cursorRight(bv);
1770 text->clearSelection();
1771 return DISPATCHED_NOUPDATE;
1776 InsetText::moveLeftIntern(BufferView * bv, bool front,
1777 bool activate_inset, bool selecting)
1779 LyXText * text = getLyXText(bv);
1781 if (cpar(bv) == paragraphs.begin() && (cpos(bv) <= 0))
1783 text->cursorLeft(bv);
1785 text->clearSelection();
1786 if (activate_inset && checkAndActivateInset(bv, front))
1788 return DISPATCHED_NOUPDATE;
1792 Inset::RESULT InsetText::moveUp(BufferView * bv)
1794 if (crow(bv) == getLyXText(bv)->rows().begin())
1796 getLyXText(bv)->cursorUp(bv);
1797 getLyXText(bv)->clearSelection();
1798 return DISPATCHED_NOUPDATE;
1802 Inset::RESULT InsetText::moveDown(BufferView * bv)
1804 if (boost::next(crow(bv)) == getLyXText(bv)->rows().end())
1805 return FINISHED_DOWN;
1806 getLyXText(bv)->cursorDown(bv);
1807 getLyXText(bv)->clearSelection();
1808 return DISPATCHED_NOUPDATE;
1812 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1814 if (the_locking_inset) {
1815 if (the_locking_inset->insetAllowed(inset))
1816 return the_locking_inset->insertInset(bv, inset);
1819 inset->setOwner(this);
1820 getLyXText(bv)->insertInset(inset);
1822 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1827 bool InsetText::insetAllowed(Inset::Code code) const
1829 // in_insetAllowed is a really gross hack,
1830 // to allow us to call the owner's insetAllowed
1831 // without stack overflow, which can happen
1832 // when the owner uses InsetCollapsable::insetAllowed()
1834 if (in_insetAllowed)
1836 in_insetAllowed = true;
1837 if (the_locking_inset)
1838 ret = the_locking_inset->insetAllowed(code);
1840 ret = owner()->insetAllowed(code);
1841 in_insetAllowed = false;
1846 UpdatableInset * InsetText::getLockingInset() const
1848 return the_locking_inset ? the_locking_inset->getLockingInset() :
1849 const_cast<InsetText *>(this);
1853 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1857 if (the_locking_inset)
1858 return the_locking_inset->getFirstLockingInsetOfType(c);
1863 bool InsetText::showInsetDialog(BufferView * bv) const
1865 if (the_locking_inset)
1866 return the_locking_inset->showInsetDialog(bv);
1871 vector<string> const InsetText::getLabelList() const
1873 vector<string> label_list;
1875 ParagraphList::iterator pit = paragraphs.begin();
1876 ParagraphList::iterator pend = paragraphs.end();
1877 for (; pit != pend; ++pit) {
1878 InsetList::iterator beg = pit->insetlist.begin();
1879 InsetList::iterator end = pit->insetlist.end();
1880 for (; beg != end; ++beg) {
1881 vector<string> const l = beg.getInset()->getLabelList();
1882 label_list.insert(label_list.end(), l.begin(), l.end());
1889 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1892 if (the_locking_inset) {
1893 the_locking_inset->setFont(bv, font, toggleall, selectall);
1896 if ((boost::next(paragraphs.begin()) == paragraphs.end() &&
1897 paragraphs.begin()->empty()) || cpar(bv)->empty()) {
1898 getLyXText(bv)->setFont(font, toggleall);
1903 lt = getLyXText(bv);
1906 if (lt->selection.set()) {
1907 setUndo(bv, Undo::EDIT, lt->cursor.par(), boost::next(lt->cursor.par()));
1911 lt->toggleFree(font, toggleall);
1913 lt->clearSelection();
1915 bool flag = (selectall || lt->selection.set());
1919 updateLocal(bv, FULL, true);
1921 updateLocal(bv, CURSOR_PAR, true);
1925 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1927 if (cpar(bv)->isInset(cpos(bv))) {
1929 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1930 if (!isHighlyEditableInset(inset))
1932 inset->edit(bv, front);
1933 if (!the_locking_inset)
1935 updateLocal(bv, CURSOR, false);
1942 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1943 mouse_button::state button)
1945 x -= drawTextXOffset;
1947 int dummyy = y + insetAscent;
1948 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1949 // we only do the edit() call if the inset was hit by the mouse
1950 // or if it is a highly editable inset. So we should call this
1951 // function from our own edit with button < 0.
1952 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1954 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1962 inset_x = cix(bv) - top_x + drawTextXOffset;
1963 inset_y = ciy(bv) + drawTextYOffset;
1964 inset->edit(bv, x - inset_x, y - inset_y, button);
1965 if (!the_locking_inset)
1967 updateLocal(bv, CURSOR, false);
1974 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1977 int w = UpdatableInset::getMaxWidth(bv, inset);
1982 w = w - top_x + owner()->x();
1985 w -= (2 * TEXT_TO_INSET_OFFSET);
1988 return UpdatableInset::getMaxWidth(bv, inset);
1993 void InsetText::setParagraphData(ParagraphList const & plist, bool same_id)
1995 // we have to unlock any locked inset otherwise we're in troubles
1996 the_locking_inset = 0;
1999 ParagraphList::iterator pit = plist.begin();
2000 ParagraphList::iterator pend = plist.end();
2001 for (; pit != pend; ++pit) {
2002 Paragraph * new_par = new Paragraph(*pit, same_id);
2003 new_par->setInsetOwner(this);
2004 paragraphs.push_back(new_par);
2012 void InsetText::markNew(bool track_changes)
2014 ParagraphList::iterator pit = paragraphs.begin();
2015 ParagraphList::iterator pend = paragraphs.end();
2016 for (; pit != pend; ++pit) {
2017 if (track_changes) {
2018 pit->trackChanges();
2020 // no-op when not tracking
2021 pit->cleanChanges();
2027 void InsetText::setText(string const & data, LyXFont const & font)
2030 for (unsigned int i = 0; i < data.length(); ++i)
2031 paragraphs.begin()->insertChar(i, data[i], font);
2036 void InsetText::setAutoBreakRows(bool flag)
2038 if (flag != autoBreakRows) {
2039 autoBreakRows = flag;
2047 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2049 if (how != drawFrame_) {
2052 updateLocal(bv, DRAW_FRAME, false);
2057 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2059 if (frame_color != col) {
2062 updateLocal(bv, DRAW_FRAME, false);
2067 int InsetText::cx(BufferView * bv) const
2069 // we do nothing dangerous so we use a local cache
2070 LyXText * llt = getLyXText(bv);
2071 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2072 if (the_locking_inset) {
2073 LyXFont font = llt->getFont(bv->buffer(), &*llt->cursor.par(),
2075 if (font.isVisibleRightToLeft())
2076 x -= the_locking_inset->width(bv, font);
2082 int InsetText::cix(BufferView * bv) const
2084 // we do nothing dangerous so we use a local cache
2085 LyXText * llt = getLyXText(bv);
2086 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2087 if (the_locking_inset) {
2088 LyXFont font = llt->getFont(bv->buffer(), &*llt->cursor.par(),
2090 if (font.isVisibleRightToLeft())
2091 x -= the_locking_inset->width(bv, font);
2097 int InsetText::cy(BufferView * bv) const
2100 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2104 int InsetText::ciy(BufferView * bv) const
2107 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2111 pos_type InsetText::cpos(BufferView * bv) const
2113 return getLyXText(bv)->cursor.pos();
2117 ParagraphList::iterator InsetText::cpar(BufferView * bv) const
2119 return getLyXText(bv)->cursor.par();
2123 bool InsetText::cboundary(BufferView * bv) const
2125 return getLyXText(bv)->cursor.boundary();
2129 RowList::iterator InsetText::crow(BufferView * bv) const
2131 return getLyXText(bv)->cursor.row();
2135 LyXText * InsetText::getLyXText(BufferView const * lbv,
2136 bool const recursive) const
2138 if (cached_bview == lbv) {
2139 if (recursive && the_locking_inset)
2140 return the_locking_inset->getLyXText(lbv, true);
2141 LyXText * lt = cached_text.get();
2142 lyx::Assert(lt && lt->rows().begin()->par() == &*(paragraphs.begin()));
2145 // Super UGLY! (Lgb)
2146 BufferView * bv = const_cast<BufferView *>(lbv);
2149 Cache::iterator it = cache.find(bv);
2151 if (it != cache.end()) {
2154 } else if (do_resize) {
2155 resizeLyXText(do_resize);
2157 if (lt || !it->second.remove) {
2158 lyx::Assert(it->second.text.get());
2159 cached_text = it->second.text;
2160 if (recursive && the_locking_inset) {
2161 return the_locking_inset->getLyXText(bv, true);
2163 return cached_text.get();
2164 } else if (it->second.remove) {
2166 saveLyXTextState(it->second.text.get());
2172 // when we have to reinit the existing LyXText!
2174 it->second.text->init(bv);
2175 restoreLyXTextState(it->second.text.get());
2176 it->second.remove = false;
2178 cached_text = it->second.text;
2179 if (the_locking_inset && recursive) {
2180 return the_locking_inset->getLyXText(bv);
2182 return cached_text.get();
2185 // we are here only if we don't have a BufferView * in the cache!!!
2187 cached_text.reset(new LyXText(bv, const_cast<InsetText *>(this)));
2188 cached_text->init(bv);
2189 restoreLyXTextState(cached_text.get());
2191 cache.insert(make_pair(bv, cached_text));
2193 if (the_locking_inset && recursive) {
2194 return the_locking_inset->getLyXText(bv);
2196 return cached_text.get();
2200 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2204 Cache::iterator it = cache.find(bv);
2206 if (it == cache.end()) {
2210 lyx::Assert(it->second.text.get());
2212 it->second.remove = true;
2214 /// then remove all LyXText in text-insets
2215 for_each(paragraphs.begin(), paragraphs.end(),
2216 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2221 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2224 // we cannot resize this because we are in use!
2225 // so do this on the next possible getLyXText()
2230 if (boost::next(paragraphs.begin()) == paragraphs.end() &&
2231 paragraphs.begin()->empty()) { // no data, resize not neccessary!
2232 // we have to do this as a fixed width may have changed!
2233 LyXText * t = getLyXText(bv);
2234 saveLyXTextState(t);
2236 restoreLyXTextState(t);
2239 // one endless line, resize normally not necessary
2240 if (!force && getMaxWidth(bv, this) < 0)
2243 Cache::iterator it = cache.find(bv);
2244 if (it == cache.end()) {
2247 lyx::Assert(it->second.text.get());
2249 LyXText * t = it->second.text.get();
2250 saveLyXTextState(t);
2252 for_each(paragraphs.begin(), paragraphs.end(),
2253 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2256 restoreLyXTextState(t);
2257 if (the_locking_inset) {
2258 inset_x = cix(bv) - top_x + drawTextXOffset;
2259 inset_y = ciy(bv) + drawTextYOffset;
2262 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2264 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2265 // this will scroll the screen such that the cursor becomes visible
2266 bv->updateScrollbar();
2268 need_update |= FULL;
2273 void InsetText::reinitLyXText() const
2276 // we cannot resize this because we are in use!
2277 // so do this on the next possible getLyXText()
2283 for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2284 lyx::Assert(it->second.text.get());
2286 LyXText * t = it->second.text.get();
2287 BufferView * bv = it->first;
2289 saveLyXTextState(t);
2291 for_each(paragraphs.begin(), paragraphs.end(),
2292 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2295 restoreLyXTextState(t);
2296 if (the_locking_inset) {
2297 inset_x = cix(bv) - top_x + drawTextXOffset;
2298 inset_y = ciy(bv) + drawTextYOffset;
2300 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2302 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2303 // this will scroll the screen such that the cursor becomes visible
2304 bv->updateScrollbar();
2312 void InsetText::removeNewlines()
2314 bool changed = false;
2316 ParagraphList::iterator it = paragraphs.begin();
2317 ParagraphList::iterator end = paragraphs.end();
2318 for (; it != end; ++it) {
2319 for (int i = 0; i < it->size(); ++i) {
2320 if (it->isNewline(i)) {
2331 bool InsetText::nodraw() const
2333 if (the_locking_inset)
2334 return the_locking_inset->nodraw();
2335 return UpdatableInset::nodraw();
2339 int InsetText::scroll(bool recursive) const
2341 int sx = UpdatableInset::scroll(false);
2343 if (recursive && the_locking_inset)
2344 sx += the_locking_inset->scroll(recursive);
2350 void InsetText::selectAll(BufferView * bv)
2352 getLyXText(bv)->cursorTop();
2353 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2354 getLyXText(bv)->cursorBottom();
2355 getLyXText(bv)->setSelection();
2359 void InsetText::clearSelection(BufferView * bv)
2361 getLyXText(bv)->clearSelection();
2365 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2367 Painter & pain = bv->painter();
2369 int h = insetAscent + insetDescent;
2370 int ty = baseline - insetAscent;
2376 if ((ty + h) > pain.paperHeight())
2377 h = pain.paperHeight();
2378 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2379 w = pain.paperWidth();
2380 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2385 Paragraph * InsetText::firstParagraph() const
2388 if (the_locking_inset)
2389 if ((result = the_locking_inset->firstParagraph()))
2391 return &*(paragraphs.begin());
2395 Paragraph * InsetText::getFirstParagraph(int i) const
2397 return (i == 0) ? &*(paragraphs.begin()) : 0;
2401 LyXCursor const & InsetText::cursor(BufferView * bv) const
2403 if (the_locking_inset)
2404 return the_locking_inset->cursor(bv);
2405 return getLyXText(bv)->cursor;
2409 void InsetText::paragraph(Paragraph * p)
2411 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2412 // caller of this function has to take care of it. This IS important
2413 // as we could have to insert a paragraph before this one and just
2414 // link the actual to a new ones next and set it with this function
2417 // set ourself as owner for all the paragraphs inserted!
2418 for_each(paragraphs.begin(), paragraphs.end(),
2419 boost::bind(&Paragraph::setInsetOwner, _1, this));
2422 // redraw myself when asked for
2427 Inset * InsetText::getInsetFromID(int id_arg) const
2430 return const_cast<InsetText *>(this);
2432 ParagraphList::iterator pit = paragraphs.begin();
2433 ParagraphList::iterator pend = paragraphs.end();
2434 for (; pit != pend; ++pit) {
2435 InsetList::iterator it = pit->insetlist.begin();
2436 InsetList::iterator end = pit->insetlist.end();
2437 for (; it != end; ++it) {
2438 if (it.getInset()->id() == id_arg)
2439 return it.getInset();
2440 Inset * in = it.getInset()->getInsetFromID(id_arg);
2450 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2451 float & value) const
2457 lt = getLyXText(bv);
2460 if (the_locking_inset) {
2461 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2462 if (!word.word().empty()) {
2468 // we have to go on checking so move cursor to the next char
2469 lt->cursor.pos(lt->cursor.pos() + 1);
2471 word = lt->selectNextWordToSpellcheck(value);
2472 if (word.word().empty())
2473 bv->unlockInset(const_cast<InsetText *>(this));
2482 void InsetText::selectSelectedWord(BufferView * bv)
2484 if (the_locking_inset) {
2485 the_locking_inset->selectSelectedWord(bv);
2488 getLyXText(bv)->selectSelectedWord();
2489 updateLocal(bv, SELECTION, false);
2493 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2495 if (the_locking_inset) {
2496 the_locking_inset->toggleSelection(bv, kill_selection);
2500 lt = getLyXText(bv);
2504 int x = top_x + TEXT_TO_INSET_OFFSET;
2506 RowList::iterator rowit = lt->rows().begin();
2507 RowList::iterator end = lt->rows().end();
2508 int y_offset = top_baseline - rowit->ascent_of_text();
2510 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2511 y += rowit->height();
2517 if (need_update & SELECTION)
2519 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2525 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2529 lt = getLyXText(bv);
2532 if (the_locking_inset) {
2533 if (the_locking_inset->nextChange(bv, length))
2535 lt->cursorRight(true);
2537 lyxfind::SearchResult result =
2538 lyxfind::findNextChange(bv, lt, length);
2540 if (result == lyxfind::SR_FOUND) {
2541 LyXCursor cur = lt->cursor;
2542 bv->unlockInset(bv->theLockingInset());
2543 if (bv->lockInset(this))
2546 lt->setSelectionRange(length);
2547 updateLocal(bv, SELECTION, false);
2551 return result != lyxfind::SR_NOT_FOUND;
2555 bool InsetText::searchForward(BufferView * bv, string const & str,
2560 lt = getLyXText(bv);
2563 if (the_locking_inset) {
2564 if (the_locking_inset->searchForward(bv, str, cs, mw))
2566 lt->cursorRight(true);
2568 lyxfind::SearchResult result =
2569 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2571 if (result == lyxfind::SR_FOUND) {
2572 LyXCursor cur = lt->cursor;
2573 bv->unlockInset(bv->theLockingInset());
2574 if (bv->lockInset(this))
2577 lt->setSelectionRange(str.length());
2578 updateLocal(bv, SELECTION, false);
2582 return (result != lyxfind::SR_NOT_FOUND);
2585 bool InsetText::searchBackward(BufferView * bv, string const & str,
2588 if (the_locking_inset) {
2589 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2594 lt = getLyXText(bv);
2598 ParagraphList::iterator pit = paragraphs.begin();
2599 ParagraphList::iterator pend = paragraphs.end();
2601 while (boost::next(pit) != pend)
2604 lt->setCursor(&*pit, pit->size());
2606 lyxfind::SearchResult result =
2607 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2609 if (result == lyxfind::SR_FOUND) {
2610 LyXCursor cur = lt->cursor;
2611 bv->unlockInset(bv->theLockingInset());
2612 if (bv->lockInset(this))
2615 lt->setSelectionRange(str.length());
2616 updateLocal(bv, SELECTION, false);
2620 return (result != lyxfind::SR_NOT_FOUND);
2624 bool InsetText::checkInsertChar(LyXFont & font)
2627 return owner()->checkInsertChar(font);
2632 void InsetText::collapseParagraphs(BufferView * bv)
2634 LyXText * llt = getLyXText(bv);
2636 while (boost::next(paragraphs.begin()) != paragraphs.end()) {
2637 if (!paragraphs.begin()->empty() &&
2638 !boost::next(paragraphs.begin())->empty() &&
2639 !paragraphs.begin()->isSeparator(paragraphs.begin()->size() - 1))
2641 paragraphs.begin()->insertChar(paragraphs.begin()->size(), ' ');
2643 if (llt->selection.set()) {
2644 if (llt->selection.start.par() == boost::next(paragraphs.begin())) {
2645 llt->selection.start.par(&*(paragraphs.begin()));
2646 llt->selection.start.pos(
2647 llt->selection.start.pos() + paragraphs.begin()->size());
2649 if (llt->selection.end.par() == boost::next(paragraphs.begin())) {
2650 llt->selection.end.par(&*(paragraphs.begin()));
2651 llt->selection.end.pos(
2652 llt->selection.end.pos() + paragraphs.begin()->size());
2655 mergeParagraph(bv->buffer()->params, paragraphs, paragraphs.begin());
2661 void InsetText::getDrawFont(LyXFont & font) const
2665 owner()->getDrawFont(font);
2669 void InsetText::appendParagraphs(Buffer * buffer,
2670 ParagraphList & plist)
2672 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2673 // And it probably does. You have to take a look at this John. (Lgb)
2674 #warning John, have a look here. (Lgb)
2675 BufferParams const & bparams = buffer->params;
2678 Paragraph * tmpbuf = newpar;
2679 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2680 if (bparams.tracking_changes)
2681 buf->cleanChanges();
2683 while (tmpbuf->next()) {
2684 tmpbuf = tmpbuf->next();
2685 lastbuffer->next(new Paragraph(*tmpbuf, false));
2686 lastbuffer->next()->previous(lastbuffer);
2687 lastbuffer = lastbuffer->next();
2688 if (bparams.tracking_changes)
2689 lastbuffer->cleanChanges();
2692 lastbuffer = &*(paragraphs.begin());
2693 while (lastbuffer->next())
2694 lastbuffer = lastbuffer->next();
2695 if (!newpar->empty() && !lastbuffer->empty() &&
2696 !lastbuffer->isSeparator(lastbuffer->size() - 1))
2698 lastbuffer->insertChar(lastbuffer->size(), ' ');
2701 // make the buf exactly the same layout than our last paragraph
2702 buf->makeSameLayout(lastbuffer);
2705 lastbuffer->next(buf);
2706 buf->previous(lastbuffer);
2707 mergeParagraph(buffer->params, paragraphs, lastbuffer);
2709 ParagraphList::iterator pit = plist.begin();
2710 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(),
2711 new Paragraph(*pit, false));
2713 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2715 ParagraphList::iterator pend = plist.end();
2716 for (; pit != pend; ++pit) {
2717 paragraphs.push_back(new Paragraph(*pit, false));
2726 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2728 ParagraphList::iterator pit = paragraphs.begin();
2729 ParagraphList::iterator pend = paragraphs.end();
2731 for (; pit != pend; ++pit) {
2732 InsetList::iterator it = pit->insetlist.begin();
2733 InsetList::iterator end = pit->insetlist.end();
2734 for (; it != end; ++it) {
2735 it.getInset()->addPreview(loader);