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 * ====================================================== */
15 #include "paragraph.h"
16 #include "BufferView.h"
17 #include "funcrequest.h"
21 #include "bufferparams.h"
23 #include "bufferview_funcs.h"
24 #include "ParagraphParameters.h"
30 #include "support/tostr.h"
31 #include "support/lstrings.h"
32 #include "support/LAssert.h"
33 #include "frontends/LyXView.h"
34 #include "frontends/screen.h"
35 #include "frontends/Dialogs.h"
36 #include "insets/insetspecialchar.h"
37 #include "insets/insettext.h"
38 #include "insets/insetquotes.h"
39 #include "insets/insetcommand.h"
40 #include "insets/insetnewline.h"
41 #include "undo_funcs.h"
42 #include "text_funcs.h"
48 using namespace lyx::support;
49 using namespace bv_funcs;
56 extern string current_layout;
57 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
59 // the selection possible is needed, that only motion events are
60 // used, where the bottom press event was on the drawing area too
61 bool selection_possible = false;
66 void moveCursorUpdate(BufferView * bv, bool selecting)
68 LyXText * lt = bv->getLyXText();
70 //if (!lt->selection.set())
71 // lt->selection.cursor = lt->cursor;
73 if (selecting || lt->selection.mark())
76 if (!lt->selection.set())
77 bv->haveSelection(false);
84 void finishChange(BufferView * bv, bool selecting = false)
87 moveCursorUpdate(bv, selecting);
88 bv->owner()->view_state_changed();
91 // check if the given co-ordinates are inside an inset at the
92 // given cursor, if one exists. If so, the inset is returned,
93 // and the co-ordinates are made relative. Otherwise, 0 is returned.
94 InsetOld * checkInset(BufferView * /*bv*/, LyXText & text,
95 LyXCursor const & cur, int & x, int & y)
97 lyx::pos_type const pos = cur.pos();
98 ParagraphList::iterator par = cur.par();
100 if (pos >= par->size() || !par->isInset(pos))
103 InsetOld /*const*/ * inset = par->getInset(pos);
105 if (!isEditableInset(inset))
108 // get inset dimensions
109 Assert(par->getInset(pos));
111 LyXFont const & font = text.getFont(par, pos);
113 int const width = inset->width();
114 int const inset_x = font.isVisibleRightToLeft()
115 ? (cur.ix() - width) : cur.ix();
118 inset_x + inset->scroll(),
120 cur.iy() - inset->ascent(),
121 cur.iy() + inset->descent()
124 if (!b.contained(x, y)) {
125 lyxerr[Debug::GUI] << "Missed inset at x,y "
127 << " box " << b << endl;
131 text.setCursor(par, pos, true);
134 // The origin of an inset is on the baseline
135 y -= text.cursor.iy();
143 InsetOld * LyXText::checkInsetHit(int & x, int & y)
145 int y_tmp = y + top_y();
148 setCursorFromCoordinates(cur, x, y_tmp);
150 InsetOld * inset = checkInset(bv(), *this, cur, x, y_tmp);
156 // look at previous position
161 setCursor(cur, cur.par(), cur.pos() - 1, true);
163 inset = checkInset(bv(), *this, cur, x, y_tmp);
170 bool LyXText::gotoNextInset(vector<InsetOld::Code> const & codes,
171 string const & contents)
173 ParagraphList::iterator end = ownerParagraphs().end();
174 ParagraphList::iterator pit = cursor.par();
175 pos_type pos = cursor.pos();
179 if (pos + 1 < pit->size()) {
186 } while (pit != end &&
187 !(pit->isInset(pos) &&
188 (inset = pit->getInset(pos)) != 0 &&
189 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
191 static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
195 setCursor(pit, pos, false);
202 void LyXText::gotoInset(vector<InsetOld::Code> const & codes,
205 bv()->beforeChange(this);
208 if (same_content && cursor.pos() < cursor.par()->size()
209 && cursor.par()->isInset(cursor.pos())) {
210 InsetOld const * inset = cursor.par()->getInset(cursor.pos());
211 if (find(codes.begin(), codes.end(), inset->lyxCode())
213 contents = static_cast<InsetCommand const *>(inset)->getContents();
216 if (!gotoNextInset(codes, contents)) {
217 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
218 LyXCursor tmp = cursor;
219 cursor.par(ownerParagraphs().begin());
221 if (!gotoNextInset(codes, contents)) {
223 bv()->owner()->message(_("No more insets"));
226 bv()->owner()->message(_("No more insets"));
230 selection.cursor = cursor;
234 void LyXText::gotoInset(InsetOld::Code code, bool same_content)
236 gotoInset(vector<InsetOld::Code>(1, code), same_content);
240 void LyXText::cursorPrevious()
244 if (cursorRow() == rows().begin()) {
246 bv()->updateScrollbar();
250 RowList::iterator cursorrow = cursorRow();
252 setCursorFromCoordinates(cursor.x_fix(), y);
256 if (cursorrow == bv()->text->cursorRow()) {
257 // we have a row which is taller than the workarea. The
258 // simplest solution is to move to the previous row instead.
261 // This is what we used to do, so we wouldn't skip right past
262 // tall rows, but it's not working right now.
265 new_y = bv()->text->cursor.iy()
266 + bv()->theLockingInset()->insetInInsetY() + y
267 + cursorRow()->height()
268 - bv()->workHeight() + 1;
271 - cursorRow()->baseline()
272 + cursorRow()->height()
273 - bv()->workHeight() + 1;
276 //bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
277 if (cursorRow() != rows().begin()) {
279 setCursor(cur, boost::prior(cursorRow())->par(),
280 boost::prior(cursorRow())->pos(), false);
281 if (cur.y() > top_y()) {
285 bv()->updateScrollbar();
289 void LyXText::cursorNext()
293 if (boost::next(cursorRow()) == rows().end()) {
294 int y = cursor.y() - cursorRow()->baseline() +
295 cursorRow()->height();
296 if (y > topy + bv()->workHeight()) {
297 //bv()->screen().draw(bv()->text, bv(), bv()->text->top_y() + bv()->workHeight());
298 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());
312 RowList::iterator cursorrow = cursorRow();
313 setCursorFromCoordinates(cursor.x_fix(), y);
314 // + bv->workHeight());
318 if (cursorrow == 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 - cursorRow()->baseline();
334 new_y = cursor.y() - cursorRow()->baseline();
337 //bv()->screen().draw(bv()->text, bv(), new_y);
339 RowList::iterator next_row = boost::next(cursorRow());
340 if (next_row != rows().end()) {
342 setCursor(cur, next_row->par(), next_row->pos(), false);
343 if (cur.y() < top_y() + bv()->workHeight()) {
347 bv()->updateScrollbar();
351 void LyXText::update()
358 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
361 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
362 replaceSelection(lt);
363 if (!bv->insertInset(new_inset))
370 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
371 bool edit, bool pastesel)
373 InsetOld * inset = createInset(cmd);
374 BufferView * bv = cmd.view();
378 if (lt->selection.set()) {
379 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
382 if (bv->insertInset(inset)) {
384 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
385 inset->localDispatch(cmd);
387 if (gotsel && pastesel)
388 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
397 InsetOld::RESULT LyXText::dispatch(FuncRequest const & cmd)
399 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
400 <<"] arg[" << cmd.argument << ']' << endl;
402 BufferView * bv = cmd.view();
404 switch (cmd.action) {
406 case LFUN_APPENDIX: {
407 ParagraphList::iterator pit = cursor.par();
408 bool start = !pit->params().startOfAppendix();
410 // ensure that we have only one start_of_appendix in this document
411 ParagraphList::iterator tmp = ownerParagraphs().begin();
412 ParagraphList::iterator end = ownerParagraphs().end();
414 for (; tmp != end; ++tmp) {
415 if (tmp->params().startOfAppendix()) {
416 recordUndo(bv, Undo::ATOMIC, tmp);
417 tmp->params().startOfAppendix(false);
419 setHeightOfRow(getRow(tmp, 0, tmpy));
424 recordUndo(bv, Undo::ATOMIC, pit);
425 pit->params().startOfAppendix(start);
427 // we can set the refreshing parameters now
429 redoHeightOfParagraph();
430 setCursor(cursor.par(), cursor.pos());
435 case LFUN_DELETE_WORD_FORWARD:
436 bv->beforeChange(this);
442 case LFUN_DELETE_WORD_BACKWARD:
443 bv->beforeChange(this);
444 deleteWordBackward();
449 case LFUN_DELETE_LINE_FORWARD:
450 bv->beforeChange(this);
457 if (!selection.mark())
458 bv->beforeChange(this);
459 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
462 cursorRightOneWord();
468 if (!selection.mark())
469 bv->beforeChange(this);
470 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
471 cursorRightOneWord();
477 case LFUN_BEGINNINGBUF:
478 if (!selection.mark())
479 bv->beforeChange(this);
485 if (selection.mark())
486 bv->beforeChange(this);
492 if (!selection.set())
493 selection.cursor = cursor;
494 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
498 finishChange(bv, true);
502 if (!selection.set())
503 selection.cursor = cursor;
504 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
508 finishChange(bv, true);
512 if (!selection.set())
513 selection.cursor = cursor;
515 finishChange(bv, true);
519 if (!selection.set())
520 selection.cursor = cursor;
522 finishChange(bv, true);
525 case LFUN_UP_PARAGRAPHSEL:
526 if (!selection.set())
527 selection.cursor = cursor;
529 finishChange(bv, true);
532 case LFUN_DOWN_PARAGRAPHSEL:
533 if (!selection.set())
534 selection.cursor = cursor;
535 cursorDownParagraph();
536 finishChange(bv, true);
540 if (!selection.set())
541 selection.cursor = cursor;
543 finishChange(bv, true);
547 if (!selection.set())
548 selection.cursor = cursor;
550 finishChange(bv, true);
554 if (!selection.set())
555 selection.cursor = cursor;
557 finishChange(bv, true);
561 if (!selection.set())
562 selection.cursor = cursor;
564 finishChange(bv, true);
567 case LFUN_WORDRIGHTSEL:
568 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
571 cursorRightOneWord();
572 finishChange(bv, true);
575 case LFUN_WORDLEFTSEL:
576 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
577 cursorRightOneWord();
580 finishChange(bv, true);
584 LyXCursor cur1 = cursor;
586 ::getWord(cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs());
587 setCursor(cur1.par(), cur1.pos());
588 bv->beforeChange(this);
589 setCursor(cur2.par(), cur2.pos());
590 finishChange(bv, true);
595 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
596 if (!selection.mark())
597 bv->beforeChange(this);
600 if (cursor.pos() < cursor.par()->size()
601 && cursor.par()->isInset(cursor.pos())
602 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
603 InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
604 cmd.message(tmpinset->editMessage());
605 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
606 tmpinset->localDispatch(cmd1);
616 // This is soooo ugly. Isn`t it possible to make
618 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
619 if (!selection.mark())
620 bv->beforeChange(this);
621 LyXCursor const cur = cursor;
624 if ((is_rtl || cur != cursor) && // only if really moved!
625 cursor.pos() < cursor.par()->size() &&
626 cursor.par()->isInset(cursor.pos()) &&
627 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
628 InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
629 cmd.message(tmpinset->editMessage());
630 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
631 tmpinset->localDispatch(cmd1);
641 if (!selection.mark())
642 bv->beforeChange(this);
648 if (!selection.mark())
649 bv->beforeChange(this);
654 case LFUN_UP_PARAGRAPH:
655 if (!selection.mark())
656 bv->beforeChange(this);
661 case LFUN_DOWN_PARAGRAPH:
662 if (!selection.mark())
663 bv->beforeChange(this);
664 cursorDownParagraph();
665 finishChange(bv, false);
669 if (!selection.mark())
670 bv->beforeChange(this);
672 finishChange(bv, false);
676 if (!selection.mark())
677 bv->beforeChange(this);
679 finishChange(bv, false);
683 if (!selection.mark())
684 bv->beforeChange(this);
686 finishChange(bv, false);
690 if (!selection.mark())
691 bv->beforeChange(this);
693 finishChange(bv, false);
696 case LFUN_BREAKLINE: {
697 lyx::pos_type body = cursor.par()->beginningOfBody();
699 // Not allowed by LaTeX (labels or empty par)
700 if (cursor.pos() <= body)
703 replaceSelection(bv->getLyXText());
704 insertInset(new InsetNewline);
705 setCursor(cursor.par(), cursor.pos());
706 moveCursorUpdate(bv, false);
711 if (!selection.set()) {
713 selection.cursor = cursor;
714 // It is possible to make it a lot faster still
715 // just comment out the line below...
717 cutSelection(true, false);
719 moveCursorUpdate(bv, false);
720 bv->owner()->view_state_changed();
724 case LFUN_DELETE_SKIP:
725 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
726 if (!selection.set()) {
727 LyXCursor cur = cursor;
728 if (cur.pos() == cur.par()->size()) {
732 && !(cur.par()->params().spaceTop()
733 == VSpace (VSpace::NONE))) {
735 cur.par()->params().lineTop(),
736 cur.par()->params().lineBottom(),
737 cur.par()->params().pagebreakTop(),
738 cur.par()->params().pagebreakBottom(),
739 VSpace(VSpace::NONE),
740 cur.par()->params().spaceBottom(),
741 cur.par()->params().spacing(),
742 cur.par()->params().align(),
743 cur.par()->params().labelWidthString(), 0);
748 selection.cursor = cursor;
752 selection.cursor = cursor;
755 cutSelection(true, false);
762 if (!selection.set()) {
763 if (bv->owner()->getIntl().getTransManager().backspace()) {
765 selection.cursor = cursor;
766 // It is possible to make it a lot faster still
767 // just comment out the line below...
770 cutSelection(true, false);
772 bv->owner()->view_state_changed();
777 case LFUN_BACKSPACE_SKIP:
778 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
779 if (!selection.set()) {
780 LyXCursor cur = cursor;
782 && !(cur.par()->params().spaceTop() == VSpace(VSpace::NONE))) {
784 cur.par()->params().lineTop(),
785 cur.par()->params().lineBottom(),
786 cur.par()->params().pagebreakTop(),
787 cur.par()->params().pagebreakBottom(),
788 VSpace(VSpace::NONE),
789 cur.par()->params().spaceBottom(),
790 cur.par()->params().spacing(),
791 cur.par()->params().align(),
792 cur.par()->params().labelWidthString(), 0);
795 selection.cursor = cur;
799 cutSelection(true, false);
804 case LFUN_BREAKPARAGRAPH:
805 replaceSelection(bv->getLyXText());
806 breakParagraph(bv->buffer()->paragraphs, 0);
808 selection.cursor = cursor;
810 bv->owner()->view_state_changed();
813 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
814 replaceSelection(bv->getLyXText());
815 breakParagraph(bv->buffer()->paragraphs, 1);
817 selection.cursor = cursor;
819 bv->owner()->view_state_changed();
822 case LFUN_BREAKPARAGRAPH_SKIP: {
823 // When at the beginning of a paragraph, remove
824 // indentation and add a "defskip" at the top.
825 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
826 LyXCursor cur = cursor;
827 replaceSelection(bv->getLyXText());
828 if (cur.pos() == 0) {
829 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
831 cur.par()->params().lineTop(),
832 cur.par()->params().lineBottom(),
833 cur.par()->params().pagebreakTop(),
834 cur.par()->params().pagebreakBottom(),
835 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
836 cur.par()->params().spacing(),
837 cur.par()->params().align(),
838 cur.par()->params().labelWidthString(), 1);
842 breakParagraph(bv->buffer()->paragraphs, 0);
845 selection.cursor = cur;
847 bv->owner()->view_state_changed();
851 case LFUN_PARAGRAPH_SPACING: {
852 ParagraphList::iterator pit = cursor.par();
853 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
854 float cur_value = 1.0;
855 if (cur_spacing == Spacing::Other)
856 cur_value = pit->params().spacing().getValue();
858 istringstream is(STRCONV(cmd.argument));
861 Spacing::Space new_spacing = cur_spacing;
862 float new_value = cur_value;
864 lyxerr << "Missing argument to `paragraph-spacing'"
866 } else if (tmp == "single") {
867 new_spacing = Spacing::Single;
868 } else if (tmp == "onehalf") {
869 new_spacing = Spacing::Onehalf;
870 } else if (tmp == "double") {
871 new_spacing = Spacing::Double;
872 } else if (tmp == "other") {
873 new_spacing = Spacing::Other;
876 lyxerr << "new_value = " << tmpval << endl;
879 } else if (tmp == "default") {
880 new_spacing = Spacing::Default;
882 lyxerr << _("Unknown spacing argument: ")
883 << cmd.argument << endl;
885 if (cur_spacing != new_spacing || cur_value != new_value) {
886 pit->params().spacing(Spacing(new_spacing, new_value));
893 case LFUN_INSET_SETTINGS:
894 Assert(bv->theLockingInset());
895 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
898 case LFUN_INSET_TOGGLE:
899 bv->beforeChange(this);
906 case LFUN_SPACE_INSERT:
907 if (cursor.par()->layout()->free_spacing) {
911 doInsertInset(this, cmd, false, false);
913 moveCursorUpdate(bv, false);
916 case LFUN_HYPHENATION:
917 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
920 case LFUN_LIGATURE_BREAK:
921 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
925 specialChar(this, bv, InsetSpecialChar::LDOTS);
928 case LFUN_END_OF_SENTENCE:
929 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
932 case LFUN_MENU_SEPARATOR:
933 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
937 bv->beforeChange(this);
939 selection.cursor = cursor;
940 cmd.message(N_("Mark off"));
944 bv->beforeChange(this);
945 selection.mark(true);
947 selection.cursor = cursor;
948 cmd.message(N_("Mark on"));
952 bv->beforeChange(this);
953 if (selection.mark()) {
954 cmd.message(N_("Mark removed"));
956 selection.mark(true);
957 cmd.message(N_("Mark set"));
959 selection.cursor = cursor;
963 case LFUN_UPCASE_WORD:
965 changeCase(LyXText::text_uppercase);
971 case LFUN_LOWCASE_WORD:
972 changeCase(LyXText::text_lowercase);
978 case LFUN_CAPITALIZE_WORD:
979 changeCase(LyXText::text_capitalization);
985 case LFUN_TRANSPOSE_CHARS:
986 recordUndo(bv, Undo::ATOMIC, cursor.par());
987 if (transposeChars(cursor))
988 checkParagraph(cursor.par(), cursor.pos());
995 cmd.message(_("Paste"));
996 replaceSelection(bv->getLyXText());
997 size_t sel_index = 0;
998 string const & arg = cmd.argument;
999 if (isStrUnsignedInt(arg)) {
1000 size_t const paste_arg = strToUnsignedInt(arg);
1001 #warning FIXME Check if the arg is in the domain of available selections.
1002 sel_index = paste_arg;
1004 pasteSelection(sel_index);
1005 clearSelection(); // bug 393
1012 cutSelection(true, true);
1013 cmd.message(_("Cut"));
1019 cmd.message(_("Copy"));
1022 case LFUN_BEGINNINGBUFSEL:
1024 return UNDISPATCHED;
1026 finishChange(bv, true);
1030 case LFUN_ENDBUFSEL:
1032 return UNDISPATCHED;
1034 finishChange(bv, true);
1039 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1045 istringstream is(STRCONV(cmd.argument));
1048 lyxerr << "SETXY: Could not parse coordinates in '"
1049 << cmd.argument << std::endl;
1051 setCursorFromCoordinates(x, y);
1056 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1058 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1064 case LFUN_GETLAYOUT:
1065 cmd.message(tostr(cursor.par()->layout()));
1069 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1070 << cmd.argument << endl;
1072 // This is not the good solution to the empty argument
1073 // problem, but it will hopefully suffice for 1.2.0.
1074 // The correct solution would be to augument the
1075 // function list/array with information about what
1076 // functions needs arguments and their type.
1077 if (cmd.argument.empty()) {
1078 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1082 // Derive layout number from given argument (string)
1083 // and current buffer's textclass (number)
1084 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1085 bool hasLayout = tclass.hasLayout(cmd.argument);
1086 string layout = cmd.argument;
1088 // If the entry is obsolete, use the new one instead.
1090 string const & obs = tclass[layout]->obsoleted_by();
1096 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1101 bool change_layout = (current_layout != layout);
1103 if (!change_layout && selection.set() &&
1104 selection.start.par() != selection.end.par())
1106 ParagraphList::iterator spit = selection.start.par();
1107 ParagraphList::iterator epit = boost::next(selection.end.par());
1108 while (spit != epit) {
1109 if (spit->layout()->name() != current_layout) {
1110 change_layout = true;
1117 if (change_layout) {
1118 current_layout = layout;
1120 bv->owner()->setLayout(layout);
1127 case LFUN_PASTESELECTION: {
1128 // this was originally a beforeChange(bv->text), i.e
1129 // the outermost LyXText!
1130 bv->beforeChange(this);
1131 string const clip = bv->getClipboard();
1132 if (!clip.empty()) {
1133 if (cmd.argument == "paragraph")
1134 insertStringAsParagraphs(clip);
1136 insertStringAsLines(clip);
1143 case LFUN_GOTOERROR:
1144 gotoInset(InsetOld::ERROR_CODE, false);
1148 gotoInset(InsetOld::NOTE_CODE, false);
1151 case LFUN_REFERENCE_GOTO:
1153 vector<InsetOld::Code> tmp;
1154 tmp.push_back(InsetOld::LABEL_CODE);
1155 tmp.push_back(InsetOld::REF_CODE);
1156 gotoInset(tmp, true);
1161 replaceSelection(bv->getLyXText());
1162 ParagraphList::iterator pit = cursor.par();
1163 lyx::pos_type pos = cursor.pos();
1167 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1170 c = pit->getChar(pos - 1);
1172 LyXLayout_ptr const & style = pit->layout();
1174 if (style->pass_thru ||
1175 pit->getFontSettings(bv->buffer()->params,
1176 pos).language()->lang() == "hebrew" ||
1177 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1178 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1182 case LFUN_DATE_INSERT: {
1183 replaceSelection(bv->getLyXText());
1184 time_t now_time_t = time(NULL);
1185 struct tm * now_tm = localtime(&now_time_t);
1186 setlocale(LC_TIME, "");
1188 if (!cmd.argument.empty())
1191 arg = lyxrc.date_insert_format;
1193 int const datetmp_len =
1194 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1196 for (int i = 0; i < datetmp_len; i++) {
1197 insertChar(datetmp[i]);
1200 selection.cursor = cursor;
1201 moveCursorUpdate(bv, false);
1205 case LFUN_MOUSE_TRIPLE:
1208 if (!isInInset() && bv->theLockingInset())
1210 if (cmd.button() == mouse_button::button1) {
1212 bv->screen().toggleSelection(this, bv);
1214 selection.cursor = cursor;
1218 bv->screen().toggleSelection(this, bv, false);
1220 bv->haveSelection(selection.set());
1224 case LFUN_MOUSE_DOUBLE:
1227 if (!isInInset() && bv->theLockingInset())
1229 if (cmd.button() == mouse_button::button1) {
1231 bv->screen().toggleSelection(this, bv);
1232 selectWord(lyx::WHOLE_WORD_STRICT);
1233 bv->screen().toggleSelection(this, bv, false);
1235 selectWord(lyx::WHOLE_WORD_STRICT);
1238 bv->haveSelection(selection.set());
1242 case LFUN_MOUSE_MOTION:
1244 // Only use motion with button 1
1245 //if (ev.button() != mouse_button::button1)
1251 // Check for inset locking
1252 if (bv->theLockingInset()) {
1253 InsetOld * tli = bv->theLockingInset();
1254 LyXCursor cursor = bv->text->cursor;
1255 LyXFont font = bv->text->getFont(cursor.par(), cursor.pos());
1256 int width = tli->width();
1257 int inset_x = font.isVisibleRightToLeft()
1258 ? cursor.ix() - width : cursor.ix();
1259 int start_x = inset_x + tli->scroll();
1260 FuncRequest cmd1 = cmd;
1261 cmd1.x = cmd.x - start_x;
1262 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1263 tli->localDispatch(cmd1);
1267 // The test for not selection possible is needed, that only motion
1268 // events are used, where the bottom press event was on
1269 // the drawing area too
1270 if (!selection_possible) {
1271 lyxerr[Debug::ACTION]
1272 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1276 RowList::iterator cursorrow = bv->text->cursorRow();
1277 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->text->top_y());
1279 // sorry for this but I have a strange error that the y value jumps at
1280 // a certain point. This seems like an error in my xforms library or
1281 // in some other local environment, but I would like to leave this here
1282 // for the moment until I can remove this (Jug 20020418)
1283 if (y_before < bv->text->cursor.y())
1284 lyxerr << y_before << ':'
1285 << bv->text->cursor.y() << endl;
1287 // This is to allow jumping over large insets
1288 if (cursorrow == bv->text->cursorRow()) {
1289 if (cmd.y >= bv->workHeight())
1290 bv->text->cursorDown(false);
1292 bv->text->cursorUp(false);
1295 bv->text->setSelection();
1301 // Single-click on work area
1302 case LFUN_MOUSE_PRESS:
1307 // ok ok, this is a hack (for xforms)
1308 // We shouldn't go further down as we really should only do the
1309 // scrolling and be done with this. Otherwise we may open some
1310 // dialogs (Jug 20020424).
1311 if (cmd.button() == mouse_button::button4) {
1312 bv->scroll(-lyxrc.wheel_jump);
1315 if (cmd.button() == mouse_button::button5) {
1316 bv->scroll(lyxrc.wheel_jump);
1322 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1324 // Middle button press pastes if we have a selection
1325 // We do this here as if the selection was inside an inset
1326 // it could get cleared on the unlocking of the inset so
1327 // we have to check this first
1328 bool paste_internally = false;
1329 if (cmd.button() == mouse_button::button2 && selection.set()) {
1330 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1331 paste_internally = true;
1334 int const screen_first = bv->text->top_y();
1336 if (bv->theLockingInset()) {
1337 // We are in inset locking mode
1339 // Check whether the inset was hit. If not reset mode,
1340 // otherwise give the event to the inset
1341 if (inset_hit == bv->theLockingInset()) {
1342 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1343 bv->theLockingInset()->localDispatch(cmd1);
1346 bv->unlockInset(bv->theLockingInset());
1350 selection_possible = true;
1352 // Clear the selection
1353 bv->screen().toggleSelection(bv->text, bv);
1354 bv->text->clearSelection();
1356 bv->updateScrollbar();
1358 // Single left click in math inset?
1359 if (isHighlyEditableInset(inset_hit)) {
1360 // Highly editable inset, like math
1361 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1362 selection_possible = false;
1363 bv->owner()->message(inset->editMessage());
1364 // We just have to lock the inset before calling a PressEvent on it!
1365 if (!bv->lockInset(inset))
1366 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1367 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1368 inset->localDispatch(cmd1);
1371 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1373 // Right click on a footnote flag opens float menu
1374 if (cmd.button() == mouse_button::button3) {
1375 selection_possible = false;
1379 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1380 bv->text->setCursorFromCoordinates(x, y + screen_first);
1382 bv->text->selection.cursor = bv->text->cursor;
1383 bv->text->cursor.x_fix(bv->text->cursor.x());
1385 if (bv->fitCursor())
1386 selection_possible = false;
1388 // Insert primary selection with middle mouse
1389 // if there is a local selection in the current buffer,
1391 if (cmd.button() == mouse_button::button2) {
1392 if (paste_internally)
1393 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1395 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1396 selection_possible = false;
1401 case LFUN_MOUSE_RELEASE:
1403 // do nothing if we used the mouse wheel
1407 if (cmd.button() == mouse_button::button4
1408 || cmd.button() == mouse_button::button5)
1411 // If we hit an inset, we have the inset coordinates in these
1412 // and inset_hit points to the inset. If we do not hit an
1413 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1416 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1418 if (bv->theLockingInset()) {
1419 // We are in inset locking mode.
1421 // LyX does a kind of work-area grabbing for insets.
1422 // Only a ButtonPress FuncRequest outside the inset will
1423 // force a insetUnlock.
1424 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1425 bv->theLockingInset()->localDispatch(cmd1);
1429 selection_possible = false;
1431 if (cmd.button() == mouse_button::button2)
1435 if (cmd.button() == mouse_button::button1)
1436 bv->haveSelection(selection.set());
1439 bv->owner()->view_state_changed();
1440 bv->owner()->updateMenubar();
1441 bv->owner()->updateToolbar();
1443 // Did we hit an editable inset?
1445 selection_possible = false;
1447 // if we reach this point with a selection, it
1448 // must mean we are currently selecting.
1449 // But we don't want to open the inset
1450 // because that is annoying for the user.
1451 // So just pretend we didn't hit it.
1452 // this is OK because a "kosher" ButtonRelease
1453 // will follow a ButtonPress that clears
1455 // Note this also fixes selection drawing
1456 // problems if we end up opening an inset
1457 if (selection.set())
1460 // CHECK fix this proper in 0.13
1461 // well, maybe 13.0 !!!!!!!!!
1463 // Following a ref shouldn't issue
1464 // a push on the undo-stack
1465 // anylonger, now that we have
1466 // keybindings for following
1467 // references and returning from
1468 // references. IMHO though, it
1469 // should be the inset's own business
1470 // to push or not push on the undo
1471 // stack. They don't *have* to
1472 // alter the document...
1474 // ...or maybe the SetCursorParUndo()
1475 // below isn't necessary at all anylonger?
1476 if (inset_hit->lyxCode() == InsetOld::REF_CODE)
1477 recordUndo(bv, Undo::ATOMIC);
1479 bv->owner()->message(inset_hit->editMessage());
1481 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1482 inset_hit->localDispatch(cmd1);
1488 case LFUN_SELFINSERT: {
1489 if (cmd.argument.empty())
1492 // Automatically delete the currently selected
1493 // text and replace it with what is being
1494 // typed in now. Depends on lyxrc settings
1495 // "auto_region_delete", which defaults to
1498 if (lyxrc.auto_region_delete) {
1499 if (selection.set())
1500 cutSelection(false, false);
1501 bv->haveSelection(false);
1504 bv->beforeChange(this);
1505 LyXFont const old_font(real_current_font);
1507 string::const_iterator cit = cmd.argument.begin();
1508 string::const_iterator end = cmd.argument.end();
1509 for (; cit != end; ++cit)
1510 bv->owner()->getIntl().getTransManager().
1511 TranslateAndInsert(*cit, this);
1514 selection.cursor = cursor;
1515 moveCursorUpdate(bv, false);
1517 // real_current_font.number can change so we need to
1518 // update the minibuffer
1519 if (old_font != real_current_font)
1520 bv->owner()->view_state_changed();
1524 case LFUN_HTMLURL: {
1525 InsetCommandParams p("htmlurl");
1526 string const data = InsetCommandMailer::params2string("url", p);
1527 bv->owner()->getDialogs().show("url", data, 0);
1532 InsetCommandParams p("url");
1533 string const data = InsetCommandMailer::params2string("url", p);
1534 bv->owner()->getDialogs().show("url", data, 0);
1540 case LFUN_INSET_LIST:
1541 case LFUN_INSET_THEOREM:
1542 case LFUN_INSET_CAPTION:
1544 case LFUN_INSERT_NOTE:
1545 case LFUN_INSERT_BIBITEM:
1546 case LFUN_INSET_ERT:
1547 case LFUN_INSET_FLOAT:
1548 case LFUN_INSET_FOOTNOTE:
1549 case LFUN_INSET_MARGINAL:
1550 case LFUN_INSET_MINIPAGE:
1551 case LFUN_INSET_OPTARG:
1552 case LFUN_INSET_WIDE_FLOAT:
1553 case LFUN_INSET_WRAP:
1554 case LFUN_TABULAR_INSERT:
1555 case LFUN_ENVIRONMENT_INSERT:
1556 // Open the inset, and move the current selection
1558 doInsertInset(this, cmd, true, true);
1561 case LFUN_INDEX_INSERT:
1562 // Just open the inset
1563 doInsertInset(this, cmd, true, false);
1566 case LFUN_INDEX_PRINT:
1567 case LFUN_TOC_INSERT:
1570 doInsertInset(this, cmd, false, false);
1574 return UNDISPATCHED;