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"
41 #include "ParagraphParameters.h"
42 #include "undo_funcs.h"
45 #include "frontends/Alert.h"
46 #include "frontends/Dialogs.h"
48 #include "support/textutils.h"
49 #include "support/LAssert.h"
50 #include "support/lstrings.h"
51 #include "support/lyxalgo.h" // lyx::count
68 using lyx::textclass_type;
70 extern unsigned char getCurrentTextClass(Buffer *);
71 extern bool math_insert_greek(BufferView *, char);
72 extern int greek_kb_flag;
75 // These functions should probably go into bufferview_funcs somehow (Jug)
77 void InsetText::saveLyXTextState(LyXText * t) const
79 // check if my paragraphs are still valid
82 if (p == t->cursor.par())
87 if (p && t->cursor.pos() <= p->size()) {
88 sstate.lpar = t->cursor.par();
89 sstate.pos = t->cursor.pos();
90 sstate.boundary = t->cursor.boundary();
91 sstate.selstartpar = t->selection.start.par();
92 sstate.selstartpos = t->selection.start.pos();
93 sstate.selstartboundary = t->selection.start.boundary();
94 sstate.selendpar = t->selection.end.par();
95 sstate.selendpos = t->selection.end.pos();
96 sstate.selendboundary = t->selection.end.boundary();
97 sstate.selection = t->selection.set();
98 sstate.mark_set = t->selection.mark();
99 sstate.refresh = t->refresh_row != 0;
105 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
108 t->selection.set(true);
109 /* at this point just to avoid the Delete-Empty-Paragraph
110 * Mechanism when setting the cursor */
111 t->selection.mark(sstate.mark_set);
112 if (sstate.selection) {
113 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
114 true, sstate.selstartboundary);
115 t->selection.cursor = t->cursor;
116 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
117 true, sstate.selendboundary);
119 t->setCursor(bv, sstate.lpar, sstate.pos);
121 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
122 t->selection.cursor = t->cursor;
123 t->selection.set(false);
125 if (sstate.refresh) {
131 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
138 InsetText::InsetText(BufferParams const & bp)
139 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
143 par->layout(textclasslist[bp.textclass].defaultLayoutName());
149 InsetText::InsetText(InsetText const & in, bool same_id)
150 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
158 InsetText & InsetText::operator=(InsetText const & it)
165 void InsetText::init(InsetText const * ins, bool same_id)
168 setParagraphData(ins->par, same_id);
169 autoBreakRows = ins->autoBreakRows;
170 drawFrame_ = ins->drawFrame_;
171 frame_color = ins->frame_color;
177 p->setInsetOwner(this);
180 the_locking_inset = 0;
182 frame_color = LColor::insetframe;
183 autoBreakRows = false;
190 no_selection = false;
197 last_drawn_width = -1;
198 frame_is_visible = false;
201 in_insetAllowed = false;
205 InsetText::~InsetText()
212 Paragraph * tmp = par->next();
219 void InsetText::clear()
221 // This is a gross hack...
222 string old_layout = par->layout();
225 Paragraph * tmp = par->next();
230 par->setInsetOwner(this);
231 par->layout(old_layout);
238 Inset * InsetText::clone(Buffer const &, bool same_id) const
240 return new InsetText(*this, same_id);
244 void InsetText::write(Buffer const * buf, ostream & os) const
247 writeParagraphData(buf, os);
251 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
253 par->writeFile(buf, os, buf->params, 0);
257 void InsetText::read(Buffer const * buf, LyXLex & lex)
261 Paragraph * return_par = 0;
262 Paragraph::depth_type depth = 0;
263 LyXFont font(LyXFont::ALL_INHERIT);
269 token = lex.getString();
272 if (token == "\\end_inset") {
273 #ifndef NO_COMPABILITY
274 const_cast<Buffer*>(buf)->insertErtContents(par, pos, false);
279 if (const_cast<Buffer*>(buf)->
280 parseSingleLyXformat2Token(lex, par, return_par,
281 token, pos, depth, font)) {
282 // the_end read this should NEVER happen
283 lex.printError("\\the_end read in inset! Error in document!");
291 return_par->setInsetOwner(this);
292 return_par = return_par->next();
295 if (token != "\\end_inset") {
296 lex.printError("Missing \\end_inset at this point. "
303 int InsetText::ascent(BufferView * bv, LyXFont const &) const
305 insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
306 TEXT_TO_INSET_OFFSET;
311 int InsetText::descent(BufferView * bv, LyXFont const &) const
313 LyXText * llt = getLyXText(bv);
314 insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
315 TEXT_TO_INSET_OFFSET;
320 int InsetText::width(BufferView * bv, LyXFont const &) const
322 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
323 (2 * TEXT_TO_INSET_OFFSET);
324 insetWidth = max(insetWidth, 10);
329 int InsetText::textWidth(BufferView * bv, bool fordraw) const
332 if (!autoBreakRows) {
335 w = getMaxWidth(bv, this);
338 return max(w - (2 * TEXT_TO_INSET_OFFSET),
339 (int)getLyXText(bv)->width);
343 return w - (2 * TEXT_TO_INSET_OFFSET);
347 void InsetText::draw(BufferView * bv, LyXFont const & f,
348 int baseline, float & x, bool cleared) const
353 Painter & pain = bv->painter();
355 // this is the first thing we have to ask because if the x pos
356 // changed we have to do a complete rebreak of the text as we
357 // may have few space to draw in. Well we should check on this too
359 if (top_x != int(x)) {
362 int nw = getMaxWidth(bv, this);
363 if (nw > 0 && old_max_width != nw) {
366 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
371 // call these methods so that insetWidth, insetAscent and
372 // insetDescent have the right values.
377 // repaint the background if needed
378 if (cleared && backgroundColor() != LColor::background) {
379 clearInset(bv, baseline, cleared);
382 // no draw is necessary !!!
383 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
384 top_baseline = baseline;
386 if (need_update & CLEAR_FRAME)
387 clearFrame(pain, cleared);
394 x += static_cast<float>(scroll());
396 // if top_x differs we did it already
397 if (!cleared && (old_x == int(x))
398 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
399 ||(last_drawn_width != insetWidth)))
401 // Condition necessary to eliminate bug 59 attachment 37
403 clearInset(bv, baseline, cleared);
407 frame_is_visible = false;
409 if (!cleared && (need_update == NONE)) {
411 drawFrame(pain, cleared);
415 top_baseline = baseline;
416 top_y = baseline - insetAscent;
418 if (last_drawn_width != insetWidth) {
420 clearInset(bv, baseline, cleared);
422 last_drawn_width = insetWidth;
425 if (the_locking_inset && (cpar(bv) == inset_par)
426 && (cpos(bv) == inset_pos)) {
427 inset_x = cx(bv) - top_x + drawTextXOffset;
428 inset_y = cy(bv) + drawTextYOffset;
430 if (!cleared && (need_update == CURSOR)
431 && !getLyXText(bv)->selection.set()) {
432 drawFrame(pain, cleared);
442 x += TEXT_TO_INSET_OFFSET;
444 Row * row = lt->firstRow();
445 int y_offset = baseline - row->ascent_of_text();
446 int ph = pain.paperHeight();
449 while ((row != 0) && ((y+row->height()) <= 0)) {
451 first += row->height();
457 if (cleared || (need_update&(INIT|FULL))) {
460 while ((row != 0) && (yf < ph)) {
461 lt->getVisibleRow(bv, y+y_offset, int(x), row,
467 } else if (!locked) {
468 if (need_update & CURSOR) {
469 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
470 lt->clearSelection();
471 lt->selection.cursor = lt->cursor;
473 bv->screen()->update(lt, bv, y_offset, int(x));
476 if (need_update & SELECTION) {
477 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
478 } else if (need_update & CURSOR) {
479 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
480 lt->clearSelection();
481 lt->selection.cursor = lt->cursor;
483 bv->screen()->update(lt, bv, y_offset, int(x));
488 lt->status(bv, LyXText::UNCHANGED);
489 if ((drawFrame_ == ALWAYS) ||
490 ((cleared || (need_update != CURSOR_PAR)) &&
491 (drawFrame_ == LOCKED) && locked))
493 drawFrame(pain, cleared);
494 } else if (need_update & CLEAR_FRAME) {
495 clearFrame(pain, cleared);
498 x += insetWidth - TEXT_TO_INSET_OFFSET;
500 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
502 } else if (need_update != INIT) {
510 void InsetText::drawFrame(Painter & pain, bool cleared) const
512 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
513 if (!frame_is_visible || cleared) {
514 frame_x = top_x + ttoD2;
515 frame_y = top_baseline - insetAscent + ttoD2;
516 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
517 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
518 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
520 frame_is_visible = true;
525 void InsetText::clearFrame(Painter & pain, bool cleared) const
527 if (frame_is_visible) {
529 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
532 frame_is_visible = false;
537 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
540 if (reinit && owner()) {
542 owner()->update(bv, font, true);
547 if (reinit || need_update == INIT) {
549 // we should put this call where we set need_update to INIT!
552 owner()->update(bv, font, true);
556 if (the_locking_inset) {
557 inset_x = cx(bv) - top_x + drawTextXOffset;
558 inset_y = cy(bv) + drawTextYOffset;
559 the_locking_inset->update(bv, font, reinit);
567 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
570 lt->updateInset(bv, the_locking_inset);
572 if (lt->status() == LyXText::NEED_MORE_REFRESH)
580 void InsetText::setUpdateStatus(BufferView * bv, int what) const
582 // this does nothing dangerous so use only a localized buffer
583 LyXText * llt = getLyXText(bv);
586 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
587 // if we don't break row so that we only have one row to update!
588 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
590 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
593 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
594 need_update |= CURSOR_PAR;
597 // this to not draw a selection when we redraw all of it!
598 if (need_update & CURSOR && !(need_update & SELECTION)) {
599 if (llt->selection.set())
601 llt->clearSelection();
606 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
608 if (!autoBreakRows && par->next())
609 collapseParagraphs(bv->buffer()->params);
616 setUpdateStatus(bv, what);
617 bool flag = (((need_update != CURSOR) && (need_update != NONE)) ||
618 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
619 if (!lt->selection.set())
620 lt->selection.cursor = lt->cursor;
624 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
628 if (need_update == CURSOR)
630 bv->owner()->showState();
631 bv->owner()->updateMenubar();
632 bv->owner()->updateToolbar();
633 if (old_par != cpar(bv)) {
634 bv->owner()->setLayout(cpar(bv)->layout());
640 string const InsetText::editMessage() const
642 return _("Opened Text Inset");
646 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
648 UpdatableInset::edit(bv, x, y, button);
650 if (!bv->lockInset(this)) {
651 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
655 the_locking_inset = 0;
656 inset_pos = inset_x = inset_y = 0;
657 inset_boundary = false;
660 int tmp_y = (y < 0) ? 0 : y;
666 if (!checkAndActivateInset(bv, x, tmp_y, button))
667 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
669 lt->clearSelection();
671 // If the inset is empty set the language of the current font to the
672 // language to the surronding text (if different).
673 if (par->size() == 0 && !par->next() &&
674 bv->getParentLanguage(this) != lt->current_font.language())
676 LyXFont font(LyXFont::ALL_IGNORE);
677 font.setLanguage(bv->getParentLanguage(this));
678 setFont(bv, font, false);
680 // showInsetCursor(bv);
685 if (drawFrame_ == LOCKED)
686 code = CURSOR|DRAW_FRAME;
687 updateLocal(bv, code, false);
689 // Tell the paragraph dialog that we've entered an insettext.
690 bv->owner()->getDialogs()->updateParagraph();
694 void InsetText::edit(BufferView * bv, bool front)
696 UpdatableInset::edit(bv, front);
698 if (!bv->lockInset(this)) {
699 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
703 the_locking_inset = 0;
704 inset_pos = inset_x = inset_y = 0;
705 inset_boundary = false;
714 lt->setCursor(bv, par, 0);
719 // int const pos = (p->size() ? p->size()-1 : p->size());
720 lt->setCursor(bv, p, p->size());
722 lt->clearSelection();
724 // If the inset is empty set the language of the current font to the
725 // language to the surronding text (if different).
726 if (par->size() == 0 && !par->next() &&
727 bv->getParentLanguage(this) != lt->current_font.language()) {
728 LyXFont font(LyXFont::ALL_IGNORE);
729 font.setLanguage(bv->getParentLanguage(this));
730 setFont(bv, font, false);
732 // showInsetCursor(bv);
736 if (drawFrame_ == LOCKED)
737 code = CURSOR|DRAW_FRAME;
738 updateLocal(bv, code, false);
742 void InsetText::insetUnlock(BufferView * bv)
744 if (the_locking_inset) {
745 the_locking_inset->insetUnlock(bv);
746 the_locking_inset = 0;
749 no_selection = false;
752 if (drawFrame_ == LOCKED)
753 code = CURSOR|CLEAR_FRAME;
761 if (lt->selection.set()) {
762 lt->clearSelection();
764 } else if (owner()) {
765 bv->owner()->setLayout(owner()->getLyXText(bv)
766 ->cursor.par()->layout());
768 bv->owner()->setLayout(bv->text->cursor.par()->layout());
769 // hack for deleteEmptyParMech
771 lt->setCursor(bv, par, 0);
772 } else if (par->next()) {
773 lt->setCursor(bv, par->next(), 0);
777 updateLocal(bv, code, false);
780 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
782 the_locking_inset = inset;
783 inset_x = cx(bv) - top_x + drawTextXOffset;
784 inset_y = cy(bv) + drawTextYOffset;
785 inset_pos = cpos(bv);
786 inset_par = cpar(bv);
787 inset_boundary = cboundary(bv);
788 updateLocal(bv, CURSOR, false);
792 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
794 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
798 if (!the_locking_inset) {
800 int const id = inset->id();
802 Paragraph::inset_iterator it =
803 p->inset_iterator_begin();
804 Paragraph::inset_iterator const end =
805 p->inset_iterator_end();
806 for (; it != end; ++it) {
807 if ((*it) == inset) {
808 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
809 lockInset(bv, inset);
812 if ((*it)->getInsetFromID(id)) {
813 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
815 return the_locking_inset->lockInsetInInset(bv, inset);
822 if (inset == cpar(bv)->getInset(cpos(bv))) {
823 lyxerr[Debug::INSETS] << "OK" << endl;
824 lockInset(bv, inset);
826 } else if (the_locking_inset && (the_locking_inset == inset)) {
827 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
828 lyxerr[Debug::INSETS] << "OK" << endl;
829 inset_x = cx(bv) - top_x + drawTextXOffset;
830 inset_y = cy(bv) + drawTextYOffset;
832 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
834 } else if (the_locking_inset) {
835 lyxerr[Debug::INSETS] << "MAYBE" << endl;
836 return the_locking_inset->lockInsetInInset(bv, inset);
838 lyxerr[Debug::INSETS] << "NOT OK" << endl;
843 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
846 if (!the_locking_inset)
848 if (the_locking_inset == inset) {
849 the_locking_inset->insetUnlock(bv);
850 getLyXText(bv)->updateInset(bv, inset);
851 the_locking_inset = 0;
853 moveRight(bv, false);
854 old_par = 0; // force layout setting
858 updateLocal(bv, CURSOR, false);
861 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
865 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
867 if (!autoBreakRows && par->next())
868 collapseParagraphs(bv->buffer()->params);
876 if (inset->owner() != this) {
877 int ustat = CURSOR_PAR;
879 UpdatableInset * tl_inset = the_locking_inset;
881 found = tl_inset->updateInsetInInset(bv, inset);
883 tl_inset = static_cast<UpdatableInset *>(inset);
884 while(tl_inset->owner() && tl_inset->owner() != this)
885 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
886 if (!tl_inset->owner())
888 found = tl_inset->updateInsetInInset(bv, inset);
892 lt->updateInset(bv, tl_inset);
896 setUpdateStatus(bv, ustat);
899 bool found = lt->updateInset(bv, inset);
903 setUpdateStatus(bv, CURSOR_PAR);
904 if (the_locking_inset &&
905 cpar(bv) == inset_par && cpos(bv) == inset_pos)
907 inset_x = cx(bv) - top_x + drawTextXOffset;
908 inset_y = cy(bv) + drawTextYOffset;
915 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
919 // use this to check mouse motion for selection!
923 int tmp_x = x - drawTextXOffset;
924 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
925 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
928 if (the_locking_inset) {
929 if (the_locking_inset == inset) {
930 the_locking_inset->insetButtonPress(bv,
934 no_selection = false;
937 // otherwise unlock the_locking_inset and lock the new inset
938 the_locking_inset->insetUnlock(bv);
939 inset_x = cx(bv) - top_x + drawTextXOffset;
940 inset_y = cy(bv) + drawTextYOffset;
941 the_locking_inset = 0;
942 inset->insetButtonPress(bv, x - inset_x,
943 y - inset_y, button);
944 // inset->edit(bv, x - inset_x, y - inset_y, button);
945 if (the_locking_inset)
946 updateLocal(bv, CURSOR, false);
947 no_selection = false;
950 // otherwise only unlock the_locking_inset
951 the_locking_inset->insetUnlock(bv);
952 the_locking_inset = 0;
954 if (bv->theLockingInset()) {
955 if (isHighlyEditableInset(inset)) {
956 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
957 inset_x = cx(bv) - top_x + drawTextXOffset;
958 inset_y = cy(bv) + drawTextYOffset;
959 inset_pos = cpos(bv);
960 inset_par = cpar(bv);
961 inset_boundary = cboundary(bv);
962 the_locking_inset = uinset;
963 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
965 uinset->edit(bv, x - inset_x, y - inset_y, 0);
966 if (the_locking_inset)
967 updateLocal(bv, CURSOR, false);
968 no_selection = false;
972 if (!inset) { // && (button == 2)) {
973 bool paste_internally = false;
974 if ((button == 2) && getLyXText(bv)->selection.set()) {
975 localDispatch(bv, LFUN_COPY, "");
976 paste_internally = true;
984 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
986 // set the selection cursor!
987 lt->selection.cursor = lt->cursor;
988 lt->cursor.x_fix(lt->cursor.x());
990 if (lt->selection.set()) {
991 lt->clearSelection();
994 updateLocal(bv, FULL, false);
996 lt->clearSelection();
999 updateLocal(bv, CURSOR, false);
1001 bv->owner()->setLayout(cpar(bv)->layout());
1003 // Insert primary selection with middle mouse
1004 // if there is a local selection in the current buffer,
1007 if (paste_internally)
1008 localDispatch(bv, LFUN_PASTE, "");
1010 localDispatch(bv, LFUN_PASTESELECTION,
1014 getLyXText(bv)->clearSelection();
1016 showInsetCursor(bv);
1017 no_selection = false;
1021 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
1023 if (the_locking_inset) {
1024 return the_locking_inset->insetButtonRelease(bv,
1025 x - inset_x, y - inset_y,
1028 int tmp_x = x - drawTextXOffset;
1029 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1030 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1033 if (isHighlyEditableInset(inset)) {
1034 ret = inset->insetButtonRelease(bv, x - inset_x,
1035 y - inset_y, button);
1037 inset_x = cx(bv) - top_x + drawTextXOffset;
1038 inset_y = cy(bv) + drawTextYOffset;
1039 ret = inset->insetButtonRelease(bv, x - inset_x,
1040 y - inset_y, button);
1041 inset->edit(bv, x - inset_x,
1042 y - inset_y, button);
1044 updateLocal(bv, CURSOR_PAR, false);
1050 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1052 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1054 if (the_locking_inset) {
1055 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1061 lt = getLyXText(bv);
1064 hideInsetCursor(bv);
1065 LyXCursor cur = lt->cursor;
1066 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1067 if (cur == lt->cursor) {
1072 lt->setSelection(bv);
1073 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1074 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1078 updateLocal(bv, SELECTION, false);
1080 showInsetCursor(bv);
1084 void InsetText::insetKeyPress(XKeyEvent * xke)
1086 if (the_locking_inset) {
1087 the_locking_inset->insetKeyPress(xke);
1093 UpdatableInset::RESULT
1094 InsetText::localDispatch(BufferView * bv,
1095 kb_action action, string const & arg)
1097 bool was_empty = par->size() == 0 && !par->next();
1098 no_selection = false;
1099 UpdatableInset::RESULT
1100 result= UpdatableInset::localDispatch(bv, action, arg);
1101 if (result != UNDISPATCHED) {
1105 result = DISPATCHED;
1106 if ((action < 0) && arg.empty())
1109 if (the_locking_inset) {
1110 result = the_locking_inset->localDispatch(bv, action, arg);
1111 if (result == DISPATCHED_NOUPDATE)
1113 else if (result == DISPATCHED) {
1114 updateLocal(bv, CURSOR_PAR, false);
1116 } else if (result >= FINISHED) {
1118 case FINISHED_RIGHT:
1119 moveRightIntern(bv, false, false);
1120 result = DISPATCHED;
1123 if ((result = moveUp(bv)) >= FINISHED) {
1124 updateLocal(bv, CURSOR, false);
1125 bv->unlockInset(this);
1129 if ((result = moveDown(bv)) >= FINISHED) {
1130 updateLocal(bv, CURSOR, false);
1131 bv->unlockInset(this);
1135 result = DISPATCHED;
1138 the_locking_inset = 0;
1139 #ifdef WITH_WARNINGS
1140 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1145 hideInsetCursor(bv);
1148 lt = getLyXText(bv);
1152 int updflag = false;
1155 case LFUN_SELFINSERT:
1156 if (bv->buffer()->isReadonly()) {
1157 // setErrorMessage(N_("Document is read only"));
1161 /* Automatically delete the currently selected
1162 * text and replace it with what is being
1163 * typed in now. Depends on lyxrc settings
1164 * "auto_region_delete", which defaults to
1167 setUndo(bv, Undo::INSERT,
1168 lt->cursor.par(), lt->cursor.par()->next());
1170 if (lyxrc.auto_region_delete) {
1171 if (lt->selection.set()) {
1172 lt->cutSelection(bv, false);
1175 lt->clearSelection();
1176 for (string::size_type i = 0; i < arg.length(); ++i) {
1177 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1180 lt->selection.cursor = lt->cursor;
1181 updwhat = CURSOR_PAR;
1183 result = DISPATCHED_NOUPDATE;
1185 // --- Cursor Movements -----------------------------------
1188 moveRight(bv, false, true);
1189 lt->setSelection(bv);
1190 updwhat = SELECTION;
1193 result = moveRight(bv);
1199 moveLeft(bv, false, true);
1200 lt->setSelection(bv);
1201 updwhat = SELECTION;
1205 result = moveLeft(bv);
1211 lt->setSelection(bv);
1212 updwhat = SELECTION;
1216 result = moveDown(bv);
1222 lt->setSelection(bv);
1223 updwhat = SELECTION;
1227 result = moveUp(bv);
1239 case LFUN_BACKSPACE: {
1240 setUndo(bv, Undo::DELETE,
1241 lt->cursor.par(), lt->cursor.par()->next());
1242 if (lt->selection.set())
1243 lt->cutSelection(bv);
1246 updwhat = CURSOR_PAR;
1252 setUndo(bv, Undo::DELETE,
1253 lt->cursor.par(), lt->cursor.par()->next());
1254 if (lt->selection.set()) {
1255 lt->cutSelection(bv);
1259 updwhat = CURSOR_PAR;
1265 setUndo(bv, Undo::DELETE,
1266 lt->cursor.par(), lt->cursor.par()->next());
1267 lt->cutSelection(bv);
1268 updwhat = CURSOR_PAR;
1275 lt->copySelection(bv);
1276 updwhat = CURSOR_PAR;
1278 case LFUN_PASTESELECTION:
1280 string const clip(bv->getClipboard());
1284 if (arg == "paragraph") {
1285 lt->insertStringAsParagraphs(bv, clip);
1287 lt->insertStringAsLines(bv, clip);
1289 updwhat = CURSOR_PAR;
1294 if (!autoBreakRows) {
1296 if (CutAndPaste::nrOfParagraphs() > 1) {
1297 Alert::alert(_("Impossible operation"),
1298 _("Cannot include more than one paragraph!"),
1303 setUndo(bv, Undo::INSERT,
1304 lt->cursor.par(), lt->cursor.par()->next());
1305 lt->pasteSelection(bv);
1306 updwhat = CURSOR_PAR;
1311 case LFUN_BREAKPARAGRAPH:
1312 if (!autoBreakRows) {
1313 result = DISPATCHED;
1316 lt->breakParagraph(bv, 0);
1320 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1321 if (!autoBreakRows) {
1322 result = DISPATCHED;
1325 lt->breakParagraph(bv, 1);
1330 case LFUN_BREAKLINE: {
1331 if (!autoBreakRows) {
1332 result = DISPATCHED;
1335 setUndo(bv, Undo::INSERT,
1336 lt->cursor.par(), lt->cursor.par()->next());
1337 lt->insertChar(bv, Paragraph::META_NEWLINE);
1338 updwhat = CURSOR_PAR;
1344 // do not set layouts on non breakable textinsets
1345 if (autoBreakRows) {
1346 string cur_layout = cpar(bv)->layout();
1348 // Derive layout number from given argument (string)
1349 // and current buffer's textclass (number). */
1350 textclass_type tclass = bv->buffer()->params.textclass;
1351 string layout = arg;
1352 bool hasLayout = textclasslist[tclass].hasLayout(layout);
1354 // If the entry is obsolete, use the new one instead.
1356 string const & obs =
1357 textclasslist[tclass][layout].
1363 // see if we found the layout number:
1365 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1366 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1370 if (cur_layout != layout) {
1371 cur_layout = layout;
1372 lt->setLayout(bv, layout);
1373 bv->owner()->setLayout(cpar(bv)->layout());
1374 updwhat = CURSOR_PAR;
1378 // reset the layout box
1379 bv->owner()->setLayout(cpar(bv)->layout());
1382 case LFUN_PARAGRAPH_SPACING:
1383 // This one is absolutely not working. When fiddling with this
1384 // it also seems to me that the paragraphs inside the insettext
1385 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1387 Paragraph * par = lt->cursor.par();
1388 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1389 float cur_value = 1.0;
1390 if (cur_spacing == Spacing::Other) {
1391 cur_value = par->params().spacing().getValue();
1394 istringstream istr(arg.c_str());
1397 Spacing::Space new_spacing = cur_spacing;
1398 float new_value = cur_value;
1400 lyxerr << "Missing argument to `paragraph-spacing'"
1402 } else if (tmp == "single") {
1403 new_spacing = Spacing::Single;
1404 } else if (tmp == "onehalf") {
1405 new_spacing = Spacing::Onehalf;
1406 } else if (tmp == "double") {
1407 new_spacing = Spacing::Double;
1408 } else if (tmp == "other") {
1409 new_spacing = Spacing::Other;
1412 lyxerr << "new_value = " << tmpval << endl;
1415 } else if (tmp == "default") {
1416 new_spacing = Spacing::Default;
1418 lyxerr << _("Unknown spacing argument: ")
1421 if (cur_spacing != new_spacing || cur_value != new_value) {
1422 par->params().spacing(Spacing(new_spacing, new_value));
1423 updwhat = CURSOR_PAR;
1430 if (!bv->Dispatch(action, arg))
1431 result = UNDISPATCHED;
1438 updateLocal(bv, updwhat, updflag);
1439 /// If the action has deleted all text in the inset, we need to change the
1440 // language to the language of the surronding text.
1441 if (!was_empty && par->size() == 0 && !par->next()) {
1442 LyXFont font(LyXFont::ALL_IGNORE);
1443 font.setLanguage(bv->getParentLanguage(this));
1444 setFont(bv, font, false);
1447 if (result >= FINISHED)
1448 bv->unlockInset(this);
1453 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1456 buf->latexParagraphs(os, par, 0, texrow);
1457 return texrow.rows();
1461 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1463 Paragraph * p = par;
1464 unsigned int lines = 0;
1467 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1468 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1476 int InsetText::docbook(Buffer const * buf, ostream & os) const
1478 Paragraph * p = par;
1479 unsigned int lines = 0;
1481 vector<string> environment_stack(10);
1482 vector<string> environment_inner(10);
1484 int const command_depth = 0;
1487 Paragraph::depth_type depth = 0; // paragraph depth
1491 int desc_on = 0; // description mode
1493 LyXLayout const & style =
1494 textclasslist[buf->params.textclass][p->layout()];
1496 // environment tag closing
1497 for (; depth > p->params().depth(); --depth) {
1498 if (environment_inner[depth] != "!-- --") {
1499 item_name = "listitem";
1500 buf->sgmlCloseTag(os, command_depth + depth,
1502 if (environment_inner[depth] == "varlistentry")
1503 buf->sgmlCloseTag(os, depth+command_depth,
1504 environment_inner[depth]);
1506 buf->sgmlCloseTag(os, depth + command_depth,
1507 environment_stack[depth]);
1508 environment_stack[depth].erase();
1509 environment_inner[depth].erase();
1512 if (depth == p->params().depth()
1513 && environment_stack[depth] != style.latexname()
1514 && !environment_stack[depth].empty()) {
1515 if (environment_inner[depth] != "!-- --") {
1516 item_name= "listitem";
1517 buf->sgmlCloseTag(os, command_depth+depth,
1519 if (environment_inner[depth] == "varlistentry")
1520 buf->sgmlCloseTag(os,
1521 depth + command_depth,
1522 environment_inner[depth]);
1525 buf->sgmlCloseTag(os, depth + command_depth,
1526 environment_stack[depth]);
1528 environment_stack[depth].erase();
1529 environment_inner[depth].erase();
1532 // Write opening SGML tags.
1533 switch (style.latextype) {
1534 case LATEX_PARAGRAPH:
1535 buf->sgmlOpenTag(os, depth + command_depth,
1540 buf->sgmlError(p, 0,
1541 _("Error : LatexType Command not allowed here.\n"));
1545 case LATEX_ENVIRONMENT:
1546 case LATEX_ITEM_ENVIRONMENT:
1547 if (depth < p->params().depth()) {
1548 depth = p->params().depth();
1549 environment_stack[depth].erase();
1552 if (environment_stack[depth] != style.latexname()) {
1553 if (environment_stack.size() == depth + 1) {
1554 environment_stack.push_back("!-- --");
1555 environment_inner.push_back("!-- --");
1557 environment_stack[depth] = style.latexname();
1558 environment_inner[depth] = "!-- --";
1559 buf->sgmlOpenTag(os, depth + command_depth,
1560 environment_stack[depth]);
1562 if (environment_inner[depth] != "!-- --") {
1563 item_name= "listitem";
1564 buf->sgmlCloseTag(os,
1565 command_depth + depth,
1567 if (environment_inner[depth] == "varlistentry")
1568 buf->sgmlCloseTag(os,
1569 depth + command_depth,
1570 environment_inner[depth]);
1574 if (style.latextype == LATEX_ENVIRONMENT) {
1575 if (!style.latexparam().empty()) {
1576 if (style.latexparam() == "CDATA")
1579 buf->sgmlOpenTag(os, depth + command_depth,
1580 style.latexparam());
1585 desc_on = (style.labeltype == LABEL_MANUAL);
1588 environment_inner[depth]= "varlistentry";
1590 environment_inner[depth]= "listitem";
1592 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1593 environment_inner[depth]);
1597 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1601 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1606 buf->sgmlOpenTag(os, depth + command_depth,
1611 buf->simpleDocBookOnePar(os, p, desc_on,
1612 depth + 1 + command_depth);
1616 // write closing SGML tags
1617 switch (style.latextype) {
1618 case LATEX_ENVIRONMENT:
1619 if (!style.latexparam().empty()) {
1620 if (style.latexparam() == "CDATA")
1623 buf->sgmlCloseTag(os, depth + command_depth,
1624 style.latexparam());
1627 case LATEX_ITEM_ENVIRONMENT:
1628 if (desc_on == 1) break;
1630 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1632 case LATEX_PARAGRAPH:
1633 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1636 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1642 for (int d = depth; d >= 0; --d) {
1643 if (!environment_stack[depth].empty()) {
1644 if (environment_inner[depth] != "!-- --") {
1645 item_name = "listitem";
1646 buf->sgmlCloseTag(os, command_depth + depth,
1648 if (environment_inner[depth] == "varlistentry")
1649 buf->sgmlCloseTag(os, depth + command_depth,
1650 environment_inner[depth]);
1653 buf->sgmlCloseTag(os, depth + command_depth,
1654 environment_stack[depth]);
1662 void InsetText::validate(LaTeXFeatures & features) const
1664 Paragraph * p = par;
1666 p->validate(features);
1672 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1674 if (textclasslist[buf->params.textclass][p->layout()].labeltype != LABEL_MANUAL)
1677 return p->beginningOfMainBody();
1681 void InsetText::getCursorPos(BufferView * bv,
1682 int & x, int & y) const
1684 if (the_locking_inset) {
1685 the_locking_inset->getCursorPos(bv, x, y);
1693 unsigned int InsetText::insetInInsetY()
1695 if (!the_locking_inset)
1698 return (inset_y + the_locking_inset->insetInInsetY());
1702 void InsetText::toggleInsetCursor(BufferView * bv)
1704 if (the_locking_inset) {
1705 the_locking_inset->toggleInsetCursor(bv);
1709 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1711 int const asc = lyxfont::maxAscent(font);
1712 int const desc = lyxfont::maxDescent(font);
1714 if (isCursorVisible())
1715 bv->hideLockedInsetCursor();
1717 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1718 toggleCursorVisible();
1722 void InsetText::showInsetCursor(BufferView * bv, bool show)
1724 if (the_locking_inset) {
1725 the_locking_inset->showInsetCursor(bv, show);
1728 if (!isCursorVisible()) {
1729 LyXFont const font =
1730 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1732 int const asc = lyxfont::maxAscent(font);
1733 int const desc = lyxfont::maxDescent(font);
1735 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1737 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1738 setCursorVisible(true);
1743 void InsetText::hideInsetCursor(BufferView * bv)
1745 if (isCursorVisible()) {
1746 bv->hideLockedInsetCursor();
1747 setCursorVisible(false);
1749 if (the_locking_inset)
1750 the_locking_inset->hideInsetCursor(bv);
1754 void InsetText::fitInsetCursor(BufferView * bv) const
1756 if (the_locking_inset) {
1757 the_locking_inset->fitInsetCursor(bv);
1760 LyXFont const font =
1761 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1763 int const asc = lyxfont::maxAscent(font);
1764 int const desc = lyxfont::maxDescent(font);
1766 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1770 UpdatableInset::RESULT
1771 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1773 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1774 return moveLeftIntern(bv, false, activate_inset, selecting);
1776 return moveRightIntern(bv, false, activate_inset, selecting);
1780 UpdatableInset::RESULT
1781 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1783 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1784 return moveRightIntern(bv, true, activate_inset, selecting);
1786 return moveLeftIntern(bv, true, activate_inset, selecting);
1790 UpdatableInset::RESULT
1791 InsetText::moveRightIntern(BufferView * bv, bool behind,
1792 bool activate_inset, bool selecting)
1794 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1795 return FINISHED_RIGHT;
1796 if (activate_inset && checkAndActivateInset(bv, behind))
1798 getLyXText(bv)->cursorRight(bv);
1800 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1801 return DISPATCHED_NOUPDATE;
1805 UpdatableInset::RESULT
1806 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1807 bool activate_inset, bool selecting)
1809 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1811 getLyXText(bv)->cursorLeft(bv);
1813 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1814 if (activate_inset && checkAndActivateInset(bv, behind))
1816 return DISPATCHED_NOUPDATE;
1820 UpdatableInset::RESULT
1821 InsetText::moveUp(BufferView * bv)
1823 if (!crow(bv)->previous())
1825 getLyXText(bv)->cursorUp(bv);
1826 return DISPATCHED_NOUPDATE;
1830 UpdatableInset::RESULT
1831 InsetText::moveDown(BufferView * bv)
1833 if (!crow(bv)->next())
1834 return FINISHED_DOWN;
1835 getLyXText(bv)->cursorDown(bv);
1836 return DISPATCHED_NOUPDATE;
1840 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1842 if (the_locking_inset) {
1843 if (the_locking_inset->insetAllowed(inset))
1844 return the_locking_inset->insertInset(bv, inset);
1849 lt = getLyXText(bv);
1852 setUndo(bv, Undo::FINISH, lt->cursor.par(), lt->cursor.par()->next());
1854 inset->setOwner(this);
1855 hideInsetCursor(bv);
1856 lt->insertInset(bv, inset);
1860 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1866 bool InsetText::insetAllowed(Inset::Code code) const
1868 // in_insetAllowed is a really gross hack,
1869 // to allow us to call the owner's insetAllowed
1870 // without stack overflow, which can happen
1871 // when the owner uses InsetCollapsable::insetAllowed()
1873 if (in_insetAllowed)
1875 in_insetAllowed = true;
1876 if (the_locking_inset)
1877 ret = the_locking_inset->insetAllowed(code);
1879 ret = owner()->insetAllowed(code);
1880 in_insetAllowed = false;
1885 UpdatableInset * InsetText::getLockingInset() const
1887 return the_locking_inset ? the_locking_inset->getLockingInset() :
1888 const_cast<InsetText *>(this);
1892 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1896 if (the_locking_inset)
1897 return the_locking_inset->getFirstLockingInsetOfType(c);
1902 bool InsetText::showInsetDialog(BufferView * bv) const
1904 if (the_locking_inset)
1905 return the_locking_inset->showInsetDialog(bv);
1910 vector<string> const InsetText::getLabelList() const
1912 vector<string> label_list;
1914 Paragraph * tpar = par;
1916 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1917 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1918 for (; beg != end; ++beg) {
1919 vector<string> const l = (*beg)->getLabelList();
1920 label_list.insert(label_list.end(), l.begin(), l.end());
1922 tpar = tpar->next();
1928 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1931 if (the_locking_inset) {
1932 the_locking_inset->setFont(bv, font, toggleall, selectall);
1935 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1936 getLyXText(bv)->setFont(bv, font, toggleall);
1941 lt = getLyXText(bv);
1944 if (lt->selection.set()) {
1945 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1949 lt->toggleFree(bv, font, toggleall);
1951 lt->clearSelection();
1953 bool flag = (selectall || lt->selection.set());
1957 updateLocal(bv, FULL, true);
1959 updateLocal(bv, CURSOR_PAR, true);
1963 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1965 if (cpar(bv)->isInset(cpos(bv))) {
1969 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1970 if (!isHighlyEditableInset(inset))
1972 LyXFont const font =
1973 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1975 x = inset->width(bv, font);
1976 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1979 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1981 //inset_x = cx(bv) - top_x + drawTextXOffset;
1982 //inset_y = cy(bv) + drawTextYOffset;
1983 inset->edit(bv, x, y, 0);
1984 if (!the_locking_inset)
1986 updateLocal(bv, CURSOR, false);
1993 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1996 x -= drawTextXOffset;
1998 int dummyy = y + insetAscent;
1999 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
2006 inset_x = cx(bv) - top_x + drawTextXOffset;
2007 inset_y = cy(bv) + drawTextYOffset;
2008 inset->edit(bv, x - inset_x, y - inset_y, button);
2009 if (!the_locking_inset)
2011 updateLocal(bv, CURSOR, false);
2018 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2021 int w = UpdatableInset::getMaxWidth(bv, inset);
2026 w = w - top_x + owner()->x();
2029 w -= (2 * TEXT_TO_INSET_OFFSET);
2032 return UpdatableInset::getMaxWidth(bv, inset);
2037 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2039 // we have to unlock any locked inset otherwise we're in troubles
2040 the_locking_inset = 0;
2042 Paragraph * tmp = par->next();
2047 par = new Paragraph(*p, same_id);
2048 par->setInsetOwner(this);
2049 Paragraph * np = par;
2052 np->next(new Paragraph(*p, same_id));
2053 np->next()->previous(np);
2055 np->setInsetOwner(this);
2062 void InsetText::setText(string const & data)
2065 LyXFont font(LyXFont::ALL_SANE);
2066 for (unsigned int i=0; i < data.length(); ++i)
2067 par->insertChar(i, data[i], font);
2071 void InsetText::setAutoBreakRows(bool flag)
2073 if (flag != autoBreakRows) {
2074 autoBreakRows = flag;
2082 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2084 if (how != drawFrame_) {
2087 updateLocal(bv, DRAW_FRAME, false);
2092 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2094 if (frame_color != col) {
2097 updateLocal(bv, DRAW_FRAME, false);
2102 int InsetText::cx(BufferView * bv) const
2104 // we do nothing dangerous so we use a local cache
2105 LyXText * llt = getLyXText(bv);
2106 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2107 if (the_locking_inset) {
2108 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2110 if (font.isVisibleRightToLeft())
2111 x -= the_locking_inset->width(bv, font);
2117 int InsetText::cy(BufferView * bv) const
2120 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2124 pos_type InsetText::cpos(BufferView * bv) const
2126 return getLyXText(bv)->cursor.pos();
2130 Paragraph * InsetText::cpar(BufferView * bv) const
2132 return getLyXText(bv)->cursor.par();
2136 bool InsetText::cboundary(BufferView * bv) const
2138 return getLyXText(bv)->cursor.boundary();
2142 Row * InsetText::crow(BufferView * bv) const
2144 return getLyXText(bv)->cursor.row();
2148 LyXText * InsetText::getLyXText(BufferView const * lbv,
2149 bool const recursive) const
2151 if (cached_bview == lbv) {
2152 if (recursive && the_locking_inset)
2153 return the_locking_inset->getLyXText(lbv, true);
2154 LyXText * lt = cached_text.get();
2155 lyx::Assert(lt && lt->firstRow()->par() == par);
2158 // Super UGLY! (Lgb)
2159 BufferView * bv = const_cast<BufferView *>(lbv);
2162 Cache::iterator it = cache.find(bv);
2164 if (it != cache.end()) {
2167 } else if (do_resize) {
2168 resizeLyXText(do_resize);
2170 if (lt || !it->second.remove) {
2171 lyx::Assert(it->second.text.get());
2172 cached_text = it->second.text;
2173 if (recursive && the_locking_inset) {
2174 return the_locking_inset->getLyXText(bv, true);
2176 return cached_text.get();
2177 } else if (it->second.remove) {
2179 saveLyXTextState(it->second.text.get());
2185 // when we have to reinit the existing LyXText!
2187 it->second.text->init(bv);
2188 restoreLyXTextState(bv, it->second.text.get());
2189 it->second.remove = false;
2191 cached_text = it->second.text;
2192 if (the_locking_inset && recursive) {
2193 return the_locking_inset->getLyXText(bv);
2195 return cached_text.get();
2198 // we are here only if we don't have a BufferView * in the cache!!!
2200 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2201 cached_text->init(bv);
2202 restoreLyXTextState(bv, cached_text.get());
2204 cache.insert(make_pair(bv, cached_text));
2206 if (the_locking_inset && recursive) {
2207 return the_locking_inset->getLyXText(bv);
2209 return cached_text.get();
2213 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2217 Cache::iterator it = cache.find(bv);
2219 if (it == cache.end()) {
2223 lyx::Assert(it->second.text.get());
2225 it->second.remove = true;
2227 /// then remove all LyXText in text-insets
2228 Paragraph * p = par;
2229 for (; p; p = p->next()) {
2230 p->deleteInsetsLyXText(bv);
2236 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2239 // we cannot resize this because we are in use!
2240 // so do this on the next possible getLyXText()
2245 // lyxerr << "InsetText::resizeLyXText\n";
2246 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2247 // we have to do this as a fixed width may have changed!
2248 LyXText * t = getLyXText(bv);
2249 saveLyXTextState(t);
2251 restoreLyXTextState(bv, t);
2254 // one endless line, resize normally not necessary
2255 if (!force && getMaxWidth(bv, this) < 0)
2258 Cache::iterator it = cache.find(bv);
2259 if (it == cache.end()) {
2262 lyx::Assert(it->second.text.get());
2264 LyXText * t = it->second.text.get();
2265 saveLyXTextState(t);
2266 for (Paragraph * p = par; p; p = p->next()) {
2267 p->resizeInsetsLyXText(bv);
2270 restoreLyXTextState(bv, t);
2271 if (the_locking_inset) {
2272 inset_x = cx(bv) - top_x + drawTextXOffset;
2273 inset_y = cy(bv) + drawTextYOffset;
2277 t->first_y = bv->screen()->topCursorVisible(t);
2280 updateLocal(bv, FULL, false);
2281 // this will scroll the screen such that the cursor becomes visible
2282 bv->updateScrollbar();
2284 need_update |= FULL;
2289 void InsetText::reinitLyXText() const
2292 // we cannot resize this because we are in use!
2293 // so do this on the next possible getLyXText()
2299 // lyxerr << "InsetText::reinitLyXText\n";
2300 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2301 lyx::Assert(it->second.text.get());
2303 LyXText * t = it->second.text.get();
2304 BufferView * bv = it->first;
2306 saveLyXTextState(t);
2307 for (Paragraph * p = par; p; p = p->next()) {
2308 p->resizeInsetsLyXText(bv);
2311 restoreLyXTextState(bv, t);
2312 if (the_locking_inset) {
2313 inset_x = cx(bv) - top_x + drawTextXOffset;
2314 inset_y = cy(bv) + drawTextYOffset;
2317 t->first_y = bv->screen()->topCursorVisible(t);
2320 updateLocal(bv, FULL, false);
2321 // this will scroll the screen such that the cursor becomes visible
2322 bv->updateScrollbar();
2330 void InsetText::removeNewlines()
2332 bool changed = false;
2334 for (Paragraph * p = par; p; p = p->next()) {
2335 for (int i = 0; i < p->size(); ++i) {
2336 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2347 bool InsetText::nodraw() const
2349 if (the_locking_inset)
2350 return the_locking_inset->nodraw();
2351 return UpdatableInset::nodraw();
2355 int InsetText::scroll(bool recursive) const
2357 int sx = UpdatableInset::scroll(false);
2359 if (recursive && the_locking_inset)
2360 sx += the_locking_inset->scroll(recursive);
2366 bool InsetText::doClearArea() const
2368 return !locked || (need_update & (FULL|INIT));
2372 void InsetText::selectAll(BufferView * bv)
2374 getLyXText(bv)->cursorTop(bv);
2375 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2376 getLyXText(bv)->cursorBottom(bv);
2377 getLyXText(bv)->setSelection(bv);
2381 void InsetText::clearSelection(BufferView * bv)
2383 getLyXText(bv)->clearSelection();
2387 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2389 Painter & pain = bv->painter();
2391 int h = insetAscent + insetDescent;
2392 int ty = baseline - insetAscent;
2398 if ((ty + h) > pain.paperHeight())
2399 h = pain.paperHeight();
2400 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2401 w = pain.paperWidth();
2402 // w -= TEXT_TO_INSET_OFFSET;
2403 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2406 frame_is_visible = false;
2410 Paragraph * InsetText::getParFromID(int id) const
2413 Paragraph * result = par;
2414 Paragraph * ires = 0;
2415 while (result && result->id() != id) {
2416 if ((ires = result->getParFromID(id)))
2418 result = result->next();
2422 Paragraph * tmp = par;
2424 if (tmp->id() == id) {
2427 Paragraph * tmp2 = tmp->getParFromID(id);
2438 Paragraph * InsetText::firstParagraph() const
2441 if (the_locking_inset)
2442 if ((result = the_locking_inset->firstParagraph()))
2448 Paragraph * InsetText::getFirstParagraph(int i) const
2450 return (i == 0) ? par : 0;
2454 LyXCursor const & InsetText::cursor(BufferView * bv) const
2456 if (the_locking_inset)
2457 return the_locking_inset->cursor(bv);
2458 return getLyXText(bv)->cursor;
2462 Paragraph * InsetText::paragraph() const
2468 void InsetText::paragraph(Paragraph * p)
2470 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2471 // caller of this function has to take care of it. This IS important
2472 // as we could have to insert a paragraph before this one and just
2473 // link the actual to a new ones next and set it with this function
2476 // set ourself as owner for all the paragraphs inserted!
2477 Paragraph * np = par;
2479 np->setInsetOwner(this);
2483 // redraw myself when asked for
2488 Inset * InsetText::getInsetFromID(int id_arg) const
2491 return const_cast<InsetText *>(this);
2493 Paragraph * lp = par;
2496 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2497 en = lp->inset_iterator_end();
2500 if ((*it)->id() == id_arg)
2502 Inset * in = (*it)->getInsetFromID(id_arg);
2512 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2518 lt = getLyXText(bv);
2521 if (the_locking_inset) {
2522 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2529 // we have to go on checking so move cusor to the next char
2530 lt->cursor.pos(lt->cursor.pos() + 1);
2532 str = lt->selectNextWordToSpellcheck(bv, value);
2534 bv->unlockInset(const_cast<InsetText *>(this));
2543 void InsetText::selectSelectedWord(BufferView * bv)
2545 if (the_locking_inset) {
2546 the_locking_inset->selectSelectedWord(bv);
2549 getLyXText(bv)->selectSelectedWord(bv);
2550 updateLocal(bv, SELECTION, false);
2554 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2556 if (the_locking_inset) {
2557 the_locking_inset->toggleSelection(bv, kill_selection);
2561 lt = getLyXText(bv);
2565 int x = top_x + TEXT_TO_INSET_OFFSET;
2567 Row * row = lt->firstRow();
2568 int y_offset = top_baseline - row->ascent_of_text();
2570 while ((row != 0) && ((y+row->height()) <= 0)) {
2577 if (need_update & SELECTION)
2579 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2585 bool InsetText::searchForward(BufferView * bv, string const & str,
2588 if (the_locking_inset) {
2589 if (the_locking_inset->searchForward(bv, str, cs, mw))
2593 lt = getLyXText(bv);
2596 Paragraph * lpar = lt->cursor.par();
2597 pos_type pos = lt->cursor.pos();
2598 if (pos < lpar->size() - 1)
2602 lpar = lpar->next();
2607 // we have to unlock ourself in this function by default!
2608 bv->unlockInset(const_cast<InsetText *>(this));
2611 lt->setCursor(bv, lpar, pos);
2615 if (LyXFind(bv, str, true, true, cs , mw)) {
2618 // we have to unlock ourself in this function by default!
2619 bv->unlockInset(const_cast<InsetText *>(this));
2623 bool InsetText::searchBackward(BufferView * bv, string const & str,
2626 if (the_locking_inset)
2627 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2629 if (LyXFind(bv, str, false, true, cs, mw)) {
2632 // we have to unlock ourself in this function by default!
2633 bv->unlockInset(const_cast<InsetText *>(this));
2638 bool InsetText::checkInsertChar(LyXFont & font)
2641 return owner()->checkInsertChar(font);
2646 void InsetText::collapseParagraphs(BufferParams const & bparams) const
2648 while(par->next()) {
2649 if (!par->isSeparator(par->size()-1))
2650 par->insertChar(par->size()-1, ' ');
2651 par->pasteParagraph(bparams);
2657 void InsetText::getDrawFont(LyXFont & font) const
2661 owner()->getDrawFont(font);
2665 void InsetText::appendParagraphs(BufferParams const & bparams,
2669 Paragraph * tmpbuf = newpar;
2670 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2672 while (tmpbuf->next()) {
2673 tmpbuf = tmpbuf->next();
2674 lastbuffer->next(new Paragraph(*tmpbuf, false));
2675 lastbuffer->next()->previous(lastbuffer);
2676 lastbuffer = lastbuffer->next();
2679 while (lastbuffer->next())
2680 lastbuffer = lastbuffer->next();
2681 if (newpar->size() && lastbuffer->size() &&
2682 !lastbuffer->isSeparator(lastbuffer->size()-1))
2684 lastbuffer->insertChar(lastbuffer->size(), ' ');
2687 // make the buf exactly the same layout than our last paragraph
2688 buf->makeSameLayout(lastbuffer);
2691 lastbuffer->next(buf);
2692 buf->previous(lastbuffer);
2693 lastbuffer->pasteParagraph(bparams);