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);
557 if (!autoBreakRows && par->next())
558 collapseParagraphs(bv->buffer()->params);
560 if (the_locking_inset) {
561 inset_x = cx(bv) - top_x + drawTextXOffset;
562 inset_y = cy(bv) + drawTextYOffset;
563 the_locking_inset->update(bv, font, reinit);
571 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
574 lt->updateInset(bv, the_locking_inset);
576 if (lt->status() == LyXText::NEED_MORE_REFRESH)
584 void InsetText::setUpdateStatus(BufferView * bv, int what) const
586 // this does nothing dangerous so use only a localized buffer
587 LyXText * llt = getLyXText(bv);
590 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
591 // if we don't break row so that we only have one row to update!
592 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
594 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
597 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
598 need_update |= CURSOR_PAR;
601 // this to not draw a selection when we redraw all of it!
602 if (need_update & CURSOR && !(need_update & SELECTION)) {
603 if (llt->selection.set())
605 llt->clearSelection();
610 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
612 if (!autoBreakRows && par->next())
613 collapseParagraphs(bv->buffer()->params);
620 setUpdateStatus(bv, what);
621 bool flag = (((need_update != CURSOR) && (need_update != NONE)) ||
622 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
623 if (!lt->selection.set())
624 lt->selection.cursor = lt->cursor;
628 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
632 if (need_update == CURSOR)
634 bv->owner()->showState();
635 bv->owner()->updateMenubar();
636 bv->owner()->updateToolbar();
637 if (old_par != cpar(bv)) {
638 bv->owner()->setLayout(cpar(bv)->layout());
644 string const InsetText::editMessage() const
646 return _("Opened Text Inset");
650 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
652 UpdatableInset::edit(bv, x, y, button);
654 if (!bv->lockInset(this)) {
655 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
659 the_locking_inset = 0;
660 inset_pos = inset_x = inset_y = 0;
661 inset_boundary = false;
664 int tmp_y = (y < 0) ? 0 : y;
670 if (!checkAndActivateInset(bv, x, tmp_y, button))
671 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
673 lt->clearSelection();
675 // If the inset is empty set the language of the current font to the
676 // language to the surronding text (if different).
677 if (par->size() == 0 && !par->next() &&
678 bv->getParentLanguage(this) != lt->current_font.language())
680 LyXFont font(LyXFont::ALL_IGNORE);
681 font.setLanguage(bv->getParentLanguage(this));
682 setFont(bv, font, false);
684 // showInsetCursor(bv);
689 if (drawFrame_ == LOCKED)
690 code = CURSOR|DRAW_FRAME;
691 updateLocal(bv, code, false);
693 // Tell the paragraph dialog that we've entered an insettext.
694 bv->owner()->getDialogs()->updateParagraph();
698 void InsetText::edit(BufferView * bv, bool front)
700 UpdatableInset::edit(bv, front);
702 if (!bv->lockInset(this)) {
703 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
707 the_locking_inset = 0;
708 inset_pos = inset_x = inset_y = 0;
709 inset_boundary = false;
718 lt->setCursor(bv, par, 0);
723 // int const pos = (p->size() ? p->size()-1 : p->size());
724 lt->setCursor(bv, p, p->size());
726 lt->clearSelection();
728 // If the inset is empty set the language of the current font to the
729 // language to the surronding text (if different).
730 if (par->size() == 0 && !par->next() &&
731 bv->getParentLanguage(this) != lt->current_font.language()) {
732 LyXFont font(LyXFont::ALL_IGNORE);
733 font.setLanguage(bv->getParentLanguage(this));
734 setFont(bv, font, false);
736 // showInsetCursor(bv);
740 if (drawFrame_ == LOCKED)
741 code = CURSOR|DRAW_FRAME;
742 updateLocal(bv, code, false);
746 void InsetText::insetUnlock(BufferView * bv)
748 if (the_locking_inset) {
749 the_locking_inset->insetUnlock(bv);
750 the_locking_inset = 0;
753 no_selection = false;
756 if (drawFrame_ == LOCKED)
757 code = CURSOR|CLEAR_FRAME;
765 if (lt->selection.set()) {
766 lt->clearSelection();
768 } else if (owner()) {
769 bv->owner()->setLayout(owner()->getLyXText(bv)
770 ->cursor.par()->layout());
772 bv->owner()->setLayout(bv->text->cursor.par()->layout());
773 // hack for deleteEmptyParMech
775 lt->setCursor(bv, par, 0);
776 } else if (par->next()) {
777 lt->setCursor(bv, par->next(), 0);
781 updateLocal(bv, code, false);
784 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
786 the_locking_inset = inset;
787 inset_x = cx(bv) - top_x + drawTextXOffset;
788 inset_y = cy(bv) + drawTextYOffset;
789 inset_pos = cpos(bv);
790 inset_par = cpar(bv);
791 inset_boundary = cboundary(bv);
792 updateLocal(bv, CURSOR, false);
796 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
798 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
802 if (!the_locking_inset) {
804 int const id = inset->id();
806 Paragraph::inset_iterator it =
807 p->inset_iterator_begin();
808 Paragraph::inset_iterator const end =
809 p->inset_iterator_end();
810 for (; it != end; ++it) {
811 if ((*it) == inset) {
812 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
813 lockInset(bv, inset);
816 if ((*it)->getInsetFromID(id)) {
817 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
819 return the_locking_inset->lockInsetInInset(bv, inset);
826 if (inset == cpar(bv)->getInset(cpos(bv))) {
827 lyxerr[Debug::INSETS] << "OK" << endl;
828 lockInset(bv, inset);
830 } else if (the_locking_inset && (the_locking_inset == inset)) {
831 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
832 lyxerr[Debug::INSETS] << "OK" << endl;
833 inset_x = cx(bv) - top_x + drawTextXOffset;
834 inset_y = cy(bv) + drawTextYOffset;
836 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
838 } else if (the_locking_inset) {
839 lyxerr[Debug::INSETS] << "MAYBE" << endl;
840 return the_locking_inset->lockInsetInInset(bv, inset);
842 lyxerr[Debug::INSETS] << "NOT OK" << endl;
847 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
850 if (!the_locking_inset)
852 if (the_locking_inset == inset) {
853 the_locking_inset->insetUnlock(bv);
854 getLyXText(bv)->updateInset(bv, inset);
855 the_locking_inset = 0;
857 moveRight(bv, false);
858 old_par = 0; // force layout setting
862 updateLocal(bv, CURSOR, false);
865 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
869 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
871 if (!autoBreakRows && par->next())
872 collapseParagraphs(bv->buffer()->params);
880 if (inset->owner() != this) {
881 int ustat = CURSOR_PAR;
883 UpdatableInset * tl_inset = the_locking_inset;
885 found = tl_inset->updateInsetInInset(bv, inset);
887 tl_inset = static_cast<UpdatableInset *>(inset);
888 while(tl_inset->owner() && tl_inset->owner() != this)
889 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
890 if (!tl_inset->owner())
892 found = tl_inset->updateInsetInInset(bv, inset);
896 lt->updateInset(bv, tl_inset);
900 setUpdateStatus(bv, ustat);
903 bool found = lt->updateInset(bv, inset);
907 setUpdateStatus(bv, CURSOR_PAR);
908 if (the_locking_inset &&
909 cpar(bv) == inset_par && cpos(bv) == inset_pos)
911 inset_x = cx(bv) - top_x + drawTextXOffset;
912 inset_y = cy(bv) + drawTextYOffset;
919 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
923 // use this to check mouse motion for selection!
927 int tmp_x = x - drawTextXOffset;
928 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
929 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
932 if (the_locking_inset) {
933 if (the_locking_inset == inset) {
934 the_locking_inset->insetButtonPress(bv,
938 no_selection = false;
941 // otherwise unlock the_locking_inset and lock the new inset
942 the_locking_inset->insetUnlock(bv);
943 inset_x = cx(bv) - top_x + drawTextXOffset;
944 inset_y = cy(bv) + drawTextYOffset;
945 the_locking_inset = 0;
946 inset->insetButtonPress(bv, x - inset_x,
947 y - inset_y, button);
948 // inset->edit(bv, x - inset_x, y - inset_y, button);
949 if (the_locking_inset)
950 updateLocal(bv, CURSOR, false);
951 no_selection = false;
954 // otherwise only unlock the_locking_inset
955 the_locking_inset->insetUnlock(bv);
956 the_locking_inset = 0;
958 if (bv->theLockingInset()) {
959 if (isHighlyEditableInset(inset)) {
960 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
961 inset_x = cx(bv) - top_x + drawTextXOffset;
962 inset_y = cy(bv) + drawTextYOffset;
963 inset_pos = cpos(bv);
964 inset_par = cpar(bv);
965 inset_boundary = cboundary(bv);
966 the_locking_inset = uinset;
967 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
969 uinset->edit(bv, x - inset_x, y - inset_y, 0);
970 if (the_locking_inset)
971 updateLocal(bv, CURSOR, false);
972 no_selection = false;
976 if (!inset) { // && (button == 2)) {
977 bool paste_internally = false;
978 if ((button == 2) && getLyXText(bv)->selection.set()) {
979 localDispatch(bv, LFUN_COPY, "");
980 paste_internally = true;
988 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
990 // set the selection cursor!
991 lt->selection.cursor = lt->cursor;
992 lt->cursor.x_fix(lt->cursor.x());
994 if (lt->selection.set()) {
995 lt->clearSelection();
998 updateLocal(bv, FULL, false);
1000 lt->clearSelection();
1003 updateLocal(bv, CURSOR, false);
1005 bv->owner()->setLayout(cpar(bv)->layout());
1007 // Insert primary selection with middle mouse
1008 // if there is a local selection in the current buffer,
1011 if (paste_internally)
1012 localDispatch(bv, LFUN_PASTE, "");
1014 localDispatch(bv, LFUN_PASTESELECTION,
1018 getLyXText(bv)->clearSelection();
1020 showInsetCursor(bv);
1021 no_selection = false;
1025 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
1027 if (the_locking_inset) {
1028 return the_locking_inset->insetButtonRelease(bv,
1029 x - inset_x, y - inset_y,
1032 int tmp_x = x - drawTextXOffset;
1033 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1034 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1037 if (isHighlyEditableInset(inset)) {
1038 ret = inset->insetButtonRelease(bv, x - inset_x,
1039 y - inset_y, button);
1041 inset_x = cx(bv) - top_x + drawTextXOffset;
1042 inset_y = cy(bv) + drawTextYOffset;
1043 ret = inset->insetButtonRelease(bv, x - inset_x,
1044 y - inset_y, button);
1045 inset->edit(bv, x - inset_x,
1046 y - inset_y, button);
1048 updateLocal(bv, CURSOR_PAR, false);
1054 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1056 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1058 if (the_locking_inset) {
1059 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1065 lt = getLyXText(bv);
1068 hideInsetCursor(bv);
1069 LyXCursor cur = lt->cursor;
1070 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1071 if (cur == lt->cursor) {
1076 lt->setSelection(bv);
1077 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1078 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1082 updateLocal(bv, SELECTION, false);
1084 showInsetCursor(bv);
1088 void InsetText::insetKeyPress(XKeyEvent * xke)
1090 if (the_locking_inset) {
1091 the_locking_inset->insetKeyPress(xke);
1097 UpdatableInset::RESULT
1098 InsetText::localDispatch(BufferView * bv,
1099 kb_action action, string const & arg)
1101 bool was_empty = par->size() == 0 && !par->next();
1102 no_selection = false;
1103 UpdatableInset::RESULT
1104 result= UpdatableInset::localDispatch(bv, action, arg);
1105 if (result != UNDISPATCHED) {
1109 result = DISPATCHED;
1110 if ((action < 0) && arg.empty())
1113 if (the_locking_inset) {
1114 result = the_locking_inset->localDispatch(bv, action, arg);
1115 if (result == DISPATCHED_NOUPDATE)
1117 else if (result == DISPATCHED) {
1118 updateLocal(bv, CURSOR_PAR, false);
1120 } else if (result >= FINISHED) {
1122 case FINISHED_RIGHT:
1123 moveRightIntern(bv, false, false);
1124 result = DISPATCHED;
1127 if ((result = moveUp(bv)) >= FINISHED) {
1128 updateLocal(bv, CURSOR, false);
1129 bv->unlockInset(this);
1133 if ((result = moveDown(bv)) >= FINISHED) {
1134 updateLocal(bv, CURSOR, false);
1135 bv->unlockInset(this);
1139 result = DISPATCHED;
1142 the_locking_inset = 0;
1143 #ifdef WITH_WARNINGS
1144 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1149 hideInsetCursor(bv);
1152 lt = getLyXText(bv);
1156 int updflag = false;
1159 case LFUN_SELFINSERT:
1160 if (bv->buffer()->isReadonly()) {
1161 // setErrorMessage(N_("Document is read only"));
1165 /* Automatically delete the currently selected
1166 * text and replace it with what is being
1167 * typed in now. Depends on lyxrc settings
1168 * "auto_region_delete", which defaults to
1171 setUndo(bv, Undo::INSERT,
1172 lt->cursor.par(), lt->cursor.par()->next());
1174 if (lyxrc.auto_region_delete) {
1175 if (lt->selection.set()) {
1176 lt->cutSelection(bv, false);
1179 lt->clearSelection();
1180 for (string::size_type i = 0; i < arg.length(); ++i) {
1181 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1184 lt->selection.cursor = lt->cursor;
1185 updwhat = CURSOR_PAR;
1187 result = DISPATCHED_NOUPDATE;
1189 // --- Cursor Movements -----------------------------------
1192 moveRight(bv, false, true);
1193 lt->setSelection(bv);
1194 updwhat = SELECTION;
1197 result = moveRight(bv);
1203 moveLeft(bv, false, true);
1204 lt->setSelection(bv);
1205 updwhat = SELECTION;
1209 result = moveLeft(bv);
1215 lt->setSelection(bv);
1216 updwhat = SELECTION;
1220 result = moveDown(bv);
1226 lt->setSelection(bv);
1227 updwhat = SELECTION;
1231 result = moveUp(bv);
1243 case LFUN_BACKSPACE: {
1244 setUndo(bv, Undo::DELETE,
1245 lt->cursor.par(), lt->cursor.par()->next());
1246 if (lt->selection.set())
1247 lt->cutSelection(bv);
1250 updwhat = CURSOR_PAR;
1256 setUndo(bv, Undo::DELETE,
1257 lt->cursor.par(), lt->cursor.par()->next());
1258 if (lt->selection.set()) {
1259 lt->cutSelection(bv);
1263 updwhat = CURSOR_PAR;
1269 setUndo(bv, Undo::DELETE,
1270 lt->cursor.par(), lt->cursor.par()->next());
1271 lt->cutSelection(bv);
1272 updwhat = CURSOR_PAR;
1279 lt->copySelection(bv);
1280 updwhat = CURSOR_PAR;
1282 case LFUN_PASTESELECTION:
1284 string const clip(bv->getClipboard());
1288 if (arg == "paragraph") {
1289 lt->insertStringAsParagraphs(bv, clip);
1291 lt->insertStringAsLines(bv, clip);
1293 updwhat = CURSOR_PAR;
1298 if (!autoBreakRows) {
1300 if (CutAndPaste::nrOfParagraphs() > 1) {
1301 Alert::alert(_("Impossible operation"),
1302 _("Cannot include more than one paragraph!"),
1307 setUndo(bv, Undo::INSERT,
1308 lt->cursor.par(), lt->cursor.par()->next());
1309 lt->pasteSelection(bv);
1310 updwhat = CURSOR_PAR;
1315 case LFUN_BREAKPARAGRAPH:
1316 if (!autoBreakRows) {
1317 result = DISPATCHED;
1320 lt->breakParagraph(bv, 0);
1324 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1325 if (!autoBreakRows) {
1326 result = DISPATCHED;
1329 lt->breakParagraph(bv, 1);
1334 case LFUN_BREAKLINE: {
1335 if (!autoBreakRows) {
1336 result = DISPATCHED;
1339 setUndo(bv, Undo::INSERT,
1340 lt->cursor.par(), lt->cursor.par()->next());
1341 lt->insertChar(bv, Paragraph::META_NEWLINE);
1342 updwhat = CURSOR_PAR;
1348 // do not set layouts on non breakable textinsets
1349 if (autoBreakRows) {
1350 string cur_layout = cpar(bv)->layout();
1352 // Derive layout number from given argument (string)
1353 // and current buffer's textclass (number). */
1354 textclass_type tclass = bv->buffer()->params.textclass;
1355 string layout = arg;
1356 bool hasLayout = textclasslist[tclass].hasLayout(layout);
1358 // If the entry is obsolete, use the new one instead.
1360 string const & obs =
1361 textclasslist[tclass][layout].
1367 // see if we found the layout number:
1369 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1370 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1374 if (cur_layout != layout) {
1375 cur_layout = layout;
1376 lt->setLayout(bv, layout);
1377 bv->owner()->setLayout(cpar(bv)->layout());
1378 updwhat = CURSOR_PAR;
1382 // reset the layout box
1383 bv->owner()->setLayout(cpar(bv)->layout());
1386 case LFUN_PARAGRAPH_SPACING:
1387 // This one is absolutely not working. When fiddling with this
1388 // it also seems to me that the paragraphs inside the insettext
1389 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1391 Paragraph * par = lt->cursor.par();
1392 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1393 float cur_value = 1.0;
1394 if (cur_spacing == Spacing::Other) {
1395 cur_value = par->params().spacing().getValue();
1398 istringstream istr(arg.c_str());
1401 Spacing::Space new_spacing = cur_spacing;
1402 float new_value = cur_value;
1404 lyxerr << "Missing argument to `paragraph-spacing'"
1406 } else if (tmp == "single") {
1407 new_spacing = Spacing::Single;
1408 } else if (tmp == "onehalf") {
1409 new_spacing = Spacing::Onehalf;
1410 } else if (tmp == "double") {
1411 new_spacing = Spacing::Double;
1412 } else if (tmp == "other") {
1413 new_spacing = Spacing::Other;
1416 lyxerr << "new_value = " << tmpval << endl;
1419 } else if (tmp == "default") {
1420 new_spacing = Spacing::Default;
1422 lyxerr << _("Unknown spacing argument: ")
1425 if (cur_spacing != new_spacing || cur_value != new_value) {
1426 par->params().spacing(Spacing(new_spacing, new_value));
1427 updwhat = CURSOR_PAR;
1434 if (!bv->Dispatch(action, arg))
1435 result = UNDISPATCHED;
1442 updateLocal(bv, updwhat, updflag);
1443 /// If the action has deleted all text in the inset, we need to change the
1444 // language to the language of the surronding text.
1445 if (!was_empty && par->size() == 0 && !par->next()) {
1446 LyXFont font(LyXFont::ALL_IGNORE);
1447 font.setLanguage(bv->getParentLanguage(this));
1448 setFont(bv, font, false);
1451 if (result >= FINISHED)
1452 bv->unlockInset(this);
1457 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1460 buf->latexParagraphs(os, par, 0, texrow);
1461 return texrow.rows();
1465 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1467 Paragraph * p = par;
1468 unsigned int lines = 0;
1471 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1472 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1480 int InsetText::docbook(Buffer const * buf, ostream & os) const
1482 Paragraph * p = par;
1483 unsigned int lines = 0;
1485 vector<string> environment_stack(10);
1486 vector<string> environment_inner(10);
1488 int const command_depth = 0;
1491 Paragraph::depth_type depth = 0; // paragraph depth
1495 int desc_on = 0; // description mode
1497 LyXLayout const & style =
1498 textclasslist[buf->params.textclass][p->layout()];
1500 // environment tag closing
1501 for (; depth > p->params().depth(); --depth) {
1502 if (environment_inner[depth] != "!-- --") {
1503 item_name = "listitem";
1504 buf->sgmlCloseTag(os, command_depth + depth,
1506 if (environment_inner[depth] == "varlistentry")
1507 buf->sgmlCloseTag(os, depth+command_depth,
1508 environment_inner[depth]);
1510 buf->sgmlCloseTag(os, depth + command_depth,
1511 environment_stack[depth]);
1512 environment_stack[depth].erase();
1513 environment_inner[depth].erase();
1516 if (depth == p->params().depth()
1517 && environment_stack[depth] != style.latexname()
1518 && !environment_stack[depth].empty()) {
1519 if (environment_inner[depth] != "!-- --") {
1520 item_name= "listitem";
1521 buf->sgmlCloseTag(os, command_depth+depth,
1523 if (environment_inner[depth] == "varlistentry")
1524 buf->sgmlCloseTag(os,
1525 depth + command_depth,
1526 environment_inner[depth]);
1529 buf->sgmlCloseTag(os, depth + command_depth,
1530 environment_stack[depth]);
1532 environment_stack[depth].erase();
1533 environment_inner[depth].erase();
1536 // Write opening SGML tags.
1537 switch (style.latextype) {
1538 case LATEX_PARAGRAPH:
1539 buf->sgmlOpenTag(os, depth + command_depth,
1544 buf->sgmlError(p, 0,
1545 _("Error : LatexType Command not allowed here.\n"));
1549 case LATEX_ENVIRONMENT:
1550 case LATEX_ITEM_ENVIRONMENT:
1551 if (depth < p->params().depth()) {
1552 depth = p->params().depth();
1553 environment_stack[depth].erase();
1556 if (environment_stack[depth] != style.latexname()) {
1557 if (environment_stack.size() == depth + 1) {
1558 environment_stack.push_back("!-- --");
1559 environment_inner.push_back("!-- --");
1561 environment_stack[depth] = style.latexname();
1562 environment_inner[depth] = "!-- --";
1563 buf->sgmlOpenTag(os, depth + command_depth,
1564 environment_stack[depth]);
1566 if (environment_inner[depth] != "!-- --") {
1567 item_name= "listitem";
1568 buf->sgmlCloseTag(os,
1569 command_depth + depth,
1571 if (environment_inner[depth] == "varlistentry")
1572 buf->sgmlCloseTag(os,
1573 depth + command_depth,
1574 environment_inner[depth]);
1578 if (style.latextype == LATEX_ENVIRONMENT) {
1579 if (!style.latexparam().empty()) {
1580 if (style.latexparam() == "CDATA")
1583 buf->sgmlOpenTag(os, depth + command_depth,
1584 style.latexparam());
1589 desc_on = (style.labeltype == LABEL_MANUAL);
1592 environment_inner[depth]= "varlistentry";
1594 environment_inner[depth]= "listitem";
1596 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1597 environment_inner[depth]);
1601 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1605 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1610 buf->sgmlOpenTag(os, depth + command_depth,
1615 buf->simpleDocBookOnePar(os, p, desc_on,
1616 depth + 1 + command_depth);
1620 // write closing SGML tags
1621 switch (style.latextype) {
1622 case LATEX_ENVIRONMENT:
1623 if (!style.latexparam().empty()) {
1624 if (style.latexparam() == "CDATA")
1627 buf->sgmlCloseTag(os, depth + command_depth,
1628 style.latexparam());
1631 case LATEX_ITEM_ENVIRONMENT:
1632 if (desc_on == 1) break;
1634 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1636 case LATEX_PARAGRAPH:
1637 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1640 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1646 for (int d = depth; d >= 0; --d) {
1647 if (!environment_stack[depth].empty()) {
1648 if (environment_inner[depth] != "!-- --") {
1649 item_name = "listitem";
1650 buf->sgmlCloseTag(os, command_depth + depth,
1652 if (environment_inner[depth] == "varlistentry")
1653 buf->sgmlCloseTag(os, depth + command_depth,
1654 environment_inner[depth]);
1657 buf->sgmlCloseTag(os, depth + command_depth,
1658 environment_stack[depth]);
1666 void InsetText::validate(LaTeXFeatures & features) const
1668 Paragraph * p = par;
1670 p->validate(features);
1676 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1678 if (textclasslist[buf->params.textclass][p->layout()].labeltype != LABEL_MANUAL)
1681 return p->beginningOfMainBody();
1685 void InsetText::getCursorPos(BufferView * bv,
1686 int & x, int & y) const
1688 if (the_locking_inset) {
1689 the_locking_inset->getCursorPos(bv, x, y);
1697 unsigned int InsetText::insetInInsetY()
1699 if (!the_locking_inset)
1702 return (inset_y + the_locking_inset->insetInInsetY());
1706 void InsetText::toggleInsetCursor(BufferView * bv)
1708 if (the_locking_inset) {
1709 the_locking_inset->toggleInsetCursor(bv);
1713 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1715 int const asc = lyxfont::maxAscent(font);
1716 int const desc = lyxfont::maxDescent(font);
1718 if (isCursorVisible())
1719 bv->hideLockedInsetCursor();
1721 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1722 toggleCursorVisible();
1726 void InsetText::showInsetCursor(BufferView * bv, bool show)
1728 if (the_locking_inset) {
1729 the_locking_inset->showInsetCursor(bv, show);
1732 if (!isCursorVisible()) {
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);
1741 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1742 setCursorVisible(true);
1747 void InsetText::hideInsetCursor(BufferView * bv)
1749 if (isCursorVisible()) {
1750 bv->hideLockedInsetCursor();
1751 setCursorVisible(false);
1753 if (the_locking_inset)
1754 the_locking_inset->hideInsetCursor(bv);
1758 void InsetText::fitInsetCursor(BufferView * bv) const
1760 if (the_locking_inset) {
1761 the_locking_inset->fitInsetCursor(bv);
1764 LyXFont const font =
1765 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1767 int const asc = lyxfont::maxAscent(font);
1768 int const desc = lyxfont::maxDescent(font);
1770 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1774 UpdatableInset::RESULT
1775 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1777 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1778 return moveLeftIntern(bv, false, activate_inset, selecting);
1780 return moveRightIntern(bv, false, activate_inset, selecting);
1784 UpdatableInset::RESULT
1785 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1787 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1788 return moveRightIntern(bv, true, activate_inset, selecting);
1790 return moveLeftIntern(bv, true, activate_inset, selecting);
1794 UpdatableInset::RESULT
1795 InsetText::moveRightIntern(BufferView * bv, bool behind,
1796 bool activate_inset, bool selecting)
1798 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1799 return FINISHED_RIGHT;
1800 if (activate_inset && checkAndActivateInset(bv, behind))
1802 getLyXText(bv)->cursorRight(bv);
1804 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1805 return DISPATCHED_NOUPDATE;
1809 UpdatableInset::RESULT
1810 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1811 bool activate_inset, bool selecting)
1813 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1815 getLyXText(bv)->cursorLeft(bv);
1817 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1818 if (activate_inset && checkAndActivateInset(bv, behind))
1820 return DISPATCHED_NOUPDATE;
1824 UpdatableInset::RESULT
1825 InsetText::moveUp(BufferView * bv)
1827 if (!crow(bv)->previous())
1829 getLyXText(bv)->cursorUp(bv);
1830 return DISPATCHED_NOUPDATE;
1834 UpdatableInset::RESULT
1835 InsetText::moveDown(BufferView * bv)
1837 if (!crow(bv)->next())
1838 return FINISHED_DOWN;
1839 getLyXText(bv)->cursorDown(bv);
1840 return DISPATCHED_NOUPDATE;
1844 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1846 if (the_locking_inset) {
1847 if (the_locking_inset->insetAllowed(inset))
1848 return the_locking_inset->insertInset(bv, inset);
1853 lt = getLyXText(bv);
1856 setUndo(bv, Undo::FINISH, lt->cursor.par(), lt->cursor.par()->next());
1858 inset->setOwner(this);
1859 hideInsetCursor(bv);
1860 lt->insertInset(bv, inset);
1864 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1870 bool InsetText::insetAllowed(Inset::Code code) const
1872 // in_insetAllowed is a really gross hack,
1873 // to allow us to call the owner's insetAllowed
1874 // without stack overflow, which can happen
1875 // when the owner uses InsetCollapsable::insetAllowed()
1877 if (in_insetAllowed)
1879 in_insetAllowed = true;
1880 if (the_locking_inset)
1881 ret = the_locking_inset->insetAllowed(code);
1883 ret = owner()->insetAllowed(code);
1884 in_insetAllowed = false;
1889 UpdatableInset * InsetText::getLockingInset() const
1891 return the_locking_inset ? the_locking_inset->getLockingInset() :
1892 const_cast<InsetText *>(this);
1896 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1900 if (the_locking_inset)
1901 return the_locking_inset->getFirstLockingInsetOfType(c);
1906 bool InsetText::showInsetDialog(BufferView * bv) const
1908 if (the_locking_inset)
1909 return the_locking_inset->showInsetDialog(bv);
1914 vector<string> const InsetText::getLabelList() const
1916 vector<string> label_list;
1918 Paragraph * tpar = par;
1920 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1921 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1922 for (; beg != end; ++beg) {
1923 vector<string> const l = (*beg)->getLabelList();
1924 label_list.insert(label_list.end(), l.begin(), l.end());
1926 tpar = tpar->next();
1932 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1935 if (the_locking_inset) {
1936 the_locking_inset->setFont(bv, font, toggleall, selectall);
1939 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1940 getLyXText(bv)->setFont(bv, font, toggleall);
1945 lt = getLyXText(bv);
1948 if (lt->selection.set()) {
1949 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1953 lt->toggleFree(bv, font, toggleall);
1955 lt->clearSelection();
1957 bool flag = (selectall || lt->selection.set());
1961 updateLocal(bv, FULL, true);
1963 updateLocal(bv, CURSOR_PAR, true);
1967 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1969 if (cpar(bv)->isInset(cpos(bv))) {
1973 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1974 if (!isHighlyEditableInset(inset))
1976 LyXFont const font =
1977 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1979 x = inset->width(bv, font);
1980 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1983 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1985 //inset_x = cx(bv) - top_x + drawTextXOffset;
1986 //inset_y = cy(bv) + drawTextYOffset;
1987 inset->edit(bv, x, y, 0);
1988 if (!the_locking_inset)
1990 updateLocal(bv, CURSOR, false);
1997 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2000 x -= drawTextXOffset;
2002 int dummyy = y + insetAscent;
2003 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
2010 inset_x = cx(bv) - top_x + drawTextXOffset;
2011 inset_y = cy(bv) + drawTextYOffset;
2012 inset->edit(bv, x - inset_x, y - inset_y, button);
2013 if (!the_locking_inset)
2015 updateLocal(bv, CURSOR, false);
2022 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2025 int w = UpdatableInset::getMaxWidth(bv, inset);
2030 w = w - top_x + owner()->x();
2033 w -= (2 * TEXT_TO_INSET_OFFSET);
2036 return UpdatableInset::getMaxWidth(bv, inset);
2041 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2043 // we have to unlock any locked inset otherwise we're in troubles
2044 the_locking_inset = 0;
2046 Paragraph * tmp = par->next();
2051 par = new Paragraph(*p, same_id);
2052 par->setInsetOwner(this);
2053 Paragraph * np = par;
2056 np->next(new Paragraph(*p, same_id));
2057 np->next()->previous(np);
2059 np->setInsetOwner(this);
2066 void InsetText::setText(string const & data, LyXFont const & font)
2069 for (unsigned int i=0; i < data.length(); ++i)
2070 par->insertChar(i, data[i], font);
2075 void InsetText::setAutoBreakRows(bool flag)
2077 if (flag != autoBreakRows) {
2078 autoBreakRows = flag;
2086 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2088 if (how != drawFrame_) {
2091 updateLocal(bv, DRAW_FRAME, false);
2096 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2098 if (frame_color != col) {
2101 updateLocal(bv, DRAW_FRAME, false);
2106 int InsetText::cx(BufferView * bv) const
2108 // we do nothing dangerous so we use a local cache
2109 LyXText * llt = getLyXText(bv);
2110 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2111 if (the_locking_inset) {
2112 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2114 if (font.isVisibleRightToLeft())
2115 x -= the_locking_inset->width(bv, font);
2121 int InsetText::cy(BufferView * bv) const
2124 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2128 pos_type InsetText::cpos(BufferView * bv) const
2130 return getLyXText(bv)->cursor.pos();
2134 Paragraph * InsetText::cpar(BufferView * bv) const
2136 return getLyXText(bv)->cursor.par();
2140 bool InsetText::cboundary(BufferView * bv) const
2142 return getLyXText(bv)->cursor.boundary();
2146 Row * InsetText::crow(BufferView * bv) const
2148 return getLyXText(bv)->cursor.row();
2152 LyXText * InsetText::getLyXText(BufferView const * lbv,
2153 bool const recursive) const
2155 if (cached_bview == lbv) {
2156 if (recursive && the_locking_inset)
2157 return the_locking_inset->getLyXText(lbv, true);
2158 LyXText * lt = cached_text.get();
2159 lyx::Assert(lt && lt->firstRow()->par() == par);
2162 // Super UGLY! (Lgb)
2163 BufferView * bv = const_cast<BufferView *>(lbv);
2166 Cache::iterator it = cache.find(bv);
2168 if (it != cache.end()) {
2171 } else if (do_resize) {
2172 resizeLyXText(do_resize);
2174 if (lt || !it->second.remove) {
2175 lyx::Assert(it->second.text.get());
2176 cached_text = it->second.text;
2177 if (recursive && the_locking_inset) {
2178 return the_locking_inset->getLyXText(bv, true);
2180 return cached_text.get();
2181 } else if (it->second.remove) {
2183 saveLyXTextState(it->second.text.get());
2189 // when we have to reinit the existing LyXText!
2191 it->second.text->init(bv);
2192 restoreLyXTextState(bv, it->second.text.get());
2193 it->second.remove = false;
2195 cached_text = it->second.text;
2196 if (the_locking_inset && recursive) {
2197 return the_locking_inset->getLyXText(bv);
2199 return cached_text.get();
2202 // we are here only if we don't have a BufferView * in the cache!!!
2204 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2205 cached_text->init(bv);
2206 restoreLyXTextState(bv, cached_text.get());
2208 cache.insert(make_pair(bv, cached_text));
2210 if (the_locking_inset && recursive) {
2211 return the_locking_inset->getLyXText(bv);
2213 return cached_text.get();
2217 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2221 Cache::iterator it = cache.find(bv);
2223 if (it == cache.end()) {
2227 lyx::Assert(it->second.text.get());
2229 it->second.remove = true;
2231 /// then remove all LyXText in text-insets
2232 Paragraph * p = par;
2233 for (; p; p = p->next()) {
2234 p->deleteInsetsLyXText(bv);
2240 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2243 // we cannot resize this because we are in use!
2244 // so do this on the next possible getLyXText()
2249 // lyxerr << "InsetText::resizeLyXText\n";
2250 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2251 // we have to do this as a fixed width may have changed!
2252 LyXText * t = getLyXText(bv);
2253 saveLyXTextState(t);
2255 restoreLyXTextState(bv, t);
2258 // one endless line, resize normally not necessary
2259 if (!force && getMaxWidth(bv, this) < 0)
2262 Cache::iterator it = cache.find(bv);
2263 if (it == cache.end()) {
2266 lyx::Assert(it->second.text.get());
2268 LyXText * t = it->second.text.get();
2269 saveLyXTextState(t);
2270 for (Paragraph * p = par; p; p = p->next()) {
2271 p->resizeInsetsLyXText(bv);
2274 restoreLyXTextState(bv, t);
2275 if (the_locking_inset) {
2276 inset_x = cx(bv) - top_x + drawTextXOffset;
2277 inset_y = cy(bv) + drawTextYOffset;
2281 t->first_y = bv->screen()->topCursorVisible(t);
2284 updateLocal(bv, FULL, false);
2285 // this will scroll the screen such that the cursor becomes visible
2286 bv->updateScrollbar();
2288 need_update |= FULL;
2293 void InsetText::reinitLyXText() const
2296 // we cannot resize this because we are in use!
2297 // so do this on the next possible getLyXText()
2303 // lyxerr << "InsetText::reinitLyXText\n";
2304 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2305 lyx::Assert(it->second.text.get());
2307 LyXText * t = it->second.text.get();
2308 BufferView * bv = it->first;
2310 saveLyXTextState(t);
2311 for (Paragraph * p = par; p; p = p->next()) {
2312 p->resizeInsetsLyXText(bv);
2315 restoreLyXTextState(bv, t);
2316 if (the_locking_inset) {
2317 inset_x = cx(bv) - top_x + drawTextXOffset;
2318 inset_y = cy(bv) + drawTextYOffset;
2321 t->first_y = bv->screen()->topCursorVisible(t);
2324 updateLocal(bv, FULL, false);
2325 // this will scroll the screen such that the cursor becomes visible
2326 bv->updateScrollbar();
2334 void InsetText::removeNewlines()
2336 bool changed = false;
2338 for (Paragraph * p = par; p; p = p->next()) {
2339 for (int i = 0; i < p->size(); ++i) {
2340 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2351 bool InsetText::nodraw() const
2353 if (the_locking_inset)
2354 return the_locking_inset->nodraw();
2355 return UpdatableInset::nodraw();
2359 int InsetText::scroll(bool recursive) const
2361 int sx = UpdatableInset::scroll(false);
2363 if (recursive && the_locking_inset)
2364 sx += the_locking_inset->scroll(recursive);
2370 bool InsetText::doClearArea() const
2372 return !locked || (need_update & (FULL|INIT));
2376 void InsetText::selectAll(BufferView * bv)
2378 getLyXText(bv)->cursorTop(bv);
2379 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2380 getLyXText(bv)->cursorBottom(bv);
2381 getLyXText(bv)->setSelection(bv);
2385 void InsetText::clearSelection(BufferView * bv)
2387 getLyXText(bv)->clearSelection();
2391 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2393 Painter & pain = bv->painter();
2395 int h = insetAscent + insetDescent;
2396 int ty = baseline - insetAscent;
2402 if ((ty + h) > pain.paperHeight())
2403 h = pain.paperHeight();
2404 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2405 w = pain.paperWidth();
2406 // w -= TEXT_TO_INSET_OFFSET;
2407 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2410 frame_is_visible = false;
2414 Paragraph * InsetText::getParFromID(int id) const
2417 Paragraph * result = par;
2418 Paragraph * ires = 0;
2419 while (result && result->id() != id) {
2420 if ((ires = result->getParFromID(id)))
2422 result = result->next();
2426 Paragraph * tmp = par;
2428 if (tmp->id() == id) {
2431 Paragraph * tmp2 = tmp->getParFromID(id);
2442 Paragraph * InsetText::firstParagraph() const
2445 if (the_locking_inset)
2446 if ((result = the_locking_inset->firstParagraph()))
2452 Paragraph * InsetText::getFirstParagraph(int i) const
2454 return (i == 0) ? par : 0;
2458 LyXCursor const & InsetText::cursor(BufferView * bv) const
2460 if (the_locking_inset)
2461 return the_locking_inset->cursor(bv);
2462 return getLyXText(bv)->cursor;
2466 Paragraph * InsetText::paragraph() const
2472 void InsetText::paragraph(Paragraph * p)
2474 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2475 // caller of this function has to take care of it. This IS important
2476 // as we could have to insert a paragraph before this one and just
2477 // link the actual to a new ones next and set it with this function
2480 // set ourself as owner for all the paragraphs inserted!
2481 Paragraph * np = par;
2483 np->setInsetOwner(this);
2487 // redraw myself when asked for
2492 Inset * InsetText::getInsetFromID(int id_arg) const
2495 return const_cast<InsetText *>(this);
2497 Paragraph * lp = par;
2500 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2501 en = lp->inset_iterator_end();
2504 if ((*it)->id() == id_arg)
2506 Inset * in = (*it)->getInsetFromID(id_arg);
2516 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2522 lt = getLyXText(bv);
2525 if (the_locking_inset) {
2526 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2533 // we have to go on checking so move cusor to the next char
2534 lt->cursor.pos(lt->cursor.pos() + 1);
2536 str = lt->selectNextWordToSpellcheck(bv, value);
2538 bv->unlockInset(const_cast<InsetText *>(this));
2547 void InsetText::selectSelectedWord(BufferView * bv)
2549 if (the_locking_inset) {
2550 the_locking_inset->selectSelectedWord(bv);
2553 getLyXText(bv)->selectSelectedWord(bv);
2554 updateLocal(bv, SELECTION, false);
2558 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2560 if (the_locking_inset) {
2561 the_locking_inset->toggleSelection(bv, kill_selection);
2565 lt = getLyXText(bv);
2569 int x = top_x + TEXT_TO_INSET_OFFSET;
2571 Row * row = lt->firstRow();
2572 int y_offset = top_baseline - row->ascent_of_text();
2574 while ((row != 0) && ((y+row->height()) <= 0)) {
2581 if (need_update & SELECTION)
2583 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2589 bool InsetText::searchForward(BufferView * bv, string const & str,
2592 if (the_locking_inset) {
2593 if (the_locking_inset->searchForward(bv, str, cs, mw))
2597 lt = getLyXText(bv);
2600 Paragraph * lpar = lt->cursor.par();
2601 pos_type pos = lt->cursor.pos();
2602 if (pos < lpar->size() - 1)
2606 lpar = lpar->next();
2611 // we have to unlock ourself in this function by default!
2612 bv->unlockInset(const_cast<InsetText *>(this));
2615 lt->setCursor(bv, lpar, pos);
2619 if (LyXFind(bv, str, true, true, cs , mw)) {
2622 // we have to unlock ourself in this function by default!
2623 bv->unlockInset(const_cast<InsetText *>(this));
2627 bool InsetText::searchBackward(BufferView * bv, string const & str,
2630 if (the_locking_inset)
2631 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2633 if (LyXFind(bv, str, false, true, cs, mw)) {
2636 // we have to unlock ourself in this function by default!
2637 bv->unlockInset(const_cast<InsetText *>(this));
2642 bool InsetText::checkInsertChar(LyXFont & font)
2645 return owner()->checkInsertChar(font);
2650 void InsetText::collapseParagraphs(BufferParams const & bparams) const
2652 while(par->next()) {
2653 if (par->size() && par->next()->size() &&
2654 !par->isSeparator(par->size()-1))
2656 par->insertChar(par->size(), ' ');
2658 par->pasteParagraph(bparams);
2664 void InsetText::getDrawFont(LyXFont & font) const
2668 owner()->getDrawFont(font);
2672 void InsetText::appendParagraphs(BufferParams const & bparams,
2676 Paragraph * tmpbuf = newpar;
2677 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2679 while (tmpbuf->next()) {
2680 tmpbuf = tmpbuf->next();
2681 lastbuffer->next(new Paragraph(*tmpbuf, false));
2682 lastbuffer->next()->previous(lastbuffer);
2683 lastbuffer = lastbuffer->next();
2686 while (lastbuffer->next())
2687 lastbuffer = lastbuffer->next();
2688 if (newpar->size() && lastbuffer->size() &&
2689 !lastbuffer->isSeparator(lastbuffer->size()-1))
2691 lastbuffer->insertChar(lastbuffer->size(), ' ');
2694 // make the buf exactly the same layout than our last paragraph
2695 buf->makeSameLayout(lastbuffer);
2698 lastbuffer->next(buf);
2699 buf->previous(lastbuffer);
2700 lastbuffer->pasteParagraph(bparams);