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"
22 #include "errorlist.h"
24 #include "LaTeXFeatures.h"
28 #include "lyxcursor.h"
34 #include "paragraph.h"
35 #include "ParagraphParameters.h"
36 #include "trans_mgr.h"
37 #include "undo_funcs.h"
38 #include "WordLangTuple.h"
39 #include "paragraph_funcs.h"
41 #include "rowpainter.h"
42 #include "insetnewline.h"
43 #include "metricsinfo.h"
45 #include "frontends/Alert.h"
46 #include "frontends/Dialogs.h"
47 #include "frontends/font_metrics.h"
48 #include "frontends/LyXView.h"
49 #include "frontends/Painter.h"
50 #include "frontends/screen.h"
52 #include "support/textutils.h"
53 #include "support/LAssert.h"
54 #include "support/lstrings.h"
55 #include "support/lyxalgo.h" // lyx::count
57 #include <boost/bind.hpp>
74 using namespace lyx::support;
77 using lyx::textclass_type;
80 // These functions should probably go into bufferview_funcs somehow (Jug)
82 void InsetText::saveLyXTextState(LyXText * t) const
84 // check if my paragraphs are still valid
85 ParagraphList::iterator it = const_cast<ParagraphList&>(paragraphs).begin();
86 ParagraphList::iterator end = const_cast<ParagraphList&>(paragraphs).end();
87 for (; it != end; ++it) {
88 if (it == t->cursor.par())
92 if (it != end && t->cursor.pos() <= it->size())
93 sstate = *t; // slicing intended
95 sstate.cursor.par(end);
99 void InsetText::restoreLyXTextState(LyXText * t) const
101 if (sstate.cursor.par() == const_cast<ParagraphList&>(paragraphs).end())
104 t->selection.set(true);
105 // at this point just to avoid the DEPM when setting the cursor
106 t->selection.mark(sstate.selection.mark());
107 if (sstate.selection.set()) {
108 t->setCursor(sstate.selection.start.par(),
109 sstate.selection.start.pos(),
110 true, sstate.selection.start.boundary());
111 t->selection.cursor = t->cursor;
112 t->setCursor(sstate.selection.end.par(), sstate.selection.end.pos(),
113 true, sstate.selection.end.boundary());
115 t->setCursor(sstate.cursor.par(), sstate.cursor.pos());
117 t->setCursor(sstate.cursor.par(), sstate.cursor.pos(),
118 true, sstate.cursor.boundary());
119 t->selection.cursor = t->cursor;
120 t->selection.set(false);
125 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
132 InsetText::InsetText(BufferParams const & bp)
133 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
136 paragraphs.push_back(Paragraph());
137 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
138 if (bp.tracking_changes)
139 paragraphs.begin()->trackChanges();
144 InsetText::InsetText(InsetText const & in)
145 : UpdatableInset(in), lt(0), in_update(false), do_resize(0),
152 InsetText & InsetText::operator=(InsetText const & it)
159 void InsetText::init(InsetText const * ins)
162 setParagraphData(ins->paragraphs);
163 autoBreakRows = ins->autoBreakRows;
164 drawFrame_ = ins->drawFrame_;
165 frame_color = ins->frame_color;
167 for_each(paragraphs.begin(), paragraphs.end(),
168 boost::bind(&Paragraph::setInsetOwner, _1, this));
170 the_locking_inset = 0;
172 frame_color = LColor::insetframe;
173 autoBreakRows = false;
182 old_par = paragraphs.end();
183 last_drawn_width = -1;
185 sstate.cursor.par(paragraphs.end());
186 in_insetAllowed = false;
190 void InsetText::clear(bool just_mark_erased)
192 if (just_mark_erased) {
193 ParagraphList::iterator it = paragraphs.begin();
194 ParagraphList::iterator end = paragraphs.end();
195 for (; it != end; ++it) {
202 // This is a gross hack...
203 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
206 paragraphs.push_back(Paragraph());
207 paragraphs.begin()->setInsetOwner(this);
208 paragraphs.begin()->layout(old_layout);
215 InsetBase * InsetText::clone() const
217 return new InsetText(*this);
221 void InsetText::write(Buffer const * buf, ostream & os) const
224 writeParagraphData(buf, os);
228 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
230 ParagraphList::const_iterator it = paragraphs.begin();
231 ParagraphList::const_iterator end = paragraphs.end();
232 Paragraph::depth_type dth = 0;
233 for (; it != end; ++it) {
234 it->write(buf, os, buf->params, dth);
239 void InsetText::read(Buffer const * buf, LyXLex & lex)
242 Paragraph::depth_type depth = 0;
246 if (buf->params.tracking_changes)
247 paragraphs.begin()->trackChanges();
249 // delete the initial paragraph
251 ParagraphList::iterator pit = paragraphs.begin();
255 token = lex.getString();
258 if (token == "\\end_inset") {
262 if (token == "\\the_end") {
263 lex.printError("\\the_end read in inset! Error in document!");
268 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
271 pit = paragraphs.begin();
272 ParagraphList::iterator const end = paragraphs.end();
273 for (; pit != end; ++pit)
274 pit->setInsetOwner(this);
276 if (token != "\\end_inset") {
277 lex.printError("Missing \\end_inset at this point. "
284 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
286 BufferView * bv = mi.base.bv;
287 LyXText * text = getLyXText(bv);
288 dim.asc = text->rows().begin()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
289 dim.des = text->height - dim.asc + TEXT_TO_INSET_OFFSET;
290 dim.wid = max(textWidth(bv), int(text->width)) + 2 * TEXT_TO_INSET_OFFSET;
291 dim.wid = max(dim.wid, 10);
296 int InsetText::textWidth(BufferView * bv, bool fordraw) const
298 int w = autoBreakRows ? getMaxWidth(bv, this) : -1;
301 return max(w - 2 * TEXT_TO_INSET_OFFSET,
302 (int)getLyXText(bv)->width);
307 return w - 2 * TEXT_TO_INSET_OFFSET;
311 void InsetText::draw(PainterInfo & pi, int x, int baseline) const
316 // update our idea of where we are. Clearly, we should
317 // not have to know this information.
321 int const start_x = x;
323 BufferView * bv = pi.base.bv;
324 Painter & pain = pi.pain;
326 // repaint the background if needed
327 if (backgroundColor() != LColor::background)
328 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
330 // no draw is necessary !!!
331 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
332 top_baseline = baseline;
340 top_baseline = baseline;
341 top_y = baseline - dim_.asc;
343 if (last_drawn_width != dim_.wid) {
345 last_drawn_width = dim_.wid;
348 if (the_locking_inset && (cpar(bv) == inset_par)
349 && (cpos(bv) == inset_pos)) {
350 inset_x = cix(bv) - int(x) + drawTextXOffset;
351 inset_y = ciy(bv) + drawTextYOffset;
359 x += TEXT_TO_INSET_OFFSET;
361 RowList::iterator rowit = lt->rows().begin();
362 RowList::iterator end = lt->rows().end();
364 int y_offset = baseline - rowit->ascent_of_text();
365 int ph = pain.paperHeight();
368 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
369 y += rowit->height();
370 first += rowit->height();
374 lt->top_y(-y_offset);
382 int yf = y_offset + first;
387 while ((rowit != end) && (yf < ph)) {
388 RowPainter rp(*bv, *lt, rowit);
389 rp.paint(y + y_offset + first, int(x), y + lt->top_y());
390 y += rowit->height();
391 yf += rowit->height();
397 if ((drawFrame_ == ALWAYS) || (drawFrame_ == LOCKED && locked)) {
398 drawFrame(pain, int(start_x));
401 x += dim_.wid - TEXT_TO_INSET_OFFSET;
403 if (need_update != INIT) {
411 void InsetText::drawFrame(Painter & pain, int x) const
413 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
415 frame_y = top_baseline - dim_.asc + ttoD2;
416 frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
417 frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
418 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
423 void InsetText::update(BufferView * bv, bool reinit)
426 if (reinit && owner()) {
428 owner()->update(bv, true);
434 if (reinit || need_update == INIT) {
436 // we should put this call where we set need_update to INIT!
439 owner()->update(bv, true);
442 int nw = getMaxWidth(bv, this);
443 if (nw > 0 && old_max_width != nw) {
450 if (!autoBreakRows && paragraphs.size() > 1)
451 collapseParagraphs(bv);
453 if (the_locking_inset) {
454 inset_x = cix(bv) - top_x + drawTextXOffset;
455 inset_y = ciy(bv) + drawTextYOffset;
456 the_locking_inset->update(bv, reinit);
464 if ((need_update & CURSOR_PAR) && (lt->refreshStatus() == LyXText::REFRESH_NONE) &&
466 lt->updateInset(the_locking_inset);
469 if (lt->refreshStatus() == LyXText::REFRESH_AREA)
476 int nw = getMaxWidth(bv, this);
477 if (nw > 0 && old_max_width != nw) {
484 void InsetText::setUpdateStatus(BufferView * bv, int what) const
486 // this does nothing dangerous so use only a localized buffer
487 LyXText * llt = getLyXText(bv);
490 // we have to redraw us full if our LyXText REFRESH_AREA or
491 // if we don't break row so that we only have one row to update!
492 if ((llt->refreshStatus() == LyXText::REFRESH_AREA) ||
494 (llt->refreshStatus() == LyXText::REFRESH_ROW)))
497 } else if (llt->refreshStatus() == LyXText::REFRESH_ROW) {
498 need_update |= CURSOR_PAR;
501 // this to not draw a selection when we redraw all of it!
502 if (need_update & CURSOR && !(need_update & SELECTION)) {
503 if (llt->selection.set())
505 llt->clearSelection();
510 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
512 if (!autoBreakRows && paragraphs.size() > 1)
513 collapseParagraphs(bv);
520 lt->partialRebreak();
521 setUpdateStatus(bv, what);
522 bool flag = mark_dirty ||
523 (((need_update != CURSOR) && (need_update != NONE)) ||
524 (lt->refreshStatus() != LyXText::REFRESH_NONE) || lt->selection.set());
525 if (!lt->selection.set())
526 lt->selection.cursor = lt->cursor;
532 bv->updateInset(const_cast<InsetText *>(this));
538 if (need_update == CURSOR)
540 bv->owner()->view_state_changed();
541 bv->owner()->updateMenubar();
542 bv->owner()->updateToolbar();
543 if (old_par != cpar(bv)) {
544 bv->owner()->setLayout(cpar(bv)->layout()->name());
550 string const InsetText::editMessage() const
552 return _("Opened Text Inset");
556 void InsetText::insetUnlock(BufferView * bv)
558 if (the_locking_inset) {
559 the_locking_inset->insetUnlock(bv);
560 the_locking_inset = 0;
561 updateLocal(bv, CURSOR_PAR, false);
572 if (lt->selection.set()) {
573 lt->clearSelection();
575 } else if (owner()) {
576 bv->owner()->setLayout(owner()->getLyXText(bv)
577 ->cursor.par()->layout()->name());
579 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
580 // hack for deleteEmptyParMech
581 ParagraphList::iterator first_par = paragraphs.begin();
582 if (!first_par->empty()) {
583 lt->setCursor(first_par, 0);
584 } else if (paragraphs.size() > 1) {
585 lt->setCursor(boost::next(first_par), 0);
590 updateLocal(bv, code, false);
593 setUpdateStatus(bv, code);
598 void InsetText::lockInset(BufferView * bv)
601 the_locking_inset = 0;
602 inset_pos = inset_x = inset_y = 0;
603 inset_boundary = false;
604 inset_par = paragraphs.end();
605 old_par = paragraphs.end();
611 lt->setCursor(paragraphs.begin(), 0);
612 lt->clearSelection();
614 // If the inset is empty set the language of the current font to the
615 // language to the surronding text (if different).
616 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
617 bv->getParentLanguage(this) != lt->current_font.language()) {
618 LyXFont font(LyXFont::ALL_IGNORE);
619 font.setLanguage(bv->getParentLanguage(this));
620 setFont(bv, font, false);
625 if (drawFrame_ == LOCKED)
626 code = CURSOR|DRAW_FRAME;
627 setUpdateStatus(bv, code);
631 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
633 the_locking_inset = inset;
634 inset_x = cix(bv) - top_x + drawTextXOffset;
635 inset_y = ciy(bv) + drawTextYOffset;
636 inset_pos = cpos(bv);
637 inset_par = cpar(bv);
638 inset_boundary = cboundary(bv);
639 updateLocal(bv, CURSOR, false);
643 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
645 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
649 if (!the_locking_inset) {
650 ParagraphList::iterator pit = paragraphs.begin();
651 ParagraphList::iterator pend = paragraphs.end();
653 int const id = inset->id();
654 for (; pit != pend; ++pit) {
655 InsetList::iterator it =
656 pit->insetlist.begin();
657 InsetList::iterator const end =
658 pit->insetlist.end();
659 for (; it != end; ++it) {
660 if (it->inset == inset) {
661 getLyXText(bv)->setCursorIntern(pit, it->pos);
662 lockInset(bv, inset);
665 if (it->inset->getInsetFromID(id)) {
666 getLyXText(bv)->setCursorIntern(pit, it->pos);
667 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
668 return the_locking_inset->lockInsetInInset(bv, inset);
674 if (inset == cpar(bv)->getInset(cpos(bv))) {
675 lyxerr[Debug::INSETS] << "OK" << endl;
676 lockInset(bv, inset);
678 } else if (the_locking_inset && (the_locking_inset == inset)) {
679 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
680 lyxerr[Debug::INSETS] << "OK" << endl;
681 inset_x = cix(bv) - top_x + drawTextXOffset;
682 inset_y = ciy(bv) + drawTextYOffset;
684 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
686 } else if (the_locking_inset) {
687 lyxerr[Debug::INSETS] << "MAYBE" << endl;
688 return the_locking_inset->lockInsetInInset(bv, inset);
690 lyxerr[Debug::INSETS] << "NOT OK" << endl;
695 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
698 if (!the_locking_inset)
700 if (the_locking_inset == inset) {
701 the_locking_inset->insetUnlock(bv);
702 getLyXText(bv)->updateInset(inset);
703 the_locking_inset = 0;
705 moveRightIntern(bv, true, false);
706 old_par = paragraphs.end(); // force layout setting
710 updateLocal(bv, CURSOR, false);
713 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
717 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
719 if (!autoBreakRows && paragraphs.size() > 1)
720 collapseParagraphs(bv);
730 if (inset->owner() != this) {
731 int ustat = CURSOR_PAR;
733 UpdatableInset * tl_inset = the_locking_inset;
735 found = tl_inset->updateInsetInInset(bv, inset);
737 tl_inset = static_cast<UpdatableInset *>(inset);
738 while(tl_inset->owner() && tl_inset->owner() != this)
739 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
740 if (!tl_inset->owner())
742 found = tl_inset->updateInsetInInset(bv, inset);
746 lt->updateInset(tl_inset);
750 setUpdateStatus(bv, ustat);
753 bool found = lt->updateInset(inset);
757 setUpdateStatus(bv, CURSOR_PAR);
758 if (the_locking_inset &&
759 cpar(bv) == inset_par && cpos(bv) == inset_pos)
761 inset_x = cix(bv) - top_x + drawTextXOffset;
762 inset_y = ciy(bv) + drawTextYOffset;
769 void InsetText::lfunMousePress(FuncRequest const & cmd)
773 // use this to check mouse motion for selection!
777 BufferView * bv = cmd.view();
778 FuncRequest cmd1 = cmd;
784 int tmp_x = cmd.x - drawTextXOffset;
785 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
786 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
788 if (the_locking_inset) {
789 if (the_locking_inset == inset) {
790 the_locking_inset->localDispatch(cmd1);
793 // otherwise only unlock the_locking_inset
794 the_locking_inset->insetUnlock(bv);
795 the_locking_inset = 0;
798 no_selection = false;
800 if (bv->theLockingInset()) {
801 if (isHighlyEditableInset(inset)) {
802 // We just have to lock the inset before calling a
804 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
805 if (!bv->lockInset(uinset)) {
806 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
808 inset->localDispatch(cmd1);
809 if (the_locking_inset)
810 updateLocal(bv, CURSOR, false);
815 bool paste_internally = false;
816 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
817 localDispatch(FuncRequest(bv, LFUN_COPY));
818 paste_internally = true;
825 int old_top_y = lt->top_y();
827 lt->setCursorFromCoordinates(cmd.x - drawTextXOffset,
829 // set the selection cursor!
830 lt->selection.cursor = lt->cursor;
831 lt->cursor.x_fix(lt->cursor.x());
833 if (lt->selection.set()) {
834 lt->clearSelection();
835 updateLocal(bv, FULL, false);
837 lt->clearSelection();
838 updateLocal(bv, CURSOR, false);
844 bv->owner()->setLayout(cpar(bv)->layout()->name());
846 // we moved the view we cannot do mouse selection in this case!
847 if (getLyXText(bv)->top_y() != old_top_y)
850 // Insert primary selection with middle mouse
851 // if there is a local selection in the current buffer,
853 if (cmd.button() == mouse_button::button2) {
854 if (paste_internally)
855 localDispatch(FuncRequest(bv, LFUN_PASTE));
857 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
860 getLyXText(bv)->clearSelection();
865 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
867 BufferView * bv = cmd.view();
868 FuncRequest cmd1 = cmd;
873 if (the_locking_inset)
874 return the_locking_inset->localDispatch(cmd1);
876 int tmp_x = cmd.x - drawTextXOffset;
877 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
878 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
881 // This code should probably be removed now. Simple insets
882 // (!highlyEditable) can actually take the localDispatch,
883 // and turn it into edit() if necessary. But we still
884 // need to deal properly with the whole relative vs.
885 // absolute mouse co-ords thing in a realiable, sensible way
887 if (isHighlyEditableInset(inset))
888 ret = inset->localDispatch(cmd1);
890 inset_x = cix(bv) - top_x + drawTextXOffset;
891 inset_y = ciy(bv) + drawTextYOffset;
892 cmd1.x = cmd.x - inset_x;
893 cmd1.y = cmd.x - inset_y;
894 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
898 ret = inset->localDispatch(cmd1);
899 updateLocal(bv, CURSOR_PAR, false);
906 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
908 FuncRequest cmd1 = cmd;
912 if (the_locking_inset) {
913 the_locking_inset->localDispatch(cmd1);
917 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
920 BufferView * bv = cmd.view();
926 LyXCursor cur = lt->cursor;
927 lt->setCursorFromCoordinates
928 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
929 lt->cursor.x_fix(lt->cursor.x());
930 if (cur == lt->cursor) {
936 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
937 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
941 updateLocal(bv, SELECTION, false);
946 Inset::RESULT InsetText::localDispatch(FuncRequest const & cmd)
948 BufferView * bv = cmd.view();
950 if (cmd.action == LFUN_INSET_EDIT) {
951 UpdatableInset::localDispatch(cmd);
953 if (!bv->lockInset(this)) {
954 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
959 the_locking_inset = 0;
960 inset_pos = inset_x = inset_y = 0;
961 inset_boundary = false;
962 inset_par = paragraphs.end();
963 old_par = paragraphs.end();
971 if (cmd.argument.size()) {
972 if (cmd.argument == "left")
973 lt->setCursor(paragraphs.begin(), 0);
975 ParagraphList::iterator it = paragraphs.begin();
976 ParagraphList::iterator end = paragraphs.end();
977 while (boost::next(it) != end)
979 // int const pos = (p->size() ? p->size()-1 : p->size());
980 lt->setCursor(it, it->size());
983 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
984 // we put here -1 and not button as now the button in the
985 // edit call should not be needed we will fix this in 1.3.x
986 // cycle hopefully (Jug 20020509)
987 // FIXME: GUII I've changed this to none: probably WRONG
988 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
989 lt->setCursorFromCoordinates(cmd.x - drawTextXOffset,
991 lt->cursor.x_fix(lt->cursor.x());
995 lt->clearSelection();
998 // If the inset is empty set the language of the current font to the
999 // language to the surronding text (if different).
1000 if (paragraphs.begin()->empty() &&
1001 paragraphs.size() == 1 &&
1002 bv->getParentLanguage(this) != lt->current_font.language())
1004 LyXFont font(LyXFont::ALL_IGNORE);
1005 font.setLanguage(bv->getParentLanguage(this));
1006 setFont(bv, font, false);
1012 if (drawFrame_ == LOCKED)
1013 code = CURSOR | DRAW_FRAME;
1015 updateLocal(bv, code, false);
1016 // Tell the paragraph dialog that we've entered an insettext.
1017 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1022 switch (cmd.action) {
1023 case LFUN_MOUSE_PRESS:
1024 lfunMousePress(cmd);
1026 case LFUN_MOUSE_MOTION:
1027 lfunMouseMotion(cmd);
1029 case LFUN_MOUSE_RELEASE:
1030 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
1035 bool was_empty = (paragraphs.begin()->empty() &&
1036 paragraphs.size() == 1);
1038 no_selection = false;
1039 RESULT result = UpdatableInset::localDispatch(cmd);
1040 if (result != UNDISPATCHED)
1043 result = DISPATCHED;
1044 if (cmd.action < 0 && cmd.argument.empty())
1047 if (the_locking_inset) {
1048 result = the_locking_inset->localDispatch(cmd);
1049 if (result == DISPATCHED_NOUPDATE)
1051 else if (result == DISPATCHED) {
1052 updateLocal(bv, CURSOR_PAR, false);
1054 } else if (result >= FINISHED) {
1056 case FINISHED_RIGHT:
1057 moveRightIntern(bv, false, false);
1058 result = DISPATCHED;
1061 if ((result = moveUp(bv)) >= FINISHED) {
1062 updateLocal(bv, CURSOR, false);
1063 bv->unlockInset(this);
1067 if ((result = moveDown(bv)) >= FINISHED) {
1068 updateLocal(bv, CURSOR, false);
1069 bv->unlockInset(this);
1073 result = DISPATCHED;
1076 the_locking_inset = 0;
1077 updateLocal(bv, CURSOR, false);
1078 // make sure status gets reset immediately
1079 bv->owner()->clearMessage();
1085 lt = getLyXText(bv);
1089 int updflag = false;
1091 // what type of update to do on a cursor movement
1092 int cursor_update = CURSOR;
1094 if (lt->selection.set())
1095 cursor_update = SELECTION;
1097 switch (cmd.action) {
1100 case LFUN_SELFINSERT:
1101 if (bv->buffer()->isReadonly()) {
1102 // setErrorMessage(N_("Document is read only"));
1105 if (!cmd.argument.empty()) {
1106 /* Automatically delete the currently selected
1107 * text and replace it with what is being
1108 * typed in now. Depends on lyxrc settings
1109 * "auto_region_delete", which defaults to
1112 // This should not be needed here and is also WRONG!
1113 setUndo(bv, Undo::INSERT, lt->cursor.par());
1116 if (lyxrc.auto_region_delete) {
1117 if (lt->selection.set()) {
1118 lt->cutSelection(false, false);
1121 lt->clearSelection();
1122 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
1123 bv->owner()->getIntl().getTransManager().
1124 TranslateAndInsert(cmd.argument[i], lt);
1127 lt->selection.cursor = lt->cursor;
1128 updwhat = CURSOR | CURSOR_PAR;
1130 result = DISPATCHED_NOUPDATE;
1133 // cursor movements that need special handling
1136 result = moveRight(bv);
1138 updwhat = cursor_update;
1142 result = moveLeft(bv);
1143 updwhat = cursor_update;
1147 result = moveDown(bv);
1148 updwhat = cursor_update;
1152 result = moveUp(bv);
1153 updwhat = cursor_update;
1157 if (crow(bv) == lt->rows().begin())
1158 result = FINISHED_UP;
1160 lt->cursorPrevious();
1161 lt->clearSelection();
1162 result = DISPATCHED_NOUPDATE;
1164 updwhat = cursor_update;
1168 if (boost::next(crow(bv)) == lt->rows().end())
1169 result = FINISHED_DOWN;
1172 lt->clearSelection();
1173 result = DISPATCHED_NOUPDATE;
1175 updwhat = cursor_update;
1178 case LFUN_BACKSPACE: {
1179 if (lt->selection.set())
1180 lt->cutSelection(true, false);
1183 updwhat = CURSOR_PAR;
1189 if (lt->selection.set()) {
1190 lt->cutSelection(true, false);
1194 updwhat = CURSOR_PAR;
1200 lt->cutSelection(true, true);
1201 updwhat = CURSOR_PAR;
1208 lt->copySelection();
1209 updwhat = CURSOR_PAR;
1212 case LFUN_PASTESELECTION:
1214 string const clip(bv->getClipboard());
1218 if (cmd.argument == "paragraph") {
1219 lt->insertStringAsParagraphs(clip);
1221 lt->insertStringAsLines(clip);
1224 lt->clearSelection();
1226 updwhat = CURSOR_PAR;
1232 if (!autoBreakRows) {
1233 if (CutAndPaste::nrOfParagraphs() > 1) {
1234 #ifdef WITH_WARNINGS
1235 #warning FIXME horrendously bad UI
1237 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1242 size_t sel_index = 0;
1243 string const & arg = cmd.argument;
1244 if (isStrUnsignedInt(arg)) {
1245 size_t const paste_arg = strToUnsignedInt(arg);
1246 #warning FIXME Check if the arg is in the domain of available selections.
1247 sel_index = paste_arg;
1249 lt->pasteSelection(sel_index);
1251 lt->clearSelection();
1252 updwhat = CURSOR_PAR;
1257 case LFUN_BREAKPARAGRAPH:
1258 if (!autoBreakRows) {
1259 result = DISPATCHED;
1262 lt->breakParagraph(paragraphs, 0);
1263 updwhat = CURSOR | FULL;
1267 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1268 if (!autoBreakRows) {
1269 result = DISPATCHED;
1272 lt->breakParagraph(paragraphs, 1);
1273 updwhat = CURSOR | FULL;
1277 case LFUN_BREAKLINE: {
1278 if (!autoBreakRows) {
1279 result = DISPATCHED;
1283 lt->insertInset(new InsetNewline);
1284 updwhat = CURSOR | CURSOR_PAR;
1290 // do not set layouts on non breakable textinsets
1291 if (autoBreakRows) {
1292 string cur_layout = cpar(bv)->layout()->name();
1294 // Derive layout number from given argument (string)
1295 // and current buffer's textclass (number). */
1296 LyXTextClass const & tclass =
1297 bv->buffer()->params.getLyXTextClass();
1298 string layout = cmd.argument;
1299 bool hasLayout = tclass.hasLayout(layout);
1301 // If the entry is obsolete, use the new one instead.
1303 string const & obs =
1304 tclass[layout]->obsoleted_by();
1309 // see if we found the layout number:
1311 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1312 bv->owner()->dispatch(lf);
1316 if (cur_layout != layout) {
1317 cur_layout = layout;
1318 lt->setLayout(layout);
1319 bv->owner()->setLayout(cpar(bv)->layout()->name());
1320 updwhat = CURSOR_PAR;
1324 // reset the layout box
1325 bv->owner()->setLayout(cpar(bv)->layout()->name());
1328 case LFUN_PARAGRAPH_SPACING:
1329 // This one is absolutely not working. When fiddling with this
1330 // it also seems to me that the paragraphs inside the insettext
1331 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1332 // FIXME: how old is this comment ? ...
1334 ParagraphList::iterator pit = lt->cursor.par();
1335 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1336 float cur_value = 1.0;
1337 if (cur_spacing == Spacing::Other) {
1338 cur_value = pit->params().spacing().getValue();
1341 istringstream istr(STRCONV(cmd.argument));
1344 Spacing::Space new_spacing = cur_spacing;
1345 float new_value = cur_value;
1347 lyxerr << "Missing argument to `paragraph-spacing'"
1349 } else if (tmp == "single") {
1350 new_spacing = Spacing::Single;
1351 } else if (tmp == "onehalf") {
1352 new_spacing = Spacing::Onehalf;
1353 } else if (tmp == "double") {
1354 new_spacing = Spacing::Double;
1355 } else if (tmp == "other") {
1356 new_spacing = Spacing::Other;
1359 lyxerr << "new_value = " << tmpval << endl;
1362 } else if (tmp == "default") {
1363 new_spacing = Spacing::Default;
1365 lyxerr << _("Unknown spacing argument: ")
1366 << cmd.argument << endl;
1368 if (cur_spacing != new_spacing || cur_value != new_value) {
1369 pit->params().spacing(Spacing(new_spacing, new_value));
1370 updwhat = CURSOR_PAR;
1376 // These need to do repaints but don't require
1377 // special handling otherwise. A *lot* of the
1378 // above could probably be done similarly ...
1383 case LFUN_WORDRIGHT:
1384 // these two are really unhandled ...
1386 case LFUN_BEGINNINGBUF:
1387 updwhat = cursor_update;
1388 if (!bv->dispatch(cmd))
1389 result = UNDISPATCHED;
1398 case LFUN_WORDLEFTSEL:
1399 case LFUN_WORDRIGHTSEL:
1400 updwhat = SELECTION;
1405 if (!bv->dispatch(cmd))
1406 result = UNDISPATCHED;
1413 updateLocal(bv, updwhat, updflag);
1414 /// If the action has deleted all text in the inset, we need to change the
1415 // language to the language of the surronding text.
1416 if (!was_empty && paragraphs.begin()->empty() &&
1417 paragraphs.size() == 1) {
1418 LyXFont font(LyXFont::ALL_IGNORE);
1419 font.setLanguage(bv->getParentLanguage(this));
1420 setFont(bv, font, false);
1423 if (result >= FINISHED)
1424 bv->unlockInset(this);
1426 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1427 result = DISPATCHED;
1432 int InsetText::latex(Buffer const * buf, ostream & os,
1433 LatexRunParams const & runparams) const
1436 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1437 return texrow.rows();
1441 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1443 unsigned int lines = 0;
1445 ParagraphList::const_iterator beg = paragraphs.begin();
1446 ParagraphList::const_iterator end = paragraphs.end();
1447 ParagraphList::const_iterator it = beg;
1448 for (; it != end; ++it) {
1449 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1450 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1457 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1459 unsigned int lines = 0;
1461 vector<string> environment_stack(10);
1462 vector<string> environment_inner(10);
1464 int const command_depth = 0;
1467 Paragraph::depth_type depth = 0; // paragraph depth
1469 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1470 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1472 for (; pit != pend; ++pit) {
1474 int desc_on = 0; // description mode
1476 LyXLayout_ptr const & style = pit->layout();
1478 // environment tag closing
1479 for (; depth > pit->params().depth(); --depth) {
1480 if (environment_inner[depth] != "!-- --") {
1481 item_name = "listitem";
1482 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1483 if (environment_inner[depth] == "varlistentry")
1484 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1486 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1487 environment_stack[depth].erase();
1488 environment_inner[depth].erase();
1491 if (depth == pit->params().depth()
1492 && environment_stack[depth] != style->latexname()
1493 && !environment_stack[depth].empty()) {
1494 if (environment_inner[depth] != "!-- --") {
1495 item_name= "listitem";
1496 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1497 if (environment_inner[depth] == "varlistentry")
1498 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1501 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1503 environment_stack[depth].erase();
1504 environment_inner[depth].erase();
1507 // Write opening SGML tags.
1508 switch (style->latextype) {
1509 case LATEX_PARAGRAPH:
1510 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1514 buf->parseError(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1518 case LATEX_ENVIRONMENT:
1519 case LATEX_ITEM_ENVIRONMENT:
1520 if (depth < pit->params().depth()) {
1521 depth = pit->params().depth();
1522 environment_stack[depth].erase();
1525 if (environment_stack[depth] != style->latexname()) {
1526 if (environment_stack.size() == depth + 1) {
1527 environment_stack.push_back("!-- --");
1528 environment_inner.push_back("!-- --");
1530 environment_stack[depth] = style->latexname();
1531 environment_inner[depth] = "!-- --";
1532 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1534 if (environment_inner[depth] != "!-- --") {
1535 item_name= "listitem";
1536 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1537 if (environment_inner[depth] == "varlistentry")
1538 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1542 if (style->latextype == LATEX_ENVIRONMENT) {
1543 if (!style->latexparam().empty()) {
1544 if (style->latexparam() == "CDATA")
1547 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1552 desc_on = (style->labeltype == LABEL_MANUAL);
1554 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1555 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1557 item_name = desc_on ? "term" : "para";
1558 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1562 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1566 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1569 // write closing SGML tags
1570 switch (style->latextype) {
1571 case LATEX_ENVIRONMENT:
1572 if (!style->latexparam().empty()) {
1573 if (style->latexparam() == "CDATA")
1576 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1579 case LATEX_ITEM_ENVIRONMENT:
1580 if (desc_on == 1) break;
1582 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1584 case LATEX_PARAGRAPH:
1585 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1588 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1594 for (int d = depth; d >= 0; --d) {
1595 if (!environment_stack[depth].empty()) {
1596 if (environment_inner[depth] != "!-- --") {
1597 item_name = "listitem";
1598 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1599 if (environment_inner[depth] == "varlistentry")
1600 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1603 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1611 void InsetText::validate(LaTeXFeatures & features) const
1613 for_each(paragraphs.begin(), paragraphs.end(),
1614 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1618 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1620 if (the_locking_inset) {
1621 the_locking_inset->getCursor(bv, x, y);
1625 y = cy(&bv) + InsetText::y();
1629 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1631 if (the_locking_inset) {
1632 the_locking_inset->getCursorPos(bv, x, y);
1635 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1636 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1640 int InsetText::insetInInsetY() const
1642 if (!the_locking_inset)
1645 return (inset_y + the_locking_inset->insetInInsetY());
1649 void InsetText::fitInsetCursor(BufferView * bv) const
1651 if (the_locking_inset) {
1652 the_locking_inset->fitInsetCursor(bv);
1655 LyXFont const font =
1656 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1658 int const asc = font_metrics::maxAscent(font);
1659 int const desc = font_metrics::maxDescent(font);
1661 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1662 need_update |= FULL;
1667 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1669 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1670 return moveLeftIntern(bv, false, activate_inset, selecting);
1672 return moveRightIntern(bv, true, activate_inset, selecting);
1677 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1679 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1680 return moveRightIntern(bv, true, activate_inset, selecting);
1682 return moveLeftIntern(bv, false, activate_inset, selecting);
1687 InsetText::moveRightIntern(BufferView * bv, bool front,
1688 bool activate_inset, bool selecting)
1690 LyXText * text = getLyXText(bv);
1692 ParagraphList::iterator c_par = cpar(bv);
1694 if (boost::next(c_par) == paragraphs.end() &&
1695 (cpos(bv) >= c_par->size()))
1696 return FINISHED_RIGHT;
1697 if (activate_inset && checkAndActivateInset(bv, front))
1699 text->cursorRight(bv);
1701 text->clearSelection();
1702 return DISPATCHED_NOUPDATE;
1707 InsetText::moveLeftIntern(BufferView * bv, bool front,
1708 bool activate_inset, bool selecting)
1710 LyXText * text = getLyXText(bv);
1712 if (cpar(bv) == paragraphs.begin() && (cpos(bv) <= 0))
1714 text->cursorLeft(bv);
1716 text->clearSelection();
1717 if (activate_inset && checkAndActivateInset(bv, front))
1719 return DISPATCHED_NOUPDATE;
1723 Inset::RESULT InsetText::moveUp(BufferView * bv)
1725 if (crow(bv) == getLyXText(bv)->rows().begin())
1727 getLyXText(bv)->cursorUp(bv);
1728 getLyXText(bv)->clearSelection();
1729 return DISPATCHED_NOUPDATE;
1733 Inset::RESULT InsetText::moveDown(BufferView * bv)
1735 if (boost::next(crow(bv)) == getLyXText(bv)->rows().end())
1736 return FINISHED_DOWN;
1737 getLyXText(bv)->cursorDown(bv);
1738 getLyXText(bv)->clearSelection();
1739 return DISPATCHED_NOUPDATE;
1743 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1745 if (the_locking_inset) {
1746 if (the_locking_inset->insetAllowed(inset))
1747 return the_locking_inset->insertInset(bv, inset);
1750 inset->setOwner(this);
1751 getLyXText(bv)->insertInset(inset);
1753 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1758 bool InsetText::insetAllowed(Inset::Code code) const
1760 // in_insetAllowed is a really gross hack,
1761 // to allow us to call the owner's insetAllowed
1762 // without stack overflow, which can happen
1763 // when the owner uses InsetCollapsable::insetAllowed()
1765 if (in_insetAllowed)
1767 in_insetAllowed = true;
1768 if (the_locking_inset)
1769 ret = the_locking_inset->insetAllowed(code);
1771 ret = owner()->insetAllowed(code);
1772 in_insetAllowed = false;
1777 UpdatableInset * InsetText::getLockingInset() const
1779 return the_locking_inset ? the_locking_inset->getLockingInset() :
1780 const_cast<InsetText *>(this);
1784 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1788 if (the_locking_inset)
1789 return the_locking_inset->getFirstLockingInsetOfType(c);
1794 bool InsetText::showInsetDialog(BufferView * bv) const
1796 if (the_locking_inset)
1797 return the_locking_inset->showInsetDialog(bv);
1802 void InsetText::getLabelList(std::vector<string> & list) const
1804 ParagraphList::const_iterator pit = paragraphs.begin();
1805 ParagraphList::const_iterator pend = paragraphs.end();
1806 for (; pit != pend; ++pit) {
1807 InsetList::const_iterator beg = pit->insetlist.begin();
1808 InsetList::const_iterator end = pit->insetlist.end();
1809 for (; beg != end; ++beg)
1810 beg->inset->getLabelList(list);
1815 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1818 if (the_locking_inset) {
1819 the_locking_inset->setFont(bv, font, toggleall, selectall);
1823 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1824 || cpar(bv)->empty()) {
1825 getLyXText(bv)->setFont(font, toggleall);
1831 lt = getLyXText(bv);
1835 if (lt->selection.set()) {
1836 setUndo(bv, Undo::EDIT, lt->cursor.par());
1842 lt->toggleFree(font, toggleall);
1845 lt->clearSelection();
1849 bool flag = (selectall || lt->selection.set());
1855 updateLocal(bv, FULL, true);
1857 updateLocal(bv, CURSOR_PAR, true);
1861 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1863 if (cpar(bv)->isInset(cpos(bv))) {
1865 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1866 if (!isHighlyEditableInset(inset))
1868 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1869 inset->localDispatch(cmd);
1870 if (!the_locking_inset)
1872 updateLocal(bv, CURSOR, false);
1879 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1880 mouse_button::state button)
1882 x -= drawTextXOffset;
1884 int dummyy = y + dim_.asc;
1885 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1886 // we only do the edit() call if the inset was hit by the mouse
1887 // or if it is a highly editable inset. So we should call this
1888 // function from our own edit with button < 0.
1889 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1891 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1899 inset_x = cix(bv) - top_x + drawTextXOffset;
1900 inset_y = ciy(bv) + drawTextYOffset;
1901 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1902 inset->localDispatch(cmd);
1903 if (!the_locking_inset)
1905 updateLocal(bv, CURSOR, false);
1912 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1915 int w = UpdatableInset::getMaxWidth(bv, inset);
1920 w = w - top_x + owner()->x();
1923 w -= (2 * TEXT_TO_INSET_OFFSET);
1926 return UpdatableInset::getMaxWidth(bv, inset);
1931 void InsetText::setParagraphData(ParagraphList const & plist)
1933 // we have to unlock any locked inset otherwise we're in troubles
1934 the_locking_inset = 0;
1936 // But it it makes no difference that is a lot better.
1938 // See if this can be simplified when std::list is in effect.
1941 ParagraphList::const_iterator it = plist.begin();
1942 ParagraphList::const_iterator end = plist.end();
1943 for (; it != end; ++it) {
1944 paragraphs.push_back(*it);
1945 Paragraph & tmp = paragraphs.back();
1946 tmp.setInsetOwner(this);
1954 void InsetText::markNew(bool track_changes)
1956 ParagraphList::iterator pit = paragraphs.begin();
1957 ParagraphList::iterator pend = paragraphs.end();
1958 for (; pit != pend; ++pit) {
1959 if (track_changes) {
1960 pit->trackChanges();
1962 // no-op when not tracking
1963 pit->cleanChanges();
1969 void InsetText::setText(string const & data, LyXFont const & font)
1972 for (unsigned int i = 0; i < data.length(); ++i)
1973 paragraphs.begin()->insertChar(i, data[i], font);
1978 void InsetText::setAutoBreakRows(bool flag)
1980 if (flag != autoBreakRows) {
1981 autoBreakRows = flag;
1989 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1991 if (how != drawFrame_) {
1994 updateLocal(bv, DRAW_FRAME, false);
1999 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2001 if (frame_color != col) {
2004 updateLocal(bv, DRAW_FRAME, false);
2009 int InsetText::cx(BufferView * bv) const
2011 // we do nothing dangerous so we use a local cache
2012 LyXText * llt = getLyXText(bv);
2013 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2014 if (the_locking_inset) {
2015 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2017 if (font.isVisibleRightToLeft())
2018 x -= the_locking_inset->width(bv, font);
2024 int InsetText::cix(BufferView * bv) const
2026 // we do nothing dangerous so we use a local cache
2027 LyXText * llt = getLyXText(bv);
2028 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2029 if (the_locking_inset) {
2030 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2032 if (font.isVisibleRightToLeft())
2033 x -= the_locking_inset->width(bv, font);
2039 int InsetText::cy(BufferView * bv) const
2042 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2046 int InsetText::ciy(BufferView * bv) const
2049 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2053 pos_type InsetText::cpos(BufferView * bv) const
2055 return getLyXText(bv)->cursor.pos();
2059 ParagraphList::iterator InsetText::cpar(BufferView * bv) const
2061 return getLyXText(bv)->cursor.par();
2065 bool InsetText::cboundary(BufferView * bv) const
2067 return getLyXText(bv)->cursor.boundary();
2071 RowList::iterator InsetText::crow(BufferView * bv) const
2073 return getLyXText(bv)->cursorRow();
2077 LyXText * InsetText::getLyXText(BufferView const * lbv,
2078 bool const recursive) const
2080 if (cached_bview == lbv) {
2081 if (recursive && the_locking_inset)
2082 return the_locking_inset->getLyXText(lbv, true);
2083 LyXText * lt = cached_text.get();
2084 Assert(lt && lt->rows().begin()->par() == const_cast<ParagraphList&>(paragraphs).begin());
2087 // Super UGLY! (Lgb)
2088 BufferView * bv = const_cast<BufferView *>(lbv);
2091 Cache::iterator it = cache.find(bv);
2093 if (it != cache.end()) {
2096 } else if (do_resize) {
2097 resizeLyXText(do_resize);
2099 if (lt || !it->second.remove) {
2100 Assert(it->second.text.get());
2101 cached_text = it->second.text;
2102 if (recursive && the_locking_inset) {
2103 return the_locking_inset->getLyXText(bv, true);
2105 return cached_text.get();
2106 } else if (it->second.remove) {
2108 saveLyXTextState(it->second.text.get());
2110 sstate.cursor.par(const_cast<ParagraphList&>(paragraphs).end());
2114 // when we have to reinit the existing LyXText!
2116 it->second.text->init(bv);
2117 restoreLyXTextState(it->second.text.get());
2118 it->second.remove = false;
2120 cached_text = it->second.text;
2121 if (the_locking_inset && recursive) {
2122 return the_locking_inset->getLyXText(bv);
2124 return cached_text.get();
2127 // we are here only if we don't have a BufferView * in the cache!!!
2129 cached_text.reset(new LyXText(bv, const_cast<InsetText *>(this)));
2130 cached_text->init(bv);
2131 restoreLyXTextState(cached_text.get());
2133 cache.insert(make_pair(bv, cached_text));
2135 if (the_locking_inset && recursive) {
2136 return the_locking_inset->getLyXText(bv);
2138 return cached_text.get();
2142 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2146 Cache::iterator it = cache.find(bv);
2148 if (it == cache.end()) {
2152 Assert(it->second.text.get());
2154 it->second.remove = true;
2156 /// then remove all LyXText in text-insets
2157 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2158 const_cast<ParagraphList&>(paragraphs).end(),
2159 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2164 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2167 // we cannot resize this because we are in use!
2168 // so do this on the next possible getLyXText()
2173 if (paragraphs.size() == 1 && paragraphs.begin()->empty()) {
2174 // no data, resize not neccessary!
2175 // we have to do this as a fixed width may have changed!
2176 LyXText * t = getLyXText(bv);
2177 saveLyXTextState(t);
2179 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()) {
2192 Assert(it->second.text.get());
2194 LyXText * t = it->second.text.get();
2195 saveLyXTextState(t);
2197 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2198 const_cast<ParagraphList&>(paragraphs).end(),
2199 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2202 restoreLyXTextState(t);
2204 if (the_locking_inset) {
2205 inset_x = cix(bv) - top_x + drawTextXOffset;
2206 inset_y = ciy(bv) + drawTextYOffset;
2209 t->top_y(bv->screen().topCursorVisible(t));
2211 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2212 // this will scroll the screen such that the cursor becomes visible
2213 bv->updateScrollbar();
2215 need_update |= FULL;
2220 void InsetText::reinitLyXText() const
2223 // we cannot resize this because we are in use!
2224 // so do this on the next possible getLyXText()
2230 for (Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2231 Assert(it->second.text.get());
2233 LyXText * t = it->second.text.get();
2234 BufferView * bv = it->first;
2236 saveLyXTextState(t);
2238 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2239 const_cast<ParagraphList&>(paragraphs).end(),
2240 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2243 restoreLyXTextState(t);
2244 if (the_locking_inset) {
2245 inset_x = cix(bv) - top_x + drawTextXOffset;
2246 inset_y = ciy(bv) + drawTextYOffset;
2248 t->top_y(bv->screen().topCursorVisible(t));
2250 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2251 // this will scroll the screen such that the cursor becomes visible
2252 bv->updateScrollbar();
2260 void InsetText::removeNewlines()
2262 bool changed = false;
2264 ParagraphList::iterator it = paragraphs.begin();
2265 ParagraphList::iterator end = paragraphs.end();
2266 for (; it != end; ++it) {
2267 for (int i = 0; i < it->size(); ++i) {
2268 if (it->isNewline(i)) {
2279 bool InsetText::nodraw() const
2281 if (the_locking_inset)
2282 return the_locking_inset->nodraw();
2283 return UpdatableInset::nodraw();
2287 int InsetText::scroll(bool recursive) const
2289 int sx = UpdatableInset::scroll(false);
2291 if (recursive && the_locking_inset)
2292 sx += the_locking_inset->scroll(recursive);
2298 void InsetText::selectAll(BufferView * bv)
2300 getLyXText(bv)->cursorTop();
2301 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2302 getLyXText(bv)->cursorBottom();
2303 getLyXText(bv)->setSelection();
2307 void InsetText::clearSelection(BufferView * bv)
2309 getLyXText(bv)->clearSelection();
2313 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2315 Painter & pain = bv->painter();
2317 int h = dim_.asc + dim_.des;
2318 int ty = baseline - dim_.asc;
2324 if ((ty + h) > pain.paperHeight())
2325 h = pain.paperHeight();
2326 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2327 w = pain.paperWidth();
2328 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2333 ParagraphList * InsetText::getParagraphs(int i) const
2335 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2339 LyXCursor const & InsetText::cursor(BufferView * bv) const
2341 if (the_locking_inset)
2342 return the_locking_inset->cursor(bv);
2343 return getLyXText(bv)->cursor;
2347 Inset * InsetText::getInsetFromID(int id_arg) const
2350 return const_cast<InsetText *>(this);
2352 ParagraphList::const_iterator pit = paragraphs.begin();
2353 ParagraphList::const_iterator pend = paragraphs.end();
2354 for (; pit != pend; ++pit) {
2355 InsetList::const_iterator it = pit->insetlist.begin();
2356 InsetList::const_iterator end = pit->insetlist.end();
2357 for (; it != end; ++it) {
2358 if (it->inset->id() == id_arg)
2360 Inset * in = it->inset->getInsetFromID(id_arg);
2370 InsetText::selectNextWordToSpellcheck(BufferView * bv,
2371 float & value) const
2377 lt = getLyXText(bv);
2380 if (the_locking_inset) {
2381 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2382 if (!word.word().empty()) {
2388 // we have to go on checking so move cursor to the next char
2389 lt->cursor.pos(lt->cursor.pos() + 1);
2391 word = lt->selectNextWordToSpellcheck(value);
2392 if (word.word().empty())
2393 bv->unlockInset(const_cast<InsetText *>(this));
2402 void InsetText::selectSelectedWord(BufferView * bv)
2404 if (the_locking_inset) {
2405 the_locking_inset->selectSelectedWord(bv);
2408 getLyXText(bv)->selectSelectedWord();
2409 updateLocal(bv, SELECTION, false);
2413 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2415 if (the_locking_inset) {
2416 the_locking_inset->toggleSelection(bv, kill_selection);
2420 lt = getLyXText(bv);
2424 int x = top_x + TEXT_TO_INSET_OFFSET;
2426 RowList::iterator rowit = lt->rows().begin();
2427 RowList::iterator end = lt->rows().end();
2428 int y_offset = top_baseline - rowit->ascent_of_text();
2430 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2431 y += rowit->height();
2437 if (need_update & SELECTION)
2439 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2445 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2449 lt = getLyXText(bv);
2452 if (the_locking_inset) {
2453 if (the_locking_inset->nextChange(bv, length))
2455 lt->cursorRight(true);
2457 lyxfind::SearchResult result =
2458 lyxfind::findNextChange(bv, lt, length);
2460 if (result == lyxfind::SR_FOUND) {
2461 LyXCursor cur = lt->cursor;
2462 bv->unlockInset(bv->theLockingInset());
2463 if (bv->lockInset(this))
2466 lt->setSelectionRange(length);
2467 updateLocal(bv, SELECTION, false);
2471 return result != lyxfind::SR_NOT_FOUND;
2475 bool InsetText::searchForward(BufferView * bv, string const & str,
2480 lt = getLyXText(bv);
2483 if (the_locking_inset) {
2484 if (the_locking_inset->searchForward(bv, str, cs, mw))
2486 lt->cursorRight(true);
2488 lyxfind::SearchResult result =
2489 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2491 if (result == lyxfind::SR_FOUND) {
2492 LyXCursor cur = lt->cursor;
2493 bv->unlockInset(bv->theLockingInset());
2494 if (bv->lockInset(this))
2497 lt->setSelectionRange(str.length());
2498 updateLocal(bv, SELECTION, false);
2502 return (result != lyxfind::SR_NOT_FOUND);
2505 bool InsetText::searchBackward(BufferView * bv, string const & str,
2508 if (the_locking_inset) {
2509 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2514 lt = getLyXText(bv);
2518 ParagraphList::iterator pit = paragraphs.begin();
2519 ParagraphList::iterator pend = paragraphs.end();
2521 while (boost::next(pit) != pend)
2524 lt->setCursor(pit, pit->size());
2526 lyxfind::SearchResult result =
2527 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2529 if (result == lyxfind::SR_FOUND) {
2530 LyXCursor cur = lt->cursor;
2531 bv->unlockInset(bv->theLockingInset());
2532 if (bv->lockInset(this))
2535 lt->setSelectionRange(str.length());
2536 updateLocal(bv, SELECTION, false);
2540 return (result != lyxfind::SR_NOT_FOUND);
2544 bool InsetText::checkInsertChar(LyXFont & font)
2547 return owner()->checkInsertChar(font);
2552 void InsetText::collapseParagraphs(BufferView * bv)
2554 LyXText * llt = getLyXText(bv);
2556 while (paragraphs.size() > 1) {
2557 ParagraphList::iterator first_par = paragraphs.begin();
2558 ParagraphList::iterator next_par = boost::next(first_par);
2559 size_t const first_par_size = first_par->size();
2561 if (!first_par->empty() &&
2562 !next_par->empty() &&
2563 !first_par->isSeparator(first_par_size - 1)) {
2564 first_par->insertChar(first_par_size, ' ');
2567 if (llt->selection.set()) {
2568 if (llt->selection.start.par() == next_par) {
2569 llt->selection.start.par(first_par);
2570 llt->selection.start.pos(
2571 llt->selection.start.pos() + first_par_size);
2573 if (llt->selection.end.par() == next_par) {
2574 llt->selection.end.par(first_par);
2575 llt->selection.end.pos(
2576 llt->selection.end.pos() + first_par_size);
2580 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2586 void InsetText::getDrawFont(LyXFont & font) const
2590 owner()->getDrawFont(font);
2594 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2596 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2597 // And it probably does. You have to take a look at this John. (Lgb)
2598 #warning John, have a look here. (Lgb)
2599 ParagraphList::iterator pit = plist.begin();
2600 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2602 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2604 ParagraphList::iterator pend = plist.end();
2605 for (; pit != pend; ++pit) {
2606 paragraphs.push_back(*pit);
2613 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2615 ParagraphList::const_iterator pit = paragraphs.begin();
2616 ParagraphList::const_iterator pend = paragraphs.end();
2618 for (; pit != pend; ++pit) {
2619 InsetList::const_iterator it = pit->insetlist.begin();
2620 InsetList::const_iterator end = pit->insetlist.end();
2621 for (; it != end; ++it) {
2622 it->inset->addPreview(loader);