1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team.
8 * ======================================================
20 #pragma implementation
23 #include "insettext.h"
24 #include "paragraph.h"
28 #include "commandtags.h"
31 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
35 #include "frontends/Alert.h"
37 #include "lyxcursor.h"
38 #include "CutAndPaste.h"
41 #include "support/textutils.h"
42 #include "support/LAssert.h"
43 #include "support/lstrings.h"
47 #include "trans_mgr.h"
48 #include "lyxscreen.h"
52 #include "ParagraphParameters.h"
53 #include "undo_funcs.h"
65 using lyx::layout_type;
66 using lyx::textclass_type;
68 extern unsigned char getCurrentTextClass(Buffer *);
69 extern bool math_insert_greek(BufferView *, char);
70 extern int greek_kb_flag;
73 // These functions should probably go into bufferview_funcs somehow (Jug)
75 void InsetText::saveLyXTextState(LyXText * t) const
77 // check if my paragraphs are still valid
80 if (p == t->cursor.par())
85 if (p && t->cursor.pos() <= p->size()) {
86 sstate.lpar = t->cursor.par();
87 sstate.pos = t->cursor.pos();
88 sstate.boundary = t->cursor.boundary();
89 sstate.selstartpar = t->selection.start.par();
90 sstate.selstartpos = t->selection.start.pos();
91 sstate.selstartboundary = t->selection.start.boundary();
92 sstate.selendpar = t->selection.end.par();
93 sstate.selendpos = t->selection.end.pos();
94 sstate.selendboundary = t->selection.end.boundary();
95 sstate.selection = t->selection.set();
96 sstate.mark_set = t->selection.mark();
97 sstate.refresh = t->refresh_row != 0;
103 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
106 t->selection.set(true);
107 /* at this point just to avoid the Delete-Empty-Paragraph
108 * Mechanism when setting the cursor */
109 t->selection.mark(sstate.mark_set);
110 if (sstate.selection) {
111 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
112 true, sstate.selstartboundary);
113 t->selection.cursor = t->cursor;
114 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
115 true, sstate.selendboundary);
117 t->setCursor(bv, sstate.lpar, sstate.pos);
119 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
120 t->selection.cursor = t->cursor;
121 t->selection.set(false);
123 if (sstate.refresh) {
129 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
136 InsetText::InsetText()
137 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
145 InsetText::InsetText(InsetText const & in, bool same_id)
146 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
154 InsetText & InsetText::operator=(InsetText const & it)
161 void InsetText::init(InsetText const * ins, bool same_id)
164 setParagraphData(ins->par, same_id);
165 autoBreakRows = ins->autoBreakRows;
166 drawFrame_ = ins->drawFrame_;
167 frame_color = ins->frame_color;
173 p->setInsetOwner(this);
176 the_locking_inset = 0;
178 frame_color = LColor::insetframe;
179 autoBreakRows = false;
186 no_selection = false;
193 last_drawn_width = -1;
194 frame_is_visible = false;
197 in_insetAllowed = false;
201 InsetText::~InsetText()
208 Paragraph * tmp = par->next();
215 void InsetText::clear()
218 Paragraph * tmp = par->next();
228 Inset * InsetText::clone(Buffer const &, bool same_id) const
230 return new InsetText(*this, same_id);
234 void InsetText::write(Buffer const * buf, ostream & os) const
237 writeParagraphData(buf, os);
241 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
243 par->writeFile(buf, os, buf->params, 0);
247 void InsetText::read(Buffer const * buf, LyXLex & lex)
251 Paragraph * return_par = 0;
252 Paragraph::depth_type depth = 0;
253 LyXFont font(LyXFont::ALL_INHERIT);
259 token = lex.getString();
262 if (token == "\\end_inset") {
263 #ifndef NO_COMPABILITY
264 const_cast<Buffer*>(buf)->insertErtContents(par, pos, font, false);
269 if (const_cast<Buffer*>(buf)->
270 parseSingleLyXformat2Token(lex, par, return_par,
271 token, pos, depth, font)) {
272 // the_end read this should NEVER happen
273 lex.printError("\\the_end read in inset! Error in document!");
281 return_par->setInsetOwner(this);
282 return_par = return_par->next();
285 if (token != "\\end_inset") {
286 lex.printError("Missing \\end_inset at this point. "
293 int InsetText::ascent(BufferView * bv, LyXFont const &) const
300 insetAscent = lt->firstRow()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
307 int InsetText::descent(BufferView * bv, LyXFont const &) const
314 insetDescent = lt->height - lt->firstRow()->ascent_of_text() +
315 TEXT_TO_INSET_OFFSET;
322 int InsetText::width(BufferView * bv, LyXFont const &) const
324 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
325 (2 * TEXT_TO_INSET_OFFSET);
326 insetWidth = max(insetWidth, 10);
331 int InsetText::textWidth(BufferView * bv, bool fordraw) const
334 if (!autoBreakRows) {
337 w = getMaxWidth(bv, this);
340 return max(w - (2 * TEXT_TO_INSET_OFFSET),
341 (int)getLyXText(bv)->width);
345 return w - (2 * TEXT_TO_INSET_OFFSET);
349 void InsetText::draw(BufferView * bv, LyXFont const & f,
350 int baseline, float & x, bool cleared) const
355 Painter & pain = bv->painter();
357 // this is the first thing we have to ask because if the x pos
358 // changed we have to do a complete rebreak of the text as we
359 // may have few space to draw in. Well we should check on this too
361 if (top_x != int(x)) {
363 int nw = getMaxWidth(bv, this);
364 if (nw > 0 && old_max_width != nw) {
367 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
375 // repaint the background if needed
376 if (cleared && backgroundColor() != LColor::background) {
378 clearInset(bv, baseline, cleared);
382 // no draw is necessary !!!
383 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
386 top_baseline = baseline;
388 if (need_update & CLEAR_FRAME)
389 clearFrame(pain, cleared);
396 x += static_cast<float>(scroll());
398 // if top_x differs we did it already
399 if (!cleared && (top_x == int(x))
400 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
401 ||(last_drawn_width != insetWidth))) {
402 clearInset(bv, baseline, cleared);
408 frame_is_visible = false;
410 if (!cleared && (need_update == NONE)) {
412 drawFrame(pain, cleared);
416 // call these methods so that insetWidth, insetAscent and
417 // insetDescent have the right values.
422 top_baseline = baseline;
423 top_y = baseline - insetAscent;
425 if (last_drawn_width != insetWidth) {
427 clearInset(bv, baseline, cleared);
429 last_drawn_width = insetWidth;
432 if (the_locking_inset && (cpar(bv) == inset_par)
433 && (cpos(bv) == inset_pos)) {
434 inset_x = cx(bv) - top_x + drawTextXOffset;
435 inset_y = cy(bv) + drawTextYOffset;
437 if (!cleared && (need_update == CURSOR)
438 && !getLyXText(bv)->selection.set()) {
439 drawFrame(pain, cleared);
449 x += TEXT_TO_INSET_OFFSET;
451 Row * row = lt->firstRow();
452 int y_offset = baseline - row->ascent_of_text();
453 int ph = pain.paperHeight();
456 while ((row != 0) && ((y+row->height()) <= 0)) {
458 first += row->height();
464 if (cleared || (need_update&(INIT|FULL))) {
467 while ((row != 0) && (yf < ph)) {
468 lt->getVisibleRow(bv, y+y_offset, int(x), row,
474 } else if (!locked) {
475 if (need_update & CURSOR) {
476 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
477 lt->clearSelection();
478 lt->selection.cursor = lt->cursor;
480 bv->screen()->update(lt, bv, y_offset, int(x));
483 if (need_update & SELECTION) {
484 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
485 } else if (need_update & CURSOR) {
486 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
487 lt->clearSelection();
488 lt->selection.cursor = lt->cursor;
490 bv->screen()->update(lt, bv, y_offset, int(x));
495 lt->status(bv, LyXText::UNCHANGED);
496 if ((need_update != CURSOR_PAR) &&
497 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked))) {
498 drawFrame(pain, cleared);
499 } else if (need_update & CLEAR_FRAME) {
500 clearFrame(pain, cleared);
503 x += insetWidth - TEXT_TO_INSET_OFFSET;
505 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
507 } else if (need_update != INIT) {
516 void InsetText::drawFrame(Painter & pain, bool cleared) const
518 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
519 if (!frame_is_visible || cleared) {
520 frame_x = top_x + ttoD2;
521 frame_y = top_baseline - insetAscent + ttoD2;
522 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
523 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
524 pain.rectangle(frame_x, frame_y,
527 frame_is_visible = true;
532 void InsetText::clearFrame(Painter & pain, bool cleared) const
534 if (frame_is_visible) {
536 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
539 frame_is_visible = false;
544 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
547 if (reinit && owner()) {
549 owner()->update(bv, font, true);
554 if (reinit || need_update == INIT) {
556 // we should put this call where we set need_update to INIT!
559 owner()->update(bv, font, true);
563 if (the_locking_inset) {
564 inset_x = cx(bv) - top_x + drawTextXOffset;
565 inset_y = cy(bv) + drawTextYOffset;
566 the_locking_inset->update(bv, font, reinit);
575 int oldw = insetWidth;
576 insetWidth = lt->width + (2 * TEXT_TO_INSET_OFFSET);
577 if (oldw != insetWidth) {
586 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
589 lt->updateInset(bv, the_locking_inset);
591 if (lt->status() == LyXText::NEED_MORE_REFRESH)
599 void InsetText::setUpdateStatus(BufferView * bv, int what) const
607 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
608 // if we don't break row so that we only have one row to update!
609 if ((lt->status() == LyXText::NEED_MORE_REFRESH) ||
611 (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
614 } else if (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
615 need_update |= CURSOR_PAR;
618 // this to not draw a selection when we redraw all of it!
619 if (need_update & CURSOR && !(need_update & SELECTION)) {
620 if (lt->selection.set())
622 lt->clearSelection();
629 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
637 setUpdateStatus(bv, what);
638 if (((need_update != CURSOR) && (need_update != NONE)) ||
639 (lt->status() != LyXText::UNCHANGED) || lt->selection.set())
641 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
643 if (need_update == CURSOR)
645 bv->owner()->showState();
646 if (old_par != cpar(bv)) {
647 bv->owner()->setLayout(cpar(bv)->getLayout());
655 string const InsetText::editMessage() const
657 return _("Opened Text Inset");
661 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
663 UpdatableInset::edit(bv, x, y, button);
665 if (!bv->lockInset(this)) {
666 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
670 the_locking_inset = 0;
671 inset_pos = inset_x = inset_y = 0;
672 inset_boundary = false;
675 int tmp_y = (y < 0) ? 0 : y;
682 if (!checkAndActivateInset(bv, x, tmp_y, button))
683 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
685 lt->clearSelection();
688 updateLocal(bv, CURSOR, false);
690 // If the inset is empty set the language of the current font to the
691 // language to the surronding text (if different).
692 if (par->size() == 0 && !par->next() &&
693 bv->getParentLanguage(this) != lt->current_font.language()) {
694 LyXFont font(LyXFont::ALL_IGNORE);
695 font.setLanguage(bv->getParentLanguage(this));
696 setFont(bv, font, false);
703 void InsetText::edit(BufferView * bv, bool front)
705 UpdatableInset::edit(bv, front);
707 if (!bv->lockInset(this)) {
708 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
712 the_locking_inset = 0;
713 inset_pos = inset_x = inset_y = 0;
714 inset_boundary = false;
723 lt->setCursor(bv, par, 0);
728 // int const pos = (p->size() ? p->size()-1 : p->size());
729 lt->setCursor(bv, p, p->size());
731 lt->clearSelection();
734 updateLocal(bv, CURSOR, false);
736 // If the inset is empty set the language of the current font to the
737 // language to the surronding text (if different).
738 if (par->size() == 0 && !par->next() &&
739 bv->getParentLanguage(this) != lt->current_font.language()) {
740 LyXFont font(LyXFont::ALL_IGNORE);
741 font.setLanguage(bv->getParentLanguage(this));
742 setFont(bv, font, false);
749 void InsetText::insetUnlock(BufferView * bv)
751 if (the_locking_inset) {
752 the_locking_inset->insetUnlock(bv);
753 the_locking_inset = 0;
756 no_selection = false;
759 if (drawFrame_ == LOCKED)
760 code = CURSOR|CLEAR_FRAME;
768 if (lt->selection.set()) {
769 lt->clearSelection();
771 } else if (owner()) {
772 bv->owner()->setLayout(owner()->getLyXText(bv)
773 ->cursor.par()->getLayout());
775 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
776 // hack for deleteEmptyParMech
777 lt->setCursor(bv, par, 0);
778 updateLocal(bv, code, false);
783 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
785 the_locking_inset = inset;
786 inset_x = cx(bv) - top_x + drawTextXOffset;
787 inset_y = cy(bv) + drawTextYOffset;
788 inset_pos = cpos(bv);
789 inset_par = cpar(bv);
790 inset_boundary = cboundary(bv);
791 updateLocal(bv, CURSOR, false);
795 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
797 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
801 if (!the_locking_inset) {
803 int const id = inset->id();
805 Paragraph::inset_iterator it =
806 p->inset_iterator_begin();
807 Paragraph::inset_iterator const end =
808 p->inset_iterator_end();
809 for (; it != end; ++it) {
810 if ((*it) == inset) {
811 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
812 lockInset(bv, inset);
815 if ((*it)->getInsetFromID(id)) {
816 lockInset(bv, static_cast<UpdatableInset *>(*it));
817 return the_locking_inset->lockInsetInInset(bv, inset);
824 if (inset == cpar(bv)->getInset(cpos(bv))) {
825 lyxerr[Debug::INSETS] << "OK" << endl;
826 lockInset(bv, inset);
828 } else if (the_locking_inset && (the_locking_inset == inset)) {
829 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
830 lyxerr[Debug::INSETS] << "OK" << endl;
831 inset_x = cx(bv) - top_x + drawTextXOffset;
832 inset_y = cy(bv) + drawTextYOffset;
834 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
836 } else if (the_locking_inset) {
837 lyxerr[Debug::INSETS] << "MAYBE" << endl;
838 return the_locking_inset->lockInsetInInset(bv, inset);
840 lyxerr[Debug::INSETS] << "NOT OK" << endl;
845 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
848 if (!the_locking_inset)
850 if (the_locking_inset == inset) {
851 the_locking_inset->insetUnlock(bv);
852 getLyXText(bv)->updateInset(bv, inset);
853 the_locking_inset = 0;
855 moveRight(bv, false);
856 old_par = 0; // force layout setting
860 updateLocal(bv, CURSOR, false);
863 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
867 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
874 if (!the_locking_inset) {
875 bool found = lt->updateInset(bv, inset);
879 setUpdateStatus(bv, NONE);
882 if (the_locking_inset != inset) {
884 lt->updateInset(bv, the_locking_inset);
885 setUpdateStatus(bv, CURSOR_PAR);
887 bool found = the_locking_inset->updateInsetInInset(bv, inset);
891 setUpdateStatus(bv, CURSOR_PAR);
894 bool found = lt->updateInset(bv, inset);
898 setUpdateStatus(bv, CURSOR_PAR);
900 updateLocal(bv, CURSOR_PAR, false);
902 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
903 inset_x = cx(bv) - top_x + drawTextXOffset;
904 inset_y = cy(bv) + drawTextYOffset;
911 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
915 int tmp_x = x - drawTextXOffset;
916 int tmp_y = y + insetAscent - getLyXText(bv)->first;
917 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
920 if (the_locking_inset) {
921 if (the_locking_inset == inset) {
922 the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
923 no_selection = false;
926 // otherwise unlock the_locking_inset and lock the new inset
927 the_locking_inset->insetUnlock(bv);
928 inset_x = cx(bv) - top_x + drawTextXOffset;
929 inset_y = cy(bv) + drawTextYOffset;
930 the_locking_inset = static_cast<UpdatableInset*>(inset);
931 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
932 inset->edit(bv, x - inset_x, y - inset_y, button);
933 if (the_locking_inset)
934 updateLocal(bv, CURSOR, false);
935 no_selection = false;
938 // otherwise only unlock the_locking_inset
939 the_locking_inset->insetUnlock(bv);
940 the_locking_inset = 0;
942 if (bv->theLockingInset()) {
943 if (isHighlyEditableInset(inset)) {
944 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
945 inset_x = cx(bv) - top_x + drawTextXOffset;
946 inset_y = cy(bv) + drawTextYOffset;
947 inset_pos = cpos(bv);
948 inset_par = cpar(bv);
949 inset_boundary = cboundary(bv);
950 the_locking_inset = uinset;
951 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
953 uinset->edit(bv, x - inset_x, y - inset_y, 0);
954 if (the_locking_inset)
955 updateLocal(bv, CURSOR, false);
956 no_selection = false;
960 if (!inset) { // && (button == 2)) {
961 bool paste_internally = false;
962 if ((button == 2) && getLyXText(bv)->selection.set()) {
963 localDispatch(bv, LFUN_COPY, "");
964 paste_internally = true;
971 lt->setCursorFromCoordinates(bv, x-drawTextXOffset, y + insetAscent);
972 if (lt->selection.set()) {
973 lt->clearSelection();
974 updateLocal(bv, FULL, false);
976 lt->clearSelection();
978 bv->owner()->setLayout(cpar(bv)->getLayout());
980 // Insert primary selection with middle mouse
981 // if there is a local selection in the current buffer,
984 if (paste_internally)
985 localDispatch(bv, LFUN_PASTE, "");
987 localDispatch(bv, LFUN_PASTESELECTION,
994 no_selection = false;
998 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
1000 UpdatableInset * inset = 0;
1002 if (the_locking_inset) {
1003 the_locking_inset->insetButtonRelease(bv,
1004 x - inset_x, y - inset_y,
1007 if (cpar(bv)->isInset(cpos(bv))) {
1008 inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1009 if (isHighlyEditableInset(inset)) {
1010 inset->insetButtonRelease(bv,
1012 y - inset_y, button);
1014 inset_x = cx(bv) - top_x + drawTextXOffset;
1015 inset_y = cy(bv) + drawTextYOffset;
1016 inset->insetButtonRelease(bv,
1018 y - inset_y, button);
1020 x - inset_x, y - inset_y, button);
1022 updateLocal(bv, CURSOR_PAR, false);
1025 no_selection = false;
1029 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1033 if (the_locking_inset) {
1034 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1040 lt = getLyXText(bv);
1043 hideInsetCursor(bv);
1044 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1045 lt->setSelection(bv);
1046 if (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1047 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos())
1049 updateLocal(bv, SELECTION, false);
1051 showInsetCursor(bv);
1057 void InsetText::insetKeyPress(XKeyEvent * xke)
1059 if (the_locking_inset) {
1060 the_locking_inset->insetKeyPress(xke);
1066 UpdatableInset::RESULT
1067 InsetText::localDispatch(BufferView * bv,
1068 kb_action action, string const & arg)
1070 bool was_empty = par->size() == 0 && !par->next();
1071 no_selection = false;
1072 UpdatableInset::RESULT
1073 result= UpdatableInset::localDispatch(bv, action, arg);
1074 if (result != UNDISPATCHED) {
1078 result = DISPATCHED;
1079 if ((action < 0) && arg.empty())
1082 if (the_locking_inset) {
1083 result = the_locking_inset->localDispatch(bv, action, arg);
1084 if (result == DISPATCHED_NOUPDATE)
1086 else if (result == DISPATCHED) {
1087 updateLocal(bv, CURSOR_PAR, false);
1089 } else if (result >= FINISHED) {
1091 case FINISHED_RIGHT:
1092 moveRightIntern(bv, false, false);
1093 result = DISPATCHED;
1096 if ((result = moveUp(bv)) >= FINISHED) {
1097 updateLocal(bv, CURSOR, false);
1098 bv->unlockInset(this);
1102 if ((result = moveDown(bv)) >= FINISHED) {
1103 updateLocal(bv, CURSOR, false);
1104 bv->unlockInset(this);
1108 result = DISPATCHED;
1111 the_locking_inset = 0;
1112 #ifdef WITH_WARNINGS
1113 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1118 hideInsetCursor(bv);
1121 lt = getLyXText(bv);
1126 case LFUN_SELFINSERT:
1127 if (bv->buffer()->isReadonly()) {
1128 // setErrorMessage(N_("Document is read only"));
1132 /* Automatically delete the currently selected
1133 * text and replace it with what is being
1134 * typed in now. Depends on lyxrc settings
1135 * "auto_region_delete", which defaults to
1138 setUndo(bv, Undo::INSERT,
1139 lt->cursor.par(), lt->cursor.par()->next());
1141 if (lyxrc.auto_region_delete) {
1142 if (lt->selection.set()) {
1143 lt->cutSelection(bv, false);
1146 lt->clearSelection();
1147 for (string::size_type i = 0; i < arg.length(); ++i) {
1148 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1151 lt->selection.cursor = lt->cursor;
1152 updateLocal(bv, CURSOR_PAR, true);
1153 result = DISPATCHED_NOUPDATE;
1155 // --- Cursor Movements -----------------------------------
1158 moveRight(bv, false, true);
1159 lt->setSelection(bv);
1160 updateLocal(bv, SELECTION, false);
1163 result = moveRight(bv);
1165 updateLocal(bv, CURSOR, false);
1169 moveLeft(bv, false, true);
1170 lt->setSelection(bv);
1171 updateLocal(bv, SELECTION, false);
1175 result = moveLeft(bv);
1176 updateLocal(bv, CURSOR, false);
1181 lt->setSelection(bv);
1182 updateLocal(bv, SELECTION, false);
1186 result = moveDown(bv);
1187 updateLocal(bv, CURSOR, false);
1192 lt->setSelection(bv);
1193 updateLocal(bv, SELECTION, false);
1197 result = moveUp(bv);
1198 updateLocal(bv, CURSOR, false);
1203 updateLocal(bv, CURSOR, false);
1207 updateLocal(bv, CURSOR, false);
1209 case LFUN_BACKSPACE: {
1210 setUndo(bv, Undo::DELETE,
1211 lt->cursor.par(), lt->cursor.par()->next());
1212 if (lt->selection.set())
1213 lt->cutSelection(bv);
1216 updateLocal(bv, CURSOR_PAR, true);
1221 setUndo(bv, Undo::DELETE,
1222 lt->cursor.par(), lt->cursor.par()->next());
1223 if (lt->selection.set()) {
1224 lt->cutSelection(bv);
1228 updateLocal(bv, CURSOR_PAR, true);
1233 setUndo(bv, Undo::DELETE,
1234 lt->cursor.par(), lt->cursor.par()->next());
1235 lt->cutSelection(bv);
1236 updateLocal(bv, CURSOR_PAR, true);
1242 lt->copySelection(bv);
1243 updateLocal(bv, CURSOR_PAR, false);
1245 case LFUN_PASTESELECTION:
1247 string const clip(bv->getClipboard());
1251 if (arg == "paragraph") {
1252 lt->insertStringAsParagraphs(bv, clip);
1254 lt->insertStringAsLines(bv, clip);
1256 updateLocal(bv, CURSOR_PAR, true);
1260 if (!autoBreakRows) {
1262 if (CutAndPaste::nrOfParagraphs() > 1) {
1263 Alert::alert(_("Impossible operation"),
1264 _("Cannot include more than one paragraph!"),
1269 setUndo(bv, Undo::INSERT,
1270 lt->cursor.par(), lt->cursor.par()->next());
1271 lt->pasteSelection(bv);
1272 updateLocal(bv, CURSOR_PAR, true);
1276 case LFUN_BREAKPARAGRAPH:
1277 if (!autoBreakRows) {
1278 result = DISPATCHED;
1281 lt->breakParagraph(bv, 0);
1282 updateLocal(bv, FULL, true);
1284 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1285 if (!autoBreakRows) {
1286 result = DISPATCHED;
1289 lt->breakParagraph(bv, 1);
1290 updateLocal(bv, FULL, true);
1293 case LFUN_BREAKLINE: {
1294 if (!autoBreakRows) {
1295 result = DISPATCHED;
1298 setUndo(bv, Undo::INSERT,
1299 lt->cursor.par(), lt->cursor.par()->next());
1300 lt->insertChar(bv, Paragraph::META_NEWLINE);
1301 updateLocal(bv, CURSOR_PAR, true);
1306 // do not set layouts on non breakable textinsets
1307 if (autoBreakRows) {
1308 layout_type cur_layout = cpar(bv)->layout;
1310 // Derive layout number from given argument (string)
1311 // and current buffer's textclass (number). */
1312 textclass_type tclass = bv->buffer()->params.textclass;
1313 std::pair <bool, layout_type> layout =
1314 textclasslist.NumberOfLayout(tclass, arg);
1316 // If the entry is obsolete, use the new one instead.
1318 string obs = textclasslist.Style(tclass,layout.second).
1321 layout = textclasslist.NumberOfLayout(tclass, obs);
1324 // see if we found the layout number:
1325 if (!layout.first) {
1326 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1327 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1331 if (cur_layout != layout.second) {
1332 cur_layout = layout.second;
1333 lt->setLayout(bv, layout.second);
1334 bv->owner()->setLayout(cpar(bv)->getLayout());
1335 updateLocal(bv, CURSOR_PAR, true);
1338 // reset the layout box
1339 bv->owner()->setLayout(cpar(bv)->getLayout());
1342 case LFUN_PARAGRAPH_SPACING:
1343 // This one is absolutely not working. When fiddling with this
1344 // it also seems to me that the paragraphs inside the insettext
1345 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1347 Paragraph * par = lt->cursor.par();
1348 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1349 float cur_value = 1.0;
1350 if (cur_spacing == Spacing::Other) {
1351 cur_value = par->params().spacing().getValue();
1354 istringstream istr(arg.c_str());
1357 Spacing::Space new_spacing = cur_spacing;
1358 float new_value = cur_value;
1360 lyxerr << "Missing argument to `paragraph-spacing'"
1362 } else if (tmp == "single") {
1363 new_spacing = Spacing::Single;
1364 } else if (tmp == "onehalf") {
1365 new_spacing = Spacing::Onehalf;
1366 } else if (tmp == "double") {
1367 new_spacing = Spacing::Double;
1368 } else if (tmp == "other") {
1369 new_spacing = Spacing::Other;
1372 lyxerr << "new_value = " << tmpval << endl;
1375 } else if (tmp == "default") {
1376 new_spacing = Spacing::Default;
1378 lyxerr << _("Unknown spacing argument: ")
1381 if (cur_spacing != new_spacing || cur_value != new_value) {
1382 par->params().spacing(Spacing(new_spacing, new_value));
1383 updateLocal(bv, CURSOR_PAR, true);
1389 if (!bv->Dispatch(action, arg))
1390 result = UNDISPATCHED;
1394 /// If the action has deleted all text in the inset, we need to change the
1395 // language to the language of the surronding text.
1396 if (!was_empty && par->size() == 0 && !par->next()) {
1397 LyXFont font(LyXFont::ALL_IGNORE);
1398 font.setLanguage(bv->getParentLanguage(this));
1399 setFont(bv, font, false);
1402 if (result < FINISHED) {
1403 showInsetCursor(bv);
1405 bv->unlockInset(this);
1412 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1415 buf->latexParagraphs(os, par, 0, texrow);
1416 return texrow.rows();
1420 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1422 Paragraph * p = par;
1423 unsigned int lines = 0;
1426 string const tmp = buf->asciiParagraph(p, linelen);
1427 lines += countChar(tmp, '\n');
1435 int InsetText::docbook(Buffer const * buf, ostream & os) const
1437 Paragraph * p = par;
1438 unsigned int lines = 0;
1440 vector<string> environment_stack(10);
1441 vector<string> environment_inner(10);
1443 int const command_depth = 0;
1446 Paragraph::depth_type depth = 0; // paragraph depth
1450 int desc_on = 0; // description mode
1452 LyXLayout const & style =
1453 textclasslist.Style(buf->params.textclass,
1456 // environment tag closing
1457 for (; depth > p->params().depth(); --depth) {
1458 if (environment_inner[depth] != "!-- --") {
1459 item_name = "listitem";
1460 buf->sgmlCloseTag(os, command_depth + depth,
1462 if (environment_inner[depth] == "varlistentry")
1463 buf->sgmlCloseTag(os, depth+command_depth,
1464 environment_inner[depth]);
1466 buf->sgmlCloseTag(os, depth + command_depth,
1467 environment_stack[depth]);
1468 environment_stack[depth].erase();
1469 environment_inner[depth].erase();
1472 if (depth == p->params().depth()
1473 && environment_stack[depth] != style.latexname()
1474 && !environment_stack[depth].empty()) {
1475 if (environment_inner[depth] != "!-- --") {
1476 item_name= "listitem";
1477 buf->sgmlCloseTag(os, command_depth+depth,
1479 if (environment_inner[depth] == "varlistentry")
1480 buf->sgmlCloseTag(os,
1481 depth + command_depth,
1482 environment_inner[depth]);
1485 buf->sgmlCloseTag(os, depth + command_depth,
1486 environment_stack[depth]);
1488 environment_stack[depth].erase();
1489 environment_inner[depth].erase();
1492 // Write opening SGML tags.
1493 switch (style.latextype) {
1494 case LATEX_PARAGRAPH:
1495 buf->sgmlOpenTag(os, depth + command_depth,
1500 buf->sgmlError(p, 0,
1501 _("Error : LatexType Command not allowed here.\n"));
1505 case LATEX_ENVIRONMENT:
1506 case LATEX_ITEM_ENVIRONMENT:
1507 if (depth < p->params().depth()) {
1508 depth = p->params().depth();
1509 environment_stack[depth].erase();
1512 if (environment_stack[depth] != style.latexname()) {
1513 if(environment_stack.size() == depth + 1) {
1514 environment_stack.push_back("!-- --");
1515 environment_inner.push_back("!-- --");
1517 environment_stack[depth] = style.latexname();
1518 environment_inner[depth] = "!-- --";
1519 buf->sgmlOpenTag(os, depth + command_depth,
1520 environment_stack[depth]);
1522 if (environment_inner[depth] != "!-- --") {
1523 item_name= "listitem";
1524 buf->sgmlCloseTag(os,
1525 command_depth + depth,
1527 if (environment_inner[depth] == "varlistentry")
1528 buf->sgmlCloseTag(os,
1529 depth + command_depth,
1530 environment_inner[depth]);
1534 if (style.latextype == LATEX_ENVIRONMENT) {
1535 if (!style.latexparam().empty()) {
1536 if(style.latexparam() == "CDATA")
1539 buf->sgmlOpenTag(os, depth + command_depth,
1540 style.latexparam());
1545 desc_on = (style.labeltype == LABEL_MANUAL);
1548 environment_inner[depth]= "varlistentry";
1550 environment_inner[depth]= "listitem";
1552 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1553 environment_inner[depth]);
1557 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1561 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1566 buf->sgmlOpenTag(os, depth + command_depth,
1571 buf->simpleDocBookOnePar(os, p, desc_on,
1572 depth + 1 + command_depth);
1576 // write closing SGML tags
1577 switch (style.latextype) {
1578 case LATEX_ENVIRONMENT:
1579 if (!style.latexparam().empty()) {
1580 if(style.latexparam() == "CDATA")
1583 buf->sgmlCloseTag(os, depth + command_depth,
1584 style.latexparam());
1587 case LATEX_ITEM_ENVIRONMENT:
1588 if (desc_on == 1) break;
1590 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1592 case LATEX_PARAGRAPH:
1593 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1596 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1602 for (int d = depth; d >= 0; --d) {
1603 if (!environment_stack[depth].empty()) {
1604 if (environment_inner[depth] != "!-- --") {
1605 item_name = "listitem";
1606 buf->sgmlCloseTag(os, command_depth + depth,
1608 if (environment_inner[depth] == "varlistentry")
1609 buf->sgmlCloseTag(os, depth + command_depth,
1610 environment_inner[depth]);
1613 buf->sgmlCloseTag(os, depth + command_depth,
1614 environment_stack[depth]);
1622 void InsetText::validate(LaTeXFeatures & features) const
1624 Paragraph * p = par;
1626 p->validate(features);
1632 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1634 if (textclasslist.Style(buf->params.textclass,
1635 p->getLayout()).labeltype != LABEL_MANUAL)
1638 return p->beginningOfMainBody();
1642 void InsetText::getCursorPos(BufferView * bv,
1643 int & x, int & y) const
1645 if (the_locking_inset) {
1646 the_locking_inset->getCursorPos(bv, x, y);
1654 unsigned int InsetText::insetInInsetY()
1656 if (!the_locking_inset)
1659 return (inset_y + the_locking_inset->insetInInsetY());
1663 void InsetText::toggleInsetCursor(BufferView * bv)
1665 if (the_locking_inset) {
1666 the_locking_inset->toggleInsetCursor(bv);
1670 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1672 int const asc = lyxfont::maxAscent(font);
1673 int const desc = lyxfont::maxDescent(font);
1675 if (isCursorVisible())
1676 bv->hideLockedInsetCursor();
1678 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1679 toggleCursorVisible();
1683 void InsetText::showInsetCursor(BufferView * bv, bool show)
1685 if (the_locking_inset) {
1686 the_locking_inset->showInsetCursor(bv, show);
1689 if (!isCursorVisible()) {
1690 LyXFont const font =
1691 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1693 int const asc = lyxfont::maxAscent(font);
1694 int const desc = lyxfont::maxDescent(font);
1696 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1698 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1699 setCursorVisible(true);
1704 void InsetText::hideInsetCursor(BufferView * bv)
1706 if (isCursorVisible()) {
1707 bv->hideLockedInsetCursor();
1708 setCursorVisible(false);
1710 if (the_locking_inset)
1711 the_locking_inset->hideInsetCursor(bv);
1715 void InsetText::fitInsetCursor(BufferView * bv) const
1717 if (the_locking_inset) {
1718 the_locking_inset->fitInsetCursor(bv);
1721 LyXFont const font =
1722 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1724 int const asc = lyxfont::maxAscent(font);
1725 int const desc = lyxfont::maxDescent(font);
1727 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1731 UpdatableInset::RESULT
1732 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1734 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1735 return moveLeftIntern(bv, false, activate_inset, selecting);
1737 return moveRightIntern(bv, false, activate_inset, selecting);
1741 UpdatableInset::RESULT
1742 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1744 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1745 return moveRightIntern(bv, true, activate_inset, selecting);
1747 return moveLeftIntern(bv, true, activate_inset, selecting);
1751 UpdatableInset::RESULT
1752 InsetText::moveRightIntern(BufferView * bv, bool behind,
1753 bool activate_inset, bool selecting)
1755 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1756 return FINISHED_RIGHT;
1757 if (activate_inset && checkAndActivateInset(bv, behind))
1759 getLyXText(bv)->cursorRight(bv);
1761 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1762 return DISPATCHED_NOUPDATE;
1766 UpdatableInset::RESULT
1767 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1768 bool activate_inset, bool selecting)
1770 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1772 getLyXText(bv)->cursorLeft(bv);
1774 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1775 if (activate_inset && checkAndActivateInset(bv, behind))
1777 return DISPATCHED_NOUPDATE;
1781 UpdatableInset::RESULT
1782 InsetText::moveUp(BufferView * bv)
1784 if (!crow(bv)->previous())
1786 getLyXText(bv)->cursorUp(bv);
1787 return DISPATCHED_NOUPDATE;
1791 UpdatableInset::RESULT
1792 InsetText::moveDown(BufferView * bv)
1794 if (!crow(bv)->next())
1795 return FINISHED_DOWN;
1796 getLyXText(bv)->cursorDown(bv);
1797 return DISPATCHED_NOUPDATE;
1801 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1803 if (the_locking_inset) {
1804 if (the_locking_inset->insetAllowed(inset))
1805 return the_locking_inset->insertInset(bv, inset);
1808 inset->setOwner(this);
1809 hideInsetCursor(bv);
1813 lt = getLyXText(bv);
1816 lt->insertInset(bv, inset);
1818 if ((!cpar(bv)->isInset(cpos(bv))) ||
1819 (cpar(bv)->getInset(cpos(bv)) != inset))
1823 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1824 // showInsetCursor(bv);
1831 bool InsetText::insetAllowed(Inset::Code code) const
1833 // in_insetAllowed is a really gross hack,
1834 // to allow us to call the owner's insetAllowed
1835 // without stack overflow, which can happen
1836 // when the owner uses InsetCollapsable::insetAllowed()
1838 if (in_insetAllowed)
1840 in_insetAllowed = true;
1841 if (the_locking_inset)
1842 ret = the_locking_inset->insetAllowed(code);
1844 ret = owner()->insetAllowed(code);
1845 in_insetAllowed = false;
1850 UpdatableInset * InsetText::getLockingInset() const
1852 return the_locking_inset ? the_locking_inset->getLockingInset() :
1853 const_cast<InsetText *>(this);
1857 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1861 if (the_locking_inset)
1862 return the_locking_inset->getFirstLockingInsetOfType(c);
1867 bool InsetText::showInsetDialog(BufferView * bv) const
1869 if (the_locking_inset)
1870 return the_locking_inset->showInsetDialog(bv);
1875 std::vector<string> const InsetText::getLabelList() const
1877 std::vector<string> label_list;
1879 Paragraph * tpar = par;
1881 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1882 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1883 for (; beg != end; ++beg) {
1884 std::vector<string> const l = (*beg)->getLabelList();
1885 label_list.insert(label_list.end(), l.begin(), l.end());
1887 tpar = tpar->next();
1893 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1896 if (the_locking_inset) {
1897 the_locking_inset->setFont(bv, font, toggleall, selectall);
1900 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1901 getLyXText(bv)->setFont(bv, font, toggleall);
1906 lt = getLyXText(bv);
1909 if (lt->selection.set()) {
1910 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1914 lt->toggleFree(bv, font, toggleall);
1916 lt->clearSelection();
1918 if (selectall || lt->selection.set())
1919 updateLocal(bv, FULL, true);
1921 updateLocal(bv, CURSOR_PAR, true);
1927 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1929 if (cpar(bv)->isInset(cpos(bv))) {
1933 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1934 if (!isHighlyEditableInset(inset))
1936 LyXFont const font =
1937 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1939 x = inset->width(bv, font);
1940 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1943 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1945 //inset_x = cx(bv) - top_x + drawTextXOffset;
1946 //inset_y = cy(bv) + drawTextYOffset;
1947 inset->edit(bv, x, y, 0);
1948 if (!the_locking_inset)
1950 updateLocal(bv, CURSOR, false);
1957 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1960 x -= drawTextXOffset;
1962 int dummyy = y + insetAscent;
1963 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1970 inset_x = cx(bv) - top_x + drawTextXOffset;
1971 inset_y = cy(bv) + drawTextYOffset;
1972 inset->edit(bv, x - inset_x, y - inset_y, button);
1973 if (!the_locking_inset)
1975 updateLocal(bv, CURSOR, false);
1982 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1985 int w = UpdatableInset::getMaxWidth(bv, inset);
1990 w = w - top_x + owner()->x();
1993 w -= (2 * TEXT_TO_INSET_OFFSET);
1996 return UpdatableInset::getMaxWidth(bv, inset);
2001 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2003 // we have to unlock any locked inset otherwise we're in troubles
2004 the_locking_inset = 0;
2006 Paragraph * tmp = par->next();
2011 par = new Paragraph(*p, same_id);
2012 par->setInsetOwner(this);
2013 Paragraph * np = par;
2016 np->next(new Paragraph(*p, same_id));
2017 np->next()->previous(np);
2019 np->setInsetOwner(this);
2026 void InsetText::setText(string const & data)
2029 LyXFont font(LyXFont::ALL_SANE);
2030 for (unsigned int i=0; i < data.length(); ++i)
2031 par->insertChar(i, data[i], font);
2035 void InsetText::setAutoBreakRows(bool flag)
2037 if (flag != autoBreakRows) {
2038 autoBreakRows = flag;
2046 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2048 if (how != drawFrame_) {
2051 updateLocal(bv, DRAW_FRAME, false);
2056 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2058 if (frame_color != col) {
2061 updateLocal(bv, DRAW_FRAME, false);
2066 int InsetText::cx(BufferView * bv) const
2070 lt = getLyXText(bv);
2073 int x = lt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2074 if (the_locking_inset) {
2075 LyXFont font = lt->getFont(bv->buffer(),
2078 if (font.isVisibleRightToLeft())
2079 x -= the_locking_inset->width(bv, font);
2087 int InsetText::cy(BufferView * bv) const
2090 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2094 pos_type InsetText::cpos(BufferView * bv) const
2096 return getLyXText(bv)->cursor.pos();
2100 Paragraph * InsetText::cpar(BufferView * bv) const
2102 return getLyXText(bv)->cursor.par();
2106 bool InsetText::cboundary(BufferView * bv) const
2108 return getLyXText(bv)->cursor.boundary();
2112 Row * InsetText::crow(BufferView * bv) const
2114 return getLyXText(bv)->cursor.row();
2118 LyXText * InsetText::getLyXText(BufferView const * lbv,
2119 bool const recursive) const
2121 if (!recursive && (cached_bview == lbv)) {
2122 LyXText * lt = cached_text.get();
2123 lyx::Assert(lt && lt->firstRow()->par() == par);
2124 return cached_text.get();
2127 // Super UGLY! (Lgb)
2128 BufferView * bv = const_cast<BufferView *>(lbv);
2131 Cache::iterator it = cache.find(bv);
2133 if (it != cache.end()) {
2137 resizeLyXText(do_resize);
2138 if (lt || !it->second.remove) {
2139 lyx::Assert(it->second.text.get());
2140 cached_text = it->second.text;
2141 if (recursive && the_locking_inset) {
2142 return the_locking_inset->getLyXText(bv, true);
2144 return cached_text.get();
2145 } else if (it->second.remove) {
2147 saveLyXTextState(it->second.text.get());
2154 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2155 cached_text->init(bv);
2156 restoreLyXTextState(bv, cached_text.get());
2158 cache.insert(make_pair(bv, cached_text));
2160 if (the_locking_inset && recursive) {
2161 return the_locking_inset->getLyXText(bv);
2163 return cached_text.get();
2167 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2171 Cache::iterator it = cache.find(bv);
2173 if (it == cache.end()) {
2177 lyx::Assert(it->second.text.get());
2179 it->second.remove = true;
2181 /// then remove all LyXText in text-insets
2182 Paragraph * p = par;
2183 for (; p; p = p->next()) {
2184 p->deleteInsetsLyXText(bv);
2190 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2193 // we cannot resize this because we are in use!
2194 // so do this on the next possible getLyXText()
2199 // lyxerr << "InsetText::resizeLyXText\n";
2200 if (!par->next() && !par->size()) // no data, resize not neccessary!
2202 // one endless line, resize normally not necessary
2203 if (!force && getMaxWidth(bv, this) < 0)
2206 Cache::iterator it = cache.find(bv);
2207 if (it == cache.end()) {
2210 lyx::Assert(it->second.text.get());
2212 LyXText * t = it->second.text.get();
2213 saveLyXTextState(t);
2214 for (Paragraph * p = par; p; p = p->next()) {
2215 p->resizeInsetsLyXText(bv);
2218 restoreLyXTextState(bv, t);
2219 if (the_locking_inset) {
2220 inset_x = cx(bv) - top_x + drawTextXOffset;
2221 inset_y = cy(bv) + drawTextYOffset;
2225 t->first = bv->screen()->topCursorVisible(t);
2228 updateLocal(bv, FULL, false);
2229 // this will scroll the screen such that the cursor becomes visible
2230 bv->updateScrollbar();
2232 need_update |= FULL;
2237 void InsetText::reinitLyXText() const
2240 // we cannot resize this because we are in use!
2241 // so do this on the next possible getLyXText()
2247 // lyxerr << "InsetText::reinitLyXText\n";
2248 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2249 lyx::Assert(it->second.text.get());
2251 LyXText * t = it->second.text.get();
2252 BufferView * bv = it->first;
2254 saveLyXTextState(t);
2255 for (Paragraph * p = par; p; p = p->next()) {
2256 p->resizeInsetsLyXText(bv);
2259 restoreLyXTextState(bv, t);
2260 if (the_locking_inset) {
2261 inset_x = cx(bv) - top_x + drawTextXOffset;
2262 inset_y = cy(bv) + drawTextYOffset;
2265 t->first = bv->screen()->topCursorVisible(t);
2268 updateLocal(bv, FULL, false);
2269 // this will scroll the screen such that the cursor becomes visible
2270 bv->updateScrollbar();
2278 void InsetText::removeNewlines()
2280 bool changed = false;
2282 for (Paragraph * p = par; p; p = p->next()) {
2283 for (int i = 0; i < p->size(); ++i) {
2284 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2295 bool InsetText::nodraw() const
2297 if (the_locking_inset)
2298 return the_locking_inset->nodraw();
2299 return UpdatableInset::nodraw();
2303 int InsetText::scroll(bool recursive) const
2305 int sx = UpdatableInset::scroll(false);
2307 if (recursive && the_locking_inset)
2308 sx += the_locking_inset->scroll(recursive);
2314 bool InsetText::doClearArea() const
2316 return !locked || (need_update & (FULL|INIT));
2320 void InsetText::selectAll(BufferView * bv)
2322 getLyXText(bv)->cursorTop(bv);
2323 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2324 getLyXText(bv)->cursorBottom(bv);
2325 getLyXText(bv)->setSelection(bv);
2329 void InsetText::clearSelection(BufferView * bv)
2331 getLyXText(bv)->clearSelection();
2335 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2338 Painter & pain = bv->painter();
2340 int h = ascent(bv, dummy) + descent(bv, dummy);
2341 int ty = baseline - insetAscent;
2347 if ((ty + h) > pain.paperHeight())
2348 h = pain.paperHeight();
2349 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2350 w = pain.paperWidth();
2351 // w -= TEXT_TO_INSET_OFFSET;
2352 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2355 frame_is_visible = false;
2359 Paragraph * InsetText::getParFromID(int id) const
2362 Paragraph * result = par;
2363 Paragraph * ires = 0;
2364 while (result && result->id() != id) {
2365 if ((ires = result->getParFromID(id)))
2367 result = result->next();
2371 Paragraph * tmp = par;
2373 if (tmp->id() == id) {
2376 Paragraph * tmp2 = tmp->getParFromID(id);
2387 Paragraph * InsetText::firstParagraph() const
2390 if (the_locking_inset)
2391 if ((result = the_locking_inset->firstParagraph()))
2397 Paragraph * InsetText::getFirstParagraph(int i) const
2399 return (i == 0) ? par : 0;
2403 LyXCursor const & InsetText::cursor(BufferView * bv) const
2405 if (the_locking_inset)
2406 return the_locking_inset->cursor(bv);
2407 return getLyXText(bv)->cursor;
2411 Paragraph * InsetText::paragraph() const
2417 void InsetText::paragraph(Paragraph * p)
2419 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2420 // caller of this function has to take care of it. This IS important
2421 // as we could have to insert a paragraph before this one and just
2422 // link the actual to a new ones next and set it with this function
2425 // set ourself as owner for all the paragraphs inserted!
2426 Paragraph * np = par;
2428 np->setInsetOwner(this);
2432 // redraw myself when asked for
2437 Inset * InsetText::getInsetFromID(int id_arg) const
2440 return const_cast<InsetText *>(this);
2442 Paragraph * lp = par;
2445 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2446 en = lp->inset_iterator_end();
2449 if ((*it)->id() == id_arg)
2451 Inset * in = (*it)->getInsetFromID(id_arg);
2461 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2467 lt = getLyXText(bv);
2470 if (the_locking_inset) {
2471 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2478 #warning Dekel please have a look on this one RTL? (Jug)
2480 // we have to go on checking so move cusor to the right
2481 lt->cursor.pos(lt->cursor.pos() + 1);
2483 str = lt->selectNextWordToSpellcheck(bv, value);
2485 bv->unlockInset(const_cast<InsetText *>(this));
2494 void InsetText::selectSelectedWord(BufferView * bv)
2496 if (the_locking_inset) {
2497 the_locking_inset->selectSelectedWord(bv);
2500 getLyXText(bv)->selectSelectedWord(bv);
2501 updateLocal(bv, SELECTION, false);
2505 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2507 if (the_locking_inset) {
2508 the_locking_inset->toggleSelection(bv, kill_selection);
2512 lt = getLyXText(bv);
2516 int x = top_x + TEXT_TO_INSET_OFFSET;
2518 Row * row = lt->firstRow();
2519 int y_offset = top_baseline - row->ascent_of_text();
2521 while ((row != 0) && ((y+row->height()) <= 0)) {
2528 if (need_update & SELECTION)
2530 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2536 bool InsetText::searchForward(BufferView * bv, string const & str,
2537 bool const & cs, bool const & mw)
2539 if (the_locking_inset) {
2540 if (the_locking_inset->searchForward(bv, str, cs, mw))
2544 lt = getLyXText(bv);
2547 Paragraph * lpar = lt->cursor.par();
2548 pos_type pos = lt->cursor.pos();
2549 if (pos < lpar->size() - 1)
2553 lpar = lpar->next();
2558 // we have to unlock ourself in this function by default!
2559 bv->unlockInset(const_cast<InsetText *>(this));
2562 lt->setCursor(bv, lpar, pos);
2566 if (LyXFind(bv, str, true, true, cs , mw)) {
2569 // we have to unlock ourself in this function by default!
2570 bv->unlockInset(const_cast<InsetText *>(this));
2574 bool InsetText::searchBackward(BufferView * bv, string const & str,
2575 bool const & cs, bool const & mw)
2577 if (the_locking_inset)
2578 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2580 if (LyXFind(bv, str, false, true, cs, mw)) {
2583 // we have to unlock ourself in this function by default!
2584 bv->unlockInset(const_cast<InsetText *>(this));
2589 bool InsetText::checkInsertChar(LyXFont & font)
2592 return owner()->checkInsertChar(font);