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 (selecting || lt->selection.mark()) {
73 bv->updateInset(lt->inset_owner);
77 if (!lt->selection.set())
78 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);
209 if (same_content && cursor.pos() < cursor.par()->size()
210 && cursor.par()->isInset(cursor.pos())) {
211 InsetOld const * inset = cursor.par()->getInset(cursor.pos());
212 if (find(codes.begin(), codes.end(), inset->lyxCode())
214 contents = static_cast<InsetCommand const *>(inset)->getContents();
217 if (!gotoNextInset(codes, contents)) {
218 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
219 LyXCursor tmp = cursor;
220 cursor.par(ownerParagraphs().begin());
222 if (!gotoNextInset(codes, contents)) {
224 bv()->owner()->message(_("No more insets"));
227 bv()->owner()->message(_("No more insets"));
231 selection.cursor = cursor;
235 void LyXText::gotoInset(InsetOld::Code code, bool same_content)
237 gotoInset(vector<InsetOld::Code>(1, code), same_content);
241 void LyXText::cursorPrevious()
245 if (cursorRow() == rows().begin()) {
247 bv()->updateScrollbar();
251 RowList::iterator cursorrow = cursorRow();
253 setCursorFromCoordinates(cursor.x_fix(), y);
257 if (cursorrow == bv()->text->cursorRow()) {
258 // we have a row which is taller than the workarea. The
259 // simplest solution is to move to the previous row instead.
262 // This is what we used to do, so we wouldn't skip right past
263 // tall rows, but it's not working right now.
266 new_y = bv()->text->cursor.iy()
267 + bv()->theLockingInset()->insetInInsetY() + y
268 + cursorRow()->height()
269 - bv()->workHeight() + 1;
272 - cursorRow()->baseline()
273 + cursorRow()->height()
274 - bv()->workHeight() + 1;
277 //bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
278 if (cursorRow() != rows().begin()) {
280 setCursor(cur, boost::prior(cursorRow())->par(),
281 boost::prior(cursorRow())->pos(), false);
282 if (cur.y() > top_y()) {
286 bv()->updateScrollbar();
290 void LyXText::cursorNext()
294 if (boost::next(cursorRow()) == rows().end()) {
295 int y = cursor.y() - cursorRow()->baseline() +
296 cursorRow()->height();
297 if (y > topy + bv()->workHeight()) {
298 //bv()->screen().draw(bv()->text, bv(), bv()->text->top_y() + bv()->workHeight());
299 bv()->updateScrollbar();
304 int y = topy + bv()->workHeight();
305 if (inset_owner && !topy) {
306 y -= (bv()->text->cursor.iy()
307 - bv()->text->top_y()
308 + bv()->theLockingInset()->insetInInsetY());
313 RowList::iterator cursorrow = cursorRow();
314 setCursorFromCoordinates(cursor.x_fix(), y);
315 // + bv->workHeight());
319 if (cursorrow == bv()->text->cursorRow()) {
320 // we have a row which is taller than the workarea. The
321 // simplest solution is to move to the next row instead.
324 // This is what we used to do, so we wouldn't skip right past
325 // tall rows, but it's not working right now.
327 new_y = bv->text->top_y() + bv->workHeight();
331 new_y = bv()->text->cursor.iy()
332 + bv()->theLockingInset()->insetInInsetY()
333 + y - cursorRow()->baseline();
335 new_y = cursor.y() - cursorRow()->baseline();
338 //bv()->screen().draw(bv()->text, bv(), new_y);
340 RowList::iterator next_row = boost::next(cursorRow());
341 if (next_row != rows().end()) {
343 setCursor(cur, next_row->par(), next_row->pos(), false);
344 if (cur.y() < top_y() + bv()->workHeight()) {
348 bv()->updateScrollbar();
352 void LyXText::update()
354 bv()->update(this, BufferView::SELECT);
359 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
362 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
363 replaceSelection(lt);
364 if (!bv->insertInset(new_inset))
367 bv->updateInset(new_inset);
371 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
372 bool edit, bool pastesel)
374 InsetOld * inset = createInset(cmd);
375 BufferView * bv = cmd.view();
379 if (lt->selection.set()) {
380 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
383 if (bv->insertInset(inset)) {
385 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
386 inset->localDispatch(cmd);
388 if (gotsel && pastesel)
389 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
398 InsetOld::RESULT LyXText::dispatch(FuncRequest const & cmd)
400 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
401 <<"] arg[" << cmd.argument << ']' << endl;
403 BufferView * bv = cmd.view();
405 switch (cmd.action) {
407 case LFUN_APPENDIX: {
408 ParagraphList::iterator pit = cursor.par();
409 bool start = !pit->params().startOfAppendix();
411 // ensure that we have only one start_of_appendix in this document
412 ParagraphList::iterator tmp = ownerParagraphs().begin();
413 ParagraphList::iterator end = ownerParagraphs().end();
415 for (; tmp != end; ++tmp) {
416 if (tmp->params().startOfAppendix()) {
417 recordUndo(bv, Undo::ATOMIC, tmp);
418 tmp->params().startOfAppendix(false);
420 setHeightOfRow(getRow(tmp, 0, tmpy));
425 recordUndo(bv, Undo::ATOMIC, pit);
426 pit->params().startOfAppendix(start);
428 // we can set the refreshing parameters now
430 redoHeightOfParagraph();
431 setCursor(cursor.par(), cursor.pos());
436 case LFUN_DELETE_WORD_FORWARD:
437 bv->beforeChange(this);
444 case LFUN_DELETE_WORD_BACKWARD:
445 bv->beforeChange(this);
447 deleteWordBackward();
452 case LFUN_DELETE_LINE_FORWARD:
453 bv->beforeChange(this);
461 if (!selection.mark())
462 bv->beforeChange(this);
464 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
467 cursorRightOneWord();
472 if (!selection.mark())
473 bv->beforeChange(this);
475 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
476 cursorRightOneWord();
482 case LFUN_BEGINNINGBUF:
483 if (!selection.mark())
484 bv->beforeChange(this);
491 if (selection.mark())
492 bv->beforeChange(this);
500 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
504 finishChange(bv, true);
509 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
513 finishChange(bv, true);
519 finishChange(bv, true);
525 finishChange(bv, true);
528 case LFUN_UP_PARAGRAPHSEL:
531 finishChange(bv, true);
534 case LFUN_DOWN_PARAGRAPHSEL:
536 cursorDownParagraph();
537 finishChange(bv, true);
543 finishChange(bv, true);
549 finishChange(bv, true);
555 finishChange(bv, true);
561 finishChange(bv, true);
564 case LFUN_WORDRIGHTSEL:
566 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
569 cursorRightOneWord();
570 finishChange(bv, true);
573 case LFUN_WORDLEFTSEL:
575 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
576 cursorRightOneWord();
579 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);
601 if (cursor.pos() < cursor.par()->size()
602 && cursor.par()->isInset(cursor.pos())
603 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
604 InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
605 cmd.message(tmpinset->editMessage());
606 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
607 tmpinset->localDispatch(cmd1);
617 // This is soooo ugly. Isn`t it possible to make
619 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
620 if (!selection.mark())
621 bv->beforeChange(this);
623 LyXCursor const cur = cursor;
626 if ((is_rtl || cur != cursor) && // only if really moved!
627 cursor.pos() < cursor.par()->size() &&
628 cursor.par()->isInset(cursor.pos()) &&
629 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
630 InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
631 cmd.message(tmpinset->editMessage());
632 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
633 tmpinset->localDispatch(cmd1);
643 if (!selection.mark())
644 bv->beforeChange(this);
645 bv->update(this, BufferView::UPDATE);
651 if (!selection.mark())
652 bv->beforeChange(this);
653 bv->update(this, BufferView::UPDATE);
658 case LFUN_UP_PARAGRAPH:
659 if (!selection.mark())
660 bv->beforeChange(this);
661 bv->update(this, BufferView::UPDATE);
666 case LFUN_DOWN_PARAGRAPH:
667 if (!selection.mark())
668 bv->beforeChange(this);
669 bv->update(this, BufferView::UPDATE);
670 cursorDownParagraph();
671 finishChange(bv, false);
675 if (!selection.mark())
676 bv->beforeChange(this);
677 bv->update(this, BufferView::UPDATE);
679 finishChange(bv, false);
683 if (!selection.mark())
684 bv->beforeChange(this);
685 bv->update(this, BufferView::UPDATE);
687 finishChange(bv, false);
691 if (!selection.mark())
692 bv->beforeChange(this);
695 finishChange(bv, false);
699 if (!selection.mark())
700 bv->beforeChange(this);
703 finishChange(bv, false);
706 case LFUN_BREAKLINE: {
707 lyx::pos_type body = cursor.par()->beginningOfBody();
709 // Not allowed by LaTeX (labels or empty par)
710 if (cursor.pos() <= body)
713 replaceSelection(bv->getLyXText());
714 insertInset(new InsetNewline);
716 setCursor(cursor.par(), cursor.pos());
717 moveCursorUpdate(bv, false);
722 if (!selection.set()) {
724 selection.cursor = cursor;
726 // It is possible to make it a lot faster still
727 // just comment out the line below...
730 cutSelection(true, false);
733 moveCursorUpdate(bv, false);
734 bv->owner()->view_state_changed();
738 case LFUN_DELETE_SKIP:
739 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
740 if (!selection.set()) {
741 LyXCursor cur = cursor;
742 if (cur.pos() == cur.par()->size()) {
746 && !(cur.par()->params().spaceTop()
747 == VSpace (VSpace::NONE))) {
749 cur.par()->params().lineTop(),
750 cur.par()->params().lineBottom(),
751 cur.par()->params().pagebreakTop(),
752 cur.par()->params().pagebreakBottom(),
753 VSpace(VSpace::NONE),
754 cur.par()->params().spaceBottom(),
755 cur.par()->params().spacing(),
756 cur.par()->params().align(),
757 cur.par()->params().labelWidthString(), 0);
763 selection.cursor = cursor;
767 selection.cursor = cursor;
771 cutSelection(true, false);
778 if (!selection.set()) {
779 if (bv->owner()->getIntl().getTransManager().backspace()) {
781 selection.cursor = cursor;
783 // It is possible to make it a lot faster still
784 // just comment out the line below...
788 cutSelection(true, false);
791 bv->owner()->view_state_changed();
795 case LFUN_BACKSPACE_SKIP:
796 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
797 if (!selection.set()) {
798 LyXCursor cur = cursor;
800 && !(cur.par()->params().spaceTop()
801 == VSpace (VSpace::NONE))) {
803 cur.par()->params().lineTop(),
804 cur.par()->params().lineBottom(),
805 cur.par()->params().pagebreakTop(),
806 cur.par()->params().pagebreakBottom(),
807 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
808 cur.par()->params().spacing(),
809 cur.par()->params().align(),
810 cur.par()->params().labelWidthString(), 0);
813 selection.cursor = cur;
817 cutSelection(true, false);
822 case LFUN_BREAKPARAGRAPH:
823 replaceSelection(bv->getLyXText());
824 breakParagraph(bv->buffer()->paragraphs, 0);
826 selection.cursor = cursor;
828 bv->owner()->view_state_changed();
831 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
832 replaceSelection(bv->getLyXText());
833 breakParagraph(bv->buffer()->paragraphs, 1);
835 selection.cursor = cursor;
837 bv->owner()->view_state_changed();
840 case LFUN_BREAKPARAGRAPH_SKIP: {
841 // When at the beginning of a paragraph, remove
842 // indentation and add a "defskip" at the top.
843 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
844 LyXCursor cur = cursor;
845 replaceSelection(bv->getLyXText());
846 if (cur.pos() == 0) {
847 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
849 cur.par()->params().lineTop(),
850 cur.par()->params().lineBottom(),
851 cur.par()->params().pagebreakTop(),
852 cur.par()->params().pagebreakBottom(),
853 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
854 cur.par()->params().spacing(),
855 cur.par()->params().align(),
856 cur.par()->params().labelWidthString(), 1);
860 breakParagraph(bv->buffer()->paragraphs, 0);
863 selection.cursor = cur;
865 bv->owner()->view_state_changed();
869 case LFUN_PARAGRAPH_SPACING: {
870 ParagraphList::iterator pit = cursor.par();
871 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
872 float cur_value = 1.0;
873 if (cur_spacing == Spacing::Other)
874 cur_value = pit->params().spacing().getValue();
876 istringstream is(STRCONV(cmd.argument));
879 Spacing::Space new_spacing = cur_spacing;
880 float new_value = cur_value;
882 lyxerr << "Missing argument to `paragraph-spacing'"
884 } else if (tmp == "single") {
885 new_spacing = Spacing::Single;
886 } else if (tmp == "onehalf") {
887 new_spacing = Spacing::Onehalf;
888 } else if (tmp == "double") {
889 new_spacing = Spacing::Double;
890 } else if (tmp == "other") {
891 new_spacing = Spacing::Other;
894 lyxerr << "new_value = " << tmpval << endl;
897 } else if (tmp == "default") {
898 new_spacing = Spacing::Default;
900 lyxerr << _("Unknown spacing argument: ")
901 << cmd.argument << endl;
903 if (cur_spacing != new_spacing || cur_value != new_value) {
904 pit->params().spacing(Spacing(new_spacing, new_value));
911 case LFUN_INSET_SETTINGS:
912 Assert(bv->theLockingInset());
913 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
916 case LFUN_INSET_TOGGLE:
917 bv->beforeChange(this);
924 case LFUN_SPACE_INSERT:
925 if (cursor.par()->layout()->free_spacing) {
929 doInsertInset(this, cmd, false, false);
931 moveCursorUpdate(bv, false);
934 case LFUN_HYPHENATION:
935 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
938 case LFUN_LIGATURE_BREAK:
939 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
943 specialChar(this, bv, InsetSpecialChar::LDOTS);
946 case LFUN_END_OF_SENTENCE:
947 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
950 case LFUN_MENU_SEPARATOR:
951 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
955 bv->beforeChange(this);
957 selection.cursor = cursor;
958 cmd.message(N_("Mark off"));
962 bv->beforeChange(this);
963 selection.mark(true);
965 selection.cursor = cursor;
966 cmd.message(N_("Mark on"));
970 bv->beforeChange(this);
971 if (selection.mark()) {
973 cmd.message(N_("Mark removed"));
975 selection.mark(true);
977 cmd.message(N_("Mark set"));
979 selection.cursor = cursor;
982 case LFUN_UPCASE_WORD:
984 changeCase(LyXText::text_uppercase);
986 bv->updateInset(inset_owner);
990 case LFUN_LOWCASE_WORD:
992 changeCase(LyXText::text_lowercase);
994 bv->updateInset(inset_owner);
998 case LFUN_CAPITALIZE_WORD:
1000 changeCase(LyXText::text_capitalization);
1002 bv->updateInset(inset_owner);
1006 case LFUN_TRANSPOSE_CHARS:
1008 recordUndo(bv, Undo::ATOMIC, cursor.par());
1009 if (transposeChars(cursor))
1010 checkParagraph(cursor.par(), cursor.pos());
1012 bv->updateInset(inset_owner);
1017 cmd.message(_("Paste"));
1018 replaceSelection(bv->getLyXText());
1019 size_t sel_index = 0;
1020 string const & arg = cmd.argument;
1021 if (isStrUnsignedInt(arg)) {
1022 size_t const paste_arg = strToUnsignedInt(arg);
1023 #warning FIXME Check if the arg is in the domain of available selections.
1024 sel_index = paste_arg;
1026 pasteSelection(sel_index);
1027 clearSelection(); // bug 393
1035 cutSelection(true, true);
1037 cmd.message(_("Cut"));
1042 cmd.message(_("Copy"));
1045 case LFUN_BEGINNINGBUFSEL:
1047 return UNDISPATCHED;
1050 finishChange(bv, true);
1053 case LFUN_ENDBUFSEL:
1055 return UNDISPATCHED;
1058 finishChange(bv, true);
1062 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1068 istringstream is(STRCONV(cmd.argument));
1071 lyxerr << "SETXY: Could not parse coordinates in '"
1072 << cmd.argument << std::endl;
1074 setCursorFromCoordinates(x, y);
1079 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1081 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1087 case LFUN_GETLAYOUT:
1088 cmd.message(tostr(cursor.par()->layout()));
1092 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1093 << cmd.argument << endl;
1095 // This is not the good solution to the empty argument
1096 // problem, but it will hopefully suffice for 1.2.0.
1097 // The correct solution would be to augument the
1098 // function list/array with information about what
1099 // functions needs arguments and their type.
1100 if (cmd.argument.empty()) {
1101 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1105 // Derive layout number from given argument (string)
1106 // and current buffer's textclass (number)
1107 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1108 bool hasLayout = tclass.hasLayout(cmd.argument);
1109 string layout = cmd.argument;
1111 // If the entry is obsolete, use the new one instead.
1113 string const & obs = tclass[layout]->obsoleted_by();
1119 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1124 bool change_layout = (current_layout != layout);
1126 if (!change_layout && selection.set() &&
1127 selection.start.par() != selection.end.par())
1129 ParagraphList::iterator spit = selection.start.par();
1130 ParagraphList::iterator epit = boost::next(selection.end.par());
1131 while (spit != epit) {
1132 if (spit->layout()->name() != current_layout) {
1133 change_layout = true;
1140 if (change_layout) {
1141 current_layout = layout;
1144 bv->owner()->setLayout(layout);
1151 case LFUN_PASTESELECTION: {
1154 // this was originally a beforeChange(bv->text), i.e
1155 // the outermost LyXText!
1156 bv->beforeChange(this);
1157 string const clip = bv->getClipboard();
1158 if (!clip.empty()) {
1159 if (cmd.argument == "paragraph")
1160 insertStringAsParagraphs(clip);
1162 insertStringAsLines(clip);
1169 case LFUN_GOTOERROR:
1170 gotoInset(InsetOld::ERROR_CODE, false);
1174 gotoInset(InsetOld::NOTE_CODE, false);
1177 case LFUN_REFERENCE_GOTO:
1179 vector<InsetOld::Code> tmp;
1180 tmp.push_back(InsetOld::LABEL_CODE);
1181 tmp.push_back(InsetOld::REF_CODE);
1182 gotoInset(tmp, true);
1187 replaceSelection(bv->getLyXText());
1188 ParagraphList::iterator pit = cursor.par();
1189 lyx::pos_type pos = cursor.pos();
1193 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1196 c = pit->getChar(pos - 1);
1198 LyXLayout_ptr const & style = pit->layout();
1200 if (style->pass_thru ||
1201 pit->getFontSettings(bv->buffer()->params,
1202 pos).language()->lang() == "hebrew" ||
1203 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1204 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1208 case LFUN_DATE_INSERT: {
1209 replaceSelection(bv->getLyXText());
1210 time_t now_time_t = time(NULL);
1211 struct tm * now_tm = localtime(&now_time_t);
1212 setlocale(LC_TIME, "");
1214 if (!cmd.argument.empty())
1217 arg = lyxrc.date_insert_format;
1219 int const datetmp_len =
1220 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1222 for (int i = 0; i < datetmp_len; i++) {
1223 insertChar(datetmp[i]);
1226 selection.cursor = cursor;
1227 moveCursorUpdate(bv, false);
1231 case LFUN_MOUSE_TRIPLE:
1234 if (!isInInset() && bv->theLockingInset())
1236 if (cmd.button() == mouse_button::button1) {
1238 bv->screen().toggleSelection(this, bv);
1240 selection.cursor = cursor;
1244 bv->screen().toggleSelection(this, bv, false);
1246 bv->haveSelection(selection.set());
1250 case LFUN_MOUSE_DOUBLE:
1253 if (!isInInset() && bv->theLockingInset())
1255 if (cmd.button() == mouse_button::button1) {
1257 bv->screen().toggleSelection(this, bv);
1258 selectWord(lyx::WHOLE_WORD_STRICT);
1259 bv->screen().toggleSelection(this, bv, false);
1261 selectWord(lyx::WHOLE_WORD_STRICT);
1264 bv->haveSelection(selection.set());
1268 case LFUN_MOUSE_MOTION:
1270 // Only use motion with button 1
1271 //if (ev.button() != mouse_button::button1)
1277 // Check for inset locking
1278 if (bv->theLockingInset()) {
1279 InsetOld * tli = bv->theLockingInset();
1280 LyXCursor cursor = bv->text->cursor;
1281 LyXFont font = bv->text->getFont(cursor.par(), cursor.pos());
1282 int width = tli->width();
1283 int inset_x = font.isVisibleRightToLeft()
1284 ? cursor.ix() - width : cursor.ix();
1285 int start_x = inset_x + tli->scroll();
1286 FuncRequest cmd1 = cmd;
1287 cmd1.x = cmd.x - start_x;
1288 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1289 tli->localDispatch(cmd1);
1293 // The test for not selection possible is needed, that only motion
1294 // events are used, where the bottom press event was on
1295 // the drawing area too
1296 if (!selection_possible) {
1297 lyxerr[Debug::ACTION]
1298 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1302 RowList::iterator cursorrow = bv->text->cursorRow();
1303 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->text->top_y());
1305 // sorry for this but I have a strange error that the y value jumps at
1306 // a certain point. This seems like an error in my xforms library or
1307 // in some other local environment, but I would like to leave this here
1308 // for the moment until I can remove this (Jug 20020418)
1309 if (y_before < bv->text->cursor.y())
1310 lyxerr << y_before << ':'
1311 << bv->text->cursor.y() << endl;
1313 // This is to allow jumping over large insets
1314 if (cursorrow == bv->text->cursorRow()) {
1315 if (cmd.y >= bv->workHeight())
1316 bv->text->cursorDown(false);
1318 bv->text->cursorUp(false);
1321 // Maybe an empty line was deleted
1322 if (!bv->text->selection.set())
1323 bv->update(BufferView::UPDATE);
1324 bv->text->setSelection();
1330 // Single-click on work area
1331 case LFUN_MOUSE_PRESS:
1336 // ok ok, this is a hack (for xforms)
1337 // We shouldn't go further down as we really should only do the
1338 // scrolling and be done with this. Otherwise we may open some
1339 // dialogs (Jug 20020424).
1340 if (cmd.button() == mouse_button::button4) {
1341 bv->scroll(-lyxrc.wheel_jump);
1344 if (cmd.button() == mouse_button::button5) {
1345 bv->scroll(lyxrc.wheel_jump);
1351 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1353 // Middle button press pastes if we have a selection
1354 // We do this here as if the selection was inside an inset
1355 // it could get cleared on the unlocking of the inset so
1356 // we have to check this first
1357 bool paste_internally = false;
1358 if (cmd.button() == mouse_button::button2 && selection.set()) {
1359 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1360 paste_internally = true;
1363 int const screen_first = bv->text->top_y();
1365 if (bv->theLockingInset()) {
1366 // We are in inset locking mode
1368 // Check whether the inset was hit. If not reset mode,
1369 // otherwise give the event to the inset
1370 if (inset_hit == bv->theLockingInset()) {
1371 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1372 bv->theLockingInset()->localDispatch(cmd1);
1375 bv->unlockInset(bv->theLockingInset());
1379 selection_possible = true;
1381 // Clear the selection
1382 bv->screen().toggleSelection(bv->text, bv);
1383 bv->text->clearSelection();
1384 bv->text->partialRebreak();
1386 bv->updateScrollbar();
1388 // Single left click in math inset?
1389 if (isHighlyEditableInset(inset_hit)) {
1390 // Highly editable inset, like math
1391 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1392 selection_possible = false;
1393 bv->owner()->message(inset->editMessage());
1394 // We just have to lock the inset before calling a PressEvent on it!
1395 if (!bv->lockInset(inset))
1396 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1397 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1398 inset->localDispatch(cmd1);
1401 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1403 // Right click on a footnote flag opens float menu
1404 if (cmd.button() == mouse_button::button3) {
1405 selection_possible = false;
1409 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1410 bv->text->setCursorFromCoordinates(x, y + screen_first);
1412 bv->text->selection.cursor = bv->text->cursor;
1413 bv->text->cursor.x_fix(bv->text->cursor.x());
1415 if (bv->fitCursor())
1416 selection_possible = false;
1418 // Insert primary selection with middle mouse
1419 // if there is a local selection in the current buffer,
1421 if (cmd.button() == mouse_button::button2) {
1422 if (paste_internally)
1423 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1425 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1426 selection_possible = false;
1431 case LFUN_MOUSE_RELEASE:
1433 // do nothing if we used the mouse wheel
1437 if (cmd.button() == mouse_button::button4
1438 || cmd.button() == mouse_button::button5)
1441 // If we hit an inset, we have the inset coordinates in these
1442 // and inset_hit points to the inset. If we do not hit an
1443 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1446 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1448 if (bv->theLockingInset()) {
1449 // We are in inset locking mode.
1451 // LyX does a kind of work-area grabbing for insets.
1452 // Only a ButtonPress FuncRequest outside the inset will
1453 // force a insetUnlock.
1454 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1455 bv->theLockingInset()->localDispatch(cmd1);
1459 selection_possible = false;
1461 if (cmd.button() == mouse_button::button2)
1465 if (cmd.button() == mouse_button::button1)
1466 bv->haveSelection(selection.set());
1469 bv->owner()->view_state_changed();
1470 bv->owner()->updateMenubar();
1471 bv->owner()->updateToolbar();
1473 // Did we hit an editable inset?
1475 selection_possible = false;
1477 // if we reach this point with a selection, it
1478 // must mean we are currently selecting.
1479 // But we don't want to open the inset
1480 // because that is annoying for the user.
1481 // So just pretend we didn't hit it.
1482 // this is OK because a "kosher" ButtonRelease
1483 // will follow a ButtonPress that clears
1485 // Note this also fixes selection drawing
1486 // problems if we end up opening an inset
1487 if (selection.set())
1490 // CHECK fix this proper in 0.13
1491 // well, maybe 13.0 !!!!!!!!!
1493 // Following a ref shouldn't issue
1494 // a push on the undo-stack
1495 // anylonger, now that we have
1496 // keybindings for following
1497 // references and returning from
1498 // references. IMHO though, it
1499 // should be the inset's own business
1500 // to push or not push on the undo
1501 // stack. They don't *have* to
1502 // alter the document...
1504 // ...or maybe the SetCursorParUndo()
1505 // below isn't necessary at all anylonger?
1506 if (inset_hit->lyxCode() == InsetOld::REF_CODE)
1507 recordUndo(bv, Undo::ATOMIC);
1509 bv->owner()->message(inset_hit->editMessage());
1511 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1512 inset_hit->localDispatch(cmd1);
1518 case LFUN_SELFINSERT: {
1519 if (cmd.argument.empty())
1522 // Automatically delete the currently selected
1523 // text and replace it with what is being
1524 // typed in now. Depends on lyxrc settings
1525 // "auto_region_delete", which defaults to
1528 if (lyxrc.auto_region_delete) {
1529 if (selection.set()) {
1530 cutSelection(false, false);
1533 bv->haveSelection(false);
1536 bv->beforeChange(this);
1537 LyXFont const old_font(real_current_font);
1539 string::const_iterator cit = cmd.argument.begin();
1540 string::const_iterator end = cmd.argument.end();
1541 for (; cit != end; ++cit)
1542 bv->owner()->getIntl().getTransManager().
1543 TranslateAndInsert(*cit, this);
1546 selection.cursor = cursor;
1547 moveCursorUpdate(bv, false);
1549 // real_current_font.number can change so we need to
1550 // update the minibuffer
1551 if (old_font != real_current_font)
1552 bv->owner()->view_state_changed();
1556 case LFUN_HTMLURL: {
1557 InsetCommandParams p("htmlurl");
1558 string const data = InsetCommandMailer::params2string("url", p);
1559 bv->owner()->getDialogs().show("url", data, 0);
1564 InsetCommandParams p("url");
1565 string const data = InsetCommandMailer::params2string("url", p);
1566 bv->owner()->getDialogs().show("url", data, 0);
1572 case LFUN_INSET_LIST:
1573 case LFUN_INSET_THEOREM:
1574 case LFUN_INSET_CAPTION:
1576 case LFUN_INSERT_NOTE:
1577 case LFUN_INSERT_BIBITEM:
1578 case LFUN_INSET_ERT:
1579 case LFUN_INSET_FLOAT:
1580 case LFUN_INSET_FOOTNOTE:
1581 case LFUN_INSET_MARGINAL:
1582 case LFUN_INSET_MINIPAGE:
1583 case LFUN_INSET_OPTARG:
1584 case LFUN_INSET_WIDE_FLOAT:
1585 case LFUN_INSET_WRAP:
1586 case LFUN_TABULAR_INSERT:
1587 case LFUN_ENVIRONMENT_INSERT:
1588 // Open the inset, and move the current selection
1590 doInsertInset(this, cmd, true, true);
1593 case LFUN_INDEX_INSERT:
1594 // Just open the inset
1595 doInsertInset(this, cmd, true, false);
1598 case LFUN_INDEX_PRINT:
1599 case LFUN_TOC_INSERT:
1602 doInsertInset(this, cmd, false, false);
1606 return UNDISPATCHED;