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 == rows().begin()) {
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 if (rit == beginRow(pit))
280 rit = previousRow(rit);
281 setCursor(cur, pit, rit->pos(), false);
282 if (cur.y() > top_y())
284 bv()->updateScrollbar();
288 void LyXText::cursorNext()
292 RowList::iterator rit = cursorRow();
293 if (rit == lastRow()) {
294 int y = cursor.y() - rit->baseline() + cursorRow()->height();
295 if (y > topy + bv()->workHeight())
296 bv()->updateScrollbar();
300 int y = topy + bv()->workHeight();
301 if (inset_owner && !topy) {
302 y -= (bv()->text->cursor.iy()
303 - bv()->text->top_y()
304 + bv()->theLockingInset()->insetInInsetY());
309 setCursorFromCoordinates(cursor.x_fix(), y);
310 // + bv->workHeight());
314 if (rit == bv()->text->cursorRow()) {
315 // we have a row which is taller than the workarea. The
316 // simplest solution is to move to the next row instead.
319 // This is what we used to do, so we wouldn't skip right past
320 // tall rows, but it's not working right now.
322 new_y = bv->text->top_y() + bv->workHeight();
326 new_y = bv()->text->cursor.iy()
327 + bv()->theLockingInset()->insetInInsetY()
328 + y - rit->baseline();
330 new_y = cursor.y() - cursorRow()->baseline();
334 ParagraphList::iterator pit = cursor.par();
335 if (boost::next(rit) == endRow(pit))
339 setCursor(cur, pit, rit->pos(), false);
340 if (cur.y() < top_y() + bv()->workHeight())
342 bv()->updateScrollbar();
348 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
351 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
352 replaceSelection(lt);
353 if (!bv->insertInset(new_inset))
360 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
361 bool edit, bool pastesel)
363 InsetOld * inset = createInset(cmd);
364 BufferView * bv = cmd.view();
368 if (lt->selection.set()) {
369 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
372 if (bv->insertInset(inset)) {
374 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
375 inset->localDispatch(cmd);
377 if (gotsel && pastesel)
378 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
387 InsetOld::RESULT LyXText::dispatch(FuncRequest const & cmd)
389 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
390 <<"] arg[" << cmd.argument << ']' << endl;
392 BufferView * bv = cmd.view();
394 switch (cmd.action) {
396 case LFUN_APPENDIX: {
397 ParagraphList::iterator pit = cursor.par();
398 bool start = !pit->params().startOfAppendix();
400 // ensure that we have only one start_of_appendix in this document
401 ParagraphList::iterator tmp = ownerParagraphs().begin();
402 ParagraphList::iterator end = ownerParagraphs().end();
404 for (; tmp != end; ++tmp) {
405 if (tmp->params().startOfAppendix()) {
406 recordUndo(bv, Undo::ATOMIC, tmp);
407 tmp->params().startOfAppendix(false);
408 setHeightOfRow(tmp, getRow(tmp, 0));
413 recordUndo(bv, Undo::ATOMIC, pit);
414 pit->params().startOfAppendix(start);
416 // we can set the refreshing parameters now
418 redoParagraph(cursor.par());
419 setCursor(cursor.par(), cursor.pos());
424 case LFUN_DELETE_WORD_FORWARD:
425 bv->beforeChange(this);
430 case LFUN_DELETE_WORD_BACKWARD:
431 bv->beforeChange(this);
432 deleteWordBackward();
436 case LFUN_DELETE_LINE_FORWARD:
437 bv->beforeChange(this);
443 if (!selection.mark())
444 bv->beforeChange(this);
445 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
448 cursorRightOneWord();
453 if (!selection.mark())
454 bv->beforeChange(this);
455 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
456 cursorRightOneWord();
462 case LFUN_BEGINNINGBUF:
463 if (!selection.mark())
464 bv->beforeChange(this);
470 if (selection.mark())
471 bv->beforeChange(this);
477 if (!selection.set())
478 selection.cursor = cursor;
479 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
483 finishChange(bv, true);
487 if (!selection.set())
488 selection.cursor = cursor;
489 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
493 finishChange(bv, true);
497 if (!selection.set())
498 selection.cursor = cursor;
500 finishChange(bv, true);
504 if (!selection.set())
505 selection.cursor = cursor;
507 finishChange(bv, true);
510 case LFUN_UP_PARAGRAPHSEL:
511 if (!selection.set())
512 selection.cursor = cursor;
514 finishChange(bv, true);
517 case LFUN_DOWN_PARAGRAPHSEL:
518 if (!selection.set())
519 selection.cursor = cursor;
520 cursorDownParagraph();
521 finishChange(bv, true);
525 if (!selection.set())
526 selection.cursor = cursor;
528 finishChange(bv, true);
532 if (!selection.set())
533 selection.cursor = cursor;
535 finishChange(bv, true);
539 if (!selection.set())
540 selection.cursor = cursor;
542 finishChange(bv, true);
546 if (!selection.set())
547 selection.cursor = cursor;
549 finishChange(bv, true);
552 case LFUN_WORDRIGHTSEL:
553 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
556 cursorRightOneWord();
557 finishChange(bv, true);
560 case LFUN_WORDLEFTSEL:
561 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
562 cursorRightOneWord();
565 finishChange(bv, true);
569 LyXCursor cur1 = cursor;
571 ::getWord(cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs());
572 setCursor(cur1.par(), cur1.pos());
573 bv->beforeChange(this);
574 setCursor(cur2.par(), cur2.pos());
575 finishChange(bv, true);
580 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
581 if (!selection.mark())
582 bv->beforeChange(this);
585 if (cursor.pos() < cursor.par()->size()
586 && cursor.par()->isInset(cursor.pos())
587 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
588 InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
589 cmd.message(tmpinset->editMessage());
590 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
591 tmpinset->localDispatch(cmd1);
601 // This is soooo ugly. Isn`t it possible to make
603 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
604 if (!selection.mark())
605 bv->beforeChange(this);
606 LyXCursor const cur = cursor;
609 if ((is_rtl || cur != cursor) && // only if really moved!
610 cursor.pos() < cursor.par()->size() &&
611 cursor.par()->isInset(cursor.pos()) &&
612 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
613 InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
614 cmd.message(tmpinset->editMessage());
615 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
616 tmpinset->localDispatch(cmd1);
626 if (!selection.mark())
627 bv->beforeChange(this);
633 if (!selection.mark())
634 bv->beforeChange(this);
639 case LFUN_UP_PARAGRAPH:
640 if (!selection.mark())
641 bv->beforeChange(this);
646 case LFUN_DOWN_PARAGRAPH:
647 if (!selection.mark())
648 bv->beforeChange(this);
649 cursorDownParagraph();
650 finishChange(bv, false);
654 if (!selection.mark())
655 bv->beforeChange(this);
657 finishChange(bv, false);
661 if (!selection.mark())
662 bv->beforeChange(this);
664 finishChange(bv, false);
668 if (!selection.mark())
669 bv->beforeChange(this);
671 finishChange(bv, false);
675 if (!selection.mark())
676 bv->beforeChange(this);
678 finishChange(bv, false);
681 case LFUN_BREAKLINE: {
682 lyx::pos_type body = cursor.par()->beginningOfBody();
684 // Not allowed by LaTeX (labels or empty par)
685 if (cursor.pos() <= body)
688 replaceSelection(bv->getLyXText());
689 insertInset(new InsetNewline);
690 setCursor(cursor.par(), cursor.pos());
691 moveCursorUpdate(bv, false);
696 if (!selection.set()) {
698 selection.cursor = cursor;
699 // It is possible to make it a lot faster still
700 // just comment out the line below...
702 cutSelection(true, false);
704 moveCursorUpdate(bv, false);
705 bv->owner()->view_state_changed();
708 case LFUN_DELETE_SKIP:
709 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
710 if (!selection.set()) {
711 LyXCursor cur = cursor;
712 if (cur.pos() == cur.par()->size()) {
716 && !(cur.par()->params().spaceTop()
717 == VSpace (VSpace::NONE))) {
719 cur.par()->params().lineTop(),
720 cur.par()->params().lineBottom(),
721 cur.par()->params().pagebreakTop(),
722 cur.par()->params().pagebreakBottom(),
723 VSpace(VSpace::NONE),
724 cur.par()->params().spaceBottom(),
725 cur.par()->params().spacing(),
726 cur.par()->params().align(),
727 cur.par()->params().labelWidthString(), 0);
732 selection.cursor = cursor;
736 selection.cursor = cursor;
739 cutSelection(true, false);
746 if (!selection.set()) {
747 if (bv->owner()->getIntl().getTransManager().backspace()) {
749 selection.cursor = cursor;
750 // It is possible to make it a lot faster still
751 // just comment out the line below...
754 cutSelection(true, false);
756 bv->owner()->view_state_changed();
761 case LFUN_BACKSPACE_SKIP:
762 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
763 if (!selection.set()) {
764 LyXCursor cur = cursor;
766 && !(cur.par()->params().spaceTop() == VSpace(VSpace::NONE))) {
768 cur.par()->params().lineTop(),
769 cur.par()->params().lineBottom(),
770 cur.par()->params().pagebreakTop(),
771 cur.par()->params().pagebreakBottom(),
772 VSpace(VSpace::NONE),
773 cur.par()->params().spaceBottom(),
774 cur.par()->params().spacing(),
775 cur.par()->params().align(),
776 cur.par()->params().labelWidthString(), 0);
779 selection.cursor = cur;
782 cutSelection(true, false);
787 case LFUN_BREAKPARAGRAPH:
788 replaceSelection(bv->getLyXText());
789 breakParagraph(bv->buffer()->paragraphs, 0);
791 selection.cursor = cursor;
793 bv->owner()->view_state_changed();
796 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
797 replaceSelection(bv->getLyXText());
798 breakParagraph(bv->buffer()->paragraphs, 1);
800 selection.cursor = cursor;
802 bv->owner()->view_state_changed();
805 case LFUN_BREAKPARAGRAPH_SKIP: {
806 // When at the beginning of a paragraph, remove
807 // indentation and add a "defskip" at the top.
808 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
809 LyXCursor cur = cursor;
810 replaceSelection(bv->getLyXText());
811 if (cur.pos() == 0) {
812 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
814 cur.par()->params().lineTop(),
815 cur.par()->params().lineBottom(),
816 cur.par()->params().pagebreakTop(),
817 cur.par()->params().pagebreakBottom(),
818 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
819 cur.par()->params().spacing(),
820 cur.par()->params().align(),
821 cur.par()->params().labelWidthString(), 1);
825 breakParagraph(bv->buffer()->paragraphs, 0);
828 selection.cursor = cur;
830 bv->owner()->view_state_changed();
834 case LFUN_PARAGRAPH_SPACING: {
835 ParagraphList::iterator pit = cursor.par();
836 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
837 float cur_value = 1.0;
838 if (cur_spacing == Spacing::Other)
839 cur_value = pit->params().spacing().getValue();
841 istringstream is(STRCONV(cmd.argument));
844 Spacing::Space new_spacing = cur_spacing;
845 float new_value = cur_value;
847 lyxerr << "Missing argument to `paragraph-spacing'"
849 } else if (tmp == "single") {
850 new_spacing = Spacing::Single;
851 } else if (tmp == "onehalf") {
852 new_spacing = Spacing::Onehalf;
853 } else if (tmp == "double") {
854 new_spacing = Spacing::Double;
855 } else if (tmp == "other") {
856 new_spacing = Spacing::Other;
859 lyxerr << "new_value = " << tmpval << endl;
862 } else if (tmp == "default") {
863 new_spacing = Spacing::Default;
865 lyxerr << _("Unknown spacing argument: ")
866 << cmd.argument << endl;
868 if (cur_spacing != new_spacing || cur_value != new_value) {
869 pit->params().spacing(Spacing(new_spacing, new_value));
876 case LFUN_INSET_SETTINGS:
877 Assert(bv->theLockingInset());
878 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
881 case LFUN_INSET_TOGGLE:
882 bv->beforeChange(this);
888 case LFUN_SPACE_INSERT:
889 if (cursor.par()->layout()->free_spacing)
892 doInsertInset(this, cmd, false, false);
893 moveCursorUpdate(bv, false);
896 case LFUN_HYPHENATION:
897 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
900 case LFUN_LIGATURE_BREAK:
901 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
905 specialChar(this, bv, InsetSpecialChar::LDOTS);
908 case LFUN_END_OF_SENTENCE:
909 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
912 case LFUN_MENU_SEPARATOR:
913 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
917 bv->beforeChange(this);
919 selection.cursor = cursor;
920 cmd.message(N_("Mark off"));
924 bv->beforeChange(this);
925 selection.mark(true);
927 selection.cursor = cursor;
928 cmd.message(N_("Mark on"));
932 bv->beforeChange(this);
933 if (selection.mark()) {
934 cmd.message(N_("Mark removed"));
936 selection.mark(true);
937 cmd.message(N_("Mark set"));
939 selection.cursor = cursor;
943 case LFUN_UPCASE_WORD:
944 changeCase(LyXText::text_uppercase);
948 case LFUN_LOWCASE_WORD:
949 changeCase(LyXText::text_lowercase);
953 case LFUN_CAPITALIZE_WORD:
954 changeCase(LyXText::text_capitalization);
958 case LFUN_TRANSPOSE_CHARS:
959 recordUndo(bv, Undo::ATOMIC, cursor.par());
965 cmd.message(_("Paste"));
966 replaceSelection(bv->getLyXText());
967 size_t sel_index = 0;
968 string const & arg = cmd.argument;
969 if (isStrUnsignedInt(arg)) {
970 size_t const paste_arg = strToUnsignedInt(arg);
971 #warning FIXME Check if the arg is in the domain of available selections.
972 sel_index = paste_arg;
974 pasteSelection(sel_index);
975 clearSelection(); // bug 393
982 cutSelection(true, true);
983 cmd.message(_("Cut"));
989 cmd.message(_("Copy"));
992 case LFUN_BEGINNINGBUFSEL:
996 finishChange(bv, true);
1001 return UNDISPATCHED;
1003 finishChange(bv, true);
1007 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1013 istringstream is(STRCONV(cmd.argument));
1016 lyxerr << "SETXY: Could not parse coordinates in '"
1017 << cmd.argument << std::endl;
1019 setCursorFromCoordinates(x, y);
1024 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1026 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1032 case LFUN_GETLAYOUT:
1033 cmd.message(tostr(cursor.par()->layout()));
1037 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1038 << cmd.argument << endl;
1040 // This is not the good solution to the empty argument
1041 // problem, but it will hopefully suffice for 1.2.0.
1042 // The correct solution would be to augument the
1043 // function list/array with information about what
1044 // functions needs arguments and their type.
1045 if (cmd.argument.empty()) {
1046 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1050 // Derive layout number from given argument (string)
1051 // and current buffer's textclass (number)
1052 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1053 bool hasLayout = tclass.hasLayout(cmd.argument);
1054 string layout = cmd.argument;
1056 // If the entry is obsolete, use the new one instead.
1058 string const & obs = tclass[layout]->obsoleted_by();
1064 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1069 bool change_layout = (current_layout != layout);
1071 if (!change_layout && selection.set() &&
1072 selection.start.par() != selection.end.par())
1074 ParagraphList::iterator spit = selection.start.par();
1075 ParagraphList::iterator epit = boost::next(selection.end.par());
1076 while (spit != epit) {
1077 if (spit->layout()->name() != current_layout) {
1078 change_layout = true;
1085 if (change_layout) {
1086 current_layout = layout;
1088 bv->owner()->setLayout(layout);
1095 case LFUN_PASTESELECTION: {
1096 // this was originally a beforeChange(bv->text), i.e
1097 // the outermost LyXText!
1098 bv->beforeChange(this);
1099 string const clip = bv->getClipboard();
1100 if (!clip.empty()) {
1101 if (cmd.argument == "paragraph")
1102 insertStringAsParagraphs(clip);
1104 insertStringAsLines(clip);
1111 case LFUN_GOTOERROR:
1112 gotoInset(InsetOld::ERROR_CODE, false);
1116 gotoInset(InsetOld::NOTE_CODE, false);
1119 case LFUN_REFERENCE_GOTO:
1121 vector<InsetOld::Code> tmp;
1122 tmp.push_back(InsetOld::LABEL_CODE);
1123 tmp.push_back(InsetOld::REF_CODE);
1124 gotoInset(tmp, true);
1129 replaceSelection(bv->getLyXText());
1130 ParagraphList::iterator pit = cursor.par();
1131 lyx::pos_type pos = cursor.pos();
1135 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1138 c = pit->getChar(pos - 1);
1140 LyXLayout_ptr const & style = pit->layout();
1142 if (style->pass_thru ||
1143 pit->getFontSettings(bv->buffer()->params,
1144 pos).language()->lang() == "hebrew" ||
1145 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1146 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1150 case LFUN_DATE_INSERT: {
1151 replaceSelection(bv->getLyXText());
1152 time_t now_time_t = time(NULL);
1153 struct tm * now_tm = localtime(&now_time_t);
1154 setlocale(LC_TIME, "");
1156 if (!cmd.argument.empty())
1159 arg = lyxrc.date_insert_format;
1161 int const datetmp_len =
1162 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1164 for (int i = 0; i < datetmp_len; i++)
1165 insertChar(datetmp[i]);
1167 selection.cursor = cursor;
1168 moveCursorUpdate(bv, false);
1172 case LFUN_MOUSE_TRIPLE:
1175 if (!isInInset() && bv->theLockingInset())
1177 if (cmd.button() == mouse_button::button1) {
1179 bv->screen().toggleSelection(this, bv);
1181 selection.cursor = cursor;
1185 bv->screen().toggleSelection(this, bv, false);
1187 bv->haveSelection(selection.set());
1191 case LFUN_MOUSE_DOUBLE:
1194 if (!isInInset() && bv->theLockingInset())
1196 if (cmd.button() == mouse_button::button1) {
1198 bv->screen().toggleSelection(this, bv);
1199 selectWord(lyx::WHOLE_WORD_STRICT);
1200 bv->screen().toggleSelection(this, bv, false);
1202 selectWord(lyx::WHOLE_WORD_STRICT);
1205 bv->haveSelection(selection.set());
1209 case LFUN_MOUSE_MOTION:
1211 // Only use motion with button 1
1212 //if (ev.button() != mouse_button::button1)
1218 // Check for inset locking
1219 if (bv->theLockingInset()) {
1220 InsetOld * tli = bv->theLockingInset();
1221 LyXCursor cursor = bv->text->cursor;
1222 LyXFont font = bv->text->getFont(cursor.par(), cursor.pos());
1223 int width = tli->width();
1224 int inset_x = font.isVisibleRightToLeft()
1225 ? cursor.ix() - width : cursor.ix();
1226 int start_x = inset_x + tli->scroll();
1227 FuncRequest cmd1 = cmd;
1228 cmd1.x = cmd.x - start_x;
1229 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1230 tli->localDispatch(cmd1);
1234 // The test for not selection possible is needed, that only motion
1235 // events are used, where the bottom press event was on
1236 // the drawing area too
1237 if (!selection_possible) {
1238 lyxerr[Debug::ACTION]
1239 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1243 RowList::iterator cursorrow = bv->text->cursorRow();
1244 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->text->top_y());
1246 // sorry for this but I have a strange error that the y value jumps at
1247 // a certain point. This seems like an error in my xforms library or
1248 // in some other local environment, but I would like to leave this here
1249 // for the moment until I can remove this (Jug 20020418)
1250 if (y_before < bv->text->cursor.y())
1251 lyxerr << y_before << ':'
1252 << bv->text->cursor.y() << endl;
1254 // This is to allow jumping over large insets
1255 if (cursorrow == bv->text->cursorRow()) {
1256 if (cmd.y >= bv->workHeight())
1257 bv->text->cursorDown(false);
1259 bv->text->cursorUp(false);
1262 bv->text->setSelection();
1268 // Single-click on work area
1269 case LFUN_MOUSE_PRESS:
1274 // ok ok, this is a hack (for xforms)
1275 // We shouldn't go further down as we really should only do the
1276 // scrolling and be done with this. Otherwise we may open some
1277 // dialogs (Jug 20020424).
1278 if (cmd.button() == mouse_button::button4) {
1279 bv->scroll(-lyxrc.wheel_jump);
1282 if (cmd.button() == mouse_button::button5) {
1283 bv->scroll(lyxrc.wheel_jump);
1289 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1291 // Middle button press pastes if we have a selection
1292 // We do this here as if the selection was inside an inset
1293 // it could get cleared on the unlocking of the inset so
1294 // we have to check this first
1295 bool paste_internally = false;
1296 if (cmd.button() == mouse_button::button2 && selection.set()) {
1297 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1298 paste_internally = true;
1301 int const screen_first = bv->text->top_y();
1303 if (bv->theLockingInset()) {
1304 // We are in inset locking mode
1306 // Check whether the inset was hit. If not reset mode,
1307 // otherwise give the event to the inset
1308 if (inset_hit == bv->theLockingInset()) {
1309 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1310 bv->theLockingInset()->localDispatch(cmd1);
1313 bv->unlockInset(bv->theLockingInset());
1317 selection_possible = true;
1319 // Clear the selection
1320 bv->screen().toggleSelection(bv->text, bv);
1321 bv->text->clearSelection();
1323 bv->updateScrollbar();
1325 // Single left click in math inset?
1326 if (isHighlyEditableInset(inset_hit)) {
1327 // Highly editable inset, like math
1328 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1329 selection_possible = false;
1330 bv->owner()->message(inset->editMessage());
1331 // We just have to lock the inset before calling a PressEvent on it!
1332 if (!bv->lockInset(inset))
1333 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1334 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1335 inset->localDispatch(cmd1);
1338 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1340 // Right click on a footnote flag opens float menu
1341 if (cmd.button() == mouse_button::button3) {
1342 selection_possible = false;
1346 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1347 bv->text->setCursorFromCoordinates(x, y + screen_first);
1349 bv->text->selection.cursor = bv->text->cursor;
1350 bv->text->cursor.x_fix(bv->text->cursor.x());
1352 if (bv->fitCursor())
1353 selection_possible = false;
1355 // Insert primary selection with middle mouse
1356 // if there is a local selection in the current buffer,
1358 if (cmd.button() == mouse_button::button2) {
1359 if (paste_internally)
1360 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1362 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1363 selection_possible = false;
1368 case LFUN_MOUSE_RELEASE:
1370 // do nothing if we used the mouse wheel
1374 if (cmd.button() == mouse_button::button4
1375 || cmd.button() == mouse_button::button5)
1378 // If we hit an inset, we have the inset coordinates in these
1379 // and inset_hit points to the inset. If we do not hit an
1380 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1383 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1385 if (bv->theLockingInset()) {
1386 // We are in inset locking mode.
1388 // LyX does a kind of work-area grabbing for insets.
1389 // Only a ButtonPress FuncRequest outside the inset will
1390 // force a insetUnlock.
1391 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1392 bv->theLockingInset()->localDispatch(cmd1);
1396 selection_possible = false;
1398 if (cmd.button() == mouse_button::button2)
1402 if (cmd.button() == mouse_button::button1)
1403 bv->haveSelection(selection.set());
1406 bv->owner()->view_state_changed();
1407 bv->owner()->updateMenubar();
1408 bv->owner()->updateToolbar();
1410 // Did we hit an editable inset?
1412 selection_possible = false;
1414 // if we reach this point with a selection, it
1415 // must mean we are currently selecting.
1416 // But we don't want to open the inset
1417 // because that is annoying for the user.
1418 // So just pretend we didn't hit it.
1419 // this is OK because a "kosher" ButtonRelease
1420 // will follow a ButtonPress that clears
1422 // Note this also fixes selection drawing
1423 // problems if we end up opening an inset
1424 if (selection.set())
1427 // CHECK fix this proper in 0.13
1428 // well, maybe 13.0 !!!!!!!!!
1430 // Following a ref shouldn't issue
1431 // a push on the undo-stack
1432 // anylonger, now that we have
1433 // keybindings for following
1434 // references and returning from
1435 // references. IMHO though, it
1436 // should be the inset's own business
1437 // to push or not push on the undo
1438 // stack. They don't *have* to
1439 // alter the document...
1441 // ...or maybe the SetCursorParUndo()
1442 // below isn't necessary at all anylonger?
1443 if (inset_hit->lyxCode() == InsetOld::REF_CODE)
1444 recordUndo(bv, Undo::ATOMIC);
1446 bv->owner()->message(inset_hit->editMessage());
1448 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1449 inset_hit->localDispatch(cmd1);
1455 case LFUN_SELFINSERT: {
1456 if (cmd.argument.empty())
1459 // Automatically delete the currently selected
1460 // text and replace it with what is being
1461 // typed in now. Depends on lyxrc settings
1462 // "auto_region_delete", which defaults to
1465 if (lyxrc.auto_region_delete) {
1466 if (selection.set())
1467 cutSelection(false, false);
1468 bv->haveSelection(false);
1471 bv->beforeChange(this);
1472 LyXFont const old_font(real_current_font);
1474 string::const_iterator cit = cmd.argument.begin();
1475 string::const_iterator end = cmd.argument.end();
1476 for (; cit != end; ++cit)
1477 bv->owner()->getIntl().getTransManager().
1478 TranslateAndInsert(*cit, this);
1480 selection.cursor = cursor;
1481 moveCursorUpdate(bv, false);
1483 // real_current_font.number can change so we need to
1484 // update the minibuffer
1485 if (old_font != real_current_font)
1486 bv->owner()->view_state_changed();
1490 case LFUN_HTMLURL: {
1491 InsetCommandParams p("htmlurl");
1492 string const data = InsetCommandMailer::params2string("url", p);
1493 bv->owner()->getDialogs().show("url", data, 0);
1498 InsetCommandParams p("url");
1499 string const data = InsetCommandMailer::params2string("url", p);
1500 bv->owner()->getDialogs().show("url", data, 0);
1506 case LFUN_INSET_LIST:
1507 case LFUN_INSET_THEOREM:
1508 case LFUN_INSET_CAPTION:
1510 case LFUN_INSERT_NOTE:
1511 case LFUN_INSERT_BIBITEM:
1512 case LFUN_INSET_ERT:
1513 case LFUN_INSET_FLOAT:
1514 case LFUN_INSET_FOOTNOTE:
1515 case LFUN_INSET_MARGINAL:
1516 case LFUN_INSET_MINIPAGE:
1517 case LFUN_INSET_OPTARG:
1518 case LFUN_INSET_WIDE_FLOAT:
1519 case LFUN_INSET_WRAP:
1520 case LFUN_TABULAR_INSERT:
1521 case LFUN_ENVIRONMENT_INSERT:
1522 // Open the inset, and move the current selection
1524 doInsertInset(this, cmd, true, true);
1527 case LFUN_INDEX_INSERT:
1528 // Just open the inset
1529 doInsertInset(this, cmd, true, false);
1532 case LFUN_INDEX_PRINT:
1533 case LFUN_TOC_INSERT:
1536 doInsertInset(this, cmd, false, false);
1540 return UNDISPATCHED;