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 "lyx_gui_misc.h"
37 #include "lyxcursor.h"
38 #include "CutAndPaste.h"
41 #include "support/textutils.h"
42 #include "support/LAssert.h"
46 #include "trans_mgr.h"
47 #include "lyxscreen.h"
51 #include "ParagraphParameters.h"
52 #include "undo_funcs.h"
62 extern unsigned char getCurrentTextClass(Buffer *);
63 extern bool math_insert_greek(BufferView *, char);
64 extern int greek_kb_flag;
67 #warning this functions should probably go into bufferview_funcs somehow (Jug)
69 void InsetText::saveLyXTextState(LyXText * t) const
71 sstate.lpar = t->cursor.par();
72 sstate.pos = t->cursor.pos();
73 sstate.boundary = t->cursor.boundary();
74 sstate.selstartpar = t->selection.start.par();
75 sstate.selstartpos = t->selection.start.pos();
76 sstate.selstartboundary = t->selection.start.boundary();
77 sstate.selendpar = t->selection.end.par();
78 sstate.selendpos = t->selection.end.pos();
79 sstate.selendboundary = t->selection.end.boundary();
80 sstate.selection = t->selection.set();
81 sstate.mark_set = t->selection.mark();
84 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
87 t->selection.set(true);
88 /* at this point just to avoid the Delete-Empty-Paragraph
89 * Mechanism when setting the cursor */
90 t->selection.mark(sstate.mark_set);
91 if (sstate.selection) {
92 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
93 true, sstate.selstartboundary);
94 t->selection.cursor = t->cursor;
95 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
96 true, sstate.selendboundary);
98 t->setCursor(bv, sstate.lpar, sstate.pos);
100 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
101 t->selection.cursor = t->cursor;
102 t->selection.set(false);
108 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
115 InsetText::InsetText()
122 InsetText::InsetText(InsetText const & ins, bool same_id)
127 autoBreakRows = ins.autoBreakRows;
131 InsetText & InsetText::operator=(InsetText const & it)
134 autoBreakRows = it.autoBreakRows;
139 void InsetText::init(InsetText const * ins, bool same_id)
147 the_locking_inset = 0;
149 no_selection = false;
153 autoBreakRows = false;
156 frame_color = LColor::insetframe;
158 setParagraphData(ins->par);
159 autoBreakRows = ins->autoBreakRows;
160 drawFrame_ = ins->drawFrame_;
161 frame_color = ins->frame_color;
165 par->setInsetOwner(this);
168 last_drawn_width = -1;
169 frame_is_visible = false;
176 InsetText::~InsetText()
183 Paragraph * tmp = par->next();
190 void InsetText::clear()
194 // now also delete all caches this should be safe, hopefully
198 Paragraph * tmp = par->next();
206 Inset * InsetText::clone(Buffer const &, bool same_id) const
208 return new InsetText(*this, same_id);
212 void InsetText::write(Buffer const * buf, ostream & os) const
215 writeParagraphData(buf, os);
219 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
221 par->writeFile(buf, os, buf->params, 0);
225 void InsetText::read(Buffer const * buf, LyXLex & lex)
229 Paragraph * return_par = 0;
230 Paragraph::depth_type depth = 0;
231 LyXFont font(LyXFont::ALL_INHERIT);
237 token = lex.GetString();
240 if (token == "\\end_inset") {
242 const_cast<Buffer*>(buf)->insertErtContents(par, pos, font, false);
247 if (const_cast<Buffer*>(buf)->
248 parseSingleLyXformat2Token(lex, par, return_par,
249 token, pos, depth, font)) {
250 // the_end read this should NEVER happen
251 lex.printError("\\the_end read in inset! Error in document!");
259 return_par->setInsetOwner(this);
260 return_par = return_par->next();
263 if (token != "\\end_inset") {
264 lex.printError("Missing \\end_inset at this point. "
271 int InsetText::ascent(BufferView * bv, LyXFont const &) const
274 Row * row = getLyXText(bv)->getRowNearY(y_temp);
275 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
280 int InsetText::descent(BufferView * bv, LyXFont const &) const
288 Row * row = lt->getRowNearY(y_temp);
289 insetDescent = lt->height - row->ascent_of_text() +
290 TEXT_TO_INSET_OFFSET;
297 int InsetText::width(BufferView * bv, LyXFont const &) const
299 insetWidth = max(textWidth(bv),
300 (int)getLyXText(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
305 int InsetText::textWidth(BufferView * bv) const
307 int const w = getMaxWidth(bv, this);
312 void InsetText::draw(BufferView * bv, LyXFont const & f,
313 int baseline, float & x, bool cleared) const
318 Painter & pain = bv->painter();
320 // no draw is necessary !!!
321 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
323 top_baseline = baseline;
325 if (!cleared && (need_update & CLEAR_FRAME))
326 clearFrame(pain, cleared);
328 frame_is_visible = false;
335 UpdatableInset::draw(bv, f, baseline, x, cleared);
338 x += static_cast<float>(scroll());
341 // update insetWidth and insetHeight with dummy calls
343 (void)descent(bv, f);
347 // if top_x differs we have a rule down and we don't have to clear anything
348 if (!cleared && (top_x == int(x)) &&
349 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
350 (last_drawn_width!=insetWidth))) {
351 clearInset(pain, baseline, cleared);
354 frame_is_visible = false;
356 if (!cleared && (need_update == NONE))
359 if (top_x != int(x)) {
360 if ((getMaxWidth(bv, this) > 0) &&
361 (getLyXText(bv)->width != old_max_width)) {
364 old_max_width = getLyXText(bv)->width;
365 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
368 clearInset(pain, baseline, cleared);
371 // lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
373 if (cleared || (last_drawn_width != insetWidth)) {
375 last_drawn_width = insetWidth;
378 top_baseline = baseline;
379 top_y = baseline - ascent(bv, f);
380 last_width = width(bv, f);
381 last_height = ascent(bv, f) + descent(bv, f);
383 if (the_locking_inset && (cpar(bv) == inset_par)
384 && (cpos(bv) == inset_pos)) {
385 inset_x = cx(bv) - top_x + drawTextXOffset;
386 inset_y = cy(bv) + drawTextYOffset;
388 if (!cleared && (need_update == CURSOR)
389 && !getLyXText(bv)->selection.set()) {
390 drawFrame(pain, cleared);
391 x += last_width; // was width(bv, f);
400 x += TEXT_TO_INSET_OFFSET;
403 Row * row = lt->getRowNearY(y);
404 int y_offset = baseline - row->ascent_of_text();
405 int ph = pain.paperHeight();
408 while ((row != 0) && ((y+row->height()) <= 0)) {
410 first += row->height();
419 while ((row != 0) && (yf < ph)) {
420 lt->getVisibleRow(bv, y+y_offset, int(x), row,
426 } else if (!locked) {
427 if (need_update & CURSOR) {
428 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
429 lt->clearSelection(bv);
430 lt->selection.cursor = lt->cursor;
432 bv->screen()->update(lt, bv, y_offset, int(x));
435 if (need_update & SELECTION)
436 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
437 else if (need_update & CURSOR) {
438 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
439 lt->clearSelection(bv);
440 lt->selection.cursor = lt->cursor;
442 bv->screen()->update(lt, bv, y_offset, int(x));
447 lt->status(bv, LyXText::UNCHANGED);
448 if ((need_update != CURSOR_PAR) &&
449 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
450 drawFrame(pain, cleared);
451 else if (need_update & CLEAR_FRAME)
452 clearFrame(pain, cleared);
453 x += last_width /* was width(bv, f) */ - TEXT_TO_INSET_OFFSET;
454 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
456 } else if (need_update != INIT)
463 void InsetText::drawFrame(Painter & pain, bool cleared) const
465 if (!frame_is_visible || cleared) {
466 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
467 insetWidth - 1, insetAscent + insetDescent - 1,
469 frame_is_visible = true;
474 void InsetText::clearFrame(Painter & pain, bool cleared) const
476 if (frame_is_visible) {
478 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
479 insetWidth - 1, insetAscent + insetDescent - 1,
482 frame_is_visible = false;
487 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
493 owner()->update(bv, font, true);
496 if (the_locking_inset) {
497 inset_x = cx(bv) - top_x + drawTextXOffset;
498 inset_y = cy(bv) + drawTextYOffset;
499 the_locking_inset->update(bv, font, reinit);
507 int oldw = insetWidth;
508 insetWidth = lt->width + (2 * TEXT_TO_INSET_OFFSET);
509 if (oldw != insetWidth) {
516 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
519 lt->updateInset(bv, the_locking_inset);
521 if (lt->status() == LyXText::NEED_MORE_REFRESH)
528 void InsetText::setUpdateStatus(BufferView * bv, int what) const
536 if (lt->status() == LyXText::NEED_MORE_REFRESH)
538 else if (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)
539 need_update |= CURSOR_PAR;
541 // this to not draw a selection when we redraw all of it!
542 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
543 lt->clearSelection(bv);
549 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
557 setUpdateStatus(bv, what);
558 if ((need_update != CURSOR) || (lt->status() != LyXText::UNCHANGED) ||
561 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
563 bv->owner()->showState();
564 if (old_par != cpar(bv)) {
565 bv->owner()->setLayout(cpar(bv)->getLayout());
573 string const InsetText::editMessage() const
575 return _("Opened Text Inset");
579 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
581 UpdatableInset::edit(bv, x, y, button);
583 if (!bv->lockInset(this)) {
584 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
588 the_locking_inset = 0;
589 inset_pos = inset_x = inset_y = 0;
590 inset_boundary = false;
593 int tmp_y = (y < 0) ? 0 : y;
600 if (!checkAndActivateInset(bv, x, tmp_y, button))
601 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
603 lt->clearSelection(bv);
606 updateLocal(bv, CURSOR, false);
608 // If the inset is empty set the language of the current font to the
609 // language to the surronding text (if different).
610 if (par->size() == 0 && !par->next() &&
611 bv->getParentLanguage(this) != lt->current_font.language()) {
612 LyXFont font(LyXFont::ALL_IGNORE);
613 font.setLanguage(bv->getParentLanguage(this));
614 setFont(bv, font, false);
621 void InsetText::edit(BufferView * bv, bool front)
623 UpdatableInset::edit(bv, front);
625 if (!bv->lockInset(this)) {
626 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
630 the_locking_inset = 0;
631 inset_pos = inset_x = inset_y = 0;
632 inset_boundary = false;
641 lt->setCursor(bv, par, 0);
646 lt->setCursor(bv, p, p->size()-1);
648 lt->clearSelection(bv);
651 updateLocal(bv, CURSOR, false);
653 // If the inset is empty set the language of the current font to the
654 // language to the surronding text (if different).
655 if (par->size() == 0 && !par->next() &&
656 bv->getParentLanguage(this) != lt->current_font.language()) {
657 LyXFont font(LyXFont::ALL_IGNORE);
658 font.setLanguage(bv->getParentLanguage(this));
659 setFont(bv, font, false);
666 void InsetText::insetUnlock(BufferView * bv)
668 if (the_locking_inset) {
669 the_locking_inset->insetUnlock(bv);
670 the_locking_inset = 0;
673 no_selection = false;
676 if (drawFrame_ == LOCKED)
677 code = CURSOR|CLEAR_FRAME;
685 if (lt->selection.set()) {
686 lt->clearSelection(bv);
688 } else if (owner()) {
689 bv->owner()->setLayout(owner()->getLyXText(bv)
690 ->cursor.par()->getLayout());
692 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
693 updateLocal(bv, code, false);
699 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
701 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
705 if (inset == cpar(bv)->getInset(cpos(bv))) {
706 lyxerr[Debug::INSETS] << "OK" << endl;
707 the_locking_inset = inset;
708 inset_x = cx(bv) - top_x + drawTextXOffset;
709 inset_y = cy(bv) + drawTextYOffset;
710 inset_pos = cpos(bv);
711 inset_par = cpar(bv);
712 inset_boundary = cboundary(bv);
713 updateLocal(bv, CURSOR, false);
715 } else if (the_locking_inset && (the_locking_inset == inset)) {
716 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
717 lyxerr[Debug::INSETS] << "OK" << endl;
718 inset_x = cx(bv) - top_x + drawTextXOffset;
719 inset_y = cy(bv) + drawTextYOffset;
721 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
723 } else if (the_locking_inset) {
724 lyxerr[Debug::INSETS] << "MAYBE" << endl;
725 return the_locking_inset->lockInsetInInset(bv, inset);
727 lyxerr[Debug::INSETS] << "NOT OK" << endl;
732 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
735 if (!the_locking_inset)
737 if (the_locking_inset == inset) {
738 the_locking_inset->insetUnlock(bv);
739 getLyXText(bv)->updateInset(bv, inset);
740 the_locking_inset = 0;
742 moveRight(bv, false);
743 old_par = 0; // force layout setting
747 updateLocal(bv, CURSOR, false);
750 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
754 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
756 if (!the_locking_inset)
758 if (the_locking_inset != inset) {
759 getLyXText(bv)->updateInset(bv, the_locking_inset);
760 setUpdateStatus(bv, CURSOR_PAR);
761 return the_locking_inset->updateInsetInInset(bv, inset);
763 // updateLocal(bv, FULL, false);
764 if (getLyXText(bv)->updateInset(bv, inset))
765 updateLocal(bv, CURSOR_PAR, false);
766 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
767 inset_x = cx(bv) - top_x + drawTextXOffset;
768 inset_y = cy(bv) + drawTextYOffset;
774 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
778 int tmp_x = x - drawTextXOffset;
779 int tmp_y = y + insetAscent - getLyXText(bv)->first;
780 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
783 if (the_locking_inset) {
784 if (the_locking_inset == inset) {
785 the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
786 no_selection = false;
789 // otherwise unlock the_locking_inset and lock the new inset
790 the_locking_inset->insetUnlock(bv);
791 inset_x = cx(bv) - top_x + drawTextXOffset;
792 inset_y = cy(bv) + drawTextYOffset;
793 the_locking_inset = static_cast<UpdatableInset*>(inset);
794 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
795 inset->edit(bv, x - inset_x, y - inset_y, button);
796 if (the_locking_inset)
797 updateLocal(bv, CURSOR, false);
798 no_selection = false;
801 // otherwise only unlock the_locking_inset
802 the_locking_inset->insetUnlock(bv);
803 the_locking_inset = 0;
805 if (bv->theLockingInset()) {
806 if (inset && inset->editable() == Inset::HIGHLY_EDITABLE) {
807 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
808 inset_x = cx(bv) - top_x + drawTextXOffset;
809 inset_y = cy(bv) + drawTextYOffset;
810 inset_pos = cpos(bv);
811 inset_par = cpar(bv);
812 inset_boundary = cboundary(bv);
813 the_locking_inset = uinset;
814 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
816 uinset->edit(bv, x - inset_x, y - inset_y, 0);
817 if (the_locking_inset)
818 updateLocal(bv, CURSOR, false);
819 no_selection = false;
823 if (!inset) { // && (button == 2)) {
824 bool paste_internally = false;
825 if ((button == 2) && getLyXText(bv)->selection.set()) {
826 localDispatch(bv, LFUN_COPY, "");
827 paste_internally = true;
829 getLyXText(bv)->setCursorFromCoordinates(bv, x-drawTextXOffset,
831 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
832 updateLocal(bv, CURSOR, false);
833 bv->owner()->setLayout(cpar(bv)->getLayout());
835 // Insert primary selection with middle mouse
836 // if there is a local selection in the current buffer,
839 if (paste_internally)
840 localDispatch(bv, LFUN_PASTE, "");
842 localDispatch(bv, LFUN_PASTESELECTION,
847 no_selection = false;
851 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
853 UpdatableInset * inset = 0;
855 if (the_locking_inset) {
856 the_locking_inset->insetButtonRelease(bv,
857 x - inset_x, y - inset_y,
860 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
861 inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
862 if (inset->editable() == Inset::HIGHLY_EDITABLE) {
863 inset->insetButtonRelease(bv,
865 y - inset_y, button);
867 inset_x = cx(bv) - top_x + drawTextXOffset;
868 inset_y = cy(bv) + drawTextYOffset;
869 inset->insetButtonRelease(bv,
871 y - inset_y, button);
873 x - inset_x, y - inset_y, button);
875 updateLocal(bv, CURSOR_PAR, false);
878 no_selection = false;
882 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
886 if (the_locking_inset) {
887 the_locking_inset->insetMotionNotify(bv, x - inset_x,
891 LyXText * t = getLyXText(bv);
893 t->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
895 if (t->toggle_cursor.par() != t->toggle_end_cursor.par() ||
896 t->toggle_cursor.pos() != t->toggle_end_cursor.pos())
897 updateLocal(bv, SELECTION, false);
902 void InsetText::insetKeyPress(XKeyEvent * xke)
904 if (the_locking_inset) {
905 the_locking_inset->insetKeyPress(xke);
911 UpdatableInset::RESULT
912 InsetText::localDispatch(BufferView * bv,
913 kb_action action, string const & arg)
915 no_selection = false;
916 UpdatableInset::RESULT
917 result= UpdatableInset::localDispatch(bv, action, arg);
918 if (result != UNDISPATCHED) {
923 if ((action < 0) && arg.empty())
926 if (the_locking_inset) {
927 result = the_locking_inset->localDispatch(bv, action, arg);
928 if (result == DISPATCHED_NOUPDATE)
930 else if (result == DISPATCHED) {
931 updateLocal(bv, CURSOR_PAR, false);
933 } else if (result == FINISHED) {
934 bool dispatched = false;
936 case LFUN_UNKNOWN_ACTION:
937 case LFUN_BREAKPARAGRAPH:
939 moveRightIntern(bv, false, false);
942 if (!getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
943 moveRightIntern(bv, false, false);
947 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
948 moveRightIntern(bv, false, false);
954 the_locking_inset = 0;
967 case LFUN_SELFINSERT:
968 if (bv->buffer()->isReadonly()) {
969 // setErrorMessage(N_("Document is read only"));
973 /* Automatically delete the currently selected
974 * text and replace it with what is being
975 * typed in now. Depends on lyxrc settings
976 * "auto_region_delete", which defaults to
979 setUndo(bv, Undo::INSERT,
980 lt->cursor.par(), lt->cursor.par()->next());
982 if (lyxrc.auto_region_delete) {
983 if (lt->selection.set()) {
984 lt->cutSelection(bv, false);
987 lt->clearSelection(bv);
988 for (string::size_type i = 0; i < arg.length(); ++i) {
990 if (!math_insert_greek(bv, arg[i])) {
991 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
992 } else if (!the_locking_inset) {
993 (void)moveRight(bv, false);
996 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1000 lt->selection.cursor = lt->cursor;
1001 updateLocal(bv, CURSOR_PAR, true);
1002 result = DISPATCHED_NOUPDATE;
1004 // --- Cursor Movements -----------------------------------
1007 moveRight(bv, false, true);
1008 lt->setSelection(bv);
1009 updateLocal(bv, SELECTION, false);
1012 result = moveRight(bv);
1014 updateLocal(bv, CURSOR, false);
1018 moveLeft(bv, false, true);
1019 lt->setSelection(bv);
1020 updateLocal(bv, SELECTION, false);
1024 result = moveLeft(bv);
1025 updateLocal(bv, CURSOR, false);
1030 lt->setSelection(bv);
1031 updateLocal(bv, SELECTION, false);
1035 result = moveDown(bv);
1036 updateLocal(bv, CURSOR, false);
1041 lt->setSelection(bv);
1042 updateLocal(bv, SELECTION, false);
1046 result = moveUp(bv);
1047 updateLocal(bv, CURSOR, false);
1052 updateLocal(bv, CURSOR, false);
1056 updateLocal(bv, CURSOR, false);
1058 case LFUN_BACKSPACE: {
1059 setUndo(bv, Undo::DELETE,
1060 lt->cursor.par(), lt->cursor.par()->next());
1061 if (lt->selection.set())
1062 lt->cutSelection(bv);
1065 updateLocal(bv, CURSOR_PAR, true);
1070 setUndo(bv, Undo::DELETE,
1071 lt->cursor.par(), lt->cursor.par()->next());
1072 if (lt->selection.set()) {
1073 lt->cutSelection(bv);
1077 updateLocal(bv, CURSOR_PAR, true);
1082 setUndo(bv, Undo::DELETE,
1083 lt->cursor.par(), lt->cursor.par()->next());
1084 lt->cutSelection(bv);
1085 updateLocal(bv, CURSOR_PAR, true);
1091 lt->copySelection(bv);
1092 updateLocal(bv, CURSOR_PAR, false);
1094 case LFUN_PASTESELECTION:
1096 string const clip(bv->getClipboard());
1100 if (arg == "paragraph") {
1101 lt->insertStringAsParagraphs(bv, clip);
1103 lt->insertStringAsLines(bv, clip);
1105 updateLocal(bv, CURSOR_PAR, true);
1109 if (!autoBreakRows) {
1111 if (CutAndPaste::nrOfParagraphs() > 1) {
1112 WriteAlert(_("Impossible operation"),
1113 _("Cannot include more than one paragraph!"),
1118 setUndo(bv, Undo::INSERT,
1119 lt->cursor.par(), lt->cursor.par()->next());
1120 lt->pasteSelection(bv);
1121 updateLocal(bv, CURSOR_PAR, true);
1125 case LFUN_BREAKPARAGRAPH:
1126 if (!autoBreakRows) {
1127 result = DISPATCHED;
1130 lt->breakParagraph(bv, 0);
1131 updateLocal(bv, FULL, true);
1133 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1134 if (!autoBreakRows) {
1135 result = DISPATCHED;
1138 lt->breakParagraph(bv, 1);
1139 updateLocal(bv, FULL, true);
1142 case LFUN_BREAKLINE: {
1143 if (!autoBreakRows) {
1144 result = DISPATCHED;
1147 setUndo(bv, Undo::INSERT,
1148 lt->cursor.par(), lt->cursor.par()->next());
1149 lt->insertChar(bv, Paragraph::META_NEWLINE);
1150 updateLocal(bv, CURSOR_PAR, true);
1155 // do not set layouts on non breakable textinsets
1156 if (autoBreakRows) {
1157 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1159 // Derive layout number from given argument (string)
1160 // and current buffer's textclass (number). */
1161 LyXTextClassList::ClassList::size_type tclass =
1162 bv->buffer()->params.textclass;
1163 std::pair <bool, LyXTextClass::size_type> layout =
1164 textclasslist.NumberOfLayout(tclass, arg);
1166 // If the entry is obsolete, use the new one instead.
1168 string obs = textclasslist.Style(tclass,layout.second).
1171 layout = textclasslist.NumberOfLayout(tclass, obs);
1174 // see if we found the layout number:
1175 if (!layout.first) {
1176 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1177 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1181 if (cur_layout != layout.second) {
1182 cur_layout = layout.second;
1183 lt->setLayout(bv, layout.second);
1184 bv->owner()->setLayout(cpar(bv)->getLayout());
1185 updateLocal(bv, CURSOR_PAR, true);
1188 // reset the layout box
1189 bv->owner()->setLayout(cpar(bv)->getLayout());
1192 case LFUN_PARAGRAPH_SPACING:
1193 // This one is absolutely not working. When fiddling with this
1194 // it also seems to me that the paragraphs inside the insettext
1195 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1197 Paragraph * par = lt->cursor.par();
1198 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1199 float cur_value = 1.0;
1200 if (cur_spacing == Spacing::Other) {
1201 cur_value = par->params().spacing().getValue();
1204 istringstream istr(arg.c_str());
1207 Spacing::Space new_spacing = cur_spacing;
1208 float new_value = cur_value;
1210 lyxerr << "Missing argument to `paragraph-spacing'"
1212 } else if (tmp == "single") {
1213 new_spacing = Spacing::Single;
1214 } else if (tmp == "onehalf") {
1215 new_spacing = Spacing::Onehalf;
1216 } else if (tmp == "double") {
1217 new_spacing = Spacing::Double;
1218 } else if (tmp == "other") {
1219 new_spacing = Spacing::Other;
1222 lyxerr << "new_value = " << tmpval << endl;
1225 } else if (tmp == "default") {
1226 new_spacing = Spacing::Default;
1228 lyxerr << _("Unknown spacing argument: ")
1231 if (cur_spacing != new_spacing || cur_value != new_value) {
1232 par->params().spacing(Spacing(new_spacing, new_value));
1233 updateLocal(bv, CURSOR_PAR, true);
1239 if (!bv->Dispatch(action, arg))
1240 result = UNDISPATCHED;
1244 /// If the action has deleted all text in the inset, we need to change the
1245 // language to the language of the surronding text.
1246 if (par->size() == 0 && !par->next()) {
1247 LyXFont font(LyXFont::ALL_IGNORE);
1248 font.setLanguage(bv->getParentLanguage(this));
1249 setFont(bv, font, false);
1252 if (result != FINISHED) {
1253 showInsetCursor(bv);
1255 bv->unlockInset(this);
1262 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1265 buf->latexParagraphs(os, par, 0, texrow);
1266 return texrow.rows();
1270 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1272 Paragraph * p = par;
1273 unsigned int lines = 0;
1276 string const tmp = buf->asciiParagraph(p, linelen);
1277 lines += countChar(tmp, '\n');
1285 int InsetText::docBook(Buffer const * buf, ostream & os) const
1287 Paragraph * p = par;
1288 unsigned int lines = 0;
1293 buf->simpleDocBookOnePar(os, tmp, p, desc, 0);
1301 void InsetText::validate(LaTeXFeatures & features) const
1303 Paragraph * p = par;
1305 p->validate(features);
1311 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1313 if (textclasslist.Style(buf->params.textclass,
1314 p->getLayout()).labeltype != LABEL_MANUAL)
1317 return p->beginningOfMainBody();
1321 void InsetText::getCursorPos(BufferView * bv,
1322 int & x, int & y) const
1324 if (the_locking_inset) {
1325 the_locking_inset->getCursorPos(bv, x, y);
1333 unsigned int InsetText::insetInInsetY()
1335 if (!the_locking_inset)
1338 return (inset_y + the_locking_inset->insetInInsetY());
1342 void InsetText::toggleInsetCursor(BufferView * bv)
1344 if (the_locking_inset) {
1345 the_locking_inset->toggleInsetCursor(bv);
1349 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1351 int const asc = lyxfont::maxAscent(font);
1352 int const desc = lyxfont::maxDescent(font);
1354 if (isCursorVisible())
1355 bv->hideLockedInsetCursor();
1357 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1358 toggleCursorVisible();
1362 void InsetText::showInsetCursor(BufferView * bv, bool show)
1364 if (the_locking_inset) {
1365 the_locking_inset->showInsetCursor(bv, show);
1368 if (!isCursorVisible()) {
1369 LyXFont const font =
1370 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1372 int const asc = lyxfont::maxAscent(font);
1373 int const desc = lyxfont::maxDescent(font);
1375 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1377 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1378 setCursorVisible(true);
1383 void InsetText::hideInsetCursor(BufferView * bv)
1385 if (isCursorVisible()) {
1386 bv->hideLockedInsetCursor();
1387 setCursorVisible(false);
1389 if (the_locking_inset)
1390 the_locking_inset->hideInsetCursor(bv);
1394 void InsetText::fitInsetCursor(BufferView * bv) const
1396 if (the_locking_inset) {
1397 the_locking_inset->fitInsetCursor(bv);
1400 LyXFont const font =
1401 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1403 int const asc = lyxfont::maxAscent(font);
1404 int const desc = lyxfont::maxDescent(font);
1406 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1410 UpdatableInset::RESULT
1411 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1413 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1414 return moveLeftIntern(bv, false, activate_inset, selecting);
1416 return moveRightIntern(bv, false, activate_inset, selecting);
1420 UpdatableInset::RESULT
1421 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1423 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1424 return moveRightIntern(bv, true, activate_inset, selecting);
1426 return moveLeftIntern(bv, true, activate_inset, selecting);
1430 UpdatableInset::RESULT
1431 InsetText::moveRightIntern(BufferView * bv, bool behind,
1432 bool activate_inset, bool selecting)
1434 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1436 if (activate_inset && checkAndActivateInset(bv, behind))
1438 getLyXText(bv)->cursorRight(bv);
1440 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1441 return DISPATCHED_NOUPDATE;
1445 UpdatableInset::RESULT
1446 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1447 bool activate_inset, bool selecting)
1449 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1451 getLyXText(bv)->cursorLeft(bv);
1453 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1454 if (activate_inset && checkAndActivateInset(bv, behind))
1456 return DISPATCHED_NOUPDATE;
1460 UpdatableInset::RESULT
1461 InsetText::moveUp(BufferView * bv)
1463 if (!crow(bv)->previous())
1465 getLyXText(bv)->cursorUp(bv);
1466 return DISPATCHED_NOUPDATE;
1470 UpdatableInset::RESULT
1471 InsetText::moveDown(BufferView * bv)
1473 if (!crow(bv)->next())
1475 getLyXText(bv)->cursorDown(bv);
1476 return DISPATCHED_NOUPDATE;
1480 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1482 if (the_locking_inset) {
1483 if (the_locking_inset->insetAllowed(inset))
1484 return the_locking_inset->insertInset(bv, inset);
1487 inset->setOwner(this);
1488 hideInsetCursor(bv);
1492 lt = getLyXText(bv);
1495 lt->insertInset(bv, inset);
1496 if ((cpar(bv)->getChar(cpos(bv)) != Paragraph::META_INSET) ||
1497 (cpar(bv)->getInset(cpos(bv)) != inset))
1500 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1501 showInsetCursor(bv);
1508 bool InsetText::insetAllowed(Inset::Code code) const
1510 if (the_locking_inset)
1511 return the_locking_inset->insetAllowed(code);
1516 UpdatableInset * InsetText::getLockingInset() const
1518 return the_locking_inset ? the_locking_inset->getLockingInset() :
1519 const_cast<InsetText *>(this);
1523 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1527 if (the_locking_inset)
1528 return the_locking_inset->getFirstLockingInsetOfType(c);
1533 bool InsetText::showInsetDialog(BufferView * bv) const
1535 if (the_locking_inset)
1536 return the_locking_inset->showInsetDialog(bv);
1541 std::vector<string> const InsetText::getLabelList() const
1543 std::vector<string> label_list;
1545 Paragraph * tpar = par;
1547 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1548 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1549 for (; beg != end; ++beg) {
1550 std::vector<string> const l = (*beg)->getLabelList();
1551 label_list.insert(label_list.end(), l.begin(), l.end());
1553 tpar = tpar->next();
1559 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1562 if (the_locking_inset) {
1563 the_locking_inset->setFont(bv, font, toggleall, selectall);
1568 lt = getLyXText(bv);
1571 if (lt->selection.set()) {
1572 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1577 lt->toggleFree(bv, font, toggleall);
1579 lt->setFont(bv, font, toggleall);
1582 lt->clearSelection(bv);
1584 if (selectall || lt->selection.set())
1585 updateLocal(bv, FULL, true);
1587 updateLocal(bv, CURSOR_PAR, true);
1593 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1595 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
1599 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1600 if (!inset || inset->editable() != Inset::HIGHLY_EDITABLE)
1602 LyXFont const font =
1603 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1605 x = inset->width(bv, font);
1606 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1609 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1611 //inset_x = cx(bv) - top_x + drawTextXOffset;
1612 //inset_y = cy(bv) + drawTextYOffset;
1613 inset->edit(bv, x, y, 0);
1614 if (!the_locking_inset)
1616 updateLocal(bv, CURSOR, false);
1623 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1626 x -= drawTextXOffset;
1628 int dummyy = y + insetAscent;
1629 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1636 inset_x = cx(bv) - top_x + drawTextXOffset;
1637 inset_y = cy(bv) + drawTextYOffset;
1638 inset->edit(bv, x - inset_x, y - inset_y, button);
1639 if (!the_locking_inset)
1641 updateLocal(bv, CURSOR, false);
1648 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1650 int w = UpdatableInset::getMaxWidth(bv, inset);
1655 w = w - top_x + owner()->x();
1658 w -= (2 * TEXT_TO_INSET_OFFSET);
1660 // return w - (2*TEXT_TO_INSET_OFFSET);
1664 void InsetText::setParagraphData(Paragraph * p)
1668 // now also delete all caches this should be safe, hopefully
1672 Paragraph * tmp = par->next();
1677 par = new Paragraph(*p);
1678 par->setInsetOwner(this);
1679 Paragraph * np = par;
1682 np->next(new Paragraph(*p));
1683 np->next()->previous(np);
1685 np->setInsetOwner(this);
1692 void InsetText::setText(string const & data)
1695 LyXFont font(LyXFont::ALL_SANE);
1696 for (unsigned int i=0; i < data.length(); ++i)
1697 par->insertChar(i, data[i], font);
1701 void InsetText::setAutoBreakRows(bool flag)
1703 if (flag != autoBreakRows) {
1704 autoBreakRows = flag;
1712 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1714 if (how != drawFrame_) {
1717 updateLocal(bv, DRAW_FRAME, false);
1722 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1724 if (frame_color != col) {
1727 updateLocal(bv, DRAW_FRAME, false);
1732 int InsetText::cx(BufferView * bv) const
1736 lt = getLyXText(bv);
1739 int x = lt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1740 if (the_locking_inset) {
1741 LyXFont font = lt->getFont(bv->buffer(),
1744 if (font.isVisibleRightToLeft())
1745 x -= the_locking_inset->width(bv, font);
1753 int InsetText::cy(BufferView * bv) const
1756 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1760 Paragraph::size_type InsetText::cpos(BufferView * bv) const
1762 return getLyXText(bv)->cursor.pos();
1766 Paragraph * InsetText::cpar(BufferView * bv) const
1768 return getLyXText(bv)->cursor.par();
1772 bool InsetText::cboundary(BufferView * bv) const
1774 return getLyXText(bv)->cursor.boundary();
1778 Row * InsetText::crow(BufferView * bv) const
1780 return getLyXText(bv)->cursor.row();
1784 LyXText * InsetText::getLyXText(BufferView const * lbv,
1785 bool const recursive) const
1787 if (!recursive && (cached_bview == lbv))
1788 return cached_text.get();
1790 // Super UGLY! (Lgb)
1791 BufferView * bv = const_cast<BufferView *>(lbv);
1794 Cache::iterator it = cache.find(bv);
1796 if (it != cache.end()) {
1797 if (lt || !it->second.remove) {
1798 lyx::Assert(it->second.text.get());
1799 cached_text = it->second.text;
1800 if (recursive && the_locking_inset) {
1801 return the_locking_inset->getLyXText(bv);
1803 return cached_text.get();
1804 } else if (it->second.remove) {
1806 saveLyXTextState(it->second.text.get());
1813 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
1814 cached_text->init(bv);
1815 restoreLyXTextState(bv, cached_text.get());
1817 cache.insert(make_pair(bv, cached_text));
1819 if (the_locking_inset && recursive) {
1820 return the_locking_inset->getLyXText(bv);
1822 return cached_text.get();
1826 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1830 Cache::iterator it = cache.find(bv);
1832 if (it == cache.end()) {
1836 lyx::Assert(it->second.text.get());
1838 it->second.remove = true;
1840 /// then remove all LyXText in text-insets
1841 Paragraph * p = par;
1842 for (; p; p = p->next()) {
1843 p->deleteInsetsLyXText(bv);
1849 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1851 if (!par->next() && !par->size()) // no data, resize not neccessary!
1853 // one endless line, resize normally not necessary
1854 if (!force && getMaxWidth(bv, this) < 0)
1857 Cache::iterator it = cache.find(bv);
1858 if (it == cache.end()) {
1861 lyx::Assert(it->second.text.get());
1863 LyXText * t = it->second.text.get();
1864 saveLyXTextState(t);
1865 for (Paragraph * p = par; p; p = p->next()) {
1866 p->resizeInsetsLyXText(bv);
1869 restoreLyXTextState(bv, t);
1870 if (the_locking_inset) {
1871 inset_x = cx(bv) - top_x + drawTextXOffset;
1872 inset_y = cy(bv) + drawTextYOffset;
1876 t->first = bv->screen()->topCursorVisible(t);
1879 updateLocal(bv, FULL, false);
1882 // this will scroll the screen such that the cursor becomes visible
1883 bv->updateScrollbar();
1887 void InsetText::removeNewlines()
1889 for (Paragraph * p = par; p; p = p->next()) {
1890 for (int i = 0; i < p->size(); ++i) {
1891 if (p->getChar(i) == Paragraph::META_NEWLINE)
1898 bool InsetText::nodraw() const
1900 if (the_locking_inset)
1901 return the_locking_inset->nodraw();
1902 return UpdatableInset::nodraw();
1906 int InsetText::scroll(bool recursive) const
1908 int sx = UpdatableInset::scroll(false);
1910 if (recursive && the_locking_inset)
1911 sx += the_locking_inset->scroll(recursive);
1917 bool InsetText::doClearArea() const
1919 return !locked || (need_update & (FULL|INIT));
1923 void InsetText::selectAll(BufferView * bv)
1925 getLyXText(bv)->cursorTop(bv);
1926 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1927 getLyXText(bv)->cursorBottom(bv);
1928 getLyXText(bv)->setSelection(bv);
1932 void InsetText::clearSelection(BufferView * bv)
1934 getLyXText(bv)->clearSelection(bv);
1938 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
1941 int h = insetAscent + insetDescent;
1942 int ty = baseline - insetAscent;
1948 if ((ty + h) > pain.paperHeight())
1949 h = pain.paperHeight();
1950 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
1951 w = pain.paperWidth();
1952 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h,
1959 Paragraph * InsetText::getParFromID(int id) const
1961 Paragraph * result = par;
1962 Paragraph * ires = 0;
1963 while (result && result->id() != id) {
1964 if ((ires = result->getParFromID(id)))
1966 result = result->next();
1972 Paragraph * InsetText::firstParagraph() const
1975 if (the_locking_inset)
1976 if ((result = the_locking_inset->firstParagraph()))
1982 LyXCursor const & InsetText::cursor(BufferView * bv) const
1984 if (the_locking_inset)
1985 return the_locking_inset->cursor(bv);
1986 return getLyXText(bv)->cursor;
1990 Paragraph * InsetText::paragraph() const
1996 void InsetText::paragraph(Paragraph * p)
2000 // we now have to update/redraw all instances
2001 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
2006 // redraw myself when asked for
2007 need_update |= INIT;
2011 Inset * InsetText::getInsetFromID(int id_arg) const
2014 return const_cast<InsetText *>(this);
2016 Paragraph * lp = par;
2019 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2020 en = lp->inset_iterator_end();
2023 if ((*it)->id() == id_arg)
2025 Inset * in = (*it)->getInsetFromID(id_arg);
2035 string const InsetText::selectNextWord(BufferView * bv, float & value) const
2041 lt = getLyXText(bv);
2044 if (the_locking_inset) {
2045 str = the_locking_inset->selectNextWord(bv, value);
2052 #warning Dekel please have a look on this one RTL? (Jug)
2053 // we have to go on checking so move cusor to the right
2054 lt->cursor.pos(lt->cursor.pos() + 1);
2056 str = lt->selectNextWord(bv, value);
2058 bv->unlockInset(const_cast<InsetText *>(this));
2067 void InsetText::selectSelectedWord(BufferView * bv)
2069 if (the_locking_inset) {
2070 the_locking_inset->selectSelectedWord(bv);
2073 getLyXText(bv)->selectSelectedWord(bv);
2074 updateLocal(bv, SELECTION, false);
2078 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2080 if (the_locking_inset) {
2081 the_locking_inset->toggleSelection(bv, kill_selection);
2085 lt = getLyXText(bv);
2089 int x = top_x + TEXT_TO_INSET_OFFSET;
2092 Row * row = lt->getRowNearY(y);
2093 int y_offset = top_baseline - row->ascent_of_text();
2095 while ((row != 0) && ((y+row->height()) <= 0)) {
2102 if (need_update & SELECTION)
2104 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2110 bool InsetText::searchForward(BufferView * bv, string const & str,
2111 bool const & cs, bool const & mw)
2113 if (the_locking_inset)
2114 if (the_locking_inset->searchForward(bv, str, cs, mw))
2116 if (LyXFind(bv, str, true, true, cs , mw)) {
2119 // we have to unlock ourself in this function by default!
2120 bv->unlockInset(const_cast<InsetText *>(this));
2124 bool InsetText::searchBackward(BufferView * bv, string const & str,
2125 bool const & cs, bool const & mw)
2127 if (the_locking_inset)
2128 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2130 if (LyXFind(bv, str, false, true, cs, mw)) {
2133 // we have to unlock ourself in this function by default!
2134 bv->unlockInset(const_cast<InsetText *>(this));