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 "paragraph.h"
20 #include "BufferView.h"
21 #include "funcrequest.h"
25 #include "bufferparams.h"
27 #include "bufferview_funcs.h"
28 #include "ParagraphParameters.h"
34 #include "support/tostr.h"
35 #include "support/lstrings.h"
36 #include "support/LAssert.h"
37 #include "frontends/LyXView.h"
38 #include "frontends/screen.h"
39 #include "frontends/Dialogs.h"
40 #include "insets/insetspecialchar.h"
41 #include "insets/insettext.h"
42 #include "insets/insetquotes.h"
43 #include "insets/insetcommand.h"
44 #include "insets/insetnewline.h"
45 #include "undo_funcs.h"
46 #include "text_funcs.h"
52 using namespace lyx::support;
53 using namespace bv_funcs;
60 extern string current_layout;
61 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
63 // the selection possible is needed, that only motion events are
64 // used, where the bottom press event was on the drawing area too
65 bool selection_possible = false;
70 void moveCursorUpdate(BufferView * bv, bool selecting)
72 LyXText * lt = bv->getLyXText();
74 //if (!lt->selection.set())
75 // lt->selection.cursor = lt->cursor;
77 if (selecting || lt->selection.mark())
80 if (!lt->selection.set())
81 bv->haveSelection(false);
88 void finishChange(BufferView * bv, bool selecting = false)
91 moveCursorUpdate(bv, selecting);
92 bv->owner()->view_state_changed();
95 // check if the given co-ordinates are inside an inset at the
96 // given cursor, if one exists. If so, the inset is returned,
97 // and the co-ordinates are made relative. Otherwise, 0 is returned.
98 InsetOld * checkInset(BufferView * /*bv*/, LyXText & text,
99 LyXCursor const & cur, int & x, int & y)
101 lyx::pos_type const pos = cur.pos();
102 ParagraphList::iterator par = cur.par();
104 if (pos >= par->size() || !par->isInset(pos))
107 InsetOld /*const*/ * inset = par->getInset(pos);
109 if (!isEditableInset(inset))
112 // get inset dimensions
113 Assert(par->getInset(pos));
115 LyXFont const & font = text.getFont(par, pos);
117 int const width = inset->width();
118 int const inset_x = font.isVisibleRightToLeft()
119 ? (cur.ix() - width) : cur.ix();
122 inset_x + inset->scroll(),
124 cur.iy() - inset->ascent(),
125 cur.iy() + inset->descent()
128 if (!b.contained(x, y)) {
129 lyxerr[Debug::GUI] << "Missed inset at x,y "
131 << " box " << b << endl;
135 text.setCursor(par, pos, true);
138 // The origin of an inset is on the baseline
139 y -= text.cursor.iy();
147 InsetOld * LyXText::checkInsetHit(int & x, int & y)
149 int y_tmp = y + top_y();
152 setCursorFromCoordinates(cur, x, y_tmp);
154 InsetOld * inset = checkInset(bv(), *this, cur, x, y_tmp);
160 // look at previous position
165 setCursor(cur, cur.par(), cur.pos() - 1, true);
167 inset = checkInset(bv(), *this, cur, x, y_tmp);
174 bool LyXText::gotoNextInset(vector<InsetOld::Code> const & codes,
175 string const & contents)
177 ParagraphList::iterator end = ownerParagraphs().end();
178 ParagraphList::iterator pit = cursor.par();
179 pos_type pos = cursor.pos();
183 if (pos + 1 < pit->size()) {
190 } while (pit != end &&
191 !(pit->isInset(pos) &&
192 (inset = pit->getInset(pos)) != 0 &&
193 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
195 static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
199 setCursor(pit, pos, false);
206 void LyXText::gotoInset(vector<InsetOld::Code> const & codes,
209 bv()->beforeChange(this);
212 if (same_content && cursor.pos() < cursor.par()->size()
213 && cursor.par()->isInset(cursor.pos())) {
214 InsetOld const * inset = cursor.par()->getInset(cursor.pos());
215 if (find(codes.begin(), codes.end(), inset->lyxCode())
217 contents = static_cast<InsetCommand const *>(inset)->getContents();
220 if (!gotoNextInset(codes, contents)) {
221 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
222 LyXCursor tmp = cursor;
223 cursor.par(ownerParagraphs().begin());
225 if (!gotoNextInset(codes, contents)) {
227 bv()->owner()->message(_("No more insets"));
230 bv()->owner()->message(_("No more insets"));
234 selection.cursor = cursor;
238 void LyXText::gotoInset(InsetOld::Code code, bool same_content)
240 gotoInset(vector<InsetOld::Code>(1, code), same_content);
244 void LyXText::cursorPrevious()
248 RowList::iterator rit = cursorRow();
250 if (rit == firstRow()) {
252 bv()->updateScrollbar();
256 setCursorFromCoordinates(cursor.x_fix(), y);
260 if (rit == bv()->text->cursorRow()) {
261 // we have a row which is taller than the workarea. The
262 // simplest solution is to move to the previous row instead.
265 // This is what we used to do, so we wouldn't skip right past
266 // tall rows, but it's not working right now.
269 new_y = bv()->text->cursor.iy()
270 + bv()->theLockingInset()->insetInInsetY() + y
272 - bv()->workHeight() + 1;
277 - bv()->workHeight() + 1;
282 ParagraphList::iterator pit = cursor.par();
283 previousRow(pit, rit);
284 setCursor(cur, pit, rit->pos(), false);
285 if (cur.y() > top_y())
287 bv()->updateScrollbar();
291 void LyXText::cursorNext()
295 RowList::iterator rit = cursorRow();
296 if (rit == lastRow()) {
297 int y = cursor.y() - rit->baseline() + cursorRow()->height();
298 if (y > topy + bv()->workHeight())
299 bv()->updateScrollbar();
303 int y = topy + bv()->workHeight();
304 if (inset_owner && !topy) {
305 y -= (bv()->text->cursor.iy()
306 - bv()->text->top_y()
307 + bv()->theLockingInset()->insetInInsetY());
310 ParagraphList::iterator dummypit;
311 y = getRowNearY(y, dummypit)->y();
313 setCursorFromCoordinates(cursor.x_fix(), y);
314 // + bv->workHeight());
318 if (rit == bv()->text->cursorRow()) {
319 // we have a row which is taller than the workarea. The
320 // simplest solution is to move to the next row instead.
323 // This is what we used to do, so we wouldn't skip right past
324 // tall rows, but it's not working right now.
326 new_y = bv->text->top_y() + bv->workHeight();
330 new_y = bv()->text->cursor.iy()
331 + bv()->theLockingInset()->insetInInsetY()
332 + y - rit->baseline();
334 new_y = cursor.y() - cursorRow()->baseline();
338 ParagraphList::iterator pit = cursor.par();
341 setCursor(cur, pit, rit->pos(), false);
342 if (cur.y() < top_y() + bv()->workHeight())
344 bv()->updateScrollbar();
350 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
353 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
354 replaceSelection(lt);
355 if (!bv->insertInset(new_inset))
362 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
363 bool edit, bool pastesel)
365 InsetOld * inset = createInset(cmd);
366 BufferView * bv = cmd.view();
370 if (lt->selection.set()) {
371 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
374 if (bv->insertInset(inset)) {
376 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
377 inset->localDispatch(cmd);
379 if (gotsel && pastesel)
380 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
389 InsetOld::RESULT LyXText::dispatch(FuncRequest const & cmd)
391 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
392 <<"] arg[" << cmd.argument << ']' << endl;
394 BufferView * bv = cmd.view();
396 switch (cmd.action) {
398 case LFUN_APPENDIX: {
399 ParagraphList::iterator pit = cursor.par();
400 bool start = !pit->params().startOfAppendix();
402 // ensure that we have only one start_of_appendix in this document
403 ParagraphList::iterator tmp = ownerParagraphs().begin();
404 ParagraphList::iterator end = ownerParagraphs().end();
406 for (; tmp != end; ++tmp) {
407 if (tmp->params().startOfAppendix()) {
408 recordUndo(bv, Undo::ATOMIC, tmp);
409 tmp->params().startOfAppendix(false);
415 recordUndo(bv, Undo::ATOMIC, pit);
416 pit->params().startOfAppendix(start);
418 // we can set the refreshing parameters now
420 redoParagraph(cursor.par());
421 setCursor(cursor.par(), cursor.pos());
426 case LFUN_DELETE_WORD_FORWARD:
427 bv->beforeChange(this);
432 case LFUN_DELETE_WORD_BACKWARD:
433 bv->beforeChange(this);
434 deleteWordBackward();
438 case LFUN_DELETE_LINE_FORWARD:
439 bv->beforeChange(this);
445 if (!selection.mark())
446 bv->beforeChange(this);
447 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
450 cursorRightOneWord();
455 if (!selection.mark())
456 bv->beforeChange(this);
457 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
458 cursorRightOneWord();
464 case LFUN_BEGINNINGBUF:
465 if (!selection.mark())
466 bv->beforeChange(this);
472 if (selection.mark())
473 bv->beforeChange(this);
479 if (!selection.set())
480 selection.cursor = cursor;
481 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
485 finishChange(bv, true);
489 if (!selection.set())
490 selection.cursor = cursor;
491 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
495 finishChange(bv, true);
499 if (!selection.set())
500 selection.cursor = cursor;
502 finishChange(bv, true);
506 if (!selection.set())
507 selection.cursor = cursor;
509 finishChange(bv, true);
512 case LFUN_UP_PARAGRAPHSEL:
513 if (!selection.set())
514 selection.cursor = cursor;
516 finishChange(bv, true);
519 case LFUN_DOWN_PARAGRAPHSEL:
520 if (!selection.set())
521 selection.cursor = cursor;
522 cursorDownParagraph();
523 finishChange(bv, true);
527 if (!selection.set())
528 selection.cursor = cursor;
530 finishChange(bv, true);
534 if (!selection.set())
535 selection.cursor = cursor;
537 finishChange(bv, true);
541 if (!selection.set())
542 selection.cursor = cursor;
544 finishChange(bv, true);
548 if (!selection.set())
549 selection.cursor = cursor;
551 finishChange(bv, true);
554 case LFUN_WORDRIGHTSEL:
555 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
558 cursorRightOneWord();
559 finishChange(bv, true);
562 case LFUN_WORDLEFTSEL:
563 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
564 cursorRightOneWord();
567 finishChange(bv, true);
571 LyXCursor cur1 = cursor;
573 ::getWord(cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs());
574 setCursor(cur1.par(), cur1.pos());
575 bv->beforeChange(this);
576 setCursor(cur2.par(), cur2.pos());
577 finishChange(bv, true);
582 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
583 if (!selection.mark())
584 bv->beforeChange(this);
587 if (cursor.pos() < cursor.par()->size()
588 && cursor.par()->isInset(cursor.pos())
589 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
590 InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
591 cmd.message(tmpinset->editMessage());
592 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
593 tmpinset->localDispatch(cmd1);
603 // This is soooo ugly. Isn`t it possible to make
605 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
606 if (!selection.mark())
607 bv->beforeChange(this);
608 LyXCursor const cur = cursor;
611 if ((is_rtl || cur != cursor) && // only if really moved!
612 cursor.pos() < cursor.par()->size() &&
613 cursor.par()->isInset(cursor.pos()) &&
614 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
615 InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
616 cmd.message(tmpinset->editMessage());
617 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
618 tmpinset->localDispatch(cmd1);
628 if (!selection.mark())
629 bv->beforeChange(this);
635 if (!selection.mark())
636 bv->beforeChange(this);
641 case LFUN_UP_PARAGRAPH:
642 if (!selection.mark())
643 bv->beforeChange(this);
648 case LFUN_DOWN_PARAGRAPH:
649 if (!selection.mark())
650 bv->beforeChange(this);
651 cursorDownParagraph();
652 finishChange(bv, false);
656 if (!selection.mark())
657 bv->beforeChange(this);
659 finishChange(bv, false);
663 if (!selection.mark())
664 bv->beforeChange(this);
666 finishChange(bv, false);
670 if (!selection.mark())
671 bv->beforeChange(this);
673 finishChange(bv, false);
677 if (!selection.mark())
678 bv->beforeChange(this);
680 finishChange(bv, false);
683 case LFUN_BREAKLINE: {
684 lyx::pos_type body = cursor.par()->beginningOfBody();
686 // Not allowed by LaTeX (labels or empty par)
687 if (cursor.pos() <= body)
690 replaceSelection(bv->getLyXText());
691 insertInset(new InsetNewline);
692 setCursor(cursor.par(), cursor.pos());
693 moveCursorUpdate(bv, false);
698 if (!selection.set()) {
700 selection.cursor = cursor;
701 // It is possible to make it a lot faster still
702 // just comment out the line below...
704 cutSelection(true, false);
706 moveCursorUpdate(bv, false);
707 bv->owner()->view_state_changed();
710 case LFUN_DELETE_SKIP:
711 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
712 if (!selection.set()) {
713 LyXCursor cur = cursor;
714 if (cur.pos() == cur.par()->size()) {
718 && !(cur.par()->params().spaceTop()
719 == VSpace (VSpace::NONE))) {
721 cur.par()->params().lineTop(),
722 cur.par()->params().lineBottom(),
723 cur.par()->params().pagebreakTop(),
724 cur.par()->params().pagebreakBottom(),
725 VSpace(VSpace::NONE),
726 cur.par()->params().spaceBottom(),
727 cur.par()->params().spacing(),
728 cur.par()->params().align(),
729 cur.par()->params().labelWidthString(), 0);
734 selection.cursor = cursor;
738 selection.cursor = cursor;
741 cutSelection(true, false);
748 if (!selection.set()) {
749 if (bv->owner()->getIntl().getTransManager().backspace()) {
751 selection.cursor = cursor;
752 // It is possible to make it a lot faster still
753 // just comment out the line below...
756 cutSelection(true, false);
758 bv->owner()->view_state_changed();
763 case LFUN_BACKSPACE_SKIP:
764 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
765 if (!selection.set()) {
766 LyXCursor cur = cursor;
768 && !(cur.par()->params().spaceTop() == VSpace(VSpace::NONE))) {
770 cur.par()->params().lineTop(),
771 cur.par()->params().lineBottom(),
772 cur.par()->params().pagebreakTop(),
773 cur.par()->params().pagebreakBottom(),
774 VSpace(VSpace::NONE),
775 cur.par()->params().spaceBottom(),
776 cur.par()->params().spacing(),
777 cur.par()->params().align(),
778 cur.par()->params().labelWidthString(), 0);
781 selection.cursor = cur;
784 cutSelection(true, false);
789 case LFUN_BREAKPARAGRAPH:
790 replaceSelection(bv->getLyXText());
791 breakParagraph(bv->buffer()->paragraphs, 0);
793 selection.cursor = cursor;
795 bv->owner()->view_state_changed();
798 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
799 replaceSelection(bv->getLyXText());
800 breakParagraph(bv->buffer()->paragraphs, 1);
802 selection.cursor = cursor;
804 bv->owner()->view_state_changed();
807 case LFUN_BREAKPARAGRAPH_SKIP: {
808 // When at the beginning of a paragraph, remove
809 // indentation and add a "defskip" at the top.
810 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
811 LyXCursor cur = cursor;
812 replaceSelection(bv->getLyXText());
813 if (cur.pos() == 0) {
814 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
816 cur.par()->params().lineTop(),
817 cur.par()->params().lineBottom(),
818 cur.par()->params().pagebreakTop(),
819 cur.par()->params().pagebreakBottom(),
820 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
821 cur.par()->params().spacing(),
822 cur.par()->params().align(),
823 cur.par()->params().labelWidthString(), 1);
827 breakParagraph(bv->buffer()->paragraphs, 0);
830 selection.cursor = cur;
832 bv->owner()->view_state_changed();
836 case LFUN_PARAGRAPH_SPACING: {
837 ParagraphList::iterator pit = cursor.par();
838 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
839 float cur_value = 1.0;
840 if (cur_spacing == Spacing::Other)
841 cur_value = pit->params().spacing().getValue();
843 istringstream is(STRCONV(cmd.argument));
846 Spacing::Space new_spacing = cur_spacing;
847 float new_value = cur_value;
849 lyxerr << "Missing argument to `paragraph-spacing'"
851 } else if (tmp == "single") {
852 new_spacing = Spacing::Single;
853 } else if (tmp == "onehalf") {
854 new_spacing = Spacing::Onehalf;
855 } else if (tmp == "double") {
856 new_spacing = Spacing::Double;
857 } else if (tmp == "other") {
858 new_spacing = Spacing::Other;
861 lyxerr << "new_value = " << tmpval << endl;
864 } else if (tmp == "default") {
865 new_spacing = Spacing::Default;
867 lyxerr << _("Unknown spacing argument: ")
868 << cmd.argument << endl;
870 if (cur_spacing != new_spacing || cur_value != new_value) {
871 pit->params().spacing(Spacing(new_spacing, new_value));
878 case LFUN_INSET_SETTINGS:
879 Assert(bv->theLockingInset());
880 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
883 case LFUN_INSET_TOGGLE:
884 bv->beforeChange(this);
890 case LFUN_SPACE_INSERT:
891 if (cursor.par()->layout()->free_spacing)
894 doInsertInset(this, cmd, false, false);
895 moveCursorUpdate(bv, false);
898 case LFUN_HYPHENATION:
899 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
902 case LFUN_LIGATURE_BREAK:
903 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
907 specialChar(this, bv, InsetSpecialChar::LDOTS);
910 case LFUN_END_OF_SENTENCE:
911 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
914 case LFUN_MENU_SEPARATOR:
915 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
919 bv->beforeChange(this);
921 selection.cursor = cursor;
922 cmd.message(N_("Mark off"));
926 bv->beforeChange(this);
927 selection.mark(true);
929 selection.cursor = cursor;
930 cmd.message(N_("Mark on"));
934 bv->beforeChange(this);
935 if (selection.mark()) {
936 cmd.message(N_("Mark removed"));
938 selection.mark(true);
939 cmd.message(N_("Mark set"));
941 selection.cursor = cursor;
945 case LFUN_UPCASE_WORD:
946 changeCase(LyXText::text_uppercase);
950 case LFUN_LOWCASE_WORD:
951 changeCase(LyXText::text_lowercase);
955 case LFUN_CAPITALIZE_WORD:
956 changeCase(LyXText::text_capitalization);
960 case LFUN_TRANSPOSE_CHARS:
961 recordUndo(bv, Undo::ATOMIC, cursor.par());
967 cmd.message(_("Paste"));
968 replaceSelection(bv->getLyXText());
969 size_t sel_index = 0;
970 string const & arg = cmd.argument;
971 if (isStrUnsignedInt(arg)) {
972 size_t const paste_arg = strToUnsignedInt(arg);
973 #warning FIXME Check if the arg is in the domain of available selections.
974 sel_index = paste_arg;
976 pasteSelection(sel_index);
977 clearSelection(); // bug 393
984 cutSelection(true, true);
985 cmd.message(_("Cut"));
991 cmd.message(_("Copy"));
994 case LFUN_BEGINNINGBUFSEL:
998 finishChange(bv, true);
1001 case LFUN_ENDBUFSEL:
1003 return UNDISPATCHED;
1005 finishChange(bv, true);
1009 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1015 istringstream is(STRCONV(cmd.argument));
1018 lyxerr << "SETXY: Could not parse coordinates in '"
1019 << cmd.argument << std::endl;
1021 setCursorFromCoordinates(x, y);
1026 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1028 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1034 case LFUN_GETLAYOUT:
1035 cmd.message(tostr(cursor.par()->layout()));
1039 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1040 << cmd.argument << endl;
1042 // This is not the good solution to the empty argument
1043 // problem, but it will hopefully suffice for 1.2.0.
1044 // The correct solution would be to augument the
1045 // function list/array with information about what
1046 // functions needs arguments and their type.
1047 if (cmd.argument.empty()) {
1048 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1052 // Derive layout number from given argument (string)
1053 // and current buffer's textclass (number)
1054 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1055 bool hasLayout = tclass.hasLayout(cmd.argument);
1056 string layout = cmd.argument;
1058 // If the entry is obsolete, use the new one instead.
1060 string const & obs = tclass[layout]->obsoleted_by();
1066 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1071 bool change_layout = (current_layout != layout);
1073 if (!change_layout && selection.set() &&
1074 selection.start.par() != selection.end.par())
1076 ParagraphList::iterator spit = selection.start.par();
1077 ParagraphList::iterator epit = boost::next(selection.end.par());
1078 while (spit != epit) {
1079 if (spit->layout()->name() != current_layout) {
1080 change_layout = true;
1087 if (change_layout) {
1088 current_layout = layout;
1090 bv->owner()->setLayout(layout);
1097 case LFUN_PASTESELECTION: {
1098 // this was originally a beforeChange(bv->text), i.e
1099 // the outermost LyXText!
1100 bv->beforeChange(this);
1101 string const clip = bv->getClipboard();
1102 if (!clip.empty()) {
1103 if (cmd.argument == "paragraph")
1104 insertStringAsParagraphs(clip);
1106 insertStringAsLines(clip);
1113 case LFUN_GOTOERROR:
1114 gotoInset(InsetOld::ERROR_CODE, false);
1118 gotoInset(InsetOld::NOTE_CODE, false);
1121 case LFUN_REFERENCE_GOTO:
1123 vector<InsetOld::Code> tmp;
1124 tmp.push_back(InsetOld::LABEL_CODE);
1125 tmp.push_back(InsetOld::REF_CODE);
1126 gotoInset(tmp, true);
1131 replaceSelection(bv->getLyXText());
1132 ParagraphList::iterator pit = cursor.par();
1133 lyx::pos_type pos = cursor.pos();
1137 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1140 c = pit->getChar(pos - 1);
1142 LyXLayout_ptr const & style = pit->layout();
1144 if (style->pass_thru ||
1145 pit->getFontSettings(bv->buffer()->params,
1146 pos).language()->lang() == "hebrew" ||
1147 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1148 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1152 case LFUN_DATE_INSERT: {
1153 replaceSelection(bv->getLyXText());
1154 time_t now_time_t = time(NULL);
1155 struct tm * now_tm = localtime(&now_time_t);
1156 setlocale(LC_TIME, "");
1158 if (!cmd.argument.empty())
1161 arg = lyxrc.date_insert_format;
1163 int const datetmp_len =
1164 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1166 for (int i = 0; i < datetmp_len; i++)
1167 insertChar(datetmp[i]);
1169 selection.cursor = cursor;
1170 moveCursorUpdate(bv, false);
1174 case LFUN_MOUSE_TRIPLE:
1177 if (!isInInset() && bv->theLockingInset())
1179 if (cmd.button() == mouse_button::button1) {
1181 selection.cursor = cursor;
1185 bv->haveSelection(selection.set());
1189 case LFUN_MOUSE_DOUBLE:
1192 if (!isInInset() && bv->theLockingInset())
1194 if (cmd.button() == mouse_button::button1) {
1195 selectWord(lyx::WHOLE_WORD_STRICT);
1197 bv->haveSelection(selection.set());
1201 case LFUN_MOUSE_MOTION:
1203 // Only use motion with button 1
1204 //if (ev.button() != mouse_button::button1)
1210 // Check for inset locking
1211 if (bv->theLockingInset()) {
1212 InsetOld * tli = bv->theLockingInset();
1213 LyXCursor cursor = bv->text->cursor;
1214 LyXFont font = bv->text->getFont(cursor.par(), cursor.pos());
1215 int width = tli->width();
1216 int inset_x = font.isVisibleRightToLeft()
1217 ? cursor.ix() - width : cursor.ix();
1218 int start_x = inset_x + tli->scroll();
1219 FuncRequest cmd1 = cmd;
1220 cmd1.x = cmd.x - start_x;
1221 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1222 tli->localDispatch(cmd1);
1226 // The test for not selection possible is needed, that only motion
1227 // events are used, where the bottom press event was on
1228 // the drawing area too
1229 if (!selection_possible) {
1230 lyxerr[Debug::ACTION]
1231 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1235 RowList::iterator cursorrow = bv->text->cursorRow();
1236 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->text->top_y());
1238 // sorry for this but I have a strange error that the y value jumps at
1239 // a certain point. This seems like an error in my xforms library or
1240 // in some other local environment, but I would like to leave this here
1241 // for the moment until I can remove this (Jug 20020418)
1242 if (y_before < bv->text->cursor.y())
1243 lyxerr << y_before << ':'
1244 << bv->text->cursor.y() << endl;
1246 // This is to allow jumping over large insets
1247 if (cursorrow == bv->text->cursorRow()) {
1248 if (cmd.y >= bv->workHeight())
1249 bv->text->cursorDown(false);
1251 bv->text->cursorUp(false);
1254 bv->text->setSelection();
1259 // Single-click on work area
1260 case LFUN_MOUSE_PRESS:
1265 // ok ok, this is a hack (for xforms)
1266 // We shouldn't go further down as we really should only do the
1267 // scrolling and be done with this. Otherwise we may open some
1268 // dialogs (Jug 20020424).
1269 if (cmd.button() == mouse_button::button4) {
1270 bv->scroll(-lyxrc.wheel_jump);
1273 if (cmd.button() == mouse_button::button5) {
1274 bv->scroll(lyxrc.wheel_jump);
1280 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1282 // Middle button press pastes if we have a selection
1283 // We do this here as if the selection was inside an inset
1284 // it could get cleared on the unlocking of the inset so
1285 // we have to check this first
1286 bool paste_internally = false;
1287 if (cmd.button() == mouse_button::button2 && selection.set()) {
1288 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1289 paste_internally = true;
1292 int const screen_first = bv->text->top_y();
1294 if (bv->theLockingInset()) {
1295 // We are in inset locking mode
1297 // Check whether the inset was hit. If not reset mode,
1298 // otherwise give the event to the inset
1299 if (inset_hit == bv->theLockingInset()) {
1300 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1301 bv->theLockingInset()->localDispatch(cmd1);
1304 bv->unlockInset(bv->theLockingInset());
1308 selection_possible = true;
1310 // Clear the selection
1311 bv->text->clearSelection();
1313 bv->updateScrollbar();
1315 // Single left click in math inset?
1316 if (isHighlyEditableInset(inset_hit)) {
1317 // Highly editable inset, like math
1318 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1319 selection_possible = false;
1320 bv->owner()->message(inset->editMessage());
1321 // We just have to lock the inset before calling a PressEvent on it!
1322 if (!bv->lockInset(inset))
1323 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1324 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1325 inset->localDispatch(cmd1);
1328 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1330 // Right click on a footnote flag opens float menu
1331 if (cmd.button() == mouse_button::button3) {
1332 selection_possible = false;
1336 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1337 bv->text->setCursorFromCoordinates(x, y + screen_first);
1339 bv->text->selection.cursor = bv->text->cursor;
1340 bv->text->cursor.x_fix(bv->text->cursor.x());
1342 if (bv->fitCursor())
1343 selection_possible = false;
1345 // Insert primary selection with middle mouse
1346 // if there is a local selection in the current buffer,
1348 if (cmd.button() == mouse_button::button2) {
1349 if (paste_internally)
1350 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1352 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1353 selection_possible = false;
1358 case LFUN_MOUSE_RELEASE:
1360 // do nothing if we used the mouse wheel
1364 if (cmd.button() == mouse_button::button4
1365 || cmd.button() == mouse_button::button5)
1368 // If we hit an inset, we have the inset coordinates in these
1369 // and inset_hit points to the inset. If we do not hit an
1370 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1373 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1375 if (bv->theLockingInset()) {
1376 // We are in inset locking mode.
1378 // LyX does a kind of work-area grabbing for insets.
1379 // Only a ButtonPress FuncRequest outside the inset will
1380 // force a insetUnlock.
1381 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1382 bv->theLockingInset()->localDispatch(cmd1);
1386 selection_possible = false;
1388 if (cmd.button() == mouse_button::button2)
1392 if (cmd.button() == mouse_button::button1)
1393 bv->haveSelection(selection.set());
1396 bv->owner()->view_state_changed();
1397 bv->owner()->updateMenubar();
1398 bv->owner()->updateToolbar();
1400 // Did we hit an editable inset?
1402 selection_possible = false;
1404 // if we reach this point with a selection, it
1405 // must mean we are currently selecting.
1406 // But we don't want to open the inset
1407 // because that is annoying for the user.
1408 // So just pretend we didn't hit it.
1409 // this is OK because a "kosher" ButtonRelease
1410 // will follow a ButtonPress that clears
1412 // Note this also fixes selection drawing
1413 // problems if we end up opening an inset
1414 if (selection.set())
1417 // CHECK fix this proper in 0.13
1418 // well, maybe 13.0 !!!!!!!!!
1420 // Following a ref shouldn't issue
1421 // a push on the undo-stack
1422 // anylonger, now that we have
1423 // keybindings for following
1424 // references and returning from
1425 // references. IMHO though, it
1426 // should be the inset's own business
1427 // to push or not push on the undo
1428 // stack. They don't *have* to
1429 // alter the document...
1431 // ...or maybe the recordUndo()
1432 // below isn't necessary at all anylonger?
1433 if (inset_hit->lyxCode() == InsetOld::REF_CODE)
1434 recordUndo(bv, Undo::ATOMIC);
1436 bv->owner()->message(inset_hit->editMessage());
1438 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1439 inset_hit->localDispatch(cmd1);
1445 case LFUN_SELFINSERT: {
1446 if (cmd.argument.empty())
1449 // Automatically delete the currently selected
1450 // text and replace it with what is being
1451 // typed in now. Depends on lyxrc settings
1452 // "auto_region_delete", which defaults to
1455 if (lyxrc.auto_region_delete) {
1456 if (selection.set())
1457 cutSelection(false, false);
1458 bv->haveSelection(false);
1461 bv->beforeChange(this);
1462 LyXFont const old_font = real_current_font;
1464 string::const_iterator cit = cmd.argument.begin();
1465 string::const_iterator end = cmd.argument.end();
1466 for (; cit != end; ++cit)
1467 bv->owner()->getIntl().getTransManager().
1468 TranslateAndInsert(*cit, this);
1470 selection.cursor = cursor;
1471 moveCursorUpdate(bv, false);
1473 // real_current_font.number can change so we need to
1474 // update the minibuffer
1475 if (old_font != real_current_font)
1476 bv->owner()->view_state_changed();
1477 bv->updateScrollbar();
1481 case LFUN_HTMLURL: {
1482 InsetCommandParams p("htmlurl");
1483 string const data = InsetCommandMailer::params2string("url", p);
1484 bv->owner()->getDialogs().show("url", data, 0);
1489 InsetCommandParams p("url");
1490 string const data = InsetCommandMailer::params2string("url", p);
1491 bv->owner()->getDialogs().show("url", data, 0);
1497 case LFUN_INSET_LIST:
1498 case LFUN_INSET_THEOREM:
1499 case LFUN_INSET_CAPTION:
1501 case LFUN_INSERT_NOTE:
1502 case LFUN_INSERT_BRANCH:
1503 case LFUN_INSERT_BIBITEM:
1504 case LFUN_INSET_ERT:
1505 case LFUN_INSET_FLOAT:
1506 case LFUN_INSET_FOOTNOTE:
1507 case LFUN_INSET_MARGINAL:
1508 case LFUN_INSET_MINIPAGE:
1509 case LFUN_INSET_OPTARG:
1510 case LFUN_INSET_WIDE_FLOAT:
1511 case LFUN_INSET_WRAP:
1512 case LFUN_TABULAR_INSERT:
1513 case LFUN_ENVIRONMENT_INSERT:
1514 // Open the inset, and move the current selection
1516 doInsertInset(this, cmd, true, true);
1519 case LFUN_INDEX_INSERT:
1520 // Just open the inset
1521 doInsertInset(this, cmd, true, false);
1524 case LFUN_INDEX_PRINT:
1525 case LFUN_TOC_INSERT:
1528 doInsertInset(this, cmd, false, false);
1532 return UNDISPATCHED;