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 "bufferview_funcs.h"
18 #include "CutAndPaste.h"
20 #include "dimension.h"
21 #include "funcrequest.h"
23 #include "errorlist.h"
25 #include "LaTeXFeatures.h"
29 #include "lyxcursor.h"
35 #include "paragraph.h"
36 #include "ParagraphParameters.h"
37 #include "trans_mgr.h"
38 #include "undo_funcs.h"
39 #include "WordLangTuple.h"
40 #include "paragraph_funcs.h"
42 #include "rowpainter.h"
43 #include "insetnewline.h"
44 #include "metricsinfo.h"
46 #include "frontends/Alert.h"
47 #include "frontends/Dialogs.h"
48 #include "frontends/font_metrics.h"
49 #include "frontends/LyXView.h"
50 #include "frontends/Painter.h"
51 #include "frontends/screen.h"
53 #include "support/textutils.h"
54 #include "support/LAssert.h"
55 #include "support/lstrings.h"
56 #include "support/lyxalgo.h" // lyx::count
58 #include <boost/bind.hpp>
75 using namespace lyx::support;
76 using namespace lyx::graphics;
77 using namespace bv_funcs;
80 using lyx::textclass_type;
82 // These functions should probably go into bufferview_funcs somehow (Jug)
84 void InsetText::saveLyXTextState() const
86 // check if my paragraphs are still valid
87 ParagraphList::iterator it = const_cast<ParagraphList&>(paragraphs).begin();
88 ParagraphList::iterator end = const_cast<ParagraphList&>(paragraphs).end();
89 for (; it != end; ++it) {
90 if (it == text_.cursor.par())
94 if (it != end && text_.cursor.pos() <= it->size())
95 sstate = text_; // slicing intended
97 sstate.cursor.par(end);
101 void InsetText::restoreLyXTextState() const
103 if (sstate.cursor.par() == const_cast<ParagraphList&>(paragraphs).end())
106 text_.selection.set(true);
107 // at this point just to avoid the DEPM when setting the cursor
108 text_.selection.mark(sstate.selection.mark());
109 if (sstate.selection.set()) {
110 text_.setCursor(sstate.selection.start.par(),
111 sstate.selection.start.pos(),
112 true, sstate.selection.start.boundary());
113 text_.selection.cursor = text_.cursor;
114 text_.setCursor(sstate.selection.end.par(), sstate.selection.end.pos(),
115 true, sstate.selection.end.boundary());
116 text_.setSelection();
117 text_.setCursor(sstate.cursor.par(), sstate.cursor.pos());
119 text_.setCursor(sstate.cursor.par(), sstate.cursor.pos(),
120 true, sstate.cursor.boundary());
121 text_.selection.cursor = text_.cursor;
122 text_.selection.set(false);
127 InsetText::InsetText(BufferParams const & bp)
128 : UpdatableInset(), text_(0, this)
130 paragraphs.push_back(Paragraph());
131 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
132 if (bp.tracking_changes)
133 paragraphs.begin()->trackChanges();
138 InsetText::InsetText(InsetText const & in)
139 : UpdatableInset(in), text_(0, this)
145 InsetText & InsetText::operator=(InsetText const & it)
152 void InsetText::init(InsetText const * ins)
155 textwidth_ = ins->textwidth_;
156 text_.bv_owner = ins->text_.bv_owner;
157 setParagraphData(ins->paragraphs);
158 autoBreakRows = ins->autoBreakRows;
159 drawFrame_ = ins->drawFrame_;
160 frame_color = ins->frame_color;
162 textwidth_ = 0; // unbounded
164 frame_color = LColor::insetframe;
165 autoBreakRows = false;
167 the_locking_inset = 0;
168 for_each(paragraphs.begin(), paragraphs.end(),
169 boost::bind(&Paragraph::setInsetOwner, _1, this));
176 old_par = paragraphs.end();
177 last_drawn_width = -1;
178 sstate.cursor.par(paragraphs.end());
179 in_insetAllowed = false;
183 void InsetText::clear(bool just_mark_erased)
185 if (just_mark_erased) {
186 ParagraphList::iterator it = paragraphs.begin();
187 ParagraphList::iterator end = paragraphs.end();
188 for (; it != end; ++it) {
195 // This is a gross hack...
196 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
199 paragraphs.push_back(Paragraph());
200 paragraphs.begin()->setInsetOwner(this);
201 paragraphs.begin()->layout(old_layout);
208 auto_ptr<InsetBase> InsetText::clone() const
210 return auto_ptr<InsetBase>(new InsetText(*this));
214 void InsetText::write(Buffer const * buf, ostream & os) const
217 writeParagraphData(buf, os);
221 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
223 ParagraphList::const_iterator it = paragraphs.begin();
224 ParagraphList::const_iterator end = paragraphs.end();
225 Paragraph::depth_type dth = 0;
226 for (; it != end; ++it) {
227 it->write(buf, os, buf->params, dth);
232 void InsetText::read(Buffer const * buf, LyXLex & lex)
235 Paragraph::depth_type depth = 0;
239 if (buf->params.tracking_changes)
240 paragraphs.begin()->trackChanges();
242 // delete the initial paragraph
244 ParagraphList::iterator pit = paragraphs.begin();
248 token = lex.getString();
251 if (token == "\\end_inset") {
255 if (token == "\\the_end") {
256 lex.printError("\\the_end read in inset! Error in document!");
261 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
264 pit = paragraphs.begin();
265 ParagraphList::iterator const end = paragraphs.end();
266 for (; pit != end; ++pit)
267 pit->setInsetOwner(this);
269 if (token != "\\end_inset") {
270 lex.printError("Missing \\end_inset at this point. "
277 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
279 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << "\n";
281 textwidth_ = mi.base.textwidth;
282 BufferView * bv = mi.base.bv;
284 text_.metrics(mi, dim);
285 dim.asc += TEXT_TO_INSET_OFFSET;
286 dim.des += TEXT_TO_INSET_OFFSET;
287 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
288 dim.wid = max(dim.wid, 10);
293 int InsetText::textWidth() const
299 void InsetText::draw(PainterInfo & pi, int x, int baseline) const
304 // update our idea of where we are. Clearly, we should
305 // not have to know this information.
308 int const start_x = x;
310 BufferView * bv = pi.base.bv;
311 Painter & pain = pi.pain;
313 // repaint the background if needed
314 if (backgroundColor() != LColor::background)
315 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
317 // no draw is necessary !!!
318 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
319 top_baseline = baseline;
327 top_baseline = baseline;
328 top_y = baseline - dim_.asc;
330 if (last_drawn_width != dim_.wid) {
332 last_drawn_width = dim_.wid;
335 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
336 inset_x = cix(bv) - x + drawTextXOffset;
337 inset_y = ciy() + drawTextYOffset;
340 x += TEXT_TO_INSET_OFFSET;
342 RowList::iterator rowit = text_.rows().begin();
343 RowList::iterator end = text_.rows().end();
345 int y_offset = baseline - rowit->ascent_of_text();
346 int ph = pain.paperHeight();
349 while (rowit != end && y + rowit->height() <= 0) {
350 y += rowit->height();
351 first += rowit->height();
355 text_.top_y(-y_offset);
363 int yf = y_offset + first;
368 while (rowit != end && yf < ph) {
369 paintRows(*bv, text_, rowit,
370 y + y_offset + first, int(x), y + text_.top_y());
371 y += rowit->height();
372 yf += rowit->height();
378 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked)) {
379 drawFrame(pain, int(start_x));
382 if (need_update != INIT) {
388 void InsetText::drawFrame(Painter & pain, int x) const
390 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
392 frame_y = top_baseline - dim_.asc + ttoD2;
393 frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
394 frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
395 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
400 void InsetText::setUpdateStatus(int what) const
403 // we will to redraw us full if our LyXText wants it
404 if (text_.needRefresh())
407 // this to not draw a selection when we redraw all of it!
408 if (need_update & CURSOR && !(need_update & SELECTION)) {
409 if (text_.selection.set())
411 text_.clearSelection();
416 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
421 if (!autoBreakRows && paragraphs.size() > 1)
422 collapseParagraphs(bv);
424 text_.partialRebreak();
425 setUpdateStatus(what);
426 bool flag = mark_dirty ||
427 ((need_update != CURSOR && need_update != NONE) ||
428 text_.needRefresh() || text_.selection.set());
429 if (!text_.selection.set())
430 text_.selection.cursor = text_.cursor;
436 bv->updateInset(const_cast<InsetText *>(this));
439 if (need_update == CURSOR)
441 bv->owner()->view_state_changed();
442 bv->owner()->updateMenubar();
443 bv->owner()->updateToolbar();
444 if (old_par != cpar()) {
445 bv->owner()->setLayout(cpar()->layout()->name());
451 string const InsetText::editMessage() const
453 return _("Opened Text Inset");
457 void InsetText::insetUnlock(BufferView * bv)
459 if (the_locking_inset) {
460 the_locking_inset->insetUnlock(bv);
461 the_locking_inset = 0;
462 updateLocal(bv, CURSOR_PAR, false);
468 if (text_.selection.set()) {
469 text_.clearSelection();
471 } else if (owner()) {
472 bv->owner()->setLayout(owner()->getLyXText(bv)
473 ->cursor.par()->layout()->name());
475 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
476 // hack for deleteEmptyParMech
477 ParagraphList::iterator first_par = paragraphs.begin();
478 if (!first_par->empty()) {
479 text_.setCursor(first_par, 0);
480 } else if (paragraphs.size() > 1) {
481 text_.setCursor(boost::next(first_par), 0);
484 updateLocal(bv, code, false);
487 setUpdateStatus(code);
492 void InsetText::lockInset(BufferView * bv)
495 the_locking_inset = 0;
496 inset_pos = inset_x = inset_y = 0;
497 inset_boundary = false;
498 inset_par = paragraphs.end();
499 old_par = paragraphs.end();
500 text_.setCursorIntern(paragraphs.begin(), 0);
501 text_.clearSelection();
503 // If the inset is empty set the language of the current font to the
504 // language to the surronding text (if different).
505 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
506 bv->getParentLanguage(this) != text_.current_font.language()) {
507 LyXFont font(LyXFont::ALL_IGNORE);
508 font.setLanguage(bv->getParentLanguage(this));
509 setFont(bv, font, false);
512 if (drawFrame_ == LOCKED)
513 code = CURSOR|DRAW_FRAME;
514 setUpdateStatus(code);
518 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
520 the_locking_inset = inset;
521 inset_x = cix(bv) - top_x + drawTextXOffset;
522 inset_y = ciy() + drawTextYOffset;
525 inset_boundary = cboundary();
526 //updateLocal(bv, CURSOR, false);
530 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
532 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
536 if (!the_locking_inset) {
537 ParagraphList::iterator pit = paragraphs.begin();
538 ParagraphList::iterator pend = paragraphs.end();
540 int const id = inset->id();
541 for (; pit != pend; ++pit) {
542 InsetList::iterator it = pit->insetlist.begin();
543 InsetList::iterator const end = pit->insetlist.end();
544 for (; it != end; ++it) {
545 if (it->inset == inset) {
546 lyxerr << "InsetText::lockInsetInInset: 1 a\n";
547 text_.setCursorIntern(pit, it->pos);
548 lyxerr << "InsetText::lockInsetInInset: 1 b\n";
549 lyxerr << "bv: " << bv << " inset: " << inset << "\n";
550 lockInset(bv, inset);
551 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
554 if (it->inset->getInsetFromID(id)) {
555 lyxerr << "InsetText::lockInsetInInset: 2\n";
556 text_.setCursorIntern(pit, it->pos);
557 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
558 return the_locking_inset->lockInsetInInset(bv, inset);
562 lyxerr << "InsetText::lockInsetInInset: 3\n";
565 if (inset == cpar()->getInset(cpos())) {
566 lyxerr[Debug::INSETS] << "OK" << endl;
567 lockInset(bv, inset);
571 if (the_locking_inset && the_locking_inset == inset) {
572 if (cpar() == inset_par && cpos() == inset_pos) {
573 lyxerr[Debug::INSETS] << "OK" << endl;
574 inset_x = cix(bv) - top_x + drawTextXOffset;
575 inset_y = ciy() + drawTextYOffset;
577 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
579 } else if (the_locking_inset) {
580 lyxerr[Debug::INSETS] << "MAYBE" << endl;
581 return the_locking_inset->lockInsetInInset(bv, inset);
583 lyxerr[Debug::INSETS] << "NOT OK" << endl;
588 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
591 if (!the_locking_inset)
593 if (the_locking_inset == inset) {
594 the_locking_inset->insetUnlock(bv);
595 getLyXText(bv)->updateInset(inset);
596 the_locking_inset = 0;
598 moveRightIntern(bv, true, false);
599 old_par = paragraphs.end(); // force layout setting
603 updateLocal(bv, CURSOR, false);
606 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
610 bool InsetText::updateInsetInInset(BufferView * bv, InsetOld * inset)
612 if (!autoBreakRows && paragraphs.size() > 1)
613 collapseParagraphs(bv);
618 if (inset->owner() != this) {
619 int ustat = CURSOR_PAR;
621 UpdatableInset * tl_inset = the_locking_inset;
623 found = tl_inset->updateInsetInInset(bv, inset);
625 tl_inset = static_cast<UpdatableInset *>(inset);
626 while(tl_inset->owner() && tl_inset->owner() != this)
627 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
628 if (!tl_inset->owner())
630 found = tl_inset->updateInsetInInset(bv, inset);
633 text_.updateInset(tl_inset);
634 setUpdateStatus(ustat);
638 bool found = text_.updateInset(inset);
640 setUpdateStatus(CURSOR_PAR);
641 if (the_locking_inset &&
642 cpar() == inset_par && cpos() == inset_pos)
644 inset_x = cix(bv) - top_x + drawTextXOffset;
645 inset_y = ciy() + drawTextYOffset;
652 void InsetText::lfunMousePress(FuncRequest const & cmd)
656 // use this to check mouse motion for selection!
660 BufferView * bv = cmd.view();
661 FuncRequest cmd1 = cmd;
667 int tmp_x = cmd.x - drawTextXOffset;
668 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
669 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
671 if (the_locking_inset) {
672 if (the_locking_inset == inset) {
673 the_locking_inset->localDispatch(cmd1);
676 // otherwise only unlock the_locking_inset
677 the_locking_inset->insetUnlock(bv);
678 the_locking_inset = 0;
681 no_selection = false;
683 if (bv->theLockingInset()) {
684 if (isHighlyEditableInset(inset)) {
685 // We just have to lock the inset before calling a
687 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
688 if (!bv->lockInset(uinset)) {
689 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
691 inset->localDispatch(cmd1);
692 if (the_locking_inset)
693 updateLocal(bv, CURSOR, false);
698 bool paste_internally = false;
699 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
700 localDispatch(FuncRequest(bv, LFUN_COPY));
701 paste_internally = true;
703 int old_top_y = text_.top_y();
705 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
707 // set the selection cursor!
708 text_.selection.cursor = text_.cursor;
709 text_.cursor.x_fix(text_.cursor.x());
711 if (text_.selection.set()) {
712 text_.clearSelection();
713 updateLocal(bv, FULL, false);
715 text_.clearSelection();
716 updateLocal(bv, CURSOR, false);
719 bv->owner()->setLayout(cpar()->layout()->name());
721 // we moved the view we cannot do mouse selection in this case!
722 if (getLyXText(bv)->top_y() != old_top_y)
725 // Insert primary selection with middle mouse
726 // if there is a local selection in the current buffer,
728 if (cmd.button() == mouse_button::button2) {
729 if (paste_internally)
730 localDispatch(FuncRequest(bv, LFUN_PASTE));
732 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
735 getLyXText(bv)->clearSelection();
740 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
742 BufferView * bv = cmd.view();
743 FuncRequest cmd1 = cmd;
748 if (the_locking_inset)
749 return the_locking_inset->localDispatch(cmd1);
751 int tmp_x = cmd.x - drawTextXOffset;
752 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
753 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
756 // This code should probably be removed now. Simple insets
757 // (!highlyEditable) can actually take the localDispatch,
758 // and turn it into edit() if necessary. But we still
759 // need to deal properly with the whole relative vs.
760 // absolute mouse co-ords thing in a realiable, sensible way
762 if (isHighlyEditableInset(inset))
763 ret = inset->localDispatch(cmd1);
765 inset_x = cix(bv) - top_x + drawTextXOffset;
766 inset_y = ciy() + drawTextYOffset;
767 cmd1.x = cmd.x - inset_x;
768 cmd1.y = cmd.x - inset_y;
769 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
773 ret = inset->localDispatch(cmd1);
774 updateLocal(bv, CURSOR_PAR, false);
781 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
783 FuncRequest cmd1 = cmd;
787 if (the_locking_inset) {
788 the_locking_inset->localDispatch(cmd1);
792 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
795 BufferView * bv = cmd.view();
796 LyXCursor cur = text_.cursor;
797 text_.setCursorFromCoordinates
798 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
799 text_.cursor.x_fix(text_.cursor.x());
800 if (cur == text_.cursor)
802 text_.setSelection();
803 bool flag = (text_.toggle_cursor.par() != text_.toggle_end_cursor.par() ||
804 text_.toggle_cursor.pos() != text_.toggle_end_cursor.pos());
806 updateLocal(bv, SELECTION, false);
811 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
813 BufferView * bv = cmd.view();
816 switch (cmd.action) {
817 case LFUN_INSET_EDIT: {
818 UpdatableInset::localDispatch(cmd);
820 if (!bv->lockInset(this)) {
821 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
826 the_locking_inset = 0;
830 inset_boundary = false;
831 inset_par = paragraphs.end();
832 old_par = paragraphs.end();
835 if (cmd.argument.size()) {
836 if (cmd.argument == "left")
837 text_.setCursorIntern(paragraphs.begin(), 0);
839 ParagraphList::iterator it = boost::prior(paragraphs.end());
840 text_.setCursor(it, it->size());
843 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
844 // we put here -1 and not button as now the button in the
845 // edit call should not be needed we will fix this in 1.3.x
846 // cycle hopefully (Jug 20020509)
847 // FIXME: GUII I've changed this to none: probably WRONG
848 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
849 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
851 text_.cursor.x_fix(text_.cursor.x());
855 text_.clearSelection();
858 // If the inset is empty set the language of the current font to the
859 // language to the surronding text (if different).
860 if (paragraphs.begin()->empty() &&
861 paragraphs.size() == 1 &&
862 bv->getParentLanguage(this) != text_.current_font.language())
864 LyXFont font(LyXFont::ALL_IGNORE);
865 font.setLanguage(bv->getParentLanguage(this));
866 setFont(bv, font, false);
870 if (drawFrame_ == LOCKED)
871 code = CURSOR | DRAW_FRAME;
873 updateLocal(bv, code, false);
874 // Tell the paragraph dialog that we've entered an insettext.
875 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
879 case LFUN_MOUSE_PRESS:
883 case LFUN_MOUSE_MOTION:
884 lfunMouseMotion(cmd);
887 case LFUN_MOUSE_RELEASE:
888 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
894 bool was_empty = (paragraphs.begin()->empty() &&
895 paragraphs.size() == 1);
897 no_selection = false;
898 RESULT result = UpdatableInset::localDispatch(cmd);
899 if (result != UNDISPATCHED)
903 if (cmd.action < 0 && cmd.argument.empty())
906 if (the_locking_inset) {
907 result = the_locking_inset->localDispatch(cmd);
908 if (result == DISPATCHED_NOUPDATE)
910 else if (result == DISPATCHED) {
911 updateLocal(bv, CURSOR_PAR, false);
913 } else if (result >= FINISHED) {
916 moveRightIntern(bv, false, false);
920 if ((result = moveUp(bv)) >= FINISHED) {
921 updateLocal(bv, CURSOR, false);
922 bv->unlockInset(this);
926 if ((result = moveDown(bv)) >= FINISHED) {
927 updateLocal(bv, CURSOR, false);
928 bv->unlockInset(this);
935 the_locking_inset = 0;
936 updateLocal(bv, CURSOR, false);
937 // make sure status gets reset immediately
938 bv->owner()->clearMessage();
945 // what type of update to do on a cursor movement
946 int cursor_update = CURSOR;
948 if (text_.selection.set())
949 cursor_update = SELECTION;
951 switch (cmd.action) {
954 case LFUN_SELFINSERT:
955 if (bv->buffer()->isReadonly()) {
956 // setErrorMessage(N_("Document is read only"));
959 if (!cmd.argument.empty()) {
960 /* Automatically delete the currently selected
961 * text and replace it with what is being
962 * typed in now. Depends on lyxrc settings
963 * "auto_region_delete", which defaults to
966 // This should not be needed here and is also WRONG!
967 recordUndo(bv, Undo::INSERT, text_.cursor.par());
970 if (lyxrc.auto_region_delete) {
971 if (text_.selection.set()) {
972 text_.cutSelection(false, false);
975 text_.clearSelection();
976 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
977 bv->owner()->getIntl().getTransManager().
978 TranslateAndInsert(cmd.argument[i], &text_);
981 text_.selection.cursor = text_.cursor;
982 updwhat = CURSOR | CURSOR_PAR;
984 result = DISPATCHED_NOUPDATE;
987 // cursor movements that need special handling
990 result = moveRight(bv);
992 updwhat = cursor_update;
996 result = moveLeft(bv);
997 updwhat = cursor_update;
1001 result = moveDown(bv);
1002 updwhat = cursor_update;
1006 result = moveUp(bv);
1007 updwhat = cursor_update;
1011 if (crow() == text_.rows().begin())
1012 result = FINISHED_UP;
1014 text_.cursorPrevious();
1015 text_.clearSelection();
1016 result = DISPATCHED_NOUPDATE;
1018 updwhat = cursor_update;
1022 if (boost::next(crow()) == text_.rows().end())
1023 result = FINISHED_DOWN;
1026 text_.clearSelection();
1027 result = DISPATCHED_NOUPDATE;
1029 updwhat = cursor_update;
1032 case LFUN_BACKSPACE: {
1033 if (text_.selection.set())
1034 text_.cutSelection(true, false);
1037 updwhat = CURSOR_PAR;
1043 if (text_.selection.set()) {
1044 text_.cutSelection(true, false);
1048 updwhat = CURSOR_PAR;
1054 text_.cutSelection(true, true);
1055 updwhat = CURSOR_PAR;
1062 text_.copySelection();
1063 updwhat = CURSOR_PAR;
1066 case LFUN_PASTESELECTION:
1068 string const clip(bv->getClipboard());
1072 if (cmd.argument == "paragraph") {
1073 text_.insertStringAsParagraphs(clip);
1075 text_.insertStringAsLines(clip);
1078 text_.clearSelection();
1080 updwhat = CURSOR_PAR;
1086 if (!autoBreakRows) {
1087 if (CutAndPaste::nrOfParagraphs() > 1) {
1088 #ifdef WITH_WARNINGS
1089 #warning FIXME horrendously bad UI
1091 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1096 replaceSelection(bv->getLyXText());
1097 size_t sel_index = 0;
1098 string const & arg = cmd.argument;
1099 if (isStrUnsignedInt(arg)) {
1100 size_t const paste_arg = strToUnsignedInt(arg);
1101 #warning FIXME Check if the arg is in the domain of available selections.
1102 sel_index = paste_arg;
1104 text_.pasteSelection(sel_index);
1106 text_.clearSelection();
1107 updwhat = CURSOR_PAR;
1112 case LFUN_BREAKPARAGRAPH:
1113 if (!autoBreakRows) {
1114 result = DISPATCHED;
1117 replaceSelection(bv->getLyXText());
1118 text_.breakParagraph(paragraphs, 0);
1119 updwhat = CURSOR | FULL;
1123 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1124 if (!autoBreakRows) {
1125 result = DISPATCHED;
1128 replaceSelection(bv->getLyXText());
1129 text_.breakParagraph(paragraphs, 1);
1130 updwhat = CURSOR | FULL;
1134 case LFUN_BREAKLINE: {
1135 if (!autoBreakRows) {
1136 result = DISPATCHED;
1140 replaceSelection(bv->getLyXText());
1141 text_.insertInset(new InsetNewline);
1142 updwhat = CURSOR | CURSOR_PAR;
1148 // do not set layouts on non breakable textinsets
1149 if (autoBreakRows) {
1150 string cur_layout = cpar()->layout()->name();
1152 // Derive layout number from given argument (string)
1153 // and current buffer's textclass (number). */
1154 LyXTextClass const & tclass =
1155 bv->buffer()->params.getLyXTextClass();
1156 string layout = cmd.argument;
1157 bool hasLayout = tclass.hasLayout(layout);
1159 // If the entry is obsolete, use the new one instead.
1161 string const & obs =
1162 tclass[layout]->obsoleted_by();
1167 // see if we found the layout number:
1169 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1170 bv->owner()->dispatch(lf);
1174 if (cur_layout != layout) {
1175 cur_layout = layout;
1176 text_.setLayout(layout);
1177 bv->owner()->setLayout(cpar()->layout()->name());
1178 updwhat = CURSOR_PAR;
1182 // reset the layout box
1183 bv->owner()->setLayout(cpar()->layout()->name());
1186 case LFUN_PARAGRAPH_SPACING:
1187 // This one is absolutely not working. When fiddling with this
1188 // it also seems to me that the paragraphs inside the insettext
1189 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1190 // FIXME: how old is this comment ? ...
1192 ParagraphList::iterator pit = text_.cursor.par();
1193 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1194 float cur_value = 1.0;
1195 if (cur_spacing == Spacing::Other) {
1196 cur_value = pit->params().spacing().getValue();
1199 istringstream istr(STRCONV(cmd.argument));
1202 Spacing::Space new_spacing = cur_spacing;
1203 float new_value = cur_value;
1205 lyxerr << "Missing argument to `paragraph-spacing'"
1207 } else if (tmp == "single") {
1208 new_spacing = Spacing::Single;
1209 } else if (tmp == "onehalf") {
1210 new_spacing = Spacing::Onehalf;
1211 } else if (tmp == "double") {
1212 new_spacing = Spacing::Double;
1213 } else if (tmp == "other") {
1214 new_spacing = Spacing::Other;
1217 lyxerr << "new_value = " << tmpval << endl;
1220 } else if (tmp == "default") {
1221 new_spacing = Spacing::Default;
1223 lyxerr << _("Unknown spacing argument: ")
1224 << cmd.argument << endl;
1226 if (cur_spacing != new_spacing || cur_value != new_value) {
1227 pit->params().spacing(Spacing(new_spacing, new_value));
1228 updwhat = CURSOR_PAR;
1234 // These need to do repaints but don't require
1235 // special handling otherwise. A *lot* of the
1236 // above could probably be done similarly ...
1241 case LFUN_WORDRIGHT:
1242 // these two are really unhandled ...
1244 case LFUN_BEGINNINGBUF:
1245 updwhat = cursor_update;
1246 if (!bv->dispatch(cmd))
1247 result = UNDISPATCHED;
1256 case LFUN_WORDLEFTSEL:
1257 case LFUN_WORDRIGHTSEL:
1258 updwhat = SELECTION;
1263 if (!bv->dispatch(cmd))
1264 result = UNDISPATCHED;
1269 updateLocal(bv, updwhat, updflag);
1270 /// If the action has deleted all text in the inset, we need to change the
1271 // language to the language of the surronding text.
1272 if (!was_empty && paragraphs.begin()->empty() &&
1273 paragraphs.size() == 1) {
1274 LyXFont font(LyXFont::ALL_IGNORE);
1275 font.setLanguage(bv->getParentLanguage(this));
1276 setFont(bv, font, false);
1279 if (result >= FINISHED)
1280 bv->unlockInset(this);
1282 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1283 result = DISPATCHED;
1288 int InsetText::latex(Buffer const * buf, ostream & os,
1289 LatexRunParams const & runparams) const
1292 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1293 return texrow.rows();
1297 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1299 unsigned int lines = 0;
1301 ParagraphList::const_iterator beg = paragraphs.begin();
1302 ParagraphList::const_iterator end = paragraphs.end();
1303 ParagraphList::const_iterator it = beg;
1304 for (; it != end; ++it) {
1305 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1306 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1313 int InsetText::linuxdoc(Buffer const * buf, ostream & os) const
1315 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1316 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1318 // There is a confusion between the empty paragraph and the default paragraph
1319 // The default paragraph is <p></p>, the empty paragraph is *empty*
1320 // Since none of the floats of linuxdoc accepts standard paragraphs
1321 // I disable them. I don't expect problems. (jamatos 2003/07/27)
1322 for (; pit != pend; ++pit) {
1323 const string name = pit->layout()->latexname();
1325 sgml::openTag(os, 1, 0, name);
1326 buf->simpleLinuxDocOnePar(os, pit, 0);
1328 sgml::closeTag(os, 1, 0, name);
1334 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1336 unsigned int lines = 0;
1338 vector<string> environment_stack(10);
1339 vector<string> environment_inner(10);
1341 int const command_depth = 0;
1344 Paragraph::depth_type depth = 0; // paragraph depth
1346 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1347 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1349 for (; pit != pend; ++pit) {
1351 int desc_on = 0; // description mode
1353 LyXLayout_ptr const & style = pit->layout();
1355 // environment tag closing
1356 for (; depth > pit->params().depth(); --depth) {
1357 if (environment_inner[depth] != "!-- --") {
1358 item_name = "listitem";
1359 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1360 if (environment_inner[depth] == "varlistentry")
1361 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1363 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1364 environment_stack[depth].erase();
1365 environment_inner[depth].erase();
1368 if (depth == pit->params().depth()
1369 && environment_stack[depth] != style->latexname()
1370 && !environment_stack[depth].empty()) {
1371 if (environment_inner[depth] != "!-- --") {
1372 item_name= "listitem";
1373 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1374 if (environment_inner[depth] == "varlistentry")
1375 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1378 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1380 environment_stack[depth].erase();
1381 environment_inner[depth].erase();
1384 // Write opening SGML tags.
1385 switch (style->latextype) {
1386 case LATEX_PARAGRAPH:
1387 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1391 buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1395 case LATEX_ENVIRONMENT:
1396 case LATEX_ITEM_ENVIRONMENT:
1397 if (depth < pit->params().depth()) {
1398 depth = pit->params().depth();
1399 environment_stack[depth].erase();
1402 if (environment_stack[depth] != style->latexname()) {
1403 if (environment_stack.size() == depth + 1) {
1404 environment_stack.push_back("!-- --");
1405 environment_inner.push_back("!-- --");
1407 environment_stack[depth] = style->latexname();
1408 environment_inner[depth] = "!-- --";
1409 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1411 if (environment_inner[depth] != "!-- --") {
1412 item_name= "listitem";
1413 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1414 if (environment_inner[depth] == "varlistentry")
1415 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1419 if (style->latextype == LATEX_ENVIRONMENT) {
1420 if (!style->latexparam().empty()) {
1421 if (style->latexparam() == "CDATA")
1424 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1429 desc_on = (style->labeltype == LABEL_MANUAL);
1431 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1432 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1434 item_name = desc_on ? "term" : "para";
1435 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1439 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1443 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1446 // write closing SGML tags
1447 switch (style->latextype) {
1448 case LATEX_ENVIRONMENT:
1449 if (!style->latexparam().empty()) {
1450 if (style->latexparam() == "CDATA")
1453 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1456 case LATEX_ITEM_ENVIRONMENT:
1457 if (desc_on == 1) break;
1459 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1461 case LATEX_PARAGRAPH:
1462 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1465 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1471 for (int d = depth; d >= 0; --d) {
1472 if (!environment_stack[depth].empty()) {
1473 if (environment_inner[depth] != "!-- --") {
1474 item_name = "listitem";
1475 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1476 if (environment_inner[depth] == "varlistentry")
1477 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1480 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1488 void InsetText::validate(LaTeXFeatures & features) const
1490 for_each(paragraphs.begin(), paragraphs.end(),
1491 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1495 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1497 if (the_locking_inset) {
1498 the_locking_inset->getCursor(bv, x, y);
1502 y = cy() + InsetText::y();
1506 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1508 if (the_locking_inset) {
1509 the_locking_inset->getCursorPos(bv, x, y);
1512 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1513 y = cy() - TEXT_TO_INSET_OFFSET;
1517 int InsetText::insetInInsetY() const
1519 if (!the_locking_inset)
1522 return inset_y + the_locking_inset->insetInInsetY();
1526 void InsetText::fitInsetCursor(BufferView * bv) const
1528 if (the_locking_inset) {
1529 the_locking_inset->fitInsetCursor(bv);
1532 LyXFont const font = text_.getFont(bv->buffer(), cpar(), cpos());
1534 int const asc = font_metrics::maxAscent(font);
1535 int const desc = font_metrics::maxDescent(font);
1537 if (bv->fitLockedInsetCursor(cx(bv), cy(), asc, desc))
1538 need_update |= FULL;
1543 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1545 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1546 return moveLeftIntern(bv, false, activate_inset, selecting);
1548 return moveRightIntern(bv, true, activate_inset, selecting);
1553 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1555 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1556 return moveRightIntern(bv, true, activate_inset, selecting);
1558 return moveLeftIntern(bv, false, activate_inset, selecting);
1563 InsetText::moveRightIntern(BufferView * bv, bool front,
1564 bool activate_inset, bool selecting)
1566 ParagraphList::iterator c_par = cpar();
1568 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1569 return FINISHED_RIGHT;
1570 if (activate_inset && checkAndActivateInset(bv, front))
1572 text_.cursorRight(bv);
1574 text_.clearSelection();
1575 return DISPATCHED_NOUPDATE;
1580 InsetText::moveLeftIntern(BufferView * bv, bool front,
1581 bool activate_inset, bool selecting)
1583 if (cpar() == paragraphs.begin() && cpos() <= 0)
1585 text_.cursorLeft(bv);
1587 text_.clearSelection();
1588 if (activate_inset && checkAndActivateInset(bv, front))
1590 return DISPATCHED_NOUPDATE;
1594 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1596 if (crow() == text_.rows().begin())
1599 text_.clearSelection();
1600 return DISPATCHED_NOUPDATE;
1604 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1606 if (boost::next(crow()) == text_.rows().end())
1607 return FINISHED_DOWN;
1608 text_.cursorDown(bv);
1609 text_.clearSelection();
1610 return DISPATCHED_NOUPDATE;
1614 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1616 if (the_locking_inset) {
1617 if (the_locking_inset->insetAllowed(inset))
1618 return the_locking_inset->insertInset(bv, inset);
1621 inset->setOwner(this);
1622 text_.insertInset(inset);
1624 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1629 bool InsetText::insetAllowed(InsetOld::Code code) const
1631 // in_insetAllowed is a really gross hack,
1632 // to allow us to call the owner's insetAllowed
1633 // without stack overflow, which can happen
1634 // when the owner uses InsetCollapsable::insetAllowed()
1636 if (in_insetAllowed)
1638 in_insetAllowed = true;
1639 if (the_locking_inset)
1640 ret = the_locking_inset->insetAllowed(code);
1642 ret = owner()->insetAllowed(code);
1643 in_insetAllowed = false;
1648 UpdatableInset * InsetText::getLockingInset() const
1650 return the_locking_inset ? the_locking_inset->getLockingInset() :
1651 const_cast<InsetText *>(this);
1655 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1659 if (the_locking_inset)
1660 return the_locking_inset->getFirstLockingInsetOfType(c);
1665 bool InsetText::showInsetDialog(BufferView * bv) const
1667 if (the_locking_inset)
1668 return the_locking_inset->showInsetDialog(bv);
1673 void InsetText::getLabelList(std::vector<string> & list) const
1675 ParagraphList::const_iterator pit = paragraphs.begin();
1676 ParagraphList::const_iterator pend = paragraphs.end();
1677 for (; pit != pend; ++pit) {
1678 InsetList::const_iterator beg = pit->insetlist.begin();
1679 InsetList::const_iterator end = pit->insetlist.end();
1680 for (; beg != end; ++beg)
1681 beg->inset->getLabelList(list);
1686 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1689 if (the_locking_inset) {
1690 the_locking_inset->setFont(bv, font, toggleall, selectall);
1694 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1695 || cpar()->empty()) {
1696 text_.setFont(font, toggleall);
1701 if (text_.selection.set())
1702 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1706 text_.selection.cursor = text_.cursor;
1707 text_.cursorBottom();
1708 text_.setSelection();
1711 text_.toggleFree(font, toggleall);
1714 text_.clearSelection();
1718 bool flag = (selectall || text_.selection.set());
1721 updateLocal(bv, FULL, true);
1723 updateLocal(bv, CURSOR_PAR, true);
1727 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1729 InsetOld * inset = cpar()->getInset(cpos());
1730 if (!isHighlyEditableInset(inset))
1732 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1733 inset->localDispatch(cmd);
1734 if (!the_locking_inset)
1736 updateLocal(bv, CURSOR, false);
1741 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1742 mouse_button::state button)
1744 x -= drawTextXOffset;
1746 int dummyy = y + dim_.asc;
1747 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1748 // we only do the edit() call if the inset was hit by the mouse
1749 // or if it is a highly editable inset. So we should call this
1750 // function from our own edit with button < 0.
1751 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1753 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1762 inset_x = cix(bv) - top_x + drawTextXOffset;
1763 inset_y = ciy() + drawTextYOffset;
1764 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1765 inset->localDispatch(cmd);
1766 if (!the_locking_inset)
1768 updateLocal(bv, CURSOR, false);
1773 void InsetText::setParagraphData(ParagraphList const & plist)
1775 // we have to unlock any locked inset otherwise we're in troubles
1776 the_locking_inset = 0;
1778 // But it it makes no difference that is a lot better.
1780 // See if this can be simplified when std::list is in effect.
1783 ParagraphList::const_iterator it = plist.begin();
1784 ParagraphList::const_iterator end = plist.end();
1785 for (; it != end; ++it) {
1786 paragraphs.push_back(*it);
1787 paragraphs.back().setInsetOwner(this);
1795 void InsetText::markNew(bool track_changes)
1797 ParagraphList::iterator pit = paragraphs.begin();
1798 ParagraphList::iterator pend = paragraphs.end();
1799 for (; pit != pend; ++pit) {
1800 if (track_changes) {
1801 pit->trackChanges();
1803 // no-op when not tracking
1804 pit->cleanChanges();
1810 void InsetText::setText(string const & data, LyXFont const & font)
1813 for (unsigned int i = 0; i < data.length(); ++i)
1814 paragraphs.begin()->insertChar(i, data[i], font);
1819 void InsetText::setAutoBreakRows(bool flag)
1821 if (flag != autoBreakRows) {
1822 autoBreakRows = flag;
1830 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1832 if (how != drawFrame_) {
1835 updateLocal(bv, DRAW_FRAME, false);
1840 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1842 if (frame_color != col) {
1845 updateLocal(bv, DRAW_FRAME, false);
1850 int InsetText::cx(BufferView * bv) const
1852 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1853 if (the_locking_inset) {
1854 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1855 text_.cursor.pos());
1856 if (font.isVisibleRightToLeft())
1857 x -= the_locking_inset->width();
1863 int InsetText::cix(BufferView * bv) const
1865 int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1866 if (the_locking_inset) {
1867 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1868 text_.cursor.pos());
1869 if (font.isVisibleRightToLeft())
1870 x -= the_locking_inset->width();
1876 int InsetText::cy() const
1878 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1882 int InsetText::ciy() const
1884 return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1888 pos_type InsetText::cpos() const
1890 return text_.cursor.pos();
1894 ParagraphList::iterator InsetText::cpar() const
1896 return text_.cursor.par();
1900 bool InsetText::cboundary() const
1902 return text_.cursor.boundary();
1906 RowList::iterator InsetText::crow() const
1908 return text_.cursorRow();
1912 LyXText * InsetText::getLyXText(BufferView const * bv,
1913 bool const recursive) const
1916 if (recursive && the_locking_inset)
1917 return the_locking_inset->getLyXText(bv, true);
1922 void InsetText::setViewCache(BufferView const * bv) const
1925 text_.bv_owner = const_cast<BufferView *>(bv);
1929 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1932 /// then remove all LyXText in text-insets
1933 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1934 const_cast<ParagraphList&>(paragraphs).end(),
1935 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1940 void InsetText::resizeLyXText(BufferView * bv, bool /*force*/) const
1942 if (paragraphs.size() == 1 && paragraphs.begin()->empty()) {
1943 // no data, resize not neccessary!
1944 // we have to do this as a fixed width may have changed!
1947 restoreLyXTextState();
1959 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1960 const_cast<ParagraphList&>(paragraphs).end(),
1961 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1964 restoreLyXTextState();
1966 // seems to be unneeded
1968 if (the_locking_inset) {
1969 inset_x = cix(bv) - top_x + drawTextXOffset;
1970 inset_y = ciy() + drawTextYOffset;
1973 text_.top_y(bv->screen().topCursorVisible(&text_));
1975 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
1976 // this will scroll the screen such that the cursor becomes visible
1977 bv->updateScrollbar();
1979 need_update |= FULL;
1985 void InsetText::reinitLyXText() const
1987 BufferView * bv = text_.bv_owner;
1994 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1995 const_cast<ParagraphList&>(paragraphs).end(),
1996 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1999 restoreLyXTextState();
2000 if (the_locking_inset) {
2001 inset_x = cix(bv) - top_x + drawTextXOffset;
2002 inset_y = ciy() + drawTextYOffset;
2004 text_.top_y(bv->screen().topCursorVisible(&text_));
2006 // this will scroll the screen such that the cursor becomes visible
2007 bv->updateScrollbar();
2014 void InsetText::removeNewlines()
2016 bool changed = false;
2018 ParagraphList::iterator it = paragraphs.begin();
2019 ParagraphList::iterator end = paragraphs.end();
2020 for (; it != end; ++it) {
2021 for (int i = 0; i < it->size(); ++i) {
2022 if (it->isNewline(i)) {
2033 bool InsetText::nodraw() const
2035 if (the_locking_inset)
2036 return the_locking_inset->nodraw();
2037 return UpdatableInset::nodraw();
2041 int InsetText::scroll(bool recursive) const
2043 int sx = UpdatableInset::scroll(false);
2045 if (recursive && the_locking_inset)
2046 sx += the_locking_inset->scroll(recursive);
2052 void InsetText::clearSelection(BufferView * bv)
2054 getLyXText(bv)->clearSelection();
2058 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2060 Painter & pain = bv->painter();
2062 int h = dim_.asc + dim_.des;
2063 int ty = baseline - dim_.asc;
2069 if ((ty + h) > pain.paperHeight())
2070 h = pain.paperHeight();
2071 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2072 w = pain.paperWidth();
2073 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2078 ParagraphList * InsetText::getParagraphs(int i) const
2080 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2084 LyXCursor const & InsetText::cursor(BufferView * bv) const
2086 if (the_locking_inset)
2087 return the_locking_inset->cursor(bv);
2088 return getLyXText(bv)->cursor;
2092 InsetOld * InsetText::getInsetFromID(int id_arg) const
2095 return const_cast<InsetText *>(this);
2097 ParagraphList::const_iterator pit = paragraphs.begin();
2098 ParagraphList::const_iterator pend = paragraphs.end();
2099 for (; pit != pend; ++pit) {
2100 InsetList::const_iterator it = pit->insetlist.begin();
2101 InsetList::const_iterator end = pit->insetlist.end();
2102 for (; it != end; ++it) {
2103 if (it->inset->id() == id_arg)
2105 InsetOld * in = it->inset->getInsetFromID(id_arg);
2115 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2118 if (the_locking_inset) {
2119 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2120 if (!word.word().empty()) {
2124 // we have to go on checking so move cursor to the next char
2125 text_.cursor.pos(text_.cursor.pos() + 1);
2127 word = text_.selectNextWordToSpellcheck(value);
2128 if (word.word().empty())
2129 bv->unlockInset(const_cast<InsetText *>(this));
2136 void InsetText::selectSelectedWord(BufferView * bv)
2138 if (the_locking_inset) {
2139 the_locking_inset->selectSelectedWord(bv);
2142 getLyXText(bv)->selectSelectedWord();
2143 updateLocal(bv, SELECTION, false);
2147 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2149 if (the_locking_inset) {
2150 the_locking_inset->toggleSelection(bv, kill_selection);
2153 int x = top_x + TEXT_TO_INSET_OFFSET;
2155 RowList::iterator rowit = text_.rows().begin();
2156 RowList::iterator end = text_.rows().end();
2157 int y_offset = top_baseline - rowit->ascent_of_text();
2159 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2160 y += rowit->height();
2166 if (need_update & SELECTION)
2168 bv->screen().toggleSelection(&text_, bv, kill_selection, y_offset, x);
2172 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2174 if (the_locking_inset) {
2175 if (the_locking_inset->nextChange(bv, length))
2177 text_.cursorRight(true);
2179 lyx::find::SearchResult result =
2180 lyx::find::findNextChange(bv, &text_, length);
2182 if (result == lyx::find::SR_FOUND) {
2183 LyXCursor cur = text_.cursor;
2184 bv->unlockInset(bv->theLockingInset());
2185 if (bv->lockInset(this))
2188 text_.setSelectionRange(length);
2189 updateLocal(bv, SELECTION, false);
2191 return result != lyx::find::SR_NOT_FOUND;
2195 bool InsetText::searchForward(BufferView * bv, string const & str,
2198 if (the_locking_inset) {
2199 if (the_locking_inset->searchForward(bv, str, cs, mw))
2201 text_.cursorRight(true);
2203 lyx::find::SearchResult result =
2204 lyx::find::find(bv, &text_, str, true, cs, mw);
2206 if (result == lyx::find::SR_FOUND) {
2207 LyXCursor cur = text_.cursor;
2208 bv->unlockInset(bv->theLockingInset());
2209 if (bv->lockInset(this))
2212 text_.setSelectionRange(str.length());
2213 updateLocal(bv, SELECTION, false);
2215 return (result != lyx::find::SR_NOT_FOUND);
2218 bool InsetText::searchBackward(BufferView * bv, string const & str,
2221 if (the_locking_inset) {
2222 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2226 ParagraphList::iterator pit = paragraphs.begin();
2227 ParagraphList::iterator pend = paragraphs.end();
2229 while (boost::next(pit) != pend)
2232 text_.setCursor(pit, pit->size());
2234 lyx::find::SearchResult result =
2235 lyx::find::find(bv, &text_, str, false, cs, mw);
2237 if (result == lyx::find::SR_FOUND) {
2238 LyXCursor cur = text_.cursor;
2239 bv->unlockInset(bv->theLockingInset());
2240 if (bv->lockInset(this))
2243 text_.setSelectionRange(str.length());
2244 updateLocal(bv, SELECTION, false);
2246 return (result != lyx::find::SR_NOT_FOUND);
2250 bool InsetText::checkInsertChar(LyXFont & font)
2253 return owner()->checkInsertChar(font);
2258 void InsetText::collapseParagraphs(BufferView * bv)
2260 while (paragraphs.size() > 1) {
2261 ParagraphList::iterator first_par = paragraphs.begin();
2262 ParagraphList::iterator next_par = boost::next(first_par);
2263 size_t const first_par_size = first_par->size();
2265 if (!first_par->empty() &&
2266 !next_par->empty() &&
2267 !first_par->isSeparator(first_par_size - 1)) {
2268 first_par->insertChar(first_par_size, ' ');
2271 if (text_.selection.set()) {
2272 if (text_.selection.start.par() == next_par) {
2273 text_.selection.start.par(first_par);
2274 text_.selection.start.pos(
2275 text_.selection.start.pos() + first_par_size);
2277 if (text_.selection.end.par() == next_par) {
2278 text_.selection.end.par(first_par);
2279 text_.selection.end.pos(
2280 text_.selection.end.pos() + first_par_size);
2284 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2290 void InsetText::getDrawFont(LyXFont & font) const
2294 owner()->getDrawFont(font);
2298 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2300 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2301 // And it probably does. You have to take a look at this John. (Lgb)
2302 #warning John, have a look here. (Lgb)
2303 ParagraphList::iterator pit = plist.begin();
2304 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2306 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2308 ParagraphList::iterator pend = plist.end();
2309 for (; pit != pend; ++pit) {
2310 paragraphs.push_back(*pit);
2317 void InsetText::addPreview(PreviewLoader & loader) const
2319 ParagraphList::const_iterator pit = paragraphs.begin();
2320 ParagraphList::const_iterator pend = paragraphs.end();
2322 for (; pit != pend; ++pit) {
2323 InsetList::const_iterator it = pit->insetlist.begin();
2324 InsetList::const_iterator end = pit->insetlist.end();
2325 for (; it != end; ++it) {
2326 it->inset->addPreview(loader);