1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2002 The LyX Team.
9 * ====================================================== */
15 #include "paragraph.h"
16 #include "BufferView.h"
17 #include "funcrequest.h"
21 #include "bufferparams.h"
23 #include "bufferview_funcs.h"
24 #include "ParagraphParameters.h"
30 #include "support/tostr.h"
31 #include "support/lstrings.h"
32 #include "support/LAssert.h"
33 #include "frontends/LyXView.h"
34 #include "frontends/screen.h"
35 #include "frontends/Dialogs.h"
36 #include "insets/insetspecialchar.h"
37 #include "insets/insettext.h"
38 #include "insets/insetquotes.h"
39 #include "insets/insetcommand.h"
40 #include "insets/insetnewline.h"
41 #include "undo_funcs.h"
42 #include "text_funcs.h"
48 using namespace lyx::support;
49 using namespace bv_funcs;
56 extern string current_layout;
57 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
59 // the selection possible is needed, that only motion events are
60 // used, where the bottom press event was on the drawing area too
61 bool selection_possible = false;
66 void moveCursorUpdate(BufferView * bv, bool selecting)
68 LyXText * lt = bv->getLyXText();
70 if (selecting || lt->selection.mark()) {
73 bv->updateInset(lt->inset_owner);
77 if (!lt->isInInset()) {
78 bv->update(lt, BufferView::SELECT);
79 } else if (bv->text->needRefresh()) {
80 bv->update(BufferView::SELECT);
83 if (!lt->selection.set())
84 bv->haveSelection(false);
90 void finishChange(BufferView * bv, bool selecting = false)
93 moveCursorUpdate(bv, selecting);
94 bv->owner()->view_state_changed();
97 // check if the given co-ordinates are inside an inset at the
98 // given cursor, if one exists. If so, the inset is returned,
99 // and the co-ordinates are made relative. Otherwise, 0 is returned.
100 InsetOld * checkInset(BufferView * /*bv*/, LyXText & text,
101 LyXCursor const & cur, int & x, int & y)
103 lyx::pos_type const pos = cur.pos();
104 ParagraphList::iterator par = cur.par();
106 if (pos >= par->size() || !par->isInset(pos))
109 InsetOld /*const*/ * inset = par->getInset(pos);
111 if (!isEditableInset(inset))
114 // get inset dimensions
115 Assert(par->getInset(pos));
117 LyXFont const & font = text.getFont(par, pos);
119 int const width = inset->width();
120 int const inset_x = font.isVisibleRightToLeft()
121 ? (cur.ix() - width) : cur.ix();
124 inset_x + inset->scroll(),
126 cur.iy() - inset->ascent(),
127 cur.iy() + inset->descent()
130 if (!b.contained(x, y)) {
131 lyxerr[Debug::GUI] << "Missed inset at x,y "
133 << " box " << b << endl;
137 text.setCursor(par, pos, true);
140 // The origin of an inset is on the baseline
141 y -= text.cursor.iy();
149 InsetOld * LyXText::checkInsetHit(int & x, int & y)
151 int y_tmp = y + top_y();
154 setCursorFromCoordinates(cur, x, y_tmp);
156 InsetOld * inset = checkInset(bv(), *this, cur, x, y_tmp);
162 // look at previous position
167 setCursor(cur, cur.par(), cur.pos() - 1, true);
169 inset = checkInset(bv(), *this, cur, x, y_tmp);
176 bool LyXText::gotoNextInset(vector<InsetOld::Code> const & codes,
177 string const & contents)
179 ParagraphList::iterator end = ownerParagraphs().end();
180 ParagraphList::iterator pit = cursor.par();
181 pos_type pos = cursor.pos();
185 if (pos + 1 < pit->size()) {
192 } while (pit != end &&
193 !(pit->isInset(pos) &&
194 (inset = pit->getInset(pos)) != 0 &&
195 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
197 static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
201 setCursor(pit, pos, false);
208 void LyXText::gotoInset(vector<InsetOld::Code> const & codes,
211 bv()->beforeChange(this);
215 if (same_content && cursor.pos() < cursor.par()->size()
216 && cursor.par()->isInset(cursor.pos())) {
217 InsetOld const * inset = cursor.par()->getInset(cursor.pos());
218 if (find(codes.begin(), codes.end(), inset->lyxCode())
220 contents = static_cast<InsetCommand const *>(inset)->getContents();
223 if (!gotoNextInset(codes, contents)) {
224 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
225 LyXCursor tmp = cursor;
226 cursor.par(ownerParagraphs().begin());
228 if (!gotoNextInset(codes, contents)) {
230 bv()->owner()->message(_("No more insets"));
233 bv()->owner()->message(_("No more insets"));
237 selection.cursor = cursor;
241 void LyXText::gotoInset(InsetOld::Code code, bool same_content)
243 gotoInset(vector<InsetOld::Code>(1, code), same_content);
247 void LyXText::cursorPrevious()
251 if (cursorRow() == rows().begin()) {
253 bv()->updateScrollbar();
257 RowList::iterator cursorrow = cursorRow();
259 setCursorFromCoordinates(cursor.x_fix(), y);
263 if (cursorrow == bv()->text->cursorRow()) {
264 // we have a row which is taller than the workarea. The
265 // simplest solution is to move to the previous row instead.
268 // This is what we used to do, so we wouldn't skip right past
269 // tall rows, but it's not working right now.
272 new_y = bv()->text->cursor.iy()
273 + bv()->theLockingInset()->insetInInsetY() + y
274 + cursorRow()->height()
275 - bv()->workHeight() + 1;
278 - cursorRow()->baseline()
279 + cursorRow()->height()
280 - bv()->workHeight() + 1;
283 //bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
284 if (cursorRow() != rows().begin()) {
286 setCursor(cur, boost::prior(cursorRow())->par(),
287 boost::prior(cursorRow())->pos(), false);
288 if (cur.y() > top_y()) {
292 bv()->updateScrollbar();
296 void LyXText::cursorNext()
300 if (boost::next(cursorRow()) == rows().end()) {
301 int y = cursor.y() - cursorRow()->baseline() +
302 cursorRow()->height();
303 if (y > topy + bv()->workHeight()) {
304 //bv()->screen().draw(bv()->text, bv(), bv()->text->top_y() + bv()->workHeight());
305 bv()->updateScrollbar();
310 int y = topy + bv()->workHeight();
311 if (inset_owner && !topy) {
312 y -= (bv()->text->cursor.iy()
313 - bv()->text->top_y()
314 + bv()->theLockingInset()->insetInInsetY());
319 RowList::iterator cursorrow = cursorRow();
320 setCursorFromCoordinates(cursor.x_fix(), y);
321 // + bv->workHeight());
325 if (cursorrow == bv()->text->cursorRow()) {
326 // we have a row which is taller than the workarea. The
327 // simplest solution is to move to the next row instead.
330 // This is what we used to do, so we wouldn't skip right past
331 // tall rows, but it's not working right now.
333 new_y = bv->text->top_y() + bv->workHeight();
337 new_y = bv()->text->cursor.iy()
338 + bv()->theLockingInset()->insetInInsetY()
339 + y - cursorRow()->baseline();
341 new_y = cursor.y() - cursorRow()->baseline();
344 //bv()->screen().draw(bv()->text, bv(), new_y);
346 RowList::iterator next_row = boost::next(cursorRow());
347 if (next_row != rows().end()) {
349 setCursor(cur, next_row->par(), next_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)
368 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
369 replaceSelection(lt);
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 InsetOld * 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 InsetOld::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 recordUndo(bv, Undo::ATOMIC, tmp);
424 tmp->params().startOfAppendix(false);
426 setHeightOfRow(getRow(tmp, 0, tmpy));
431 recordUndo(bv, Undo::ATOMIC, 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);
591 LyXCursor cur1 = cursor;
593 ::getWord(cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs());
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 InsetOld * 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 InsetOld * 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);
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 replaceSelection(bv->getLyXText());
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...
737 cutSelection(true, false);
740 moveCursorUpdate(bv, false);
741 bv->owner()->view_state_changed();
745 case LFUN_DELETE_SKIP:
746 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
747 if (!selection.set()) {
748 LyXCursor cur = cursor;
749 if (cur.pos() == cur.par()->size()) {
753 && !(cur.par()->params().spaceTop()
754 == VSpace (VSpace::NONE))) {
756 cur.par()->params().lineTop(),
757 cur.par()->params().lineBottom(),
758 cur.par()->params().pagebreakTop(),
759 cur.par()->params().pagebreakBottom(),
760 VSpace(VSpace::NONE),
761 cur.par()->params().spaceBottom(),
762 cur.par()->params().spacing(),
763 cur.par()->params().align(),
764 cur.par()->params().labelWidthString(), 0);
770 selection.cursor = cursor;
774 selection.cursor = cursor;
778 cutSelection(true, false);
785 if (!selection.set()) {
786 if (bv->owner()->getIntl().getTransManager().backspace()) {
788 selection.cursor = cursor;
790 // It is possible to make it a lot faster still
791 // just comment out the line below...
795 cutSelection(true, false);
798 bv->owner()->view_state_changed();
802 case LFUN_BACKSPACE_SKIP:
803 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
804 if (!selection.set()) {
805 LyXCursor cur = cursor;
807 && !(cur.par()->params().spaceTop()
808 == VSpace (VSpace::NONE))) {
810 cur.par()->params().lineTop(),
811 cur.par()->params().lineBottom(),
812 cur.par()->params().pagebreakTop(),
813 cur.par()->params().pagebreakBottom(),
814 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
815 cur.par()->params().spacing(),
816 cur.par()->params().align(),
817 cur.par()->params().labelWidthString(), 0);
820 selection.cursor = cur;
824 cutSelection(true, false);
829 case LFUN_BREAKPARAGRAPH:
830 replaceSelection(bv->getLyXText());
831 breakParagraph(bv->buffer()->paragraphs, 0);
833 selection.cursor = cursor;
835 bv->owner()->view_state_changed();
838 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
839 replaceSelection(bv->getLyXText());
840 breakParagraph(bv->buffer()->paragraphs, 1);
842 selection.cursor = cursor;
844 bv->owner()->view_state_changed();
847 case LFUN_BREAKPARAGRAPH_SKIP: {
848 // When at the beginning of a paragraph, remove
849 // indentation and add a "defskip" at the top.
850 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
851 LyXCursor cur = cursor;
852 replaceSelection(bv->getLyXText());
853 if (cur.pos() == 0) {
854 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
856 cur.par()->params().lineTop(),
857 cur.par()->params().lineBottom(),
858 cur.par()->params().pagebreakTop(),
859 cur.par()->params().pagebreakBottom(),
860 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
861 cur.par()->params().spacing(),
862 cur.par()->params().align(),
863 cur.par()->params().labelWidthString(), 1);
867 breakParagraph(bv->buffer()->paragraphs, 0);
870 selection.cursor = cur;
872 bv->owner()->view_state_changed();
876 case LFUN_PARAGRAPH_SPACING: {
877 ParagraphList::iterator pit = cursor.par();
878 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
879 float cur_value = 1.0;
880 if (cur_spacing == Spacing::Other)
881 cur_value = pit->params().spacing().getValue();
883 istringstream is(STRCONV(cmd.argument));
886 Spacing::Space new_spacing = cur_spacing;
887 float new_value = cur_value;
889 lyxerr << "Missing argument to `paragraph-spacing'"
891 } else if (tmp == "single") {
892 new_spacing = Spacing::Single;
893 } else if (tmp == "onehalf") {
894 new_spacing = Spacing::Onehalf;
895 } else if (tmp == "double") {
896 new_spacing = Spacing::Double;
897 } else if (tmp == "other") {
898 new_spacing = Spacing::Other;
901 lyxerr << "new_value = " << tmpval << endl;
904 } else if (tmp == "default") {
905 new_spacing = Spacing::Default;
907 lyxerr << _("Unknown spacing argument: ")
908 << cmd.argument << endl;
910 if (cur_spacing != new_spacing || cur_value != new_value) {
911 pit->params().spacing(Spacing(new_spacing, new_value));
918 case LFUN_INSET_SETTINGS:
919 Assert(bv->theLockingInset());
920 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
923 case LFUN_INSET_TOGGLE:
924 bv->beforeChange(this);
931 case LFUN_SPACE_INSERT:
932 if (cursor.par()->layout()->free_spacing) {
936 doInsertInset(this, cmd, false, false);
938 moveCursorUpdate(bv, false);
941 case LFUN_HYPHENATION:
942 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
945 case LFUN_LIGATURE_BREAK:
946 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
950 specialChar(this, bv, InsetSpecialChar::LDOTS);
953 case LFUN_END_OF_SENTENCE:
954 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
957 case LFUN_MENU_SEPARATOR:
958 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
962 bv->beforeChange(this);
964 selection.cursor = cursor;
965 cmd.message(N_("Mark off"));
969 bv->beforeChange(this);
970 selection.mark(true);
972 selection.cursor = cursor;
973 cmd.message(N_("Mark on"));
977 bv->beforeChange(this);
978 if (selection.mark()) {
980 cmd.message(N_("Mark removed"));
982 selection.mark(true);
984 cmd.message(N_("Mark set"));
986 selection.cursor = cursor;
989 case LFUN_UPCASE_WORD:
991 changeCase(LyXText::text_uppercase);
993 bv->updateInset(inset_owner);
997 case LFUN_LOWCASE_WORD:
999 changeCase(LyXText::text_lowercase);
1001 bv->updateInset(inset_owner);
1005 case LFUN_CAPITALIZE_WORD:
1007 changeCase(LyXText::text_capitalization);
1009 bv->updateInset(inset_owner);
1013 case LFUN_TRANSPOSE_CHARS:
1015 recordUndo(bv, Undo::ATOMIC, cursor.par());
1016 if (transposeChars(cursor))
1017 checkParagraph(cursor.par(), cursor.pos());
1019 bv->updateInset(inset_owner);
1024 cmd.message(_("Paste"));
1025 replaceSelection(bv->getLyXText());
1026 size_t sel_index = 0;
1027 string const & arg = cmd.argument;
1028 if (isStrUnsignedInt(arg)) {
1029 size_t const paste_arg = strToUnsignedInt(arg);
1030 #warning FIXME Check if the arg is in the domain of available selections.
1031 sel_index = paste_arg;
1033 pasteSelection(sel_index);
1034 clearSelection(); // bug 393
1042 cutSelection(true, true);
1044 cmd.message(_("Cut"));
1049 cmd.message(_("Copy"));
1052 case LFUN_BEGINNINGBUFSEL:
1054 return UNDISPATCHED;
1057 finishChange(bv, true);
1060 case LFUN_ENDBUFSEL:
1062 return UNDISPATCHED;
1065 finishChange(bv, true);
1069 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1075 istringstream is(STRCONV(cmd.argument));
1078 lyxerr << "SETXY: Could not parse coordinates in '"
1079 << cmd.argument << std::endl;
1081 setCursorFromCoordinates(x, y);
1086 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1088 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1094 case LFUN_GETLAYOUT:
1095 cmd.message(tostr(cursor.par()->layout()));
1099 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1100 << cmd.argument << endl;
1102 // This is not the good solution to the empty argument
1103 // problem, but it will hopefully suffice for 1.2.0.
1104 // The correct solution would be to augument the
1105 // function list/array with information about what
1106 // functions needs arguments and their type.
1107 if (cmd.argument.empty()) {
1108 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1112 // Derive layout number from given argument (string)
1113 // and current buffer's textclass (number)
1114 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1115 bool hasLayout = tclass.hasLayout(cmd.argument);
1116 string layout = cmd.argument;
1118 // If the entry is obsolete, use the new one instead.
1120 string const & obs = tclass[layout]->obsoleted_by();
1126 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1131 bool change_layout = (current_layout != layout);
1133 if (!change_layout && selection.set() &&
1134 selection.start.par() != selection.end.par())
1136 ParagraphList::iterator spit = selection.start.par();
1137 ParagraphList::iterator epit = boost::next(selection.end.par());
1138 while (spit != epit) {
1139 if (spit->layout()->name() != current_layout) {
1140 change_layout = true;
1147 if (change_layout) {
1148 current_layout = layout;
1151 bv->owner()->setLayout(layout);
1158 case LFUN_PASTESELECTION: {
1161 // this was originally a beforeChange(bv->text), i.e
1162 // the outermost LyXText!
1163 bv->beforeChange(this);
1164 string const clip = bv->getClipboard();
1165 if (!clip.empty()) {
1166 if (cmd.argument == "paragraph")
1167 insertStringAsParagraphs(clip);
1169 insertStringAsLines(clip);
1176 case LFUN_GOTOERROR:
1177 gotoInset(InsetOld::ERROR_CODE, false);
1181 gotoInset(InsetOld::NOTE_CODE, false);
1184 case LFUN_REFERENCE_GOTO:
1186 vector<InsetOld::Code> tmp;
1187 tmp.push_back(InsetOld::LABEL_CODE);
1188 tmp.push_back(InsetOld::REF_CODE);
1189 gotoInset(tmp, true);
1194 replaceSelection(bv->getLyXText());
1195 ParagraphList::iterator pit = cursor.par();
1196 lyx::pos_type pos = cursor.pos();
1200 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1203 c = pit->getChar(pos - 1);
1205 LyXLayout_ptr const & style = pit->layout();
1207 if (style->pass_thru ||
1208 pit->getFontSettings(bv->buffer()->params,
1209 pos).language()->lang() == "hebrew" ||
1210 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1211 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1215 case LFUN_DATE_INSERT: {
1216 replaceSelection(bv->getLyXText());
1217 time_t now_time_t = time(NULL);
1218 struct tm * now_tm = localtime(&now_time_t);
1219 setlocale(LC_TIME, "");
1221 if (!cmd.argument.empty())
1224 arg = lyxrc.date_insert_format;
1226 int const datetmp_len =
1227 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1229 for (int i = 0; i < datetmp_len; i++) {
1230 insertChar(datetmp[i]);
1233 selection.cursor = cursor;
1234 moveCursorUpdate(bv, false);
1238 case LFUN_MOUSE_TRIPLE:
1241 if (!isInInset() && bv->theLockingInset())
1243 if (cmd.button() == mouse_button::button1) {
1245 bv->screen().toggleSelection(this, bv);
1247 selection.cursor = cursor;
1251 bv->screen().toggleSelection(this, bv, false);
1253 bv->haveSelection(selection.set());
1257 case LFUN_MOUSE_DOUBLE:
1260 if (!isInInset() && bv->theLockingInset())
1262 if (cmd.button() == mouse_button::button1) {
1264 bv->screen().toggleSelection(this, bv);
1265 selectWord(lyx::WHOLE_WORD_STRICT);
1266 bv->screen().toggleSelection(this, bv, false);
1268 selectWord(lyx::WHOLE_WORD_STRICT);
1271 bv->haveSelection(selection.set());
1275 case LFUN_MOUSE_MOTION:
1277 // Only use motion with button 1
1278 //if (ev.button() != mouse_button::button1)
1284 // Check for inset locking
1285 if (bv->theLockingInset()) {
1286 InsetOld * tli = bv->theLockingInset();
1287 LyXCursor cursor = bv->text->cursor;
1288 LyXFont font = bv->text->getFont(cursor.par(), cursor.pos());
1289 int width = tli->width();
1290 int inset_x = font.isVisibleRightToLeft()
1291 ? cursor.ix() - width : cursor.ix();
1292 int start_x = inset_x + tli->scroll();
1293 FuncRequest cmd1 = cmd;
1294 cmd1.x = cmd.x - start_x;
1295 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1296 tli->localDispatch(cmd1);
1300 // The test for not selection possible is needed, that only motion
1301 // events are used, where the bottom press event was on
1302 // the drawing area too
1303 if (!selection_possible) {
1304 lyxerr[Debug::ACTION]
1305 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1309 RowList::iterator cursorrow = bv->text->cursorRow();
1310 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->text->top_y());
1312 // sorry for this but I have a strange error that the y value jumps at
1313 // a certain point. This seems like an error in my xforms library or
1314 // in some other local environment, but I would like to leave this here
1315 // for the moment until I can remove this (Jug 20020418)
1316 if (y_before < bv->text->cursor.y())
1317 lyxerr << y_before << ':'
1318 << bv->text->cursor.y() << endl;
1320 // This is to allow jumping over large insets
1321 if (cursorrow == bv->text->cursorRow()) {
1322 if (cmd.y >= bv->workHeight())
1323 bv->text->cursorDown(false);
1325 bv->text->cursorUp(false);
1328 // Maybe an empty line was deleted
1329 if (!bv->text->selection.set())
1330 bv->update(BufferView::UPDATE);
1331 bv->text->setSelection();
1337 // Single-click on work area
1338 case LFUN_MOUSE_PRESS:
1343 // ok ok, this is a hack (for xforms)
1344 // We shouldn't go further down as we really should only do the
1345 // scrolling and be done with this. Otherwise we may open some
1346 // dialogs (Jug 20020424).
1347 if (cmd.button() == mouse_button::button4) {
1348 bv->scroll(-lyxrc.wheel_jump);
1351 if (cmd.button() == mouse_button::button5) {
1352 bv->scroll(lyxrc.wheel_jump);
1358 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1360 // Middle button press pastes if we have a selection
1361 // We do this here as if the selection was inside an inset
1362 // it could get cleared on the unlocking of the inset so
1363 // we have to check this first
1364 bool paste_internally = false;
1365 if (cmd.button() == mouse_button::button2 && selection.set()) {
1366 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1367 paste_internally = true;
1370 int const screen_first = bv->text->top_y();
1372 if (bv->theLockingInset()) {
1373 // We are in inset locking mode
1375 // Check whether the inset was hit. If not reset mode,
1376 // otherwise give the event to the inset
1377 if (inset_hit == bv->theLockingInset()) {
1378 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1379 bv->theLockingInset()->localDispatch(cmd1);
1382 bv->unlockInset(bv->theLockingInset());
1386 selection_possible = true;
1388 // Clear the selection
1389 bv->screen().toggleSelection(bv->text, bv);
1390 bv->text->clearSelection();
1391 bv->text->partialRebreak();
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 // We just have to lock the inset before calling a PressEvent on it!
1402 if (!bv->lockInset(inset))
1403 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1404 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1405 inset->localDispatch(cmd1);
1408 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1410 // Right click on a footnote flag opens float menu
1411 if (cmd.button() == mouse_button::button3) {
1412 selection_possible = false;
1416 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1417 bv->text->setCursorFromCoordinates(x, y + screen_first);
1419 bv->text->selection.cursor = bv->text->cursor;
1420 bv->text->cursor.x_fix(bv->text->cursor.x());
1422 if (bv->fitCursor())
1423 selection_possible = false;
1425 // Insert primary selection with middle mouse
1426 // if there is a local selection in the current buffer,
1428 if (cmd.button() == mouse_button::button2) {
1429 if (paste_internally)
1430 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1432 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1433 selection_possible = false;
1438 case LFUN_MOUSE_RELEASE:
1440 // do nothing if we used the mouse wheel
1444 if (cmd.button() == mouse_button::button4
1445 || cmd.button() == mouse_button::button5)
1448 // If we hit an inset, we have the inset coordinates in these
1449 // and inset_hit points to the inset. If we do not hit an
1450 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1453 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1455 if (bv->theLockingInset()) {
1456 // We are in inset locking mode.
1458 // LyX does a kind of work-area grabbing for insets.
1459 // Only a ButtonPress FuncRequest outside the inset will
1460 // force a insetUnlock.
1461 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1462 bv->theLockingInset()->localDispatch(cmd1);
1466 selection_possible = false;
1468 if (cmd.button() == mouse_button::button2)
1472 if (cmd.button() == mouse_button::button1)
1473 bv->haveSelection(selection.set());
1476 bv->owner()->view_state_changed();
1477 bv->owner()->updateMenubar();
1478 bv->owner()->updateToolbar();
1480 // Did we hit an editable inset?
1482 selection_possible = false;
1484 // if we reach this point with a selection, it
1485 // must mean we are currently selecting.
1486 // But we don't want to open the inset
1487 // because that is annoying for the user.
1488 // So just pretend we didn't hit it.
1489 // this is OK because a "kosher" ButtonRelease
1490 // will follow a ButtonPress that clears
1492 // Note this also fixes selection drawing
1493 // problems if we end up opening an inset
1494 if (selection.set())
1497 // CHECK fix this proper in 0.13
1498 // well, maybe 13.0 !!!!!!!!!
1500 // Following a ref shouldn't issue
1501 // a push on the undo-stack
1502 // anylonger, now that we have
1503 // keybindings for following
1504 // references and returning from
1505 // references. IMHO though, it
1506 // should be the inset's own business
1507 // to push or not push on the undo
1508 // stack. They don't *have* to
1509 // alter the document...
1511 // ...or maybe the SetCursorParUndo()
1512 // below isn't necessary at all anylonger?
1513 if (inset_hit->lyxCode() == InsetOld::REF_CODE)
1514 recordUndo(bv, Undo::ATOMIC);
1516 bv->owner()->message(inset_hit->editMessage());
1518 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1519 inset_hit->localDispatch(cmd1);
1525 case LFUN_SELFINSERT: {
1526 if (cmd.argument.empty())
1529 // Automatically delete the currently selected
1530 // text and replace it with what is being
1531 // typed in now. Depends on lyxrc settings
1532 // "auto_region_delete", which defaults to
1535 if (lyxrc.auto_region_delete) {
1536 if (selection.set()) {
1537 cutSelection(false, false);
1540 bv->haveSelection(false);
1543 bv->beforeChange(this);
1544 LyXFont const old_font(real_current_font);
1546 string::const_iterator cit = cmd.argument.begin();
1547 string::const_iterator end = cmd.argument.end();
1548 for (; cit != end; ++cit)
1549 bv->owner()->getIntl().getTransManager().
1550 TranslateAndInsert(*cit, this);
1553 selection.cursor = cursor;
1554 moveCursorUpdate(bv, false);
1556 // real_current_font.number can change so we need to
1557 // update the minibuffer
1558 if (old_font != real_current_font)
1559 bv->owner()->view_state_changed();
1563 case LFUN_HTMLURL: {
1564 InsetCommandParams p("htmlurl");
1565 string const data = InsetCommandMailer::params2string("url", p);
1566 bv->owner()->getDialogs().show("url", data, 0);
1571 InsetCommandParams p("url");
1572 string const data = InsetCommandMailer::params2string("url", p);
1573 bv->owner()->getDialogs().show("url", data, 0);
1579 case LFUN_INSET_LIST:
1580 case LFUN_INSET_THEOREM:
1581 case LFUN_INSET_CAPTION:
1583 case LFUN_INSERT_NOTE:
1584 case LFUN_INSERT_BIBITEM:
1585 case LFUN_INSET_ERT:
1586 case LFUN_INSET_FLOAT:
1587 case LFUN_INSET_FOOTNOTE:
1588 case LFUN_INSET_MARGINAL:
1589 case LFUN_INSET_MINIPAGE:
1590 case LFUN_INSET_OPTARG:
1591 case LFUN_INSET_WIDE_FLOAT:
1592 case LFUN_INSET_WRAP:
1593 case LFUN_TABULAR_INSERT:
1594 case LFUN_ENVIRONMENT_INSERT:
1595 // Open the inset, and move the current selection
1597 doInsertInset(this, cmd, true, true);
1600 case LFUN_INDEX_INSERT:
1601 // Just open the inset
1602 doInsertInset(this, cmd, true, false);
1605 case LFUN_INDEX_PRINT:
1606 case LFUN_TOC_INSERT:
1609 doInsertInset(this, cmd, false, false);
1613 return UNDISPATCHED;