1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2002 The LyX Team.
9 * ====================================================== */
14 #include "paragraph.h"
15 #include "BufferView.h"
16 #include "funcrequest.h"
20 #include "bufferparams.h"
22 #include "bufferview_funcs.h"
23 #include "ParagraphParameters.h"
29 #include "support/tostr.h"
30 #include "support/lstrings.h"
31 #include "support/LAssert.h"
32 #include "frontends/LyXView.h"
33 #include "frontends/screen.h"
34 #include "frontends/Dialogs.h"
35 #include "insets/insetspecialchar.h"
36 #include "insets/insettext.h"
37 #include "insets/insetquotes.h"
38 #include "insets/insetcommand.h"
39 #include "insets/insetnewline.h"
40 #include "undo_funcs.h"
41 #include "text_funcs.h"
47 using namespace lyx::support;
48 using namespace bv_funcs;
55 extern string current_layout;
56 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
58 // the selection possible is needed, that only motion events are
59 // used, where the bottom press event was on the drawing area too
60 bool selection_possible = false;
65 void moveCursorUpdate(BufferView * bv, bool selecting)
67 LyXText * lt = bv->getLyXText();
69 //if (!lt->selection.set())
70 // lt->selection.cursor = lt->cursor;
72 if (selecting || lt->selection.mark())
75 if (!lt->selection.set())
76 bv->haveSelection(false);
83 void finishChange(BufferView * bv, bool selecting = false)
86 moveCursorUpdate(bv, selecting);
87 bv->owner()->view_state_changed();
90 // check if the given co-ordinates are inside an inset at the
91 // given cursor, if one exists. If so, the inset is returned,
92 // and the co-ordinates are made relative. Otherwise, 0 is returned.
93 InsetOld * checkInset(BufferView * /*bv*/, LyXText & text,
94 LyXCursor const & cur, int & x, int & y)
96 lyx::pos_type const pos = cur.pos();
97 ParagraphList::iterator par = cur.par();
99 if (pos >= par->size() || !par->isInset(pos))
102 InsetOld /*const*/ * inset = par->getInset(pos);
104 if (!isEditableInset(inset))
107 // get inset dimensions
108 Assert(par->getInset(pos));
110 LyXFont const & font = text.getFont(par, pos);
112 int const width = inset->width();
113 int const inset_x = font.isVisibleRightToLeft()
114 ? (cur.ix() - width) : cur.ix();
117 inset_x + inset->scroll(),
119 cur.iy() - inset->ascent(),
120 cur.iy() + inset->descent()
123 if (!b.contained(x, y)) {
124 lyxerr[Debug::GUI] << "Missed inset at x,y "
126 << " box " << b << endl;
130 text.setCursor(par, pos, true);
133 // The origin of an inset is on the baseline
134 y -= text.cursor.iy();
142 InsetOld * LyXText::checkInsetHit(int & x, int & y)
144 int y_tmp = y + top_y();
147 setCursorFromCoordinates(cur, x, y_tmp);
149 InsetOld * inset = checkInset(bv(), *this, cur, x, y_tmp);
155 // look at previous position
160 setCursor(cur, cur.par(), cur.pos() - 1, true);
162 inset = checkInset(bv(), *this, cur, x, y_tmp);
169 bool LyXText::gotoNextInset(vector<InsetOld::Code> const & codes,
170 string const & contents)
172 ParagraphList::iterator end = ownerParagraphs().end();
173 ParagraphList::iterator pit = cursor.par();
174 pos_type pos = cursor.pos();
178 if (pos + 1 < pit->size()) {
185 } while (pit != end &&
186 !(pit->isInset(pos) &&
187 (inset = pit->getInset(pos)) != 0 &&
188 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
190 static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
194 setCursor(pit, pos, false);
201 void LyXText::gotoInset(vector<InsetOld::Code> const & codes,
204 bv()->beforeChange(this);
207 if (same_content && cursor.pos() < cursor.par()->size()
208 && cursor.par()->isInset(cursor.pos())) {
209 InsetOld const * inset = cursor.par()->getInset(cursor.pos());
210 if (find(codes.begin(), codes.end(), inset->lyxCode())
212 contents = static_cast<InsetCommand const *>(inset)->getContents();
215 if (!gotoNextInset(codes, contents)) {
216 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
217 LyXCursor tmp = cursor;
218 cursor.par(ownerParagraphs().begin());
220 if (!gotoNextInset(codes, contents)) {
222 bv()->owner()->message(_("No more insets"));
225 bv()->owner()->message(_("No more insets"));
229 selection.cursor = cursor;
233 void LyXText::gotoInset(InsetOld::Code code, bool same_content)
235 gotoInset(vector<InsetOld::Code>(1, code), same_content);
239 void LyXText::cursorPrevious()
243 RowList::iterator rit = cursorRow();
245 if (rit == firstRow()) {
247 bv()->updateScrollbar();
251 setCursorFromCoordinates(cursor.x_fix(), y);
255 if (rit == bv()->text->cursorRow()) {
256 // we have a row which is taller than the workarea. The
257 // simplest solution is to move to the previous row instead.
260 // This is what we used to do, so we wouldn't skip right past
261 // tall rows, but it's not working right now.
264 new_y = bv()->text->cursor.iy()
265 + bv()->theLockingInset()->insetInInsetY() + y
267 - bv()->workHeight() + 1;
272 - bv()->workHeight() + 1;
277 ParagraphList::iterator pit = cursor.par();
278 previousRow(pit, rit);
279 setCursor(cur, pit, rit->pos(), false);
280 if (cur.y() > top_y())
282 bv()->updateScrollbar();
286 void LyXText::cursorNext()
290 RowList::iterator rit = cursorRow();
291 if (rit == lastRow()) {
292 int y = cursor.y() - rit->baseline() + cursorRow()->height();
293 if (y > topy + bv()->workHeight())
294 bv()->updateScrollbar();
298 int y = topy + bv()->workHeight();
299 if (inset_owner && !topy) {
300 y -= (bv()->text->cursor.iy()
301 - bv()->text->top_y()
302 + bv()->theLockingInset()->insetInInsetY());
305 ParagraphList::iterator dummypit;
306 getRowNearY(y, dummypit);
308 setCursorFromCoordinates(cursor.x_fix(), y);
309 // + bv->workHeight());
313 if (rit == bv()->text->cursorRow()) {
314 // we have a row which is taller than the workarea. The
315 // simplest solution is to move to the next row instead.
318 // This is what we used to do, so we wouldn't skip right past
319 // tall rows, but it's not working right now.
321 new_y = bv->text->top_y() + bv->workHeight();
325 new_y = bv()->text->cursor.iy()
326 + bv()->theLockingInset()->insetInInsetY()
327 + y - rit->baseline();
329 new_y = cursor.y() - cursorRow()->baseline();
333 ParagraphList::iterator pit = cursor.par();
336 setCursor(cur, pit, rit->pos(), false);
337 if (cur.y() < top_y() + bv()->workHeight())
339 bv()->updateScrollbar();
345 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
348 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
349 replaceSelection(lt);
350 if (!bv->insertInset(new_inset))
357 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
358 bool edit, bool pastesel)
360 InsetOld * inset = createInset(cmd);
361 BufferView * bv = cmd.view();
365 if (lt->selection.set()) {
366 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
369 if (bv->insertInset(inset)) {
371 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
372 inset->localDispatch(cmd);
374 if (gotsel && pastesel)
375 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
384 InsetOld::RESULT LyXText::dispatch(FuncRequest const & cmd)
386 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
387 <<"] arg[" << cmd.argument << ']' << endl;
389 BufferView * bv = cmd.view();
391 switch (cmd.action) {
393 case LFUN_APPENDIX: {
394 ParagraphList::iterator pit = cursor.par();
395 bool start = !pit->params().startOfAppendix();
397 // ensure that we have only one start_of_appendix in this document
398 ParagraphList::iterator tmp = ownerParagraphs().begin();
399 ParagraphList::iterator end = ownerParagraphs().end();
401 for (; tmp != end; ++tmp) {
402 if (tmp->params().startOfAppendix()) {
403 recordUndo(bv, Undo::ATOMIC, tmp);
404 tmp->params().startOfAppendix(false);
405 setHeightOfRow(tmp, getRow(tmp, 0));
410 recordUndo(bv, Undo::ATOMIC, pit);
411 pit->params().startOfAppendix(start);
413 // we can set the refreshing parameters now
415 redoParagraph(cursor.par());
416 setCursor(cursor.par(), cursor.pos());
421 case LFUN_DELETE_WORD_FORWARD:
422 bv->beforeChange(this);
427 case LFUN_DELETE_WORD_BACKWARD:
428 bv->beforeChange(this);
429 deleteWordBackward();
433 case LFUN_DELETE_LINE_FORWARD:
434 bv->beforeChange(this);
440 if (!selection.mark())
441 bv->beforeChange(this);
442 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
445 cursorRightOneWord();
450 if (!selection.mark())
451 bv->beforeChange(this);
452 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
453 cursorRightOneWord();
459 case LFUN_BEGINNINGBUF:
460 if (!selection.mark())
461 bv->beforeChange(this);
467 if (selection.mark())
468 bv->beforeChange(this);
474 if (!selection.set())
475 selection.cursor = cursor;
476 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
480 finishChange(bv, true);
484 if (!selection.set())
485 selection.cursor = cursor;
486 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
490 finishChange(bv, true);
494 if (!selection.set())
495 selection.cursor = cursor;
497 finishChange(bv, true);
501 if (!selection.set())
502 selection.cursor = cursor;
504 finishChange(bv, true);
507 case LFUN_UP_PARAGRAPHSEL:
508 if (!selection.set())
509 selection.cursor = cursor;
511 finishChange(bv, true);
514 case LFUN_DOWN_PARAGRAPHSEL:
515 if (!selection.set())
516 selection.cursor = cursor;
517 cursorDownParagraph();
518 finishChange(bv, true);
522 if (!selection.set())
523 selection.cursor = cursor;
525 finishChange(bv, true);
529 if (!selection.set())
530 selection.cursor = cursor;
532 finishChange(bv, true);
536 if (!selection.set())
537 selection.cursor = cursor;
539 finishChange(bv, true);
543 if (!selection.set())
544 selection.cursor = cursor;
546 finishChange(bv, true);
549 case LFUN_WORDRIGHTSEL:
550 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
553 cursorRightOneWord();
554 finishChange(bv, true);
557 case LFUN_WORDLEFTSEL:
558 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
559 cursorRightOneWord();
562 finishChange(bv, true);
566 LyXCursor cur1 = cursor;
568 ::getWord(cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs());
569 setCursor(cur1.par(), cur1.pos());
570 bv->beforeChange(this);
571 setCursor(cur2.par(), cur2.pos());
572 finishChange(bv, true);
577 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
578 if (!selection.mark())
579 bv->beforeChange(this);
582 if (cursor.pos() < cursor.par()->size()
583 && cursor.par()->isInset(cursor.pos())
584 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
585 InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
586 cmd.message(tmpinset->editMessage());
587 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
588 tmpinset->localDispatch(cmd1);
598 // This is soooo ugly. Isn`t it possible to make
600 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
601 if (!selection.mark())
602 bv->beforeChange(this);
603 LyXCursor const cur = cursor;
606 if ((is_rtl || cur != cursor) && // only if really moved!
607 cursor.pos() < cursor.par()->size() &&
608 cursor.par()->isInset(cursor.pos()) &&
609 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
610 InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
611 cmd.message(tmpinset->editMessage());
612 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
613 tmpinset->localDispatch(cmd1);
623 if (!selection.mark())
624 bv->beforeChange(this);
630 if (!selection.mark())
631 bv->beforeChange(this);
636 case LFUN_UP_PARAGRAPH:
637 if (!selection.mark())
638 bv->beforeChange(this);
643 case LFUN_DOWN_PARAGRAPH:
644 if (!selection.mark())
645 bv->beforeChange(this);
646 cursorDownParagraph();
647 finishChange(bv, false);
651 if (!selection.mark())
652 bv->beforeChange(this);
654 finishChange(bv, false);
658 if (!selection.mark())
659 bv->beforeChange(this);
661 finishChange(bv, false);
665 if (!selection.mark())
666 bv->beforeChange(this);
668 finishChange(bv, false);
672 if (!selection.mark())
673 bv->beforeChange(this);
675 finishChange(bv, false);
678 case LFUN_BREAKLINE: {
679 lyx::pos_type body = cursor.par()->beginningOfBody();
681 // Not allowed by LaTeX (labels or empty par)
682 if (cursor.pos() <= body)
685 replaceSelection(bv->getLyXText());
686 insertInset(new InsetNewline);
687 setCursor(cursor.par(), cursor.pos());
688 moveCursorUpdate(bv, false);
693 if (!selection.set()) {
695 selection.cursor = cursor;
696 // It is possible to make it a lot faster still
697 // just comment out the line below...
699 cutSelection(true, false);
701 moveCursorUpdate(bv, false);
702 bv->owner()->view_state_changed();
705 case LFUN_DELETE_SKIP:
706 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
707 if (!selection.set()) {
708 LyXCursor cur = cursor;
709 if (cur.pos() == cur.par()->size()) {
713 && !(cur.par()->params().spaceTop()
714 == VSpace (VSpace::NONE))) {
716 cur.par()->params().lineTop(),
717 cur.par()->params().lineBottom(),
718 cur.par()->params().pagebreakTop(),
719 cur.par()->params().pagebreakBottom(),
720 VSpace(VSpace::NONE),
721 cur.par()->params().spaceBottom(),
722 cur.par()->params().spacing(),
723 cur.par()->params().align(),
724 cur.par()->params().labelWidthString(), 0);
729 selection.cursor = cursor;
733 selection.cursor = cursor;
736 cutSelection(true, false);
743 if (!selection.set()) {
744 if (bv->owner()->getIntl().getTransManager().backspace()) {
746 selection.cursor = cursor;
747 // It is possible to make it a lot faster still
748 // just comment out the line below...
751 cutSelection(true, false);
753 bv->owner()->view_state_changed();
758 case LFUN_BACKSPACE_SKIP:
759 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
760 if (!selection.set()) {
761 LyXCursor cur = cursor;
763 && !(cur.par()->params().spaceTop() == VSpace(VSpace::NONE))) {
765 cur.par()->params().lineTop(),
766 cur.par()->params().lineBottom(),
767 cur.par()->params().pagebreakTop(),
768 cur.par()->params().pagebreakBottom(),
769 VSpace(VSpace::NONE),
770 cur.par()->params().spaceBottom(),
771 cur.par()->params().spacing(),
772 cur.par()->params().align(),
773 cur.par()->params().labelWidthString(), 0);
776 selection.cursor = cur;
779 cutSelection(true, false);
784 case LFUN_BREAKPARAGRAPH:
785 replaceSelection(bv->getLyXText());
786 breakParagraph(bv->buffer()->paragraphs, 0);
788 selection.cursor = cursor;
790 bv->owner()->view_state_changed();
793 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
794 replaceSelection(bv->getLyXText());
795 breakParagraph(bv->buffer()->paragraphs, 1);
797 selection.cursor = cursor;
799 bv->owner()->view_state_changed();
802 case LFUN_BREAKPARAGRAPH_SKIP: {
803 // When at the beginning of a paragraph, remove
804 // indentation and add a "defskip" at the top.
805 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
806 LyXCursor cur = cursor;
807 replaceSelection(bv->getLyXText());
808 if (cur.pos() == 0) {
809 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
811 cur.par()->params().lineTop(),
812 cur.par()->params().lineBottom(),
813 cur.par()->params().pagebreakTop(),
814 cur.par()->params().pagebreakBottom(),
815 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
816 cur.par()->params().spacing(),
817 cur.par()->params().align(),
818 cur.par()->params().labelWidthString(), 1);
822 breakParagraph(bv->buffer()->paragraphs, 0);
825 selection.cursor = cur;
827 bv->owner()->view_state_changed();
831 case LFUN_PARAGRAPH_SPACING: {
832 ParagraphList::iterator pit = cursor.par();
833 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
834 float cur_value = 1.0;
835 if (cur_spacing == Spacing::Other)
836 cur_value = pit->params().spacing().getValue();
838 istringstream is(STRCONV(cmd.argument));
841 Spacing::Space new_spacing = cur_spacing;
842 float new_value = cur_value;
844 lyxerr << "Missing argument to `paragraph-spacing'"
846 } else if (tmp == "single") {
847 new_spacing = Spacing::Single;
848 } else if (tmp == "onehalf") {
849 new_spacing = Spacing::Onehalf;
850 } else if (tmp == "double") {
851 new_spacing = Spacing::Double;
852 } else if (tmp == "other") {
853 new_spacing = Spacing::Other;
856 lyxerr << "new_value = " << tmpval << endl;
859 } else if (tmp == "default") {
860 new_spacing = Spacing::Default;
862 lyxerr << _("Unknown spacing argument: ")
863 << cmd.argument << endl;
865 if (cur_spacing != new_spacing || cur_value != new_value) {
866 pit->params().spacing(Spacing(new_spacing, new_value));
873 case LFUN_INSET_SETTINGS:
874 Assert(bv->theLockingInset());
875 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
878 case LFUN_INSET_TOGGLE:
879 bv->beforeChange(this);
885 case LFUN_SPACE_INSERT:
886 if (cursor.par()->layout()->free_spacing)
889 doInsertInset(this, cmd, false, false);
890 moveCursorUpdate(bv, false);
893 case LFUN_HYPHENATION:
894 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
897 case LFUN_LIGATURE_BREAK:
898 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
902 specialChar(this, bv, InsetSpecialChar::LDOTS);
905 case LFUN_END_OF_SENTENCE:
906 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
909 case LFUN_MENU_SEPARATOR:
910 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
914 bv->beforeChange(this);
916 selection.cursor = cursor;
917 cmd.message(N_("Mark off"));
921 bv->beforeChange(this);
922 selection.mark(true);
924 selection.cursor = cursor;
925 cmd.message(N_("Mark on"));
929 bv->beforeChange(this);
930 if (selection.mark()) {
931 cmd.message(N_("Mark removed"));
933 selection.mark(true);
934 cmd.message(N_("Mark set"));
936 selection.cursor = cursor;
940 case LFUN_UPCASE_WORD:
941 changeCase(LyXText::text_uppercase);
945 case LFUN_LOWCASE_WORD:
946 changeCase(LyXText::text_lowercase);
950 case LFUN_CAPITALIZE_WORD:
951 changeCase(LyXText::text_capitalization);
955 case LFUN_TRANSPOSE_CHARS:
956 recordUndo(bv, Undo::ATOMIC, cursor.par());
962 cmd.message(_("Paste"));
963 replaceSelection(bv->getLyXText());
964 size_t sel_index = 0;
965 string const & arg = cmd.argument;
966 if (isStrUnsignedInt(arg)) {
967 size_t const paste_arg = strToUnsignedInt(arg);
968 #warning FIXME Check if the arg is in the domain of available selections.
969 sel_index = paste_arg;
971 pasteSelection(sel_index);
972 clearSelection(); // bug 393
979 cutSelection(true, true);
980 cmd.message(_("Cut"));
986 cmd.message(_("Copy"));
989 case LFUN_BEGINNINGBUFSEL:
993 finishChange(bv, true);
1000 finishChange(bv, true);
1004 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1010 istringstream is(STRCONV(cmd.argument));
1013 lyxerr << "SETXY: Could not parse coordinates in '"
1014 << cmd.argument << std::endl;
1016 setCursorFromCoordinates(x, y);
1021 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1023 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1029 case LFUN_GETLAYOUT:
1030 cmd.message(tostr(cursor.par()->layout()));
1034 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1035 << cmd.argument << endl;
1037 // This is not the good solution to the empty argument
1038 // problem, but it will hopefully suffice for 1.2.0.
1039 // The correct solution would be to augument the
1040 // function list/array with information about what
1041 // functions needs arguments and their type.
1042 if (cmd.argument.empty()) {
1043 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1047 // Derive layout number from given argument (string)
1048 // and current buffer's textclass (number)
1049 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1050 bool hasLayout = tclass.hasLayout(cmd.argument);
1051 string layout = cmd.argument;
1053 // If the entry is obsolete, use the new one instead.
1055 string const & obs = tclass[layout]->obsoleted_by();
1061 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1066 bool change_layout = (current_layout != layout);
1068 if (!change_layout && selection.set() &&
1069 selection.start.par() != selection.end.par())
1071 ParagraphList::iterator spit = selection.start.par();
1072 ParagraphList::iterator epit = boost::next(selection.end.par());
1073 while (spit != epit) {
1074 if (spit->layout()->name() != current_layout) {
1075 change_layout = true;
1082 if (change_layout) {
1083 current_layout = layout;
1085 bv->owner()->setLayout(layout);
1092 case LFUN_PASTESELECTION: {
1093 // this was originally a beforeChange(bv->text), i.e
1094 // the outermost LyXText!
1095 bv->beforeChange(this);
1096 string const clip = bv->getClipboard();
1097 if (!clip.empty()) {
1098 if (cmd.argument == "paragraph")
1099 insertStringAsParagraphs(clip);
1101 insertStringAsLines(clip);
1108 case LFUN_GOTOERROR:
1109 gotoInset(InsetOld::ERROR_CODE, false);
1113 gotoInset(InsetOld::NOTE_CODE, false);
1116 case LFUN_REFERENCE_GOTO:
1118 vector<InsetOld::Code> tmp;
1119 tmp.push_back(InsetOld::LABEL_CODE);
1120 tmp.push_back(InsetOld::REF_CODE);
1121 gotoInset(tmp, true);
1126 replaceSelection(bv->getLyXText());
1127 ParagraphList::iterator pit = cursor.par();
1128 lyx::pos_type pos = cursor.pos();
1132 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1135 c = pit->getChar(pos - 1);
1137 LyXLayout_ptr const & style = pit->layout();
1139 if (style->pass_thru ||
1140 pit->getFontSettings(bv->buffer()->params,
1141 pos).language()->lang() == "hebrew" ||
1142 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1143 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1147 case LFUN_DATE_INSERT: {
1148 replaceSelection(bv->getLyXText());
1149 time_t now_time_t = time(NULL);
1150 struct tm * now_tm = localtime(&now_time_t);
1151 setlocale(LC_TIME, "");
1153 if (!cmd.argument.empty())
1156 arg = lyxrc.date_insert_format;
1158 int const datetmp_len =
1159 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1161 for (int i = 0; i < datetmp_len; i++)
1162 insertChar(datetmp[i]);
1164 selection.cursor = cursor;
1165 moveCursorUpdate(bv, false);
1169 case LFUN_MOUSE_TRIPLE:
1172 if (!isInInset() && bv->theLockingInset())
1174 if (cmd.button() == mouse_button::button1) {
1176 selection.cursor = cursor;
1180 bv->haveSelection(selection.set());
1184 case LFUN_MOUSE_DOUBLE:
1187 if (!isInInset() && bv->theLockingInset())
1189 if (cmd.button() == mouse_button::button1) {
1190 selectWord(lyx::WHOLE_WORD_STRICT);
1192 bv->haveSelection(selection.set());
1196 case LFUN_MOUSE_MOTION:
1198 // Only use motion with button 1
1199 //if (ev.button() != mouse_button::button1)
1205 // Check for inset locking
1206 if (bv->theLockingInset()) {
1207 InsetOld * tli = bv->theLockingInset();
1208 LyXCursor cursor = bv->text->cursor;
1209 LyXFont font = bv->text->getFont(cursor.par(), cursor.pos());
1210 int width = tli->width();
1211 int inset_x = font.isVisibleRightToLeft()
1212 ? cursor.ix() - width : cursor.ix();
1213 int start_x = inset_x + tli->scroll();
1214 FuncRequest cmd1 = cmd;
1215 cmd1.x = cmd.x - start_x;
1216 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1217 tli->localDispatch(cmd1);
1221 // The test for not selection possible is needed, that only motion
1222 // events are used, where the bottom press event was on
1223 // the drawing area too
1224 if (!selection_possible) {
1225 lyxerr[Debug::ACTION]
1226 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1230 RowList::iterator cursorrow = bv->text->cursorRow();
1231 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->text->top_y());
1233 // sorry for this but I have a strange error that the y value jumps at
1234 // a certain point. This seems like an error in my xforms library or
1235 // in some other local environment, but I would like to leave this here
1236 // for the moment until I can remove this (Jug 20020418)
1237 if (y_before < bv->text->cursor.y())
1238 lyxerr << y_before << ':'
1239 << bv->text->cursor.y() << endl;
1241 // This is to allow jumping over large insets
1242 if (cursorrow == bv->text->cursorRow()) {
1243 if (cmd.y >= bv->workHeight())
1244 bv->text->cursorDown(false);
1246 bv->text->cursorUp(false);
1249 bv->text->setSelection();
1255 // Single-click on work area
1256 case LFUN_MOUSE_PRESS:
1261 // ok ok, this is a hack (for xforms)
1262 // We shouldn't go further down as we really should only do the
1263 // scrolling and be done with this. Otherwise we may open some
1264 // dialogs (Jug 20020424).
1265 if (cmd.button() == mouse_button::button4) {
1266 bv->scroll(-lyxrc.wheel_jump);
1269 if (cmd.button() == mouse_button::button5) {
1270 bv->scroll(lyxrc.wheel_jump);
1276 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1278 // Middle button press pastes if we have a selection
1279 // We do this here as if the selection was inside an inset
1280 // it could get cleared on the unlocking of the inset so
1281 // we have to check this first
1282 bool paste_internally = false;
1283 if (cmd.button() == mouse_button::button2 && selection.set()) {
1284 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1285 paste_internally = true;
1288 int const screen_first = bv->text->top_y();
1290 if (bv->theLockingInset()) {
1291 // We are in inset locking mode
1293 // Check whether the inset was hit. If not reset mode,
1294 // otherwise give the event to the inset
1295 if (inset_hit == bv->theLockingInset()) {
1296 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1297 bv->theLockingInset()->localDispatch(cmd1);
1300 bv->unlockInset(bv->theLockingInset());
1304 selection_possible = true;
1306 // Clear the selection
1307 bv->text->clearSelection();
1309 bv->updateScrollbar();
1311 // Single left click in math inset?
1312 if (isHighlyEditableInset(inset_hit)) {
1313 // Highly editable inset, like math
1314 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1315 selection_possible = false;
1316 bv->owner()->message(inset->editMessage());
1317 // We just have to lock the inset before calling a PressEvent on it!
1318 if (!bv->lockInset(inset))
1319 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1320 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1321 inset->localDispatch(cmd1);
1324 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1326 // Right click on a footnote flag opens float menu
1327 if (cmd.button() == mouse_button::button3) {
1328 selection_possible = false;
1332 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1333 bv->text->setCursorFromCoordinates(x, y + screen_first);
1335 bv->text->selection.cursor = bv->text->cursor;
1336 bv->text->cursor.x_fix(bv->text->cursor.x());
1338 if (bv->fitCursor())
1339 selection_possible = false;
1341 // Insert primary selection with middle mouse
1342 // if there is a local selection in the current buffer,
1344 if (cmd.button() == mouse_button::button2) {
1345 if (paste_internally)
1346 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1348 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1349 selection_possible = false;
1354 case LFUN_MOUSE_RELEASE:
1356 // do nothing if we used the mouse wheel
1360 if (cmd.button() == mouse_button::button4
1361 || cmd.button() == mouse_button::button5)
1364 // If we hit an inset, we have the inset coordinates in these
1365 // and inset_hit points to the inset. If we do not hit an
1366 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1369 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1371 if (bv->theLockingInset()) {
1372 // We are in inset locking mode.
1374 // LyX does a kind of work-area grabbing for insets.
1375 // Only a ButtonPress FuncRequest outside the inset will
1376 // force a insetUnlock.
1377 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1378 bv->theLockingInset()->localDispatch(cmd1);
1382 selection_possible = false;
1384 if (cmd.button() == mouse_button::button2)
1388 if (cmd.button() == mouse_button::button1)
1389 bv->haveSelection(selection.set());
1392 bv->owner()->view_state_changed();
1393 bv->owner()->updateMenubar();
1394 bv->owner()->updateToolbar();
1396 // Did we hit an editable inset?
1398 selection_possible = false;
1400 // if we reach this point with a selection, it
1401 // must mean we are currently selecting.
1402 // But we don't want to open the inset
1403 // because that is annoying for the user.
1404 // So just pretend we didn't hit it.
1405 // this is OK because a "kosher" ButtonRelease
1406 // will follow a ButtonPress that clears
1408 // Note this also fixes selection drawing
1409 // problems if we end up opening an inset
1410 if (selection.set())
1413 // CHECK fix this proper in 0.13
1414 // well, maybe 13.0 !!!!!!!!!
1416 // Following a ref shouldn't issue
1417 // a push on the undo-stack
1418 // anylonger, now that we have
1419 // keybindings for following
1420 // references and returning from
1421 // references. IMHO though, it
1422 // should be the inset's own business
1423 // to push or not push on the undo
1424 // stack. They don't *have* to
1425 // alter the document...
1427 // ...or maybe the SetCursorParUndo()
1428 // below isn't necessary at all anylonger?
1429 if (inset_hit->lyxCode() == InsetOld::REF_CODE)
1430 recordUndo(bv, Undo::ATOMIC);
1432 bv->owner()->message(inset_hit->editMessage());
1434 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1435 inset_hit->localDispatch(cmd1);
1441 case LFUN_SELFINSERT: {
1442 if (cmd.argument.empty())
1445 // Automatically delete the currently selected
1446 // text and replace it with what is being
1447 // typed in now. Depends on lyxrc settings
1448 // "auto_region_delete", which defaults to
1451 if (lyxrc.auto_region_delete) {
1452 if (selection.set())
1453 cutSelection(false, false);
1454 bv->haveSelection(false);
1457 bv->beforeChange(this);
1458 LyXFont const old_font(real_current_font);
1460 string::const_iterator cit = cmd.argument.begin();
1461 string::const_iterator end = cmd.argument.end();
1462 for (; cit != end; ++cit)
1463 bv->owner()->getIntl().getTransManager().
1464 TranslateAndInsert(*cit, this);
1466 selection.cursor = cursor;
1467 moveCursorUpdate(bv, false);
1469 // real_current_font.number can change so we need to
1470 // update the minibuffer
1471 if (old_font != real_current_font)
1472 bv->owner()->view_state_changed();
1476 case LFUN_HTMLURL: {
1477 InsetCommandParams p("htmlurl");
1478 string const data = InsetCommandMailer::params2string("url", p);
1479 bv->owner()->getDialogs().show("url", data, 0);
1484 InsetCommandParams p("url");
1485 string const data = InsetCommandMailer::params2string("url", p);
1486 bv->owner()->getDialogs().show("url", data, 0);
1492 case LFUN_INSET_LIST:
1493 case LFUN_INSET_THEOREM:
1494 case LFUN_INSET_CAPTION:
1496 case LFUN_INSERT_NOTE:
1497 case LFUN_INSERT_BIBITEM:
1498 case LFUN_INSET_ERT:
1499 case LFUN_INSET_FLOAT:
1500 case LFUN_INSET_FOOTNOTE:
1501 case LFUN_INSET_MARGINAL:
1502 case LFUN_INSET_MINIPAGE:
1503 case LFUN_INSET_OPTARG:
1504 case LFUN_INSET_WIDE_FLOAT:
1505 case LFUN_INSET_WRAP:
1506 case LFUN_TABULAR_INSERT:
1507 case LFUN_ENVIRONMENT_INSERT:
1508 // Open the inset, and move the current selection
1510 doInsertInset(this, cmd, true, true);
1513 case LFUN_INDEX_INSERT:
1514 // Just open the inset
1515 doInsertInset(this, cmd, true, false);
1518 case LFUN_INDEX_PRINT:
1519 case LFUN_TOC_INSERT:
1522 doInsertInset(this, cmd, false, false);
1526 return UNDISPATCHED;