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;
199 in_insetAllowed = false;
203 InsetText::~InsetText()
210 Paragraph * tmp = par->next();
217 void InsetText::clear()
220 Paragraph * tmp = par->next();
230 Inset * InsetText::clone(Buffer const &, bool same_id) const
232 return new InsetText(*this, same_id);
236 void InsetText::write(Buffer const * buf, ostream & os) const
239 writeParagraphData(buf, os);
243 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
245 par->writeFile(buf, os, buf->params, 0);
249 void InsetText::read(Buffer const * buf, LyXLex & lex)
253 Paragraph * return_par = 0;
254 Paragraph::depth_type depth = 0;
255 LyXFont font(LyXFont::ALL_INHERIT);
261 token = lex.getString();
264 if (token == "\\end_inset") {
265 #ifndef NO_COMPABILITY
266 const_cast<Buffer*>(buf)->insertErtContents(par, pos, font, false);
271 if (const_cast<Buffer*>(buf)->
272 parseSingleLyXformat2Token(lex, par, return_par,
273 token, pos, depth, font)) {
274 // the_end read this should NEVER happen
275 lex.printError("\\the_end read in inset! Error in document!");
283 return_par->setInsetOwner(this);
284 return_par = return_par->next();
287 if (token != "\\end_inset") {
288 lex.printError("Missing \\end_inset at this point. "
295 int InsetText::ascent(BufferView * bv, LyXFont const &) const
302 insetAscent = lt->firstRow()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
309 int InsetText::descent(BufferView * bv, LyXFont const &) const
316 insetDescent = lt->height - lt->firstRow()->ascent_of_text() +
317 TEXT_TO_INSET_OFFSET;
324 int InsetText::width(BufferView * bv, LyXFont const &) const
326 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
327 (2 * TEXT_TO_INSET_OFFSET);
328 insetWidth = max(insetWidth, 10);
333 int InsetText::textWidth(BufferView * bv, bool fordraw) const
336 if (!autoBreakRows) {
339 w = getMaxWidth(bv, this);
342 return max(w - (2 * TEXT_TO_INSET_OFFSET),
343 (int)getLyXText(bv)->width);
347 return w - (2 * TEXT_TO_INSET_OFFSET);
351 void InsetText::draw(BufferView * bv, LyXFont const & f,
352 int baseline, float & x, bool cleared) const
357 Painter & pain = bv->painter();
359 // this is the first thing we have to ask because if the x pos
360 // changed we have to do a complete rebreak of the text as we
361 // may have few space to draw in. Well we should check on this too
363 if (top_x != int(x)) {
365 int nw = getMaxWidth(bv, this);
366 if (nw > 0 && old_max_width != nw) {
369 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
377 // repaint the background if needed
378 if (cleared && backgroundColor() != LColor::background) {
380 clearInset(bv, baseline, cleared);
384 // no draw is necessary !!!
385 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
388 top_baseline = baseline;
390 if (need_update & CLEAR_FRAME)
391 clearFrame(pain, cleared);
398 x += static_cast<float>(scroll());
400 // if top_x differs we did it already
401 if (!cleared && (top_x == int(x)) &&
402 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
403 (last_drawn_width!=insetWidth))) {
404 clearInset(bv, baseline, cleared);
410 frame_is_visible = false;
412 if (!cleared && (need_update == NONE)) {
414 drawFrame(pain, cleared);
418 top_baseline = baseline;
419 top_y = baseline - ascent(bv, f);
420 last_width = width(bv, f);
421 last_height = ascent(bv, f) + descent(bv, f);
423 if (last_drawn_width != insetWidth) {
425 clearInset(bv, baseline, cleared);
427 last_drawn_width = insetWidth;
430 if (the_locking_inset && (cpar(bv) == inset_par)
431 && (cpos(bv) == inset_pos)) {
432 inset_x = cx(bv) - top_x + drawTextXOffset;
433 inset_y = cy(bv) + drawTextYOffset;
435 if (!cleared && (need_update == CURSOR)
436 && !getLyXText(bv)->selection.set()) {
437 drawFrame(pain, cleared);
438 x += last_width; // was width(bv, f);
447 x += TEXT_TO_INSET_OFFSET;
449 Row * row = lt->firstRow();
450 int y_offset = baseline - row->ascent_of_text();
451 int ph = pain.paperHeight();
454 while ((row != 0) && ((y+row->height()) <= 0)) {
456 first += row->height();
462 if (cleared || (need_update&(INIT|FULL))) {
465 while ((row != 0) && (yf < ph)) {
466 lt->getVisibleRow(bv, y+y_offset, int(x), row,
472 } else if (!locked) {
473 if (need_update & CURSOR) {
474 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
475 lt->clearSelection();
476 lt->selection.cursor = lt->cursor;
478 bv->screen()->update(lt, bv, y_offset, int(x));
481 if (need_update & SELECTION) {
482 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
483 } else if (need_update & CURSOR) {
484 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
485 lt->clearSelection();
486 lt->selection.cursor = lt->cursor;
488 bv->screen()->update(lt, bv, y_offset, int(x));
493 lt->status(bv, LyXText::UNCHANGED);
494 if ((need_update != CURSOR_PAR) &&
495 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked))) {
496 drawFrame(pain, cleared);
497 } else if (need_update & CLEAR_FRAME) {
498 clearFrame(pain, cleared);
501 x += last_width /* was width(bv, f) */ - TEXT_TO_INSET_OFFSET;
503 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
505 } else if (need_update != INIT) {
514 void InsetText::drawFrame(Painter & pain, bool cleared) const
516 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
517 if (!frame_is_visible || cleared) {
518 frame_x = top_x + ttoD2;
519 frame_y = top_baseline - insetAscent + ttoD2;
520 frame_w = last_width - TEXT_TO_INSET_OFFSET;
521 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
522 pain.rectangle(frame_x, frame_y,
525 frame_is_visible = true;
530 void InsetText::clearFrame(Painter & pain, bool cleared) const
532 if (frame_is_visible) {
534 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
537 frame_is_visible = false;
542 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
545 if (reinit && owner()) {
547 owner()->update(bv, font, true);
552 if (reinit || need_update == INIT) {
554 // we should put this call where we set need_update to INIT!
557 owner()->update(bv, font, true);
561 if (the_locking_inset) {
562 inset_x = cx(bv) - top_x + drawTextXOffset;
563 inset_y = cy(bv) + drawTextYOffset;
564 the_locking_inset->update(bv, font, reinit);
573 int oldw = insetWidth;
574 insetWidth = lt->width + (2 * TEXT_TO_INSET_OFFSET);
575 if (oldw != insetWidth) {
584 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
587 lt->updateInset(bv, the_locking_inset);
589 if (lt->status() == LyXText::NEED_MORE_REFRESH)
597 void InsetText::setUpdateStatus(BufferView * bv, int what) const
605 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
606 // if we don't break row so that we only have one row to update!
607 if ((lt->status() == LyXText::NEED_MORE_REFRESH) ||
609 (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
612 } else if (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
613 need_update |= CURSOR_PAR;
616 // this to not draw a selection when we redraw all of it!
617 if (need_update & CURSOR && !(need_update & SELECTION)) {
618 if (lt->selection.set())
620 lt->clearSelection();
627 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
635 setUpdateStatus(bv, what);
636 if (((need_update != CURSOR) && (need_update != NONE)) ||
637 (lt->status() != LyXText::UNCHANGED) || lt->selection.set())
639 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
641 if (need_update == CURSOR)
643 bv->owner()->showState();
644 if (old_par != cpar(bv)) {
645 bv->owner()->setLayout(cpar(bv)->getLayout());
653 string const InsetText::editMessage() const
655 return _("Opened Text Inset");
659 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
661 UpdatableInset::edit(bv, x, y, button);
663 if (!bv->lockInset(this)) {
664 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
668 the_locking_inset = 0;
669 inset_pos = inset_x = inset_y = 0;
670 inset_boundary = false;
673 int tmp_y = (y < 0) ? 0 : y;
680 if (!checkAndActivateInset(bv, x, tmp_y, button))
681 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
683 lt->clearSelection();
686 updateLocal(bv, CURSOR, false);
688 // If the inset is empty set the language of the current font to the
689 // language to the surronding text (if different).
690 if (par->size() == 0 && !par->next() &&
691 bv->getParentLanguage(this) != lt->current_font.language()) {
692 LyXFont font(LyXFont::ALL_IGNORE);
693 font.setLanguage(bv->getParentLanguage(this));
694 setFont(bv, font, false);
701 void InsetText::edit(BufferView * bv, bool front)
703 UpdatableInset::edit(bv, front);
705 if (!bv->lockInset(this)) {
706 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
710 the_locking_inset = 0;
711 inset_pos = inset_x = inset_y = 0;
712 inset_boundary = false;
721 lt->setCursor(bv, par, 0);
726 // int const pos = (p->size() ? p->size()-1 : p->size());
727 lt->setCursor(bv, p, p->size());
729 lt->clearSelection();
732 updateLocal(bv, CURSOR, false);
734 // If the inset is empty set the language of the current font to the
735 // language to the surronding text (if different).
736 if (par->size() == 0 && !par->next() &&
737 bv->getParentLanguage(this) != lt->current_font.language()) {
738 LyXFont font(LyXFont::ALL_IGNORE);
739 font.setLanguage(bv->getParentLanguage(this));
740 setFont(bv, font, false);
747 void InsetText::insetUnlock(BufferView * bv)
749 if (the_locking_inset) {
750 the_locking_inset->insetUnlock(bv);
751 the_locking_inset = 0;
754 no_selection = false;
757 if (drawFrame_ == LOCKED)
758 code = CURSOR|CLEAR_FRAME;
766 if (lt->selection.set()) {
767 lt->clearSelection();
769 } else if (owner()) {
770 bv->owner()->setLayout(owner()->getLyXText(bv)
771 ->cursor.par()->getLayout());
773 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
774 // hack for deleteEmptyParMech
775 lt->setCursor(bv, par, 0);
776 updateLocal(bv, code, false);
782 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
784 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
788 if (inset == cpar(bv)->getInset(cpos(bv))) {
789 lyxerr[Debug::INSETS] << "OK" << endl;
790 the_locking_inset = inset;
791 inset_x = cx(bv) - top_x + drawTextXOffset;
792 inset_y = cy(bv) + drawTextYOffset;
793 inset_pos = cpos(bv);
794 inset_par = cpar(bv);
795 inset_boundary = cboundary(bv);
796 updateLocal(bv, CURSOR, false);
798 } else if (the_locking_inset && (the_locking_inset == inset)) {
799 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
800 lyxerr[Debug::INSETS] << "OK" << endl;
801 inset_x = cx(bv) - top_x + drawTextXOffset;
802 inset_y = cy(bv) + drawTextYOffset;
804 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
806 } else if (the_locking_inset) {
807 lyxerr[Debug::INSETS] << "MAYBE" << endl;
808 return the_locking_inset->lockInsetInInset(bv, inset);
810 lyxerr[Debug::INSETS] << "NOT OK" << endl;
815 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
818 if (!the_locking_inset)
820 if (the_locking_inset == inset) {
821 the_locking_inset->insetUnlock(bv);
822 getLyXText(bv)->updateInset(bv, inset);
823 the_locking_inset = 0;
825 moveRight(bv, false);
826 old_par = 0; // force layout setting
830 updateLocal(bv, CURSOR, false);
833 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
837 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
844 if (!the_locking_inset) {
845 bool found = lt->updateInset(bv, inset);
849 setUpdateStatus(bv, NONE);
852 if (the_locking_inset != inset) {
854 lt->updateInset(bv, the_locking_inset);
855 setUpdateStatus(bv, CURSOR_PAR);
857 bool found = the_locking_inset->updateInsetInInset(bv, inset);
861 setUpdateStatus(bv, CURSOR_PAR);
864 bool found = lt->updateInset(bv, inset);
868 setUpdateStatus(bv, CURSOR_PAR);
870 updateLocal(bv, CURSOR_PAR, false);
872 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
873 inset_x = cx(bv) - top_x + drawTextXOffset;
874 inset_y = cy(bv) + drawTextYOffset;
881 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
885 int tmp_x = x - drawTextXOffset;
886 int tmp_y = y + insetAscent - getLyXText(bv)->first;
887 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
890 if (the_locking_inset) {
891 if (the_locking_inset == inset) {
892 the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
893 no_selection = false;
896 // otherwise unlock the_locking_inset and lock the new inset
897 the_locking_inset->insetUnlock(bv);
898 inset_x = cx(bv) - top_x + drawTextXOffset;
899 inset_y = cy(bv) + drawTextYOffset;
900 the_locking_inset = static_cast<UpdatableInset*>(inset);
901 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
902 inset->edit(bv, x - inset_x, y - inset_y, button);
903 if (the_locking_inset)
904 updateLocal(bv, CURSOR, false);
905 no_selection = false;
908 // otherwise only unlock the_locking_inset
909 the_locking_inset->insetUnlock(bv);
910 the_locking_inset = 0;
912 if (bv->theLockingInset()) {
913 if (isHighlyEditableInset(inset)) {
914 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
915 inset_x = cx(bv) - top_x + drawTextXOffset;
916 inset_y = cy(bv) + drawTextYOffset;
917 inset_pos = cpos(bv);
918 inset_par = cpar(bv);
919 inset_boundary = cboundary(bv);
920 the_locking_inset = uinset;
921 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
923 uinset->edit(bv, x - inset_x, y - inset_y, 0);
924 if (the_locking_inset)
925 updateLocal(bv, CURSOR, false);
926 no_selection = false;
930 if (!inset) { // && (button == 2)) {
931 bool paste_internally = false;
932 if ((button == 2) && getLyXText(bv)->selection.set()) {
933 localDispatch(bv, LFUN_COPY, "");
934 paste_internally = true;
941 lt->setCursorFromCoordinates(bv, x-drawTextXOffset, y + insetAscent);
942 if (lt->selection.set()) {
943 lt->clearSelection();
944 updateLocal(bv, FULL, false);
946 lt->clearSelection();
948 bv->owner()->setLayout(cpar(bv)->getLayout());
950 // Insert primary selection with middle mouse
951 // if there is a local selection in the current buffer,
954 if (paste_internally)
955 localDispatch(bv, LFUN_PASTE, "");
957 localDispatch(bv, LFUN_PASTESELECTION,
964 no_selection = false;
968 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
970 UpdatableInset * inset = 0;
972 if (the_locking_inset) {
973 the_locking_inset->insetButtonRelease(bv,
974 x - inset_x, y - inset_y,
977 if (cpar(bv)->isInset(cpos(bv))) {
978 inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
979 if (isHighlyEditableInset(inset)) {
980 inset->insetButtonRelease(bv,
982 y - inset_y, button);
984 inset_x = cx(bv) - top_x + drawTextXOffset;
985 inset_y = cy(bv) + drawTextYOffset;
986 inset->insetButtonRelease(bv,
988 y - inset_y, button);
990 x - inset_x, y - inset_y, button);
992 updateLocal(bv, CURSOR_PAR, false);
995 no_selection = false;
999 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1003 if (the_locking_inset) {
1004 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1010 lt = getLyXText(bv);
1013 hideInsetCursor(bv);
1014 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1015 lt->setSelection(bv);
1016 if (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1017 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos())
1019 updateLocal(bv, SELECTION, false);
1021 showInsetCursor(bv);
1027 void InsetText::insetKeyPress(XKeyEvent * xke)
1029 if (the_locking_inset) {
1030 the_locking_inset->insetKeyPress(xke);
1036 UpdatableInset::RESULT
1037 InsetText::localDispatch(BufferView * bv,
1038 kb_action action, string const & arg)
1040 bool was_empty = par->size() == 0 && !par->next();
1041 no_selection = false;
1042 UpdatableInset::RESULT
1043 result= UpdatableInset::localDispatch(bv, action, arg);
1044 if (result != UNDISPATCHED) {
1048 result = DISPATCHED;
1049 if ((action < 0) && arg.empty())
1052 if (the_locking_inset) {
1053 result = the_locking_inset->localDispatch(bv, action, arg);
1054 if (result == DISPATCHED_NOUPDATE)
1056 else if (result == DISPATCHED) {
1057 updateLocal(bv, CURSOR_PAR, false);
1059 } else if (result >= FINISHED) {
1061 case FINISHED_RIGHT:
1062 moveRightIntern(bv, false, false);
1063 result = DISPATCHED;
1066 if ((result = moveUp(bv)) >= FINISHED) {
1067 updateLocal(bv, CURSOR, false);
1068 bv->unlockInset(this);
1072 if ((result = moveDown(bv)) >= FINISHED) {
1073 updateLocal(bv, CURSOR, false);
1074 bv->unlockInset(this);
1078 result = DISPATCHED;
1081 the_locking_inset = 0;
1082 #ifdef WITH_WARNINGS
1083 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1088 hideInsetCursor(bv);
1091 lt = getLyXText(bv);
1096 case LFUN_SELFINSERT:
1097 if (bv->buffer()->isReadonly()) {
1098 // setErrorMessage(N_("Document is read only"));
1102 /* Automatically delete the currently selected
1103 * text and replace it with what is being
1104 * typed in now. Depends on lyxrc settings
1105 * "auto_region_delete", which defaults to
1108 setUndo(bv, Undo::INSERT,
1109 lt->cursor.par(), lt->cursor.par()->next());
1111 if (lyxrc.auto_region_delete) {
1112 if (lt->selection.set()) {
1113 lt->cutSelection(bv, false);
1116 lt->clearSelection();
1117 for (string::size_type i = 0; i < arg.length(); ++i) {
1118 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1121 lt->selection.cursor = lt->cursor;
1122 updateLocal(bv, CURSOR_PAR, true);
1123 result = DISPATCHED_NOUPDATE;
1125 // --- Cursor Movements -----------------------------------
1128 moveRight(bv, false, true);
1129 lt->setSelection(bv);
1130 updateLocal(bv, SELECTION, false);
1133 result = moveRight(bv);
1135 updateLocal(bv, CURSOR, false);
1139 moveLeft(bv, false, true);
1140 lt->setSelection(bv);
1141 updateLocal(bv, SELECTION, false);
1145 result = moveLeft(bv);
1146 updateLocal(bv, CURSOR, false);
1151 lt->setSelection(bv);
1152 updateLocal(bv, SELECTION, false);
1156 result = moveDown(bv);
1157 updateLocal(bv, CURSOR, false);
1162 lt->setSelection(bv);
1163 updateLocal(bv, SELECTION, false);
1167 result = moveUp(bv);
1168 updateLocal(bv, CURSOR, false);
1173 updateLocal(bv, CURSOR, false);
1177 updateLocal(bv, CURSOR, false);
1179 case LFUN_BACKSPACE: {
1180 setUndo(bv, Undo::DELETE,
1181 lt->cursor.par(), lt->cursor.par()->next());
1182 if (lt->selection.set())
1183 lt->cutSelection(bv);
1186 updateLocal(bv, CURSOR_PAR, true);
1191 setUndo(bv, Undo::DELETE,
1192 lt->cursor.par(), lt->cursor.par()->next());
1193 if (lt->selection.set()) {
1194 lt->cutSelection(bv);
1198 updateLocal(bv, CURSOR_PAR, true);
1203 setUndo(bv, Undo::DELETE,
1204 lt->cursor.par(), lt->cursor.par()->next());
1205 lt->cutSelection(bv);
1206 updateLocal(bv, CURSOR_PAR, true);
1212 lt->copySelection(bv);
1213 updateLocal(bv, CURSOR_PAR, false);
1215 case LFUN_PASTESELECTION:
1217 string const clip(bv->getClipboard());
1221 if (arg == "paragraph") {
1222 lt->insertStringAsParagraphs(bv, clip);
1224 lt->insertStringAsLines(bv, clip);
1226 updateLocal(bv, CURSOR_PAR, true);
1230 if (!autoBreakRows) {
1232 if (CutAndPaste::nrOfParagraphs() > 1) {
1233 Alert::alert(_("Impossible operation"),
1234 _("Cannot include more than one paragraph!"),
1239 setUndo(bv, Undo::INSERT,
1240 lt->cursor.par(), lt->cursor.par()->next());
1241 lt->pasteSelection(bv);
1242 updateLocal(bv, CURSOR_PAR, true);
1246 case LFUN_BREAKPARAGRAPH:
1247 if (!autoBreakRows) {
1248 result = DISPATCHED;
1251 lt->breakParagraph(bv, 0);
1252 updateLocal(bv, FULL, true);
1254 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1255 if (!autoBreakRows) {
1256 result = DISPATCHED;
1259 lt->breakParagraph(bv, 1);
1260 updateLocal(bv, FULL, true);
1263 case LFUN_BREAKLINE: {
1264 if (!autoBreakRows) {
1265 result = DISPATCHED;
1268 setUndo(bv, Undo::INSERT,
1269 lt->cursor.par(), lt->cursor.par()->next());
1270 lt->insertChar(bv, Paragraph::META_NEWLINE);
1271 updateLocal(bv, CURSOR_PAR, true);
1276 // do not set layouts on non breakable textinsets
1277 if (autoBreakRows) {
1278 layout_type cur_layout = cpar(bv)->layout;
1280 // Derive layout number from given argument (string)
1281 // and current buffer's textclass (number). */
1282 textclass_type tclass = bv->buffer()->params.textclass;
1283 std::pair <bool, layout_type> layout =
1284 textclasslist.NumberOfLayout(tclass, arg);
1286 // If the entry is obsolete, use the new one instead.
1288 string obs = textclasslist.Style(tclass,layout.second).
1291 layout = textclasslist.NumberOfLayout(tclass, obs);
1294 // see if we found the layout number:
1295 if (!layout.first) {
1296 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1297 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1301 if (cur_layout != layout.second) {
1302 cur_layout = layout.second;
1303 lt->setLayout(bv, layout.second);
1304 bv->owner()->setLayout(cpar(bv)->getLayout());
1305 updateLocal(bv, CURSOR_PAR, true);
1308 // reset the layout box
1309 bv->owner()->setLayout(cpar(bv)->getLayout());
1312 case LFUN_PARAGRAPH_SPACING:
1313 // This one is absolutely not working. When fiddling with this
1314 // it also seems to me that the paragraphs inside the insettext
1315 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1317 Paragraph * par = lt->cursor.par();
1318 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1319 float cur_value = 1.0;
1320 if (cur_spacing == Spacing::Other) {
1321 cur_value = par->params().spacing().getValue();
1324 istringstream istr(arg.c_str());
1327 Spacing::Space new_spacing = cur_spacing;
1328 float new_value = cur_value;
1330 lyxerr << "Missing argument to `paragraph-spacing'"
1332 } else if (tmp == "single") {
1333 new_spacing = Spacing::Single;
1334 } else if (tmp == "onehalf") {
1335 new_spacing = Spacing::Onehalf;
1336 } else if (tmp == "double") {
1337 new_spacing = Spacing::Double;
1338 } else if (tmp == "other") {
1339 new_spacing = Spacing::Other;
1342 lyxerr << "new_value = " << tmpval << endl;
1345 } else if (tmp == "default") {
1346 new_spacing = Spacing::Default;
1348 lyxerr << _("Unknown spacing argument: ")
1351 if (cur_spacing != new_spacing || cur_value != new_value) {
1352 par->params().spacing(Spacing(new_spacing, new_value));
1353 updateLocal(bv, CURSOR_PAR, true);
1359 if (!bv->Dispatch(action, arg))
1360 result = UNDISPATCHED;
1364 /// If the action has deleted all text in the inset, we need to change the
1365 // language to the language of the surronding text.
1366 if (!was_empty && par->size() == 0 && !par->next()) {
1367 LyXFont font(LyXFont::ALL_IGNORE);
1368 font.setLanguage(bv->getParentLanguage(this));
1369 setFont(bv, font, false);
1372 if (result < FINISHED) {
1373 showInsetCursor(bv);
1375 bv->unlockInset(this);
1382 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1385 buf->latexParagraphs(os, par, 0, texrow);
1386 return texrow.rows();
1390 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1392 Paragraph * p = par;
1393 unsigned int lines = 0;
1396 string const tmp = buf->asciiParagraph(p, linelen);
1397 lines += countChar(tmp, '\n');
1405 int InsetText::docbook(Buffer const * buf, ostream & os) const
1407 Paragraph * p = par;
1408 unsigned int lines = 0;
1410 vector<string> environment_stack(10);
1411 vector<string> environment_inner(10);
1413 int const command_depth = 0;
1416 Paragraph::depth_type depth = 0; // paragraph depth
1420 int desc_on = 0; // description mode
1422 LyXLayout const & style =
1423 textclasslist.Style(buf->params.textclass,
1426 // environment tag closing
1427 for (; depth > p->params().depth(); --depth) {
1428 if (environment_inner[depth] != "!-- --") {
1429 item_name = "listitem";
1430 buf->sgmlCloseTag(os, command_depth + depth,
1432 if (environment_inner[depth] == "varlistentry")
1433 buf->sgmlCloseTag(os, depth+command_depth,
1434 environment_inner[depth]);
1436 buf->sgmlCloseTag(os, depth + command_depth,
1437 environment_stack[depth]);
1438 environment_stack[depth].erase();
1439 environment_inner[depth].erase();
1442 if (depth == p->params().depth()
1443 && environment_stack[depth] != style.latexname()
1444 && !environment_stack[depth].empty()) {
1445 if (environment_inner[depth] != "!-- --") {
1446 item_name= "listitem";
1447 buf->sgmlCloseTag(os, command_depth+depth,
1449 if (environment_inner[depth] == "varlistentry")
1450 buf->sgmlCloseTag(os,
1451 depth + command_depth,
1452 environment_inner[depth]);
1455 buf->sgmlCloseTag(os, depth + command_depth,
1456 environment_stack[depth]);
1458 environment_stack[depth].erase();
1459 environment_inner[depth].erase();
1462 // Write opening SGML tags.
1463 switch (style.latextype) {
1464 case LATEX_PARAGRAPH:
1465 buf->sgmlOpenTag(os, depth + command_depth,
1470 buf->sgmlError(p, 0,
1471 _("Error : LatexType Command not allowed here.\n"));
1475 case LATEX_ENVIRONMENT:
1476 case LATEX_ITEM_ENVIRONMENT:
1477 if (depth < p->params().depth()) {
1478 depth = p->params().depth();
1479 environment_stack[depth].erase();
1482 if (environment_stack[depth] != style.latexname()) {
1483 if(environment_stack.size() == depth + 1) {
1484 environment_stack.push_back("!-- --");
1485 environment_inner.push_back("!-- --");
1487 environment_stack[depth] = style.latexname();
1488 environment_inner[depth] = "!-- --";
1489 buf->sgmlOpenTag(os, depth + command_depth,
1490 environment_stack[depth]);
1492 if (environment_inner[depth] != "!-- --") {
1493 item_name= "listitem";
1494 buf->sgmlCloseTag(os,
1495 command_depth + depth,
1497 if (environment_inner[depth] == "varlistentry")
1498 buf->sgmlCloseTag(os,
1499 depth + command_depth,
1500 environment_inner[depth]);
1504 if (style.latextype == LATEX_ENVIRONMENT) {
1505 if (!style.latexparam().empty()) {
1506 if(style.latexparam() == "CDATA")
1509 buf->sgmlOpenTag(os, depth + command_depth,
1510 style.latexparam());
1515 desc_on = (style.labeltype == LABEL_MANUAL);
1518 environment_inner[depth]= "varlistentry";
1520 environment_inner[depth]= "listitem";
1522 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1523 environment_inner[depth]);
1527 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1531 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1536 buf->sgmlOpenTag(os, depth + command_depth,
1541 buf->simpleDocBookOnePar(os, p, desc_on,
1542 depth + 1 + command_depth);
1546 // write closing SGML tags
1547 switch (style.latextype) {
1548 case LATEX_ENVIRONMENT:
1549 if (!style.latexparam().empty()) {
1550 if(style.latexparam() == "CDATA")
1553 buf->sgmlCloseTag(os, depth + command_depth,
1554 style.latexparam());
1557 case LATEX_ITEM_ENVIRONMENT:
1558 if (desc_on == 1) break;
1560 buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1562 case LATEX_PARAGRAPH:
1563 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1566 buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1572 for (int d = depth; d >= 0; --d) {
1573 if (!environment_stack[depth].empty()) {
1574 if (environment_inner[depth] != "!-- --") {
1575 item_name = "listitem";
1576 buf->sgmlCloseTag(os, command_depth + depth,
1578 if (environment_inner[depth] == "varlistentry")
1579 buf->sgmlCloseTag(os, depth + command_depth,
1580 environment_inner[depth]);
1583 buf->sgmlCloseTag(os, depth + command_depth,
1584 environment_stack[depth]);
1592 void InsetText::validate(LaTeXFeatures & features) const
1594 Paragraph * p = par;
1596 p->validate(features);
1602 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1604 if (textclasslist.Style(buf->params.textclass,
1605 p->getLayout()).labeltype != LABEL_MANUAL)
1608 return p->beginningOfMainBody();
1612 void InsetText::getCursorPos(BufferView * bv,
1613 int & x, int & y) const
1615 if (the_locking_inset) {
1616 the_locking_inset->getCursorPos(bv, x, y);
1624 unsigned int InsetText::insetInInsetY()
1626 if (!the_locking_inset)
1629 return (inset_y + the_locking_inset->insetInInsetY());
1633 void InsetText::toggleInsetCursor(BufferView * bv)
1635 if (the_locking_inset) {
1636 the_locking_inset->toggleInsetCursor(bv);
1640 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1642 int const asc = lyxfont::maxAscent(font);
1643 int const desc = lyxfont::maxDescent(font);
1645 if (isCursorVisible())
1646 bv->hideLockedInsetCursor();
1648 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1649 toggleCursorVisible();
1653 void InsetText::showInsetCursor(BufferView * bv, bool show)
1655 if (the_locking_inset) {
1656 the_locking_inset->showInsetCursor(bv, show);
1659 if (!isCursorVisible()) {
1660 LyXFont const font =
1661 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1663 int const asc = lyxfont::maxAscent(font);
1664 int const desc = lyxfont::maxDescent(font);
1666 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1668 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1669 setCursorVisible(true);
1674 void InsetText::hideInsetCursor(BufferView * bv)
1676 if (isCursorVisible()) {
1677 bv->hideLockedInsetCursor();
1678 setCursorVisible(false);
1680 if (the_locking_inset)
1681 the_locking_inset->hideInsetCursor(bv);
1685 void InsetText::fitInsetCursor(BufferView * bv) const
1687 if (the_locking_inset) {
1688 the_locking_inset->fitInsetCursor(bv);
1691 LyXFont const font =
1692 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1694 int const asc = lyxfont::maxAscent(font);
1695 int const desc = lyxfont::maxDescent(font);
1697 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1701 UpdatableInset::RESULT
1702 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1704 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1705 return moveLeftIntern(bv, false, activate_inset, selecting);
1707 return moveRightIntern(bv, false, activate_inset, selecting);
1711 UpdatableInset::RESULT
1712 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1714 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1715 return moveRightIntern(bv, true, activate_inset, selecting);
1717 return moveLeftIntern(bv, true, activate_inset, selecting);
1721 UpdatableInset::RESULT
1722 InsetText::moveRightIntern(BufferView * bv, bool behind,
1723 bool activate_inset, bool selecting)
1725 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1726 return FINISHED_RIGHT;
1727 if (activate_inset && checkAndActivateInset(bv, behind))
1729 getLyXText(bv)->cursorRight(bv);
1731 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1732 return DISPATCHED_NOUPDATE;
1736 UpdatableInset::RESULT
1737 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1738 bool activate_inset, bool selecting)
1740 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1742 getLyXText(bv)->cursorLeft(bv);
1744 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1745 if (activate_inset && checkAndActivateInset(bv, behind))
1747 return DISPATCHED_NOUPDATE;
1751 UpdatableInset::RESULT
1752 InsetText::moveUp(BufferView * bv)
1754 if (!crow(bv)->previous())
1756 getLyXText(bv)->cursorUp(bv);
1757 return DISPATCHED_NOUPDATE;
1761 UpdatableInset::RESULT
1762 InsetText::moveDown(BufferView * bv)
1764 if (!crow(bv)->next())
1765 return FINISHED_DOWN;
1766 getLyXText(bv)->cursorDown(bv);
1767 return DISPATCHED_NOUPDATE;
1771 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1773 if (the_locking_inset) {
1774 if (the_locking_inset->insetAllowed(inset))
1775 return the_locking_inset->insertInset(bv, inset);
1778 inset->setOwner(this);
1779 hideInsetCursor(bv);
1783 lt = getLyXText(bv);
1786 lt->insertInset(bv, inset);
1788 if ((!cpar(bv)->isInset(cpos(bv))) ||
1789 (cpar(bv)->getInset(cpos(bv)) != inset))
1793 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1794 // showInsetCursor(bv);
1801 bool InsetText::insetAllowed(Inset::Code code) const
1803 // in_insetAllowed is a really gross hack,
1804 // to allow us to call the owner's insetAllowed
1805 // without stack overflow, which can happen
1806 // when the owner uses InsetCollapsable::insetAllowed()
1808 if (in_insetAllowed)
1810 in_insetAllowed = true;
1811 if (the_locking_inset)
1812 ret = the_locking_inset->insetAllowed(code);
1814 ret = owner()->insetAllowed(code);
1815 in_insetAllowed = false;
1820 UpdatableInset * InsetText::getLockingInset() const
1822 return the_locking_inset ? the_locking_inset->getLockingInset() :
1823 const_cast<InsetText *>(this);
1827 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1831 if (the_locking_inset)
1832 return the_locking_inset->getFirstLockingInsetOfType(c);
1837 bool InsetText::showInsetDialog(BufferView * bv) const
1839 if (the_locking_inset)
1840 return the_locking_inset->showInsetDialog(bv);
1845 std::vector<string> const InsetText::getLabelList() const
1847 std::vector<string> label_list;
1849 Paragraph * tpar = par;
1851 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1852 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1853 for (; beg != end; ++beg) {
1854 std::vector<string> const l = (*beg)->getLabelList();
1855 label_list.insert(label_list.end(), l.begin(), l.end());
1857 tpar = tpar->next();
1863 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1866 if (the_locking_inset) {
1867 the_locking_inset->setFont(bv, font, toggleall, selectall);
1870 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1871 getLyXText(bv)->setFont(bv, font, toggleall);
1876 lt = getLyXText(bv);
1879 if (lt->selection.set()) {
1880 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1884 lt->toggleFree(bv, font, toggleall);
1886 lt->clearSelection();
1888 if (selectall || lt->selection.set())
1889 updateLocal(bv, FULL, true);
1891 updateLocal(bv, CURSOR_PAR, true);
1897 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1899 if (cpar(bv)->isInset(cpos(bv))) {
1903 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1904 if (!isHighlyEditableInset(inset))
1906 LyXFont const font =
1907 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1909 x = inset->width(bv, font);
1910 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1913 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1915 //inset_x = cx(bv) - top_x + drawTextXOffset;
1916 //inset_y = cy(bv) + drawTextYOffset;
1917 inset->edit(bv, x, y, 0);
1918 if (!the_locking_inset)
1920 updateLocal(bv, CURSOR, false);
1927 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1930 x -= drawTextXOffset;
1932 int dummyy = y + insetAscent;
1933 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1940 inset_x = cx(bv) - top_x + drawTextXOffset;
1941 inset_y = cy(bv) + drawTextYOffset;
1942 inset->edit(bv, x - inset_x, y - inset_y, button);
1943 if (!the_locking_inset)
1945 updateLocal(bv, CURSOR, false);
1952 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1955 int w = UpdatableInset::getMaxWidth(bv, inset);
1960 w = w - top_x + owner()->x();
1963 w -= (2 * TEXT_TO_INSET_OFFSET);
1966 return UpdatableInset::getMaxWidth(bv, inset);
1971 void InsetText::setParagraphData(Paragraph * p, bool same_id)
1973 // we have to unlock any locked inset otherwise we're in troubles
1974 the_locking_inset = 0;
1976 Paragraph * tmp = par->next();
1981 par = new Paragraph(*p, same_id);
1982 par->setInsetOwner(this);
1983 Paragraph * np = par;
1986 np->next(new Paragraph(*p, same_id));
1987 np->next()->previous(np);
1989 np->setInsetOwner(this);
1996 void InsetText::setText(string const & data)
1999 LyXFont font(LyXFont::ALL_SANE);
2000 for (unsigned int i=0; i < data.length(); ++i)
2001 par->insertChar(i, data[i], font);
2005 void InsetText::setAutoBreakRows(bool flag)
2007 if (flag != autoBreakRows) {
2008 autoBreakRows = flag;
2016 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2018 if (how != drawFrame_) {
2021 updateLocal(bv, DRAW_FRAME, false);
2026 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2028 if (frame_color != col) {
2031 updateLocal(bv, DRAW_FRAME, false);
2036 int InsetText::cx(BufferView * bv) const
2040 lt = getLyXText(bv);
2043 int x = lt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2044 if (the_locking_inset) {
2045 LyXFont font = lt->getFont(bv->buffer(),
2048 if (font.isVisibleRightToLeft())
2049 x -= the_locking_inset->width(bv, font);
2057 int InsetText::cy(BufferView * bv) const
2060 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2064 pos_type InsetText::cpos(BufferView * bv) const
2066 return getLyXText(bv)->cursor.pos();
2070 Paragraph * InsetText::cpar(BufferView * bv) const
2072 return getLyXText(bv)->cursor.par();
2076 bool InsetText::cboundary(BufferView * bv) const
2078 return getLyXText(bv)->cursor.boundary();
2082 Row * InsetText::crow(BufferView * bv) const
2084 return getLyXText(bv)->cursor.row();
2088 LyXText * InsetText::getLyXText(BufferView const * lbv,
2089 bool const recursive) const
2091 if (!recursive && (cached_bview == lbv)) {
2092 LyXText * lt = cached_text.get();
2093 lyx::Assert(lt && lt->firstRow()->par() == par);
2094 return cached_text.get();
2097 // Super UGLY! (Lgb)
2098 BufferView * bv = const_cast<BufferView *>(lbv);
2101 Cache::iterator it = cache.find(bv);
2103 if (it != cache.end()) {
2107 resizeLyXText(do_resize);
2108 if (lt || !it->second.remove) {
2109 lyx::Assert(it->second.text.get());
2110 cached_text = it->second.text;
2111 if (recursive && the_locking_inset) {
2112 return the_locking_inset->getLyXText(bv, true);
2114 return cached_text.get();
2115 } else if (it->second.remove) {
2117 saveLyXTextState(it->second.text.get());
2124 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2125 cached_text->init(bv);
2126 restoreLyXTextState(bv, cached_text.get());
2128 cache.insert(make_pair(bv, cached_text));
2130 if (the_locking_inset && recursive) {
2131 return the_locking_inset->getLyXText(bv);
2133 return cached_text.get();
2137 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2141 Cache::iterator it = cache.find(bv);
2143 if (it == cache.end()) {
2147 lyx::Assert(it->second.text.get());
2149 it->second.remove = true;
2151 /// then remove all LyXText in text-insets
2152 Paragraph * p = par;
2153 for (; p; p = p->next()) {
2154 p->deleteInsetsLyXText(bv);
2160 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2163 // we cannot resize this because we are in use!
2164 // so do this on the next possible getLyXText()
2169 // lyxerr << "InsetText::resizeLyXText\n";
2170 if (!par->next() && !par->size()) // no data, resize not neccessary!
2172 // one endless line, resize normally not necessary
2173 if (!force && getMaxWidth(bv, this) < 0)
2176 Cache::iterator it = cache.find(bv);
2177 if (it == cache.end()) {
2180 lyx::Assert(it->second.text.get());
2182 LyXText * t = it->second.text.get();
2183 saveLyXTextState(t);
2184 for (Paragraph * p = par; p; p = p->next()) {
2185 p->resizeInsetsLyXText(bv);
2188 restoreLyXTextState(bv, t);
2189 if (the_locking_inset) {
2190 inset_x = cx(bv) - top_x + drawTextXOffset;
2191 inset_y = cy(bv) + drawTextYOffset;
2195 t->first = bv->screen()->topCursorVisible(t);
2198 updateLocal(bv, FULL, false);
2199 // this will scroll the screen such that the cursor becomes visible
2200 bv->updateScrollbar();
2202 need_update |= FULL;
2207 void InsetText::reinitLyXText() const
2210 // we cannot resize this because we are in use!
2211 // so do this on the next possible getLyXText()
2217 // lyxerr << "InsetText::reinitLyXText\n";
2218 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2219 lyx::Assert(it->second.text.get());
2221 LyXText * t = it->second.text.get();
2222 BufferView * bv = it->first;
2224 saveLyXTextState(t);
2225 for (Paragraph * p = par; p; p = p->next()) {
2226 p->resizeInsetsLyXText(bv);
2229 restoreLyXTextState(bv, t);
2230 if (the_locking_inset) {
2231 inset_x = cx(bv) - top_x + drawTextXOffset;
2232 inset_y = cy(bv) + drawTextYOffset;
2235 t->first = bv->screen()->topCursorVisible(t);
2238 updateLocal(bv, FULL, false);
2239 // this will scroll the screen such that the cursor becomes visible
2240 bv->updateScrollbar();
2248 void InsetText::removeNewlines()
2250 bool changed = false;
2252 for (Paragraph * p = par; p; p = p->next()) {
2253 for (int i = 0; i < p->size(); ++i) {
2254 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2265 bool InsetText::nodraw() const
2267 if (the_locking_inset)
2268 return the_locking_inset->nodraw();
2269 return UpdatableInset::nodraw();
2273 int InsetText::scroll(bool recursive) const
2275 int sx = UpdatableInset::scroll(false);
2277 if (recursive && the_locking_inset)
2278 sx += the_locking_inset->scroll(recursive);
2284 bool InsetText::doClearArea() const
2286 return !locked || (need_update & (FULL|INIT));
2290 void InsetText::selectAll(BufferView * bv)
2292 getLyXText(bv)->cursorTop(bv);
2293 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2294 getLyXText(bv)->cursorBottom(bv);
2295 getLyXText(bv)->setSelection(bv);
2299 void InsetText::clearSelection(BufferView * bv)
2301 getLyXText(bv)->clearSelection();
2305 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2308 Painter & pain = bv->painter();
2310 int h = ascent(bv, dummy) + descent(bv, dummy);
2311 int ty = baseline - insetAscent;
2317 if ((ty + h) > pain.paperHeight())
2318 h = pain.paperHeight();
2319 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2320 w = pain.paperWidth();
2321 // w -= TEXT_TO_INSET_OFFSET;
2322 pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2325 frame_is_visible = false;
2329 Paragraph * InsetText::getParFromID(int id) const
2332 Paragraph * result = par;
2333 Paragraph * ires = 0;
2334 while (result && result->id() != id) {
2335 if ((ires = result->getParFromID(id)))
2337 result = result->next();
2341 Paragraph * tmp = par;
2343 if (tmp->id() == id) {
2346 Paragraph * tmp2 = tmp->getParFromID(id);
2357 Paragraph * InsetText::firstParagraph() const
2360 if (the_locking_inset)
2361 if ((result = the_locking_inset->firstParagraph()))
2367 Paragraph * InsetText::getFirstParagraph(int i) const
2369 return (i == 0) ? par : 0;
2373 LyXCursor const & InsetText::cursor(BufferView * bv) const
2375 if (the_locking_inset)
2376 return the_locking_inset->cursor(bv);
2377 return getLyXText(bv)->cursor;
2381 Paragraph * InsetText::paragraph() const
2387 void InsetText::paragraph(Paragraph * p)
2389 // first we have to delete the old contents otherwise we'll have a
2390 // memory leak! But there check that we don't delete the paragraphs
2391 // the new par is refering to (could happen in LyXText::EmptyParagrapM...)
2392 // well don't do deletes at all if we come from there. Really stupid
2393 // thing! (Jug 20011205)
2394 if (par->next() != p) {
2395 the_locking_inset = 0;
2396 while (par && par != p) {
2397 Paragraph * tmp = par->next();
2403 // set ourself as owner for all the paragraphs inserted!
2404 Paragraph * np = par;
2406 np->setInsetOwner(this);
2410 // redraw myself when asked for
2415 Inset * InsetText::getInsetFromID(int id_arg) const
2418 return const_cast<InsetText *>(this);
2420 Paragraph * lp = par;
2423 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2424 en = lp->inset_iterator_end();
2427 if ((*it)->id() == id_arg)
2429 Inset * in = (*it)->getInsetFromID(id_arg);
2439 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2445 lt = getLyXText(bv);
2448 if (the_locking_inset) {
2449 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2456 #warning Dekel please have a look on this one RTL? (Jug)
2458 // we have to go on checking so move cusor to the right
2459 lt->cursor.pos(lt->cursor.pos() + 1);
2461 str = lt->selectNextWordToSpellcheck(bv, value);
2463 bv->unlockInset(const_cast<InsetText *>(this));
2472 void InsetText::selectSelectedWord(BufferView * bv)
2474 if (the_locking_inset) {
2475 the_locking_inset->selectSelectedWord(bv);
2478 getLyXText(bv)->selectSelectedWord(bv);
2479 updateLocal(bv, SELECTION, false);
2483 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2485 if (the_locking_inset) {
2486 the_locking_inset->toggleSelection(bv, kill_selection);
2490 lt = getLyXText(bv);
2494 int x = top_x + TEXT_TO_INSET_OFFSET;
2496 Row * row = lt->firstRow();
2497 int y_offset = top_baseline - row->ascent_of_text();
2499 while ((row != 0) && ((y+row->height()) <= 0)) {
2506 if (need_update & SELECTION)
2508 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2514 bool InsetText::searchForward(BufferView * bv, string const & str,
2515 bool const & cs, bool const & mw)
2517 if (the_locking_inset) {
2518 if (the_locking_inset->searchForward(bv, str, cs, mw))
2522 lt = getLyXText(bv);
2525 Paragraph * lpar = lt->cursor.par();
2526 pos_type pos = lt->cursor.pos();
2527 if (pos < lpar->size() - 1)
2531 lpar = lpar->next();
2536 // we have to unlock ourself in this function by default!
2537 bv->unlockInset(const_cast<InsetText *>(this));
2540 lt->setCursor(bv, lpar, pos);
2544 if (LyXFind(bv, str, true, true, cs , mw)) {
2547 // we have to unlock ourself in this function by default!
2548 bv->unlockInset(const_cast<InsetText *>(this));
2552 bool InsetText::searchBackward(BufferView * bv, string const & str,
2553 bool const & cs, bool const & mw)
2555 if (the_locking_inset)
2556 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2558 if (LyXFind(bv, str, false, true, cs, mw)) {
2561 // we have to unlock ourself in this function by default!
2562 bv->unlockInset(const_cast<InsetText *>(this));
2567 bool InsetText::checkInsertChar(LyXFont & font)
2570 return owner()->checkInsertChar(font);