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 "funcrequest.h"
31 #include "paragraph.h"
32 #include "ParagraphParameters.h"
33 #include "text_funcs.h"
37 #include "frontends/Dialogs.h"
38 #include "frontends/LyXView.h"
40 #include "insets/insetcommand.h"
41 #include "insets/insetnewline.h"
42 #include "insets/insetspecialchar.h"
43 #include "insets/insettext.h"
45 #include "support/lstrings.h"
46 #include "support/tostr.h"
48 #include "support/std_sstream.h"
51 using bv_funcs::replaceSelection;
55 using lyx::support::isStrUnsignedInt;
56 using lyx::support::strToUnsignedInt;
61 using std::istringstream;
65 extern string current_layout;
66 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
68 // the selection possible is needed, that only motion events are
69 // used, where the bottom press event was on the drawing area too
70 bool selection_possible = false;
75 void moveCursorUpdate(BufferView * bv, bool selecting)
77 LyXText * lt = bv->getLyXText();
79 // if (!lt->selection.set())
80 // lt->selection.cursor = lt->cursor;
82 if (selecting || lt->selection.mark())
85 if (!lt->selection.set())
86 bv->haveSelection(false);
93 void finishChange(BufferView * bv, bool selecting = false)
96 moveCursorUpdate(bv, selecting);
97 bv->owner()->view_state_changed();
100 // check if the given co-ordinates are inside an inset at the
101 // given cursor, if one exists. If so, the inset is returned,
102 // and the co-ordinates are made relative. Otherwise, 0 is returned.
103 InsetOld * checkInset(LyXText & text,
104 LyXCursor const & cur, int & x, int & y)
106 lyx::pos_type const pos = cur.pos();
107 ParagraphList::iterator par = text.getPar(cur);
109 if (pos >= par->size() || !par->isInset(pos))
112 InsetOld /*const*/ * inset = par->getInset(pos);
114 if (!isEditableInset(inset))
117 // get inset dimensions
118 BOOST_ASSERT(par->getInset(pos));
120 LyXFont const & font = text.getFont(par, pos);
122 int const width = inset->width();
123 int const inset_x = font.isVisibleRightToLeft()
124 ? (cur.x() - width) : cur.x();
127 inset_x + inset->scroll(),
129 cur.y() - inset->ascent(),
130 cur.y() + inset->descent()
133 if (!b.contains(x, y)) {
134 lyxerr[Debug::GUI] << "Missed inset at x,y "
136 << " box " << b << endl;
140 text.setCursor(cur.par(), pos, true);
143 // The origin of an inset is on the baseline
144 y -= text.cursor.y();
152 InsetOld * LyXText::checkInsetHit(int & x, int & y)
154 int y_tmp = y + bv_owner->top_y();
157 setCursorFromCoordinates(cur, x, y_tmp);
159 InsetOld * inset = checkInset(*this, cur, x, y_tmp);
165 // look at previous position
170 setCursor(cur, cur.par(), cur.pos() - 1, true);
172 inset = checkInset(*this, cur, x, y_tmp);
179 bool LyXText::gotoNextInset(vector<InsetOld::Code> const & codes,
180 string const & contents)
182 ParagraphList::iterator end = ownerParagraphs().end();
183 ParagraphList::iterator pit = cursorPar();
184 pos_type pos = cursor.pos();
188 if (pos + 1 < pit->size()) {
195 } while (pit != end &&
196 !(pit->isInset(pos) &&
197 (inset = pit->getInset(pos)) != 0 &&
198 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
200 static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
206 setCursor(parOffset(pit), pos, false);
211 void LyXText::gotoInset(vector<InsetOld::Code> const & codes,
214 bv()->beforeChange(this);
217 if (same_content && cursor.pos() < cursorPar()->size()
218 && cursorPar()->isInset(cursor.pos())) {
219 InsetOld const * inset = cursorPar()->getInset(cursor.pos());
220 if (find(codes.begin(), codes.end(), inset->lyxCode())
222 contents = static_cast<InsetCommand const *>(inset)->getContents();
225 if (!gotoNextInset(codes, contents)) {
226 if (cursor.pos() || cursorPar() != ownerParagraphs().begin()) {
227 LyXCursor tmp = cursor;
230 if (!gotoNextInset(codes, contents)) {
232 bv()->owner()->message(_("No more insets"));
235 bv()->owner()->message(_("No more insets"));
239 selection.cursor = cursor;
243 void LyXText::gotoInset(InsetOld::Code code, bool same_content)
245 gotoInset(vector<InsetOld::Code>(1, code), same_content);
249 void LyXText::cursorPrevious()
251 int y = bv()->top_y();
253 ParagraphList::iterator cpit = cursorPar();
254 RowList::iterator crit = cpit->getRow(cursor.pos());
256 if (isFirstRow(cpit, *crit)) {
258 bv()->updateScrollbar();
262 setCursorFromCoordinates(cursor.x_fix(), y);
265 if (crit == bv()->text->cursorRow()) {
266 // we have a row which is taller than the workarea. The
267 // simplest solution is to move to the previous row instead.
272 int new_y = + crit->height() - bv()->workHeight() + 1;
275 new_y += bv()->text->cursor.y()
276 + bv()->theLockingInset()->insetInInsetY() + y;
278 new_y += cursor.y() - crit->baseline();
281 previousRow(cpit, crit);
283 setCursor(cur, parOffset(cpit), crit->pos(), false);
284 if (cur.y() > bv()->top_y())
286 bv()->updateScrollbar();
290 void LyXText::cursorNext()
292 int topy = bv()->top_y();
294 ParagraphList::iterator cpit = cursorPar();
295 RowList::iterator crit = cpit->getRow(cursor.pos());
297 if (isLastRow(cpit, *crit)) {
298 int y = cursor.y() - crit->baseline() + crit->height();
299 if (y > topy + bv()->workHeight())
300 bv()->updateScrollbar();
304 int y = topy + bv()->workHeight();
305 if (inset_owner && !topy) {
306 y -= (bv()->text->cursor.y()
308 + bv()->theLockingInset()->insetInInsetY());
311 ParagraphList::iterator dummypit;
312 Row const & rr = *getRowNearY(y, dummypit);
313 y = dummypit->y + rr.y_offset();
315 setCursorFromCoordinates(cursor.x_fix(), y);
316 // + bv->workHeight());
320 if (crit == bv()->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();
333 new_y = bv()->text->cursor.y()
334 + bv()->theLockingInset()->insetInInsetY()
335 + y - crit->baseline();
337 new_y = cursor.y() - crit->baseline();
343 setCursor(cur, parOffset(cpit), crit->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->dispatch(cmd);
381 if (gotsel && pastesel)
382 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
392 dispatch_result 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 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
597 tmpinset->dispatch(cmd1);
607 // This is soooo ugly. Isn`t it possible to make
609 bool const is_rtl = cursorPar()->isRightToLeftPar(bv->buffer()->params());
610 if (!selection.mark())
611 bv->beforeChange(this);
612 LyXCursor const cur = cursor;
615 if ((is_rtl || cur != cursor) && // only if really moved!
616 cursor.pos() < cursorPar()->size() &&
617 cursorPar()->isInset(cursor.pos()) &&
618 isHighlyEditableInset(cursorPar()->getInset(cursor.pos()))) {
619 InsetOld * tmpinset = cursorPar()->getInset(cursor.pos());
620 cmd.message(tmpinset->editMessage());
621 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
622 tmpinset->dispatch(cmd1);
632 if (!selection.mark())
633 bv->beforeChange(this);
639 if (!selection.mark())
640 bv->beforeChange(this);
645 case LFUN_UP_PARAGRAPH:
646 if (!selection.mark())
647 bv->beforeChange(this);
652 case LFUN_DOWN_PARAGRAPH:
653 if (!selection.mark())
654 bv->beforeChange(this);
655 cursorDownParagraph();
656 finishChange(bv, false);
660 if (!selection.mark())
661 bv->beforeChange(this);
663 finishChange(bv, false);
667 if (!selection.mark())
668 bv->beforeChange(this);
670 finishChange(bv, false);
674 if (!selection.mark())
675 bv->beforeChange(this);
677 finishChange(bv, false);
681 if (!selection.mark())
682 bv->beforeChange(this);
684 finishChange(bv, false);
687 case LFUN_BREAKLINE: {
688 lyx::pos_type body = cursorPar()->beginningOfBody();
690 // Not allowed by LaTeX (labels or empty par)
691 if (cursor.pos() <= body)
694 replaceSelection(bv->getLyXText());
695 insertInset(new InsetNewline);
696 setCursor(cursorPar(), cursor.pos());
697 moveCursorUpdate(bv, false);
702 if (!selection.set()) {
704 selection.cursor = cursor;
705 // It is possible to make it a lot faster still
706 // just comment out the line below...
708 cutSelection(true, false);
710 moveCursorUpdate(bv, false);
711 bv->owner()->view_state_changed();
714 case LFUN_DELETE_SKIP:
715 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
716 if (!selection.set()) {
717 if (cursor.pos() == cursorPar()->size()) {
719 ParagraphParameters & params = cursorPar()->params();
720 if (cursor.pos() == 0
721 && !(params.spaceTop() == VSpace (VSpace::NONE))) {
723 VSpace(VSpace::NONE),
724 params.spaceBottom(),
727 params.labelWidthString(), 0);
732 selection.cursor = cursor;
736 selection.cursor = cursor;
739 cutSelection(true, false);
746 if (!selection.set()) {
747 if (bv->owner()->getIntl().getTransManager().backspace()) {
749 selection.cursor = cursor;
750 // It is possible to make it a lot faster still
751 // just comment out the line below...
754 cutSelection(true, false);
756 bv->owner()->view_state_changed();
761 case LFUN_BACKSPACE_SKIP:
762 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
763 if (!selection.set()) {
764 ParagraphParameters & params = cursorPar()->params();
765 if (cursor.pos() == 0 && !(params.spaceTop() == VSpace(VSpace::NONE))) {
767 VSpace(VSpace::NONE),
768 params.spaceBottom(),
771 params.labelWidthString(), 0);
773 LyXCursor cur = cursor;
775 selection.cursor = cur;
778 cutSelection(true, false);
783 case LFUN_BREAKPARAGRAPH:
784 replaceSelection(bv->getLyXText());
785 breakParagraph(bv->buffer()->paragraphs(), 0);
787 selection.cursor = cursor;
789 bv->owner()->view_state_changed();
792 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
793 replaceSelection(bv->getLyXText());
794 breakParagraph(bv->buffer()->paragraphs(), 1);
796 selection.cursor = cursor;
798 bv->owner()->view_state_changed();
801 case LFUN_BREAKPARAGRAPH_SKIP: {
802 // When at the beginning of a paragraph, remove
803 // indentation and add a "defskip" at the top.
804 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
805 LyXCursor cur = cursor;
806 replaceSelection(bv->getLyXText());
807 if (cur.pos() == 0) {
808 ParagraphParameters & params = getPar(cur)->params();
809 if (params.spaceTop() == VSpace(VSpace::NONE)) {
811 VSpace(VSpace::DEFSKIP), params.spaceBottom(),
814 params.labelWidthString(), 1);
818 breakParagraph(bv->buffer()->paragraphs(), 0);
821 selection.cursor = cur;
823 bv->owner()->view_state_changed();
827 case LFUN_PARAGRAPH_SPACING: {
828 ParagraphList::iterator pit = cursorPar();
829 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
830 float cur_value = 1.0;
831 if (cur_spacing == Spacing::Other)
832 cur_value = pit->params().spacing().getValue();
834 istringstream is(cmd.argument);
837 Spacing::Space new_spacing = cur_spacing;
838 float new_value = cur_value;
840 lyxerr << "Missing argument to `paragraph-spacing'"
842 } else if (tmp == "single") {
843 new_spacing = Spacing::Single;
844 } else if (tmp == "onehalf") {
845 new_spacing = Spacing::Onehalf;
846 } else if (tmp == "double") {
847 new_spacing = Spacing::Double;
848 } else if (tmp == "other") {
849 new_spacing = Spacing::Other;
852 lyxerr << "new_value = " << tmpval << endl;
855 } else if (tmp == "default") {
856 new_spacing = Spacing::Default;
858 lyxerr << _("Unknown spacing argument: ")
859 << cmd.argument << endl;
861 if (cur_spacing != new_spacing || cur_value != new_value) {
862 pit->params().spacing(Spacing(new_spacing, new_value));
869 case LFUN_INSET_SETTINGS:
870 BOOST_ASSERT(bv->theLockingInset());
871 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
874 case LFUN_INSET_TOGGLE:
875 bv->beforeChange(this);
881 case LFUN_SPACE_INSERT:
882 if (cursorPar()->layout()->free_spacing)
885 doInsertInset(this, cmd, false, false);
886 moveCursorUpdate(bv, false);
889 case LFUN_HYPHENATION:
890 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
893 case LFUN_LIGATURE_BREAK:
894 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
898 specialChar(this, bv, InsetSpecialChar::LDOTS);
901 case LFUN_END_OF_SENTENCE:
902 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
905 case LFUN_MENU_SEPARATOR:
906 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
910 bv->beforeChange(this);
912 selection.cursor = cursor;
913 cmd.message(N_("Mark off"));
917 bv->beforeChange(this);
918 selection.mark(true);
920 selection.cursor = cursor;
921 cmd.message(N_("Mark on"));
925 bv->beforeChange(this);
926 if (selection.mark()) {
927 cmd.message(N_("Mark removed"));
929 selection.mark(true);
930 cmd.message(N_("Mark set"));
932 selection.cursor = cursor;
936 case LFUN_UPCASE_WORD:
937 changeCase(LyXText::text_uppercase);
941 case LFUN_LOWCASE_WORD:
942 changeCase(LyXText::text_lowercase);
946 case LFUN_CAPITALIZE_WORD:
947 changeCase(LyXText::text_capitalization);
951 case LFUN_TRANSPOSE_CHARS:
952 recUndo(cursor.par());
958 cmd.message(_("Paste"));
959 replaceSelection(bv->getLyXText());
960 #warning FIXME Check if the arg is in the domain of available selections.
961 if (isStrUnsignedInt(cmd.argument))
962 pasteSelection(strToUnsignedInt(cmd.argument));
965 clearSelection(); // bug 393
972 cutSelection(true, true);
973 cmd.message(_("Cut"));
979 cmd.message(_("Copy"));
982 case LFUN_BEGINNINGBUFSEL:
986 finishChange(bv, true);
993 finishChange(bv, true);
997 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1003 istringstream is(cmd.argument);
1006 lyxerr << "SETXY: Could not parse coordinates in '"
1007 << cmd.argument << std::endl;
1009 setCursorFromCoordinates(x, y);
1014 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1016 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1022 case LFUN_GETLAYOUT:
1023 cmd.message(tostr(cursorPar()->layout()));
1027 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1028 << cmd.argument << endl;
1030 // This is not the good solution to the empty argument
1031 // problem, but it will hopefully suffice for 1.2.0.
1032 // The correct solution would be to augument the
1033 // function list/array with information about what
1034 // functions needs arguments and their type.
1035 if (cmd.argument.empty()) {
1036 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1040 // Derive layout number from given argument (string)
1041 // and current buffer's textclass (number)
1042 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1043 bool hasLayout = tclass.hasLayout(cmd.argument);
1044 string layout = cmd.argument;
1046 // If the entry is obsolete, use the new one instead.
1048 string const & obs = tclass[layout]->obsoleted_by();
1054 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1059 bool change_layout = (current_layout != layout);
1061 if (!change_layout && selection.set() &&
1062 selection.start.par() != selection.end.par())
1064 ParagraphList::iterator spit = getPar(selection.start);
1065 ParagraphList::iterator epit = boost::next(getPar(selection.end));
1066 while (spit != epit) {
1067 if (spit->layout()->name() != current_layout) {
1068 change_layout = true;
1075 if (change_layout) {
1076 current_layout = layout;
1078 bv->owner()->setLayout(layout);
1085 case LFUN_PASTESELECTION: {
1086 // this was originally a beforeChange(bv->text), i.e
1087 // the outermost LyXText!
1088 bv->beforeChange(this);
1089 string const clip = bv->getClipboard();
1090 if (!clip.empty()) {
1091 if (cmd.argument == "paragraph")
1092 insertStringAsParagraphs(clip);
1094 insertStringAsLines(clip);
1101 case LFUN_GOTOERROR:
1102 gotoInset(InsetOld::ERROR_CODE, false);
1106 gotoInset(InsetOld::NOTE_CODE, false);
1109 case LFUN_REFERENCE_GOTO:
1111 vector<InsetOld::Code> tmp;
1112 tmp.push_back(InsetOld::LABEL_CODE);
1113 tmp.push_back(InsetOld::REF_CODE);
1114 gotoInset(tmp, true);
1119 replaceSelection(bv->getLyXText());
1120 ParagraphList::iterator pit = cursorPar();
1121 lyx::pos_type pos = cursor.pos();
1125 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1128 c = pit->getChar(pos - 1);
1130 LyXLayout_ptr const & style = pit->layout();
1132 BufferParams const & bufparams = bv->buffer()->params();
1133 if (style->pass_thru ||
1134 pit->getFontSettings(bufparams,pos).language()->lang() == "hebrew" ||
1135 !bv->insertInset(new InsetQuotes(c, bufparams)))
1136 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1140 case LFUN_DATE_INSERT: {
1141 replaceSelection(bv->getLyXText());
1142 time_t now_time_t = time(NULL);
1143 struct tm * now_tm = localtime(&now_time_t);
1144 setlocale(LC_TIME, "");
1146 if (!cmd.argument.empty())
1149 arg = lyxrc.date_insert_format;
1151 int const datetmp_len =
1152 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1154 for (int i = 0; i < datetmp_len; i++)
1155 insertChar(datetmp[i]);
1157 selection.cursor = cursor;
1158 moveCursorUpdate(bv, false);
1162 case LFUN_MOUSE_TRIPLE:
1165 if (!isInInset() && bv->theLockingInset())
1167 if (cmd.button() == mouse_button::button1) {
1169 selection.cursor = cursor;
1173 bv->haveSelection(selection.set());
1177 case LFUN_MOUSE_DOUBLE:
1180 if (!isInInset() && bv->theLockingInset())
1182 if (cmd.button() == mouse_button::button1) {
1183 selectWord(lyx::WHOLE_WORD_STRICT);
1185 bv->haveSelection(selection.set());
1189 case LFUN_MOUSE_MOTION:
1191 // Only use motion with button 1
1192 //if (ev.button() != mouse_button::button1)
1198 // Check for inset locking
1199 if (bv->theLockingInset()) {
1200 InsetOld * tli = bv->theLockingInset();
1201 LyXCursor cursor = bv->text->cursor;
1202 LyXFont font = bv->text->getFont(bv->text->cursorPar(), cursor.pos());
1203 int width = tli->width();
1204 int inset_x = font.isVisibleRightToLeft()
1205 ? cursor.x() - width : cursor.x();
1206 int start_x = inset_x + tli->scroll();
1207 FuncRequest cmd1 = cmd;
1208 cmd1.x = cmd.x - start_x;
1209 cmd1.y = cmd.y - cursor.y() + bv->top_y();
1210 tli->dispatch(cmd1);
1214 // The test for not selection possible is needed, that only motion
1215 // events are used, where the bottom press event was on
1216 // the drawing area too
1217 if (!selection_possible) {
1218 lyxerr[Debug::ACTION]
1219 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1223 RowList::iterator cursorrow = bv->text->cursorRow();
1224 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->top_y());
1226 // sorry for this but I have a strange error that the y value jumps at
1227 // a certain point. This seems like an error in my xforms library or
1228 // in some other local environment, but I would like to leave this here
1229 // for the moment until I can remove this (Jug 20020418)
1230 if (y_before < bv->text->cursor.y())
1231 lyxerr << y_before << ':'
1232 << bv->text->cursor.y() << endl;
1234 // This is to allow jumping over large insets
1235 if (cursorrow == bv->text->cursorRow()) {
1236 if (cmd.y >= bv->workHeight())
1237 bv->text->cursorDown(false);
1239 bv->text->cursorUp(false);
1242 bv->text->setSelection();
1247 // Single-click on work area
1248 case LFUN_MOUSE_PRESS:
1253 // ok ok, this is a hack (for xforms)
1254 // We shouldn't go further down as we really should only do the
1255 // scrolling and be done with this. Otherwise we may open some
1256 // dialogs (Jug 20020424).
1257 if (cmd.button() == mouse_button::button4) {
1258 bv->scroll(-lyxrc.wheel_jump);
1261 if (cmd.button() == mouse_button::button5) {
1262 bv->scroll(lyxrc.wheel_jump);
1268 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1270 // Middle button press pastes if we have a selection
1271 // We do this here as if the selection was inside an inset
1272 // it could get cleared on the unlocking of the inset so
1273 // we have to check this first
1274 bool paste_internally = false;
1275 if (cmd.button() == mouse_button::button2 && selection.set()) {
1276 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1277 paste_internally = true;
1280 int const screen_first = bv->top_y();
1282 if (bv->theLockingInset()) {
1283 // We are in inset locking mode
1285 // Check whether the inset was hit. If not reset mode,
1286 // otherwise give the event to the inset
1287 if (inset_hit == bv->theLockingInset()) {
1288 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1289 bv->theLockingInset()->dispatch(cmd1);
1292 bv->unlockInset(bv->theLockingInset());
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 // Highly editable inset, like math
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[Debug::INSETS] << "Cannot lock inset" << endl;
1312 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1313 inset->dispatch(cmd1);
1316 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1318 // Right click on a footnote flag opens float menu
1319 if (cmd.button() == mouse_button::button3) {
1320 selection_possible = false;
1324 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1325 bv->text->setCursorFromCoordinates(x, y + screen_first);
1327 bv->text->selection.cursor = bv->text->cursor;
1328 bv->text->cursor.x_fix(bv->text->cursor.x());
1330 if (bv->fitCursor())
1331 selection_possible = false;
1333 // Insert primary selection with middle mouse
1334 // if there is a local selection in the current buffer,
1336 if (cmd.button() == mouse_button::button2) {
1337 if (paste_internally)
1338 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1340 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1341 selection_possible = false;
1346 case LFUN_MOUSE_RELEASE:
1348 // do nothing if we used the mouse wheel
1352 if (cmd.button() == mouse_button::button4
1353 || cmd.button() == mouse_button::button5)
1356 // If we hit an inset, we have the inset coordinates in these
1357 // and inset_hit points to the inset. If we do not hit an
1358 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1361 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1363 if (bv->theLockingInset()) {
1364 // We are in inset locking mode.
1366 // LyX does a kind of work-area grabbing for insets.
1367 // Only a ButtonPress FuncRequest outside the inset will
1368 // force a insetUnlock.
1369 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1370 bv->theLockingInset()->dispatch(cmd1);
1374 selection_possible = false;
1376 if (cmd.button() == mouse_button::button2)
1380 if (cmd.button() == mouse_button::button1)
1381 bv->haveSelection(selection.set());
1384 bv->owner()->view_state_changed();
1385 bv->owner()->updateMenubar();
1386 bv->owner()->updateToolbar();
1388 // Did we hit an editable inset?
1390 selection_possible = false;
1392 // if we reach this point with a selection, it
1393 // must mean we are currently selecting.
1394 // But we don't want to open the inset
1395 // because that is annoying for the user.
1396 // So just pretend we didn't hit it.
1397 // this is OK because a "kosher" ButtonRelease
1398 // will follow a ButtonPress that clears
1400 // Note this also fixes selection drawing
1401 // problems if we end up opening an inset
1402 if (selection.set())
1405 // CHECK fix this proper in 0.13
1406 // well, maybe 13.0 !!!!!!!!!
1408 // Following a ref shouldn't issue
1409 // a push on the undo-stack
1410 // anylonger, now that we have
1411 // keybindings for following
1412 // references and returning from
1413 // references. IMHO though, it
1414 // should be the inset's own business
1415 // to push or not push on the undo
1416 // stack. They don't *have* to
1417 // alter the document...
1419 // ...or maybe the recordUndo()
1420 // below isn't necessary at all anylonger?
1421 if (inset_hit->lyxCode() == InsetOld::REF_CODE)
1422 recUndo(cursor.par());
1424 bv->owner()->message(inset_hit->editMessage());
1426 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1427 inset_hit->dispatch(cmd1);
1433 case LFUN_SELFINSERT: {
1434 if (cmd.argument.empty())
1437 // Automatically delete the currently selected
1438 // text and replace it with what is being
1439 // typed in now. Depends on lyxrc settings
1440 // "auto_region_delete", which defaults to
1443 if (lyxrc.auto_region_delete) {
1444 if (selection.set())
1445 cutSelection(false, false);
1446 bv->haveSelection(false);
1449 bv->beforeChange(this);
1450 LyXFont const old_font = real_current_font;
1452 string::const_iterator cit = cmd.argument.begin();
1453 string::const_iterator end = cmd.argument.end();
1454 for (; cit != end; ++cit)
1455 bv->owner()->getIntl().getTransManager().
1456 TranslateAndInsert(*cit, this);
1458 selection.cursor = cursor;
1459 moveCursorUpdate(bv, false);
1461 // real_current_font.number can change so we need to
1462 // update the minibuffer
1463 if (old_font != real_current_font)
1464 bv->owner()->view_state_changed();
1465 bv->updateScrollbar();
1469 case LFUN_HTMLURL: {
1470 InsetCommandParams p("htmlurl");
1471 string const data = InsetCommandMailer::params2string("url", p);
1472 bv->owner()->getDialogs().show("url", data, 0);
1477 InsetCommandParams p("url");
1478 string const data = InsetCommandMailer::params2string("url", p);
1479 bv->owner()->getDialogs().show("url", data, 0);
1485 case LFUN_INSET_LIST:
1486 case LFUN_INSET_THEOREM:
1487 case LFUN_INSET_CAPTION:
1489 case LFUN_INSERT_NOTE:
1490 case LFUN_INSERT_BOX:
1491 case LFUN_INSERT_BRANCH:
1492 case LFUN_INSERT_BIBITEM:
1493 case LFUN_INSET_ERT:
1494 case LFUN_INSET_FLOAT:
1495 case LFUN_INSET_FOOTNOTE:
1496 case LFUN_INSET_MARGINAL:
1497 case LFUN_INSET_MINIPAGE:
1498 case LFUN_INSET_OPTARG:
1499 case LFUN_INSET_WIDE_FLOAT:
1500 case LFUN_INSET_WRAP:
1501 case LFUN_TABULAR_INSERT:
1502 case LFUN_ENVIRONMENT_INSERT:
1503 // Open the inset, and move the current selection
1505 doInsertInset(this, cmd, true, true);
1508 case LFUN_INDEX_INSERT:
1509 // Just open the inset
1510 doInsertInset(this, cmd, true, false);
1513 case LFUN_INDEX_PRINT:
1514 case LFUN_TOC_INSERT:
1516 case LFUN_INSERT_LINE:
1517 case LFUN_INSERT_PAGEBREAK:
1519 doInsertInset(this, cmd, false, false);
1522 case LFUN_DEPTH_MIN:
1523 bv_funcs::changeDepth(bv, this, bv_funcs::DEC_DEPTH, false);
1528 case LFUN_DEPTH_PLUS:
1530 bv_funcs::changeDepth(bv, this, bv_funcs::INC_DEPTH, false);
1536 return UNDISPATCHED;