1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team.
8 * ======================================================
14 #pragma implementation
17 #include "insettext.h"
18 #include "paragraph.h"
22 #include "commandtags.h"
25 #include "BufferView.h"
27 #include "LaTeXFeatures.h"
30 #include "lyxcursor.h"
31 #include "CutAndPaste.h"
37 #include "trans_mgr.h"
38 #include "lyxscreen.h"
42 #include "ParagraphParameters.h"
43 #include "undo_funcs.h"
46 #include "frontends/Alert.h"
48 #include "support/textutils.h"
49 #include "support/LAssert.h"
50 #include "support/lstrings.h"
66 using lyx::layout_type;
67 using lyx::textclass_type;
69 extern unsigned char getCurrentTextClass(Buffer *);
70 extern bool math_insert_greek(BufferView *, char);
71 extern int greek_kb_flag;
74 // These functions should probably go into bufferview_funcs somehow (Jug)
76 void InsetText::saveLyXTextState(LyXText * t) const
78 // check if my paragraphs are still valid
81 if (p == t->cursor.par())
86 if (p && t->cursor.pos() <= p->size()) {
87 sstate.lpar = t->cursor.par();
88 sstate.pos = t->cursor.pos();
89 sstate.boundary = t->cursor.boundary();
90 sstate.selstartpar = t->selection.start.par();
91 sstate.selstartpos = t->selection.start.pos();
92 sstate.selstartboundary = t->selection.start.boundary();
93 sstate.selendpar = t->selection.end.par();
94 sstate.selendpos = t->selection.end.pos();
95 sstate.selendboundary = t->selection.end.boundary();
96 sstate.selection = t->selection.set();
97 sstate.mark_set = t->selection.mark();
98 sstate.refresh = t->refresh_row != 0;
104 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
107 t->selection.set(true);
108 /* at this point just to avoid the Delete-Empty-Paragraph
109 * Mechanism when setting the cursor */
110 t->selection.mark(sstate.mark_set);
111 if (sstate.selection) {
112 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
113 true, sstate.selstartboundary);
114 t->selection.cursor = t->cursor;
115 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
116 true, sstate.selendboundary);
118 t->setCursor(bv, sstate.lpar, sstate.pos);
120 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
121 t->selection.cursor = t->cursor;
122 t->selection.set(false);
124 if (sstate.refresh) {
130 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
137 InsetText::InsetText()
138 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
146 InsetText::InsetText(InsetText const & in, bool same_id)
147 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
155 InsetText & InsetText::operator=(InsetText const & it)
162 void InsetText::init(InsetText const * ins, bool same_id)
165 setParagraphData(ins->par, same_id);
166 autoBreakRows = ins->autoBreakRows;
167 drawFrame_ = ins->drawFrame_;
168 frame_color = ins->frame_color;
174 p->setInsetOwner(this);
177 the_locking_inset = 0;
179 frame_color = LColor::insetframe;
180 autoBreakRows = false;
187 no_selection = false;
194 last_drawn_width = -1;
195 frame_is_visible = false;
198 in_insetAllowed = false;
202 InsetText::~InsetText()
209 Paragraph * tmp = par->next();
216 void InsetText::clear()
219 Paragraph * tmp = par->next();
229 Inset * InsetText::clone(Buffer const &, bool same_id) const
231 return new InsetText(*this, same_id);
235 void InsetText::write(Buffer const * buf, ostream & os) const
238 writeParagraphData(buf, os);
242 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
244 par->writeFile(buf, os, buf->params, 0);
248 void InsetText::read(Buffer const * buf, LyXLex & lex)
252 Paragraph * return_par = 0;
253 Paragraph::depth_type depth = 0;
254 LyXFont font(LyXFont::ALL_INHERIT);
260 token = lex.getString();
263 if (token == "\\end_inset") {
264 #ifndef NO_COMPABILITY
265 const_cast<Buffer*>(buf)->insertErtContents(par, pos, font, false);
270 if (const_cast<Buffer*>(buf)->
271 parseSingleLyXformat2Token(lex, par, return_par,
272 token, pos, depth, font)) {
273 // the_end read this should NEVER happen
274 lex.printError("\\the_end read in inset! Error in document!");
282 return_par->setInsetOwner(this);
283 return_par = return_par->next();
286 if (token != "\\end_inset") {
287 lex.printError("Missing \\end_inset at this point. "
294 int InsetText::ascent(BufferView * bv, LyXFont const &) const
301 insetAscent = lt->firstRow()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
308 int InsetText::descent(BufferView * bv, LyXFont const &) const
315 insetDescent = lt->height - lt->firstRow()->ascent_of_text() +
316 TEXT_TO_INSET_OFFSET;
323 int InsetText::width(BufferView * bv, LyXFont const &) const
325 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
326 (2 * TEXT_TO_INSET_OFFSET);
327 insetWidth = max(insetWidth, 10);
332 int InsetText::textWidth(BufferView * bv, bool fordraw) const
335 if (!autoBreakRows) {
338 w = getMaxWidth(bv, this);
341 return max(w - (2 * TEXT_TO_INSET_OFFSET),
342 (int)getLyXText(bv)->width);
346 return w - (2 * TEXT_TO_INSET_OFFSET);
350 void InsetText::draw(BufferView * bv, LyXFont const & f,
351 int baseline, float & x, bool cleared) const
356 Painter & pain = bv->painter();
358 // this is the first thing we have to ask because if the x pos
359 // changed we have to do a complete rebreak of the text as we
360 // may have few space to draw in. Well we should check on this too
362 if (top_x != int(x)) {
364 int nw = getMaxWidth(bv, this);
365 if (nw > 0 && old_max_width != nw) {
368 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
376 // repaint the background if needed
377 if (cleared && backgroundColor() != LColor::background) {
379 clearInset(bv, baseline, cleared);
383 // no draw is necessary !!!
384 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
387 top_baseline = baseline;
389 if (need_update & CLEAR_FRAME)
390 clearFrame(pain, cleared);
397 x += static_cast<float>(scroll());
399 // if top_x differs we did it already
400 if (!cleared && (top_x == int(x))
401 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
402 ||(last_drawn_width != insetWidth))) {
403 clearInset(bv, baseline, cleared);
409 frame_is_visible = false;
411 if (!cleared && (need_update == NONE)) {
413 drawFrame(pain, cleared);
417 // call these methods so that insetWidth, insetAscent and
418 // insetDescent have the right values.
423 top_baseline = baseline;
424 top_y = baseline - insetAscent;
426 if (last_drawn_width != insetWidth) {
428 clearInset(bv, baseline, cleared);
430 last_drawn_width = insetWidth;
433 if (the_locking_inset && (cpar(bv) == inset_par)
434 && (cpos(bv) == inset_pos)) {
435 inset_x = cx(bv) - top_x + drawTextXOffset;
436 inset_y = cy(bv) + drawTextYOffset;
438 if (!cleared && (need_update == CURSOR)
439 && !getLyXText(bv)->selection.set()) {
440 drawFrame(pain, cleared);
450 x += TEXT_TO_INSET_OFFSET;
452 Row * row = lt->firstRow();
453 int y_offset = baseline - row->ascent_of_text();
454 int ph = pain.paperHeight();
457 while ((row != 0) && ((y+row->height()) <= 0)) {
459 first += row->height();
465 if (cleared || (need_update&(INIT|FULL))) {
468 while ((row != 0) && (yf < ph)) {
469 lt->getVisibleRow(bv, y+y_offset, int(x), row,
475 } else if (!locked) {
476 if (need_update & CURSOR) {
477 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
478 lt->clearSelection();
479 lt->selection.cursor = lt->cursor;
481 bv->screen()->update(lt, bv, y_offset, int(x));
484 if (need_update & SELECTION) {
485 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
486 } else if (need_update & CURSOR) {
487 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
488 lt->clearSelection();
489 lt->selection.cursor = lt->cursor;
491 bv->screen()->update(lt, bv, y_offset, int(x));
496 lt->status(bv, LyXText::UNCHANGED);
497 if ((need_update != CURSOR_PAR) &&
498 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked))) {
499 drawFrame(pain, cleared);
500 } else if (need_update & CLEAR_FRAME) {
501 clearFrame(pain, cleared);
504 x += insetWidth - TEXT_TO_INSET_OFFSET;
506 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
508 } else if (need_update != INIT) {
517 void InsetText::drawFrame(Painter & pain, bool cleared) const
519 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
520 if (!frame_is_visible || cleared) {
521 frame_x = top_x + ttoD2;
522 frame_y = top_baseline - insetAscent + ttoD2;
523 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
524 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
525 pain.rectangle(frame_x, frame_y,
528 frame_is_visible = true;
533 void InsetText::clearFrame(Painter & pain, bool cleared) const
535 if (frame_is_visible) {
537 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
540 frame_is_visible = false;
545 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
548 if (reinit && owner()) {
550 owner()->update(bv, font, true);
555 if (reinit || need_update == INIT) {
557 // we should put this call where we set need_update to INIT!
560 owner()->update(bv, font, true);
564 if (the_locking_inset) {
565 inset_x = cx(bv) - top_x + drawTextXOffset;
566 inset_y = cy(bv) + drawTextYOffset;
567 the_locking_inset->update(bv, font, reinit);
576 int oldw = insetWidth;
577 insetWidth = lt->width + (2 * TEXT_TO_INSET_OFFSET);
578 if (oldw != insetWidth) {
587 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
590 lt->updateInset(bv, the_locking_inset);
592 if (lt->status() == LyXText::NEED_MORE_REFRESH)
600 void InsetText::setUpdateStatus(BufferView * bv, int what) const
608 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
609 // if we don't break row so that we only have one row to update!
610 if ((lt->status() == LyXText::NEED_MORE_REFRESH) ||
612 (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
615 } else if (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
616 need_update |= CURSOR_PAR;
619 // this to not draw a selection when we redraw all of it!
620 if (need_update & CURSOR && !(need_update & SELECTION)) {
621 if (lt->selection.set())
623 lt->clearSelection();
630 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
638 setUpdateStatus(bv, what);
639 if (((need_update != CURSOR) && (need_update != NONE)) ||
640 (lt->status() != LyXText::UNCHANGED) || lt->selection.set())
642 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
644 if (need_update == CURSOR)
646 bv->owner()->showState();
647 if (old_par != cpar(bv)) {
648 bv->owner()->setLayout(cpar(bv)->getLayout());
656 string const InsetText::editMessage() const
658 return _("Opened Text Inset");
662 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
664 UpdatableInset::edit(bv, x, y, button);
666 if (!bv->lockInset(this)) {
667 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
671 the_locking_inset = 0;
672 inset_pos = inset_x = inset_y = 0;
673 inset_boundary = false;
676 int tmp_y = (y < 0) ? 0 : y;
683 if (!checkAndActivateInset(bv, x, tmp_y, button))
684 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
686 lt->clearSelection();
689 updateLocal(bv, CURSOR, false);
691 // If the inset is empty set the language of the current font to the
692 // language to the surronding text (if different).
693 if (par->size() == 0 && !par->next() &&
694 bv->getParentLanguage(this) != lt->current_font.language()) {
695 LyXFont font(LyXFont::ALL_IGNORE);
696 font.setLanguage(bv->getParentLanguage(this));
697 setFont(bv, font, false);
704 void InsetText::edit(BufferView * bv, bool front)
706 UpdatableInset::edit(bv, front);
708 if (!bv->lockInset(this)) {
709 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
713 the_locking_inset = 0;
714 inset_pos = inset_x = inset_y = 0;
715 inset_boundary = false;
724 lt->setCursor(bv, par, 0);
729 // int const pos = (p->size() ? p->size()-1 : p->size());
730 lt->setCursor(bv, p, p->size());
732 lt->clearSelection();
735 updateLocal(bv, CURSOR, false);
737 // If the inset is empty set the language of the current font to the
738 // language to the surronding text (if different).
739 if (par->size() == 0 && !par->next() &&
740 bv->getParentLanguage(this) != lt->current_font.language()) {
741 LyXFont font(LyXFont::ALL_IGNORE);
742 font.setLanguage(bv->getParentLanguage(this));
743 setFont(bv, font, false);
750 void InsetText::insetUnlock(BufferView * bv)
752 if (the_locking_inset) {
753 the_locking_inset->insetUnlock(bv);
754 the_locking_inset = 0;
757 no_selection = false;
760 if (drawFrame_ == LOCKED)
761 code = CURSOR|CLEAR_FRAME;
769 if (lt->selection.set()) {
770 lt->clearSelection();
772 } else if (owner()) {
773 bv->owner()->setLayout(owner()->getLyXText(bv)
774 ->cursor.par()->getLayout());
776 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
777 // hack for deleteEmptyParMech
778 lt->setCursor(bv, par, 0);
779 updateLocal(bv, code, false);
784 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
786 the_locking_inset = inset;
787 inset_x = cx(bv) - top_x + drawTextXOffset;
788 inset_y = cy(bv) + drawTextYOffset;
789 inset_pos = cpos(bv);
790 inset_par = cpar(bv);
791 inset_boundary = cboundary(bv);
792 updateLocal(bv, CURSOR, false);
796 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
798 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
802 if (!the_locking_inset) {
804 int const id = inset->id();
806 Paragraph::inset_iterator it =
807 p->inset_iterator_begin();
808 Paragraph::inset_iterator const end =
809 p->inset_iterator_end();
810 for (; it != end; ++it) {
811 if ((*it) == inset) {
812 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
813 lockInset(bv, inset);
816 if ((*it)->getInsetFromID(id)) {
817 lockInset(bv, static_cast<UpdatableInset *>(*it));
818 return the_locking_inset->lockInsetInInset(bv, inset);
825 if (inset == cpar(bv)->getInset(cpos(bv))) {
826 lyxerr[Debug::INSETS] << "OK" << endl;
827 lockInset(bv, inset);
829 } else if (the_locking_inset && (the_locking_inset == inset)) {
830 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
831 lyxerr[Debug::INSETS] << "OK" << endl;
832 inset_x = cx(bv) - top_x + drawTextXOffset;
833 inset_y = cy(bv) + drawTextYOffset;
835 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
837 } else if (the_locking_inset) {
838 lyxerr[Debug::INSETS] << "MAYBE" << endl;
839 return the_locking_inset->lockInsetInInset(bv, inset);
841 lyxerr[Debug::INSETS] << "NOT OK" << endl;
846 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
849 if (!the_locking_inset)
851 if (the_locking_inset == inset) {
852 the_locking_inset->insetUnlock(bv);
853 getLyXText(bv)->updateInset(bv, inset);
854 the_locking_inset = 0;
856 moveRight(bv, false);
857 old_par = 0; // force layout setting
861 updateLocal(bv, CURSOR, false);
864 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
868 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
875 if (!the_locking_inset) {
876 bool found = lt->updateInset(bv, inset);
880 setUpdateStatus(bv, NONE);
883 if (the_locking_inset != inset) {
885 lt->updateInset(bv, the_locking_inset);
886 setUpdateStatus(bv, CURSOR_PAR);
888 bool found = the_locking_inset->updateInsetInInset(bv, inset);
892 setUpdateStatus(bv, CURSOR_PAR);
895 bool found = lt->updateInset(bv, inset);
899 setUpdateStatus(bv, CURSOR_PAR);
901 updateLocal(bv, CURSOR_PAR, false);
903 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
904 inset_x = cx(bv) - top_x + drawTextXOffset;
905 inset_y = cy(bv) + drawTextYOffset;
912 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
916 int tmp_x = x - drawTextXOffset;
917 int tmp_y = y + insetAscent - getLyXText(bv)->first;
918 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
921 if (the_locking_inset) {
922 if (the_locking_inset == inset) {
923 the_locking_inset->insetButtonPress(bv,
927 no_selection = false;
930 // otherwise unlock the_locking_inset and lock the new inset
931 the_locking_inset->insetUnlock(bv);
932 inset_x = cx(bv) - top_x + drawTextXOffset;
933 inset_y = cy(bv) + drawTextYOffset;
934 the_locking_inset = static_cast<UpdatableInset*>(inset);
935 inset->insetButtonPress(bv, x - inset_x,
936 y - inset_y, button);
937 inset->edit(bv, x - inset_x, y - inset_y, button);
938 if (the_locking_inset)
939 updateLocal(bv, CURSOR, false);
940 no_selection = false;
943 // otherwise only unlock the_locking_inset
944 the_locking_inset->insetUnlock(bv);
945 the_locking_inset = 0;
947 if (bv->theLockingInset()) {
948 if (isHighlyEditableInset(inset)) {
949 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
950 inset_x = cx(bv) - top_x + drawTextXOffset;
951 inset_y = cy(bv) + drawTextYOffset;
952 inset_pos = cpos(bv);
953 inset_par = cpar(bv);
954 inset_boundary = cboundary(bv);
955 the_locking_inset = uinset;
956 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
958 uinset->edit(bv, x - inset_x, y - inset_y, 0);
959 if (the_locking_inset)
960 updateLocal(bv, CURSOR, false);
961 no_selection = false;
965 if (!inset) { // && (button == 2)) {
966 bool paste_internally = false;
967 if ((button == 2) && getLyXText(bv)->selection.set()) {
968 localDispatch(bv, LFUN_COPY, "");
969 paste_internally = true;
977 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
979 // set the selection cursor!
980 lt->selection.cursor = lt->cursor;
981 lt->cursor.x_fix(lt->cursor.x());
983 if (lt->selection.set()) {
984 lt->clearSelection();
985 updateLocal(bv, FULL, false);
987 lt->clearSelection();
989 bv->owner()->setLayout(cpar(bv)->getLayout());
991 // Insert primary selection with middle mouse
992 // if there is a local selection in the current buffer,
995 if (paste_internally)
996 localDispatch(bv, LFUN_PASTE, "");
998 localDispatch(bv, LFUN_PASTESELECTION,
1004 getLyXText(bv)->clearSelection();
1006 showInsetCursor(bv);
1007 no_selection = false;
1011 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
1013 if (the_locking_inset) {
1014 return the_locking_inset->insetButtonRelease(bv,
1015 x - inset_x, y - inset_y,
1018 int tmp_x = x - drawTextXOffset;
1019 int tmp_y = y + insetAscent - getLyXText(bv)->first;
1020 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1023 if (isHighlyEditableInset(inset)) {
1024 ret = inset->insetButtonRelease(bv, x - inset_x,
1025 y - inset_y, button);
1027 inset_x = cx(bv) - top_x + drawTextXOffset;
1028 inset_y = cy(bv) + drawTextYOffset;
1029 ret = inset->insetButtonRelease(bv, x - inset_x,
1030 y - inset_y, button);
1031 inset->edit(bv, x - inset_x,
1032 y - inset_y, button);
1034 updateLocal(bv, CURSOR_PAR, false);
1040 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1044 if (the_locking_inset) {
1045 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1051 lt = getLyXText(bv);
1054 hideInsetCursor(bv);
1055 LyXCursor cur = lt->cursor;
1056 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1057 if (cur == lt->cursor)
1059 lt->setSelection(bv);
1060 if (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1061 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos())
1063 updateLocal(bv, SELECTION, false);
1065 showInsetCursor(bv);
1071 void InsetText::insetKeyPress(XKeyEvent * xke)
1073 if (the_locking_inset) {
1074 the_locking_inset->insetKeyPress(xke);
1080 UpdatableInset::RESULT
1081 InsetText::localDispatch(BufferView * bv,
1082 kb_action action, string const & arg)
1084 bool was_empty = par->size() == 0 && !par->next();
1085 no_selection = false;
1086 UpdatableInset::RESULT
1087 result= UpdatableInset::localDispatch(bv, action, arg);
1088 if (result != UNDISPATCHED) {
1092 result = DISPATCHED;
1093 if ((action < 0) && arg.empty())
1096 if (the_locking_inset) {
1097 result = the_locking_inset->localDispatch(bv, action, arg);
1098 if (result == DISPATCHED_NOUPDATE)
1100 else if (result == DISPATCHED) {
1101 updateLocal(bv, CURSOR_PAR, false);
1103 } else if (result >= FINISHED) {
1105 case FINISHED_RIGHT:
1106 moveRightIntern(bv, false, false);
1107 result = DISPATCHED;
1110 if ((result = moveUp(bv)) >= FINISHED) {
1111 updateLocal(bv, CURSOR, false);
1112 bv->unlockInset(this);
1116 if ((result = moveDown(bv)) >= FINISHED) {
1117 updateLocal(bv, CURSOR, false);
1118 bv->unlockInset(this);
1122 result = DISPATCHED;
1125 the_locking_inset = 0;
1126 #ifdef WITH_WARNINGS
1127 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1132 hideInsetCursor(bv);
1135 lt = getLyXText(bv);
1140 case LFUN_SELFINSERT:
1141 if (bv->buffer()->isReadonly()) {
1142 // setErrorMessage(N_("Document is read only"));
1146 /* Automatically delete the currently selected
1147 * text and replace it with what is being
1148 * typed in now. Depends on lyxrc settings
1149 * "auto_region_delete", which defaults to
1152 setUndo(bv, Undo::INSERT,
1153 lt->cursor.par(), lt->cursor.par()->next());
1155 if (lyxrc.auto_region_delete) {
1156 if (lt->selection.set()) {
1157 lt->cutSelection(bv, false);
1160 lt->clearSelection();
1161 for (string::size_type i = 0; i < arg.length(); ++i) {
1162 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1165 lt->selection.cursor = lt->cursor;
1166 updateLocal(bv, CURSOR_PAR, true);
1167 result = DISPATCHED_NOUPDATE;
1169 // --- Cursor Movements -----------------------------------
1172 moveRight(bv, false, true);
1173 lt->setSelection(bv);
1174 updateLocal(bv, SELECTION, false);
1177 result = moveRight(bv);
1179 updateLocal(bv, CURSOR, false);
1183 moveLeft(bv, false, true);
1184 lt->setSelection(bv);
1185 updateLocal(bv, SELECTION, false);
1189 result = moveLeft(bv);
1190 updateLocal(bv, CURSOR, false);
1195 lt->setSelection(bv);
1196 updateLocal(bv, SELECTION, false);
1200 result = moveDown(bv);
1201 updateLocal(bv, CURSOR, false);
1206 lt->setSelection(bv);
1207 updateLocal(bv, SELECTION, false);
1211 result = moveUp(bv);
1212 updateLocal(bv, CURSOR, false);
1217 updateLocal(bv, CURSOR, false);
1221 updateLocal(bv, CURSOR, false);
1223 case LFUN_BACKSPACE: {
1224 setUndo(bv, Undo::DELETE,
1225 lt->cursor.par(), lt->cursor.par()->next());
1226 if (lt->selection.set())
1227 lt->cutSelection(bv);
1230 updateLocal(bv, CURSOR_PAR, true);
1235 setUndo(bv, Undo::DELETE,
1236 lt->cursor.par(), lt->cursor.par()->next());
1237 if (lt->selection.set()) {
1238 lt->cutSelection(bv);
1242 updateLocal(bv, CURSOR_PAR, true);
1247 setUndo(bv, Undo::DELETE,
1248 lt->cursor.par(), lt->cursor.par()->next());
1249 lt->cutSelection(bv);
1250 updateLocal(bv, CURSOR_PAR, true);
1256 lt->copySelection(bv);
1257 updateLocal(bv, CURSOR_PAR, false);
1259 case LFUN_PASTESELECTION:
1261 string const clip(bv->getClipboard());
1265 if (arg == "paragraph") {
1266 lt->insertStringAsParagraphs(bv, clip);
1268 lt->insertStringAsLines(bv, clip);
1270 updateLocal(bv, CURSOR_PAR, true);
1274 if (!autoBreakRows) {
1276 if (CutAndPaste::nrOfParagraphs() > 1) {
1277 Alert::alert(_("Impossible operation"),
1278 _("Cannot include more than one paragraph!"),
1283 setUndo(bv, Undo::INSERT,
1284 lt->cursor.par(), lt->cursor.par()->next());
1285 lt->pasteSelection(bv);
1286 updateLocal(bv, CURSOR_PAR, true);
1290 case LFUN_BREAKPARAGRAPH:
1291 if (!autoBreakRows) {
1292 result = DISPATCHED;
1295 lt->breakParagraph(bv, 0);
1296 updateLocal(bv, FULL, true);
1298 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1299 if (!autoBreakRows) {
1300 result = DISPATCHED;
1303 lt->breakParagraph(bv, 1);
1304 updateLocal(bv, FULL, true);
1307 case LFUN_BREAKLINE: {
1308 if (!autoBreakRows) {
1309 result = DISPATCHED;
1312 setUndo(bv, Undo::INSERT,
1313 lt->cursor.par(), lt->cursor.par()->next());
1314 lt->insertChar(bv, Paragraph::META_NEWLINE);
1315 updateLocal(bv, CURSOR_PAR, true);
1320 // do not set layouts on non breakable textinsets
1321 if (autoBreakRows) {
1322 layout_type cur_layout = cpar(bv)->layout;
1324 // Derive layout number from given argument (string)
1325 // and current buffer's textclass (number). */
1326 textclass_type tclass = bv->buffer()->params.textclass;
1327 std::pair <bool, layout_type> layout =
1328 textclasslist.NumberOfLayout(tclass, arg);
1330 // If the entry is obsolete, use the new one instead.
1332 string obs = textclasslist.Style(tclass,layout.second).
1335 layout = textclasslist.NumberOfLayout(tclass, obs);
1338 // see if we found the layout number:
1339 if (!layout.first) {
1340 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1341 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1345 if (cur_layout != layout.second) {
1346 cur_layout = layout.second;
1347 lt->setLayout(bv, layout.second);
1348 bv->owner()->setLayout(cpar(bv)->getLayout());
1349 updateLocal(bv, CURSOR_PAR, true);
1352 // reset the layout box
1353 bv->owner()->setLayout(cpar(bv)->getLayout());
1356 case LFUN_PARAGRAPH_SPACING:
1357 // This one is absolutely not working. When fiddling with this
1358 // it also seems to me that the paragraphs inside the insettext
1359 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1361 Paragraph * par = lt->cursor.par();
1362 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1363 float cur_value = 1.0;
1364 if (cur_spacing == Spacing::Other) {
1365 cur_value = par->params().spacing().getValue();
1368 istringstream istr(arg.c_str());
1371 Spacing::Space new_spacing = cur_spacing;
1372 float new_value = cur_value;
1374 lyxerr << "Missing argument to `paragraph-spacing'"
1376 } else if (tmp == "single") {
1377 new_spacing = Spacing::Single;
1378 } else if (tmp == "onehalf") {
1379 new_spacing = Spacing::Onehalf;
1380 } else if (tmp == "double") {
1381 new_spacing = Spacing::Double;
1382 } else if (tmp == "other") {
1383 new_spacing = Spacing::Other;
1386 lyxerr << "new_value = " << tmpval << endl;
1389 } else if (tmp == "default") {
1390 new_spacing = Spacing::Default;
1392 lyxerr << _("Unknown spacing argument: ")
1395 if (cur_spacing != new_spacing || cur_value != new_value) {
1396 par->params().spacing(Spacing(new_spacing, new_value));
1397 updateLocal(bv, CURSOR_PAR, true);
1403 if (!bv->Dispatch(action, arg))
1404 result = UNDISPATCHED;
1408 /// If the action has deleted all text in the inset, we need to change the
1409 // language to the language of the surronding text.
1410 if (!was_empty && par->size() == 0 && !par->next()) {
1411 LyXFont font(LyXFont::ALL_IGNORE);
1412 font.setLanguage(bv->getParentLanguage(this));
1413 setFont(bv, font, false);
1416 if (result < FINISHED) {
1417 showInsetCursor(bv);
1419 bv->unlockInset(this);
1426 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1429 buf->latexParagraphs(os, par, 0, texrow);
1430 return texrow.rows();
1434 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1436 Paragraph * p = par;
1437 unsigned int lines = 0;
1440 string const tmp = buf->asciiParagraph(p, linelen);
1441 lines += countChar(tmp, '\n');
1449 int InsetText::docbook(Buffer const * buf, ostream & os) const
1451 Paragraph * p = par;
1452 unsigned int lines = 0;
1454 vector<string> environment_stack(10);
1455 vector<string> environment_inner(10);
1457 int const command_depth = 0;
1460 Paragraph::depth_type depth = 0; // paragraph depth
1464 int desc_on = 0; // description mode
1466 LyXLayout const & style =
1467 textclasslist.Style(buf->params.textclass,
1470 // environment tag closing
1471 for (; depth > p->params().depth(); --depth) {
1472 if (environment_inner[depth] != "!-- --") {
1473 item_name = "listitem";
1474 buf->sgmlCloseTag(os, command_depth + depth,
1476 if (environment_inner[depth] == "varlistentry")
1477 buf->sgmlCloseTag(os, depth+command_depth,
1478 environment_inner[depth]);
1480 buf->sgmlCloseTag(os, depth + command_depth,
1481 environment_stack[depth]);
1482 environment_stack[depth].erase();
1483 environment_inner[depth].erase();
1486 if (depth == p->params().depth()
1487 && environment_stack[depth] != style.latexname()
1488 && !environment_stack[depth].empty()) {
1489 if (environment_inner[depth] != "!-- --") {
1490 item_name= "listitem";
1491 buf->sgmlCloseTag(os, command_depth+depth,
1493 if (environment_inner[depth] == "varlistentry")
1494 buf->sgmlCloseTag(os,
1495 depth + command_depth,
1496 environment_inner[depth]);
1499 buf->sgmlCloseTag(os, depth + command_depth,
1500 environment_stack[depth]);
1502 environment_stack[depth].erase();
1503 environment_inner[depth].erase();
1506 // Write opening SGML tags.
1507 switch (style.latextype) {
1508 case LATEX_PARAGRAPH:
1509 buf->sgmlOpenTag(os, depth + command_depth,
1514 buf->sgmlError(p, 0,
1515 _("Error : LatexType Command not allowed here.\n"));
1519 case LATEX_ENVIRONMENT:
1520 case LATEX_ITEM_ENVIRONMENT:
1521 if (depth < p->params().depth()) {
1522 depth = p->params().depth();
1523 environment_stack[depth].erase();
1526 if (environment_stack[depth] != style.latexname()) {
1527 if(environment_stack.size() == depth + 1) {
1528 environment_stack.push_back("!-- --");
1529 environment_inner.push_back("!-- --");
1531 environment_stack[depth] = style.latexname();
1532 environment_inner[depth] = "!-- --";
1533 buf->sgmlOpenTag(os, depth + command_depth,
1534 environment_stack[depth]);
1536 if (environment_inner[depth] != "!-- --") {
1537 item_name= "listitem";
1538 buf->sgmlCloseTag(os,
1539 command_depth + depth,
1541 if (environment_inner[depth] == "varlistentry")
1542 buf->sgmlCloseTag(os,
1543 depth + command_depth,
1544 environment_inner[depth]);
1548 if (style.latextype == LATEX_ENVIRONMENT) {
1549 if (!style.latexparam().empty()) {
1550 if(style.latexparam() == "CDATA")
1553 buf->sgmlOpenTag(os, depth + command_depth,
1554 style.latexparam());
1559 desc_on = (style.labeltype == LABEL_MANUAL);
1562 environment_inner[depth]= "varlistentry";
1564 environment_inner[depth]= "listitem";
1566 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1567 environment_inner[depth]);
1571 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1575 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1580 buf->sgmlOpenTag(os, depth + command_depth,
1585 buf->simpleDocBookOnePar(os, p, desc_on,
1586 depth + 1 + command_depth);
1590 // write closing SGML tags
1591 switch (style.latextype) {
1592 case LATEX_ENVIRONMENT:
1593 if (!style.latexparam().empty()) {
1594 if(style.latexparam() == "CDATA")
1597 buf->sgmlCloseTag(os, depth + command_depth,
1598 style.latexparam());
1601 case LATEX_ITEM_ENVIRONMENT:
1602 if (desc_on == 1) break;
1604 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1606 case LATEX_PARAGRAPH:
1607 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1610 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1616 for (int d = depth; d >= 0; --d) {
1617 if (!environment_stack[depth].empty()) {
1618 if (environment_inner[depth] != "!-- --") {
1619 item_name = "listitem";
1620 buf->sgmlCloseTag(os, command_depth + depth,
1622 if (environment_inner[depth] == "varlistentry")
1623 buf->sgmlCloseTag(os, depth + command_depth,
1624 environment_inner[depth]);
1627 buf->sgmlCloseTag(os, depth + command_depth,
1628 environment_stack[depth]);
1636 void InsetText::validate(LaTeXFeatures & features) const
1638 Paragraph * p = par;
1640 p->validate(features);
1646 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1648 if (textclasslist.Style(buf->params.textclass,
1649 p->getLayout()).labeltype != LABEL_MANUAL)
1652 return p->beginningOfMainBody();
1656 void InsetText::getCursorPos(BufferView * bv,
1657 int & x, int & y) const
1659 if (the_locking_inset) {
1660 the_locking_inset->getCursorPos(bv, x, y);
1668 unsigned int InsetText::insetInInsetY()
1670 if (!the_locking_inset)
1673 return (inset_y + the_locking_inset->insetInInsetY());
1677 void InsetText::toggleInsetCursor(BufferView * bv)
1679 if (the_locking_inset) {
1680 the_locking_inset->toggleInsetCursor(bv);
1684 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1686 int const asc = lyxfont::maxAscent(font);
1687 int const desc = lyxfont::maxDescent(font);
1689 if (isCursorVisible())
1690 bv->hideLockedInsetCursor();
1692 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1693 toggleCursorVisible();
1697 void InsetText::showInsetCursor(BufferView * bv, bool show)
1699 if (the_locking_inset) {
1700 the_locking_inset->showInsetCursor(bv, show);
1703 if (!isCursorVisible()) {
1704 LyXFont const font =
1705 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1707 int const asc = lyxfont::maxAscent(font);
1708 int const desc = lyxfont::maxDescent(font);
1710 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1712 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1713 setCursorVisible(true);
1718 void InsetText::hideInsetCursor(BufferView * bv)
1720 if (isCursorVisible()) {
1721 bv->hideLockedInsetCursor();
1722 setCursorVisible(false);
1724 if (the_locking_inset)
1725 the_locking_inset->hideInsetCursor(bv);
1729 void InsetText::fitInsetCursor(BufferView * bv) const
1731 if (the_locking_inset) {
1732 the_locking_inset->fitInsetCursor(bv);
1735 LyXFont const font =
1736 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1738 int const asc = lyxfont::maxAscent(font);
1739 int const desc = lyxfont::maxDescent(font);
1741 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1745 UpdatableInset::RESULT
1746 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1748 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1749 return moveLeftIntern(bv, false, activate_inset, selecting);
1751 return moveRightIntern(bv, false, activate_inset, selecting);
1755 UpdatableInset::RESULT
1756 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1758 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1759 return moveRightIntern(bv, true, activate_inset, selecting);
1761 return moveLeftIntern(bv, true, activate_inset, selecting);
1765 UpdatableInset::RESULT
1766 InsetText::moveRightIntern(BufferView * bv, bool behind,
1767 bool activate_inset, bool selecting)
1769 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1770 return FINISHED_RIGHT;
1771 if (activate_inset && checkAndActivateInset(bv, behind))
1773 getLyXText(bv)->cursorRight(bv);
1775 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1776 return DISPATCHED_NOUPDATE;
1780 UpdatableInset::RESULT
1781 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1782 bool activate_inset, bool selecting)
1784 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1786 getLyXText(bv)->cursorLeft(bv);
1788 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1789 if (activate_inset && checkAndActivateInset(bv, behind))
1791 return DISPATCHED_NOUPDATE;
1795 UpdatableInset::RESULT
1796 InsetText::moveUp(BufferView * bv)
1798 if (!crow(bv)->previous())
1800 getLyXText(bv)->cursorUp(bv);
1801 return DISPATCHED_NOUPDATE;
1805 UpdatableInset::RESULT
1806 InsetText::moveDown(BufferView * bv)
1808 if (!crow(bv)->next())
1809 return FINISHED_DOWN;
1810 getLyXText(bv)->cursorDown(bv);
1811 return DISPATCHED_NOUPDATE;
1815 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1817 if (the_locking_inset) {
1818 if (the_locking_inset->insetAllowed(inset))
1819 return the_locking_inset->insertInset(bv, inset);
1822 inset->setOwner(this);
1823 hideInsetCursor(bv);
1827 lt = getLyXText(bv);
1830 lt->insertInset(bv, inset);
1832 if ((!cpar(bv)->isInset(cpos(bv))) ||
1833 (cpar(bv)->getInset(cpos(bv)) != inset))
1837 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1838 // showInsetCursor(bv);
1845 bool InsetText::insetAllowed(Inset::Code code) const
1847 // in_insetAllowed is a really gross hack,
1848 // to allow us to call the owner's insetAllowed
1849 // without stack overflow, which can happen
1850 // when the owner uses InsetCollapsable::insetAllowed()
1852 if (in_insetAllowed)
1854 in_insetAllowed = true;
1855 if (the_locking_inset)
1856 ret = the_locking_inset->insetAllowed(code);
1858 ret = owner()->insetAllowed(code);
1859 in_insetAllowed = false;
1864 UpdatableInset * InsetText::getLockingInset() const
1866 return the_locking_inset ? the_locking_inset->getLockingInset() :
1867 const_cast<InsetText *>(this);
1871 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1875 if (the_locking_inset)
1876 return the_locking_inset->getFirstLockingInsetOfType(c);
1881 bool InsetText::showInsetDialog(BufferView * bv) const
1883 if (the_locking_inset)
1884 return the_locking_inset->showInsetDialog(bv);
1889 std::vector<string> const InsetText::getLabelList() const
1891 std::vector<string> label_list;
1893 Paragraph * tpar = par;
1895 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1896 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1897 for (; beg != end; ++beg) {
1898 std::vector<string> const l = (*beg)->getLabelList();
1899 label_list.insert(label_list.end(), l.begin(), l.end());
1901 tpar = tpar->next();
1907 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1910 if (the_locking_inset) {
1911 the_locking_inset->setFont(bv, font, toggleall, selectall);
1914 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1915 getLyXText(bv)->setFont(bv, font, toggleall);
1920 lt = getLyXText(bv);
1923 if (lt->selection.set()) {
1924 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1928 lt->toggleFree(bv, font, toggleall);
1930 lt->clearSelection();
1932 if (selectall || lt->selection.set())
1933 updateLocal(bv, FULL, true);
1935 updateLocal(bv, CURSOR_PAR, true);
1941 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1943 if (cpar(bv)->isInset(cpos(bv))) {
1947 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1948 if (!isHighlyEditableInset(inset))
1950 LyXFont const font =
1951 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1953 x = inset->width(bv, font);
1954 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1957 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1959 //inset_x = cx(bv) - top_x + drawTextXOffset;
1960 //inset_y = cy(bv) + drawTextYOffset;
1961 inset->edit(bv, x, y, 0);
1962 if (!the_locking_inset)
1964 updateLocal(bv, CURSOR, false);
1971 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1974 x -= drawTextXOffset;
1976 int dummyy = y + insetAscent;
1977 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
1984 inset_x = cx(bv) - top_x + drawTextXOffset;
1985 inset_y = cy(bv) + drawTextYOffset;
1986 inset->edit(bv, x - inset_x, y - inset_y, button);
1987 if (!the_locking_inset)
1989 updateLocal(bv, CURSOR, false);
1996 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1999 int w = UpdatableInset::getMaxWidth(bv, inset);
2004 w = w - top_x + owner()->x();
2007 w -= (2 * TEXT_TO_INSET_OFFSET);
2010 return UpdatableInset::getMaxWidth(bv, inset);
2015 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2017 // we have to unlock any locked inset otherwise we're in troubles
2018 the_locking_inset = 0;
2020 Paragraph * tmp = par->next();
2025 par = new Paragraph(*p, same_id);
2026 par->setInsetOwner(this);
2027 Paragraph * np = par;
2030 np->next(new Paragraph(*p, same_id));
2031 np->next()->previous(np);
2033 np->setInsetOwner(this);
2040 void InsetText::setText(string const & data)
2043 LyXFont font(LyXFont::ALL_SANE);
2044 for (unsigned int i=0; i < data.length(); ++i)
2045 par->insertChar(i, data[i], font);
2049 void InsetText::setAutoBreakRows(bool flag)
2051 if (flag != autoBreakRows) {
2052 autoBreakRows = flag;
2060 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2062 if (how != drawFrame_) {
2065 updateLocal(bv, DRAW_FRAME, false);
2070 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2072 if (frame_color != col) {
2075 updateLocal(bv, DRAW_FRAME, false);
2080 int InsetText::cx(BufferView * bv) const
2084 lt = getLyXText(bv);
2087 int x = lt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2088 if (the_locking_inset) {
2089 LyXFont font = lt->getFont(bv->buffer(),
2092 if (font.isVisibleRightToLeft())
2093 x -= the_locking_inset->width(bv, font);
2101 int InsetText::cy(BufferView * bv) const
2104 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2108 pos_type InsetText::cpos(BufferView * bv) const
2110 return getLyXText(bv)->cursor.pos();
2114 Paragraph * InsetText::cpar(BufferView * bv) const
2116 return getLyXText(bv)->cursor.par();
2120 bool InsetText::cboundary(BufferView * bv) const
2122 return getLyXText(bv)->cursor.boundary();
2126 Row * InsetText::crow(BufferView * bv) const
2128 return getLyXText(bv)->cursor.row();
2132 LyXText * InsetText::getLyXText(BufferView const * lbv,
2133 bool const recursive) const
2135 if (!recursive && (cached_bview == lbv)) {
2136 LyXText * lt = cached_text.get();
2137 lyx::Assert(lt && lt->firstRow()->par() == par);
2138 return cached_text.get();
2141 // Super UGLY! (Lgb)
2142 BufferView * bv = const_cast<BufferView *>(lbv);
2145 Cache::iterator it = cache.find(bv);
2147 if (it != cache.end()) {
2151 resizeLyXText(do_resize);
2152 if (lt || !it->second.remove) {
2153 lyx::Assert(it->second.text.get());
2154 cached_text = it->second.text;
2155 if (recursive && the_locking_inset) {
2156 return the_locking_inset->getLyXText(bv, true);
2158 return cached_text.get();
2159 } else if (it->second.remove) {
2161 saveLyXTextState(it->second.text.get());
2168 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2169 cached_text->init(bv);
2170 restoreLyXTextState(bv, cached_text.get());
2172 cache.insert(make_pair(bv, cached_text));
2174 if (the_locking_inset && recursive) {
2175 return the_locking_inset->getLyXText(bv);
2177 return cached_text.get();
2181 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2185 Cache::iterator it = cache.find(bv);
2187 if (it == cache.end()) {
2191 lyx::Assert(it->second.text.get());
2193 it->second.remove = true;
2195 /// then remove all LyXText in text-insets
2196 Paragraph * p = par;
2197 for (; p; p = p->next()) {
2198 p->deleteInsetsLyXText(bv);
2204 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2207 // we cannot resize this because we are in use!
2208 // so do this on the next possible getLyXText()
2213 // lyxerr << "InsetText::resizeLyXText\n";
2214 if (!par->next() && !par->size()) // no data, resize not neccessary!
2216 // one endless line, resize normally not necessary
2217 if (!force && getMaxWidth(bv, this) < 0)
2220 Cache::iterator it = cache.find(bv);
2221 if (it == cache.end()) {
2224 lyx::Assert(it->second.text.get());
2226 LyXText * t = it->second.text.get();
2227 saveLyXTextState(t);
2228 for (Paragraph * p = par; p; p = p->next()) {
2229 p->resizeInsetsLyXText(bv);
2232 restoreLyXTextState(bv, t);
2233 if (the_locking_inset) {
2234 inset_x = cx(bv) - top_x + drawTextXOffset;
2235 inset_y = cy(bv) + drawTextYOffset;
2239 t->first = bv->screen()->topCursorVisible(t);
2242 updateLocal(bv, FULL, false);
2243 // this will scroll the screen such that the cursor becomes visible
2244 bv->updateScrollbar();
2246 need_update |= FULL;
2251 void InsetText::reinitLyXText() const
2254 // we cannot resize this because we are in use!
2255 // so do this on the next possible getLyXText()
2261 // lyxerr << "InsetText::reinitLyXText\n";
2262 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2263 lyx::Assert(it->second.text.get());
2265 LyXText * t = it->second.text.get();
2266 BufferView * bv = it->first;
2268 saveLyXTextState(t);
2269 for (Paragraph * p = par; p; p = p->next()) {
2270 p->resizeInsetsLyXText(bv);
2273 restoreLyXTextState(bv, t);
2274 if (the_locking_inset) {
2275 inset_x = cx(bv) - top_x + drawTextXOffset;
2276 inset_y = cy(bv) + drawTextYOffset;
2279 t->first = bv->screen()->topCursorVisible(t);
2282 updateLocal(bv, FULL, false);
2283 // this will scroll the screen such that the cursor becomes visible
2284 bv->updateScrollbar();
2292 void InsetText::removeNewlines()
2294 bool changed = false;
2296 for (Paragraph * p = par; p; p = p->next()) {
2297 for (int i = 0; i < p->size(); ++i) {
2298 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2309 bool InsetText::nodraw() const
2311 if (the_locking_inset)
2312 return the_locking_inset->nodraw();
2313 return UpdatableInset::nodraw();
2317 int InsetText::scroll(bool recursive) const
2319 int sx = UpdatableInset::scroll(false);
2321 if (recursive && the_locking_inset)
2322 sx += the_locking_inset->scroll(recursive);
2328 bool InsetText::doClearArea() const
2330 return !locked || (need_update & (FULL|INIT));
2334 void InsetText::selectAll(BufferView * bv)
2336 getLyXText(bv)->cursorTop(bv);
2337 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2338 getLyXText(bv)->cursorBottom(bv);
2339 getLyXText(bv)->setSelection(bv);
2343 void InsetText::clearSelection(BufferView * bv)
2345 getLyXText(bv)->clearSelection();
2349 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2352 Painter & pain = bv->painter();
2354 int h = ascent(bv, dummy) + descent(bv, dummy);
2355 int ty = baseline - insetAscent;
2361 if ((ty + h) > pain.paperHeight())
2362 h = pain.paperHeight();
2363 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2364 w = pain.paperWidth();
2365 // w -= TEXT_TO_INSET_OFFSET;
2366 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2369 frame_is_visible = false;
2373 Paragraph * InsetText::getParFromID(int id) const
2376 Paragraph * result = par;
2377 Paragraph * ires = 0;
2378 while (result && result->id() != id) {
2379 if ((ires = result->getParFromID(id)))
2381 result = result->next();
2385 Paragraph * tmp = par;
2387 if (tmp->id() == id) {
2390 Paragraph * tmp2 = tmp->getParFromID(id);
2401 Paragraph * InsetText::firstParagraph() const
2404 if (the_locking_inset)
2405 if ((result = the_locking_inset->firstParagraph()))
2411 Paragraph * InsetText::getFirstParagraph(int i) const
2413 return (i == 0) ? par : 0;
2417 LyXCursor const & InsetText::cursor(BufferView * bv) const
2419 if (the_locking_inset)
2420 return the_locking_inset->cursor(bv);
2421 return getLyXText(bv)->cursor;
2425 Paragraph * InsetText::paragraph() const
2431 void InsetText::paragraph(Paragraph * p)
2433 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2434 // caller of this function has to take care of it. This IS important
2435 // as we could have to insert a paragraph before this one and just
2436 // link the actual to a new ones next and set it with this function
2439 // set ourself as owner for all the paragraphs inserted!
2440 Paragraph * np = par;
2442 np->setInsetOwner(this);
2446 // redraw myself when asked for
2451 Inset * InsetText::getInsetFromID(int id_arg) const
2454 return const_cast<InsetText *>(this);
2456 Paragraph * lp = par;
2459 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2460 en = lp->inset_iterator_end();
2463 if ((*it)->id() == id_arg)
2465 Inset * in = (*it)->getInsetFromID(id_arg);
2475 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2481 lt = getLyXText(bv);
2484 if (the_locking_inset) {
2485 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2492 #warning Dekel please have a look on this one RTL? (Jug)
2494 // we have to go on checking so move cusor to the right
2495 lt->cursor.pos(lt->cursor.pos() + 1);
2497 str = lt->selectNextWordToSpellcheck(bv, value);
2499 bv->unlockInset(const_cast<InsetText *>(this));
2508 void InsetText::selectSelectedWord(BufferView * bv)
2510 if (the_locking_inset) {
2511 the_locking_inset->selectSelectedWord(bv);
2514 getLyXText(bv)->selectSelectedWord(bv);
2515 updateLocal(bv, SELECTION, false);
2519 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2521 if (the_locking_inset) {
2522 the_locking_inset->toggleSelection(bv, kill_selection);
2526 lt = getLyXText(bv);
2530 int x = top_x + TEXT_TO_INSET_OFFSET;
2532 Row * row = lt->firstRow();
2533 int y_offset = top_baseline - row->ascent_of_text();
2535 while ((row != 0) && ((y+row->height()) <= 0)) {
2542 if (need_update & SELECTION)
2544 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2550 bool InsetText::searchForward(BufferView * bv, string const & str,
2553 if (the_locking_inset) {
2554 if (the_locking_inset->searchForward(bv, str, cs, mw))
2558 lt = getLyXText(bv);
2561 Paragraph * lpar = lt->cursor.par();
2562 pos_type pos = lt->cursor.pos();
2563 if (pos < lpar->size() - 1)
2567 lpar = lpar->next();
2572 // we have to unlock ourself in this function by default!
2573 bv->unlockInset(const_cast<InsetText *>(this));
2576 lt->setCursor(bv, lpar, pos);
2580 if (LyXFind(bv, str, true, true, cs , mw)) {
2583 // we have to unlock ourself in this function by default!
2584 bv->unlockInset(const_cast<InsetText *>(this));
2588 bool InsetText::searchBackward(BufferView * bv, string const & str,
2591 if (the_locking_inset)
2592 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2594 if (LyXFind(bv, str, false, true, cs, mw)) {
2597 // we have to unlock ourself in this function by default!
2598 bv->unlockInset(const_cast<InsetText *>(this));
2603 bool InsetText::checkInsertChar(LyXFont & font)
2606 return owner()->checkInsertChar(font);