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"
43 #include "support/lstrings.h"
47 #include "trans_mgr.h"
48 #include "lyxscreen.h"
52 #include "ParagraphParameters.h"
53 #include "undo_funcs.h"
63 extern unsigned char getCurrentTextClass(Buffer *);
64 extern bool math_insert_greek(BufferView *, char);
65 extern int greek_kb_flag;
68 #warning this functions should probably go into bufferview_funcs somehow (Jug)
70 void InsetText::saveLyXTextState(LyXText * t) const
72 sstate.lpar = t->cursor.par();
73 sstate.pos = t->cursor.pos();
74 sstate.boundary = t->cursor.boundary();
75 sstate.selstartpar = t->selection.start.par();
76 sstate.selstartpos = t->selection.start.pos();
77 sstate.selstartboundary = t->selection.start.boundary();
78 sstate.selendpar = t->selection.end.par();
79 sstate.selendpos = t->selection.end.pos();
80 sstate.selendboundary = t->selection.end.boundary();
81 sstate.selection = t->selection.set();
82 sstate.mark_set = t->selection.mark();
85 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
88 t->selection.set(true);
89 /* at this point just to avoid the Delete-Empty-Paragraph
90 * Mechanism when setting the cursor */
91 t->selection.mark(sstate.mark_set);
92 if (sstate.selection) {
93 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
94 true, sstate.selstartboundary);
95 t->selection.cursor = t->cursor;
96 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
97 true, sstate.selendboundary);
99 t->setCursor(bv, sstate.lpar, sstate.pos);
101 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
102 t->selection.cursor = t->cursor;
103 t->selection.set(false);
109 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
116 InsetText::InsetText()
123 InsetText::InsetText(InsetText const & ins, bool same_id)
128 autoBreakRows = ins.autoBreakRows;
132 InsetText & InsetText::operator=(InsetText const & it)
135 autoBreakRows = it.autoBreakRows;
140 void InsetText::init(InsetText const * ins, bool same_id)
148 the_locking_inset = 0;
150 no_selection = false;
154 autoBreakRows = false;
157 frame_color = LColor::insetframe;
159 setParagraphData(ins->par);
160 autoBreakRows = ins->autoBreakRows;
161 drawFrame_ = ins->drawFrame_;
162 frame_color = ins->frame_color;
166 par->setInsetOwner(this);
169 last_drawn_width = -1;
170 frame_is_visible = false;
177 InsetText::~InsetText()
184 Paragraph * tmp = par->next();
191 void InsetText::clear()
195 // now also delete all caches this should be safe, hopefully
199 Paragraph * tmp = par->next();
207 Inset * InsetText::clone(Buffer const &, bool same_id) const
209 return new InsetText(*this, same_id);
213 void InsetText::write(Buffer const * buf, ostream & os) const
216 writeParagraphData(buf, os);
220 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
222 par->writeFile(buf, os, buf->params, 0);
226 void InsetText::read(Buffer const * buf, LyXLex & lex)
230 Paragraph * return_par = 0;
231 Paragraph::depth_type depth = 0;
232 LyXFont font(LyXFont::ALL_INHERIT);
238 token = lex.GetString();
241 if (token == "\\end_inset") {
243 const_cast<Buffer*>(buf)->insertErtContents(par, pos, font, false);
248 if (const_cast<Buffer*>(buf)->
249 parseSingleLyXformat2Token(lex, par, return_par,
250 token, pos, depth, font)) {
251 // the_end read this should NEVER happen
252 lex.printError("\\the_end read in inset! Error in document!");
260 return_par->setInsetOwner(this);
261 return_par = return_par->next();
264 if (token != "\\end_inset") {
265 lex.printError("Missing \\end_inset at this point. "
272 int InsetText::ascent(BufferView * bv, LyXFont const &) const
275 Row * row = getLyXText(bv)->getRowNearY(y_temp);
276 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
281 int InsetText::descent(BufferView * bv, LyXFont const &) const
289 Row * row = lt->getRowNearY(y_temp);
290 insetDescent = lt->height - row->ascent_of_text() +
291 TEXT_TO_INSET_OFFSET;
298 int InsetText::width(BufferView * bv, LyXFont const &) const
300 insetWidth = max(textWidth(bv),
301 (int)getLyXText(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
306 int InsetText::textWidth(BufferView * bv) const
308 int const w = getMaxWidth(bv, this);
313 void InsetText::draw(BufferView * bv, LyXFont const & f,
314 int baseline, float & x, bool cleared) const
319 Painter & pain = bv->painter();
321 // no draw is necessary !!!
322 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
324 top_baseline = baseline;
326 if (!cleared && (need_update & CLEAR_FRAME))
327 clearFrame(pain, cleared);
329 frame_is_visible = false;
336 UpdatableInset::draw(bv, f, baseline, x, cleared);
339 x += static_cast<float>(scroll());
342 // update insetWidth and insetHeight with dummy calls
344 (void)descent(bv, f);
348 // if top_x differs we have a rule down and we don't have to clear anything
349 if (!cleared && (top_x == int(x)) &&
350 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
351 (last_drawn_width!=insetWidth))) {
352 clearInset(pain, baseline, cleared);
355 frame_is_visible = false;
357 if (!cleared && (need_update == NONE))
360 if (top_x != int(x)) {
361 if ((getMaxWidth(bv, this) > 0) &&
362 (getLyXText(bv)->width != old_max_width)) {
365 old_max_width = getLyXText(bv)->width;
366 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
369 clearInset(pain, baseline, cleared);
372 // lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
374 if (cleared || (last_drawn_width != insetWidth)) {
376 last_drawn_width = insetWidth;
379 top_baseline = baseline;
380 top_y = baseline - ascent(bv, f);
381 last_width = width(bv, f);
382 last_height = ascent(bv, f) + descent(bv, f);
384 if (the_locking_inset && (cpar(bv) == inset_par)
385 && (cpos(bv) == inset_pos)) {
386 inset_x = cx(bv) - top_x + drawTextXOffset;
387 inset_y = cy(bv) + drawTextYOffset;
389 if (!cleared && (need_update == CURSOR)
390 && !getLyXText(bv)->selection.set()) {
391 drawFrame(pain, cleared);
392 x += last_width; // was width(bv, f);
401 x += TEXT_TO_INSET_OFFSET;
404 Row * row = lt->getRowNearY(y);
405 int y_offset = baseline - row->ascent_of_text();
406 int ph = pain.paperHeight();
409 while ((row != 0) && ((y+row->height()) <= 0)) {
411 first += row->height();
420 while ((row != 0) && (yf < ph)) {
421 lt->getVisibleRow(bv, y+y_offset, int(x), row,
427 } else if (!locked) {
428 if (need_update & CURSOR) {
429 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
430 lt->clearSelection(bv);
431 lt->selection.cursor = lt->cursor;
433 bv->screen()->update(lt, bv, y_offset, int(x));
436 if (need_update & SELECTION)
437 bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
438 else if (need_update & CURSOR) {
439 bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
440 lt->clearSelection(bv);
441 lt->selection.cursor = lt->cursor;
443 bv->screen()->update(lt, bv, y_offset, int(x));
448 lt->status(bv, LyXText::UNCHANGED);
449 if ((need_update != CURSOR_PAR) &&
450 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
451 drawFrame(pain, cleared);
452 else if (need_update & CLEAR_FRAME)
453 clearFrame(pain, cleared);
454 x += last_width /* was width(bv, f) */ - TEXT_TO_INSET_OFFSET;
455 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
457 } else if (need_update != INIT)
464 void InsetText::drawFrame(Painter & pain, bool cleared) const
466 if (!frame_is_visible || cleared) {
467 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
468 insetWidth - 1, insetAscent + insetDescent - 1,
470 frame_is_visible = true;
475 void InsetText::clearFrame(Painter & pain, bool cleared) const
477 if (frame_is_visible) {
479 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
480 insetWidth - 1, insetAscent + insetDescent - 1,
483 frame_is_visible = false;
488 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
494 owner()->update(bv, font, true);
497 if (the_locking_inset) {
498 inset_x = cx(bv) - top_x + drawTextXOffset;
499 inset_y = cy(bv) + drawTextYOffset;
500 the_locking_inset->update(bv, font, reinit);
508 int oldw = insetWidth;
509 insetWidth = lt->width + (2 * TEXT_TO_INSET_OFFSET);
510 if (oldw != insetWidth) {
517 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
520 lt->updateInset(bv, the_locking_inset);
522 if (lt->status() == LyXText::NEED_MORE_REFRESH)
529 void InsetText::setUpdateStatus(BufferView * bv, int what) const
537 if (lt->status() == LyXText::NEED_MORE_REFRESH)
539 else if (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)
540 need_update |= CURSOR_PAR;
542 // this to not draw a selection when we redraw all of it!
543 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
544 lt->clearSelection(bv);
550 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
558 setUpdateStatus(bv, what);
559 if ((need_update != CURSOR) || (lt->status() != LyXText::UNCHANGED) ||
562 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
564 bv->owner()->showState();
565 if (old_par != cpar(bv)) {
566 bv->owner()->setLayout(cpar(bv)->getLayout());
574 string const InsetText::editMessage() const
576 return _("Opened Text Inset");
580 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
582 UpdatableInset::edit(bv, x, y, button);
584 if (!bv->lockInset(this)) {
585 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
589 the_locking_inset = 0;
590 inset_pos = inset_x = inset_y = 0;
591 inset_boundary = false;
594 int tmp_y = (y < 0) ? 0 : y;
601 if (!checkAndActivateInset(bv, x, tmp_y, button))
602 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
604 lt->clearSelection(bv);
607 updateLocal(bv, CURSOR, false);
609 // If the inset is empty set the language of the current font to the
610 // language to the surronding text (if different).
611 if (par->size() == 0 && !par->next() &&
612 bv->getParentLanguage(this) != lt->current_font.language()) {
613 LyXFont font(LyXFont::ALL_IGNORE);
614 font.setLanguage(bv->getParentLanguage(this));
615 setFont(bv, font, false);
622 void InsetText::edit(BufferView * bv, bool front)
624 UpdatableInset::edit(bv, front);
626 if (!bv->lockInset(this)) {
627 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
631 the_locking_inset = 0;
632 inset_pos = inset_x = inset_y = 0;
633 inset_boundary = false;
642 lt->setCursor(bv, par, 0);
647 lt->setCursor(bv, p, p->size()-1);
649 lt->clearSelection(bv);
652 updateLocal(bv, CURSOR, false);
654 // If the inset is empty set the language of the current font to the
655 // language to the surronding text (if different).
656 if (par->size() == 0 && !par->next() &&
657 bv->getParentLanguage(this) != lt->current_font.language()) {
658 LyXFont font(LyXFont::ALL_IGNORE);
659 font.setLanguage(bv->getParentLanguage(this));
660 setFont(bv, font, false);
667 void InsetText::insetUnlock(BufferView * bv)
669 if (the_locking_inset) {
670 the_locking_inset->insetUnlock(bv);
671 the_locking_inset = 0;
674 no_selection = false;
677 if (drawFrame_ == LOCKED)
678 code = CURSOR|CLEAR_FRAME;
686 if (lt->selection.set()) {
687 lt->clearSelection(bv);
689 } else if (owner()) {
690 bv->owner()->setLayout(owner()->getLyXText(bv)
691 ->cursor.par()->getLayout());
693 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
694 updateLocal(bv, code, false);
700 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
702 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
706 if (inset == cpar(bv)->getInset(cpos(bv))) {
707 lyxerr[Debug::INSETS] << "OK" << endl;
708 the_locking_inset = inset;
709 inset_x = cx(bv) - top_x + drawTextXOffset;
710 inset_y = cy(bv) + drawTextYOffset;
711 inset_pos = cpos(bv);
712 inset_par = cpar(bv);
713 inset_boundary = cboundary(bv);
714 updateLocal(bv, CURSOR, false);
716 } else if (the_locking_inset && (the_locking_inset == inset)) {
717 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
718 lyxerr[Debug::INSETS] << "OK" << endl;
719 inset_x = cx(bv) - top_x + drawTextXOffset;
720 inset_y = cy(bv) + drawTextYOffset;
722 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
724 } else if (the_locking_inset) {
725 lyxerr[Debug::INSETS] << "MAYBE" << endl;
726 return the_locking_inset->lockInsetInInset(bv, inset);
728 lyxerr[Debug::INSETS] << "NOT OK" << endl;
733 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
736 if (!the_locking_inset)
738 if (the_locking_inset == inset) {
739 the_locking_inset->insetUnlock(bv);
740 getLyXText(bv)->updateInset(bv, inset);
741 the_locking_inset = 0;
743 moveRight(bv, false);
744 old_par = 0; // force layout setting
748 updateLocal(bv, CURSOR, false);
751 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
755 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
757 if (!the_locking_inset)
759 if (the_locking_inset != inset) {
760 getLyXText(bv)->updateInset(bv, the_locking_inset);
761 setUpdateStatus(bv, CURSOR_PAR);
762 return the_locking_inset->updateInsetInInset(bv, inset);
764 // updateLocal(bv, FULL, false);
765 if (getLyXText(bv)->updateInset(bv, inset))
766 updateLocal(bv, CURSOR_PAR, false);
767 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
768 inset_x = cx(bv) - top_x + drawTextXOffset;
769 inset_y = cy(bv) + drawTextYOffset;
775 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
779 int tmp_x = x - drawTextXOffset;
780 int tmp_y = y + insetAscent - getLyXText(bv)->first;
781 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
784 if (the_locking_inset) {
785 if (the_locking_inset == inset) {
786 the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
787 no_selection = false;
790 // otherwise unlock the_locking_inset and lock the new inset
791 the_locking_inset->insetUnlock(bv);
792 inset_x = cx(bv) - top_x + drawTextXOffset;
793 inset_y = cy(bv) + drawTextYOffset;
794 the_locking_inset = static_cast<UpdatableInset*>(inset);
795 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
796 inset->edit(bv, x - inset_x, y - inset_y, button);
797 if (the_locking_inset)
798 updateLocal(bv, CURSOR, false);
799 no_selection = false;
802 // otherwise only unlock the_locking_inset
803 the_locking_inset->insetUnlock(bv);
804 the_locking_inset = 0;
806 if (bv->theLockingInset()) {
807 if (inset && inset->editable() == Inset::HIGHLY_EDITABLE) {
808 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
809 inset_x = cx(bv) - top_x + drawTextXOffset;
810 inset_y = cy(bv) + drawTextYOffset;
811 inset_pos = cpos(bv);
812 inset_par = cpar(bv);
813 inset_boundary = cboundary(bv);
814 the_locking_inset = uinset;
815 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
817 uinset->edit(bv, x - inset_x, y - inset_y, 0);
818 if (the_locking_inset)
819 updateLocal(bv, CURSOR, false);
820 no_selection = false;
824 if (!inset) { // && (button == 2)) {
825 bool paste_internally = false;
826 if ((button == 2) && getLyXText(bv)->selection.set()) {
827 localDispatch(bv, LFUN_COPY, "");
828 paste_internally = true;
830 getLyXText(bv)->setCursorFromCoordinates(bv, x-drawTextXOffset,
832 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
833 updateLocal(bv, CURSOR, false);
834 bv->owner()->setLayout(cpar(bv)->getLayout());
836 // Insert primary selection with middle mouse
837 // if there is a local selection in the current buffer,
840 if (paste_internally)
841 localDispatch(bv, LFUN_PASTE, "");
843 localDispatch(bv, LFUN_PASTESELECTION,
848 no_selection = false;
852 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
854 UpdatableInset * inset = 0;
856 if (the_locking_inset) {
857 the_locking_inset->insetButtonRelease(bv,
858 x - inset_x, y - inset_y,
861 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
862 inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
863 if (inset->editable() == Inset::HIGHLY_EDITABLE) {
864 inset->insetButtonRelease(bv,
866 y - inset_y, button);
868 inset_x = cx(bv) - top_x + drawTextXOffset;
869 inset_y = cy(bv) + drawTextYOffset;
870 inset->insetButtonRelease(bv,
872 y - inset_y, button);
874 x - inset_x, y - inset_y, button);
876 updateLocal(bv, CURSOR_PAR, false);
879 no_selection = false;
883 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
887 if (the_locking_inset) {
888 the_locking_inset->insetMotionNotify(bv, x - inset_x,
892 LyXText * t = getLyXText(bv);
894 t->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
896 if (t->toggle_cursor.par() != t->toggle_end_cursor.par() ||
897 t->toggle_cursor.pos() != t->toggle_end_cursor.pos())
898 updateLocal(bv, SELECTION, false);
903 void InsetText::insetKeyPress(XKeyEvent * xke)
905 if (the_locking_inset) {
906 the_locking_inset->insetKeyPress(xke);
912 UpdatableInset::RESULT
913 InsetText::localDispatch(BufferView * bv,
914 kb_action action, string const & arg)
916 no_selection = false;
917 UpdatableInset::RESULT
918 result= UpdatableInset::localDispatch(bv, action, arg);
919 if (result != UNDISPATCHED) {
924 if ((action < 0) && arg.empty())
927 if (the_locking_inset) {
928 result = the_locking_inset->localDispatch(bv, action, arg);
929 if (result == DISPATCHED_NOUPDATE)
931 else if (result == DISPATCHED) {
932 updateLocal(bv, CURSOR_PAR, false);
934 } else if (result == FINISHED) {
935 bool dispatched = false;
937 case LFUN_UNKNOWN_ACTION:
938 case LFUN_BREAKPARAGRAPH:
940 moveRightIntern(bv, false, false);
943 if (!getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
944 moveRightIntern(bv, false, false);
948 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
949 moveRightIntern(bv, false, false);
955 the_locking_inset = 0;
968 case LFUN_SELFINSERT:
969 if (bv->buffer()->isReadonly()) {
970 // setErrorMessage(N_("Document is read only"));
974 /* Automatically delete the currently selected
975 * text and replace it with what is being
976 * typed in now. Depends on lyxrc settings
977 * "auto_region_delete", which defaults to
980 setUndo(bv, Undo::INSERT,
981 lt->cursor.par(), lt->cursor.par()->next());
983 if (lyxrc.auto_region_delete) {
984 if (lt->selection.set()) {
985 lt->cutSelection(bv, false);
988 lt->clearSelection(bv);
989 for (string::size_type i = 0; i < arg.length(); ++i) {
991 if (!math_insert_greek(bv, arg[i])) {
992 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
993 } else if (!the_locking_inset) {
994 (void)moveRight(bv, false);
997 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1001 lt->selection.cursor = lt->cursor;
1002 updateLocal(bv, CURSOR_PAR, true);
1003 result = DISPATCHED_NOUPDATE;
1005 // --- Cursor Movements -----------------------------------
1008 moveRight(bv, false, true);
1009 lt->setSelection(bv);
1010 updateLocal(bv, SELECTION, false);
1013 result = moveRight(bv);
1015 updateLocal(bv, CURSOR, false);
1019 moveLeft(bv, false, true);
1020 lt->setSelection(bv);
1021 updateLocal(bv, SELECTION, false);
1025 result = moveLeft(bv);
1026 updateLocal(bv, CURSOR, false);
1031 lt->setSelection(bv);
1032 updateLocal(bv, SELECTION, false);
1036 result = moveDown(bv);
1037 updateLocal(bv, CURSOR, false);
1042 lt->setSelection(bv);
1043 updateLocal(bv, SELECTION, false);
1047 result = moveUp(bv);
1048 updateLocal(bv, CURSOR, false);
1053 updateLocal(bv, CURSOR, false);
1057 updateLocal(bv, CURSOR, false);
1059 case LFUN_BACKSPACE: {
1060 setUndo(bv, Undo::DELETE,
1061 lt->cursor.par(), lt->cursor.par()->next());
1062 if (lt->selection.set())
1063 lt->cutSelection(bv);
1066 updateLocal(bv, CURSOR_PAR, true);
1071 setUndo(bv, Undo::DELETE,
1072 lt->cursor.par(), lt->cursor.par()->next());
1073 if (lt->selection.set()) {
1074 lt->cutSelection(bv);
1078 updateLocal(bv, CURSOR_PAR, true);
1083 setUndo(bv, Undo::DELETE,
1084 lt->cursor.par(), lt->cursor.par()->next());
1085 lt->cutSelection(bv);
1086 updateLocal(bv, CURSOR_PAR, true);
1092 lt->copySelection(bv);
1093 updateLocal(bv, CURSOR_PAR, false);
1095 case LFUN_PASTESELECTION:
1097 string const clip(bv->getClipboard());
1101 if (arg == "paragraph") {
1102 lt->insertStringAsParagraphs(bv, clip);
1104 lt->insertStringAsLines(bv, clip);
1106 updateLocal(bv, CURSOR_PAR, true);
1110 if (!autoBreakRows) {
1112 if (CutAndPaste::nrOfParagraphs() > 1) {
1113 WriteAlert(_("Impossible operation"),
1114 _("Cannot include more than one paragraph!"),
1119 setUndo(bv, Undo::INSERT,
1120 lt->cursor.par(), lt->cursor.par()->next());
1121 lt->pasteSelection(bv);
1122 updateLocal(bv, CURSOR_PAR, true);
1126 case LFUN_BREAKPARAGRAPH:
1127 if (!autoBreakRows) {
1128 result = DISPATCHED;
1131 lt->breakParagraph(bv, 0);
1132 updateLocal(bv, FULL, true);
1134 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1135 if (!autoBreakRows) {
1136 result = DISPATCHED;
1139 lt->breakParagraph(bv, 1);
1140 updateLocal(bv, FULL, true);
1143 case LFUN_BREAKLINE: {
1144 if (!autoBreakRows) {
1145 result = DISPATCHED;
1148 setUndo(bv, Undo::INSERT,
1149 lt->cursor.par(), lt->cursor.par()->next());
1150 lt->insertChar(bv, Paragraph::META_NEWLINE);
1151 updateLocal(bv, CURSOR_PAR, true);
1156 // do not set layouts on non breakable textinsets
1157 if (autoBreakRows) {
1158 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1160 // Derive layout number from given argument (string)
1161 // and current buffer's textclass (number). */
1162 LyXTextClassList::ClassList::size_type tclass =
1163 bv->buffer()->params.textclass;
1164 std::pair <bool, LyXTextClass::size_type> layout =
1165 textclasslist.NumberOfLayout(tclass, arg);
1167 // If the entry is obsolete, use the new one instead.
1169 string obs = textclasslist.Style(tclass,layout.second).
1172 layout = textclasslist.NumberOfLayout(tclass, obs);
1175 // see if we found the layout number:
1176 if (!layout.first) {
1177 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1178 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1182 if (cur_layout != layout.second) {
1183 cur_layout = layout.second;
1184 lt->setLayout(bv, layout.second);
1185 bv->owner()->setLayout(cpar(bv)->getLayout());
1186 updateLocal(bv, CURSOR_PAR, true);
1189 // reset the layout box
1190 bv->owner()->setLayout(cpar(bv)->getLayout());
1193 case LFUN_PARAGRAPH_SPACING:
1194 // This one is absolutely not working. When fiddling with this
1195 // it also seems to me that the paragraphs inside the insettext
1196 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1198 Paragraph * par = lt->cursor.par();
1199 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1200 float cur_value = 1.0;
1201 if (cur_spacing == Spacing::Other) {
1202 cur_value = par->params().spacing().getValue();
1205 istringstream istr(arg.c_str());
1208 Spacing::Space new_spacing = cur_spacing;
1209 float new_value = cur_value;
1211 lyxerr << "Missing argument to `paragraph-spacing'"
1213 } else if (tmp == "single") {
1214 new_spacing = Spacing::Single;
1215 } else if (tmp == "onehalf") {
1216 new_spacing = Spacing::Onehalf;
1217 } else if (tmp == "double") {
1218 new_spacing = Spacing::Double;
1219 } else if (tmp == "other") {
1220 new_spacing = Spacing::Other;
1223 lyxerr << "new_value = " << tmpval << endl;
1226 } else if (tmp == "default") {
1227 new_spacing = Spacing::Default;
1229 lyxerr << _("Unknown spacing argument: ")
1232 if (cur_spacing != new_spacing || cur_value != new_value) {
1233 par->params().spacing(Spacing(new_spacing, new_value));
1234 updateLocal(bv, CURSOR_PAR, true);
1240 if (!bv->Dispatch(action, arg))
1241 result = UNDISPATCHED;
1245 /// If the action has deleted all text in the inset, we need to change the
1246 // language to the language of the surronding text.
1247 if (par->size() == 0 && !par->next()) {
1248 LyXFont font(LyXFont::ALL_IGNORE);
1249 font.setLanguage(bv->getParentLanguage(this));
1250 setFont(bv, font, false);
1253 if (result != FINISHED) {
1254 showInsetCursor(bv);
1256 bv->unlockInset(this);
1263 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1266 buf->latexParagraphs(os, par, 0, texrow);
1267 return texrow.rows();
1271 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1273 Paragraph * p = par;
1274 unsigned int lines = 0;
1277 string const tmp = buf->asciiParagraph(p, linelen);
1278 lines += countChar(tmp, '\n');
1286 int InsetText::docBook(Buffer const * buf, ostream & os) const
1288 Paragraph * p = par;
1289 unsigned int lines = 0;
1294 buf->simpleDocBookOnePar(os, tmp, p, desc, 0);
1302 void InsetText::validate(LaTeXFeatures & features) const
1304 Paragraph * p = par;
1306 p->validate(features);
1312 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1314 if (textclasslist.Style(buf->params.textclass,
1315 p->getLayout()).labeltype != LABEL_MANUAL)
1318 return p->beginningOfMainBody();
1322 void InsetText::getCursorPos(BufferView * bv,
1323 int & x, int & y) const
1325 if (the_locking_inset) {
1326 the_locking_inset->getCursorPos(bv, x, y);
1334 unsigned int InsetText::insetInInsetY()
1336 if (!the_locking_inset)
1339 return (inset_y + the_locking_inset->insetInInsetY());
1343 void InsetText::toggleInsetCursor(BufferView * bv)
1345 if (the_locking_inset) {
1346 the_locking_inset->toggleInsetCursor(bv);
1350 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1352 int const asc = lyxfont::maxAscent(font);
1353 int const desc = lyxfont::maxDescent(font);
1355 if (isCursorVisible())
1356 bv->hideLockedInsetCursor();
1358 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1359 toggleCursorVisible();
1363 void InsetText::showInsetCursor(BufferView * bv, bool show)
1365 if (the_locking_inset) {
1366 the_locking_inset->showInsetCursor(bv, show);
1369 if (!isCursorVisible()) {
1370 LyXFont const font =
1371 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1373 int const asc = lyxfont::maxAscent(font);
1374 int const desc = lyxfont::maxDescent(font);
1376 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1378 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1379 setCursorVisible(true);
1384 void InsetText::hideInsetCursor(BufferView * bv)
1386 if (isCursorVisible()) {
1387 bv->hideLockedInsetCursor();
1388 setCursorVisible(false);
1390 if (the_locking_inset)
1391 the_locking_inset->hideInsetCursor(bv);
1395 void InsetText::fitInsetCursor(BufferView * bv) const
1397 if (the_locking_inset) {
1398 the_locking_inset->fitInsetCursor(bv);
1401 LyXFont const font =
1402 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1404 int const asc = lyxfont::maxAscent(font);
1405 int const desc = lyxfont::maxDescent(font);
1407 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1411 UpdatableInset::RESULT
1412 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1414 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1415 return moveLeftIntern(bv, false, activate_inset, selecting);
1417 return moveRightIntern(bv, false, activate_inset, selecting);
1421 UpdatableInset::RESULT
1422 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1424 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1425 return moveRightIntern(bv, true, activate_inset, selecting);
1427 return moveLeftIntern(bv, true, activate_inset, selecting);
1431 UpdatableInset::RESULT
1432 InsetText::moveRightIntern(BufferView * bv, bool behind,
1433 bool activate_inset, bool selecting)
1435 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1437 if (activate_inset && checkAndActivateInset(bv, behind))
1439 getLyXText(bv)->cursorRight(bv);
1441 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1442 return DISPATCHED_NOUPDATE;
1446 UpdatableInset::RESULT
1447 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1448 bool activate_inset, bool selecting)
1450 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1452 getLyXText(bv)->cursorLeft(bv);
1454 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1455 if (activate_inset && checkAndActivateInset(bv, behind))
1457 return DISPATCHED_NOUPDATE;
1461 UpdatableInset::RESULT
1462 InsetText::moveUp(BufferView * bv)
1464 if (!crow(bv)->previous())
1466 getLyXText(bv)->cursorUp(bv);
1467 return DISPATCHED_NOUPDATE;
1471 UpdatableInset::RESULT
1472 InsetText::moveDown(BufferView * bv)
1474 if (!crow(bv)->next())
1476 getLyXText(bv)->cursorDown(bv);
1477 return DISPATCHED_NOUPDATE;
1481 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1483 if (the_locking_inset) {
1484 if (the_locking_inset->insetAllowed(inset))
1485 return the_locking_inset->insertInset(bv, inset);
1488 inset->setOwner(this);
1489 hideInsetCursor(bv);
1493 lt = getLyXText(bv);
1496 lt->insertInset(bv, inset);
1497 if ((cpar(bv)->getChar(cpos(bv)) != Paragraph::META_INSET) ||
1498 (cpar(bv)->getInset(cpos(bv)) != inset))
1501 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1502 showInsetCursor(bv);
1509 bool InsetText::insetAllowed(Inset::Code code) const
1511 if (the_locking_inset)
1512 return the_locking_inset->insetAllowed(code);
1517 UpdatableInset * InsetText::getLockingInset() const
1519 return the_locking_inset ? the_locking_inset->getLockingInset() :
1520 const_cast<InsetText *>(this);
1524 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1528 if (the_locking_inset)
1529 return the_locking_inset->getFirstLockingInsetOfType(c);
1534 bool InsetText::showInsetDialog(BufferView * bv) const
1536 if (the_locking_inset)
1537 return the_locking_inset->showInsetDialog(bv);
1542 std::vector<string> const InsetText::getLabelList() const
1544 std::vector<string> label_list;
1546 Paragraph * tpar = par;
1548 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1549 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1550 for (; beg != end; ++beg) {
1551 std::vector<string> const l = (*beg)->getLabelList();
1552 label_list.insert(label_list.end(), l.begin(), l.end());
1554 tpar = tpar->next();
1560 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1563 if (the_locking_inset) {
1564 the_locking_inset->setFont(bv, font, toggleall, selectall);
1569 lt = getLyXText(bv);
1572 if (lt->selection.set()) {
1573 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1578 lt->toggleFree(bv, font, toggleall);
1580 lt->setFont(bv, font, toggleall);
1583 lt->clearSelection(bv);
1585 if (selectall || lt->selection.set())
1586 updateLocal(bv, FULL, true);
1588 updateLocal(bv, CURSOR_PAR, true);
1594 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1596 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
1600 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1601 if (!inset || inset->editable() != Inset::HIGHLY_EDITABLE)
1603 LyXFont const font =
1604 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1606 x = inset->width(bv, font);
1607 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1610 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1612 //inset_x = cx(bv) - top_x + drawTextXOffset;
1613 //inset_y = cy(bv) + drawTextYOffset;
1614 inset->edit(bv, x, y, 0);
1615 if (!the_locking_inset)
1617 updateLocal(bv, CURSOR, false);
1624 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1627 x -= drawTextXOffset;
1629 int dummyy = y + insetAscent;
1630 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1637 inset_x = cx(bv) - top_x + drawTextXOffset;
1638 inset_y = cy(bv) + drawTextYOffset;
1639 inset->edit(bv, x - inset_x, y - inset_y, button);
1640 if (!the_locking_inset)
1642 updateLocal(bv, CURSOR, false);
1649 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1651 int w = UpdatableInset::getMaxWidth(bv, inset);
1656 w = w - top_x + owner()->x();
1659 w -= (2 * TEXT_TO_INSET_OFFSET);
1661 // return w - (2*TEXT_TO_INSET_OFFSET);
1665 void InsetText::setParagraphData(Paragraph * p)
1669 // now also delete all caches this should be safe, hopefully
1673 Paragraph * tmp = par->next();
1678 par = new Paragraph(*p);
1679 par->setInsetOwner(this);
1680 Paragraph * np = par;
1683 np->next(new Paragraph(*p));
1684 np->next()->previous(np);
1686 np->setInsetOwner(this);
1693 void InsetText::setText(string const & data)
1696 LyXFont font(LyXFont::ALL_SANE);
1697 for (unsigned int i=0; i < data.length(); ++i)
1698 par->insertChar(i, data[i], font);
1702 void InsetText::setAutoBreakRows(bool flag)
1704 if (flag != autoBreakRows) {
1705 autoBreakRows = flag;
1713 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1715 if (how != drawFrame_) {
1718 updateLocal(bv, DRAW_FRAME, false);
1723 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1725 if (frame_color != col) {
1728 updateLocal(bv, DRAW_FRAME, false);
1733 int InsetText::cx(BufferView * bv) const
1737 lt = getLyXText(bv);
1740 int x = lt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1741 if (the_locking_inset) {
1742 LyXFont font = lt->getFont(bv->buffer(),
1745 if (font.isVisibleRightToLeft())
1746 x -= the_locking_inset->width(bv, font);
1754 int InsetText::cy(BufferView * bv) const
1757 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1761 Paragraph::size_type InsetText::cpos(BufferView * bv) const
1763 return getLyXText(bv)->cursor.pos();
1767 Paragraph * InsetText::cpar(BufferView * bv) const
1769 return getLyXText(bv)->cursor.par();
1773 bool InsetText::cboundary(BufferView * bv) const
1775 return getLyXText(bv)->cursor.boundary();
1779 Row * InsetText::crow(BufferView * bv) const
1781 return getLyXText(bv)->cursor.row();
1785 LyXText * InsetText::getLyXText(BufferView const * lbv,
1786 bool const recursive) const
1788 if (!recursive && (cached_bview == lbv))
1789 return cached_text.get();
1791 // Super UGLY! (Lgb)
1792 BufferView * bv = const_cast<BufferView *>(lbv);
1795 Cache::iterator it = cache.find(bv);
1797 if (it != cache.end()) {
1798 if (lt || !it->second.remove) {
1799 lyx::Assert(it->second.text.get());
1800 cached_text = it->second.text;
1801 if (recursive && the_locking_inset) {
1802 return the_locking_inset->getLyXText(bv);
1804 return cached_text.get();
1805 } else if (it->second.remove) {
1807 saveLyXTextState(it->second.text.get());
1814 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
1815 cached_text->init(bv);
1816 restoreLyXTextState(bv, cached_text.get());
1818 cache.insert(make_pair(bv, cached_text));
1820 if (the_locking_inset && recursive) {
1821 return the_locking_inset->getLyXText(bv);
1823 return cached_text.get();
1827 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1831 Cache::iterator it = cache.find(bv);
1833 if (it == cache.end()) {
1837 lyx::Assert(it->second.text.get());
1839 it->second.remove = true;
1841 /// then remove all LyXText in text-insets
1842 Paragraph * p = par;
1843 for (; p; p = p->next()) {
1844 p->deleteInsetsLyXText(bv);
1850 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1852 if (!par->next() && !par->size()) // no data, resize not neccessary!
1854 // one endless line, resize normally not necessary
1855 if (!force && getMaxWidth(bv, this) < 0)
1858 Cache::iterator it = cache.find(bv);
1859 if (it == cache.end()) {
1862 lyx::Assert(it->second.text.get());
1864 LyXText * t = it->second.text.get();
1865 saveLyXTextState(t);
1866 for (Paragraph * p = par; p; p = p->next()) {
1867 p->resizeInsetsLyXText(bv);
1870 restoreLyXTextState(bv, t);
1871 if (the_locking_inset) {
1872 inset_x = cx(bv) - top_x + drawTextXOffset;
1873 inset_y = cy(bv) + drawTextYOffset;
1877 t->first = bv->screen()->topCursorVisible(t);
1880 updateLocal(bv, FULL, false);
1883 // this will scroll the screen such that the cursor becomes visible
1884 bv->updateScrollbar();
1888 void InsetText::removeNewlines()
1890 for (Paragraph * p = par; p; p = p->next()) {
1891 for (int i = 0; i < p->size(); ++i) {
1892 if (p->getChar(i) == Paragraph::META_NEWLINE)
1899 bool InsetText::nodraw() const
1901 if (the_locking_inset)
1902 return the_locking_inset->nodraw();
1903 return UpdatableInset::nodraw();
1907 int InsetText::scroll(bool recursive) const
1909 int sx = UpdatableInset::scroll(false);
1911 if (recursive && the_locking_inset)
1912 sx += the_locking_inset->scroll(recursive);
1918 bool InsetText::doClearArea() const
1920 return !locked || (need_update & (FULL|INIT));
1924 void InsetText::selectAll(BufferView * bv)
1926 getLyXText(bv)->cursorTop(bv);
1927 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1928 getLyXText(bv)->cursorBottom(bv);
1929 getLyXText(bv)->setSelection(bv);
1933 void InsetText::clearSelection(BufferView * bv)
1935 getLyXText(bv)->clearSelection(bv);
1939 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
1942 int h = insetAscent + insetDescent;
1943 int ty = baseline - insetAscent;
1949 if ((ty + h) > pain.paperHeight())
1950 h = pain.paperHeight();
1951 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
1952 w = pain.paperWidth();
1953 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h,
1960 Paragraph * InsetText::getParFromID(int id) const
1962 Paragraph * result = par;
1963 Paragraph * ires = 0;
1964 while (result && result->id() != id) {
1965 if ((ires = result->getParFromID(id)))
1967 result = result->next();
1973 Paragraph * InsetText::firstParagraph() const
1976 if (the_locking_inset)
1977 if ((result = the_locking_inset->firstParagraph()))
1983 LyXCursor const & InsetText::cursor(BufferView * bv) const
1985 if (the_locking_inset)
1986 return the_locking_inset->cursor(bv);
1987 return getLyXText(bv)->cursor;
1991 Paragraph * InsetText::paragraph() const
1997 void InsetText::paragraph(Paragraph * p)
2001 // we now have to update/redraw all instances
2002 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
2007 // redraw myself when asked for
2008 need_update |= INIT;
2012 Inset * InsetText::getInsetFromID(int id_arg) const
2015 return const_cast<InsetText *>(this);
2017 Paragraph * lp = par;
2020 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2021 en = lp->inset_iterator_end();
2024 if ((*it)->id() == id_arg)
2026 Inset * in = (*it)->getInsetFromID(id_arg);
2036 string const InsetText::selectNextWord(BufferView * bv, float & value) const
2042 lt = getLyXText(bv);
2045 if (the_locking_inset) {
2046 str = the_locking_inset->selectNextWord(bv, value);
2053 #warning Dekel please have a look on this one RTL? (Jug)
2054 // we have to go on checking so move cusor to the right
2055 lt->cursor.pos(lt->cursor.pos() + 1);
2057 str = lt->selectNextWord(bv, value);
2059 bv->unlockInset(const_cast<InsetText *>(this));
2068 void InsetText::selectSelectedWord(BufferView * bv)
2070 if (the_locking_inset) {
2071 the_locking_inset->selectSelectedWord(bv);
2074 getLyXText(bv)->selectSelectedWord(bv);
2075 updateLocal(bv, SELECTION, false);
2079 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2081 if (the_locking_inset) {
2082 the_locking_inset->toggleSelection(bv, kill_selection);
2086 lt = getLyXText(bv);
2090 int x = top_x + TEXT_TO_INSET_OFFSET;
2093 Row * row = lt->getRowNearY(y);
2094 int y_offset = top_baseline - row->ascent_of_text();
2096 while ((row != 0) && ((y+row->height()) <= 0)) {
2103 if (need_update & SELECTION)
2105 bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2111 bool InsetText::searchForward(BufferView * bv, string const & str,
2112 bool const & cs, bool const & mw)
2114 if (the_locking_inset)
2115 if (the_locking_inset->searchForward(bv, str, cs, mw))
2117 if (LyXFind(bv, str, true, true, cs , mw)) {
2120 // we have to unlock ourself in this function by default!
2121 bv->unlockInset(const_cast<InsetText *>(this));
2125 bool InsetText::searchBackward(BufferView * bv, string const & str,
2126 bool const & cs, bool const & mw)
2128 if (the_locking_inset)
2129 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2131 if (LyXFind(bv, str, false, true, cs, mw)) {
2134 // we have to unlock ourself in this function by default!
2135 bv->unlockInset(const_cast<InsetText *>(this));