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>
74 using namespace lyx::support;
75 using namespace bv_funcs;
78 using lyx::textclass_type;
80 namespace grfx = lyx::graphics;
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 InsetBase * InsetText::clone() const
210 return 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: " << getInsetName()
280 // << " width: " << mi.base.textwidth << "\n";
281 if (mi.base.textwidth)
282 textwidth_ = mi.base.textwidth;
283 BufferView * bv = mi.base.bv;
285 text_.rebuild(mi.base.textwidth);
286 dim.asc = text_.rows().begin()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
287 dim.des = text_.height - dim.asc + TEXT_TO_INSET_OFFSET;
288 dim.wid = max(textwidth_, int(text_.width)) + 2 * TEXT_TO_INSET_OFFSET;
289 dim.wid = max(dim.wid, 10);
294 int InsetText::textWidth() const
300 void InsetText::draw(PainterInfo & pi, int x, int baseline) const
305 // update our idea of where we are. Clearly, we should
306 // not have to know this information.
310 int const start_x = x;
312 BufferView * bv = pi.base.bv;
313 Painter & pain = pi.pain;
315 // repaint the background if needed
316 if (backgroundColor() != LColor::background)
317 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
319 // no draw is necessary !!!
320 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
321 top_baseline = baseline;
329 top_baseline = baseline;
330 top_y = baseline - dim_.asc;
332 if (last_drawn_width != dim_.wid) {
334 last_drawn_width = dim_.wid;
337 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
338 inset_x = cix(bv) - int(x) + drawTextXOffset;
339 inset_y = ciy() + drawTextYOffset;
342 x += TEXT_TO_INSET_OFFSET;
344 RowList::iterator rowit = text_.rows().begin();
345 RowList::iterator end = text_.rows().end();
347 int y_offset = baseline - rowit->ascent_of_text();
348 int ph = pain.paperHeight();
351 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
352 y += rowit->height();
353 first += rowit->height();
357 text_.top_y(-y_offset);
365 int yf = y_offset + first;
370 while (rowit != end && yf < ph) {
371 paintRows(*bv, text_, rowit,
372 y + y_offset + first, int(x), y + text_.top_y());
373 y += rowit->height();
374 yf += rowit->height();
380 if ((drawFrame_ == ALWAYS) || (drawFrame_ == LOCKED && locked)) {
381 drawFrame(pain, int(start_x));
384 if (need_update != INIT) {
390 void InsetText::drawFrame(Painter & pain, int x) const
392 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
394 frame_y = top_baseline - dim_.asc + ttoD2;
395 frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
396 frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
397 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
402 void InsetText::setUpdateStatus(int what) const
405 // we will to redraw us full if our LyXText wants it
406 if (text_.needRefresh())
409 // this to not draw a selection when we redraw all of it!
410 if (need_update & CURSOR && !(need_update & SELECTION)) {
411 if (text_.selection.set())
413 text_.clearSelection();
418 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
423 if (!autoBreakRows && paragraphs.size() > 1)
424 collapseParagraphs(bv);
426 text_.partialRebreak();
427 setUpdateStatus(what);
428 bool flag = mark_dirty ||
429 ((need_update != CURSOR && need_update != NONE) ||
430 text_.needRefresh() || text_.selection.set());
431 if (!text_.selection.set())
432 text_.selection.cursor = text_.cursor;
438 bv->updateInset(const_cast<InsetText *>(this));
441 if (need_update == CURSOR)
443 bv->owner()->view_state_changed();
444 bv->owner()->updateMenubar();
445 bv->owner()->updateToolbar();
446 if (old_par != cpar()) {
447 bv->owner()->setLayout(cpar()->layout()->name());
453 string const InsetText::editMessage() const
455 return _("Opened Text Inset");
459 void InsetText::insetUnlock(BufferView * bv)
461 if (the_locking_inset) {
462 the_locking_inset->insetUnlock(bv);
463 the_locking_inset = 0;
464 updateLocal(bv, CURSOR_PAR, false);
470 if (text_.selection.set()) {
471 text_.clearSelection();
473 } else if (owner()) {
474 bv->owner()->setLayout(owner()->getLyXText(bv)
475 ->cursor.par()->layout()->name());
477 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
478 // hack for deleteEmptyParMech
479 ParagraphList::iterator first_par = paragraphs.begin();
480 if (!first_par->empty()) {
481 text_.setCursor(first_par, 0);
482 } else if (paragraphs.size() > 1) {
483 text_.setCursor(boost::next(first_par), 0);
486 updateLocal(bv, code, false);
489 setUpdateStatus(code);
494 void InsetText::lockInset(BufferView * bv)
497 the_locking_inset = 0;
498 inset_pos = inset_x = inset_y = 0;
499 inset_boundary = false;
500 inset_par = paragraphs.end();
501 old_par = paragraphs.end();
502 text_.setCursorIntern(paragraphs.begin(), 0);
503 text_.clearSelection();
505 // If the inset is empty set the language of the current font to the
506 // language to the surronding text (if different).
507 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
508 bv->getParentLanguage(this) != text_.current_font.language()) {
509 LyXFont font(LyXFont::ALL_IGNORE);
510 font.setLanguage(bv->getParentLanguage(this));
511 setFont(bv, font, false);
514 if (drawFrame_ == LOCKED)
515 code = CURSOR|DRAW_FRAME;
516 setUpdateStatus(code);
520 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
522 the_locking_inset = inset;
523 inset_x = cix(bv) - top_x + drawTextXOffset;
524 inset_y = ciy() + drawTextYOffset;
527 inset_boundary = cboundary();
528 //updateLocal(bv, CURSOR, false);
532 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
534 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
538 if (!the_locking_inset) {
539 ParagraphList::iterator pit = paragraphs.begin();
540 ParagraphList::iterator pend = paragraphs.end();
542 int const id = inset->id();
543 for (; pit != pend; ++pit) {
544 InsetList::iterator it = pit->insetlist.begin();
545 InsetList::iterator const end = pit->insetlist.end();
546 for (; it != end; ++it) {
547 if (it->inset == inset) {
548 lyxerr << "InsetText::lockInsetInInset: 1 a\n";
549 text_.setCursorIntern(pit, it->pos);
550 lyxerr << "InsetText::lockInsetInInset: 1 b\n";
551 lyxerr << "bv: " << bv << " inset: " << inset << "\n";
552 lockInset(bv, inset);
553 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
556 if (it->inset->getInsetFromID(id)) {
557 lyxerr << "InsetText::lockInsetInInset: 2\n";
558 text_.setCursorIntern(pit, it->pos);
559 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
560 return the_locking_inset->lockInsetInInset(bv, inset);
564 lyxerr << "InsetText::lockInsetInInset: 3\n";
567 if (inset == cpar()->getInset(cpos())) {
568 lyxerr[Debug::INSETS] << "OK" << endl;
569 lockInset(bv, inset);
573 if (the_locking_inset && the_locking_inset == inset) {
574 if (cpar() == inset_par && cpos() == inset_pos) {
575 lyxerr[Debug::INSETS] << "OK" << endl;
576 inset_x = cix(bv) - top_x + drawTextXOffset;
577 inset_y = ciy() + drawTextYOffset;
579 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
581 } else if (the_locking_inset) {
582 lyxerr[Debug::INSETS] << "MAYBE" << endl;
583 return the_locking_inset->lockInsetInInset(bv, inset);
585 lyxerr[Debug::INSETS] << "NOT OK" << endl;
590 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
593 if (!the_locking_inset)
595 if (the_locking_inset == inset) {
596 the_locking_inset->insetUnlock(bv);
597 getLyXText(bv)->updateInset(inset);
598 the_locking_inset = 0;
600 moveRightIntern(bv, true, false);
601 old_par = paragraphs.end(); // force layout setting
605 updateLocal(bv, CURSOR, false);
608 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
612 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
614 if (!autoBreakRows && paragraphs.size() > 1)
615 collapseParagraphs(bv);
620 if (inset->owner() != this) {
621 int ustat = CURSOR_PAR;
623 UpdatableInset * tl_inset = the_locking_inset;
625 found = tl_inset->updateInsetInInset(bv, inset);
627 tl_inset = static_cast<UpdatableInset *>(inset);
628 while(tl_inset->owner() && tl_inset->owner() != this)
629 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
630 if (!tl_inset->owner())
632 found = tl_inset->updateInsetInInset(bv, inset);
636 text_.updateInset(tl_inset);
638 setUpdateStatus(ustat);
641 bool found = text_.updateInset(inset);
643 setUpdateStatus(CURSOR_PAR);
644 if (the_locking_inset &&
645 cpar() == inset_par && cpos() == inset_pos)
647 inset_x = cix(bv) - top_x + drawTextXOffset;
648 inset_y = ciy() + drawTextYOffset;
655 void InsetText::lfunMousePress(FuncRequest const & cmd)
659 // use this to check mouse motion for selection!
663 BufferView * bv = cmd.view();
664 FuncRequest cmd1 = cmd;
670 int tmp_x = cmd.x - drawTextXOffset;
671 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
672 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
674 if (the_locking_inset) {
675 if (the_locking_inset == inset) {
676 the_locking_inset->localDispatch(cmd1);
679 // otherwise only unlock the_locking_inset
680 the_locking_inset->insetUnlock(bv);
681 the_locking_inset = 0;
684 no_selection = false;
686 if (bv->theLockingInset()) {
687 if (isHighlyEditableInset(inset)) {
688 // We just have to lock the inset before calling a
690 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
691 if (!bv->lockInset(uinset)) {
692 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
694 inset->localDispatch(cmd1);
695 if (the_locking_inset)
696 updateLocal(bv, CURSOR, false);
701 bool paste_internally = false;
702 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
703 localDispatch(FuncRequest(bv, LFUN_COPY));
704 paste_internally = true;
706 int old_top_y = text_.top_y();
708 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
710 // set the selection cursor!
711 text_.selection.cursor = text_.cursor;
712 text_.cursor.x_fix(text_.cursor.x());
714 if (text_.selection.set()) {
715 text_.clearSelection();
716 updateLocal(bv, FULL, false);
718 text_.clearSelection();
719 updateLocal(bv, CURSOR, false);
722 bv->owner()->setLayout(cpar()->layout()->name());
724 // we moved the view we cannot do mouse selection in this case!
725 if (getLyXText(bv)->top_y() != old_top_y)
728 // Insert primary selection with middle mouse
729 // if there is a local selection in the current buffer,
731 if (cmd.button() == mouse_button::button2) {
732 if (paste_internally)
733 localDispatch(FuncRequest(bv, LFUN_PASTE));
735 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
738 getLyXText(bv)->clearSelection();
743 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
745 BufferView * bv = cmd.view();
746 FuncRequest cmd1 = cmd;
751 if (the_locking_inset)
752 return the_locking_inset->localDispatch(cmd1);
754 int tmp_x = cmd.x - drawTextXOffset;
755 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
756 Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
759 // This code should probably be removed now. Simple insets
760 // (!highlyEditable) can actually take the localDispatch,
761 // and turn it into edit() if necessary. But we still
762 // need to deal properly with the whole relative vs.
763 // absolute mouse co-ords thing in a realiable, sensible way
765 if (isHighlyEditableInset(inset))
766 ret = inset->localDispatch(cmd1);
768 inset_x = cix(bv) - top_x + drawTextXOffset;
769 inset_y = ciy() + drawTextYOffset;
770 cmd1.x = cmd.x - inset_x;
771 cmd1.y = cmd.x - inset_y;
772 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
776 ret = inset->localDispatch(cmd1);
777 updateLocal(bv, CURSOR_PAR, false);
784 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
786 FuncRequest cmd1 = cmd;
790 if (the_locking_inset) {
791 the_locking_inset->localDispatch(cmd1);
795 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
798 BufferView * bv = cmd.view();
799 LyXCursor cur = text_.cursor;
800 text_.setCursorFromCoordinates
801 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
802 text_.cursor.x_fix(text_.cursor.x());
803 if (cur == text_.cursor)
805 text_.setSelection();
806 bool flag = (text_.toggle_cursor.par() != text_.toggle_end_cursor.par() ||
807 text_.toggle_cursor.pos() != text_.toggle_end_cursor.pos());
809 updateLocal(bv, SELECTION, false);
814 Inset::RESULT InsetText::localDispatch(FuncRequest const & cmd)
816 BufferView * bv = cmd.view();
819 if (cmd.action == LFUN_INSET_EDIT) {
820 UpdatableInset::localDispatch(cmd);
822 if (!bv->lockInset(this)) {
823 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
828 the_locking_inset = 0;
829 inset_pos = inset_x = inset_y = 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 = paragraphs.begin();
840 ParagraphList::iterator end = paragraphs.end();
841 while (boost::next(it) != end)
843 // int const pos = (p->size() ? p->size()-1 : p->size());
844 text_.setCursor(it, it->size());
847 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
848 // we put here -1 and not button as now the button in the
849 // edit call should not be needed we will fix this in 1.3.x
850 // cycle hopefully (Jug 20020509)
851 // FIXME: GUII I've changed this to none: probably WRONG
852 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
853 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
855 text_.cursor.x_fix(text_.cursor.x());
859 text_.clearSelection();
862 // If the inset is empty set the language of the current font to the
863 // language to the surronding text (if different).
864 if (paragraphs.begin()->empty() &&
865 paragraphs.size() == 1 &&
866 bv->getParentLanguage(this) != text_.current_font.language())
868 LyXFont font(LyXFont::ALL_IGNORE);
869 font.setLanguage(bv->getParentLanguage(this));
870 setFont(bv, font, false);
874 if (drawFrame_ == LOCKED)
875 code = CURSOR | DRAW_FRAME;
877 updateLocal(bv, code, false);
878 // Tell the paragraph dialog that we've entered an insettext.
879 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
884 switch (cmd.action) {
885 case LFUN_MOUSE_PRESS:
888 case LFUN_MOUSE_MOTION:
889 lfunMouseMotion(cmd);
891 case LFUN_MOUSE_RELEASE:
892 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
897 bool was_empty = (paragraphs.begin()->empty() &&
898 paragraphs.size() == 1);
900 no_selection = false;
901 RESULT result = UpdatableInset::localDispatch(cmd);
902 if (result != UNDISPATCHED)
906 if (cmd.action < 0 && cmd.argument.empty())
909 if (the_locking_inset) {
910 result = the_locking_inset->localDispatch(cmd);
911 if (result == DISPATCHED_NOUPDATE)
913 else if (result == DISPATCHED) {
914 updateLocal(bv, CURSOR_PAR, false);
916 } else if (result >= FINISHED) {
919 moveRightIntern(bv, false, false);
923 if ((result = moveUp(bv)) >= FINISHED) {
924 updateLocal(bv, CURSOR, false);
925 bv->unlockInset(this);
929 if ((result = moveDown(bv)) >= FINISHED) {
930 updateLocal(bv, CURSOR, false);
931 bv->unlockInset(this);
938 the_locking_inset = 0;
939 updateLocal(bv, CURSOR, false);
940 // make sure status gets reset immediately
941 bv->owner()->clearMessage();
948 // what type of update to do on a cursor movement
949 int cursor_update = CURSOR;
951 if (text_.selection.set())
952 cursor_update = SELECTION;
954 switch (cmd.action) {
957 case LFUN_SELFINSERT:
958 if (bv->buffer()->isReadonly()) {
959 // setErrorMessage(N_("Document is read only"));
962 if (!cmd.argument.empty()) {
963 /* Automatically delete the currently selected
964 * text and replace it with what is being
965 * typed in now. Depends on lyxrc settings
966 * "auto_region_delete", which defaults to
969 // This should not be needed here and is also WRONG!
970 setUndo(bv, Undo::INSERT, text_.cursor.par());
973 if (lyxrc.auto_region_delete) {
974 if (text_.selection.set()) {
975 text_.cutSelection(false, false);
978 text_.clearSelection();
979 for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
980 bv->owner()->getIntl().getTransManager().
981 TranslateAndInsert(cmd.argument[i], &text_);
984 text_.selection.cursor = text_.cursor;
985 updwhat = CURSOR | CURSOR_PAR;
987 result = DISPATCHED_NOUPDATE;
990 // cursor movements that need special handling
993 result = moveRight(bv);
995 updwhat = cursor_update;
999 result = moveLeft(bv);
1000 updwhat = cursor_update;
1004 result = moveDown(bv);
1005 updwhat = cursor_update;
1009 result = moveUp(bv);
1010 updwhat = cursor_update;
1014 if (crow() == text_.rows().begin())
1015 result = FINISHED_UP;
1017 text_.cursorPrevious();
1018 text_.clearSelection();
1019 result = DISPATCHED_NOUPDATE;
1021 updwhat = cursor_update;
1025 if (boost::next(crow()) == text_.rows().end())
1026 result = FINISHED_DOWN;
1029 text_.clearSelection();
1030 result = DISPATCHED_NOUPDATE;
1032 updwhat = cursor_update;
1035 case LFUN_BACKSPACE: {
1036 if (text_.selection.set())
1037 text_.cutSelection(true, false);
1040 updwhat = CURSOR_PAR;
1046 if (text_.selection.set()) {
1047 text_.cutSelection(true, false);
1051 updwhat = CURSOR_PAR;
1057 text_.cutSelection(true, true);
1058 updwhat = CURSOR_PAR;
1065 text_.copySelection();
1066 updwhat = CURSOR_PAR;
1069 case LFUN_PASTESELECTION:
1071 string const clip(bv->getClipboard());
1075 if (cmd.argument == "paragraph") {
1076 text_.insertStringAsParagraphs(clip);
1078 text_.insertStringAsLines(clip);
1081 text_.clearSelection();
1083 updwhat = CURSOR_PAR;
1089 if (!autoBreakRows) {
1090 if (CutAndPaste::nrOfParagraphs() > 1) {
1091 #ifdef WITH_WARNINGS
1092 #warning FIXME horrendously bad UI
1094 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1099 replaceSelection(bv->getLyXText());
1100 size_t sel_index = 0;
1101 string const & arg = cmd.argument;
1102 if (isStrUnsignedInt(arg)) {
1103 size_t const paste_arg = strToUnsignedInt(arg);
1104 #warning FIXME Check if the arg is in the domain of available selections.
1105 sel_index = paste_arg;
1107 text_.pasteSelection(sel_index);
1109 text_.clearSelection();
1110 updwhat = CURSOR_PAR;
1115 case LFUN_BREAKPARAGRAPH:
1116 if (!autoBreakRows) {
1117 result = DISPATCHED;
1120 replaceSelection(bv->getLyXText());
1121 text_.breakParagraph(paragraphs, 0);
1122 updwhat = CURSOR | FULL;
1126 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1127 if (!autoBreakRows) {
1128 result = DISPATCHED;
1131 replaceSelection(bv->getLyXText());
1132 text_.breakParagraph(paragraphs, 1);
1133 updwhat = CURSOR | FULL;
1137 case LFUN_BREAKLINE: {
1138 if (!autoBreakRows) {
1139 result = DISPATCHED;
1143 replaceSelection(bv->getLyXText());
1144 text_.insertInset(new InsetNewline);
1145 updwhat = CURSOR | CURSOR_PAR;
1151 // do not set layouts on non breakable textinsets
1152 if (autoBreakRows) {
1153 string cur_layout = cpar()->layout()->name();
1155 // Derive layout number from given argument (string)
1156 // and current buffer's textclass (number). */
1157 LyXTextClass const & tclass =
1158 bv->buffer()->params.getLyXTextClass();
1159 string layout = cmd.argument;
1160 bool hasLayout = tclass.hasLayout(layout);
1162 // If the entry is obsolete, use the new one instead.
1164 string const & obs =
1165 tclass[layout]->obsoleted_by();
1170 // see if we found the layout number:
1172 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1173 bv->owner()->dispatch(lf);
1177 if (cur_layout != layout) {
1178 cur_layout = layout;
1179 text_.setLayout(layout);
1180 bv->owner()->setLayout(cpar()->layout()->name());
1181 updwhat = CURSOR_PAR;
1185 // reset the layout box
1186 bv->owner()->setLayout(cpar()->layout()->name());
1189 case LFUN_PARAGRAPH_SPACING:
1190 // This one is absolutely not working. When fiddling with this
1191 // it also seems to me that the paragraphs inside the insettext
1192 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1193 // FIXME: how old is this comment ? ...
1195 ParagraphList::iterator pit = text_.cursor.par();
1196 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1197 float cur_value = 1.0;
1198 if (cur_spacing == Spacing::Other) {
1199 cur_value = pit->params().spacing().getValue();
1202 istringstream istr(STRCONV(cmd.argument));
1205 Spacing::Space new_spacing = cur_spacing;
1206 float new_value = cur_value;
1208 lyxerr << "Missing argument to `paragraph-spacing'"
1210 } else if (tmp == "single") {
1211 new_spacing = Spacing::Single;
1212 } else if (tmp == "onehalf") {
1213 new_spacing = Spacing::Onehalf;
1214 } else if (tmp == "double") {
1215 new_spacing = Spacing::Double;
1216 } else if (tmp == "other") {
1217 new_spacing = Spacing::Other;
1220 lyxerr << "new_value = " << tmpval << endl;
1223 } else if (tmp == "default") {
1224 new_spacing = Spacing::Default;
1226 lyxerr << _("Unknown spacing argument: ")
1227 << cmd.argument << endl;
1229 if (cur_spacing != new_spacing || cur_value != new_value) {
1230 pit->params().spacing(Spacing(new_spacing, new_value));
1231 updwhat = CURSOR_PAR;
1237 // These need to do repaints but don't require
1238 // special handling otherwise. A *lot* of the
1239 // above could probably be done similarly ...
1244 case LFUN_WORDRIGHT:
1245 // these two are really unhandled ...
1247 case LFUN_BEGINNINGBUF:
1248 updwhat = cursor_update;
1249 if (!bv->dispatch(cmd))
1250 result = UNDISPATCHED;
1259 case LFUN_WORDLEFTSEL:
1260 case LFUN_WORDRIGHTSEL:
1261 updwhat = SELECTION;
1266 if (!bv->dispatch(cmd))
1267 result = UNDISPATCHED;
1272 updateLocal(bv, updwhat, updflag);
1273 /// If the action has deleted all text in the inset, we need to change the
1274 // language to the language of the surronding text.
1275 if (!was_empty && paragraphs.begin()->empty() &&
1276 paragraphs.size() == 1) {
1277 LyXFont font(LyXFont::ALL_IGNORE);
1278 font.setLanguage(bv->getParentLanguage(this));
1279 setFont(bv, font, false);
1282 if (result >= FINISHED)
1283 bv->unlockInset(this);
1285 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1286 result = DISPATCHED;
1291 int InsetText::latex(Buffer const * buf, ostream & os,
1292 LatexRunParams const & runparams) const
1295 latexParagraphs(buf, paragraphs, os, texrow, runparams);
1296 return texrow.rows();
1300 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1302 unsigned int lines = 0;
1304 ParagraphList::const_iterator beg = paragraphs.begin();
1305 ParagraphList::const_iterator end = paragraphs.end();
1306 ParagraphList::const_iterator it = beg;
1307 for (; it != end; ++it) {
1308 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1309 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1316 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1318 unsigned int lines = 0;
1320 vector<string> environment_stack(10);
1321 vector<string> environment_inner(10);
1323 int const command_depth = 0;
1326 Paragraph::depth_type depth = 0; // paragraph depth
1328 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1329 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1331 for (; pit != pend; ++pit) {
1333 int desc_on = 0; // description mode
1335 LyXLayout_ptr const & style = pit->layout();
1337 // environment tag closing
1338 for (; depth > pit->params().depth(); --depth) {
1339 if (environment_inner[depth] != "!-- --") {
1340 item_name = "listitem";
1341 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1342 if (environment_inner[depth] == "varlistentry")
1343 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1345 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1346 environment_stack[depth].erase();
1347 environment_inner[depth].erase();
1350 if (depth == pit->params().depth()
1351 && environment_stack[depth] != style->latexname()
1352 && !environment_stack[depth].empty()) {
1353 if (environment_inner[depth] != "!-- --") {
1354 item_name= "listitem";
1355 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1356 if (environment_inner[depth] == "varlistentry")
1357 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1360 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1362 environment_stack[depth].erase();
1363 environment_inner[depth].erase();
1366 // Write opening SGML tags.
1367 switch (style->latextype) {
1368 case LATEX_PARAGRAPH:
1369 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1373 buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1377 case LATEX_ENVIRONMENT:
1378 case LATEX_ITEM_ENVIRONMENT:
1379 if (depth < pit->params().depth()) {
1380 depth = pit->params().depth();
1381 environment_stack[depth].erase();
1384 if (environment_stack[depth] != style->latexname()) {
1385 if (environment_stack.size() == depth + 1) {
1386 environment_stack.push_back("!-- --");
1387 environment_inner.push_back("!-- --");
1389 environment_stack[depth] = style->latexname();
1390 environment_inner[depth] = "!-- --";
1391 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1393 if (environment_inner[depth] != "!-- --") {
1394 item_name= "listitem";
1395 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1396 if (environment_inner[depth] == "varlistentry")
1397 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1401 if (style->latextype == LATEX_ENVIRONMENT) {
1402 if (!style->latexparam().empty()) {
1403 if (style->latexparam() == "CDATA")
1406 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1411 desc_on = (style->labeltype == LABEL_MANUAL);
1413 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1414 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1416 item_name = desc_on ? "term" : "para";
1417 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1421 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1425 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1428 // write closing SGML tags
1429 switch (style->latextype) {
1430 case LATEX_ENVIRONMENT:
1431 if (!style->latexparam().empty()) {
1432 if (style->latexparam() == "CDATA")
1435 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1438 case LATEX_ITEM_ENVIRONMENT:
1439 if (desc_on == 1) break;
1441 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1443 case LATEX_PARAGRAPH:
1444 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1447 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1453 for (int d = depth; d >= 0; --d) {
1454 if (!environment_stack[depth].empty()) {
1455 if (environment_inner[depth] != "!-- --") {
1456 item_name = "listitem";
1457 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1458 if (environment_inner[depth] == "varlistentry")
1459 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1462 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1470 void InsetText::validate(LaTeXFeatures & features) const
1472 for_each(paragraphs.begin(), paragraphs.end(),
1473 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1477 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1479 if (the_locking_inset) {
1480 the_locking_inset->getCursor(bv, x, y);
1484 y = cy() + InsetText::y();
1488 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1490 if (the_locking_inset) {
1491 the_locking_inset->getCursorPos(bv, x, y);
1494 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1495 y = cy() - TEXT_TO_INSET_OFFSET;
1499 int InsetText::insetInInsetY() const
1501 if (!the_locking_inset)
1504 return (inset_y + the_locking_inset->insetInInsetY());
1508 void InsetText::fitInsetCursor(BufferView * bv) const
1510 if (the_locking_inset) {
1511 the_locking_inset->fitInsetCursor(bv);
1514 LyXFont const font =
1515 getLyXText(bv)->getFont(bv->buffer(), cpar(), cpos());
1517 int const asc = font_metrics::maxAscent(font);
1518 int const desc = font_metrics::maxDescent(font);
1520 if (bv->fitLockedInsetCursor(cx(bv), cy(), asc, desc))
1521 need_update |= FULL;
1526 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1528 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1529 return moveLeftIntern(bv, false, activate_inset, selecting);
1531 return moveRightIntern(bv, true, activate_inset, selecting);
1536 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1538 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1539 return moveRightIntern(bv, true, activate_inset, selecting);
1541 return moveLeftIntern(bv, false, activate_inset, selecting);
1546 InsetText::moveRightIntern(BufferView * bv, bool front,
1547 bool activate_inset, bool selecting)
1549 ParagraphList::iterator c_par = cpar();
1551 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1552 return FINISHED_RIGHT;
1553 if (activate_inset && checkAndActivateInset(bv, front))
1555 text_.cursorRight(bv);
1557 text_.clearSelection();
1558 return DISPATCHED_NOUPDATE;
1563 InsetText::moveLeftIntern(BufferView * bv, bool front,
1564 bool activate_inset, bool selecting)
1566 if (cpar() == paragraphs.begin() && cpos() <= 0)
1568 text_.cursorLeft(bv);
1570 text_.clearSelection();
1571 if (activate_inset && checkAndActivateInset(bv, front))
1573 return DISPATCHED_NOUPDATE;
1577 Inset::RESULT InsetText::moveUp(BufferView * bv)
1579 if (crow() == text_.rows().begin())
1582 text_.clearSelection();
1583 return DISPATCHED_NOUPDATE;
1587 Inset::RESULT InsetText::moveDown(BufferView * bv)
1589 if (boost::next(crow()) == text_.rows().end())
1590 return FINISHED_DOWN;
1591 text_.cursorDown(bv);
1592 text_.clearSelection();
1593 return DISPATCHED_NOUPDATE;
1597 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1599 if (the_locking_inset) {
1600 if (the_locking_inset->insetAllowed(inset))
1601 return the_locking_inset->insertInset(bv, inset);
1604 inset->setOwner(this);
1605 getLyXText(bv)->insertInset(inset);
1607 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1612 bool InsetText::insetAllowed(Inset::Code code) const
1614 // in_insetAllowed is a really gross hack,
1615 // to allow us to call the owner's insetAllowed
1616 // without stack overflow, which can happen
1617 // when the owner uses InsetCollapsable::insetAllowed()
1619 if (in_insetAllowed)
1621 in_insetAllowed = true;
1622 if (the_locking_inset)
1623 ret = the_locking_inset->insetAllowed(code);
1625 ret = owner()->insetAllowed(code);
1626 in_insetAllowed = false;
1631 UpdatableInset * InsetText::getLockingInset() const
1633 return the_locking_inset ? the_locking_inset->getLockingInset() :
1634 const_cast<InsetText *>(this);
1638 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1642 if (the_locking_inset)
1643 return the_locking_inset->getFirstLockingInsetOfType(c);
1648 bool InsetText::showInsetDialog(BufferView * bv) const
1650 if (the_locking_inset)
1651 return the_locking_inset->showInsetDialog(bv);
1656 void InsetText::getLabelList(std::vector<string> & list) const
1658 ParagraphList::const_iterator pit = paragraphs.begin();
1659 ParagraphList::const_iterator pend = paragraphs.end();
1660 for (; pit != pend; ++pit) {
1661 InsetList::const_iterator beg = pit->insetlist.begin();
1662 InsetList::const_iterator end = pit->insetlist.end();
1663 for (; beg != end; ++beg)
1664 beg->inset->getLabelList(list);
1669 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1672 if (the_locking_inset) {
1673 the_locking_inset->setFont(bv, font, toggleall, selectall);
1677 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1678 || cpar()->empty()) {
1679 getLyXText(bv)->setFont(font, toggleall);
1684 if (text_.selection.set())
1685 setUndo(bv, Undo::EDIT, text_.cursor.par());
1689 text_.selection.cursor = text_.cursor;
1690 text_.cursorBottom();
1691 text_.setSelection();
1694 text_.toggleFree(font, toggleall);
1697 text_.clearSelection();
1701 bool flag = (selectall || text_.selection.set());
1704 updateLocal(bv, FULL, true);
1706 updateLocal(bv, CURSOR_PAR, true);
1710 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1712 if (cpar()->isInset(cpos())) {
1714 static_cast<UpdatableInset*>(cpar()->getInset(cpos()));
1715 if (!isHighlyEditableInset(inset))
1717 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1718 inset->localDispatch(cmd);
1719 if (!the_locking_inset)
1721 updateLocal(bv, CURSOR, false);
1728 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1729 mouse_button::state button)
1731 x -= drawTextXOffset;
1733 int dummyy = y + dim_.asc;
1734 Inset * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1735 // we only do the edit() call if the inset was hit by the mouse
1736 // or if it is a highly editable inset. So we should call this
1737 // function from our own edit with button < 0.
1738 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1740 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1748 inset_x = cix(bv) - top_x + drawTextXOffset;
1749 inset_y = ciy() + drawTextYOffset;
1750 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1751 inset->localDispatch(cmd);
1752 if (!the_locking_inset)
1754 updateLocal(bv, CURSOR, false);
1761 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1764 int w = UpdatableInset::getMaxWidth(bv, inset);
1769 w = w - top_x + owner()->x();
1772 w -= (2 * TEXT_TO_INSET_OFFSET);
1775 return UpdatableInset::getMaxWidth(bv, inset);
1780 void InsetText::setParagraphData(ParagraphList const & plist)
1782 // we have to unlock any locked inset otherwise we're in troubles
1783 the_locking_inset = 0;
1785 // But it it makes no difference that is a lot better.
1787 // See if this can be simplified when std::list is in effect.
1790 ParagraphList::const_iterator it = plist.begin();
1791 ParagraphList::const_iterator end = plist.end();
1792 for (; it != end; ++it) {
1793 paragraphs.push_back(*it);
1794 Paragraph & tmp = paragraphs.back();
1795 tmp.setInsetOwner(this);
1803 void InsetText::markNew(bool track_changes)
1805 ParagraphList::iterator pit = paragraphs.begin();
1806 ParagraphList::iterator pend = paragraphs.end();
1807 for (; pit != pend; ++pit) {
1808 if (track_changes) {
1809 pit->trackChanges();
1811 // no-op when not tracking
1812 pit->cleanChanges();
1818 void InsetText::setText(string const & data, LyXFont const & font)
1821 for (unsigned int i = 0; i < data.length(); ++i)
1822 paragraphs.begin()->insertChar(i, data[i], font);
1827 void InsetText::setAutoBreakRows(bool flag)
1829 if (flag != autoBreakRows) {
1830 autoBreakRows = flag;
1838 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1840 if (how != drawFrame_) {
1843 updateLocal(bv, DRAW_FRAME, false);
1848 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1850 if (frame_color != col) {
1853 updateLocal(bv, DRAW_FRAME, false);
1858 int InsetText::cx(BufferView * bv) const
1860 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1861 if (the_locking_inset) {
1862 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1863 text_.cursor.pos());
1864 if (font.isVisibleRightToLeft())
1865 x -= the_locking_inset->width();
1871 int InsetText::cix(BufferView * bv) const
1873 int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1874 if (the_locking_inset) {
1875 LyXFont font = text_.getFont(bv->buffer(), text_.cursor.par(),
1876 text_.cursor.pos());
1877 if (font.isVisibleRightToLeft())
1878 x -= the_locking_inset->width();
1884 int InsetText::cy() const
1886 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1890 int InsetText::ciy() const
1892 return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1896 pos_type InsetText::cpos() const
1898 return text_.cursor.pos();
1902 ParagraphList::iterator InsetText::cpar() const
1904 return text_.cursor.par();
1908 bool InsetText::cboundary() const
1910 return text_.cursor.boundary();
1914 RowList::iterator InsetText::crow() const
1916 return text_.cursorRow();
1920 LyXText * InsetText::getLyXText(BufferView const * bv,
1921 bool const recursive) const
1924 if (recursive && the_locking_inset)
1925 return the_locking_inset->getLyXText(bv, true);
1930 void InsetText::setViewCache(BufferView const * bv) const
1933 text_.bv_owner = const_cast<BufferView *>(bv);
1937 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1940 /// then remove all LyXText in text-insets
1941 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1942 const_cast<ParagraphList&>(paragraphs).end(),
1943 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1948 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1950 if (paragraphs.size() == 1 && paragraphs.begin()->empty()) {
1951 // no data, resize not neccessary!
1952 // we have to do this as a fixed width may have changed!
1955 restoreLyXTextState();
1965 // one endless line, resize normally not necessary
1966 if (!force && getMaxWidth(bv, this) < 0)
1971 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1972 const_cast<ParagraphList&>(paragraphs).end(),
1973 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1976 restoreLyXTextState();
1978 if (the_locking_inset) {
1979 inset_x = cix(bv) - top_x + drawTextXOffset;
1980 inset_y = ciy() + drawTextYOffset;
1983 text_.top_y(bv->screen().topCursorVisible(&text_));
1985 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
1986 // this will scroll the screen such that the cursor becomes visible
1987 bv->updateScrollbar();
1989 need_update |= FULL;
1994 void InsetText::reinitLyXText() const
1996 BufferView * bv = text_.bv_owner;
2003 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
2004 const_cast<ParagraphList&>(paragraphs).end(),
2005 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
2008 restoreLyXTextState();
2009 if (the_locking_inset) {
2010 inset_x = cix(bv) - top_x + drawTextXOffset;
2011 inset_y = ciy() + drawTextYOffset;
2013 text_.top_y(bv->screen().topCursorVisible(&text_));
2015 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
2016 // this will scroll the screen such that the cursor becomes visible
2017 bv->updateScrollbar();
2024 void InsetText::removeNewlines()
2026 bool changed = false;
2028 ParagraphList::iterator it = paragraphs.begin();
2029 ParagraphList::iterator end = paragraphs.end();
2030 for (; it != end; ++it) {
2031 for (int i = 0; i < it->size(); ++i) {
2032 if (it->isNewline(i)) {
2043 bool InsetText::nodraw() const
2045 if (the_locking_inset)
2046 return the_locking_inset->nodraw();
2047 return UpdatableInset::nodraw();
2051 int InsetText::scroll(bool recursive) const
2053 int sx = UpdatableInset::scroll(false);
2055 if (recursive && the_locking_inset)
2056 sx += the_locking_inset->scroll(recursive);
2062 void InsetText::clearSelection(BufferView * bv)
2064 getLyXText(bv)->clearSelection();
2068 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2070 Painter & pain = bv->painter();
2072 int h = dim_.asc + dim_.des;
2073 int ty = baseline - dim_.asc;
2079 if ((ty + h) > pain.paperHeight())
2080 h = pain.paperHeight();
2081 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2082 w = pain.paperWidth();
2083 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2088 ParagraphList * InsetText::getParagraphs(int i) const
2090 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
2094 LyXCursor const & InsetText::cursor(BufferView * bv) const
2096 if (the_locking_inset)
2097 return the_locking_inset->cursor(bv);
2098 return getLyXText(bv)->cursor;
2102 Inset * InsetText::getInsetFromID(int id_arg) const
2105 return const_cast<InsetText *>(this);
2107 ParagraphList::const_iterator pit = paragraphs.begin();
2108 ParagraphList::const_iterator pend = paragraphs.end();
2109 for (; pit != pend; ++pit) {
2110 InsetList::const_iterator it = pit->insetlist.begin();
2111 InsetList::const_iterator end = pit->insetlist.end();
2112 for (; it != end; ++it) {
2113 if (it->inset->id() == id_arg)
2115 Inset * in = it->inset->getInsetFromID(id_arg);
2125 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2128 if (the_locking_inset) {
2129 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2130 if (!word.word().empty()) {
2134 // we have to go on checking so move cursor to the next char
2135 text_.cursor.pos(text_.cursor.pos() + 1);
2137 word = text_.selectNextWordToSpellcheck(value);
2138 if (word.word().empty())
2139 bv->unlockInset(const_cast<InsetText *>(this));
2146 void InsetText::selectSelectedWord(BufferView * bv)
2148 if (the_locking_inset) {
2149 the_locking_inset->selectSelectedWord(bv);
2152 getLyXText(bv)->selectSelectedWord();
2153 updateLocal(bv, SELECTION, false);
2157 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2159 if (the_locking_inset) {
2160 the_locking_inset->toggleSelection(bv, kill_selection);
2163 int x = top_x + TEXT_TO_INSET_OFFSET;
2165 RowList::iterator rowit = text_.rows().begin();
2166 RowList::iterator end = text_.rows().end();
2167 int y_offset = top_baseline - rowit->ascent_of_text();
2169 while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2170 y += rowit->height();
2176 if (need_update & SELECTION)
2178 bv->screen().toggleSelection(&text_, bv, kill_selection, y_offset, x);
2182 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2184 if (the_locking_inset) {
2185 if (the_locking_inset->nextChange(bv, length))
2187 text_.cursorRight(true);
2189 lyxfind::SearchResult result =
2190 lyxfind::findNextChange(bv, &text_, length);
2192 if (result == lyxfind::SR_FOUND) {
2193 LyXCursor cur = text_.cursor;
2194 bv->unlockInset(bv->theLockingInset());
2195 if (bv->lockInset(this))
2198 text_.setSelectionRange(length);
2199 updateLocal(bv, SELECTION, false);
2201 return result != lyxfind::SR_NOT_FOUND;
2205 bool InsetText::searchForward(BufferView * bv, string const & str,
2208 if (the_locking_inset) {
2209 if (the_locking_inset->searchForward(bv, str, cs, mw))
2211 text_.cursorRight(true);
2213 lyxfind::SearchResult result =
2214 lyxfind::LyXFind(bv, &text_, str, true, cs, mw);
2216 if (result == lyxfind::SR_FOUND) {
2217 LyXCursor cur = text_.cursor;
2218 bv->unlockInset(bv->theLockingInset());
2219 if (bv->lockInset(this))
2222 text_.setSelectionRange(str.length());
2223 updateLocal(bv, SELECTION, false);
2225 return (result != lyxfind::SR_NOT_FOUND);
2228 bool InsetText::searchBackward(BufferView * bv, string const & str,
2231 if (the_locking_inset) {
2232 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2236 ParagraphList::iterator pit = paragraphs.begin();
2237 ParagraphList::iterator pend = paragraphs.end();
2239 while (boost::next(pit) != pend)
2242 text_.setCursor(pit, pit->size());
2244 lyxfind::SearchResult result =
2245 lyxfind::LyXFind(bv, &text_, str, false, cs, mw);
2247 if (result == lyxfind::SR_FOUND) {
2248 LyXCursor cur = text_.cursor;
2249 bv->unlockInset(bv->theLockingInset());
2250 if (bv->lockInset(this))
2253 text_.setSelectionRange(str.length());
2254 updateLocal(bv, SELECTION, false);
2256 return (result != lyxfind::SR_NOT_FOUND);
2260 bool InsetText::checkInsertChar(LyXFont & font)
2263 return owner()->checkInsertChar(font);
2268 void InsetText::collapseParagraphs(BufferView * bv)
2270 while (paragraphs.size() > 1) {
2271 ParagraphList::iterator first_par = paragraphs.begin();
2272 ParagraphList::iterator next_par = boost::next(first_par);
2273 size_t const first_par_size = first_par->size();
2275 if (!first_par->empty() &&
2276 !next_par->empty() &&
2277 !first_par->isSeparator(first_par_size - 1)) {
2278 first_par->insertChar(first_par_size, ' ');
2281 if (text_.selection.set()) {
2282 if (text_.selection.start.par() == next_par) {
2283 text_.selection.start.par(first_par);
2284 text_.selection.start.pos(
2285 text_.selection.start.pos() + first_par_size);
2287 if (text_.selection.end.par() == next_par) {
2288 text_.selection.end.par(first_par);
2289 text_.selection.end.pos(
2290 text_.selection.end.pos() + first_par_size);
2294 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2300 void InsetText::getDrawFont(LyXFont & font) const
2304 owner()->getDrawFont(font);
2308 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2310 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2311 // And it probably does. You have to take a look at this John. (Lgb)
2312 #warning John, have a look here. (Lgb)
2313 ParagraphList::iterator pit = plist.begin();
2314 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2316 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2318 ParagraphList::iterator pend = plist.end();
2319 for (; pit != pend; ++pit) {
2320 paragraphs.push_back(*pit);
2327 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2329 ParagraphList::const_iterator pit = paragraphs.begin();
2330 ParagraphList::const_iterator pend = paragraphs.end();
2332 for (; pit != pend; ++pit) {
2333 InsetList::const_iterator it = pit->insetlist.begin();
2334 InsetList::const_iterator end = pit->insetlist.end();
2335 for (; it != end; ++it) {
2336 it->inset->addPreview(loader);