3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Alfredo Braunstein
9 * \author Angus Leeming
11 * \author André Pönitz
13 * Full author contact details are available in file CREDITS.
21 #include "BufferView.h"
24 #include "funcrequest.h"
30 #include "paragraph.h"
31 #include "ParagraphParameters.h"
32 #include "text_funcs.h"
33 #include "undo_funcs.h"
35 #include "frontends/Dialogs.h"
36 #include "frontends/LyXView.h"
38 #include "insets/insetcommand.h"
39 #include "insets/insetnewline.h"
40 #include "insets/insetspecialchar.h"
41 #include "insets/insettext.h"
43 #include "support/LAssert.h"
44 #include "support/lstrings.h"
45 #include "support/tostr.h"
47 #include "support/std_sstream.h"
50 using namespace lyx::support;
51 using namespace bv_funcs;
56 using std::istringstream;
62 extern string current_layout;
63 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
65 // the selection possible is needed, that only motion events are
66 // used, where the bottom press event was on the drawing area too
67 bool selection_possible = false;
72 void moveCursorUpdate(BufferView * bv, bool selecting)
74 LyXText * lt = bv->getLyXText();
76 //if (!lt->selection.set())
77 // lt->selection.cursor = lt->cursor;
79 if (selecting || lt->selection.mark())
82 if (!lt->selection.set())
83 bv->haveSelection(false);
90 void finishChange(BufferView * bv, bool selecting = false)
93 moveCursorUpdate(bv, selecting);
94 bv->owner()->view_state_changed();
97 // check if the given co-ordinates are inside an inset at the
98 // given cursor, if one exists. If so, the inset is returned,
99 // and the co-ordinates are made relative. Otherwise, 0 is returned.
100 InsetOld * checkInset(BufferView * /*bv*/, LyXText & text,
101 LyXCursor const & cur, int & x, int & y)
103 lyx::pos_type const pos = cur.pos();
104 ParagraphList::iterator par = cur.par();
106 if (pos >= par->size() || !par->isInset(pos))
109 InsetOld /*const*/ * inset = par->getInset(pos);
111 if (!isEditableInset(inset))
114 // get inset dimensions
115 Assert(par->getInset(pos));
117 LyXFont const & font = text.getFont(par, pos);
119 int const width = inset->width();
120 int const inset_x = font.isVisibleRightToLeft()
121 ? (cur.x() - width) : cur.x();
124 inset_x + inset->scroll(),
126 cur.y() - inset->ascent(),
127 cur.y() + inset->descent()
130 if (!b.contains(x, y)) {
131 lyxerr[Debug::GUI] << "Missed inset at x,y "
133 << " box " << b << endl;
137 text.setCursor(par, pos, true);
140 // The origin of an inset is on the baseline
141 y -= text.cursor.y();
149 InsetOld * LyXText::checkInsetHit(int & x, int & y)
151 int y_tmp = y + bv_owner->top_y();
154 setCursorFromCoordinates(cur, x, y_tmp);
156 InsetOld * inset = checkInset(bv(), *this, cur, x, y_tmp);
162 // look at previous position
167 setCursor(cur, cur.par(), cur.pos() - 1, true);
169 inset = checkInset(bv(), *this, cur, x, y_tmp);
176 bool LyXText::gotoNextInset(vector<InsetOld::Code> const & codes,
177 string const & contents)
179 ParagraphList::iterator end = ownerParagraphs().end();
180 ParagraphList::iterator pit = cursor.par();
181 pos_type pos = cursor.pos();
185 if (pos + 1 < pit->size()) {
192 } while (pit != end &&
193 !(pit->isInset(pos) &&
194 (inset = pit->getInset(pos)) != 0 &&
195 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
197 static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
201 setCursor(pit, pos, false);
208 void LyXText::gotoInset(vector<InsetOld::Code> const & codes,
211 bv()->beforeChange(this);
214 if (same_content && cursor.pos() < cursor.par()->size()
215 && cursor.par()->isInset(cursor.pos())) {
216 InsetOld const * inset = cursor.par()->getInset(cursor.pos());
217 if (find(codes.begin(), codes.end(), inset->lyxCode())
219 contents = static_cast<InsetCommand const *>(inset)->getContents();
222 if (!gotoNextInset(codes, contents)) {
223 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
224 LyXCursor tmp = cursor;
225 cursor.par(ownerParagraphs().begin());
227 if (!gotoNextInset(codes, contents)) {
229 bv()->owner()->message(_("No more insets"));
232 bv()->owner()->message(_("No more insets"));
236 selection.cursor = cursor;
240 void LyXText::gotoInset(InsetOld::Code code, bool same_content)
242 gotoInset(vector<InsetOld::Code>(1, code), same_content);
246 void LyXText::cursorPrevious()
248 int y = bv_owner->top_y();
250 RowList::iterator rit = cursorRow();
252 if (rit == firstRow()) {
254 bv()->updateScrollbar();
258 setCursorFromCoordinates(cursor.x_fix(), y);
262 if (rit == bv()->text->cursorRow()) {
263 // we have a row which is taller than the workarea. The
264 // simplest solution is to move to the previous row instead.
267 // This is what we used to do, so we wouldn't skip right past
268 // tall rows, but it's not working right now.
271 new_y = bv()->text->cursor.y()
272 + bv()->theLockingInset()->insetInInsetY() + y
274 - bv()->workHeight() + 1;
279 - bv()->workHeight() + 1;
284 ParagraphList::iterator pit = cursor.par();
285 previousRow(pit, rit);
286 setCursor(cur, pit, rit->pos(), false);
287 if (cur.y() > bv_owner->top_y())
289 bv()->updateScrollbar();
293 void LyXText::cursorNext()
295 int topy = bv_owner->top_y();
297 RowList::iterator rit = cursorRow();
298 if (rit == lastRow()) {
299 int y = cursor.y() - rit->baseline() + cursorRow()->height();
300 if (y > topy + bv()->workHeight())
301 bv_owner->updateScrollbar();
305 int y = topy + bv_owner->workHeight();
306 if (inset_owner && !topy) {
307 y -= (bv_owner->text->cursor.y()
309 + bv_owner->theLockingInset()->insetInInsetY());
312 ParagraphList::iterator dummypit;
313 y = getRowNearY(y, dummypit)->y();
315 setCursorFromCoordinates(cursor.x_fix(), y);
316 // + bv->workHeight());
320 if (rit == bv_owner->text->cursorRow()) {
321 // we have a row which is taller than the workarea. The
322 // simplest solution is to move to the next row instead.
325 // This is what we used to do, so we wouldn't skip right past
326 // tall rows, but it's not working right now.
328 new_y = bv->top_y() + bv->workHeight();
332 new_y = bv()->text->cursor.y()
333 + bv()->theLockingInset()->insetInInsetY()
334 + y - rit->baseline();
336 new_y = cursor.y() - cursorRow()->baseline();
340 ParagraphList::iterator pit = cursor.par();
343 setCursor(cur, pit, rit->pos(), false);
344 if (cur.y() < bv_owner->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))
360 bv->updateInset(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] << "LyXText::dispatch: action[" << cmd.action
394 <<"] arg[" << cmd.argument << ']' << "xy[" <<
395 cmd.x << ',' << cmd.y << ']' << endl;
397 BufferView * bv = cmd.view();
399 switch (cmd.action) {
401 case LFUN_APPENDIX: {
402 ParagraphList::iterator pit = cursor.par();
403 bool start = !pit->params().startOfAppendix();
405 // ensure that we have only one start_of_appendix in this document
406 ParagraphList::iterator tmp = ownerParagraphs().begin();
407 ParagraphList::iterator end = ownerParagraphs().end();
409 for (; tmp != end; ++tmp) {
410 if (tmp->params().startOfAppendix()) {
411 recordUndo(bv, Undo::ATOMIC, tmp);
412 tmp->params().startOfAppendix(false);
418 recordUndo(bv, Undo::ATOMIC, pit);
419 pit->params().startOfAppendix(start);
421 // we can set the refreshing parameters now
423 redoParagraph(cursor.par());
424 setCursor(cursor.par(), cursor.pos());
429 case LFUN_DELETE_WORD_FORWARD:
430 bv->beforeChange(this);
435 case LFUN_DELETE_WORD_BACKWARD:
436 bv->beforeChange(this);
437 deleteWordBackward();
441 case LFUN_DELETE_LINE_FORWARD:
442 bv->beforeChange(this);
448 if (!selection.mark())
449 bv->beforeChange(this);
450 if (cursor.par()->isRightToLeftPar(bv->buffer()->params()))
453 cursorRightOneWord();
458 if (!selection.mark())
459 bv->beforeChange(this);
460 if (cursor.par()->isRightToLeftPar(bv->buffer()->params()))
461 cursorRightOneWord();
467 case LFUN_BEGINNINGBUF:
468 if (!selection.mark())
469 bv->beforeChange(this);
475 if (selection.mark())
476 bv->beforeChange(this);
482 if (!selection.set())
483 selection.cursor = cursor;
484 if (cursor.par()->isRightToLeftPar(bv->buffer()->params()))
488 finishChange(bv, true);
492 if (!selection.set())
493 selection.cursor = cursor;
494 if (cursor.par()->isRightToLeftPar(bv->buffer()->params()))
498 finishChange(bv, true);
502 if (!selection.set())
503 selection.cursor = cursor;
505 finishChange(bv, true);
509 if (!selection.set())
510 selection.cursor = cursor;
512 finishChange(bv, true);
515 case LFUN_UP_PARAGRAPHSEL:
516 if (!selection.set())
517 selection.cursor = cursor;
519 finishChange(bv, true);
522 case LFUN_DOWN_PARAGRAPHSEL:
523 if (!selection.set())
524 selection.cursor = cursor;
525 cursorDownParagraph();
526 finishChange(bv, true);
530 if (!selection.set())
531 selection.cursor = cursor;
533 finishChange(bv, true);
537 if (!selection.set())
538 selection.cursor = cursor;
540 finishChange(bv, true);
544 if (!selection.set())
545 selection.cursor = cursor;
547 finishChange(bv, true);
551 if (!selection.set())
552 selection.cursor = cursor;
554 finishChange(bv, true);
557 case LFUN_WORDRIGHTSEL:
558 if (cursor.par()->isRightToLeftPar(bv->buffer()->params()))
561 cursorRightOneWord();
562 finishChange(bv, true);
565 case LFUN_WORDLEFTSEL:
566 if (cursor.par()->isRightToLeftPar(bv->buffer()->params()))
567 cursorRightOneWord();
570 finishChange(bv, true);
574 LyXCursor cur1 = cursor;
576 ::getWord(cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs());
577 setCursor(cur1.par(), cur1.pos());
578 bv->beforeChange(this);
579 setCursor(cur2.par(), cur2.pos());
580 finishChange(bv, true);
585 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params());
586 if (!selection.mark())
587 bv->beforeChange(this);
590 if (cursor.pos() < cursor.par()->size()
591 && cursor.par()->isInset(cursor.pos())
592 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
593 InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
594 cmd.message(tmpinset->editMessage());
595 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
596 tmpinset->localDispatch(cmd1);
606 // This is soooo ugly. Isn`t it possible to make
608 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params());
609 if (!selection.mark())
610 bv->beforeChange(this);
611 LyXCursor const cur = cursor;
614 if ((is_rtl || cur != cursor) && // only if really moved!
615 cursor.pos() < cursor.par()->size() &&
616 cursor.par()->isInset(cursor.pos()) &&
617 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
618 InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
619 cmd.message(tmpinset->editMessage());
620 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
621 tmpinset->localDispatch(cmd1);
631 if (!selection.mark())
632 bv->beforeChange(this);
638 if (!selection.mark())
639 bv->beforeChange(this);
644 case LFUN_UP_PARAGRAPH:
645 if (!selection.mark())
646 bv->beforeChange(this);
651 case LFUN_DOWN_PARAGRAPH:
652 if (!selection.mark())
653 bv->beforeChange(this);
654 cursorDownParagraph();
655 finishChange(bv, false);
659 if (!selection.mark())
660 bv->beforeChange(this);
662 finishChange(bv, false);
666 if (!selection.mark())
667 bv->beforeChange(this);
669 finishChange(bv, false);
673 if (!selection.mark())
674 bv->beforeChange(this);
676 finishChange(bv, false);
680 if (!selection.mark())
681 bv->beforeChange(this);
683 finishChange(bv, false);
686 case LFUN_BREAKLINE: {
687 lyx::pos_type body = cursor.par()->beginningOfBody();
689 // Not allowed by LaTeX (labels or empty par)
690 if (cursor.pos() <= body)
693 replaceSelection(bv->getLyXText());
694 insertInset(new InsetNewline);
695 setCursor(cursor.par(), cursor.pos());
696 moveCursorUpdate(bv, false);
701 if (!selection.set()) {
703 selection.cursor = cursor;
704 // It is possible to make it a lot faster still
705 // just comment out the line below...
707 cutSelection(true, false);
709 moveCursorUpdate(bv, false);
710 bv->owner()->view_state_changed();
713 case LFUN_DELETE_SKIP:
714 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
715 if (!selection.set()) {
716 LyXCursor cur = cursor;
717 if (cur.pos() == cur.par()->size()) {
721 && !(cur.par()->params().spaceTop()
722 == VSpace (VSpace::NONE))) {
724 cur.par()->params().lineTop(),
725 cur.par()->params().lineBottom(),
726 cur.par()->params().pagebreakTop(),
727 cur.par()->params().pagebreakBottom(),
728 VSpace(VSpace::NONE),
729 cur.par()->params().spaceBottom(),
730 cur.par()->params().spacing(),
731 cur.par()->params().align(),
732 cur.par()->params().labelWidthString(), 0);
737 selection.cursor = cursor;
741 selection.cursor = cursor;
744 cutSelection(true, false);
751 if (!selection.set()) {
752 if (bv->owner()->getIntl().getTransManager().backspace()) {
754 selection.cursor = cursor;
755 // It is possible to make it a lot faster still
756 // just comment out the line below...
759 cutSelection(true, false);
761 bv->owner()->view_state_changed();
766 case LFUN_BACKSPACE_SKIP:
767 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
768 if (!selection.set()) {
769 LyXCursor cur = cursor;
771 && !(cur.par()->params().spaceTop() == VSpace(VSpace::NONE))) {
773 cur.par()->params().lineTop(),
774 cur.par()->params().lineBottom(),
775 cur.par()->params().pagebreakTop(),
776 cur.par()->params().pagebreakBottom(),
777 VSpace(VSpace::NONE),
778 cur.par()->params().spaceBottom(),
779 cur.par()->params().spacing(),
780 cur.par()->params().align(),
781 cur.par()->params().labelWidthString(), 0);
784 selection.cursor = cur;
787 cutSelection(true, false);
792 case LFUN_BREAKPARAGRAPH:
793 replaceSelection(bv->getLyXText());
794 breakParagraph(bv->buffer()->paragraphs(), 0);
796 selection.cursor = cursor;
798 bv->owner()->view_state_changed();
801 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
802 replaceSelection(bv->getLyXText());
803 breakParagraph(bv->buffer()->paragraphs(), 1);
805 selection.cursor = cursor;
807 bv->owner()->view_state_changed();
810 case LFUN_BREAKPARAGRAPH_SKIP: {
811 // When at the beginning of a paragraph, remove
812 // indentation and add a "defskip" at the top.
813 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
814 LyXCursor cur = cursor;
815 replaceSelection(bv->getLyXText());
816 if (cur.pos() == 0) {
817 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
819 cur.par()->params().lineTop(),
820 cur.par()->params().lineBottom(),
821 cur.par()->params().pagebreakTop(),
822 cur.par()->params().pagebreakBottom(),
823 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
824 cur.par()->params().spacing(),
825 cur.par()->params().align(),
826 cur.par()->params().labelWidthString(), 1);
830 breakParagraph(bv->buffer()->paragraphs(), 0);
833 selection.cursor = cur;
835 bv->owner()->view_state_changed();
839 case LFUN_PARAGRAPH_SPACING: {
840 ParagraphList::iterator pit = cursor.par();
841 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
842 float cur_value = 1.0;
843 if (cur_spacing == Spacing::Other)
844 cur_value = pit->params().spacing().getValue();
846 istringstream is(STRCONV(cmd.argument));
849 Spacing::Space new_spacing = cur_spacing;
850 float new_value = cur_value;
852 lyxerr << "Missing argument to `paragraph-spacing'"
854 } else if (tmp == "single") {
855 new_spacing = Spacing::Single;
856 } else if (tmp == "onehalf") {
857 new_spacing = Spacing::Onehalf;
858 } else if (tmp == "double") {
859 new_spacing = Spacing::Double;
860 } else if (tmp == "other") {
861 new_spacing = Spacing::Other;
864 lyxerr << "new_value = " << tmpval << endl;
867 } else if (tmp == "default") {
868 new_spacing = Spacing::Default;
870 lyxerr << _("Unknown spacing argument: ")
871 << cmd.argument << endl;
873 if (cur_spacing != new_spacing || cur_value != new_value) {
874 pit->params().spacing(Spacing(new_spacing, new_value));
881 case LFUN_INSET_SETTINGS:
882 Assert(bv->theLockingInset());
883 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
886 case LFUN_INSET_TOGGLE:
887 bv->beforeChange(this);
893 case LFUN_SPACE_INSERT:
894 if (cursor.par()->layout()->free_spacing)
897 doInsertInset(this, cmd, false, false);
898 moveCursorUpdate(bv, false);
901 case LFUN_HYPHENATION:
902 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
905 case LFUN_LIGATURE_BREAK:
906 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
910 specialChar(this, bv, InsetSpecialChar::LDOTS);
913 case LFUN_END_OF_SENTENCE:
914 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
917 case LFUN_MENU_SEPARATOR:
918 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
922 bv->beforeChange(this);
924 selection.cursor = cursor;
925 cmd.message(N_("Mark off"));
929 bv->beforeChange(this);
930 selection.mark(true);
932 selection.cursor = cursor;
933 cmd.message(N_("Mark on"));
937 bv->beforeChange(this);
938 if (selection.mark()) {
939 cmd.message(N_("Mark removed"));
941 selection.mark(true);
942 cmd.message(N_("Mark set"));
944 selection.cursor = cursor;
948 case LFUN_UPCASE_WORD:
949 changeCase(LyXText::text_uppercase);
953 case LFUN_LOWCASE_WORD:
954 changeCase(LyXText::text_lowercase);
958 case LFUN_CAPITALIZE_WORD:
959 changeCase(LyXText::text_capitalization);
963 case LFUN_TRANSPOSE_CHARS:
964 recordUndo(bv, Undo::ATOMIC, cursor.par());
970 cmd.message(_("Paste"));
971 replaceSelection(bv->getLyXText());
972 size_t sel_index = 0;
973 string const & arg = cmd.argument;
974 if (isStrUnsignedInt(arg)) {
975 size_t const paste_arg = strToUnsignedInt(arg);
976 #warning FIXME Check if the arg is in the domain of available selections.
977 sel_index = paste_arg;
979 pasteSelection(sel_index);
980 clearSelection(); // bug 393
987 cutSelection(true, true);
988 cmd.message(_("Cut"));
994 cmd.message(_("Copy"));
997 case LFUN_BEGINNINGBUFSEL:
1001 finishChange(bv, true);
1004 case LFUN_ENDBUFSEL:
1006 return UNDISPATCHED;
1008 finishChange(bv, true);
1012 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1018 istringstream is(STRCONV(cmd.argument));
1021 lyxerr << "SETXY: Could not parse coordinates in '"
1022 << cmd.argument << std::endl;
1024 setCursorFromCoordinates(x, y);
1029 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1031 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1037 case LFUN_GETLAYOUT:
1038 cmd.message(tostr(cursor.par()->layout()));
1042 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1043 << cmd.argument << endl;
1045 // This is not the good solution to the empty argument
1046 // problem, but it will hopefully suffice for 1.2.0.
1047 // The correct solution would be to augument the
1048 // function list/array with information about what
1049 // functions needs arguments and their type.
1050 if (cmd.argument.empty()) {
1051 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1055 // Derive layout number from given argument (string)
1056 // and current buffer's textclass (number)
1057 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1058 bool hasLayout = tclass.hasLayout(cmd.argument);
1059 string layout = cmd.argument;
1061 // If the entry is obsolete, use the new one instead.
1063 string const & obs = tclass[layout]->obsoleted_by();
1069 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1074 bool change_layout = (current_layout != layout);
1076 if (!change_layout && selection.set() &&
1077 selection.start.par() != selection.end.par())
1079 ParagraphList::iterator spit = selection.start.par();
1080 ParagraphList::iterator epit = boost::next(selection.end.par());
1081 while (spit != epit) {
1082 if (spit->layout()->name() != current_layout) {
1083 change_layout = true;
1090 if (change_layout) {
1091 current_layout = layout;
1093 bv->owner()->setLayout(layout);
1100 case LFUN_PASTESELECTION: {
1101 // this was originally a beforeChange(bv->text), i.e
1102 // the outermost LyXText!
1103 bv->beforeChange(this);
1104 string const clip = bv->getClipboard();
1105 if (!clip.empty()) {
1106 if (cmd.argument == "paragraph")
1107 insertStringAsParagraphs(clip);
1109 insertStringAsLines(clip);
1116 case LFUN_GOTOERROR:
1117 gotoInset(InsetOld::ERROR_CODE, false);
1121 gotoInset(InsetOld::NOTE_CODE, false);
1124 case LFUN_REFERENCE_GOTO:
1126 vector<InsetOld::Code> tmp;
1127 tmp.push_back(InsetOld::LABEL_CODE);
1128 tmp.push_back(InsetOld::REF_CODE);
1129 gotoInset(tmp, true);
1134 replaceSelection(bv->getLyXText());
1135 ParagraphList::iterator pit = cursor.par();
1136 lyx::pos_type pos = cursor.pos();
1140 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1143 c = pit->getChar(pos - 1);
1145 LyXLayout_ptr const & style = pit->layout();
1147 BufferParams const & bufparams = bv->buffer()->params();
1148 if (style->pass_thru ||
1149 pit->getFontSettings(bufparams,pos).language()->lang() == "hebrew" ||
1150 !bv->insertInset(new InsetQuotes(c, bufparams)))
1151 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1155 case LFUN_DATE_INSERT: {
1156 replaceSelection(bv->getLyXText());
1157 time_t now_time_t = time(NULL);
1158 struct tm * now_tm = localtime(&now_time_t);
1159 setlocale(LC_TIME, "");
1161 if (!cmd.argument.empty())
1164 arg = lyxrc.date_insert_format;
1166 int const datetmp_len =
1167 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1169 for (int i = 0; i < datetmp_len; i++)
1170 insertChar(datetmp[i]);
1172 selection.cursor = cursor;
1173 moveCursorUpdate(bv, false);
1177 case LFUN_MOUSE_TRIPLE:
1180 if (!isInInset() && bv->theLockingInset())
1182 if (cmd.button() == mouse_button::button1) {
1184 selection.cursor = cursor;
1188 bv->haveSelection(selection.set());
1192 case LFUN_MOUSE_DOUBLE:
1195 if (!isInInset() && bv->theLockingInset())
1197 if (cmd.button() == mouse_button::button1) {
1198 selectWord(lyx::WHOLE_WORD_STRICT);
1200 bv->haveSelection(selection.set());
1204 case LFUN_MOUSE_MOTION:
1206 // Only use motion with button 1
1207 //if (ev.button() != mouse_button::button1)
1213 // Check for inset locking
1214 if (bv->theLockingInset()) {
1215 InsetOld * tli = bv->theLockingInset();
1216 LyXCursor cursor = bv->text->cursor;
1217 LyXFont font = bv->text->getFont(cursor.par(), cursor.pos());
1218 int width = tli->width();
1219 int inset_x = font.isVisibleRightToLeft()
1220 ? cursor.x() - width : cursor.x();
1221 int start_x = inset_x + tli->scroll();
1222 FuncRequest cmd1 = cmd;
1223 cmd1.x = cmd.x - start_x;
1224 cmd1.y = cmd.y - cursor.y() + bv->top_y();
1225 tli->localDispatch(cmd1);
1229 // The test for not selection possible is needed, that only motion
1230 // events are used, where the bottom press event was on
1231 // the drawing area too
1232 if (!selection_possible) {
1233 lyxerr[Debug::ACTION]
1234 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1238 RowList::iterator cursorrow = bv->text->cursorRow();
1239 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->top_y());
1241 // sorry for this but I have a strange error that the y value jumps at
1242 // a certain point. This seems like an error in my xforms library or
1243 // in some other local environment, but I would like to leave this here
1244 // for the moment until I can remove this (Jug 20020418)
1245 if (y_before < bv->text->cursor.y())
1246 lyxerr << y_before << ':'
1247 << bv->text->cursor.y() << endl;
1249 // This is to allow jumping over large insets
1250 if (cursorrow == bv->text->cursorRow()) {
1251 if (cmd.y >= bv->workHeight())
1252 bv->text->cursorDown(false);
1254 bv->text->cursorUp(false);
1257 bv->text->setSelection();
1262 // Single-click on work area
1263 case LFUN_MOUSE_PRESS:
1268 // ok ok, this is a hack (for xforms)
1269 // We shouldn't go further down as we really should only do the
1270 // scrolling and be done with this. Otherwise we may open some
1271 // dialogs (Jug 20020424).
1272 if (cmd.button() == mouse_button::button4) {
1273 bv->scroll(-lyxrc.wheel_jump);
1276 if (cmd.button() == mouse_button::button5) {
1277 bv->scroll(lyxrc.wheel_jump);
1283 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1285 // Middle button press pastes if we have a selection
1286 // We do this here as if the selection was inside an inset
1287 // it could get cleared on the unlocking of the inset so
1288 // we have to check this first
1289 bool paste_internally = false;
1290 if (cmd.button() == mouse_button::button2 && selection.set()) {
1291 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1292 paste_internally = true;
1295 int const screen_first = bv->top_y();
1297 if (bv->theLockingInset()) {
1298 // We are in inset locking mode
1300 // Check whether the inset was hit. If not reset mode,
1301 // otherwise give the event to the inset
1302 if (inset_hit == bv->theLockingInset()) {
1303 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1304 bv->theLockingInset()->localDispatch(cmd1);
1307 bv->unlockInset(bv->theLockingInset());
1311 selection_possible = true;
1313 // Clear the selection
1314 bv->text->clearSelection();
1316 bv->updateScrollbar();
1318 // Single left click in math inset?
1319 if (isHighlyEditableInset(inset_hit)) {
1320 // Highly editable inset, like math
1321 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1322 selection_possible = false;
1323 bv->owner()->message(inset->editMessage());
1324 // We just have to lock the inset before calling a PressEvent on it!
1325 if (!bv->lockInset(inset))
1326 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1327 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1328 inset->localDispatch(cmd1);
1331 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1333 // Right click on a footnote flag opens float menu
1334 if (cmd.button() == mouse_button::button3) {
1335 selection_possible = false;
1339 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1340 bv->text->setCursorFromCoordinates(x, y + screen_first);
1342 bv->text->selection.cursor = bv->text->cursor;
1343 bv->text->cursor.x_fix(bv->text->cursor.x());
1345 if (bv->fitCursor())
1346 selection_possible = false;
1348 // Insert primary selection with middle mouse
1349 // if there is a local selection in the current buffer,
1351 if (cmd.button() == mouse_button::button2) {
1352 if (paste_internally)
1353 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1355 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1356 selection_possible = false;
1361 case LFUN_MOUSE_RELEASE:
1363 // do nothing if we used the mouse wheel
1367 if (cmd.button() == mouse_button::button4
1368 || cmd.button() == mouse_button::button5)
1371 // If we hit an inset, we have the inset coordinates in these
1372 // and inset_hit points to the inset. If we do not hit an
1373 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1376 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1378 if (bv->theLockingInset()) {
1379 // We are in inset locking mode.
1381 // LyX does a kind of work-area grabbing for insets.
1382 // Only a ButtonPress FuncRequest outside the inset will
1383 // force a insetUnlock.
1384 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1385 bv->theLockingInset()->localDispatch(cmd1);
1389 selection_possible = false;
1391 if (cmd.button() == mouse_button::button2)
1395 if (cmd.button() == mouse_button::button1)
1396 bv->haveSelection(selection.set());
1399 bv->owner()->view_state_changed();
1400 bv->owner()->updateMenubar();
1401 bv->owner()->updateToolbar();
1403 // Did we hit an editable inset?
1405 selection_possible = false;
1407 // if we reach this point with a selection, it
1408 // must mean we are currently selecting.
1409 // But we don't want to open the inset
1410 // because that is annoying for the user.
1411 // So just pretend we didn't hit it.
1412 // this is OK because a "kosher" ButtonRelease
1413 // will follow a ButtonPress that clears
1415 // Note this also fixes selection drawing
1416 // problems if we end up opening an inset
1417 if (selection.set())
1420 // CHECK fix this proper in 0.13
1421 // well, maybe 13.0 !!!!!!!!!
1423 // Following a ref shouldn't issue
1424 // a push on the undo-stack
1425 // anylonger, now that we have
1426 // keybindings for following
1427 // references and returning from
1428 // references. IMHO though, it
1429 // should be the inset's own business
1430 // to push or not push on the undo
1431 // stack. They don't *have* to
1432 // alter the document...
1434 // ...or maybe the recordUndo()
1435 // below isn't necessary at all anylonger?
1436 if (inset_hit->lyxCode() == InsetOld::REF_CODE)
1437 recordUndo(bv, Undo::ATOMIC);
1439 bv->owner()->message(inset_hit->editMessage());
1441 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1442 inset_hit->localDispatch(cmd1);
1448 case LFUN_SELFINSERT: {
1449 if (cmd.argument.empty())
1452 // Automatically delete the currently selected
1453 // text and replace it with what is being
1454 // typed in now. Depends on lyxrc settings
1455 // "auto_region_delete", which defaults to
1458 if (lyxrc.auto_region_delete) {
1459 if (selection.set())
1460 cutSelection(false, false);
1461 bv->haveSelection(false);
1464 bv->beforeChange(this);
1465 LyXFont const old_font = real_current_font;
1467 string::const_iterator cit = cmd.argument.begin();
1468 string::const_iterator end = cmd.argument.end();
1469 for (; cit != end; ++cit)
1470 bv->owner()->getIntl().getTransManager().
1471 TranslateAndInsert(*cit, this);
1473 selection.cursor = cursor;
1474 moveCursorUpdate(bv, false);
1476 // real_current_font.number can change so we need to
1477 // update the minibuffer
1478 if (old_font != real_current_font)
1479 bv->owner()->view_state_changed();
1480 bv->updateScrollbar();
1484 case LFUN_HTMLURL: {
1485 InsetCommandParams p("htmlurl");
1486 string const data = InsetCommandMailer::params2string("url", p);
1487 bv->owner()->getDialogs().show("url", data, 0);
1492 InsetCommandParams p("url");
1493 string const data = InsetCommandMailer::params2string("url", p);
1494 bv->owner()->getDialogs().show("url", data, 0);
1500 case LFUN_INSET_LIST:
1501 case LFUN_INSET_THEOREM:
1502 case LFUN_INSET_CAPTION:
1504 case LFUN_INSERT_NOTE:
1505 case LFUN_INSERT_BRANCH:
1506 case LFUN_INSERT_BIBITEM:
1507 case LFUN_INSET_ERT:
1508 case LFUN_INSET_FLOAT:
1509 case LFUN_INSET_FOOTNOTE:
1510 case LFUN_INSET_MARGINAL:
1511 case LFUN_INSET_MINIPAGE:
1512 case LFUN_INSET_OPTARG:
1513 case LFUN_INSET_WIDE_FLOAT:
1514 case LFUN_INSET_WRAP:
1515 case LFUN_TABULAR_INSERT:
1516 case LFUN_ENVIRONMENT_INSERT:
1517 // Open the inset, and move the current selection
1519 doInsertInset(this, cmd, true, true);
1522 case LFUN_INDEX_INSERT:
1523 // Just open the inset
1524 doInsertInset(this, cmd, true, false);
1527 case LFUN_INDEX_PRINT:
1528 case LFUN_TOC_INSERT:
1531 doInsertInset(this, cmd, false, false);
1535 return UNDISPATCHED;