1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team.
8 * ======================================================
14 #pragma implementation
17 #include "insettext.h"
18 #include "paragraph.h"
22 #include "commandtags.h"
25 #include "BufferView.h"
26 #include "lyxtextclasslist.h"
27 #include "LaTeXFeatures.h"
30 #include "lyxcursor.h"
31 #include "CutAndPaste.h"
37 #include "trans_mgr.h"
38 #include "lyxscreen.h"
42 #include "ParagraphParameters.h"
43 #include "undo_funcs.h"
46 #include "frontends/Alert.h"
48 #include "support/textutils.h"
49 #include "support/LAssert.h"
50 #include "support/lstrings.h"
66 using lyx::layout_type;
67 using lyx::textclass_type;
69 extern unsigned char getCurrentTextClass(Buffer *);
70 extern bool math_insert_greek(BufferView *, char);
71 extern int greek_kb_flag;
74 // These functions should probably go into bufferview_funcs somehow (Jug)
76 void InsetText::saveLyXTextState(LyXText * t) const
78 // check if my paragraphs are still valid
81 if (p == t->cursor.par())
86 if (p && t->cursor.pos() <= p->size()) {
87 sstate.lpar = t->cursor.par();
88 sstate.pos = t->cursor.pos();
89 sstate.boundary = t->cursor.boundary();
90 sstate.selstartpar = t->selection.start.par();
91 sstate.selstartpos = t->selection.start.pos();
92 sstate.selstartboundary = t->selection.start.boundary();
93 sstate.selendpar = t->selection.end.par();
94 sstate.selendpos = t->selection.end.pos();
95 sstate.selendboundary = t->selection.end.boundary();
96 sstate.selection = t->selection.set();
97 sstate.mark_set = t->selection.mark();
98 sstate.refresh = t->refresh_row != 0;
104 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
107 t->selection.set(true);
108 /* at this point just to avoid the Delete-Empty-Paragraph
109 * Mechanism when setting the cursor */
110 t->selection.mark(sstate.mark_set);
111 if (sstate.selection) {
112 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
113 true, sstate.selstartboundary);
114 t->selection.cursor = t->cursor;
115 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
116 true, sstate.selendboundary);
118 t->setCursor(bv, sstate.lpar, sstate.pos);
120 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
121 t->selection.cursor = t->cursor;
122 t->selection.set(false);
124 if (sstate.refresh) {
130 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
137 InsetText::InsetText()
138 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
146 InsetText::InsetText(InsetText const & in, bool same_id)
147 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
155 InsetText & InsetText::operator=(InsetText const & it)
162 void InsetText::init(InsetText const * ins, bool same_id)
165 setParagraphData(ins->par, same_id);
166 autoBreakRows = ins->autoBreakRows;
167 drawFrame_ = ins->drawFrame_;
168 frame_color = ins->frame_color;
174 p->setInsetOwner(this);
177 the_locking_inset = 0;
179 frame_color = LColor::insetframe;
180 autoBreakRows = false;
187 no_selection = false;
194 last_drawn_width = -1;
195 frame_is_visible = false;
198 in_insetAllowed = false;
202 InsetText::~InsetText()
209 Paragraph * tmp = par->next();
216 void InsetText::clear()
219 Paragraph * tmp = par->next();
229 Inset * InsetText::clone(Buffer const &, bool same_id) const
231 return new InsetText(*this, same_id);
235 void InsetText::write(Buffer const * buf, ostream & os) const
238 writeParagraphData(buf, os);
242 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
244 par->writeFile(buf, os, buf->params, 0);
248 void InsetText::read(Buffer const * buf, LyXLex & lex)
252 Paragraph * return_par = 0;
253 Paragraph::depth_type depth = 0;
254 LyXFont font(LyXFont::ALL_INHERIT);
260 token = lex.getString();
263 if (token == "\\end_inset") {
264 #ifndef NO_COMPABILITY
265 const_cast<Buffer*>(buf)->insertErtContents(par, pos, font, false);
270 if (const_cast<Buffer*>(buf)->
271 parseSingleLyXformat2Token(lex, par, return_par,
272 token, pos, depth, font)) {
273 // the_end read this should NEVER happen
274 lex.printError("\\the_end read in inset! Error in document!");
282 return_par->setInsetOwner(this);
283 return_par = return_par->next();
286 if (token != "\\end_inset") {
287 lex.printError("Missing \\end_inset at this point. "
294 int InsetText::ascent(BufferView * bv, LyXFont const &) const
296 insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
297 TEXT_TO_INSET_OFFSET;
302 int InsetText::descent(BufferView * bv, LyXFont const &) const
304 LyXText * llt = getLyXText(bv);
305 insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
306 TEXT_TO_INSET_OFFSET;
311 int InsetText::width(BufferView * bv, LyXFont const &) const
313 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
314 (2 * TEXT_TO_INSET_OFFSET);
315 insetWidth = max(insetWidth, 10);
320 int InsetText::textWidth(BufferView * bv, bool fordraw) const
323 if (!autoBreakRows) {
326 w = getMaxWidth(bv, this);
329 return max(w - (2 * TEXT_TO_INSET_OFFSET),
330 (int)getLyXText(bv)->width);
334 return w - (2 * TEXT_TO_INSET_OFFSET);
338 void InsetText::draw(BufferView * bv, LyXFont const & f,
339 int baseline, float & x, bool cleared) const
344 Painter & pain = bv->painter();
346 // this is the first thing we have to ask because if the x pos
347 // changed we have to do a complete rebreak of the text as we
348 // may have few space to draw in. Well we should check on this too
350 if (top_x != int(x)) {
352 int nw = getMaxWidth(bv, this);
353 if (nw > 0 && old_max_width != nw) {
356 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
364 // call these methods so that insetWidth, insetAscent and
365 // insetDescent have the right values.
370 // repaint the background if needed
371 if (cleared && backgroundColor() != LColor::background) {
373 clearInset(bv, baseline, cleared);
377 // no draw is necessary !!!
378 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
381 top_baseline = baseline;
383 if (need_update & CLEAR_FRAME)
384 clearFrame(pain, cleared);
391 x += static_cast<float>(scroll());
393 // if top_x differs we did it already
394 if (!cleared && (top_x == int(x))
395 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
396 ||(last_drawn_width != insetWidth))) {
397 // Condition necessary to eliminate bug 59 attachment 37
398 if (baseline > 0) clearInset(bv, baseline, cleared);
404 frame_is_visible = false;
406 if (!cleared && (need_update == NONE)) {
408 drawFrame(pain, cleared);
412 top_baseline = baseline;
413 top_y = baseline - insetAscent;
415 if (last_drawn_width != insetWidth) {
417 clearInset(bv, baseline, cleared);
419 last_drawn_width = insetWidth;
422 if (the_locking_inset && (cpar(bv) == inset_par)
423 && (cpos(bv) == inset_pos)) {
424 inset_x = cx(bv) - top_x + drawTextXOffset;
425 inset_y = cy(bv) + drawTextYOffset;
427 if (!cleared && (need_update == CURSOR)
428 && !getLyXText(bv)->selection.set()) {
429 drawFrame(pain, cleared);
439 x += TEXT_TO_INSET_OFFSET;
441 Row * row = lt->firstRow();
442 int y_offset = baseline - row->ascent_of_text();
443 int ph = pain.paperHeight();
446 while ((row != 0) && ((y+row->height()) <= 0)) {
448 first += row->height();
454 if (cleared || (need_update&(INIT|FULL))) {
457 while ((row != 0) && (yf < ph)) {
458 lt->getVisibleRow(bv, y+y_offset, int(x), row,
464 } else if (!locked) {
465 if (need_update & CURSOR) {
466 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
467 lt->clearSelection();
468 lt->selection.cursor = lt->cursor;
470 bv->screen()->update(lt, bv, y_offset, int(x));
473 if (need_update & SELECTION) {
474 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
475 } else if (need_update & CURSOR) {
476 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
477 lt->clearSelection();
478 lt->selection.cursor = lt->cursor;
480 bv->screen()->update(lt, bv, y_offset, int(x));
485 lt->status(bv, LyXText::UNCHANGED);
486 if ((need_update != CURSOR_PAR) &&
487 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked))) {
488 drawFrame(pain, cleared);
489 } else if (need_update & CLEAR_FRAME) {
490 clearFrame(pain, cleared);
493 x += insetWidth - TEXT_TO_INSET_OFFSET;
495 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
497 } else if (need_update != INIT) {
505 void InsetText::drawFrame(Painter & pain, bool cleared) const
507 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
508 if (!frame_is_visible || cleared) {
509 frame_x = top_x + ttoD2;
510 frame_y = top_baseline - insetAscent + ttoD2;
511 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
512 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
513 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
515 frame_is_visible = true;
520 void InsetText::clearFrame(Painter & pain, bool cleared) const
522 if (frame_is_visible) {
524 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
527 frame_is_visible = false;
532 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
535 if (reinit && owner()) {
537 owner()->update(bv, font, true);
542 if (reinit || need_update == INIT) {
544 // we should put this call where we set need_update to INIT!
547 owner()->update(bv, font, true);
551 if (the_locking_inset) {
552 inset_x = cx(bv) - top_x + drawTextXOffset;
553 inset_y = cy(bv) + drawTextYOffset;
554 the_locking_inset->update(bv, font, reinit);
562 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
565 lt->updateInset(bv, the_locking_inset);
567 if (lt->status() == LyXText::NEED_MORE_REFRESH)
575 void InsetText::setUpdateStatus(BufferView * bv, int what) const
577 // this does nothing dangerous so use only a localized buffer
578 LyXText * llt = getLyXText(bv);
581 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
582 // if we don't break row so that we only have one row to update!
583 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
585 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
588 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
589 need_update |= CURSOR_PAR;
592 // this to not draw a selection when we redraw all of it!
593 if (need_update & CURSOR && !(need_update & SELECTION)) {
594 if (llt->selection.set())
596 llt->clearSelection();
601 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
603 if (!autoBreakRows && par->next())
604 collapseParagraphs(bv->buffer()->params);
611 setUpdateStatus(bv, what);
612 bool flag = (((need_update != CURSOR) && (need_update != NONE)) ||
613 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
617 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
621 if (need_update == CURSOR)
623 bv->owner()->showState();
624 if (old_par != cpar(bv)) {
625 bv->owner()->setLayout(cpar(bv)->getLayout());
631 string const InsetText::editMessage() const
633 return _("Opened Text Inset");
637 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
639 UpdatableInset::edit(bv, x, y, button);
641 if (!bv->lockInset(this)) {
642 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
646 the_locking_inset = 0;
647 inset_pos = inset_x = inset_y = 0;
648 inset_boundary = false;
651 int tmp_y = (y < 0) ? 0 : y;
657 if (!checkAndActivateInset(bv, x, tmp_y, button))
658 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
660 lt->clearSelection();
662 // If the inset is empty set the language of the current font to the
663 // language to the surronding text (if different).
664 if (par->size() == 0 && !par->next() &&
665 bv->getParentLanguage(this) != lt->current_font.language())
667 LyXFont font(LyXFont::ALL_IGNORE);
668 font.setLanguage(bv->getParentLanguage(this));
669 setFont(bv, font, false);
674 updateLocal(bv, CURSOR, false);
678 void InsetText::edit(BufferView * bv, bool front)
680 UpdatableInset::edit(bv, front);
682 if (!bv->lockInset(this)) {
683 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
687 the_locking_inset = 0;
688 inset_pos = inset_x = inset_y = 0;
689 inset_boundary = false;
698 lt->setCursor(bv, par, 0);
703 // int const pos = (p->size() ? p->size()-1 : p->size());
704 lt->setCursor(bv, p, p->size());
706 lt->clearSelection();
708 // If the inset is empty set the language of the current font to the
709 // language to the surronding text (if different).
710 if (par->size() == 0 && !par->next() &&
711 bv->getParentLanguage(this) != lt->current_font.language()) {
712 LyXFont font(LyXFont::ALL_IGNORE);
713 font.setLanguage(bv->getParentLanguage(this));
714 setFont(bv, font, false);
719 updateLocal(bv, CURSOR, false);
723 void InsetText::insetUnlock(BufferView * bv)
725 if (the_locking_inset) {
726 the_locking_inset->insetUnlock(bv);
727 the_locking_inset = 0;
730 no_selection = false;
733 if (drawFrame_ == LOCKED)
734 code = CURSOR|CLEAR_FRAME;
742 if (lt->selection.set()) {
743 lt->clearSelection();
745 } else if (owner()) {
746 bv->owner()->setLayout(owner()->getLyXText(bv)
747 ->cursor.par()->getLayout());
749 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
750 // hack for deleteEmptyParMech
751 lt->setCursor(bv, par, 0);
754 updateLocal(bv, code, false);
757 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
759 the_locking_inset = inset;
760 inset_x = cx(bv) - top_x + drawTextXOffset;
761 inset_y = cy(bv) + drawTextYOffset;
762 inset_pos = cpos(bv);
763 inset_par = cpar(bv);
764 inset_boundary = cboundary(bv);
765 updateLocal(bv, CURSOR, false);
769 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
771 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
775 if (!the_locking_inset) {
777 int const id = inset->id();
779 Paragraph::inset_iterator it =
780 p->inset_iterator_begin();
781 Paragraph::inset_iterator const end =
782 p->inset_iterator_end();
783 for (; it != end; ++it) {
784 if ((*it) == inset) {
785 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
786 lockInset(bv, inset);
789 if ((*it)->getInsetFromID(id)) {
790 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
791 lockInset(bv, static_cast<UpdatableInset *>(*it));
792 return the_locking_inset->lockInsetInInset(bv, inset);
799 if (inset == cpar(bv)->getInset(cpos(bv))) {
800 lyxerr[Debug::INSETS] << "OK" << endl;
801 lockInset(bv, inset);
803 } else if (the_locking_inset && (the_locking_inset == inset)) {
804 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
805 lyxerr[Debug::INSETS] << "OK" << endl;
806 inset_x = cx(bv) - top_x + drawTextXOffset;
807 inset_y = cy(bv) + drawTextYOffset;
809 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
811 } else if (the_locking_inset) {
812 lyxerr[Debug::INSETS] << "MAYBE" << endl;
813 return the_locking_inset->lockInsetInInset(bv, inset);
815 lyxerr[Debug::INSETS] << "NOT OK" << endl;
820 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
823 if (!the_locking_inset)
825 if (the_locking_inset == inset) {
826 the_locking_inset->insetUnlock(bv);
827 getLyXText(bv)->updateInset(bv, inset);
828 the_locking_inset = 0;
830 moveRight(bv, false);
831 old_par = 0; // force layout setting
835 updateLocal(bv, CURSOR, false);
838 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
842 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
844 if (!autoBreakRows && par->next())
845 collapseParagraphs(bv->buffer()->params);
853 if (!the_locking_inset) {
854 bool found = lt->updateInset(bv, inset);
858 setUpdateStatus(bv, NONE);
861 if (the_locking_inset != inset) {
862 bool found = the_locking_inset->updateInsetInInset(bv, inset);
866 setUpdateStatus(bv, CURSOR_PAR);
869 bool found = lt->updateInset(bv, inset);
873 setUpdateStatus(bv, CURSOR_PAR);
874 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
875 inset_x = cx(bv) - top_x + drawTextXOffset;
876 inset_y = cy(bv) + drawTextYOffset;
883 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
887 // use this to check mouse motion for selection!
891 int tmp_x = x - drawTextXOffset;
892 int tmp_y = y + insetAscent - getLyXText(bv)->first;
893 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
896 if (the_locking_inset) {
897 if (the_locking_inset == inset) {
898 the_locking_inset->insetButtonPress(bv,
902 no_selection = false;
905 // otherwise unlock the_locking_inset and lock the new inset
906 the_locking_inset->insetUnlock(bv);
907 inset_x = cx(bv) - top_x + drawTextXOffset;
908 inset_y = cy(bv) + drawTextYOffset;
909 the_locking_inset = static_cast<UpdatableInset*>(inset);
910 inset->insetButtonPress(bv, x - inset_x,
911 y - inset_y, button);
912 inset->edit(bv, x - inset_x, y - inset_y, button);
913 if (the_locking_inset)
914 updateLocal(bv, CURSOR, false);
915 no_selection = false;
918 // otherwise only unlock the_locking_inset
919 the_locking_inset->insetUnlock(bv);
920 the_locking_inset = 0;
922 if (bv->theLockingInset()) {
923 if (isHighlyEditableInset(inset)) {
924 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
925 inset_x = cx(bv) - top_x + drawTextXOffset;
926 inset_y = cy(bv) + drawTextYOffset;
927 inset_pos = cpos(bv);
928 inset_par = cpar(bv);
929 inset_boundary = cboundary(bv);
930 the_locking_inset = uinset;
931 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
933 uinset->edit(bv, x - inset_x, y - inset_y, 0);
934 if (the_locking_inset)
935 updateLocal(bv, CURSOR, false);
936 no_selection = false;
940 if (!inset) { // && (button == 2)) {
941 bool paste_internally = false;
942 if ((button == 2) && getLyXText(bv)->selection.set()) {
943 localDispatch(bv, LFUN_COPY, "");
944 paste_internally = true;
952 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
954 // set the selection cursor!
955 lt->selection.cursor = lt->cursor;
956 lt->cursor.x_fix(lt->cursor.x());
958 if (lt->selection.set()) {
959 lt->clearSelection();
962 updateLocal(bv, FULL, false);
964 lt->clearSelection();
967 updateLocal(bv, CURSOR, false);
969 bv->owner()->setLayout(cpar(bv)->getLayout());
971 // Insert primary selection with middle mouse
972 // if there is a local selection in the current buffer,
975 if (paste_internally)
976 localDispatch(bv, LFUN_PASTE, "");
978 localDispatch(bv, LFUN_PASTESELECTION,
982 getLyXText(bv)->clearSelection();
985 no_selection = false;
989 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
991 if (the_locking_inset) {
992 return the_locking_inset->insetButtonRelease(bv,
993 x - inset_x, y - inset_y,
996 int tmp_x = x - drawTextXOffset;
997 int tmp_y = y + insetAscent - getLyXText(bv)->first;
998 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1001 if (isHighlyEditableInset(inset)) {
1002 ret = inset->insetButtonRelease(bv, x - inset_x,
1003 y - inset_y, button);
1005 inset_x = cx(bv) - top_x + drawTextXOffset;
1006 inset_y = cy(bv) + drawTextYOffset;
1007 ret = inset->insetButtonRelease(bv, x - inset_x,
1008 y - inset_y, button);
1009 inset->edit(bv, x - inset_x,
1010 y - inset_y, button);
1012 updateLocal(bv, CURSOR_PAR, false);
1018 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1020 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1022 if (the_locking_inset) {
1023 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1029 lt = getLyXText(bv);
1032 hideInsetCursor(bv);
1033 LyXCursor cur = lt->cursor;
1034 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1035 if (cur == lt->cursor) {
1040 lt->setSelection(bv);
1041 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1042 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1046 updateLocal(bv, SELECTION, false);
1048 showInsetCursor(bv);
1052 void InsetText::insetKeyPress(XKeyEvent * xke)
1054 if (the_locking_inset) {
1055 the_locking_inset->insetKeyPress(xke);
1061 UpdatableInset::RESULT
1062 InsetText::localDispatch(BufferView * bv,
1063 kb_action action, string const & arg)
1065 bool was_empty = par->size() == 0 && !par->next();
1066 no_selection = false;
1067 UpdatableInset::RESULT
1068 result= UpdatableInset::localDispatch(bv, action, arg);
1069 if (result != UNDISPATCHED) {
1073 result = DISPATCHED;
1074 if ((action < 0) && arg.empty())
1077 if (the_locking_inset) {
1078 result = the_locking_inset->localDispatch(bv, action, arg);
1079 if (result == DISPATCHED_NOUPDATE)
1081 else if (result == DISPATCHED) {
1082 updateLocal(bv, CURSOR_PAR, false);
1084 } else if (result >= FINISHED) {
1086 case FINISHED_RIGHT:
1087 moveRightIntern(bv, false, false);
1088 result = DISPATCHED;
1091 if ((result = moveUp(bv)) >= FINISHED) {
1092 updateLocal(bv, CURSOR, false);
1093 bv->unlockInset(this);
1097 if ((result = moveDown(bv)) >= FINISHED) {
1098 updateLocal(bv, CURSOR, false);
1099 bv->unlockInset(this);
1103 result = DISPATCHED;
1106 the_locking_inset = 0;
1107 #ifdef WITH_WARNINGS
1108 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1113 hideInsetCursor(bv);
1116 lt = getLyXText(bv);
1120 int updflag = false;
1123 case LFUN_SELFINSERT:
1124 if (bv->buffer()->isReadonly()) {
1125 // setErrorMessage(N_("Document is read only"));
1129 /* Automatically delete the currently selected
1130 * text and replace it with what is being
1131 * typed in now. Depends on lyxrc settings
1132 * "auto_region_delete", which defaults to
1135 setUndo(bv, Undo::INSERT,
1136 lt->cursor.par(), lt->cursor.par()->next());
1138 if (lyxrc.auto_region_delete) {
1139 if (lt->selection.set()) {
1140 lt->cutSelection(bv, false);
1143 lt->clearSelection();
1144 for (string::size_type i = 0; i < arg.length(); ++i) {
1145 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1148 lt->selection.cursor = lt->cursor;
1149 updwhat = CURSOR_PAR;
1151 result = DISPATCHED_NOUPDATE;
1153 // --- Cursor Movements -----------------------------------
1156 moveRight(bv, false, true);
1157 lt->setSelection(bv);
1158 updwhat = SELECTION;
1161 result = moveRight(bv);
1167 moveLeft(bv, false, true);
1168 lt->setSelection(bv);
1169 updwhat = SELECTION;
1173 result = moveLeft(bv);
1179 lt->setSelection(bv);
1180 updwhat = SELECTION;
1184 result = moveDown(bv);
1190 lt->setSelection(bv);
1191 updwhat = SELECTION;
1195 result = moveUp(bv);
1207 case LFUN_BACKSPACE: {
1208 setUndo(bv, Undo::DELETE,
1209 lt->cursor.par(), lt->cursor.par()->next());
1210 if (lt->selection.set())
1211 lt->cutSelection(bv);
1214 updwhat = CURSOR_PAR;
1220 setUndo(bv, Undo::DELETE,
1221 lt->cursor.par(), lt->cursor.par()->next());
1222 if (lt->selection.set()) {
1223 lt->cutSelection(bv);
1227 updwhat = CURSOR_PAR;
1233 setUndo(bv, Undo::DELETE,
1234 lt->cursor.par(), lt->cursor.par()->next());
1235 lt->cutSelection(bv);
1236 updwhat = CURSOR_PAR;
1243 lt->copySelection(bv);
1244 updwhat = CURSOR_PAR;
1246 case LFUN_PASTESELECTION:
1248 string const clip(bv->getClipboard());
1252 if (arg == "paragraph") {
1253 lt->insertStringAsParagraphs(bv, clip);
1255 lt->insertStringAsLines(bv, clip);
1257 updwhat = CURSOR_PAR;
1262 if (!autoBreakRows) {
1264 if (CutAndPaste::nrOfParagraphs() > 1) {
1265 Alert::alert(_("Impossible operation"),
1266 _("Cannot include more than one paragraph!"),
1271 setUndo(bv, Undo::INSERT,
1272 lt->cursor.par(), lt->cursor.par()->next());
1273 lt->pasteSelection(bv);
1274 updwhat = CURSOR_PAR;
1279 case LFUN_BREAKPARAGRAPH:
1280 if (!autoBreakRows) {
1281 result = DISPATCHED;
1284 lt->breakParagraph(bv, 0);
1288 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1289 if (!autoBreakRows) {
1290 result = DISPATCHED;
1293 lt->breakParagraph(bv, 1);
1298 case LFUN_BREAKLINE: {
1299 if (!autoBreakRows) {
1300 result = DISPATCHED;
1303 setUndo(bv, Undo::INSERT,
1304 lt->cursor.par(), lt->cursor.par()->next());
1305 lt->insertChar(bv, Paragraph::META_NEWLINE);
1306 updwhat = CURSOR_PAR;
1312 // do not set layouts on non breakable textinsets
1313 if (autoBreakRows) {
1314 layout_type cur_layout = cpar(bv)->layout;
1316 // Derive layout number from given argument (string)
1317 // and current buffer's textclass (number). */
1318 textclass_type tclass = bv->buffer()->params.textclass;
1319 std::pair <bool, layout_type> layout =
1320 textclasslist.NumberOfLayout(tclass, arg);
1322 // If the entry is obsolete, use the new one instead.
1324 string obs = textclasslist.Style(tclass,layout.second).
1327 layout = textclasslist.NumberOfLayout(tclass, obs);
1330 // see if we found the layout number:
1331 if (!layout.first) {
1332 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1333 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1337 if (cur_layout != layout.second) {
1338 cur_layout = layout.second;
1339 lt->setLayout(bv, layout.second);
1340 bv->owner()->setLayout(cpar(bv)->getLayout());
1341 updwhat = CURSOR_PAR;
1345 // reset the layout box
1346 bv->owner()->setLayout(cpar(bv)->getLayout());
1349 case LFUN_PARAGRAPH_SPACING:
1350 // This one is absolutely not working. When fiddling with this
1351 // it also seems to me that the paragraphs inside the insettext
1352 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1354 Paragraph * par = lt->cursor.par();
1355 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1356 float cur_value = 1.0;
1357 if (cur_spacing == Spacing::Other) {
1358 cur_value = par->params().spacing().getValue();
1361 istringstream istr(arg.c_str());
1364 Spacing::Space new_spacing = cur_spacing;
1365 float new_value = cur_value;
1367 lyxerr << "Missing argument to `paragraph-spacing'"
1369 } else if (tmp == "single") {
1370 new_spacing = Spacing::Single;
1371 } else if (tmp == "onehalf") {
1372 new_spacing = Spacing::Onehalf;
1373 } else if (tmp == "double") {
1374 new_spacing = Spacing::Double;
1375 } else if (tmp == "other") {
1376 new_spacing = Spacing::Other;
1379 lyxerr << "new_value = " << tmpval << endl;
1382 } else if (tmp == "default") {
1383 new_spacing = Spacing::Default;
1385 lyxerr << _("Unknown spacing argument: ")
1388 if (cur_spacing != new_spacing || cur_value != new_value) {
1389 par->params().spacing(Spacing(new_spacing, new_value));
1390 updwhat = CURSOR_PAR;
1397 if (!bv->Dispatch(action, arg))
1398 result = UNDISPATCHED;
1405 updateLocal(bv, updwhat, updflag);
1406 /// If the action has deleted all text in the inset, we need to change the
1407 // language to the language of the surronding text.
1408 if (!was_empty && par->size() == 0 && !par->next()) {
1409 LyXFont font(LyXFont::ALL_IGNORE);
1410 font.setLanguage(bv->getParentLanguage(this));
1411 setFont(bv, font, false);
1414 if (result < FINISHED) {
1415 showInsetCursor(bv);
1417 bv->unlockInset(this);
1422 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1425 buf->latexParagraphs(os, par, 0, texrow);
1426 return texrow.rows();
1430 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1432 Paragraph * p = par;
1433 unsigned int lines = 0;
1436 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1437 lines += countChar(tmp, '\n');
1445 int InsetText::docbook(Buffer const * buf, ostream & os) const
1447 Paragraph * p = par;
1448 unsigned int lines = 0;
1450 vector<string> environment_stack(10);
1451 vector<string> environment_inner(10);
1453 int const command_depth = 0;
1456 Paragraph::depth_type depth = 0; // paragraph depth
1460 int desc_on = 0; // description mode
1462 LyXLayout const & style =
1463 textclasslist.Style(buf->params.textclass,
1466 // environment tag closing
1467 for (; depth > p->params().depth(); --depth) {
1468 if (environment_inner[depth] != "!-- --") {
1469 item_name = "listitem";
1470 buf->sgmlCloseTag(os, command_depth + depth,
1472 if (environment_inner[depth] == "varlistentry")
1473 buf->sgmlCloseTag(os, depth+command_depth,
1474 environment_inner[depth]);
1476 buf->sgmlCloseTag(os, depth + command_depth,
1477 environment_stack[depth]);
1478 environment_stack[depth].erase();
1479 environment_inner[depth].erase();
1482 if (depth == p->params().depth()
1483 && environment_stack[depth] != style.latexname()
1484 && !environment_stack[depth].empty()) {
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,
1491 depth + command_depth,
1492 environment_inner[depth]);
1495 buf->sgmlCloseTag(os, depth + command_depth,
1496 environment_stack[depth]);
1498 environment_stack[depth].erase();
1499 environment_inner[depth].erase();
1502 // Write opening SGML tags.
1503 switch (style.latextype) {
1504 case LATEX_PARAGRAPH:
1505 buf->sgmlOpenTag(os, depth + command_depth,
1510 buf->sgmlError(p, 0,
1511 _("Error : LatexType Command not allowed here.\n"));
1515 case LATEX_ENVIRONMENT:
1516 case LATEX_ITEM_ENVIRONMENT:
1517 if (depth < p->params().depth()) {
1518 depth = p->params().depth();
1519 environment_stack[depth].erase();
1522 if (environment_stack[depth] != style.latexname()) {
1523 if(environment_stack.size() == depth + 1) {
1524 environment_stack.push_back("!-- --");
1525 environment_inner.push_back("!-- --");
1527 environment_stack[depth] = style.latexname();
1528 environment_inner[depth] = "!-- --";
1529 buf->sgmlOpenTag(os, depth + command_depth,
1530 environment_stack[depth]);
1532 if (environment_inner[depth] != "!-- --") {
1533 item_name= "listitem";
1534 buf->sgmlCloseTag(os,
1535 command_depth + depth,
1537 if (environment_inner[depth] == "varlistentry")
1538 buf->sgmlCloseTag(os,
1539 depth + command_depth,
1540 environment_inner[depth]);
1544 if (style.latextype == LATEX_ENVIRONMENT) {
1545 if (!style.latexparam().empty()) {
1546 if(style.latexparam() == "CDATA")
1549 buf->sgmlOpenTag(os, depth + command_depth,
1550 style.latexparam());
1555 desc_on = (style.labeltype == LABEL_MANUAL);
1558 environment_inner[depth]= "varlistentry";
1560 environment_inner[depth]= "listitem";
1562 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1563 environment_inner[depth]);
1567 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1571 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1576 buf->sgmlOpenTag(os, depth + command_depth,
1581 buf->simpleDocBookOnePar(os, p, desc_on,
1582 depth + 1 + command_depth);
1586 // write closing SGML tags
1587 switch (style.latextype) {
1588 case LATEX_ENVIRONMENT:
1589 if (!style.latexparam().empty()) {
1590 if(style.latexparam() == "CDATA")
1593 buf->sgmlCloseTag(os, depth + command_depth,
1594 style.latexparam());
1597 case LATEX_ITEM_ENVIRONMENT:
1598 if (desc_on == 1) break;
1600 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1602 case LATEX_PARAGRAPH:
1603 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1606 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1612 for (int d = depth; d >= 0; --d) {
1613 if (!environment_stack[depth].empty()) {
1614 if (environment_inner[depth] != "!-- --") {
1615 item_name = "listitem";
1616 buf->sgmlCloseTag(os, command_depth + depth,
1618 if (environment_inner[depth] == "varlistentry")
1619 buf->sgmlCloseTag(os, depth + command_depth,
1620 environment_inner[depth]);
1623 buf->sgmlCloseTag(os, depth + command_depth,
1624 environment_stack[depth]);
1632 void InsetText::validate(LaTeXFeatures & features) const
1634 Paragraph * p = par;
1636 p->validate(features);
1642 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1644 if (textclasslist.Style(buf->params.textclass,
1645 p->getLayout()).labeltype != LABEL_MANUAL)
1648 return p->beginningOfMainBody();
1652 void InsetText::getCursorPos(BufferView * bv,
1653 int & x, int & y) const
1655 if (the_locking_inset) {
1656 the_locking_inset->getCursorPos(bv, x, y);
1664 unsigned int InsetText::insetInInsetY()
1666 if (!the_locking_inset)
1669 return (inset_y + the_locking_inset->insetInInsetY());
1673 void InsetText::toggleInsetCursor(BufferView * bv)
1675 if (the_locking_inset) {
1676 the_locking_inset->toggleInsetCursor(bv);
1680 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1682 int const asc = lyxfont::maxAscent(font);
1683 int const desc = lyxfont::maxDescent(font);
1685 if (isCursorVisible())
1686 bv->hideLockedInsetCursor();
1688 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1689 toggleCursorVisible();
1693 void InsetText::showInsetCursor(BufferView * bv, bool show)
1695 if (the_locking_inset) {
1696 the_locking_inset->showInsetCursor(bv, show);
1699 if (!isCursorVisible()) {
1700 LyXFont const font =
1701 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1703 int const asc = lyxfont::maxAscent(font);
1704 int const desc = lyxfont::maxDescent(font);
1706 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1708 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1709 setCursorVisible(true);
1714 void InsetText::hideInsetCursor(BufferView * bv)
1716 if (isCursorVisible()) {
1717 bv->hideLockedInsetCursor();
1718 setCursorVisible(false);
1720 if (the_locking_inset)
1721 the_locking_inset->hideInsetCursor(bv);
1725 void InsetText::fitInsetCursor(BufferView * bv) const
1727 if (the_locking_inset) {
1728 the_locking_inset->fitInsetCursor(bv);
1731 LyXFont const font =
1732 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1734 int const asc = lyxfont::maxAscent(font);
1735 int const desc = lyxfont::maxDescent(font);
1737 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1741 UpdatableInset::RESULT
1742 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1744 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1745 return moveLeftIntern(bv, false, activate_inset, selecting);
1747 return moveRightIntern(bv, false, activate_inset, selecting);
1751 UpdatableInset::RESULT
1752 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1754 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1755 return moveRightIntern(bv, true, activate_inset, selecting);
1757 return moveLeftIntern(bv, true, activate_inset, selecting);
1761 UpdatableInset::RESULT
1762 InsetText::moveRightIntern(BufferView * bv, bool behind,
1763 bool activate_inset, bool selecting)
1765 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1766 return FINISHED_RIGHT;
1767 if (activate_inset && checkAndActivateInset(bv, behind))
1769 getLyXText(bv)->cursorRight(bv);
1771 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1772 return DISPATCHED_NOUPDATE;
1776 UpdatableInset::RESULT
1777 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1778 bool activate_inset, bool selecting)
1780 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1782 getLyXText(bv)->cursorLeft(bv);
1784 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1785 if (activate_inset && checkAndActivateInset(bv, behind))
1787 return DISPATCHED_NOUPDATE;
1791 UpdatableInset::RESULT
1792 InsetText::moveUp(BufferView * bv)
1794 if (!crow(bv)->previous())
1796 getLyXText(bv)->cursorUp(bv);
1797 return DISPATCHED_NOUPDATE;
1801 UpdatableInset::RESULT
1802 InsetText::moveDown(BufferView * bv)
1804 if (!crow(bv)->next())
1805 return FINISHED_DOWN;
1806 getLyXText(bv)->cursorDown(bv);
1807 return DISPATCHED_NOUPDATE;
1811 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1813 if (the_locking_inset) {
1814 if (the_locking_inset->insetAllowed(inset))
1815 return the_locking_inset->insertInset(bv, inset);
1820 lt = getLyXText(bv);
1823 setUndo(bv, Undo::FINISH, lt->cursor.par(), lt->cursor.par()->next());
1825 inset->setOwner(this);
1826 hideInsetCursor(bv);
1827 lt->insertInset(bv, inset);
1831 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1837 bool InsetText::insetAllowed(Inset::Code code) const
1839 // in_insetAllowed is a really gross hack,
1840 // to allow us to call the owner's insetAllowed
1841 // without stack overflow, which can happen
1842 // when the owner uses InsetCollapsable::insetAllowed()
1844 if (in_insetAllowed)
1846 in_insetAllowed = true;
1847 if (the_locking_inset)
1848 ret = the_locking_inset->insetAllowed(code);
1850 ret = owner()->insetAllowed(code);
1851 in_insetAllowed = false;
1856 UpdatableInset * InsetText::getLockingInset() const
1858 return the_locking_inset ? the_locking_inset->getLockingInset() :
1859 const_cast<InsetText *>(this);
1863 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1867 if (the_locking_inset)
1868 return the_locking_inset->getFirstLockingInsetOfType(c);
1873 bool InsetText::showInsetDialog(BufferView * bv) const
1875 if (the_locking_inset)
1876 return the_locking_inset->showInsetDialog(bv);
1881 std::vector<string> const InsetText::getLabelList() const
1883 std::vector<string> label_list;
1885 Paragraph * tpar = par;
1887 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1888 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1889 for (; beg != end; ++beg) {
1890 std::vector<string> const l = (*beg)->getLabelList();
1891 label_list.insert(label_list.end(), l.begin(), l.end());
1893 tpar = tpar->next();
1899 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1902 if (the_locking_inset) {
1903 the_locking_inset->setFont(bv, font, toggleall, selectall);
1906 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1907 getLyXText(bv)->setFont(bv, font, toggleall);
1912 lt = getLyXText(bv);
1915 if (lt->selection.set()) {
1916 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1920 lt->toggleFree(bv, font, toggleall);
1922 lt->clearSelection();
1924 bool flag = (selectall || lt->selection.set());
1928 updateLocal(bv, FULL, true);
1930 updateLocal(bv, CURSOR_PAR, true);
1934 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1936 if (cpar(bv)->isInset(cpos(bv))) {
1940 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1941 if (!isHighlyEditableInset(inset))
1943 LyXFont const font =
1944 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1946 x = inset->width(bv, font);
1947 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1950 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1952 //inset_x = cx(bv) - top_x + drawTextXOffset;
1953 //inset_y = cy(bv) + drawTextYOffset;
1954 inset->edit(bv, x, y, 0);
1955 if (!the_locking_inset)
1957 updateLocal(bv, CURSOR, false);
1964 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1967 x -= drawTextXOffset;
1969 int dummyy = y + insetAscent;
1970 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
1977 inset_x = cx(bv) - top_x + drawTextXOffset;
1978 inset_y = cy(bv) + drawTextYOffset;
1979 inset->edit(bv, x - inset_x, y - inset_y, button);
1980 if (!the_locking_inset)
1982 updateLocal(bv, CURSOR, false);
1989 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1992 int w = UpdatableInset::getMaxWidth(bv, inset);
1997 w = w - top_x + owner()->x();
2000 w -= (2 * TEXT_TO_INSET_OFFSET);
2003 return UpdatableInset::getMaxWidth(bv, inset);
2008 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2010 // we have to unlock any locked inset otherwise we're in troubles
2011 the_locking_inset = 0;
2013 Paragraph * tmp = par->next();
2018 par = new Paragraph(*p, same_id);
2019 par->setInsetOwner(this);
2020 Paragraph * np = par;
2023 np->next(new Paragraph(*p, same_id));
2024 np->next()->previous(np);
2026 np->setInsetOwner(this);
2033 void InsetText::setText(string const & data)
2036 LyXFont font(LyXFont::ALL_SANE);
2037 for (unsigned int i=0; i < data.length(); ++i)
2038 par->insertChar(i, data[i], font);
2042 void InsetText::setAutoBreakRows(bool flag)
2044 if (flag != autoBreakRows) {
2045 autoBreakRows = flag;
2053 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2055 if (how != drawFrame_) {
2058 updateLocal(bv, DRAW_FRAME, false);
2063 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2065 if (frame_color != col) {
2068 updateLocal(bv, DRAW_FRAME, false);
2073 int InsetText::cx(BufferView * bv) const
2075 // we do nothing dangerous so we use a local cache
2076 LyXText * llt = getLyXText(bv);
2077 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2078 if (the_locking_inset) {
2079 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2081 if (font.isVisibleRightToLeft())
2082 x -= the_locking_inset->width(bv, font);
2088 int InsetText::cy(BufferView * bv) const
2091 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2095 pos_type InsetText::cpos(BufferView * bv) const
2097 return getLyXText(bv)->cursor.pos();
2101 Paragraph * InsetText::cpar(BufferView * bv) const
2103 return getLyXText(bv)->cursor.par();
2107 bool InsetText::cboundary(BufferView * bv) const
2109 return getLyXText(bv)->cursor.boundary();
2113 Row * InsetText::crow(BufferView * bv) const
2115 return getLyXText(bv)->cursor.row();
2119 LyXText * InsetText::getLyXText(BufferView const * lbv,
2120 bool const recursive) const
2122 if (!recursive && (cached_bview == lbv)) {
2123 LyXText * lt = cached_text.get();
2124 lyx::Assert(lt && lt->firstRow()->par() == par);
2125 return cached_text.get();
2128 // Super UGLY! (Lgb)
2129 BufferView * bv = const_cast<BufferView *>(lbv);
2132 Cache::iterator it = cache.find(bv);
2134 if (it != cache.end()) {
2137 } else if (do_resize) {
2138 resizeLyXText(do_resize);
2140 if (lt || !it->second.remove) {
2141 lyx::Assert(it->second.text.get());
2142 cached_text = it->second.text;
2143 if (recursive && the_locking_inset) {
2144 return the_locking_inset->getLyXText(bv, true);
2146 return cached_text.get();
2147 } else if (it->second.remove) {
2149 saveLyXTextState(it->second.text.get());
2155 // when we have to reinit the existing LyXText!
2157 it->second.text->init(bv);
2158 restoreLyXTextState(bv, it->second.text.get());
2159 it->second.remove = false;
2161 cached_text = it->second.text;
2162 if (the_locking_inset && recursive) {
2163 return the_locking_inset->getLyXText(bv);
2165 return cached_text.get();
2168 // we are here only if we don't have a BufferView * in the cache!!!
2170 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2171 cached_text->init(bv);
2172 restoreLyXTextState(bv, cached_text.get());
2174 cache.insert(make_pair(bv, cached_text));
2176 if (the_locking_inset && recursive) {
2177 return the_locking_inset->getLyXText(bv);
2179 return cached_text.get();
2183 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2187 Cache::iterator it = cache.find(bv);
2189 if (it == cache.end()) {
2193 lyx::Assert(it->second.text.get());
2195 it->second.remove = true;
2197 /// then remove all LyXText in text-insets
2198 Paragraph * p = par;
2199 for (; p; p = p->next()) {
2200 p->deleteInsetsLyXText(bv);
2206 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2209 // we cannot resize this because we are in use!
2210 // so do this on the next possible getLyXText()
2215 // lyxerr << "InsetText::resizeLyXText\n";
2216 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2217 // we have to do this as a fixed width may have changed!
2218 LyXText * t = getLyXText(bv);
2219 saveLyXTextState(t);
2221 restoreLyXTextState(bv, t);
2224 // one endless line, resize normally not necessary
2225 if (!force && getMaxWidth(bv, this) < 0)
2228 Cache::iterator it = cache.find(bv);
2229 if (it == cache.end()) {
2232 lyx::Assert(it->second.text.get());
2234 LyXText * t = it->second.text.get();
2235 saveLyXTextState(t);
2236 for (Paragraph * p = par; p; p = p->next()) {
2237 p->resizeInsetsLyXText(bv);
2240 restoreLyXTextState(bv, t);
2241 if (the_locking_inset) {
2242 inset_x = cx(bv) - top_x + drawTextXOffset;
2243 inset_y = cy(bv) + drawTextYOffset;
2247 t->first = bv->screen()->topCursorVisible(t);
2250 updateLocal(bv, FULL, false);
2251 // this will scroll the screen such that the cursor becomes visible
2252 bv->updateScrollbar();
2254 need_update |= FULL;
2259 void InsetText::reinitLyXText() const
2262 // we cannot resize this because we are in use!
2263 // so do this on the next possible getLyXText()
2269 // lyxerr << "InsetText::reinitLyXText\n";
2270 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2271 lyx::Assert(it->second.text.get());
2273 LyXText * t = it->second.text.get();
2274 BufferView * bv = it->first;
2276 saveLyXTextState(t);
2277 for (Paragraph * p = par; p; p = p->next()) {
2278 p->resizeInsetsLyXText(bv);
2281 restoreLyXTextState(bv, t);
2282 if (the_locking_inset) {
2283 inset_x = cx(bv) - top_x + drawTextXOffset;
2284 inset_y = cy(bv) + drawTextYOffset;
2287 t->first = bv->screen()->topCursorVisible(t);
2290 updateLocal(bv, FULL, false);
2291 // this will scroll the screen such that the cursor becomes visible
2292 bv->updateScrollbar();
2300 void InsetText::removeNewlines()
2302 bool changed = false;
2304 for (Paragraph * p = par; p; p = p->next()) {
2305 for (int i = 0; i < p->size(); ++i) {
2306 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2317 bool InsetText::nodraw() const
2319 if (the_locking_inset)
2320 return the_locking_inset->nodraw();
2321 return UpdatableInset::nodraw();
2325 int InsetText::scroll(bool recursive) const
2327 int sx = UpdatableInset::scroll(false);
2329 if (recursive && the_locking_inset)
2330 sx += the_locking_inset->scroll(recursive);
2336 bool InsetText::doClearArea() const
2338 return !locked || (need_update & (FULL|INIT));
2342 void InsetText::selectAll(BufferView * bv)
2344 getLyXText(bv)->cursorTop(bv);
2345 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2346 getLyXText(bv)->cursorBottom(bv);
2347 getLyXText(bv)->setSelection(bv);
2351 void InsetText::clearSelection(BufferView * bv)
2353 getLyXText(bv)->clearSelection();
2357 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2359 Painter & pain = bv->painter();
2361 int h = insetAscent + insetDescent;
2362 int ty = baseline - insetAscent;
2368 if ((ty + h) > pain.paperHeight())
2369 h = pain.paperHeight();
2370 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2371 w = pain.paperWidth();
2372 // w -= TEXT_TO_INSET_OFFSET;
2373 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2376 frame_is_visible = false;
2380 Paragraph * InsetText::getParFromID(int id) const
2383 Paragraph * result = par;
2384 Paragraph * ires = 0;
2385 while (result && result->id() != id) {
2386 if ((ires = result->getParFromID(id)))
2388 result = result->next();
2392 Paragraph * tmp = par;
2394 if (tmp->id() == id) {
2397 Paragraph * tmp2 = tmp->getParFromID(id);
2408 Paragraph * InsetText::firstParagraph() const
2411 if (the_locking_inset)
2412 if ((result = the_locking_inset->firstParagraph()))
2418 Paragraph * InsetText::getFirstParagraph(int i) const
2420 return (i == 0) ? par : 0;
2424 LyXCursor const & InsetText::cursor(BufferView * bv) const
2426 if (the_locking_inset)
2427 return the_locking_inset->cursor(bv);
2428 return getLyXText(bv)->cursor;
2432 Paragraph * InsetText::paragraph() const
2438 void InsetText::paragraph(Paragraph * p)
2440 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2441 // caller of this function has to take care of it. This IS important
2442 // as we could have to insert a paragraph before this one and just
2443 // link the actual to a new ones next and set it with this function
2446 // set ourself as owner for all the paragraphs inserted!
2447 Paragraph * np = par;
2449 np->setInsetOwner(this);
2453 // redraw myself when asked for
2458 Inset * InsetText::getInsetFromID(int id_arg) const
2461 return const_cast<InsetText *>(this);
2463 Paragraph * lp = par;
2466 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2467 en = lp->inset_iterator_end();
2470 if ((*it)->id() == id_arg)
2472 Inset * in = (*it)->getInsetFromID(id_arg);
2482 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2488 lt = getLyXText(bv);
2491 if (the_locking_inset) {
2492 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2499 #warning Dekel please have a look on this one RTL? (Jug)
2501 // we have to go on checking so move cusor to the right
2502 lt->cursor.pos(lt->cursor.pos() + 1);
2504 str = lt->selectNextWordToSpellcheck(bv, value);
2506 bv->unlockInset(const_cast<InsetText *>(this));
2515 void InsetText::selectSelectedWord(BufferView * bv)
2517 if (the_locking_inset) {
2518 the_locking_inset->selectSelectedWord(bv);
2521 getLyXText(bv)->selectSelectedWord(bv);
2522 updateLocal(bv, SELECTION, false);
2526 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2528 if (the_locking_inset) {
2529 the_locking_inset->toggleSelection(bv, kill_selection);
2533 lt = getLyXText(bv);
2537 int x = top_x + TEXT_TO_INSET_OFFSET;
2539 Row * row = lt->firstRow();
2540 int y_offset = top_baseline - row->ascent_of_text();
2542 while ((row != 0) && ((y+row->height()) <= 0)) {
2549 if (need_update & SELECTION)
2551 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2557 bool InsetText::searchForward(BufferView * bv, string const & str,
2560 if (the_locking_inset) {
2561 if (the_locking_inset->searchForward(bv, str, cs, mw))
2565 lt = getLyXText(bv);
2568 Paragraph * lpar = lt->cursor.par();
2569 pos_type pos = lt->cursor.pos();
2570 if (pos < lpar->size() - 1)
2574 lpar = lpar->next();
2579 // we have to unlock ourself in this function by default!
2580 bv->unlockInset(const_cast<InsetText *>(this));
2583 lt->setCursor(bv, lpar, pos);
2587 if (LyXFind(bv, str, true, true, cs , mw)) {
2590 // we have to unlock ourself in this function by default!
2591 bv->unlockInset(const_cast<InsetText *>(this));
2595 bool InsetText::searchBackward(BufferView * bv, string const & str,
2598 if (the_locking_inset)
2599 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2601 if (LyXFind(bv, str, false, true, cs, mw)) {
2604 // we have to unlock ourself in this function by default!
2605 bv->unlockInset(const_cast<InsetText *>(this));
2610 bool InsetText::checkInsertChar(LyXFont & font)
2613 return owner()->checkInsertChar(font);
2618 void InsetText::collapseParagraphs(BufferParams const & bparams) const
2620 while(par->next()) {
2621 if (!par->isSeparator(par->size()-1))
2622 par->insertChar(par->size()-1, ' ');
2623 par->pasteParagraph(bparams);