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;
186 no_selection = false;
193 last_drawn_width = -1;
194 frame_is_visible = false;
197 in_insetAllowed = false;
201 InsetText::~InsetText()
208 Paragraph * tmp = par->next();
215 void InsetText::clear()
218 Paragraph * tmp = par->next();
228 Inset * InsetText::clone(Buffer const &, bool same_id) const
230 return new InsetText(*this, same_id);
234 void InsetText::write(Buffer const * buf, ostream & os) const
237 writeParagraphData(buf, os);
241 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
243 par->writeFile(buf, os, buf->params, 0);
247 void InsetText::read(Buffer const * buf, LyXLex & lex)
251 Paragraph * return_par = 0;
252 Paragraph::depth_type depth = 0;
253 LyXFont font(LyXFont::ALL_INHERIT);
259 token = lex.getString();
262 if (token == "\\end_inset") {
263 #ifndef NO_COMPABILITY
264 const_cast<Buffer*>(buf)->insertErtContents(par, pos, font, false);
269 if (const_cast<Buffer*>(buf)->
270 parseSingleLyXformat2Token(lex, par, return_par,
271 token, pos, depth, font)) {
272 // the_end read this should NEVER happen
273 lex.printError("\\the_end read in inset! Error in document!");
281 return_par->setInsetOwner(this);
282 return_par = return_par->next();
285 if (token != "\\end_inset") {
286 lex.printError("Missing \\end_inset at this point. "
293 int InsetText::ascent(BufferView * bv, LyXFont const &) const
300 insetAscent = lt->firstRow()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
307 int InsetText::descent(BufferView * bv, LyXFont const &) const
314 insetDescent = lt->height - lt->firstRow()->ascent_of_text() +
315 TEXT_TO_INSET_OFFSET;
322 int InsetText::width(BufferView * bv, LyXFont const &) const
324 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
325 (2 * TEXT_TO_INSET_OFFSET);
326 insetWidth = max(insetWidth, 10);
331 int InsetText::textWidth(BufferView * bv, bool fordraw) const
334 if (!autoBreakRows) {
337 w = getMaxWidth(bv, this);
340 return max(w - (2 * TEXT_TO_INSET_OFFSET),
341 (int)getLyXText(bv)->width);
345 return w - (2 * TEXT_TO_INSET_OFFSET);
349 void InsetText::draw(BufferView * bv, LyXFont const & f,
350 int baseline, float & x, bool cleared) const
355 Painter & pain = bv->painter();
357 // this is the first thing we have to ask because if the x pos
358 // changed we have to do a complete rebreak of the text as we
359 // may have few space to draw in. Well we should check on this too
361 if (top_x != int(x)) {
363 int nw = getMaxWidth(bv, this);
364 if (nw > 0 && old_max_width != nw) {
367 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
375 // repaint the background if needed
376 if (cleared && backgroundColor() != LColor::background) {
378 clearInset(bv, baseline, cleared);
382 // no draw is necessary !!!
383 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
386 top_baseline = baseline;
388 if (need_update & CLEAR_FRAME)
389 clearFrame(pain, cleared);
396 x += static_cast<float>(scroll());
398 // if top_x differs we did it already
399 if (!cleared && (top_x == int(x))
400 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
401 ||(last_drawn_width != insetWidth))) {
402 clearInset(bv, baseline, cleared);
408 frame_is_visible = false;
410 if (!cleared && (need_update == NONE)) {
412 drawFrame(pain, cleared);
416 top_baseline = baseline;
417 top_y = baseline - ascent(bv, f);
419 if (last_drawn_width != insetWidth) {
421 clearInset(bv, baseline, cleared);
423 last_drawn_width = insetWidth;
426 if (the_locking_inset && (cpar(bv) == inset_par)
427 && (cpos(bv) == inset_pos)) {
428 inset_x = cx(bv) - top_x + drawTextXOffset;
429 inset_y = cy(bv) + drawTextYOffset;
431 if (!cleared && (need_update == CURSOR)
432 && !getLyXText(bv)->selection.set()) {
433 drawFrame(pain, cleared);
443 x += TEXT_TO_INSET_OFFSET;
445 Row * row = lt->firstRow();
446 int y_offset = baseline - row->ascent_of_text();
447 int ph = pain.paperHeight();
450 while ((row != 0) && ((y+row->height()) <= 0)) {
452 first += row->height();
458 if (cleared || (need_update&(INIT|FULL))) {
461 while ((row != 0) && (yf < ph)) {
462 lt->getVisibleRow(bv, y+y_offset, int(x), row,
468 } else if (!locked) {
469 if (need_update & CURSOR) {
470 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
471 lt->clearSelection();
472 lt->selection.cursor = lt->cursor;
474 bv->screen()->update(lt, bv, y_offset, int(x));
477 if (need_update & SELECTION) {
478 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
479 } else if (need_update & CURSOR) {
480 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
481 lt->clearSelection();
482 lt->selection.cursor = lt->cursor;
484 bv->screen()->update(lt, bv, y_offset, int(x));
489 lt->status(bv, LyXText::UNCHANGED);
490 if ((need_update != CURSOR_PAR) &&
491 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked))) {
492 drawFrame(pain, cleared);
493 } else if (need_update & CLEAR_FRAME) {
494 clearFrame(pain, cleared);
497 x += insetWidth - TEXT_TO_INSET_OFFSET;
499 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
501 } else if (need_update != INIT) {
510 void InsetText::drawFrame(Painter & pain, bool cleared) const
512 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
513 if (!frame_is_visible || cleared) {
514 frame_x = top_x + ttoD2;
515 frame_y = top_baseline - insetAscent + ttoD2;
516 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
517 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
518 pain.rectangle(frame_x, frame_y,
521 frame_is_visible = true;
526 void InsetText::clearFrame(Painter & pain, bool cleared) const
528 if (frame_is_visible) {
530 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
533 frame_is_visible = false;
538 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
541 if (reinit && owner()) {
543 owner()->update(bv, font, true);
548 if (reinit || need_update == INIT) {
550 // we should put this call where we set need_update to INIT!
553 owner()->update(bv, font, true);
557 if (the_locking_inset) {
558 inset_x = cx(bv) - top_x + drawTextXOffset;
559 inset_y = cy(bv) + drawTextYOffset;
560 the_locking_inset->update(bv, font, reinit);
569 int oldw = insetWidth;
570 insetWidth = lt->width + (2 * TEXT_TO_INSET_OFFSET);
571 if (oldw != insetWidth) {
580 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
583 lt->updateInset(bv, the_locking_inset);
585 if (lt->status() == LyXText::NEED_MORE_REFRESH)
593 void InsetText::setUpdateStatus(BufferView * bv, int what) const
601 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
602 // if we don't break row so that we only have one row to update!
603 if ((lt->status() == LyXText::NEED_MORE_REFRESH) ||
605 (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
608 } else if (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
609 need_update |= CURSOR_PAR;
612 // this to not draw a selection when we redraw all of it!
613 if (need_update & CURSOR && !(need_update & SELECTION)) {
614 if (lt->selection.set())
616 lt->clearSelection();
623 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
631 setUpdateStatus(bv, what);
632 if (((need_update != CURSOR) && (need_update != NONE)) ||
633 (lt->status() != LyXText::UNCHANGED) || lt->selection.set())
635 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
637 if (need_update == CURSOR)
639 bv->owner()->showState();
640 if (old_par != cpar(bv)) {
641 bv->owner()->setLayout(cpar(bv)->getLayout());
649 string const InsetText::editMessage() const
651 return _("Opened Text Inset");
655 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
657 UpdatableInset::edit(bv, x, y, button);
659 if (!bv->lockInset(this)) {
660 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
664 the_locking_inset = 0;
665 inset_pos = inset_x = inset_y = 0;
666 inset_boundary = false;
669 int tmp_y = (y < 0) ? 0 : y;
676 if (!checkAndActivateInset(bv, x, tmp_y, button))
677 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
679 lt->clearSelection();
682 updateLocal(bv, CURSOR, false);
684 // If the inset is empty set the language of the current font to the
685 // language to the surronding text (if different).
686 if (par->size() == 0 && !par->next() &&
687 bv->getParentLanguage(this) != lt->current_font.language()) {
688 LyXFont font(LyXFont::ALL_IGNORE);
689 font.setLanguage(bv->getParentLanguage(this));
690 setFont(bv, font, false);
697 void InsetText::edit(BufferView * bv, bool front)
699 UpdatableInset::edit(bv, front);
701 if (!bv->lockInset(this)) {
702 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
706 the_locking_inset = 0;
707 inset_pos = inset_x = inset_y = 0;
708 inset_boundary = false;
717 lt->setCursor(bv, par, 0);
722 // int const pos = (p->size() ? p->size()-1 : p->size());
723 lt->setCursor(bv, p, p->size());
725 lt->clearSelection();
728 updateLocal(bv, CURSOR, false);
730 // If the inset is empty set the language of the current font to the
731 // language to the surronding text (if different).
732 if (par->size() == 0 && !par->next() &&
733 bv->getParentLanguage(this) != lt->current_font.language()) {
734 LyXFont font(LyXFont::ALL_IGNORE);
735 font.setLanguage(bv->getParentLanguage(this));
736 setFont(bv, font, false);
743 void InsetText::insetUnlock(BufferView * bv)
745 if (the_locking_inset) {
746 the_locking_inset->insetUnlock(bv);
747 the_locking_inset = 0;
750 no_selection = false;
753 if (drawFrame_ == LOCKED)
754 code = CURSOR|CLEAR_FRAME;
762 if (lt->selection.set()) {
763 lt->clearSelection();
765 } else if (owner()) {
766 bv->owner()->setLayout(owner()->getLyXText(bv)
767 ->cursor.par()->getLayout());
769 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
770 // hack for deleteEmptyParMech
771 lt->setCursor(bv, par, 0);
772 updateLocal(bv, code, false);
778 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
780 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
784 if (inset == cpar(bv)->getInset(cpos(bv))) {
785 lyxerr[Debug::INSETS] << "OK" << endl;
786 the_locking_inset = inset;
787 inset_x = cx(bv) - top_x + drawTextXOffset;
788 inset_y = cy(bv) + drawTextYOffset;
789 inset_pos = cpos(bv);
790 inset_par = cpar(bv);
791 inset_boundary = cboundary(bv);
792 updateLocal(bv, CURSOR, false);
794 } else if (the_locking_inset && (the_locking_inset == inset)) {
795 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
796 lyxerr[Debug::INSETS] << "OK" << endl;
797 inset_x = cx(bv) - top_x + drawTextXOffset;
798 inset_y = cy(bv) + drawTextYOffset;
800 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
802 } else if (the_locking_inset) {
803 lyxerr[Debug::INSETS] << "MAYBE" << endl;
804 return the_locking_inset->lockInsetInInset(bv, inset);
806 lyxerr[Debug::INSETS] << "NOT OK" << endl;
811 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
814 if (!the_locking_inset)
816 if (the_locking_inset == inset) {
817 the_locking_inset->insetUnlock(bv);
818 getLyXText(bv)->updateInset(bv, inset);
819 the_locking_inset = 0;
821 moveRight(bv, false);
822 old_par = 0; // force layout setting
826 updateLocal(bv, CURSOR, false);
829 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
833 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
840 if (!the_locking_inset) {
841 bool found = lt->updateInset(bv, inset);
845 setUpdateStatus(bv, NONE);
848 if (the_locking_inset != inset) {
850 lt->updateInset(bv, the_locking_inset);
851 setUpdateStatus(bv, CURSOR_PAR);
853 bool found = the_locking_inset->updateInsetInInset(bv, inset);
857 setUpdateStatus(bv, CURSOR_PAR);
860 bool found = lt->updateInset(bv, inset);
864 setUpdateStatus(bv, CURSOR_PAR);
866 updateLocal(bv, CURSOR_PAR, false);
868 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
869 inset_x = cx(bv) - top_x + drawTextXOffset;
870 inset_y = cy(bv) + drawTextYOffset;
877 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
881 int tmp_x = x - drawTextXOffset;
882 int tmp_y = y + insetAscent - getLyXText(bv)->first;
883 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
886 if (the_locking_inset) {
887 if (the_locking_inset == inset) {
888 the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
889 no_selection = false;
892 // otherwise unlock the_locking_inset and lock the new inset
893 the_locking_inset->insetUnlock(bv);
894 inset_x = cx(bv) - top_x + drawTextXOffset;
895 inset_y = cy(bv) + drawTextYOffset;
896 the_locking_inset = static_cast<UpdatableInset*>(inset);
897 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
898 inset->edit(bv, x - inset_x, y - inset_y, button);
899 if (the_locking_inset)
900 updateLocal(bv, CURSOR, false);
901 no_selection = false;
904 // otherwise only unlock the_locking_inset
905 the_locking_inset->insetUnlock(bv);
906 the_locking_inset = 0;
908 if (bv->theLockingInset()) {
909 if (isHighlyEditableInset(inset)) {
910 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
911 inset_x = cx(bv) - top_x + drawTextXOffset;
912 inset_y = cy(bv) + drawTextYOffset;
913 inset_pos = cpos(bv);
914 inset_par = cpar(bv);
915 inset_boundary = cboundary(bv);
916 the_locking_inset = uinset;
917 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
919 uinset->edit(bv, x - inset_x, y - inset_y, 0);
920 if (the_locking_inset)
921 updateLocal(bv, CURSOR, false);
922 no_selection = false;
926 if (!inset) { // && (button == 2)) {
927 bool paste_internally = false;
928 if ((button == 2) && getLyXText(bv)->selection.set()) {
929 localDispatch(bv, LFUN_COPY, "");
930 paste_internally = true;
937 lt->setCursorFromCoordinates(bv, x-drawTextXOffset, y + insetAscent);
938 if (lt->selection.set()) {
939 lt->clearSelection();
940 updateLocal(bv, FULL, false);
942 lt->clearSelection();
944 bv->owner()->setLayout(cpar(bv)->getLayout());
946 // Insert primary selection with middle mouse
947 // if there is a local selection in the current buffer,
950 if (paste_internally)
951 localDispatch(bv, LFUN_PASTE, "");
953 localDispatch(bv, LFUN_PASTESELECTION,
960 no_selection = false;
964 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
966 UpdatableInset * inset = 0;
968 if (the_locking_inset) {
969 the_locking_inset->insetButtonRelease(bv,
970 x - inset_x, y - inset_y,
973 if (cpar(bv)->isInset(cpos(bv))) {
974 inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
975 if (isHighlyEditableInset(inset)) {
976 inset->insetButtonRelease(bv,
978 y - inset_y, button);
980 inset_x = cx(bv) - top_x + drawTextXOffset;
981 inset_y = cy(bv) + drawTextYOffset;
982 inset->insetButtonRelease(bv,
984 y - inset_y, button);
986 x - inset_x, y - inset_y, button);
988 updateLocal(bv, CURSOR_PAR, false);
991 no_selection = false;
995 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
999 if (the_locking_inset) {
1000 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1006 lt = getLyXText(bv);
1009 hideInsetCursor(bv);
1010 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1011 lt->setSelection(bv);
1012 if (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1013 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos())
1015 updateLocal(bv, SELECTION, false);
1017 showInsetCursor(bv);
1023 void InsetText::insetKeyPress(XKeyEvent * xke)
1025 if (the_locking_inset) {
1026 the_locking_inset->insetKeyPress(xke);
1032 UpdatableInset::RESULT
1033 InsetText::localDispatch(BufferView * bv,
1034 kb_action action, string const & arg)
1036 bool was_empty = par->size() == 0 && !par->next();
1037 no_selection = false;
1038 UpdatableInset::RESULT
1039 result= UpdatableInset::localDispatch(bv, action, arg);
1040 if (result != UNDISPATCHED) {
1044 result = DISPATCHED;
1045 if ((action < 0) && arg.empty())
1048 if (the_locking_inset) {
1049 result = the_locking_inset->localDispatch(bv, action, arg);
1050 if (result == DISPATCHED_NOUPDATE)
1052 else if (result == DISPATCHED) {
1053 updateLocal(bv, CURSOR_PAR, false);
1055 } else if (result >= FINISHED) {
1057 case FINISHED_RIGHT:
1058 moveRightIntern(bv, false, false);
1059 result = DISPATCHED;
1062 if ((result = moveUp(bv)) >= FINISHED) {
1063 updateLocal(bv, CURSOR, false);
1064 bv->unlockInset(this);
1068 if ((result = moveDown(bv)) >= FINISHED) {
1069 updateLocal(bv, CURSOR, false);
1070 bv->unlockInset(this);
1074 result = DISPATCHED;
1077 the_locking_inset = 0;
1078 #ifdef WITH_WARNINGS
1079 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1084 hideInsetCursor(bv);
1087 lt = getLyXText(bv);
1092 case LFUN_SELFINSERT:
1093 if (bv->buffer()->isReadonly()) {
1094 // setErrorMessage(N_("Document is read only"));
1098 /* Automatically delete the currently selected
1099 * text and replace it with what is being
1100 * typed in now. Depends on lyxrc settings
1101 * "auto_region_delete", which defaults to
1104 setUndo(bv, Undo::INSERT,
1105 lt->cursor.par(), lt->cursor.par()->next());
1107 if (lyxrc.auto_region_delete) {
1108 if (lt->selection.set()) {
1109 lt->cutSelection(bv, false);
1112 lt->clearSelection();
1113 for (string::size_type i = 0; i < arg.length(); ++i) {
1114 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1117 lt->selection.cursor = lt->cursor;
1118 updateLocal(bv, CURSOR_PAR, true);
1119 result = DISPATCHED_NOUPDATE;
1121 // --- Cursor Movements -----------------------------------
1124 moveRight(bv, false, true);
1125 lt->setSelection(bv);
1126 updateLocal(bv, SELECTION, false);
1129 result = moveRight(bv);
1131 updateLocal(bv, CURSOR, false);
1135 moveLeft(bv, false, true);
1136 lt->setSelection(bv);
1137 updateLocal(bv, SELECTION, false);
1141 result = moveLeft(bv);
1142 updateLocal(bv, CURSOR, false);
1147 lt->setSelection(bv);
1148 updateLocal(bv, SELECTION, false);
1152 result = moveDown(bv);
1153 updateLocal(bv, CURSOR, false);
1158 lt->setSelection(bv);
1159 updateLocal(bv, SELECTION, false);
1163 result = moveUp(bv);
1164 updateLocal(bv, CURSOR, false);
1169 updateLocal(bv, CURSOR, false);
1173 updateLocal(bv, CURSOR, false);
1175 case LFUN_BACKSPACE: {
1176 setUndo(bv, Undo::DELETE,
1177 lt->cursor.par(), lt->cursor.par()->next());
1178 if (lt->selection.set())
1179 lt->cutSelection(bv);
1182 updateLocal(bv, CURSOR_PAR, true);
1187 setUndo(bv, Undo::DELETE,
1188 lt->cursor.par(), lt->cursor.par()->next());
1189 if (lt->selection.set()) {
1190 lt->cutSelection(bv);
1194 updateLocal(bv, CURSOR_PAR, true);
1199 setUndo(bv, Undo::DELETE,
1200 lt->cursor.par(), lt->cursor.par()->next());
1201 lt->cutSelection(bv);
1202 updateLocal(bv, CURSOR_PAR, true);
1208 lt->copySelection(bv);
1209 updateLocal(bv, CURSOR_PAR, false);
1211 case LFUN_PASTESELECTION:
1213 string const clip(bv->getClipboard());
1217 if (arg == "paragraph") {
1218 lt->insertStringAsParagraphs(bv, clip);
1220 lt->insertStringAsLines(bv, clip);
1222 updateLocal(bv, CURSOR_PAR, true);
1226 if (!autoBreakRows) {
1228 if (CutAndPaste::nrOfParagraphs() > 1) {
1229 Alert::alert(_("Impossible operation"),
1230 _("Cannot include more than one paragraph!"),
1235 setUndo(bv, Undo::INSERT,
1236 lt->cursor.par(), lt->cursor.par()->next());
1237 lt->pasteSelection(bv);
1238 updateLocal(bv, CURSOR_PAR, true);
1242 case LFUN_BREAKPARAGRAPH:
1243 if (!autoBreakRows) {
1244 result = DISPATCHED;
1247 lt->breakParagraph(bv, 0);
1248 updateLocal(bv, FULL, true);
1250 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1251 if (!autoBreakRows) {
1252 result = DISPATCHED;
1255 lt->breakParagraph(bv, 1);
1256 updateLocal(bv, FULL, true);
1259 case LFUN_BREAKLINE: {
1260 if (!autoBreakRows) {
1261 result = DISPATCHED;
1264 setUndo(bv, Undo::INSERT,
1265 lt->cursor.par(), lt->cursor.par()->next());
1266 lt->insertChar(bv, Paragraph::META_NEWLINE);
1267 updateLocal(bv, CURSOR_PAR, true);
1272 // do not set layouts on non breakable textinsets
1273 if (autoBreakRows) {
1274 layout_type cur_layout = cpar(bv)->layout;
1276 // Derive layout number from given argument (string)
1277 // and current buffer's textclass (number). */
1278 textclass_type tclass = bv->buffer()->params.textclass;
1279 std::pair <bool, layout_type> layout =
1280 textclasslist.NumberOfLayout(tclass, arg);
1282 // If the entry is obsolete, use the new one instead.
1284 string obs = textclasslist.Style(tclass,layout.second).
1287 layout = textclasslist.NumberOfLayout(tclass, obs);
1290 // see if we found the layout number:
1291 if (!layout.first) {
1292 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1293 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1297 if (cur_layout != layout.second) {
1298 cur_layout = layout.second;
1299 lt->setLayout(bv, layout.second);
1300 bv->owner()->setLayout(cpar(bv)->getLayout());
1301 updateLocal(bv, CURSOR_PAR, true);
1304 // reset the layout box
1305 bv->owner()->setLayout(cpar(bv)->getLayout());
1308 case LFUN_PARAGRAPH_SPACING:
1309 // This one is absolutely not working. When fiddling with this
1310 // it also seems to me that the paragraphs inside the insettext
1311 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1313 Paragraph * par = lt->cursor.par();
1314 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1315 float cur_value = 1.0;
1316 if (cur_spacing == Spacing::Other) {
1317 cur_value = par->params().spacing().getValue();
1320 istringstream istr(arg.c_str());
1323 Spacing::Space new_spacing = cur_spacing;
1324 float new_value = cur_value;
1326 lyxerr << "Missing argument to `paragraph-spacing'"
1328 } else if (tmp == "single") {
1329 new_spacing = Spacing::Single;
1330 } else if (tmp == "onehalf") {
1331 new_spacing = Spacing::Onehalf;
1332 } else if (tmp == "double") {
1333 new_spacing = Spacing::Double;
1334 } else if (tmp == "other") {
1335 new_spacing = Spacing::Other;
1338 lyxerr << "new_value = " << tmpval << endl;
1341 } else if (tmp == "default") {
1342 new_spacing = Spacing::Default;
1344 lyxerr << _("Unknown spacing argument: ")
1347 if (cur_spacing != new_spacing || cur_value != new_value) {
1348 par->params().spacing(Spacing(new_spacing, new_value));
1349 updateLocal(bv, CURSOR_PAR, true);
1355 if (!bv->Dispatch(action, arg))
1356 result = UNDISPATCHED;
1360 /// If the action has deleted all text in the inset, we need to change the
1361 // language to the language of the surronding text.
1362 if (!was_empty && par->size() == 0 && !par->next()) {
1363 LyXFont font(LyXFont::ALL_IGNORE);
1364 font.setLanguage(bv->getParentLanguage(this));
1365 setFont(bv, font, false);
1368 if (result < FINISHED) {
1369 showInsetCursor(bv);
1371 bv->unlockInset(this);
1378 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1381 buf->latexParagraphs(os, par, 0, texrow);
1382 return texrow.rows();
1386 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1388 Paragraph * p = par;
1389 unsigned int lines = 0;
1392 string const tmp = buf->asciiParagraph(p, linelen);
1393 lines += countChar(tmp, '\n');
1401 int InsetText::docbook(Buffer const * buf, ostream & os) const
1403 Paragraph * p = par;
1404 unsigned int lines = 0;
1406 vector<string> environment_stack(10);
1407 vector<string> environment_inner(10);
1409 int const command_depth = 0;
1412 Paragraph::depth_type depth = 0; // paragraph depth
1416 int desc_on = 0; // description mode
1418 LyXLayout const & style =
1419 textclasslist.Style(buf->params.textclass,
1422 // environment tag closing
1423 for (; depth > p->params().depth(); --depth) {
1424 if (environment_inner[depth] != "!-- --") {
1425 item_name = "listitem";
1426 buf->sgmlCloseTag(os, command_depth + depth,
1428 if (environment_inner[depth] == "varlistentry")
1429 buf->sgmlCloseTag(os, depth+command_depth,
1430 environment_inner[depth]);
1432 buf->sgmlCloseTag(os, depth + command_depth,
1433 environment_stack[depth]);
1434 environment_stack[depth].erase();
1435 environment_inner[depth].erase();
1438 if (depth == p->params().depth()
1439 && environment_stack[depth] != style.latexname()
1440 && !environment_stack[depth].empty()) {
1441 if (environment_inner[depth] != "!-- --") {
1442 item_name= "listitem";
1443 buf->sgmlCloseTag(os, command_depth+depth,
1445 if (environment_inner[depth] == "varlistentry")
1446 buf->sgmlCloseTag(os,
1447 depth + command_depth,
1448 environment_inner[depth]);
1451 buf->sgmlCloseTag(os, depth + command_depth,
1452 environment_stack[depth]);
1454 environment_stack[depth].erase();
1455 environment_inner[depth].erase();
1458 // Write opening SGML tags.
1459 switch (style.latextype) {
1460 case LATEX_PARAGRAPH:
1461 buf->sgmlOpenTag(os, depth + command_depth,
1466 buf->sgmlError(p, 0,
1467 _("Error : LatexType Command not allowed here.\n"));
1471 case LATEX_ENVIRONMENT:
1472 case LATEX_ITEM_ENVIRONMENT:
1473 if (depth < p->params().depth()) {
1474 depth = p->params().depth();
1475 environment_stack[depth].erase();
1478 if (environment_stack[depth] != style.latexname()) {
1479 if(environment_stack.size() == depth + 1) {
1480 environment_stack.push_back("!-- --");
1481 environment_inner.push_back("!-- --");
1483 environment_stack[depth] = style.latexname();
1484 environment_inner[depth] = "!-- --";
1485 buf->sgmlOpenTag(os, depth + command_depth,
1486 environment_stack[depth]);
1488 if (environment_inner[depth] != "!-- --") {
1489 item_name= "listitem";
1490 buf->sgmlCloseTag(os,
1491 command_depth + depth,
1493 if (environment_inner[depth] == "varlistentry")
1494 buf->sgmlCloseTag(os,
1495 depth + command_depth,
1496 environment_inner[depth]);
1500 if (style.latextype == LATEX_ENVIRONMENT) {
1501 if (!style.latexparam().empty()) {
1502 if(style.latexparam() == "CDATA")
1505 buf->sgmlOpenTag(os, depth + command_depth,
1506 style.latexparam());
1511 desc_on = (style.labeltype == LABEL_MANUAL);
1514 environment_inner[depth]= "varlistentry";
1516 environment_inner[depth]= "listitem";
1518 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1519 environment_inner[depth]);
1523 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1527 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1532 buf->sgmlOpenTag(os, depth + command_depth,
1537 buf->simpleDocBookOnePar(os, p, desc_on,
1538 depth + 1 + command_depth);
1542 // write closing SGML tags
1543 switch (style.latextype) {
1544 case LATEX_ENVIRONMENT:
1545 if (!style.latexparam().empty()) {
1546 if(style.latexparam() == "CDATA")
1549 buf->sgmlCloseTag(os, depth + command_depth,
1550 style.latexparam());
1553 case LATEX_ITEM_ENVIRONMENT:
1554 if (desc_on == 1) break;
1556 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1558 case LATEX_PARAGRAPH:
1559 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1562 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1568 for (int d = depth; d >= 0; --d) {
1569 if (!environment_stack[depth].empty()) {
1570 if (environment_inner[depth] != "!-- --") {
1571 item_name = "listitem";
1572 buf->sgmlCloseTag(os, command_depth + depth,
1574 if (environment_inner[depth] == "varlistentry")
1575 buf->sgmlCloseTag(os, depth + command_depth,
1576 environment_inner[depth]);
1579 buf->sgmlCloseTag(os, depth + command_depth,
1580 environment_stack[depth]);
1588 void InsetText::validate(LaTeXFeatures & features) const
1590 Paragraph * p = par;
1592 p->validate(features);
1598 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1600 if (textclasslist.Style(buf->params.textclass,
1601 p->getLayout()).labeltype != LABEL_MANUAL)
1604 return p->beginningOfMainBody();
1608 void InsetText::getCursorPos(BufferView * bv,
1609 int & x, int & y) const
1611 if (the_locking_inset) {
1612 the_locking_inset->getCursorPos(bv, x, y);
1620 unsigned int InsetText::insetInInsetY()
1622 if (!the_locking_inset)
1625 return (inset_y + the_locking_inset->insetInInsetY());
1629 void InsetText::toggleInsetCursor(BufferView * bv)
1631 if (the_locking_inset) {
1632 the_locking_inset->toggleInsetCursor(bv);
1636 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1638 int const asc = lyxfont::maxAscent(font);
1639 int const desc = lyxfont::maxDescent(font);
1641 if (isCursorVisible())
1642 bv->hideLockedInsetCursor();
1644 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1645 toggleCursorVisible();
1649 void InsetText::showInsetCursor(BufferView * bv, bool show)
1651 if (the_locking_inset) {
1652 the_locking_inset->showInsetCursor(bv, show);
1655 if (!isCursorVisible()) {
1656 LyXFont const font =
1657 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1659 int const asc = lyxfont::maxAscent(font);
1660 int const desc = lyxfont::maxDescent(font);
1662 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1664 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1665 setCursorVisible(true);
1670 void InsetText::hideInsetCursor(BufferView * bv)
1672 if (isCursorVisible()) {
1673 bv->hideLockedInsetCursor();
1674 setCursorVisible(false);
1676 if (the_locking_inset)
1677 the_locking_inset->hideInsetCursor(bv);
1681 void InsetText::fitInsetCursor(BufferView * bv) const
1683 if (the_locking_inset) {
1684 the_locking_inset->fitInsetCursor(bv);
1687 LyXFont const font =
1688 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1690 int const asc = lyxfont::maxAscent(font);
1691 int const desc = lyxfont::maxDescent(font);
1693 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1697 UpdatableInset::RESULT
1698 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1700 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1701 return moveLeftIntern(bv, false, activate_inset, selecting);
1703 return moveRightIntern(bv, false, activate_inset, selecting);
1707 UpdatableInset::RESULT
1708 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1710 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1711 return moveRightIntern(bv, true, activate_inset, selecting);
1713 return moveLeftIntern(bv, true, activate_inset, selecting);
1717 UpdatableInset::RESULT
1718 InsetText::moveRightIntern(BufferView * bv, bool behind,
1719 bool activate_inset, bool selecting)
1721 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1722 return FINISHED_RIGHT;
1723 if (activate_inset && checkAndActivateInset(bv, behind))
1725 getLyXText(bv)->cursorRight(bv);
1727 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1728 return DISPATCHED_NOUPDATE;
1732 UpdatableInset::RESULT
1733 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1734 bool activate_inset, bool selecting)
1736 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1738 getLyXText(bv)->cursorLeft(bv);
1740 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1741 if (activate_inset && checkAndActivateInset(bv, behind))
1743 return DISPATCHED_NOUPDATE;
1747 UpdatableInset::RESULT
1748 InsetText::moveUp(BufferView * bv)
1750 if (!crow(bv)->previous())
1752 getLyXText(bv)->cursorUp(bv);
1753 return DISPATCHED_NOUPDATE;
1757 UpdatableInset::RESULT
1758 InsetText::moveDown(BufferView * bv)
1760 if (!crow(bv)->next())
1761 return FINISHED_DOWN;
1762 getLyXText(bv)->cursorDown(bv);
1763 return DISPATCHED_NOUPDATE;
1767 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1769 if (the_locking_inset) {
1770 if (the_locking_inset->insetAllowed(inset))
1771 return the_locking_inset->insertInset(bv, inset);
1774 inset->setOwner(this);
1775 hideInsetCursor(bv);
1779 lt = getLyXText(bv);
1782 lt->insertInset(bv, inset);
1784 if ((!cpar(bv)->isInset(cpos(bv))) ||
1785 (cpar(bv)->getInset(cpos(bv)) != inset))
1789 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1790 // showInsetCursor(bv);
1797 bool InsetText::insetAllowed(Inset::Code code) const
1799 // in_insetAllowed is a really gross hack,
1800 // to allow us to call the owner's insetAllowed
1801 // without stack overflow, which can happen
1802 // when the owner uses InsetCollapsable::insetAllowed()
1804 if (in_insetAllowed)
1806 in_insetAllowed = true;
1807 if (the_locking_inset)
1808 ret = the_locking_inset->insetAllowed(code);
1810 ret = owner()->insetAllowed(code);
1811 in_insetAllowed = false;
1816 UpdatableInset * InsetText::getLockingInset() const
1818 return the_locking_inset ? the_locking_inset->getLockingInset() :
1819 const_cast<InsetText *>(this);
1823 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1827 if (the_locking_inset)
1828 return the_locking_inset->getFirstLockingInsetOfType(c);
1833 bool InsetText::showInsetDialog(BufferView * bv) const
1835 if (the_locking_inset)
1836 return the_locking_inset->showInsetDialog(bv);
1841 std::vector<string> const InsetText::getLabelList() const
1843 std::vector<string> label_list;
1845 Paragraph * tpar = par;
1847 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1848 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1849 for (; beg != end; ++beg) {
1850 std::vector<string> const l = (*beg)->getLabelList();
1851 label_list.insert(label_list.end(), l.begin(), l.end());
1853 tpar = tpar->next();
1859 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1862 if (the_locking_inset) {
1863 the_locking_inset->setFont(bv, font, toggleall, selectall);
1866 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1867 getLyXText(bv)->setFont(bv, font, toggleall);
1872 lt = getLyXText(bv);
1875 if (lt->selection.set()) {
1876 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1880 lt->toggleFree(bv, font, toggleall);
1882 lt->clearSelection();
1884 if (selectall || lt->selection.set())
1885 updateLocal(bv, FULL, true);
1887 updateLocal(bv, CURSOR_PAR, true);
1893 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1895 if (cpar(bv)->isInset(cpos(bv))) {
1899 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1900 if (!isHighlyEditableInset(inset))
1902 LyXFont const font =
1903 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1905 x = inset->width(bv, font);
1906 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1909 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1911 //inset_x = cx(bv) - top_x + drawTextXOffset;
1912 //inset_y = cy(bv) + drawTextYOffset;
1913 inset->edit(bv, x, y, 0);
1914 if (!the_locking_inset)
1916 updateLocal(bv, CURSOR, false);
1923 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1926 x -= drawTextXOffset;
1928 int dummyy = y + insetAscent;
1929 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1936 inset_x = cx(bv) - top_x + drawTextXOffset;
1937 inset_y = cy(bv) + drawTextYOffset;
1938 inset->edit(bv, x - inset_x, y - inset_y, button);
1939 if (!the_locking_inset)
1941 updateLocal(bv, CURSOR, false);
1948 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1951 int w = UpdatableInset::getMaxWidth(bv, inset);
1956 w = w - top_x + owner()->x();
1959 w -= (2 * TEXT_TO_INSET_OFFSET);
1962 return UpdatableInset::getMaxWidth(bv, inset);
1967 void InsetText::setParagraphData(Paragraph * p, bool same_id)
1969 // we have to unlock any locked inset otherwise we're in troubles
1970 the_locking_inset = 0;
1972 Paragraph * tmp = par->next();
1977 par = new Paragraph(*p, same_id);
1978 par->setInsetOwner(this);
1979 Paragraph * np = par;
1982 np->next(new Paragraph(*p, same_id));
1983 np->next()->previous(np);
1985 np->setInsetOwner(this);
1992 void InsetText::setText(string const & data)
1995 LyXFont font(LyXFont::ALL_SANE);
1996 for (unsigned int i=0; i < data.length(); ++i)
1997 par->insertChar(i, data[i], font);
2001 void InsetText::setAutoBreakRows(bool flag)
2003 if (flag != autoBreakRows) {
2004 autoBreakRows = flag;
2012 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2014 if (how != drawFrame_) {
2017 updateLocal(bv, DRAW_FRAME, false);
2022 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2024 if (frame_color != col) {
2027 updateLocal(bv, DRAW_FRAME, false);
2032 int InsetText::cx(BufferView * bv) const
2036 lt = getLyXText(bv);
2039 int x = lt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2040 if (the_locking_inset) {
2041 LyXFont font = lt->getFont(bv->buffer(),
2044 if (font.isVisibleRightToLeft())
2045 x -= the_locking_inset->width(bv, font);
2053 int InsetText::cy(BufferView * bv) const
2056 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2060 pos_type InsetText::cpos(BufferView * bv) const
2062 return getLyXText(bv)->cursor.pos();
2066 Paragraph * InsetText::cpar(BufferView * bv) const
2068 return getLyXText(bv)->cursor.par();
2072 bool InsetText::cboundary(BufferView * bv) const
2074 return getLyXText(bv)->cursor.boundary();
2078 Row * InsetText::crow(BufferView * bv) const
2080 return getLyXText(bv)->cursor.row();
2084 LyXText * InsetText::getLyXText(BufferView const * lbv,
2085 bool const recursive) const
2087 if (!recursive && (cached_bview == lbv)) {
2088 LyXText * lt = cached_text.get();
2089 lyx::Assert(lt && lt->firstRow()->par() == par);
2090 return cached_text.get();
2093 // Super UGLY! (Lgb)
2094 BufferView * bv = const_cast<BufferView *>(lbv);
2097 Cache::iterator it = cache.find(bv);
2099 if (it != cache.end()) {
2103 resizeLyXText(do_resize);
2104 if (lt || !it->second.remove) {
2105 lyx::Assert(it->second.text.get());
2106 cached_text = it->second.text;
2107 if (recursive && the_locking_inset) {
2108 return the_locking_inset->getLyXText(bv, true);
2110 return cached_text.get();
2111 } else if (it->second.remove) {
2113 saveLyXTextState(it->second.text.get());
2120 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2121 cached_text->init(bv);
2122 restoreLyXTextState(bv, cached_text.get());
2124 cache.insert(make_pair(bv, cached_text));
2126 if (the_locking_inset && recursive) {
2127 return the_locking_inset->getLyXText(bv);
2129 return cached_text.get();
2133 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2137 Cache::iterator it = cache.find(bv);
2139 if (it == cache.end()) {
2143 lyx::Assert(it->second.text.get());
2145 it->second.remove = true;
2147 /// then remove all LyXText in text-insets
2148 Paragraph * p = par;
2149 for (; p; p = p->next()) {
2150 p->deleteInsetsLyXText(bv);
2156 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2159 // we cannot resize this because we are in use!
2160 // so do this on the next possible getLyXText()
2165 // lyxerr << "InsetText::resizeLyXText\n";
2166 if (!par->next() && !par->size()) // no data, resize not neccessary!
2168 // one endless line, resize normally not necessary
2169 if (!force && getMaxWidth(bv, this) < 0)
2172 Cache::iterator it = cache.find(bv);
2173 if (it == cache.end()) {
2176 lyx::Assert(it->second.text.get());
2178 LyXText * t = it->second.text.get();
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;
2191 t->first = bv->screen()->topCursorVisible(t);
2194 updateLocal(bv, FULL, false);
2195 // this will scroll the screen such that the cursor becomes visible
2196 bv->updateScrollbar();
2198 need_update |= FULL;
2203 void InsetText::reinitLyXText() const
2206 // we cannot resize this because we are in use!
2207 // so do this on the next possible getLyXText()
2213 // lyxerr << "InsetText::reinitLyXText\n";
2214 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2215 lyx::Assert(it->second.text.get());
2217 LyXText * t = it->second.text.get();
2218 BufferView * bv = it->first;
2220 saveLyXTextState(t);
2221 for (Paragraph * p = par; p; p = p->next()) {
2222 p->resizeInsetsLyXText(bv);
2225 restoreLyXTextState(bv, t);
2226 if (the_locking_inset) {
2227 inset_x = cx(bv) - top_x + drawTextXOffset;
2228 inset_y = cy(bv) + drawTextYOffset;
2231 t->first = bv->screen()->topCursorVisible(t);
2234 updateLocal(bv, FULL, false);
2235 // this will scroll the screen such that the cursor becomes visible
2236 bv->updateScrollbar();
2244 void InsetText::removeNewlines()
2246 bool changed = false;
2248 for (Paragraph * p = par; p; p = p->next()) {
2249 for (int i = 0; i < p->size(); ++i) {
2250 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2261 bool InsetText::nodraw() const
2263 if (the_locking_inset)
2264 return the_locking_inset->nodraw();
2265 return UpdatableInset::nodraw();
2269 int InsetText::scroll(bool recursive) const
2271 int sx = UpdatableInset::scroll(false);
2273 if (recursive && the_locking_inset)
2274 sx += the_locking_inset->scroll(recursive);
2280 bool InsetText::doClearArea() const
2282 return !locked || (need_update & (FULL|INIT));
2286 void InsetText::selectAll(BufferView * bv)
2288 getLyXText(bv)->cursorTop(bv);
2289 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2290 getLyXText(bv)->cursorBottom(bv);
2291 getLyXText(bv)->setSelection(bv);
2295 void InsetText::clearSelection(BufferView * bv)
2297 getLyXText(bv)->clearSelection();
2301 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2304 Painter & pain = bv->painter();
2306 int h = ascent(bv, dummy) + descent(bv, dummy);
2307 int ty = baseline - insetAscent;
2313 if ((ty + h) > pain.paperHeight())
2314 h = pain.paperHeight();
2315 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2316 w = pain.paperWidth();
2317 // w -= TEXT_TO_INSET_OFFSET;
2318 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2321 frame_is_visible = false;
2325 Paragraph * InsetText::getParFromID(int id) const
2328 Paragraph * result = par;
2329 Paragraph * ires = 0;
2330 while (result && result->id() != id) {
2331 if ((ires = result->getParFromID(id)))
2333 result = result->next();
2337 Paragraph * tmp = par;
2339 if (tmp->id() == id) {
2342 Paragraph * tmp2 = tmp->getParFromID(id);
2353 Paragraph * InsetText::firstParagraph() const
2356 if (the_locking_inset)
2357 if ((result = the_locking_inset->firstParagraph()))
2363 Paragraph * InsetText::getFirstParagraph(int i) const
2365 return (i == 0) ? par : 0;
2369 LyXCursor const & InsetText::cursor(BufferView * bv) const
2371 if (the_locking_inset)
2372 return the_locking_inset->cursor(bv);
2373 return getLyXText(bv)->cursor;
2377 Paragraph * InsetText::paragraph() const
2383 void InsetText::paragraph(Paragraph * p)
2385 // first we have to delete the old contents otherwise we'll have a
2386 // memory leak! But there check that we don't delete the paragraphs
2387 // the new par is refering to (could happen in LyXText::EmptyParagrapM...)
2388 // well don't do deletes at all if we come from there. Really stupid
2389 // thing! (Jug 20011205)
2390 if (par->next() != p) {
2391 the_locking_inset = 0;
2392 while (par && par != p) {
2393 Paragraph * tmp = par->next();
2399 // set ourself as owner for all the paragraphs inserted!
2400 Paragraph * np = par;
2402 np->setInsetOwner(this);
2406 // redraw myself when asked for
2411 Inset * InsetText::getInsetFromID(int id_arg) const
2414 return const_cast<InsetText *>(this);
2416 Paragraph * lp = par;
2419 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2420 en = lp->inset_iterator_end();
2423 if ((*it)->id() == id_arg)
2425 Inset * in = (*it)->getInsetFromID(id_arg);
2435 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2441 lt = getLyXText(bv);
2444 if (the_locking_inset) {
2445 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2452 #warning Dekel please have a look on this one RTL? (Jug)
2454 // we have to go on checking so move cusor to the right
2455 lt->cursor.pos(lt->cursor.pos() + 1);
2457 str = lt->selectNextWordToSpellcheck(bv, value);
2459 bv->unlockInset(const_cast<InsetText *>(this));
2468 void InsetText::selectSelectedWord(BufferView * bv)
2470 if (the_locking_inset) {
2471 the_locking_inset->selectSelectedWord(bv);
2474 getLyXText(bv)->selectSelectedWord(bv);
2475 updateLocal(bv, SELECTION, false);
2479 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2481 if (the_locking_inset) {
2482 the_locking_inset->toggleSelection(bv, kill_selection);
2486 lt = getLyXText(bv);
2490 int x = top_x + TEXT_TO_INSET_OFFSET;
2492 Row * row = lt->firstRow();
2493 int y_offset = top_baseline - row->ascent_of_text();
2495 while ((row != 0) && ((y+row->height()) <= 0)) {
2502 if (need_update & SELECTION)
2504 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2510 bool InsetText::searchForward(BufferView * bv, string const & str,
2511 bool const & cs, bool const & mw)
2513 if (the_locking_inset) {
2514 if (the_locking_inset->searchForward(bv, str, cs, mw))
2518 lt = getLyXText(bv);
2521 Paragraph * lpar = lt->cursor.par();
2522 pos_type pos = lt->cursor.pos();
2523 if (pos < lpar->size() - 1)
2527 lpar = lpar->next();
2532 // we have to unlock ourself in this function by default!
2533 bv->unlockInset(const_cast<InsetText *>(this));
2536 lt->setCursor(bv, lpar, pos);
2540 if (LyXFind(bv, str, true, true, cs , mw)) {
2543 // we have to unlock ourself in this function by default!
2544 bv->unlockInset(const_cast<InsetText *>(this));
2548 bool InsetText::searchBackward(BufferView * bv, string const & str,
2549 bool const & cs, bool const & mw)
2551 if (the_locking_inset)
2552 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2554 if (LyXFind(bv, str, false, true, cs, mw)) {
2557 // we have to unlock ourself in this function by default!
2558 bv->unlockInset(const_cast<InsetText *>(this));
2563 bool InsetText::checkInsertChar(LyXFont & font)
2566 return owner()->checkInsertChar(font);