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>
73 using namespace lyx::support;
76 using lyx::textclass_type;
78 namespace grfx = lyx::graphics;
80 // These functions should probably go into bufferview_funcs somehow (Jug)
82 void InsetText::saveLyXTextState() 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 == text_.cursor.par())
92 if (it != end && text_.cursor.pos() <= it->size())
93 sstate = text_; // slicing intended
95 sstate.cursor.par(end);
99 void InsetText::restoreLyXTextState() const
101 if (sstate.cursor.par() == const_cast<ParagraphList&>(paragraphs).end())
104 text_.selection.set(true);
105 // at this point just to avoid the DEPM when setting the cursor
106 text_.selection.mark(sstate.selection.mark());
107 if (sstate.selection.set()) {
108 text_.setCursor(sstate.selection.start.par(),
109 sstate.selection.start.pos(),
110 true, sstate.selection.start.boundary());
111 text_.selection.cursor = text_.cursor;
112 text_.setCursor(sstate.selection.end.par(), sstate.selection.end.pos(),
113 true, sstate.selection.end.boundary());
114 text_.setSelection();
115 text_.setCursor(sstate.cursor.par(), sstate.cursor.pos());
117 text_.setCursor(sstate.cursor.par(), sstate.cursor.pos(),
118 true, sstate.cursor.boundary());
119 text_.selection.cursor = text_.cursor;
120 text_.selection.set(false);
125 InsetText::InsetText(BufferParams const & bp)
126 : UpdatableInset(), in_update(false), text_(0, this)
128 paragraphs.push_back(Paragraph());
129 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
130 if (bp.tracking_changes)
131 paragraphs.begin()->trackChanges();
136 InsetText::InsetText(InsetText const & in)
137 : UpdatableInset(in), in_update(false), text_(0, this)
143 InsetText & InsetText::operator=(InsetText const & it)
150 void InsetText::init(InsetText const * ins)
153 text_.bv_owner = ins->text_.bv_owner;
154 setParagraphData(ins->paragraphs);
155 autoBreakRows = ins->autoBreakRows;
156 drawFrame_ = ins->drawFrame_;
157 frame_color = ins->frame_color;
160 frame_color = LColor::insetframe;
161 autoBreakRows = false;
163 the_locking_inset = 0;
164 for_each(paragraphs.begin(), paragraphs.end(),
165 boost::bind(&Paragraph::setInsetOwner, _1, this));
173 old_par = paragraphs.end();
174 last_drawn_width = -1;
175 sstate.cursor.par(paragraphs.end());
176 in_insetAllowed = false;
180 void InsetText::clear(bool just_mark_erased)
182 if (just_mark_erased) {
183 ParagraphList::iterator it = paragraphs.begin();
184 ParagraphList::iterator end = paragraphs.end();
185 for (; it != end; ++it) {
192 // This is a gross hack...
193 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
196 paragraphs.push_back(Paragraph());
197 paragraphs.begin()->setInsetOwner(this);
198 paragraphs.begin()->layout(old_layout);
205 InsetBase * InsetText::clone() const
207 return new InsetText(*this);
211 void InsetText::write(Buffer const * buf, ostream & os) const
214 writeParagraphData(buf, os);
218 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
220 ParagraphList::const_iterator it = paragraphs.begin();
221 ParagraphList::const_iterator end = paragraphs.end();
222 Paragraph::depth_type dth = 0;
223 for (; it != end; ++it) {
224 it->write(buf, os, buf->params, dth);
229 void InsetText::read(Buffer const * buf, LyXLex & lex)
232 Paragraph::depth_type depth = 0;
236 if (buf->params.tracking_changes)
237 paragraphs.begin()->trackChanges();
239 // delete the initial paragraph
241 ParagraphList::iterator pit = paragraphs.begin();
245 token = lex.getString();
248 if (token == "\\end_inset") {
252 if (token == "\\the_end") {
253 lex.printError("\\the_end read in inset! Error in document!");
258 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
261 pit = paragraphs.begin();
262 ParagraphList::iterator const end = paragraphs.end();
263 for (; pit != end; ++pit)
264 pit->setInsetOwner(this);
266 if (token != "\\end_inset") {
267 lex.printError("Missing \\end_inset at this point. "
274 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
276 BufferView * bv = mi.base.bv;
278 dim.asc = text_.rows().begin()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
279 dim.des = text_.height - dim.asc + TEXT_TO_INSET_OFFSET;
280 dim.wid = max(textWidth(bv), int(text_.width)) + 2 * TEXT_TO_INSET_OFFSET;
281 dim.wid = max(dim.wid, 10);
286 int InsetText::textWidth(BufferView * bv, bool fordraw) const
288 int w = autoBreakRows ? getMaxWidth(bv, this) : -1;
291 return max(w - 2 * TEXT_TO_INSET_OFFSET,
292 (int)getLyXText(bv)->width);
297 return w - 2 * TEXT_TO_INSET_OFFSET;
301 void InsetText::draw(PainterInfo & pi, int x, int baseline) const
306 // update our idea of where we are. Clearly, we should
307 // not have to know this information.
311 int const start_x = x;
313 BufferView * bv = pi.base.bv;
314 Painter & pain = pi.pain;
316 // repaint the background if needed
317 if (backgroundColor() != LColor::background)
318 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
320 // no draw is necessary !!!
321 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
322 top_baseline = baseline;
330 top_baseline = baseline;
331 top_y = baseline - dim_.asc;
333 if (last_drawn_width != dim_.wid) {
335 last_drawn_width = dim_.wid;
338 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
339 inset_x = cix(bv) - int(x) + drawTextXOffset;
340 inset_y = ciy(bv) + drawTextYOffset;
343 x += TEXT_TO_INSET_OFFSET;
345 RowList::iterator rowit = text_.rows().begin();
346 RowList::iterator end = text_.rows().end();
348 int y_offset = baseline - rowit->ascent_of_text();
349 int ph = pain.paperHeight();
352 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
353 y += rowit->height();
354 first += rowit->height();
358 text_.top_y(-y_offset);
366 int yf = y_offset + first;
371 while ((rowit != end) && (yf < ph)) {
372 RowPainter rp(*bv, text_, rowit);
373 rp.paint(y + y_offset + first, int(x), y + text_.top_y());
374 y += rowit->height();
375 yf += rowit->height();
381 if ((drawFrame_ == ALWAYS) || (drawFrame_ == LOCKED && locked)) {
382 drawFrame(pain, int(start_x));
385 if (need_update != INIT) {
391 void InsetText::drawFrame(Painter & pain, int x) const
393 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
395 frame_y = top_baseline - dim_.asc + ttoD2;
396 frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
397 frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
398 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
403 void InsetText::update(BufferView * bv, bool reinit)
406 text_.bv_owner = const_cast<BufferView *>(bv);
409 if (reinit && owner()) {
411 owner()->update(bv, true);
417 if (reinit || need_update == INIT) {
419 // we should put this call where we set need_update to INIT!
422 owner()->update(bv, true);
425 int nw = getMaxWidth(bv, this);
426 if (nw > 0 && old_max_width != nw) {
433 if (!autoBreakRows && paragraphs.size() > 1)
434 collapseParagraphs(bv);
436 if (the_locking_inset) {
437 inset_x = cix(bv) - top_x + drawTextXOffset;
438 inset_y = ciy(bv) + drawTextYOffset;
439 the_locking_inset->update(bv, reinit);
442 if ((need_update & CURSOR_PAR) && (text_.refreshStatus() == LyXText::REFRESH_NONE) &&
444 text_.updateInset(the_locking_inset);
447 if (text_.refreshStatus() == LyXText::REFRESH_AREA)
452 int nw = getMaxWidth(bv, this);
453 if (nw > 0 && old_max_width != nw) {
460 void InsetText::setUpdateStatus(BufferView *, int what) const
463 // we have to redraw us full if our LyXText REFRESH_AREA or
464 // if we don't break row so that we only have one row to update!
465 if ((text_.refreshStatus() == LyXText::REFRESH_AREA) ||
467 (text_.refreshStatus() == LyXText::REFRESH_ROW)))
470 } else if (text_.refreshStatus() == LyXText::REFRESH_ROW) {
471 need_update |= CURSOR_PAR;
474 // this to not draw a selection when we redraw all of it!
475 if (need_update & CURSOR && !(need_update & SELECTION)) {
476 if (text_.selection.set())
478 text_.clearSelection();
483 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
485 if (!autoBreakRows && paragraphs.size() > 1)
486 collapseParagraphs(bv);
488 text_.partialRebreak();
489 setUpdateStatus(bv, what);
490 bool flag = mark_dirty ||
491 (((need_update != CURSOR) && (need_update != NONE)) ||
492 (text_.refreshStatus() != LyXText::REFRESH_NONE) || text_.selection.set());
493 if (!text_.selection.set())
494 text_.selection.cursor = text_.cursor;
500 bv->updateInset(const_cast<InsetText *>(this));
503 if (need_update == CURSOR)
505 bv->owner()->view_state_changed();
506 bv->owner()->updateMenubar();
507 bv->owner()->updateToolbar();
508 if (old_par != cpar()) {
509 bv->owner()->setLayout(cpar()->layout()->name());
515 string const InsetText::editMessage() const
517 return _("Opened Text Inset");
521 void InsetText::insetUnlock(BufferView * bv)
523 if (the_locking_inset) {
524 the_locking_inset->insetUnlock(bv);
525 the_locking_inset = 0;
526 updateLocal(bv, CURSOR_PAR, false);
532 if (text_.selection.set()) {
533 text_.clearSelection();
535 } else if (owner()) {
536 bv->owner()->setLayout(owner()->getLyXText(bv)
537 ->cursor.par()->layout()->name());
539 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
540 // hack for deleteEmptyParMech
541 ParagraphList::iterator first_par = paragraphs.begin();
542 if (!first_par->empty()) {
543 text_.setCursor(first_par, 0);
544 } else if (paragraphs.size() > 1) {
545 text_.setCursor(boost::next(first_par), 0);
548 updateLocal(bv, code, false);
551 setUpdateStatus(bv, code);
556 void InsetText::lockInset(BufferView * bv)
559 the_locking_inset = 0;
560 inset_pos = inset_x = inset_y = 0;
561 inset_boundary = false;
562 inset_par = paragraphs.end();
563 old_par = paragraphs.end();
564 text_.setCursor(paragraphs.begin(), 0);
565 text_.clearSelection();
567 // If the inset is empty set the language of the current font to the
568 // language to the surronding text (if different).
569 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
570 bv->getParentLanguage(this) != text_.current_font.language()) {
571 LyXFont font(LyXFont::ALL_IGNORE);
572 font.setLanguage(bv->getParentLanguage(this));
573 setFont(bv, font, false);
576 if (drawFrame_ == LOCKED)
577 code = CURSOR|DRAW_FRAME;
578 setUpdateStatus(bv, code);
582 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
584 the_locking_inset = inset;
585 inset_x = cix(bv) - top_x + drawTextXOffset;
586 inset_y = ciy(bv) + drawTextYOffset;
589 inset_boundary = cboundary();
590 updateLocal(bv, CURSOR, false);
594 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
596 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
600 if (!the_locking_inset) {
601 ParagraphList::iterator pit = paragraphs.begin();
602 ParagraphList::iterator pend = paragraphs.end();
604 int const id = inset->id();
605 for (; pit != pend; ++pit) {
606 InsetList::iterator it =
607 pit->insetlist.begin();
608 InsetList::iterator const end =
609 pit->insetlist.end();
610 for (; it != end; ++it) {
611 if (it->inset == inset) {
612 getLyXText(bv)->setCursorIntern(pit, it->pos);
613 lockInset(bv, inset);
616 if (it->inset->getInsetFromID(id)) {
617 getLyXText(bv)->setCursorIntern(pit, it->pos);
618 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
619 return the_locking_inset->lockInsetInInset(bv, inset);
625 if (inset == cpar()->getInset(cpos())) {
626 lyxerr[Debug::INSETS] << "OK" << endl;
627 lockInset(bv, inset);
629 } else if (the_locking_inset && (the_locking_inset == inset)) {
630 if (cpar() == inset_par && cpos() == inset_pos) {
631 lyxerr[Debug::INSETS] << "OK" << endl;
632 inset_x = cix(bv) - top_x + drawTextXOffset;
633 inset_y = ciy(bv) + drawTextYOffset;
635 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
637 } else if (the_locking_inset) {
638 lyxerr[Debug::INSETS] << "MAYBE" << endl;
639 return the_locking_inset->lockInsetInInset(bv, inset);
641 lyxerr[Debug::INSETS] << "NOT OK" << endl;
646 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
649 if (!the_locking_inset)
651 if (the_locking_inset == inset) {
652 the_locking_inset->insetUnlock(bv);
653 getLyXText(bv)->updateInset(inset);
654 the_locking_inset = 0;
656 moveRightIntern(bv, true, false);
657 old_par = paragraphs.end(); // force layout setting
661 updateLocal(bv, CURSOR, false);
664 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
668 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
670 if (!autoBreakRows && paragraphs.size() > 1)
671 collapseParagraphs(bv);
676 if (inset->owner() != this) {
677 int ustat = CURSOR_PAR;
679 UpdatableInset * tl_inset = the_locking_inset;
681 found = tl_inset->updateInsetInInset(bv, inset);
683 tl_inset = static_cast<UpdatableInset *>(inset);
684 while(tl_inset->owner() && tl_inset->owner() != this)
685 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
686 if (!tl_inset->owner())
688 found = tl_inset->updateInsetInInset(bv, inset);
692 text_.updateInset(tl_inset);
694 setUpdateStatus(bv, ustat);
697 bool found = text_.updateInset(inset);
699 setUpdateStatus(bv, CURSOR_PAR);
700 if (the_locking_inset &&
701 cpar() == inset_par && cpos() == inset_pos)
703 inset_x = cix(bv) - top_x + drawTextXOffset;
704 inset_y = ciy(bv) + drawTextYOffset;
711 void InsetText::lfunMousePress(FuncRequest const & cmd)
715 // use this to check mouse motion for selection!
719 BufferView * bv = cmd.view();
720 FuncRequest cmd1 = cmd;
726 int tmp_x = cmd.x - drawTextXOffset;
727 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
728 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
730 if (the_locking_inset) {
731 if (the_locking_inset == inset) {
732 the_locking_inset->localDispatch(cmd1);
735 // otherwise only unlock the_locking_inset
736 the_locking_inset->insetUnlock(bv);
737 the_locking_inset = 0;
740 no_selection = false;
742 if (bv->theLockingInset()) {
743 if (isHighlyEditableInset(inset)) {
744 // We just have to lock the inset before calling a
746 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
747 if (!bv->lockInset(uinset)) {
748 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
750 inset->localDispatch(cmd1);
751 if (the_locking_inset)
752 updateLocal(bv, CURSOR, false);
757 bool paste_internally = false;
758 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
759 localDispatch(FuncRequest(bv, LFUN_COPY));
760 paste_internally = true;
762 int old_top_y = text_.top_y();
764 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
766 // set the selection cursor!
767 text_.selection.cursor = text_.cursor;
768 text_.cursor.x_fix(text_.cursor.x());
770 if (text_.selection.set()) {
771 text_.clearSelection();
772 updateLocal(bv, FULL, false);
774 text_.clearSelection();
775 updateLocal(bv, CURSOR, false);
778 bv->owner()->setLayout(cpar()->layout()->name());
780 // we moved the view we cannot do mouse selection in this case!
781 if (getLyXText(bv)->top_y() != old_top_y)
784 // Insert primary selection with middle mouse
785 // if there is a local selection in the current buffer,
787 if (cmd.button() == mouse_button::button2) {
788 if (paste_internally)
789 localDispatch(FuncRequest(bv, LFUN_PASTE));
791 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
794 getLyXText(bv)->clearSelection();
799 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
801 BufferView * bv = cmd.view();
802 FuncRequest cmd1 = cmd;
807 if (the_locking_inset)
808 return the_locking_inset->localDispatch(cmd1);
810 int tmp_x = cmd.x - drawTextXOffset;
811 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
812 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
815 // This code should probably be removed now. Simple insets
816 // (!highlyEditable) can actually take the localDispatch,
817 // and turn it into edit() if necessary. But we still
818 // need to deal properly with the whole relative vs.
819 // absolute mouse co-ords thing in a realiable, sensible way
821 if (isHighlyEditableInset(inset))
822 ret = inset->localDispatch(cmd1);
824 inset_x = cix(bv) - top_x + drawTextXOffset;
825 inset_y = ciy(bv) + drawTextYOffset;
826 cmd1.x = cmd.x - inset_x;
827 cmd1.y = cmd.x - inset_y;
828 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
832 ret = inset->localDispatch(cmd1);
833 updateLocal(bv, CURSOR_PAR, false);
840 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
842 FuncRequest cmd1 = cmd;
846 if (the_locking_inset) {
847 the_locking_inset->localDispatch(cmd1);
851 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
854 BufferView * bv = cmd.view();
855 LyXCursor cur = text_.cursor;
856 text_.setCursorFromCoordinates
857 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
858 text_.cursor.x_fix(text_.cursor.x());
859 if (cur == text_.cursor)
861 text_.setSelection();
862 bool flag = (text_.toggle_cursor.par() != text_.toggle_end_cursor.par() ||
863 text_.toggle_cursor.pos() != text_.toggle_end_cursor.pos());
865 updateLocal(bv, SELECTION, false);
870 Inset::RESULT InsetText::localDispatch(FuncRequest const & cmd)
872 BufferView * bv = cmd.view();
876 if (cmd.action == LFUN_INSET_EDIT) {
877 UpdatableInset::localDispatch(cmd);
879 if (!bv->lockInset(this)) {
880 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
885 the_locking_inset = 0;
886 inset_pos = inset_x = inset_y = 0;
887 inset_boundary = false;
888 inset_par = paragraphs.end();
889 old_par = paragraphs.end();
892 if (cmd.argument.size()) {
893 if (cmd.argument == "left")
894 text_.setCursor(paragraphs.begin(), 0);
896 ParagraphList::iterator it = paragraphs.begin();
897 ParagraphList::iterator end = paragraphs.end();
898 while (boost::next(it) != end)
900 // int const pos = (p->size() ? p->size()-1 : p->size());
901 text_.setCursor(it, it->size());
904 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
905 // we put here -1 and not button as now the button in the
906 // edit call should not be needed we will fix this in 1.3.x
907 // cycle hopefully (Jug 20020509)
908 // FIXME: GUII I've changed this to none: probably WRONG
909 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
910 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
912 text_.cursor.x_fix(text_.cursor.x());
916 text_.clearSelection();
919 // If the inset is empty set the language of the current font to the
920 // language to the surronding text (if different).
921 if (paragraphs.begin()->empty() &&
922 paragraphs.size() == 1 &&
923 bv->getParentLanguage(this) != text_.current_font.language())
925 LyXFont font(LyXFont::ALL_IGNORE);
926 font.setLanguage(bv->getParentLanguage(this));
927 setFont(bv, font, false);
931 if (drawFrame_ == LOCKED)
932 code = CURSOR | DRAW_FRAME;
934 updateLocal(bv, code, false);
935 // Tell the paragraph dialog that we've entered an insettext.
936 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
941 switch (cmd.action) {
942 case LFUN_MOUSE_PRESS:
945 case LFUN_MOUSE_MOTION:
946 lfunMouseMotion(cmd);
948 case LFUN_MOUSE_RELEASE:
949 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
954 bool was_empty = (paragraphs.begin()->empty() &&
955 paragraphs.size() == 1);
957 no_selection = false;
958 RESULT result = UpdatableInset::localDispatch(cmd);
959 if (result != UNDISPATCHED)
963 if (cmd.action < 0 && cmd.argument.empty())
966 if (the_locking_inset) {
967 result = the_locking_inset->localDispatch(cmd);
968 if (result == DISPATCHED_NOUPDATE)
970 else if (result == DISPATCHED) {
971 updateLocal(bv, CURSOR_PAR, false);
973 } else if (result >= FINISHED) {
976 moveRightIntern(bv, false, false);
980 if ((result = moveUp(bv)) >= FINISHED) {
981 updateLocal(bv, CURSOR, false);
982 bv->unlockInset(this);
986 if ((result = moveDown(bv)) >= FINISHED) {
987 updateLocal(bv, CURSOR, false);
988 bv->unlockInset(this);
995 the_locking_inset = 0;
996 updateLocal(bv, CURSOR, false);
997 // make sure status gets reset immediately
998 bv->owner()->clearMessage();
1003 int updflag = false;
1005 // what type of update to do on a cursor movement
1006 int cursor_update = CURSOR;
1008 if (text_.selection.set())
1009 cursor_update = SELECTION;
1011 switch (cmd.action) {
1014 case LFUN_SELFINSERT:
1015 if (bv->buffer()->isReadonly()) {
1016 // setErrorMessage(N_("Document is read only"));
1019 if (!cmd.argument.empty()) {
1020 /* Automatically delete the currently selected
1021 * text and replace it with what is being
1022 * typed in now. Depends on lyxrc settings
1023 * "auto_region_delete", which defaults to
1026 // This should not be needed here and is also WRONG!
1027 setUndo(bv, Undo::INSERT, text_.cursor.par());
1030 if (lyxrc.auto_region_delete) {
1031 if (text_.selection.set()) {
1032 text_.cutSelection(false, false);
1035 text_.clearSelection();
1036 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
1037 bv->owner()->getIntl().getTransManager().
1038 TranslateAndInsert(cmd.argument[i], &text_);
1041 text_.selection.cursor = text_.cursor;
1042 updwhat = CURSOR | CURSOR_PAR;
1044 result = DISPATCHED_NOUPDATE;
1047 // cursor movements that need special handling
1050 result = moveRight(bv);
1052 updwhat = cursor_update;
1056 result = moveLeft(bv);
1057 updwhat = cursor_update;
1061 result = moveDown(bv);
1062 updwhat = cursor_update;
1066 result = moveUp(bv);
1067 updwhat = cursor_update;
1071 if (crow() == text_.rows().begin())
1072 result = FINISHED_UP;
1074 text_.cursorPrevious();
1075 text_.clearSelection();
1076 result = DISPATCHED_NOUPDATE;
1078 updwhat = cursor_update;
1082 if (boost::next(crow()) == text_.rows().end())
1083 result = FINISHED_DOWN;
1086 text_.clearSelection();
1087 result = DISPATCHED_NOUPDATE;
1089 updwhat = cursor_update;
1092 case LFUN_BACKSPACE: {
1093 if (text_.selection.set())
1094 text_.cutSelection(true, false);
1097 updwhat = CURSOR_PAR;
1103 if (text_.selection.set()) {
1104 text_.cutSelection(true, false);
1108 updwhat = CURSOR_PAR;
1114 text_.cutSelection(true, true);
1115 updwhat = CURSOR_PAR;
1122 text_.copySelection();
1123 updwhat = CURSOR_PAR;
1126 case LFUN_PASTESELECTION:
1128 string const clip(bv->getClipboard());
1132 if (cmd.argument == "paragraph") {
1133 text_.insertStringAsParagraphs(clip);
1135 text_.insertStringAsLines(clip);
1138 text_.clearSelection();
1140 updwhat = CURSOR_PAR;
1146 if (!autoBreakRows) {
1147 if (CutAndPaste::nrOfParagraphs() > 1) {
1148 #ifdef WITH_WARNINGS
1149 #warning FIXME horrendously bad UI
1151 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1156 size_t sel_index = 0;
1157 string const & arg = cmd.argument;
1158 if (isStrUnsignedInt(arg)) {
1159 size_t const paste_arg = strToUnsignedInt(arg);
1160 #warning FIXME Check if the arg is in the domain of available selections.
1161 sel_index = paste_arg;
1163 text_.pasteSelection(sel_index);
1165 text_.clearSelection();
1166 updwhat = CURSOR_PAR;
1171 case LFUN_BREAKPARAGRAPH:
1172 if (!autoBreakRows) {
1173 result = DISPATCHED;
1176 text_.breakParagraph(paragraphs, 0);
1177 updwhat = CURSOR | FULL;
1181 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1182 if (!autoBreakRows) {
1183 result = DISPATCHED;
1186 text_.breakParagraph(paragraphs, 1);
1187 updwhat = CURSOR | FULL;
1191 case LFUN_BREAKLINE: {
1192 if (!autoBreakRows) {
1193 result = DISPATCHED;
1197 text_.insertInset(new InsetNewline);
1198 updwhat = CURSOR | CURSOR_PAR;
1204 // do not set layouts on non breakable textinsets
1205 if (autoBreakRows) {
1206 string cur_layout = cpar()->layout()->name();
1208 // Derive layout number from given argument (string)
1209 // and current buffer's textclass (number). */
1210 LyXTextClass const & tclass =
1211 bv->buffer()->params.getLyXTextClass();
1212 string layout = cmd.argument;
1213 bool hasLayout = tclass.hasLayout(layout);
1215 // If the entry is obsolete, use the new one instead.
1217 string const & obs =
1218 tclass[layout]->obsoleted_by();
1223 // see if we found the layout number:
1225 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1226 bv->owner()->dispatch(lf);
1230 if (cur_layout != layout) {
1231 cur_layout = layout;
1232 text_.setLayout(layout);
1233 bv->owner()->setLayout(cpar()->layout()->name());
1234 updwhat = CURSOR_PAR;
1238 // reset the layout box
1239 bv->owner()->setLayout(cpar()->layout()->name());
1242 case LFUN_PARAGRAPH_SPACING:
1243 // This one is absolutely not working. When fiddling with this
1244 // it also seems to me that the paragraphs inside the insettext
1245 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1246 // FIXME: how old is this comment ? ...
1248 ParagraphList::iterator pit = text_.cursor.par();
1249 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1250 float cur_value = 1.0;
1251 if (cur_spacing == Spacing::Other) {
1252 cur_value = pit->params().spacing().getValue();
1255 istringstream istr(STRCONV(cmd.argument));
1258 Spacing::Space new_spacing = cur_spacing;
1259 float new_value = cur_value;
1261 lyxerr << "Missing argument to `paragraph-spacing'"
1263 } else if (tmp == "single") {
1264 new_spacing = Spacing::Single;
1265 } else if (tmp == "onehalf") {
1266 new_spacing = Spacing::Onehalf;
1267 } else if (tmp == "double") {
1268 new_spacing = Spacing::Double;
1269 } else if (tmp == "other") {
1270 new_spacing = Spacing::Other;
1273 lyxerr << "new_value = " << tmpval << endl;
1276 } else if (tmp == "default") {
1277 new_spacing = Spacing::Default;
1279 lyxerr << _("Unknown spacing argument: ")
1280 << cmd.argument << endl;
1282 if (cur_spacing != new_spacing || cur_value != new_value) {
1283 pit->params().spacing(Spacing(new_spacing, new_value));
1284 updwhat = CURSOR_PAR;
1290 // These need to do repaints but don't require
1291 // special handling otherwise. A *lot* of the
1292 // above could probably be done similarly ...
1297 case LFUN_WORDRIGHT:
1298 // these two are really unhandled ...
1300 case LFUN_BEGINNINGBUF:
1301 updwhat = cursor_update;
1302 if (!bv->dispatch(cmd))
1303 result = UNDISPATCHED;
1312 case LFUN_WORDLEFTSEL:
1313 case LFUN_WORDRIGHTSEL:
1314 updwhat = SELECTION;
1319 if (!bv->dispatch(cmd))
1320 result = UNDISPATCHED;
1325 updateLocal(bv, updwhat, updflag);
1326 /// If the action has deleted all text in the inset, we need to change the
1327 // language to the language of the surronding text.
1328 if (!was_empty && paragraphs.begin()->empty() &&
1329 paragraphs.size() == 1) {
1330 LyXFont font(LyXFont::ALL_IGNORE);
1331 font.setLanguage(bv->getParentLanguage(this));
1332 setFont(bv, font, false);
1335 if (result >= FINISHED)
1336 bv->unlockInset(this);
1338 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1339 result = DISPATCHED;
1344 int InsetText::latex(Buffer const * buf, ostream & os,
1345 LatexRunParams const & runparams) const
1348 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1349 return texrow.rows();
1353 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1355 unsigned int lines = 0;
1357 ParagraphList::const_iterator beg = paragraphs.begin();
1358 ParagraphList::const_iterator end = paragraphs.end();
1359 ParagraphList::const_iterator it = beg;
1360 for (; it != end; ++it) {
1361 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1362 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1369 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1371 unsigned int lines = 0;
1373 vector<string> environment_stack(10);
1374 vector<string> environment_inner(10);
1376 int const command_depth = 0;
1379 Paragraph::depth_type depth = 0; // paragraph depth
1381 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1382 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1384 for (; pit != pend; ++pit) {
1386 int desc_on = 0; // description mode
1388 LyXLayout_ptr const & style = pit->layout();
1390 // environment tag closing
1391 for (; depth > pit->params().depth(); --depth) {
1392 if (environment_inner[depth] != "!-- --") {
1393 item_name = "listitem";
1394 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1395 if (environment_inner[depth] == "varlistentry")
1396 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1398 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1399 environment_stack[depth].erase();
1400 environment_inner[depth].erase();
1403 if (depth == pit->params().depth()
1404 && environment_stack[depth] != style->latexname()
1405 && !environment_stack[depth].empty()) {
1406 if (environment_inner[depth] != "!-- --") {
1407 item_name= "listitem";
1408 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1409 if (environment_inner[depth] == "varlistentry")
1410 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1413 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1415 environment_stack[depth].erase();
1416 environment_inner[depth].erase();
1419 // Write opening SGML tags.
1420 switch (style->latextype) {
1421 case LATEX_PARAGRAPH:
1422 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1426 buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1430 case LATEX_ENVIRONMENT:
1431 case LATEX_ITEM_ENVIRONMENT:
1432 if (depth < pit->params().depth()) {
1433 depth = pit->params().depth();
1434 environment_stack[depth].erase();
1437 if (environment_stack[depth] != style->latexname()) {
1438 if (environment_stack.size() == depth + 1) {
1439 environment_stack.push_back("!-- --");
1440 environment_inner.push_back("!-- --");
1442 environment_stack[depth] = style->latexname();
1443 environment_inner[depth] = "!-- --";
1444 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1446 if (environment_inner[depth] != "!-- --") {
1447 item_name= "listitem";
1448 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1449 if (environment_inner[depth] == "varlistentry")
1450 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1454 if (style->latextype == LATEX_ENVIRONMENT) {
1455 if (!style->latexparam().empty()) {
1456 if (style->latexparam() == "CDATA")
1459 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1464 desc_on = (style->labeltype == LABEL_MANUAL);
1466 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1467 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1469 item_name = desc_on ? "term" : "para";
1470 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1474 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1478 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1481 // write closing SGML tags
1482 switch (style->latextype) {
1483 case LATEX_ENVIRONMENT:
1484 if (!style->latexparam().empty()) {
1485 if (style->latexparam() == "CDATA")
1488 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1491 case LATEX_ITEM_ENVIRONMENT:
1492 if (desc_on == 1) break;
1494 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1496 case LATEX_PARAGRAPH:
1497 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1500 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1506 for (int d = depth; d >= 0; --d) {
1507 if (!environment_stack[depth].empty()) {
1508 if (environment_inner[depth] != "!-- --") {
1509 item_name = "listitem";
1510 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1511 if (environment_inner[depth] == "varlistentry")
1512 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1515 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1523 void InsetText::validate(LaTeXFeatures & features) const
1525 for_each(paragraphs.begin(), paragraphs.end(),
1526 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1530 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1532 if (the_locking_inset) {
1533 the_locking_inset->getCursor(bv, x, y);
1537 y = cy(&bv) + InsetText::y();
1541 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1543 if (the_locking_inset) {
1544 the_locking_inset->getCursorPos(bv, x, y);
1547 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1548 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1552 int InsetText::insetInInsetY() const
1554 if (!the_locking_inset)
1557 return (inset_y + the_locking_inset->insetInInsetY());
1561 void InsetText::fitInsetCursor(BufferView * bv) const
1563 if (the_locking_inset) {
1564 the_locking_inset->fitInsetCursor(bv);
1567 LyXFont const font =
1568 getLyXText(bv)->getFont(bv->buffer(), cpar(), cpos());
1570 int const asc = font_metrics::maxAscent(font);
1571 int const desc = font_metrics::maxDescent(font);
1573 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1574 need_update |= FULL;
1579 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1581 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1582 return moveLeftIntern(bv, false, activate_inset, selecting);
1584 return moveRightIntern(bv, true, activate_inset, selecting);
1589 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1591 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1592 return moveRightIntern(bv, true, activate_inset, selecting);
1594 return moveLeftIntern(bv, false, activate_inset, selecting);
1599 InsetText::moveRightIntern(BufferView * bv, bool front,
1600 bool activate_inset, bool selecting)
1602 LyXText * text = getLyXText(bv);
1604 ParagraphList::iterator c_par = cpar();
1606 if (boost::next(c_par) == paragraphs.end() &&
1607 (cpos() >= c_par->size()))
1608 return FINISHED_RIGHT;
1609 if (activate_inset && checkAndActivateInset(bv, front))
1611 text->cursorRight(bv);
1613 text->clearSelection();
1614 return DISPATCHED_NOUPDATE;
1619 InsetText::moveLeftIntern(BufferView * bv, bool front,
1620 bool activate_inset, bool selecting)
1622 LyXText * text = getLyXText(bv);
1624 if (cpar() == paragraphs.begin() && (cpos() <= 0))
1626 text->cursorLeft(bv);
1628 text->clearSelection();
1629 if (activate_inset && checkAndActivateInset(bv, front))
1631 return DISPATCHED_NOUPDATE;
1635 Inset::RESULT InsetText::moveUp(BufferView * bv)
1637 if (crow() == getLyXText(bv)->rows().begin())
1639 getLyXText(bv)->cursorUp(bv);
1640 getLyXText(bv)->clearSelection();
1641 return DISPATCHED_NOUPDATE;
1645 Inset::RESULT InsetText::moveDown(BufferView * bv)
1647 if (boost::next(crow()) == getLyXText(bv)->rows().end())
1648 return FINISHED_DOWN;
1649 getLyXText(bv)->cursorDown(bv);
1650 getLyXText(bv)->clearSelection();
1651 return DISPATCHED_NOUPDATE;
1655 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1657 if (the_locking_inset) {
1658 if (the_locking_inset->insetAllowed(inset))
1659 return the_locking_inset->insertInset(bv, inset);
1662 inset->setOwner(this);
1663 getLyXText(bv)->insertInset(inset);
1665 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1670 bool InsetText::insetAllowed(Inset::Code code) const
1672 // in_insetAllowed is a really gross hack,
1673 // to allow us to call the owner's insetAllowed
1674 // without stack overflow, which can happen
1675 // when the owner uses InsetCollapsable::insetAllowed()
1677 if (in_insetAllowed)
1679 in_insetAllowed = true;
1680 if (the_locking_inset)
1681 ret = the_locking_inset->insetAllowed(code);
1683 ret = owner()->insetAllowed(code);
1684 in_insetAllowed = false;
1689 UpdatableInset * InsetText::getLockingInset() const
1691 return the_locking_inset ? the_locking_inset->getLockingInset() :
1692 const_cast<InsetText *>(this);
1696 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1700 if (the_locking_inset)
1701 return the_locking_inset->getFirstLockingInsetOfType(c);
1706 bool InsetText::showInsetDialog(BufferView * bv) const
1708 if (the_locking_inset)
1709 return the_locking_inset->showInsetDialog(bv);
1714 void InsetText::getLabelList(std::vector<string> & list) const
1716 ParagraphList::const_iterator pit = paragraphs.begin();
1717 ParagraphList::const_iterator pend = paragraphs.end();
1718 for (; pit != pend; ++pit) {
1719 InsetList::const_iterator beg = pit->insetlist.begin();
1720 InsetList::const_iterator end = pit->insetlist.end();
1721 for (; beg != end; ++beg)
1722 beg->inset->getLabelList(list);
1727 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1730 if (the_locking_inset) {
1731 the_locking_inset->setFont(bv, font, toggleall, selectall);
1735 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1736 || cpar()->empty()) {
1737 getLyXText(bv)->setFont(font, toggleall);
1742 if (text_.selection.set()) {
1743 setUndo(bv, Undo::EDIT, text_.cursor.par());
1749 text_.toggleFree(font, toggleall);
1752 text_.clearSelection();
1756 bool flag = (selectall || text_.selection.set());
1759 updateLocal(bv, FULL, true);
1761 updateLocal(bv, CURSOR_PAR, true);
1765 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1767 if (cpar()->isInset(cpos())) {
1769 static_cast<UpdatableInset*>(cpar()->getInset(cpos()));
1770 if (!isHighlyEditableInset(inset))
1772 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1773 inset->localDispatch(cmd);
1774 if (!the_locking_inset)
1776 updateLocal(bv, CURSOR, false);
1783 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1784 mouse_button::state button)
1786 x -= drawTextXOffset;
1788 int dummyy = y + dim_.asc;
1789 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1790 // we only do the edit() call if the inset was hit by the mouse
1791 // or if it is a highly editable inset. So we should call this
1792 // function from our own edit with button < 0.
1793 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1795 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1803 inset_x = cix(bv) - top_x + drawTextXOffset;
1804 inset_y = ciy(bv) + drawTextYOffset;
1805 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1806 inset->localDispatch(cmd);
1807 if (!the_locking_inset)
1809 updateLocal(bv, CURSOR, false);
1816 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1819 int w = UpdatableInset::getMaxWidth(bv, inset);
1824 w = w - top_x + owner()->x();
1827 w -= (2 * TEXT_TO_INSET_OFFSET);
1830 return UpdatableInset::getMaxWidth(bv, inset);
1835 void InsetText::setParagraphData(ParagraphList const & plist)
1837 // we have to unlock any locked inset otherwise we're in troubles
1838 the_locking_inset = 0;
1840 // But it it makes no difference that is a lot better.
1842 // See if this can be simplified when std::list is in effect.
1845 ParagraphList::const_iterator it = plist.begin();
1846 ParagraphList::const_iterator end = plist.end();
1847 for (; it != end; ++it) {
1848 paragraphs.push_back(*it);
1849 Paragraph & tmp = paragraphs.back();
1850 tmp.setInsetOwner(this);
1858 void InsetText::markNew(bool track_changes)
1860 ParagraphList::iterator pit = paragraphs.begin();
1861 ParagraphList::iterator pend = paragraphs.end();
1862 for (; pit != pend; ++pit) {
1863 if (track_changes) {
1864 pit->trackChanges();
1866 // no-op when not tracking
1867 pit->cleanChanges();
1873 void InsetText::setText(string const & data, LyXFont const & font)
1876 for (unsigned int i = 0; i < data.length(); ++i)
1877 paragraphs.begin()->insertChar(i, data[i], font);
1882 void InsetText::setAutoBreakRows(bool flag)
1884 if (flag != autoBreakRows) {
1885 autoBreakRows = flag;
1893 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1895 if (how != drawFrame_) {
1898 updateLocal(bv, DRAW_FRAME, false);
1903 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1905 if (frame_color != col) {
1908 updateLocal(bv, DRAW_FRAME, false);
1913 int InsetText::cx(BufferView * bv) const
1915 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1916 if (the_locking_inset) {
1917 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1918 text_.cursor.pos());
1919 if (font.isVisibleRightToLeft())
1920 x -= the_locking_inset->width(bv, font);
1926 int InsetText::cix(BufferView * bv) const
1928 int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1929 if (the_locking_inset) {
1930 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1931 text_.cursor.pos());
1932 if (font.isVisibleRightToLeft())
1933 x -= the_locking_inset->width(bv, font);
1939 int InsetText::cy(BufferView * bv) const
1941 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1945 int InsetText::ciy(BufferView * bv) const
1947 return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1951 pos_type InsetText::cpos() const
1953 return text_.cursor.pos();
1957 ParagraphList::iterator InsetText::cpar() const
1959 return text_.cursor.par();
1963 bool InsetText::cboundary() const
1965 return text_.cursor.boundary();
1969 RowList::iterator InsetText::crow() const
1971 return text_.cursorRow();
1975 LyXText * InsetText::getLyXText(BufferView const * bv,
1976 bool const recursive) const
1979 if (recursive && the_locking_inset)
1980 return the_locking_inset->getLyXText(bv, true);
1985 void InsetText::setViewCache(BufferView const * bv) const
1988 text_.bv_owner = const_cast<BufferView *>(bv);
1992 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1995 /// then remove all LyXText in text-insets
1996 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1997 const_cast<ParagraphList&>(paragraphs).end(),
1998 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
2003 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2005 if (paragraphs.size() == 1 && paragraphs.begin()->empty()) {
2006 // no data, resize not neccessary!
2007 // we have to do this as a fixed width may have changed!
2009 text_.init(bv, true);
2010 restoreLyXTextState();
2018 text_.bv_owner = bv;
2020 // one endless line, resize normally not necessary
2021 if (!force && getMaxWidth(bv, this) < 0)
2026 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2027 const_cast<ParagraphList&>(paragraphs).end(),
2028 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2030 text_.init(bv, true);
2031 restoreLyXTextState();
2033 if (the_locking_inset) {
2034 inset_x = cix(bv) - top_x + drawTextXOffset;
2035 inset_y = ciy(bv) + drawTextYOffset;
2038 text_.top_y(bv->screen().topCursorVisible(&text_));
2040 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2041 // this will scroll the screen such that the cursor becomes visible
2042 bv->updateScrollbar();
2044 need_update |= FULL;
2049 void InsetText::reinitLyXText() const
2051 BufferView * bv = text_.bv_owner;
2058 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2059 const_cast<ParagraphList&>(paragraphs).end(),
2060 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2062 text_.init(bv, true);
2063 restoreLyXTextState();
2064 if (the_locking_inset) {
2065 inset_x = cix(bv) - top_x + drawTextXOffset;
2066 inset_y = ciy(bv) + drawTextYOffset;
2068 text_.top_y(bv->screen().topCursorVisible(&text_));
2070 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2071 // this will scroll the screen such that the cursor becomes visible
2072 bv->updateScrollbar();
2079 void InsetText::removeNewlines()
2081 bool changed = false;
2083 ParagraphList::iterator it = paragraphs.begin();
2084 ParagraphList::iterator end = paragraphs.end();
2085 for (; it != end; ++it) {
2086 for (int i = 0; i < it->size(); ++i) {
2087 if (it->isNewline(i)) {
2098 bool InsetText::nodraw() const
2100 if (the_locking_inset)
2101 return the_locking_inset->nodraw();
2102 return UpdatableInset::nodraw();
2106 int InsetText::scroll(bool recursive) const
2108 int sx = UpdatableInset::scroll(false);
2110 if (recursive && the_locking_inset)
2111 sx += the_locking_inset->scroll(recursive);
2117 void InsetText::selectAll(BufferView * bv)
2119 getLyXText(bv)->cursorTop();
2120 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2121 getLyXText(bv)->cursorBottom();
2122 getLyXText(bv)->setSelection();
2126 void InsetText::clearSelection(BufferView * bv)
2128 getLyXText(bv)->clearSelection();
2132 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2134 Painter & pain = bv->painter();
2136 int h = dim_.asc + dim_.des;
2137 int ty = baseline - dim_.asc;
2143 if ((ty + h) > pain.paperHeight())
2144 h = pain.paperHeight();
2145 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2146 w = pain.paperWidth();
2147 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2152 ParagraphList * InsetText::getParagraphs(int i) const
2154 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2158 LyXCursor const & InsetText::cursor(BufferView * bv) const
2160 if (the_locking_inset)
2161 return the_locking_inset->cursor(bv);
2162 return getLyXText(bv)->cursor;
2166 Inset * InsetText::getInsetFromID(int id_arg) const
2169 return const_cast<InsetText *>(this);
2171 ParagraphList::const_iterator pit = paragraphs.begin();
2172 ParagraphList::const_iterator pend = paragraphs.end();
2173 for (; pit != pend; ++pit) {
2174 InsetList::const_iterator it = pit->insetlist.begin();
2175 InsetList::const_iterator end = pit->insetlist.end();
2176 for (; it != end; ++it) {
2177 if (it->inset->id() == id_arg)
2179 Inset * in = it->inset->getInsetFromID(id_arg);
2189 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2192 if (the_locking_inset) {
2193 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2194 if (!word.word().empty()) {
2198 // we have to go on checking so move cursor to the next char
2199 text_.cursor.pos(text_.cursor.pos() + 1);
2201 word = text_.selectNextWordToSpellcheck(value);
2202 if (word.word().empty())
2203 bv->unlockInset(const_cast<InsetText *>(this));
2210 void InsetText::selectSelectedWord(BufferView * bv)
2212 if (the_locking_inset) {
2213 the_locking_inset->selectSelectedWord(bv);
2216 getLyXText(bv)->selectSelectedWord();
2217 updateLocal(bv, SELECTION, false);
2221 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2223 if (the_locking_inset) {
2224 the_locking_inset->toggleSelection(bv, kill_selection);
2227 int x = top_x + TEXT_TO_INSET_OFFSET;
2229 RowList::iterator rowit = text_.rows().begin();
2230 RowList::iterator end = text_.rows().end();
2231 int y_offset = top_baseline - rowit->ascent_of_text();
2233 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2234 y += rowit->height();
2240 if (need_update & SELECTION)
2242 bv->screen().toggleSelection(&text_, bv, kill_selection, y_offset, x);
2246 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2248 if (the_locking_inset) {
2249 if (the_locking_inset->nextChange(bv, length))
2251 text_.cursorRight(true);
2253 lyxfind::SearchResult result =
2254 lyxfind::findNextChange(bv, &text_, length);
2256 if (result == lyxfind::SR_FOUND) {
2257 LyXCursor cur = text_.cursor;
2258 bv->unlockInset(bv->theLockingInset());
2259 if (bv->lockInset(this))
2262 text_.setSelectionRange(length);
2263 updateLocal(bv, SELECTION, false);
2265 return result != lyxfind::SR_NOT_FOUND;
2269 bool InsetText::searchForward(BufferView * bv, string const & str,
2272 if (the_locking_inset) {
2273 if (the_locking_inset->searchForward(bv, str, cs, mw))
2275 text_.cursorRight(true);
2277 lyxfind::SearchResult result =
2278 lyxfind::LyXFind(bv, &text_, str, true, cs, mw);
2280 if (result == lyxfind::SR_FOUND) {
2281 LyXCursor cur = text_.cursor;
2282 bv->unlockInset(bv->theLockingInset());
2283 if (bv->lockInset(this))
2286 text_.setSelectionRange(str.length());
2287 updateLocal(bv, SELECTION, false);
2289 return (result != lyxfind::SR_NOT_FOUND);
2292 bool InsetText::searchBackward(BufferView * bv, string const & str,
2295 if (the_locking_inset) {
2296 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2300 ParagraphList::iterator pit = paragraphs.begin();
2301 ParagraphList::iterator pend = paragraphs.end();
2303 while (boost::next(pit) != pend)
2306 text_.setCursor(pit, pit->size());
2308 lyxfind::SearchResult result =
2309 lyxfind::LyXFind(bv, &text_, str, false, cs, mw);
2311 if (result == lyxfind::SR_FOUND) {
2312 LyXCursor cur = text_.cursor;
2313 bv->unlockInset(bv->theLockingInset());
2314 if (bv->lockInset(this))
2317 text_.setSelectionRange(str.length());
2318 updateLocal(bv, SELECTION, false);
2320 return (result != lyxfind::SR_NOT_FOUND);
2324 bool InsetText::checkInsertChar(LyXFont & font)
2327 return owner()->checkInsertChar(font);
2332 void InsetText::collapseParagraphs(BufferView * bv)
2334 while (paragraphs.size() > 1) {
2335 ParagraphList::iterator first_par = paragraphs.begin();
2336 ParagraphList::iterator next_par = boost::next(first_par);
2337 size_t const first_par_size = first_par->size();
2339 if (!first_par->empty() &&
2340 !next_par->empty() &&
2341 !first_par->isSeparator(first_par_size - 1)) {
2342 first_par->insertChar(first_par_size, ' ');
2345 if (text_.selection.set()) {
2346 if (text_.selection.start.par() == next_par) {
2347 text_.selection.start.par(first_par);
2348 text_.selection.start.pos(
2349 text_.selection.start.pos() + first_par_size);
2351 if (text_.selection.end.par() == next_par) {
2352 text_.selection.end.par(first_par);
2353 text_.selection.end.pos(
2354 text_.selection.end.pos() + first_par_size);
2358 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2364 void InsetText::getDrawFont(LyXFont & font) const
2368 owner()->getDrawFont(font);
2372 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2374 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2375 // And it probably does. You have to take a look at this John. (Lgb)
2376 #warning John, have a look here. (Lgb)
2377 ParagraphList::iterator pit = plist.begin();
2378 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2380 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2382 ParagraphList::iterator pend = plist.end();
2383 for (; pit != pend; ++pit) {
2384 paragraphs.push_back(*pit);
2391 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2393 ParagraphList::const_iterator pit = paragraphs.begin();
2394 ParagraphList::const_iterator pend = paragraphs.end();
2396 for (; pit != pend; ++pit) {
2397 InsetList::const_iterator it = pit->insetlist.begin();
2398 InsetList::const_iterator end = pit->insetlist.end();
2399 for (; it != end; ++it) {
2400 it->inset->addPreview(loader);