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
296 insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
297 TEXT_TO_INSET_OFFSET;
302 int InsetText::descent(BufferView * bv, LyXFont const &) const
304 LyXText * llt = getLyXText(bv);
305 insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
306 TEXT_TO_INSET_OFFSET;
311 int InsetText::width(BufferView * bv, LyXFont const &) const
313 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
314 (2 * TEXT_TO_INSET_OFFSET);
315 insetWidth = max(insetWidth, 10);
320 int InsetText::textWidth(BufferView * bv, bool fordraw) const
323 if (!autoBreakRows) {
326 w = getMaxWidth(bv, this);
329 return max(w - (2 * TEXT_TO_INSET_OFFSET),
330 (int)getLyXText(bv)->width);
334 return w - (2 * TEXT_TO_INSET_OFFSET);
338 void InsetText::draw(BufferView * bv, LyXFont const & f,
339 int baseline, float & x, bool cleared) const
344 Painter & pain = bv->painter();
346 // this is the first thing we have to ask because if the x pos
347 // changed we have to do a complete rebreak of the text as we
348 // may have few space to draw in. Well we should check on this too
350 if (top_x != int(x)) {
352 int nw = getMaxWidth(bv, this);
353 if (nw > 0 && old_max_width != nw) {
356 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
364 // repaint the background if needed
365 if (cleared && backgroundColor() != LColor::background) {
367 clearInset(bv, baseline, cleared);
371 // no draw is necessary !!!
372 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
375 top_baseline = baseline;
377 if (need_update & CLEAR_FRAME)
378 clearFrame(pain, cleared);
385 x += static_cast<float>(scroll());
387 // if top_x differs we did it already
388 if (!cleared && (top_x == int(x))
389 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
390 ||(last_drawn_width != insetWidth))) {
391 clearInset(bv, baseline, cleared);
397 frame_is_visible = false;
399 if (!cleared && (need_update == NONE)) {
401 drawFrame(pain, cleared);
405 // call these methods so that insetWidth, insetAscent and
406 // insetDescent have the right values.
411 top_baseline = baseline;
412 top_y = baseline - insetAscent;
414 if (last_drawn_width != insetWidth) {
416 clearInset(bv, baseline, cleared);
418 last_drawn_width = insetWidth;
421 if (the_locking_inset && (cpar(bv) == inset_par)
422 && (cpos(bv) == inset_pos)) {
423 inset_x = cx(bv) - top_x + drawTextXOffset;
424 inset_y = cy(bv) + drawTextYOffset;
426 if (!cleared && (need_update == CURSOR)
427 && !getLyXText(bv)->selection.set()) {
428 drawFrame(pain, cleared);
438 x += TEXT_TO_INSET_OFFSET;
440 Row * row = lt->firstRow();
441 int y_offset = baseline - row->ascent_of_text();
442 int ph = pain.paperHeight();
445 while ((row != 0) && ((y+row->height()) <= 0)) {
447 first += row->height();
453 if (cleared || (need_update&(INIT|FULL))) {
456 while ((row != 0) && (yf < ph)) {
457 lt->getVisibleRow(bv, y+y_offset, int(x), row,
463 } else if (!locked) {
464 if (need_update & CURSOR) {
465 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
466 lt->clearSelection();
467 lt->selection.cursor = lt->cursor;
469 bv->screen()->update(lt, bv, y_offset, int(x));
472 if (need_update & SELECTION) {
473 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
474 } else if (need_update & CURSOR) {
475 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
476 lt->clearSelection();
477 lt->selection.cursor = lt->cursor;
479 bv->screen()->update(lt, bv, y_offset, int(x));
484 lt->status(bv, LyXText::UNCHANGED);
485 if ((need_update != CURSOR_PAR) &&
486 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked))) {
487 drawFrame(pain, cleared);
488 } else if (need_update & CLEAR_FRAME) {
489 clearFrame(pain, cleared);
492 x += insetWidth - TEXT_TO_INSET_OFFSET;
494 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
496 } else if (need_update != INIT) {
504 void InsetText::drawFrame(Painter & pain, bool cleared) const
506 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
507 if (!frame_is_visible || cleared) {
508 frame_x = top_x + ttoD2;
509 frame_y = top_baseline - insetAscent + ttoD2;
510 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
511 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
512 pain.rectangle(frame_x, frame_y,
515 frame_is_visible = true;
520 void InsetText::clearFrame(Painter & pain, bool cleared) const
522 if (frame_is_visible) {
524 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
527 frame_is_visible = false;
532 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
535 if (reinit && owner()) {
537 owner()->update(bv, font, true);
542 if (reinit || need_update == INIT) {
544 // we should put this call where we set need_update to INIT!
547 owner()->update(bv, font, true);
551 if (the_locking_inset) {
552 inset_x = cx(bv) - top_x + drawTextXOffset;
553 inset_y = cy(bv) + drawTextYOffset;
554 the_locking_inset->update(bv, font, reinit);
562 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
565 lt->updateInset(bv, the_locking_inset);
567 if (lt->status() == LyXText::NEED_MORE_REFRESH)
575 void InsetText::setUpdateStatus(BufferView * bv, int what) const
577 // this does nothing dangerous so use only a localized buffer
578 LyXText * llt = getLyXText(bv);
581 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
582 // if we don't break row so that we only have one row to update!
583 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
585 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
588 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
589 need_update |= CURSOR_PAR;
592 // this to not draw a selection when we redraw all of it!
593 if (need_update & CURSOR && !(need_update & SELECTION)) {
594 if (llt->selection.set())
596 llt->clearSelection();
601 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
603 if (!autoBreakRows && par->next())
604 collapseParagraphs(bv->buffer()->params);
611 setUpdateStatus(bv, what);
612 bool flag = (((need_update != CURSOR) && (need_update != NONE)) ||
613 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
617 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
618 if (need_update == CURSOR)
620 bv->owner()->showState();
621 if (old_par != cpar(bv)) {
622 bv->owner()->setLayout(cpar(bv)->getLayout());
628 string const InsetText::editMessage() const
630 return _("Opened Text Inset");
634 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
636 UpdatableInset::edit(bv, x, y, button);
638 if (!bv->lockInset(this)) {
639 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
643 the_locking_inset = 0;
644 inset_pos = inset_x = inset_y = 0;
645 inset_boundary = false;
648 int tmp_y = (y < 0) ? 0 : y;
654 if (!checkAndActivateInset(bv, x, tmp_y, button))
655 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
657 lt->clearSelection();
659 // If the inset is empty set the language of the current font to the
660 // language to the surronding text (if different).
661 if (par->size() == 0 && !par->next() &&
662 bv->getParentLanguage(this) != lt->current_font.language())
664 LyXFont font(LyXFont::ALL_IGNORE);
665 font.setLanguage(bv->getParentLanguage(this));
666 setFont(bv, font, false);
671 updateLocal(bv, CURSOR, false);
675 void InsetText::edit(BufferView * bv, bool front)
677 UpdatableInset::edit(bv, front);
679 if (!bv->lockInset(this)) {
680 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
684 the_locking_inset = 0;
685 inset_pos = inset_x = inset_y = 0;
686 inset_boundary = false;
695 lt->setCursor(bv, par, 0);
700 // int const pos = (p->size() ? p->size()-1 : p->size());
701 lt->setCursor(bv, p, p->size());
703 lt->clearSelection();
705 // If the inset is empty set the language of the current font to the
706 // language to the surronding text (if different).
707 if (par->size() == 0 && !par->next() &&
708 bv->getParentLanguage(this) != lt->current_font.language()) {
709 LyXFont font(LyXFont::ALL_IGNORE);
710 font.setLanguage(bv->getParentLanguage(this));
711 setFont(bv, font, false);
716 updateLocal(bv, CURSOR, false);
720 void InsetText::insetUnlock(BufferView * bv)
722 if (the_locking_inset) {
723 the_locking_inset->insetUnlock(bv);
724 the_locking_inset = 0;
727 no_selection = false;
730 if (drawFrame_ == LOCKED)
731 code = CURSOR|CLEAR_FRAME;
739 if (lt->selection.set()) {
740 lt->clearSelection();
742 } else if (owner()) {
743 bv->owner()->setLayout(owner()->getLyXText(bv)
744 ->cursor.par()->getLayout());
746 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
747 // hack for deleteEmptyParMech
748 lt->setCursor(bv, par, 0);
751 updateLocal(bv, code, false);
754 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
756 the_locking_inset = inset;
757 inset_x = cx(bv) - top_x + drawTextXOffset;
758 inset_y = cy(bv) + drawTextYOffset;
759 inset_pos = cpos(bv);
760 inset_par = cpar(bv);
761 inset_boundary = cboundary(bv);
762 updateLocal(bv, CURSOR, false);
766 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
768 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
772 if (!the_locking_inset) {
774 int const id = inset->id();
776 Paragraph::inset_iterator it =
777 p->inset_iterator_begin();
778 Paragraph::inset_iterator const end =
779 p->inset_iterator_end();
780 for (; it != end; ++it) {
781 if ((*it) == inset) {
782 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
783 lockInset(bv, inset);
786 if ((*it)->getInsetFromID(id)) {
787 lockInset(bv, static_cast<UpdatableInset *>(*it));
788 return the_locking_inset->lockInsetInInset(bv, inset);
795 if (inset == cpar(bv)->getInset(cpos(bv))) {
796 lyxerr[Debug::INSETS] << "OK" << endl;
797 lockInset(bv, inset);
799 } else if (the_locking_inset && (the_locking_inset == inset)) {
800 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
801 lyxerr[Debug::INSETS] << "OK" << endl;
802 inset_x = cx(bv) - top_x + drawTextXOffset;
803 inset_y = cy(bv) + drawTextYOffset;
805 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
807 } else if (the_locking_inset) {
808 lyxerr[Debug::INSETS] << "MAYBE" << endl;
809 return the_locking_inset->lockInsetInInset(bv, inset);
811 lyxerr[Debug::INSETS] << "NOT OK" << endl;
816 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
819 if (!the_locking_inset)
821 if (the_locking_inset == inset) {
822 the_locking_inset->insetUnlock(bv);
823 getLyXText(bv)->updateInset(bv, inset);
824 the_locking_inset = 0;
826 moveRight(bv, false);
827 old_par = 0; // force layout setting
831 updateLocal(bv, CURSOR, false);
834 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
838 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
840 if (!autoBreakRows && par->next())
841 collapseParagraphs(bv->buffer()->params);
847 if (!the_locking_inset) {
848 bool found = lt->updateInset(bv, inset);
852 setUpdateStatus(bv, NONE);
855 if (the_locking_inset != inset) {
856 bool found = the_locking_inset->updateInsetInInset(bv, inset);
860 setUpdateStatus(bv, CURSOR_PAR);
863 bool found = lt->updateInset(bv, inset);
867 setUpdateStatus(bv, CURSOR_PAR);
868 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
869 inset_x = cx(bv) - top_x + drawTextXOffset;
870 inset_y = cy(bv) + drawTextYOffset;
877 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
881 int tmp_x = x - drawTextXOffset;
882 int tmp_y = y + insetAscent - getLyXText(bv)->first;
883 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
886 if (the_locking_inset) {
887 if (the_locking_inset == inset) {
888 the_locking_inset->insetButtonPress(bv,
892 no_selection = false;
895 // otherwise unlock the_locking_inset and lock the new inset
896 the_locking_inset->insetUnlock(bv);
897 inset_x = cx(bv) - top_x + drawTextXOffset;
898 inset_y = cy(bv) + drawTextYOffset;
899 the_locking_inset = static_cast<UpdatableInset*>(inset);
900 inset->insetButtonPress(bv, x - inset_x,
901 y - inset_y, button);
902 inset->edit(bv, x - inset_x, y - inset_y, button);
903 if (the_locking_inset)
904 updateLocal(bv, CURSOR, false);
905 no_selection = false;
908 // otherwise only unlock the_locking_inset
909 the_locking_inset->insetUnlock(bv);
910 the_locking_inset = 0;
912 if (bv->theLockingInset()) {
913 if (isHighlyEditableInset(inset)) {
914 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
915 inset_x = cx(bv) - top_x + drawTextXOffset;
916 inset_y = cy(bv) + drawTextYOffset;
917 inset_pos = cpos(bv);
918 inset_par = cpar(bv);
919 inset_boundary = cboundary(bv);
920 the_locking_inset = uinset;
921 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
923 uinset->edit(bv, x - inset_x, y - inset_y, 0);
924 if (the_locking_inset)
925 updateLocal(bv, CURSOR, false);
926 no_selection = false;
930 if (!inset) { // && (button == 2)) {
931 bool paste_internally = false;
932 if ((button == 2) && getLyXText(bv)->selection.set()) {
933 localDispatch(bv, LFUN_COPY, "");
934 paste_internally = true;
942 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
944 // set the selection cursor!
945 lt->selection.cursor = lt->cursor;
946 lt->cursor.x_fix(lt->cursor.x());
948 if (lt->selection.set()) {
949 lt->clearSelection();
952 updateLocal(bv, FULL, false);
954 lt->clearSelection();
958 bv->owner()->setLayout(cpar(bv)->getLayout());
960 // Insert primary selection with middle mouse
961 // if there is a local selection in the current buffer,
964 if (paste_internally)
965 localDispatch(bv, LFUN_PASTE, "");
967 localDispatch(bv, LFUN_PASTESELECTION,
971 getLyXText(bv)->clearSelection();
974 no_selection = false;
978 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
980 if (the_locking_inset) {
981 return the_locking_inset->insetButtonRelease(bv,
982 x - inset_x, y - inset_y,
985 int tmp_x = x - drawTextXOffset;
986 int tmp_y = y + insetAscent - getLyXText(bv)->first;
987 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
990 if (isHighlyEditableInset(inset)) {
991 ret = inset->insetButtonRelease(bv, x - inset_x,
992 y - inset_y, button);
994 inset_x = cx(bv) - top_x + drawTextXOffset;
995 inset_y = cy(bv) + drawTextYOffset;
996 ret = inset->insetButtonRelease(bv, x - inset_x,
997 y - inset_y, button);
998 inset->edit(bv, x - inset_x,
999 y - inset_y, button);
1001 updateLocal(bv, CURSOR_PAR, false);
1007 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1011 if (the_locking_inset) {
1012 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1018 lt = getLyXText(bv);
1021 hideInsetCursor(bv);
1022 LyXCursor cur = lt->cursor;
1023 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1024 if (cur == lt->cursor) {
1029 lt->setSelection(bv);
1030 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1031 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1035 updateLocal(bv, SELECTION, false);
1037 showInsetCursor(bv);
1041 void InsetText::insetKeyPress(XKeyEvent * xke)
1043 if (the_locking_inset) {
1044 the_locking_inset->insetKeyPress(xke);
1050 UpdatableInset::RESULT
1051 InsetText::localDispatch(BufferView * bv,
1052 kb_action action, string const & arg)
1054 bool was_empty = par->size() == 0 && !par->next();
1055 no_selection = false;
1056 UpdatableInset::RESULT
1057 result= UpdatableInset::localDispatch(bv, action, arg);
1058 if (result != UNDISPATCHED) {
1062 result = DISPATCHED;
1063 if ((action < 0) && arg.empty())
1066 if (the_locking_inset) {
1067 result = the_locking_inset->localDispatch(bv, action, arg);
1068 if (result == DISPATCHED_NOUPDATE)
1070 else if (result == DISPATCHED) {
1071 updateLocal(bv, CURSOR_PAR, false);
1073 } else if (result >= FINISHED) {
1075 case FINISHED_RIGHT:
1076 moveRightIntern(bv, false, false);
1077 result = DISPATCHED;
1080 if ((result = moveUp(bv)) >= FINISHED) {
1081 updateLocal(bv, CURSOR, false);
1082 bv->unlockInset(this);
1086 if ((result = moveDown(bv)) >= FINISHED) {
1087 updateLocal(bv, CURSOR, false);
1088 bv->unlockInset(this);
1092 result = DISPATCHED;
1095 the_locking_inset = 0;
1096 #ifdef WITH_WARNINGS
1097 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1102 hideInsetCursor(bv);
1105 lt = getLyXText(bv);
1109 int updflag = false;
1112 case LFUN_SELFINSERT:
1113 if (bv->buffer()->isReadonly()) {
1114 // setErrorMessage(N_("Document is read only"));
1118 /* Automatically delete the currently selected
1119 * text and replace it with what is being
1120 * typed in now. Depends on lyxrc settings
1121 * "auto_region_delete", which defaults to
1124 setUndo(bv, Undo::INSERT,
1125 lt->cursor.par(), lt->cursor.par()->next());
1127 if (lyxrc.auto_region_delete) {
1128 if (lt->selection.set()) {
1129 lt->cutSelection(bv, false);
1132 lt->clearSelection();
1133 for (string::size_type i = 0; i < arg.length(); ++i) {
1134 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1137 lt->selection.cursor = lt->cursor;
1138 updwhat = CURSOR_PAR;
1140 result = DISPATCHED_NOUPDATE;
1142 // --- Cursor Movements -----------------------------------
1145 moveRight(bv, false, true);
1146 lt->setSelection(bv);
1147 updwhat = SELECTION;
1150 result = moveRight(bv);
1156 moveLeft(bv, false, true);
1157 lt->setSelection(bv);
1158 updwhat = SELECTION;
1162 result = moveLeft(bv);
1168 lt->setSelection(bv);
1169 updwhat = SELECTION;
1173 result = moveDown(bv);
1179 lt->setSelection(bv);
1180 updwhat = SELECTION;
1184 result = moveUp(bv);
1196 case LFUN_BACKSPACE: {
1197 setUndo(bv, Undo::DELETE,
1198 lt->cursor.par(), lt->cursor.par()->next());
1199 if (lt->selection.set())
1200 lt->cutSelection(bv);
1203 updwhat = CURSOR_PAR;
1209 setUndo(bv, Undo::DELETE,
1210 lt->cursor.par(), lt->cursor.par()->next());
1211 if (lt->selection.set()) {
1212 lt->cutSelection(bv);
1216 updwhat = CURSOR_PAR;
1222 setUndo(bv, Undo::DELETE,
1223 lt->cursor.par(), lt->cursor.par()->next());
1224 lt->cutSelection(bv);
1225 updwhat = CURSOR_PAR;
1232 lt->copySelection(bv);
1233 updwhat = CURSOR_PAR;
1235 case LFUN_PASTESELECTION:
1237 string const clip(bv->getClipboard());
1241 if (arg == "paragraph") {
1242 lt->insertStringAsParagraphs(bv, clip);
1244 lt->insertStringAsLines(bv, clip);
1246 updwhat = CURSOR_PAR;
1251 if (!autoBreakRows) {
1253 if (CutAndPaste::nrOfParagraphs() > 1) {
1254 Alert::alert(_("Impossible operation"),
1255 _("Cannot include more than one paragraph!"),
1260 setUndo(bv, Undo::INSERT,
1261 lt->cursor.par(), lt->cursor.par()->next());
1262 lt->pasteSelection(bv);
1263 updwhat = CURSOR_PAR;
1268 case LFUN_BREAKPARAGRAPH:
1269 if (!autoBreakRows) {
1270 result = DISPATCHED;
1273 lt->breakParagraph(bv, 0);
1277 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1278 if (!autoBreakRows) {
1279 result = DISPATCHED;
1282 lt->breakParagraph(bv, 1);
1287 case LFUN_BREAKLINE: {
1288 if (!autoBreakRows) {
1289 result = DISPATCHED;
1292 setUndo(bv, Undo::INSERT,
1293 lt->cursor.par(), lt->cursor.par()->next());
1294 lt->insertChar(bv, Paragraph::META_NEWLINE);
1295 updwhat = CURSOR_PAR;
1301 // do not set layouts on non breakable textinsets
1302 if (autoBreakRows) {
1303 layout_type cur_layout = cpar(bv)->layout;
1305 // Derive layout number from given argument (string)
1306 // and current buffer's textclass (number). */
1307 textclass_type tclass = bv->buffer()->params.textclass;
1308 std::pair <bool, layout_type> layout =
1309 textclasslist.NumberOfLayout(tclass, arg);
1311 // If the entry is obsolete, use the new one instead.
1313 string obs = textclasslist.Style(tclass,layout.second).
1316 layout = textclasslist.NumberOfLayout(tclass, obs);
1319 // see if we found the layout number:
1320 if (!layout.first) {
1321 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1322 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1326 if (cur_layout != layout.second) {
1327 cur_layout = layout.second;
1328 lt->setLayout(bv, layout.second);
1329 bv->owner()->setLayout(cpar(bv)->getLayout());
1330 updwhat = CURSOR_PAR;
1334 // reset the layout box
1335 bv->owner()->setLayout(cpar(bv)->getLayout());
1338 case LFUN_PARAGRAPH_SPACING:
1339 // This one is absolutely not working. When fiddling with this
1340 // it also seems to me that the paragraphs inside the insettext
1341 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1343 Paragraph * par = lt->cursor.par();
1344 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1345 float cur_value = 1.0;
1346 if (cur_spacing == Spacing::Other) {
1347 cur_value = par->params().spacing().getValue();
1350 istringstream istr(arg.c_str());
1353 Spacing::Space new_spacing = cur_spacing;
1354 float new_value = cur_value;
1356 lyxerr << "Missing argument to `paragraph-spacing'"
1358 } else if (tmp == "single") {
1359 new_spacing = Spacing::Single;
1360 } else if (tmp == "onehalf") {
1361 new_spacing = Spacing::Onehalf;
1362 } else if (tmp == "double") {
1363 new_spacing = Spacing::Double;
1364 } else if (tmp == "other") {
1365 new_spacing = Spacing::Other;
1368 lyxerr << "new_value = " << tmpval << endl;
1371 } else if (tmp == "default") {
1372 new_spacing = Spacing::Default;
1374 lyxerr << _("Unknown spacing argument: ")
1377 if (cur_spacing != new_spacing || cur_value != new_value) {
1378 par->params().spacing(Spacing(new_spacing, new_value));
1379 updwhat = CURSOR_PAR;
1386 if (!bv->Dispatch(action, arg))
1387 result = UNDISPATCHED;
1394 updateLocal(bv, updwhat, updflag);
1395 /// If the action has deleted all text in the inset, we need to change the
1396 // language to the language of the surronding text.
1397 if (!was_empty && par->size() == 0 && !par->next()) {
1398 LyXFont font(LyXFont::ALL_IGNORE);
1399 font.setLanguage(bv->getParentLanguage(this));
1400 setFont(bv, font, false);
1403 if (result < FINISHED) {
1404 showInsetCursor(bv);
1406 bv->unlockInset(this);
1411 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1414 buf->latexParagraphs(os, par, 0, texrow);
1415 return texrow.rows();
1419 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1421 Paragraph * p = par;
1422 unsigned int lines = 0;
1425 string const tmp = buf->asciiParagraph(p, linelen);
1426 lines += countChar(tmp, '\n');
1434 int InsetText::docbook(Buffer const * buf, ostream & os) const
1436 Paragraph * p = par;
1437 unsigned int lines = 0;
1439 vector<string> environment_stack(10);
1440 vector<string> environment_inner(10);
1442 int const command_depth = 0;
1445 Paragraph::depth_type depth = 0; // paragraph depth
1449 int desc_on = 0; // description mode
1451 LyXLayout const & style =
1452 textclasslist.Style(buf->params.textclass,
1455 // environment tag closing
1456 for (; depth > p->params().depth(); --depth) {
1457 if (environment_inner[depth] != "!-- --") {
1458 item_name = "listitem";
1459 buf->sgmlCloseTag(os, command_depth + depth,
1461 if (environment_inner[depth] == "varlistentry")
1462 buf->sgmlCloseTag(os, depth+command_depth,
1463 environment_inner[depth]);
1465 buf->sgmlCloseTag(os, depth + command_depth,
1466 environment_stack[depth]);
1467 environment_stack[depth].erase();
1468 environment_inner[depth].erase();
1471 if (depth == p->params().depth()
1472 && environment_stack[depth] != style.latexname()
1473 && !environment_stack[depth].empty()) {
1474 if (environment_inner[depth] != "!-- --") {
1475 item_name= "listitem";
1476 buf->sgmlCloseTag(os, command_depth+depth,
1478 if (environment_inner[depth] == "varlistentry")
1479 buf->sgmlCloseTag(os,
1480 depth + command_depth,
1481 environment_inner[depth]);
1484 buf->sgmlCloseTag(os, depth + command_depth,
1485 environment_stack[depth]);
1487 environment_stack[depth].erase();
1488 environment_inner[depth].erase();
1491 // Write opening SGML tags.
1492 switch (style.latextype) {
1493 case LATEX_PARAGRAPH:
1494 buf->sgmlOpenTag(os, depth + command_depth,
1499 buf->sgmlError(p, 0,
1500 _("Error : LatexType Command not allowed here.\n"));
1504 case LATEX_ENVIRONMENT:
1505 case LATEX_ITEM_ENVIRONMENT:
1506 if (depth < p->params().depth()) {
1507 depth = p->params().depth();
1508 environment_stack[depth].erase();
1511 if (environment_stack[depth] != style.latexname()) {
1512 if(environment_stack.size() == depth + 1) {
1513 environment_stack.push_back("!-- --");
1514 environment_inner.push_back("!-- --");
1516 environment_stack[depth] = style.latexname();
1517 environment_inner[depth] = "!-- --";
1518 buf->sgmlOpenTag(os, depth + command_depth,
1519 environment_stack[depth]);
1521 if (environment_inner[depth] != "!-- --") {
1522 item_name= "listitem";
1523 buf->sgmlCloseTag(os,
1524 command_depth + depth,
1526 if (environment_inner[depth] == "varlistentry")
1527 buf->sgmlCloseTag(os,
1528 depth + command_depth,
1529 environment_inner[depth]);
1533 if (style.latextype == LATEX_ENVIRONMENT) {
1534 if (!style.latexparam().empty()) {
1535 if(style.latexparam() == "CDATA")
1538 buf->sgmlOpenTag(os, depth + command_depth,
1539 style.latexparam());
1544 desc_on = (style.labeltype == LABEL_MANUAL);
1547 environment_inner[depth]= "varlistentry";
1549 environment_inner[depth]= "listitem";
1551 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1552 environment_inner[depth]);
1556 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1560 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1565 buf->sgmlOpenTag(os, depth + command_depth,
1570 buf->simpleDocBookOnePar(os, p, desc_on,
1571 depth + 1 + command_depth);
1575 // write closing SGML tags
1576 switch (style.latextype) {
1577 case LATEX_ENVIRONMENT:
1578 if (!style.latexparam().empty()) {
1579 if(style.latexparam() == "CDATA")
1582 buf->sgmlCloseTag(os, depth + command_depth,
1583 style.latexparam());
1586 case LATEX_ITEM_ENVIRONMENT:
1587 if (desc_on == 1) break;
1589 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1591 case LATEX_PARAGRAPH:
1592 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1595 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1601 for (int d = depth; d >= 0; --d) {
1602 if (!environment_stack[depth].empty()) {
1603 if (environment_inner[depth] != "!-- --") {
1604 item_name = "listitem";
1605 buf->sgmlCloseTag(os, command_depth + depth,
1607 if (environment_inner[depth] == "varlistentry")
1608 buf->sgmlCloseTag(os, depth + command_depth,
1609 environment_inner[depth]);
1612 buf->sgmlCloseTag(os, depth + command_depth,
1613 environment_stack[depth]);
1621 void InsetText::validate(LaTeXFeatures & features) const
1623 Paragraph * p = par;
1625 p->validate(features);
1631 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1633 if (textclasslist.Style(buf->params.textclass,
1634 p->getLayout()).labeltype != LABEL_MANUAL)
1637 return p->beginningOfMainBody();
1641 void InsetText::getCursorPos(BufferView * bv,
1642 int & x, int & y) const
1644 if (the_locking_inset) {
1645 the_locking_inset->getCursorPos(bv, x, y);
1653 unsigned int InsetText::insetInInsetY()
1655 if (!the_locking_inset)
1658 return (inset_y + the_locking_inset->insetInInsetY());
1662 void InsetText::toggleInsetCursor(BufferView * bv)
1664 if (the_locking_inset) {
1665 the_locking_inset->toggleInsetCursor(bv);
1669 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1671 int const asc = lyxfont::maxAscent(font);
1672 int const desc = lyxfont::maxDescent(font);
1674 if (isCursorVisible())
1675 bv->hideLockedInsetCursor();
1677 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1678 toggleCursorVisible();
1682 void InsetText::showInsetCursor(BufferView * bv, bool show)
1684 if (the_locking_inset) {
1685 the_locking_inset->showInsetCursor(bv, show);
1688 if (!isCursorVisible()) {
1689 LyXFont const font =
1690 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1692 int const asc = lyxfont::maxAscent(font);
1693 int const desc = lyxfont::maxDescent(font);
1695 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1697 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1698 setCursorVisible(true);
1703 void InsetText::hideInsetCursor(BufferView * bv)
1705 if (isCursorVisible()) {
1706 bv->hideLockedInsetCursor();
1707 setCursorVisible(false);
1709 if (the_locking_inset)
1710 the_locking_inset->hideInsetCursor(bv);
1714 void InsetText::fitInsetCursor(BufferView * bv) const
1716 if (the_locking_inset) {
1717 the_locking_inset->fitInsetCursor(bv);
1720 LyXFont const font =
1721 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1723 int const asc = lyxfont::maxAscent(font);
1724 int const desc = lyxfont::maxDescent(font);
1726 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1730 UpdatableInset::RESULT
1731 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1733 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1734 return moveLeftIntern(bv, false, activate_inset, selecting);
1736 return moveRightIntern(bv, false, activate_inset, selecting);
1740 UpdatableInset::RESULT
1741 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1743 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1744 return moveRightIntern(bv, true, activate_inset, selecting);
1746 return moveLeftIntern(bv, true, activate_inset, selecting);
1750 UpdatableInset::RESULT
1751 InsetText::moveRightIntern(BufferView * bv, bool behind,
1752 bool activate_inset, bool selecting)
1754 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1755 return FINISHED_RIGHT;
1756 if (activate_inset && checkAndActivateInset(bv, behind))
1758 getLyXText(bv)->cursorRight(bv);
1760 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1761 return DISPATCHED_NOUPDATE;
1765 UpdatableInset::RESULT
1766 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1767 bool activate_inset, bool selecting)
1769 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1771 getLyXText(bv)->cursorLeft(bv);
1773 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1774 if (activate_inset && checkAndActivateInset(bv, behind))
1776 return DISPATCHED_NOUPDATE;
1780 UpdatableInset::RESULT
1781 InsetText::moveUp(BufferView * bv)
1783 if (!crow(bv)->previous())
1785 getLyXText(bv)->cursorUp(bv);
1786 return DISPATCHED_NOUPDATE;
1790 UpdatableInset::RESULT
1791 InsetText::moveDown(BufferView * bv)
1793 if (!crow(bv)->next())
1794 return FINISHED_DOWN;
1795 getLyXText(bv)->cursorDown(bv);
1796 return DISPATCHED_NOUPDATE;
1800 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1802 if (the_locking_inset) {
1803 if (the_locking_inset->insetAllowed(inset))
1804 return the_locking_inset->insertInset(bv, inset);
1807 inset->setOwner(this);
1808 hideInsetCursor(bv);
1812 lt = getLyXText(bv);
1815 lt->insertInset(bv, inset);
1819 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1824 bool InsetText::insetAllowed(Inset::Code code) const
1826 // in_insetAllowed is a really gross hack,
1827 // to allow us to call the owner's insetAllowed
1828 // without stack overflow, which can happen
1829 // when the owner uses InsetCollapsable::insetAllowed()
1831 if (in_insetAllowed)
1833 in_insetAllowed = true;
1834 if (the_locking_inset)
1835 ret = the_locking_inset->insetAllowed(code);
1837 ret = owner()->insetAllowed(code);
1838 in_insetAllowed = false;
1843 UpdatableInset * InsetText::getLockingInset() const
1845 return the_locking_inset ? the_locking_inset->getLockingInset() :
1846 const_cast<InsetText *>(this);
1850 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1854 if (the_locking_inset)
1855 return the_locking_inset->getFirstLockingInsetOfType(c);
1860 bool InsetText::showInsetDialog(BufferView * bv) const
1862 if (the_locking_inset)
1863 return the_locking_inset->showInsetDialog(bv);
1868 std::vector<string> const InsetText::getLabelList() const
1870 std::vector<string> label_list;
1872 Paragraph * tpar = par;
1874 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1875 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1876 for (; beg != end; ++beg) {
1877 std::vector<string> const l = (*beg)->getLabelList();
1878 label_list.insert(label_list.end(), l.begin(), l.end());
1880 tpar = tpar->next();
1886 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1889 if (the_locking_inset) {
1890 the_locking_inset->setFont(bv, font, toggleall, selectall);
1893 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1894 getLyXText(bv)->setFont(bv, font, toggleall);
1899 lt = getLyXText(bv);
1902 if (lt->selection.set()) {
1903 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1907 lt->toggleFree(bv, font, toggleall);
1909 lt->clearSelection();
1911 bool flag = (selectall || lt->selection.set());
1915 updateLocal(bv, FULL, true);
1917 updateLocal(bv, CURSOR_PAR, true);
1921 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1923 if (cpar(bv)->isInset(cpos(bv))) {
1927 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1928 if (!isHighlyEditableInset(inset))
1930 LyXFont const font =
1931 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1933 x = inset->width(bv, font);
1934 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1937 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1939 //inset_x = cx(bv) - top_x + drawTextXOffset;
1940 //inset_y = cy(bv) + drawTextYOffset;
1941 inset->edit(bv, x, y, 0);
1942 if (!the_locking_inset)
1944 updateLocal(bv, CURSOR, false);
1951 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1954 x -= drawTextXOffset;
1956 int dummyy = y + insetAscent;
1957 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
1964 inset_x = cx(bv) - top_x + drawTextXOffset;
1965 inset_y = cy(bv) + drawTextYOffset;
1966 inset->edit(bv, x - inset_x, y - inset_y, button);
1967 if (!the_locking_inset)
1969 updateLocal(bv, CURSOR, false);
1976 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1979 int w = UpdatableInset::getMaxWidth(bv, inset);
1984 w = w - top_x + owner()->x();
1987 w -= (2 * TEXT_TO_INSET_OFFSET);
1990 return UpdatableInset::getMaxWidth(bv, inset);
1995 void InsetText::setParagraphData(Paragraph * p, bool same_id)
1997 // we have to unlock any locked inset otherwise we're in troubles
1998 the_locking_inset = 0;
2000 Paragraph * tmp = par->next();
2005 par = new Paragraph(*p, same_id);
2006 par->setInsetOwner(this);
2007 Paragraph * np = par;
2010 np->next(new Paragraph(*p, same_id));
2011 np->next()->previous(np);
2013 np->setInsetOwner(this);
2020 void InsetText::setText(string const & data)
2023 LyXFont font(LyXFont::ALL_SANE);
2024 for (unsigned int i=0; i < data.length(); ++i)
2025 par->insertChar(i, data[i], font);
2029 void InsetText::setAutoBreakRows(bool flag)
2031 if (flag != autoBreakRows) {
2032 autoBreakRows = flag;
2040 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2042 if (how != drawFrame_) {
2045 updateLocal(bv, DRAW_FRAME, false);
2050 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2052 if (frame_color != col) {
2055 updateLocal(bv, DRAW_FRAME, false);
2060 int InsetText::cx(BufferView * bv) const
2062 // we do nothing dangerous so we use a local cache
2063 LyXText * llt = getLyXText(bv);
2064 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2065 if (the_locking_inset) {
2066 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2068 if (font.isVisibleRightToLeft())
2069 x -= the_locking_inset->width(bv, font);
2075 int InsetText::cy(BufferView * bv) const
2078 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2082 pos_type InsetText::cpos(BufferView * bv) const
2084 return getLyXText(bv)->cursor.pos();
2088 Paragraph * InsetText::cpar(BufferView * bv) const
2090 return getLyXText(bv)->cursor.par();
2094 bool InsetText::cboundary(BufferView * bv) const
2096 return getLyXText(bv)->cursor.boundary();
2100 Row * InsetText::crow(BufferView * bv) const
2102 return getLyXText(bv)->cursor.row();
2106 LyXText * InsetText::getLyXText(BufferView const * lbv,
2107 bool const recursive) const
2109 if (!recursive && (cached_bview == lbv)) {
2110 LyXText * lt = cached_text.get();
2111 lyx::Assert(lt && lt->firstRow()->par() == par);
2112 return cached_text.get();
2115 // Super UGLY! (Lgb)
2116 BufferView * bv = const_cast<BufferView *>(lbv);
2119 Cache::iterator it = cache.find(bv);
2121 if (it != cache.end()) {
2125 resizeLyXText(do_resize);
2126 if (lt || !it->second.remove) {
2127 lyx::Assert(it->second.text.get());
2128 cached_text = it->second.text;
2129 if (recursive && the_locking_inset) {
2130 return the_locking_inset->getLyXText(bv, true);
2132 return cached_text.get();
2133 } else if (it->second.remove) {
2135 saveLyXTextState(it->second.text.get());
2142 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2143 cached_text->init(bv);
2144 restoreLyXTextState(bv, cached_text.get());
2146 cache.insert(make_pair(bv, cached_text));
2148 if (the_locking_inset && recursive) {
2149 return the_locking_inset->getLyXText(bv);
2151 return cached_text.get();
2155 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2159 Cache::iterator it = cache.find(bv);
2161 if (it == cache.end()) {
2165 lyx::Assert(it->second.text.get());
2167 it->second.remove = true;
2169 /// then remove all LyXText in text-insets
2170 Paragraph * p = par;
2171 for (; p; p = p->next()) {
2172 p->deleteInsetsLyXText(bv);
2178 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2181 // we cannot resize this because we are in use!
2182 // so do this on the next possible getLyXText()
2187 // lyxerr << "InsetText::resizeLyXText\n";
2188 if (!par->next() && !par->size()) // no data, resize not neccessary!
2190 // one endless line, resize normally not necessary
2191 if (!force && getMaxWidth(bv, this) < 0)
2194 Cache::iterator it = cache.find(bv);
2195 if (it == cache.end()) {
2198 lyx::Assert(it->second.text.get());
2200 LyXText * t = it->second.text.get();
2201 saveLyXTextState(t);
2202 for (Paragraph * p = par; p; p = p->next()) {
2203 p->resizeInsetsLyXText(bv);
2206 restoreLyXTextState(bv, t);
2207 if (the_locking_inset) {
2208 inset_x = cx(bv) - top_x + drawTextXOffset;
2209 inset_y = cy(bv) + drawTextYOffset;
2213 t->first = bv->screen()->topCursorVisible(t);
2216 updateLocal(bv, FULL, false);
2217 // this will scroll the screen such that the cursor becomes visible
2218 bv->updateScrollbar();
2220 need_update |= FULL;
2225 void InsetText::reinitLyXText() const
2228 // we cannot resize this because we are in use!
2229 // so do this on the next possible getLyXText()
2235 // lyxerr << "InsetText::reinitLyXText\n";
2236 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2237 lyx::Assert(it->second.text.get());
2239 LyXText * t = it->second.text.get();
2240 BufferView * bv = it->first;
2242 saveLyXTextState(t);
2243 for (Paragraph * p = par; p; p = p->next()) {
2244 p->resizeInsetsLyXText(bv);
2247 restoreLyXTextState(bv, t);
2248 if (the_locking_inset) {
2249 inset_x = cx(bv) - top_x + drawTextXOffset;
2250 inset_y = cy(bv) + drawTextYOffset;
2253 t->first = bv->screen()->topCursorVisible(t);
2256 updateLocal(bv, FULL, false);
2257 // this will scroll the screen such that the cursor becomes visible
2258 bv->updateScrollbar();
2266 void InsetText::removeNewlines()
2268 bool changed = false;
2270 for (Paragraph * p = par; p; p = p->next()) {
2271 for (int i = 0; i < p->size(); ++i) {
2272 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2283 bool InsetText::nodraw() const
2285 if (the_locking_inset)
2286 return the_locking_inset->nodraw();
2287 return UpdatableInset::nodraw();
2291 int InsetText::scroll(bool recursive) const
2293 int sx = UpdatableInset::scroll(false);
2295 if (recursive && the_locking_inset)
2296 sx += the_locking_inset->scroll(recursive);
2302 bool InsetText::doClearArea() const
2304 return !locked || (need_update & (FULL|INIT));
2308 void InsetText::selectAll(BufferView * bv)
2310 getLyXText(bv)->cursorTop(bv);
2311 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2312 getLyXText(bv)->cursorBottom(bv);
2313 getLyXText(bv)->setSelection(bv);
2317 void InsetText::clearSelection(BufferView * bv)
2319 getLyXText(bv)->clearSelection();
2323 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2326 Painter & pain = bv->painter();
2328 int h = ascent(bv, dummy) + descent(bv, dummy);
2329 int ty = baseline - insetAscent;
2335 if ((ty + h) > pain.paperHeight())
2336 h = pain.paperHeight();
2337 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2338 w = pain.paperWidth();
2339 // w -= TEXT_TO_INSET_OFFSET;
2340 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2343 frame_is_visible = false;
2347 Paragraph * InsetText::getParFromID(int id) const
2350 Paragraph * result = par;
2351 Paragraph * ires = 0;
2352 while (result && result->id() != id) {
2353 if ((ires = result->getParFromID(id)))
2355 result = result->next();
2359 Paragraph * tmp = par;
2361 if (tmp->id() == id) {
2364 Paragraph * tmp2 = tmp->getParFromID(id);
2375 Paragraph * InsetText::firstParagraph() const
2378 if (the_locking_inset)
2379 if ((result = the_locking_inset->firstParagraph()))
2385 Paragraph * InsetText::getFirstParagraph(int i) const
2387 return (i == 0) ? par : 0;
2391 LyXCursor const & InsetText::cursor(BufferView * bv) const
2393 if (the_locking_inset)
2394 return the_locking_inset->cursor(bv);
2395 return getLyXText(bv)->cursor;
2399 Paragraph * InsetText::paragraph() const
2405 void InsetText::paragraph(Paragraph * p)
2407 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2408 // caller of this function has to take care of it. This IS important
2409 // as we could have to insert a paragraph before this one and just
2410 // link the actual to a new ones next and set it with this function
2413 // set ourself as owner for all the paragraphs inserted!
2414 Paragraph * np = par;
2416 np->setInsetOwner(this);
2420 // redraw myself when asked for
2425 Inset * InsetText::getInsetFromID(int id_arg) const
2428 return const_cast<InsetText *>(this);
2430 Paragraph * lp = par;
2433 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2434 en = lp->inset_iterator_end();
2437 if ((*it)->id() == id_arg)
2439 Inset * in = (*it)->getInsetFromID(id_arg);
2449 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2455 lt = getLyXText(bv);
2458 if (the_locking_inset) {
2459 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2466 #warning Dekel please have a look on this one RTL? (Jug)
2468 // we have to go on checking so move cusor to the right
2469 lt->cursor.pos(lt->cursor.pos() + 1);
2471 str = lt->selectNextWordToSpellcheck(bv, value);
2473 bv->unlockInset(const_cast<InsetText *>(this));
2482 void InsetText::selectSelectedWord(BufferView * bv)
2484 if (the_locking_inset) {
2485 the_locking_inset->selectSelectedWord(bv);
2488 getLyXText(bv)->selectSelectedWord(bv);
2489 updateLocal(bv, SELECTION, false);
2493 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2495 if (the_locking_inset) {
2496 the_locking_inset->toggleSelection(bv, kill_selection);
2500 lt = getLyXText(bv);
2504 int x = top_x + TEXT_TO_INSET_OFFSET;
2506 Row * row = lt->firstRow();
2507 int y_offset = top_baseline - row->ascent_of_text();
2509 while ((row != 0) && ((y+row->height()) <= 0)) {
2516 if (need_update & SELECTION)
2518 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2524 bool InsetText::searchForward(BufferView * bv, string const & str,
2527 if (the_locking_inset) {
2528 if (the_locking_inset->searchForward(bv, str, cs, mw))
2532 lt = getLyXText(bv);
2535 Paragraph * lpar = lt->cursor.par();
2536 pos_type pos = lt->cursor.pos();
2537 if (pos < lpar->size() - 1)
2541 lpar = lpar->next();
2546 // we have to unlock ourself in this function by default!
2547 bv->unlockInset(const_cast<InsetText *>(this));
2550 lt->setCursor(bv, lpar, pos);
2554 if (LyXFind(bv, str, true, true, cs , mw)) {
2557 // we have to unlock ourself in this function by default!
2558 bv->unlockInset(const_cast<InsetText *>(this));
2562 bool InsetText::searchBackward(BufferView * bv, string const & str,
2565 if (the_locking_inset)
2566 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2568 if (LyXFind(bv, str, false, true, cs, mw)) {
2571 // we have to unlock ourself in this function by default!
2572 bv->unlockInset(const_cast<InsetText *>(this));
2577 bool InsetText::checkInsertChar(LyXFont & font)
2580 return owner()->checkInsertChar(font);
2585 void InsetText::collapseParagraphs(BufferParams const & bparams) const
2587 while(par->next()) {
2588 par->pasteParagraph(bparams);