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/WorkArea.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"
44 extern string current_layout;
45 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
47 // the selection possible is needed, that only motion events are
48 // used, where the bottom press event was on the drawing area too
49 bool selection_possible = false;
54 void moveCursorUpdate(BufferView * bv, bool selecting)
56 LyXText * lt = bv->getLyXText();
58 if (selecting || lt->selection.mark()) {
63 bv->updateInset(lt->inset_owner, false);
67 // bv->update(lt, BufferView::SELECT|BufferView::FITCUR);
69 bv->update(lt, BufferView::SELECT|BufferView::FITCUR);
71 } else if (bv->text->status() != LyXText::UNCHANGED) {
72 bv->theLockingInset()->hideInsetCursor(bv);
73 bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
77 if (!lt->selection.set())
78 bv->workarea().haveSelection(false);
84 void finishChange(BufferView * bv, bool selecting = false)
87 moveCursorUpdate(bv, selecting);
88 bv->owner()->view_state_changed();
91 // check if the given co-ordinates are inside an inset at the
92 // given cursor, if one exists. If so, the inset is returned,
93 // and the co-ordinates are made relative. Otherwise, 0 is returned.
94 Inset * checkInset(BufferView * bv, LyXText const & text, int & x, int & y)
96 LyXCursor const & cursor = text.cursor;
97 lyx::pos_type const pos = cursor.pos();
98 Paragraph /*const*/ & par = *cursor.par();
100 if (pos >= par.size() || !par.isInset(pos))
103 Inset /*const*/ * inset = par.getInset(pos);
105 if (!isEditableInset(inset))
108 // get inset dimensions
109 lyx::Assert(par.getInset(pos));
111 LyXFont const & font = text.getFont(bv->buffer(), &par, pos);
113 int const width = inset->width(bv, font);
114 int const inset_x = font.isVisibleRightToLeft()
115 ? (cursor.ix() - width) : cursor.ix();
118 inset_x + inset->scroll(),
120 cursor.iy() - inset->ascent(bv, font),
121 cursor.iy() + inset->descent(bv, font));
123 if (!b.contained(x, y)) {
124 lyxerr[Debug::GUI] << "Missed inset at x,y " << x << "," << y
125 << " box " << b << endl;
129 text.setCursor(bv, &par, pos, true);
132 // The origin of an inset is on the baseline
133 y -= text.cursor.iy();
141 Inset * LyXText::checkInsetHit(BufferView * bv, int & x, int & y) const
143 int y_tmp = y + first_y;
146 setCursorFromCoordinates(bv, cursor, x, y_tmp);
148 Inset * inset = checkInset(bv, *this, x, y_tmp);
154 // look at previous position
155 if (cursor.pos() == 0)
159 setCursor(bv, cursor, cursor.par(), cursor.pos() - 1, true);
161 inset = checkInset(bv, *this, x, y_tmp);
168 bool LyXText::gotoNextInset(BufferView * bv,
169 vector<Inset::Code> const & codes, string const & contents) const
171 LyXCursor res = cursor;
174 if (res.pos() < res.par()->size() - 1) {
175 res.pos(res.pos() + 1);
177 res.par(res.par()->next());
181 } while (res.par() &&
182 !(res.par()->isInset(res.pos())
183 && (inset = res.par()->getInset(res.pos())) != 0
184 && find(codes.begin(), codes.end(), inset->lyxCode())
186 && (contents.empty() ||
187 static_cast<InsetCommand *>(
188 res.par()->getInset(res.pos()))->getContents()
192 setCursor(bv, res.par(), res.pos(), false);
199 void LyXText::gotoInset(BufferView * bv, vector<Inset::Code> const & codes,
203 bv->beforeChange(this);
207 if (same_content && cursor.par()->isInset(cursor.pos())) {
208 Inset const * inset = cursor.par()->getInset(cursor.pos());
209 if (find(codes.begin(), codes.end(), inset->lyxCode())
211 contents = static_cast<InsetCommand const *>(inset)->getContents();
214 if (!gotoNextInset(bv, codes, contents)) {
215 if (cursor.pos() || cursor.par() != ownerParagraph()) {
216 LyXCursor tmp = cursor;
217 cursor.par(ownerParagraph());
219 if (!gotoNextInset(bv, codes, contents)) {
221 bv->owner()->message(_("No more insets"));
224 bv->owner()->message(_("No more insets"));
228 selection.cursor = cursor;
232 void LyXText::gotoInset(BufferView * bv, Inset::Code code, bool same_content)
234 gotoInset(bv, vector<Inset::Code>(1, code), same_content);
238 void LyXText::cursorPrevious(BufferView * bv)
240 if (!cursor.row()->previous()) {
242 int new_y = bv->text->first_y - bv->workarea().workHeight();
243 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
244 bv->updateScrollbar();
250 Row * cursorrow = cursor.row();
252 setCursorFromCoordinates(bv, cursor.x_fix(), y);
256 if (cursorrow == bv->text->cursor.row()) {
257 // we have a row which is higher than the workarea so we leave the
258 // cursor on the start of the row and move only the draw up as soon
259 // as we move the cursor or do something while inside the row (it may
260 // span several workarea-heights) we'll move to the top again, but this
261 // is better than just jump down and only display part of the row.
262 new_y = bv->text->first_y - bv->workarea().workHeight();
265 new_y = bv->text->cursor.iy()
266 + bv->theLockingInset()->insetInInsetY() + y
267 + cursor.row()->height()
268 - bv->workarea().workHeight() + 1;
271 - cursor.row()->baseline()
272 + cursor.row()->height()
273 - bv->workarea().workHeight() + 1;
276 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
277 if (cursor.row()->previous()) {
279 setCursor(bv, cur, cursor.row()->previous()->par(),
280 cursor.row()->previous()->pos(), false);
281 if (cur.y() > first_y) {
285 bv->updateScrollbar();
289 void LyXText::cursorNext(BufferView * bv)
291 if (!cursor.row()->next()) {
292 int y = cursor.y() - cursor.row()->baseline() +
293 cursor.row()->height();
294 if (y > int(first_y + bv->workarea().workHeight())) {
295 bv->screen().draw(bv->text, bv,
296 bv->text->first_y + bv->workarea().workHeight());
297 bv->updateScrollbar();
302 int y = first_y + bv->workarea().workHeight();
303 if (inset_owner && !first_y) {
304 y -= (bv->text->cursor.iy()
306 + bv->theLockingInset()->insetInInsetY());
311 Row * cursorrow = cursor.row();
312 setCursorFromCoordinates(bv, cursor.x_fix(), y);
313 // + workarea().workHeight());
317 if (cursorrow == bv->text->cursor.row()) {
318 // we have a row which is higher than the workarea so we leave the
319 // cursor on the start of the row and move only the draw down as soon
320 // as we move the cursor or do something while inside the row (it may
321 // span several workarea-heights) we'll move to the top again, but this
322 // is better than just jump down and only display part of the row.
323 new_y = bv->text->first_y + bv->workarea().workHeight();
326 new_y = bv->text->cursor.iy()
327 + bv->theLockingInset()->insetInInsetY()
328 + y - cursor.row()->baseline();
330 new_y = cursor.y() - cursor.row()->baseline();
333 bv->screen().draw(bv->text, bv, new_y);
334 if (cursor.row()->next()) {
336 setCursor(bv, cur, cursor.row()->next()->par(),
337 cursor.row()->next()->pos(), false);
338 if (cur.y() < int(first_y + bv->workarea().workHeight())) {
339 cursorDown(bv, true);
342 bv->updateScrollbar();
346 void LyXText::update(BufferView * bv, bool changed)
348 BufferView::UpdateCodes c = BufferView::SELECT | BufferView::FITCUR;
350 bv->update(this, c | BufferView::CHANGE);
356 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
360 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
361 if (!bv->insertInset(new_inset))
364 bv->updateInset(new_inset, true);
368 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
370 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
371 <<"] arg[" << cmd.argument << "]" << endl;
373 BufferView * bv = cmd.view();
375 switch (cmd.action) {
377 case LFUN_APPENDIX: {
378 Paragraph * par = cursor.par();
379 bool start = !par->params().startOfAppendix();
381 // ensure that we have only one start_of_appendix in this document
382 Paragraph * tmp = ownerParagraph();
383 for (; tmp; tmp = tmp->next())
384 tmp->params().startOfAppendix(false);
386 par->params().startOfAppendix(start);
388 // we can set the refreshing parameters now
389 status(cmd.view(), LyXText::NEED_MORE_REFRESH);
391 refresh_row = 0; // not needed for full update
392 updateCounters(cmd.view());
393 setCursor(cmd.view(), cursor.par(), cursor.pos());
398 case LFUN_DELETE_WORD_FORWARD:
399 bv->beforeChange(this);
401 deleteWordForward(bv);
406 case LFUN_DELETE_WORD_BACKWARD:
407 bv->beforeChange(this);
409 deleteWordBackward(bv);
414 case LFUN_DELETE_LINE_FORWARD:
415 bv->beforeChange(this);
417 deleteLineForward(bv);
424 if (!selection.mark())
425 bv->beforeChange(this);
432 if (!selection.mark())
433 bv->beforeChange(this);
435 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
436 cursorLeftOneWord(bv);
438 cursorRightOneWord(bv);
443 if (!selection.mark())
444 bv->beforeChange(this);
446 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
447 cursorRightOneWord(bv);
449 cursorLeftOneWord(bv);
453 case LFUN_BEGINNINGBUF:
454 if (!selection.mark())
455 bv->beforeChange(this);
462 if (selection.mark())
463 bv->beforeChange(this);
471 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
475 finishChange(bv, true);
480 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
484 finishChange(bv, true);
490 finishChange(bv, true);
495 cursorDown(bv, true);
496 finishChange(bv, true);
499 case LFUN_UP_PARAGRAPHSEL:
501 cursorUpParagraph(bv);
502 finishChange(bv, true);
505 case LFUN_DOWN_PARAGRAPHSEL:
507 cursorDownParagraph(bv);
508 finishChange(bv, true);
514 finishChange(bv, true);
520 finishChange(bv, true);
526 finishChange(bv, true);
532 finishChange(bv, true);
535 case LFUN_WORDRIGHTSEL:
537 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
538 cursorLeftOneWord(bv);
540 cursorRightOneWord(bv);
541 finishChange(bv, true);
544 case LFUN_WORDLEFTSEL:
546 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
547 cursorRightOneWord(bv);
549 cursorLeftOneWord(bv);
550 finishChange(bv, true);
554 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
555 if (!selection.mark())
556 bv->beforeChange(this);
559 cursorLeft(bv, false);
560 if (cursor.pos() < cursor.par()->size()
561 && cursor.par()->isInset(cursor.pos())
562 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
563 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
564 cmd.message(tmpinset->editMessage());
565 tmpinset->edit(bv, !is_rtl);
569 cursorRight(bv, false);
575 // This is soooo ugly. Isn`t it possible to make
577 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
578 if (!selection.mark())
579 bv->beforeChange(this);
581 LyXCursor const cur = cursor;
583 cursorLeft(bv, false);
584 if ((is_rtl || cur != cursor) && // only if really moved!
585 cursor.pos() < cursor.par()->size() &&
586 cursor.par()->isInset(cursor.pos()) &&
587 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
588 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
589 cmd.message(tmpinset->editMessage());
590 tmpinset->edit(bv, is_rtl);
594 cursorRight(bv, false);
600 if (!selection.mark())
601 bv->beforeChange(this);
602 bv->update(this, BufferView::UPDATE);
608 if (!selection.mark())
609 bv->beforeChange(this);
610 bv->update(this, BufferView::UPDATE);
615 case LFUN_UP_PARAGRAPH:
616 if (!selection.mark())
617 bv->beforeChange(this);
618 bv->update(this, BufferView::UPDATE);
619 cursorUpParagraph(bv);
623 case LFUN_DOWN_PARAGRAPH:
624 if (!selection.mark())
625 bv->beforeChange(this);
626 bv->update(this, BufferView::UPDATE);
627 cursorDownParagraph(bv);
628 finishChange(bv, false);
632 if (!selection.mark())
633 bv->beforeChange(this);
634 bv->update(this, BufferView::UPDATE);
636 finishChange(bv, false);
639 // moveCursorUpdate(bv, false, false);
640 // owner_->view_state_changed();
644 if (!selection.mark())
645 bv->beforeChange(this);
646 bv->update(this, BufferView::UPDATE);
648 finishChange(bv, false);
652 if (!selection.mark())
653 bv->beforeChange(this);
656 finishChange(bv, false);
660 if (!selection.mark())
661 bv->beforeChange(this);
664 finishChange(bv, false);
668 bv->beforeChange(this);
669 insertChar(bv, Paragraph::META_NEWLINE);
671 setCursor(bv, cursor.par(), cursor.pos());
672 moveCursorUpdate(bv, false);
676 if (!selection.set()) {
678 selection.cursor = cursor;
680 // It is possible to make it a lot faster still
681 // just comment out the line below...
685 cutSelection(bv, true);
688 moveCursorUpdate(bv, false);
689 bv->owner()->view_state_changed();
693 case LFUN_DELETE_SKIP:
694 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
695 if (!selection.set()) {
696 LyXCursor cur = cursor;
697 if (cur.pos() == cur.par()->size()) {
701 && !(cur.par()->params().spaceTop()
702 == VSpace (VSpace::NONE))) {
704 cur.par()->params().lineTop(),
705 cur.par()->params().lineBottom(),
706 cur.par()->params().pagebreakTop(),
707 cur.par()->params().pagebreakBottom(),
708 VSpace(VSpace::NONE),
709 cur.par()->params().spaceBottom(),
710 cur.par()->params().spacing(),
711 cur.par()->params().align(),
712 cur.par()->params().labelWidthString(), 0);
718 selection.cursor = cursor;
722 selection.cursor = cursor;
726 cutSelection(bv, true);
733 if (!selection.set()) {
734 if (bv->owner()->getIntl().getTransManager().backspace()) {
736 selection.cursor = cursor;
738 // It is possible to make it a lot faster still
739 // just comment out the line below...
744 cutSelection(bv, true);
747 bv->owner()->view_state_changed();
751 case LFUN_BACKSPACE_SKIP:
752 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
753 if (!selection.set()) {
754 LyXCursor cur = cursor;
756 && !(cur.par()->params().spaceTop()
757 == VSpace (VSpace::NONE))) {
759 cur.par()->params().lineTop(),
760 cur.par()->params().lineBottom(),
761 cur.par()->params().pagebreakTop(),
762 cur.par()->params().pagebreakBottom(),
763 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
764 cur.par()->params().spacing(),
765 cur.par()->params().align(),
766 cur.par()->params().labelWidthString(), 0);
769 selection.cursor = cur;
773 cutSelection(bv, true);
778 case LFUN_BREAKPARAGRAPH:
779 bv->beforeChange(this);
780 breakParagraph(bv, 0);
782 selection.cursor = cursor;
784 bv->owner()->view_state_changed();
787 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
788 bv->beforeChange(this);
789 breakParagraph(bv, 1);
791 selection.cursor = cursor;
793 bv->owner()->view_state_changed();
796 case LFUN_BREAKPARAGRAPH_SKIP: {
797 // When at the beginning of a paragraph, remove
798 // indentation and add a "defskip" at the top.
799 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
800 LyXCursor cur = cursor;
801 bv->beforeChange(this);
802 if (cur.pos() == 0) {
803 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
805 cur.par()->params().lineTop(),
806 cur.par()->params().lineBottom(),
807 cur.par()->params().pagebreakTop(),
808 cur.par()->params().pagebreakBottom(),
809 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
810 cur.par()->params().spacing(),
811 cur.par()->params().align(),
812 cur.par()->params().labelWidthString(), 1);
817 breakParagraph(bv, 0);
821 selection.cursor = cur;
823 bv->owner()->view_state_changed();
827 case LFUN_PARAGRAPH_SPACING: {
828 Paragraph * par = cursor.par();
829 Spacing::Space cur_spacing = par->params().spacing().getSpace();
830 float cur_value = 1.0;
831 if (cur_spacing == Spacing::Other)
832 cur_value = par->params().spacing().getValue();
834 istringstream is(cmd.argument.c_str());
837 Spacing::Space new_spacing = cur_spacing;
838 float new_value = cur_value;
840 lyxerr << "Missing argument to `paragraph-spacing'"
842 } else if (tmp == "single") {
843 new_spacing = Spacing::Single;
844 } else if (tmp == "onehalf") {
845 new_spacing = Spacing::Onehalf;
846 } else if (tmp == "double") {
847 new_spacing = Spacing::Double;
848 } else if (tmp == "other") {
849 new_spacing = Spacing::Other;
852 lyxerr << "new_value = " << tmpval << endl;
855 } else if (tmp == "default") {
856 new_spacing = Spacing::Default;
858 lyxerr << _("Unknown spacing argument: ")
859 << cmd.argument << endl;
861 if (cur_spacing != new_spacing || cur_value != new_value) {
862 par->params().spacing(Spacing(new_spacing, new_value));
869 case LFUN_INSET_TOGGLE:
871 bv->beforeChange(this);
878 case LFUN_PROTECTEDSPACE:
879 if (cursor.par()->layout()->free_spacing) {
883 specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
885 moveCursorUpdate(bv, false);
888 case LFUN_HYPHENATION:
889 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
892 case LFUN_LIGATURE_BREAK:
893 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
897 specialChar(this, bv, InsetSpecialChar::LDOTS);
903 insertChar(bv, Paragraph::META_HFILL);
907 case LFUN_END_OF_SENTENCE:
908 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
911 case LFUN_MENU_SEPARATOR:
912 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
916 bv->beforeChange(this);
918 selection.cursor = cursor;
919 cmd.message(N_("Mark off"));
923 bv->beforeChange(this);
924 selection.mark(true);
926 selection.cursor = cursor;
927 cmd.message(N_("Mark on"));
931 bv->beforeChange(this);
932 if (selection.mark()) {
934 cmd.message(N_("Mark removed"));
936 selection.mark(true);
938 cmd.message(N_("Mark set"));
940 selection.cursor = cursor;
943 case LFUN_UPCASE_WORD:
945 changeCase(bv, LyXText::text_uppercase);
947 bv->updateInset(inset_owner, true);
951 case LFUN_LOWCASE_WORD:
953 changeCase(bv, LyXText::text_lowercase);
955 bv->updateInset(inset_owner, true);
959 case LFUN_CAPITALIZE_WORD:
961 changeCase(bv, LyXText::text_capitalization);
963 bv->updateInset(inset_owner, true);
967 case LFUN_TRANSPOSE_CHARS:
971 bv->updateInset(inset_owner, true);
976 cmd.message(_("Paste"));
978 // clear the selection
979 bv->toggleSelection();
983 clearSelection(); // bug 393
992 cutSelection(bv, true);
994 cmd.message(_("Cut"));
999 cmd.message(_("Copy"));
1002 case LFUN_BEGINNINGBUFSEL:
1004 return Inset::UNDISPATCHED;
1007 finishChange(bv, true);
1010 case LFUN_ENDBUFSEL:
1012 return Inset::UNDISPATCHED;
1015 finishChange(bv, true);
1019 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1025 istringstream is(cmd.argument.c_str());
1028 lyxerr << "SETXY: Could not parse coordinates in '"
1029 << cmd.argument << std::endl;
1031 setCursorFromCoordinates(bv, x, y);
1036 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1038 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1044 case LFUN_GETLAYOUT:
1045 cmd.message(tostr(cursor.par()->layout()));
1049 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1050 << cmd.argument << endl;
1052 // This is not the good solution to the empty argument
1053 // problem, but it will hopefully suffice for 1.2.0.
1054 // The correct solution would be to augument the
1055 // function list/array with information about what
1056 // functions needs arguments and their type.
1057 if (cmd.argument.empty()) {
1058 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1062 // Derive layout number from given argument (string)
1063 // and current buffer's textclass (number)
1064 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1065 bool hasLayout = tclass.hasLayout(cmd.argument);
1066 string layout = cmd.argument;
1068 // If the entry is obsolete, use the new one instead.
1070 string const & obs = tclass[layout]->obsoleted_by();
1076 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1081 bool change_layout = (current_layout != layout);
1082 if (!change_layout && selection.set() &&
1083 selection.start.par() != selection.end.par())
1085 Paragraph * spar = selection.start.par();
1086 Paragraph * epar = selection.end.par()->next();
1087 while (spar != epar) {
1088 if (spar->layout()->name() != current_layout) {
1089 change_layout = true;
1094 if (change_layout) {
1096 current_layout = layout;
1098 setLayout(bv, layout);
1099 bv->owner()->setLayout(layout);
1106 case LFUN_PASTESELECTION: {
1110 // this was originally a beforeChange(bv->text), i.e
1111 // the outermost LyXText!
1112 bv->beforeChange(this);
1113 string const clip = bv->workarea().getClipboard();
1114 if (!clip.empty()) {
1115 if (cmd.argument == "paragraph")
1116 insertStringAsParagraphs(bv, clip);
1118 insertStringAsLines(bv, clip);
1125 case LFUN_GOTOERROR:
1126 gotoInset(bv, Inset::ERROR_CODE, false);
1130 gotoInset(bv, Inset::NOTE_CODE, false);
1133 case LFUN_REFERENCE_GOTO:
1135 vector<Inset::Code> tmp;
1136 tmp.push_back(Inset::LABEL_CODE);
1137 tmp.push_back(Inset::REF_CODE);
1138 gotoInset(bv, tmp, true);
1143 case LFUN_INSET_LIST:
1144 case LFUN_INSET_THEOREM:
1146 case LFUN_INSERT_NOTE:
1147 case LFUN_INSET_ERT:
1148 case LFUN_INSET_EXTERNAL:
1149 case LFUN_INSET_FLOAT:
1150 case LFUN_INSET_FOOTNOTE:
1151 case LFUN_INSET_MARGINAL:
1152 case LFUN_INSET_MINIPAGE:
1153 case LFUN_INSET_OPTARG:
1154 case LFUN_INSET_WIDE_FLOAT:
1155 case LFUN_TABULAR_INSERT:
1157 Inset * inset = createInset(cmd);
1159 bool gotsel = false;
1160 if (selection.set()) {
1161 cutSelection(bv, true, false);
1164 if (bv->insertInset(inset)) {
1167 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
1177 Paragraph const * par = cursor.par();
1178 lyx::pos_type pos = cursor.pos();
1182 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1185 c = par->getChar(pos - 1);
1188 LyXLayout_ptr const & style = par->layout();
1190 if (style->pass_thru ||
1191 par->getFontSettings(bv->buffer()->params,
1192 pos).language()->lang() == "hebrew" ||
1193 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1194 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1198 case LFUN_DATE_INSERT: {
1199 time_t now_time_t = time(NULL);
1200 struct tm * now_tm = localtime(&now_time_t);
1201 setlocale(LC_TIME, "");
1203 if (!cmd.argument.empty())
1206 arg = lyxrc.date_insert_format;
1208 int const datetmp_len =
1209 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1211 for (int i = 0; i < datetmp_len; i++) {
1212 insertChar(bv, datetmp[i]);
1215 selection.cursor = cursor;
1216 moveCursorUpdate(bv, false);
1220 case LFUN_MOUSE_TRIPLE:
1223 if (bv_owner && bv->theLockingInset())
1225 if (cmd.button() == mouse_button::button1) {
1227 bv->screen().hideCursor();
1228 bv->screen().toggleSelection(this, bv);
1231 selection.cursor = cursor;
1235 bv->screen().toggleSelection(this, bv, false);
1237 bv->workarea().haveSelection(selection.set());
1241 case LFUN_MOUSE_DOUBLE:
1244 if (bv_owner && bv->theLockingInset())
1246 if (cmd.button() == mouse_button::button1) {
1248 bv->screen().hideCursor();
1249 bv->screen().toggleSelection(this, bv);
1250 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1251 bv->screen().toggleSelection(this, bv, false);
1253 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1256 bv->workarea().haveSelection(selection.set());
1260 case LFUN_MOUSE_MOTION:
1262 // Only use motion with button 1
1263 //if (ev.button() != mouse_button::button1)
1269 // Check for inset locking
1270 if (bv->theLockingInset()) {
1271 Inset * tli = bv->theLockingInset();
1272 LyXCursor cursor = bv->text->cursor;
1273 LyXFont font = bv->text->getFont(bv->buffer(),
1274 cursor.par(), cursor.pos());
1275 int width = tli->width(bv, font);
1276 int inset_x = font.isVisibleRightToLeft()
1277 ? cursor.ix() - width : cursor.ix();
1278 int start_x = inset_x + tli->scroll();
1279 FuncRequest cmd1 = cmd;
1281 cmd1.y -= cursor.iy() + bv->text->first_y;
1282 tli->localDispatch(cmd1);
1286 // The test for not selection possible is needed, that only motion
1287 // events are used, where the bottom press event was on
1288 // the drawing area too
1289 if (!selection_possible) {
1290 lyxerr[Debug::ACTION]
1291 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1295 bv->screen().hideCursor();
1297 Row * cursorrow = bv->text->cursor.row();
1298 bv->text->setCursorFromCoordinates(bv, cmd.x, cmd.y + bv->text->first_y);
1300 // sorry for this but I have a strange error that the y value jumps at
1301 // a certain point. This seems like an error in my xforms library or
1302 // in some other local environment, but I would like to leave this here
1303 // for the moment until I can remove this (Jug 20020418)
1304 if (y_before < bv->text->cursor.y())
1305 lyxerr << y_before << ":" << bv->text->cursor.y() << endl;
1307 // This is to allow jumping over large insets
1308 if (cursorrow == bv->text->cursor.row()) {
1309 if (cmd.y >= int(bv->workarea().workHeight()))
1310 bv->text->cursorDown(bv, false);
1312 bv->text->cursorUp(bv, false);
1315 // Maybe an empty line was deleted
1316 if (!bv->text->selection.set())
1317 bv->update(bv->text, BufferView::UPDATE);
1318 bv->text->setSelection(bv);
1319 bv->screen().toggleToggle(bv->text, bv);
1325 // Single-click on work area
1326 case LFUN_MOUSE_PRESS:
1331 // ok ok, this is a hack (for xforms)
1332 // We shouldn't go further down as we really should only do the
1333 // scrolling and be done with this. Otherwise we may open some
1334 // dialogs (Jug 20020424).
1335 if (cmd.button() == mouse_button::button4) {
1336 bv->scroll(-lyxrc.wheel_jump);
1339 if (cmd.button() == mouse_button::button5) {
1340 bv->scroll(lyxrc.wheel_jump);
1346 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1348 // Middle button press pastes if we have a selection
1349 // We do this here as if the selection was inside an inset
1350 // it could get cleared on the unlocking of the inset so
1351 // we have to check this first
1352 bool paste_internally = false;
1353 if (cmd.button() == mouse_button::button2 && selection.set()) {
1354 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1355 paste_internally = true;
1358 int const screen_first = bv->text->first_y;
1360 if (bv->theLockingInset()) {
1361 // We are in inset locking mode
1363 // Check whether the inset was hit. If not reset mode,
1364 // otherwise give the event to the inset
1365 if (inset_hit == bv->theLockingInset()) {
1366 FuncRequest cmd(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1367 bv->theLockingInset()->localDispatch(cmd);
1370 bv->unlockInset(bv->theLockingInset());
1374 selection_possible = true;
1375 bv->screen().hideCursor();
1377 // Clear the selection
1378 bv->screen().toggleSelection(bv->text, bv);
1379 bv->text->clearSelection();
1380 bv->text->fullRebreak(bv);
1382 bv->updateScrollbar();
1384 // Single left click in math inset?
1385 if (isHighlyEditableInset(inset_hit)) {
1386 // Highly editable inset, like math
1387 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1388 selection_possible = false;
1389 bv->owner()->updateLayoutChoice();
1390 bv->owner()->message(inset->editMessage());
1391 //inset->edit(bv, x, y, cmd.button());
1392 // We just have to lock the inset before calling a PressEvent on it!
1393 // we don't need the edit() call here! (Jug20020329)
1394 if (!bv->lockInset(inset))
1395 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1396 FuncRequest cmd(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1397 inset->localDispatch(cmd);
1400 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1402 // Right click on a footnote flag opens float menu
1403 if (cmd.button() == mouse_button::button3) {
1404 selection_possible = false;
1408 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1409 bv->text->setCursorFromCoordinates(bv, x, y + screen_first);
1411 bv->text->selection.cursor = bv->text->cursor;
1412 bv->text->cursor.x_fix(bv->text->cursor.x());
1414 bv->owner()->updateLayoutChoice();
1415 if (bv->fitCursor())
1416 selection_possible = false;
1418 // Insert primary selection with middle mouse
1419 // if there is a local selection in the current buffer,
1421 if (cmd.button() == mouse_button::button2) {
1422 if (paste_internally)
1423 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1425 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1426 selection_possible = false;
1431 case LFUN_MOUSE_RELEASE:
1433 // do nothing if we used the mouse wheel
1437 if (cmd.button() == mouse_button::button4
1438 || cmd.button() == mouse_button::button5)
1441 // If we hit an inset, we have the inset coordinates in these
1442 // and inset_hit points to the inset. If we do not hit an
1443 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1446 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1448 if (bv->theLockingInset()) {
1449 // We are in inset locking mode.
1451 // LyX does a kind of work-area grabbing for insets.
1452 // Only a ButtonPress FuncRequest outside the inset will
1453 // force a insetUnlock.
1454 FuncRequest cmd(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1455 bv->theLockingInset()->localDispatch(cmd);
1459 selection_possible = false;
1461 if (cmd.button() == mouse_button::button2)
1465 if (cmd.button() == mouse_button::button1)
1466 bv->workarea().haveSelection(selection.set());
1469 bv->owner()->view_state_changed();
1470 bv->owner()->updateMenubar();
1471 bv->owner()->updateToolbar();
1473 // Did we hit an editable inset?
1475 selection_possible = false;
1477 // if we reach this point with a selection, it
1478 // must mean we are currently selecting.
1479 // But we don't want to open the inset
1480 // because that is annoying for the user.
1481 // So just pretend we didn't hit it.
1482 // this is OK because a "kosher" ButtonRelease
1483 // will follow a ButtonPress that clears
1485 // Note this also fixes selection drawing
1486 // problems if we end up opening an inset
1487 if (selection.set())
1490 // CHECK fix this proper in 0.13
1491 // well, maybe 13.0 !!!!!!!!!
1493 // Following a ref shouldn't issue
1494 // a push on the undo-stack
1495 // anylonger, now that we have
1496 // keybindings for following
1497 // references and returning from
1498 // references. IMHO though, it
1499 // should be the inset's own business
1500 // to push or not push on the undo
1501 // stack. They don't *have* to
1502 // alter the document...
1504 // ...or maybe the SetCursorParUndo()
1505 // below isn't necessary at all anylonger?
1506 if (inset_hit->lyxCode() == Inset::REF_CODE)
1507 setCursorParUndo(bv);
1509 bv->owner()->message(inset_hit->editMessage());
1511 if (isHighlyEditableInset(inset_hit)) {
1512 // Highly editable inset, like math
1513 UpdatableInset * inset = (UpdatableInset *) inset_hit;
1514 FuncRequest cmd(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1515 inset->localDispatch(cmd);
1517 FuncRequest cmd(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1518 inset_hit->localDispatch(cmd);
1519 // IMO this is a grosshack! Inset's should be changed so that
1520 // they call the actions they have to do with the insetButtonRel.
1521 // function and not in the edit(). This should be changed
1523 #ifdef WITH_WARNINGS
1524 #warning Please remove donot call inset->edit() here (Jug 20020812)
1526 inset_hit->edit(bv, x, y, cmd.button());
1531 // Maybe we want to edit a bibitem ale970302
1532 if (bv->text->cursor.par()->bibkey) {
1534 bv->text->cursor.par()->isRightToLeftPar(bv->buffer()->params);
1536 bibitemMaxWidth(bv, bv->buffer()->params.getLyXTextClass().defaultfont());
1537 if ((is_rtl && x > bv->text->workWidth(bv)-20-width) ||
1538 (!is_rtl && x < 20 + width)) {
1539 bv->text->cursor.par()->bibkey->edit(bv, 0, 0, mouse_button::none);
1545 case LFUN_SELFINSERT: {
1546 if (cmd.argument.empty())
1549 // Automatically delete the currently selected
1550 // text and replace it with what is being
1551 // typed in now. Depends on lyxrc settings
1552 // "auto_region_delete", which defaults to
1555 if (lyxrc.auto_region_delete) {
1556 if (selection.set()) {
1557 cutSelection(bv, false, false);
1560 bv->workarea().haveSelection(false);
1563 bv->beforeChange(this);
1564 LyXFont const old_font(real_current_font);
1566 string::const_iterator cit = cmd.argument.begin();
1567 string::const_iterator end = cmd.argument.end();
1568 for (; cit != end; ++cit)
1569 bv->owner()->getIntl().getTransManager().
1570 TranslateAndInsert(*cit, this);
1573 selection.cursor = cursor;
1574 moveCursorUpdate(bv, false);
1576 // real_current_font.number can change so we need to
1577 // update the minibuffer
1578 if (old_font != real_current_font)
1579 bv->owner()->view_state_changed();
1584 return Inset::UNDISPATCHED;
1587 return Inset::DISPATCHED;