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"
26 #include "lyxtextclasslist.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, 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 // call these methods so that insetWidth, insetAscent and
365 // insetDescent have the right values.
370 // repaint the background if needed
371 if (cleared && backgroundColor() != LColor::background) {
373 clearInset(bv, baseline, cleared);
377 // no draw is necessary !!!
378 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
381 top_baseline = baseline;
383 if (need_update & CLEAR_FRAME)
384 clearFrame(pain, cleared);
391 x += static_cast<float>(scroll());
393 // if top_x differs we did it already
394 if (!cleared && (top_x == int(x))
395 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
396 ||(last_drawn_width != insetWidth))) {
397 // Condition necessary to eliminate bug 59 attachment 37
398 if (baseline > 0) clearInset(bv, baseline, cleared);
404 frame_is_visible = false;
406 if (!cleared && (need_update == NONE)) {
408 drawFrame(pain, cleared);
412 top_baseline = baseline;
413 top_y = baseline - insetAscent;
415 if (last_drawn_width != insetWidth) {
417 clearInset(bv, baseline, cleared);
419 last_drawn_width = insetWidth;
422 if (the_locking_inset && (cpar(bv) == inset_par)
423 && (cpos(bv) == inset_pos)) {
424 inset_x = cx(bv) - top_x + drawTextXOffset;
425 inset_y = cy(bv) + drawTextYOffset;
427 if (!cleared && (need_update == CURSOR)
428 && !getLyXText(bv)->selection.set()) {
429 drawFrame(pain, cleared);
439 x += TEXT_TO_INSET_OFFSET;
441 Row * row = lt->firstRow();
442 int y_offset = baseline - row->ascent_of_text();
443 int ph = pain.paperHeight();
446 while ((row != 0) && ((y+row->height()) <= 0)) {
448 first += row->height();
454 if (cleared || (need_update&(INIT|FULL))) {
457 while ((row != 0) && (yf < ph)) {
458 lt->getVisibleRow(bv, y+y_offset, int(x), row,
464 } else if (!locked) {
465 if (need_update & CURSOR) {
466 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
467 lt->clearSelection();
468 lt->selection.cursor = lt->cursor;
470 bv->screen()->update(lt, bv, y_offset, int(x));
473 if (need_update & SELECTION) {
474 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
475 } else if (need_update & CURSOR) {
476 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
477 lt->clearSelection();
478 lt->selection.cursor = lt->cursor;
480 bv->screen()->update(lt, bv, y_offset, int(x));
485 lt->status(bv, LyXText::UNCHANGED);
486 if ((need_update != CURSOR_PAR) &&
487 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked))) {
488 drawFrame(pain, cleared);
489 } else if (need_update & CLEAR_FRAME) {
490 clearFrame(pain, cleared);
493 x += insetWidth - TEXT_TO_INSET_OFFSET;
495 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
497 } else if (need_update != INIT) {
505 void InsetText::drawFrame(Painter & pain, bool cleared) const
507 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
508 if (!frame_is_visible || cleared) {
509 frame_x = top_x + ttoD2;
510 frame_y = top_baseline - insetAscent + ttoD2;
511 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
512 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
513 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
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);
621 if (need_update == CURSOR)
623 bv->owner()->showState();
624 bv->owner()->updateMenubar();
625 bv->owner()->updateToolbar();
626 if (old_par != cpar(bv)) {
627 bv->owner()->setLayout(cpar(bv)->getLayout());
633 string const InsetText::editMessage() const
635 return _("Opened Text Inset");
639 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
641 UpdatableInset::edit(bv, x, y, button);
643 if (!bv->lockInset(this)) {
644 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
648 the_locking_inset = 0;
649 inset_pos = inset_x = inset_y = 0;
650 inset_boundary = false;
653 int tmp_y = (y < 0) ? 0 : y;
659 if (!checkAndActivateInset(bv, x, tmp_y, button))
660 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
662 lt->clearSelection();
664 // If the inset is empty set the language of the current font to the
665 // language to the surronding text (if different).
666 if (par->size() == 0 && !par->next() &&
667 bv->getParentLanguage(this) != lt->current_font.language())
669 LyXFont font(LyXFont::ALL_IGNORE);
670 font.setLanguage(bv->getParentLanguage(this));
671 setFont(bv, font, false);
676 updateLocal(bv, CURSOR, false);
680 void InsetText::edit(BufferView * bv, bool front)
682 UpdatableInset::edit(bv, front);
684 if (!bv->lockInset(this)) {
685 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
689 the_locking_inset = 0;
690 inset_pos = inset_x = inset_y = 0;
691 inset_boundary = false;
700 lt->setCursor(bv, par, 0);
705 // int const pos = (p->size() ? p->size()-1 : p->size());
706 lt->setCursor(bv, p, p->size());
708 lt->clearSelection();
710 // If the inset is empty set the language of the current font to the
711 // language to the surronding text (if different).
712 if (par->size() == 0 && !par->next() &&
713 bv->getParentLanguage(this) != lt->current_font.language()) {
714 LyXFont font(LyXFont::ALL_IGNORE);
715 font.setLanguage(bv->getParentLanguage(this));
716 setFont(bv, font, false);
721 updateLocal(bv, CURSOR, false);
725 void InsetText::insetUnlock(BufferView * bv)
727 if (the_locking_inset) {
728 the_locking_inset->insetUnlock(bv);
729 the_locking_inset = 0;
732 no_selection = false;
735 if (drawFrame_ == LOCKED)
736 code = CURSOR|CLEAR_FRAME;
744 if (lt->selection.set()) {
745 lt->clearSelection();
747 } else if (owner()) {
748 bv->owner()->setLayout(owner()->getLyXText(bv)
749 ->cursor.par()->getLayout());
751 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
752 // hack for deleteEmptyParMech
753 lt->setCursor(bv, par, 0);
756 updateLocal(bv, code, false);
759 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
761 the_locking_inset = inset;
762 inset_x = cx(bv) - top_x + drawTextXOffset;
763 inset_y = cy(bv) + drawTextYOffset;
764 inset_pos = cpos(bv);
765 inset_par = cpar(bv);
766 inset_boundary = cboundary(bv);
767 updateLocal(bv, CURSOR, false);
771 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
773 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
777 if (!the_locking_inset) {
779 int const id = inset->id();
781 Paragraph::inset_iterator it =
782 p->inset_iterator_begin();
783 Paragraph::inset_iterator const end =
784 p->inset_iterator_end();
785 for (; it != end; ++it) {
786 if ((*it) == inset) {
787 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
788 lockInset(bv, inset);
791 if ((*it)->getInsetFromID(id)) {
792 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
793 lockInset(bv, static_cast<UpdatableInset *>(*it));
794 return the_locking_inset->lockInsetInInset(bv, inset);
801 if (inset == cpar(bv)->getInset(cpos(bv))) {
802 lyxerr[Debug::INSETS] << "OK" << endl;
803 lockInset(bv, inset);
805 } else if (the_locking_inset && (the_locking_inset == inset)) {
806 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
807 lyxerr[Debug::INSETS] << "OK" << endl;
808 inset_x = cx(bv) - top_x + drawTextXOffset;
809 inset_y = cy(bv) + drawTextYOffset;
811 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
813 } else if (the_locking_inset) {
814 lyxerr[Debug::INSETS] << "MAYBE" << endl;
815 return the_locking_inset->lockInsetInInset(bv, inset);
817 lyxerr[Debug::INSETS] << "NOT OK" << endl;
822 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
825 if (!the_locking_inset)
827 if (the_locking_inset == inset) {
828 the_locking_inset->insetUnlock(bv);
829 getLyXText(bv)->updateInset(bv, inset);
830 the_locking_inset = 0;
832 moveRight(bv, false);
833 old_par = 0; // force layout setting
837 updateLocal(bv, CURSOR, false);
840 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
844 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
846 if (!autoBreakRows && par->next())
847 collapseParagraphs(bv->buffer()->params);
855 if (!the_locking_inset) {
856 bool found = lt->updateInset(bv, inset);
860 setUpdateStatus(bv, NONE);
863 if (the_locking_inset != inset) {
864 bool found = the_locking_inset->updateInsetInInset(bv, inset);
868 setUpdateStatus(bv, CURSOR_PAR);
871 bool found = lt->updateInset(bv, inset);
875 setUpdateStatus(bv, CURSOR_PAR);
876 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
877 inset_x = cx(bv) - top_x + drawTextXOffset;
878 inset_y = cy(bv) + drawTextYOffset;
885 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
889 // use this to check mouse motion for selection!
893 int tmp_x = x - drawTextXOffset;
894 int tmp_y = y + insetAscent - getLyXText(bv)->first;
895 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
898 if (the_locking_inset) {
899 if (the_locking_inset == inset) {
900 the_locking_inset->insetButtonPress(bv,
904 no_selection = false;
907 // otherwise unlock the_locking_inset and lock the new inset
908 the_locking_inset->insetUnlock(bv);
909 inset_x = cx(bv) - top_x + drawTextXOffset;
910 inset_y = cy(bv) + drawTextYOffset;
911 the_locking_inset = 0;
912 inset->insetButtonPress(bv, x - inset_x,
913 y - inset_y, button);
914 inset->edit(bv, x - inset_x, y - inset_y, button);
915 if (the_locking_inset)
916 updateLocal(bv, CURSOR, false);
917 no_selection = false;
920 // otherwise only unlock the_locking_inset
921 the_locking_inset->insetUnlock(bv);
922 the_locking_inset = 0;
924 if (bv->theLockingInset()) {
925 if (isHighlyEditableInset(inset)) {
926 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
927 inset_x = cx(bv) - top_x + drawTextXOffset;
928 inset_y = cy(bv) + drawTextYOffset;
929 inset_pos = cpos(bv);
930 inset_par = cpar(bv);
931 inset_boundary = cboundary(bv);
932 the_locking_inset = uinset;
933 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
935 uinset->edit(bv, x - inset_x, y - inset_y, 0);
936 if (the_locking_inset)
937 updateLocal(bv, CURSOR, false);
938 no_selection = false;
942 if (!inset) { // && (button == 2)) {
943 bool paste_internally = false;
944 if ((button == 2) && getLyXText(bv)->selection.set()) {
945 localDispatch(bv, LFUN_COPY, "");
946 paste_internally = true;
954 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
956 // set the selection cursor!
957 lt->selection.cursor = lt->cursor;
958 lt->cursor.x_fix(lt->cursor.x());
960 if (lt->selection.set()) {
961 lt->clearSelection();
964 updateLocal(bv, FULL, false);
966 lt->clearSelection();
969 updateLocal(bv, CURSOR, false);
971 bv->owner()->setLayout(cpar(bv)->getLayout());
973 // Insert primary selection with middle mouse
974 // if there is a local selection in the current buffer,
977 if (paste_internally)
978 localDispatch(bv, LFUN_PASTE, "");
980 localDispatch(bv, LFUN_PASTESELECTION,
984 getLyXText(bv)->clearSelection();
987 no_selection = false;
991 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
993 if (the_locking_inset) {
994 return the_locking_inset->insetButtonRelease(bv,
995 x - inset_x, y - inset_y,
998 int tmp_x = x - drawTextXOffset;
999 int tmp_y = y + insetAscent - getLyXText(bv)->first;
1000 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1003 if (isHighlyEditableInset(inset)) {
1004 ret = inset->insetButtonRelease(bv, x - inset_x,
1005 y - inset_y, button);
1007 inset_x = cx(bv) - top_x + drawTextXOffset;
1008 inset_y = cy(bv) + drawTextYOffset;
1009 ret = inset->insetButtonRelease(bv, x - inset_x,
1010 y - inset_y, button);
1011 inset->edit(bv, x - inset_x,
1012 y - inset_y, button);
1014 updateLocal(bv, CURSOR_PAR, false);
1020 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1022 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1024 if (the_locking_inset) {
1025 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1031 lt = getLyXText(bv);
1034 hideInsetCursor(bv);
1035 LyXCursor cur = lt->cursor;
1036 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1037 if (cur == lt->cursor) {
1042 lt->setSelection(bv);
1043 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1044 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1048 updateLocal(bv, SELECTION, false);
1050 showInsetCursor(bv);
1054 void InsetText::insetKeyPress(XKeyEvent * xke)
1056 if (the_locking_inset) {
1057 the_locking_inset->insetKeyPress(xke);
1063 UpdatableInset::RESULT
1064 InsetText::localDispatch(BufferView * bv,
1065 kb_action action, string const & arg)
1067 bool was_empty = par->size() == 0 && !par->next();
1068 no_selection = false;
1069 UpdatableInset::RESULT
1070 result= UpdatableInset::localDispatch(bv, action, arg);
1071 if (result != UNDISPATCHED) {
1075 result = DISPATCHED;
1076 if ((action < 0) && arg.empty())
1079 if (the_locking_inset) {
1080 result = the_locking_inset->localDispatch(bv, action, arg);
1081 if (result == DISPATCHED_NOUPDATE)
1083 else if (result == DISPATCHED) {
1084 updateLocal(bv, CURSOR_PAR, false);
1086 } else if (result >= FINISHED) {
1088 case FINISHED_RIGHT:
1089 moveRightIntern(bv, false, false);
1090 result = DISPATCHED;
1093 if ((result = moveUp(bv)) >= FINISHED) {
1094 updateLocal(bv, CURSOR, false);
1095 bv->unlockInset(this);
1099 if ((result = moveDown(bv)) >= FINISHED) {
1100 updateLocal(bv, CURSOR, false);
1101 bv->unlockInset(this);
1105 result = DISPATCHED;
1108 the_locking_inset = 0;
1109 #ifdef WITH_WARNINGS
1110 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1115 hideInsetCursor(bv);
1118 lt = getLyXText(bv);
1122 int updflag = false;
1125 case LFUN_SELFINSERT:
1126 if (bv->buffer()->isReadonly()) {
1127 // setErrorMessage(N_("Document is read only"));
1131 /* Automatically delete the currently selected
1132 * text and replace it with what is being
1133 * typed in now. Depends on lyxrc settings
1134 * "auto_region_delete", which defaults to
1137 setUndo(bv, Undo::INSERT,
1138 lt->cursor.par(), lt->cursor.par()->next());
1140 if (lyxrc.auto_region_delete) {
1141 if (lt->selection.set()) {
1142 lt->cutSelection(bv, false);
1145 lt->clearSelection();
1146 for (string::size_type i = 0; i < arg.length(); ++i) {
1147 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1150 lt->selection.cursor = lt->cursor;
1151 updwhat = CURSOR_PAR;
1153 result = DISPATCHED_NOUPDATE;
1155 // --- Cursor Movements -----------------------------------
1158 moveRight(bv, false, true);
1159 lt->setSelection(bv);
1160 updwhat = SELECTION;
1163 result = moveRight(bv);
1169 moveLeft(bv, false, true);
1170 lt->setSelection(bv);
1171 updwhat = SELECTION;
1175 result = moveLeft(bv);
1181 lt->setSelection(bv);
1182 updwhat = SELECTION;
1186 result = moveDown(bv);
1192 lt->setSelection(bv);
1193 updwhat = SELECTION;
1197 result = moveUp(bv);
1209 case LFUN_BACKSPACE: {
1210 setUndo(bv, Undo::DELETE,
1211 lt->cursor.par(), lt->cursor.par()->next());
1212 if (lt->selection.set())
1213 lt->cutSelection(bv);
1216 updwhat = CURSOR_PAR;
1222 setUndo(bv, Undo::DELETE,
1223 lt->cursor.par(), lt->cursor.par()->next());
1224 if (lt->selection.set()) {
1225 lt->cutSelection(bv);
1229 updwhat = CURSOR_PAR;
1235 setUndo(bv, Undo::DELETE,
1236 lt->cursor.par(), lt->cursor.par()->next());
1237 lt->cutSelection(bv);
1238 updwhat = CURSOR_PAR;
1245 lt->copySelection(bv);
1246 updwhat = CURSOR_PAR;
1248 case LFUN_PASTESELECTION:
1250 string const clip(bv->getClipboard());
1254 if (arg == "paragraph") {
1255 lt->insertStringAsParagraphs(bv, clip);
1257 lt->insertStringAsLines(bv, clip);
1259 updwhat = CURSOR_PAR;
1264 if (!autoBreakRows) {
1266 if (CutAndPaste::nrOfParagraphs() > 1) {
1267 Alert::alert(_("Impossible operation"),
1268 _("Cannot include more than one paragraph!"),
1273 setUndo(bv, Undo::INSERT,
1274 lt->cursor.par(), lt->cursor.par()->next());
1275 lt->pasteSelection(bv);
1276 updwhat = CURSOR_PAR;
1281 case LFUN_BREAKPARAGRAPH:
1282 if (!autoBreakRows) {
1283 result = DISPATCHED;
1286 lt->breakParagraph(bv, 0);
1290 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1291 if (!autoBreakRows) {
1292 result = DISPATCHED;
1295 lt->breakParagraph(bv, 1);
1300 case LFUN_BREAKLINE: {
1301 if (!autoBreakRows) {
1302 result = DISPATCHED;
1305 setUndo(bv, Undo::INSERT,
1306 lt->cursor.par(), lt->cursor.par()->next());
1307 lt->insertChar(bv, Paragraph::META_NEWLINE);
1308 updwhat = CURSOR_PAR;
1314 // do not set layouts on non breakable textinsets
1315 if (autoBreakRows) {
1316 layout_type cur_layout = cpar(bv)->layout;
1318 // Derive layout number from given argument (string)
1319 // and current buffer's textclass (number). */
1320 textclass_type tclass = bv->buffer()->params.textclass;
1321 std::pair <bool, layout_type> layout =
1322 textclasslist.NumberOfLayout(tclass, arg);
1324 // If the entry is obsolete, use the new one instead.
1326 string obs = textclasslist.Style(tclass,layout.second).
1329 layout = textclasslist.NumberOfLayout(tclass, obs);
1332 // see if we found the layout number:
1333 if (!layout.first) {
1334 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1335 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1339 if (cur_layout != layout.second) {
1340 cur_layout = layout.second;
1341 lt->setLayout(bv, layout.second);
1342 bv->owner()->setLayout(cpar(bv)->getLayout());
1343 updwhat = CURSOR_PAR;
1347 // reset the layout box
1348 bv->owner()->setLayout(cpar(bv)->getLayout());
1351 case LFUN_PARAGRAPH_SPACING:
1352 // This one is absolutely not working. When fiddling with this
1353 // it also seems to me that the paragraphs inside the insettext
1354 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1356 Paragraph * par = lt->cursor.par();
1357 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1358 float cur_value = 1.0;
1359 if (cur_spacing == Spacing::Other) {
1360 cur_value = par->params().spacing().getValue();
1363 istringstream istr(arg.c_str());
1366 Spacing::Space new_spacing = cur_spacing;
1367 float new_value = cur_value;
1369 lyxerr << "Missing argument to `paragraph-spacing'"
1371 } else if (tmp == "single") {
1372 new_spacing = Spacing::Single;
1373 } else if (tmp == "onehalf") {
1374 new_spacing = Spacing::Onehalf;
1375 } else if (tmp == "double") {
1376 new_spacing = Spacing::Double;
1377 } else if (tmp == "other") {
1378 new_spacing = Spacing::Other;
1381 lyxerr << "new_value = " << tmpval << endl;
1384 } else if (tmp == "default") {
1385 new_spacing = Spacing::Default;
1387 lyxerr << _("Unknown spacing argument: ")
1390 if (cur_spacing != new_spacing || cur_value != new_value) {
1391 par->params().spacing(Spacing(new_spacing, new_value));
1392 updwhat = CURSOR_PAR;
1399 if (!bv->Dispatch(action, arg))
1400 result = UNDISPATCHED;
1407 updateLocal(bv, updwhat, updflag);
1408 /// If the action has deleted all text in the inset, we need to change the
1409 // language to the language of the surronding text.
1410 if (!was_empty && par->size() == 0 && !par->next()) {
1411 LyXFont font(LyXFont::ALL_IGNORE);
1412 font.setLanguage(bv->getParentLanguage(this));
1413 setFont(bv, font, false);
1416 if (result < FINISHED) {
1417 showInsetCursor(bv);
1419 bv->unlockInset(this);
1424 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1427 buf->latexParagraphs(os, par, 0, texrow);
1428 return texrow.rows();
1432 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1434 Paragraph * p = par;
1435 unsigned int lines = 0;
1438 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1439 lines += countChar(tmp, '\n');
1447 int InsetText::docbook(Buffer const * buf, ostream & os) const
1449 Paragraph * p = par;
1450 unsigned int lines = 0;
1452 vector<string> environment_stack(10);
1453 vector<string> environment_inner(10);
1455 int const command_depth = 0;
1458 Paragraph::depth_type depth = 0; // paragraph depth
1462 int desc_on = 0; // description mode
1464 LyXLayout const & style =
1465 textclasslist.Style(buf->params.textclass,
1468 // environment tag closing
1469 for (; depth > p->params().depth(); --depth) {
1470 if (environment_inner[depth] != "!-- --") {
1471 item_name = "listitem";
1472 buf->sgmlCloseTag(os, command_depth + depth,
1474 if (environment_inner[depth] == "varlistentry")
1475 buf->sgmlCloseTag(os, depth+command_depth,
1476 environment_inner[depth]);
1478 buf->sgmlCloseTag(os, depth + command_depth,
1479 environment_stack[depth]);
1480 environment_stack[depth].erase();
1481 environment_inner[depth].erase();
1484 if (depth == p->params().depth()
1485 && environment_stack[depth] != style.latexname()
1486 && !environment_stack[depth].empty()) {
1487 if (environment_inner[depth] != "!-- --") {
1488 item_name= "listitem";
1489 buf->sgmlCloseTag(os, command_depth+depth,
1491 if (environment_inner[depth] == "varlistentry")
1492 buf->sgmlCloseTag(os,
1493 depth + command_depth,
1494 environment_inner[depth]);
1497 buf->sgmlCloseTag(os, depth + command_depth,
1498 environment_stack[depth]);
1500 environment_stack[depth].erase();
1501 environment_inner[depth].erase();
1504 // Write opening SGML tags.
1505 switch (style.latextype) {
1506 case LATEX_PARAGRAPH:
1507 buf->sgmlOpenTag(os, depth + command_depth,
1512 buf->sgmlError(p, 0,
1513 _("Error : LatexType Command not allowed here.\n"));
1517 case LATEX_ENVIRONMENT:
1518 case LATEX_ITEM_ENVIRONMENT:
1519 if (depth < p->params().depth()) {
1520 depth = p->params().depth();
1521 environment_stack[depth].erase();
1524 if (environment_stack[depth] != style.latexname()) {
1525 if(environment_stack.size() == depth + 1) {
1526 environment_stack.push_back("!-- --");
1527 environment_inner.push_back("!-- --");
1529 environment_stack[depth] = style.latexname();
1530 environment_inner[depth] = "!-- --";
1531 buf->sgmlOpenTag(os, depth + command_depth,
1532 environment_stack[depth]);
1534 if (environment_inner[depth] != "!-- --") {
1535 item_name= "listitem";
1536 buf->sgmlCloseTag(os,
1537 command_depth + depth,
1539 if (environment_inner[depth] == "varlistentry")
1540 buf->sgmlCloseTag(os,
1541 depth + command_depth,
1542 environment_inner[depth]);
1546 if (style.latextype == LATEX_ENVIRONMENT) {
1547 if (!style.latexparam().empty()) {
1548 if(style.latexparam() == "CDATA")
1551 buf->sgmlOpenTag(os, depth + command_depth,
1552 style.latexparam());
1557 desc_on = (style.labeltype == LABEL_MANUAL);
1560 environment_inner[depth]= "varlistentry";
1562 environment_inner[depth]= "listitem";
1564 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1565 environment_inner[depth]);
1569 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1573 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1578 buf->sgmlOpenTag(os, depth + command_depth,
1583 buf->simpleDocBookOnePar(os, p, desc_on,
1584 depth + 1 + command_depth);
1588 // write closing SGML tags
1589 switch (style.latextype) {
1590 case LATEX_ENVIRONMENT:
1591 if (!style.latexparam().empty()) {
1592 if(style.latexparam() == "CDATA")
1595 buf->sgmlCloseTag(os, depth + command_depth,
1596 style.latexparam());
1599 case LATEX_ITEM_ENVIRONMENT:
1600 if (desc_on == 1) break;
1602 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1604 case LATEX_PARAGRAPH:
1605 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1608 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1614 for (int d = depth; d >= 0; --d) {
1615 if (!environment_stack[depth].empty()) {
1616 if (environment_inner[depth] != "!-- --") {
1617 item_name = "listitem";
1618 buf->sgmlCloseTag(os, command_depth + depth,
1620 if (environment_inner[depth] == "varlistentry")
1621 buf->sgmlCloseTag(os, depth + command_depth,
1622 environment_inner[depth]);
1625 buf->sgmlCloseTag(os, depth + command_depth,
1626 environment_stack[depth]);
1634 void InsetText::validate(LaTeXFeatures & features) const
1636 Paragraph * p = par;
1638 p->validate(features);
1644 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1646 if (textclasslist.Style(buf->params.textclass,
1647 p->getLayout()).labeltype != LABEL_MANUAL)
1650 return p->beginningOfMainBody();
1654 void InsetText::getCursorPos(BufferView * bv,
1655 int & x, int & y) const
1657 if (the_locking_inset) {
1658 the_locking_inset->getCursorPos(bv, x, y);
1666 unsigned int InsetText::insetInInsetY()
1668 if (!the_locking_inset)
1671 return (inset_y + the_locking_inset->insetInInsetY());
1675 void InsetText::toggleInsetCursor(BufferView * bv)
1677 if (the_locking_inset) {
1678 the_locking_inset->toggleInsetCursor(bv);
1682 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1684 int const asc = lyxfont::maxAscent(font);
1685 int const desc = lyxfont::maxDescent(font);
1687 if (isCursorVisible())
1688 bv->hideLockedInsetCursor();
1690 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1691 toggleCursorVisible();
1695 void InsetText::showInsetCursor(BufferView * bv, bool show)
1697 if (the_locking_inset) {
1698 the_locking_inset->showInsetCursor(bv, show);
1701 if (!isCursorVisible()) {
1702 LyXFont const font =
1703 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1705 int const asc = lyxfont::maxAscent(font);
1706 int const desc = lyxfont::maxDescent(font);
1708 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1710 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1711 setCursorVisible(true);
1716 void InsetText::hideInsetCursor(BufferView * bv)
1718 if (isCursorVisible()) {
1719 bv->hideLockedInsetCursor();
1720 setCursorVisible(false);
1722 if (the_locking_inset)
1723 the_locking_inset->hideInsetCursor(bv);
1727 void InsetText::fitInsetCursor(BufferView * bv) const
1729 if (the_locking_inset) {
1730 the_locking_inset->fitInsetCursor(bv);
1733 LyXFont const font =
1734 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1736 int const asc = lyxfont::maxAscent(font);
1737 int const desc = lyxfont::maxDescent(font);
1739 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1743 UpdatableInset::RESULT
1744 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1746 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1747 return moveLeftIntern(bv, false, activate_inset, selecting);
1749 return moveRightIntern(bv, false, activate_inset, selecting);
1753 UpdatableInset::RESULT
1754 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1756 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1757 return moveRightIntern(bv, true, activate_inset, selecting);
1759 return moveLeftIntern(bv, true, activate_inset, selecting);
1763 UpdatableInset::RESULT
1764 InsetText::moveRightIntern(BufferView * bv, bool behind,
1765 bool activate_inset, bool selecting)
1767 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1768 return FINISHED_RIGHT;
1769 if (activate_inset && checkAndActivateInset(bv, behind))
1771 getLyXText(bv)->cursorRight(bv);
1773 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1774 return DISPATCHED_NOUPDATE;
1778 UpdatableInset::RESULT
1779 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1780 bool activate_inset, bool selecting)
1782 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1784 getLyXText(bv)->cursorLeft(bv);
1786 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1787 if (activate_inset && checkAndActivateInset(bv, behind))
1789 return DISPATCHED_NOUPDATE;
1793 UpdatableInset::RESULT
1794 InsetText::moveUp(BufferView * bv)
1796 if (!crow(bv)->previous())
1798 getLyXText(bv)->cursorUp(bv);
1799 return DISPATCHED_NOUPDATE;
1803 UpdatableInset::RESULT
1804 InsetText::moveDown(BufferView * bv)
1806 if (!crow(bv)->next())
1807 return FINISHED_DOWN;
1808 getLyXText(bv)->cursorDown(bv);
1809 return DISPATCHED_NOUPDATE;
1813 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1815 if (the_locking_inset) {
1816 if (the_locking_inset->insetAllowed(inset))
1817 return the_locking_inset->insertInset(bv, inset);
1822 lt = getLyXText(bv);
1825 setUndo(bv, Undo::FINISH, lt->cursor.par(), lt->cursor.par()->next());
1827 inset->setOwner(this);
1828 hideInsetCursor(bv);
1829 lt->insertInset(bv, inset);
1833 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1839 bool InsetText::insetAllowed(Inset::Code code) const
1841 // in_insetAllowed is a really gross hack,
1842 // to allow us to call the owner's insetAllowed
1843 // without stack overflow, which can happen
1844 // when the owner uses InsetCollapsable::insetAllowed()
1846 if (in_insetAllowed)
1848 in_insetAllowed = true;
1849 if (the_locking_inset)
1850 ret = the_locking_inset->insetAllowed(code);
1852 ret = owner()->insetAllowed(code);
1853 in_insetAllowed = false;
1858 UpdatableInset * InsetText::getLockingInset() const
1860 return the_locking_inset ? the_locking_inset->getLockingInset() :
1861 const_cast<InsetText *>(this);
1865 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1869 if (the_locking_inset)
1870 return the_locking_inset->getFirstLockingInsetOfType(c);
1875 bool InsetText::showInsetDialog(BufferView * bv) const
1877 if (the_locking_inset)
1878 return the_locking_inset->showInsetDialog(bv);
1883 std::vector<string> const InsetText::getLabelList() const
1885 std::vector<string> label_list;
1887 Paragraph * tpar = par;
1889 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1890 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1891 for (; beg != end; ++beg) {
1892 std::vector<string> const l = (*beg)->getLabelList();
1893 label_list.insert(label_list.end(), l.begin(), l.end());
1895 tpar = tpar->next();
1901 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1904 if (the_locking_inset) {
1905 the_locking_inset->setFont(bv, font, toggleall, selectall);
1908 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1909 getLyXText(bv)->setFont(bv, font, toggleall);
1914 lt = getLyXText(bv);
1917 if (lt->selection.set()) {
1918 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1922 lt->toggleFree(bv, font, toggleall);
1924 lt->clearSelection();
1926 bool flag = (selectall || lt->selection.set());
1930 updateLocal(bv, FULL, true);
1932 updateLocal(bv, CURSOR_PAR, true);
1936 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1938 if (cpar(bv)->isInset(cpos(bv))) {
1942 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1943 if (!isHighlyEditableInset(inset))
1945 LyXFont const font =
1946 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1948 x = inset->width(bv, font);
1949 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1952 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1954 //inset_x = cx(bv) - top_x + drawTextXOffset;
1955 //inset_y = cy(bv) + drawTextYOffset;
1956 inset->edit(bv, x, y, 0);
1957 if (!the_locking_inset)
1959 updateLocal(bv, CURSOR, false);
1966 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1969 x -= drawTextXOffset;
1971 int dummyy = y + insetAscent;
1972 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
1979 inset_x = cx(bv) - top_x + drawTextXOffset;
1980 inset_y = cy(bv) + drawTextYOffset;
1981 inset->edit(bv, x - inset_x, y - inset_y, button);
1982 if (!the_locking_inset)
1984 updateLocal(bv, CURSOR, false);
1991 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1994 int w = UpdatableInset::getMaxWidth(bv, inset);
1999 w = w - top_x + owner()->x();
2002 w -= (2 * TEXT_TO_INSET_OFFSET);
2005 return UpdatableInset::getMaxWidth(bv, inset);
2010 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2012 // we have to unlock any locked inset otherwise we're in troubles
2013 the_locking_inset = 0;
2015 Paragraph * tmp = par->next();
2020 par = new Paragraph(*p, same_id);
2021 par->setInsetOwner(this);
2022 Paragraph * np = par;
2025 np->next(new Paragraph(*p, same_id));
2026 np->next()->previous(np);
2028 np->setInsetOwner(this);
2035 void InsetText::setText(string const & data)
2038 LyXFont font(LyXFont::ALL_SANE);
2039 for (unsigned int i=0; i < data.length(); ++i)
2040 par->insertChar(i, data[i], font);
2044 void InsetText::setAutoBreakRows(bool flag)
2046 if (flag != autoBreakRows) {
2047 autoBreakRows = flag;
2055 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2057 if (how != drawFrame_) {
2060 updateLocal(bv, DRAW_FRAME, false);
2065 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2067 if (frame_color != col) {
2070 updateLocal(bv, DRAW_FRAME, false);
2075 int InsetText::cx(BufferView * bv) const
2077 // we do nothing dangerous so we use a local cache
2078 LyXText * llt = getLyXText(bv);
2079 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2080 if (the_locking_inset) {
2081 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2083 if (font.isVisibleRightToLeft())
2084 x -= the_locking_inset->width(bv, font);
2090 int InsetText::cy(BufferView * bv) const
2093 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2097 pos_type InsetText::cpos(BufferView * bv) const
2099 return getLyXText(bv)->cursor.pos();
2103 Paragraph * InsetText::cpar(BufferView * bv) const
2105 return getLyXText(bv)->cursor.par();
2109 bool InsetText::cboundary(BufferView * bv) const
2111 return getLyXText(bv)->cursor.boundary();
2115 Row * InsetText::crow(BufferView * bv) const
2117 return getLyXText(bv)->cursor.row();
2121 LyXText * InsetText::getLyXText(BufferView const * lbv,
2122 bool const recursive) const
2124 if (!recursive && (cached_bview == lbv)) {
2125 LyXText * lt = cached_text.get();
2126 lyx::Assert(lt && lt->firstRow()->par() == par);
2127 return cached_text.get();
2130 // Super UGLY! (Lgb)
2131 BufferView * bv = const_cast<BufferView *>(lbv);
2134 Cache::iterator it = cache.find(bv);
2136 if (it != cache.end()) {
2139 } else if (do_resize) {
2140 resizeLyXText(do_resize);
2142 if (lt || !it->second.remove) {
2143 lyx::Assert(it->second.text.get());
2144 cached_text = it->second.text;
2145 if (recursive && the_locking_inset) {
2146 return the_locking_inset->getLyXText(bv, true);
2148 return cached_text.get();
2149 } else if (it->second.remove) {
2151 saveLyXTextState(it->second.text.get());
2157 // when we have to reinit the existing LyXText!
2159 it->second.text->init(bv);
2160 restoreLyXTextState(bv, it->second.text.get());
2161 it->second.remove = false;
2163 cached_text = it->second.text;
2164 if (the_locking_inset && recursive) {
2165 return the_locking_inset->getLyXText(bv);
2167 return cached_text.get();
2170 // we are here only if we don't have a BufferView * in the cache!!!
2172 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2173 cached_text->init(bv);
2174 restoreLyXTextState(bv, cached_text.get());
2176 cache.insert(make_pair(bv, cached_text));
2178 if (the_locking_inset && recursive) {
2179 return the_locking_inset->getLyXText(bv);
2181 return cached_text.get();
2185 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2189 Cache::iterator it = cache.find(bv);
2191 if (it == cache.end()) {
2195 lyx::Assert(it->second.text.get());
2197 it->second.remove = true;
2199 /// then remove all LyXText in text-insets
2200 Paragraph * p = par;
2201 for (; p; p = p->next()) {
2202 p->deleteInsetsLyXText(bv);
2208 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2211 // we cannot resize this because we are in use!
2212 // so do this on the next possible getLyXText()
2217 // lyxerr << "InsetText::resizeLyXText\n";
2218 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2219 // we have to do this as a fixed width may have changed!
2220 LyXText * t = getLyXText(bv);
2221 saveLyXTextState(t);
2223 restoreLyXTextState(bv, t);
2226 // one endless line, resize normally not necessary
2227 if (!force && getMaxWidth(bv, this) < 0)
2230 Cache::iterator it = cache.find(bv);
2231 if (it == cache.end()) {
2234 lyx::Assert(it->second.text.get());
2236 LyXText * t = it->second.text.get();
2237 saveLyXTextState(t);
2238 for (Paragraph * p = par; p; p = p->next()) {
2239 p->resizeInsetsLyXText(bv);
2242 restoreLyXTextState(bv, t);
2243 if (the_locking_inset) {
2244 inset_x = cx(bv) - top_x + drawTextXOffset;
2245 inset_y = cy(bv) + drawTextYOffset;
2249 t->first = bv->screen()->topCursorVisible(t);
2252 updateLocal(bv, FULL, false);
2253 // this will scroll the screen such that the cursor becomes visible
2254 bv->updateScrollbar();
2256 need_update |= FULL;
2261 void InsetText::reinitLyXText() const
2264 // we cannot resize this because we are in use!
2265 // so do this on the next possible getLyXText()
2271 // lyxerr << "InsetText::reinitLyXText\n";
2272 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2273 lyx::Assert(it->second.text.get());
2275 LyXText * t = it->second.text.get();
2276 BufferView * bv = it->first;
2278 saveLyXTextState(t);
2279 for (Paragraph * p = par; p; p = p->next()) {
2280 p->resizeInsetsLyXText(bv);
2283 restoreLyXTextState(bv, t);
2284 if (the_locking_inset) {
2285 inset_x = cx(bv) - top_x + drawTextXOffset;
2286 inset_y = cy(bv) + drawTextYOffset;
2289 t->first = bv->screen()->topCursorVisible(t);
2292 updateLocal(bv, FULL, false);
2293 // this will scroll the screen such that the cursor becomes visible
2294 bv->updateScrollbar();
2302 void InsetText::removeNewlines()
2304 bool changed = false;
2306 for (Paragraph * p = par; p; p = p->next()) {
2307 for (int i = 0; i < p->size(); ++i) {
2308 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2319 bool InsetText::nodraw() const
2321 if (the_locking_inset)
2322 return the_locking_inset->nodraw();
2323 return UpdatableInset::nodraw();
2327 int InsetText::scroll(bool recursive) const
2329 int sx = UpdatableInset::scroll(false);
2331 if (recursive && the_locking_inset)
2332 sx += the_locking_inset->scroll(recursive);
2338 bool InsetText::doClearArea() const
2340 return !locked || (need_update & (FULL|INIT));
2344 void InsetText::selectAll(BufferView * bv)
2346 getLyXText(bv)->cursorTop(bv);
2347 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2348 getLyXText(bv)->cursorBottom(bv);
2349 getLyXText(bv)->setSelection(bv);
2353 void InsetText::clearSelection(BufferView * bv)
2355 getLyXText(bv)->clearSelection();
2359 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2361 Painter & pain = bv->painter();
2363 int h = insetAscent + insetDescent;
2364 int ty = baseline - insetAscent;
2370 if ((ty + h) > pain.paperHeight())
2371 h = pain.paperHeight();
2372 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2373 w = pain.paperWidth();
2374 // w -= TEXT_TO_INSET_OFFSET;
2375 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2378 frame_is_visible = false;
2382 Paragraph * InsetText::getParFromID(int id) const
2385 Paragraph * result = par;
2386 Paragraph * ires = 0;
2387 while (result && result->id() != id) {
2388 if ((ires = result->getParFromID(id)))
2390 result = result->next();
2394 Paragraph * tmp = par;
2396 if (tmp->id() == id) {
2399 Paragraph * tmp2 = tmp->getParFromID(id);
2410 Paragraph * InsetText::firstParagraph() const
2413 if (the_locking_inset)
2414 if ((result = the_locking_inset->firstParagraph()))
2420 Paragraph * InsetText::getFirstParagraph(int i) const
2422 return (i == 0) ? par : 0;
2426 LyXCursor const & InsetText::cursor(BufferView * bv) const
2428 if (the_locking_inset)
2429 return the_locking_inset->cursor(bv);
2430 return getLyXText(bv)->cursor;
2434 Paragraph * InsetText::paragraph() const
2440 void InsetText::paragraph(Paragraph * p)
2442 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2443 // caller of this function has to take care of it. This IS important
2444 // as we could have to insert a paragraph before this one and just
2445 // link the actual to a new ones next and set it with this function
2448 // set ourself as owner for all the paragraphs inserted!
2449 Paragraph * np = par;
2451 np->setInsetOwner(this);
2455 // redraw myself when asked for
2460 Inset * InsetText::getInsetFromID(int id_arg) const
2463 return const_cast<InsetText *>(this);
2465 Paragraph * lp = par;
2468 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2469 en = lp->inset_iterator_end();
2472 if ((*it)->id() == id_arg)
2474 Inset * in = (*it)->getInsetFromID(id_arg);
2484 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2490 lt = getLyXText(bv);
2493 if (the_locking_inset) {
2494 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2501 // we have to go on checking so move cusor to the next char
2502 lt->cursor.pos(lt->cursor.pos() + 1);
2504 str = lt->selectNextWordToSpellcheck(bv, value);
2506 bv->unlockInset(const_cast<InsetText *>(this));
2515 void InsetText::selectSelectedWord(BufferView * bv)
2517 if (the_locking_inset) {
2518 the_locking_inset->selectSelectedWord(bv);
2521 getLyXText(bv)->selectSelectedWord(bv);
2522 updateLocal(bv, SELECTION, false);
2526 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2528 if (the_locking_inset) {
2529 the_locking_inset->toggleSelection(bv, kill_selection);
2533 lt = getLyXText(bv);
2537 int x = top_x + TEXT_TO_INSET_OFFSET;
2539 Row * row = lt->firstRow();
2540 int y_offset = top_baseline - row->ascent_of_text();
2542 while ((row != 0) && ((y+row->height()) <= 0)) {
2549 if (need_update & SELECTION)
2551 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2557 bool InsetText::searchForward(BufferView * bv, string const & str,
2560 if (the_locking_inset) {
2561 if (the_locking_inset->searchForward(bv, str, cs, mw))
2565 lt = getLyXText(bv);
2568 Paragraph * lpar = lt->cursor.par();
2569 pos_type pos = lt->cursor.pos();
2570 if (pos < lpar->size() - 1)
2574 lpar = lpar->next();
2579 // we have to unlock ourself in this function by default!
2580 bv->unlockInset(const_cast<InsetText *>(this));
2583 lt->setCursor(bv, lpar, pos);
2587 if (LyXFind(bv, str, true, true, cs , mw)) {
2590 // we have to unlock ourself in this function by default!
2591 bv->unlockInset(const_cast<InsetText *>(this));
2595 bool InsetText::searchBackward(BufferView * bv, string const & str,
2598 if (the_locking_inset)
2599 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2601 if (LyXFind(bv, str, false, true, cs, mw)) {
2604 // we have to unlock ourself in this function by default!
2605 bv->unlockInset(const_cast<InsetText *>(this));
2610 bool InsetText::checkInsertChar(LyXFont & font)
2613 return owner()->checkInsertChar(font);
2618 void InsetText::collapseParagraphs(BufferParams const & bparams) const
2620 while(par->next()) {
2621 if (!par->isSeparator(par->size()-1))
2622 par->insertChar(par->size()-1, ' ');
2623 par->pasteParagraph(bparams);