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);
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 replaceSelection(bv->getLyXText());
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 replaceSelection(bv->getLyXText());
835 breakParagraph(bv->buffer()->paragraphs, 0);
837 selection.cursor = cursor;
839 bv->owner()->view_state_changed();
842 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
843 replaceSelection(bv->getLyXText());
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 replaceSelection(bv->getLyXText());
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 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 recordUndo(bv, Undo::ATOMIC, cursor.par());
1020 if (transposeChars(cursor))
1021 checkParagraph(cursor.par(), cursor.pos());
1023 bv->updateInset(inset_owner);
1028 cmd.message(_("Paste"));
1029 replaceSelection(bv->getLyXText());
1030 size_t sel_index = 0;
1031 string const & arg = cmd.argument;
1032 if (isStrUnsignedInt(arg)) {
1033 size_t const paste_arg = strToUnsignedInt(arg);
1034 #warning FIXME Check if the arg is in the domain of available selections.
1035 sel_index = paste_arg;
1037 pasteSelection(sel_index);
1038 clearSelection(); // bug 393
1046 cutSelection(true, true);
1048 cmd.message(_("Cut"));
1053 cmd.message(_("Copy"));
1056 case LFUN_BEGINNINGBUFSEL:
1058 return UNDISPATCHED;
1061 finishChange(bv, true);
1064 case LFUN_ENDBUFSEL:
1066 return UNDISPATCHED;
1069 finishChange(bv, true);
1073 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1079 istringstream is(STRCONV(cmd.argument));
1082 lyxerr << "SETXY: Could not parse coordinates in '"
1083 << cmd.argument << std::endl;
1085 setCursorFromCoordinates(x, y);
1090 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1092 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1098 case LFUN_GETLAYOUT:
1099 cmd.message(tostr(cursor.par()->layout()));
1103 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1104 << cmd.argument << endl;
1106 // This is not the good solution to the empty argument
1107 // problem, but it will hopefully suffice for 1.2.0.
1108 // The correct solution would be to augument the
1109 // function list/array with information about what
1110 // functions needs arguments and their type.
1111 if (cmd.argument.empty()) {
1112 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1116 // Derive layout number from given argument (string)
1117 // and current buffer's textclass (number)
1118 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1119 bool hasLayout = tclass.hasLayout(cmd.argument);
1120 string layout = cmd.argument;
1122 // If the entry is obsolete, use the new one instead.
1124 string const & obs = tclass[layout]->obsoleted_by();
1130 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1135 bool change_layout = (current_layout != layout);
1137 if (!change_layout && selection.set() &&
1138 selection.start.par() != selection.end.par())
1140 ParagraphList::iterator spit = selection.start.par();
1141 ParagraphList::iterator epit = boost::next(selection.end.par());
1142 while (spit != epit) {
1143 if (spit->layout()->name() != current_layout) {
1144 change_layout = true;
1151 if (change_layout) {
1152 current_layout = layout;
1155 bv->owner()->setLayout(layout);
1162 case LFUN_PASTESELECTION: {
1165 // this was originally a beforeChange(bv->text), i.e
1166 // the outermost LyXText!
1167 bv->beforeChange(this);
1168 string const clip = bv->getClipboard();
1169 if (!clip.empty()) {
1170 if (cmd.argument == "paragraph")
1171 insertStringAsParagraphs(clip);
1173 insertStringAsLines(clip);
1180 case LFUN_GOTOERROR:
1181 gotoInset(InsetOld::ERROR_CODE, false);
1185 gotoInset(InsetOld::NOTE_CODE, false);
1188 case LFUN_REFERENCE_GOTO:
1190 vector<InsetOld::Code> tmp;
1191 tmp.push_back(InsetOld::LABEL_CODE);
1192 tmp.push_back(InsetOld::REF_CODE);
1193 gotoInset(tmp, true);
1198 replaceSelection(bv->getLyXText());
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 replaceSelection(bv->getLyXText());
1221 time_t now_time_t = time(NULL);
1222 struct tm * now_tm = localtime(&now_time_t);
1223 setlocale(LC_TIME, "");
1225 if (!cmd.argument.empty())
1228 arg = lyxrc.date_insert_format;
1230 int const datetmp_len =
1231 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1233 for (int i = 0; i < datetmp_len; i++) {
1234 insertChar(datetmp[i]);
1237 selection.cursor = cursor;
1238 moveCursorUpdate(bv, false);
1242 case LFUN_MOUSE_TRIPLE:
1245 if (!isInInset() && bv->theLockingInset())
1247 if (cmd.button() == mouse_button::button1) {
1249 bv->screen().toggleSelection(this, bv);
1252 selection.cursor = cursor;
1256 bv->screen().toggleSelection(this, bv, false);
1258 bv->haveSelection(selection.set());
1262 case LFUN_MOUSE_DOUBLE:
1265 if (!isInInset() && bv->theLockingInset())
1267 if (cmd.button() == mouse_button::button1) {
1269 bv->screen().toggleSelection(this, bv);
1270 selectWord(lyx::WHOLE_WORD_STRICT);
1271 bv->screen().toggleSelection(this, bv, false);
1273 selectWord(lyx::WHOLE_WORD_STRICT);
1276 bv->haveSelection(selection.set());
1280 case LFUN_MOUSE_MOTION:
1282 // Only use motion with button 1
1283 //if (ev.button() != mouse_button::button1)
1289 // Check for inset locking
1290 if (bv->theLockingInset()) {
1291 InsetOld * tli = bv->theLockingInset();
1292 LyXCursor cursor = bv->text->cursor;
1293 LyXFont font = bv->text->getFont(cursor.par(), cursor.pos());
1294 int width = tli->width();
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();
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 InsetOld * 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->partialRebreak();
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 InsetOld * 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() == InsetOld::REF_CODE)
1519 recordUndo(bv, Undo::ATOMIC);
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;