1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2002 The LyX Team.
9 * ====================================================== */
15 #include "paragraph.h"
16 #include "BufferView.h"
17 #include "funcrequest.h"
20 #include "bufferparams.h"
22 #include "ParagraphParameters.h"
28 #include "support/lstrings.h"
29 #include "frontends/LyXView.h"
30 #include "frontends/screen.h"
31 #include "frontends/Dialogs.h"
32 #include "insets/insetspecialchar.h"
33 #include "insets/insettext.h"
34 #include "insets/insetbib.h"
35 #include "insets/insetquotes.h"
36 #include "insets/insetcommand.h"
37 #include "undo_funcs.h"
46 extern string current_layout;
47 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
49 // the selection possible is needed, that only motion events are
50 // used, where the bottom press event was on the drawing area too
51 bool selection_possible = false;
56 void moveCursorUpdate(BufferView * bv, bool selecting)
58 LyXText * lt = bv->getLyXText();
60 if (selecting || lt->selection.mark()) {
65 bv->updateInset(lt->inset_owner, false);
69 // bv->update(lt, BufferView::SELECT|BufferView::FITCUR);
71 bv->update(lt, BufferView::SELECT|BufferView::FITCUR);
73 } else if (bv->text->status() != LyXText::UNCHANGED) {
74 bv->theLockingInset()->hideInsetCursor(bv);
75 bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
79 if (!lt->selection.set())
80 bv->haveSelection(false);
86 void finishChange(BufferView * bv, bool selecting = false)
89 moveCursorUpdate(bv, selecting);
90 bv->owner()->view_state_changed();
93 // check if the given co-ordinates are inside an inset at the
94 // given cursor, if one exists. If so, the inset is returned,
95 // and the co-ordinates are made relative. Otherwise, 0 is returned.
96 Inset * checkInset(BufferView * bv, LyXText const & text,
97 LyXCursor const & cur, int & x, int & y)
99 lyx::pos_type const pos = cur.pos();
100 Paragraph /*const*/ & par = *cur.par();
102 if (pos >= par.size() || !par.isInset(pos))
105 Inset /*const*/ * inset = par.getInset(pos);
107 if (!isEditableInset(inset))
110 // get inset dimensions
111 lyx::Assert(par.getInset(pos));
113 LyXFont const & font = text.getFont(bv->buffer(), &par, pos);
115 int const width = inset->width(bv, font);
116 int const inset_x = font.isVisibleRightToLeft()
117 ? (cur.ix() - width) : cur.ix();
120 inset_x + inset->scroll(),
122 cur.iy() - inset->ascent(bv, font),
123 cur.iy() + inset->descent(bv, font)
126 if (!b.contained(x, y)) {
127 lyxerr[Debug::GUI] << "Missed inset at x,y "
129 << " box " << b << endl;
133 text.setCursor(bv, &par, pos, true);
136 // The origin of an inset is on the baseline
137 y -= text.cursor.iy();
145 Inset * LyXText::checkInsetHit(BufferView * bv, int & x, int & y) const
147 int y_tmp = y + first_y;
150 setCursorFromCoordinates(bv, cur, x, y_tmp);
152 Inset * inset = checkInset(bv, *this, cur, x, y_tmp);
158 // look at previous position
163 setCursor(bv, cur, cur.par(), cur.pos() - 1, true);
165 inset = checkInset(bv, *this, cur, x, y_tmp);
172 bool LyXText::gotoNextInset(BufferView * bv,
173 vector<Inset::Code> const & codes, string const & contents) const
175 LyXCursor res = cursor;
178 if (res.pos() < res.par()->size() - 1) {
179 res.pos(res.pos() + 1);
181 res.par(res.par()->next());
185 } while (res.par() &&
186 !(res.par()->isInset(res.pos())
187 && (inset = res.par()->getInset(res.pos())) != 0
188 && find(codes.begin(), codes.end(), inset->lyxCode())
190 && (contents.empty() ||
191 static_cast<InsetCommand *>(
192 res.par()->getInset(res.pos()))->getContents()
196 setCursor(bv, res.par(), res.pos(), false);
203 void LyXText::gotoInset(BufferView * bv, vector<Inset::Code> const & codes,
207 bv->beforeChange(this);
211 if (same_content && cursor.par()->isInset(cursor.pos())) {
212 Inset const * inset = cursor.par()->getInset(cursor.pos());
213 if (find(codes.begin(), codes.end(), inset->lyxCode())
215 contents = static_cast<InsetCommand const *>(inset)->getContents();
218 if (!gotoNextInset(bv, codes, contents)) {
219 if (cursor.pos() || cursor.par() != ownerParagraph()) {
220 LyXCursor tmp = cursor;
221 cursor.par(ownerParagraph());
223 if (!gotoNextInset(bv, codes, contents)) {
225 bv->owner()->message(_("No more insets"));
228 bv->owner()->message(_("No more insets"));
232 selection.cursor = cursor;
236 void LyXText::gotoInset(BufferView * bv, Inset::Code code, bool same_content)
238 gotoInset(bv, vector<Inset::Code>(1, code), same_content);
242 void LyXText::cursorPrevious(BufferView * bv)
244 if (!cursor.row()->previous()) {
246 int new_y = bv->text->first_y - bv->workHeight();
247 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
248 bv->updateScrollbar();
254 Row * cursorrow = cursor.row();
256 setCursorFromCoordinates(bv, cursor.x_fix(), y);
260 if (cursorrow == bv->text->cursor.row()) {
261 // we have a row which is higher than the workarea so we leave the
262 // cursor on the start of the row and move only the draw up as soon
263 // as we move the cursor or do something while inside the row (it may
264 // span several workarea-heights) we'll move to the top again, but this
265 // is better than just jump down and only display part of the row.
266 new_y = bv->text->first_y - bv->workHeight();
269 new_y = bv->text->cursor.iy()
270 + bv->theLockingInset()->insetInInsetY() + y
271 + cursor.row()->height()
272 - bv->workHeight() + 1;
275 - cursor.row()->baseline()
276 + cursor.row()->height()
277 - bv->workHeight() + 1;
280 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
281 if (cursor.row()->previous()) {
283 setCursor(bv, cur, cursor.row()->previous()->par(),
284 cursor.row()->previous()->pos(), false);
285 if (cur.y() > first_y) {
289 bv->updateScrollbar();
293 void LyXText::cursorNext(BufferView * bv)
295 if (!cursor.row()->next()) {
296 int y = cursor.y() - cursor.row()->baseline() +
297 cursor.row()->height();
298 if (y > int(first_y + bv->workHeight())) {
299 bv->screen().draw(bv->text, bv,
300 bv->text->first_y + bv->workHeight());
301 bv->updateScrollbar();
306 int y = first_y + bv->workHeight();
307 if (inset_owner && !first_y) {
308 y -= (bv->text->cursor.iy()
310 + bv->theLockingInset()->insetInInsetY());
315 Row * cursorrow = cursor.row();
316 setCursorFromCoordinates(bv, cursor.x_fix(), y);
317 // + bv->workHeight());
321 if (cursorrow == bv->text->cursor.row()) {
322 // we have a row which is higher than the workarea so we leave the
323 // cursor on the start of the row and move only the draw down as soon
324 // as we move the cursor or do something while inside the row (it may
325 // span several workarea-heights) we'll move to the top again, but this
326 // is better than just jump down and only display part of the row.
327 new_y = bv->text->first_y + bv->workHeight();
330 new_y = bv->text->cursor.iy()
331 + bv->theLockingInset()->insetInInsetY()
332 + y - cursor.row()->baseline();
334 new_y = cursor.y() - cursor.row()->baseline();
337 bv->screen().draw(bv->text, bv, new_y);
338 if (cursor.row()->next()) {
340 setCursor(bv, cur, cursor.row()->next()->par(),
341 cursor.row()->next()->pos(), false);
342 if (cur.y() < int(first_y + bv->workHeight())) {
343 cursorDown(bv, true);
346 bv->updateScrollbar();
350 void LyXText::update(BufferView * bv, bool changed)
352 BufferView::UpdateCodes c = BufferView::SELECT | BufferView::FITCUR;
354 bv->update(this, c | BufferView::CHANGE);
360 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
364 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
365 if (!bv->insertInset(new_inset))
368 bv->updateInset(new_inset, true);
372 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
374 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
375 <<"] arg[" << cmd.argument << ']' << endl;
377 BufferView * bv = cmd.view();
379 switch (cmd.action) {
381 case LFUN_APPENDIX: {
382 Paragraph * par = cursor.par();
383 bool start = !par->params().startOfAppendix();
385 // ensure that we have only one start_of_appendix in this document
386 Paragraph * tmp = ownerParagraph();
387 for (; tmp; tmp = tmp->next()) {
388 if (tmp->params().startOfAppendix()) {
389 setUndo(bv, Undo::EDIT, tmp, tmp->next());
390 tmp->params().startOfAppendix(false);
395 setUndo(bv, Undo::EDIT, par, par->next());
396 par->params().startOfAppendix(start);
398 // we can set the refreshing parameters now
399 status(cmd.view(), LyXText::NEED_MORE_REFRESH);
401 refresh_row = 0; // not needed for full update
402 updateCounters(cmd.view());
403 setCursor(cmd.view(), cursor.par(), cursor.pos());
408 case LFUN_DELETE_WORD_FORWARD:
409 bv->beforeChange(this);
411 deleteWordForward(bv);
416 case LFUN_DELETE_WORD_BACKWARD:
417 bv->beforeChange(this);
419 deleteWordBackward(bv);
424 case LFUN_DELETE_LINE_FORWARD:
425 bv->beforeChange(this);
427 deleteLineForward(bv);
434 if (!selection.mark())
435 bv->beforeChange(this);
442 if (!selection.mark())
443 bv->beforeChange(this);
445 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
446 cursorLeftOneWord(bv);
448 cursorRightOneWord(bv);
453 if (!selection.mark())
454 bv->beforeChange(this);
456 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
457 cursorRightOneWord(bv);
459 cursorLeftOneWord(bv);
463 case LFUN_BEGINNINGBUF:
464 if (!selection.mark())
465 bv->beforeChange(this);
472 if (selection.mark())
473 bv->beforeChange(this);
481 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
485 finishChange(bv, true);
490 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
494 finishChange(bv, true);
500 finishChange(bv, true);
505 cursorDown(bv, true);
506 finishChange(bv, true);
509 case LFUN_UP_PARAGRAPHSEL:
511 cursorUpParagraph(bv);
512 finishChange(bv, true);
515 case LFUN_DOWN_PARAGRAPHSEL:
517 cursorDownParagraph(bv);
518 finishChange(bv, true);
524 finishChange(bv, true);
530 finishChange(bv, true);
536 finishChange(bv, true);
542 finishChange(bv, true);
545 case LFUN_WORDRIGHTSEL:
547 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
548 cursorLeftOneWord(bv);
550 cursorRightOneWord(bv);
551 finishChange(bv, true);
554 case LFUN_WORDLEFTSEL:
556 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
557 cursorRightOneWord(bv);
559 cursorLeftOneWord(bv);
560 finishChange(bv, true);
567 getWord(cur1, cur2, WHOLE_WORD);
568 setCursor(bv, cur1.par(), cur1.pos());
569 bv->beforeChange(this);
570 setCursor(bv, cur2.par(), cur2.pos());
571 finishChange(bv, true);
576 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
577 if (!selection.mark())
578 bv->beforeChange(this);
581 cursorLeft(bv, false);
582 if (cursor.pos() < cursor.par()->size()
583 && cursor.par()->isInset(cursor.pos())
584 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
585 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
586 cmd.message(tmpinset->editMessage());
587 tmpinset->edit(bv, !is_rtl);
591 cursorRight(bv, false);
597 // This is soooo ugly. Isn`t it possible to make
599 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
600 if (!selection.mark())
601 bv->beforeChange(this);
603 LyXCursor const cur = cursor;
605 cursorLeft(bv, false);
606 if ((is_rtl || cur != cursor) && // only if really moved!
607 cursor.pos() < cursor.par()->size() &&
608 cursor.par()->isInset(cursor.pos()) &&
609 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
610 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
611 cmd.message(tmpinset->editMessage());
612 tmpinset->edit(bv, is_rtl);
616 cursorRight(bv, false);
622 if (!selection.mark())
623 bv->beforeChange(this);
624 bv->update(this, BufferView::UPDATE);
630 if (!selection.mark())
631 bv->beforeChange(this);
632 bv->update(this, BufferView::UPDATE);
637 case LFUN_UP_PARAGRAPH:
638 if (!selection.mark())
639 bv->beforeChange(this);
640 bv->update(this, BufferView::UPDATE);
641 cursorUpParagraph(bv);
645 case LFUN_DOWN_PARAGRAPH:
646 if (!selection.mark())
647 bv->beforeChange(this);
648 bv->update(this, BufferView::UPDATE);
649 cursorDownParagraph(bv);
650 finishChange(bv, false);
654 if (!selection.mark())
655 bv->beforeChange(this);
656 bv->update(this, BufferView::UPDATE);
658 finishChange(bv, false);
661 // moveCursorUpdate(bv, false, false);
662 // owner_->view_state_changed();
666 if (!selection.mark())
667 bv->beforeChange(this);
668 bv->update(this, BufferView::UPDATE);
670 finishChange(bv, false);
674 if (!selection.mark())
675 bv->beforeChange(this);
678 finishChange(bv, false);
682 if (!selection.mark())
683 bv->beforeChange(this);
686 finishChange(bv, false);
690 bv->beforeChange(this);
691 insertChar(bv, Paragraph::META_NEWLINE);
693 setCursor(bv, cursor.par(), cursor.pos());
694 moveCursorUpdate(bv, false);
698 if (!selection.set()) {
700 selection.cursor = cursor;
702 // It is possible to make it a lot faster still
703 // just comment out the line below...
707 cutSelection(bv, true);
710 moveCursorUpdate(bv, false);
711 bv->owner()->view_state_changed();
715 case LFUN_DELETE_SKIP:
716 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
717 if (!selection.set()) {
718 LyXCursor cur = cursor;
719 if (cur.pos() == cur.par()->size()) {
723 && !(cur.par()->params().spaceTop()
724 == VSpace (VSpace::NONE))) {
726 cur.par()->params().lineTop(),
727 cur.par()->params().lineBottom(),
728 cur.par()->params().pagebreakTop(),
729 cur.par()->params().pagebreakBottom(),
730 VSpace(VSpace::NONE),
731 cur.par()->params().spaceBottom(),
732 cur.par()->params().spacing(),
733 cur.par()->params().align(),
734 cur.par()->params().labelWidthString(), 0);
740 selection.cursor = cursor;
744 selection.cursor = cursor;
748 cutSelection(bv, true);
755 if (!selection.set()) {
756 if (bv->owner()->getIntl().getTransManager().backspace()) {
758 selection.cursor = cursor;
760 // It is possible to make it a lot faster still
761 // just comment out the line below...
766 cutSelection(bv, true);
769 bv->owner()->view_state_changed();
773 case LFUN_BACKSPACE_SKIP:
774 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
775 if (!selection.set()) {
776 LyXCursor cur = cursor;
778 && !(cur.par()->params().spaceTop()
779 == VSpace (VSpace::NONE))) {
781 cur.par()->params().lineTop(),
782 cur.par()->params().lineBottom(),
783 cur.par()->params().pagebreakTop(),
784 cur.par()->params().pagebreakBottom(),
785 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
786 cur.par()->params().spacing(),
787 cur.par()->params().align(),
788 cur.par()->params().labelWidthString(), 0);
791 selection.cursor = cur;
795 cutSelection(bv, true);
800 case LFUN_BREAKPARAGRAPH:
801 bv->beforeChange(this);
802 breakParagraph(bv, 0);
804 selection.cursor = cursor;
806 bv->owner()->view_state_changed();
809 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
810 bv->beforeChange(this);
811 breakParagraph(bv, 1);
813 selection.cursor = cursor;
815 bv->owner()->view_state_changed();
818 case LFUN_BREAKPARAGRAPH_SKIP: {
819 // When at the beginning of a paragraph, remove
820 // indentation and add a "defskip" at the top.
821 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
822 LyXCursor cur = cursor;
823 bv->beforeChange(this);
824 if (cur.pos() == 0) {
825 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
827 cur.par()->params().lineTop(),
828 cur.par()->params().lineBottom(),
829 cur.par()->params().pagebreakTop(),
830 cur.par()->params().pagebreakBottom(),
831 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
832 cur.par()->params().spacing(),
833 cur.par()->params().align(),
834 cur.par()->params().labelWidthString(), 1);
839 breakParagraph(bv, 0);
843 selection.cursor = cur;
845 bv->owner()->view_state_changed();
849 case LFUN_PARAGRAPH_SPACING: {
850 Paragraph * par = cursor.par();
851 Spacing::Space cur_spacing = par->params().spacing().getSpace();
852 float cur_value = 1.0;
853 if (cur_spacing == Spacing::Other)
854 cur_value = par->params().spacing().getValue();
856 istringstream is(cmd.argument.c_str());
859 Spacing::Space new_spacing = cur_spacing;
860 float new_value = cur_value;
862 lyxerr << "Missing argument to `paragraph-spacing'"
864 } else if (tmp == "single") {
865 new_spacing = Spacing::Single;
866 } else if (tmp == "onehalf") {
867 new_spacing = Spacing::Onehalf;
868 } else if (tmp == "double") {
869 new_spacing = Spacing::Double;
870 } else if (tmp == "other") {
871 new_spacing = Spacing::Other;
874 lyxerr << "new_value = " << tmpval << endl;
877 } else if (tmp == "default") {
878 new_spacing = Spacing::Default;
880 lyxerr << _("Unknown spacing argument: ")
881 << cmd.argument << endl;
883 if (cur_spacing != new_spacing || cur_value != new_value) {
884 par->params().spacing(Spacing(new_spacing, new_value));
891 case LFUN_INSET_TOGGLE:
893 bv->beforeChange(this);
900 case LFUN_PROTECTEDSPACE:
901 if (cursor.par()->layout()->free_spacing) {
905 specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
907 moveCursorUpdate(bv, false);
910 case LFUN_HYPHENATION:
911 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
914 case LFUN_LIGATURE_BREAK:
915 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
919 specialChar(this, bv, InsetSpecialChar::LDOTS);
925 insertChar(bv, Paragraph::META_HFILL);
929 case LFUN_END_OF_SENTENCE:
930 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
933 case LFUN_MENU_SEPARATOR:
934 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
938 bv->beforeChange(this);
940 selection.cursor = cursor;
941 cmd.message(N_("Mark off"));
945 bv->beforeChange(this);
946 selection.mark(true);
948 selection.cursor = cursor;
949 cmd.message(N_("Mark on"));
953 bv->beforeChange(this);
954 if (selection.mark()) {
956 cmd.message(N_("Mark removed"));
958 selection.mark(true);
960 cmd.message(N_("Mark set"));
962 selection.cursor = cursor;
965 case LFUN_UPCASE_WORD:
967 changeCase(bv, LyXText::text_uppercase);
969 bv->updateInset(inset_owner, true);
973 case LFUN_LOWCASE_WORD:
975 changeCase(bv, LyXText::text_lowercase);
977 bv->updateInset(inset_owner, true);
981 case LFUN_CAPITALIZE_WORD:
983 changeCase(bv, LyXText::text_capitalization);
985 bv->updateInset(inset_owner, true);
989 case LFUN_TRANSPOSE_CHARS:
993 bv->updateInset(inset_owner, true);
998 cmd.message(_("Paste"));
1000 // clear the selection
1001 bv->toggleSelection();
1005 clearSelection(); // bug 393
1014 cutSelection(bv, true);
1016 cmd.message(_("Cut"));
1021 cmd.message(_("Copy"));
1024 case LFUN_BEGINNINGBUFSEL:
1026 return Inset::UNDISPATCHED;
1029 finishChange(bv, true);
1032 case LFUN_ENDBUFSEL:
1034 return Inset::UNDISPATCHED;
1037 finishChange(bv, true);
1041 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1047 istringstream is(cmd.argument.c_str());
1050 lyxerr << "SETXY: Could not parse coordinates in '"
1051 << cmd.argument << std::endl;
1053 setCursorFromCoordinates(bv, x, y);
1058 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1060 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1066 case LFUN_GETLAYOUT:
1067 cmd.message(tostr(cursor.par()->layout()));
1071 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1072 << cmd.argument << endl;
1074 // This is not the good solution to the empty argument
1075 // problem, but it will hopefully suffice for 1.2.0.
1076 // The correct solution would be to augument the
1077 // function list/array with information about what
1078 // functions needs arguments and their type.
1079 if (cmd.argument.empty()) {
1080 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1084 // Derive layout number from given argument (string)
1085 // and current buffer's textclass (number)
1086 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1087 bool hasLayout = tclass.hasLayout(cmd.argument);
1088 string layout = cmd.argument;
1090 // If the entry is obsolete, use the new one instead.
1092 string const & obs = tclass[layout]->obsoleted_by();
1098 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1103 bool change_layout = (current_layout != layout);
1104 if (!change_layout && selection.set() &&
1105 selection.start.par() != selection.end.par())
1107 Paragraph * spar = selection.start.par();
1108 Paragraph * epar = selection.end.par()->next();
1109 while (spar != epar) {
1110 if (spar->layout()->name() != current_layout) {
1111 change_layout = true;
1114 spar = spar->next();
1117 if (change_layout) {
1119 current_layout = layout;
1121 setLayout(bv, layout);
1122 bv->owner()->setLayout(layout);
1129 case LFUN_PASTESELECTION: {
1133 // this was originally a beforeChange(bv->text), i.e
1134 // the outermost LyXText!
1135 bv->beforeChange(this);
1136 string const clip = bv->getClipboard();
1137 if (!clip.empty()) {
1138 if (cmd.argument == "paragraph")
1139 insertStringAsParagraphs(bv, clip);
1141 insertStringAsLines(bv, clip);
1148 case LFUN_GOTOERROR:
1149 gotoInset(bv, Inset::ERROR_CODE, false);
1153 gotoInset(bv, Inset::NOTE_CODE, false);
1156 case LFUN_REFERENCE_GOTO:
1158 vector<Inset::Code> tmp;
1159 tmp.push_back(Inset::LABEL_CODE);
1160 tmp.push_back(Inset::REF_CODE);
1161 gotoInset(bv, tmp, true);
1166 Paragraph const * par = cursor.par();
1167 lyx::pos_type pos = cursor.pos();
1171 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1174 c = par->getChar(pos - 1);
1177 LyXLayout_ptr const & style = par->layout();
1179 if (style->pass_thru ||
1180 par->getFontSettings(bv->buffer()->params,
1181 pos).language()->lang() == "hebrew" ||
1182 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1183 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1187 case LFUN_DATE_INSERT: {
1188 time_t now_time_t = time(NULL);
1189 struct tm * now_tm = localtime(&now_time_t);
1190 setlocale(LC_TIME, "");
1192 if (!cmd.argument.empty())
1195 arg = lyxrc.date_insert_format;
1197 int const datetmp_len =
1198 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1200 for (int i = 0; i < datetmp_len; i++) {
1201 insertChar(bv, datetmp[i]);
1204 selection.cursor = cursor;
1205 moveCursorUpdate(bv, false);
1209 case LFUN_MOUSE_TRIPLE:
1212 if (bv_owner && bv->theLockingInset())
1214 if (cmd.button() == mouse_button::button1) {
1216 bv->screen().hideCursor();
1217 bv->screen().toggleSelection(this, bv);
1220 selection.cursor = cursor;
1224 bv->screen().toggleSelection(this, bv, false);
1226 bv->haveSelection(selection.set());
1230 case LFUN_MOUSE_DOUBLE:
1233 if (bv_owner && bv->theLockingInset())
1235 if (cmd.button() == mouse_button::button1) {
1237 bv->screen().hideCursor();
1238 bv->screen().toggleSelection(this, bv);
1239 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1240 bv->screen().toggleSelection(this, bv, false);
1242 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1245 bv->haveSelection(selection.set());
1249 case LFUN_MOUSE_MOTION:
1251 // Only use motion with button 1
1252 //if (ev.button() != mouse_button::button1)
1258 // Check for inset locking
1259 if (bv->theLockingInset()) {
1260 Inset * tli = bv->theLockingInset();
1261 LyXCursor cursor = bv->text->cursor;
1262 LyXFont font = bv->text->getFont(bv->buffer(),
1263 cursor.par(), cursor.pos());
1264 int width = tli->width(bv, font);
1265 int inset_x = font.isVisibleRightToLeft()
1266 ? cursor.ix() - width : cursor.ix();
1267 int start_x = inset_x + tli->scroll();
1268 FuncRequest cmd1 = cmd;
1269 cmd1.x = cmd.x - start_x;
1270 cmd1.y = cmd.y - cursor.iy() + bv->text->first_y;
1271 tli->localDispatch(cmd1);
1275 // The test for not selection possible is needed, that only motion
1276 // events are used, where the bottom press event was on
1277 // the drawing area too
1278 if (!selection_possible) {
1279 lyxerr[Debug::ACTION]
1280 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1284 bv->screen().hideCursor();
1286 Row * cursorrow = bv->text->cursor.row();
1287 bv->text->setCursorFromCoordinates(bv, cmd.x, cmd.y + bv->text->first_y);
1289 // sorry for this but I have a strange error that the y value jumps at
1290 // a certain point. This seems like an error in my xforms library or
1291 // in some other local environment, but I would like to leave this here
1292 // for the moment until I can remove this (Jug 20020418)
1293 if (y_before < bv->text->cursor.y())
1294 lyxerr << y_before << ':'
1295 << bv->text->cursor.y() << endl;
1297 // This is to allow jumping over large insets
1298 if (cursorrow == bv->text->cursor.row()) {
1299 if (cmd.y >= int(bv->workHeight()))
1300 bv->text->cursorDown(bv, false);
1302 bv->text->cursorUp(bv, false);
1305 // Maybe an empty line was deleted
1306 if (!bv->text->selection.set())
1307 bv->update(bv->text, BufferView::UPDATE);
1308 bv->text->setSelection(bv);
1309 bv->screen().toggleToggle(bv->text, bv);
1315 // Single-click on work area
1316 case LFUN_MOUSE_PRESS:
1321 // ok ok, this is a hack (for xforms)
1322 // We shouldn't go further down as we really should only do the
1323 // scrolling and be done with this. Otherwise we may open some
1324 // dialogs (Jug 20020424).
1325 if (cmd.button() == mouse_button::button4) {
1326 bv->scroll(-lyxrc.wheel_jump);
1329 if (cmd.button() == mouse_button::button5) {
1330 bv->scroll(lyxrc.wheel_jump);
1336 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1338 // Middle button press pastes if we have a selection
1339 // We do this here as if the selection was inside an inset
1340 // it could get cleared on the unlocking of the inset so
1341 // we have to check this first
1342 bool paste_internally = false;
1343 if (cmd.button() == mouse_button::button2 && selection.set()) {
1344 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1345 paste_internally = true;
1348 int const screen_first = bv->text->first_y;
1350 if (bv->theLockingInset()) {
1351 // We are in inset locking mode
1353 // Check whether the inset was hit. If not reset mode,
1354 // otherwise give the event to the inset
1355 if (inset_hit == bv->theLockingInset()) {
1356 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1357 bv->theLockingInset()->localDispatch(cmd1);
1360 bv->unlockInset(bv->theLockingInset());
1364 selection_possible = true;
1365 bv->screen().hideCursor();
1367 // Clear the selection
1368 bv->screen().toggleSelection(bv->text, bv);
1369 bv->text->clearSelection();
1370 bv->text->fullRebreak(bv);
1372 bv->updateScrollbar();
1374 // Single left click in math inset?
1375 if (isHighlyEditableInset(inset_hit)) {
1376 // Highly editable inset, like math
1377 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1378 selection_possible = false;
1379 bv->owner()->updateLayoutChoice();
1380 bv->owner()->message(inset->editMessage());
1381 //inset->edit(bv, x, y, cmd.button());
1382 // We just have to lock the inset before calling a PressEvent on it!
1383 // we don't need the edit() call here! (Jug20020329)
1384 if (!bv->lockInset(inset))
1385 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1386 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1387 inset->localDispatch(cmd1);
1390 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1392 // Right click on a footnote flag opens float menu
1393 if (cmd.button() == mouse_button::button3) {
1394 selection_possible = false;
1398 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1399 bv->text->setCursorFromCoordinates(bv, x, y + screen_first);
1401 bv->text->selection.cursor = bv->text->cursor;
1402 bv->text->cursor.x_fix(bv->text->cursor.x());
1404 bv->owner()->updateLayoutChoice();
1405 if (bv->fitCursor())
1406 selection_possible = false;
1408 // Insert primary selection with middle mouse
1409 // if there is a local selection in the current buffer,
1411 if (cmd.button() == mouse_button::button2) {
1412 if (paste_internally)
1413 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1415 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1416 selection_possible = false;
1421 case LFUN_MOUSE_RELEASE:
1423 // do nothing if we used the mouse wheel
1427 if (cmd.button() == mouse_button::button4
1428 || cmd.button() == mouse_button::button5)
1431 // If we hit an inset, we have the inset coordinates in these
1432 // and inset_hit points to the inset. If we do not hit an
1433 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1436 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1438 if (bv->theLockingInset()) {
1439 // We are in inset locking mode.
1441 // LyX does a kind of work-area grabbing for insets.
1442 // Only a ButtonPress FuncRequest outside the inset will
1443 // force a insetUnlock.
1444 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1445 bv->theLockingInset()->localDispatch(cmd1);
1449 selection_possible = false;
1451 if (cmd.button() == mouse_button::button2)
1455 if (cmd.button() == mouse_button::button1)
1456 bv->haveSelection(selection.set());
1459 bv->owner()->view_state_changed();
1460 bv->owner()->updateMenubar();
1461 bv->owner()->updateToolbar();
1463 // Did we hit an editable inset?
1465 selection_possible = false;
1467 // if we reach this point with a selection, it
1468 // must mean we are currently selecting.
1469 // But we don't want to open the inset
1470 // because that is annoying for the user.
1471 // So just pretend we didn't hit it.
1472 // this is OK because a "kosher" ButtonRelease
1473 // will follow a ButtonPress that clears
1475 // Note this also fixes selection drawing
1476 // problems if we end up opening an inset
1477 if (selection.set())
1480 // CHECK fix this proper in 0.13
1481 // well, maybe 13.0 !!!!!!!!!
1483 // Following a ref shouldn't issue
1484 // a push on the undo-stack
1485 // anylonger, now that we have
1486 // keybindings for following
1487 // references and returning from
1488 // references. IMHO though, it
1489 // should be the inset's own business
1490 // to push or not push on the undo
1491 // stack. They don't *have* to
1492 // alter the document...
1494 // ...or maybe the SetCursorParUndo()
1495 // below isn't necessary at all anylonger?
1496 if (inset_hit->lyxCode() == Inset::REF_CODE)
1497 setCursorParUndo(bv);
1499 bv->owner()->message(inset_hit->editMessage());
1501 if (isHighlyEditableInset(inset_hit)) {
1502 // Highly editable inset, like math
1503 UpdatableInset * inset = (UpdatableInset *) inset_hit;
1504 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1505 inset->localDispatch(cmd1);
1507 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1508 inset_hit->localDispatch(cmd1);
1509 // IMO this is a grosshack! Inset's should be changed so that
1510 // they call the actions they have to do with the insetButtonRel.
1511 // function and not in the edit(). This should be changed
1513 #ifdef WITH_WARNINGS
1514 #warning Please remove donot call inset->edit() here (Jug 20020812)
1516 inset_hit->edit(bv, x, y, cmd.button());
1521 // Maybe we want to edit a bibitem ale970302
1522 if (bv->text->cursor.par()->bibkey) {
1524 bv->text->cursor.par()->isRightToLeftPar(bv->buffer()->params);
1526 bibitemMaxWidth(bv, bv->buffer()->params.getLyXTextClass().defaultfont());
1527 if ((is_rtl && x > bv->text->workWidth(bv)-20-width) ||
1528 (!is_rtl && x < 20 + width)) {
1529 bv->text->cursor.par()->bibkey->edit(bv, 0, 0, mouse_button::none);
1535 case LFUN_SELFINSERT: {
1536 if (cmd.argument.empty())
1539 // Automatically delete the currently selected
1540 // text and replace it with what is being
1541 // typed in now. Depends on lyxrc settings
1542 // "auto_region_delete", which defaults to
1545 if (lyxrc.auto_region_delete) {
1546 if (selection.set()) {
1547 cutSelection(bv, false, false);
1550 bv->haveSelection(false);
1553 bv->beforeChange(this);
1554 LyXFont const old_font(real_current_font);
1556 string::const_iterator cit = cmd.argument.begin();
1557 string::const_iterator end = cmd.argument.end();
1558 for (; cit != end; ++cit)
1559 bv->owner()->getIntl().getTransManager().
1560 TranslateAndInsert(*cit, this);
1563 selection.cursor = cursor;
1564 moveCursorUpdate(bv, false);
1566 // real_current_font.number can change so we need to
1567 // update the minibuffer
1568 if (old_font != real_current_font)
1569 bv->owner()->view_state_changed();
1573 case LFUN_HTMLURL: {
1574 InsetCommandParams p("htmlurl");
1575 bv->owner()->getDialogs().createUrl(p.getAsString());
1580 InsetCommandParams p("url");
1581 bv->owner()->getDialogs().createUrl(p.getAsString());
1587 case LFUN_INSET_LIST:
1588 case LFUN_INSET_THEOREM:
1590 case LFUN_INSERT_NOTE:
1591 case LFUN_INSERT_URL:
1592 case LFUN_INSET_CAPTION:
1593 case LFUN_INSET_ERT:
1594 case LFUN_INSET_EXTERNAL:
1595 case LFUN_INSET_FLOAT:
1596 case LFUN_INSET_FOOTNOTE:
1597 case LFUN_INSET_MARGINAL:
1598 case LFUN_INSET_MINIPAGE:
1599 case LFUN_INSET_OPTARG:
1600 case LFUN_INSET_WIDE_FLOAT:
1601 case LFUN_INSET_WRAP:
1602 case LFUN_TABULAR_INSERT:
1603 case LFUN_INDEX_INSERT:
1604 case LFUN_INDEX_PRINT:
1605 case LFUN_PARENTINSERT:
1606 case LFUN_TOC_INSERT:
1608 Inset * inset = createInset(cmd);
1610 bool gotsel = false;
1611 if (selection.set()) {
1612 cutSelection(bv, true, false);
1615 if (bv->insertInset(inset)) {
1618 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
1628 return Inset::UNDISPATCHED;
1631 return Inset::DISPATCHED;