1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team.
8 * ======================================================
14 #pragma implementation
17 #include "insettext.h"
18 #include "paragraph.h"
22 #include "commandtags.h"
25 #include "BufferView.h"
26 #include "lyxtextclasslist.h"
27 #include "LaTeXFeatures.h"
30 #include "lyxcursor.h"
31 #include "CutAndPaste.h"
37 #include "trans_mgr.h"
38 #include "lyxscreen.h"
42 #include "ParagraphParameters.h"
43 #include "undo_funcs.h"
46 #include "frontends/Alert.h"
48 #include "support/textutils.h"
49 #include "support/LAssert.h"
50 #include "support/lstrings.h"
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->layout(old_layout);
237 Inset * InsetText::clone(Buffer const &, bool same_id) const
239 return new InsetText(*this, same_id);
243 void InsetText::write(Buffer const * buf, ostream & os) const
246 writeParagraphData(buf, os);
250 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
252 par->writeFile(buf, os, buf->params, 0);
256 void InsetText::read(Buffer const * buf, LyXLex & lex)
260 Paragraph * return_par = 0;
261 Paragraph::depth_type depth = 0;
262 LyXFont font(LyXFont::ALL_INHERIT);
268 token = lex.getString();
271 if (token == "\\end_inset") {
272 #ifndef NO_COMPABILITY
273 const_cast<Buffer*>(buf)->insertErtContents(par, pos, false);
278 if (const_cast<Buffer*>(buf)->
279 parseSingleLyXformat2Token(lex, par, return_par,
280 token, pos, depth, font)) {
281 // the_end read this should NEVER happen
282 lex.printError("\\the_end read in inset! Error in document!");
290 return_par->setInsetOwner(this);
291 return_par = return_par->next();
294 if (token != "\\end_inset") {
295 lex.printError("Missing \\end_inset at this point. "
302 int InsetText::ascent(BufferView * bv, LyXFont const &) const
304 insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
305 TEXT_TO_INSET_OFFSET;
310 int InsetText::descent(BufferView * bv, LyXFont const &) const
312 LyXText * llt = getLyXText(bv);
313 insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
314 TEXT_TO_INSET_OFFSET;
319 int InsetText::width(BufferView * bv, LyXFont const &) const
321 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
322 (2 * TEXT_TO_INSET_OFFSET);
323 insetWidth = max(insetWidth, 10);
328 int InsetText::textWidth(BufferView * bv, bool fordraw) const
331 if (!autoBreakRows) {
334 w = getMaxWidth(bv, this);
337 return max(w - (2 * TEXT_TO_INSET_OFFSET),
338 (int)getLyXText(bv)->width);
342 return w - (2 * TEXT_TO_INSET_OFFSET);
346 void InsetText::draw(BufferView * bv, LyXFont const & f,
347 int baseline, float & x, bool cleared) const
352 Painter & pain = bv->painter();
354 // this is the first thing we have to ask because if the x pos
355 // changed we have to do a complete rebreak of the text as we
356 // may have few space to draw in. Well we should check on this too
358 if (top_x != int(x)) {
361 int nw = getMaxWidth(bv, this);
362 if (nw > 0 && old_max_width != nw) {
365 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
370 // call these methods so that insetWidth, insetAscent and
371 // insetDescent have the right values.
376 // repaint the background if needed
377 if (cleared && backgroundColor() != LColor::background) {
378 clearInset(bv, baseline, cleared);
381 // no draw is necessary !!!
382 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
383 top_baseline = baseline;
385 if (need_update & CLEAR_FRAME)
386 clearFrame(pain, cleared);
393 x += static_cast<float>(scroll());
395 // if top_x differs we did it already
396 if (!cleared && (old_x == int(x))
397 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
398 ||(last_drawn_width != insetWidth)))
400 // Condition necessary to eliminate bug 59 attachment 37
402 clearInset(bv, baseline, cleared);
406 frame_is_visible = false;
408 if (!cleared && (need_update == NONE)) {
410 drawFrame(pain, cleared);
414 top_baseline = baseline;
415 top_y = baseline - insetAscent;
417 if (last_drawn_width != insetWidth) {
419 clearInset(bv, baseline, cleared);
421 last_drawn_width = insetWidth;
424 if (the_locking_inset && (cpar(bv) == inset_par)
425 && (cpos(bv) == inset_pos)) {
426 inset_x = cx(bv) - top_x + drawTextXOffset;
427 inset_y = cy(bv) + drawTextYOffset;
429 if (!cleared && (need_update == CURSOR)
430 && !getLyXText(bv)->selection.set()) {
431 drawFrame(pain, cleared);
441 x += TEXT_TO_INSET_OFFSET;
443 Row * row = lt->firstRow();
444 int y_offset = baseline - row->ascent_of_text();
445 int ph = pain.paperHeight();
448 while ((row != 0) && ((y+row->height()) <= 0)) {
450 first += row->height();
456 if (cleared || (need_update&(INIT|FULL))) {
459 while ((row != 0) && (yf < ph)) {
460 lt->getVisibleRow(bv, y+y_offset, int(x), row,
466 } else if (!locked) {
467 if (need_update & CURSOR) {
468 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
469 lt->clearSelection();
470 lt->selection.cursor = lt->cursor;
472 bv->screen()->update(lt, bv, y_offset, int(x));
475 if (need_update & SELECTION) {
476 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
477 } else if (need_update & CURSOR) {
478 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
479 lt->clearSelection();
480 lt->selection.cursor = lt->cursor;
482 bv->screen()->update(lt, bv, y_offset, int(x));
487 lt->status(bv, LyXText::UNCHANGED);
488 if ((need_update != CURSOR_PAR) &&
489 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked))) {
490 drawFrame(pain, cleared);
491 } else if (need_update & CLEAR_FRAME) {
492 clearFrame(pain, cleared);
495 x += insetWidth - TEXT_TO_INSET_OFFSET;
497 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
499 } else if (need_update != INIT) {
507 void InsetText::drawFrame(Painter & pain, bool cleared) const
509 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
510 if (!frame_is_visible || cleared) {
511 frame_x = top_x + ttoD2;
512 frame_y = top_baseline - insetAscent + ttoD2;
513 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
514 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
515 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
517 frame_is_visible = true;
522 void InsetText::clearFrame(Painter & pain, bool cleared) const
524 if (frame_is_visible) {
526 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
529 frame_is_visible = false;
534 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
537 if (reinit && owner()) {
539 owner()->update(bv, font, true);
544 if (reinit || need_update == INIT) {
546 // we should put this call where we set need_update to INIT!
549 owner()->update(bv, font, true);
553 if (the_locking_inset) {
554 inset_x = cx(bv) - top_x + drawTextXOffset;
555 inset_y = cy(bv) + drawTextYOffset;
556 the_locking_inset->update(bv, font, reinit);
564 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
567 lt->updateInset(bv, the_locking_inset);
569 if (lt->status() == LyXText::NEED_MORE_REFRESH)
577 void InsetText::setUpdateStatus(BufferView * bv, int what) const
579 // this does nothing dangerous so use only a localized buffer
580 LyXText * llt = getLyXText(bv);
583 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
584 // if we don't break row so that we only have one row to update!
585 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
587 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
590 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
591 need_update |= CURSOR_PAR;
594 // this to not draw a selection when we redraw all of it!
595 if (need_update & CURSOR && !(need_update & SELECTION)) {
596 if (llt->selection.set())
598 llt->clearSelection();
603 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
605 if (!autoBreakRows && par->next())
606 collapseParagraphs(bv->buffer()->params);
613 setUpdateStatus(bv, what);
614 bool flag = (((need_update != CURSOR) && (need_update != NONE)) ||
615 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
616 if (!lt->selection.set())
617 lt->selection.cursor = lt->cursor;
621 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
625 if (need_update == CURSOR)
627 bv->owner()->showState();
628 bv->owner()->updateMenubar();
629 bv->owner()->updateToolbar();
630 if (old_par != cpar(bv)) {
631 bv->owner()->setLayout(cpar(bv)->layout());
637 string const InsetText::editMessage() const
639 return _("Opened Text Inset");
643 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
645 UpdatableInset::edit(bv, x, y, button);
647 if (!bv->lockInset(this)) {
648 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
652 the_locking_inset = 0;
653 inset_pos = inset_x = inset_y = 0;
654 inset_boundary = false;
657 int tmp_y = (y < 0) ? 0 : y;
663 if (!checkAndActivateInset(bv, x, tmp_y, button))
664 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
666 lt->clearSelection();
668 // If the inset is empty set the language of the current font to the
669 // language to the surronding text (if different).
670 if (par->size() == 0 && !par->next() &&
671 bv->getParentLanguage(this) != lt->current_font.language())
673 LyXFont font(LyXFont::ALL_IGNORE);
674 font.setLanguage(bv->getParentLanguage(this));
675 setFont(bv, font, false);
682 if (drawFrame_ == LOCKED)
683 code = CURSOR|DRAW_FRAME;
684 updateLocal(bv, code, false);
688 void InsetText::edit(BufferView * bv, bool front)
690 UpdatableInset::edit(bv, front);
692 if (!bv->lockInset(this)) {
693 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
697 the_locking_inset = 0;
698 inset_pos = inset_x = inset_y = 0;
699 inset_boundary = false;
708 lt->setCursor(bv, par, 0);
713 // int const pos = (p->size() ? p->size()-1 : p->size());
714 lt->setCursor(bv, p, p->size());
716 lt->clearSelection();
718 // If the inset is empty set the language of the current font to the
719 // language to the surronding text (if different).
720 if (par->size() == 0 && !par->next() &&
721 bv->getParentLanguage(this) != lt->current_font.language()) {
722 LyXFont font(LyXFont::ALL_IGNORE);
723 font.setLanguage(bv->getParentLanguage(this));
724 setFont(bv, font, false);
730 if (drawFrame_ == LOCKED)
731 code = CURSOR|DRAW_FRAME;
732 updateLocal(bv, code, false);
736 void InsetText::insetUnlock(BufferView * bv)
738 if (the_locking_inset) {
739 the_locking_inset->insetUnlock(bv);
740 the_locking_inset = 0;
743 no_selection = false;
746 if (drawFrame_ == LOCKED)
747 code = CURSOR|CLEAR_FRAME;
755 if (lt->selection.set()) {
756 lt->clearSelection();
758 } else if (owner()) {
759 bv->owner()->setLayout(owner()->getLyXText(bv)
760 ->cursor.par()->layout());
762 bv->owner()->setLayout(bv->text->cursor.par()->layout());
763 // hack for deleteEmptyParMech
765 lt->setCursor(bv, par, 0);
766 } else if (par->next()) {
767 lt->setCursor(bv, par->next(), 0);
771 updateLocal(bv, code, false);
774 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
776 the_locking_inset = inset;
777 inset_x = cx(bv) - top_x + drawTextXOffset;
778 inset_y = cy(bv) + drawTextYOffset;
779 inset_pos = cpos(bv);
780 inset_par = cpar(bv);
781 inset_boundary = cboundary(bv);
782 updateLocal(bv, CURSOR, false);
786 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
788 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
792 if (!the_locking_inset) {
794 int const id = inset->id();
796 Paragraph::inset_iterator it =
797 p->inset_iterator_begin();
798 Paragraph::inset_iterator const end =
799 p->inset_iterator_end();
800 for (; it != end; ++it) {
801 if ((*it) == inset) {
802 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
803 lockInset(bv, inset);
806 if ((*it)->getInsetFromID(id)) {
807 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
809 return the_locking_inset->lockInsetInInset(bv, inset);
816 if (inset == cpar(bv)->getInset(cpos(bv))) {
817 lyxerr[Debug::INSETS] << "OK" << endl;
818 lockInset(bv, inset);
820 } else if (the_locking_inset && (the_locking_inset == inset)) {
821 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
822 lyxerr[Debug::INSETS] << "OK" << endl;
823 inset_x = cx(bv) - top_x + drawTextXOffset;
824 inset_y = cy(bv) + drawTextYOffset;
826 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
828 } else if (the_locking_inset) {
829 lyxerr[Debug::INSETS] << "MAYBE" << endl;
830 return the_locking_inset->lockInsetInInset(bv, inset);
832 lyxerr[Debug::INSETS] << "NOT OK" << endl;
837 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
840 if (!the_locking_inset)
842 if (the_locking_inset == inset) {
843 the_locking_inset->insetUnlock(bv);
844 getLyXText(bv)->updateInset(bv, inset);
845 the_locking_inset = 0;
847 moveRight(bv, false);
848 old_par = 0; // force layout setting
852 updateLocal(bv, CURSOR, false);
855 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
859 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
861 if (!autoBreakRows && par->next())
862 collapseParagraphs(bv->buffer()->params);
870 if (!the_locking_inset) {
871 bool found = lt->updateInset(bv, inset);
875 setUpdateStatus(bv, NONE);
878 if (the_locking_inset != inset) {
879 bool found = the_locking_inset->updateInsetInInset(bv, inset);
883 setUpdateStatus(bv, CURSOR_PAR);
886 bool found = lt->updateInset(bv, inset);
890 setUpdateStatus(bv, CURSOR_PAR);
891 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
892 inset_x = cx(bv) - top_x + drawTextXOffset;
893 inset_y = cy(bv) + drawTextYOffset;
900 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
904 // use this to check mouse motion for selection!
908 int tmp_x = x - drawTextXOffset;
909 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
910 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
913 if (the_locking_inset) {
914 if (the_locking_inset == inset) {
915 the_locking_inset->insetButtonPress(bv,
919 no_selection = false;
922 // otherwise unlock the_locking_inset and lock the new inset
923 the_locking_inset->insetUnlock(bv);
924 inset_x = cx(bv) - top_x + drawTextXOffset;
925 inset_y = cy(bv) + drawTextYOffset;
926 the_locking_inset = 0;
927 inset->insetButtonPress(bv, x - inset_x,
928 y - inset_y, button);
929 // inset->edit(bv, x - inset_x, y - inset_y, button);
930 if (the_locking_inset)
931 updateLocal(bv, CURSOR, false);
932 no_selection = false;
935 // otherwise only unlock the_locking_inset
936 the_locking_inset->insetUnlock(bv);
937 the_locking_inset = 0;
939 if (bv->theLockingInset()) {
940 if (isHighlyEditableInset(inset)) {
941 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
942 inset_x = cx(bv) - top_x + drawTextXOffset;
943 inset_y = cy(bv) + drawTextYOffset;
944 inset_pos = cpos(bv);
945 inset_par = cpar(bv);
946 inset_boundary = cboundary(bv);
947 the_locking_inset = uinset;
948 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
950 uinset->edit(bv, x - inset_x, y - inset_y, 0);
951 if (the_locking_inset)
952 updateLocal(bv, CURSOR, false);
953 no_selection = false;
957 if (!inset) { // && (button == 2)) {
958 bool paste_internally = false;
959 if ((button == 2) && getLyXText(bv)->selection.set()) {
960 localDispatch(bv, LFUN_COPY, "");
961 paste_internally = true;
969 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
971 // set the selection cursor!
972 lt->selection.cursor = lt->cursor;
973 lt->cursor.x_fix(lt->cursor.x());
975 if (lt->selection.set()) {
976 lt->clearSelection();
979 updateLocal(bv, FULL, false);
981 lt->clearSelection();
984 updateLocal(bv, CURSOR, false);
986 bv->owner()->setLayout(cpar(bv)->layout());
988 // Insert primary selection with middle mouse
989 // if there is a local selection in the current buffer,
992 if (paste_internally)
993 localDispatch(bv, LFUN_PASTE, "");
995 localDispatch(bv, LFUN_PASTESELECTION,
999 getLyXText(bv)->clearSelection();
1001 showInsetCursor(bv);
1002 no_selection = false;
1006 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
1008 if (the_locking_inset) {
1009 return the_locking_inset->insetButtonRelease(bv,
1010 x - inset_x, y - inset_y,
1013 int tmp_x = x - drawTextXOffset;
1014 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1015 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1018 if (isHighlyEditableInset(inset)) {
1019 ret = inset->insetButtonRelease(bv, x - inset_x,
1020 y - inset_y, button);
1022 inset_x = cx(bv) - top_x + drawTextXOffset;
1023 inset_y = cy(bv) + drawTextYOffset;
1024 ret = inset->insetButtonRelease(bv, x - inset_x,
1025 y - inset_y, button);
1026 inset->edit(bv, x - inset_x,
1027 y - inset_y, button);
1029 updateLocal(bv, CURSOR_PAR, false);
1035 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1037 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1039 if (the_locking_inset) {
1040 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1046 lt = getLyXText(bv);
1049 hideInsetCursor(bv);
1050 LyXCursor cur = lt->cursor;
1051 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1052 if (cur == lt->cursor) {
1057 lt->setSelection(bv);
1058 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1059 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1063 updateLocal(bv, SELECTION, false);
1065 showInsetCursor(bv);
1069 void InsetText::insetKeyPress(XKeyEvent * xke)
1071 if (the_locking_inset) {
1072 the_locking_inset->insetKeyPress(xke);
1078 UpdatableInset::RESULT
1079 InsetText::localDispatch(BufferView * bv,
1080 kb_action action, string const & arg)
1082 bool was_empty = par->size() == 0 && !par->next();
1083 no_selection = false;
1084 UpdatableInset::RESULT
1085 result= UpdatableInset::localDispatch(bv, action, arg);
1086 if (result != UNDISPATCHED) {
1090 result = DISPATCHED;
1091 if ((action < 0) && arg.empty())
1094 if (the_locking_inset) {
1095 result = the_locking_inset->localDispatch(bv, action, arg);
1096 if (result == DISPATCHED_NOUPDATE)
1098 else if (result == DISPATCHED) {
1099 updateLocal(bv, CURSOR_PAR, false);
1101 } else if (result >= FINISHED) {
1103 case FINISHED_RIGHT:
1104 moveRightIntern(bv, false, false);
1105 result = DISPATCHED;
1108 if ((result = moveUp(bv)) >= FINISHED) {
1109 updateLocal(bv, CURSOR, false);
1110 bv->unlockInset(this);
1114 if ((result = moveDown(bv)) >= FINISHED) {
1115 updateLocal(bv, CURSOR, false);
1116 bv->unlockInset(this);
1120 result = DISPATCHED;
1123 the_locking_inset = 0;
1124 #ifdef WITH_WARNINGS
1125 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1130 hideInsetCursor(bv);
1133 lt = getLyXText(bv);
1137 int updflag = false;
1140 case LFUN_SELFINSERT:
1141 if (bv->buffer()->isReadonly()) {
1142 // setErrorMessage(N_("Document is read only"));
1146 /* Automatically delete the currently selected
1147 * text and replace it with what is being
1148 * typed in now. Depends on lyxrc settings
1149 * "auto_region_delete", which defaults to
1152 setUndo(bv, Undo::INSERT,
1153 lt->cursor.par(), lt->cursor.par()->next());
1155 if (lyxrc.auto_region_delete) {
1156 if (lt->selection.set()) {
1157 lt->cutSelection(bv, false);
1160 lt->clearSelection();
1161 for (string::size_type i = 0; i < arg.length(); ++i) {
1162 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1165 lt->selection.cursor = lt->cursor;
1166 updwhat = CURSOR_PAR;
1168 result = DISPATCHED_NOUPDATE;
1170 // --- Cursor Movements -----------------------------------
1173 moveRight(bv, false, true);
1174 lt->setSelection(bv);
1175 updwhat = SELECTION;
1178 result = moveRight(bv);
1184 moveLeft(bv, false, true);
1185 lt->setSelection(bv);
1186 updwhat = SELECTION;
1190 result = moveLeft(bv);
1196 lt->setSelection(bv);
1197 updwhat = SELECTION;
1201 result = moveDown(bv);
1207 lt->setSelection(bv);
1208 updwhat = SELECTION;
1212 result = moveUp(bv);
1224 case LFUN_BACKSPACE: {
1225 setUndo(bv, Undo::DELETE,
1226 lt->cursor.par(), lt->cursor.par()->next());
1227 if (lt->selection.set())
1228 lt->cutSelection(bv);
1231 updwhat = CURSOR_PAR;
1237 setUndo(bv, Undo::DELETE,
1238 lt->cursor.par(), lt->cursor.par()->next());
1239 if (lt->selection.set()) {
1240 lt->cutSelection(bv);
1244 updwhat = CURSOR_PAR;
1250 setUndo(bv, Undo::DELETE,
1251 lt->cursor.par(), lt->cursor.par()->next());
1252 lt->cutSelection(bv);
1253 updwhat = CURSOR_PAR;
1260 lt->copySelection(bv);
1261 updwhat = CURSOR_PAR;
1263 case LFUN_PASTESELECTION:
1265 string const clip(bv->getClipboard());
1269 if (arg == "paragraph") {
1270 lt->insertStringAsParagraphs(bv, clip);
1272 lt->insertStringAsLines(bv, clip);
1274 updwhat = CURSOR_PAR;
1279 if (!autoBreakRows) {
1281 if (CutAndPaste::nrOfParagraphs() > 1) {
1282 Alert::alert(_("Impossible operation"),
1283 _("Cannot include more than one paragraph!"),
1288 setUndo(bv, Undo::INSERT,
1289 lt->cursor.par(), lt->cursor.par()->next());
1290 lt->pasteSelection(bv);
1291 updwhat = CURSOR_PAR;
1296 case LFUN_BREAKPARAGRAPH:
1297 if (!autoBreakRows) {
1298 result = DISPATCHED;
1301 lt->breakParagraph(bv, 0);
1305 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1306 if (!autoBreakRows) {
1307 result = DISPATCHED;
1310 lt->breakParagraph(bv, 1);
1315 case LFUN_BREAKLINE: {
1316 if (!autoBreakRows) {
1317 result = DISPATCHED;
1320 setUndo(bv, Undo::INSERT,
1321 lt->cursor.par(), lt->cursor.par()->next());
1322 lt->insertChar(bv, Paragraph::META_NEWLINE);
1323 updwhat = CURSOR_PAR;
1329 // do not set layouts on non breakable textinsets
1330 if (autoBreakRows) {
1331 string cur_layout = cpar(bv)->layout();
1333 // Derive layout number from given argument (string)
1334 // and current buffer's textclass (number). */
1335 textclass_type tclass = bv->buffer()->params.textclass;
1336 string layout = arg;
1337 bool hasLayout = textclasslist[tclass].hasLayout(layout);
1339 // If the entry is obsolete, use the new one instead.
1341 string const & obs =
1342 textclasslist[tclass][layout].
1348 // see if we found the layout number:
1350 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1351 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1355 if (cur_layout != layout) {
1356 cur_layout = layout;
1357 lt->setLayout(bv, layout);
1358 bv->owner()->setLayout(cpar(bv)->layout());
1359 updwhat = CURSOR_PAR;
1363 // reset the layout box
1364 bv->owner()->setLayout(cpar(bv)->layout());
1367 case LFUN_PARAGRAPH_SPACING:
1368 // This one is absolutely not working. When fiddling with this
1369 // it also seems to me that the paragraphs inside the insettext
1370 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1372 Paragraph * par = lt->cursor.par();
1373 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1374 float cur_value = 1.0;
1375 if (cur_spacing == Spacing::Other) {
1376 cur_value = par->params().spacing().getValue();
1379 istringstream istr(arg.c_str());
1382 Spacing::Space new_spacing = cur_spacing;
1383 float new_value = cur_value;
1385 lyxerr << "Missing argument to `paragraph-spacing'"
1387 } else if (tmp == "single") {
1388 new_spacing = Spacing::Single;
1389 } else if (tmp == "onehalf") {
1390 new_spacing = Spacing::Onehalf;
1391 } else if (tmp == "double") {
1392 new_spacing = Spacing::Double;
1393 } else if (tmp == "other") {
1394 new_spacing = Spacing::Other;
1397 lyxerr << "new_value = " << tmpval << endl;
1400 } else if (tmp == "default") {
1401 new_spacing = Spacing::Default;
1403 lyxerr << _("Unknown spacing argument: ")
1406 if (cur_spacing != new_spacing || cur_value != new_value) {
1407 par->params().spacing(Spacing(new_spacing, new_value));
1408 updwhat = CURSOR_PAR;
1415 if (!bv->Dispatch(action, arg))
1416 result = UNDISPATCHED;
1423 updateLocal(bv, updwhat, updflag);
1424 /// If the action has deleted all text in the inset, we need to change the
1425 // language to the language of the surronding text.
1426 if (!was_empty && par->size() == 0 && !par->next()) {
1427 LyXFont font(LyXFont::ALL_IGNORE);
1428 font.setLanguage(bv->getParentLanguage(this));
1429 setFont(bv, font, false);
1432 if (result < FINISHED) {
1433 showInsetCursor(bv);
1435 bv->unlockInset(this);
1440 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1443 buf->latexParagraphs(os, par, 0, texrow);
1444 return texrow.rows();
1448 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1450 Paragraph * p = par;
1451 unsigned int lines = 0;
1454 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1455 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1463 int InsetText::docbook(Buffer const * buf, ostream & os) const
1465 Paragraph * p = par;
1466 unsigned int lines = 0;
1468 vector<string> environment_stack(10);
1469 vector<string> environment_inner(10);
1471 int const command_depth = 0;
1474 Paragraph::depth_type depth = 0; // paragraph depth
1478 int desc_on = 0; // description mode
1480 LyXLayout const & style =
1481 textclasslist[buf->params.textclass][p->layout()];
1483 // environment tag closing
1484 for (; depth > p->params().depth(); --depth) {
1485 if (environment_inner[depth] != "!-- --") {
1486 item_name = "listitem";
1487 buf->sgmlCloseTag(os, command_depth + depth,
1489 if (environment_inner[depth] == "varlistentry")
1490 buf->sgmlCloseTag(os, depth+command_depth,
1491 environment_inner[depth]);
1493 buf->sgmlCloseTag(os, depth + command_depth,
1494 environment_stack[depth]);
1495 environment_stack[depth].erase();
1496 environment_inner[depth].erase();
1499 if (depth == p->params().depth()
1500 && environment_stack[depth] != style.latexname()
1501 && !environment_stack[depth].empty()) {
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,
1508 depth + command_depth,
1509 environment_inner[depth]);
1512 buf->sgmlCloseTag(os, depth + command_depth,
1513 environment_stack[depth]);
1515 environment_stack[depth].erase();
1516 environment_inner[depth].erase();
1519 // Write opening SGML tags.
1520 switch (style.latextype) {
1521 case LATEX_PARAGRAPH:
1522 buf->sgmlOpenTag(os, depth + command_depth,
1527 buf->sgmlError(p, 0,
1528 _("Error : LatexType Command not allowed here.\n"));
1532 case LATEX_ENVIRONMENT:
1533 case LATEX_ITEM_ENVIRONMENT:
1534 if (depth < p->params().depth()) {
1535 depth = p->params().depth();
1536 environment_stack[depth].erase();
1539 if (environment_stack[depth] != style.latexname()) {
1540 if (environment_stack.size() == depth + 1) {
1541 environment_stack.push_back("!-- --");
1542 environment_inner.push_back("!-- --");
1544 environment_stack[depth] = style.latexname();
1545 environment_inner[depth] = "!-- --";
1546 buf->sgmlOpenTag(os, depth + command_depth,
1547 environment_stack[depth]);
1549 if (environment_inner[depth] != "!-- --") {
1550 item_name= "listitem";
1551 buf->sgmlCloseTag(os,
1552 command_depth + depth,
1554 if (environment_inner[depth] == "varlistentry")
1555 buf->sgmlCloseTag(os,
1556 depth + command_depth,
1557 environment_inner[depth]);
1561 if (style.latextype == LATEX_ENVIRONMENT) {
1562 if (!style.latexparam().empty()) {
1563 if (style.latexparam() == "CDATA")
1566 buf->sgmlOpenTag(os, depth + command_depth,
1567 style.latexparam());
1572 desc_on = (style.labeltype == LABEL_MANUAL);
1575 environment_inner[depth]= "varlistentry";
1577 environment_inner[depth]= "listitem";
1579 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1580 environment_inner[depth]);
1584 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1588 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1593 buf->sgmlOpenTag(os, depth + command_depth,
1598 buf->simpleDocBookOnePar(os, p, desc_on,
1599 depth + 1 + command_depth);
1603 // write closing SGML tags
1604 switch (style.latextype) {
1605 case LATEX_ENVIRONMENT:
1606 if (!style.latexparam().empty()) {
1607 if (style.latexparam() == "CDATA")
1610 buf->sgmlCloseTag(os, depth + command_depth,
1611 style.latexparam());
1614 case LATEX_ITEM_ENVIRONMENT:
1615 if (desc_on == 1) break;
1617 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1619 case LATEX_PARAGRAPH:
1620 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1623 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1629 for (int d = depth; d >= 0; --d) {
1630 if (!environment_stack[depth].empty()) {
1631 if (environment_inner[depth] != "!-- --") {
1632 item_name = "listitem";
1633 buf->sgmlCloseTag(os, command_depth + depth,
1635 if (environment_inner[depth] == "varlistentry")
1636 buf->sgmlCloseTag(os, depth + command_depth,
1637 environment_inner[depth]);
1640 buf->sgmlCloseTag(os, depth + command_depth,
1641 environment_stack[depth]);
1649 void InsetText::validate(LaTeXFeatures & features) const
1651 Paragraph * p = par;
1653 p->validate(features);
1659 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1661 if (textclasslist[buf->params.textclass][p->layout()].labeltype != LABEL_MANUAL)
1664 return p->beginningOfMainBody();
1668 void InsetText::getCursorPos(BufferView * bv,
1669 int & x, int & y) const
1671 if (the_locking_inset) {
1672 the_locking_inset->getCursorPos(bv, x, y);
1680 unsigned int InsetText::insetInInsetY()
1682 if (!the_locking_inset)
1685 return (inset_y + the_locking_inset->insetInInsetY());
1689 void InsetText::toggleInsetCursor(BufferView * bv)
1691 if (the_locking_inset) {
1692 the_locking_inset->toggleInsetCursor(bv);
1696 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1698 int const asc = lyxfont::maxAscent(font);
1699 int const desc = lyxfont::maxDescent(font);
1701 if (isCursorVisible())
1702 bv->hideLockedInsetCursor();
1704 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1705 toggleCursorVisible();
1709 void InsetText::showInsetCursor(BufferView * bv, bool show)
1711 if (the_locking_inset) {
1712 the_locking_inset->showInsetCursor(bv, show);
1715 if (!isCursorVisible()) {
1716 LyXFont const font =
1717 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1719 int const asc = lyxfont::maxAscent(font);
1720 int const desc = lyxfont::maxDescent(font);
1722 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1724 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1725 setCursorVisible(true);
1730 void InsetText::hideInsetCursor(BufferView * bv)
1732 if (isCursorVisible()) {
1733 bv->hideLockedInsetCursor();
1734 setCursorVisible(false);
1736 if (the_locking_inset)
1737 the_locking_inset->hideInsetCursor(bv);
1741 void InsetText::fitInsetCursor(BufferView * bv) const
1743 if (the_locking_inset) {
1744 the_locking_inset->fitInsetCursor(bv);
1747 LyXFont const font =
1748 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1750 int const asc = lyxfont::maxAscent(font);
1751 int const desc = lyxfont::maxDescent(font);
1753 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1757 UpdatableInset::RESULT
1758 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1760 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1761 return moveLeftIntern(bv, false, activate_inset, selecting);
1763 return moveRightIntern(bv, false, activate_inset, selecting);
1767 UpdatableInset::RESULT
1768 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1770 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1771 return moveRightIntern(bv, true, activate_inset, selecting);
1773 return moveLeftIntern(bv, true, activate_inset, selecting);
1777 UpdatableInset::RESULT
1778 InsetText::moveRightIntern(BufferView * bv, bool behind,
1779 bool activate_inset, bool selecting)
1781 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1782 return FINISHED_RIGHT;
1783 if (activate_inset && checkAndActivateInset(bv, behind))
1785 getLyXText(bv)->cursorRight(bv);
1787 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1788 return DISPATCHED_NOUPDATE;
1792 UpdatableInset::RESULT
1793 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1794 bool activate_inset, bool selecting)
1796 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1798 getLyXText(bv)->cursorLeft(bv);
1800 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1801 if (activate_inset && checkAndActivateInset(bv, behind))
1803 return DISPATCHED_NOUPDATE;
1807 UpdatableInset::RESULT
1808 InsetText::moveUp(BufferView * bv)
1810 if (!crow(bv)->previous())
1812 getLyXText(bv)->cursorUp(bv);
1813 return DISPATCHED_NOUPDATE;
1817 UpdatableInset::RESULT
1818 InsetText::moveDown(BufferView * bv)
1820 if (!crow(bv)->next())
1821 return FINISHED_DOWN;
1822 getLyXText(bv)->cursorDown(bv);
1823 return DISPATCHED_NOUPDATE;
1827 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1829 if (the_locking_inset) {
1830 if (the_locking_inset->insetAllowed(inset))
1831 return the_locking_inset->insertInset(bv, inset);
1836 lt = getLyXText(bv);
1839 setUndo(bv, Undo::FINISH, lt->cursor.par(), lt->cursor.par()->next());
1841 inset->setOwner(this);
1842 hideInsetCursor(bv);
1843 lt->insertInset(bv, inset);
1847 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1853 bool InsetText::insetAllowed(Inset::Code code) const
1855 // in_insetAllowed is a really gross hack,
1856 // to allow us to call the owner's insetAllowed
1857 // without stack overflow, which can happen
1858 // when the owner uses InsetCollapsable::insetAllowed()
1860 if (in_insetAllowed)
1862 in_insetAllowed = true;
1863 if (the_locking_inset)
1864 ret = the_locking_inset->insetAllowed(code);
1866 ret = owner()->insetAllowed(code);
1867 in_insetAllowed = false;
1872 UpdatableInset * InsetText::getLockingInset() const
1874 return the_locking_inset ? the_locking_inset->getLockingInset() :
1875 const_cast<InsetText *>(this);
1879 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1883 if (the_locking_inset)
1884 return the_locking_inset->getFirstLockingInsetOfType(c);
1889 bool InsetText::showInsetDialog(BufferView * bv) const
1891 if (the_locking_inset)
1892 return the_locking_inset->showInsetDialog(bv);
1897 vector<string> const InsetText::getLabelList() const
1899 vector<string> label_list;
1901 Paragraph * tpar = par;
1903 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1904 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1905 for (; beg != end; ++beg) {
1906 vector<string> const l = (*beg)->getLabelList();
1907 label_list.insert(label_list.end(), l.begin(), l.end());
1909 tpar = tpar->next();
1915 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1918 if (the_locking_inset) {
1919 the_locking_inset->setFont(bv, font, toggleall, selectall);
1922 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1923 getLyXText(bv)->setFont(bv, font, toggleall);
1928 lt = getLyXText(bv);
1931 if (lt->selection.set()) {
1932 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1936 lt->toggleFree(bv, font, toggleall);
1938 lt->clearSelection();
1940 bool flag = (selectall || lt->selection.set());
1944 updateLocal(bv, FULL, true);
1946 updateLocal(bv, CURSOR_PAR, true);
1950 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1952 if (cpar(bv)->isInset(cpos(bv))) {
1956 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1957 if (!isHighlyEditableInset(inset))
1959 LyXFont const font =
1960 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1962 x = inset->width(bv, font);
1963 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1966 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1968 //inset_x = cx(bv) - top_x + drawTextXOffset;
1969 //inset_y = cy(bv) + drawTextYOffset;
1970 inset->edit(bv, x, y, 0);
1971 if (!the_locking_inset)
1973 updateLocal(bv, CURSOR, false);
1980 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1983 x -= drawTextXOffset;
1985 int dummyy = y + insetAscent;
1986 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
1993 inset_x = cx(bv) - top_x + drawTextXOffset;
1994 inset_y = cy(bv) + drawTextYOffset;
1995 inset->edit(bv, x - inset_x, y - inset_y, button);
1996 if (!the_locking_inset)
1998 updateLocal(bv, CURSOR, false);
2005 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2008 int w = UpdatableInset::getMaxWidth(bv, inset);
2013 w = w - top_x + owner()->x();
2016 w -= (2 * TEXT_TO_INSET_OFFSET);
2019 return UpdatableInset::getMaxWidth(bv, inset);
2024 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2026 // we have to unlock any locked inset otherwise we're in troubles
2027 the_locking_inset = 0;
2029 Paragraph * tmp = par->next();
2034 par = new Paragraph(*p, same_id);
2035 par->setInsetOwner(this);
2036 Paragraph * np = par;
2039 np->next(new Paragraph(*p, same_id));
2040 np->next()->previous(np);
2042 np->setInsetOwner(this);
2049 void InsetText::setText(string const & data)
2052 LyXFont font(LyXFont::ALL_SANE);
2053 for (unsigned int i=0; i < data.length(); ++i)
2054 par->insertChar(i, data[i], font);
2058 void InsetText::setAutoBreakRows(bool flag)
2060 if (flag != autoBreakRows) {
2061 autoBreakRows = flag;
2069 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2071 if (how != drawFrame_) {
2074 updateLocal(bv, DRAW_FRAME, false);
2079 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2081 if (frame_color != col) {
2084 updateLocal(bv, DRAW_FRAME, false);
2089 int InsetText::cx(BufferView * bv) const
2091 // we do nothing dangerous so we use a local cache
2092 LyXText * llt = getLyXText(bv);
2093 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2094 if (the_locking_inset) {
2095 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2097 if (font.isVisibleRightToLeft())
2098 x -= the_locking_inset->width(bv, font);
2104 int InsetText::cy(BufferView * bv) const
2107 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2111 pos_type InsetText::cpos(BufferView * bv) const
2113 return getLyXText(bv)->cursor.pos();
2117 Paragraph * InsetText::cpar(BufferView * bv) const
2119 return getLyXText(bv)->cursor.par();
2123 bool InsetText::cboundary(BufferView * bv) const
2125 return getLyXText(bv)->cursor.boundary();
2129 Row * InsetText::crow(BufferView * bv) const
2131 return getLyXText(bv)->cursor.row();
2135 LyXText * InsetText::getLyXText(BufferView const * lbv,
2136 bool const recursive) const
2138 if (!recursive && (cached_bview == lbv)) {
2139 LyXText * lt = cached_text.get();
2140 lyx::Assert(lt && lt->firstRow()->par() == par);
2144 // Super UGLY! (Lgb)
2145 BufferView * bv = const_cast<BufferView *>(lbv);
2148 Cache::iterator it = cache.find(bv);
2150 if (it != cache.end()) {
2153 } else if (do_resize) {
2154 resizeLyXText(do_resize);
2156 if (lt || !it->second.remove) {
2157 lyx::Assert(it->second.text.get());
2158 cached_text = it->second.text;
2159 if (recursive && the_locking_inset) {
2160 return the_locking_inset->getLyXText(bv, true);
2162 return cached_text.get();
2163 } else if (it->second.remove) {
2165 saveLyXTextState(it->second.text.get());
2171 // when we have to reinit the existing LyXText!
2173 it->second.text->init(bv);
2174 restoreLyXTextState(bv, it->second.text.get());
2175 it->second.remove = false;
2177 cached_text = it->second.text;
2178 if (the_locking_inset && recursive) {
2179 return the_locking_inset->getLyXText(bv);
2181 return cached_text.get();
2184 // we are here only if we don't have a BufferView * in the cache!!!
2186 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2187 cached_text->init(bv);
2188 restoreLyXTextState(bv, cached_text.get());
2190 cache.insert(make_pair(bv, cached_text));
2192 if (the_locking_inset && recursive) {
2193 return the_locking_inset->getLyXText(bv);
2195 return cached_text.get();
2199 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2203 Cache::iterator it = cache.find(bv);
2205 if (it == cache.end()) {
2209 lyx::Assert(it->second.text.get());
2211 it->second.remove = true;
2213 /// then remove all LyXText in text-insets
2214 Paragraph * p = par;
2215 for (; p; p = p->next()) {
2216 p->deleteInsetsLyXText(bv);
2222 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2225 // we cannot resize this because we are in use!
2226 // so do this on the next possible getLyXText()
2231 // lyxerr << "InsetText::resizeLyXText\n";
2232 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2233 // we have to do this as a fixed width may have changed!
2234 LyXText * t = getLyXText(bv);
2235 saveLyXTextState(t);
2237 restoreLyXTextState(bv, t);
2240 // one endless line, resize normally not necessary
2241 if (!force && getMaxWidth(bv, this) < 0)
2244 Cache::iterator it = cache.find(bv);
2245 if (it == cache.end()) {
2248 lyx::Assert(it->second.text.get());
2250 LyXText * t = it->second.text.get();
2251 saveLyXTextState(t);
2252 for (Paragraph * p = par; p; p = p->next()) {
2253 p->resizeInsetsLyXText(bv);
2256 restoreLyXTextState(bv, t);
2257 if (the_locking_inset) {
2258 inset_x = cx(bv) - top_x + drawTextXOffset;
2259 inset_y = cy(bv) + drawTextYOffset;
2263 t->first_y = bv->screen()->topCursorVisible(t);
2266 updateLocal(bv, FULL, false);
2267 // this will scroll the screen such that the cursor becomes visible
2268 bv->updateScrollbar();
2270 need_update |= FULL;
2275 void InsetText::reinitLyXText() const
2278 // we cannot resize this because we are in use!
2279 // so do this on the next possible getLyXText()
2285 // lyxerr << "InsetText::reinitLyXText\n";
2286 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2287 lyx::Assert(it->second.text.get());
2289 LyXText * t = it->second.text.get();
2290 BufferView * bv = it->first;
2292 saveLyXTextState(t);
2293 for (Paragraph * p = par; p; p = p->next()) {
2294 p->resizeInsetsLyXText(bv);
2297 restoreLyXTextState(bv, t);
2298 if (the_locking_inset) {
2299 inset_x = cx(bv) - top_x + drawTextXOffset;
2300 inset_y = cy(bv) + drawTextYOffset;
2303 t->first_y = bv->screen()->topCursorVisible(t);
2306 updateLocal(bv, FULL, false);
2307 // this will scroll the screen such that the cursor becomes visible
2308 bv->updateScrollbar();
2316 void InsetText::removeNewlines()
2318 bool changed = false;
2320 for (Paragraph * p = par; p; p = p->next()) {
2321 for (int i = 0; i < p->size(); ++i) {
2322 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2333 bool InsetText::nodraw() const
2335 if (the_locking_inset)
2336 return the_locking_inset->nodraw();
2337 return UpdatableInset::nodraw();
2341 int InsetText::scroll(bool recursive) const
2343 int sx = UpdatableInset::scroll(false);
2345 if (recursive && the_locking_inset)
2346 sx += the_locking_inset->scroll(recursive);
2352 bool InsetText::doClearArea() const
2354 return !locked || (need_update & (FULL|INIT));
2358 void InsetText::selectAll(BufferView * bv)
2360 getLyXText(bv)->cursorTop(bv);
2361 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2362 getLyXText(bv)->cursorBottom(bv);
2363 getLyXText(bv)->setSelection(bv);
2367 void InsetText::clearSelection(BufferView * bv)
2369 getLyXText(bv)->clearSelection();
2373 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2375 Painter & pain = bv->painter();
2377 int h = insetAscent + insetDescent;
2378 int ty = baseline - insetAscent;
2384 if ((ty + h) > pain.paperHeight())
2385 h = pain.paperHeight();
2386 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2387 w = pain.paperWidth();
2388 // w -= TEXT_TO_INSET_OFFSET;
2389 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2392 frame_is_visible = false;
2396 Paragraph * InsetText::getParFromID(int id) const
2399 Paragraph * result = par;
2400 Paragraph * ires = 0;
2401 while (result && result->id() != id) {
2402 if ((ires = result->getParFromID(id)))
2404 result = result->next();
2408 Paragraph * tmp = par;
2410 if (tmp->id() == id) {
2413 Paragraph * tmp2 = tmp->getParFromID(id);
2424 Paragraph * InsetText::firstParagraph() const
2427 if (the_locking_inset)
2428 if ((result = the_locking_inset->firstParagraph()))
2434 Paragraph * InsetText::getFirstParagraph(int i) const
2436 return (i == 0) ? par : 0;
2440 LyXCursor const & InsetText::cursor(BufferView * bv) const
2442 if (the_locking_inset)
2443 return the_locking_inset->cursor(bv);
2444 return getLyXText(bv)->cursor;
2448 Paragraph * InsetText::paragraph() const
2454 void InsetText::paragraph(Paragraph * p)
2456 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2457 // caller of this function has to take care of it. This IS important
2458 // as we could have to insert a paragraph before this one and just
2459 // link the actual to a new ones next and set it with this function
2462 // set ourself as owner for all the paragraphs inserted!
2463 Paragraph * np = par;
2465 np->setInsetOwner(this);
2469 // redraw myself when asked for
2474 Inset * InsetText::getInsetFromID(int id_arg) const
2477 return const_cast<InsetText *>(this);
2479 Paragraph * lp = par;
2482 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2483 en = lp->inset_iterator_end();
2486 if ((*it)->id() == id_arg)
2488 Inset * in = (*it)->getInsetFromID(id_arg);
2498 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2504 lt = getLyXText(bv);
2507 if (the_locking_inset) {
2508 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2515 // we have to go on checking so move cusor to the next char
2516 lt->cursor.pos(lt->cursor.pos() + 1);
2518 str = lt->selectNextWordToSpellcheck(bv, value);
2520 bv->unlockInset(const_cast<InsetText *>(this));
2529 void InsetText::selectSelectedWord(BufferView * bv)
2531 if (the_locking_inset) {
2532 the_locking_inset->selectSelectedWord(bv);
2535 getLyXText(bv)->selectSelectedWord(bv);
2536 updateLocal(bv, SELECTION, false);
2540 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2542 if (the_locking_inset) {
2543 the_locking_inset->toggleSelection(bv, kill_selection);
2547 lt = getLyXText(bv);
2551 int x = top_x + TEXT_TO_INSET_OFFSET;
2553 Row * row = lt->firstRow();
2554 int y_offset = top_baseline - row->ascent_of_text();
2556 while ((row != 0) && ((y+row->height()) <= 0)) {
2563 if (need_update & SELECTION)
2565 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2571 bool InsetText::searchForward(BufferView * bv, string const & str,
2574 if (the_locking_inset) {
2575 if (the_locking_inset->searchForward(bv, str, cs, mw))
2579 lt = getLyXText(bv);
2582 Paragraph * lpar = lt->cursor.par();
2583 pos_type pos = lt->cursor.pos();
2584 if (pos < lpar->size() - 1)
2588 lpar = lpar->next();
2593 // we have to unlock ourself in this function by default!
2594 bv->unlockInset(const_cast<InsetText *>(this));
2597 lt->setCursor(bv, lpar, pos);
2601 if (LyXFind(bv, str, true, true, cs , mw)) {
2604 // we have to unlock ourself in this function by default!
2605 bv->unlockInset(const_cast<InsetText *>(this));
2609 bool InsetText::searchBackward(BufferView * bv, string const & str,
2612 if (the_locking_inset)
2613 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2615 if (LyXFind(bv, str, false, true, cs, mw)) {
2618 // we have to unlock ourself in this function by default!
2619 bv->unlockInset(const_cast<InsetText *>(this));
2624 bool InsetText::checkInsertChar(LyXFont & font)
2627 return owner()->checkInsertChar(font);
2632 void InsetText::collapseParagraphs(BufferParams const & bparams) const
2634 while(par->next()) {
2635 if (!par->isSeparator(par->size()-1))
2636 par->insertChar(par->size()-1, ' ');
2637 par->pasteParagraph(bparams);
2643 void InsetText::getDrawFont(LyXFont & font) const
2647 owner()->getDrawFont(font);