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 if (gotsel && pastesel)
382 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
392 DispatchResult LyXText::dispatch(FuncRequest const & cmd)
394 lyxerr[Debug::ACTION] << "LyXText::dispatch: action[" << cmd.action
395 <<"] arg[" << cmd.argument << ']' << "xy[" <<
396 cmd.x << ',' << cmd.y << ']' << endl;
398 BufferView * bv = cmd.view();
400 switch (cmd.action) {
402 case LFUN_APPENDIX: {
403 ParagraphList::iterator pit = cursorPar();
404 bool start = !pit->params().startOfAppendix();
406 // ensure that we have only one start_of_appendix in this document
407 ParagraphList::iterator tmp = ownerParagraphs().begin();
408 ParagraphList::iterator end = ownerParagraphs().end();
410 for (; tmp != end; ++tmp) {
411 if (tmp->params().startOfAppendix()) {
412 recUndo(parOffset(tmp));
413 tmp->params().startOfAppendix(false);
419 recUndo(parOffset(pit));
420 pit->params().startOfAppendix(start);
422 // we can set the refreshing parameters now
424 redoParagraph(cursorPar());
425 setCursor(cursorPar(), cursor.pos());
430 case LFUN_DELETE_WORD_FORWARD:
431 bv->beforeChange(this);
436 case LFUN_DELETE_WORD_BACKWARD:
437 bv->beforeChange(this);
438 deleteWordBackward();
442 case LFUN_DELETE_LINE_FORWARD:
443 bv->beforeChange(this);
449 if (!selection.mark())
450 bv->beforeChange(this);
451 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
454 cursorRightOneWord();
459 if (!selection.mark())
460 bv->beforeChange(this);
461 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
462 cursorRightOneWord();
468 case LFUN_BEGINNINGBUF:
469 if (!selection.mark())
470 bv->beforeChange(this);
476 if (selection.mark())
477 bv->beforeChange(this);
483 if (!selection.set())
484 selection.cursor = cursor;
485 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
489 finishChange(bv, true);
493 if (!selection.set())
494 selection.cursor = cursor;
495 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
499 finishChange(bv, true);
503 if (!selection.set())
504 selection.cursor = cursor;
506 finishChange(bv, true);
510 if (!selection.set())
511 selection.cursor = cursor;
513 finishChange(bv, true);
516 case LFUN_UP_PARAGRAPHSEL:
517 if (!selection.set())
518 selection.cursor = cursor;
520 finishChange(bv, true);
523 case LFUN_DOWN_PARAGRAPHSEL:
524 if (!selection.set())
525 selection.cursor = cursor;
526 cursorDownParagraph();
527 finishChange(bv, true);
531 if (!selection.set())
532 selection.cursor = cursor;
534 finishChange(bv, true);
538 if (!selection.set())
539 selection.cursor = cursor;
541 finishChange(bv, true);
545 if (!selection.set())
546 selection.cursor = cursor;
548 finishChange(bv, true);
552 if (!selection.set())
553 selection.cursor = cursor;
555 finishChange(bv, true);
558 case LFUN_WORDRIGHTSEL:
559 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
562 cursorRightOneWord();
563 finishChange(bv, true);
566 case LFUN_WORDLEFTSEL:
567 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
568 cursorRightOneWord();
571 finishChange(bv, true);
575 LyXCursor cur1 = cursor;
577 ::getWord(*this, cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs());
578 setCursor(cur1.par(), cur1.pos());
579 bv->beforeChange(this);
580 setCursor(cur2.par(), cur2.pos());
581 finishChange(bv, true);
586 bool is_rtl = cursorPar()->isRightToLeftPar(bv->buffer()->params());
587 if (!selection.mark())
588 bv->beforeChange(this);
591 if (cursor.pos() < cursorPar()->size()
592 && cursorPar()->isInset(cursor.pos())
593 && isHighlyEditableInset(cursorPar()->getInset(cursor.pos()))) {
594 InsetOld * tmpinset = cursorPar()->getInset(cursor.pos());
595 cmd.message(tmpinset->editMessage());
596 tmpinset->edit(bv, !is_rtl);
606 // This is soooo ugly. Isn`t it possible to make
608 bool const is_rtl = cursorPar()->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() < cursorPar()->size() &&
616 cursorPar()->isInset(cursor.pos()) &&
617 isHighlyEditableInset(cursorPar()->getInset(cursor.pos()))) {
618 InsetOld * tmpinset = cursorPar()->getInset(cursor.pos());
619 cmd.message(tmpinset->editMessage());
620 tmpinset->edit(bv, is_rtl);
621 bv->cursor().push(tmpinset, tmpinset->getText(0));
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 = cursorPar()->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(cursorPar(), 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 if (cursor.pos() == cursorPar()->size()) {
718 ParagraphParameters & params = cursorPar()->params();
719 if (cursor.pos() == 0
720 && !(params.spaceTop() == VSpace (VSpace::NONE))) {
722 VSpace(VSpace::NONE),
723 params.spaceBottom(),
726 params.labelWidthString(), 0);
731 selection.cursor = cursor;
735 selection.cursor = cursor;
738 cutSelection(true, false);
745 if (!selection.set()) {
746 if (bv->owner()->getIntl().getTransManager().backspace()) {
748 selection.cursor = cursor;
749 // It is possible to make it a lot faster still
750 // just comment out the line below...
753 cutSelection(true, false);
755 bv->owner()->view_state_changed();
760 case LFUN_BACKSPACE_SKIP:
761 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
762 if (!selection.set()) {
763 ParagraphParameters & params = cursorPar()->params();
764 if (cursor.pos() == 0 && !(params.spaceTop() == VSpace(VSpace::NONE))) {
766 VSpace(VSpace::NONE),
767 params.spaceBottom(),
770 params.labelWidthString(), 0);
772 LyXCursor cur = cursor;
774 selection.cursor = cur;
777 cutSelection(true, false);
782 case LFUN_BREAKPARAGRAPH:
783 replaceSelection(bv->getLyXText());
784 breakParagraph(bv->buffer()->paragraphs(), 0);
786 selection.cursor = cursor;
788 bv->owner()->view_state_changed();
791 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
792 replaceSelection(bv->getLyXText());
793 breakParagraph(bv->buffer()->paragraphs(), 1);
795 selection.cursor = cursor;
797 bv->owner()->view_state_changed();
800 case LFUN_BREAKPARAGRAPH_SKIP: {
801 // When at the beginning of a paragraph, remove
802 // indentation and add a "defskip" at the top.
803 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
804 LyXCursor cur = cursor;
805 replaceSelection(bv->getLyXText());
806 if (cur.pos() == 0) {
807 ParagraphParameters & params = getPar(cur)->params();
808 if (params.spaceTop() == VSpace(VSpace::NONE)) {
810 VSpace(VSpace::DEFSKIP), params.spaceBottom(),
813 params.labelWidthString(), 1);
817 breakParagraph(bv->buffer()->paragraphs(), 0);
820 selection.cursor = cur;
822 bv->owner()->view_state_changed();
826 case LFUN_PARAGRAPH_SPACING: {
827 ParagraphList::iterator pit = cursorPar();
828 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
829 float cur_value = 1.0;
830 if (cur_spacing == Spacing::Other)
831 cur_value = pit->params().spacing().getValue();
833 istringstream is(cmd.argument);
836 Spacing::Space new_spacing = cur_spacing;
837 float new_value = cur_value;
839 lyxerr << "Missing argument to `paragraph-spacing'"
841 } else if (tmp == "single") {
842 new_spacing = Spacing::Single;
843 } else if (tmp == "onehalf") {
844 new_spacing = Spacing::Onehalf;
845 } else if (tmp == "double") {
846 new_spacing = Spacing::Double;
847 } else if (tmp == "other") {
848 new_spacing = Spacing::Other;
851 lyxerr << "new_value = " << tmpval << endl;
854 } else if (tmp == "default") {
855 new_spacing = Spacing::Default;
857 lyxerr << _("Unknown spacing argument: ")
858 << cmd.argument << endl;
860 if (cur_spacing != new_spacing || cur_value != new_value) {
861 pit->params().spacing(Spacing(new_spacing, new_value));
868 case LFUN_INSET_SETTINGS:
869 BOOST_ASSERT(bv->theLockingInset());
870 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
873 case LFUN_INSET_TOGGLE:
874 bv->beforeChange(this);
880 case LFUN_SPACE_INSERT:
881 if (cursorPar()->layout()->free_spacing)
884 doInsertInset(this, cmd, false, false);
885 moveCursorUpdate(bv, false);
888 case LFUN_HYPHENATION:
889 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
892 case LFUN_LIGATURE_BREAK:
893 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
897 specialChar(this, bv, InsetSpecialChar::LDOTS);
900 case LFUN_END_OF_SENTENCE:
901 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
904 case LFUN_MENU_SEPARATOR:
905 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
909 bv->beforeChange(this);
911 selection.cursor = cursor;
912 cmd.message(N_("Mark off"));
916 bv->beforeChange(this);
917 selection.mark(true);
919 selection.cursor = cursor;
920 cmd.message(N_("Mark on"));
924 bv->beforeChange(this);
925 if (selection.mark()) {
926 cmd.message(N_("Mark removed"));
928 selection.mark(true);
929 cmd.message(N_("Mark set"));
931 selection.cursor = cursor;
935 case LFUN_UPCASE_WORD:
936 changeCase(LyXText::text_uppercase);
940 case LFUN_LOWCASE_WORD:
941 changeCase(LyXText::text_lowercase);
945 case LFUN_CAPITALIZE_WORD:
946 changeCase(LyXText::text_capitalization);
950 case LFUN_TRANSPOSE_CHARS:
951 recUndo(cursor.par());
957 cmd.message(_("Paste"));
958 replaceSelection(bv->getLyXText());
959 #warning FIXME Check if the arg is in the domain of available selections.
960 if (isStrUnsignedInt(cmd.argument))
961 pasteSelection(strToUnsignedInt(cmd.argument));
964 clearSelection(); // bug 393
971 cutSelection(true, true);
972 cmd.message(_("Cut"));
978 cmd.message(_("Copy"));
981 case LFUN_BEGINNINGBUFSEL:
983 return DispatchResult(false);
985 finishChange(bv, true);
990 return DispatchResult(false);
992 finishChange(bv, true);
996 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1002 istringstream is(cmd.argument);
1005 lyxerr << "SETXY: Could not parse coordinates in '"
1006 << cmd.argument << std::endl;
1008 setCursorFromCoordinates(x, y);
1013 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1015 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1021 case LFUN_GETLAYOUT:
1022 cmd.message(tostr(cursorPar()->layout()));
1026 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1027 << cmd.argument << endl;
1029 // This is not the good solution to the empty argument
1030 // problem, but it will hopefully suffice for 1.2.0.
1031 // The correct solution would be to augument the
1032 // function list/array with information about what
1033 // functions needs arguments and their type.
1034 if (cmd.argument.empty()) {
1035 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1039 // Derive layout number from given argument (string)
1040 // and current buffer's textclass (number)
1041 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1042 bool hasLayout = tclass.hasLayout(cmd.argument);
1043 string layout = cmd.argument;
1045 // If the entry is obsolete, use the new one instead.
1047 string const & obs = tclass[layout]->obsoleted_by();
1053 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1058 bool change_layout = (current_layout != layout);
1060 if (!change_layout && selection.set() &&
1061 selection.start.par() != selection.end.par())
1063 ParagraphList::iterator spit = getPar(selection.start);
1064 ParagraphList::iterator epit = boost::next(getPar(selection.end));
1065 while (spit != epit) {
1066 if (spit->layout()->name() != current_layout) {
1067 change_layout = true;
1074 if (change_layout) {
1075 current_layout = layout;
1077 bv->owner()->setLayout(layout);
1084 case LFUN_PASTESELECTION: {
1085 // this was originally a beforeChange(bv->text), i.e
1086 // the outermost LyXText!
1087 bv->beforeChange(this);
1088 string const clip = bv->getClipboard();
1089 if (!clip.empty()) {
1090 if (cmd.argument == "paragraph")
1091 insertStringAsParagraphs(clip);
1093 insertStringAsLines(clip);
1100 case LFUN_GOTOERROR:
1101 gotoInset(InsetOld::ERROR_CODE, false);
1105 gotoInset(InsetOld::NOTE_CODE, false);
1108 case LFUN_REFERENCE_GOTO:
1110 vector<InsetOld::Code> tmp;
1111 tmp.push_back(InsetOld::LABEL_CODE);
1112 tmp.push_back(InsetOld::REF_CODE);
1113 gotoInset(tmp, true);
1118 replaceSelection(bv->getLyXText());
1119 ParagraphList::iterator pit = cursorPar();
1120 lyx::pos_type pos = cursor.pos();
1124 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1127 c = pit->getChar(pos - 1);
1129 LyXLayout_ptr const & style = pit->layout();
1131 BufferParams const & bufparams = bv->buffer()->params();
1132 if (style->pass_thru ||
1133 pit->getFontSettings(bufparams,pos).language()->lang() == "hebrew" ||
1134 !bv->insertInset(new InsetQuotes(c, bufparams)))
1135 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1139 case LFUN_DATE_INSERT: {
1140 replaceSelection(bv->getLyXText());
1141 time_t now_time_t = time(NULL);
1142 struct tm * now_tm = localtime(&now_time_t);
1143 setlocale(LC_TIME, "");
1145 if (!cmd.argument.empty())
1148 arg = lyxrc.date_insert_format;
1150 int const datetmp_len =
1151 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1153 for (int i = 0; i < datetmp_len; i++)
1154 insertChar(datetmp[i]);
1156 selection.cursor = cursor;
1157 moveCursorUpdate(bv, false);
1161 case LFUN_MOUSE_TRIPLE:
1164 if (!isInInset() && bv->theLockingInset())
1166 if (cmd.button() == mouse_button::button1) {
1168 selection.cursor = cursor;
1172 bv->haveSelection(selection.set());
1176 case LFUN_MOUSE_DOUBLE:
1179 if (!isInInset() && bv->theLockingInset())
1181 if (cmd.button() == mouse_button::button1) {
1182 selectWord(lyx::WHOLE_WORD_STRICT);
1184 bv->haveSelection(selection.set());
1188 case LFUN_MOUSE_MOTION:
1190 // Only use motion with button 1
1191 //if (ev.button() != mouse_button::button1)
1197 // Check for inset locking
1198 if (bv->theLockingInset()) {
1199 InsetOld * tli = bv->theLockingInset();
1200 LyXCursor cursor = bv->text->cursor;
1201 LyXFont font = bv->text->getFont(bv->text->cursorPar(), cursor.pos());
1202 int width = tli->width();
1203 int inset_x = font.isVisibleRightToLeft()
1204 ? cursor.x() - width : cursor.x();
1205 int start_x = inset_x + tli->scroll();
1206 FuncRequest cmd1 = cmd;
1207 cmd1.x = cmd.x - start_x;
1208 cmd1.y = cmd.y - cursor.y() + bv->top_y();
1209 tli->dispatch(cmd1);
1213 // The test for not selection possible is needed, that only motion
1214 // events are used, where the bottom press event was on
1215 // the drawing area too
1216 if (!selection_possible) {
1217 lyxerr[Debug::ACTION]
1218 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1222 RowList::iterator cursorrow = bv->text->cursorRow();
1223 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->top_y());
1225 // sorry for this but I have a strange error that the y value jumps at
1226 // a certain point. This seems like an error in my xforms library or
1227 // in some other local environment, but I would like to leave this here
1228 // for the moment until I can remove this (Jug 20020418)
1229 if (y_before < bv->text->cursor.y())
1230 lyxerr << y_before << ':'
1231 << bv->text->cursor.y() << endl;
1233 // This is to allow jumping over large insets
1234 if (cursorrow == bv->text->cursorRow()) {
1235 if (cmd.y >= bv->workHeight())
1236 bv->text->cursorDown(false);
1238 bv->text->cursorUp(false);
1241 bv->text->setSelection();
1246 // Single-click on work area
1247 case LFUN_MOUSE_PRESS: {
1251 // ok ok, this is a hack (for xforms)
1252 // We shouldn't go further down as we really should only do the
1253 // scrolling and be done with this. Otherwise we may open some
1254 // dialogs (Jug 20020424).
1255 if (cmd.button() == mouse_button::button4) {
1256 bv->scroll(-lyxrc.wheel_jump);
1259 if (cmd.button() == mouse_button::button5) {
1260 bv->scroll(lyxrc.wheel_jump);
1266 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1268 // Middle button press pastes if we have a selection
1269 // We do this here as if the selection was inside an inset
1270 // it could get cleared on the unlocking of the inset so
1271 // we have to check this first
1272 bool paste_internally = false;
1273 if (cmd.button() == mouse_button::button2 && selection.set()) {
1274 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1275 paste_internally = true;
1278 int const screen_first = bv->top_y();
1280 if (bv->theLockingInset()) {
1281 // We are in inset locking mode
1283 // Check whether the inset was hit. If not reset mode,
1284 // otherwise give the event to the inset
1285 if (inset_hit == bv->theLockingInset()) {
1286 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1287 bv->theLockingInset()->dispatch(cmd1);
1290 bv->unlockInset(bv->theLockingInset());
1291 lyxerr << "Re-initializing cursor" << endl;
1292 bv->cursor() = LCursor(bv);
1296 selection_possible = true;
1298 // Clear the selection
1299 bv->text->clearSelection();
1301 bv->updateScrollbar();
1303 // Single left click in math inset?
1304 if (isHighlyEditableInset(inset_hit)) {
1305 lyxerr << "click on highly editable inset, like math" << endl;
1306 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1307 selection_possible = false;
1308 bv->owner()->message(inset->editMessage());
1309 // We just have to lock the inset before calling a PressEvent on it!
1310 if (!bv->lockInset(inset))
1311 lyxerr << "Cannot lock inset" << endl;
1312 #warning cell 0 is certainly not always good.
1313 bv->cursor().push(inset, inset->getText(0));
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);