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"
46 extern string current_layout;
47 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
49 // the selection possible is needed, that only motion events are
50 // used, where the bottom press event was on the drawing area too
51 bool selection_possible = false;
56 void moveCursorUpdate(BufferView * bv, bool selecting)
58 LyXText * lt = bv->getLyXText();
60 if (selecting || lt->selection.mark()) {
63 bv->updateInset(lt->inset_owner, false);
67 if (!lt->isInInset()) {
69 // bv->update(lt, BufferView::SELECT|BufferView::FITCUR);
71 bv->update(lt, BufferView::SELECT|BufferView::FITCUR);
73 } else if (bv->text->status() != LyXText::UNCHANGED) {
74 bv->theLockingInset()->hideInsetCursor(bv);
75 bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
79 if (!lt->selection.set())
80 bv->haveSelection(false);
86 void finishChange(BufferView * bv, bool selecting = false)
89 moveCursorUpdate(bv, selecting);
90 bv->owner()->view_state_changed();
93 // check if the given co-ordinates are inside an inset at the
94 // given cursor, if one exists. If so, the inset is returned,
95 // and the co-ordinates are made relative. Otherwise, 0 is returned.
96 Inset * checkInset(BufferView * bv, LyXText & text,
97 LyXCursor const & cur, int & x, int & y)
99 lyx::pos_type const pos = cur.pos();
100 Paragraph /*const*/ & par = *cur.par();
102 if (pos >= par.size() || !par.isInset(pos))
105 Inset /*const*/ * inset = par.getInset(pos);
107 if (!isEditableInset(inset))
110 // get inset dimensions
111 lyx::Assert(par.getInset(pos));
113 LyXFont const & font = text.getFont(bv->buffer(), &par, pos);
115 int const width = inset->width(bv, font);
116 int const inset_x = font.isVisibleRightToLeft()
117 ? (cur.ix() - width) : cur.ix();
120 inset_x + inset->scroll(),
122 cur.iy() - inset->ascent(bv, font),
123 cur.iy() + inset->descent(bv, font)
126 if (!b.contained(x, y)) {
127 lyxerr[Debug::GUI] << "Missed inset at x,y "
129 << " box " << b << endl;
133 text.setCursor(&par, pos, true);
136 // The origin of an inset is on the baseline
137 y -= text.cursor.iy();
145 Inset * LyXText::checkInsetHit(int & x, int & y)
147 int y_tmp = y + top_y();
150 setCursorFromCoordinates(cur, x, y_tmp);
152 Inset * inset = checkInset(bv(), *this, cur, x, y_tmp);
158 // look at previous position
163 setCursor(cur, cur.par(), cur.pos() - 1, true);
165 inset = checkInset(bv(), *this, cur, x, y_tmp);
172 bool LyXText::gotoNextInset(vector<Inset::Code> const & codes,
173 string const & contents)
175 LyXCursor res = cursor;
178 if (res.pos() < res.par()->size() - 1) {
179 res.pos(res.pos() + 1);
181 res.par(res.par()->next());
185 } while (res.par() &&
186 !(res.par()->isInset(res.pos())
187 && (inset = res.par()->getInset(res.pos())) != 0
188 && find(codes.begin(), codes.end(), inset->lyxCode())
190 && (contents.empty() ||
191 static_cast<InsetCommand *>(
192 res.par()->getInset(res.pos()))->getContents()
196 setCursor(res.par(), res.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() != ownerParagraph()) {
220 LyXCursor tmp = cursor;
221 cursor.par(ownerParagraph());
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()
244 if (!cursor.row()->previous()) {
246 int new_y = bv()->text->top_y() - bv()->workHeight();
247 bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
248 bv()->updateScrollbar();
254 Row * 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()->previous()) {
286 setCursor(cur, cursor.row()->previous()->par(),
287 cursor.row()->previous()->pos(), false);
288 if (cur.y() > top_y()) {
292 bv()->updateScrollbar();
296 void LyXText::cursorNext()
298 if (!cursor.row()->next()) {
299 int y = cursor.y() - cursor.row()->baseline() +
300 cursor.row()->height();
301 if (y > top_y() + bv()->workHeight()) {
302 bv()->screen().draw(bv()->text, bv(), bv()->text->top_y() + bv()->workHeight());
303 bv()->updateScrollbar();
308 int y = top_y() + bv()->workHeight();
309 if (inset_owner && !top_y()) {
310 y -= (bv()->text->cursor.iy()
311 - bv()->text->top_y()
312 + bv()->theLockingInset()->insetInInsetY());
317 Row * cursorrow = cursor.row();
318 setCursorFromCoordinates(cursor.x_fix(), y);
319 // + bv->workHeight());
323 if (cursorrow == bv()->text->cursor.row()) {
324 // we have a row which is taller than the workarea. The
325 // simplest solution is to move to the next row instead.
328 // This is what we used to do, so we wouldn't skip right past
329 // tall rows, but it's not working right now.
331 new_y = bv->text->top_y() + bv->workHeight();
335 new_y = bv()->text->cursor.iy()
336 + bv()->theLockingInset()->insetInInsetY()
337 + y - cursor.row()->baseline();
339 new_y = cursor.y() - cursor.row()->baseline();
342 bv()->screen().draw(bv()->text, bv(), new_y);
343 if (cursor.row()->next()) {
345 setCursor(cur, cursor.row()->next()->par(),
346 cursor.row()->next()->pos(), false);
347 if (cur.y() < top_y() + bv()->workHeight()) {
351 bv()->updateScrollbar();
355 void LyXText::update(bool changed)
357 BufferView::UpdateCodes c = BufferView::SELECT | BufferView::FITCUR;
359 bv()->update(this, c | BufferView::CHANGE);
361 bv()->update(this, c);
366 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
370 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
371 if (!bv->insertInset(new_inset))
374 bv->updateInset(new_inset, true);
378 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
379 bool edit, bool pastesel)
381 Inset * inset = createInset(cmd);
382 BufferView * bv = cmd.view();
386 if (lt->selection.set()) {
387 lt->cutSelection(true, false);
390 if (bv->insertInset(inset)) {
393 if (gotsel && pastesel)
394 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
403 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
405 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
406 <<"] arg[" << cmd.argument << ']' << endl;
408 BufferView * bv = cmd.view();
410 switch (cmd.action) {
412 case LFUN_APPENDIX: {
413 Paragraph * par = cursor.par();
414 bool start = !par->params().startOfAppendix();
416 // ensure that we have only one start_of_appendix in this document
417 Paragraph * tmp = ownerParagraph();
418 for (; tmp; tmp = tmp->next()) {
419 if (tmp->params().startOfAppendix()) {
420 setUndo(bv, Undo::EDIT, tmp, tmp->next());
421 tmp->params().startOfAppendix(false);
423 setHeightOfRow(getRow(tmp, 0, tmpy));
428 setUndo(bv, Undo::EDIT, par, par->next());
429 par->params().startOfAppendix(start);
431 // we can set the refreshing parameters now
433 redoHeightOfParagraph();
435 setCursor(cursor.par(), cursor.pos());
440 case LFUN_DELETE_WORD_FORWARD:
441 bv->beforeChange(this);
448 case LFUN_DELETE_WORD_BACKWARD:
449 bv->beforeChange(this);
451 deleteWordBackward();
456 case LFUN_DELETE_LINE_FORWARD:
457 bv->beforeChange(this);
466 if (!selection.mark())
467 bv->beforeChange(this);
474 if (!selection.mark())
475 bv->beforeChange(this);
477 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
480 cursorRightOneWord();
485 if (!selection.mark())
486 bv->beforeChange(this);
488 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
489 cursorRightOneWord();
495 case LFUN_BEGINNINGBUF:
496 if (!selection.mark())
497 bv->beforeChange(this);
504 if (selection.mark())
505 bv->beforeChange(this);
513 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
517 finishChange(bv, true);
522 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
526 finishChange(bv, true);
532 finishChange(bv, true);
538 finishChange(bv, true);
541 case LFUN_UP_PARAGRAPHSEL:
544 finishChange(bv, true);
547 case LFUN_DOWN_PARAGRAPHSEL:
549 cursorDownParagraph();
550 finishChange(bv, true);
556 finishChange(bv, true);
562 finishChange(bv, true);
568 finishChange(bv, true);
574 finishChange(bv, true);
577 case LFUN_WORDRIGHTSEL:
579 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
582 cursorRightOneWord();
583 finishChange(bv, true);
586 case LFUN_WORDLEFTSEL:
588 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
589 cursorRightOneWord();
592 finishChange(bv, true);
599 getWord(cur1, cur2, WHOLE_WORD);
600 setCursor(cur1.par(), cur1.pos());
601 bv->beforeChange(this);
602 setCursor(cur2.par(), cur2.pos());
603 finishChange(bv, true);
608 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
609 if (!selection.mark())
610 bv->beforeChange(this);
614 if (cursor.pos() < cursor.par()->size()
615 && cursor.par()->isInset(cursor.pos())
616 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
617 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
618 cmd.message(tmpinset->editMessage());
619 tmpinset->edit(bv, !is_rtl);
629 // This is soooo ugly. Isn`t it possible to make
631 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
632 if (!selection.mark())
633 bv->beforeChange(this);
635 LyXCursor const cur = cursor;
638 if ((is_rtl || cur != cursor) && // only if really moved!
639 cursor.pos() < cursor.par()->size() &&
640 cursor.par()->isInset(cursor.pos()) &&
641 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
642 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
643 cmd.message(tmpinset->editMessage());
644 tmpinset->edit(bv, is_rtl);
654 if (!selection.mark())
655 bv->beforeChange(this);
656 bv->update(this, BufferView::UPDATE);
662 if (!selection.mark())
663 bv->beforeChange(this);
664 bv->update(this, BufferView::UPDATE);
669 case LFUN_UP_PARAGRAPH:
670 if (!selection.mark())
671 bv->beforeChange(this);
672 bv->update(this, BufferView::UPDATE);
677 case LFUN_DOWN_PARAGRAPH:
678 if (!selection.mark())
679 bv->beforeChange(this);
680 bv->update(this, BufferView::UPDATE);
681 cursorDownParagraph();
682 finishChange(bv, false);
686 if (!selection.mark())
687 bv->beforeChange(this);
688 bv->update(this, BufferView::UPDATE);
690 finishChange(bv, false);
693 // moveCursorUpdate(bv, false, false);
694 // owner_->view_state_changed();
698 if (!selection.mark())
699 bv->beforeChange(this);
700 bv->update(this, BufferView::UPDATE);
702 finishChange(bv, false);
706 if (!selection.mark())
707 bv->beforeChange(this);
710 finishChange(bv, false);
714 if (!selection.mark())
715 bv->beforeChange(this);
718 finishChange(bv, false);
721 case LFUN_BREAKLINE: {
722 lyx::pos_type body = cursor.par()->beginningOfBody();
724 // Not allowed by LaTeX (labels or empty par)
725 if (cursor.pos() <= body)
728 bv->beforeChange(this);
729 insertInset(new InsetNewline);
731 setCursor(cursor.par(), cursor.pos());
732 moveCursorUpdate(bv, false);
737 if (!selection.set()) {
739 selection.cursor = cursor;
741 // It is possible to make it a lot faster still
742 // just comment out the line below...
746 cutSelection(bv, true);
749 moveCursorUpdate(bv, false);
750 bv->owner()->view_state_changed();
754 case LFUN_DELETE_SKIP:
755 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
756 if (!selection.set()) {
757 LyXCursor cur = cursor;
758 if (cur.pos() == cur.par()->size()) {
762 && !(cur.par()->params().spaceTop()
763 == VSpace (VSpace::NONE))) {
765 cur.par()->params().lineTop(),
766 cur.par()->params().lineBottom(),
767 cur.par()->params().pagebreakTop(),
768 cur.par()->params().pagebreakBottom(),
769 VSpace(VSpace::NONE),
770 cur.par()->params().spaceBottom(),
771 cur.par()->params().spacing(),
772 cur.par()->params().align(),
773 cur.par()->params().labelWidthString(), 0);
779 selection.cursor = cursor;
783 selection.cursor = cursor;
787 cutSelection(bv, true);
794 if (!selection.set()) {
795 if (bv->owner()->getIntl().getTransManager().backspace()) {
797 selection.cursor = cursor;
799 // It is possible to make it a lot faster still
800 // just comment out the line below...
805 cutSelection(bv, true);
808 bv->owner()->view_state_changed();
812 case LFUN_BACKSPACE_SKIP:
813 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
814 if (!selection.set()) {
815 LyXCursor cur = cursor;
817 && !(cur.par()->params().spaceTop()
818 == VSpace (VSpace::NONE))) {
820 cur.par()->params().lineTop(),
821 cur.par()->params().lineBottom(),
822 cur.par()->params().pagebreakTop(),
823 cur.par()->params().pagebreakBottom(),
824 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
825 cur.par()->params().spacing(),
826 cur.par()->params().align(),
827 cur.par()->params().labelWidthString(), 0);
830 selection.cursor = cur;
834 cutSelection(bv, true);
839 case LFUN_BREAKPARAGRAPH:
840 bv->beforeChange(this);
841 breakParagraph(bv->buffer()->paragraphs, 0);
843 selection.cursor = cursor;
845 bv->owner()->view_state_changed();
848 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
849 bv->beforeChange(this);
850 breakParagraph(bv->buffer()->paragraphs, 1);
852 selection.cursor = cursor;
854 bv->owner()->view_state_changed();
857 case LFUN_BREAKPARAGRAPH_SKIP: {
858 // When at the beginning of a paragraph, remove
859 // indentation and add a "defskip" at the top.
860 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
861 LyXCursor cur = cursor;
862 bv->beforeChange(this);
863 if (cur.pos() == 0) {
864 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
866 cur.par()->params().lineTop(),
867 cur.par()->params().lineBottom(),
868 cur.par()->params().pagebreakTop(),
869 cur.par()->params().pagebreakBottom(),
870 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
871 cur.par()->params().spacing(),
872 cur.par()->params().align(),
873 cur.par()->params().labelWidthString(), 1);
878 breakParagraph(bv->buffer()->paragraphs, 0);
882 selection.cursor = cur;
884 bv->owner()->view_state_changed();
888 case LFUN_PARAGRAPH_SPACING: {
889 Paragraph * par = cursor.par();
890 Spacing::Space cur_spacing = par->params().spacing().getSpace();
891 float cur_value = 1.0;
892 if (cur_spacing == Spacing::Other)
893 cur_value = par->params().spacing().getValue();
895 istringstream is(cmd.argument.c_str());
898 Spacing::Space new_spacing = cur_spacing;
899 float new_value = cur_value;
901 lyxerr << "Missing argument to `paragraph-spacing'"
903 } else if (tmp == "single") {
904 new_spacing = Spacing::Single;
905 } else if (tmp == "onehalf") {
906 new_spacing = Spacing::Onehalf;
907 } else if (tmp == "double") {
908 new_spacing = Spacing::Double;
909 } else if (tmp == "other") {
910 new_spacing = Spacing::Other;
913 lyxerr << "new_value = " << tmpval << endl;
916 } else if (tmp == "default") {
917 new_spacing = Spacing::Default;
919 lyxerr << _("Unknown spacing argument: ")
920 << cmd.argument << endl;
922 if (cur_spacing != new_spacing || cur_value != new_value) {
923 par->params().spacing(Spacing(new_spacing, new_value));
930 case LFUN_INSET_TOGGLE:
932 bv->beforeChange(this);
939 case LFUN_PROTECTEDSPACE:
940 if (cursor.par()->layout()->free_spacing) {
944 specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
946 moveCursorUpdate(bv, false);
949 case LFUN_HYPHENATION:
950 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
953 case LFUN_LIGATURE_BREAK:
954 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
958 specialChar(this, bv, InsetSpecialChar::LDOTS);
961 case LFUN_END_OF_SENTENCE:
962 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
965 case LFUN_MENU_SEPARATOR:
966 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
970 bv->beforeChange(this);
972 selection.cursor = cursor;
973 cmd.message(N_("Mark off"));
977 bv->beforeChange(this);
978 selection.mark(true);
980 selection.cursor = cursor;
981 cmd.message(N_("Mark on"));
985 bv->beforeChange(this);
986 if (selection.mark()) {
988 cmd.message(N_("Mark removed"));
990 selection.mark(true);
992 cmd.message(N_("Mark set"));
994 selection.cursor = cursor;
997 case LFUN_UPCASE_WORD:
999 changeCase(LyXText::text_uppercase);
1001 bv->updateInset(inset_owner, true);
1005 case LFUN_LOWCASE_WORD:
1007 changeCase(LyXText::text_lowercase);
1009 bv->updateInset(inset_owner, true);
1013 case LFUN_CAPITALIZE_WORD:
1015 changeCase(LyXText::text_capitalization);
1017 bv->updateInset(inset_owner, true);
1021 case LFUN_TRANSPOSE_CHARS:
1025 bv->updateInset(inset_owner, true);
1030 cmd.message(_("Paste"));
1032 // clear the selection
1033 bv->toggleSelection();
1037 clearSelection(); // bug 393
1046 cutSelection(bv, true);
1048 cmd.message(_("Cut"));
1053 cmd.message(_("Copy"));
1056 case LFUN_BEGINNINGBUFSEL:
1058 return UNDISPATCHED;
1061 finishChange(bv, true);
1064 case LFUN_ENDBUFSEL:
1066 return UNDISPATCHED;
1069 finishChange(bv, true);
1073 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1079 istringstream is(cmd.argument.c_str());
1082 lyxerr << "SETXY: Could not parse coordinates in '"
1083 << cmd.argument << std::endl;
1085 setCursorFromCoordinates(x, y);
1090 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1092 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1098 case LFUN_GETLAYOUT:
1099 cmd.message(tostr(cursor.par()->layout()));
1103 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1104 << cmd.argument << endl;
1106 // This is not the good solution to the empty argument
1107 // problem, but it will hopefully suffice for 1.2.0.
1108 // The correct solution would be to augument the
1109 // function list/array with information about what
1110 // functions needs arguments and their type.
1111 if (cmd.argument.empty()) {
1112 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1116 // Derive layout number from given argument (string)
1117 // and current buffer's textclass (number)
1118 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1119 bool hasLayout = tclass.hasLayout(cmd.argument);
1120 string layout = cmd.argument;
1122 // If the entry is obsolete, use the new one instead.
1124 string const & obs = tclass[layout]->obsoleted_by();
1130 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1135 bool change_layout = (current_layout != layout);
1136 if (!change_layout && selection.set() &&
1137 selection.start.par() != selection.end.par())
1139 Paragraph * spar = selection.start.par();
1140 Paragraph * epar = selection.end.par()->next();
1141 while (spar != epar) {
1142 if (spar->layout()->name() != current_layout) {
1143 change_layout = true;
1146 spar = spar->next();
1149 if (change_layout) {
1151 current_layout = layout;
1154 bv->owner()->setLayout(layout);
1161 case LFUN_PASTESELECTION: {
1165 // this was originally a beforeChange(bv->text), i.e
1166 // the outermost LyXText!
1167 bv->beforeChange(this);
1168 string const clip = bv->getClipboard();
1169 if (!clip.empty()) {
1170 if (cmd.argument == "paragraph")
1171 insertStringAsParagraphs(clip);
1173 insertStringAsLines(clip);
1180 case LFUN_GOTOERROR:
1181 gotoInset(Inset::ERROR_CODE, false);
1185 gotoInset(Inset::NOTE_CODE, false);
1188 case LFUN_REFERENCE_GOTO:
1190 vector<Inset::Code> tmp;
1191 tmp.push_back(Inset::LABEL_CODE);
1192 tmp.push_back(Inset::REF_CODE);
1193 gotoInset(tmp, true);
1198 Paragraph const * par = cursor.par();
1199 lyx::pos_type pos = cursor.pos();
1203 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1206 c = par->getChar(pos - 1);
1209 LyXLayout_ptr const & style = par->layout();
1211 if (style->pass_thru ||
1212 par->getFontSettings(bv->buffer()->params,
1213 pos).language()->lang() == "hebrew" ||
1214 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1215 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1219 case LFUN_DATE_INSERT: {
1220 time_t now_time_t = time(NULL);
1221 struct tm * now_tm = localtime(&now_time_t);
1222 setlocale(LC_TIME, "");
1224 if (!cmd.argument.empty())
1227 arg = lyxrc.date_insert_format;
1229 int const datetmp_len =
1230 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1232 for (int i = 0; i < datetmp_len; i++) {
1233 insertChar(datetmp[i]);
1236 selection.cursor = cursor;
1237 moveCursorUpdate(bv, false);
1241 case LFUN_MOUSE_TRIPLE:
1244 if (!isInInset() && bv->theLockingInset())
1246 if (cmd.button() == mouse_button::button1) {
1248 bv->screen().hideCursor();
1249 bv->screen().toggleSelection(this, bv);
1252 selection.cursor = cursor;
1256 bv->screen().toggleSelection(this, bv, false);
1258 bv->haveSelection(selection.set());
1262 case LFUN_MOUSE_DOUBLE:
1265 if (!isInInset() && bv->theLockingInset())
1267 if (cmd.button() == mouse_button::button1) {
1269 bv->screen().hideCursor();
1270 bv->screen().toggleSelection(this, bv);
1271 selectWord(LyXText::WHOLE_WORD_STRICT);
1272 bv->screen().toggleSelection(this, bv, false);
1274 selectWord(LyXText::WHOLE_WORD_STRICT);
1277 bv->haveSelection(selection.set());
1281 case LFUN_MOUSE_MOTION:
1283 // Only use motion with button 1
1284 //if (ev.button() != mouse_button::button1)
1290 // Check for inset locking
1291 if (bv->theLockingInset()) {
1292 Inset * tli = bv->theLockingInset();
1293 LyXCursor cursor = bv->text->cursor;
1294 LyXFont font = bv->text->getFont(bv->buffer(),
1295 cursor.par(), cursor.pos());
1296 int width = tli->width(bv, font);
1297 int inset_x = font.isVisibleRightToLeft()
1298 ? cursor.ix() - width : cursor.ix();
1299 int start_x = inset_x + tli->scroll();
1300 FuncRequest cmd1 = cmd;
1301 cmd1.x = cmd.x - start_x;
1302 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1303 tli->localDispatch(cmd1);
1307 // The test for not selection possible is needed, that only motion
1308 // events are used, where the bottom press event was on
1309 // the drawing area too
1310 if (!selection_possible) {
1311 lyxerr[Debug::ACTION]
1312 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1316 bv->screen().hideCursor();
1318 Row * cursorrow = bv->text->cursor.row();
1319 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->text->top_y());
1321 // sorry for this but I have a strange error that the y value jumps at
1322 // a certain point. This seems like an error in my xforms library or
1323 // in some other local environment, but I would like to leave this here
1324 // for the moment until I can remove this (Jug 20020418)
1325 if (y_before < bv->text->cursor.y())
1326 lyxerr << y_before << ':'
1327 << bv->text->cursor.y() << endl;
1329 // This is to allow jumping over large insets
1330 if (cursorrow == bv->text->cursor.row()) {
1331 if (cmd.y >= bv->workHeight())
1332 bv->text->cursorDown(false);
1334 bv->text->cursorUp(false);
1337 // Maybe an empty line was deleted
1338 if (!bv->text->selection.set())
1339 bv->update(bv->text, BufferView::UPDATE);
1340 bv->text->setSelection();
1341 bv->screen().toggleToggle(bv->text, bv);
1347 // Single-click on work area
1348 case LFUN_MOUSE_PRESS:
1353 // ok ok, this is a hack (for xforms)
1354 // We shouldn't go further down as we really should only do the
1355 // scrolling and be done with this. Otherwise we may open some
1356 // dialogs (Jug 20020424).
1357 if (cmd.button() == mouse_button::button4) {
1358 bv->scroll(-lyxrc.wheel_jump);
1361 if (cmd.button() == mouse_button::button5) {
1362 bv->scroll(lyxrc.wheel_jump);
1368 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1370 // Middle button press pastes if we have a selection
1371 // We do this here as if the selection was inside an inset
1372 // it could get cleared on the unlocking of the inset so
1373 // we have to check this first
1374 bool paste_internally = false;
1375 if (cmd.button() == mouse_button::button2 && selection.set()) {
1376 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1377 paste_internally = true;
1380 int const screen_first = bv->text->top_y();
1382 if (bv->theLockingInset()) {
1383 // We are in inset locking mode
1385 // Check whether the inset was hit. If not reset mode,
1386 // otherwise give the event to the inset
1387 if (inset_hit == bv->theLockingInset()) {
1388 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1389 bv->theLockingInset()->localDispatch(cmd1);
1392 bv->unlockInset(bv->theLockingInset());
1396 selection_possible = true;
1397 bv->screen().hideCursor();
1399 // Clear the selection
1400 bv->screen().toggleSelection(bv->text, bv);
1401 bv->text->clearSelection();
1402 bv->text->fullRebreak();
1404 bv->updateScrollbar();
1406 // Single left click in math inset?
1407 if (isHighlyEditableInset(inset_hit)) {
1408 // Highly editable inset, like math
1409 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1410 selection_possible = false;
1411 bv->owner()->message(inset->editMessage());
1412 //inset->edit(bv, x, y, cmd.button());
1413 // We just have to lock the inset before calling a PressEvent on it!
1414 // we don't need the edit() call here! (Jug20020329)
1415 if (!bv->lockInset(inset))
1416 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1417 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1418 inset->localDispatch(cmd1);
1421 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1423 // Right click on a footnote flag opens float menu
1424 if (cmd.button() == mouse_button::button3) {
1425 selection_possible = false;
1429 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1430 bv->text->setCursorFromCoordinates(x, y + screen_first);
1432 bv->text->selection.cursor = bv->text->cursor;
1433 bv->text->cursor.x_fix(bv->text->cursor.x());
1435 if (bv->fitCursor())
1436 selection_possible = false;
1438 // Insert primary selection with middle mouse
1439 // if there is a local selection in the current buffer,
1441 if (cmd.button() == mouse_button::button2) {
1442 if (paste_internally)
1443 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1445 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1446 selection_possible = false;
1451 case LFUN_MOUSE_RELEASE:
1453 // do nothing if we used the mouse wheel
1457 if (cmd.button() == mouse_button::button4
1458 || cmd.button() == mouse_button::button5)
1461 // If we hit an inset, we have the inset coordinates in these
1462 // and inset_hit points to the inset. If we do not hit an
1463 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1466 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1468 if (bv->theLockingInset()) {
1469 // We are in inset locking mode.
1471 // LyX does a kind of work-area grabbing for insets.
1472 // Only a ButtonPress FuncRequest outside the inset will
1473 // force a insetUnlock.
1474 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1475 bv->theLockingInset()->localDispatch(cmd1);
1479 selection_possible = false;
1481 if (cmd.button() == mouse_button::button2)
1485 if (cmd.button() == mouse_button::button1)
1486 bv->haveSelection(selection.set());
1489 bv->owner()->view_state_changed();
1490 bv->owner()->updateMenubar();
1491 bv->owner()->updateToolbar();
1493 // Did we hit an editable inset?
1495 selection_possible = false;
1497 // if we reach this point with a selection, it
1498 // must mean we are currently selecting.
1499 // But we don't want to open the inset
1500 // because that is annoying for the user.
1501 // So just pretend we didn't hit it.
1502 // this is OK because a "kosher" ButtonRelease
1503 // will follow a ButtonPress that clears
1505 // Note this also fixes selection drawing
1506 // problems if we end up opening an inset
1507 if (selection.set())
1510 // CHECK fix this proper in 0.13
1511 // well, maybe 13.0 !!!!!!!!!
1513 // Following a ref shouldn't issue
1514 // a push on the undo-stack
1515 // anylonger, now that we have
1516 // keybindings for following
1517 // references and returning from
1518 // references. IMHO though, it
1519 // should be the inset's own business
1520 // to push or not push on the undo
1521 // stack. They don't *have* to
1522 // alter the document...
1524 // ...or maybe the SetCursorParUndo()
1525 // below isn't necessary at all anylonger?
1526 if (inset_hit->lyxCode() == Inset::REF_CODE)
1527 setCursorParUndo(bv);
1529 bv->owner()->message(inset_hit->editMessage());
1531 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1532 inset_hit->localDispatch(cmd1);
1538 case LFUN_SELFINSERT: {
1539 if (cmd.argument.empty())
1542 // Automatically delete the currently selected
1543 // text and replace it with what is being
1544 // typed in now. Depends on lyxrc settings
1545 // "auto_region_delete", which defaults to
1548 if (lyxrc.auto_region_delete) {
1549 if (selection.set()) {
1550 cutSelection(false, false);
1553 bv->haveSelection(false);
1556 bv->beforeChange(this);
1557 LyXFont const old_font(real_current_font);
1559 string::const_iterator cit = cmd.argument.begin();
1560 string::const_iterator end = cmd.argument.end();
1561 for (; cit != end; ++cit)
1562 bv->owner()->getIntl().getTransManager().
1563 TranslateAndInsert(*cit, this);
1566 selection.cursor = cursor;
1567 moveCursorUpdate(bv, false);
1569 // real_current_font.number can change so we need to
1570 // update the minibuffer
1571 if (old_font != real_current_font)
1572 bv->owner()->view_state_changed();
1576 case LFUN_HTMLURL: {
1577 InsetCommandParams p("htmlurl");
1578 string const data = InsetCommandMailer::params2string("url", p);
1579 bv->owner()->getDialogs().show("url", data, 0);
1584 InsetCommandParams p("url");
1585 string const data = InsetCommandMailer::params2string("url", p);
1586 bv->owner()->getDialogs().show("url", data, 0);
1592 case LFUN_INSET_LIST:
1593 case LFUN_INSET_THEOREM:
1594 case LFUN_INSET_CAPTION:
1596 case LFUN_INSERT_NOTE:
1597 case LFUN_INSERT_BIBITEM:
1598 case LFUN_INSET_ERT:
1599 case LFUN_INSET_FLOAT:
1600 case LFUN_INSET_FOOTNOTE:
1601 case LFUN_INSET_MARGINAL:
1602 case LFUN_INSET_MINIPAGE:
1603 case LFUN_INSET_OPTARG:
1604 case LFUN_INSET_WIDE_FLOAT:
1605 case LFUN_INSET_WRAP:
1606 case LFUN_TABULAR_INSERT:
1607 case LFUN_ENVIRONMENT_INSERT:
1608 // Open the inset, and move the current selection
1610 doInsertInset(this, cmd, true, true);
1613 case LFUN_INDEX_INSERT:
1614 // Just open the inset
1615 doInsertInset(this, cmd, true, false);
1618 case LFUN_INDEX_PRINT:
1619 case LFUN_PARENTINSERT:
1620 case LFUN_TOC_INSERT:
1623 doInsertInset(this, cmd, false, false);
1627 return UNDISPATCHED;