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 "bufferparams.h"
22 #include "BufferView.h"
25 #include "dispatchresult.h"
27 #include "funcrequest.h"
33 #include "paragraph.h"
34 #include "ParagraphParameters.h"
35 #include "text_funcs.h"
39 #include "frontends/Dialogs.h"
40 #include "frontends/LyXView.h"
42 #include "insets/insetcommand.h"
43 #include "insets/insetnewline.h"
44 #include "insets/insetspecialchar.h"
45 #include "insets/insettext.h"
47 #include "support/lstrings.h"
48 #include "support/tostr.h"
50 #include "support/std_sstream.h"
53 using bv_funcs::replaceSelection;
57 using lyx::support::isStrUnsignedInt;
58 using lyx::support::strToUnsignedInt;
63 using std::istringstream;
67 extern string current_layout;
68 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
70 // the selection possible is needed, that only motion events are
71 // used, where the bottom press event was on the drawing area too
72 bool selection_possible = false;
77 void moveCursorUpdate(BufferView * bv, bool selecting)
79 LyXText * lt = bv->getLyXText();
81 // if (!lt->selection.set())
82 // lt->selection.cursor = lt->cursor;
84 if (selecting || lt->selection.mark())
87 if (!lt->selection.set())
88 bv->haveSelection(false);
95 void finishChange(BufferView * bv, bool selecting = false)
98 moveCursorUpdate(bv, selecting);
99 bv->owner()->view_state_changed();
102 // check if the given co-ordinates are inside an inset at the
103 // given cursor, if one exists. If so, the inset is returned,
104 // and the co-ordinates are made relative. Otherwise, 0 is returned.
105 InsetOld * checkInset(LyXText & text,
106 LyXCursor const & cur, int & x, int & y)
108 lyx::pos_type const pos = cur.pos();
109 ParagraphList::iterator par = text.getPar(cur);
111 if (pos >= par->size() || !par->isInset(pos))
114 InsetOld /*const*/ * inset = par->getInset(pos);
116 if (!isEditableInset(inset))
119 // get inset dimensions
120 BOOST_ASSERT(par->getInset(pos));
122 LyXFont const & font = text.getFont(par, pos);
124 int const width = inset->width();
125 int const inset_x = font.isVisibleRightToLeft()
126 ? (cur.x() - width) : cur.x();
129 inset_x + inset->scroll(),
131 cur.y() - inset->ascent(),
132 cur.y() + inset->descent()
135 if (!b.contains(x, y)) {
136 lyxerr[Debug::GUI] << "Missed inset at x,y "
138 << " box " << b << endl;
142 text.setCursor(cur.par(), pos, true);
145 // The origin of an inset is on the baseline
146 y -= text.cursor.y();
154 InsetOld * LyXText::checkInsetHit(int & x, int & y)
156 int y_tmp = y + bv_owner->top_y();
159 setCursorFromCoordinates(cur, x, y_tmp);
161 InsetOld * inset = checkInset(*this, cur, x, y_tmp);
167 // look at previous position
172 setCursor(cur, cur.par(), cur.pos() - 1, true);
174 inset = checkInset(*this, cur, x, y_tmp);
181 bool LyXText::gotoNextInset(vector<InsetOld::Code> const & codes,
182 string const & contents)
184 ParagraphList::iterator end = ownerParagraphs().end();
185 ParagraphList::iterator pit = cursorPar();
186 pos_type pos = cursor.pos();
190 if (pos + 1 < pit->size()) {
197 } while (pit != end &&
198 !(pit->isInset(pos) &&
199 (inset = pit->getInset(pos)) != 0 &&
200 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
202 static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
208 setCursor(parOffset(pit), pos, false);
213 void LyXText::gotoInset(vector<InsetOld::Code> const & codes,
216 bv()->beforeChange(this);
219 if (same_content && cursor.pos() < cursorPar()->size()
220 && cursorPar()->isInset(cursor.pos())) {
221 InsetOld const * inset = cursorPar()->getInset(cursor.pos());
222 if (find(codes.begin(), codes.end(), inset->lyxCode())
224 contents = static_cast<InsetCommand const *>(inset)->getContents();
227 if (!gotoNextInset(codes, contents)) {
228 if (cursor.pos() || cursorPar() != ownerParagraphs().begin()) {
229 LyXCursor tmp = cursor;
232 if (!gotoNextInset(codes, contents)) {
234 bv()->owner()->message(_("No more insets"));
237 bv()->owner()->message(_("No more insets"));
241 selection.cursor = cursor;
245 void LyXText::gotoInset(InsetOld::Code code, bool same_content)
247 gotoInset(vector<InsetOld::Code>(1, code), same_content);
251 void LyXText::cursorPrevious()
253 int y = bv()->top_y();
255 ParagraphList::iterator cpit = cursorPar();
256 RowList::iterator crit = cpit->getRow(cursor.pos());
258 if (isFirstRow(cpit, *crit)) {
260 bv()->updateScrollbar();
264 setCursorFromCoordinates(bv()->x_target(), y);
267 if (crit == bv()->text->cursorRow()) {
268 // we have a row which is taller than the workarea. The
269 // simplest solution is to move to the previous row instead.
274 int new_y = + crit->height() - bv()->workHeight() + 1;
277 new_y += bv()->text->cursor.y()
278 + bv()->theLockingInset()->insetInInsetY() + y;
280 new_y += cursor.y() - crit->baseline();
283 previousRow(cpit, crit);
285 setCursor(cur, parOffset(cpit), crit->pos(), false);
286 if (cur.y() > bv()->top_y())
288 bv()->updateScrollbar();
292 void LyXText::cursorNext()
294 int topy = bv()->top_y();
296 ParagraphList::iterator cpit = cursorPar();
297 RowList::iterator crit = cpit->getRow(cursor.pos());
299 if (isLastRow(cpit, *crit)) {
300 int y = cursor.y() - crit->baseline() + crit->height();
301 if (y > topy + bv()->workHeight())
302 bv()->updateScrollbar();
306 int y = topy + bv()->workHeight();
307 if (inset_owner && !topy) {
308 y -= (bv()->text->cursor.y()
310 + bv()->theLockingInset()->insetInInsetY());
313 ParagraphList::iterator dummypit;
314 Row const & rr = *getRowNearY(y, dummypit);
315 y = dummypit->y + rr.y_offset();
317 setCursorFromCoordinates(bv()->x_target(), y);
318 // + bv->workHeight());
322 if (crit == bv()->text->cursorRow()) {
323 // we have a row which is taller than the workarea. The
324 // simplest solution is to move to the next row instead.
327 // This is what we used to do, so we wouldn't skip right past
328 // tall rows, but it's not working right now.
330 new_y = bv->top_y() + bv->workHeight();
335 new_y = bv()->text->cursor.y()
336 + bv()->theLockingInset()->insetInInsetY()
337 + y - crit->baseline();
339 new_y = cursor.y() - crit->baseline();
345 setCursor(cur, parOffset(cpit), crit->pos(), false);
346 if (cur.y() < bv_owner->top_y() + bv()->workHeight())
348 bv()->updateScrollbar();
354 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
357 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
358 replaceSelection(lt);
359 if (!bv->insertInset(new_inset))
362 bv->updateInset(new_inset);
366 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
367 bool edit, bool pastesel)
369 InsetOld * inset = createInset(cmd);
370 BufferView * bv = cmd.view();
374 if (lt->selection.set()) {
375 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
378 if (bv->insertInset(inset)) {
380 inset->edit(bv, true);
381 bv->cursor().push(inset, inset->getText(0));
383 if (gotsel && pastesel)
384 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
394 DispatchResult LyXText::dispatch(FuncRequest const & cmd)
396 lyxerr[Debug::ACTION] << "LyXText::dispatch: action[" << cmd.action
397 <<"] arg[" << cmd.argument << ']' << "xy[" <<
398 cmd.x << ',' << cmd.y << ']' << endl;
400 BufferView * bv = cmd.view();
402 switch (cmd.action) {
404 case LFUN_APPENDIX: {
405 ParagraphList::iterator pit = cursorPar();
406 bool start = !pit->params().startOfAppendix();
408 // ensure that we have only one start_of_appendix in this document
409 ParagraphList::iterator tmp = ownerParagraphs().begin();
410 ParagraphList::iterator end = ownerParagraphs().end();
412 for (; tmp != end; ++tmp) {
413 if (tmp->params().startOfAppendix()) {
414 recUndo(parOffset(tmp));
415 tmp->params().startOfAppendix(false);
421 recUndo(parOffset(pit));
422 pit->params().startOfAppendix(start);
424 // we can set the refreshing parameters now
426 redoParagraph(cursorPar());
427 setCursor(cursorPar(), cursor.pos());
432 case LFUN_DELETE_WORD_FORWARD:
433 bv->beforeChange(this);
438 case LFUN_DELETE_WORD_BACKWARD:
439 bv->beforeChange(this);
440 deleteWordBackward();
444 case LFUN_DELETE_LINE_FORWARD:
445 bv->beforeChange(this);
451 if (!selection.mark())
452 bv->beforeChange(this);
453 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
456 cursorRightOneWord();
461 if (!selection.mark())
462 bv->beforeChange(this);
463 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
464 cursorRightOneWord();
470 case LFUN_BEGINNINGBUF:
471 if (!selection.mark())
472 bv->beforeChange(this);
478 if (selection.mark())
479 bv->beforeChange(this);
485 if (!selection.set())
486 selection.cursor = cursor;
487 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
491 finishChange(bv, true);
495 if (!selection.set())
496 selection.cursor = cursor;
497 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
501 finishChange(bv, true);
505 if (!selection.set())
506 selection.cursor = cursor;
508 finishChange(bv, true);
512 if (!selection.set())
513 selection.cursor = cursor;
515 finishChange(bv, true);
518 case LFUN_UP_PARAGRAPHSEL:
519 if (!selection.set())
520 selection.cursor = cursor;
522 finishChange(bv, true);
525 case LFUN_DOWN_PARAGRAPHSEL:
526 if (!selection.set())
527 selection.cursor = cursor;
528 cursorDownParagraph();
529 finishChange(bv, true);
533 if (!selection.set())
534 selection.cursor = cursor;
536 finishChange(bv, true);
540 if (!selection.set())
541 selection.cursor = cursor;
543 finishChange(bv, true);
547 if (!selection.set())
548 selection.cursor = cursor;
550 finishChange(bv, true);
554 if (!selection.set())
555 selection.cursor = cursor;
557 finishChange(bv, true);
560 case LFUN_WORDRIGHTSEL:
561 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
564 cursorRightOneWord();
565 finishChange(bv, true);
568 case LFUN_WORDLEFTSEL:
569 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
570 cursorRightOneWord();
573 finishChange(bv, true);
577 LyXCursor cur1 = cursor;
579 ::getWord(*this, cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs());
580 setCursor(cur1.par(), cur1.pos());
581 bv->beforeChange(this);
582 setCursor(cur2.par(), cur2.pos());
583 finishChange(bv, true);
588 bool is_rtl = cursorPar()->isRightToLeftPar(bv->buffer()->params());
589 if (!selection.mark())
590 bv->beforeChange(this);
593 if (cursor.pos() < cursorPar()->size()
594 && cursorPar()->isInset(cursor.pos())
595 && isHighlyEditableInset(cursorPar()->getInset(cursor.pos()))) {
596 InsetOld * tmpinset = cursorPar()->getInset(cursor.pos());
597 cmd.message(tmpinset->editMessage());
598 tmpinset->edit(bv, !is_rtl);
599 bv->cursor().push(tmpinset, tmpinset->getText(0));
609 // This is soooo ugly. Isn`t it possible to make
611 bool const is_rtl = cursorPar()->isRightToLeftPar(bv->buffer()->params());
612 if (!selection.mark())
613 bv->beforeChange(this);
614 LyXCursor const cur = cursor;
617 if ((is_rtl || cur != cursor) && // only if really moved!
618 cursor.pos() < cursorPar()->size() &&
619 cursorPar()->isInset(cursor.pos()) &&
620 isHighlyEditableInset(cursorPar()->getInset(cursor.pos()))) {
621 InsetOld * tmpinset = cursorPar()->getInset(cursor.pos());
622 cmd.message(tmpinset->editMessage());
623 tmpinset->edit(bv, is_rtl);
624 bv->cursor().push(tmpinset, tmpinset->getText(0));
634 if (!selection.mark())
635 bv->beforeChange(this);
641 if (!selection.mark())
642 bv->beforeChange(this);
647 case LFUN_UP_PARAGRAPH:
648 if (!selection.mark())
649 bv->beforeChange(this);
654 case LFUN_DOWN_PARAGRAPH:
655 if (!selection.mark())
656 bv->beforeChange(this);
657 cursorDownParagraph();
658 finishChange(bv, false);
662 if (!selection.mark())
663 bv->beforeChange(this);
665 finishChange(bv, false);
669 if (!selection.mark())
670 bv->beforeChange(this);
672 finishChange(bv, false);
676 if (!selection.mark())
677 bv->beforeChange(this);
679 finishChange(bv, false);
683 if (!selection.mark())
684 bv->beforeChange(this);
686 finishChange(bv, false);
689 case LFUN_BREAKLINE: {
690 lyx::pos_type body = cursorPar()->beginningOfBody();
692 // Not allowed by LaTeX (labels or empty par)
693 if (cursor.pos() <= body)
696 replaceSelection(bv->getLyXText());
697 insertInset(new InsetNewline);
698 setCursor(cursorPar(), cursor.pos());
699 moveCursorUpdate(bv, false);
704 if (!selection.set()) {
706 selection.cursor = cursor;
707 // It is possible to make it a lot faster still
708 // just comment out the line below...
710 cutSelection(true, false);
712 moveCursorUpdate(bv, false);
713 bv->owner()->view_state_changed();
716 case LFUN_DELETE_SKIP:
717 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
718 if (!selection.set()) {
719 if (cursor.pos() == cursorPar()->size()) {
721 ParagraphParameters & params = cursorPar()->params();
722 if (cursor.pos() == 0
723 && !(params.spaceTop() == VSpace (VSpace::NONE))) {
725 VSpace(VSpace::NONE),
726 params.spaceBottom(),
729 params.labelWidthString(), 0);
734 selection.cursor = cursor;
738 selection.cursor = cursor;
741 cutSelection(true, false);
748 if (!selection.set()) {
749 if (bv->owner()->getIntl().getTransManager().backspace()) {
751 selection.cursor = cursor;
752 // It is possible to make it a lot faster still
753 // just comment out the line below...
756 cutSelection(true, false);
758 bv->owner()->view_state_changed();
763 case LFUN_BACKSPACE_SKIP:
764 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
765 if (!selection.set()) {
766 ParagraphParameters & params = cursorPar()->params();
767 if (cursor.pos() == 0 && !(params.spaceTop() == VSpace(VSpace::NONE))) {
769 VSpace(VSpace::NONE),
770 params.spaceBottom(),
773 params.labelWidthString(), 0);
775 LyXCursor cur = cursor;
777 selection.cursor = cur;
780 cutSelection(true, false);
785 case LFUN_BREAKPARAGRAPH:
786 replaceSelection(bv->getLyXText());
787 breakParagraph(bv->buffer()->paragraphs(), 0);
789 selection.cursor = cursor;
791 bv->owner()->view_state_changed();
794 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
795 replaceSelection(bv->getLyXText());
796 breakParagraph(bv->buffer()->paragraphs(), 1);
798 selection.cursor = cursor;
800 bv->owner()->view_state_changed();
803 case LFUN_BREAKPARAGRAPH_SKIP: {
804 // When at the beginning of a paragraph, remove
805 // indentation and add a "defskip" at the top.
806 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
807 LyXCursor cur = cursor;
808 replaceSelection(bv->getLyXText());
809 if (cur.pos() == 0) {
810 ParagraphParameters & params = getPar(cur)->params();
811 if (params.spaceTop() == VSpace(VSpace::NONE)) {
813 VSpace(VSpace::DEFSKIP), params.spaceBottom(),
816 params.labelWidthString(), 1);
820 breakParagraph(bv->buffer()->paragraphs(), 0);
823 selection.cursor = cur;
825 bv->owner()->view_state_changed();
829 case LFUN_PARAGRAPH_SPACING: {
830 ParagraphList::iterator pit = cursorPar();
831 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
832 float cur_value = 1.0;
833 if (cur_spacing == Spacing::Other)
834 cur_value = pit->params().spacing().getValue();
836 istringstream is(cmd.argument);
839 Spacing::Space new_spacing = cur_spacing;
840 float new_value = cur_value;
842 lyxerr << "Missing argument to `paragraph-spacing'"
844 } else if (tmp == "single") {
845 new_spacing = Spacing::Single;
846 } else if (tmp == "onehalf") {
847 new_spacing = Spacing::Onehalf;
848 } else if (tmp == "double") {
849 new_spacing = Spacing::Double;
850 } else if (tmp == "other") {
851 new_spacing = Spacing::Other;
854 lyxerr << "new_value = " << tmpval << endl;
857 } else if (tmp == "default") {
858 new_spacing = Spacing::Default;
860 lyxerr << _("Unknown spacing argument: ")
861 << cmd.argument << endl;
863 if (cur_spacing != new_spacing || cur_value != new_value) {
864 pit->params().spacing(Spacing(new_spacing, new_value));
871 case LFUN_INSET_SETTINGS:
872 BOOST_ASSERT(bv->theLockingInset());
873 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
876 case LFUN_INSET_TOGGLE:
877 bv->beforeChange(this);
883 case LFUN_SPACE_INSERT:
884 if (cursorPar()->layout()->free_spacing)
887 doInsertInset(this, cmd, false, false);
888 moveCursorUpdate(bv, false);
891 case LFUN_HYPHENATION:
892 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
895 case LFUN_LIGATURE_BREAK:
896 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
900 specialChar(this, bv, InsetSpecialChar::LDOTS);
903 case LFUN_END_OF_SENTENCE:
904 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
907 case LFUN_MENU_SEPARATOR:
908 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
912 bv->beforeChange(this);
914 selection.cursor = cursor;
915 cmd.message(N_("Mark off"));
919 bv->beforeChange(this);
920 selection.mark(true);
922 selection.cursor = cursor;
923 cmd.message(N_("Mark on"));
927 bv->beforeChange(this);
928 if (selection.mark()) {
929 cmd.message(N_("Mark removed"));
931 selection.mark(true);
932 cmd.message(N_("Mark set"));
934 selection.cursor = cursor;
938 case LFUN_UPCASE_WORD:
939 changeCase(LyXText::text_uppercase);
943 case LFUN_LOWCASE_WORD:
944 changeCase(LyXText::text_lowercase);
948 case LFUN_CAPITALIZE_WORD:
949 changeCase(LyXText::text_capitalization);
953 case LFUN_TRANSPOSE_CHARS:
954 recUndo(cursor.par());
960 cmd.message(_("Paste"));
961 replaceSelection(bv->getLyXText());
962 #warning FIXME Check if the arg is in the domain of available selections.
963 if (isStrUnsignedInt(cmd.argument))
964 pasteSelection(strToUnsignedInt(cmd.argument));
967 clearSelection(); // bug 393
974 cutSelection(true, true);
975 cmd.message(_("Cut"));
981 cmd.message(_("Copy"));
984 case LFUN_BEGINNINGBUFSEL:
986 return DispatchResult(false);
988 finishChange(bv, true);
993 return DispatchResult(false);
995 finishChange(bv, true);
999 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1005 istringstream is(cmd.argument);
1008 lyxerr << "SETXY: Could not parse coordinates in '"
1009 << cmd.argument << std::endl;
1011 setCursorFromCoordinates(x, y);
1016 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1018 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1024 case LFUN_GETLAYOUT:
1025 cmd.message(tostr(cursorPar()->layout()));
1029 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1030 << cmd.argument << endl;
1032 // This is not the good solution to the empty argument
1033 // problem, but it will hopefully suffice for 1.2.0.
1034 // The correct solution would be to augument the
1035 // function list/array with information about what
1036 // functions needs arguments and their type.
1037 if (cmd.argument.empty()) {
1038 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1042 // Derive layout number from given argument (string)
1043 // and current buffer's textclass (number)
1044 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1045 bool hasLayout = tclass.hasLayout(cmd.argument);
1046 string layout = cmd.argument;
1048 // If the entry is obsolete, use the new one instead.
1050 string const & obs = tclass[layout]->obsoleted_by();
1056 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1061 bool change_layout = (current_layout != layout);
1063 if (!change_layout && selection.set() &&
1064 selection.start.par() != selection.end.par())
1066 ParagraphList::iterator spit = getPar(selection.start);
1067 ParagraphList::iterator epit = boost::next(getPar(selection.end));
1068 while (spit != epit) {
1069 if (spit->layout()->name() != current_layout) {
1070 change_layout = true;
1077 if (change_layout) {
1078 current_layout = layout;
1080 bv->owner()->setLayout(layout);
1087 case LFUN_PASTESELECTION: {
1088 // this was originally a beforeChange(bv->text), i.e
1089 // the outermost LyXText!
1090 bv->beforeChange(this);
1091 string const clip = bv->getClipboard();
1092 if (!clip.empty()) {
1093 if (cmd.argument == "paragraph")
1094 insertStringAsParagraphs(clip);
1096 insertStringAsLines(clip);
1103 case LFUN_GOTOERROR:
1104 gotoInset(InsetOld::ERROR_CODE, false);
1108 gotoInset(InsetOld::NOTE_CODE, false);
1111 case LFUN_REFERENCE_GOTO:
1113 vector<InsetOld::Code> tmp;
1114 tmp.push_back(InsetOld::LABEL_CODE);
1115 tmp.push_back(InsetOld::REF_CODE);
1116 gotoInset(tmp, true);
1121 replaceSelection(bv->getLyXText());
1122 ParagraphList::iterator pit = cursorPar();
1123 lyx::pos_type pos = cursor.pos();
1127 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1130 c = pit->getChar(pos - 1);
1132 LyXLayout_ptr const & style = pit->layout();
1134 BufferParams const & bufparams = bv->buffer()->params();
1135 if (style->pass_thru ||
1136 pit->getFontSettings(bufparams,pos).language()->lang() == "hebrew" ||
1137 !bv->insertInset(new InsetQuotes(c, bufparams)))
1138 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1142 case LFUN_DATE_INSERT: {
1143 replaceSelection(bv->getLyXText());
1144 time_t now_time_t = time(NULL);
1145 struct tm * now_tm = localtime(&now_time_t);
1146 setlocale(LC_TIME, "");
1148 if (!cmd.argument.empty())
1151 arg = lyxrc.date_insert_format;
1153 int const datetmp_len =
1154 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1156 for (int i = 0; i < datetmp_len; i++)
1157 insertChar(datetmp[i]);
1159 selection.cursor = cursor;
1160 moveCursorUpdate(bv, false);
1164 case LFUN_MOUSE_TRIPLE:
1167 if (!isInInset() && bv->theLockingInset())
1169 if (cmd.button() == mouse_button::button1) {
1171 selection.cursor = cursor;
1175 bv->haveSelection(selection.set());
1179 case LFUN_MOUSE_DOUBLE:
1182 if (!isInInset() && bv->theLockingInset())
1184 if (cmd.button() == mouse_button::button1) {
1185 selectWord(lyx::WHOLE_WORD_STRICT);
1187 bv->haveSelection(selection.set());
1191 case LFUN_MOUSE_MOTION:
1193 // Only use motion with button 1
1194 //if (ev.button() != mouse_button::button1)
1200 // Check for inset locking
1201 if (bv->theLockingInset()) {
1202 InsetOld * tli = bv->theLockingInset();
1203 LyXCursor cursor = bv->text->cursor;
1204 LyXFont font = bv->text->getFont(bv->text->cursorPar(), cursor.pos());
1205 int width = tli->width();
1206 int inset_x = font.isVisibleRightToLeft()
1207 ? cursor.x() - width : cursor.x();
1208 int start_x = inset_x + tli->scroll();
1209 FuncRequest cmd1 = cmd;
1210 cmd1.x = cmd.x - start_x;
1211 cmd1.y = cmd.y - cursor.y() + bv->top_y();
1212 tli->dispatch(cmd1);
1216 // The test for not selection possible is needed, that only motion
1217 // events are used, where the bottom press event was on
1218 // the drawing area too
1219 if (!selection_possible) {
1220 lyxerr[Debug::ACTION]
1221 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1225 RowList::iterator cursorrow = bv->text->cursorRow();
1226 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->top_y());
1228 // sorry for this but I have a strange error that the y value jumps at
1229 // a certain point. This seems like an error in my xforms library or
1230 // in some other local environment, but I would like to leave this here
1231 // for the moment until I can remove this (Jug 20020418)
1232 if (y_before < bv->text->cursor.y())
1233 lyxerr << y_before << ':'
1234 << bv->text->cursor.y() << endl;
1236 // This is to allow jumping over large insets
1237 if (cursorrow == bv->text->cursorRow()) {
1238 if (cmd.y >= bv->workHeight())
1239 bv->text->cursorDown(false);
1241 bv->text->cursorUp(false);
1244 bv->text->setSelection();
1249 // Single-click on work area
1250 case LFUN_MOUSE_PRESS:
1255 // ok ok, this is a hack (for xforms)
1256 // We shouldn't go further down as we really should only do the
1257 // scrolling and be done with this. Otherwise we may open some
1258 // dialogs (Jug 20020424).
1259 if (cmd.button() == mouse_button::button4) {
1260 bv->scroll(-lyxrc.wheel_jump);
1263 if (cmd.button() == mouse_button::button5) {
1264 bv->scroll(lyxrc.wheel_jump);
1270 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1272 // Middle button press pastes if we have a selection
1273 // We do this here as if the selection was inside an inset
1274 // it could get cleared on the unlocking of the inset so
1275 // we have to check this first
1276 bool paste_internally = false;
1277 if (cmd.button() == mouse_button::button2 && selection.set()) {
1278 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1279 paste_internally = true;
1282 int const screen_first = bv->top_y();
1284 if (bv->theLockingInset()) {
1285 // We are in inset locking mode
1287 // Check whether the inset was hit. If not reset mode,
1288 // otherwise give the event to the inset
1289 if (inset_hit == bv->theLockingInset()) {
1290 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1291 bv->theLockingInset()->dispatch(cmd1);
1294 bv->unlockInset(bv->theLockingInset());
1298 selection_possible = true;
1300 // Clear the selection
1301 bv->text->clearSelection();
1303 bv->updateScrollbar();
1305 // Single left click in math inset?
1306 if (isHighlyEditableInset(inset_hit)) {
1307 // Highly editable inset, like math
1308 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1309 selection_possible = false;
1310 bv->owner()->message(inset->editMessage());
1311 // We just have to lock the inset before calling a PressEvent on it!
1312 if (!bv->lockInset(inset))
1313 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1314 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1315 inset->dispatch(cmd1);
1318 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1320 // Right click on a footnote flag opens float menu
1321 if (cmd.button() == mouse_button::button3) {
1322 selection_possible = false;
1326 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1327 bv->text->setCursorFromCoordinates(x, y + screen_first);
1329 bv->text->selection.cursor = bv->text->cursor;
1330 bv->x_target(bv->text->cursor.x());
1332 if (bv->fitCursor())
1333 selection_possible = false;
1335 // Insert primary selection with middle mouse
1336 // if there is a local selection in the current buffer,
1338 if (cmd.button() == mouse_button::button2) {
1339 if (paste_internally)
1340 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1342 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1343 selection_possible = false;
1348 case LFUN_MOUSE_RELEASE:
1350 // do nothing if we used the mouse wheel
1354 if (cmd.button() == mouse_button::button4
1355 || cmd.button() == mouse_button::button5)
1358 // If we hit an inset, we have the inset coordinates in these
1359 // and inset_hit points to the inset. If we do not hit an
1360 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1363 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1365 if (bv->theLockingInset()) {
1366 // We are in inset locking mode.
1368 // LyX does a kind of work-area grabbing for insets.
1369 // Only a ButtonPress FuncRequest outside the inset will
1370 // force a insetUnlock.
1371 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1372 bv->theLockingInset()->dispatch(cmd1);
1376 selection_possible = false;
1378 if (cmd.button() == mouse_button::button2)
1382 if (cmd.button() == mouse_button::button1)
1383 bv->haveSelection(selection.set());
1386 bv->owner()->view_state_changed();
1387 bv->owner()->updateMenubar();
1388 bv->owner()->updateToolbar();
1390 // Did we hit an editable inset?
1392 selection_possible = false;
1394 // if we reach this point with a selection, it
1395 // must mean we are currently selecting.
1396 // But we don't want to open the inset
1397 // because that is annoying for the user.
1398 // So just pretend we didn't hit it.
1399 // this is OK because a "kosher" ButtonRelease
1400 // will follow a ButtonPress that clears
1402 // Note this also fixes selection drawing
1403 // problems if we end up opening an inset
1404 if (selection.set())
1407 // CHECK fix this proper in 0.13
1408 // well, maybe 13.0 !!!!!!!!!
1410 // Following a ref shouldn't issue
1411 // a push on the undo-stack
1412 // anylonger, now that we have
1413 // keybindings for following
1414 // references and returning from
1415 // references. IMHO though, it
1416 // should be the inset's own business
1417 // to push or not push on the undo
1418 // stack. They don't *have* to
1419 // alter the document...
1421 // ...or maybe the recordUndo()
1422 // below isn't necessary at all anylonger?
1423 if (inset_hit->lyxCode() == InsetOld::REF_CODE)
1424 recUndo(cursor.par());
1426 bv->owner()->message(inset_hit->editMessage());
1428 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1429 inset_hit->dispatch(cmd1);
1435 case LFUN_SELFINSERT: {
1436 if (cmd.argument.empty())
1439 // Automatically delete the currently selected
1440 // text and replace it with what is being
1441 // typed in now. Depends on lyxrc settings
1442 // "auto_region_delete", which defaults to
1445 if (lyxrc.auto_region_delete) {
1446 if (selection.set())
1447 cutSelection(false, false);
1448 bv->haveSelection(false);
1451 bv->beforeChange(this);
1452 LyXFont const old_font = real_current_font;
1454 string::const_iterator cit = cmd.argument.begin();
1455 string::const_iterator end = cmd.argument.end();
1456 for (; cit != end; ++cit)
1457 bv->owner()->getIntl().getTransManager().
1458 TranslateAndInsert(*cit, this);
1460 selection.cursor = cursor;
1461 moveCursorUpdate(bv, false);
1463 // real_current_font.number can change so we need to
1464 // update the minibuffer
1465 if (old_font != real_current_font)
1466 bv->owner()->view_state_changed();
1467 bv->updateScrollbar();
1471 case LFUN_HTMLURL: {
1472 InsetCommandParams p("htmlurl");
1473 string const data = InsetCommandMailer::params2string("url", p);
1474 bv->owner()->getDialogs().show("url", data, 0);
1479 InsetCommandParams p("url");
1480 string const data = InsetCommandMailer::params2string("url", p);
1481 bv->owner()->getDialogs().show("url", data, 0);
1487 case LFUN_INSET_LIST:
1488 case LFUN_INSET_THEOREM:
1489 case LFUN_INSET_CAPTION:
1491 case LFUN_INSERT_NOTE:
1492 case LFUN_INSERT_BOX:
1493 case LFUN_INSERT_BRANCH:
1494 case LFUN_INSERT_BIBITEM:
1495 case LFUN_INSET_ERT:
1496 case LFUN_INSET_FLOAT:
1497 case LFUN_INSET_FOOTNOTE:
1498 case LFUN_INSET_MARGINAL:
1499 case LFUN_INSET_MINIPAGE:
1500 case LFUN_INSET_OPTARG:
1501 case LFUN_INSET_WIDE_FLOAT:
1502 case LFUN_INSET_WRAP:
1503 case LFUN_TABULAR_INSERT:
1504 case LFUN_ENVIRONMENT_INSERT:
1505 // Open the inset, and move the current selection
1507 doInsertInset(this, cmd, true, true);
1510 case LFUN_INDEX_INSERT:
1511 // Just open the inset
1512 doInsertInset(this, cmd, true, false);
1515 case LFUN_INDEX_PRINT:
1516 case LFUN_TOC_INSERT:
1518 case LFUN_INSERT_LINE:
1519 case LFUN_INSERT_PAGEBREAK:
1521 doInsertInset(this, cmd, false, false);
1524 case LFUN_DEPTH_MIN:
1525 bv_funcs::changeDepth(bv, this, bv_funcs::DEC_DEPTH, false);
1530 case LFUN_DEPTH_PLUS:
1532 bv_funcs::changeDepth(bv, this, bv_funcs::INC_DEPTH, false);
1538 return DispatchResult(false);
1541 return DispatchResult(true, true);