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.
19 #include "BufferView.h"
20 #include "funcrequest.h"
25 #include "ParagraphParameters.h"
30 #include "support/tostr.h"
31 #include "support/lstrings.h"
32 #include "support/LAssert.h"
33 #include "frontends/LyXView.h"
34 #include "frontends/Dialogs.h"
35 #include "insets/insetspecialchar.h"
36 #include "insets/insettext.h"
37 #include "insets/insetcommand.h"
38 #include "insets/insetnewline.h"
39 #include "undo_funcs.h"
40 #include "text_funcs.h"
43 using namespace lyx::support;
44 using namespace bv_funcs;
51 extern string current_layout;
52 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
54 // the selection possible is needed, that only motion events are
55 // used, where the bottom press event was on the drawing area too
56 bool selection_possible = false;
61 void moveCursorUpdate(BufferView * bv, bool selecting)
63 LyXText * lt = bv->getLyXText();
65 //if (!lt->selection.set())
66 // lt->selection.cursor = lt->cursor;
68 if (selecting || lt->selection.mark())
71 if (!lt->selection.set())
72 bv->haveSelection(false);
79 void finishChange(BufferView * bv, bool selecting = false)
82 moveCursorUpdate(bv, selecting);
83 bv->owner()->view_state_changed();
86 // check if the given co-ordinates are inside an inset at the
87 // given cursor, if one exists. If so, the inset is returned,
88 // and the co-ordinates are made relative. Otherwise, 0 is returned.
89 InsetOld * checkInset(BufferView * /*bv*/, LyXText & text,
90 LyXCursor const & cur, int & x, int & y)
92 lyx::pos_type const pos = cur.pos();
93 ParagraphList::iterator par = cur.par();
95 if (pos >= par->size() || !par->isInset(pos))
98 InsetOld /*const*/ * inset = par->getInset(pos);
100 if (!isEditableInset(inset))
103 // get inset dimensions
104 Assert(par->getInset(pos));
106 LyXFont const & font = text.getFont(par, pos);
108 int const width = inset->width();
109 int const inset_x = font.isVisibleRightToLeft()
110 ? (cur.x() - width) : cur.x();
113 inset_x + inset->scroll(),
115 cur.y() - inset->ascent(),
116 cur.y() + inset->descent()
119 if (!b.contains(x, y)) {
120 lyxerr[Debug::GUI] << "Missed inset at x,y "
122 << " box " << b << endl;
126 text.setCursor(par, pos, true);
129 // The origin of an inset is on the baseline
130 y -= text.cursor.y();
138 InsetOld * LyXText::checkInsetHit(int & x, int & y)
140 int y_tmp = y + bv_owner->top_y();
143 setCursorFromCoordinates(cur, x, y_tmp);
145 InsetOld * inset = checkInset(bv(), *this, cur, x, y_tmp);
151 // look at previous position
156 setCursor(cur, cur.par(), cur.pos() - 1, true);
158 inset = checkInset(bv(), *this, cur, x, y_tmp);
165 bool LyXText::gotoNextInset(vector<InsetOld::Code> const & codes,
166 string const & contents)
168 ParagraphList::iterator end = ownerParagraphs().end();
169 ParagraphList::iterator pit = cursor.par();
170 pos_type pos = cursor.pos();
174 if (pos + 1 < pit->size()) {
181 } while (pit != end &&
182 !(pit->isInset(pos) &&
183 (inset = pit->getInset(pos)) != 0 &&
184 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
186 static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
190 setCursor(pit, pos, false);
197 void LyXText::gotoInset(vector<InsetOld::Code> const & codes,
200 bv()->beforeChange(this);
203 if (same_content && cursor.pos() < cursor.par()->size()
204 && cursor.par()->isInset(cursor.pos())) {
205 InsetOld const * inset = cursor.par()->getInset(cursor.pos());
206 if (find(codes.begin(), codes.end(), inset->lyxCode())
208 contents = static_cast<InsetCommand const *>(inset)->getContents();
211 if (!gotoNextInset(codes, contents)) {
212 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
213 LyXCursor tmp = cursor;
214 cursor.par(ownerParagraphs().begin());
216 if (!gotoNextInset(codes, contents)) {
218 bv()->owner()->message(_("No more insets"));
221 bv()->owner()->message(_("No more insets"));
225 selection.cursor = cursor;
229 void LyXText::gotoInset(InsetOld::Code code, bool same_content)
231 gotoInset(vector<InsetOld::Code>(1, code), same_content);
235 void LyXText::cursorPrevious()
237 int y = bv_owner->top_y();
239 RowList::iterator rit = cursorRow();
241 if (rit == firstRow()) {
243 bv()->updateScrollbar();
247 setCursorFromCoordinates(cursor.x_fix(), y);
251 if (rit == bv()->text->cursorRow()) {
252 // we have a row which is taller than the workarea. The
253 // simplest solution is to move to the previous row instead.
256 // This is what we used to do, so we wouldn't skip right past
257 // tall rows, but it's not working right now.
260 new_y = bv()->text->cursor.y()
261 + bv()->theLockingInset()->insetInInsetY() + y
263 - bv()->workHeight() + 1;
268 - bv()->workHeight() + 1;
273 ParagraphList::iterator pit = cursor.par();
274 previousRow(pit, rit);
275 setCursor(cur, pit, rit->pos(), false);
276 if (cur.y() > bv_owner->top_y())
278 bv()->updateScrollbar();
282 void LyXText::cursorNext()
284 int topy = bv_owner->top_y();
286 RowList::iterator rit = cursorRow();
287 if (rit == lastRow()) {
288 int y = cursor.y() - rit->baseline() + cursorRow()->height();
289 if (y > topy + bv()->workHeight())
290 bv_owner->updateScrollbar();
294 int y = topy + bv_owner->workHeight();
295 if (inset_owner && !topy) {
296 y -= (bv_owner->text->cursor.y()
298 + bv_owner->theLockingInset()->insetInInsetY());
301 ParagraphList::iterator dummypit;
302 y = getRowNearY(y, dummypit)->y();
304 setCursorFromCoordinates(cursor.x_fix(), y);
305 // + bv->workHeight());
309 if (rit == bv_owner->text->cursorRow()) {
310 // we have a row which is taller than the workarea. The
311 // simplest solution is to move to the next row instead.
314 // This is what we used to do, so we wouldn't skip right past
315 // tall rows, but it's not working right now.
317 new_y = bv->top_y() + bv->workHeight();
321 new_y = bv()->text->cursor.y()
322 + bv()->theLockingInset()->insetInInsetY()
323 + y - rit->baseline();
325 new_y = cursor.y() - cursorRow()->baseline();
329 ParagraphList::iterator pit = cursor.par();
332 setCursor(cur, pit, rit->pos(), false);
333 if (cur.y() < bv_owner->top_y() + bv()->workHeight())
335 bv()->updateScrollbar();
341 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
344 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
345 replaceSelection(lt);
346 if (!bv->insertInset(new_inset))
349 bv->updateInset(new_inset);
353 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
354 bool edit, bool pastesel)
356 InsetOld * inset = createInset(cmd);
357 BufferView * bv = cmd.view();
361 if (lt->selection.set()) {
362 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
365 if (bv->insertInset(inset)) {
367 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
368 inset->localDispatch(cmd);
370 if (gotsel && pastesel)
371 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
380 InsetOld::RESULT LyXText::dispatch(FuncRequest const & cmd)
382 lyxerr[Debug::ACTION] << "LyXText::dispatch: action[" << cmd.action
383 <<"] arg[" << cmd.argument << ']' << "xy[" <<
384 cmd.x << ',' << cmd.y << ']' << endl;
386 BufferView * bv = cmd.view();
388 switch (cmd.action) {
390 case LFUN_APPENDIX: {
391 ParagraphList::iterator pit = cursor.par();
392 bool start = !pit->params().startOfAppendix();
394 // ensure that we have only one start_of_appendix in this document
395 ParagraphList::iterator tmp = ownerParagraphs().begin();
396 ParagraphList::iterator end = ownerParagraphs().end();
398 for (; tmp != end; ++tmp) {
399 if (tmp->params().startOfAppendix()) {
400 recordUndo(bv, Undo::ATOMIC, tmp);
401 tmp->params().startOfAppendix(false);
407 recordUndo(bv, Undo::ATOMIC, pit);
408 pit->params().startOfAppendix(start);
410 // we can set the refreshing parameters now
412 redoParagraph(cursor.par());
413 setCursor(cursor.par(), cursor.pos());
418 case LFUN_DELETE_WORD_FORWARD:
419 bv->beforeChange(this);
424 case LFUN_DELETE_WORD_BACKWARD:
425 bv->beforeChange(this);
426 deleteWordBackward();
430 case LFUN_DELETE_LINE_FORWARD:
431 bv->beforeChange(this);
437 if (!selection.mark())
438 bv->beforeChange(this);
439 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
442 cursorRightOneWord();
447 if (!selection.mark())
448 bv->beforeChange(this);
449 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
450 cursorRightOneWord();
456 case LFUN_BEGINNINGBUF:
457 if (!selection.mark())
458 bv->beforeChange(this);
464 if (selection.mark())
465 bv->beforeChange(this);
471 if (!selection.set())
472 selection.cursor = cursor;
473 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
477 finishChange(bv, true);
481 if (!selection.set())
482 selection.cursor = cursor;
483 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
487 finishChange(bv, true);
491 if (!selection.set())
492 selection.cursor = cursor;
494 finishChange(bv, true);
498 if (!selection.set())
499 selection.cursor = cursor;
501 finishChange(bv, true);
504 case LFUN_UP_PARAGRAPHSEL:
505 if (!selection.set())
506 selection.cursor = cursor;
508 finishChange(bv, true);
511 case LFUN_DOWN_PARAGRAPHSEL:
512 if (!selection.set())
513 selection.cursor = cursor;
514 cursorDownParagraph();
515 finishChange(bv, true);
519 if (!selection.set())
520 selection.cursor = cursor;
522 finishChange(bv, true);
526 if (!selection.set())
527 selection.cursor = cursor;
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);
546 case LFUN_WORDRIGHTSEL:
547 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
550 cursorRightOneWord();
551 finishChange(bv, true);
554 case LFUN_WORDLEFTSEL:
555 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
556 cursorRightOneWord();
559 finishChange(bv, true);
563 LyXCursor cur1 = cursor;
565 ::getWord(cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs());
566 setCursor(cur1.par(), cur1.pos());
567 bv->beforeChange(this);
568 setCursor(cur2.par(), cur2.pos());
569 finishChange(bv, true);
574 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
575 if (!selection.mark())
576 bv->beforeChange(this);
579 if (cursor.pos() < cursor.par()->size()
580 && cursor.par()->isInset(cursor.pos())
581 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
582 InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
583 cmd.message(tmpinset->editMessage());
584 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
585 tmpinset->localDispatch(cmd1);
595 // This is soooo ugly. Isn`t it possible to make
597 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
598 if (!selection.mark())
599 bv->beforeChange(this);
600 LyXCursor const cur = cursor;
603 if ((is_rtl || cur != cursor) && // only if really moved!
604 cursor.pos() < cursor.par()->size() &&
605 cursor.par()->isInset(cursor.pos()) &&
606 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
607 InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
608 cmd.message(tmpinset->editMessage());
609 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
610 tmpinset->localDispatch(cmd1);
620 if (!selection.mark())
621 bv->beforeChange(this);
627 if (!selection.mark())
628 bv->beforeChange(this);
633 case LFUN_UP_PARAGRAPH:
634 if (!selection.mark())
635 bv->beforeChange(this);
640 case LFUN_DOWN_PARAGRAPH:
641 if (!selection.mark())
642 bv->beforeChange(this);
643 cursorDownParagraph();
644 finishChange(bv, false);
648 if (!selection.mark())
649 bv->beforeChange(this);
651 finishChange(bv, false);
655 if (!selection.mark())
656 bv->beforeChange(this);
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);
675 case LFUN_BREAKLINE: {
676 lyx::pos_type body = cursor.par()->beginningOfBody();
678 // Not allowed by LaTeX (labels or empty par)
679 if (cursor.pos() <= body)
682 replaceSelection(bv->getLyXText());
683 insertInset(new InsetNewline);
684 setCursor(cursor.par(), cursor.pos());
685 moveCursorUpdate(bv, false);
690 if (!selection.set()) {
692 selection.cursor = cursor;
693 // It is possible to make it a lot faster still
694 // just comment out the line below...
696 cutSelection(true, false);
698 moveCursorUpdate(bv, false);
699 bv->owner()->view_state_changed();
702 case LFUN_DELETE_SKIP:
703 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
704 if (!selection.set()) {
705 LyXCursor cur = cursor;
706 if (cur.pos() == cur.par()->size()) {
710 && !(cur.par()->params().spaceTop()
711 == VSpace (VSpace::NONE))) {
713 cur.par()->params().lineTop(),
714 cur.par()->params().lineBottom(),
715 cur.par()->params().pagebreakTop(),
716 cur.par()->params().pagebreakBottom(),
717 VSpace(VSpace::NONE),
718 cur.par()->params().spaceBottom(),
719 cur.par()->params().spacing(),
720 cur.par()->params().align(),
721 cur.par()->params().labelWidthString(), 0);
726 selection.cursor = cursor;
730 selection.cursor = cursor;
733 cutSelection(true, false);
740 if (!selection.set()) {
741 if (bv->owner()->getIntl().getTransManager().backspace()) {
743 selection.cursor = cursor;
744 // It is possible to make it a lot faster still
745 // just comment out the line below...
748 cutSelection(true, false);
750 bv->owner()->view_state_changed();
755 case LFUN_BACKSPACE_SKIP:
756 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
757 if (!selection.set()) {
758 LyXCursor cur = cursor;
760 && !(cur.par()->params().spaceTop() == VSpace(VSpace::NONE))) {
762 cur.par()->params().lineTop(),
763 cur.par()->params().lineBottom(),
764 cur.par()->params().pagebreakTop(),
765 cur.par()->params().pagebreakBottom(),
766 VSpace(VSpace::NONE),
767 cur.par()->params().spaceBottom(),
768 cur.par()->params().spacing(),
769 cur.par()->params().align(),
770 cur.par()->params().labelWidthString(), 0);
773 selection.cursor = cur;
776 cutSelection(true, false);
781 case LFUN_BREAKPARAGRAPH:
782 replaceSelection(bv->getLyXText());
783 breakParagraph(bv->buffer()->paragraphs, 0);
785 selection.cursor = cursor;
787 bv->owner()->view_state_changed();
790 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
791 replaceSelection(bv->getLyXText());
792 breakParagraph(bv->buffer()->paragraphs, 1);
794 selection.cursor = cursor;
796 bv->owner()->view_state_changed();
799 case LFUN_BREAKPARAGRAPH_SKIP: {
800 // When at the beginning of a paragraph, remove
801 // indentation and add a "defskip" at the top.
802 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
803 LyXCursor cur = cursor;
804 replaceSelection(bv->getLyXText());
805 if (cur.pos() == 0) {
806 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
808 cur.par()->params().lineTop(),
809 cur.par()->params().lineBottom(),
810 cur.par()->params().pagebreakTop(),
811 cur.par()->params().pagebreakBottom(),
812 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
813 cur.par()->params().spacing(),
814 cur.par()->params().align(),
815 cur.par()->params().labelWidthString(), 1);
819 breakParagraph(bv->buffer()->paragraphs, 0);
822 selection.cursor = cur;
824 bv->owner()->view_state_changed();
828 case LFUN_PARAGRAPH_SPACING: {
829 ParagraphList::iterator pit = cursor.par();
830 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
831 float cur_value = 1.0;
832 if (cur_spacing == Spacing::Other)
833 cur_value = pit->params().spacing().getValue();
835 istringstream is(STRCONV(cmd.argument));
838 Spacing::Space new_spacing = cur_spacing;
839 float new_value = cur_value;
841 lyxerr << "Missing argument to `paragraph-spacing'"
843 } else if (tmp == "single") {
844 new_spacing = Spacing::Single;
845 } else if (tmp == "onehalf") {
846 new_spacing = Spacing::Onehalf;
847 } else if (tmp == "double") {
848 new_spacing = Spacing::Double;
849 } else if (tmp == "other") {
850 new_spacing = Spacing::Other;
853 lyxerr << "new_value = " << tmpval << endl;
856 } else if (tmp == "default") {
857 new_spacing = Spacing::Default;
859 lyxerr << _("Unknown spacing argument: ")
860 << cmd.argument << endl;
862 if (cur_spacing != new_spacing || cur_value != new_value) {
863 pit->params().spacing(Spacing(new_spacing, new_value));
870 case LFUN_INSET_SETTINGS:
871 Assert(bv->theLockingInset());
872 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
875 case LFUN_INSET_TOGGLE:
876 bv->beforeChange(this);
882 case LFUN_SPACE_INSERT:
883 if (cursor.par()->layout()->free_spacing)
886 doInsertInset(this, cmd, false, false);
887 moveCursorUpdate(bv, false);
890 case LFUN_HYPHENATION:
891 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
894 case LFUN_LIGATURE_BREAK:
895 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
899 specialChar(this, bv, InsetSpecialChar::LDOTS);
902 case LFUN_END_OF_SENTENCE:
903 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
906 case LFUN_MENU_SEPARATOR:
907 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
911 bv->beforeChange(this);
913 selection.cursor = cursor;
914 cmd.message(N_("Mark off"));
918 bv->beforeChange(this);
919 selection.mark(true);
921 selection.cursor = cursor;
922 cmd.message(N_("Mark on"));
926 bv->beforeChange(this);
927 if (selection.mark()) {
928 cmd.message(N_("Mark removed"));
930 selection.mark(true);
931 cmd.message(N_("Mark set"));
933 selection.cursor = cursor;
937 case LFUN_UPCASE_WORD:
938 changeCase(LyXText::text_uppercase);
942 case LFUN_LOWCASE_WORD:
943 changeCase(LyXText::text_lowercase);
947 case LFUN_CAPITALIZE_WORD:
948 changeCase(LyXText::text_capitalization);
952 case LFUN_TRANSPOSE_CHARS:
953 recordUndo(bv, Undo::ATOMIC, cursor.par());
959 cmd.message(_("Paste"));
960 replaceSelection(bv->getLyXText());
961 size_t sel_index = 0;
962 string const & arg = cmd.argument;
963 if (isStrUnsignedInt(arg)) {
964 size_t const paste_arg = strToUnsignedInt(arg);
965 #warning FIXME Check if the arg is in the domain of available selections.
966 sel_index = paste_arg;
968 pasteSelection(sel_index);
969 clearSelection(); // bug 393
976 cutSelection(true, true);
977 cmd.message(_("Cut"));
983 cmd.message(_("Copy"));
986 case LFUN_BEGINNINGBUFSEL:
990 finishChange(bv, true);
997 finishChange(bv, true);
1001 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1007 istringstream is(STRCONV(cmd.argument));
1010 lyxerr << "SETXY: Could not parse coordinates in '"
1011 << cmd.argument << std::endl;
1013 setCursorFromCoordinates(x, y);
1018 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1020 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1026 case LFUN_GETLAYOUT:
1027 cmd.message(tostr(cursor.par()->layout()));
1031 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1032 << cmd.argument << endl;
1034 // This is not the good solution to the empty argument
1035 // problem, but it will hopefully suffice for 1.2.0.
1036 // The correct solution would be to augument the
1037 // function list/array with information about what
1038 // functions needs arguments and their type.
1039 if (cmd.argument.empty()) {
1040 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1044 // Derive layout number from given argument (string)
1045 // and current buffer's textclass (number)
1046 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1047 bool hasLayout = tclass.hasLayout(cmd.argument);
1048 string layout = cmd.argument;
1050 // If the entry is obsolete, use the new one instead.
1052 string const & obs = tclass[layout]->obsoleted_by();
1058 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1063 bool change_layout = (current_layout != layout);
1065 if (!change_layout && selection.set() &&
1066 selection.start.par() != selection.end.par())
1068 ParagraphList::iterator spit = selection.start.par();
1069 ParagraphList::iterator epit = boost::next(selection.end.par());
1070 while (spit != epit) {
1071 if (spit->layout()->name() != current_layout) {
1072 change_layout = true;
1079 if (change_layout) {
1080 current_layout = layout;
1082 bv->owner()->setLayout(layout);
1089 case LFUN_PASTESELECTION: {
1090 // this was originally a beforeChange(bv->text), i.e
1091 // the outermost LyXText!
1092 bv->beforeChange(this);
1093 string const clip = bv->getClipboard();
1094 if (!clip.empty()) {
1095 if (cmd.argument == "paragraph")
1096 insertStringAsParagraphs(clip);
1098 insertStringAsLines(clip);
1105 case LFUN_GOTOERROR:
1106 gotoInset(InsetOld::ERROR_CODE, false);
1110 gotoInset(InsetOld::NOTE_CODE, false);
1113 case LFUN_REFERENCE_GOTO:
1115 vector<InsetOld::Code> tmp;
1116 tmp.push_back(InsetOld::LABEL_CODE);
1117 tmp.push_back(InsetOld::REF_CODE);
1118 gotoInset(tmp, true);
1123 replaceSelection(bv->getLyXText());
1124 ParagraphList::iterator pit = cursor.par();
1125 lyx::pos_type pos = cursor.pos();
1129 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1132 c = pit->getChar(pos - 1);
1134 LyXLayout_ptr const & style = pit->layout();
1136 if (style->pass_thru ||
1137 pit->getFontSettings(bv->buffer()->params,
1138 pos).language()->lang() == "hebrew" ||
1139 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1140 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1144 case LFUN_DATE_INSERT: {
1145 replaceSelection(bv->getLyXText());
1146 time_t now_time_t = time(NULL);
1147 struct tm * now_tm = localtime(&now_time_t);
1148 setlocale(LC_TIME, "");
1150 if (!cmd.argument.empty())
1153 arg = lyxrc.date_insert_format;
1155 int const datetmp_len =
1156 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1158 for (int i = 0; i < datetmp_len; i++)
1159 insertChar(datetmp[i]);
1161 selection.cursor = cursor;
1162 moveCursorUpdate(bv, false);
1166 case LFUN_MOUSE_TRIPLE:
1169 if (!isInInset() && bv->theLockingInset())
1171 if (cmd.button() == mouse_button::button1) {
1173 selection.cursor = cursor;
1177 bv->haveSelection(selection.set());
1181 case LFUN_MOUSE_DOUBLE:
1184 if (!isInInset() && bv->theLockingInset())
1186 if (cmd.button() == mouse_button::button1) {
1187 selectWord(lyx::WHOLE_WORD_STRICT);
1189 bv->haveSelection(selection.set());
1193 case LFUN_MOUSE_MOTION:
1195 // Only use motion with button 1
1196 //if (ev.button() != mouse_button::button1)
1202 // Check for inset locking
1203 if (bv->theLockingInset()) {
1204 InsetOld * tli = bv->theLockingInset();
1205 LyXCursor cursor = bv->text->cursor;
1206 LyXFont font = bv->text->getFont(cursor.par(), cursor.pos());
1207 int width = tli->width();
1208 int inset_x = font.isVisibleRightToLeft()
1209 ? cursor.x() - width : cursor.x();
1210 int start_x = inset_x + tli->scroll();
1211 FuncRequest cmd1 = cmd;
1212 cmd1.x = cmd.x - start_x;
1213 cmd1.y = cmd.y - cursor.y() + bv->top_y();
1214 tli->localDispatch(cmd1);
1218 // The test for not selection possible is needed, that only motion
1219 // events are used, where the bottom press event was on
1220 // the drawing area too
1221 if (!selection_possible) {
1222 lyxerr[Debug::ACTION]
1223 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1227 RowList::iterator cursorrow = bv->text->cursorRow();
1228 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->top_y());
1230 // sorry for this but I have a strange error that the y value jumps at
1231 // a certain point. This seems like an error in my xforms library or
1232 // in some other local environment, but I would like to leave this here
1233 // for the moment until I can remove this (Jug 20020418)
1234 if (y_before < bv->text->cursor.y())
1235 lyxerr << y_before << ':'
1236 << bv->text->cursor.y() << endl;
1238 // This is to allow jumping over large insets
1239 if (cursorrow == bv->text->cursorRow()) {
1240 if (cmd.y >= bv->workHeight())
1241 bv->text->cursorDown(false);
1243 bv->text->cursorUp(false);
1246 bv->text->setSelection();
1251 // Single-click on work area
1252 case LFUN_MOUSE_PRESS:
1257 // ok ok, this is a hack (for xforms)
1258 // We shouldn't go further down as we really should only do the
1259 // scrolling and be done with this. Otherwise we may open some
1260 // dialogs (Jug 20020424).
1261 if (cmd.button() == mouse_button::button4) {
1262 bv->scroll(-lyxrc.wheel_jump);
1265 if (cmd.button() == mouse_button::button5) {
1266 bv->scroll(lyxrc.wheel_jump);
1272 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1274 // Middle button press pastes if we have a selection
1275 // We do this here as if the selection was inside an inset
1276 // it could get cleared on the unlocking of the inset so
1277 // we have to check this first
1278 bool paste_internally = false;
1279 if (cmd.button() == mouse_button::button2 && selection.set()) {
1280 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1281 paste_internally = true;
1284 int const screen_first = bv->top_y();
1286 if (bv->theLockingInset()) {
1287 // We are in inset locking mode
1289 // Check whether the inset was hit. If not reset mode,
1290 // otherwise give the event to the inset
1291 if (inset_hit == bv->theLockingInset()) {
1292 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1293 bv->theLockingInset()->localDispatch(cmd1);
1296 bv->unlockInset(bv->theLockingInset());
1300 selection_possible = true;
1302 // Clear the selection
1303 bv->text->clearSelection();
1305 bv->updateScrollbar();
1307 // Single left click in math inset?
1308 if (isHighlyEditableInset(inset_hit)) {
1309 // Highly editable inset, like math
1310 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1311 selection_possible = false;
1312 bv->owner()->message(inset->editMessage());
1313 // We just have to lock the inset before calling a PressEvent on it!
1314 if (!bv->lockInset(inset))
1315 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1316 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1317 inset->localDispatch(cmd1);
1320 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1322 // Right click on a footnote flag opens float menu
1323 if (cmd.button() == mouse_button::button3) {
1324 selection_possible = false;
1328 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1329 bv->text->setCursorFromCoordinates(x, y + screen_first);
1331 bv->text->selection.cursor = bv->text->cursor;
1332 bv->text->cursor.x_fix(bv->text->cursor.x());
1334 if (bv->fitCursor())
1335 selection_possible = false;
1337 // Insert primary selection with middle mouse
1338 // if there is a local selection in the current buffer,
1340 if (cmd.button() == mouse_button::button2) {
1341 if (paste_internally)
1342 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1344 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1345 selection_possible = false;
1350 case LFUN_MOUSE_RELEASE:
1352 // do nothing if we used the mouse wheel
1356 if (cmd.button() == mouse_button::button4
1357 || cmd.button() == mouse_button::button5)
1360 // If we hit an inset, we have the inset coordinates in these
1361 // and inset_hit points to the inset. If we do not hit an
1362 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1365 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1367 if (bv->theLockingInset()) {
1368 // We are in inset locking mode.
1370 // LyX does a kind of work-area grabbing for insets.
1371 // Only a ButtonPress FuncRequest outside the inset will
1372 // force a insetUnlock.
1373 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1374 bv->theLockingInset()->localDispatch(cmd1);
1378 selection_possible = false;
1380 if (cmd.button() == mouse_button::button2)
1384 if (cmd.button() == mouse_button::button1)
1385 bv->haveSelection(selection.set());
1388 bv->owner()->view_state_changed();
1389 bv->owner()->updateMenubar();
1390 bv->owner()->updateToolbar();
1392 // Did we hit an editable inset?
1394 selection_possible = false;
1396 // if we reach this point with a selection, it
1397 // must mean we are currently selecting.
1398 // But we don't want to open the inset
1399 // because that is annoying for the user.
1400 // So just pretend we didn't hit it.
1401 // this is OK because a "kosher" ButtonRelease
1402 // will follow a ButtonPress that clears
1404 // Note this also fixes selection drawing
1405 // problems if we end up opening an inset
1406 if (selection.set())
1409 // CHECK fix this proper in 0.13
1410 // well, maybe 13.0 !!!!!!!!!
1412 // Following a ref shouldn't issue
1413 // a push on the undo-stack
1414 // anylonger, now that we have
1415 // keybindings for following
1416 // references and returning from
1417 // references. IMHO though, it
1418 // should be the inset's own business
1419 // to push or not push on the undo
1420 // stack. They don't *have* to
1421 // alter the document...
1423 // ...or maybe the recordUndo()
1424 // below isn't necessary at all anylonger?
1425 if (inset_hit->lyxCode() == InsetOld::REF_CODE)
1426 recordUndo(bv, Undo::ATOMIC);
1428 bv->owner()->message(inset_hit->editMessage());
1430 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1431 inset_hit->localDispatch(cmd1);
1437 case LFUN_SELFINSERT: {
1438 if (cmd.argument.empty())
1441 // Automatically delete the currently selected
1442 // text and replace it with what is being
1443 // typed in now. Depends on lyxrc settings
1444 // "auto_region_delete", which defaults to
1447 if (lyxrc.auto_region_delete) {
1448 if (selection.set())
1449 cutSelection(false, false);
1450 bv->haveSelection(false);
1453 bv->beforeChange(this);
1454 LyXFont const old_font = real_current_font;
1456 string::const_iterator cit = cmd.argument.begin();
1457 string::const_iterator end = cmd.argument.end();
1458 for (; cit != end; ++cit)
1459 bv->owner()->getIntl().getTransManager().
1460 TranslateAndInsert(*cit, this);
1462 selection.cursor = cursor;
1463 moveCursorUpdate(bv, false);
1465 // real_current_font.number can change so we need to
1466 // update the minibuffer
1467 if (old_font != real_current_font)
1468 bv->owner()->view_state_changed();
1469 bv->updateScrollbar();
1473 case LFUN_HTMLURL: {
1474 InsetCommandParams p("htmlurl");
1475 string const data = InsetCommandMailer::params2string("url", p);
1476 bv->owner()->getDialogs().show("url", data, 0);
1481 InsetCommandParams p("url");
1482 string const data = InsetCommandMailer::params2string("url", p);
1483 bv->owner()->getDialogs().show("url", data, 0);
1489 case LFUN_INSET_LIST:
1490 case LFUN_INSET_THEOREM:
1491 case LFUN_INSET_CAPTION:
1493 case LFUN_INSERT_NOTE:
1494 case LFUN_INSERT_BRANCH:
1495 case LFUN_INSERT_BIBITEM:
1496 case LFUN_INSET_ERT:
1497 case LFUN_INSET_FLOAT:
1498 case LFUN_INSET_FOOTNOTE:
1499 case LFUN_INSET_MARGINAL:
1500 case LFUN_INSET_MINIPAGE:
1501 case LFUN_INSET_OPTARG:
1502 case LFUN_INSET_WIDE_FLOAT:
1503 case LFUN_INSET_WRAP:
1504 case LFUN_TABULAR_INSERT:
1505 case LFUN_ENVIRONMENT_INSERT:
1506 // Open the inset, and move the current selection
1508 doInsertInset(this, cmd, true, true);
1511 case LFUN_INDEX_INSERT:
1512 // Just open the inset
1513 doInsertInset(this, cmd, true, false);
1516 case LFUN_INDEX_PRINT:
1517 case LFUN_TOC_INSERT:
1520 doInsertInset(this, cmd, false, false);
1524 return UNDISPATCHED;