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);
849 if (!the_locking_inset) {
850 bool found = lt->updateInset(bv, inset);
854 setUpdateStatus(bv, NONE);
857 if (the_locking_inset != inset) {
858 bool found = the_locking_inset->updateInsetInInset(bv, inset);
862 setUpdateStatus(bv, CURSOR_PAR);
865 bool found = lt->updateInset(bv, inset);
869 setUpdateStatus(bv, CURSOR_PAR);
870 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
871 inset_x = cx(bv) - top_x + drawTextXOffset;
872 inset_y = cy(bv) + drawTextYOffset;
879 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
883 int tmp_x = x - drawTextXOffset;
884 int tmp_y = y + insetAscent - getLyXText(bv)->first;
885 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
888 if (the_locking_inset) {
889 if (the_locking_inset == inset) {
890 the_locking_inset->insetButtonPress(bv,
894 no_selection = false;
897 // otherwise unlock the_locking_inset and lock the new inset
898 the_locking_inset->insetUnlock(bv);
899 inset_x = cx(bv) - top_x + drawTextXOffset;
900 inset_y = cy(bv) + drawTextYOffset;
901 the_locking_inset = static_cast<UpdatableInset*>(inset);
902 inset->insetButtonPress(bv, x - inset_x,
903 y - inset_y, button);
904 inset->edit(bv, x - inset_x, y - inset_y, button);
905 if (the_locking_inset)
906 updateLocal(bv, CURSOR, false);
907 no_selection = false;
910 // otherwise only unlock the_locking_inset
911 the_locking_inset->insetUnlock(bv);
912 the_locking_inset = 0;
914 if (bv->theLockingInset()) {
915 if (isHighlyEditableInset(inset)) {
916 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
917 inset_x = cx(bv) - top_x + drawTextXOffset;
918 inset_y = cy(bv) + drawTextYOffset;
919 inset_pos = cpos(bv);
920 inset_par = cpar(bv);
921 inset_boundary = cboundary(bv);
922 the_locking_inset = uinset;
923 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
925 uinset->edit(bv, x - inset_x, y - inset_y, 0);
926 if (the_locking_inset)
927 updateLocal(bv, CURSOR, false);
928 no_selection = false;
932 if (!inset) { // && (button == 2)) {
933 bool paste_internally = false;
934 if ((button == 2) && getLyXText(bv)->selection.set()) {
935 localDispatch(bv, LFUN_COPY, "");
936 paste_internally = true;
944 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
946 // set the selection cursor!
947 lt->selection.cursor = lt->cursor;
948 lt->cursor.x_fix(lt->cursor.x());
950 if (lt->selection.set()) {
951 lt->clearSelection();
954 updateLocal(bv, FULL, false);
956 lt->clearSelection();
960 bv->owner()->setLayout(cpar(bv)->getLayout());
962 // Insert primary selection with middle mouse
963 // if there is a local selection in the current buffer,
966 if (paste_internally)
967 localDispatch(bv, LFUN_PASTE, "");
969 localDispatch(bv, LFUN_PASTESELECTION,
973 getLyXText(bv)->clearSelection();
976 no_selection = false;
980 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
982 if (the_locking_inset) {
983 return the_locking_inset->insetButtonRelease(bv,
984 x - inset_x, y - inset_y,
987 int tmp_x = x - drawTextXOffset;
988 int tmp_y = y + insetAscent - getLyXText(bv)->first;
989 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
992 if (isHighlyEditableInset(inset)) {
993 ret = inset->insetButtonRelease(bv, x - inset_x,
994 y - inset_y, button);
996 inset_x = cx(bv) - top_x + drawTextXOffset;
997 inset_y = cy(bv) + drawTextYOffset;
998 ret = inset->insetButtonRelease(bv, x - inset_x,
999 y - inset_y, button);
1000 inset->edit(bv, x - inset_x,
1001 y - inset_y, button);
1003 updateLocal(bv, CURSOR_PAR, false);
1009 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1013 if (the_locking_inset) {
1014 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1020 lt = getLyXText(bv);
1023 hideInsetCursor(bv);
1024 LyXCursor cur = lt->cursor;
1025 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1026 if (cur == lt->cursor) {
1031 lt->setSelection(bv);
1032 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1033 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1037 updateLocal(bv, SELECTION, false);
1039 showInsetCursor(bv);
1043 void InsetText::insetKeyPress(XKeyEvent * xke)
1045 if (the_locking_inset) {
1046 the_locking_inset->insetKeyPress(xke);
1052 UpdatableInset::RESULT
1053 InsetText::localDispatch(BufferView * bv,
1054 kb_action action, string const & arg)
1056 bool was_empty = par->size() == 0 && !par->next();
1057 no_selection = false;
1058 UpdatableInset::RESULT
1059 result= UpdatableInset::localDispatch(bv, action, arg);
1060 if (result != UNDISPATCHED) {
1064 result = DISPATCHED;
1065 if ((action < 0) && arg.empty())
1068 if (the_locking_inset) {
1069 result = the_locking_inset->localDispatch(bv, action, arg);
1070 if (result == DISPATCHED_NOUPDATE)
1072 else if (result == DISPATCHED) {
1073 updateLocal(bv, CURSOR_PAR, false);
1075 } else if (result >= FINISHED) {
1077 case FINISHED_RIGHT:
1078 moveRightIntern(bv, false, false);
1079 result = DISPATCHED;
1082 if ((result = moveUp(bv)) >= FINISHED) {
1083 updateLocal(bv, CURSOR, false);
1084 bv->unlockInset(this);
1088 if ((result = moveDown(bv)) >= FINISHED) {
1089 updateLocal(bv, CURSOR, false);
1090 bv->unlockInset(this);
1094 result = DISPATCHED;
1097 the_locking_inset = 0;
1098 #ifdef WITH_WARNINGS
1099 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1104 hideInsetCursor(bv);
1107 lt = getLyXText(bv);
1111 int updflag = false;
1114 case LFUN_SELFINSERT:
1115 if (bv->buffer()->isReadonly()) {
1116 // setErrorMessage(N_("Document is read only"));
1120 /* Automatically delete the currently selected
1121 * text and replace it with what is being
1122 * typed in now. Depends on lyxrc settings
1123 * "auto_region_delete", which defaults to
1126 setUndo(bv, Undo::INSERT,
1127 lt->cursor.par(), lt->cursor.par()->next());
1129 if (lyxrc.auto_region_delete) {
1130 if (lt->selection.set()) {
1131 lt->cutSelection(bv, false);
1134 lt->clearSelection();
1135 for (string::size_type i = 0; i < arg.length(); ++i) {
1136 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1139 lt->selection.cursor = lt->cursor;
1140 updwhat = CURSOR_PAR;
1142 result = DISPATCHED_NOUPDATE;
1144 // --- Cursor Movements -----------------------------------
1147 moveRight(bv, false, true);
1148 lt->setSelection(bv);
1149 updwhat = SELECTION;
1152 result = moveRight(bv);
1158 moveLeft(bv, false, true);
1159 lt->setSelection(bv);
1160 updwhat = SELECTION;
1164 result = moveLeft(bv);
1170 lt->setSelection(bv);
1171 updwhat = SELECTION;
1175 result = moveDown(bv);
1181 lt->setSelection(bv);
1182 updwhat = SELECTION;
1186 result = moveUp(bv);
1198 case LFUN_BACKSPACE: {
1199 setUndo(bv, Undo::DELETE,
1200 lt->cursor.par(), lt->cursor.par()->next());
1201 if (lt->selection.set())
1202 lt->cutSelection(bv);
1205 updwhat = CURSOR_PAR;
1211 setUndo(bv, Undo::DELETE,
1212 lt->cursor.par(), lt->cursor.par()->next());
1213 if (lt->selection.set()) {
1214 lt->cutSelection(bv);
1218 updwhat = CURSOR_PAR;
1224 setUndo(bv, Undo::DELETE,
1225 lt->cursor.par(), lt->cursor.par()->next());
1226 lt->cutSelection(bv);
1227 updwhat = CURSOR_PAR;
1234 lt->copySelection(bv);
1235 updwhat = CURSOR_PAR;
1237 case LFUN_PASTESELECTION:
1239 string const clip(bv->getClipboard());
1243 if (arg == "paragraph") {
1244 lt->insertStringAsParagraphs(bv, clip);
1246 lt->insertStringAsLines(bv, clip);
1248 updwhat = CURSOR_PAR;
1253 if (!autoBreakRows) {
1255 if (CutAndPaste::nrOfParagraphs() > 1) {
1256 Alert::alert(_("Impossible operation"),
1257 _("Cannot include more than one paragraph!"),
1262 setUndo(bv, Undo::INSERT,
1263 lt->cursor.par(), lt->cursor.par()->next());
1264 lt->pasteSelection(bv);
1265 updwhat = CURSOR_PAR;
1270 case LFUN_BREAKPARAGRAPH:
1271 if (!autoBreakRows) {
1272 result = DISPATCHED;
1275 lt->breakParagraph(bv, 0);
1279 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1280 if (!autoBreakRows) {
1281 result = DISPATCHED;
1284 lt->breakParagraph(bv, 1);
1289 case LFUN_BREAKLINE: {
1290 if (!autoBreakRows) {
1291 result = DISPATCHED;
1294 setUndo(bv, Undo::INSERT,
1295 lt->cursor.par(), lt->cursor.par()->next());
1296 lt->insertChar(bv, Paragraph::META_NEWLINE);
1297 updwhat = CURSOR_PAR;
1303 // do not set layouts on non breakable textinsets
1304 if (autoBreakRows) {
1305 layout_type cur_layout = cpar(bv)->layout;
1307 // Derive layout number from given argument (string)
1308 // and current buffer's textclass (number). */
1309 textclass_type tclass = bv->buffer()->params.textclass;
1310 std::pair <bool, layout_type> layout =
1311 textclasslist.NumberOfLayout(tclass, arg);
1313 // If the entry is obsolete, use the new one instead.
1315 string obs = textclasslist.Style(tclass,layout.second).
1318 layout = textclasslist.NumberOfLayout(tclass, obs);
1321 // see if we found the layout number:
1322 if (!layout.first) {
1323 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1324 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1328 if (cur_layout != layout.second) {
1329 cur_layout = layout.second;
1330 lt->setLayout(bv, layout.second);
1331 bv->owner()->setLayout(cpar(bv)->getLayout());
1332 updwhat = CURSOR_PAR;
1336 // reset the layout box
1337 bv->owner()->setLayout(cpar(bv)->getLayout());
1340 case LFUN_PARAGRAPH_SPACING:
1341 // This one is absolutely not working. When fiddling with this
1342 // it also seems to me that the paragraphs inside the insettext
1343 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1345 Paragraph * par = lt->cursor.par();
1346 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1347 float cur_value = 1.0;
1348 if (cur_spacing == Spacing::Other) {
1349 cur_value = par->params().spacing().getValue();
1352 istringstream istr(arg.c_str());
1355 Spacing::Space new_spacing = cur_spacing;
1356 float new_value = cur_value;
1358 lyxerr << "Missing argument to `paragraph-spacing'"
1360 } else if (tmp == "single") {
1361 new_spacing = Spacing::Single;
1362 } else if (tmp == "onehalf") {
1363 new_spacing = Spacing::Onehalf;
1364 } else if (tmp == "double") {
1365 new_spacing = Spacing::Double;
1366 } else if (tmp == "other") {
1367 new_spacing = Spacing::Other;
1370 lyxerr << "new_value = " << tmpval << endl;
1373 } else if (tmp == "default") {
1374 new_spacing = Spacing::Default;
1376 lyxerr << _("Unknown spacing argument: ")
1379 if (cur_spacing != new_spacing || cur_value != new_value) {
1380 par->params().spacing(Spacing(new_spacing, new_value));
1381 updwhat = CURSOR_PAR;
1388 if (!bv->Dispatch(action, arg))
1389 result = UNDISPATCHED;
1396 updateLocal(bv, updwhat, updflag);
1397 /// If the action has deleted all text in the inset, we need to change the
1398 // language to the language of the surronding text.
1399 if (!was_empty && par->size() == 0 && !par->next()) {
1400 LyXFont font(LyXFont::ALL_IGNORE);
1401 font.setLanguage(bv->getParentLanguage(this));
1402 setFont(bv, font, false);
1405 if (result < FINISHED) {
1406 showInsetCursor(bv);
1408 bv->unlockInset(this);
1413 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1416 buf->latexParagraphs(os, par, 0, texrow);
1417 return texrow.rows();
1421 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1423 Paragraph * p = par;
1424 unsigned int lines = 0;
1427 string const tmp = buf->asciiParagraph(p, linelen);
1428 lines += countChar(tmp, '\n');
1436 int InsetText::docbook(Buffer const * buf, ostream & os) const
1438 Paragraph * p = par;
1439 unsigned int lines = 0;
1441 vector<string> environment_stack(10);
1442 vector<string> environment_inner(10);
1444 int const command_depth = 0;
1447 Paragraph::depth_type depth = 0; // paragraph depth
1451 int desc_on = 0; // description mode
1453 LyXLayout const & style =
1454 textclasslist.Style(buf->params.textclass,
1457 // environment tag closing
1458 for (; depth > p->params().depth(); --depth) {
1459 if (environment_inner[depth] != "!-- --") {
1460 item_name = "listitem";
1461 buf->sgmlCloseTag(os, command_depth + depth,
1463 if (environment_inner[depth] == "varlistentry")
1464 buf->sgmlCloseTag(os, depth+command_depth,
1465 environment_inner[depth]);
1467 buf->sgmlCloseTag(os, depth + command_depth,
1468 environment_stack[depth]);
1469 environment_stack[depth].erase();
1470 environment_inner[depth].erase();
1473 if (depth == p->params().depth()
1474 && environment_stack[depth] != style.latexname()
1475 && !environment_stack[depth].empty()) {
1476 if (environment_inner[depth] != "!-- --") {
1477 item_name= "listitem";
1478 buf->sgmlCloseTag(os, command_depth+depth,
1480 if (environment_inner[depth] == "varlistentry")
1481 buf->sgmlCloseTag(os,
1482 depth + command_depth,
1483 environment_inner[depth]);
1486 buf->sgmlCloseTag(os, depth + command_depth,
1487 environment_stack[depth]);
1489 environment_stack[depth].erase();
1490 environment_inner[depth].erase();
1493 // Write opening SGML tags.
1494 switch (style.latextype) {
1495 case LATEX_PARAGRAPH:
1496 buf->sgmlOpenTag(os, depth + command_depth,
1501 buf->sgmlError(p, 0,
1502 _("Error : LatexType Command not allowed here.\n"));
1506 case LATEX_ENVIRONMENT:
1507 case LATEX_ITEM_ENVIRONMENT:
1508 if (depth < p->params().depth()) {
1509 depth = p->params().depth();
1510 environment_stack[depth].erase();
1513 if (environment_stack[depth] != style.latexname()) {
1514 if(environment_stack.size() == depth + 1) {
1515 environment_stack.push_back("!-- --");
1516 environment_inner.push_back("!-- --");
1518 environment_stack[depth] = style.latexname();
1519 environment_inner[depth] = "!-- --";
1520 buf->sgmlOpenTag(os, depth + command_depth,
1521 environment_stack[depth]);
1523 if (environment_inner[depth] != "!-- --") {
1524 item_name= "listitem";
1525 buf->sgmlCloseTag(os,
1526 command_depth + depth,
1528 if (environment_inner[depth] == "varlistentry")
1529 buf->sgmlCloseTag(os,
1530 depth + command_depth,
1531 environment_inner[depth]);
1535 if (style.latextype == LATEX_ENVIRONMENT) {
1536 if (!style.latexparam().empty()) {
1537 if(style.latexparam() == "CDATA")
1540 buf->sgmlOpenTag(os, depth + command_depth,
1541 style.latexparam());
1546 desc_on = (style.labeltype == LABEL_MANUAL);
1549 environment_inner[depth]= "varlistentry";
1551 environment_inner[depth]= "listitem";
1553 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1554 environment_inner[depth]);
1558 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1562 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1567 buf->sgmlOpenTag(os, depth + command_depth,
1572 buf->simpleDocBookOnePar(os, p, desc_on,
1573 depth + 1 + command_depth);
1577 // write closing SGML tags
1578 switch (style.latextype) {
1579 case LATEX_ENVIRONMENT:
1580 if (!style.latexparam().empty()) {
1581 if(style.latexparam() == "CDATA")
1584 buf->sgmlCloseTag(os, depth + command_depth,
1585 style.latexparam());
1588 case LATEX_ITEM_ENVIRONMENT:
1589 if (desc_on == 1) break;
1591 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1593 case LATEX_PARAGRAPH:
1594 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1597 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1603 for (int d = depth; d >= 0; --d) {
1604 if (!environment_stack[depth].empty()) {
1605 if (environment_inner[depth] != "!-- --") {
1606 item_name = "listitem";
1607 buf->sgmlCloseTag(os, command_depth + depth,
1609 if (environment_inner[depth] == "varlistentry")
1610 buf->sgmlCloseTag(os, depth + command_depth,
1611 environment_inner[depth]);
1614 buf->sgmlCloseTag(os, depth + command_depth,
1615 environment_stack[depth]);
1623 void InsetText::validate(LaTeXFeatures & features) const
1625 Paragraph * p = par;
1627 p->validate(features);
1633 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1635 if (textclasslist.Style(buf->params.textclass,
1636 p->getLayout()).labeltype != LABEL_MANUAL)
1639 return p->beginningOfMainBody();
1643 void InsetText::getCursorPos(BufferView * bv,
1644 int & x, int & y) const
1646 if (the_locking_inset) {
1647 the_locking_inset->getCursorPos(bv, x, y);
1655 unsigned int InsetText::insetInInsetY()
1657 if (!the_locking_inset)
1660 return (inset_y + the_locking_inset->insetInInsetY());
1664 void InsetText::toggleInsetCursor(BufferView * bv)
1666 if (the_locking_inset) {
1667 the_locking_inset->toggleInsetCursor(bv);
1671 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1673 int const asc = lyxfont::maxAscent(font);
1674 int const desc = lyxfont::maxDescent(font);
1676 if (isCursorVisible())
1677 bv->hideLockedInsetCursor();
1679 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1680 toggleCursorVisible();
1684 void InsetText::showInsetCursor(BufferView * bv, bool show)
1686 if (the_locking_inset) {
1687 the_locking_inset->showInsetCursor(bv, show);
1690 if (!isCursorVisible()) {
1691 LyXFont const font =
1692 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1694 int const asc = lyxfont::maxAscent(font);
1695 int const desc = lyxfont::maxDescent(font);
1697 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1699 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1700 setCursorVisible(true);
1705 void InsetText::hideInsetCursor(BufferView * bv)
1707 if (isCursorVisible()) {
1708 bv->hideLockedInsetCursor();
1709 setCursorVisible(false);
1711 if (the_locking_inset)
1712 the_locking_inset->hideInsetCursor(bv);
1716 void InsetText::fitInsetCursor(BufferView * bv) const
1718 if (the_locking_inset) {
1719 the_locking_inset->fitInsetCursor(bv);
1722 LyXFont const font =
1723 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1725 int const asc = lyxfont::maxAscent(font);
1726 int const desc = lyxfont::maxDescent(font);
1728 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1732 UpdatableInset::RESULT
1733 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1735 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1736 return moveLeftIntern(bv, false, activate_inset, selecting);
1738 return moveRightIntern(bv, false, activate_inset, selecting);
1742 UpdatableInset::RESULT
1743 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1745 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1746 return moveRightIntern(bv, true, activate_inset, selecting);
1748 return moveLeftIntern(bv, true, activate_inset, selecting);
1752 UpdatableInset::RESULT
1753 InsetText::moveRightIntern(BufferView * bv, bool behind,
1754 bool activate_inset, bool selecting)
1756 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1757 return FINISHED_RIGHT;
1758 if (activate_inset && checkAndActivateInset(bv, behind))
1760 getLyXText(bv)->cursorRight(bv);
1762 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1763 return DISPATCHED_NOUPDATE;
1767 UpdatableInset::RESULT
1768 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1769 bool activate_inset, bool selecting)
1771 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1773 getLyXText(bv)->cursorLeft(bv);
1775 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1776 if (activate_inset && checkAndActivateInset(bv, behind))
1778 return DISPATCHED_NOUPDATE;
1782 UpdatableInset::RESULT
1783 InsetText::moveUp(BufferView * bv)
1785 if (!crow(bv)->previous())
1787 getLyXText(bv)->cursorUp(bv);
1788 return DISPATCHED_NOUPDATE;
1792 UpdatableInset::RESULT
1793 InsetText::moveDown(BufferView * bv)
1795 if (!crow(bv)->next())
1796 return FINISHED_DOWN;
1797 getLyXText(bv)->cursorDown(bv);
1798 return DISPATCHED_NOUPDATE;
1802 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1804 if (the_locking_inset) {
1805 if (the_locking_inset->insetAllowed(inset))
1806 return the_locking_inset->insertInset(bv, inset);
1809 inset->setOwner(this);
1810 hideInsetCursor(bv);
1814 lt = getLyXText(bv);
1817 lt->insertInset(bv, inset);
1821 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1826 bool InsetText::insetAllowed(Inset::Code code) const
1828 // in_insetAllowed is a really gross hack,
1829 // to allow us to call the owner's insetAllowed
1830 // without stack overflow, which can happen
1831 // when the owner uses InsetCollapsable::insetAllowed()
1833 if (in_insetAllowed)
1835 in_insetAllowed = true;
1836 if (the_locking_inset)
1837 ret = the_locking_inset->insetAllowed(code);
1839 ret = owner()->insetAllowed(code);
1840 in_insetAllowed = false;
1845 UpdatableInset * InsetText::getLockingInset() const
1847 return the_locking_inset ? the_locking_inset->getLockingInset() :
1848 const_cast<InsetText *>(this);
1852 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1856 if (the_locking_inset)
1857 return the_locking_inset->getFirstLockingInsetOfType(c);
1862 bool InsetText::showInsetDialog(BufferView * bv) const
1864 if (the_locking_inset)
1865 return the_locking_inset->showInsetDialog(bv);
1870 std::vector<string> const InsetText::getLabelList() const
1872 std::vector<string> label_list;
1874 Paragraph * tpar = par;
1876 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1877 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1878 for (; beg != end; ++beg) {
1879 std::vector<string> const l = (*beg)->getLabelList();
1880 label_list.insert(label_list.end(), l.begin(), l.end());
1882 tpar = tpar->next();
1888 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1891 if (the_locking_inset) {
1892 the_locking_inset->setFont(bv, font, toggleall, selectall);
1895 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1896 getLyXText(bv)->setFont(bv, font, toggleall);
1901 lt = getLyXText(bv);
1904 if (lt->selection.set()) {
1905 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1909 lt->toggleFree(bv, font, toggleall);
1911 lt->clearSelection();
1913 bool flag = (selectall || lt->selection.set());
1917 updateLocal(bv, FULL, true);
1919 updateLocal(bv, CURSOR_PAR, true);
1923 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1925 if (cpar(bv)->isInset(cpos(bv))) {
1929 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1930 if (!isHighlyEditableInset(inset))
1932 LyXFont const font =
1933 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1935 x = inset->width(bv, font);
1936 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1939 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1941 //inset_x = cx(bv) - top_x + drawTextXOffset;
1942 //inset_y = cy(bv) + drawTextYOffset;
1943 inset->edit(bv, x, y, 0);
1944 if (!the_locking_inset)
1946 updateLocal(bv, CURSOR, false);
1953 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1956 x -= drawTextXOffset;
1958 int dummyy = y + insetAscent;
1959 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
1966 inset_x = cx(bv) - top_x + drawTextXOffset;
1967 inset_y = cy(bv) + drawTextYOffset;
1968 inset->edit(bv, x - inset_x, y - inset_y, button);
1969 if (!the_locking_inset)
1971 updateLocal(bv, CURSOR, false);
1978 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1981 int w = UpdatableInset::getMaxWidth(bv, inset);
1986 w = w - top_x + owner()->x();
1989 w -= (2 * TEXT_TO_INSET_OFFSET);
1992 return UpdatableInset::getMaxWidth(bv, inset);
1997 void InsetText::setParagraphData(Paragraph * p, bool same_id)
1999 // we have to unlock any locked inset otherwise we're in troubles
2000 the_locking_inset = 0;
2002 Paragraph * tmp = par->next();
2007 par = new Paragraph(*p, same_id);
2008 par->setInsetOwner(this);
2009 Paragraph * np = par;
2012 np->next(new Paragraph(*p, same_id));
2013 np->next()->previous(np);
2015 np->setInsetOwner(this);
2022 void InsetText::setText(string const & data)
2025 LyXFont font(LyXFont::ALL_SANE);
2026 for (unsigned int i=0; i < data.length(); ++i)
2027 par->insertChar(i, data[i], font);
2031 void InsetText::setAutoBreakRows(bool flag)
2033 if (flag != autoBreakRows) {
2034 autoBreakRows = flag;
2042 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2044 if (how != drawFrame_) {
2047 updateLocal(bv, DRAW_FRAME, false);
2052 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2054 if (frame_color != col) {
2057 updateLocal(bv, DRAW_FRAME, false);
2062 int InsetText::cx(BufferView * bv) const
2064 // we do nothing dangerous so we use a local cache
2065 LyXText * llt = getLyXText(bv);
2066 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2067 if (the_locking_inset) {
2068 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2070 if (font.isVisibleRightToLeft())
2071 x -= the_locking_inset->width(bv, font);
2077 int InsetText::cy(BufferView * bv) const
2080 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2084 pos_type InsetText::cpos(BufferView * bv) const
2086 return getLyXText(bv)->cursor.pos();
2090 Paragraph * InsetText::cpar(BufferView * bv) const
2092 return getLyXText(bv)->cursor.par();
2096 bool InsetText::cboundary(BufferView * bv) const
2098 return getLyXText(bv)->cursor.boundary();
2102 Row * InsetText::crow(BufferView * bv) const
2104 return getLyXText(bv)->cursor.row();
2108 LyXText * InsetText::getLyXText(BufferView const * lbv,
2109 bool const recursive) const
2111 if (!recursive && (cached_bview == lbv)) {
2112 LyXText * lt = cached_text.get();
2113 lyx::Assert(lt && lt->firstRow()->par() == par);
2114 return cached_text.get();
2117 // Super UGLY! (Lgb)
2118 BufferView * bv = const_cast<BufferView *>(lbv);
2121 Cache::iterator it = cache.find(bv);
2123 if (it != cache.end()) {
2127 resizeLyXText(do_resize);
2128 if (lt || !it->second.remove) {
2129 lyx::Assert(it->second.text.get());
2130 cached_text = it->second.text;
2131 if (recursive && the_locking_inset) {
2132 return the_locking_inset->getLyXText(bv, true);
2134 return cached_text.get();
2135 } else if (it->second.remove) {
2137 saveLyXTextState(it->second.text.get());
2144 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2145 cached_text->init(bv);
2146 restoreLyXTextState(bv, cached_text.get());
2148 cache.insert(make_pair(bv, cached_text));
2150 if (the_locking_inset && recursive) {
2151 return the_locking_inset->getLyXText(bv);
2153 return cached_text.get();
2157 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2161 Cache::iterator it = cache.find(bv);
2163 if (it == cache.end()) {
2167 lyx::Assert(it->second.text.get());
2169 it->second.remove = true;
2171 /// then remove all LyXText in text-insets
2172 Paragraph * p = par;
2173 for (; p; p = p->next()) {
2174 p->deleteInsetsLyXText(bv);
2180 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2183 // we cannot resize this because we are in use!
2184 // so do this on the next possible getLyXText()
2189 // lyxerr << "InsetText::resizeLyXText\n";
2190 if (!par->next() && !par->size()) // no data, resize not neccessary!
2192 // one endless line, resize normally not necessary
2193 if (!force && getMaxWidth(bv, this) < 0)
2196 Cache::iterator it = cache.find(bv);
2197 if (it == cache.end()) {
2200 lyx::Assert(it->second.text.get());
2202 LyXText * t = it->second.text.get();
2203 saveLyXTextState(t);
2204 for (Paragraph * p = par; p; p = p->next()) {
2205 p->resizeInsetsLyXText(bv);
2208 restoreLyXTextState(bv, t);
2209 if (the_locking_inset) {
2210 inset_x = cx(bv) - top_x + drawTextXOffset;
2211 inset_y = cy(bv) + drawTextYOffset;
2215 t->first = bv->screen()->topCursorVisible(t);
2218 updateLocal(bv, FULL, false);
2219 // this will scroll the screen such that the cursor becomes visible
2220 bv->updateScrollbar();
2222 need_update |= FULL;
2227 void InsetText::reinitLyXText() const
2230 // we cannot resize this because we are in use!
2231 // so do this on the next possible getLyXText()
2237 // lyxerr << "InsetText::reinitLyXText\n";
2238 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2239 lyx::Assert(it->second.text.get());
2241 LyXText * t = it->second.text.get();
2242 BufferView * bv = it->first;
2244 saveLyXTextState(t);
2245 for (Paragraph * p = par; p; p = p->next()) {
2246 p->resizeInsetsLyXText(bv);
2249 restoreLyXTextState(bv, t);
2250 if (the_locking_inset) {
2251 inset_x = cx(bv) - top_x + drawTextXOffset;
2252 inset_y = cy(bv) + drawTextYOffset;
2255 t->first = bv->screen()->topCursorVisible(t);
2258 updateLocal(bv, FULL, false);
2259 // this will scroll the screen such that the cursor becomes visible
2260 bv->updateScrollbar();
2268 void InsetText::removeNewlines()
2270 bool changed = false;
2272 for (Paragraph * p = par; p; p = p->next()) {
2273 for (int i = 0; i < p->size(); ++i) {
2274 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2285 bool InsetText::nodraw() const
2287 if (the_locking_inset)
2288 return the_locking_inset->nodraw();
2289 return UpdatableInset::nodraw();
2293 int InsetText::scroll(bool recursive) const
2295 int sx = UpdatableInset::scroll(false);
2297 if (recursive && the_locking_inset)
2298 sx += the_locking_inset->scroll(recursive);
2304 bool InsetText::doClearArea() const
2306 return !locked || (need_update & (FULL|INIT));
2310 void InsetText::selectAll(BufferView * bv)
2312 getLyXText(bv)->cursorTop(bv);
2313 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2314 getLyXText(bv)->cursorBottom(bv);
2315 getLyXText(bv)->setSelection(bv);
2319 void InsetText::clearSelection(BufferView * bv)
2321 getLyXText(bv)->clearSelection();
2325 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2328 Painter & pain = bv->painter();
2330 int h = ascent(bv, dummy) + descent(bv, dummy);
2331 int ty = baseline - insetAscent;
2337 if ((ty + h) > pain.paperHeight())
2338 h = pain.paperHeight();
2339 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2340 w = pain.paperWidth();
2341 // w -= TEXT_TO_INSET_OFFSET;
2342 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2345 frame_is_visible = false;
2349 Paragraph * InsetText::getParFromID(int id) const
2352 Paragraph * result = par;
2353 Paragraph * ires = 0;
2354 while (result && result->id() != id) {
2355 if ((ires = result->getParFromID(id)))
2357 result = result->next();
2361 Paragraph * tmp = par;
2363 if (tmp->id() == id) {
2366 Paragraph * tmp2 = tmp->getParFromID(id);
2377 Paragraph * InsetText::firstParagraph() const
2380 if (the_locking_inset)
2381 if ((result = the_locking_inset->firstParagraph()))
2387 Paragraph * InsetText::getFirstParagraph(int i) const
2389 return (i == 0) ? par : 0;
2393 LyXCursor const & InsetText::cursor(BufferView * bv) const
2395 if (the_locking_inset)
2396 return the_locking_inset->cursor(bv);
2397 return getLyXText(bv)->cursor;
2401 Paragraph * InsetText::paragraph() const
2407 void InsetText::paragraph(Paragraph * p)
2409 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2410 // caller of this function has to take care of it. This IS important
2411 // as we could have to insert a paragraph before this one and just
2412 // link the actual to a new ones next and set it with this function
2415 // set ourself as owner for all the paragraphs inserted!
2416 Paragraph * np = par;
2418 np->setInsetOwner(this);
2422 // redraw myself when asked for
2427 Inset * InsetText::getInsetFromID(int id_arg) const
2430 return const_cast<InsetText *>(this);
2432 Paragraph * lp = par;
2435 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2436 en = lp->inset_iterator_end();
2439 if ((*it)->id() == id_arg)
2441 Inset * in = (*it)->getInsetFromID(id_arg);
2451 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2457 lt = getLyXText(bv);
2460 if (the_locking_inset) {
2461 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2468 #warning Dekel please have a look on this one RTL? (Jug)
2470 // we have to go on checking so move cusor to the right
2471 lt->cursor.pos(lt->cursor.pos() + 1);
2473 str = lt->selectNextWordToSpellcheck(bv, value);
2475 bv->unlockInset(const_cast<InsetText *>(this));
2484 void InsetText::selectSelectedWord(BufferView * bv)
2486 if (the_locking_inset) {
2487 the_locking_inset->selectSelectedWord(bv);
2490 getLyXText(bv)->selectSelectedWord(bv);
2491 updateLocal(bv, SELECTION, false);
2495 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2497 if (the_locking_inset) {
2498 the_locking_inset->toggleSelection(bv, kill_selection);
2502 lt = getLyXText(bv);
2506 int x = top_x + TEXT_TO_INSET_OFFSET;
2508 Row * row = lt->firstRow();
2509 int y_offset = top_baseline - row->ascent_of_text();
2511 while ((row != 0) && ((y+row->height()) <= 0)) {
2518 if (need_update & SELECTION)
2520 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2526 bool InsetText::searchForward(BufferView * bv, string const & str,
2529 if (the_locking_inset) {
2530 if (the_locking_inset->searchForward(bv, str, cs, mw))
2534 lt = getLyXText(bv);
2537 Paragraph * lpar = lt->cursor.par();
2538 pos_type pos = lt->cursor.pos();
2539 if (pos < lpar->size() - 1)
2543 lpar = lpar->next();
2548 // we have to unlock ourself in this function by default!
2549 bv->unlockInset(const_cast<InsetText *>(this));
2552 lt->setCursor(bv, lpar, pos);
2556 if (LyXFind(bv, str, true, true, cs , mw)) {
2559 // we have to unlock ourself in this function by default!
2560 bv->unlockInset(const_cast<InsetText *>(this));
2564 bool InsetText::searchBackward(BufferView * bv, string const & str,
2567 if (the_locking_inset)
2568 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2570 if (LyXFind(bv, str, false, true, cs, mw)) {
2573 // we have to unlock ourself in this function by default!
2574 bv->unlockInset(const_cast<InsetText *>(this));
2579 bool InsetText::checkInsertChar(LyXFont & font)
2582 return owner()->checkInsertChar(font);
2587 void InsetText::collapseParagraphs(BufferParams const & bparams) const
2589 while(par->next()) {
2590 if (!par->isSeparator(par->size()-1))
2591 par->insertChar(par->size()-1, ' ');
2592 par->pasteParagraph(bparams);