1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2002 The LyX Team.
9 * ====================================================== */
15 #include "paragraph.h"
16 #include "BufferView.h"
17 #include "funcrequest.h"
21 #include "bufferparams.h"
23 #include "ParagraphParameters.h"
29 #include "support/tostr.h"
30 #include "support/lstrings.h"
31 #include "frontends/LyXView.h"
32 #include "frontends/screen.h"
33 #include "frontends/Dialogs.h"
34 #include "insets/insetspecialchar.h"
35 #include "insets/insettext.h"
36 #include "insets/insetquotes.h"
37 #include "insets/insetcommand.h"
38 #include "insets/insetnewline.h"
39 #include "undo_funcs.h"
40 #include "text_funcs.h"
51 extern string current_layout;
52 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
54 // the selection possible is needed, that only motion events are
55 // used, where the bottom press event was on the drawing area too
56 bool selection_possible = false;
61 void moveCursorUpdate(BufferView * bv, bool selecting)
63 LyXText * lt = bv->getLyXText();
65 if (selecting || lt->selection.mark()) {
68 bv->updateInset(lt->inset_owner);
72 if (!lt->isInInset()) {
73 bv->update(lt, BufferView::SELECT);
74 } else if (bv->text->refreshStatus() != LyXText::REFRESH_NONE) {
75 bv->update(BufferView::SELECT);
78 if (!lt->selection.set())
79 bv->haveSelection(false);
85 void finishChange(BufferView * bv, bool selecting = false)
88 moveCursorUpdate(bv, selecting);
89 bv->owner()->view_state_changed();
92 // check if the given co-ordinates are inside an inset at the
93 // given cursor, if one exists. If so, the inset is returned,
94 // and the co-ordinates are made relative. Otherwise, 0 is returned.
95 Inset * checkInset(BufferView * bv, LyXText & text,
96 LyXCursor const & cur, int & x, int & y)
98 lyx::pos_type const pos = cur.pos();
99 ParagraphList::iterator par = cur.par();
101 if (pos >= par->size() || !par->isInset(pos))
104 Inset /*const*/ * inset = par->getInset(pos);
106 if (!isEditableInset(inset))
109 // get inset dimensions
110 lyx::Assert(par->getInset(pos));
112 LyXFont const & font = text.getFont(bv->buffer(), par, pos);
114 int const width = inset->width(bv, font);
115 int const inset_x = font.isVisibleRightToLeft()
116 ? (cur.ix() - width) : cur.ix();
119 inset_x + inset->scroll(),
121 cur.iy() - inset->ascent(bv, font),
122 cur.iy() + inset->descent(bv, font)
125 if (!b.contained(x, y)) {
126 lyxerr[Debug::GUI] << "Missed inset at x,y "
128 << " box " << b << endl;
132 text.setCursor(par, pos, true);
135 // The origin of an inset is on the baseline
136 y -= text.cursor.iy();
144 Inset * LyXText::checkInsetHit(int & x, int & y)
146 int y_tmp = y + top_y();
149 setCursorFromCoordinates(cur, x, y_tmp);
151 Inset * inset = checkInset(bv(), *this, cur, x, y_tmp);
157 // look at previous position
162 setCursor(cur, cur.par(), cur.pos() - 1, true);
164 inset = checkInset(bv(), *this, cur, x, y_tmp);
171 bool LyXText::gotoNextInset(vector<Inset::Code> const & codes,
172 string const & contents)
174 ParagraphList::iterator end = ownerParagraphs().end();
175 ParagraphList::iterator pit = cursor.par();
176 pos_type pos = cursor.pos();
180 if (pos + 1 < pit->size()) {
187 } while (pit != end &&
188 !(pit->isInset(pos) &&
189 (inset = pit->getInset(pos)) != 0 &&
190 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
192 static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
196 setCursor(pit, pos, false);
203 void LyXText::gotoInset(vector<Inset::Code> const & codes,
206 bv()->beforeChange(this);
210 if (same_content && cursor.pos() < cursor.par()->size()
211 && cursor.par()->isInset(cursor.pos())) {
212 Inset const * inset = cursor.par()->getInset(cursor.pos());
213 if (find(codes.begin(), codes.end(), inset->lyxCode())
215 contents = static_cast<InsetCommand const *>(inset)->getContents();
218 if (!gotoNextInset(codes, contents)) {
219 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
220 LyXCursor tmp = cursor;
221 cursor.par(ownerParagraphs().begin());
223 if (!gotoNextInset(codes, contents)) {
225 bv()->owner()->message(_("No more insets"));
228 bv()->owner()->message(_("No more insets"));
232 selection.cursor = cursor;
236 void LyXText::gotoInset(Inset::Code code, bool same_content)
238 gotoInset(vector<Inset::Code>(1, code), same_content);
242 void LyXText::cursorPrevious()
246 if (cursorRow() == 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 = cursorRow();
257 setCursorFromCoordinates(cursor.x_fix(), y);
261 if (cursorrow == bv()->text->cursorRow()) {
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 + cursorRow()->height()
276 - bv()->workHeight() + 1;
279 - cursorRow()->baseline()
280 + cursorRow()->height()
281 - bv()->workHeight() + 1;
284 bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
285 if (cursorRow() != rows().begin()) {
287 setCursor(cur, boost::prior(cursorRow())->par(),
288 boost::prior(cursorRow())->pos(), false);
289 if (cur.y() > top_y()) {
293 bv()->updateScrollbar();
297 void LyXText::cursorNext()
301 if (boost::next(cursorRow()) == rows().end()) {
302 int y = cursor.y() - cursorRow()->baseline() +
303 cursorRow()->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 = cursorRow();
321 setCursorFromCoordinates(cursor.x_fix(), y);
322 // + bv->workHeight());
326 if (cursorrow == bv()->text->cursorRow()) {
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 - cursorRow()->baseline();
342 new_y = cursor.y() - cursorRow()->baseline();
345 bv()->screen().draw(bv()->text, bv(), new_y);
347 RowList::iterator next_row = boost::next(cursorRow());
348 if (next_row != rows().end()) {
350 setCursor(cur, next_row->par(), next_row->pos(), false);
351 if (cur.y() < top_y() + bv()->workHeight()) {
355 bv()->updateScrollbar();
359 void LyXText::update()
361 bv()->update(this, BufferView::SELECT);
366 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 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
389 if (bv->insertInset(inset)) {
391 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
392 inset->localDispatch(cmd);
394 if (gotsel && pastesel)
395 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
404 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
406 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
407 <<"] arg[" << cmd.argument << ']' << endl;
409 BufferView * bv = cmd.view();
411 switch (cmd.action) {
413 case LFUN_APPENDIX: {
414 ParagraphList::iterator pit = cursor.par();
415 bool start = !pit->params().startOfAppendix();
417 // ensure that we have only one start_of_appendix in this document
418 ParagraphList::iterator tmp = ownerParagraphs().begin();
419 ParagraphList::iterator end = ownerParagraphs().end();
421 for (; tmp != end; ++tmp) {
422 if (tmp->params().startOfAppendix()) {
423 setUndo(bv, Undo::EDIT, tmp);
424 tmp->params().startOfAppendix(false);
426 setHeightOfRow(getRow(tmp, 0, tmpy));
431 setUndo(bv, Undo::EDIT, pit);
432 pit->params().startOfAppendix(start);
434 // we can set the refreshing parameters now
436 redoHeightOfParagraph();
438 setCursor(cursor.par(), cursor.pos());
443 case LFUN_DELETE_WORD_FORWARD:
444 bv->beforeChange(this);
451 case LFUN_DELETE_WORD_BACKWARD:
452 bv->beforeChange(this);
454 deleteWordBackward();
459 case LFUN_DELETE_LINE_FORWARD:
460 bv->beforeChange(this);
468 if (!selection.mark())
469 bv->beforeChange(this);
471 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
474 cursorRightOneWord();
479 if (!selection.mark())
480 bv->beforeChange(this);
482 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
483 cursorRightOneWord();
489 case LFUN_BEGINNINGBUF:
490 if (!selection.mark())
491 bv->beforeChange(this);
498 if (selection.mark())
499 bv->beforeChange(this);
507 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
511 finishChange(bv, true);
516 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
520 finishChange(bv, true);
526 finishChange(bv, true);
532 finishChange(bv, true);
535 case LFUN_UP_PARAGRAPHSEL:
538 finishChange(bv, true);
541 case LFUN_DOWN_PARAGRAPHSEL:
543 cursorDownParagraph();
544 finishChange(bv, true);
550 finishChange(bv, true);
556 finishChange(bv, true);
562 finishChange(bv, true);
568 finishChange(bv, true);
571 case LFUN_WORDRIGHTSEL:
573 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
576 cursorRightOneWord();
577 finishChange(bv, true);
580 case LFUN_WORDLEFTSEL:
582 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
583 cursorRightOneWord();
586 finishChange(bv, true);
593 getWord(cur1, cur2, WHOLE_WORD);
594 setCursor(cur1.par(), cur1.pos());
595 bv->beforeChange(this);
596 setCursor(cur2.par(), cur2.pos());
597 finishChange(bv, true);
602 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
603 if (!selection.mark())
604 bv->beforeChange(this);
608 if (cursor.pos() < cursor.par()->size()
609 && cursor.par()->isInset(cursor.pos())
610 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
611 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
612 cmd.message(tmpinset->editMessage());
613 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
614 tmpinset->localDispatch(cmd1);
624 // This is soooo ugly. Isn`t it possible to make
626 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
627 if (!selection.mark())
628 bv->beforeChange(this);
630 LyXCursor const cur = cursor;
633 if ((is_rtl || cur != cursor) && // only if really moved!
634 cursor.pos() < cursor.par()->size() &&
635 cursor.par()->isInset(cursor.pos()) &&
636 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
637 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
638 cmd.message(tmpinset->editMessage());
639 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
640 tmpinset->localDispatch(cmd1);
650 if (!selection.mark())
651 bv->beforeChange(this);
652 bv->update(this, BufferView::UPDATE);
658 if (!selection.mark())
659 bv->beforeChange(this);
660 bv->update(this, BufferView::UPDATE);
665 case LFUN_UP_PARAGRAPH:
666 if (!selection.mark())
667 bv->beforeChange(this);
668 bv->update(this, BufferView::UPDATE);
673 case LFUN_DOWN_PARAGRAPH:
674 if (!selection.mark())
675 bv->beforeChange(this);
676 bv->update(this, BufferView::UPDATE);
677 cursorDownParagraph();
678 finishChange(bv, false);
682 if (!selection.mark())
683 bv->beforeChange(this);
684 bv->update(this, BufferView::UPDATE);
686 finishChange(bv, false);
689 // moveCursorUpdate(bv, false, false);
690 // owner_->view_state_changed();
694 if (!selection.mark())
695 bv->beforeChange(this);
696 bv->update(this, BufferView::UPDATE);
698 finishChange(bv, false);
702 if (!selection.mark())
703 bv->beforeChange(this);
706 finishChange(bv, false);
710 if (!selection.mark())
711 bv->beforeChange(this);
714 finishChange(bv, false);
717 case LFUN_BREAKLINE: {
718 lyx::pos_type body = cursor.par()->beginningOfBody();
720 // Not allowed by LaTeX (labels or empty par)
721 if (cursor.pos() <= body)
724 bv->beforeChange(this);
725 insertInset(new InsetNewline);
727 setCursor(cursor.par(), cursor.pos());
728 moveCursorUpdate(bv, false);
733 if (!selection.set()) {
735 selection.cursor = cursor;
737 // It is possible to make it a lot faster still
738 // just comment out the line below...
741 cutSelection(true, false);
744 moveCursorUpdate(bv, false);
745 bv->owner()->view_state_changed();
749 case LFUN_DELETE_SKIP:
750 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
751 if (!selection.set()) {
752 LyXCursor cur = cursor;
753 if (cur.pos() == cur.par()->size()) {
757 && !(cur.par()->params().spaceTop()
758 == VSpace (VSpace::NONE))) {
760 cur.par()->params().lineTop(),
761 cur.par()->params().lineBottom(),
762 cur.par()->params().pagebreakTop(),
763 cur.par()->params().pagebreakBottom(),
764 VSpace(VSpace::NONE),
765 cur.par()->params().spaceBottom(),
766 cur.par()->params().spacing(),
767 cur.par()->params().align(),
768 cur.par()->params().labelWidthString(), 0);
774 selection.cursor = cursor;
778 selection.cursor = cursor;
782 cutSelection(true, false);
789 if (!selection.set()) {
790 if (bv->owner()->getIntl().getTransManager().backspace()) {
792 selection.cursor = cursor;
794 // It is possible to make it a lot faster still
795 // just comment out the line below...
799 cutSelection(true, false);
802 bv->owner()->view_state_changed();
806 case LFUN_BACKSPACE_SKIP:
807 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
808 if (!selection.set()) {
809 LyXCursor cur = cursor;
811 && !(cur.par()->params().spaceTop()
812 == VSpace (VSpace::NONE))) {
814 cur.par()->params().lineTop(),
815 cur.par()->params().lineBottom(),
816 cur.par()->params().pagebreakTop(),
817 cur.par()->params().pagebreakBottom(),
818 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
819 cur.par()->params().spacing(),
820 cur.par()->params().align(),
821 cur.par()->params().labelWidthString(), 0);
824 selection.cursor = cur;
828 cutSelection(true, false);
833 case LFUN_BREAKPARAGRAPH:
834 bv->beforeChange(this);
835 breakParagraph(bv->buffer()->paragraphs, 0);
837 selection.cursor = cursor;
839 bv->owner()->view_state_changed();
842 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
843 bv->beforeChange(this);
844 breakParagraph(bv->buffer()->paragraphs, 1);
846 selection.cursor = cursor;
848 bv->owner()->view_state_changed();
851 case LFUN_BREAKPARAGRAPH_SKIP: {
852 // When at the beginning of a paragraph, remove
853 // indentation and add a "defskip" at the top.
854 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
855 LyXCursor cur = cursor;
856 bv->beforeChange(this);
857 if (cur.pos() == 0) {
858 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
860 cur.par()->params().lineTop(),
861 cur.par()->params().lineBottom(),
862 cur.par()->params().pagebreakTop(),
863 cur.par()->params().pagebreakBottom(),
864 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
865 cur.par()->params().spacing(),
866 cur.par()->params().align(),
867 cur.par()->params().labelWidthString(), 1);
871 breakParagraph(bv->buffer()->paragraphs, 0);
874 selection.cursor = cur;
876 bv->owner()->view_state_changed();
880 case LFUN_PARAGRAPH_SPACING: {
881 ParagraphList::iterator pit = cursor.par();
882 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
883 float cur_value = 1.0;
884 if (cur_spacing == Spacing::Other)
885 cur_value = pit->params().spacing().getValue();
887 istringstream is(STRCONV(cmd.argument));
890 Spacing::Space new_spacing = cur_spacing;
891 float new_value = cur_value;
893 lyxerr << "Missing argument to `paragraph-spacing'"
895 } else if (tmp == "single") {
896 new_spacing = Spacing::Single;
897 } else if (tmp == "onehalf") {
898 new_spacing = Spacing::Onehalf;
899 } else if (tmp == "double") {
900 new_spacing = Spacing::Double;
901 } else if (tmp == "other") {
902 new_spacing = Spacing::Other;
905 lyxerr << "new_value = " << tmpval << endl;
908 } else if (tmp == "default") {
909 new_spacing = Spacing::Default;
911 lyxerr << _("Unknown spacing argument: ")
912 << cmd.argument << endl;
914 if (cur_spacing != new_spacing || cur_value != new_value) {
915 pit->params().spacing(Spacing(new_spacing, new_value));
922 case LFUN_INSET_SETTINGS:
923 lyx::Assert(bv->theLockingInset());
924 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
927 case LFUN_INSET_TOGGLE:
928 bv->beforeChange(this);
935 case LFUN_SPACE_INSERT:
936 if (cursor.par()->layout()->free_spacing) {
940 doInsertInset(this, cmd, false, false);
942 moveCursorUpdate(bv, false);
945 case LFUN_HYPHENATION:
946 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
949 case LFUN_LIGATURE_BREAK:
950 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
954 specialChar(this, bv, InsetSpecialChar::LDOTS);
957 case LFUN_END_OF_SENTENCE:
958 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
961 case LFUN_MENU_SEPARATOR:
962 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
966 bv->beforeChange(this);
968 selection.cursor = cursor;
969 cmd.message(N_("Mark off"));
973 bv->beforeChange(this);
974 selection.mark(true);
976 selection.cursor = cursor;
977 cmd.message(N_("Mark on"));
981 bv->beforeChange(this);
982 if (selection.mark()) {
984 cmd.message(N_("Mark removed"));
986 selection.mark(true);
988 cmd.message(N_("Mark set"));
990 selection.cursor = cursor;
993 case LFUN_UPCASE_WORD:
995 changeCase(LyXText::text_uppercase);
997 bv->updateInset(inset_owner);
1001 case LFUN_LOWCASE_WORD:
1003 changeCase(LyXText::text_lowercase);
1005 bv->updateInset(inset_owner);
1009 case LFUN_CAPITALIZE_WORD:
1011 changeCase(LyXText::text_capitalization);
1013 bv->updateInset(inset_owner);
1017 case LFUN_TRANSPOSE_CHARS:
1019 transposeChars(*this, cursor);
1021 bv->updateInset(inset_owner);
1026 cmd.message(_("Paste"));
1027 // clear the selection
1028 bv->toggleSelection();
1031 size_t sel_index = 0;
1032 string const & arg = cmd.argument;
1033 if (isStrUnsignedInt(arg)) {
1034 size_t const paste_arg = strToUnsignedInt(arg);
1035 #warning FIXME Check if the arg is in the domain of available selections.
1036 sel_index = paste_arg;
1038 pasteSelection(sel_index);
1039 clearSelection(); // bug 393
1047 cutSelection(true, true);
1049 cmd.message(_("Cut"));
1054 cmd.message(_("Copy"));
1057 case LFUN_BEGINNINGBUFSEL:
1059 return UNDISPATCHED;
1062 finishChange(bv, true);
1065 case LFUN_ENDBUFSEL:
1067 return UNDISPATCHED;
1070 finishChange(bv, true);
1074 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1080 istringstream is(STRCONV(cmd.argument));
1083 lyxerr << "SETXY: Could not parse coordinates in '"
1084 << cmd.argument << std::endl;
1086 setCursorFromCoordinates(x, y);
1091 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1093 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1099 case LFUN_GETLAYOUT:
1100 cmd.message(tostr(cursor.par()->layout()));
1104 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1105 << cmd.argument << endl;
1107 // This is not the good solution to the empty argument
1108 // problem, but it will hopefully suffice for 1.2.0.
1109 // The correct solution would be to augument the
1110 // function list/array with information about what
1111 // functions needs arguments and their type.
1112 if (cmd.argument.empty()) {
1113 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1117 // Derive layout number from given argument (string)
1118 // and current buffer's textclass (number)
1119 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1120 bool hasLayout = tclass.hasLayout(cmd.argument);
1121 string layout = cmd.argument;
1123 // If the entry is obsolete, use the new one instead.
1125 string const & obs = tclass[layout]->obsoleted_by();
1131 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1136 bool change_layout = (current_layout != layout);
1138 if (!change_layout && selection.set() &&
1139 selection.start.par() != selection.end.par())
1141 ParagraphList::iterator spit = selection.start.par();
1142 ParagraphList::iterator epit = boost::next(selection.end.par());
1143 while (spit != epit) {
1144 if (spit->layout()->name() != current_layout) {
1145 change_layout = true;
1152 if (change_layout) {
1153 current_layout = layout;
1156 bv->owner()->setLayout(layout);
1163 case LFUN_PASTESELECTION: {
1166 // this was originally a beforeChange(bv->text), i.e
1167 // the outermost LyXText!
1168 bv->beforeChange(this);
1169 string const clip = bv->getClipboard();
1170 if (!clip.empty()) {
1171 if (cmd.argument == "paragraph")
1172 insertStringAsParagraphs(clip);
1174 insertStringAsLines(clip);
1181 case LFUN_GOTOERROR:
1182 gotoInset(Inset::ERROR_CODE, false);
1186 gotoInset(Inset::NOTE_CODE, false);
1189 case LFUN_REFERENCE_GOTO:
1191 vector<Inset::Code> tmp;
1192 tmp.push_back(Inset::LABEL_CODE);
1193 tmp.push_back(Inset::REF_CODE);
1194 gotoInset(tmp, true);
1199 ParagraphList::iterator pit = cursor.par();
1200 lyx::pos_type pos = cursor.pos();
1204 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1207 c = pit->getChar(pos - 1);
1209 LyXLayout_ptr const & style = pit->layout();
1211 if (style->pass_thru ||
1212 pit->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().toggleSelection(this, bv);
1251 selection.cursor = cursor;
1255 bv->screen().toggleSelection(this, bv, false);
1257 bv->haveSelection(selection.set());
1261 case LFUN_MOUSE_DOUBLE:
1264 if (!isInInset() && bv->theLockingInset())
1266 if (cmd.button() == mouse_button::button1) {
1268 bv->screen().toggleSelection(this, bv);
1269 selectWord(LyXText::WHOLE_WORD_STRICT);
1270 bv->screen().toggleSelection(this, bv, false);
1272 selectWord(LyXText::WHOLE_WORD_STRICT);
1275 bv->haveSelection(selection.set());
1279 case LFUN_MOUSE_MOTION:
1281 // Only use motion with button 1
1282 //if (ev.button() != mouse_button::button1)
1288 // Check for inset locking
1289 if (bv->theLockingInset()) {
1290 Inset * tli = bv->theLockingInset();
1291 LyXCursor cursor = bv->text->cursor;
1292 LyXFont font = bv->text->getFont(bv->buffer(),
1293 cursor.par(), cursor.pos());
1294 int width = tli->width(bv, font);
1295 int inset_x = font.isVisibleRightToLeft()
1296 ? cursor.ix() - width : cursor.ix();
1297 int start_x = inset_x + tli->scroll();
1298 FuncRequest cmd1 = cmd;
1299 cmd1.x = cmd.x - start_x;
1300 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1301 tli->localDispatch(cmd1);
1305 // The test for not selection possible is needed, that only motion
1306 // events are used, where the bottom press event was on
1307 // the drawing area too
1308 if (!selection_possible) {
1309 lyxerr[Debug::ACTION]
1310 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1314 RowList::iterator cursorrow = bv->text->cursorRow();
1315 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->text->top_y());
1317 // sorry for this but I have a strange error that the y value jumps at
1318 // a certain point. This seems like an error in my xforms library or
1319 // in some other local environment, but I would like to leave this here
1320 // for the moment until I can remove this (Jug 20020418)
1321 if (y_before < bv->text->cursor.y())
1322 lyxerr << y_before << ':'
1323 << bv->text->cursor.y() << endl;
1325 // This is to allow jumping over large insets
1326 if (cursorrow == bv->text->cursorRow()) {
1327 if (cmd.y >= bv->workHeight())
1328 bv->text->cursorDown(false);
1330 bv->text->cursorUp(false);
1333 // Maybe an empty line was deleted
1334 if (!bv->text->selection.set())
1335 bv->update(BufferView::UPDATE);
1336 bv->text->setSelection();
1337 bv->screen().toggleToggle(bv->text, bv);
1342 // Single-click on work area
1343 case LFUN_MOUSE_PRESS:
1348 // ok ok, this is a hack (for xforms)
1349 // We shouldn't go further down as we really should only do the
1350 // scrolling and be done with this. Otherwise we may open some
1351 // dialogs (Jug 20020424).
1352 if (cmd.button() == mouse_button::button4) {
1353 bv->scroll(-lyxrc.wheel_jump);
1356 if (cmd.button() == mouse_button::button5) {
1357 bv->scroll(lyxrc.wheel_jump);
1363 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1365 // Middle button press pastes if we have a selection
1366 // We do this here as if the selection was inside an inset
1367 // it could get cleared on the unlocking of the inset so
1368 // we have to check this first
1369 bool paste_internally = false;
1370 if (cmd.button() == mouse_button::button2 && selection.set()) {
1371 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1372 paste_internally = true;
1375 int const screen_first = bv->text->top_y();
1377 if (bv->theLockingInset()) {
1378 // We are in inset locking mode
1380 // Check whether the inset was hit. If not reset mode,
1381 // otherwise give the event to the inset
1382 if (inset_hit == bv->theLockingInset()) {
1383 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1384 bv->theLockingInset()->localDispatch(cmd1);
1387 bv->unlockInset(bv->theLockingInset());
1391 selection_possible = true;
1393 // Clear the selection
1394 bv->screen().toggleSelection(bv->text, bv);
1395 bv->text->clearSelection();
1396 bv->text->fullRebreak();
1398 bv->updateScrollbar();
1400 // Single left click in math inset?
1401 if (isHighlyEditableInset(inset_hit)) {
1402 // Highly editable inset, like math
1403 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1404 selection_possible = false;
1405 bv->owner()->message(inset->editMessage());
1406 // We just have to lock the inset before calling a PressEvent on it!
1407 if (!bv->lockInset(inset))
1408 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1409 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1410 inset->localDispatch(cmd1);
1413 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1415 // Right click on a footnote flag opens float menu
1416 if (cmd.button() == mouse_button::button3) {
1417 selection_possible = false;
1421 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1422 bv->text->setCursorFromCoordinates(x, y + screen_first);
1424 bv->text->selection.cursor = bv->text->cursor;
1425 bv->text->cursor.x_fix(bv->text->cursor.x());
1427 if (bv->fitCursor())
1428 selection_possible = false;
1430 // Insert primary selection with middle mouse
1431 // if there is a local selection in the current buffer,
1433 if (cmd.button() == mouse_button::button2) {
1434 if (paste_internally)
1435 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1437 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1438 selection_possible = false;
1443 case LFUN_MOUSE_RELEASE:
1445 // do nothing if we used the mouse wheel
1449 if (cmd.button() == mouse_button::button4
1450 || cmd.button() == mouse_button::button5)
1453 // If we hit an inset, we have the inset coordinates in these
1454 // and inset_hit points to the inset. If we do not hit an
1455 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1458 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1460 if (bv->theLockingInset()) {
1461 // We are in inset locking mode.
1463 // LyX does a kind of work-area grabbing for insets.
1464 // Only a ButtonPress FuncRequest outside the inset will
1465 // force a insetUnlock.
1466 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1467 bv->theLockingInset()->localDispatch(cmd1);
1471 selection_possible = false;
1473 if (cmd.button() == mouse_button::button2)
1477 if (cmd.button() == mouse_button::button1)
1478 bv->haveSelection(selection.set());
1481 bv->owner()->view_state_changed();
1482 bv->owner()->updateMenubar();
1483 bv->owner()->updateToolbar();
1485 // Did we hit an editable inset?
1487 selection_possible = false;
1489 // if we reach this point with a selection, it
1490 // must mean we are currently selecting.
1491 // But we don't want to open the inset
1492 // because that is annoying for the user.
1493 // So just pretend we didn't hit it.
1494 // this is OK because a "kosher" ButtonRelease
1495 // will follow a ButtonPress that clears
1497 // Note this also fixes selection drawing
1498 // problems if we end up opening an inset
1499 if (selection.set())
1502 // CHECK fix this proper in 0.13
1503 // well, maybe 13.0 !!!!!!!!!
1505 // Following a ref shouldn't issue
1506 // a push on the undo-stack
1507 // anylonger, now that we have
1508 // keybindings for following
1509 // references and returning from
1510 // references. IMHO though, it
1511 // should be the inset's own business
1512 // to push or not push on the undo
1513 // stack. They don't *have* to
1514 // alter the document...
1516 // ...or maybe the SetCursorParUndo()
1517 // below isn't necessary at all anylonger?
1518 if (inset_hit->lyxCode() == Inset::REF_CODE)
1519 setCursorParUndo(bv);
1521 bv->owner()->message(inset_hit->editMessage());
1523 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1524 inset_hit->localDispatch(cmd1);
1530 case LFUN_SELFINSERT: {
1531 if (cmd.argument.empty())
1534 // Automatically delete the currently selected
1535 // text and replace it with what is being
1536 // typed in now. Depends on lyxrc settings
1537 // "auto_region_delete", which defaults to
1540 if (lyxrc.auto_region_delete) {
1541 if (selection.set()) {
1542 cutSelection(false, false);
1545 bv->haveSelection(false);
1548 bv->beforeChange(this);
1549 LyXFont const old_font(real_current_font);
1551 string::const_iterator cit = cmd.argument.begin();
1552 string::const_iterator end = cmd.argument.end();
1553 for (; cit != end; ++cit)
1554 bv->owner()->getIntl().getTransManager().
1555 TranslateAndInsert(*cit, this);
1558 selection.cursor = cursor;
1559 moveCursorUpdate(bv, false);
1561 // real_current_font.number can change so we need to
1562 // update the minibuffer
1563 if (old_font != real_current_font)
1564 bv->owner()->view_state_changed();
1568 case LFUN_HTMLURL: {
1569 InsetCommandParams p("htmlurl");
1570 string const data = InsetCommandMailer::params2string("url", p);
1571 bv->owner()->getDialogs().show("url", data, 0);
1576 InsetCommandParams p("url");
1577 string const data = InsetCommandMailer::params2string("url", p);
1578 bv->owner()->getDialogs().show("url", data, 0);
1584 case LFUN_INSET_LIST:
1585 case LFUN_INSET_THEOREM:
1586 case LFUN_INSET_CAPTION:
1588 case LFUN_INSERT_NOTE:
1589 case LFUN_INSERT_BIBITEM:
1590 case LFUN_INSET_ERT:
1591 case LFUN_INSET_FLOAT:
1592 case LFUN_INSET_FOOTNOTE:
1593 case LFUN_INSET_MARGINAL:
1594 case LFUN_INSET_MINIPAGE:
1595 case LFUN_INSET_OPTARG:
1596 case LFUN_INSET_WIDE_FLOAT:
1597 case LFUN_INSET_WRAP:
1598 case LFUN_TABULAR_INSERT:
1599 case LFUN_ENVIRONMENT_INSERT:
1600 // Open the inset, and move the current selection
1602 doInsertInset(this, cmd, true, true);
1605 case LFUN_INDEX_INSERT:
1606 // Just open the inset
1607 doInsertInset(this, cmd, true, false);
1610 case LFUN_INDEX_PRINT:
1611 case LFUN_TOC_INSERT:
1614 doInsertInset(this, cmd, false, false);
1618 return UNDISPATCHED;