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 "ParagraphParameters.h"
29 #include "support/tostr.h"
30 #include "support/lstrings.h"
31 #include "frontends/LyXView.h"
32 #include "frontends/screen.h"
33 #include "frontends/Dialogs.h"
34 #include "insets/insetspecialchar.h"
35 #include "insets/insettext.h"
36 #include "insets/insetquotes.h"
37 #include "insets/insetcommand.h"
38 #include "insets/insetnewline.h"
39 #include "undo_funcs.h"
40 #include "text_funcs.h"
51 extern string current_layout;
52 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
54 // the selection possible is needed, that only motion events are
55 // used, where the bottom press event was on the drawing area too
56 bool selection_possible = false;
61 void moveCursorUpdate(BufferView * bv, bool selecting)
63 LyXText * lt = bv->getLyXText();
65 if (selecting || lt->selection.mark()) {
68 bv->updateInset(lt->inset_owner);
72 if (!lt->isInInset()) {
73 bv->update(lt, BufferView::SELECT);
74 } else if (bv->text->refreshStatus() != LyXText::REFRESH_NONE) {
75 bv->update(BufferView::SELECT);
78 if (!lt->selection.set())
79 bv->haveSelection(false);
85 void finishChange(BufferView * bv, bool selecting = false)
88 moveCursorUpdate(bv, selecting);
89 bv->owner()->view_state_changed();
92 // check if the given co-ordinates are inside an inset at the
93 // given cursor, if one exists. If so, the inset is returned,
94 // and the co-ordinates are made relative. Otherwise, 0 is returned.
95 Inset * checkInset(BufferView * bv, LyXText & text,
96 LyXCursor const & cur, int & x, int & y)
98 lyx::pos_type const pos = cur.pos();
99 ParagraphList::iterator par = cur.par();
101 if (pos >= par->size() || !par->isInset(pos))
104 Inset /*const*/ * inset = par->getInset(pos);
106 if (!isEditableInset(inset))
109 // get inset dimensions
110 lyx::Assert(par->getInset(pos));
112 LyXFont const & font = text.getFont(bv->buffer(), par, pos);
114 int const width = inset->width(bv, font);
115 int const inset_x = font.isVisibleRightToLeft()
116 ? (cur.ix() - width) : cur.ix();
119 inset_x + inset->scroll(),
121 cur.iy() - inset->ascent(bv, font),
122 cur.iy() + inset->descent(bv, font)
125 if (!b.contained(x, y)) {
126 lyxerr[Debug::GUI] << "Missed inset at x,y "
128 << " box " << b << endl;
132 text.setCursor(par, pos, true);
135 // The origin of an inset is on the baseline
136 y -= text.cursor.iy();
144 Inset * LyXText::checkInsetHit(int & x, int & y)
146 int y_tmp = y + top_y();
149 setCursorFromCoordinates(cur, x, y_tmp);
151 Inset * inset = checkInset(bv(), *this, cur, x, y_tmp);
157 // look at previous position
162 setCursor(cur, cur.par(), cur.pos() - 1, true);
164 inset = checkInset(bv(), *this, cur, x, y_tmp);
171 bool LyXText::gotoNextInset(vector<Inset::Code> const & codes,
172 string const & contents)
174 ParagraphList::iterator end = ownerParagraphs().end();
175 ParagraphList::iterator pit = cursor.par();
176 pos_type pos = cursor.pos();
180 if (pos + 1 < pit->size()) {
187 } while (pit != end &&
188 !(pit->isInset(pos) &&
189 (inset = pit->getInset(pos)) != 0 &&
190 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
192 static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
196 setCursor(pit, pos, false);
203 void LyXText::gotoInset(vector<Inset::Code> const & codes,
206 bv()->beforeChange(this);
210 if (same_content && cursor.pos() < cursor.par()->size()
211 && cursor.par()->isInset(cursor.pos())) {
212 Inset const * inset = cursor.par()->getInset(cursor.pos());
213 if (find(codes.begin(), codes.end(), inset->lyxCode())
215 contents = static_cast<InsetCommand const *>(inset)->getContents();
218 if (!gotoNextInset(codes, contents)) {
219 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
220 LyXCursor tmp = cursor;
221 cursor.par(ownerParagraphs().begin());
223 if (!gotoNextInset(codes, contents)) {
225 bv()->owner()->message(_("No more insets"));
228 bv()->owner()->message(_("No more insets"));
232 selection.cursor = cursor;
236 void LyXText::gotoInset(Inset::Code code, bool same_content)
238 gotoInset(vector<Inset::Code>(1, code), same_content);
242 void LyXText::cursorPrevious()
246 if (cursor.row() == rows().begin()) {
248 int new_y = bv()->text->top_y() - bv()->workHeight();
249 bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
250 bv()->updateScrollbar();
255 RowList::iterator cursorrow = cursor.row();
257 setCursorFromCoordinates(cursor.x_fix(), y);
261 if (cursorrow == bv()->text->cursor.row()) {
262 // we have a row which is taller than the workarea. The
263 // simplest solution is to move to the previous row instead.
266 // This is what we used to do, so we wouldn't skip right past
267 // tall rows, but it's not working right now.
269 new_y = bv->text->top_y() - bv->workHeight();
273 new_y = bv()->text->cursor.iy()
274 + bv()->theLockingInset()->insetInInsetY() + y
275 + cursor.row()->height()
276 - bv()->workHeight() + 1;
279 - cursor.row()->baseline()
280 + cursor.row()->height()
281 - bv()->workHeight() + 1;
284 bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
285 if (cursor.row() != rows().begin()) {
287 setCursor(cur, boost::prior(cursor.row())->par(),
288 boost::prior(cursor.row())->pos(), false);
289 if (cur.y() > top_y()) {
293 bv()->updateScrollbar();
297 void LyXText::cursorNext()
301 if (boost::next(cursor.row()) == rows().end()) {
302 int y = cursor.y() - cursor.row()->baseline() +
303 cursor.row()->height();
304 if (y > topy + bv()->workHeight()) {
305 bv()->screen().draw(bv()->text, bv(), bv()->text->top_y() + bv()->workHeight());
306 bv()->updateScrollbar();
311 int y = topy + bv()->workHeight();
312 if (inset_owner && !topy) {
313 y -= (bv()->text->cursor.iy()
314 - bv()->text->top_y()
315 + bv()->theLockingInset()->insetInInsetY());
320 RowList::iterator cursorrow = cursor.row();
321 setCursorFromCoordinates(cursor.x_fix(), y);
322 // + bv->workHeight());
326 if (cursorrow == bv()->text->cursor.row()) {
327 // we have a row which is taller than the workarea. The
328 // simplest solution is to move to the next row instead.
331 // This is what we used to do, so we wouldn't skip right past
332 // tall rows, but it's not working right now.
334 new_y = bv->text->top_y() + bv->workHeight();
338 new_y = bv()->text->cursor.iy()
339 + bv()->theLockingInset()->insetInInsetY()
340 + y - cursor.row()->baseline();
342 new_y = cursor.y() - cursor.row()->baseline();
345 bv()->screen().draw(bv()->text, bv(), new_y);
346 if (boost::next(cursor.row()) != rows().end()) {
348 setCursor(cur, boost::next(cursor.row())->par(),
349 boost::next(cursor.row())->pos(), false);
350 if (cur.y() < top_y() + bv()->workHeight()) {
354 bv()->updateScrollbar();
358 void LyXText::update()
360 bv()->update(this, BufferView::SELECT);
365 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
368 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
369 if (!bv->insertInset(new_inset))
372 bv->updateInset(new_inset);
376 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
377 bool edit, bool pastesel)
379 Inset * inset = createInset(cmd);
380 BufferView * bv = cmd.view();
384 if (lt->selection.set()) {
385 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
388 if (bv->insertInset(inset)) {
390 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
391 inset->localDispatch(cmd);
393 if (gotsel && pastesel)
394 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
403 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
405 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
406 <<"] arg[" << cmd.argument << ']' << endl;
408 BufferView * bv = cmd.view();
410 switch (cmd.action) {
412 case LFUN_APPENDIX: {
413 ParagraphList::iterator pit = cursor.par();
414 bool start = !pit->params().startOfAppendix();
416 // ensure that we have only one start_of_appendix in this document
417 ParagraphList::iterator tmp = ownerParagraphs().begin();
418 ParagraphList::iterator end = ownerParagraphs().end();
420 for (; tmp != end; ++tmp) {
421 if (tmp->params().startOfAppendix()) {
422 setUndo(bv, Undo::EDIT, tmp);
423 tmp->params().startOfAppendix(false);
425 setHeightOfRow(getRow(tmp, 0, tmpy));
430 setUndo(bv, Undo::EDIT, pit);
431 pit->params().startOfAppendix(start);
433 // we can set the refreshing parameters now
435 redoHeightOfParagraph();
437 setCursor(cursor.par(), cursor.pos());
442 case LFUN_DELETE_WORD_FORWARD:
443 bv->beforeChange(this);
450 case LFUN_DELETE_WORD_BACKWARD:
451 bv->beforeChange(this);
453 deleteWordBackward();
458 case LFUN_DELETE_LINE_FORWARD:
459 bv->beforeChange(this);
467 if (!selection.mark())
468 bv->beforeChange(this);
470 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
473 cursorRightOneWord();
478 if (!selection.mark())
479 bv->beforeChange(this);
481 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
482 cursorRightOneWord();
488 case LFUN_BEGINNINGBUF:
489 if (!selection.mark())
490 bv->beforeChange(this);
497 if (selection.mark())
498 bv->beforeChange(this);
506 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
510 finishChange(bv, true);
515 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
519 finishChange(bv, true);
525 finishChange(bv, true);
531 finishChange(bv, true);
534 case LFUN_UP_PARAGRAPHSEL:
537 finishChange(bv, true);
540 case LFUN_DOWN_PARAGRAPHSEL:
542 cursorDownParagraph();
543 finishChange(bv, true);
549 finishChange(bv, true);
555 finishChange(bv, true);
561 finishChange(bv, true);
567 finishChange(bv, true);
570 case LFUN_WORDRIGHTSEL:
572 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
575 cursorRightOneWord();
576 finishChange(bv, true);
579 case LFUN_WORDLEFTSEL:
581 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
582 cursorRightOneWord();
585 finishChange(bv, true);
592 getWord(cur1, cur2, WHOLE_WORD);
593 setCursor(cur1.par(), cur1.pos());
594 bv->beforeChange(this);
595 setCursor(cur2.par(), cur2.pos());
596 finishChange(bv, true);
601 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
602 if (!selection.mark())
603 bv->beforeChange(this);
607 if (cursor.pos() < cursor.par()->size()
608 && cursor.par()->isInset(cursor.pos())
609 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
610 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
611 cmd.message(tmpinset->editMessage());
612 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
613 tmpinset->localDispatch(cmd1);
623 // This is soooo ugly. Isn`t it possible to make
625 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
626 if (!selection.mark())
627 bv->beforeChange(this);
629 LyXCursor const cur = cursor;
632 if ((is_rtl || cur != cursor) && // only if really moved!
633 cursor.pos() < cursor.par()->size() &&
634 cursor.par()->isInset(cursor.pos()) &&
635 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
636 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
637 cmd.message(tmpinset->editMessage());
638 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
639 tmpinset->localDispatch(cmd1);
649 if (!selection.mark())
650 bv->beforeChange(this);
651 bv->update(this, BufferView::UPDATE);
657 if (!selection.mark())
658 bv->beforeChange(this);
659 bv->update(this, BufferView::UPDATE);
664 case LFUN_UP_PARAGRAPH:
665 if (!selection.mark())
666 bv->beforeChange(this);
667 bv->update(this, BufferView::UPDATE);
672 case LFUN_DOWN_PARAGRAPH:
673 if (!selection.mark())
674 bv->beforeChange(this);
675 bv->update(this, BufferView::UPDATE);
676 cursorDownParagraph();
677 finishChange(bv, false);
681 if (!selection.mark())
682 bv->beforeChange(this);
683 bv->update(this, BufferView::UPDATE);
685 finishChange(bv, false);
688 // moveCursorUpdate(bv, false, false);
689 // owner_->view_state_changed();
693 if (!selection.mark())
694 bv->beforeChange(this);
695 bv->update(this, BufferView::UPDATE);
697 finishChange(bv, false);
701 if (!selection.mark())
702 bv->beforeChange(this);
705 finishChange(bv, false);
709 if (!selection.mark())
710 bv->beforeChange(this);
713 finishChange(bv, false);
716 case LFUN_BREAKLINE: {
717 lyx::pos_type body = cursor.par()->beginningOfBody();
719 // Not allowed by LaTeX (labels or empty par)
720 if (cursor.pos() <= body)
723 bv->beforeChange(this);
724 insertInset(new InsetNewline);
726 setCursor(cursor.par(), cursor.pos());
727 moveCursorUpdate(bv, false);
732 if (!selection.set()) {
734 selection.cursor = cursor;
736 // It is possible to make it a lot faster still
737 // just comment out the line below...
740 cutSelection(true, false);
743 moveCursorUpdate(bv, false);
744 bv->owner()->view_state_changed();
748 case LFUN_DELETE_SKIP:
749 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
750 if (!selection.set()) {
751 LyXCursor cur = cursor;
752 if (cur.pos() == cur.par()->size()) {
756 && !(cur.par()->params().spaceTop()
757 == VSpace (VSpace::NONE))) {
759 cur.par()->params().lineTop(),
760 cur.par()->params().lineBottom(),
761 cur.par()->params().pagebreakTop(),
762 cur.par()->params().pagebreakBottom(),
763 VSpace(VSpace::NONE),
764 cur.par()->params().spaceBottom(),
765 cur.par()->params().spacing(),
766 cur.par()->params().align(),
767 cur.par()->params().labelWidthString(), 0);
773 selection.cursor = cursor;
777 selection.cursor = cursor;
781 cutSelection(true, false);
788 if (!selection.set()) {
789 if (bv->owner()->getIntl().getTransManager().backspace()) {
791 selection.cursor = cursor;
793 // It is possible to make it a lot faster still
794 // just comment out the line below...
798 cutSelection(true, false);
801 bv->owner()->view_state_changed();
805 case LFUN_BACKSPACE_SKIP:
806 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
807 if (!selection.set()) {
808 LyXCursor cur = cursor;
810 && !(cur.par()->params().spaceTop()
811 == VSpace (VSpace::NONE))) {
813 cur.par()->params().lineTop(),
814 cur.par()->params().lineBottom(),
815 cur.par()->params().pagebreakTop(),
816 cur.par()->params().pagebreakBottom(),
817 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
818 cur.par()->params().spacing(),
819 cur.par()->params().align(),
820 cur.par()->params().labelWidthString(), 0);
823 selection.cursor = cur;
827 cutSelection(true, false);
832 case LFUN_BREAKPARAGRAPH:
833 bv->beforeChange(this);
834 breakParagraph(bv->buffer()->paragraphs, 0);
836 selection.cursor = cursor;
838 bv->owner()->view_state_changed();
841 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
842 bv->beforeChange(this);
843 breakParagraph(bv->buffer()->paragraphs, 1);
845 selection.cursor = cursor;
847 bv->owner()->view_state_changed();
850 case LFUN_BREAKPARAGRAPH_SKIP: {
851 // When at the beginning of a paragraph, remove
852 // indentation and add a "defskip" at the top.
853 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
854 LyXCursor cur = cursor;
855 bv->beforeChange(this);
856 if (cur.pos() == 0) {
857 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
859 cur.par()->params().lineTop(),
860 cur.par()->params().lineBottom(),
861 cur.par()->params().pagebreakTop(),
862 cur.par()->params().pagebreakBottom(),
863 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
864 cur.par()->params().spacing(),
865 cur.par()->params().align(),
866 cur.par()->params().labelWidthString(), 1);
870 breakParagraph(bv->buffer()->paragraphs, 0);
873 selection.cursor = cur;
875 bv->owner()->view_state_changed();
879 case LFUN_PARAGRAPH_SPACING: {
880 ParagraphList::iterator pit = cursor.par();
881 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
882 float cur_value = 1.0;
883 if (cur_spacing == Spacing::Other)
884 cur_value = pit->params().spacing().getValue();
886 istringstream is(STRCONV(cmd.argument));
889 Spacing::Space new_spacing = cur_spacing;
890 float new_value = cur_value;
892 lyxerr << "Missing argument to `paragraph-spacing'"
894 } else if (tmp == "single") {
895 new_spacing = Spacing::Single;
896 } else if (tmp == "onehalf") {
897 new_spacing = Spacing::Onehalf;
898 } else if (tmp == "double") {
899 new_spacing = Spacing::Double;
900 } else if (tmp == "other") {
901 new_spacing = Spacing::Other;
904 lyxerr << "new_value = " << tmpval << endl;
907 } else if (tmp == "default") {
908 new_spacing = Spacing::Default;
910 lyxerr << _("Unknown spacing argument: ")
911 << cmd.argument << endl;
913 if (cur_spacing != new_spacing || cur_value != new_value) {
914 pit->params().spacing(Spacing(new_spacing, new_value));
921 case LFUN_INSET_SETTINGS:
922 lyx::Assert(bv->theLockingInset());
923 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
926 case LFUN_INSET_TOGGLE:
927 bv->beforeChange(this);
934 case LFUN_SPACE_INSERT:
935 if (cursor.par()->layout()->free_spacing) {
939 doInsertInset(this, cmd, false, false);
941 moveCursorUpdate(bv, false);
944 case LFUN_HYPHENATION:
945 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
948 case LFUN_LIGATURE_BREAK:
949 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
953 specialChar(this, bv, InsetSpecialChar::LDOTS);
956 case LFUN_END_OF_SENTENCE:
957 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
960 case LFUN_MENU_SEPARATOR:
961 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
965 bv->beforeChange(this);
967 selection.cursor = cursor;
968 cmd.message(N_("Mark off"));
972 bv->beforeChange(this);
973 selection.mark(true);
975 selection.cursor = cursor;
976 cmd.message(N_("Mark on"));
980 bv->beforeChange(this);
981 if (selection.mark()) {
983 cmd.message(N_("Mark removed"));
985 selection.mark(true);
987 cmd.message(N_("Mark set"));
989 selection.cursor = cursor;
992 case LFUN_UPCASE_WORD:
994 changeCase(LyXText::text_uppercase);
996 bv->updateInset(inset_owner);
1000 case LFUN_LOWCASE_WORD:
1002 changeCase(LyXText::text_lowercase);
1004 bv->updateInset(inset_owner);
1008 case LFUN_CAPITALIZE_WORD:
1010 changeCase(LyXText::text_capitalization);
1012 bv->updateInset(inset_owner);
1016 case LFUN_TRANSPOSE_CHARS:
1018 transposeChars(*this, cursor);
1020 bv->updateInset(inset_owner);
1025 cmd.message(_("Paste"));
1026 // clear the selection
1027 bv->toggleSelection();
1030 size_t sel_index = 0;
1031 string const & arg = cmd.argument;
1032 if (isStrUnsignedInt(arg)) {
1033 size_t const paste_arg = strToUnsignedInt(arg);
1034 #warning FIXME Check if the arg is in the domain of available selections.
1035 sel_index = paste_arg;
1037 pasteSelection(sel_index);
1038 clearSelection(); // bug 393
1046 cutSelection(true, true);
1048 cmd.message(_("Cut"));
1053 cmd.message(_("Copy"));
1056 case LFUN_BEGINNINGBUFSEL:
1058 return UNDISPATCHED;
1061 finishChange(bv, true);
1064 case LFUN_ENDBUFSEL:
1066 return UNDISPATCHED;
1069 finishChange(bv, true);
1073 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1079 istringstream is(STRCONV(cmd.argument));
1082 lyxerr << "SETXY: Could not parse coordinates in '"
1083 << cmd.argument << std::endl;
1085 setCursorFromCoordinates(x, y);
1090 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1092 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1098 case LFUN_GETLAYOUT:
1099 cmd.message(tostr(cursor.par()->layout()));
1103 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1104 << cmd.argument << endl;
1106 // This is not the good solution to the empty argument
1107 // problem, but it will hopefully suffice for 1.2.0.
1108 // The correct solution would be to augument the
1109 // function list/array with information about what
1110 // functions needs arguments and their type.
1111 if (cmd.argument.empty()) {
1112 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1116 // Derive layout number from given argument (string)
1117 // and current buffer's textclass (number)
1118 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1119 bool hasLayout = tclass.hasLayout(cmd.argument);
1120 string layout = cmd.argument;
1122 // If the entry is obsolete, use the new one instead.
1124 string const & obs = tclass[layout]->obsoleted_by();
1130 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1135 bool change_layout = (current_layout != layout);
1137 if (!change_layout && selection.set() &&
1138 selection.start.par() != selection.end.par())
1140 ParagraphList::iterator spit = selection.start.par();
1141 ParagraphList::iterator epit = boost::next(selection.end.par());
1142 while (spit != epit) {
1143 if (spit->layout()->name() != current_layout) {
1144 change_layout = true;
1151 if (change_layout) {
1152 current_layout = layout;
1155 bv->owner()->setLayout(layout);
1162 case LFUN_PASTESELECTION: {
1165 // this was originally a beforeChange(bv->text), i.e
1166 // the outermost LyXText!
1167 bv->beforeChange(this);
1168 string const clip = bv->getClipboard();
1169 if (!clip.empty()) {
1170 if (cmd.argument == "paragraph")
1171 insertStringAsParagraphs(clip);
1173 insertStringAsLines(clip);
1180 case LFUN_GOTOERROR:
1181 gotoInset(Inset::ERROR_CODE, false);
1185 gotoInset(Inset::NOTE_CODE, false);
1188 case LFUN_REFERENCE_GOTO:
1190 vector<Inset::Code> tmp;
1191 tmp.push_back(Inset::LABEL_CODE);
1192 tmp.push_back(Inset::REF_CODE);
1193 gotoInset(tmp, true);
1198 ParagraphList::iterator pit = cursor.par();
1199 lyx::pos_type pos = cursor.pos();
1203 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1206 c = pit->getChar(pos - 1);
1208 LyXLayout_ptr const & style = pit->layout();
1210 if (style->pass_thru ||
1211 pit->getFontSettings(bv->buffer()->params,
1212 pos).language()->lang() == "hebrew" ||
1213 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1214 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1218 case LFUN_DATE_INSERT: {
1219 time_t now_time_t = time(NULL);
1220 struct tm * now_tm = localtime(&now_time_t);
1221 setlocale(LC_TIME, "");
1223 if (!cmd.argument.empty())
1226 arg = lyxrc.date_insert_format;
1228 int const datetmp_len =
1229 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1231 for (int i = 0; i < datetmp_len; i++) {
1232 insertChar(datetmp[i]);
1235 selection.cursor = cursor;
1236 moveCursorUpdate(bv, false);
1240 case LFUN_MOUSE_TRIPLE:
1243 if (!isInInset() && bv->theLockingInset())
1245 if (cmd.button() == mouse_button::button1) {
1247 bv->screen().toggleSelection(this, bv);
1250 selection.cursor = cursor;
1254 bv->screen().toggleSelection(this, bv, false);
1256 bv->haveSelection(selection.set());
1260 case LFUN_MOUSE_DOUBLE:
1263 if (!isInInset() && bv->theLockingInset())
1265 if (cmd.button() == mouse_button::button1) {
1267 bv->screen().toggleSelection(this, bv);
1268 selectWord(LyXText::WHOLE_WORD_STRICT);
1269 bv->screen().toggleSelection(this, bv, false);
1271 selectWord(LyXText::WHOLE_WORD_STRICT);
1274 bv->haveSelection(selection.set());
1278 case LFUN_MOUSE_MOTION:
1280 // Only use motion with button 1
1281 //if (ev.button() != mouse_button::button1)
1287 // Check for inset locking
1288 if (bv->theLockingInset()) {
1289 Inset * tli = bv->theLockingInset();
1290 LyXCursor cursor = bv->text->cursor;
1291 LyXFont font = bv->text->getFont(bv->buffer(),
1292 cursor.par(), cursor.pos());
1293 int width = tli->width(bv, font);
1294 int inset_x = font.isVisibleRightToLeft()
1295 ? cursor.ix() - width : cursor.ix();
1296 int start_x = inset_x + tli->scroll();
1297 FuncRequest cmd1 = cmd;
1298 cmd1.x = cmd.x - start_x;
1299 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1300 tli->localDispatch(cmd1);
1304 // The test for not selection possible is needed, that only motion
1305 // events are used, where the bottom press event was on
1306 // the drawing area too
1307 if (!selection_possible) {
1308 lyxerr[Debug::ACTION]
1309 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1313 RowList::iterator cursorrow = bv->text->cursor.row();
1314 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->text->top_y());
1316 // sorry for this but I have a strange error that the y value jumps at
1317 // a certain point. This seems like an error in my xforms library or
1318 // in some other local environment, but I would like to leave this here
1319 // for the moment until I can remove this (Jug 20020418)
1320 if (y_before < bv->text->cursor.y())
1321 lyxerr << y_before << ':'
1322 << bv->text->cursor.y() << endl;
1324 // This is to allow jumping over large insets
1325 if (cursorrow == bv->text->cursor.row()) {
1326 if (cmd.y >= bv->workHeight())
1327 bv->text->cursorDown(false);
1329 bv->text->cursorUp(false);
1332 // Maybe an empty line was deleted
1333 if (!bv->text->selection.set())
1334 bv->update(BufferView::UPDATE);
1335 bv->text->setSelection();
1336 bv->screen().toggleToggle(bv->text, bv);
1341 // Single-click on work area
1342 case LFUN_MOUSE_PRESS:
1347 // ok ok, this is a hack (for xforms)
1348 // We shouldn't go further down as we really should only do the
1349 // scrolling and be done with this. Otherwise we may open some
1350 // dialogs (Jug 20020424).
1351 if (cmd.button() == mouse_button::button4) {
1352 bv->scroll(-lyxrc.wheel_jump);
1355 if (cmd.button() == mouse_button::button5) {
1356 bv->scroll(lyxrc.wheel_jump);
1362 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1364 // Middle button press pastes if we have a selection
1365 // We do this here as if the selection was inside an inset
1366 // it could get cleared on the unlocking of the inset so
1367 // we have to check this first
1368 bool paste_internally = false;
1369 if (cmd.button() == mouse_button::button2 && selection.set()) {
1370 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1371 paste_internally = true;
1374 int const screen_first = bv->text->top_y();
1376 if (bv->theLockingInset()) {
1377 // We are in inset locking mode
1379 // Check whether the inset was hit. If not reset mode,
1380 // otherwise give the event to the inset
1381 if (inset_hit == bv->theLockingInset()) {
1382 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1383 bv->theLockingInset()->localDispatch(cmd1);
1386 bv->unlockInset(bv->theLockingInset());
1390 selection_possible = true;
1392 // Clear the selection
1393 bv->screen().toggleSelection(bv->text, bv);
1394 bv->text->clearSelection();
1395 bv->text->fullRebreak();
1397 bv->updateScrollbar();
1399 // Single left click in math inset?
1400 if (isHighlyEditableInset(inset_hit)) {
1401 // Highly editable inset, like math
1402 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1403 selection_possible = false;
1404 bv->owner()->message(inset->editMessage());
1405 // We just have to lock the inset before calling a PressEvent on it!
1406 if (!bv->lockInset(inset))
1407 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1408 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1409 inset->localDispatch(cmd1);
1412 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1414 // Right click on a footnote flag opens float menu
1415 if (cmd.button() == mouse_button::button3) {
1416 selection_possible = false;
1420 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1421 bv->text->setCursorFromCoordinates(x, y + screen_first);
1423 bv->text->selection.cursor = bv->text->cursor;
1424 bv->text->cursor.x_fix(bv->text->cursor.x());
1426 if (bv->fitCursor())
1427 selection_possible = false;
1429 // Insert primary selection with middle mouse
1430 // if there is a local selection in the current buffer,
1432 if (cmd.button() == mouse_button::button2) {
1433 if (paste_internally)
1434 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1436 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1437 selection_possible = false;
1442 case LFUN_MOUSE_RELEASE:
1444 // do nothing if we used the mouse wheel
1448 if (cmd.button() == mouse_button::button4
1449 || cmd.button() == mouse_button::button5)
1452 // If we hit an inset, we have the inset coordinates in these
1453 // and inset_hit points to the inset. If we do not hit an
1454 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1457 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1459 if (bv->theLockingInset()) {
1460 // We are in inset locking mode.
1462 // LyX does a kind of work-area grabbing for insets.
1463 // Only a ButtonPress FuncRequest outside the inset will
1464 // force a insetUnlock.
1465 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1466 bv->theLockingInset()->localDispatch(cmd1);
1470 selection_possible = false;
1472 if (cmd.button() == mouse_button::button2)
1476 if (cmd.button() == mouse_button::button1)
1477 bv->haveSelection(selection.set());
1480 bv->owner()->view_state_changed();
1481 bv->owner()->updateMenubar();
1482 bv->owner()->updateToolbar();
1484 // Did we hit an editable inset?
1486 selection_possible = false;
1488 // if we reach this point with a selection, it
1489 // must mean we are currently selecting.
1490 // But we don't want to open the inset
1491 // because that is annoying for the user.
1492 // So just pretend we didn't hit it.
1493 // this is OK because a "kosher" ButtonRelease
1494 // will follow a ButtonPress that clears
1496 // Note this also fixes selection drawing
1497 // problems if we end up opening an inset
1498 if (selection.set())
1501 // CHECK fix this proper in 0.13
1502 // well, maybe 13.0 !!!!!!!!!
1504 // Following a ref shouldn't issue
1505 // a push on the undo-stack
1506 // anylonger, now that we have
1507 // keybindings for following
1508 // references and returning from
1509 // references. IMHO though, it
1510 // should be the inset's own business
1511 // to push or not push on the undo
1512 // stack. They don't *have* to
1513 // alter the document...
1515 // ...or maybe the SetCursorParUndo()
1516 // below isn't necessary at all anylonger?
1517 if (inset_hit->lyxCode() == Inset::REF_CODE)
1518 setCursorParUndo(bv);
1520 bv->owner()->message(inset_hit->editMessage());
1522 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1523 inset_hit->localDispatch(cmd1);
1529 case LFUN_SELFINSERT: {
1530 if (cmd.argument.empty())
1533 // Automatically delete the currently selected
1534 // text and replace it with what is being
1535 // typed in now. Depends on lyxrc settings
1536 // "auto_region_delete", which defaults to
1539 if (lyxrc.auto_region_delete) {
1540 if (selection.set()) {
1541 cutSelection(false, false);
1544 bv->haveSelection(false);
1547 bv->beforeChange(this);
1548 LyXFont const old_font(real_current_font);
1550 string::const_iterator cit = cmd.argument.begin();
1551 string::const_iterator end = cmd.argument.end();
1552 for (; cit != end; ++cit)
1553 bv->owner()->getIntl().getTransManager().
1554 TranslateAndInsert(*cit, this);
1557 selection.cursor = cursor;
1558 moveCursorUpdate(bv, false);
1560 // real_current_font.number can change so we need to
1561 // update the minibuffer
1562 if (old_font != real_current_font)
1563 bv->owner()->view_state_changed();
1567 case LFUN_HTMLURL: {
1568 InsetCommandParams p("htmlurl");
1569 string const data = InsetCommandMailer::params2string("url", p);
1570 bv->owner()->getDialogs().show("url", data, 0);
1575 InsetCommandParams p("url");
1576 string const data = InsetCommandMailer::params2string("url", p);
1577 bv->owner()->getDialogs().show("url", data, 0);
1583 case LFUN_INSET_LIST:
1584 case LFUN_INSET_THEOREM:
1585 case LFUN_INSET_CAPTION:
1587 case LFUN_INSERT_NOTE:
1588 case LFUN_INSERT_BIBITEM:
1589 case LFUN_INSET_ERT:
1590 case LFUN_INSET_FLOAT:
1591 case LFUN_INSET_FOOTNOTE:
1592 case LFUN_INSET_MARGINAL:
1593 case LFUN_INSET_MINIPAGE:
1594 case LFUN_INSET_OPTARG:
1595 case LFUN_INSET_WIDE_FLOAT:
1596 case LFUN_INSET_WRAP:
1597 case LFUN_TABULAR_INSERT:
1598 case LFUN_ENVIRONMENT_INSERT:
1599 // Open the inset, and move the current selection
1601 doInsertInset(this, cmd, true, true);
1604 case LFUN_INDEX_INSERT:
1605 // Just open the inset
1606 doInsertInset(this, cmd, true, false);
1609 case LFUN_INDEX_PRINT:
1610 case LFUN_TOC_INSERT:
1613 doInsertInset(this, cmd, false, false);
1617 return UNDISPATCHED;