1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team.
8 * ======================================================
20 #pragma implementation
23 #include "insettext.h"
24 #include "paragraph.h"
28 #include "commandtags.h"
31 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
35 #include "frontends/Alert.h"
37 #include "lyxcursor.h"
38 #include "CutAndPaste.h"
41 #include "support/textutils.h"
42 #include "support/LAssert.h"
43 #include "support/lstrings.h"
47 #include "trans_mgr.h"
48 #include "lyxscreen.h"
52 #include "ParagraphParameters.h"
53 #include "undo_funcs.h"
65 using lyx::layout_type;
66 using lyx::textclass_type;
68 extern unsigned char getCurrentTextClass(Buffer *);
69 extern bool math_insert_greek(BufferView *, char);
70 extern int greek_kb_flag;
73 // These functions should probably go into bufferview_funcs somehow (Jug)
75 void InsetText::saveLyXTextState(LyXText * t) const
77 // check if my paragraphs are still valid
80 if (p == t->cursor.par())
85 if (p && t->cursor.pos() <= p->size()) {
86 sstate.lpar = t->cursor.par();
87 sstate.pos = t->cursor.pos();
88 sstate.boundary = t->cursor.boundary();
89 sstate.selstartpar = t->selection.start.par();
90 sstate.selstartpos = t->selection.start.pos();
91 sstate.selstartboundary = t->selection.start.boundary();
92 sstate.selendpar = t->selection.end.par();
93 sstate.selendpos = t->selection.end.pos();
94 sstate.selendboundary = t->selection.end.boundary();
95 sstate.selection = t->selection.set();
96 sstate.mark_set = t->selection.mark();
97 sstate.refresh = t->refresh_row != 0;
103 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
106 t->selection.set(true);
107 /* at this point just to avoid the Delete-Empty-Paragraph
108 * Mechanism when setting the cursor */
109 t->selection.mark(sstate.mark_set);
110 if (sstate.selection) {
111 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
112 true, sstate.selstartboundary);
113 t->selection.cursor = t->cursor;
114 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
115 true, sstate.selendboundary);
117 t->setCursor(bv, sstate.lpar, sstate.pos);
119 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
120 t->selection.cursor = t->cursor;
121 t->selection.set(false);
123 if (sstate.refresh) {
129 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
136 InsetText::InsetText()
137 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
145 InsetText::InsetText(InsetText const & in, bool same_id)
146 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
154 InsetText & InsetText::operator=(InsetText const & it)
161 void InsetText::init(InsetText const * ins, bool same_id)
164 setParagraphData(ins->par, same_id);
165 autoBreakRows = ins->autoBreakRows;
166 drawFrame_ = ins->drawFrame_;
167 frame_color = ins->frame_color;
173 p->setInsetOwner(this);
176 the_locking_inset = 0;
178 frame_color = LColor::insetframe;
179 autoBreakRows = false;
188 no_selection = false;
195 last_drawn_width = -1;
196 frame_is_visible = 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
301 insetAscent = lt->firstRow()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
308 int InsetText::descent(BufferView * bv, LyXFont const &) const
315 insetDescent = lt->height - lt->firstRow()->ascent_of_text() +
316 TEXT_TO_INSET_OFFSET;
323 int InsetText::width(BufferView * bv, LyXFont const &) const
325 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
326 (2 * TEXT_TO_INSET_OFFSET);
327 insetWidth = max(insetWidth, 10);
332 int InsetText::textWidth(BufferView * bv, bool fordraw) const
335 if (!autoBreakRows) {
338 w = getMaxWidth(bv, this);
341 return max(w - (2 * TEXT_TO_INSET_OFFSET),
342 (int)getLyXText(bv)->width);
346 return w - (2 * TEXT_TO_INSET_OFFSET);
350 void InsetText::draw(BufferView * bv, LyXFont const & f,
351 int baseline, float & x, bool cleared) const
356 Painter & pain = bv->painter();
358 // this is the first thing we have to ask because if the x pos
359 // changed we have to do a complete rebreak of the text as we
360 // may have few space to draw in. Well we should check on this too
362 if (top_x != int(x)) {
364 int nw = getMaxWidth(bv, this);
365 if (nw > 0 && old_max_width != nw) {
368 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
376 // repaint the background if needed
377 if (cleared && backgroundColor() != LColor::background) {
379 clearInset(bv, baseline, cleared);
383 // no draw is necessary !!!
384 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
387 top_baseline = baseline;
389 if (need_update & CLEAR_FRAME)
390 clearFrame(pain, cleared);
397 x += static_cast<float>(scroll());
399 // if top_x differs we did it already
400 if (!cleared && (top_x == int(x)) &&
401 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
402 (last_drawn_width!=insetWidth))) {
403 clearInset(bv, baseline, cleared);
409 frame_is_visible = false;
411 if (!cleared && (need_update == NONE)) {
413 drawFrame(pain, cleared);
417 top_baseline = baseline;
418 top_y = baseline - ascent(bv, f);
419 last_width = width(bv, f);
420 last_height = ascent(bv, f) + descent(bv, f);
422 if (last_drawn_width != insetWidth) {
424 clearInset(bv, baseline, cleared);
426 last_drawn_width = insetWidth;
429 if (the_locking_inset && (cpar(bv) == inset_par)
430 && (cpos(bv) == inset_pos)) {
431 inset_x = cx(bv) - top_x + drawTextXOffset;
432 inset_y = cy(bv) + drawTextYOffset;
434 if (!cleared && (need_update == CURSOR)
435 && !getLyXText(bv)->selection.set()) {
436 drawFrame(pain, cleared);
437 x += last_width; // was width(bv, f);
446 x += TEXT_TO_INSET_OFFSET;
448 Row * row = lt->firstRow();
449 int y_offset = baseline - row->ascent_of_text();
450 int ph = pain.paperHeight();
453 while ((row != 0) && ((y+row->height()) <= 0)) {
455 first += row->height();
461 if (cleared || (need_update&(INIT|FULL))) {
464 while ((row != 0) && (yf < ph)) {
465 lt->getVisibleRow(bv, y+y_offset, int(x), row,
471 } else if (!locked) {
472 if (need_update & CURSOR) {
473 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
474 lt->clearSelection();
475 lt->selection.cursor = lt->cursor;
477 bv->screen()->update(lt, bv, y_offset, int(x));
480 if (need_update & SELECTION) {
481 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
482 } else if (need_update & CURSOR) {
483 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
484 lt->clearSelection();
485 lt->selection.cursor = lt->cursor;
487 bv->screen()->update(lt, bv, y_offset, int(x));
492 lt->status(bv, LyXText::UNCHANGED);
493 if ((need_update != CURSOR_PAR) &&
494 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked))) {
495 drawFrame(pain, cleared);
496 } else if (need_update & CLEAR_FRAME) {
497 clearFrame(pain, cleared);
500 x += last_width /* was width(bv, f) */ - TEXT_TO_INSET_OFFSET;
502 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
504 } else if (need_update != INIT) {
513 void InsetText::drawFrame(Painter & pain, bool cleared) const
515 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
516 if (!frame_is_visible || cleared) {
517 frame_x = top_x + ttoD2;
518 frame_y = top_baseline - insetAscent + ttoD2;
519 frame_w = last_width - TEXT_TO_INSET_OFFSET;
520 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
521 pain.rectangle(frame_x, frame_y,
524 frame_is_visible = true;
529 void InsetText::clearFrame(Painter & pain, bool cleared) const
531 if (frame_is_visible) {
533 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
536 frame_is_visible = false;
541 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
544 if (reinit && owner()) {
546 owner()->update(bv, font, true);
551 if (reinit || need_update == INIT) {
553 // we should put this call where we set need_update to INIT!
556 owner()->update(bv, font, true);
560 if (the_locking_inset) {
561 inset_x = cx(bv) - top_x + drawTextXOffset;
562 inset_y = cy(bv) + drawTextYOffset;
563 the_locking_inset->update(bv, font, reinit);
572 int oldw = insetWidth;
573 insetWidth = lt->width + (2 * TEXT_TO_INSET_OFFSET);
574 if (oldw != insetWidth) {
583 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
586 lt->updateInset(bv, the_locking_inset);
588 if (lt->status() == LyXText::NEED_MORE_REFRESH)
596 void InsetText::setUpdateStatus(BufferView * bv, int what) const
604 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
605 // if we don't break row so that we only have one row to update!
606 if ((lt->status() == LyXText::NEED_MORE_REFRESH) ||
608 (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
611 } else if (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
612 need_update |= CURSOR_PAR;
615 // this to not draw a selection when we redraw all of it!
616 if (need_update & CURSOR && !(need_update & SELECTION)) {
617 if (lt->selection.set())
619 lt->clearSelection();
626 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
634 setUpdateStatus(bv, what);
635 if (((need_update != CURSOR) && (need_update != NONE)) ||
636 (lt->status() != LyXText::UNCHANGED) || lt->selection.set())
638 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
640 if (need_update == CURSOR)
642 bv->owner()->showState();
643 if (old_par != cpar(bv)) {
644 bv->owner()->setLayout(cpar(bv)->getLayout());
652 string const InsetText::editMessage() const
654 return _("Opened Text Inset");
658 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
660 UpdatableInset::edit(bv, x, y, button);
662 if (!bv->lockInset(this)) {
663 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
667 the_locking_inset = 0;
668 inset_pos = inset_x = inset_y = 0;
669 inset_boundary = false;
672 int tmp_y = (y < 0) ? 0 : y;
679 if (!checkAndActivateInset(bv, x, tmp_y, button))
680 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
682 lt->clearSelection();
685 updateLocal(bv, CURSOR, false);
687 // If the inset is empty set the language of the current font to the
688 // language to the surronding text (if different).
689 if (par->size() == 0 && !par->next() &&
690 bv->getParentLanguage(this) != lt->current_font.language()) {
691 LyXFont font(LyXFont::ALL_IGNORE);
692 font.setLanguage(bv->getParentLanguage(this));
693 setFont(bv, font, false);
700 void InsetText::edit(BufferView * bv, bool front)
702 UpdatableInset::edit(bv, front);
704 if (!bv->lockInset(this)) {
705 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
709 the_locking_inset = 0;
710 inset_pos = inset_x = inset_y = 0;
711 inset_boundary = false;
720 lt->setCursor(bv, par, 0);
725 // int const pos = (p->size() ? p->size()-1 : p->size());
726 lt->setCursor(bv, p, p->size());
728 lt->clearSelection();
731 updateLocal(bv, CURSOR, false);
733 // If the inset is empty set the language of the current font to the
734 // language to the surronding text (if different).
735 if (par->size() == 0 && !par->next() &&
736 bv->getParentLanguage(this) != lt->current_font.language()) {
737 LyXFont font(LyXFont::ALL_IGNORE);
738 font.setLanguage(bv->getParentLanguage(this));
739 setFont(bv, font, false);
746 void InsetText::insetUnlock(BufferView * bv)
748 if (the_locking_inset) {
749 the_locking_inset->insetUnlock(bv);
750 the_locking_inset = 0;
753 no_selection = false;
756 if (drawFrame_ == LOCKED)
757 code = CURSOR|CLEAR_FRAME;
765 if (lt->selection.set()) {
766 lt->clearSelection();
768 } else if (owner()) {
769 bv->owner()->setLayout(owner()->getLyXText(bv)
770 ->cursor.par()->getLayout());
772 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
773 // hack for deleteEmptyParMech
774 lt->setCursor(bv, par, 0);
775 updateLocal(bv, code, false);
781 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
783 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
787 if (inset == cpar(bv)->getInset(cpos(bv))) {
788 lyxerr[Debug::INSETS] << "OK" << endl;
789 the_locking_inset = inset;
790 inset_x = cx(bv) - top_x + drawTextXOffset;
791 inset_y = cy(bv) + drawTextYOffset;
792 inset_pos = cpos(bv);
793 inset_par = cpar(bv);
794 inset_boundary = cboundary(bv);
795 updateLocal(bv, CURSOR, false);
797 } else if (the_locking_inset && (the_locking_inset == inset)) {
798 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
799 lyxerr[Debug::INSETS] << "OK" << endl;
800 inset_x = cx(bv) - top_x + drawTextXOffset;
801 inset_y = cy(bv) + drawTextYOffset;
803 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
805 } else if (the_locking_inset) {
806 lyxerr[Debug::INSETS] << "MAYBE" << endl;
807 return the_locking_inset->lockInsetInInset(bv, inset);
809 lyxerr[Debug::INSETS] << "NOT OK" << endl;
814 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
817 if (!the_locking_inset)
819 if (the_locking_inset == inset) {
820 the_locking_inset->insetUnlock(bv);
821 getLyXText(bv)->updateInset(bv, inset);
822 the_locking_inset = 0;
824 moveRight(bv, false);
825 old_par = 0; // force layout setting
829 updateLocal(bv, CURSOR, false);
832 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
836 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
843 if (!the_locking_inset) {
844 bool found = lt->updateInset(bv, inset);
848 setUpdateStatus(bv, NONE);
851 if (the_locking_inset != inset) {
853 lt->updateInset(bv, the_locking_inset);
854 setUpdateStatus(bv, CURSOR_PAR);
856 bool found = the_locking_inset->updateInsetInInset(bv, inset);
860 setUpdateStatus(bv, CURSOR_PAR);
863 bool found = lt->updateInset(bv, inset);
867 setUpdateStatus(bv, CURSOR_PAR);
869 updateLocal(bv, CURSOR_PAR, false);
871 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
872 inset_x = cx(bv) - top_x + drawTextXOffset;
873 inset_y = cy(bv) + drawTextYOffset;
880 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
884 int tmp_x = x - drawTextXOffset;
885 int tmp_y = y + insetAscent - getLyXText(bv)->first;
886 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
889 if (the_locking_inset) {
890 if (the_locking_inset == inset) {
891 the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
892 no_selection = false;
895 // otherwise unlock the_locking_inset and lock the new inset
896 the_locking_inset->insetUnlock(bv);
897 inset_x = cx(bv) - top_x + drawTextXOffset;
898 inset_y = cy(bv) + drawTextYOffset;
899 the_locking_inset = static_cast<UpdatableInset*>(inset);
900 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
901 inset->edit(bv, x - inset_x, y - inset_y, button);
902 if (the_locking_inset)
903 updateLocal(bv, CURSOR, false);
904 no_selection = false;
907 // otherwise only unlock the_locking_inset
908 the_locking_inset->insetUnlock(bv);
909 the_locking_inset = 0;
911 if (bv->theLockingInset()) {
912 if (isHighlyEditableInset(inset)) {
913 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
914 inset_x = cx(bv) - top_x + drawTextXOffset;
915 inset_y = cy(bv) + drawTextYOffset;
916 inset_pos = cpos(bv);
917 inset_par = cpar(bv);
918 inset_boundary = cboundary(bv);
919 the_locking_inset = uinset;
920 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
922 uinset->edit(bv, x - inset_x, y - inset_y, 0);
923 if (the_locking_inset)
924 updateLocal(bv, CURSOR, false);
925 no_selection = false;
929 if (!inset) { // && (button == 2)) {
930 bool paste_internally = false;
931 if ((button == 2) && getLyXText(bv)->selection.set()) {
932 localDispatch(bv, LFUN_COPY, "");
933 paste_internally = true;
940 lt->setCursorFromCoordinates(bv, x-drawTextXOffset, y + insetAscent);
941 if (lt->selection.set()) {
942 lt->clearSelection();
943 updateLocal(bv, FULL, false);
945 lt->clearSelection();
947 bv->owner()->setLayout(cpar(bv)->getLayout());
949 // Insert primary selection with middle mouse
950 // if there is a local selection in the current buffer,
953 if (paste_internally)
954 localDispatch(bv, LFUN_PASTE, "");
956 localDispatch(bv, LFUN_PASTESELECTION,
963 no_selection = false;
967 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
969 UpdatableInset * inset = 0;
971 if (the_locking_inset) {
972 the_locking_inset->insetButtonRelease(bv,
973 x - inset_x, y - inset_y,
976 if (cpar(bv)->isInset(cpos(bv))) {
977 inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
978 if (isHighlyEditableInset(inset)) {
979 inset->insetButtonRelease(bv,
981 y - inset_y, button);
983 inset_x = cx(bv) - top_x + drawTextXOffset;
984 inset_y = cy(bv) + drawTextYOffset;
985 inset->insetButtonRelease(bv,
987 y - inset_y, button);
989 x - inset_x, y - inset_y, button);
991 updateLocal(bv, CURSOR_PAR, false);
994 no_selection = false;
998 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1002 if (the_locking_inset) {
1003 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1009 lt = getLyXText(bv);
1012 hideInsetCursor(bv);
1013 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1014 lt->setSelection(bv);
1015 if (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1016 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos())
1018 updateLocal(bv, SELECTION, false);
1020 showInsetCursor(bv);
1026 void InsetText::insetKeyPress(XKeyEvent * xke)
1028 if (the_locking_inset) {
1029 the_locking_inset->insetKeyPress(xke);
1035 UpdatableInset::RESULT
1036 InsetText::localDispatch(BufferView * bv,
1037 kb_action action, string const & arg)
1039 bool was_empty = par->size() == 0 && !par->next();
1040 no_selection = false;
1041 UpdatableInset::RESULT
1042 result= UpdatableInset::localDispatch(bv, action, arg);
1043 if (result != UNDISPATCHED) {
1047 result = DISPATCHED;
1048 if ((action < 0) && arg.empty())
1051 if (the_locking_inset) {
1052 result = the_locking_inset->localDispatch(bv, action, arg);
1053 if (result == DISPATCHED_NOUPDATE)
1055 else if (result == DISPATCHED) {
1056 updateLocal(bv, CURSOR_PAR, false);
1058 } else if (result >= FINISHED) {
1060 case FINISHED_RIGHT:
1061 moveRightIntern(bv, false, false);
1062 result = DISPATCHED;
1065 if ((result = moveUp(bv)) >= FINISHED) {
1066 updateLocal(bv, CURSOR, false);
1067 bv->unlockInset(this);
1071 if ((result = moveDown(bv)) >= FINISHED) {
1072 updateLocal(bv, CURSOR, false);
1073 bv->unlockInset(this);
1077 result = DISPATCHED;
1080 the_locking_inset = 0;
1081 #ifdef WITH_WARNINGS
1082 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1087 hideInsetCursor(bv);
1090 lt = getLyXText(bv);
1095 case LFUN_SELFINSERT:
1096 if (bv->buffer()->isReadonly()) {
1097 // setErrorMessage(N_("Document is read only"));
1101 /* Automatically delete the currently selected
1102 * text and replace it with what is being
1103 * typed in now. Depends on lyxrc settings
1104 * "auto_region_delete", which defaults to
1107 setUndo(bv, Undo::INSERT,
1108 lt->cursor.par(), lt->cursor.par()->next());
1110 if (lyxrc.auto_region_delete) {
1111 if (lt->selection.set()) {
1112 lt->cutSelection(bv, false);
1115 lt->clearSelection();
1116 for (string::size_type i = 0; i < arg.length(); ++i) {
1117 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1120 lt->selection.cursor = lt->cursor;
1121 updateLocal(bv, CURSOR_PAR, true);
1122 result = DISPATCHED_NOUPDATE;
1124 // --- Cursor Movements -----------------------------------
1127 moveRight(bv, false, true);
1128 lt->setSelection(bv);
1129 updateLocal(bv, SELECTION, false);
1132 result = moveRight(bv);
1134 updateLocal(bv, CURSOR, false);
1138 moveLeft(bv, false, true);
1139 lt->setSelection(bv);
1140 updateLocal(bv, SELECTION, false);
1144 result = moveLeft(bv);
1145 updateLocal(bv, CURSOR, false);
1150 lt->setSelection(bv);
1151 updateLocal(bv, SELECTION, false);
1155 result = moveDown(bv);
1156 updateLocal(bv, CURSOR, false);
1161 lt->setSelection(bv);
1162 updateLocal(bv, SELECTION, false);
1166 result = moveUp(bv);
1167 updateLocal(bv, CURSOR, false);
1172 updateLocal(bv, CURSOR, false);
1176 updateLocal(bv, CURSOR, false);
1178 case LFUN_BACKSPACE: {
1179 setUndo(bv, Undo::DELETE,
1180 lt->cursor.par(), lt->cursor.par()->next());
1181 if (lt->selection.set())
1182 lt->cutSelection(bv);
1185 updateLocal(bv, CURSOR_PAR, true);
1190 setUndo(bv, Undo::DELETE,
1191 lt->cursor.par(), lt->cursor.par()->next());
1192 if (lt->selection.set()) {
1193 lt->cutSelection(bv);
1197 updateLocal(bv, CURSOR_PAR, true);
1202 setUndo(bv, Undo::DELETE,
1203 lt->cursor.par(), lt->cursor.par()->next());
1204 lt->cutSelection(bv);
1205 updateLocal(bv, CURSOR_PAR, true);
1211 lt->copySelection(bv);
1212 updateLocal(bv, CURSOR_PAR, false);
1214 case LFUN_PASTESELECTION:
1216 string const clip(bv->getClipboard());
1220 if (arg == "paragraph") {
1221 lt->insertStringAsParagraphs(bv, clip);
1223 lt->insertStringAsLines(bv, clip);
1225 updateLocal(bv, CURSOR_PAR, true);
1229 if (!autoBreakRows) {
1231 if (CutAndPaste::nrOfParagraphs() > 1) {
1232 Alert::alert(_("Impossible operation"),
1233 _("Cannot include more than one paragraph!"),
1238 setUndo(bv, Undo::INSERT,
1239 lt->cursor.par(), lt->cursor.par()->next());
1240 lt->pasteSelection(bv);
1241 updateLocal(bv, CURSOR_PAR, true);
1245 case LFUN_BREAKPARAGRAPH:
1246 if (!autoBreakRows) {
1247 result = DISPATCHED;
1250 lt->breakParagraph(bv, 0);
1251 updateLocal(bv, FULL, true);
1253 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1254 if (!autoBreakRows) {
1255 result = DISPATCHED;
1258 lt->breakParagraph(bv, 1);
1259 updateLocal(bv, FULL, true);
1262 case LFUN_BREAKLINE: {
1263 if (!autoBreakRows) {
1264 result = DISPATCHED;
1267 setUndo(bv, Undo::INSERT,
1268 lt->cursor.par(), lt->cursor.par()->next());
1269 lt->insertChar(bv, Paragraph::META_NEWLINE);
1270 updateLocal(bv, CURSOR_PAR, true);
1275 // do not set layouts on non breakable textinsets
1276 if (autoBreakRows) {
1277 layout_type cur_layout = cpar(bv)->layout;
1279 // Derive layout number from given argument (string)
1280 // and current buffer's textclass (number). */
1281 textclass_type tclass = bv->buffer()->params.textclass;
1282 std::pair <bool, layout_type> layout =
1283 textclasslist.NumberOfLayout(tclass, arg);
1285 // If the entry is obsolete, use the new one instead.
1287 string obs = textclasslist.Style(tclass,layout.second).
1290 layout = textclasslist.NumberOfLayout(tclass, obs);
1293 // see if we found the layout number:
1294 if (!layout.first) {
1295 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1296 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1300 if (cur_layout != layout.second) {
1301 cur_layout = layout.second;
1302 lt->setLayout(bv, layout.second);
1303 bv->owner()->setLayout(cpar(bv)->getLayout());
1304 updateLocal(bv, CURSOR_PAR, true);
1307 // reset the layout box
1308 bv->owner()->setLayout(cpar(bv)->getLayout());
1311 case LFUN_PARAGRAPH_SPACING:
1312 // This one is absolutely not working. When fiddling with this
1313 // it also seems to me that the paragraphs inside the insettext
1314 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1316 Paragraph * par = lt->cursor.par();
1317 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1318 float cur_value = 1.0;
1319 if (cur_spacing == Spacing::Other) {
1320 cur_value = par->params().spacing().getValue();
1323 istringstream istr(arg.c_str());
1326 Spacing::Space new_spacing = cur_spacing;
1327 float new_value = cur_value;
1329 lyxerr << "Missing argument to `paragraph-spacing'"
1331 } else if (tmp == "single") {
1332 new_spacing = Spacing::Single;
1333 } else if (tmp == "onehalf") {
1334 new_spacing = Spacing::Onehalf;
1335 } else if (tmp == "double") {
1336 new_spacing = Spacing::Double;
1337 } else if (tmp == "other") {
1338 new_spacing = Spacing::Other;
1341 lyxerr << "new_value = " << tmpval << endl;
1344 } else if (tmp == "default") {
1345 new_spacing = Spacing::Default;
1347 lyxerr << _("Unknown spacing argument: ")
1350 if (cur_spacing != new_spacing || cur_value != new_value) {
1351 par->params().spacing(Spacing(new_spacing, new_value));
1352 updateLocal(bv, CURSOR_PAR, true);
1358 if (!bv->Dispatch(action, arg))
1359 result = UNDISPATCHED;
1363 /// If the action has deleted all text in the inset, we need to change the
1364 // language to the language of the surronding text.
1365 if (!was_empty && par->size() == 0 && !par->next()) {
1366 LyXFont font(LyXFont::ALL_IGNORE);
1367 font.setLanguage(bv->getParentLanguage(this));
1368 setFont(bv, font, false);
1371 if (result < FINISHED) {
1372 showInsetCursor(bv);
1374 bv->unlockInset(this);
1381 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1384 buf->latexParagraphs(os, par, 0, texrow);
1385 return texrow.rows();
1389 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1391 Paragraph * p = par;
1392 unsigned int lines = 0;
1395 string const tmp = buf->asciiParagraph(p, linelen);
1396 lines += countChar(tmp, '\n');
1404 int InsetText::docbook(Buffer const * buf, ostream & os) const
1406 Paragraph * p = par;
1407 unsigned int lines = 0;
1409 vector<string> environment_stack(10);
1410 vector<string> environment_inner(10);
1412 int const command_depth = 0;
1415 Paragraph::depth_type depth = 0; // paragraph depth
1419 int desc_on = 0; // description mode
1421 LyXLayout const & style =
1422 textclasslist.Style(buf->params.textclass,
1425 // environment tag closing
1426 for (; depth > p->params().depth(); --depth) {
1427 if (environment_inner[depth] != "!-- --") {
1428 item_name = "listitem";
1429 buf->sgmlCloseTag(os, command_depth + depth,
1431 if (environment_inner[depth] == "varlistentry")
1432 buf->sgmlCloseTag(os, depth+command_depth,
1433 environment_inner[depth]);
1435 buf->sgmlCloseTag(os, depth + command_depth,
1436 environment_stack[depth]);
1437 environment_stack[depth].erase();
1438 environment_inner[depth].erase();
1441 if (depth == p->params().depth()
1442 && environment_stack[depth] != style.latexname()
1443 && !environment_stack[depth].empty()) {
1444 if (environment_inner[depth] != "!-- --") {
1445 item_name= "listitem";
1446 buf->sgmlCloseTag(os, command_depth+depth,
1448 if (environment_inner[depth] == "varlistentry")
1449 buf->sgmlCloseTag(os,
1450 depth + command_depth,
1451 environment_inner[depth]);
1454 buf->sgmlCloseTag(os, depth + command_depth,
1455 environment_stack[depth]);
1457 environment_stack[depth].erase();
1458 environment_inner[depth].erase();
1461 // Write opening SGML tags.
1462 switch (style.latextype) {
1463 case LATEX_PARAGRAPH:
1464 buf->sgmlOpenTag(os, depth + command_depth,
1469 buf->sgmlError(p, 0,
1470 _("Error : LatexType Command not allowed here.\n"));
1474 case LATEX_ENVIRONMENT:
1475 case LATEX_ITEM_ENVIRONMENT:
1476 if (depth < p->params().depth()) {
1477 depth = p->params().depth();
1478 environment_stack[depth].erase();
1481 if (environment_stack[depth] != style.latexname()) {
1482 if(environment_stack.size() == depth + 1) {
1483 environment_stack.push_back("!-- --");
1484 environment_inner.push_back("!-- --");
1486 environment_stack[depth] = style.latexname();
1487 environment_inner[depth] = "!-- --";
1488 buf->sgmlOpenTag(os, depth + command_depth,
1489 environment_stack[depth]);
1491 if (environment_inner[depth] != "!-- --") {
1492 item_name= "listitem";
1493 buf->sgmlCloseTag(os,
1494 command_depth + depth,
1496 if (environment_inner[depth] == "varlistentry")
1497 buf->sgmlCloseTag(os,
1498 depth + command_depth,
1499 environment_inner[depth]);
1503 if (style.latextype == LATEX_ENVIRONMENT) {
1504 if (!style.latexparam().empty()) {
1505 if(style.latexparam() == "CDATA")
1508 buf->sgmlOpenTag(os, depth + command_depth,
1509 style.latexparam());
1514 desc_on = (style.labeltype == LABEL_MANUAL);
1517 environment_inner[depth]= "varlistentry";
1519 environment_inner[depth]= "listitem";
1521 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1522 environment_inner[depth]);
1526 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1530 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1535 buf->sgmlOpenTag(os, depth + command_depth,
1540 buf->simpleDocBookOnePar(os, p, desc_on,
1541 depth + 1 + command_depth);
1545 // write closing SGML tags
1546 switch (style.latextype) {
1547 case LATEX_ENVIRONMENT:
1548 if (!style.latexparam().empty()) {
1549 if(style.latexparam() == "CDATA")
1552 buf->sgmlCloseTag(os, depth + command_depth,
1553 style.latexparam());
1556 case LATEX_ITEM_ENVIRONMENT:
1557 if (desc_on == 1) break;
1559 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1561 case LATEX_PARAGRAPH:
1562 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1565 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1571 for (int d = depth; d >= 0; --d) {
1572 if (!environment_stack[depth].empty()) {
1573 if (environment_inner[depth] != "!-- --") {
1574 item_name = "listitem";
1575 buf->sgmlCloseTag(os, command_depth + depth,
1577 if (environment_inner[depth] == "varlistentry")
1578 buf->sgmlCloseTag(os, depth + command_depth,
1579 environment_inner[depth]);
1582 buf->sgmlCloseTag(os, depth + command_depth,
1583 environment_stack[depth]);
1591 void InsetText::validate(LaTeXFeatures & features) const
1593 Paragraph * p = par;
1595 p->validate(features);
1601 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1603 if (textclasslist.Style(buf->params.textclass,
1604 p->getLayout()).labeltype != LABEL_MANUAL)
1607 return p->beginningOfMainBody();
1611 void InsetText::getCursorPos(BufferView * bv,
1612 int & x, int & y) const
1614 if (the_locking_inset) {
1615 the_locking_inset->getCursorPos(bv, x, y);
1623 unsigned int InsetText::insetInInsetY()
1625 if (!the_locking_inset)
1628 return (inset_y + the_locking_inset->insetInInsetY());
1632 void InsetText::toggleInsetCursor(BufferView * bv)
1634 if (the_locking_inset) {
1635 the_locking_inset->toggleInsetCursor(bv);
1639 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1641 int const asc = lyxfont::maxAscent(font);
1642 int const desc = lyxfont::maxDescent(font);
1644 if (isCursorVisible())
1645 bv->hideLockedInsetCursor();
1647 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1648 toggleCursorVisible();
1652 void InsetText::showInsetCursor(BufferView * bv, bool show)
1654 if (the_locking_inset) {
1655 the_locking_inset->showInsetCursor(bv, show);
1658 if (!isCursorVisible()) {
1659 LyXFont const font =
1660 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1662 int const asc = lyxfont::maxAscent(font);
1663 int const desc = lyxfont::maxDescent(font);
1665 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1667 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1668 setCursorVisible(true);
1673 void InsetText::hideInsetCursor(BufferView * bv)
1675 if (isCursorVisible()) {
1676 bv->hideLockedInsetCursor();
1677 setCursorVisible(false);
1679 if (the_locking_inset)
1680 the_locking_inset->hideInsetCursor(bv);
1684 void InsetText::fitInsetCursor(BufferView * bv) const
1686 if (the_locking_inset) {
1687 the_locking_inset->fitInsetCursor(bv);
1690 LyXFont const font =
1691 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1693 int const asc = lyxfont::maxAscent(font);
1694 int const desc = lyxfont::maxDescent(font);
1696 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1700 UpdatableInset::RESULT
1701 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1703 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1704 return moveLeftIntern(bv, false, activate_inset, selecting);
1706 return moveRightIntern(bv, false, activate_inset, selecting);
1710 UpdatableInset::RESULT
1711 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1713 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1714 return moveRightIntern(bv, true, activate_inset, selecting);
1716 return moveLeftIntern(bv, true, activate_inset, selecting);
1720 UpdatableInset::RESULT
1721 InsetText::moveRightIntern(BufferView * bv, bool behind,
1722 bool activate_inset, bool selecting)
1724 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1725 return FINISHED_RIGHT;
1726 if (activate_inset && checkAndActivateInset(bv, behind))
1728 getLyXText(bv)->cursorRight(bv);
1730 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1731 return DISPATCHED_NOUPDATE;
1735 UpdatableInset::RESULT
1736 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1737 bool activate_inset, bool selecting)
1739 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1741 getLyXText(bv)->cursorLeft(bv);
1743 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1744 if (activate_inset && checkAndActivateInset(bv, behind))
1746 return DISPATCHED_NOUPDATE;
1750 UpdatableInset::RESULT
1751 InsetText::moveUp(BufferView * bv)
1753 if (!crow(bv)->previous())
1755 getLyXText(bv)->cursorUp(bv);
1756 return DISPATCHED_NOUPDATE;
1760 UpdatableInset::RESULT
1761 InsetText::moveDown(BufferView * bv)
1763 if (!crow(bv)->next())
1764 return FINISHED_DOWN;
1765 getLyXText(bv)->cursorDown(bv);
1766 return DISPATCHED_NOUPDATE;
1770 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1772 if (the_locking_inset) {
1773 if (the_locking_inset->insetAllowed(inset))
1774 return the_locking_inset->insertInset(bv, inset);
1777 inset->setOwner(this);
1778 hideInsetCursor(bv);
1782 lt = getLyXText(bv);
1785 lt->insertInset(bv, inset);
1787 if ((!cpar(bv)->isInset(cpos(bv))) ||
1788 (cpar(bv)->getInset(cpos(bv)) != inset))
1792 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1793 // showInsetCursor(bv);
1800 bool InsetText::insetAllowed(Inset::Code code) const
1802 if (the_locking_inset)
1803 return the_locking_inset->insetAllowed(code);
1808 UpdatableInset * InsetText::getLockingInset() const
1810 return the_locking_inset ? the_locking_inset->getLockingInset() :
1811 const_cast<InsetText *>(this);
1815 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1819 if (the_locking_inset)
1820 return the_locking_inset->getFirstLockingInsetOfType(c);
1825 bool InsetText::showInsetDialog(BufferView * bv) const
1827 if (the_locking_inset)
1828 return the_locking_inset->showInsetDialog(bv);
1833 std::vector<string> const InsetText::getLabelList() const
1835 std::vector<string> label_list;
1837 Paragraph * tpar = par;
1839 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1840 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1841 for (; beg != end; ++beg) {
1842 std::vector<string> const l = (*beg)->getLabelList();
1843 label_list.insert(label_list.end(), l.begin(), l.end());
1845 tpar = tpar->next();
1851 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1854 if (the_locking_inset) {
1855 the_locking_inset->setFont(bv, font, toggleall, selectall);
1858 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1859 getLyXText(bv)->setFont(bv, font, toggleall);
1864 lt = getLyXText(bv);
1867 if (lt->selection.set()) {
1868 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1872 lt->toggleFree(bv, font, toggleall);
1874 lt->clearSelection();
1876 if (selectall || lt->selection.set())
1877 updateLocal(bv, FULL, true);
1879 updateLocal(bv, CURSOR_PAR, true);
1885 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1887 if (cpar(bv)->isInset(cpos(bv))) {
1891 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1892 if (!isHighlyEditableInset(inset))
1894 LyXFont const font =
1895 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1897 x = inset->width(bv, font);
1898 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1901 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1903 //inset_x = cx(bv) - top_x + drawTextXOffset;
1904 //inset_y = cy(bv) + drawTextYOffset;
1905 inset->edit(bv, x, y, 0);
1906 if (!the_locking_inset)
1908 updateLocal(bv, CURSOR, false);
1915 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1918 x -= drawTextXOffset;
1920 int dummyy = y + insetAscent;
1921 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1928 inset_x = cx(bv) - top_x + drawTextXOffset;
1929 inset_y = cy(bv) + drawTextYOffset;
1930 inset->edit(bv, x - inset_x, y - inset_y, button);
1931 if (!the_locking_inset)
1933 updateLocal(bv, CURSOR, false);
1940 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1943 int w = UpdatableInset::getMaxWidth(bv, inset);
1948 w = w - top_x + owner()->x();
1951 w -= (2 * TEXT_TO_INSET_OFFSET);
1954 return UpdatableInset::getMaxWidth(bv, inset);
1959 void InsetText::setParagraphData(Paragraph * p, bool same_id)
1961 // we have to unlock any locked inset otherwise we're in troubles
1962 the_locking_inset = 0;
1964 Paragraph * tmp = par->next();
1969 par = new Paragraph(*p, same_id);
1970 par->setInsetOwner(this);
1971 Paragraph * np = par;
1974 np->next(new Paragraph(*p, same_id));
1975 np->next()->previous(np);
1977 np->setInsetOwner(this);
1984 void InsetText::setText(string const & data)
1987 LyXFont font(LyXFont::ALL_SANE);
1988 for (unsigned int i=0; i < data.length(); ++i)
1989 par->insertChar(i, data[i], font);
1993 void InsetText::setAutoBreakRows(bool flag)
1995 if (flag != autoBreakRows) {
1996 autoBreakRows = flag;
2004 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2006 if (how != drawFrame_) {
2009 updateLocal(bv, DRAW_FRAME, false);
2014 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2016 if (frame_color != col) {
2019 updateLocal(bv, DRAW_FRAME, false);
2024 int InsetText::cx(BufferView * bv) const
2028 lt = getLyXText(bv);
2031 int x = lt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2032 if (the_locking_inset) {
2033 LyXFont font = lt->getFont(bv->buffer(),
2036 if (font.isVisibleRightToLeft())
2037 x -= the_locking_inset->width(bv, font);
2045 int InsetText::cy(BufferView * bv) const
2048 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2052 pos_type InsetText::cpos(BufferView * bv) const
2054 return getLyXText(bv)->cursor.pos();
2058 Paragraph * InsetText::cpar(BufferView * bv) const
2060 return getLyXText(bv)->cursor.par();
2064 bool InsetText::cboundary(BufferView * bv) const
2066 return getLyXText(bv)->cursor.boundary();
2070 Row * InsetText::crow(BufferView * bv) const
2072 return getLyXText(bv)->cursor.row();
2076 LyXText * InsetText::getLyXText(BufferView const * lbv,
2077 bool const recursive) const
2079 if (!recursive && (cached_bview == lbv)) {
2080 LyXText * lt = cached_text.get();
2081 lyx::Assert(lt && lt->firstRow()->par() == par);
2082 return cached_text.get();
2085 // Super UGLY! (Lgb)
2086 BufferView * bv = const_cast<BufferView *>(lbv);
2089 Cache::iterator it = cache.find(bv);
2091 if (it != cache.end()) {
2095 resizeLyXText(do_resize);
2096 if (lt || !it->second.remove) {
2097 lyx::Assert(it->second.text.get());
2098 cached_text = it->second.text;
2099 if (recursive && the_locking_inset) {
2100 return the_locking_inset->getLyXText(bv, true);
2102 return cached_text.get();
2103 } else if (it->second.remove) {
2105 saveLyXTextState(it->second.text.get());
2112 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2113 cached_text->init(bv);
2114 restoreLyXTextState(bv, cached_text.get());
2116 cache.insert(make_pair(bv, cached_text));
2118 if (the_locking_inset && recursive) {
2119 return the_locking_inset->getLyXText(bv);
2121 return cached_text.get();
2125 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2129 Cache::iterator it = cache.find(bv);
2131 if (it == cache.end()) {
2135 lyx::Assert(it->second.text.get());
2137 it->second.remove = true;
2139 /// then remove all LyXText in text-insets
2140 Paragraph * p = par;
2141 for (; p; p = p->next()) {
2142 p->deleteInsetsLyXText(bv);
2148 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2151 // we cannot resize this because we are in use!
2152 // so do this on the next possible getLyXText()
2157 // lyxerr << "InsetText::resizeLyXText\n";
2158 if (!par->next() && !par->size()) // no data, resize not neccessary!
2160 // one endless line, resize normally not necessary
2161 if (!force && getMaxWidth(bv, this) < 0)
2164 Cache::iterator it = cache.find(bv);
2165 if (it == cache.end()) {
2168 lyx::Assert(it->second.text.get());
2170 LyXText * t = it->second.text.get();
2171 saveLyXTextState(t);
2172 for (Paragraph * p = par; p; p = p->next()) {
2173 p->resizeInsetsLyXText(bv);
2176 restoreLyXTextState(bv, t);
2177 if (the_locking_inset) {
2178 inset_x = cx(bv) - top_x + drawTextXOffset;
2179 inset_y = cy(bv) + drawTextYOffset;
2183 t->first = bv->screen()->topCursorVisible(t);
2186 updateLocal(bv, FULL, false);
2187 // this will scroll the screen such that the cursor becomes visible
2188 bv->updateScrollbar();
2190 need_update |= FULL;
2195 void InsetText::reinitLyXText() const
2198 // we cannot resize this because we are in use!
2199 // so do this on the next possible getLyXText()
2205 // lyxerr << "InsetText::reinitLyXText\n";
2206 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2207 lyx::Assert(it->second.text.get());
2209 LyXText * t = it->second.text.get();
2210 BufferView * bv = it->first;
2212 saveLyXTextState(t);
2213 for (Paragraph * p = par; p; p = p->next()) {
2214 p->resizeInsetsLyXText(bv);
2217 restoreLyXTextState(bv, t);
2218 if (the_locking_inset) {
2219 inset_x = cx(bv) - top_x + drawTextXOffset;
2220 inset_y = cy(bv) + drawTextYOffset;
2223 t->first = bv->screen()->topCursorVisible(t);
2226 updateLocal(bv, FULL, false);
2227 // this will scroll the screen such that the cursor becomes visible
2228 bv->updateScrollbar();
2236 void InsetText::removeNewlines()
2238 bool changed = false;
2240 for (Paragraph * p = par; p; p = p->next()) {
2241 for (int i = 0; i < p->size(); ++i) {
2242 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2253 bool InsetText::nodraw() const
2255 if (the_locking_inset)
2256 return the_locking_inset->nodraw();
2257 return UpdatableInset::nodraw();
2261 int InsetText::scroll(bool recursive) const
2263 int sx = UpdatableInset::scroll(false);
2265 if (recursive && the_locking_inset)
2266 sx += the_locking_inset->scroll(recursive);
2272 bool InsetText::doClearArea() const
2274 return !locked || (need_update & (FULL|INIT));
2278 void InsetText::selectAll(BufferView * bv)
2280 getLyXText(bv)->cursorTop(bv);
2281 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2282 getLyXText(bv)->cursorBottom(bv);
2283 getLyXText(bv)->setSelection(bv);
2287 void InsetText::clearSelection(BufferView * bv)
2289 getLyXText(bv)->clearSelection();
2293 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2296 Painter & pain = bv->painter();
2298 int h = ascent(bv, dummy) + descent(bv, dummy);
2299 int ty = baseline - insetAscent;
2305 if ((ty + h) > pain.paperHeight())
2306 h = pain.paperHeight();
2307 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2308 w = pain.paperWidth();
2309 // w -= TEXT_TO_INSET_OFFSET;
2310 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2313 frame_is_visible = false;
2317 Paragraph * InsetText::getParFromID(int id) const
2320 Paragraph * result = par;
2321 Paragraph * ires = 0;
2322 while (result && result->id() != id) {
2323 if ((ires = result->getParFromID(id)))
2325 result = result->next();
2329 Paragraph * tmp = par;
2331 if (tmp->id() == id) {
2334 Paragraph * tmp2 = tmp->getParFromID(id);
2345 Paragraph * InsetText::firstParagraph() const
2348 if (the_locking_inset)
2349 if ((result = the_locking_inset->firstParagraph()))
2355 Paragraph * InsetText::getFirstParagraph(int i) const
2357 return (i == 0) ? par : 0;
2361 LyXCursor const & InsetText::cursor(BufferView * bv) const
2363 if (the_locking_inset)
2364 return the_locking_inset->cursor(bv);
2365 return getLyXText(bv)->cursor;
2369 Paragraph * InsetText::paragraph() const
2375 void InsetText::paragraph(Paragraph * p)
2377 // first we have to delete the old contents otherwise we'll have a
2379 the_locking_inset = 0;
2381 Paragraph * tmp = par->next();
2386 // set ourself as owner for all the paragraphs inserted!
2387 Paragraph * np = par;
2389 np->setInsetOwner(this);
2393 // redraw myself when asked for
2398 Inset * InsetText::getInsetFromID(int id_arg) const
2401 return const_cast<InsetText *>(this);
2403 Paragraph * lp = par;
2406 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2407 en = lp->inset_iterator_end();
2410 if ((*it)->id() == id_arg)
2412 Inset * in = (*it)->getInsetFromID(id_arg);
2422 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2428 lt = getLyXText(bv);
2431 if (the_locking_inset) {
2432 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2439 #warning Dekel please have a look on this one RTL? (Jug)
2441 // we have to go on checking so move cusor to the right
2442 lt->cursor.pos(lt->cursor.pos() + 1);
2444 str = lt->selectNextWordToSpellcheck(bv, value);
2446 bv->unlockInset(const_cast<InsetText *>(this));
2455 void InsetText::selectSelectedWord(BufferView * bv)
2457 if (the_locking_inset) {
2458 the_locking_inset->selectSelectedWord(bv);
2461 getLyXText(bv)->selectSelectedWord(bv);
2462 updateLocal(bv, SELECTION, false);
2466 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2468 if (the_locking_inset) {
2469 the_locking_inset->toggleSelection(bv, kill_selection);
2473 lt = getLyXText(bv);
2477 int x = top_x + TEXT_TO_INSET_OFFSET;
2479 Row * row = lt->firstRow();
2480 int y_offset = top_baseline - row->ascent_of_text();
2482 while ((row != 0) && ((y+row->height()) <= 0)) {
2489 if (need_update & SELECTION)
2491 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2497 bool InsetText::searchForward(BufferView * bv, string const & str,
2498 bool const & cs, bool const & mw)
2500 if (the_locking_inset) {
2501 if (the_locking_inset->searchForward(bv, str, cs, mw))
2505 lt = getLyXText(bv);
2508 Paragraph * lpar = lt->cursor.par();
2509 pos_type pos = lt->cursor.pos();
2510 if (pos < lpar->size() - 1)
2514 lpar = lpar->next();
2519 // we have to unlock ourself in this function by default!
2520 bv->unlockInset(const_cast<InsetText *>(this));
2523 lt->setCursor(bv, lpar, pos);
2527 if (LyXFind(bv, str, true, true, cs , mw)) {
2530 // we have to unlock ourself in this function by default!
2531 bv->unlockInset(const_cast<InsetText *>(this));
2535 bool InsetText::searchBackward(BufferView * bv, string const & str,
2536 bool const & cs, bool const & mw)
2538 if (the_locking_inset)
2539 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2541 if (LyXFind(bv, str, false, true, cs, mw)) {
2544 // we have to unlock ourself in this function by default!
2545 bv->unlockInset(const_cast<InsetText *>(this));
2550 bool InsetText::checkInsertChar(LyXFont & font)
2553 return owner()->checkInsertChar(font);