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 "frontends/LyXView.h"
31 #include "frontends/screen.h"
32 #include "frontends/Dialogs.h"
33 #include "insets/insetspecialchar.h"
34 #include "insets/insettext.h"
35 #include "insets/insetquotes.h"
36 #include "insets/insetcommand.h"
37 #include "insets/insetnewline.h"
38 #include "undo_funcs.h"
39 #include "text_funcs.h"
50 extern string current_layout;
51 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
53 // the selection possible is needed, that only motion events are
54 // used, where the bottom press event was on the drawing area too
55 bool selection_possible = false;
60 void moveCursorUpdate(BufferView * bv, bool selecting)
62 LyXText * lt = bv->getLyXText();
64 if (selecting || lt->selection.mark()) {
67 bv->updateInset(lt->inset_owner);
71 if (!lt->isInInset()) {
72 bv->update(lt, BufferView::SELECT);
73 } else if (bv->text->refreshStatus() != LyXText::REFRESH_NONE) {
74 bv->update(BufferView::SELECT);
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 Inset * 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 Inset /*const*/ * inset = par->getInset(pos);
105 if (!isEditableInset(inset))
108 // get inset dimensions
109 lyx::Assert(par->getInset(pos));
111 LyXFont const & font = text.getFont(bv->buffer(), par, pos);
113 int const width = inset->width(bv, font);
114 int const inset_x = font.isVisibleRightToLeft()
115 ? (cur.ix() - width) : cur.ix();
118 inset_x + inset->scroll(),
120 cur.iy() - inset->ascent(bv, font),
121 cur.iy() + inset->descent(bv, font)
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 Inset * LyXText::checkInsetHit(int & x, int & y)
145 int y_tmp = y + top_y();
148 setCursorFromCoordinates(cur, x, y_tmp);
150 Inset * 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<Inset::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<Inset::Code> const & codes,
205 bv()->beforeChange(this);
209 if (same_content && cursor.pos() < cursor.par()->size()
210 && cursor.par()->isInset(cursor.pos())) {
211 Inset 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(Inset::Code code, bool same_content)
237 gotoInset(vector<Inset::Code>(1, code), same_content);
241 void LyXText::cursorPrevious()
245 if (cursor.row() == rows().begin()) {
247 int new_y = bv()->text->top_y() - bv()->workHeight();
248 bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
249 bv()->updateScrollbar();
254 RowList::iterator cursorrow = cursor.row();
256 setCursorFromCoordinates(cursor.x_fix(), y);
260 if (cursorrow == bv()->text->cursor.row()) {
261 // we have a row which is taller than the workarea. The
262 // simplest solution is to move to the previous row instead.
265 // This is what we used to do, so we wouldn't skip right past
266 // tall rows, but it's not working right now.
268 new_y = bv->text->top_y() - bv->workHeight();
272 new_y = bv()->text->cursor.iy()
273 + bv()->theLockingInset()->insetInInsetY() + y
274 + cursor.row()->height()
275 - bv()->workHeight() + 1;
278 - cursor.row()->baseline()
279 + cursor.row()->height()
280 - bv()->workHeight() + 1;
283 bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
284 if (cursor.row() != rows().begin()) {
286 setCursor(cur, boost::prior(cursor.row())->par(),
287 boost::prior(cursor.row())->pos(), false);
288 if (cur.y() > top_y()) {
292 bv()->updateScrollbar();
296 void LyXText::cursorNext()
300 if (boost::next(cursor.row()) == rows().end()) {
301 int y = cursor.y() - cursor.row()->baseline() +
302 cursor.row()->height();
303 if (y > topy + bv()->workHeight()) {
304 bv()->screen().draw(bv()->text, bv(), bv()->text->top_y() + bv()->workHeight());
305 bv()->updateScrollbar();
310 int y = topy + bv()->workHeight();
311 if (inset_owner && !topy) {
312 y -= (bv()->text->cursor.iy()
313 - bv()->text->top_y()
314 + bv()->theLockingInset()->insetInInsetY());
319 RowList::iterator cursorrow = cursor.row();
320 setCursorFromCoordinates(cursor.x_fix(), y);
321 // + bv->workHeight());
325 if (cursorrow == bv()->text->cursor.row()) {
326 // we have a row which is taller than the workarea. The
327 // simplest solution is to move to the next row instead.
330 // This is what we used to do, so we wouldn't skip right past
331 // tall rows, but it's not working right now.
333 new_y = bv->text->top_y() + bv->workHeight();
337 new_y = bv()->text->cursor.iy()
338 + bv()->theLockingInset()->insetInInsetY()
339 + y - cursor.row()->baseline();
341 new_y = cursor.y() - cursor.row()->baseline();
344 bv()->screen().draw(bv()->text, bv(), new_y);
345 if (boost::next(cursor.row()) != rows().end()) {
347 setCursor(cur, boost::next(cursor.row())->par(),
348 boost::next(cursor.row())->pos(), false);
349 if (cur.y() < top_y() + bv()->workHeight()) {
353 bv()->updateScrollbar();
357 void LyXText::update()
359 bv()->update(this, BufferView::SELECT);
364 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
367 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
368 if (!bv->insertInset(new_inset))
371 bv->updateInset(new_inset);
375 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
376 bool edit, bool pastesel)
378 Inset * inset = createInset(cmd);
379 BufferView * bv = cmd.view();
383 if (lt->selection.set()) {
384 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
387 if (bv->insertInset(inset)) {
389 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
390 inset->localDispatch(cmd);
392 if (gotsel && pastesel)
393 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
402 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
404 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
405 <<"] arg[" << cmd.argument << ']' << endl;
407 BufferView * bv = cmd.view();
409 switch (cmd.action) {
411 case LFUN_APPENDIX: {
412 ParagraphList::iterator pit = cursor.par();
413 bool start = !pit->params().startOfAppendix();
415 // ensure that we have only one start_of_appendix in this document
416 ParagraphList::iterator tmp = ownerParagraphs().begin();
417 ParagraphList::iterator end = ownerParagraphs().end();
419 for (; tmp != end; ++tmp) {
420 if (tmp->params().startOfAppendix()) {
421 setUndo(bv, Undo::EDIT, tmp);
422 tmp->params().startOfAppendix(false);
424 setHeightOfRow(getRow(tmp, 0, tmpy));
429 setUndo(bv, Undo::EDIT, pit);
430 pit->params().startOfAppendix(start);
432 // we can set the refreshing parameters now
434 redoHeightOfParagraph();
436 setCursor(cursor.par(), cursor.pos());
441 case LFUN_DELETE_WORD_FORWARD:
442 bv->beforeChange(this);
449 case LFUN_DELETE_WORD_BACKWARD:
450 bv->beforeChange(this);
452 deleteWordBackward();
457 case LFUN_DELETE_LINE_FORWARD:
458 bv->beforeChange(this);
466 if (!selection.mark())
467 bv->beforeChange(this);
469 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
472 cursorRightOneWord();
477 if (!selection.mark())
478 bv->beforeChange(this);
480 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
481 cursorRightOneWord();
487 case LFUN_BEGINNINGBUF:
488 if (!selection.mark())
489 bv->beforeChange(this);
496 if (selection.mark())
497 bv->beforeChange(this);
505 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
509 finishChange(bv, true);
514 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
518 finishChange(bv, true);
524 finishChange(bv, true);
530 finishChange(bv, true);
533 case LFUN_UP_PARAGRAPHSEL:
536 finishChange(bv, true);
539 case LFUN_DOWN_PARAGRAPHSEL:
541 cursorDownParagraph();
542 finishChange(bv, true);
548 finishChange(bv, true);
554 finishChange(bv, true);
560 finishChange(bv, true);
566 finishChange(bv, true);
569 case LFUN_WORDRIGHTSEL:
571 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
574 cursorRightOneWord();
575 finishChange(bv, true);
578 case LFUN_WORDLEFTSEL:
580 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
581 cursorRightOneWord();
584 finishChange(bv, true);
591 getWord(cur1, cur2, WHOLE_WORD);
592 setCursor(cur1.par(), cur1.pos());
593 bv->beforeChange(this);
594 setCursor(cur2.par(), cur2.pos());
595 finishChange(bv, true);
600 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
601 if (!selection.mark())
602 bv->beforeChange(this);
606 if (cursor.pos() < cursor.par()->size()
607 && cursor.par()->isInset(cursor.pos())
608 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
609 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
610 cmd.message(tmpinset->editMessage());
611 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
612 tmpinset->localDispatch(cmd1);
622 // This is soooo ugly. Isn`t it possible to make
624 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
625 if (!selection.mark())
626 bv->beforeChange(this);
628 LyXCursor const cur = cursor;
631 if ((is_rtl || cur != cursor) && // only if really moved!
632 cursor.pos() < cursor.par()->size() &&
633 cursor.par()->isInset(cursor.pos()) &&
634 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
635 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
636 cmd.message(tmpinset->editMessage());
637 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
638 tmpinset->localDispatch(cmd1);
648 if (!selection.mark())
649 bv->beforeChange(this);
650 bv->update(this, BufferView::UPDATE);
656 if (!selection.mark())
657 bv->beforeChange(this);
658 bv->update(this, BufferView::UPDATE);
663 case LFUN_UP_PARAGRAPH:
664 if (!selection.mark())
665 bv->beforeChange(this);
666 bv->update(this, BufferView::UPDATE);
671 case LFUN_DOWN_PARAGRAPH:
672 if (!selection.mark())
673 bv->beforeChange(this);
674 bv->update(this, BufferView::UPDATE);
675 cursorDownParagraph();
676 finishChange(bv, false);
680 if (!selection.mark())
681 bv->beforeChange(this);
682 bv->update(this, BufferView::UPDATE);
684 finishChange(bv, false);
687 // moveCursorUpdate(bv, false, false);
688 // owner_->view_state_changed();
692 if (!selection.mark())
693 bv->beforeChange(this);
694 bv->update(this, BufferView::UPDATE);
696 finishChange(bv, false);
700 if (!selection.mark())
701 bv->beforeChange(this);
704 finishChange(bv, false);
708 if (!selection.mark())
709 bv->beforeChange(this);
712 finishChange(bv, false);
715 case LFUN_BREAKLINE: {
716 lyx::pos_type body = cursor.par()->beginningOfBody();
718 // Not allowed by LaTeX (labels or empty par)
719 if (cursor.pos() <= body)
722 bv->beforeChange(this);
723 insertInset(new InsetNewline);
725 setCursor(cursor.par(), cursor.pos());
726 moveCursorUpdate(bv, false);
731 if (!selection.set()) {
733 selection.cursor = cursor;
735 // It is possible to make it a lot faster still
736 // just comment out the line below...
739 cutSelection(true, false);
742 moveCursorUpdate(bv, false);
743 bv->owner()->view_state_changed();
747 case LFUN_DELETE_SKIP:
748 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
749 if (!selection.set()) {
750 LyXCursor cur = cursor;
751 if (cur.pos() == cur.par()->size()) {
755 && !(cur.par()->params().spaceTop()
756 == VSpace (VSpace::NONE))) {
758 cur.par()->params().lineTop(),
759 cur.par()->params().lineBottom(),
760 cur.par()->params().pagebreakTop(),
761 cur.par()->params().pagebreakBottom(),
762 VSpace(VSpace::NONE),
763 cur.par()->params().spaceBottom(),
764 cur.par()->params().spacing(),
765 cur.par()->params().align(),
766 cur.par()->params().labelWidthString(), 0);
772 selection.cursor = cursor;
776 selection.cursor = cursor;
780 cutSelection(true, false);
787 if (!selection.set()) {
788 if (bv->owner()->getIntl().getTransManager().backspace()) {
790 selection.cursor = cursor;
792 // It is possible to make it a lot faster still
793 // just comment out the line below...
797 cutSelection(true, false);
800 bv->owner()->view_state_changed();
804 case LFUN_BACKSPACE_SKIP:
805 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
806 if (!selection.set()) {
807 LyXCursor cur = cursor;
809 && !(cur.par()->params().spaceTop()
810 == VSpace (VSpace::NONE))) {
812 cur.par()->params().lineTop(),
813 cur.par()->params().lineBottom(),
814 cur.par()->params().pagebreakTop(),
815 cur.par()->params().pagebreakBottom(),
816 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
817 cur.par()->params().spacing(),
818 cur.par()->params().align(),
819 cur.par()->params().labelWidthString(), 0);
822 selection.cursor = cur;
826 cutSelection(true, false);
831 case LFUN_BREAKPARAGRAPH:
832 bv->beforeChange(this);
833 breakParagraph(bv->buffer()->paragraphs, 0);
835 selection.cursor = cursor;
837 bv->owner()->view_state_changed();
840 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
841 bv->beforeChange(this);
842 breakParagraph(bv->buffer()->paragraphs, 1);
844 selection.cursor = cursor;
846 bv->owner()->view_state_changed();
849 case LFUN_BREAKPARAGRAPH_SKIP: {
850 // When at the beginning of a paragraph, remove
851 // indentation and add a "defskip" at the top.
852 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
853 LyXCursor cur = cursor;
854 bv->beforeChange(this);
855 if (cur.pos() == 0) {
856 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
858 cur.par()->params().lineTop(),
859 cur.par()->params().lineBottom(),
860 cur.par()->params().pagebreakTop(),
861 cur.par()->params().pagebreakBottom(),
862 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
863 cur.par()->params().spacing(),
864 cur.par()->params().align(),
865 cur.par()->params().labelWidthString(), 1);
869 breakParagraph(bv->buffer()->paragraphs, 0);
872 selection.cursor = cur;
874 bv->owner()->view_state_changed();
878 case LFUN_PARAGRAPH_SPACING: {
879 ParagraphList::iterator pit = cursor.par();
880 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
881 float cur_value = 1.0;
882 if (cur_spacing == Spacing::Other)
883 cur_value = pit->params().spacing().getValue();
885 istringstream is(STRCONV(cmd.argument));
888 Spacing::Space new_spacing = cur_spacing;
889 float new_value = cur_value;
891 lyxerr << "Missing argument to `paragraph-spacing'"
893 } else if (tmp == "single") {
894 new_spacing = Spacing::Single;
895 } else if (tmp == "onehalf") {
896 new_spacing = Spacing::Onehalf;
897 } else if (tmp == "double") {
898 new_spacing = Spacing::Double;
899 } else if (tmp == "other") {
900 new_spacing = Spacing::Other;
903 lyxerr << "new_value = " << tmpval << endl;
906 } else if (tmp == "default") {
907 new_spacing = Spacing::Default;
909 lyxerr << _("Unknown spacing argument: ")
910 << cmd.argument << endl;
912 if (cur_spacing != new_spacing || cur_value != new_value) {
913 pit->params().spacing(Spacing(new_spacing, new_value));
920 case LFUN_INSET_SETTINGS:
921 lyx::Assert(bv->theLockingInset());
922 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
925 case LFUN_INSET_TOGGLE:
926 bv->beforeChange(this);
933 case LFUN_SPACE_INSERT:
934 if (cursor.par()->layout()->free_spacing) {
938 doInsertInset(this, cmd, false, false);
940 moveCursorUpdate(bv, false);
943 case LFUN_HYPHENATION:
944 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
947 case LFUN_LIGATURE_BREAK:
948 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
952 specialChar(this, bv, InsetSpecialChar::LDOTS);
955 case LFUN_END_OF_SENTENCE:
956 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
959 case LFUN_MENU_SEPARATOR:
960 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
964 bv->beforeChange(this);
966 selection.cursor = cursor;
967 cmd.message(N_("Mark off"));
971 bv->beforeChange(this);
972 selection.mark(true);
974 selection.cursor = cursor;
975 cmd.message(N_("Mark on"));
979 bv->beforeChange(this);
980 if (selection.mark()) {
982 cmd.message(N_("Mark removed"));
984 selection.mark(true);
986 cmd.message(N_("Mark set"));
988 selection.cursor = cursor;
991 case LFUN_UPCASE_WORD:
993 changeCase(LyXText::text_uppercase);
995 bv->updateInset(inset_owner);
999 case LFUN_LOWCASE_WORD:
1001 changeCase(LyXText::text_lowercase);
1003 bv->updateInset(inset_owner);
1007 case LFUN_CAPITALIZE_WORD:
1009 changeCase(LyXText::text_capitalization);
1011 bv->updateInset(inset_owner);
1015 case LFUN_TRANSPOSE_CHARS:
1017 transposeChars(*this, cursor);
1019 bv->updateInset(inset_owner);
1024 cmd.message(_("Paste"));
1025 // clear the selection
1026 bv->toggleSelection();
1030 clearSelection(); // bug 393
1037 cutSelection(true, true);
1039 cmd.message(_("Cut"));
1044 cmd.message(_("Copy"));
1047 case LFUN_BEGINNINGBUFSEL:
1049 return UNDISPATCHED;
1052 finishChange(bv, true);
1055 case LFUN_ENDBUFSEL:
1057 return UNDISPATCHED;
1060 finishChange(bv, true);
1064 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1070 istringstream is(STRCONV(cmd.argument));
1073 lyxerr << "SETXY: Could not parse coordinates in '"
1074 << cmd.argument << std::endl;
1076 setCursorFromCoordinates(x, y);
1081 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1083 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1089 case LFUN_GETLAYOUT:
1090 cmd.message(tostr(cursor.par()->layout()));
1094 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1095 << cmd.argument << endl;
1097 // This is not the good solution to the empty argument
1098 // problem, but it will hopefully suffice for 1.2.0.
1099 // The correct solution would be to augument the
1100 // function list/array with information about what
1101 // functions needs arguments and their type.
1102 if (cmd.argument.empty()) {
1103 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1107 // Derive layout number from given argument (string)
1108 // and current buffer's textclass (number)
1109 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1110 bool hasLayout = tclass.hasLayout(cmd.argument);
1111 string layout = cmd.argument;
1113 // If the entry is obsolete, use the new one instead.
1115 string const & obs = tclass[layout]->obsoleted_by();
1121 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1126 bool change_layout = (current_layout != layout);
1128 if (!change_layout && selection.set() &&
1129 selection.start.par() != selection.end.par())
1131 ParagraphList::iterator spit = selection.start.par();
1132 ParagraphList::iterator epit = boost::next(selection.end.par());
1133 while (spit != epit) {
1134 if (spit->layout()->name() != current_layout) {
1135 change_layout = true;
1142 if (change_layout) {
1143 current_layout = layout;
1146 bv->owner()->setLayout(layout);
1153 case LFUN_PASTESELECTION: {
1156 // this was originally a beforeChange(bv->text), i.e
1157 // the outermost LyXText!
1158 bv->beforeChange(this);
1159 string const clip = bv->getClipboard();
1160 if (!clip.empty()) {
1161 if (cmd.argument == "paragraph")
1162 insertStringAsParagraphs(clip);
1164 insertStringAsLines(clip);
1171 case LFUN_GOTOERROR:
1172 gotoInset(Inset::ERROR_CODE, false);
1176 gotoInset(Inset::NOTE_CODE, false);
1179 case LFUN_REFERENCE_GOTO:
1181 vector<Inset::Code> tmp;
1182 tmp.push_back(Inset::LABEL_CODE);
1183 tmp.push_back(Inset::REF_CODE);
1184 gotoInset(tmp, true);
1189 ParagraphList::iterator pit = cursor.par();
1190 lyx::pos_type pos = cursor.pos();
1194 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1197 c = pit->getChar(pos - 1);
1199 LyXLayout_ptr const & style = pit->layout();
1201 if (style->pass_thru ||
1202 pit->getFontSettings(bv->buffer()->params,
1203 pos).language()->lang() == "hebrew" ||
1204 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1205 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1209 case LFUN_DATE_INSERT: {
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);
1241 selection.cursor = cursor;
1245 bv->screen().toggleSelection(this, bv, false);
1247 bv->haveSelection(selection.set());
1251 case LFUN_MOUSE_DOUBLE:
1254 if (!isInInset() && bv->theLockingInset())
1256 if (cmd.button() == mouse_button::button1) {
1258 bv->screen().toggleSelection(this, bv);
1259 selectWord(LyXText::WHOLE_WORD_STRICT);
1260 bv->screen().toggleSelection(this, bv, false);
1262 selectWord(LyXText::WHOLE_WORD_STRICT);
1265 bv->haveSelection(selection.set());
1269 case LFUN_MOUSE_MOTION:
1271 // Only use motion with button 1
1272 //if (ev.button() != mouse_button::button1)
1278 // Check for inset locking
1279 if (bv->theLockingInset()) {
1280 Inset * tli = bv->theLockingInset();
1281 LyXCursor cursor = bv->text->cursor;
1282 LyXFont font = bv->text->getFont(bv->buffer(),
1283 cursor.par(), cursor.pos());
1284 int width = tli->width(bv, font);
1285 int inset_x = font.isVisibleRightToLeft()
1286 ? cursor.ix() - width : cursor.ix();
1287 int start_x = inset_x + tli->scroll();
1288 FuncRequest cmd1 = cmd;
1289 cmd1.x = cmd.x - start_x;
1290 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1291 tli->localDispatch(cmd1);
1295 // The test for not selection possible is needed, that only motion
1296 // events are used, where the bottom press event was on
1297 // the drawing area too
1298 if (!selection_possible) {
1299 lyxerr[Debug::ACTION]
1300 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1304 RowList::iterator cursorrow = bv->text->cursor.row();
1305 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->text->top_y());
1307 // sorry for this but I have a strange error that the y value jumps at
1308 // a certain point. This seems like an error in my xforms library or
1309 // in some other local environment, but I would like to leave this here
1310 // for the moment until I can remove this (Jug 20020418)
1311 if (y_before < bv->text->cursor.y())
1312 lyxerr << y_before << ':'
1313 << bv->text->cursor.y() << endl;
1315 // This is to allow jumping over large insets
1316 if (cursorrow == bv->text->cursor.row()) {
1317 if (cmd.y >= bv->workHeight())
1318 bv->text->cursorDown(false);
1320 bv->text->cursorUp(false);
1323 // Maybe an empty line was deleted
1324 if (!bv->text->selection.set())
1325 bv->update(BufferView::UPDATE);
1326 bv->text->setSelection();
1327 bv->screen().toggleToggle(bv->text, bv);
1332 // Single-click on work area
1333 case LFUN_MOUSE_PRESS:
1338 // ok ok, this is a hack (for xforms)
1339 // We shouldn't go further down as we really should only do the
1340 // scrolling and be done with this. Otherwise we may open some
1341 // dialogs (Jug 20020424).
1342 if (cmd.button() == mouse_button::button4) {
1343 bv->scroll(-lyxrc.wheel_jump);
1346 if (cmd.button() == mouse_button::button5) {
1347 bv->scroll(lyxrc.wheel_jump);
1353 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1355 // Middle button press pastes if we have a selection
1356 // We do this here as if the selection was inside an inset
1357 // it could get cleared on the unlocking of the inset so
1358 // we have to check this first
1359 bool paste_internally = false;
1360 if (cmd.button() == mouse_button::button2 && selection.set()) {
1361 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1362 paste_internally = true;
1365 int const screen_first = bv->text->top_y();
1367 if (bv->theLockingInset()) {
1368 // We are in inset locking mode
1370 // Check whether the inset was hit. If not reset mode,
1371 // otherwise give the event to the inset
1372 if (inset_hit == bv->theLockingInset()) {
1373 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1374 bv->theLockingInset()->localDispatch(cmd1);
1377 bv->unlockInset(bv->theLockingInset());
1381 selection_possible = true;
1383 // Clear the selection
1384 bv->screen().toggleSelection(bv->text, bv);
1385 bv->text->clearSelection();
1386 bv->text->fullRebreak();
1388 bv->updateScrollbar();
1390 // Single left click in math inset?
1391 if (isHighlyEditableInset(inset_hit)) {
1392 // Highly editable inset, like math
1393 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1394 selection_possible = false;
1395 bv->owner()->message(inset->editMessage());
1396 // We just have to lock the inset before calling a PressEvent on it!
1397 if (!bv->lockInset(inset))
1398 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1399 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1400 inset->localDispatch(cmd1);
1403 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1405 // Right click on a footnote flag opens float menu
1406 if (cmd.button() == mouse_button::button3) {
1407 selection_possible = false;
1411 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1412 bv->text->setCursorFromCoordinates(x, y + screen_first);
1414 bv->text->selection.cursor = bv->text->cursor;
1415 bv->text->cursor.x_fix(bv->text->cursor.x());
1417 if (bv->fitCursor())
1418 selection_possible = false;
1420 // Insert primary selection with middle mouse
1421 // if there is a local selection in the current buffer,
1423 if (cmd.button() == mouse_button::button2) {
1424 if (paste_internally)
1425 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1427 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1428 selection_possible = false;
1433 case LFUN_MOUSE_RELEASE:
1435 // do nothing if we used the mouse wheel
1439 if (cmd.button() == mouse_button::button4
1440 || cmd.button() == mouse_button::button5)
1443 // If we hit an inset, we have the inset coordinates in these
1444 // and inset_hit points to the inset. If we do not hit an
1445 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1448 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1450 if (bv->theLockingInset()) {
1451 // We are in inset locking mode.
1453 // LyX does a kind of work-area grabbing for insets.
1454 // Only a ButtonPress FuncRequest outside the inset will
1455 // force a insetUnlock.
1456 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1457 bv->theLockingInset()->localDispatch(cmd1);
1461 selection_possible = false;
1463 if (cmd.button() == mouse_button::button2)
1467 if (cmd.button() == mouse_button::button1)
1468 bv->haveSelection(selection.set());
1471 bv->owner()->view_state_changed();
1472 bv->owner()->updateMenubar();
1473 bv->owner()->updateToolbar();
1475 // Did we hit an editable inset?
1477 selection_possible = false;
1479 // if we reach this point with a selection, it
1480 // must mean we are currently selecting.
1481 // But we don't want to open the inset
1482 // because that is annoying for the user.
1483 // So just pretend we didn't hit it.
1484 // this is OK because a "kosher" ButtonRelease
1485 // will follow a ButtonPress that clears
1487 // Note this also fixes selection drawing
1488 // problems if we end up opening an inset
1489 if (selection.set())
1492 // CHECK fix this proper in 0.13
1493 // well, maybe 13.0 !!!!!!!!!
1495 // Following a ref shouldn't issue
1496 // a push on the undo-stack
1497 // anylonger, now that we have
1498 // keybindings for following
1499 // references and returning from
1500 // references. IMHO though, it
1501 // should be the inset's own business
1502 // to push or not push on the undo
1503 // stack. They don't *have* to
1504 // alter the document...
1506 // ...or maybe the SetCursorParUndo()
1507 // below isn't necessary at all anylonger?
1508 if (inset_hit->lyxCode() == Inset::REF_CODE)
1509 setCursorParUndo(bv);
1511 bv->owner()->message(inset_hit->editMessage());
1513 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1514 inset_hit->localDispatch(cmd1);
1520 case LFUN_SELFINSERT: {
1521 if (cmd.argument.empty())
1524 // Automatically delete the currently selected
1525 // text and replace it with what is being
1526 // typed in now. Depends on lyxrc settings
1527 // "auto_region_delete", which defaults to
1530 if (lyxrc.auto_region_delete) {
1531 if (selection.set()) {
1532 cutSelection(false, false);
1535 bv->haveSelection(false);
1538 bv->beforeChange(this);
1539 LyXFont const old_font(real_current_font);
1541 string::const_iterator cit = cmd.argument.begin();
1542 string::const_iterator end = cmd.argument.end();
1543 for (; cit != end; ++cit)
1544 bv->owner()->getIntl().getTransManager().
1545 TranslateAndInsert(*cit, this);
1548 selection.cursor = cursor;
1549 moveCursorUpdate(bv, false);
1551 // real_current_font.number can change so we need to
1552 // update the minibuffer
1553 if (old_font != real_current_font)
1554 bv->owner()->view_state_changed();
1558 case LFUN_HTMLURL: {
1559 InsetCommandParams p("htmlurl");
1560 string const data = InsetCommandMailer::params2string("url", p);
1561 bv->owner()->getDialogs().show("url", data, 0);
1566 InsetCommandParams p("url");
1567 string const data = InsetCommandMailer::params2string("url", p);
1568 bv->owner()->getDialogs().show("url", data, 0);
1574 case LFUN_INSET_LIST:
1575 case LFUN_INSET_THEOREM:
1576 case LFUN_INSET_CAPTION:
1578 case LFUN_INSERT_NOTE:
1579 case LFUN_INSERT_BIBITEM:
1580 case LFUN_INSET_ERT:
1581 case LFUN_INSET_FLOAT:
1582 case LFUN_INSET_FOOTNOTE:
1583 case LFUN_INSET_MARGINAL:
1584 case LFUN_INSET_MINIPAGE:
1585 case LFUN_INSET_OPTARG:
1586 case LFUN_INSET_WIDE_FLOAT:
1587 case LFUN_INSET_WRAP:
1588 case LFUN_TABULAR_INSERT:
1589 case LFUN_ENVIRONMENT_INSERT:
1590 // Open the inset, and move the current selection
1592 doInsertInset(this, cmd, true, true);
1595 case LFUN_INDEX_INSERT:
1596 // Just open the inset
1597 doInsertInset(this, cmd, true, false);
1600 case LFUN_INDEX_PRINT:
1601 case LFUN_TOC_INSERT:
1604 doInsertInset(this, cmd, false, false);
1608 return UNDISPATCHED;