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(bv->buffer(), 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 int new_y = bv()->text->top_y() - bv()->workHeight();
254 //bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
255 bv()->updateScrollbar();
260 RowList::iterator cursorrow = cursorRow();
262 setCursorFromCoordinates(cursor.x_fix(), y);
266 if (cursorrow == bv()->text->cursorRow()) {
267 // we have a row which is taller than the workarea. The
268 // simplest solution is to move to the previous row instead.
271 // This is what we used to do, so we wouldn't skip right past
272 // tall rows, but it's not working right now.
274 new_y = bv->text->top_y() - bv->workHeight();
278 new_y = bv()->text->cursor.iy()
279 + bv()->theLockingInset()->insetInInsetY() + y
280 + cursorRow()->height()
281 - bv()->workHeight() + 1;
284 - cursorRow()->baseline()
285 + cursorRow()->height()
286 - bv()->workHeight() + 1;
289 //bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
290 if (cursorRow() != rows().begin()) {
292 setCursor(cur, boost::prior(cursorRow())->par(),
293 boost::prior(cursorRow())->pos(), false);
294 if (cur.y() > top_y()) {
298 bv()->updateScrollbar();
302 void LyXText::cursorNext()
306 if (boost::next(cursorRow()) == rows().end()) {
307 int y = cursor.y() - cursorRow()->baseline() +
308 cursorRow()->height();
309 if (y > topy + bv()->workHeight()) {
310 //bv()->screen().draw(bv()->text, bv(), bv()->text->top_y() + bv()->workHeight());
311 bv()->updateScrollbar();
316 int y = topy + bv()->workHeight();
317 if (inset_owner && !topy) {
318 y -= (bv()->text->cursor.iy()
319 - bv()->text->top_y()
320 + bv()->theLockingInset()->insetInInsetY());
325 RowList::iterator cursorrow = cursorRow();
326 setCursorFromCoordinates(cursor.x_fix(), y);
327 // + bv->workHeight());
331 if (cursorrow == bv()->text->cursorRow()) {
332 // we have a row which is taller than the workarea. The
333 // simplest solution is to move to the next row instead.
336 // This is what we used to do, so we wouldn't skip right past
337 // tall rows, but it's not working right now.
339 new_y = bv->text->top_y() + bv->workHeight();
343 new_y = bv()->text->cursor.iy()
344 + bv()->theLockingInset()->insetInInsetY()
345 + y - cursorRow()->baseline();
347 new_y = cursor.y() - cursorRow()->baseline();
350 //bv()->screen().draw(bv()->text, bv(), new_y);
352 RowList::iterator next_row = boost::next(cursorRow());
353 if (next_row != rows().end()) {
355 setCursor(cur, next_row->par(), next_row->pos(), false);
356 if (cur.y() < top_y() + bv()->workHeight()) {
360 bv()->updateScrollbar();
364 void LyXText::update()
366 bv()->update(this, BufferView::SELECT);
371 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
374 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
375 replaceSelection(lt);
376 if (!bv->insertInset(new_inset))
379 bv->updateInset(new_inset);
383 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
384 bool edit, bool pastesel)
386 InsetOld * inset = createInset(cmd);
387 BufferView * bv = cmd.view();
391 if (lt->selection.set()) {
392 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
395 if (bv->insertInset(inset)) {
397 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
398 inset->localDispatch(cmd);
400 if (gotsel && pastesel)
401 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
410 InsetOld::RESULT LyXText::dispatch(FuncRequest const & cmd)
412 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
413 <<"] arg[" << cmd.argument << ']' << endl;
415 BufferView * bv = cmd.view();
417 switch (cmd.action) {
419 case LFUN_APPENDIX: {
420 ParagraphList::iterator pit = cursor.par();
421 bool start = !pit->params().startOfAppendix();
423 // ensure that we have only one start_of_appendix in this document
424 ParagraphList::iterator tmp = ownerParagraphs().begin();
425 ParagraphList::iterator end = ownerParagraphs().end();
427 for (; tmp != end; ++tmp) {
428 if (tmp->params().startOfAppendix()) {
429 recordUndo(bv, Undo::ATOMIC, tmp);
430 tmp->params().startOfAppendix(false);
432 setHeightOfRow(getRow(tmp, 0, tmpy));
437 recordUndo(bv, Undo::ATOMIC, pit);
438 pit->params().startOfAppendix(start);
440 // we can set the refreshing parameters now
442 redoHeightOfParagraph();
444 setCursor(cursor.par(), cursor.pos());
449 case LFUN_DELETE_WORD_FORWARD:
450 bv->beforeChange(this);
457 case LFUN_DELETE_WORD_BACKWARD:
458 bv->beforeChange(this);
460 deleteWordBackward();
465 case LFUN_DELETE_LINE_FORWARD:
466 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);
597 LyXCursor cur1 = cursor;
599 ::getWord(cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs());
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 InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
618 cmd.message(tmpinset->editMessage());
619 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
620 tmpinset->localDispatch(cmd1);
630 // This is soooo ugly. Isn`t it possible to make
632 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
633 if (!selection.mark())
634 bv->beforeChange(this);
636 LyXCursor const cur = cursor;
639 if ((is_rtl || cur != cursor) && // only if really moved!
640 cursor.pos() < cursor.par()->size() &&
641 cursor.par()->isInset(cursor.pos()) &&
642 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
643 InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
644 cmd.message(tmpinset->editMessage());
645 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
646 tmpinset->localDispatch(cmd1);
656 if (!selection.mark())
657 bv->beforeChange(this);
658 bv->update(this, BufferView::UPDATE);
664 if (!selection.mark())
665 bv->beforeChange(this);
666 bv->update(this, BufferView::UPDATE);
671 case LFUN_UP_PARAGRAPH:
672 if (!selection.mark())
673 bv->beforeChange(this);
674 bv->update(this, BufferView::UPDATE);
679 case LFUN_DOWN_PARAGRAPH:
680 if (!selection.mark())
681 bv->beforeChange(this);
682 bv->update(this, BufferView::UPDATE);
683 cursorDownParagraph();
684 finishChange(bv, false);
688 if (!selection.mark())
689 bv->beforeChange(this);
690 bv->update(this, BufferView::UPDATE);
692 finishChange(bv, false);
695 // moveCursorUpdate(bv, false, false);
696 // owner_->view_state_changed();
700 if (!selection.mark())
701 bv->beforeChange(this);
702 bv->update(this, BufferView::UPDATE);
704 finishChange(bv, false);
708 if (!selection.mark())
709 bv->beforeChange(this);
712 finishChange(bv, false);
716 if (!selection.mark())
717 bv->beforeChange(this);
720 finishChange(bv, false);
723 case LFUN_BREAKLINE: {
724 lyx::pos_type body = cursor.par()->beginningOfBody();
726 // Not allowed by LaTeX (labels or empty par)
727 if (cursor.pos() <= body)
730 replaceSelection(bv->getLyXText());
731 insertInset(new InsetNewline);
733 setCursor(cursor.par(), cursor.pos());
734 moveCursorUpdate(bv, false);
739 if (!selection.set()) {
741 selection.cursor = cursor;
743 // It is possible to make it a lot faster still
744 // just comment out the line below...
747 cutSelection(true, false);
750 moveCursorUpdate(bv, false);
751 bv->owner()->view_state_changed();
755 case LFUN_DELETE_SKIP:
756 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
757 if (!selection.set()) {
758 LyXCursor cur = cursor;
759 if (cur.pos() == cur.par()->size()) {
763 && !(cur.par()->params().spaceTop()
764 == VSpace (VSpace::NONE))) {
766 cur.par()->params().lineTop(),
767 cur.par()->params().lineBottom(),
768 cur.par()->params().pagebreakTop(),
769 cur.par()->params().pagebreakBottom(),
770 VSpace(VSpace::NONE),
771 cur.par()->params().spaceBottom(),
772 cur.par()->params().spacing(),
773 cur.par()->params().align(),
774 cur.par()->params().labelWidthString(), 0);
780 selection.cursor = cursor;
784 selection.cursor = cursor;
788 cutSelection(true, false);
795 if (!selection.set()) {
796 if (bv->owner()->getIntl().getTransManager().backspace()) {
798 selection.cursor = cursor;
800 // It is possible to make it a lot faster still
801 // just comment out the line below...
805 cutSelection(true, false);
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(true, false);
839 case LFUN_BREAKPARAGRAPH:
840 replaceSelection(bv->getLyXText());
841 breakParagraph(bv->buffer()->paragraphs, 0);
843 selection.cursor = cursor;
845 bv->owner()->view_state_changed();
848 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
849 replaceSelection(bv->getLyXText());
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 replaceSelection(bv->getLyXText());
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);
877 breakParagraph(bv->buffer()->paragraphs, 0);
880 selection.cursor = cur;
882 bv->owner()->view_state_changed();
886 case LFUN_PARAGRAPH_SPACING: {
887 ParagraphList::iterator pit = cursor.par();
888 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
889 float cur_value = 1.0;
890 if (cur_spacing == Spacing::Other)
891 cur_value = pit->params().spacing().getValue();
893 istringstream is(STRCONV(cmd.argument));
896 Spacing::Space new_spacing = cur_spacing;
897 float new_value = cur_value;
899 lyxerr << "Missing argument to `paragraph-spacing'"
901 } else if (tmp == "single") {
902 new_spacing = Spacing::Single;
903 } else if (tmp == "onehalf") {
904 new_spacing = Spacing::Onehalf;
905 } else if (tmp == "double") {
906 new_spacing = Spacing::Double;
907 } else if (tmp == "other") {
908 new_spacing = Spacing::Other;
911 lyxerr << "new_value = " << tmpval << endl;
914 } else if (tmp == "default") {
915 new_spacing = Spacing::Default;
917 lyxerr << _("Unknown spacing argument: ")
918 << cmd.argument << endl;
920 if (cur_spacing != new_spacing || cur_value != new_value) {
921 pit->params().spacing(Spacing(new_spacing, new_value));
928 case LFUN_INSET_SETTINGS:
929 Assert(bv->theLockingInset());
930 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
933 case LFUN_INSET_TOGGLE:
934 bv->beforeChange(this);
941 case LFUN_SPACE_INSERT:
942 if (cursor.par()->layout()->free_spacing) {
946 doInsertInset(this, cmd, false, false);
948 moveCursorUpdate(bv, false);
951 case LFUN_HYPHENATION:
952 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
955 case LFUN_LIGATURE_BREAK:
956 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
960 specialChar(this, bv, InsetSpecialChar::LDOTS);
963 case LFUN_END_OF_SENTENCE:
964 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
967 case LFUN_MENU_SEPARATOR:
968 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
972 bv->beforeChange(this);
974 selection.cursor = cursor;
975 cmd.message(N_("Mark off"));
979 bv->beforeChange(this);
980 selection.mark(true);
982 selection.cursor = cursor;
983 cmd.message(N_("Mark on"));
987 bv->beforeChange(this);
988 if (selection.mark()) {
990 cmd.message(N_("Mark removed"));
992 selection.mark(true);
994 cmd.message(N_("Mark set"));
996 selection.cursor = cursor;
999 case LFUN_UPCASE_WORD:
1001 changeCase(LyXText::text_uppercase);
1003 bv->updateInset(inset_owner);
1007 case LFUN_LOWCASE_WORD:
1009 changeCase(LyXText::text_lowercase);
1011 bv->updateInset(inset_owner);
1015 case LFUN_CAPITALIZE_WORD:
1017 changeCase(LyXText::text_capitalization);
1019 bv->updateInset(inset_owner);
1023 case LFUN_TRANSPOSE_CHARS:
1025 recordUndo(bv, Undo::ATOMIC, cursor.par());
1026 if (transposeChars(cursor))
1027 checkParagraph(cursor.par(), cursor.pos());
1029 bv->updateInset(inset_owner);
1034 cmd.message(_("Paste"));
1035 replaceSelection(bv->getLyXText());
1036 size_t sel_index = 0;
1037 string const & arg = cmd.argument;
1038 if (isStrUnsignedInt(arg)) {
1039 size_t const paste_arg = strToUnsignedInt(arg);
1040 #warning FIXME Check if the arg is in the domain of available selections.
1041 sel_index = paste_arg;
1043 pasteSelection(sel_index);
1044 clearSelection(); // bug 393
1052 cutSelection(true, true);
1054 cmd.message(_("Cut"));
1059 cmd.message(_("Copy"));
1062 case LFUN_BEGINNINGBUFSEL:
1064 return UNDISPATCHED;
1067 finishChange(bv, true);
1070 case LFUN_ENDBUFSEL:
1072 return UNDISPATCHED;
1075 finishChange(bv, true);
1079 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1085 istringstream is(STRCONV(cmd.argument));
1088 lyxerr << "SETXY: Could not parse coordinates in '"
1089 << cmd.argument << std::endl;
1091 setCursorFromCoordinates(x, y);
1096 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1098 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1104 case LFUN_GETLAYOUT:
1105 cmd.message(tostr(cursor.par()->layout()));
1109 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1110 << cmd.argument << endl;
1112 // This is not the good solution to the empty argument
1113 // problem, but it will hopefully suffice for 1.2.0.
1114 // The correct solution would be to augument the
1115 // function list/array with information about what
1116 // functions needs arguments and their type.
1117 if (cmd.argument.empty()) {
1118 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1122 // Derive layout number from given argument (string)
1123 // and current buffer's textclass (number)
1124 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1125 bool hasLayout = tclass.hasLayout(cmd.argument);
1126 string layout = cmd.argument;
1128 // If the entry is obsolete, use the new one instead.
1130 string const & obs = tclass[layout]->obsoleted_by();
1136 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1141 bool change_layout = (current_layout != layout);
1143 if (!change_layout && selection.set() &&
1144 selection.start.par() != selection.end.par())
1146 ParagraphList::iterator spit = selection.start.par();
1147 ParagraphList::iterator epit = boost::next(selection.end.par());
1148 while (spit != epit) {
1149 if (spit->layout()->name() != current_layout) {
1150 change_layout = true;
1157 if (change_layout) {
1158 current_layout = layout;
1161 bv->owner()->setLayout(layout);
1168 case LFUN_PASTESELECTION: {
1171 // this was originally a beforeChange(bv->text), i.e
1172 // the outermost LyXText!
1173 bv->beforeChange(this);
1174 string const clip = bv->getClipboard();
1175 if (!clip.empty()) {
1176 if (cmd.argument == "paragraph")
1177 insertStringAsParagraphs(clip);
1179 insertStringAsLines(clip);
1186 case LFUN_GOTOERROR:
1187 gotoInset(InsetOld::ERROR_CODE, false);
1191 gotoInset(InsetOld::NOTE_CODE, false);
1194 case LFUN_REFERENCE_GOTO:
1196 vector<InsetOld::Code> tmp;
1197 tmp.push_back(InsetOld::LABEL_CODE);
1198 tmp.push_back(InsetOld::REF_CODE);
1199 gotoInset(tmp, true);
1204 replaceSelection(bv->getLyXText());
1205 ParagraphList::iterator pit = cursor.par();
1206 lyx::pos_type pos = cursor.pos();
1210 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1213 c = pit->getChar(pos - 1);
1215 LyXLayout_ptr const & style = pit->layout();
1217 if (style->pass_thru ||
1218 pit->getFontSettings(bv->buffer()->params,
1219 pos).language()->lang() == "hebrew" ||
1220 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1221 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1225 case LFUN_DATE_INSERT: {
1226 replaceSelection(bv->getLyXText());
1227 time_t now_time_t = time(NULL);
1228 struct tm * now_tm = localtime(&now_time_t);
1229 setlocale(LC_TIME, "");
1231 if (!cmd.argument.empty())
1234 arg = lyxrc.date_insert_format;
1236 int const datetmp_len =
1237 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1239 for (int i = 0; i < datetmp_len; i++) {
1240 insertChar(datetmp[i]);
1243 selection.cursor = cursor;
1244 moveCursorUpdate(bv, false);
1248 case LFUN_MOUSE_TRIPLE:
1251 if (!isInInset() && bv->theLockingInset())
1253 if (cmd.button() == mouse_button::button1) {
1255 bv->screen().toggleSelection(this, bv);
1258 selection.cursor = cursor;
1262 bv->screen().toggleSelection(this, bv, false);
1264 bv->haveSelection(selection.set());
1268 case LFUN_MOUSE_DOUBLE:
1271 if (!isInInset() && bv->theLockingInset())
1273 if (cmd.button() == mouse_button::button1) {
1275 bv->screen().toggleSelection(this, bv);
1276 selectWord(lyx::WHOLE_WORD_STRICT);
1277 bv->screen().toggleSelection(this, bv, false);
1279 selectWord(lyx::WHOLE_WORD_STRICT);
1282 bv->haveSelection(selection.set());
1286 case LFUN_MOUSE_MOTION:
1288 // Only use motion with button 1
1289 //if (ev.button() != mouse_button::button1)
1295 // Check for inset locking
1296 if (bv->theLockingInset()) {
1297 InsetOld * tli = bv->theLockingInset();
1298 LyXCursor cursor = bv->text->cursor;
1299 LyXFont font = bv->text->getFont(bv->buffer(),
1300 cursor.par(), cursor.pos());
1301 int width = tli->width();
1302 int inset_x = font.isVisibleRightToLeft()
1303 ? cursor.ix() - width : cursor.ix();
1304 int start_x = inset_x + tli->scroll();
1305 FuncRequest cmd1 = cmd;
1306 cmd1.x = cmd.x - start_x;
1307 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1308 tli->localDispatch(cmd1);
1312 // The test for not selection possible is needed, that only motion
1313 // events are used, where the bottom press event was on
1314 // the drawing area too
1315 if (!selection_possible) {
1316 lyxerr[Debug::ACTION]
1317 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1321 RowList::iterator cursorrow = bv->text->cursorRow();
1322 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->text->top_y());
1324 // sorry for this but I have a strange error that the y value jumps at
1325 // a certain point. This seems like an error in my xforms library or
1326 // in some other local environment, but I would like to leave this here
1327 // for the moment until I can remove this (Jug 20020418)
1328 if (y_before < bv->text->cursor.y())
1329 lyxerr << y_before << ':'
1330 << bv->text->cursor.y() << endl;
1332 // This is to allow jumping over large insets
1333 if (cursorrow == bv->text->cursorRow()) {
1334 if (cmd.y >= bv->workHeight())
1335 bv->text->cursorDown(false);
1337 bv->text->cursorUp(false);
1340 // Maybe an empty line was deleted
1341 if (!bv->text->selection.set())
1342 bv->update(BufferView::UPDATE);
1343 bv->text->setSelection();
1344 bv->screen().toggleToggle(bv->text, bv);
1349 // Single-click on work area
1350 case LFUN_MOUSE_PRESS:
1355 // ok ok, this is a hack (for xforms)
1356 // We shouldn't go further down as we really should only do the
1357 // scrolling and be done with this. Otherwise we may open some
1358 // dialogs (Jug 20020424).
1359 if (cmd.button() == mouse_button::button4) {
1360 bv->scroll(-lyxrc.wheel_jump);
1363 if (cmd.button() == mouse_button::button5) {
1364 bv->scroll(lyxrc.wheel_jump);
1370 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1372 // Middle button press pastes if we have a selection
1373 // We do this here as if the selection was inside an inset
1374 // it could get cleared on the unlocking of the inset so
1375 // we have to check this first
1376 bool paste_internally = false;
1377 if (cmd.button() == mouse_button::button2 && selection.set()) {
1378 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1379 paste_internally = true;
1382 int const screen_first = bv->text->top_y();
1384 if (bv->theLockingInset()) {
1385 // We are in inset locking mode
1387 // Check whether the inset was hit. If not reset mode,
1388 // otherwise give the event to the inset
1389 if (inset_hit == bv->theLockingInset()) {
1390 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1391 bv->theLockingInset()->localDispatch(cmd1);
1394 bv->unlockInset(bv->theLockingInset());
1398 selection_possible = true;
1400 // Clear the selection
1401 bv->screen().toggleSelection(bv->text, bv);
1402 bv->text->clearSelection();
1403 bv->text->partialRebreak();
1405 bv->updateScrollbar();
1407 // Single left click in math inset?
1408 if (isHighlyEditableInset(inset_hit)) {
1409 // Highly editable inset, like math
1410 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1411 selection_possible = false;
1412 bv->owner()->message(inset->editMessage());
1413 // We just have to lock the inset before calling a PressEvent on it!
1414 if (!bv->lockInset(inset))
1415 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1416 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1417 inset->localDispatch(cmd1);
1420 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1422 // Right click on a footnote flag opens float menu
1423 if (cmd.button() == mouse_button::button3) {
1424 selection_possible = false;
1428 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1429 bv->text->setCursorFromCoordinates(x, y + screen_first);
1431 bv->text->selection.cursor = bv->text->cursor;
1432 bv->text->cursor.x_fix(bv->text->cursor.x());
1434 if (bv->fitCursor())
1435 selection_possible = false;
1437 // Insert primary selection with middle mouse
1438 // if there is a local selection in the current buffer,
1440 if (cmd.button() == mouse_button::button2) {
1441 if (paste_internally)
1442 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1444 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1445 selection_possible = false;
1450 case LFUN_MOUSE_RELEASE:
1452 // do nothing if we used the mouse wheel
1456 if (cmd.button() == mouse_button::button4
1457 || cmd.button() == mouse_button::button5)
1460 // If we hit an inset, we have the inset coordinates in these
1461 // and inset_hit points to the inset. If we do not hit an
1462 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1465 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1467 if (bv->theLockingInset()) {
1468 // We are in inset locking mode.
1470 // LyX does a kind of work-area grabbing for insets.
1471 // Only a ButtonPress FuncRequest outside the inset will
1472 // force a insetUnlock.
1473 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1474 bv->theLockingInset()->localDispatch(cmd1);
1478 selection_possible = false;
1480 if (cmd.button() == mouse_button::button2)
1484 if (cmd.button() == mouse_button::button1)
1485 bv->haveSelection(selection.set());
1488 bv->owner()->view_state_changed();
1489 bv->owner()->updateMenubar();
1490 bv->owner()->updateToolbar();
1492 // Did we hit an editable inset?
1494 selection_possible = false;
1496 // if we reach this point with a selection, it
1497 // must mean we are currently selecting.
1498 // But we don't want to open the inset
1499 // because that is annoying for the user.
1500 // So just pretend we didn't hit it.
1501 // this is OK because a "kosher" ButtonRelease
1502 // will follow a ButtonPress that clears
1504 // Note this also fixes selection drawing
1505 // problems if we end up opening an inset
1506 if (selection.set())
1509 // CHECK fix this proper in 0.13
1510 // well, maybe 13.0 !!!!!!!!!
1512 // Following a ref shouldn't issue
1513 // a push on the undo-stack
1514 // anylonger, now that we have
1515 // keybindings for following
1516 // references and returning from
1517 // references. IMHO though, it
1518 // should be the inset's own business
1519 // to push or not push on the undo
1520 // stack. They don't *have* to
1521 // alter the document...
1523 // ...or maybe the SetCursorParUndo()
1524 // below isn't necessary at all anylonger?
1525 if (inset_hit->lyxCode() == InsetOld::REF_CODE)
1526 recordUndo(bv, Undo::ATOMIC);
1528 bv->owner()->message(inset_hit->editMessage());
1530 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1531 inset_hit->localDispatch(cmd1);
1537 case LFUN_SELFINSERT: {
1538 if (cmd.argument.empty())
1541 // Automatically delete the currently selected
1542 // text and replace it with what is being
1543 // typed in now. Depends on lyxrc settings
1544 // "auto_region_delete", which defaults to
1547 if (lyxrc.auto_region_delete) {
1548 if (selection.set()) {
1549 cutSelection(false, false);
1552 bv->haveSelection(false);
1555 bv->beforeChange(this);
1556 LyXFont const old_font(real_current_font);
1558 string::const_iterator cit = cmd.argument.begin();
1559 string::const_iterator end = cmd.argument.end();
1560 for (; cit != end; ++cit)
1561 bv->owner()->getIntl().getTransManager().
1562 TranslateAndInsert(*cit, this);
1565 selection.cursor = cursor;
1566 moveCursorUpdate(bv, false);
1568 // real_current_font.number can change so we need to
1569 // update the minibuffer
1570 if (old_font != real_current_font)
1571 bv->owner()->view_state_changed();
1575 case LFUN_HTMLURL: {
1576 InsetCommandParams p("htmlurl");
1577 string const data = InsetCommandMailer::params2string("url", p);
1578 bv->owner()->getDialogs().show("url", data, 0);
1583 InsetCommandParams p("url");
1584 string const data = InsetCommandMailer::params2string("url", p);
1585 bv->owner()->getDialogs().show("url", data, 0);
1591 case LFUN_INSET_LIST:
1592 case LFUN_INSET_THEOREM:
1593 case LFUN_INSET_CAPTION:
1595 case LFUN_INSERT_NOTE:
1596 case LFUN_INSERT_BIBITEM:
1597 case LFUN_INSET_ERT:
1598 case LFUN_INSET_FLOAT:
1599 case LFUN_INSET_FOOTNOTE:
1600 case LFUN_INSET_MARGINAL:
1601 case LFUN_INSET_MINIPAGE:
1602 case LFUN_INSET_OPTARG:
1603 case LFUN_INSET_WIDE_FLOAT:
1604 case LFUN_INSET_WRAP:
1605 case LFUN_TABULAR_INSERT:
1606 case LFUN_ENVIRONMENT_INSERT:
1607 // Open the inset, and move the current selection
1609 doInsertInset(this, cmd, true, true);
1612 case LFUN_INDEX_INSERT:
1613 // Just open the inset
1614 doInsertInset(this, cmd, true, false);
1617 case LFUN_INDEX_PRINT:
1618 case LFUN_TOC_INSERT:
1621 doInsertInset(this, cmd, false, false);
1625 return UNDISPATCHED;