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"
47 #include "support/textutils.h"
48 #include "support/LAssert.h"
49 #include "support/lstrings.h"
50 #include "support/lyxalgo.h" // lyx::count
67 using lyx::textclass_type;
69 extern unsigned char getCurrentTextClass(Buffer *);
70 extern bool math_insert_greek(BufferView *, char);
71 extern int greek_kb_flag;
74 // These functions should probably go into bufferview_funcs somehow (Jug)
76 void InsetText::saveLyXTextState(LyXText * t) const
78 // check if my paragraphs are still valid
81 if (p == t->cursor.par())
86 if (p && t->cursor.pos() <= p->size()) {
87 sstate.lpar = t->cursor.par();
88 sstate.pos = t->cursor.pos();
89 sstate.boundary = t->cursor.boundary();
90 sstate.selstartpar = t->selection.start.par();
91 sstate.selstartpos = t->selection.start.pos();
92 sstate.selstartboundary = t->selection.start.boundary();
93 sstate.selendpar = t->selection.end.par();
94 sstate.selendpos = t->selection.end.pos();
95 sstate.selendboundary = t->selection.end.boundary();
96 sstate.selection = t->selection.set();
97 sstate.mark_set = t->selection.mark();
98 sstate.refresh = t->refresh_row != 0;
104 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
107 t->selection.set(true);
108 /* at this point just to avoid the Delete-Empty-Paragraph
109 * Mechanism when setting the cursor */
110 t->selection.mark(sstate.mark_set);
111 if (sstate.selection) {
112 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
113 true, sstate.selstartboundary);
114 t->selection.cursor = t->cursor;
115 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
116 true, sstate.selendboundary);
118 t->setCursor(bv, sstate.lpar, sstate.pos);
120 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
121 t->selection.cursor = t->cursor;
122 t->selection.set(false);
124 if (sstate.refresh) {
130 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
137 InsetText::InsetText(BufferParams const & bp)
138 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
142 par->layout(textclasslist[bp.textclass].defaultLayoutName());
148 InsetText::InsetText(InsetText const & in, bool same_id)
149 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
157 InsetText & InsetText::operator=(InsetText const & it)
164 void InsetText::init(InsetText const * ins, bool same_id)
167 setParagraphData(ins->par, same_id);
168 autoBreakRows = ins->autoBreakRows;
169 drawFrame_ = ins->drawFrame_;
170 frame_color = ins->frame_color;
176 p->setInsetOwner(this);
179 the_locking_inset = 0;
181 frame_color = LColor::insetframe;
182 autoBreakRows = false;
189 no_selection = false;
196 last_drawn_width = -1;
197 frame_is_visible = false;
200 in_insetAllowed = false;
204 InsetText::~InsetText()
211 Paragraph * tmp = par->next();
218 void InsetText::clear()
220 // This is a gross hack...
221 string old_layout = par->layout();
224 Paragraph * tmp = par->next();
229 par->setInsetOwner(this);
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 ((drawFrame_ == ALWAYS) ||
489 ((cleared || (need_update != CURSOR_PAR)) &&
490 (drawFrame_ == LOCKED) && locked))
492 drawFrame(pain, cleared);
493 } else if (need_update & CLEAR_FRAME) {
494 clearFrame(pain, cleared);
497 x += insetWidth - TEXT_TO_INSET_OFFSET;
499 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
501 } else if (need_update != INIT) {
509 void InsetText::drawFrame(Painter & pain, bool cleared) const
511 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
512 if (!frame_is_visible || cleared) {
513 frame_x = top_x + ttoD2;
514 frame_y = top_baseline - insetAscent + ttoD2;
515 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
516 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
517 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
519 frame_is_visible = true;
524 void InsetText::clearFrame(Painter & pain, bool cleared) const
526 if (frame_is_visible) {
528 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
531 frame_is_visible = false;
536 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
539 if (reinit && owner()) {
541 owner()->update(bv, font, true);
546 if (reinit || need_update == INIT) {
548 // we should put this call where we set need_update to INIT!
551 owner()->update(bv, font, true);
555 if (the_locking_inset) {
556 inset_x = cx(bv) - top_x + drawTextXOffset;
557 inset_y = cy(bv) + drawTextYOffset;
558 the_locking_inset->update(bv, font, reinit);
566 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
569 lt->updateInset(bv, the_locking_inset);
571 if (lt->status() == LyXText::NEED_MORE_REFRESH)
579 void InsetText::setUpdateStatus(BufferView * bv, int what) const
581 // this does nothing dangerous so use only a localized buffer
582 LyXText * llt = getLyXText(bv);
585 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
586 // if we don't break row so that we only have one row to update!
587 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
589 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
592 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
593 need_update |= CURSOR_PAR;
596 // this to not draw a selection when we redraw all of it!
597 if (need_update & CURSOR && !(need_update & SELECTION)) {
598 if (llt->selection.set())
600 llt->clearSelection();
605 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
607 if (!autoBreakRows && par->next())
608 collapseParagraphs(bv->buffer()->params);
615 setUpdateStatus(bv, what);
616 bool flag = (((need_update != CURSOR) && (need_update != NONE)) ||
617 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
618 if (!lt->selection.set())
619 lt->selection.cursor = lt->cursor;
623 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
627 if (need_update == CURSOR)
629 bv->owner()->showState();
630 bv->owner()->updateMenubar();
631 bv->owner()->updateToolbar();
632 if (old_par != cpar(bv)) {
633 bv->owner()->setLayout(cpar(bv)->layout());
639 string const InsetText::editMessage() const
641 return _("Opened Text Inset");
645 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
647 UpdatableInset::edit(bv, x, y, button);
649 if (!bv->lockInset(this)) {
650 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
654 the_locking_inset = 0;
655 inset_pos = inset_x = inset_y = 0;
656 inset_boundary = false;
659 int tmp_y = (y < 0) ? 0 : y;
665 if (!checkAndActivateInset(bv, x, tmp_y, button))
666 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
668 lt->clearSelection();
670 // If the inset is empty set the language of the current font to the
671 // language to the surronding text (if different).
672 if (par->size() == 0 && !par->next() &&
673 bv->getParentLanguage(this) != lt->current_font.language())
675 LyXFont font(LyXFont::ALL_IGNORE);
676 font.setLanguage(bv->getParentLanguage(this));
677 setFont(bv, font, false);
684 if (drawFrame_ == LOCKED)
685 code = CURSOR|DRAW_FRAME;
686 updateLocal(bv, code, false);
690 void InsetText::edit(BufferView * bv, bool front)
692 UpdatableInset::edit(bv, front);
694 if (!bv->lockInset(this)) {
695 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
699 the_locking_inset = 0;
700 inset_pos = inset_x = inset_y = 0;
701 inset_boundary = false;
710 lt->setCursor(bv, par, 0);
715 // int const pos = (p->size() ? p->size()-1 : p->size());
716 lt->setCursor(bv, p, p->size());
718 lt->clearSelection();
720 // If the inset is empty set the language of the current font to the
721 // language to the surronding text (if different).
722 if (par->size() == 0 && !par->next() &&
723 bv->getParentLanguage(this) != lt->current_font.language()) {
724 LyXFont font(LyXFont::ALL_IGNORE);
725 font.setLanguage(bv->getParentLanguage(this));
726 setFont(bv, font, false);
732 if (drawFrame_ == LOCKED)
733 code = CURSOR|DRAW_FRAME;
734 updateLocal(bv, code, false);
738 void InsetText::insetUnlock(BufferView * bv)
740 if (the_locking_inset) {
741 the_locking_inset->insetUnlock(bv);
742 the_locking_inset = 0;
745 no_selection = false;
748 if (drawFrame_ == LOCKED)
749 code = CURSOR|CLEAR_FRAME;
757 if (lt->selection.set()) {
758 lt->clearSelection();
760 } else if (owner()) {
761 bv->owner()->setLayout(owner()->getLyXText(bv)
762 ->cursor.par()->layout());
764 bv->owner()->setLayout(bv->text->cursor.par()->layout());
765 // hack for deleteEmptyParMech
767 lt->setCursor(bv, par, 0);
768 } else if (par->next()) {
769 lt->setCursor(bv, par->next(), 0);
773 updateLocal(bv, code, false);
776 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
778 the_locking_inset = inset;
779 inset_x = cx(bv) - top_x + drawTextXOffset;
780 inset_y = cy(bv) + drawTextYOffset;
781 inset_pos = cpos(bv);
782 inset_par = cpar(bv);
783 inset_boundary = cboundary(bv);
784 updateLocal(bv, CURSOR, false);
788 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
790 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
794 if (!the_locking_inset) {
796 int const id = inset->id();
798 Paragraph::inset_iterator it =
799 p->inset_iterator_begin();
800 Paragraph::inset_iterator const end =
801 p->inset_iterator_end();
802 for (; it != end; ++it) {
803 if ((*it) == inset) {
804 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
805 lockInset(bv, inset);
808 if ((*it)->getInsetFromID(id)) {
809 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
811 return the_locking_inset->lockInsetInInset(bv, inset);
818 if (inset == cpar(bv)->getInset(cpos(bv))) {
819 lyxerr[Debug::INSETS] << "OK" << endl;
820 lockInset(bv, inset);
822 } else if (the_locking_inset && (the_locking_inset == inset)) {
823 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
824 lyxerr[Debug::INSETS] << "OK" << endl;
825 inset_x = cx(bv) - top_x + drawTextXOffset;
826 inset_y = cy(bv) + drawTextYOffset;
828 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
830 } else if (the_locking_inset) {
831 lyxerr[Debug::INSETS] << "MAYBE" << endl;
832 return the_locking_inset->lockInsetInInset(bv, inset);
834 lyxerr[Debug::INSETS] << "NOT OK" << endl;
839 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
842 if (!the_locking_inset)
844 if (the_locking_inset == inset) {
845 the_locking_inset->insetUnlock(bv);
846 getLyXText(bv)->updateInset(bv, inset);
847 the_locking_inset = 0;
849 moveRight(bv, false);
850 old_par = 0; // force layout setting
854 updateLocal(bv, CURSOR, false);
857 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
861 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
863 if (!autoBreakRows && par->next())
864 collapseParagraphs(bv->buffer()->params);
872 if (inset->owner() != this) {
873 bool found = the_locking_inset->updateInsetInInset(bv, inset);
877 setUpdateStatus(bv, CURSOR_PAR);
880 bool found = lt->updateInset(bv, inset);
884 setUpdateStatus(bv, CURSOR_PAR);
885 if (the_locking_inset &&
886 cpar(bv) == inset_par && cpos(bv) == inset_pos)
888 inset_x = cx(bv) - top_x + drawTextXOffset;
889 inset_y = cy(bv) + drawTextYOffset;
896 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
900 // use this to check mouse motion for selection!
904 int tmp_x = x - drawTextXOffset;
905 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
906 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
909 if (the_locking_inset) {
910 if (the_locking_inset == inset) {
911 the_locking_inset->insetButtonPress(bv,
915 no_selection = false;
918 // otherwise unlock the_locking_inset and lock the new inset
919 the_locking_inset->insetUnlock(bv);
920 inset_x = cx(bv) - top_x + drawTextXOffset;
921 inset_y = cy(bv) + drawTextYOffset;
922 the_locking_inset = 0;
923 inset->insetButtonPress(bv, x - inset_x,
924 y - inset_y, button);
925 // inset->edit(bv, x - inset_x, y - inset_y, button);
926 if (the_locking_inset)
927 updateLocal(bv, CURSOR, false);
928 no_selection = false;
931 // otherwise only unlock the_locking_inset
932 the_locking_inset->insetUnlock(bv);
933 the_locking_inset = 0;
935 if (bv->theLockingInset()) {
936 if (isHighlyEditableInset(inset)) {
937 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
938 inset_x = cx(bv) - top_x + drawTextXOffset;
939 inset_y = cy(bv) + drawTextYOffset;
940 inset_pos = cpos(bv);
941 inset_par = cpar(bv);
942 inset_boundary = cboundary(bv);
943 the_locking_inset = uinset;
944 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
946 uinset->edit(bv, x - inset_x, y - inset_y, 0);
947 if (the_locking_inset)
948 updateLocal(bv, CURSOR, false);
949 no_selection = false;
953 if (!inset) { // && (button == 2)) {
954 bool paste_internally = false;
955 if ((button == 2) && getLyXText(bv)->selection.set()) {
956 localDispatch(bv, LFUN_COPY, "");
957 paste_internally = true;
965 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
967 // set the selection cursor!
968 lt->selection.cursor = lt->cursor;
969 lt->cursor.x_fix(lt->cursor.x());
971 if (lt->selection.set()) {
972 lt->clearSelection();
975 updateLocal(bv, FULL, false);
977 lt->clearSelection();
980 updateLocal(bv, CURSOR, false);
982 bv->owner()->setLayout(cpar(bv)->layout());
984 // Insert primary selection with middle mouse
985 // if there is a local selection in the current buffer,
988 if (paste_internally)
989 localDispatch(bv, LFUN_PASTE, "");
991 localDispatch(bv, LFUN_PASTESELECTION,
995 getLyXText(bv)->clearSelection();
998 no_selection = false;
1002 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
1004 if (the_locking_inset) {
1005 return the_locking_inset->insetButtonRelease(bv,
1006 x - inset_x, y - inset_y,
1009 int tmp_x = x - drawTextXOffset;
1010 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1011 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1014 if (isHighlyEditableInset(inset)) {
1015 ret = inset->insetButtonRelease(bv, x - inset_x,
1016 y - inset_y, button);
1018 inset_x = cx(bv) - top_x + drawTextXOffset;
1019 inset_y = cy(bv) + drawTextYOffset;
1020 ret = inset->insetButtonRelease(bv, x - inset_x,
1021 y - inset_y, button);
1022 inset->edit(bv, x - inset_x,
1023 y - inset_y, button);
1025 updateLocal(bv, CURSOR_PAR, false);
1031 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1033 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1035 if (the_locking_inset) {
1036 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1042 lt = getLyXText(bv);
1045 hideInsetCursor(bv);
1046 LyXCursor cur = lt->cursor;
1047 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1048 if (cur == lt->cursor) {
1053 lt->setSelection(bv);
1054 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1055 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1059 updateLocal(bv, SELECTION, false);
1061 showInsetCursor(bv);
1065 void InsetText::insetKeyPress(XKeyEvent * xke)
1067 if (the_locking_inset) {
1068 the_locking_inset->insetKeyPress(xke);
1074 UpdatableInset::RESULT
1075 InsetText::localDispatch(BufferView * bv,
1076 kb_action action, string const & arg)
1078 bool was_empty = par->size() == 0 && !par->next();
1079 no_selection = false;
1080 UpdatableInset::RESULT
1081 result= UpdatableInset::localDispatch(bv, action, arg);
1082 if (result != UNDISPATCHED) {
1086 result = DISPATCHED;
1087 if ((action < 0) && arg.empty())
1090 if (the_locking_inset) {
1091 result = the_locking_inset->localDispatch(bv, action, arg);
1092 if (result == DISPATCHED_NOUPDATE)
1094 else if (result == DISPATCHED) {
1095 updateLocal(bv, CURSOR_PAR, false);
1097 } else if (result >= FINISHED) {
1099 case FINISHED_RIGHT:
1100 moveRightIntern(bv, false, false);
1101 result = DISPATCHED;
1104 if ((result = moveUp(bv)) >= FINISHED) {
1105 updateLocal(bv, CURSOR, false);
1106 bv->unlockInset(this);
1110 if ((result = moveDown(bv)) >= FINISHED) {
1111 updateLocal(bv, CURSOR, false);
1112 bv->unlockInset(this);
1116 result = DISPATCHED;
1119 the_locking_inset = 0;
1120 #ifdef WITH_WARNINGS
1121 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1126 hideInsetCursor(bv);
1129 lt = getLyXText(bv);
1133 int updflag = false;
1136 case LFUN_SELFINSERT:
1137 if (bv->buffer()->isReadonly()) {
1138 // setErrorMessage(N_("Document is read only"));
1142 /* Automatically delete the currently selected
1143 * text and replace it with what is being
1144 * typed in now. Depends on lyxrc settings
1145 * "auto_region_delete", which defaults to
1148 setUndo(bv, Undo::INSERT,
1149 lt->cursor.par(), lt->cursor.par()->next());
1151 if (lyxrc.auto_region_delete) {
1152 if (lt->selection.set()) {
1153 lt->cutSelection(bv, false);
1156 lt->clearSelection();
1157 for (string::size_type i = 0; i < arg.length(); ++i) {
1158 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1161 lt->selection.cursor = lt->cursor;
1162 updwhat = CURSOR_PAR;
1164 result = DISPATCHED_NOUPDATE;
1166 // --- Cursor Movements -----------------------------------
1169 moveRight(bv, false, true);
1170 lt->setSelection(bv);
1171 updwhat = SELECTION;
1174 result = moveRight(bv);
1180 moveLeft(bv, false, true);
1181 lt->setSelection(bv);
1182 updwhat = SELECTION;
1186 result = moveLeft(bv);
1192 lt->setSelection(bv);
1193 updwhat = SELECTION;
1197 result = moveDown(bv);
1203 lt->setSelection(bv);
1204 updwhat = SELECTION;
1208 result = moveUp(bv);
1220 case LFUN_BACKSPACE: {
1221 setUndo(bv, Undo::DELETE,
1222 lt->cursor.par(), lt->cursor.par()->next());
1223 if (lt->selection.set())
1224 lt->cutSelection(bv);
1227 updwhat = CURSOR_PAR;
1233 setUndo(bv, Undo::DELETE,
1234 lt->cursor.par(), lt->cursor.par()->next());
1235 if (lt->selection.set()) {
1236 lt->cutSelection(bv);
1240 updwhat = CURSOR_PAR;
1246 setUndo(bv, Undo::DELETE,
1247 lt->cursor.par(), lt->cursor.par()->next());
1248 lt->cutSelection(bv);
1249 updwhat = CURSOR_PAR;
1256 lt->copySelection(bv);
1257 updwhat = CURSOR_PAR;
1259 case LFUN_PASTESELECTION:
1261 string const clip(bv->getClipboard());
1265 if (arg == "paragraph") {
1266 lt->insertStringAsParagraphs(bv, clip);
1268 lt->insertStringAsLines(bv, clip);
1270 updwhat = CURSOR_PAR;
1275 if (!autoBreakRows) {
1277 if (CutAndPaste::nrOfParagraphs() > 1) {
1278 Alert::alert(_("Impossible operation"),
1279 _("Cannot include more than one paragraph!"),
1284 setUndo(bv, Undo::INSERT,
1285 lt->cursor.par(), lt->cursor.par()->next());
1286 lt->pasteSelection(bv);
1287 updwhat = CURSOR_PAR;
1292 case LFUN_BREAKPARAGRAPH:
1293 if (!autoBreakRows) {
1294 result = DISPATCHED;
1297 lt->breakParagraph(bv, 0);
1301 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1302 if (!autoBreakRows) {
1303 result = DISPATCHED;
1306 lt->breakParagraph(bv, 1);
1311 case LFUN_BREAKLINE: {
1312 if (!autoBreakRows) {
1313 result = DISPATCHED;
1316 setUndo(bv, Undo::INSERT,
1317 lt->cursor.par(), lt->cursor.par()->next());
1318 lt->insertChar(bv, Paragraph::META_NEWLINE);
1319 updwhat = CURSOR_PAR;
1325 // do not set layouts on non breakable textinsets
1326 if (autoBreakRows) {
1327 string cur_layout = cpar(bv)->layout();
1329 // Derive layout number from given argument (string)
1330 // and current buffer's textclass (number). */
1331 textclass_type tclass = bv->buffer()->params.textclass;
1332 string layout = arg;
1333 bool hasLayout = textclasslist[tclass].hasLayout(layout);
1335 // If the entry is obsolete, use the new one instead.
1337 string const & obs =
1338 textclasslist[tclass][layout].
1344 // see if we found the layout number:
1346 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1347 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1351 if (cur_layout != layout) {
1352 cur_layout = layout;
1353 lt->setLayout(bv, layout);
1354 bv->owner()->setLayout(cpar(bv)->layout());
1355 updwhat = CURSOR_PAR;
1359 // reset the layout box
1360 bv->owner()->setLayout(cpar(bv)->layout());
1363 case LFUN_PARAGRAPH_SPACING:
1364 // This one is absolutely not working. When fiddling with this
1365 // it also seems to me that the paragraphs inside the insettext
1366 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1368 Paragraph * par = lt->cursor.par();
1369 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1370 float cur_value = 1.0;
1371 if (cur_spacing == Spacing::Other) {
1372 cur_value = par->params().spacing().getValue();
1375 istringstream istr(arg.c_str());
1378 Spacing::Space new_spacing = cur_spacing;
1379 float new_value = cur_value;
1381 lyxerr << "Missing argument to `paragraph-spacing'"
1383 } else if (tmp == "single") {
1384 new_spacing = Spacing::Single;
1385 } else if (tmp == "onehalf") {
1386 new_spacing = Spacing::Onehalf;
1387 } else if (tmp == "double") {
1388 new_spacing = Spacing::Double;
1389 } else if (tmp == "other") {
1390 new_spacing = Spacing::Other;
1393 lyxerr << "new_value = " << tmpval << endl;
1396 } else if (tmp == "default") {
1397 new_spacing = Spacing::Default;
1399 lyxerr << _("Unknown spacing argument: ")
1402 if (cur_spacing != new_spacing || cur_value != new_value) {
1403 par->params().spacing(Spacing(new_spacing, new_value));
1404 updwhat = CURSOR_PAR;
1411 if (!bv->Dispatch(action, arg))
1412 result = UNDISPATCHED;
1419 updateLocal(bv, updwhat, updflag);
1420 /// If the action has deleted all text in the inset, we need to change the
1421 // language to the language of the surronding text.
1422 if (!was_empty && par->size() == 0 && !par->next()) {
1423 LyXFont font(LyXFont::ALL_IGNORE);
1424 font.setLanguage(bv->getParentLanguage(this));
1425 setFont(bv, font, false);
1428 if (result < FINISHED) {
1429 showInsetCursor(bv);
1431 bv->unlockInset(this);
1436 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1439 buf->latexParagraphs(os, par, 0, texrow);
1440 return texrow.rows();
1444 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1446 Paragraph * p = par;
1447 unsigned int lines = 0;
1450 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1451 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1459 int InsetText::docbook(Buffer const * buf, ostream & os) const
1461 Paragraph * p = par;
1462 unsigned int lines = 0;
1464 vector<string> environment_stack(10);
1465 vector<string> environment_inner(10);
1467 int const command_depth = 0;
1470 Paragraph::depth_type depth = 0; // paragraph depth
1474 int desc_on = 0; // description mode
1476 LyXLayout const & style =
1477 textclasslist[buf->params.textclass][p->layout()];
1479 // environment tag closing
1480 for (; depth > p->params().depth(); --depth) {
1481 if (environment_inner[depth] != "!-- --") {
1482 item_name = "listitem";
1483 buf->sgmlCloseTag(os, command_depth + depth,
1485 if (environment_inner[depth] == "varlistentry")
1486 buf->sgmlCloseTag(os, depth+command_depth,
1487 environment_inner[depth]);
1489 buf->sgmlCloseTag(os, depth + command_depth,
1490 environment_stack[depth]);
1491 environment_stack[depth].erase();
1492 environment_inner[depth].erase();
1495 if (depth == p->params().depth()
1496 && environment_stack[depth] != style.latexname()
1497 && !environment_stack[depth].empty()) {
1498 if (environment_inner[depth] != "!-- --") {
1499 item_name= "listitem";
1500 buf->sgmlCloseTag(os, command_depth+depth,
1502 if (environment_inner[depth] == "varlistentry")
1503 buf->sgmlCloseTag(os,
1504 depth + command_depth,
1505 environment_inner[depth]);
1508 buf->sgmlCloseTag(os, depth + command_depth,
1509 environment_stack[depth]);
1511 environment_stack[depth].erase();
1512 environment_inner[depth].erase();
1515 // Write opening SGML tags.
1516 switch (style.latextype) {
1517 case LATEX_PARAGRAPH:
1518 buf->sgmlOpenTag(os, depth + command_depth,
1523 buf->sgmlError(p, 0,
1524 _("Error : LatexType Command not allowed here.\n"));
1528 case LATEX_ENVIRONMENT:
1529 case LATEX_ITEM_ENVIRONMENT:
1530 if (depth < p->params().depth()) {
1531 depth = p->params().depth();
1532 environment_stack[depth].erase();
1535 if (environment_stack[depth] != style.latexname()) {
1536 if (environment_stack.size() == depth + 1) {
1537 environment_stack.push_back("!-- --");
1538 environment_inner.push_back("!-- --");
1540 environment_stack[depth] = style.latexname();
1541 environment_inner[depth] = "!-- --";
1542 buf->sgmlOpenTag(os, depth + command_depth,
1543 environment_stack[depth]);
1545 if (environment_inner[depth] != "!-- --") {
1546 item_name= "listitem";
1547 buf->sgmlCloseTag(os,
1548 command_depth + depth,
1550 if (environment_inner[depth] == "varlistentry")
1551 buf->sgmlCloseTag(os,
1552 depth + command_depth,
1553 environment_inner[depth]);
1557 if (style.latextype == LATEX_ENVIRONMENT) {
1558 if (!style.latexparam().empty()) {
1559 if (style.latexparam() == "CDATA")
1562 buf->sgmlOpenTag(os, depth + command_depth,
1563 style.latexparam());
1568 desc_on = (style.labeltype == LABEL_MANUAL);
1571 environment_inner[depth]= "varlistentry";
1573 environment_inner[depth]= "listitem";
1575 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1576 environment_inner[depth]);
1580 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1584 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1589 buf->sgmlOpenTag(os, depth + command_depth,
1594 buf->simpleDocBookOnePar(os, p, desc_on,
1595 depth + 1 + command_depth);
1599 // write closing SGML tags
1600 switch (style.latextype) {
1601 case LATEX_ENVIRONMENT:
1602 if (!style.latexparam().empty()) {
1603 if (style.latexparam() == "CDATA")
1606 buf->sgmlCloseTag(os, depth + command_depth,
1607 style.latexparam());
1610 case LATEX_ITEM_ENVIRONMENT:
1611 if (desc_on == 1) break;
1613 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1615 case LATEX_PARAGRAPH:
1616 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1619 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1625 for (int d = depth; d >= 0; --d) {
1626 if (!environment_stack[depth].empty()) {
1627 if (environment_inner[depth] != "!-- --") {
1628 item_name = "listitem";
1629 buf->sgmlCloseTag(os, command_depth + depth,
1631 if (environment_inner[depth] == "varlistentry")
1632 buf->sgmlCloseTag(os, depth + command_depth,
1633 environment_inner[depth]);
1636 buf->sgmlCloseTag(os, depth + command_depth,
1637 environment_stack[depth]);
1645 void InsetText::validate(LaTeXFeatures & features) const
1647 Paragraph * p = par;
1649 p->validate(features);
1655 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1657 if (textclasslist[buf->params.textclass][p->layout()].labeltype != LABEL_MANUAL)
1660 return p->beginningOfMainBody();
1664 void InsetText::getCursorPos(BufferView * bv,
1665 int & x, int & y) const
1667 if (the_locking_inset) {
1668 the_locking_inset->getCursorPos(bv, x, y);
1676 unsigned int InsetText::insetInInsetY()
1678 if (!the_locking_inset)
1681 return (inset_y + the_locking_inset->insetInInsetY());
1685 void InsetText::toggleInsetCursor(BufferView * bv)
1687 if (the_locking_inset) {
1688 the_locking_inset->toggleInsetCursor(bv);
1692 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1694 int const asc = lyxfont::maxAscent(font);
1695 int const desc = lyxfont::maxDescent(font);
1697 if (isCursorVisible())
1698 bv->hideLockedInsetCursor();
1700 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1701 toggleCursorVisible();
1705 void InsetText::showInsetCursor(BufferView * bv, bool show)
1707 if (the_locking_inset) {
1708 the_locking_inset->showInsetCursor(bv, show);
1711 if (!isCursorVisible()) {
1712 LyXFont const font =
1713 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1715 int const asc = lyxfont::maxAscent(font);
1716 int const desc = lyxfont::maxDescent(font);
1718 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1720 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1721 setCursorVisible(true);
1726 void InsetText::hideInsetCursor(BufferView * bv)
1728 if (isCursorVisible()) {
1729 bv->hideLockedInsetCursor();
1730 setCursorVisible(false);
1732 if (the_locking_inset)
1733 the_locking_inset->hideInsetCursor(bv);
1737 void InsetText::fitInsetCursor(BufferView * bv) const
1739 if (the_locking_inset) {
1740 the_locking_inset->fitInsetCursor(bv);
1743 LyXFont const font =
1744 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1746 int const asc = lyxfont::maxAscent(font);
1747 int const desc = lyxfont::maxDescent(font);
1749 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1753 UpdatableInset::RESULT
1754 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1756 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1757 return moveLeftIntern(bv, false, activate_inset, selecting);
1759 return moveRightIntern(bv, false, activate_inset, selecting);
1763 UpdatableInset::RESULT
1764 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1766 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1767 return moveRightIntern(bv, true, activate_inset, selecting);
1769 return moveLeftIntern(bv, true, activate_inset, selecting);
1773 UpdatableInset::RESULT
1774 InsetText::moveRightIntern(BufferView * bv, bool behind,
1775 bool activate_inset, bool selecting)
1777 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1778 return FINISHED_RIGHT;
1779 if (activate_inset && checkAndActivateInset(bv, behind))
1781 getLyXText(bv)->cursorRight(bv);
1783 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1784 return DISPATCHED_NOUPDATE;
1788 UpdatableInset::RESULT
1789 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1790 bool activate_inset, bool selecting)
1792 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1794 getLyXText(bv)->cursorLeft(bv);
1796 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1797 if (activate_inset && checkAndActivateInset(bv, behind))
1799 return DISPATCHED_NOUPDATE;
1803 UpdatableInset::RESULT
1804 InsetText::moveUp(BufferView * bv)
1806 if (!crow(bv)->previous())
1808 getLyXText(bv)->cursorUp(bv);
1809 return DISPATCHED_NOUPDATE;
1813 UpdatableInset::RESULT
1814 InsetText::moveDown(BufferView * bv)
1816 if (!crow(bv)->next())
1817 return FINISHED_DOWN;
1818 getLyXText(bv)->cursorDown(bv);
1819 return DISPATCHED_NOUPDATE;
1823 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1825 if (the_locking_inset) {
1826 if (the_locking_inset->insetAllowed(inset))
1827 return the_locking_inset->insertInset(bv, inset);
1832 lt = getLyXText(bv);
1835 setUndo(bv, Undo::FINISH, lt->cursor.par(), lt->cursor.par()->next());
1837 inset->setOwner(this);
1838 hideInsetCursor(bv);
1839 lt->insertInset(bv, inset);
1843 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1849 bool InsetText::insetAllowed(Inset::Code code) const
1851 // in_insetAllowed is a really gross hack,
1852 // to allow us to call the owner's insetAllowed
1853 // without stack overflow, which can happen
1854 // when the owner uses InsetCollapsable::insetAllowed()
1856 if (in_insetAllowed)
1858 in_insetAllowed = true;
1859 if (the_locking_inset)
1860 ret = the_locking_inset->insetAllowed(code);
1862 ret = owner()->insetAllowed(code);
1863 in_insetAllowed = false;
1868 UpdatableInset * InsetText::getLockingInset() const
1870 return the_locking_inset ? the_locking_inset->getLockingInset() :
1871 const_cast<InsetText *>(this);
1875 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1879 if (the_locking_inset)
1880 return the_locking_inset->getFirstLockingInsetOfType(c);
1885 bool InsetText::showInsetDialog(BufferView * bv) const
1887 if (the_locking_inset)
1888 return the_locking_inset->showInsetDialog(bv);
1893 vector<string> const InsetText::getLabelList() const
1895 vector<string> label_list;
1897 Paragraph * tpar = par;
1899 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1900 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1901 for (; beg != end; ++beg) {
1902 vector<string> const l = (*beg)->getLabelList();
1903 label_list.insert(label_list.end(), l.begin(), l.end());
1905 tpar = tpar->next();
1911 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1914 if (the_locking_inset) {
1915 the_locking_inset->setFont(bv, font, toggleall, selectall);
1918 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1919 getLyXText(bv)->setFont(bv, font, toggleall);
1924 lt = getLyXText(bv);
1927 if (lt->selection.set()) {
1928 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1932 lt->toggleFree(bv, font, toggleall);
1934 lt->clearSelection();
1936 bool flag = (selectall || lt->selection.set());
1940 updateLocal(bv, FULL, true);
1942 updateLocal(bv, CURSOR_PAR, true);
1946 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1948 if (cpar(bv)->isInset(cpos(bv))) {
1952 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1953 if (!isHighlyEditableInset(inset))
1955 LyXFont const font =
1956 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1958 x = inset->width(bv, font);
1959 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1962 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1964 //inset_x = cx(bv) - top_x + drawTextXOffset;
1965 //inset_y = cy(bv) + drawTextYOffset;
1966 inset->edit(bv, x, y, 0);
1967 if (!the_locking_inset)
1969 updateLocal(bv, CURSOR, false);
1976 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1979 x -= drawTextXOffset;
1981 int dummyy = y + insetAscent;
1982 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
1989 inset_x = cx(bv) - top_x + drawTextXOffset;
1990 inset_y = cy(bv) + drawTextYOffset;
1991 inset->edit(bv, x - inset_x, y - inset_y, button);
1992 if (!the_locking_inset)
1994 updateLocal(bv, CURSOR, false);
2001 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2004 int w = UpdatableInset::getMaxWidth(bv, inset);
2009 w = w - top_x + owner()->x();
2012 w -= (2 * TEXT_TO_INSET_OFFSET);
2015 return UpdatableInset::getMaxWidth(bv, inset);
2020 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2022 // we have to unlock any locked inset otherwise we're in troubles
2023 the_locking_inset = 0;
2025 Paragraph * tmp = par->next();
2030 par = new Paragraph(*p, same_id);
2031 par->setInsetOwner(this);
2032 Paragraph * np = par;
2035 np->next(new Paragraph(*p, same_id));
2036 np->next()->previous(np);
2038 np->setInsetOwner(this);
2045 void InsetText::setText(string const & data)
2048 LyXFont font(LyXFont::ALL_SANE);
2049 for (unsigned int i=0; i < data.length(); ++i)
2050 par->insertChar(i, data[i], font);
2054 void InsetText::setAutoBreakRows(bool flag)
2056 if (flag != autoBreakRows) {
2057 autoBreakRows = flag;
2065 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2067 if (how != drawFrame_) {
2070 updateLocal(bv, DRAW_FRAME, false);
2075 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2077 if (frame_color != col) {
2080 updateLocal(bv, DRAW_FRAME, false);
2085 int InsetText::cx(BufferView * bv) const
2087 // we do nothing dangerous so we use a local cache
2088 LyXText * llt = getLyXText(bv);
2089 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2090 if (the_locking_inset) {
2091 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2093 if (font.isVisibleRightToLeft())
2094 x -= the_locking_inset->width(bv, font);
2100 int InsetText::cy(BufferView * bv) const
2103 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2107 pos_type InsetText::cpos(BufferView * bv) const
2109 return getLyXText(bv)->cursor.pos();
2113 Paragraph * InsetText::cpar(BufferView * bv) const
2115 return getLyXText(bv)->cursor.par();
2119 bool InsetText::cboundary(BufferView * bv) const
2121 return getLyXText(bv)->cursor.boundary();
2125 Row * InsetText::crow(BufferView * bv) const
2127 return getLyXText(bv)->cursor.row();
2131 LyXText * InsetText::getLyXText(BufferView const * lbv,
2132 bool const recursive) const
2134 if (cached_bview == lbv) {
2135 if (recursive && the_locking_inset)
2136 return the_locking_inset->getLyXText(lbv, true);
2137 LyXText * lt = cached_text.get();
2138 lyx::Assert(lt && lt->firstRow()->par() == par);
2141 // Super UGLY! (Lgb)
2142 BufferView * bv = const_cast<BufferView *>(lbv);
2145 Cache::iterator it = cache.find(bv);
2147 if (it != cache.end()) {
2150 } else if (do_resize) {
2151 resizeLyXText(do_resize);
2153 if (lt || !it->second.remove) {
2154 lyx::Assert(it->second.text.get());
2155 cached_text = it->second.text;
2156 if (recursive && the_locking_inset) {
2157 return the_locking_inset->getLyXText(bv, true);
2159 return cached_text.get();
2160 } else if (it->second.remove) {
2162 saveLyXTextState(it->second.text.get());
2168 // when we have to reinit the existing LyXText!
2170 it->second.text->init(bv);
2171 restoreLyXTextState(bv, it->second.text.get());
2172 it->second.remove = false;
2174 cached_text = it->second.text;
2175 if (the_locking_inset && recursive) {
2176 return the_locking_inset->getLyXText(bv);
2178 return cached_text.get();
2181 // we are here only if we don't have a BufferView * in the cache!!!
2183 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2184 cached_text->init(bv);
2185 restoreLyXTextState(bv, cached_text.get());
2187 cache.insert(make_pair(bv, cached_text));
2189 if (the_locking_inset && recursive) {
2190 return the_locking_inset->getLyXText(bv);
2192 return cached_text.get();
2196 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2200 Cache::iterator it = cache.find(bv);
2202 if (it == cache.end()) {
2206 lyx::Assert(it->second.text.get());
2208 it->second.remove = true;
2210 /// then remove all LyXText in text-insets
2211 Paragraph * p = par;
2212 for (; p; p = p->next()) {
2213 p->deleteInsetsLyXText(bv);
2219 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2222 // we cannot resize this because we are in use!
2223 // so do this on the next possible getLyXText()
2228 // lyxerr << "InsetText::resizeLyXText\n";
2229 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2230 // we have to do this as a fixed width may have changed!
2231 LyXText * t = getLyXText(bv);
2232 saveLyXTextState(t);
2234 restoreLyXTextState(bv, t);
2237 // one endless line, resize normally not necessary
2238 if (!force && getMaxWidth(bv, this) < 0)
2241 Cache::iterator it = cache.find(bv);
2242 if (it == cache.end()) {
2245 lyx::Assert(it->second.text.get());
2247 LyXText * t = it->second.text.get();
2248 saveLyXTextState(t);
2249 for (Paragraph * p = par; p; p = p->next()) {
2250 p->resizeInsetsLyXText(bv);
2253 restoreLyXTextState(bv, t);
2254 if (the_locking_inset) {
2255 inset_x = cx(bv) - top_x + drawTextXOffset;
2256 inset_y = cy(bv) + drawTextYOffset;
2260 t->first_y = bv->screen()->topCursorVisible(t);
2263 updateLocal(bv, FULL, false);
2264 // this will scroll the screen such that the cursor becomes visible
2265 bv->updateScrollbar();
2267 need_update |= FULL;
2272 void InsetText::reinitLyXText() const
2275 // we cannot resize this because we are in use!
2276 // so do this on the next possible getLyXText()
2282 // lyxerr << "InsetText::reinitLyXText\n";
2283 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2284 lyx::Assert(it->second.text.get());
2286 LyXText * t = it->second.text.get();
2287 BufferView * bv = it->first;
2289 saveLyXTextState(t);
2290 for (Paragraph * p = par; p; p = p->next()) {
2291 p->resizeInsetsLyXText(bv);
2294 restoreLyXTextState(bv, t);
2295 if (the_locking_inset) {
2296 inset_x = cx(bv) - top_x + drawTextXOffset;
2297 inset_y = cy(bv) + drawTextYOffset;
2300 t->first_y = bv->screen()->topCursorVisible(t);
2303 updateLocal(bv, FULL, false);
2304 // this will scroll the screen such that the cursor becomes visible
2305 bv->updateScrollbar();
2313 void InsetText::removeNewlines()
2315 bool changed = false;
2317 for (Paragraph * p = par; p; p = p->next()) {
2318 for (int i = 0; i < p->size(); ++i) {
2319 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2330 bool InsetText::nodraw() const
2332 if (the_locking_inset)
2333 return the_locking_inset->nodraw();
2334 return UpdatableInset::nodraw();
2338 int InsetText::scroll(bool recursive) const
2340 int sx = UpdatableInset::scroll(false);
2342 if (recursive && the_locking_inset)
2343 sx += the_locking_inset->scroll(recursive);
2349 bool InsetText::doClearArea() const
2351 return !locked || (need_update & (FULL|INIT));
2355 void InsetText::selectAll(BufferView * bv)
2357 getLyXText(bv)->cursorTop(bv);
2358 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2359 getLyXText(bv)->cursorBottom(bv);
2360 getLyXText(bv)->setSelection(bv);
2364 void InsetText::clearSelection(BufferView * bv)
2366 getLyXText(bv)->clearSelection();
2370 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2372 Painter & pain = bv->painter();
2374 int h = insetAscent + insetDescent;
2375 int ty = baseline - insetAscent;
2381 if ((ty + h) > pain.paperHeight())
2382 h = pain.paperHeight();
2383 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2384 w = pain.paperWidth();
2385 // w -= TEXT_TO_INSET_OFFSET;
2386 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2389 frame_is_visible = false;
2393 Paragraph * InsetText::getParFromID(int id) const
2396 Paragraph * result = par;
2397 Paragraph * ires = 0;
2398 while (result && result->id() != id) {
2399 if ((ires = result->getParFromID(id)))
2401 result = result->next();
2405 Paragraph * tmp = par;
2407 if (tmp->id() == id) {
2410 Paragraph * tmp2 = tmp->getParFromID(id);
2421 Paragraph * InsetText::firstParagraph() const
2424 if (the_locking_inset)
2425 if ((result = the_locking_inset->firstParagraph()))
2431 Paragraph * InsetText::getFirstParagraph(int i) const
2433 return (i == 0) ? par : 0;
2437 LyXCursor const & InsetText::cursor(BufferView * bv) const
2439 if (the_locking_inset)
2440 return the_locking_inset->cursor(bv);
2441 return getLyXText(bv)->cursor;
2445 Paragraph * InsetText::paragraph() const
2451 void InsetText::paragraph(Paragraph * p)
2453 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2454 // caller of this function has to take care of it. This IS important
2455 // as we could have to insert a paragraph before this one and just
2456 // link the actual to a new ones next and set it with this function
2459 // set ourself as owner for all the paragraphs inserted!
2460 Paragraph * np = par;
2462 np->setInsetOwner(this);
2466 // redraw myself when asked for
2471 Inset * InsetText::getInsetFromID(int id_arg) const
2474 return const_cast<InsetText *>(this);
2476 Paragraph * lp = par;
2479 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2480 en = lp->inset_iterator_end();
2483 if ((*it)->id() == id_arg)
2485 Inset * in = (*it)->getInsetFromID(id_arg);
2495 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2501 lt = getLyXText(bv);
2504 if (the_locking_inset) {
2505 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2512 // we have to go on checking so move cusor to the next char
2513 lt->cursor.pos(lt->cursor.pos() + 1);
2515 str = lt->selectNextWordToSpellcheck(bv, value);
2517 bv->unlockInset(const_cast<InsetText *>(this));
2526 void InsetText::selectSelectedWord(BufferView * bv)
2528 if (the_locking_inset) {
2529 the_locking_inset->selectSelectedWord(bv);
2532 getLyXText(bv)->selectSelectedWord(bv);
2533 updateLocal(bv, SELECTION, false);
2537 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2539 if (the_locking_inset) {
2540 the_locking_inset->toggleSelection(bv, kill_selection);
2544 lt = getLyXText(bv);
2548 int x = top_x + TEXT_TO_INSET_OFFSET;
2550 Row * row = lt->firstRow();
2551 int y_offset = top_baseline - row->ascent_of_text();
2553 while ((row != 0) && ((y+row->height()) <= 0)) {
2560 if (need_update & SELECTION)
2562 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2568 bool InsetText::searchForward(BufferView * bv, string const & str,
2571 if (the_locking_inset) {
2572 if (the_locking_inset->searchForward(bv, str, cs, mw))
2576 lt = getLyXText(bv);
2579 Paragraph * lpar = lt->cursor.par();
2580 pos_type pos = lt->cursor.pos();
2581 if (pos < lpar->size() - 1)
2585 lpar = lpar->next();
2590 // we have to unlock ourself in this function by default!
2591 bv->unlockInset(const_cast<InsetText *>(this));
2594 lt->setCursor(bv, lpar, pos);
2598 if (LyXFind(bv, str, true, true, cs , mw)) {
2601 // we have to unlock ourself in this function by default!
2602 bv->unlockInset(const_cast<InsetText *>(this));
2606 bool InsetText::searchBackward(BufferView * bv, string const & str,
2609 if (the_locking_inset)
2610 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2612 if (LyXFind(bv, str, false, true, cs, mw)) {
2615 // we have to unlock ourself in this function by default!
2616 bv->unlockInset(const_cast<InsetText *>(this));
2621 bool InsetText::checkInsertChar(LyXFont & font)
2624 return owner()->checkInsertChar(font);
2629 void InsetText::collapseParagraphs(BufferParams const & bparams) const
2631 while(par->next()) {
2632 if (!par->isSeparator(par->size()-1))
2633 par->insertChar(par->size()-1, ' ');
2634 par->pasteParagraph(bparams);
2640 void InsetText::getDrawFont(LyXFont & font) const
2644 owner()->getDrawFont(font);