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"
42 #include "metricsinfo.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 = const_cast<ParagraphList&>(paragraphs).begin();
83 ParagraphList::iterator end = const_cast<ParagraphList&>(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();
102 sstate.lpar = const_cast<ParagraphList&>(paragraphs).end();
107 void InsetText::restoreLyXTextState(LyXText * t) const
109 if (sstate.lpar == const_cast<ParagraphList&>(paragraphs).end())
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(Paragraph());
143 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
144 if (bp.tracking_changes)
145 paragraphs.begin()->trackChanges();
150 InsetText::InsetText(InsetText const & in)
151 : UpdatableInset(in), lt(0), in_update(false), do_resize(0),
158 InsetText & InsetText::operator=(InsetText const & it)
165 void InsetText::init(InsetText const * ins)
168 setParagraphData(ins->paragraphs);
169 autoBreakRows = ins->autoBreakRows;
170 drawFrame_ = ins->drawFrame_;
171 frame_color = ins->frame_color;
173 for_each(paragraphs.begin(), paragraphs.end(),
174 boost::bind(&Paragraph::setInsetOwner, _1, this));
176 the_locking_inset = 0;
178 frame_color = LColor::insetframe;
179 autoBreakRows = false;
188 old_par = paragraphs.end();
189 last_drawn_width = -1;
191 sstate.lpar = paragraphs.end();
192 in_insetAllowed = false;
196 void InsetText::clear(bool just_mark_erased)
198 if (just_mark_erased) {
199 ParagraphList::iterator it = paragraphs.begin();
200 ParagraphList::iterator end = paragraphs.end();
201 for (; it != end; ++it) {
208 // This is a gross hack...
209 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
212 paragraphs.push_back(Paragraph());
213 paragraphs.begin()->setInsetOwner(this);
214 paragraphs.begin()->layout(old_layout);
221 InsetBase * InsetText::clone() const
223 return new InsetText(*this);
227 void InsetText::write(Buffer const * buf, ostream & os) const
230 writeParagraphData(buf, os);
234 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
236 ParagraphList::const_iterator it = paragraphs.begin();
237 ParagraphList::const_iterator end = paragraphs.end();
238 Paragraph::depth_type dth = 0;
239 for (; it != end; ++it) {
240 it->write(buf, os, buf->params, dth);
245 void InsetText::read(Buffer const * buf, LyXLex & lex)
248 Paragraph::depth_type depth = 0;
252 if (buf->params.tracking_changes)
253 paragraphs.begin()->trackChanges();
255 // delete the initial paragraph
257 ParagraphList::iterator pit = paragraphs.begin();
261 token = lex.getString();
264 if (token == "\\end_inset") {
268 if (token == "\\the_end") {
269 lex.printError("\\the_end read in inset! Error in document!");
274 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
277 pit = paragraphs.begin();
278 ParagraphList::iterator const end = paragraphs.end();
279 for (; pit != end; ++pit)
280 pit->setInsetOwner(this);
282 if (token != "\\end_inset") {
283 lex.printError("Missing \\end_inset at this point. "
290 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
292 BufferView * bv = mi.base.bv;
293 LyXText * text = getLyXText(bv);
294 dim.asc = text->rows().begin()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
295 dim.des = text->height - dim.asc + TEXT_TO_INSET_OFFSET;
296 dim.wid = max(textWidth(bv), int(text->width)) + 2 * TEXT_TO_INSET_OFFSET;
297 dim.wid = max(dim.wid, 10);
302 int InsetText::textWidth(BufferView * bv, bool fordraw) const
304 int w = autoBreakRows ? getMaxWidth(bv, this) : -1;
307 return max(w - 2 * TEXT_TO_INSET_OFFSET,
308 (int)getLyXText(bv)->width);
313 return w - 2 * TEXT_TO_INSET_OFFSET;
317 void InsetText::draw(PainterInfo & pi, int x, int baseline) const
322 // update our idea of where we are. Clearly, we should
323 // not have to know this information.
327 int const start_x = x;
329 BufferView * bv = pi.base.bv;
330 Painter & pain = pi.pain;
332 // repaint the background if needed
333 if (backgroundColor() != LColor::background)
334 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
336 // no draw is necessary !!!
337 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
338 top_baseline = baseline;
346 top_baseline = baseline;
347 top_y = baseline - dim_.asc;
349 if (last_drawn_width != dim_.wid) {
351 last_drawn_width = dim_.wid;
354 if (the_locking_inset && (cpar(bv) == inset_par)
355 && (cpos(bv) == inset_pos)) {
356 inset_x = cix(bv) - int(x) + drawTextXOffset;
357 inset_y = ciy(bv) + drawTextYOffset;
365 x += TEXT_TO_INSET_OFFSET;
367 RowList::iterator rowit = lt->rows().begin();
368 RowList::iterator end = lt->rows().end();
370 int y_offset = baseline - rowit->ascent_of_text();
371 int ph = pain.paperHeight();
374 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
375 y += rowit->height();
376 first += rowit->height();
380 lt->top_y(-y_offset);
388 int yf = y_offset + first;
393 while ((rowit != end) && (yf < ph)) {
394 RowPainter rp(*bv, *lt, rowit);
395 rp.paint(y + y_offset + first, int(x), y + lt->top_y());
396 y += rowit->height();
397 yf += rowit->height();
403 if ((drawFrame_ == ALWAYS) || (drawFrame_ == LOCKED && locked)) {
404 drawFrame(pain, int(start_x));
407 x += dim_.wid - TEXT_TO_INSET_OFFSET;
409 if (need_update != INIT) {
417 void InsetText::drawFrame(Painter & pain, int x) const
419 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
421 frame_y = top_baseline - dim_.asc + ttoD2;
422 frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
423 frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
424 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
429 void InsetText::update(BufferView * bv, bool reinit)
432 if (reinit && owner()) {
434 owner()->update(bv, true);
440 if (reinit || need_update == INIT) {
442 // we should put this call where we set need_update to INIT!
445 owner()->update(bv, true);
448 int nw = getMaxWidth(bv, this);
449 if (nw > 0 && old_max_width != nw) {
456 if (!autoBreakRows &&
457 boost::next(paragraphs.begin()) != paragraphs.end())
458 collapseParagraphs(bv);
460 if (the_locking_inset) {
461 inset_x = cix(bv) - top_x + drawTextXOffset;
462 inset_y = ciy(bv) + drawTextYOffset;
463 the_locking_inset->update(bv, reinit);
471 if ((need_update & CURSOR_PAR) && (lt->refreshStatus() == LyXText::REFRESH_NONE) &&
473 lt->updateInset(the_locking_inset);
476 if (lt->refreshStatus() == LyXText::REFRESH_AREA)
483 int nw = getMaxWidth(bv, this);
484 if (nw > 0 && old_max_width != nw) {
491 void InsetText::setUpdateStatus(BufferView * bv, int what) const
493 // this does nothing dangerous so use only a localized buffer
494 LyXText * llt = getLyXText(bv);
497 // we have to redraw us full if our LyXText REFRESH_AREA or
498 // if we don't break row so that we only have one row to update!
499 if ((llt->refreshStatus() == LyXText::REFRESH_AREA) ||
501 (llt->refreshStatus() == LyXText::REFRESH_ROW)))
504 } else if (llt->refreshStatus() == LyXText::REFRESH_ROW) {
505 need_update |= CURSOR_PAR;
508 // this to not draw a selection when we redraw all of it!
509 if (need_update & CURSOR && !(need_update & SELECTION)) {
510 if (llt->selection.set())
512 llt->clearSelection();
517 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
520 if (!autoBreakRows &&
521 boost::next(paragraphs.begin()) != paragraphs.end())
522 collapseParagraphs(bv);
524 if (!autoBreakRows && paragraphs.size() > 1)
525 collapseParagraphs(bv);
534 setUpdateStatus(bv, what);
535 bool flag = mark_dirty ||
536 (((need_update != CURSOR) && (need_update != NONE)) ||
537 (lt->refreshStatus() != LyXText::REFRESH_NONE) || lt->selection.set());
538 if (!lt->selection.set())
539 lt->selection.cursor = lt->cursor;
545 bv->updateInset(const_cast<InsetText *>(this));
551 if (need_update == CURSOR)
553 bv->owner()->view_state_changed();
554 bv->owner()->updateMenubar();
555 bv->owner()->updateToolbar();
556 if (old_par != cpar(bv)) {
557 bv->owner()->setLayout(cpar(bv)->layout()->name());
563 string const InsetText::editMessage() const
565 return _("Opened Text Inset");
569 void InsetText::insetUnlock(BufferView * bv)
571 if (the_locking_inset) {
572 the_locking_inset->insetUnlock(bv);
573 the_locking_inset = 0;
574 updateLocal(bv, CURSOR_PAR, false);
585 if (lt->selection.set()) {
586 lt->clearSelection();
588 } else if (owner()) {
589 bv->owner()->setLayout(owner()->getLyXText(bv)
590 ->cursor.par()->layout()->name());
592 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
593 // hack for deleteEmptyParMech
594 if (!paragraphs.begin()->empty()) {
595 lt->setCursor(paragraphs.begin(), 0);
596 } else if (boost::next(paragraphs.begin()) != paragraphs.end()) {
597 lt->setCursor(boost::next(paragraphs.begin()), 0);
602 updateLocal(bv, code, false);
605 setUpdateStatus(bv, code);
610 void InsetText::lockInset(BufferView * bv)
613 the_locking_inset = 0;
614 inset_pos = inset_x = inset_y = 0;
615 inset_boundary = false;
616 inset_par = paragraphs.end();
617 old_par = paragraphs.end();
623 lt->setCursor(paragraphs.begin(), 0);
624 lt->clearSelection();
626 // If the inset is empty set the language of the current font to the
627 // language to the surronding text (if different).
628 if (paragraphs.begin()->empty() &&
629 boost::next(paragraphs.begin()) == paragraphs.end() &&
630 bv->getParentLanguage(this) != lt->current_font.language()) {
631 LyXFont font(LyXFont::ALL_IGNORE);
632 font.setLanguage(bv->getParentLanguage(this));
633 setFont(bv, font, false);
638 if (drawFrame_ == LOCKED)
639 code = CURSOR|DRAW_FRAME;
640 setUpdateStatus(bv, code);
644 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
646 the_locking_inset = inset;
647 inset_x = cix(bv) - top_x + drawTextXOffset;
648 inset_y = ciy(bv) + drawTextYOffset;
649 inset_pos = cpos(bv);
650 inset_par = cpar(bv);
651 inset_boundary = cboundary(bv);
652 updateLocal(bv, CURSOR, false);
656 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
658 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
662 if (!the_locking_inset) {
663 ParagraphList::iterator pit = paragraphs.begin();
664 ParagraphList::iterator pend = paragraphs.end();
666 int const id = inset->id();
667 for (; pit != pend; ++pit) {
668 InsetList::iterator it =
669 pit->insetlist.begin();
670 InsetList::iterator const end =
671 pit->insetlist.end();
672 for (; it != end; ++it) {
673 if (it->inset == inset) {
674 getLyXText(bv)->setCursorIntern(pit, it->pos);
675 lockInset(bv, inset);
678 if (it->inset->getInsetFromID(id)) {
679 getLyXText(bv)->setCursorIntern(pit, it->pos);
680 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
681 return the_locking_inset->lockInsetInInset(bv, inset);
687 if (inset == cpar(bv)->getInset(cpos(bv))) {
688 lyxerr[Debug::INSETS] << "OK" << endl;
689 lockInset(bv, inset);
691 } else if (the_locking_inset && (the_locking_inset == inset)) {
692 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
693 lyxerr[Debug::INSETS] << "OK" << endl;
694 inset_x = cix(bv) - top_x + drawTextXOffset;
695 inset_y = ciy(bv) + drawTextYOffset;
697 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
699 } else if (the_locking_inset) {
700 lyxerr[Debug::INSETS] << "MAYBE" << endl;
701 return the_locking_inset->lockInsetInInset(bv, inset);
703 lyxerr[Debug::INSETS] << "NOT OK" << endl;
708 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
711 if (!the_locking_inset)
713 if (the_locking_inset == inset) {
714 the_locking_inset->insetUnlock(bv);
715 getLyXText(bv)->updateInset(inset);
716 the_locking_inset = 0;
718 moveRightIntern(bv, true, false);
719 old_par = paragraphs.end(); // force layout setting
723 updateLocal(bv, CURSOR, false);
726 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
730 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
732 if (!autoBreakRows &&
733 boost::next(paragraphs.begin()) != paragraphs.end())
734 collapseParagraphs(bv);
742 if (inset->owner() != this) {
743 int ustat = CURSOR_PAR;
745 UpdatableInset * tl_inset = the_locking_inset;
747 found = tl_inset->updateInsetInInset(bv, inset);
749 tl_inset = static_cast<UpdatableInset *>(inset);
750 while(tl_inset->owner() && tl_inset->owner() != this)
751 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
752 if (!tl_inset->owner())
754 found = tl_inset->updateInsetInInset(bv, inset);
758 lt->updateInset(tl_inset);
762 setUpdateStatus(bv, ustat);
765 bool found = lt->updateInset(inset);
769 setUpdateStatus(bv, CURSOR_PAR);
770 if (the_locking_inset &&
771 cpar(bv) == inset_par && cpos(bv) == inset_pos)
773 inset_x = cix(bv) - top_x + drawTextXOffset;
774 inset_y = ciy(bv) + drawTextYOffset;
781 void InsetText::lfunMousePress(FuncRequest const & cmd)
785 // use this to check mouse motion for selection!
789 BufferView * bv = cmd.view();
790 FuncRequest cmd1 = cmd;
796 int tmp_x = cmd.x - drawTextXOffset;
797 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
798 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
800 if (the_locking_inset) {
801 if (the_locking_inset == inset) {
802 the_locking_inset->localDispatch(cmd1);
805 // otherwise only unlock the_locking_inset
806 the_locking_inset->insetUnlock(bv);
807 the_locking_inset = 0;
810 no_selection = false;
812 if (bv->theLockingInset()) {
813 if (isHighlyEditableInset(inset)) {
814 // We just have to lock the inset before calling a
816 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
817 if (!bv->lockInset(uinset)) {
818 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
820 inset->localDispatch(cmd1);
821 if (the_locking_inset)
822 updateLocal(bv, CURSOR, false);
827 bool paste_internally = false;
828 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
829 localDispatch(FuncRequest(bv, LFUN_COPY));
830 paste_internally = true;
837 int old_top_y = lt->top_y();
839 lt->setCursorFromCoordinates(cmd.x - drawTextXOffset,
841 // set the selection cursor!
842 lt->selection.cursor = lt->cursor;
843 lt->cursor.x_fix(lt->cursor.x());
845 if (lt->selection.set()) {
846 lt->clearSelection();
847 updateLocal(bv, FULL, false);
849 lt->clearSelection();
850 updateLocal(bv, CURSOR, false);
856 bv->owner()->setLayout(cpar(bv)->layout()->name());
858 // we moved the view we cannot do mouse selection in this case!
859 if (getLyXText(bv)->top_y() != old_top_y)
862 // Insert primary selection with middle mouse
863 // if there is a local selection in the current buffer,
865 if (cmd.button() == mouse_button::button2) {
866 if (paste_internally)
867 localDispatch(FuncRequest(bv, LFUN_PASTE));
869 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
872 getLyXText(bv)->clearSelection();
877 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
879 BufferView * bv = cmd.view();
880 FuncRequest cmd1 = cmd;
885 if (the_locking_inset)
886 return the_locking_inset->localDispatch(cmd1);
888 int tmp_x = cmd.x - drawTextXOffset;
889 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
890 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
893 // This code should probably be removed now. Simple insets
894 // (!highlyEditable) can actually take the localDispatch,
895 // and turn it into edit() if necessary. But we still
896 // need to deal properly with the whole relative vs.
897 // absolute mouse co-ords thing in a realiable, sensible way
899 if (isHighlyEditableInset(inset))
900 ret = inset->localDispatch(cmd1);
902 inset_x = cix(bv) - top_x + drawTextXOffset;
903 inset_y = ciy(bv) + drawTextYOffset;
904 cmd1.x = cmd.x - inset_x;
905 cmd1.y = cmd.x - inset_y;
906 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
910 ret = inset->localDispatch(cmd1);
911 updateLocal(bv, CURSOR_PAR, false);
918 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
920 FuncRequest cmd1 = cmd;
924 if (the_locking_inset) {
925 the_locking_inset->localDispatch(cmd1);
929 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
932 BufferView * bv = cmd.view();
938 LyXCursor cur = lt->cursor;
939 lt->setCursorFromCoordinates
940 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
941 lt->cursor.x_fix(lt->cursor.x());
942 if (cur == lt->cursor) {
948 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
949 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
953 updateLocal(bv, SELECTION, false);
958 Inset::RESULT InsetText::localDispatch(FuncRequest const & cmd)
960 BufferView * bv = cmd.view();
962 if (cmd.action == LFUN_INSET_EDIT) {
963 UpdatableInset::localDispatch(cmd);
965 if (!bv->lockInset(this)) {
966 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
971 the_locking_inset = 0;
972 inset_pos = inset_x = inset_y = 0;
973 inset_boundary = false;
974 inset_par = paragraphs.end();
975 old_par = paragraphs.end();
983 if (cmd.argument.size()) {
984 if (cmd.argument == "left")
985 lt->setCursor(paragraphs.begin(), 0);
987 ParagraphList::iterator it = paragraphs.begin();
988 ParagraphList::iterator end = paragraphs.end();
989 while (boost::next(it) != end)
991 // int const pos = (p->size() ? p->size()-1 : p->size());
992 lt->setCursor(it, it->size());
995 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
996 // we put here -1 and not button as now the button in the
997 // edit call should not be needed we will fix this in 1.3.x
998 // cycle hopefully (Jug 20020509)
999 // FIXME: GUII I've changed this to none: probably WRONG
1000 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
1001 lt->setCursorFromCoordinates(cmd.x - drawTextXOffset,
1003 lt->cursor.x_fix(lt->cursor.x());
1007 lt->clearSelection();
1010 // If the inset is empty set the language of the current font to the
1011 // language to the surronding text (if different).
1012 if (paragraphs.begin()->empty() &&
1013 boost::next(paragraphs.begin()) == paragraphs.end()&&
1014 bv->getParentLanguage(this) != lt->current_font.language())
1016 LyXFont font(LyXFont::ALL_IGNORE);
1017 font.setLanguage(bv->getParentLanguage(this));
1018 setFont(bv, font, false);
1024 if (drawFrame_ == LOCKED)
1025 code = CURSOR | DRAW_FRAME;
1027 updateLocal(bv, code, false);
1028 // Tell the paragraph dialog that we've entered an insettext.
1029 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1034 switch (cmd.action) {
1035 case LFUN_MOUSE_PRESS:
1036 lfunMousePress(cmd);
1038 case LFUN_MOUSE_MOTION:
1039 lfunMouseMotion(cmd);
1041 case LFUN_MOUSE_RELEASE:
1042 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
1047 bool was_empty = (paragraphs.begin()->empty() &&
1048 boost::next(paragraphs.begin()) == paragraphs.end());
1049 no_selection = false;
1050 RESULT result = UpdatableInset::localDispatch(cmd);
1051 if (result != UNDISPATCHED)
1054 result = DISPATCHED;
1055 if (cmd.action < 0 && cmd.argument.empty())
1058 if (the_locking_inset) {
1059 result = the_locking_inset->localDispatch(cmd);
1060 if (result == DISPATCHED_NOUPDATE)
1062 else if (result == DISPATCHED) {
1063 updateLocal(bv, CURSOR_PAR, false);
1065 } else if (result >= FINISHED) {
1067 case FINISHED_RIGHT:
1068 moveRightIntern(bv, false, false);
1069 result = DISPATCHED;
1072 if ((result = moveUp(bv)) >= FINISHED) {
1073 updateLocal(bv, CURSOR, false);
1074 bv->unlockInset(this);
1078 if ((result = moveDown(bv)) >= FINISHED) {
1079 updateLocal(bv, CURSOR, false);
1080 bv->unlockInset(this);
1084 result = DISPATCHED;
1087 the_locking_inset = 0;
1088 updateLocal(bv, CURSOR, false);
1089 // make sure status gets reset immediately
1090 bv->owner()->clearMessage();
1096 lt = getLyXText(bv);
1100 int updflag = false;
1102 // what type of update to do on a cursor movement
1103 int cursor_update = CURSOR;
1105 if (lt->selection.set())
1106 cursor_update = SELECTION;
1108 switch (cmd.action) {
1111 case LFUN_SELFINSERT:
1112 if (bv->buffer()->isReadonly()) {
1113 // setErrorMessage(N_("Document is read only"));
1116 if (!cmd.argument.empty()) {
1117 /* Automatically delete the currently selected
1118 * text and replace it with what is being
1119 * typed in now. Depends on lyxrc settings
1120 * "auto_region_delete", which defaults to
1123 // This should not be needed here and is also WRONG!
1124 setUndo(bv, Undo::INSERT, lt->cursor.par());
1127 if (lyxrc.auto_region_delete) {
1128 if (lt->selection.set()) {
1129 lt->cutSelection(false, false);
1132 lt->clearSelection();
1133 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
1134 bv->owner()->getIntl().getTransManager().
1135 TranslateAndInsert(cmd.argument[i], lt);
1138 lt->selection.cursor = lt->cursor;
1139 updwhat = CURSOR | CURSOR_PAR;
1141 result = DISPATCHED_NOUPDATE;
1144 // cursor movements that need special handling
1147 result = moveRight(bv);
1149 updwhat = cursor_update;
1153 result = moveLeft(bv);
1154 updwhat = cursor_update;
1158 result = moveDown(bv);
1159 updwhat = cursor_update;
1163 result = moveUp(bv);
1164 updwhat = cursor_update;
1168 if (crow(bv) == lt->rows().begin())
1169 result = FINISHED_UP;
1171 lt->cursorPrevious();
1172 lt->clearSelection();
1173 result = DISPATCHED_NOUPDATE;
1175 updwhat = cursor_update;
1179 if (boost::next(crow(bv)) == lt->rows().end())
1180 result = FINISHED_DOWN;
1183 lt->clearSelection();
1184 result = DISPATCHED_NOUPDATE;
1186 updwhat = cursor_update;
1189 case LFUN_BACKSPACE: {
1190 if (lt->selection.set())
1191 lt->cutSelection(true, false);
1194 updwhat = CURSOR_PAR;
1200 if (lt->selection.set()) {
1201 lt->cutSelection(true, false);
1205 updwhat = CURSOR_PAR;
1211 lt->cutSelection(true, true);
1212 updwhat = CURSOR_PAR;
1219 lt->copySelection();
1220 updwhat = CURSOR_PAR;
1223 case LFUN_PASTESELECTION:
1225 string const clip(bv->getClipboard());
1229 if (cmd.argument == "paragraph") {
1230 lt->insertStringAsParagraphs(clip);
1232 lt->insertStringAsLines(clip);
1235 lt->clearSelection();
1237 updwhat = CURSOR_PAR;
1243 if (!autoBreakRows) {
1244 if (CutAndPaste::nrOfParagraphs() > 1) {
1245 #ifdef WITH_WARNINGS
1246 #warning FIXME horrendously bad UI
1248 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1253 size_t sel_index = 0;
1254 string const & arg = cmd.argument;
1255 if (isStrUnsignedInt(arg)) {
1256 size_t const paste_arg = strToUnsignedInt(arg);
1257 #warning FIXME Check if the arg is in the domain of available selections.
1258 sel_index = paste_arg;
1260 lt->pasteSelection(sel_index);
1262 lt->clearSelection();
1263 updwhat = CURSOR_PAR;
1268 case LFUN_BREAKPARAGRAPH:
1269 if (!autoBreakRows) {
1270 result = DISPATCHED;
1273 lt->breakParagraph(paragraphs, 0);
1274 updwhat = CURSOR | FULL;
1278 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1279 if (!autoBreakRows) {
1280 result = DISPATCHED;
1283 lt->breakParagraph(paragraphs, 1);
1284 updwhat = CURSOR | FULL;
1288 case LFUN_BREAKLINE: {
1289 if (!autoBreakRows) {
1290 result = DISPATCHED;
1294 lt->insertInset(new InsetNewline);
1295 updwhat = CURSOR | CURSOR_PAR;
1301 // do not set layouts on non breakable textinsets
1302 if (autoBreakRows) {
1303 string cur_layout = cpar(bv)->layout()->name();
1305 // Derive layout number from given argument (string)
1306 // and current buffer's textclass (number). */
1307 LyXTextClass const & tclass =
1308 bv->buffer()->params.getLyXTextClass();
1309 string layout = cmd.argument;
1310 bool hasLayout = tclass.hasLayout(layout);
1312 // If the entry is obsolete, use the new one instead.
1314 string const & obs =
1315 tclass[layout]->obsoleted_by();
1320 // see if we found the layout number:
1322 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1323 bv->owner()->dispatch(lf);
1327 if (cur_layout != layout) {
1328 cur_layout = layout;
1329 lt->setLayout(layout);
1330 bv->owner()->setLayout(cpar(bv)->layout()->name());
1331 updwhat = CURSOR_PAR;
1335 // reset the layout box
1336 bv->owner()->setLayout(cpar(bv)->layout()->name());
1339 case LFUN_PARAGRAPH_SPACING:
1340 // This one is absolutely not working. When fiddling with this
1341 // it also seems to me that the paragraphs inside the insettext
1342 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1343 // FIXME: how old is this comment ? ...
1345 ParagraphList::iterator pit = lt->cursor.par();
1346 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1347 float cur_value = 1.0;
1348 if (cur_spacing == Spacing::Other) {
1349 cur_value = pit->params().spacing().getValue();
1352 istringstream istr(STRCONV(cmd.argument));
1355 Spacing::Space new_spacing = cur_spacing;
1356 float new_value = cur_value;
1358 lyxerr << "Missing argument to `paragraph-spacing'"
1360 } else if (tmp == "single") {
1361 new_spacing = Spacing::Single;
1362 } else if (tmp == "onehalf") {
1363 new_spacing = Spacing::Onehalf;
1364 } else if (tmp == "double") {
1365 new_spacing = Spacing::Double;
1366 } else if (tmp == "other") {
1367 new_spacing = Spacing::Other;
1370 lyxerr << "new_value = " << tmpval << endl;
1373 } else if (tmp == "default") {
1374 new_spacing = Spacing::Default;
1376 lyxerr << _("Unknown spacing argument: ")
1377 << cmd.argument << endl;
1379 if (cur_spacing != new_spacing || cur_value != new_value) {
1380 pit->params().spacing(Spacing(new_spacing, new_value));
1381 updwhat = CURSOR_PAR;
1387 // These need to do repaints but don't require
1388 // special handling otherwise. A *lot* of the
1389 // above could probably be done similarly ...
1394 case LFUN_WORDRIGHT:
1395 // these two are really unhandled ...
1397 case LFUN_BEGINNINGBUF:
1398 updwhat = cursor_update;
1399 if (!bv->dispatch(cmd))
1400 result = UNDISPATCHED;
1409 case LFUN_WORDLEFTSEL:
1410 case LFUN_WORDRIGHTSEL:
1411 updwhat = SELECTION;
1416 if (!bv->dispatch(cmd))
1417 result = UNDISPATCHED;
1424 updateLocal(bv, updwhat, updflag);
1425 /// If the action has deleted all text in the inset, we need to change the
1426 // language to the language of the surronding text.
1427 if (!was_empty && paragraphs.begin()->empty() &&
1428 boost::next(paragraphs.begin()) == paragraphs.end()) {
1429 LyXFont font(LyXFont::ALL_IGNORE);
1430 font.setLanguage(bv->getParentLanguage(this));
1431 setFont(bv, font, false);
1434 if (result >= FINISHED)
1435 bv->unlockInset(this);
1437 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1438 result = DISPATCHED;
1443 int InsetText::latex(Buffer const * buf, ostream & os,
1444 LatexRunParams const & runparams) const
1447 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1448 return texrow.rows();
1452 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1454 unsigned int lines = 0;
1456 ParagraphList::const_iterator beg = paragraphs.begin();
1457 ParagraphList::const_iterator end = paragraphs.end();
1458 ParagraphList::const_iterator it = beg;
1459 for (; it != end; ++it) {
1460 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1461 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1468 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1470 unsigned int lines = 0;
1472 vector<string> environment_stack(10);
1473 vector<string> environment_inner(10);
1475 int const command_depth = 0;
1478 Paragraph::depth_type depth = 0; // paragraph depth
1480 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1481 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1483 for (; pit != pend; ++pit) {
1485 int desc_on = 0; // description mode
1487 LyXLayout_ptr const & style = pit->layout();
1489 // environment tag closing
1490 for (; depth > pit->params().depth(); --depth) {
1491 if (environment_inner[depth] != "!-- --") {
1492 item_name = "listitem";
1493 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1494 if (environment_inner[depth] == "varlistentry")
1495 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1497 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1498 environment_stack[depth].erase();
1499 environment_inner[depth].erase();
1502 if (depth == pit->params().depth()
1503 && environment_stack[depth] != style->latexname()
1504 && !environment_stack[depth].empty()) {
1505 if (environment_inner[depth] != "!-- --") {
1506 item_name= "listitem";
1507 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1508 if (environment_inner[depth] == "varlistentry")
1509 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1512 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1514 environment_stack[depth].erase();
1515 environment_inner[depth].erase();
1518 // Write opening SGML tags.
1519 switch (style->latextype) {
1520 case LATEX_PARAGRAPH:
1521 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1525 buf->sgmlError(pit, 0, _("Error: LatexType Command not allowed here.\n"));
1529 case LATEX_ENVIRONMENT:
1530 case LATEX_ITEM_ENVIRONMENT:
1531 if (depth < pit->params().depth()) {
1532 depth = pit->params().depth();
1533 environment_stack[depth].erase();
1536 if (environment_stack[depth] != style->latexname()) {
1537 if (environment_stack.size() == depth + 1) {
1538 environment_stack.push_back("!-- --");
1539 environment_inner.push_back("!-- --");
1541 environment_stack[depth] = style->latexname();
1542 environment_inner[depth] = "!-- --";
1543 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1545 if (environment_inner[depth] != "!-- --") {
1546 item_name= "listitem";
1547 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1548 if (environment_inner[depth] == "varlistentry")
1549 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1553 if (style->latextype == LATEX_ENVIRONMENT) {
1554 if (!style->latexparam().empty()) {
1555 if (style->latexparam() == "CDATA")
1558 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1563 desc_on = (style->labeltype == LABEL_MANUAL);
1565 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1566 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1568 item_name = desc_on ? "term" : "para";
1569 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1573 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1577 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1580 // write closing SGML tags
1581 switch (style->latextype) {
1582 case LATEX_ENVIRONMENT:
1583 if (!style->latexparam().empty()) {
1584 if (style->latexparam() == "CDATA")
1587 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1590 case LATEX_ITEM_ENVIRONMENT:
1591 if (desc_on == 1) break;
1593 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1595 case LATEX_PARAGRAPH:
1596 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1599 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1605 for (int d = depth; d >= 0; --d) {
1606 if (!environment_stack[depth].empty()) {
1607 if (environment_inner[depth] != "!-- --") {
1608 item_name = "listitem";
1609 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1610 if (environment_inner[depth] == "varlistentry")
1611 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1614 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1622 void InsetText::validate(LaTeXFeatures & features) const
1624 for_each(paragraphs.begin(), paragraphs.end(),
1625 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1629 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1631 if (the_locking_inset) {
1632 the_locking_inset->getCursor(bv, x, y);
1636 y = cy(&bv) + InsetText::y();
1640 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1642 if (the_locking_inset) {
1643 the_locking_inset->getCursorPos(bv, x, y);
1646 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1647 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1651 int InsetText::insetInInsetY() const
1653 if (!the_locking_inset)
1656 return (inset_y + the_locking_inset->insetInInsetY());
1660 void InsetText::fitInsetCursor(BufferView * bv) const
1662 if (the_locking_inset) {
1663 the_locking_inset->fitInsetCursor(bv);
1666 LyXFont const font =
1667 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1669 int const asc = font_metrics::maxAscent(font);
1670 int const desc = font_metrics::maxDescent(font);
1672 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1673 need_update |= FULL;
1678 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1680 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1681 return moveLeftIntern(bv, false, activate_inset, selecting);
1683 return moveRightIntern(bv, true, activate_inset, selecting);
1688 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1690 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1691 return moveRightIntern(bv, true, activate_inset, selecting);
1693 return moveLeftIntern(bv, false, activate_inset, selecting);
1698 InsetText::moveRightIntern(BufferView * bv, bool front,
1699 bool activate_inset, bool selecting)
1701 LyXText * text = getLyXText(bv);
1703 ParagraphList::iterator c_par = cpar(bv);
1705 if (boost::next(c_par) == paragraphs.end() &&
1706 (cpos(bv) >= c_par->size()))
1707 return FINISHED_RIGHT;
1708 if (activate_inset && checkAndActivateInset(bv, front))
1710 text->cursorRight(bv);
1712 text->clearSelection();
1713 return DISPATCHED_NOUPDATE;
1718 InsetText::moveLeftIntern(BufferView * bv, bool front,
1719 bool activate_inset, bool selecting)
1721 LyXText * text = getLyXText(bv);
1723 if (cpar(bv) == paragraphs.begin() && (cpos(bv) <= 0))
1725 text->cursorLeft(bv);
1727 text->clearSelection();
1728 if (activate_inset && checkAndActivateInset(bv, front))
1730 return DISPATCHED_NOUPDATE;
1734 Inset::RESULT InsetText::moveUp(BufferView * bv)
1736 if (crow(bv) == getLyXText(bv)->rows().begin())
1738 getLyXText(bv)->cursorUp(bv);
1739 getLyXText(bv)->clearSelection();
1740 return DISPATCHED_NOUPDATE;
1744 Inset::RESULT InsetText::moveDown(BufferView * bv)
1746 if (boost::next(crow(bv)) == getLyXText(bv)->rows().end())
1747 return FINISHED_DOWN;
1748 getLyXText(bv)->cursorDown(bv);
1749 getLyXText(bv)->clearSelection();
1750 return DISPATCHED_NOUPDATE;
1754 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1756 if (the_locking_inset) {
1757 if (the_locking_inset->insetAllowed(inset))
1758 return the_locking_inset->insertInset(bv, inset);
1761 inset->setOwner(this);
1762 getLyXText(bv)->insertInset(inset);
1764 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1769 bool InsetText::insetAllowed(Inset::Code code) const
1771 // in_insetAllowed is a really gross hack,
1772 // to allow us to call the owner's insetAllowed
1773 // without stack overflow, which can happen
1774 // when the owner uses InsetCollapsable::insetAllowed()
1776 if (in_insetAllowed)
1778 in_insetAllowed = true;
1779 if (the_locking_inset)
1780 ret = the_locking_inset->insetAllowed(code);
1782 ret = owner()->insetAllowed(code);
1783 in_insetAllowed = false;
1788 UpdatableInset * InsetText::getLockingInset() const
1790 return the_locking_inset ? the_locking_inset->getLockingInset() :
1791 const_cast<InsetText *>(this);
1795 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1799 if (the_locking_inset)
1800 return the_locking_inset->getFirstLockingInsetOfType(c);
1805 bool InsetText::showInsetDialog(BufferView * bv) const
1807 if (the_locking_inset)
1808 return the_locking_inset->showInsetDialog(bv);
1813 void InsetText::getLabelList(std::vector<string> & list) const
1815 ParagraphList::const_iterator pit = paragraphs.begin();
1816 ParagraphList::const_iterator pend = paragraphs.end();
1817 for (; pit != pend; ++pit) {
1818 InsetList::const_iterator beg = pit->insetlist.begin();
1819 InsetList::const_iterator end = pit->insetlist.end();
1820 for (; beg != end; ++beg)
1821 beg->inset->getLabelList(list);
1826 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1829 if (the_locking_inset) {
1830 the_locking_inset->setFont(bv, font, toggleall, selectall);
1833 if ((boost::next(paragraphs.begin()) == paragraphs.end() &&
1834 paragraphs.begin()->empty()) || cpar(bv)->empty()) {
1835 getLyXText(bv)->setFont(font, toggleall);
1840 lt = getLyXText(bv);
1843 if (lt->selection.set()) {
1844 setUndo(bv, Undo::EDIT, lt->cursor.par());
1848 lt->toggleFree(font, toggleall);
1850 lt->clearSelection();
1852 bool flag = (selectall || lt->selection.set());
1856 updateLocal(bv, FULL, true);
1858 updateLocal(bv, CURSOR_PAR, true);
1862 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1864 if (cpar(bv)->isInset(cpos(bv))) {
1866 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1867 if (!isHighlyEditableInset(inset))
1869 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1870 inset->localDispatch(cmd);
1871 if (!the_locking_inset)
1873 updateLocal(bv, CURSOR, false);
1880 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1881 mouse_button::state button)
1883 x -= drawTextXOffset;
1885 int dummyy = y + dim_.asc;
1886 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1887 // we only do the edit() call if the inset was hit by the mouse
1888 // or if it is a highly editable inset. So we should call this
1889 // function from our own edit with button < 0.
1890 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1892 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1900 inset_x = cix(bv) - top_x + drawTextXOffset;
1901 inset_y = ciy(bv) + drawTextYOffset;
1902 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1903 inset->localDispatch(cmd);
1904 if (!the_locking_inset)
1906 updateLocal(bv, CURSOR, false);
1913 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1916 int w = UpdatableInset::getMaxWidth(bv, inset);
1921 w = w - top_x + owner()->x();
1924 w -= (2 * TEXT_TO_INSET_OFFSET);
1927 return UpdatableInset::getMaxWidth(bv, inset);
1932 void InsetText::setParagraphData(ParagraphList const & plist)
1934 // we have to unlock any locked inset otherwise we're in troubles
1935 the_locking_inset = 0;
1937 // But it it makes no difference that is a lot better.
1939 // See if this can be simplified when std::list is in effect.
1942 ParagraphList::const_iterator it = plist.begin();
1943 ParagraphList::const_iterator end = plist.end();
1944 for (; it != end; ++it) {
1945 paragraphs.push_back(*it);
1946 Paragraph & tmp = paragraphs.back();
1947 tmp.setInsetOwner(this);
1955 void InsetText::markNew(bool track_changes)
1957 ParagraphList::iterator pit = paragraphs.begin();
1958 ParagraphList::iterator pend = paragraphs.end();
1959 for (; pit != pend; ++pit) {
1960 if (track_changes) {
1961 pit->trackChanges();
1963 // no-op when not tracking
1964 pit->cleanChanges();
1970 void InsetText::setText(string const & data, LyXFont const & font)
1973 for (unsigned int i = 0; i < data.length(); ++i)
1974 paragraphs.begin()->insertChar(i, data[i], font);
1979 void InsetText::setAutoBreakRows(bool flag)
1981 if (flag != autoBreakRows) {
1982 autoBreakRows = flag;
1990 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1992 if (how != drawFrame_) {
1995 updateLocal(bv, DRAW_FRAME, false);
2000 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2002 if (frame_color != col) {
2005 updateLocal(bv, DRAW_FRAME, false);
2010 int InsetText::cx(BufferView * bv) const
2012 // we do nothing dangerous so we use a local cache
2013 LyXText * llt = getLyXText(bv);
2014 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2015 if (the_locking_inset) {
2016 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2018 if (font.isVisibleRightToLeft())
2019 x -= the_locking_inset->width(bv, font);
2025 int InsetText::cix(BufferView * bv) const
2027 // we do nothing dangerous so we use a local cache
2028 LyXText * llt = getLyXText(bv);
2029 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2030 if (the_locking_inset) {
2031 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2033 if (font.isVisibleRightToLeft())
2034 x -= the_locking_inset->width(bv, font);
2040 int InsetText::cy(BufferView * bv) const
2043 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2047 int InsetText::ciy(BufferView * bv) const
2050 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2054 pos_type InsetText::cpos(BufferView * bv) const
2056 return getLyXText(bv)->cursor.pos();
2060 ParagraphList::iterator InsetText::cpar(BufferView * bv) const
2062 return getLyXText(bv)->cursor.par();
2066 bool InsetText::cboundary(BufferView * bv) const
2068 return getLyXText(bv)->cursor.boundary();
2072 RowList::iterator InsetText::crow(BufferView * bv) const
2074 return getLyXText(bv)->cursor.row();
2078 LyXText * InsetText::getLyXText(BufferView const * lbv,
2079 bool const recursive) const
2081 if (cached_bview == lbv) {
2082 if (recursive && the_locking_inset)
2083 return the_locking_inset->getLyXText(lbv, true);
2084 LyXText * lt = cached_text.get();
2085 lyx::Assert(lt && lt->rows().begin()->par() == const_cast<ParagraphList&>(paragraphs).begin());
2088 // Super UGLY! (Lgb)
2089 BufferView * bv = const_cast<BufferView *>(lbv);
2092 Cache::iterator it = cache.find(bv);
2094 if (it != cache.end()) {
2097 } else if (do_resize) {
2098 resizeLyXText(do_resize);
2100 if (lt || !it->second.remove) {
2101 lyx::Assert(it->second.text.get());
2102 cached_text = it->second.text;
2103 if (recursive && the_locking_inset) {
2104 return the_locking_inset->getLyXText(bv, true);
2106 return cached_text.get();
2107 } else if (it->second.remove) {
2109 saveLyXTextState(it->second.text.get());
2111 sstate.lpar = const_cast<ParagraphList&>(paragraphs).end();
2115 // when we have to reinit the existing LyXText!
2117 it->second.text->init(bv);
2118 restoreLyXTextState(it->second.text.get());
2119 it->second.remove = false;
2121 cached_text = it->second.text;
2122 if (the_locking_inset && recursive) {
2123 return the_locking_inset->getLyXText(bv);
2125 return cached_text.get();
2128 // we are here only if we don't have a BufferView * in the cache!!!
2130 cached_text.reset(new LyXText(bv, const_cast<InsetText *>(this)));
2131 cached_text->init(bv);
2132 restoreLyXTextState(cached_text.get());
2134 cache.insert(make_pair(bv, cached_text));
2136 if (the_locking_inset && recursive) {
2137 return the_locking_inset->getLyXText(bv);
2139 return cached_text.get();
2143 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2147 Cache::iterator it = cache.find(bv);
2149 if (it == cache.end()) {
2153 lyx::Assert(it->second.text.get());
2155 it->second.remove = true;
2157 /// then remove all LyXText in text-insets
2158 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2159 const_cast<ParagraphList&>(paragraphs).end(),
2160 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2165 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2168 // we cannot resize this because we are in use!
2169 // so do this on the next possible getLyXText()
2174 if (boost::next(paragraphs.begin()) == paragraphs.end() &&
2175 paragraphs.begin()->empty()) { // no data, resize not neccessary!
2176 // we have to do this as a fixed width may have changed!
2177 LyXText * t = getLyXText(bv);
2178 saveLyXTextState(t);
2180 restoreLyXTextState(t);
2183 // one endless line, resize normally not necessary
2184 if (!force && getMaxWidth(bv, this) < 0)
2187 Cache::iterator it = cache.find(bv);
2188 if (it == cache.end()) {
2191 lyx::Assert(it->second.text.get());
2193 LyXText * t = it->second.text.get();
2194 saveLyXTextState(t);
2196 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2197 const_cast<ParagraphList&>(paragraphs).end(),
2198 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2201 restoreLyXTextState(t);
2202 if (the_locking_inset) {
2203 inset_x = cix(bv) - top_x + drawTextXOffset;
2204 inset_y = ciy(bv) + drawTextYOffset;
2207 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2209 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2210 // this will scroll the screen such that the cursor becomes visible
2211 bv->updateScrollbar();
2213 need_update |= FULL;
2218 void InsetText::reinitLyXText() const
2221 // we cannot resize this because we are in use!
2222 // so do this on the next possible getLyXText()
2228 for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2229 lyx::Assert(it->second.text.get());
2231 LyXText * t = it->second.text.get();
2232 BufferView * bv = it->first;
2234 saveLyXTextState(t);
2236 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2237 const_cast<ParagraphList&>(paragraphs).end(),
2238 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2241 restoreLyXTextState(t);
2242 if (the_locking_inset) {
2243 inset_x = cix(bv) - top_x + drawTextXOffset;
2244 inset_y = ciy(bv) + drawTextYOffset;
2246 t->top_y(bv->screen().topCursorVisible(t->cursor, t->top_y()));
2248 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2249 // this will scroll the screen such that the cursor becomes visible
2250 bv->updateScrollbar();
2258 void InsetText::removeNewlines()
2260 bool changed = false;
2262 ParagraphList::iterator it = paragraphs.begin();
2263 ParagraphList::iterator end = paragraphs.end();
2264 for (; it != end; ++it) {
2265 for (int i = 0; i < it->size(); ++i) {
2266 if (it->isNewline(i)) {
2277 bool InsetText::nodraw() const
2279 if (the_locking_inset)
2280 return the_locking_inset->nodraw();
2281 return UpdatableInset::nodraw();
2285 int InsetText::scroll(bool recursive) const
2287 int sx = UpdatableInset::scroll(false);
2289 if (recursive && the_locking_inset)
2290 sx += the_locking_inset->scroll(recursive);
2296 void InsetText::selectAll(BufferView * bv)
2298 getLyXText(bv)->cursorTop();
2299 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2300 getLyXText(bv)->cursorBottom();
2301 getLyXText(bv)->setSelection();
2305 void InsetText::clearSelection(BufferView * bv)
2307 getLyXText(bv)->clearSelection();
2311 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2313 Painter & pain = bv->painter();
2315 int h = dim_.asc + dim_.des;
2316 int ty = baseline - dim_.asc;
2322 if ((ty + h) > pain.paperHeight())
2323 h = pain.paperHeight();
2324 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2325 w = pain.paperWidth();
2326 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2331 ParagraphList * InsetText::getParagraphs(int i) const
2333 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2337 LyXCursor const & InsetText::cursor(BufferView * bv) const
2339 if (the_locking_inset)
2340 return the_locking_inset->cursor(bv);
2341 return getLyXText(bv)->cursor;
2345 Inset * InsetText::getInsetFromID(int id_arg) const
2348 return const_cast<InsetText *>(this);
2350 ParagraphList::const_iterator pit = paragraphs.begin();
2351 ParagraphList::const_iterator pend = paragraphs.end();
2352 for (; pit != pend; ++pit) {
2353 InsetList::const_iterator it = pit->insetlist.begin();
2354 InsetList::const_iterator end = pit->insetlist.end();
2355 for (; it != end; ++it) {
2356 if (it->inset->id() == id_arg)
2358 Inset * in = it->inset->getInsetFromID(id_arg);
2368 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2369 float & value) const
2375 lt = getLyXText(bv);
2378 if (the_locking_inset) {
2379 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2380 if (!word.word().empty()) {
2386 // we have to go on checking so move cursor to the next char
2387 lt->cursor.pos(lt->cursor.pos() + 1);
2389 word = lt->selectNextWordToSpellcheck(value);
2390 if (word.word().empty())
2391 bv->unlockInset(const_cast<InsetText *>(this));
2400 void InsetText::selectSelectedWord(BufferView * bv)
2402 if (the_locking_inset) {
2403 the_locking_inset->selectSelectedWord(bv);
2406 getLyXText(bv)->selectSelectedWord();
2407 updateLocal(bv, SELECTION, false);
2411 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2413 if (the_locking_inset) {
2414 the_locking_inset->toggleSelection(bv, kill_selection);
2418 lt = getLyXText(bv);
2422 int x = top_x + TEXT_TO_INSET_OFFSET;
2424 RowList::iterator rowit = lt->rows().begin();
2425 RowList::iterator end = lt->rows().end();
2426 int y_offset = top_baseline - rowit->ascent_of_text();
2428 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2429 y += rowit->height();
2435 if (need_update & SELECTION)
2437 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2443 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2447 lt = getLyXText(bv);
2450 if (the_locking_inset) {
2451 if (the_locking_inset->nextChange(bv, length))
2453 lt->cursorRight(true);
2455 lyxfind::SearchResult result =
2456 lyxfind::findNextChange(bv, lt, length);
2458 if (result == lyxfind::SR_FOUND) {
2459 LyXCursor cur = lt->cursor;
2460 bv->unlockInset(bv->theLockingInset());
2461 if (bv->lockInset(this))
2464 lt->setSelectionRange(length);
2465 updateLocal(bv, SELECTION, false);
2469 return result != lyxfind::SR_NOT_FOUND;
2473 bool InsetText::searchForward(BufferView * bv, string const & str,
2478 lt = getLyXText(bv);
2481 if (the_locking_inset) {
2482 if (the_locking_inset->searchForward(bv, str, cs, mw))
2484 lt->cursorRight(true);
2486 lyxfind::SearchResult result =
2487 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2489 if (result == lyxfind::SR_FOUND) {
2490 LyXCursor cur = lt->cursor;
2491 bv->unlockInset(bv->theLockingInset());
2492 if (bv->lockInset(this))
2495 lt->setSelectionRange(str.length());
2496 updateLocal(bv, SELECTION, false);
2500 return (result != lyxfind::SR_NOT_FOUND);
2503 bool InsetText::searchBackward(BufferView * bv, string const & str,
2506 if (the_locking_inset) {
2507 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2512 lt = getLyXText(bv);
2516 ParagraphList::iterator pit = paragraphs.begin();
2517 ParagraphList::iterator pend = paragraphs.end();
2519 while (boost::next(pit) != pend)
2522 lt->setCursor(pit, pit->size());
2524 lyxfind::SearchResult result =
2525 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2527 if (result == lyxfind::SR_FOUND) {
2528 LyXCursor cur = lt->cursor;
2529 bv->unlockInset(bv->theLockingInset());
2530 if (bv->lockInset(this))
2533 lt->setSelectionRange(str.length());
2534 updateLocal(bv, SELECTION, false);
2538 return (result != lyxfind::SR_NOT_FOUND);
2542 bool InsetText::checkInsertChar(LyXFont & font)
2545 return owner()->checkInsertChar(font);
2550 void InsetText::collapseParagraphs(BufferView * bv)
2552 LyXText * llt = getLyXText(bv);
2554 while (boost::next(paragraphs.begin()) != paragraphs.end()) {
2555 if (!paragraphs.begin()->empty() &&
2556 !boost::next(paragraphs.begin())->empty() &&
2557 !paragraphs.begin()->isSeparator(paragraphs.begin()->size() - 1))
2559 paragraphs.begin()->insertChar(paragraphs.begin()->size(), ' ');
2561 if (llt->selection.set()) {
2562 if (llt->selection.start.par() == boost::next(paragraphs.begin())) {
2563 llt->selection.start.par(paragraphs.begin());
2564 llt->selection.start.pos(
2565 llt->selection.start.pos() + paragraphs.begin()->size());
2567 if (llt->selection.end.par() == boost::next(paragraphs.begin())) {
2568 llt->selection.end.par(paragraphs.begin());
2569 llt->selection.end.pos(
2570 llt->selection.end.pos() + paragraphs.begin()->size());
2573 mergeParagraph(bv->buffer()->params, paragraphs, paragraphs.begin());
2579 void InsetText::getDrawFont(LyXFont & font) const
2583 owner()->getDrawFont(font);
2587 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2589 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2590 // And it probably does. You have to take a look at this John. (Lgb)
2591 #warning John, have a look here. (Lgb)
2592 ParagraphList::iterator pit = plist.begin();
2593 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2595 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2597 ParagraphList::iterator pend = plist.end();
2598 for (; pit != pend; ++pit) {
2599 paragraphs.push_back(*pit);
2606 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2608 ParagraphList::const_iterator pit = paragraphs.begin();
2609 ParagraphList::const_iterator pend = paragraphs.end();
2611 for (; pit != pend; ++pit) {
2612 InsetList::const_iterator it = pit->insetlist.begin();
2613 InsetList::const_iterator end = pit->insetlist.end();
2614 for (; it != end; ++it) {
2615 it->inset->addPreview(loader);