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"
20 #include "bufferparams.h"
22 #include "ParagraphParameters.h"
28 #include "support/lstrings.h"
29 #include "frontends/LyXView.h"
30 #include "frontends/screen.h"
31 #include "frontends/Dialogs.h"
32 #include "insets/insetspecialchar.h"
33 #include "insets/insettext.h"
34 #include "insets/insetquotes.h"
35 #include "insets/insetcommand.h"
36 #include "insets/insetnewline.h"
37 #include "undo_funcs.h"
38 #include "text_funcs.h"
48 extern string current_layout;
49 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
51 // the selection possible is needed, that only motion events are
52 // used, where the bottom press event was on the drawing area too
53 bool selection_possible = false;
58 void moveCursorUpdate(BufferView * bv, bool selecting)
60 LyXText * lt = bv->getLyXText();
62 if (selecting || lt->selection.mark()) {
65 bv->updateInset(lt->inset_owner);
69 if (!lt->isInInset()) {
70 bv->update(lt, BufferView::SELECT);
72 } else if (bv->text->refreshStatus() != LyXText::REFRESH_NONE) {
73 bv->theLockingInset()->hideInsetCursor(bv);
74 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 Paragraph /*const*/ & 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,
207 bv()->beforeChange(this);
211 if (same_content && 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)
369 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
370 if (!bv->insertInset(new_inset))
373 bv->updateInset(new_inset);
377 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
378 bool edit, bool pastesel)
380 Inset * inset = createInset(cmd);
381 BufferView * bv = cmd.view();
385 if (lt->selection.set()) {
386 lt->cutSelection(true, false);
389 if (bv->insertInset(inset)) {
392 if (gotsel && pastesel)
393 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
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, &*boost::next(tmp));
422 tmp->params().startOfAppendix(false);
424 setHeightOfRow(getRow(tmp, 0, tmpy));
429 setUndo(bv, Undo::EDIT, &*pit, &*boost::next(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 tmpinset->edit(bv, !is_rtl);
621 // This is soooo ugly. Isn`t it possible to make
623 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
624 if (!selection.mark())
625 bv->beforeChange(this);
627 LyXCursor const cur = cursor;
630 if ((is_rtl || cur != cursor) && // only if really moved!
631 cursor.pos() < cursor.par()->size() &&
632 cursor.par()->isInset(cursor.pos()) &&
633 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
634 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
635 cmd.message(tmpinset->editMessage());
636 tmpinset->edit(bv, is_rtl);
646 if (!selection.mark())
647 bv->beforeChange(this);
648 bv->update(this, BufferView::UPDATE);
654 if (!selection.mark())
655 bv->beforeChange(this);
656 bv->update(this, BufferView::UPDATE);
661 case LFUN_UP_PARAGRAPH:
662 if (!selection.mark())
663 bv->beforeChange(this);
664 bv->update(this, BufferView::UPDATE);
669 case LFUN_DOWN_PARAGRAPH:
670 if (!selection.mark())
671 bv->beforeChange(this);
672 bv->update(this, BufferView::UPDATE);
673 cursorDownParagraph();
674 finishChange(bv, false);
678 if (!selection.mark())
679 bv->beforeChange(this);
680 bv->update(this, BufferView::UPDATE);
682 finishChange(bv, false);
685 // moveCursorUpdate(bv, false, false);
686 // owner_->view_state_changed();
690 if (!selection.mark())
691 bv->beforeChange(this);
692 bv->update(this, BufferView::UPDATE);
694 finishChange(bv, false);
698 if (!selection.mark())
699 bv->beforeChange(this);
702 finishChange(bv, false);
706 if (!selection.mark())
707 bv->beforeChange(this);
710 finishChange(bv, false);
713 case LFUN_BREAKLINE: {
714 lyx::pos_type body = cursor.par()->beginningOfBody();
716 // Not allowed by LaTeX (labels or empty par)
717 if (cursor.pos() <= body)
720 bv->beforeChange(this);
721 insertInset(new InsetNewline);
723 setCursor(cursor.par(), cursor.pos());
724 moveCursorUpdate(bv, false);
729 if (!selection.set()) {
731 selection.cursor = cursor;
733 // It is possible to make it a lot faster still
734 // just comment out the line below...
738 cutSelection(bv, true);
741 moveCursorUpdate(bv, false);
742 bv->owner()->view_state_changed();
746 case LFUN_DELETE_SKIP:
747 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
748 if (!selection.set()) {
749 LyXCursor cur = cursor;
750 if (cur.pos() == cur.par()->size()) {
754 && !(cur.par()->params().spaceTop()
755 == VSpace (VSpace::NONE))) {
757 cur.par()->params().lineTop(),
758 cur.par()->params().lineBottom(),
759 cur.par()->params().pagebreakTop(),
760 cur.par()->params().pagebreakBottom(),
761 VSpace(VSpace::NONE),
762 cur.par()->params().spaceBottom(),
763 cur.par()->params().spacing(),
764 cur.par()->params().align(),
765 cur.par()->params().labelWidthString(), 0);
771 selection.cursor = cursor;
775 selection.cursor = cursor;
779 cutSelection(bv, true);
786 if (!selection.set()) {
787 if (bv->owner()->getIntl().getTransManager().backspace()) {
789 selection.cursor = cursor;
791 // It is possible to make it a lot faster still
792 // just comment out the line below...
797 cutSelection(bv, true);
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(bv, true);
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(cmd.argument.c_str());
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_TOGGLE:
922 bv->beforeChange(this);
929 case LFUN_PROTECTEDSPACE:
930 if (cursor.par()->layout()->free_spacing) {
934 specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
936 moveCursorUpdate(bv, false);
939 case LFUN_HYPHENATION:
940 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
943 case LFUN_LIGATURE_BREAK:
944 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
948 specialChar(this, bv, InsetSpecialChar::LDOTS);
951 case LFUN_END_OF_SENTENCE:
952 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
955 case LFUN_MENU_SEPARATOR:
956 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
960 bv->beforeChange(this);
962 selection.cursor = cursor;
963 cmd.message(N_("Mark off"));
967 bv->beforeChange(this);
968 selection.mark(true);
970 selection.cursor = cursor;
971 cmd.message(N_("Mark on"));
975 bv->beforeChange(this);
976 if (selection.mark()) {
978 cmd.message(N_("Mark removed"));
980 selection.mark(true);
982 cmd.message(N_("Mark set"));
984 selection.cursor = cursor;
987 case LFUN_UPCASE_WORD:
989 changeCase(LyXText::text_uppercase);
991 bv->updateInset(inset_owner);
995 case LFUN_LOWCASE_WORD:
997 changeCase(LyXText::text_lowercase);
999 bv->updateInset(inset_owner);
1003 case LFUN_CAPITALIZE_WORD:
1005 changeCase(LyXText::text_capitalization);
1007 bv->updateInset(inset_owner);
1011 case LFUN_TRANSPOSE_CHARS:
1013 transposeChars(*this, cursor);
1015 bv->updateInset(inset_owner);
1020 cmd.message(_("Paste"));
1022 // clear the selection
1023 bv->toggleSelection();
1027 clearSelection(); // bug 393
1035 cutSelection(bv, 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(cmd.argument.c_str());
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);
1125 if (!change_layout && selection.set() &&
1126 selection.start.par() != selection.end.par())
1128 ParagraphList::iterator spit = selection.start.par();
1129 ParagraphList::iterator epit = boost::next(selection.end.par());
1130 while (spit != epit) {
1131 if (spit->layout()->name() != current_layout) {
1132 change_layout = true;
1138 if (change_layout) {
1140 current_layout = layout;
1143 bv->owner()->setLayout(layout);
1150 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(Inset::ERROR_CODE, false);
1174 gotoInset(Inset::NOTE_CODE, false);
1177 case LFUN_REFERENCE_GOTO:
1179 vector<Inset::Code> tmp;
1180 tmp.push_back(Inset::LABEL_CODE);
1181 tmp.push_back(Inset::REF_CODE);
1182 gotoInset(tmp, true);
1187 ParagraphList::iterator pit = cursor.par();
1188 lyx::pos_type pos = cursor.pos();
1192 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1195 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 time_t now_time_t = time(NULL);
1210 struct tm * now_tm = localtime(&now_time_t);
1211 setlocale(LC_TIME, "");
1213 if (!cmd.argument.empty())
1216 arg = lyxrc.date_insert_format;
1218 int const datetmp_len =
1219 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1221 for (int i = 0; i < datetmp_len; i++) {
1222 insertChar(datetmp[i]);
1225 selection.cursor = cursor;
1226 moveCursorUpdate(bv, false);
1230 case LFUN_MOUSE_TRIPLE:
1233 if (!isInInset() && bv->theLockingInset())
1235 if (cmd.button() == mouse_button::button1) {
1237 bv->screen().hideCursor();
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().hideCursor();
1259 bv->screen().toggleSelection(this, bv);
1260 selectWord(LyXText::WHOLE_WORD_STRICT);
1261 bv->screen().toggleSelection(this, bv, false);
1263 selectWord(LyXText::WHOLE_WORD_STRICT);
1266 bv->haveSelection(selection.set());
1270 case LFUN_MOUSE_MOTION:
1272 // Only use motion with button 1
1273 //if (ev.button() != mouse_button::button1)
1279 // Check for inset locking
1280 if (bv->theLockingInset()) {
1281 Inset * tli = bv->theLockingInset();
1282 LyXCursor cursor = bv->text->cursor;
1283 LyXFont font = bv->text->getFont(bv->buffer(),
1284 cursor.par(), cursor.pos());
1285 int width = tli->width(bv, font);
1286 int inset_x = font.isVisibleRightToLeft()
1287 ? cursor.ix() - width : cursor.ix();
1288 int start_x = inset_x + tli->scroll();
1289 FuncRequest cmd1 = cmd;
1290 cmd1.x = cmd.x - start_x;
1291 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1292 tli->localDispatch(cmd1);
1296 // The test for not selection possible is needed, that only motion
1297 // events are used, where the bottom press event was on
1298 // the drawing area too
1299 if (!selection_possible) {
1300 lyxerr[Debug::ACTION]
1301 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1305 bv->screen().hideCursor();
1307 RowList::iterator cursorrow = bv->text->cursor.row();
1308 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->text->top_y());
1310 // sorry for this but I have a strange error that the y value jumps at
1311 // a certain point. This seems like an error in my xforms library or
1312 // in some other local environment, but I would like to leave this here
1313 // for the moment until I can remove this (Jug 20020418)
1314 if (y_before < bv->text->cursor.y())
1315 lyxerr << y_before << ':'
1316 << bv->text->cursor.y() << endl;
1318 // This is to allow jumping over large insets
1319 if (cursorrow == bv->text->cursor.row()) {
1320 if (cmd.y >= bv->workHeight())
1321 bv->text->cursorDown(false);
1323 bv->text->cursorUp(false);
1326 // Maybe an empty line was deleted
1327 if (!bv->text->selection.set())
1328 bv->update(BufferView::UPDATE);
1329 bv->text->setSelection();
1330 bv->screen().toggleToggle(bv->text, bv);
1336 // Single-click on work area
1337 case LFUN_MOUSE_PRESS:
1342 // ok ok, this is a hack (for xforms)
1343 // We shouldn't go further down as we really should only do the
1344 // scrolling and be done with this. Otherwise we may open some
1345 // dialogs (Jug 20020424).
1346 if (cmd.button() == mouse_button::button4) {
1347 bv->scroll(-lyxrc.wheel_jump);
1350 if (cmd.button() == mouse_button::button5) {
1351 bv->scroll(lyxrc.wheel_jump);
1357 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1359 // Middle button press pastes if we have a selection
1360 // We do this here as if the selection was inside an inset
1361 // it could get cleared on the unlocking of the inset so
1362 // we have to check this first
1363 bool paste_internally = false;
1364 if (cmd.button() == mouse_button::button2 && selection.set()) {
1365 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1366 paste_internally = true;
1369 int const screen_first = bv->text->top_y();
1371 if (bv->theLockingInset()) {
1372 // We are in inset locking mode
1374 // Check whether the inset was hit. If not reset mode,
1375 // otherwise give the event to the inset
1376 if (inset_hit == bv->theLockingInset()) {
1377 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1378 bv->theLockingInset()->localDispatch(cmd1);
1381 bv->unlockInset(bv->theLockingInset());
1385 selection_possible = true;
1386 bv->screen().hideCursor();
1388 // Clear the selection
1389 bv->screen().toggleSelection(bv->text, bv);
1390 bv->text->clearSelection();
1391 bv->text->fullRebreak();
1393 bv->updateScrollbar();
1395 // Single left click in math inset?
1396 if (isHighlyEditableInset(inset_hit)) {
1397 // Highly editable inset, like math
1398 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1399 selection_possible = false;
1400 bv->owner()->message(inset->editMessage());
1401 //inset->edit(bv, x, y, cmd.button());
1402 // We just have to lock the inset before calling a PressEvent on it!
1403 // we don't need the edit() call here! (Jug20020329)
1404 if (!bv->lockInset(inset))
1405 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1406 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1407 inset->localDispatch(cmd1);
1410 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1412 // Right click on a footnote flag opens float menu
1413 if (cmd.button() == mouse_button::button3) {
1414 selection_possible = false;
1418 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1419 bv->text->setCursorFromCoordinates(x, y + screen_first);
1421 bv->text->selection.cursor = bv->text->cursor;
1422 bv->text->cursor.x_fix(bv->text->cursor.x());
1424 if (bv->fitCursor())
1425 selection_possible = false;
1427 // Insert primary selection with middle mouse
1428 // if there is a local selection in the current buffer,
1430 if (cmd.button() == mouse_button::button2) {
1431 if (paste_internally)
1432 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1434 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1435 selection_possible = false;
1440 case LFUN_MOUSE_RELEASE:
1442 // do nothing if we used the mouse wheel
1446 if (cmd.button() == mouse_button::button4
1447 || cmd.button() == mouse_button::button5)
1450 // If we hit an inset, we have the inset coordinates in these
1451 // and inset_hit points to the inset. If we do not hit an
1452 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1455 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1457 if (bv->theLockingInset()) {
1458 // We are in inset locking mode.
1460 // LyX does a kind of work-area grabbing for insets.
1461 // Only a ButtonPress FuncRequest outside the inset will
1462 // force a insetUnlock.
1463 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1464 bv->theLockingInset()->localDispatch(cmd1);
1468 selection_possible = false;
1470 if (cmd.button() == mouse_button::button2)
1474 if (cmd.button() == mouse_button::button1)
1475 bv->haveSelection(selection.set());
1478 bv->owner()->view_state_changed();
1479 bv->owner()->updateMenubar();
1480 bv->owner()->updateToolbar();
1482 // Did we hit an editable inset?
1484 selection_possible = false;
1486 // if we reach this point with a selection, it
1487 // must mean we are currently selecting.
1488 // But we don't want to open the inset
1489 // because that is annoying for the user.
1490 // So just pretend we didn't hit it.
1491 // this is OK because a "kosher" ButtonRelease
1492 // will follow a ButtonPress that clears
1494 // Note this also fixes selection drawing
1495 // problems if we end up opening an inset
1496 if (selection.set())
1499 // CHECK fix this proper in 0.13
1500 // well, maybe 13.0 !!!!!!!!!
1502 // Following a ref shouldn't issue
1503 // a push on the undo-stack
1504 // anylonger, now that we have
1505 // keybindings for following
1506 // references and returning from
1507 // references. IMHO though, it
1508 // should be the inset's own business
1509 // to push or not push on the undo
1510 // stack. They don't *have* to
1511 // alter the document...
1513 // ...or maybe the SetCursorParUndo()
1514 // below isn't necessary at all anylonger?
1515 if (inset_hit->lyxCode() == Inset::REF_CODE)
1516 setCursorParUndo(bv);
1518 bv->owner()->message(inset_hit->editMessage());
1520 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1521 inset_hit->localDispatch(cmd1);
1527 case LFUN_SELFINSERT: {
1528 if (cmd.argument.empty())
1531 // Automatically delete the currently selected
1532 // text and replace it with what is being
1533 // typed in now. Depends on lyxrc settings
1534 // "auto_region_delete", which defaults to
1537 if (lyxrc.auto_region_delete) {
1538 if (selection.set()) {
1539 cutSelection(false, false);
1542 bv->haveSelection(false);
1545 bv->beforeChange(this);
1546 LyXFont const old_font(real_current_font);
1548 string::const_iterator cit = cmd.argument.begin();
1549 string::const_iterator end = cmd.argument.end();
1550 for (; cit != end; ++cit)
1551 bv->owner()->getIntl().getTransManager().
1552 TranslateAndInsert(*cit, this);
1555 selection.cursor = cursor;
1556 moveCursorUpdate(bv, false);
1558 // real_current_font.number can change so we need to
1559 // update the minibuffer
1560 if (old_font != real_current_font)
1561 bv->owner()->view_state_changed();
1565 case LFUN_HTMLURL: {
1566 InsetCommandParams p("htmlurl");
1567 string const data = InsetCommandMailer::params2string("url", p);
1568 bv->owner()->getDialogs().show("url", data, 0);
1573 InsetCommandParams p("url");
1574 string const data = InsetCommandMailer::params2string("url", p);
1575 bv->owner()->getDialogs().show("url", data, 0);
1581 case LFUN_INSET_LIST:
1582 case LFUN_INSET_THEOREM:
1583 case LFUN_INSET_CAPTION:
1585 case LFUN_INSERT_NOTE:
1586 case LFUN_INSERT_BIBITEM:
1587 case LFUN_INSET_ERT:
1588 case LFUN_INSET_FLOAT:
1589 case LFUN_INSET_FOOTNOTE:
1590 case LFUN_INSET_MARGINAL:
1591 case LFUN_INSET_MINIPAGE:
1592 case LFUN_INSET_OPTARG:
1593 case LFUN_INSET_WIDE_FLOAT:
1594 case LFUN_INSET_WRAP:
1595 case LFUN_TABULAR_INSERT:
1596 case LFUN_ENVIRONMENT_INSERT:
1597 // Open the inset, and move the current selection
1599 doInsertInset(this, cmd, true, true);
1602 case LFUN_INDEX_INSERT:
1603 // Just open the inset
1604 doInsertInset(this, cmd, true, false);
1607 case LFUN_INDEX_PRINT:
1608 case LFUN_PARENTINSERT:
1609 case LFUN_TOC_INSERT:
1612 doInsertInset(this, cmd, false, false);
1616 return UNDISPATCHED;