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(pain, 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(pain, 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 (cleared || (last_drawn_width != insetWidth)) {
424 clearInset(pain, 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)
838 if (!the_locking_inset)
840 if (the_locking_inset != inset) {
841 getLyXText(bv)->updateInset(bv, the_locking_inset);
842 setUpdateStatus(bv, CURSOR_PAR);
843 return the_locking_inset->updateInsetInInset(bv, inset);
845 if (getLyXText(bv)->updateInset(bv, inset))
846 updateLocal(bv, CURSOR_PAR, false);
847 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
848 inset_x = cx(bv) - top_x + drawTextXOffset;
849 inset_y = cy(bv) + drawTextYOffset;
855 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
859 int tmp_x = x - drawTextXOffset;
860 int tmp_y = y + insetAscent - getLyXText(bv)->first;
861 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
864 if (the_locking_inset) {
865 if (the_locking_inset == inset) {
866 the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
867 no_selection = false;
870 // otherwise unlock the_locking_inset and lock the new inset
871 the_locking_inset->insetUnlock(bv);
872 inset_x = cx(bv) - top_x + drawTextXOffset;
873 inset_y = cy(bv) + drawTextYOffset;
874 the_locking_inset = static_cast<UpdatableInset*>(inset);
875 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
876 inset->edit(bv, x - inset_x, y - inset_y, button);
877 if (the_locking_inset)
878 updateLocal(bv, CURSOR, false);
879 no_selection = false;
882 // otherwise only unlock the_locking_inset
883 the_locking_inset->insetUnlock(bv);
884 the_locking_inset = 0;
886 if (bv->theLockingInset()) {
887 if (isHighlyEditableInset(inset)) {
888 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
889 inset_x = cx(bv) - top_x + drawTextXOffset;
890 inset_y = cy(bv) + drawTextYOffset;
891 inset_pos = cpos(bv);
892 inset_par = cpar(bv);
893 inset_boundary = cboundary(bv);
894 the_locking_inset = uinset;
895 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
897 uinset->edit(bv, x - inset_x, y - inset_y, 0);
898 if (the_locking_inset)
899 updateLocal(bv, CURSOR, false);
900 no_selection = false;
904 if (!inset) { // && (button == 2)) {
905 bool paste_internally = false;
906 if ((button == 2) && getLyXText(bv)->selection.set()) {
907 localDispatch(bv, LFUN_COPY, "");
908 paste_internally = true;
915 lt->setCursorFromCoordinates(bv, x-drawTextXOffset, y + insetAscent);
916 if (lt->selection.set()) {
917 lt->clearSelection();
918 updateLocal(bv, FULL, false);
920 lt->clearSelection();
922 bv->owner()->setLayout(cpar(bv)->getLayout());
924 // Insert primary selection with middle mouse
925 // if there is a local selection in the current buffer,
928 if (paste_internally)
929 localDispatch(bv, LFUN_PASTE, "");
931 localDispatch(bv, LFUN_PASTESELECTION,
938 no_selection = false;
942 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
944 UpdatableInset * inset = 0;
946 if (the_locking_inset) {
947 the_locking_inset->insetButtonRelease(bv,
948 x - inset_x, y - inset_y,
951 if (cpar(bv)->isInset(cpos(bv))) {
952 inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
953 if (isHighlyEditableInset(inset)) {
954 inset->insetButtonRelease(bv,
956 y - inset_y, button);
958 inset_x = cx(bv) - top_x + drawTextXOffset;
959 inset_y = cy(bv) + drawTextYOffset;
960 inset->insetButtonRelease(bv,
962 y - inset_y, button);
964 x - inset_x, y - inset_y, button);
966 updateLocal(bv, CURSOR_PAR, false);
969 no_selection = false;
973 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
977 if (the_locking_inset) {
978 the_locking_inset->insetMotionNotify(bv, x - inset_x,
982 LyXText * t = getLyXText(bv);
984 t->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
986 if (t->toggle_cursor.par() != t->toggle_end_cursor.par() ||
987 t->toggle_cursor.pos() != t->toggle_end_cursor.pos())
988 updateLocal(bv, SELECTION, false);
993 void InsetText::insetKeyPress(XKeyEvent * xke)
995 if (the_locking_inset) {
996 the_locking_inset->insetKeyPress(xke);
1002 UpdatableInset::RESULT
1003 InsetText::localDispatch(BufferView * bv,
1004 kb_action action, string const & arg)
1006 bool was_empty = par->size() == 0 && !par->next();
1007 no_selection = false;
1008 UpdatableInset::RESULT
1009 result= UpdatableInset::localDispatch(bv, action, arg);
1010 if (result != UNDISPATCHED) {
1014 result = DISPATCHED;
1015 if ((action < 0) && arg.empty())
1018 if (the_locking_inset) {
1019 result = the_locking_inset->localDispatch(bv, action, arg);
1020 if (result == DISPATCHED_NOUPDATE)
1022 else if (result == DISPATCHED) {
1023 updateLocal(bv, CURSOR_PAR, false);
1025 } else if (result >= FINISHED) {
1027 case FINISHED_RIGHT:
1028 moveRightIntern(bv, false, false);
1029 result = DISPATCHED;
1032 if ((result = moveUp(bv)) >= FINISHED) {
1033 updateLocal(bv, CURSOR, false);
1034 bv->unlockInset(this);
1038 if ((result = moveDown(bv)) >= FINISHED) {
1039 updateLocal(bv, CURSOR, false);
1040 bv->unlockInset(this);
1044 result = DISPATCHED;
1047 the_locking_inset = 0;
1048 #ifdef WITH_WARNINGS
1049 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1054 hideInsetCursor(bv);
1057 lt = getLyXText(bv);
1062 case LFUN_SELFINSERT:
1063 if (bv->buffer()->isReadonly()) {
1064 // setErrorMessage(N_("Document is read only"));
1068 /* Automatically delete the currently selected
1069 * text and replace it with what is being
1070 * typed in now. Depends on lyxrc settings
1071 * "auto_region_delete", which defaults to
1074 setUndo(bv, Undo::INSERT,
1075 lt->cursor.par(), lt->cursor.par()->next());
1077 if (lyxrc.auto_region_delete) {
1078 if (lt->selection.set()) {
1079 lt->cutSelection(bv, false);
1082 lt->clearSelection();
1083 for (string::size_type i = 0; i < arg.length(); ++i) {
1084 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1087 lt->selection.cursor = lt->cursor;
1088 updateLocal(bv, CURSOR_PAR, true);
1089 result = DISPATCHED_NOUPDATE;
1091 // --- Cursor Movements -----------------------------------
1094 moveRight(bv, false, true);
1095 lt->setSelection(bv);
1096 updateLocal(bv, SELECTION, false);
1099 result = moveRight(bv);
1101 updateLocal(bv, CURSOR, false);
1105 moveLeft(bv, false, true);
1106 lt->setSelection(bv);
1107 updateLocal(bv, SELECTION, false);
1111 result = moveLeft(bv);
1112 updateLocal(bv, CURSOR, false);
1117 lt->setSelection(bv);
1118 updateLocal(bv, SELECTION, false);
1122 result = moveDown(bv);
1123 updateLocal(bv, CURSOR, false);
1128 lt->setSelection(bv);
1129 updateLocal(bv, SELECTION, false);
1133 result = moveUp(bv);
1134 updateLocal(bv, CURSOR, false);
1139 updateLocal(bv, CURSOR, false);
1143 updateLocal(bv, CURSOR, false);
1145 case LFUN_BACKSPACE: {
1146 setUndo(bv, Undo::DELETE,
1147 lt->cursor.par(), lt->cursor.par()->next());
1148 if (lt->selection.set())
1149 lt->cutSelection(bv);
1152 updateLocal(bv, CURSOR_PAR, true);
1157 setUndo(bv, Undo::DELETE,
1158 lt->cursor.par(), lt->cursor.par()->next());
1159 if (lt->selection.set()) {
1160 lt->cutSelection(bv);
1164 updateLocal(bv, CURSOR_PAR, true);
1169 setUndo(bv, Undo::DELETE,
1170 lt->cursor.par(), lt->cursor.par()->next());
1171 lt->cutSelection(bv);
1172 updateLocal(bv, CURSOR_PAR, true);
1178 lt->copySelection(bv);
1179 updateLocal(bv, CURSOR_PAR, false);
1181 case LFUN_PASTESELECTION:
1183 string const clip(bv->getClipboard());
1187 if (arg == "paragraph") {
1188 lt->insertStringAsParagraphs(bv, clip);
1190 lt->insertStringAsLines(bv, clip);
1192 updateLocal(bv, CURSOR_PAR, true);
1196 if (!autoBreakRows) {
1198 if (CutAndPaste::nrOfParagraphs() > 1) {
1199 Alert::alert(_("Impossible operation"),
1200 _("Cannot include more than one paragraph!"),
1205 setUndo(bv, Undo::INSERT,
1206 lt->cursor.par(), lt->cursor.par()->next());
1207 lt->pasteSelection(bv);
1208 updateLocal(bv, CURSOR_PAR, true);
1212 case LFUN_BREAKPARAGRAPH:
1213 if (!autoBreakRows) {
1214 result = DISPATCHED;
1217 lt->breakParagraph(bv, 0);
1218 updateLocal(bv, FULL, true);
1220 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1221 if (!autoBreakRows) {
1222 result = DISPATCHED;
1225 lt->breakParagraph(bv, 1);
1226 updateLocal(bv, FULL, true);
1229 case LFUN_BREAKLINE: {
1230 if (!autoBreakRows) {
1231 result = DISPATCHED;
1234 setUndo(bv, Undo::INSERT,
1235 lt->cursor.par(), lt->cursor.par()->next());
1236 lt->insertChar(bv, Paragraph::META_NEWLINE);
1237 updateLocal(bv, CURSOR_PAR, true);
1242 // do not set layouts on non breakable textinsets
1243 if (autoBreakRows) {
1244 layout_type cur_layout = cpar(bv)->layout;
1246 // Derive layout number from given argument (string)
1247 // and current buffer's textclass (number). */
1248 textclass_type tclass = bv->buffer()->params.textclass;
1249 std::pair <bool, layout_type> layout =
1250 textclasslist.NumberOfLayout(tclass, arg);
1252 // If the entry is obsolete, use the new one instead.
1254 string obs = textclasslist.Style(tclass,layout.second).
1257 layout = textclasslist.NumberOfLayout(tclass, obs);
1260 // see if we found the layout number:
1261 if (!layout.first) {
1262 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1263 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1267 if (cur_layout != layout.second) {
1268 cur_layout = layout.second;
1269 lt->setLayout(bv, layout.second);
1270 bv->owner()->setLayout(cpar(bv)->getLayout());
1271 updateLocal(bv, CURSOR_PAR, true);
1274 // reset the layout box
1275 bv->owner()->setLayout(cpar(bv)->getLayout());
1278 case LFUN_PARAGRAPH_SPACING:
1279 // This one is absolutely not working. When fiddling with this
1280 // it also seems to me that the paragraphs inside the insettext
1281 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1283 Paragraph * par = lt->cursor.par();
1284 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1285 float cur_value = 1.0;
1286 if (cur_spacing == Spacing::Other) {
1287 cur_value = par->params().spacing().getValue();
1290 istringstream istr(arg.c_str());
1293 Spacing::Space new_spacing = cur_spacing;
1294 float new_value = cur_value;
1296 lyxerr << "Missing argument to `paragraph-spacing'"
1298 } else if (tmp == "single") {
1299 new_spacing = Spacing::Single;
1300 } else if (tmp == "onehalf") {
1301 new_spacing = Spacing::Onehalf;
1302 } else if (tmp == "double") {
1303 new_spacing = Spacing::Double;
1304 } else if (tmp == "other") {
1305 new_spacing = Spacing::Other;
1308 lyxerr << "new_value = " << tmpval << endl;
1311 } else if (tmp == "default") {
1312 new_spacing = Spacing::Default;
1314 lyxerr << _("Unknown spacing argument: ")
1317 if (cur_spacing != new_spacing || cur_value != new_value) {
1318 par->params().spacing(Spacing(new_spacing, new_value));
1319 updateLocal(bv, CURSOR_PAR, true);
1325 if (!bv->Dispatch(action, arg))
1326 result = UNDISPATCHED;
1330 /// If the action has deleted all text in the inset, we need to change the
1331 // language to the language of the surronding text.
1332 if (!was_empty && par->size() == 0 && !par->next()) {
1333 LyXFont font(LyXFont::ALL_IGNORE);
1334 font.setLanguage(bv->getParentLanguage(this));
1335 setFont(bv, font, false);
1338 if (result < FINISHED) {
1339 showInsetCursor(bv);
1341 bv->unlockInset(this);
1348 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1351 buf->latexParagraphs(os, par, 0, texrow);
1352 return texrow.rows();
1356 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1358 Paragraph * p = par;
1359 unsigned int lines = 0;
1362 string const tmp = buf->asciiParagraph(p, linelen);
1363 lines += countChar(tmp, '\n');
1371 int InsetText::docbook(Buffer const * buf, ostream & os) const
1373 Paragraph * p = par;
1374 unsigned int lines = 0;
1376 vector<string> environment_stack(10);
1377 vector<string> environment_inner(10);
1379 int const command_depth = 0;
1382 Paragraph::depth_type depth = 0; // paragraph depth
1386 int desc_on = 0; // description mode
1388 LyXLayout const & style =
1389 textclasslist.Style(buf->params.textclass,
1392 // environment tag closing
1393 for (; depth > p->params().depth(); --depth) {
1394 if (environment_inner[depth] != "!-- --") {
1395 item_name = "listitem";
1396 buf->sgmlCloseTag(os, command_depth + depth,
1398 if (environment_inner[depth] == "varlistentry")
1399 buf->sgmlCloseTag(os, depth+command_depth,
1400 environment_inner[depth]);
1402 buf->sgmlCloseTag(os, depth + command_depth,
1403 environment_stack[depth]);
1404 environment_stack[depth].erase();
1405 environment_inner[depth].erase();
1408 if (depth == p->params().depth()
1409 && environment_stack[depth] != style.latexname()
1410 && !environment_stack[depth].empty()) {
1411 if (environment_inner[depth] != "!-- --") {
1412 item_name= "listitem";
1413 buf->sgmlCloseTag(os, command_depth+depth,
1415 if (environment_inner[depth] == "varlistentry")
1416 buf->sgmlCloseTag(os,
1417 depth + command_depth,
1418 environment_inner[depth]);
1421 buf->sgmlCloseTag(os, depth + command_depth,
1422 environment_stack[depth]);
1424 environment_stack[depth].erase();
1425 environment_inner[depth].erase();
1428 // Write opening SGML tags.
1429 switch (style.latextype) {
1430 case LATEX_PARAGRAPH:
1431 buf->sgmlOpenTag(os, depth + command_depth,
1436 buf->sgmlError(p, 0,
1437 _("Error : LatexType Command not allowed here.\n"));
1441 case LATEX_ENVIRONMENT:
1442 case LATEX_ITEM_ENVIRONMENT:
1443 if (depth < p->params().depth()) {
1444 depth = p->params().depth();
1445 environment_stack[depth].erase();
1448 if (environment_stack[depth] != style.latexname()) {
1449 if(environment_stack.size() == depth + 1) {
1450 environment_stack.push_back("!-- --");
1451 environment_inner.push_back("!-- --");
1453 environment_stack[depth] = style.latexname();
1454 environment_inner[depth] = "!-- --";
1455 buf->sgmlOpenTag(os, depth + command_depth,
1456 environment_stack[depth]);
1458 if (environment_inner[depth] != "!-- --") {
1459 item_name= "listitem";
1460 buf->sgmlCloseTag(os,
1461 command_depth + depth,
1463 if (environment_inner[depth] == "varlistentry")
1464 buf->sgmlCloseTag(os,
1465 depth + command_depth,
1466 environment_inner[depth]);
1470 if (style.latextype == LATEX_ENVIRONMENT) {
1471 if (!style.latexparam().empty()) {
1472 if(style.latexparam() == "CDATA")
1475 buf->sgmlOpenTag(os, depth + command_depth,
1476 style.latexparam());
1481 desc_on = (style.labeltype == LABEL_MANUAL);
1484 environment_inner[depth]= "varlistentry";
1486 environment_inner[depth]= "listitem";
1488 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1489 environment_inner[depth]);
1493 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1497 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1502 buf->sgmlOpenTag(os, depth + command_depth,
1507 buf->simpleDocBookOnePar(os, p, desc_on,
1508 depth + 1 + command_depth);
1512 // write closing SGML tags
1513 switch (style.latextype) {
1514 case LATEX_ENVIRONMENT:
1515 if (!style.latexparam().empty()) {
1516 if(style.latexparam() == "CDATA")
1519 buf->sgmlCloseTag(os, depth + command_depth,
1520 style.latexparam());
1523 case LATEX_ITEM_ENVIRONMENT:
1524 if (desc_on == 1) break;
1526 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1528 case LATEX_PARAGRAPH:
1529 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1532 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1538 for (int d = depth; d >= 0; --d) {
1539 if (!environment_stack[depth].empty()) {
1540 if (environment_inner[depth] != "!-- --") {
1541 item_name = "listitem";
1542 buf->sgmlCloseTag(os, command_depth + depth,
1544 if (environment_inner[depth] == "varlistentry")
1545 buf->sgmlCloseTag(os, depth + command_depth,
1546 environment_inner[depth]);
1549 buf->sgmlCloseTag(os, depth + command_depth,
1550 environment_stack[depth]);
1558 void InsetText::validate(LaTeXFeatures & features) const
1560 Paragraph * p = par;
1562 p->validate(features);
1568 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1570 if (textclasslist.Style(buf->params.textclass,
1571 p->getLayout()).labeltype != LABEL_MANUAL)
1574 return p->beginningOfMainBody();
1578 void InsetText::getCursorPos(BufferView * bv,
1579 int & x, int & y) const
1581 if (the_locking_inset) {
1582 the_locking_inset->getCursorPos(bv, x, y);
1590 unsigned int InsetText::insetInInsetY()
1592 if (!the_locking_inset)
1595 return (inset_y + the_locking_inset->insetInInsetY());
1599 void InsetText::toggleInsetCursor(BufferView * bv)
1601 if (the_locking_inset) {
1602 the_locking_inset->toggleInsetCursor(bv);
1606 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1608 int const asc = lyxfont::maxAscent(font);
1609 int const desc = lyxfont::maxDescent(font);
1611 if (isCursorVisible())
1612 bv->hideLockedInsetCursor();
1614 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1615 toggleCursorVisible();
1619 void InsetText::showInsetCursor(BufferView * bv, bool show)
1621 if (the_locking_inset) {
1622 the_locking_inset->showInsetCursor(bv, show);
1625 if (!isCursorVisible()) {
1626 LyXFont const font =
1627 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1629 int const asc = lyxfont::maxAscent(font);
1630 int const desc = lyxfont::maxDescent(font);
1632 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1634 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1635 setCursorVisible(true);
1640 void InsetText::hideInsetCursor(BufferView * bv)
1642 if (isCursorVisible()) {
1643 bv->hideLockedInsetCursor();
1644 setCursorVisible(false);
1646 if (the_locking_inset)
1647 the_locking_inset->hideInsetCursor(bv);
1651 void InsetText::fitInsetCursor(BufferView * bv) const
1653 if (the_locking_inset) {
1654 the_locking_inset->fitInsetCursor(bv);
1657 LyXFont const font =
1658 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1660 int const asc = lyxfont::maxAscent(font);
1661 int const desc = lyxfont::maxDescent(font);
1663 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1667 UpdatableInset::RESULT
1668 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1670 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1671 return moveLeftIntern(bv, false, activate_inset, selecting);
1673 return moveRightIntern(bv, false, activate_inset, selecting);
1677 UpdatableInset::RESULT
1678 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1680 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1681 return moveRightIntern(bv, true, activate_inset, selecting);
1683 return moveLeftIntern(bv, true, activate_inset, selecting);
1687 UpdatableInset::RESULT
1688 InsetText::moveRightIntern(BufferView * bv, bool behind,
1689 bool activate_inset, bool selecting)
1691 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1692 return FINISHED_RIGHT;
1693 if (activate_inset && checkAndActivateInset(bv, behind))
1695 getLyXText(bv)->cursorRight(bv);
1697 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1698 return DISPATCHED_NOUPDATE;
1702 UpdatableInset::RESULT
1703 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1704 bool activate_inset, bool selecting)
1706 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1708 getLyXText(bv)->cursorLeft(bv);
1710 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1711 if (activate_inset && checkAndActivateInset(bv, behind))
1713 return DISPATCHED_NOUPDATE;
1717 UpdatableInset::RESULT
1718 InsetText::moveUp(BufferView * bv)
1720 if (!crow(bv)->previous())
1722 getLyXText(bv)->cursorUp(bv);
1723 return DISPATCHED_NOUPDATE;
1727 UpdatableInset::RESULT
1728 InsetText::moveDown(BufferView * bv)
1730 if (!crow(bv)->next())
1731 return FINISHED_DOWN;
1732 getLyXText(bv)->cursorDown(bv);
1733 return DISPATCHED_NOUPDATE;
1737 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1739 if (the_locking_inset) {
1740 if (the_locking_inset->insetAllowed(inset))
1741 return the_locking_inset->insertInset(bv, inset);
1744 inset->setOwner(this);
1745 hideInsetCursor(bv);
1749 lt = getLyXText(bv);
1752 lt->insertInset(bv, inset);
1754 if ((!cpar(bv)->isInset(cpos(bv))) ||
1755 (cpar(bv)->getInset(cpos(bv)) != inset))
1759 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1760 showInsetCursor(bv);
1767 bool InsetText::insetAllowed(Inset::Code code) const
1769 if (the_locking_inset)
1770 return the_locking_inset->insetAllowed(code);
1775 UpdatableInset * InsetText::getLockingInset() const
1777 return the_locking_inset ? the_locking_inset->getLockingInset() :
1778 const_cast<InsetText *>(this);
1782 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1786 if (the_locking_inset)
1787 return the_locking_inset->getFirstLockingInsetOfType(c);
1792 bool InsetText::showInsetDialog(BufferView * bv) const
1794 if (the_locking_inset)
1795 return the_locking_inset->showInsetDialog(bv);
1800 std::vector<string> const InsetText::getLabelList() const
1802 std::vector<string> label_list;
1804 Paragraph * tpar = par;
1806 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1807 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1808 for (; beg != end; ++beg) {
1809 std::vector<string> const l = (*beg)->getLabelList();
1810 label_list.insert(label_list.end(), l.begin(), l.end());
1812 tpar = tpar->next();
1818 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1821 if (the_locking_inset) {
1822 the_locking_inset->setFont(bv, font, toggleall, selectall);
1825 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1826 getLyXText(bv)->setFont(bv, font, toggleall);
1831 lt = getLyXText(bv);
1834 if (lt->selection.set()) {
1835 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1839 lt->toggleFree(bv, font, toggleall);
1841 lt->clearSelection();
1843 if (selectall || lt->selection.set())
1844 updateLocal(bv, FULL, true);
1846 updateLocal(bv, CURSOR_PAR, true);
1852 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1854 if (cpar(bv)->isInset(cpos(bv))) {
1858 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1859 if (!isHighlyEditableInset(inset))
1861 LyXFont const font =
1862 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1864 x = inset->width(bv, font);
1865 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1868 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1870 //inset_x = cx(bv) - top_x + drawTextXOffset;
1871 //inset_y = cy(bv) + drawTextYOffset;
1872 inset->edit(bv, x, y, 0);
1873 if (!the_locking_inset)
1875 updateLocal(bv, CURSOR, false);
1882 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1885 x -= drawTextXOffset;
1887 int dummyy = y + insetAscent;
1888 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1895 inset_x = cx(bv) - top_x + drawTextXOffset;
1896 inset_y = cy(bv) + drawTextYOffset;
1897 inset->edit(bv, x - inset_x, y - inset_y, button);
1898 if (!the_locking_inset)
1900 updateLocal(bv, CURSOR, false);
1907 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1910 int w = UpdatableInset::getMaxWidth(bv, inset);
1915 w = w - top_x + owner()->x();
1918 w -= (2 * TEXT_TO_INSET_OFFSET);
1921 return UpdatableInset::getMaxWidth(bv, inset);
1926 void InsetText::setParagraphData(Paragraph * p, bool same_id)
1928 // we have to unlock any locked inset otherwise we're in troubles
1929 the_locking_inset = 0;
1931 Paragraph * tmp = par->next();
1936 par = new Paragraph(*p, same_id);
1937 par->setInsetOwner(this);
1938 Paragraph * np = par;
1941 np->next(new Paragraph(*p, same_id));
1942 np->next()->previous(np);
1944 np->setInsetOwner(this);
1951 void InsetText::setText(string const & data)
1954 LyXFont font(LyXFont::ALL_SANE);
1955 for (unsigned int i=0; i < data.length(); ++i)
1956 par->insertChar(i, data[i], font);
1960 void InsetText::setAutoBreakRows(bool flag)
1962 if (flag != autoBreakRows) {
1963 autoBreakRows = flag;
1971 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1973 if (how != drawFrame_) {
1976 updateLocal(bv, DRAW_FRAME, false);
1981 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1983 if (frame_color != col) {
1986 updateLocal(bv, DRAW_FRAME, false);
1991 int InsetText::cx(BufferView * bv) const
1995 lt = getLyXText(bv);
1998 int x = lt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1999 if (the_locking_inset) {
2000 LyXFont font = lt->getFont(bv->buffer(),
2003 if (font.isVisibleRightToLeft())
2004 x -= the_locking_inset->width(bv, font);
2012 int InsetText::cy(BufferView * bv) const
2015 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2019 pos_type InsetText::cpos(BufferView * bv) const
2021 return getLyXText(bv)->cursor.pos();
2025 Paragraph * InsetText::cpar(BufferView * bv) const
2027 return getLyXText(bv)->cursor.par();
2031 bool InsetText::cboundary(BufferView * bv) const
2033 return getLyXText(bv)->cursor.boundary();
2037 Row * InsetText::crow(BufferView * bv) const
2039 return getLyXText(bv)->cursor.row();
2043 LyXText * InsetText::getLyXText(BufferView const * lbv,
2044 bool const recursive) const
2046 if (!recursive && (cached_bview == lbv)) {
2047 LyXText * lt = cached_text.get();
2048 lyx::Assert(lt && lt->firstRow()->par() == par);
2049 return cached_text.get();
2052 // Super UGLY! (Lgb)
2053 BufferView * bv = const_cast<BufferView *>(lbv);
2056 Cache::iterator it = cache.find(bv);
2058 if (it != cache.end()) {
2062 resizeLyXText(do_resize);
2063 if (lt || !it->second.remove) {
2064 lyx::Assert(it->second.text.get());
2065 cached_text = it->second.text;
2066 if (recursive && the_locking_inset) {
2067 return the_locking_inset->getLyXText(bv, true);
2069 return cached_text.get();
2070 } else if (it->second.remove) {
2072 saveLyXTextState(it->second.text.get());
2079 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2080 cached_text->init(bv);
2081 restoreLyXTextState(bv, cached_text.get());
2083 cache.insert(make_pair(bv, cached_text));
2085 if (the_locking_inset && recursive) {
2086 return the_locking_inset->getLyXText(bv);
2088 return cached_text.get();
2092 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2096 Cache::iterator it = cache.find(bv);
2098 if (it == cache.end()) {
2102 lyx::Assert(it->second.text.get());
2104 it->second.remove = true;
2106 /// then remove all LyXText in text-insets
2107 Paragraph * p = par;
2108 for (; p; p = p->next()) {
2109 p->deleteInsetsLyXText(bv);
2115 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2118 // we cannot resize this because we are in use!
2119 // so do this on the next possible getLyXText()
2124 // lyxerr << "InsetText::resizeLyXText\n";
2125 if (!par->next() && !par->size()) // no data, resize not neccessary!
2127 // one endless line, resize normally not necessary
2128 if (!force && getMaxWidth(bv, this) < 0)
2131 Cache::iterator it = cache.find(bv);
2132 if (it == cache.end()) {
2135 lyx::Assert(it->second.text.get());
2137 LyXText * t = it->second.text.get();
2138 saveLyXTextState(t);
2139 for (Paragraph * p = par; p; p = p->next()) {
2140 p->resizeInsetsLyXText(bv);
2143 restoreLyXTextState(bv, t);
2144 if (the_locking_inset) {
2145 inset_x = cx(bv) - top_x + drawTextXOffset;
2146 inset_y = cy(bv) + drawTextYOffset;
2150 t->first = bv->screen()->topCursorVisible(t);
2153 updateLocal(bv, FULL, false);
2154 // this will scroll the screen such that the cursor becomes visible
2155 bv->updateScrollbar();
2157 need_update |= FULL;
2162 void InsetText::reinitLyXText() const
2165 // we cannot resize this because we are in use!
2166 // so do this on the next possible getLyXText()
2172 // lyxerr << "InsetText::reinitLyXText\n";
2173 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2174 lyx::Assert(it->second.text.get());
2176 LyXText * t = it->second.text.get();
2177 BufferView * bv = it->first;
2179 saveLyXTextState(t);
2180 for (Paragraph * p = par; p; p = p->next()) {
2181 p->resizeInsetsLyXText(bv);
2184 restoreLyXTextState(bv, t);
2185 if (the_locking_inset) {
2186 inset_x = cx(bv) - top_x + drawTextXOffset;
2187 inset_y = cy(bv) + drawTextYOffset;
2190 t->first = bv->screen()->topCursorVisible(t);
2193 updateLocal(bv, FULL, false);
2194 // this will scroll the screen such that the cursor becomes visible
2195 bv->updateScrollbar();
2203 void InsetText::removeNewlines()
2205 for (Paragraph * p = par; p; p = p->next()) {
2206 for (int i = 0; i < p->size(); ++i) {
2207 if (p->getChar(i) == Paragraph::META_NEWLINE)
2214 bool InsetText::nodraw() const
2216 if (the_locking_inset)
2217 return the_locking_inset->nodraw();
2218 return UpdatableInset::nodraw();
2222 int InsetText::scroll(bool recursive) const
2224 int sx = UpdatableInset::scroll(false);
2226 if (recursive && the_locking_inset)
2227 sx += the_locking_inset->scroll(recursive);
2233 bool InsetText::doClearArea() const
2235 return !locked || (need_update & (FULL|INIT));
2239 void InsetText::selectAll(BufferView * bv)
2241 getLyXText(bv)->cursorTop(bv);
2242 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2243 getLyXText(bv)->cursorBottom(bv);
2244 getLyXText(bv)->setSelection(bv);
2248 void InsetText::clearSelection(BufferView * bv)
2250 getLyXText(bv)->clearSelection();
2254 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
2257 int h = insetAscent + insetDescent;
2258 int ty = baseline - insetAscent;
2264 if ((ty + h) > pain.paperHeight())
2265 h = pain.paperHeight();
2266 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2267 w = pain.paperWidth();
2268 // w -= TEXT_TO_INSET_OFFSET;
2269 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2272 frame_is_visible = false;
2276 Paragraph * InsetText::getParFromID(int id) const
2279 Paragraph * result = par;
2280 Paragraph * ires = 0;
2281 while (result && result->id() != id) {
2282 if ((ires = result->getParFromID(id)))
2284 result = result->next();
2288 Paragraph * tmp = par;
2290 int tmp_id = tmp->id();
2291 if (tmp->id() == id) {
2294 Paragraph * tmp2 = tmp->getParFromID(id);
2305 Paragraph * InsetText::firstParagraph() const
2308 if (the_locking_inset)
2309 if ((result = the_locking_inset->firstParagraph()))
2315 Paragraph * InsetText::getFirstParagraph(int i) const
2317 return (i == 0) ? par : 0;
2321 LyXCursor const & InsetText::cursor(BufferView * bv) const
2323 if (the_locking_inset)
2324 return the_locking_inset->cursor(bv);
2325 return getLyXText(bv)->cursor;
2329 Paragraph * InsetText::paragraph() const
2335 void InsetText::paragraph(Paragraph * p)
2338 // set ourself as owner for all the paragraphs inserted!
2339 Paragraph * np = par;
2341 np->setInsetOwner(this);
2345 // redraw myself when asked for
2350 Inset * InsetText::getInsetFromID(int id_arg) const
2353 return const_cast<InsetText *>(this);
2355 Paragraph * lp = par;
2358 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2359 en = lp->inset_iterator_end();
2362 if ((*it)->id() == id_arg)
2364 Inset * in = (*it)->getInsetFromID(id_arg);
2374 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2380 lt = getLyXText(bv);
2383 if (the_locking_inset) {
2384 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2391 #warning Dekel please have a look on this one RTL? (Jug)
2393 // we have to go on checking so move cusor to the right
2394 lt->cursor.pos(lt->cursor.pos() + 1);
2396 str = lt->selectNextWordToSpellcheck(bv, value);
2398 bv->unlockInset(const_cast<InsetText *>(this));
2407 void InsetText::selectSelectedWord(BufferView * bv)
2409 if (the_locking_inset) {
2410 the_locking_inset->selectSelectedWord(bv);
2413 getLyXText(bv)->selectSelectedWord(bv);
2414 updateLocal(bv, SELECTION, false);
2418 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2420 if (the_locking_inset) {
2421 the_locking_inset->toggleSelection(bv, kill_selection);
2425 lt = getLyXText(bv);
2429 int x = top_x + TEXT_TO_INSET_OFFSET;
2431 Row * row = lt->firstRow();
2432 int y_offset = top_baseline - row->ascent_of_text();
2434 while ((row != 0) && ((y+row->height()) <= 0)) {
2441 if (need_update & SELECTION)
2443 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2449 bool InsetText::searchForward(BufferView * bv, string const & str,
2450 bool const & cs, bool const & mw)
2452 if (the_locking_inset) {
2453 if (the_locking_inset->searchForward(bv, str, cs, mw))
2457 lt = getLyXText(bv);
2460 Paragraph * lpar = lt->cursor.par();
2461 pos_type pos = lt->cursor.pos();
2462 if (pos < lpar->size() - 1)
2466 lpar = lpar->next();
2471 // we have to unlock ourself in this function by default!
2472 bv->unlockInset(const_cast<InsetText *>(this));
2475 lt->setCursor(bv, lpar, pos);
2479 if (LyXFind(bv, str, true, true, cs , mw)) {
2482 // we have to unlock ourself in this function by default!
2483 bv->unlockInset(const_cast<InsetText *>(this));
2487 bool InsetText::searchBackward(BufferView * bv, string const & str,
2488 bool const & cs, bool const & mw)
2490 if (the_locking_inset)
2491 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2493 if (LyXFind(bv, str, false, true, cs, mw)) {
2496 // we have to unlock ourself in this function by default!
2497 bv->unlockInset(const_cast<InsetText *>(this));
2502 bool InsetText::checkInsertChar(LyXFont & font)
2505 return owner()->checkInsertChar(font);