1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2002 The LyX Team.
9 * ====================================================== */
14 #include "paragraph.h"
15 #include "BufferView.h"
16 #include "funcrequest.h"
20 #include "bufferparams.h"
22 #include "bufferview_funcs.h"
23 #include "ParagraphParameters.h"
29 #include "support/tostr.h"
30 #include "support/lstrings.h"
31 #include "support/LAssert.h"
32 #include "frontends/LyXView.h"
33 #include "frontends/screen.h"
34 #include "frontends/Dialogs.h"
35 #include "insets/insetspecialchar.h"
36 #include "insets/insettext.h"
37 #include "insets/insetquotes.h"
38 #include "insets/insetcommand.h"
39 #include "insets/insetnewline.h"
40 #include "undo_funcs.h"
41 #include "text_funcs.h"
47 using namespace lyx::support;
48 using namespace bv_funcs;
55 extern string current_layout;
56 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
58 // the selection possible is needed, that only motion events are
59 // used, where the bottom press event was on the drawing area too
60 bool selection_possible = false;
65 void moveCursorUpdate(BufferView * bv, bool selecting)
67 LyXText * lt = bv->getLyXText();
69 //if (!lt->selection.set())
70 // lt->selection.cursor = lt->cursor;
72 if (selecting || lt->selection.mark())
75 if (!lt->selection.set())
76 bv->haveSelection(false);
83 void finishChange(BufferView * bv, bool selecting = false)
86 moveCursorUpdate(bv, selecting);
87 bv->owner()->view_state_changed();
90 // check if the given co-ordinates are inside an inset at the
91 // given cursor, if one exists. If so, the inset is returned,
92 // and the co-ordinates are made relative. Otherwise, 0 is returned.
93 InsetOld * checkInset(BufferView * /*bv*/, LyXText & text,
94 LyXCursor const & cur, int & x, int & y)
96 lyx::pos_type const pos = cur.pos();
97 ParagraphList::iterator par = cur.par();
99 if (pos >= par->size() || !par->isInset(pos))
102 InsetOld /*const*/ * inset = par->getInset(pos);
104 if (!isEditableInset(inset))
107 // get inset dimensions
108 Assert(par->getInset(pos));
110 LyXFont const & font = text.getFont(par, pos);
112 int const width = inset->width();
113 int const inset_x = font.isVisibleRightToLeft()
114 ? (cur.ix() - width) : cur.ix();
117 inset_x + inset->scroll(),
119 cur.iy() - inset->ascent(),
120 cur.iy() + inset->descent()
123 if (!b.contained(x, y)) {
124 lyxerr[Debug::GUI] << "Missed inset at x,y "
126 << " box " << b << endl;
130 text.setCursor(par, pos, true);
133 // The origin of an inset is on the baseline
134 y -= text.cursor.iy();
142 InsetOld * LyXText::checkInsetHit(int & x, int & y)
144 int y_tmp = y + top_y();
147 setCursorFromCoordinates(cur, x, y_tmp);
149 InsetOld * inset = checkInset(bv(), *this, cur, x, y_tmp);
155 // look at previous position
160 setCursor(cur, cur.par(), cur.pos() - 1, true);
162 inset = checkInset(bv(), *this, cur, x, y_tmp);
169 bool LyXText::gotoNextInset(vector<InsetOld::Code> const & codes,
170 string const & contents)
172 ParagraphList::iterator end = ownerParagraphs().end();
173 ParagraphList::iterator pit = cursor.par();
174 pos_type pos = cursor.pos();
178 if (pos + 1 < pit->size()) {
185 } while (pit != end &&
186 !(pit->isInset(pos) &&
187 (inset = pit->getInset(pos)) != 0 &&
188 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
190 static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
194 setCursor(pit, pos, false);
201 void LyXText::gotoInset(vector<InsetOld::Code> const & codes,
204 bv()->beforeChange(this);
207 if (same_content && cursor.pos() < cursor.par()->size()
208 && cursor.par()->isInset(cursor.pos())) {
209 InsetOld const * inset = cursor.par()->getInset(cursor.pos());
210 if (find(codes.begin(), codes.end(), inset->lyxCode())
212 contents = static_cast<InsetCommand const *>(inset)->getContents();
215 if (!gotoNextInset(codes, contents)) {
216 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
217 LyXCursor tmp = cursor;
218 cursor.par(ownerParagraphs().begin());
220 if (!gotoNextInset(codes, contents)) {
222 bv()->owner()->message(_("No more insets"));
225 bv()->owner()->message(_("No more insets"));
229 selection.cursor = cursor;
233 void LyXText::gotoInset(InsetOld::Code code, bool same_content)
235 gotoInset(vector<InsetOld::Code>(1, code), same_content);
239 void LyXText::cursorPrevious()
243 if (cursorRow() == rows().begin()) {
245 bv()->updateScrollbar();
249 RowList::iterator cursorrow = cursorRow();
251 setCursorFromCoordinates(cursor.x_fix(), y);
255 if (cursorrow == bv()->text->cursorRow()) {
256 // we have a row which is taller than the workarea. The
257 // simplest solution is to move to the previous row instead.
260 // This is what we used to do, so we wouldn't skip right past
261 // tall rows, but it's not working right now.
264 new_y = bv()->text->cursor.iy()
265 + bv()->theLockingInset()->insetInInsetY() + y
266 + cursorRow()->height()
267 - bv()->workHeight() + 1;
270 - cursorRow()->baseline()
271 + cursorRow()->height()
272 - bv()->workHeight() + 1;
275 //bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
276 if (cursorRow() != rows().begin()) {
278 setCursor(cur, getPar(boost::prior(cursorRow())),
279 boost::prior(cursorRow())->pos(), false);
280 if (cur.y() > top_y()) {
284 bv()->updateScrollbar();
288 void LyXText::cursorNext()
292 if (boost::next(cursorRow()) == rows().end()) {
293 int y = cursor.y() - cursorRow()->baseline() +
294 cursorRow()->height();
295 if (y > topy + bv()->workHeight()) {
296 //bv()->screen().draw(bv()->text, bv(), bv()->text->top_y() + bv()->workHeight());
297 bv()->updateScrollbar();
302 int y = topy + bv()->workHeight();
303 if (inset_owner && !topy) {
304 y -= (bv()->text->cursor.iy()
305 - bv()->text->top_y()
306 + bv()->theLockingInset()->insetInInsetY());
311 RowList::iterator cursorrow = cursorRow();
312 setCursorFromCoordinates(cursor.x_fix(), y);
313 // + bv->workHeight());
317 if (cursorrow == bv()->text->cursorRow()) {
318 // we have a row which is taller than the workarea. The
319 // simplest solution is to move to the next row instead.
322 // This is what we used to do, so we wouldn't skip right past
323 // tall rows, but it's not working right now.
325 new_y = bv->text->top_y() + bv->workHeight();
329 new_y = bv()->text->cursor.iy()
330 + bv()->theLockingInset()->insetInInsetY()
331 + y - cursorRow()->baseline();
333 new_y = cursor.y() - cursorRow()->baseline();
336 //bv()->screen().draw(bv()->text, bv(), new_y);
338 RowList::iterator next_row = boost::next(cursorRow());
339 if (next_row != rows().end()) {
341 setCursor(cur, getPar(next_row), next_row->pos(), false);
342 if (cur.y() < top_y() + bv()->workHeight()) {
346 bv()->updateScrollbar();
352 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
355 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
356 replaceSelection(lt);
357 if (!bv->insertInset(new_inset))
364 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
365 bool edit, bool pastesel)
367 InsetOld * inset = createInset(cmd);
368 BufferView * bv = cmd.view();
372 if (lt->selection.set()) {
373 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
376 if (bv->insertInset(inset)) {
378 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
379 inset->localDispatch(cmd);
381 if (gotsel && pastesel)
382 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
391 InsetOld::RESULT LyXText::dispatch(FuncRequest const & cmd)
393 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
394 <<"] arg[" << cmd.argument << ']' << endl;
396 BufferView * bv = cmd.view();
398 switch (cmd.action) {
400 case LFUN_APPENDIX: {
401 ParagraphList::iterator pit = cursor.par();
402 bool start = !pit->params().startOfAppendix();
404 // ensure that we have only one start_of_appendix in this document
405 ParagraphList::iterator tmp = ownerParagraphs().begin();
406 ParagraphList::iterator end = ownerParagraphs().end();
408 for (; tmp != end; ++tmp) {
409 if (tmp->params().startOfAppendix()) {
410 recordUndo(bv, Undo::ATOMIC, tmp);
411 tmp->params().startOfAppendix(false);
412 setHeightOfRow(tmp, getRow(tmp, 0));
417 recordUndo(bv, Undo::ATOMIC, pit);
418 pit->params().startOfAppendix(start);
420 // we can set the refreshing parameters now
422 redoParagraph(cursor.par());
423 setCursor(cursor.par(), cursor.pos());
428 case LFUN_DELETE_WORD_FORWARD:
429 bv->beforeChange(this);
434 case LFUN_DELETE_WORD_BACKWARD:
435 bv->beforeChange(this);
436 deleteWordBackward();
440 case LFUN_DELETE_LINE_FORWARD:
441 bv->beforeChange(this);
447 if (!selection.mark())
448 bv->beforeChange(this);
449 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
452 cursorRightOneWord();
457 if (!selection.mark())
458 bv->beforeChange(this);
459 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
460 cursorRightOneWord();
466 case LFUN_BEGINNINGBUF:
467 if (!selection.mark())
468 bv->beforeChange(this);
474 if (selection.mark())
475 bv->beforeChange(this);
481 if (!selection.set())
482 selection.cursor = cursor;
483 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
487 finishChange(bv, true);
491 if (!selection.set())
492 selection.cursor = cursor;
493 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
497 finishChange(bv, true);
501 if (!selection.set())
502 selection.cursor = cursor;
504 finishChange(bv, true);
508 if (!selection.set())
509 selection.cursor = cursor;
511 finishChange(bv, true);
514 case LFUN_UP_PARAGRAPHSEL:
515 if (!selection.set())
516 selection.cursor = cursor;
518 finishChange(bv, true);
521 case LFUN_DOWN_PARAGRAPHSEL:
522 if (!selection.set())
523 selection.cursor = cursor;
524 cursorDownParagraph();
525 finishChange(bv, true);
529 if (!selection.set())
530 selection.cursor = cursor;
532 finishChange(bv, true);
536 if (!selection.set())
537 selection.cursor = cursor;
539 finishChange(bv, true);
543 if (!selection.set())
544 selection.cursor = cursor;
546 finishChange(bv, true);
550 if (!selection.set())
551 selection.cursor = cursor;
553 finishChange(bv, true);
556 case LFUN_WORDRIGHTSEL:
557 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
560 cursorRightOneWord();
561 finishChange(bv, true);
564 case LFUN_WORDLEFTSEL:
565 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
566 cursorRightOneWord();
569 finishChange(bv, true);
573 LyXCursor cur1 = cursor;
575 ::getWord(cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs());
576 setCursor(cur1.par(), cur1.pos());
577 bv->beforeChange(this);
578 setCursor(cur2.par(), cur2.pos());
579 finishChange(bv, true);
584 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
585 if (!selection.mark())
586 bv->beforeChange(this);
589 if (cursor.pos() < cursor.par()->size()
590 && cursor.par()->isInset(cursor.pos())
591 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
592 InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
593 cmd.message(tmpinset->editMessage());
594 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
595 tmpinset->localDispatch(cmd1);
605 // This is soooo ugly. Isn`t it possible to make
607 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
608 if (!selection.mark())
609 bv->beforeChange(this);
610 LyXCursor const cur = cursor;
613 if ((is_rtl || cur != cursor) && // only if really moved!
614 cursor.pos() < cursor.par()->size() &&
615 cursor.par()->isInset(cursor.pos()) &&
616 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
617 InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
618 cmd.message(tmpinset->editMessage());
619 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
620 tmpinset->localDispatch(cmd1);
630 if (!selection.mark())
631 bv->beforeChange(this);
637 if (!selection.mark())
638 bv->beforeChange(this);
643 case LFUN_UP_PARAGRAPH:
644 if (!selection.mark())
645 bv->beforeChange(this);
650 case LFUN_DOWN_PARAGRAPH:
651 if (!selection.mark())
652 bv->beforeChange(this);
653 cursorDownParagraph();
654 finishChange(bv, false);
658 if (!selection.mark())
659 bv->beforeChange(this);
661 finishChange(bv, false);
665 if (!selection.mark())
666 bv->beforeChange(this);
668 finishChange(bv, false);
672 if (!selection.mark())
673 bv->beforeChange(this);
675 finishChange(bv, false);
679 if (!selection.mark())
680 bv->beforeChange(this);
682 finishChange(bv, false);
685 case LFUN_BREAKLINE: {
686 lyx::pos_type body = cursor.par()->beginningOfBody();
688 // Not allowed by LaTeX (labels or empty par)
689 if (cursor.pos() <= body)
692 replaceSelection(bv->getLyXText());
693 insertInset(new InsetNewline);
694 setCursor(cursor.par(), cursor.pos());
695 moveCursorUpdate(bv, false);
700 if (!selection.set()) {
702 selection.cursor = cursor;
703 // It is possible to make it a lot faster still
704 // just comment out the line below...
706 cutSelection(true, false);
708 moveCursorUpdate(bv, false);
709 bv->owner()->view_state_changed();
712 case LFUN_DELETE_SKIP:
713 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
714 if (!selection.set()) {
715 LyXCursor cur = cursor;
716 if (cur.pos() == cur.par()->size()) {
720 && !(cur.par()->params().spaceTop()
721 == VSpace (VSpace::NONE))) {
723 cur.par()->params().lineTop(),
724 cur.par()->params().lineBottom(),
725 cur.par()->params().pagebreakTop(),
726 cur.par()->params().pagebreakBottom(),
727 VSpace(VSpace::NONE),
728 cur.par()->params().spaceBottom(),
729 cur.par()->params().spacing(),
730 cur.par()->params().align(),
731 cur.par()->params().labelWidthString(), 0);
736 selection.cursor = cursor;
740 selection.cursor = cursor;
743 cutSelection(true, false);
750 if (!selection.set()) {
751 if (bv->owner()->getIntl().getTransManager().backspace()) {
753 selection.cursor = cursor;
754 // It is possible to make it a lot faster still
755 // just comment out the line below...
758 cutSelection(true, false);
760 bv->owner()->view_state_changed();
765 case LFUN_BACKSPACE_SKIP:
766 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
767 if (!selection.set()) {
768 LyXCursor cur = cursor;
770 && !(cur.par()->params().spaceTop() == VSpace(VSpace::NONE))) {
772 cur.par()->params().lineTop(),
773 cur.par()->params().lineBottom(),
774 cur.par()->params().pagebreakTop(),
775 cur.par()->params().pagebreakBottom(),
776 VSpace(VSpace::NONE),
777 cur.par()->params().spaceBottom(),
778 cur.par()->params().spacing(),
779 cur.par()->params().align(),
780 cur.par()->params().labelWidthString(), 0);
783 selection.cursor = cur;
786 cutSelection(true, false);
791 case LFUN_BREAKPARAGRAPH:
792 replaceSelection(bv->getLyXText());
793 breakParagraph(bv->buffer()->paragraphs, 0);
795 selection.cursor = cursor;
797 bv->owner()->view_state_changed();
800 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
801 replaceSelection(bv->getLyXText());
802 breakParagraph(bv->buffer()->paragraphs, 1);
804 selection.cursor = cursor;
806 bv->owner()->view_state_changed();
809 case LFUN_BREAKPARAGRAPH_SKIP: {
810 // When at the beginning of a paragraph, remove
811 // indentation and add a "defskip" at the top.
812 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
813 LyXCursor cur = cursor;
814 replaceSelection(bv->getLyXText());
815 if (cur.pos() == 0) {
816 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
818 cur.par()->params().lineTop(),
819 cur.par()->params().lineBottom(),
820 cur.par()->params().pagebreakTop(),
821 cur.par()->params().pagebreakBottom(),
822 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
823 cur.par()->params().spacing(),
824 cur.par()->params().align(),
825 cur.par()->params().labelWidthString(), 1);
829 breakParagraph(bv->buffer()->paragraphs, 0);
832 selection.cursor = cur;
834 bv->owner()->view_state_changed();
838 case LFUN_PARAGRAPH_SPACING: {
839 ParagraphList::iterator pit = cursor.par();
840 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
841 float cur_value = 1.0;
842 if (cur_spacing == Spacing::Other)
843 cur_value = pit->params().spacing().getValue();
845 istringstream is(STRCONV(cmd.argument));
848 Spacing::Space new_spacing = cur_spacing;
849 float new_value = cur_value;
851 lyxerr << "Missing argument to `paragraph-spacing'"
853 } else if (tmp == "single") {
854 new_spacing = Spacing::Single;
855 } else if (tmp == "onehalf") {
856 new_spacing = Spacing::Onehalf;
857 } else if (tmp == "double") {
858 new_spacing = Spacing::Double;
859 } else if (tmp == "other") {
860 new_spacing = Spacing::Other;
863 lyxerr << "new_value = " << tmpval << endl;
866 } else if (tmp == "default") {
867 new_spacing = Spacing::Default;
869 lyxerr << _("Unknown spacing argument: ")
870 << cmd.argument << endl;
872 if (cur_spacing != new_spacing || cur_value != new_value) {
873 pit->params().spacing(Spacing(new_spacing, new_value));
880 case LFUN_INSET_SETTINGS:
881 Assert(bv->theLockingInset());
882 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
885 case LFUN_INSET_TOGGLE:
886 bv->beforeChange(this);
892 case LFUN_SPACE_INSERT:
893 if (cursor.par()->layout()->free_spacing)
896 doInsertInset(this, cmd, false, false);
897 moveCursorUpdate(bv, false);
900 case LFUN_HYPHENATION:
901 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
904 case LFUN_LIGATURE_BREAK:
905 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
909 specialChar(this, bv, InsetSpecialChar::LDOTS);
912 case LFUN_END_OF_SENTENCE:
913 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
916 case LFUN_MENU_SEPARATOR:
917 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
921 bv->beforeChange(this);
923 selection.cursor = cursor;
924 cmd.message(N_("Mark off"));
928 bv->beforeChange(this);
929 selection.mark(true);
931 selection.cursor = cursor;
932 cmd.message(N_("Mark on"));
936 bv->beforeChange(this);
937 if (selection.mark()) {
938 cmd.message(N_("Mark removed"));
940 selection.mark(true);
941 cmd.message(N_("Mark set"));
943 selection.cursor = cursor;
947 case LFUN_UPCASE_WORD:
948 changeCase(LyXText::text_uppercase);
952 case LFUN_LOWCASE_WORD:
953 changeCase(LyXText::text_lowercase);
957 case LFUN_CAPITALIZE_WORD:
958 changeCase(LyXText::text_capitalization);
962 case LFUN_TRANSPOSE_CHARS:
963 recordUndo(bv, Undo::ATOMIC, cursor.par());
969 cmd.message(_("Paste"));
970 replaceSelection(bv->getLyXText());
971 size_t sel_index = 0;
972 string const & arg = cmd.argument;
973 if (isStrUnsignedInt(arg)) {
974 size_t const paste_arg = strToUnsignedInt(arg);
975 #warning FIXME Check if the arg is in the domain of available selections.
976 sel_index = paste_arg;
978 pasteSelection(sel_index);
979 clearSelection(); // bug 393
986 cutSelection(true, true);
987 cmd.message(_("Cut"));
993 cmd.message(_("Copy"));
996 case LFUN_BEGINNINGBUFSEL:
1000 finishChange(bv, true);
1003 case LFUN_ENDBUFSEL:
1005 return UNDISPATCHED;
1007 finishChange(bv, true);
1011 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1017 istringstream is(STRCONV(cmd.argument));
1020 lyxerr << "SETXY: Could not parse coordinates in '"
1021 << cmd.argument << std::endl;
1023 setCursorFromCoordinates(x, y);
1028 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1030 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1036 case LFUN_GETLAYOUT:
1037 cmd.message(tostr(cursor.par()->layout()));
1041 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1042 << cmd.argument << endl;
1044 // This is not the good solution to the empty argument
1045 // problem, but it will hopefully suffice for 1.2.0.
1046 // The correct solution would be to augument the
1047 // function list/array with information about what
1048 // functions needs arguments and their type.
1049 if (cmd.argument.empty()) {
1050 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1054 // Derive layout number from given argument (string)
1055 // and current buffer's textclass (number)
1056 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1057 bool hasLayout = tclass.hasLayout(cmd.argument);
1058 string layout = cmd.argument;
1060 // If the entry is obsolete, use the new one instead.
1062 string const & obs = tclass[layout]->obsoleted_by();
1068 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1073 bool change_layout = (current_layout != layout);
1075 if (!change_layout && selection.set() &&
1076 selection.start.par() != selection.end.par())
1078 ParagraphList::iterator spit = selection.start.par();
1079 ParagraphList::iterator epit = boost::next(selection.end.par());
1080 while (spit != epit) {
1081 if (spit->layout()->name() != current_layout) {
1082 change_layout = true;
1089 if (change_layout) {
1090 current_layout = layout;
1092 bv->owner()->setLayout(layout);
1099 case LFUN_PASTESELECTION: {
1100 // this was originally a beforeChange(bv->text), i.e
1101 // the outermost LyXText!
1102 bv->beforeChange(this);
1103 string const clip = bv->getClipboard();
1104 if (!clip.empty()) {
1105 if (cmd.argument == "paragraph")
1106 insertStringAsParagraphs(clip);
1108 insertStringAsLines(clip);
1115 case LFUN_GOTOERROR:
1116 gotoInset(InsetOld::ERROR_CODE, false);
1120 gotoInset(InsetOld::NOTE_CODE, false);
1123 case LFUN_REFERENCE_GOTO:
1125 vector<InsetOld::Code> tmp;
1126 tmp.push_back(InsetOld::LABEL_CODE);
1127 tmp.push_back(InsetOld::REF_CODE);
1128 gotoInset(tmp, true);
1133 replaceSelection(bv->getLyXText());
1134 ParagraphList::iterator pit = cursor.par();
1135 lyx::pos_type pos = cursor.pos();
1139 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1142 c = pit->getChar(pos - 1);
1144 LyXLayout_ptr const & style = pit->layout();
1146 if (style->pass_thru ||
1147 pit->getFontSettings(bv->buffer()->params,
1148 pos).language()->lang() == "hebrew" ||
1149 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1150 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1154 case LFUN_DATE_INSERT: {
1155 replaceSelection(bv->getLyXText());
1156 time_t now_time_t = time(NULL);
1157 struct tm * now_tm = localtime(&now_time_t);
1158 setlocale(LC_TIME, "");
1160 if (!cmd.argument.empty())
1163 arg = lyxrc.date_insert_format;
1165 int const datetmp_len =
1166 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1168 for (int i = 0; i < datetmp_len; i++)
1169 insertChar(datetmp[i]);
1171 selection.cursor = cursor;
1172 moveCursorUpdate(bv, false);
1176 case LFUN_MOUSE_TRIPLE:
1179 if (!isInInset() && bv->theLockingInset())
1181 if (cmd.button() == mouse_button::button1) {
1183 bv->screen().toggleSelection(this, bv);
1185 selection.cursor = cursor;
1189 bv->screen().toggleSelection(this, bv, false);
1191 bv->haveSelection(selection.set());
1195 case LFUN_MOUSE_DOUBLE:
1198 if (!isInInset() && bv->theLockingInset())
1200 if (cmd.button() == mouse_button::button1) {
1202 bv->screen().toggleSelection(this, bv);
1203 selectWord(lyx::WHOLE_WORD_STRICT);
1204 bv->screen().toggleSelection(this, bv, false);
1206 selectWord(lyx::WHOLE_WORD_STRICT);
1209 bv->haveSelection(selection.set());
1213 case LFUN_MOUSE_MOTION:
1215 // Only use motion with button 1
1216 //if (ev.button() != mouse_button::button1)
1222 // Check for inset locking
1223 if (bv->theLockingInset()) {
1224 InsetOld * tli = bv->theLockingInset();
1225 LyXCursor cursor = bv->text->cursor;
1226 LyXFont font = bv->text->getFont(cursor.par(), cursor.pos());
1227 int width = tli->width();
1228 int inset_x = font.isVisibleRightToLeft()
1229 ? cursor.ix() - width : cursor.ix();
1230 int start_x = inset_x + tli->scroll();
1231 FuncRequest cmd1 = cmd;
1232 cmd1.x = cmd.x - start_x;
1233 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1234 tli->localDispatch(cmd1);
1238 // The test for not selection possible is needed, that only motion
1239 // events are used, where the bottom press event was on
1240 // the drawing area too
1241 if (!selection_possible) {
1242 lyxerr[Debug::ACTION]
1243 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1247 RowList::iterator cursorrow = bv->text->cursorRow();
1248 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->text->top_y());
1250 // sorry for this but I have a strange error that the y value jumps at
1251 // a certain point. This seems like an error in my xforms library or
1252 // in some other local environment, but I would like to leave this here
1253 // for the moment until I can remove this (Jug 20020418)
1254 if (y_before < bv->text->cursor.y())
1255 lyxerr << y_before << ':'
1256 << bv->text->cursor.y() << endl;
1258 // This is to allow jumping over large insets
1259 if (cursorrow == bv->text->cursorRow()) {
1260 if (cmd.y >= bv->workHeight())
1261 bv->text->cursorDown(false);
1263 bv->text->cursorUp(false);
1266 bv->text->setSelection();
1272 // Single-click on work area
1273 case LFUN_MOUSE_PRESS:
1278 // ok ok, this is a hack (for xforms)
1279 // We shouldn't go further down as we really should only do the
1280 // scrolling and be done with this. Otherwise we may open some
1281 // dialogs (Jug 20020424).
1282 if (cmd.button() == mouse_button::button4) {
1283 bv->scroll(-lyxrc.wheel_jump);
1286 if (cmd.button() == mouse_button::button5) {
1287 bv->scroll(lyxrc.wheel_jump);
1293 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1295 // Middle button press pastes if we have a selection
1296 // We do this here as if the selection was inside an inset
1297 // it could get cleared on the unlocking of the inset so
1298 // we have to check this first
1299 bool paste_internally = false;
1300 if (cmd.button() == mouse_button::button2 && selection.set()) {
1301 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1302 paste_internally = true;
1305 int const screen_first = bv->text->top_y();
1307 if (bv->theLockingInset()) {
1308 // We are in inset locking mode
1310 // Check whether the inset was hit. If not reset mode,
1311 // otherwise give the event to the inset
1312 if (inset_hit == bv->theLockingInset()) {
1313 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1314 bv->theLockingInset()->localDispatch(cmd1);
1317 bv->unlockInset(bv->theLockingInset());
1321 selection_possible = true;
1323 // Clear the selection
1324 bv->screen().toggleSelection(bv->text, bv);
1325 bv->text->clearSelection();
1327 bv->updateScrollbar();
1329 // Single left click in math inset?
1330 if (isHighlyEditableInset(inset_hit)) {
1331 // Highly editable inset, like math
1332 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1333 selection_possible = false;
1334 bv->owner()->message(inset->editMessage());
1335 // We just have to lock the inset before calling a PressEvent on it!
1336 if (!bv->lockInset(inset))
1337 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1338 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1339 inset->localDispatch(cmd1);
1342 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1344 // Right click on a footnote flag opens float menu
1345 if (cmd.button() == mouse_button::button3) {
1346 selection_possible = false;
1350 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1351 bv->text->setCursorFromCoordinates(x, y + screen_first);
1353 bv->text->selection.cursor = bv->text->cursor;
1354 bv->text->cursor.x_fix(bv->text->cursor.x());
1356 if (bv->fitCursor())
1357 selection_possible = false;
1359 // Insert primary selection with middle mouse
1360 // if there is a local selection in the current buffer,
1362 if (cmd.button() == mouse_button::button2) {
1363 if (paste_internally)
1364 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1366 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1367 selection_possible = false;
1372 case LFUN_MOUSE_RELEASE:
1374 // do nothing if we used the mouse wheel
1378 if (cmd.button() == mouse_button::button4
1379 || cmd.button() == mouse_button::button5)
1382 // If we hit an inset, we have the inset coordinates in these
1383 // and inset_hit points to the inset. If we do not hit an
1384 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1387 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1389 if (bv->theLockingInset()) {
1390 // We are in inset locking mode.
1392 // LyX does a kind of work-area grabbing for insets.
1393 // Only a ButtonPress FuncRequest outside the inset will
1394 // force a insetUnlock.
1395 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1396 bv->theLockingInset()->localDispatch(cmd1);
1400 selection_possible = false;
1402 if (cmd.button() == mouse_button::button2)
1406 if (cmd.button() == mouse_button::button1)
1407 bv->haveSelection(selection.set());
1410 bv->owner()->view_state_changed();
1411 bv->owner()->updateMenubar();
1412 bv->owner()->updateToolbar();
1414 // Did we hit an editable inset?
1416 selection_possible = false;
1418 // if we reach this point with a selection, it
1419 // must mean we are currently selecting.
1420 // But we don't want to open the inset
1421 // because that is annoying for the user.
1422 // So just pretend we didn't hit it.
1423 // this is OK because a "kosher" ButtonRelease
1424 // will follow a ButtonPress that clears
1426 // Note this also fixes selection drawing
1427 // problems if we end up opening an inset
1428 if (selection.set())
1431 // CHECK fix this proper in 0.13
1432 // well, maybe 13.0 !!!!!!!!!
1434 // Following a ref shouldn't issue
1435 // a push on the undo-stack
1436 // anylonger, now that we have
1437 // keybindings for following
1438 // references and returning from
1439 // references. IMHO though, it
1440 // should be the inset's own business
1441 // to push or not push on the undo
1442 // stack. They don't *have* to
1443 // alter the document...
1445 // ...or maybe the SetCursorParUndo()
1446 // below isn't necessary at all anylonger?
1447 if (inset_hit->lyxCode() == InsetOld::REF_CODE)
1448 recordUndo(bv, Undo::ATOMIC);
1450 bv->owner()->message(inset_hit->editMessage());
1452 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1453 inset_hit->localDispatch(cmd1);
1459 case LFUN_SELFINSERT: {
1460 if (cmd.argument.empty())
1463 // Automatically delete the currently selected
1464 // text and replace it with what is being
1465 // typed in now. Depends on lyxrc settings
1466 // "auto_region_delete", which defaults to
1469 if (lyxrc.auto_region_delete) {
1470 if (selection.set())
1471 cutSelection(false, false);
1472 bv->haveSelection(false);
1475 bv->beforeChange(this);
1476 LyXFont const old_font(real_current_font);
1478 string::const_iterator cit = cmd.argument.begin();
1479 string::const_iterator end = cmd.argument.end();
1480 for (; cit != end; ++cit)
1481 bv->owner()->getIntl().getTransManager().
1482 TranslateAndInsert(*cit, this);
1484 selection.cursor = cursor;
1485 moveCursorUpdate(bv, false);
1487 // real_current_font.number can change so we need to
1488 // update the minibuffer
1489 if (old_font != real_current_font)
1490 bv->owner()->view_state_changed();
1494 case LFUN_HTMLURL: {
1495 InsetCommandParams p("htmlurl");
1496 string const data = InsetCommandMailer::params2string("url", p);
1497 bv->owner()->getDialogs().show("url", data, 0);
1502 InsetCommandParams p("url");
1503 string const data = InsetCommandMailer::params2string("url", p);
1504 bv->owner()->getDialogs().show("url", data, 0);
1510 case LFUN_INSET_LIST:
1511 case LFUN_INSET_THEOREM:
1512 case LFUN_INSET_CAPTION:
1514 case LFUN_INSERT_NOTE:
1515 case LFUN_INSERT_BIBITEM:
1516 case LFUN_INSET_ERT:
1517 case LFUN_INSET_FLOAT:
1518 case LFUN_INSET_FOOTNOTE:
1519 case LFUN_INSET_MARGINAL:
1520 case LFUN_INSET_MINIPAGE:
1521 case LFUN_INSET_OPTARG:
1522 case LFUN_INSET_WIDE_FLOAT:
1523 case LFUN_INSET_WRAP:
1524 case LFUN_TABULAR_INSERT:
1525 case LFUN_ENVIRONMENT_INSERT:
1526 // Open the inset, and move the current selection
1528 doInsertInset(this, cmd, true, true);
1531 case LFUN_INDEX_INSERT:
1532 // Just open the inset
1533 doInsertInset(this, cmd, true, false);
1536 case LFUN_INDEX_PRINT:
1537 case LFUN_TOC_INSERT:
1540 doInsertInset(this, cmd, false, false);
1544 return UNDISPATCHED;