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 taller than the workarea. The
262 // simplest solution is to move to the previous row instead.
265 // This is what we used to do, so we wouldn't skip right past
266 // tall rows, but it's not working right now.
268 new_y = bv->text->first_y - bv->workHeight();
272 new_y = bv->text->cursor.iy()
273 + bv->theLockingInset()->insetInInsetY() + y
274 + cursor.row()->height()
275 - bv->workHeight() + 1;
278 - cursor.row()->baseline()
279 + cursor.row()->height()
280 - bv->workHeight() + 1;
283 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
284 if (cursor.row()->previous()) {
286 setCursor(bv, cur, cursor.row()->previous()->par(),
287 cursor.row()->previous()->pos(), false);
288 if (cur.y() > first_y) {
292 bv->updateScrollbar();
296 void LyXText::cursorNext(BufferView * bv)
298 if (!cursor.row()->next()) {
299 int y = cursor.y() - cursor.row()->baseline() +
300 cursor.row()->height();
301 if (y > int(first_y + bv->workHeight())) {
302 bv->screen().draw(bv->text, bv,
303 bv->text->first_y + bv->workHeight());
304 bv->updateScrollbar();
309 int y = first_y + bv->workHeight();
310 if (inset_owner && !first_y) {
311 y -= (bv->text->cursor.iy()
313 + bv->theLockingInset()->insetInInsetY());
318 Row * cursorrow = cursor.row();
319 setCursorFromCoordinates(bv, cursor.x_fix(), y);
320 // + bv->workHeight());
324 if (cursorrow == bv->text->cursor.row()) {
325 // we have a row which is taller than the workarea. The
326 // simplest solution is to move to the next row instead.
327 cursorDown(bv, true);
329 // This is what we used to do, so we wouldn't skip right past
330 // tall rows, but it's not working right now.
332 new_y = bv->text->first_y + bv->workHeight();
336 new_y = bv->text->cursor.iy()
337 + bv->theLockingInset()->insetInInsetY()
338 + y - cursor.row()->baseline();
340 new_y = cursor.y() - cursor.row()->baseline();
343 bv->screen().draw(bv->text, bv, new_y);
344 if (cursor.row()->next()) {
346 setCursor(bv, cur, cursor.row()->next()->par(),
347 cursor.row()->next()->pos(), false);
348 if (cur.y() < int(first_y + bv->workHeight())) {
349 cursorDown(bv, true);
352 bv->updateScrollbar();
356 void LyXText::update(BufferView * bv, bool changed)
358 BufferView::UpdateCodes c = BufferView::SELECT | BufferView::FITCUR;
360 bv->update(this, c | BufferView::CHANGE);
366 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
370 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
371 if (!bv->insertInset(new_inset))
374 bv->updateInset(new_inset, true);
378 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
380 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
381 <<"] arg[" << cmd.argument << ']' << endl;
383 BufferView * bv = cmd.view();
385 switch (cmd.action) {
387 case LFUN_APPENDIX: {
388 Paragraph * par = cursor.par();
389 bool start = !par->params().startOfAppendix();
391 // ensure that we have only one start_of_appendix in this document
392 Paragraph * tmp = ownerParagraph();
393 for (; tmp; tmp = tmp->next()) {
394 if (tmp->params().startOfAppendix()) {
395 setUndo(bv, Undo::EDIT, tmp, tmp->next());
396 tmp->params().startOfAppendix(false);
401 setUndo(bv, Undo::EDIT, par, par->next());
402 par->params().startOfAppendix(start);
404 // we can set the refreshing parameters now
405 status(cmd.view(), LyXText::NEED_MORE_REFRESH);
407 refresh_row = 0; // not needed for full update
408 updateCounters(cmd.view());
409 setCursor(cmd.view(), cursor.par(), cursor.pos());
414 case LFUN_DELETE_WORD_FORWARD:
415 bv->beforeChange(this);
417 deleteWordForward(bv);
422 case LFUN_DELETE_WORD_BACKWARD:
423 bv->beforeChange(this);
425 deleteWordBackward(bv);
430 case LFUN_DELETE_LINE_FORWARD:
431 bv->beforeChange(this);
433 deleteLineForward(bv);
440 if (!selection.mark())
441 bv->beforeChange(this);
448 if (!selection.mark())
449 bv->beforeChange(this);
451 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
452 cursorLeftOneWord(bv);
454 cursorRightOneWord(bv);
459 if (!selection.mark())
460 bv->beforeChange(this);
462 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
463 cursorRightOneWord(bv);
465 cursorLeftOneWord(bv);
469 case LFUN_BEGINNINGBUF:
470 if (!selection.mark())
471 bv->beforeChange(this);
478 if (selection.mark())
479 bv->beforeChange(this);
487 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
491 finishChange(bv, true);
496 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
500 finishChange(bv, true);
506 finishChange(bv, true);
511 cursorDown(bv, true);
512 finishChange(bv, true);
515 case LFUN_UP_PARAGRAPHSEL:
517 cursorUpParagraph(bv);
518 finishChange(bv, true);
521 case LFUN_DOWN_PARAGRAPHSEL:
523 cursorDownParagraph(bv);
524 finishChange(bv, true);
530 finishChange(bv, true);
536 finishChange(bv, true);
542 finishChange(bv, true);
548 finishChange(bv, true);
551 case LFUN_WORDRIGHTSEL:
553 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
554 cursorLeftOneWord(bv);
556 cursorRightOneWord(bv);
557 finishChange(bv, true);
560 case LFUN_WORDLEFTSEL:
562 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
563 cursorRightOneWord(bv);
565 cursorLeftOneWord(bv);
566 finishChange(bv, true);
573 getWord(cur1, cur2, WHOLE_WORD);
574 setCursor(bv, cur1.par(), cur1.pos());
575 bv->beforeChange(this);
576 setCursor(bv, cur2.par(), cur2.pos());
577 finishChange(bv, true);
582 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
583 if (!selection.mark())
584 bv->beforeChange(this);
587 cursorLeft(bv, false);
588 if (cursor.pos() < cursor.par()->size()
589 && cursor.par()->isInset(cursor.pos())
590 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
591 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
592 cmd.message(tmpinset->editMessage());
593 tmpinset->edit(bv, !is_rtl);
597 cursorRight(bv, false);
603 // This is soooo ugly. Isn`t it possible to make
605 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
606 if (!selection.mark())
607 bv->beforeChange(this);
609 LyXCursor const cur = cursor;
611 cursorLeft(bv, false);
612 if ((is_rtl || cur != cursor) && // only if really moved!
613 cursor.pos() < cursor.par()->size() &&
614 cursor.par()->isInset(cursor.pos()) &&
615 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
616 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
617 cmd.message(tmpinset->editMessage());
618 tmpinset->edit(bv, is_rtl);
622 cursorRight(bv, false);
628 if (!selection.mark())
629 bv->beforeChange(this);
630 bv->update(this, BufferView::UPDATE);
636 if (!selection.mark())
637 bv->beforeChange(this);
638 bv->update(this, BufferView::UPDATE);
643 case LFUN_UP_PARAGRAPH:
644 if (!selection.mark())
645 bv->beforeChange(this);
646 bv->update(this, BufferView::UPDATE);
647 cursorUpParagraph(bv);
651 case LFUN_DOWN_PARAGRAPH:
652 if (!selection.mark())
653 bv->beforeChange(this);
654 bv->update(this, BufferView::UPDATE);
655 cursorDownParagraph(bv);
656 finishChange(bv, false);
660 if (!selection.mark())
661 bv->beforeChange(this);
662 bv->update(this, BufferView::UPDATE);
664 finishChange(bv, false);
667 // moveCursorUpdate(bv, false, false);
668 // owner_->view_state_changed();
672 if (!selection.mark())
673 bv->beforeChange(this);
674 bv->update(this, BufferView::UPDATE);
676 finishChange(bv, false);
680 if (!selection.mark())
681 bv->beforeChange(this);
684 finishChange(bv, false);
688 if (!selection.mark())
689 bv->beforeChange(this);
692 finishChange(bv, false);
696 bv->beforeChange(this);
697 insertChar(bv, Paragraph::META_NEWLINE);
699 setCursor(bv, cursor.par(), cursor.pos());
700 moveCursorUpdate(bv, false);
704 if (!selection.set()) {
706 selection.cursor = cursor;
708 // It is possible to make it a lot faster still
709 // just comment out the line below...
713 cutSelection(bv, true);
716 moveCursorUpdate(bv, false);
717 bv->owner()->view_state_changed();
721 case LFUN_DELETE_SKIP:
722 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
723 if (!selection.set()) {
724 LyXCursor cur = cursor;
725 if (cur.pos() == cur.par()->size()) {
729 && !(cur.par()->params().spaceTop()
730 == VSpace (VSpace::NONE))) {
732 cur.par()->params().lineTop(),
733 cur.par()->params().lineBottom(),
734 cur.par()->params().pagebreakTop(),
735 cur.par()->params().pagebreakBottom(),
736 VSpace(VSpace::NONE),
737 cur.par()->params().spaceBottom(),
738 cur.par()->params().spacing(),
739 cur.par()->params().align(),
740 cur.par()->params().labelWidthString(), 0);
746 selection.cursor = cursor;
750 selection.cursor = cursor;
754 cutSelection(bv, true);
761 if (!selection.set()) {
762 if (bv->owner()->getIntl().getTransManager().backspace()) {
764 selection.cursor = cursor;
766 // It is possible to make it a lot faster still
767 // just comment out the line below...
772 cutSelection(bv, true);
775 bv->owner()->view_state_changed();
779 case LFUN_BACKSPACE_SKIP:
780 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
781 if (!selection.set()) {
782 LyXCursor cur = cursor;
784 && !(cur.par()->params().spaceTop()
785 == VSpace (VSpace::NONE))) {
787 cur.par()->params().lineTop(),
788 cur.par()->params().lineBottom(),
789 cur.par()->params().pagebreakTop(),
790 cur.par()->params().pagebreakBottom(),
791 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
792 cur.par()->params().spacing(),
793 cur.par()->params().align(),
794 cur.par()->params().labelWidthString(), 0);
797 selection.cursor = cur;
801 cutSelection(bv, true);
806 case LFUN_BREAKPARAGRAPH:
807 bv->beforeChange(this);
808 breakParagraph(bv, 0);
810 selection.cursor = cursor;
812 bv->owner()->view_state_changed();
815 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
816 bv->beforeChange(this);
817 breakParagraph(bv, 1);
819 selection.cursor = cursor;
821 bv->owner()->view_state_changed();
824 case LFUN_BREAKPARAGRAPH_SKIP: {
825 // When at the beginning of a paragraph, remove
826 // indentation and add a "defskip" at the top.
827 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
828 LyXCursor cur = cursor;
829 bv->beforeChange(this);
830 if (cur.pos() == 0) {
831 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
833 cur.par()->params().lineTop(),
834 cur.par()->params().lineBottom(),
835 cur.par()->params().pagebreakTop(),
836 cur.par()->params().pagebreakBottom(),
837 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
838 cur.par()->params().spacing(),
839 cur.par()->params().align(),
840 cur.par()->params().labelWidthString(), 1);
845 breakParagraph(bv, 0);
849 selection.cursor = cur;
851 bv->owner()->view_state_changed();
855 case LFUN_PARAGRAPH_SPACING: {
856 Paragraph * par = cursor.par();
857 Spacing::Space cur_spacing = par->params().spacing().getSpace();
858 float cur_value = 1.0;
859 if (cur_spacing == Spacing::Other)
860 cur_value = par->params().spacing().getValue();
862 istringstream is(cmd.argument.c_str());
865 Spacing::Space new_spacing = cur_spacing;
866 float new_value = cur_value;
868 lyxerr << "Missing argument to `paragraph-spacing'"
870 } else if (tmp == "single") {
871 new_spacing = Spacing::Single;
872 } else if (tmp == "onehalf") {
873 new_spacing = Spacing::Onehalf;
874 } else if (tmp == "double") {
875 new_spacing = Spacing::Double;
876 } else if (tmp == "other") {
877 new_spacing = Spacing::Other;
880 lyxerr << "new_value = " << tmpval << endl;
883 } else if (tmp == "default") {
884 new_spacing = Spacing::Default;
886 lyxerr << _("Unknown spacing argument: ")
887 << cmd.argument << endl;
889 if (cur_spacing != new_spacing || cur_value != new_value) {
890 par->params().spacing(Spacing(new_spacing, new_value));
897 case LFUN_INSET_TOGGLE:
899 bv->beforeChange(this);
906 case LFUN_PROTECTEDSPACE:
907 if (cursor.par()->layout()->free_spacing) {
911 specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
913 moveCursorUpdate(bv, false);
916 case LFUN_HYPHENATION:
917 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
920 case LFUN_LIGATURE_BREAK:
921 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
925 specialChar(this, bv, InsetSpecialChar::LDOTS);
931 insertChar(bv, Paragraph::META_HFILL);
935 case LFUN_END_OF_SENTENCE:
936 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
939 case LFUN_MENU_SEPARATOR:
940 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
944 bv->beforeChange(this);
946 selection.cursor = cursor;
947 cmd.message(N_("Mark off"));
951 bv->beforeChange(this);
952 selection.mark(true);
954 selection.cursor = cursor;
955 cmd.message(N_("Mark on"));
959 bv->beforeChange(this);
960 if (selection.mark()) {
962 cmd.message(N_("Mark removed"));
964 selection.mark(true);
966 cmd.message(N_("Mark set"));
968 selection.cursor = cursor;
971 case LFUN_UPCASE_WORD:
973 changeCase(bv, LyXText::text_uppercase);
975 bv->updateInset(inset_owner, true);
979 case LFUN_LOWCASE_WORD:
981 changeCase(bv, LyXText::text_lowercase);
983 bv->updateInset(inset_owner, true);
987 case LFUN_CAPITALIZE_WORD:
989 changeCase(bv, LyXText::text_capitalization);
991 bv->updateInset(inset_owner, true);
995 case LFUN_TRANSPOSE_CHARS:
999 bv->updateInset(inset_owner, true);
1004 cmd.message(_("Paste"));
1006 // clear the selection
1007 bv->toggleSelection();
1011 clearSelection(); // bug 393
1020 cutSelection(bv, true);
1022 cmd.message(_("Cut"));
1027 cmd.message(_("Copy"));
1030 case LFUN_BEGINNINGBUFSEL:
1032 return Inset::UNDISPATCHED;
1035 finishChange(bv, true);
1038 case LFUN_ENDBUFSEL:
1040 return Inset::UNDISPATCHED;
1043 finishChange(bv, true);
1047 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1053 istringstream is(cmd.argument.c_str());
1056 lyxerr << "SETXY: Could not parse coordinates in '"
1057 << cmd.argument << std::endl;
1059 setCursorFromCoordinates(bv, x, y);
1064 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1066 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1072 case LFUN_GETLAYOUT:
1073 cmd.message(tostr(cursor.par()->layout()));
1077 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1078 << cmd.argument << endl;
1080 // This is not the good solution to the empty argument
1081 // problem, but it will hopefully suffice for 1.2.0.
1082 // The correct solution would be to augument the
1083 // function list/array with information about what
1084 // functions needs arguments and their type.
1085 if (cmd.argument.empty()) {
1086 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1090 // Derive layout number from given argument (string)
1091 // and current buffer's textclass (number)
1092 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1093 bool hasLayout = tclass.hasLayout(cmd.argument);
1094 string layout = cmd.argument;
1096 // If the entry is obsolete, use the new one instead.
1098 string const & obs = tclass[layout]->obsoleted_by();
1104 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1109 bool change_layout = (current_layout != layout);
1110 if (!change_layout && selection.set() &&
1111 selection.start.par() != selection.end.par())
1113 Paragraph * spar = selection.start.par();
1114 Paragraph * epar = selection.end.par()->next();
1115 while (spar != epar) {
1116 if (spar->layout()->name() != current_layout) {
1117 change_layout = true;
1120 spar = spar->next();
1123 if (change_layout) {
1125 current_layout = layout;
1127 setLayout(bv, layout);
1128 bv->owner()->setLayout(layout);
1135 case LFUN_PASTESELECTION: {
1139 // this was originally a beforeChange(bv->text), i.e
1140 // the outermost LyXText!
1141 bv->beforeChange(this);
1142 string const clip = bv->getClipboard();
1143 if (!clip.empty()) {
1144 if (cmd.argument == "paragraph")
1145 insertStringAsParagraphs(bv, clip);
1147 insertStringAsLines(bv, clip);
1154 case LFUN_GOTOERROR:
1155 gotoInset(bv, Inset::ERROR_CODE, false);
1159 gotoInset(bv, Inset::NOTE_CODE, false);
1162 case LFUN_REFERENCE_GOTO:
1164 vector<Inset::Code> tmp;
1165 tmp.push_back(Inset::LABEL_CODE);
1166 tmp.push_back(Inset::REF_CODE);
1167 gotoInset(bv, tmp, true);
1172 Paragraph const * par = cursor.par();
1173 lyx::pos_type pos = cursor.pos();
1177 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1180 c = par->getChar(pos - 1);
1183 LyXLayout_ptr const & style = par->layout();
1185 if (style->pass_thru ||
1186 par->getFontSettings(bv->buffer()->params,
1187 pos).language()->lang() == "hebrew" ||
1188 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1189 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1193 case LFUN_DATE_INSERT: {
1194 time_t now_time_t = time(NULL);
1195 struct tm * now_tm = localtime(&now_time_t);
1196 setlocale(LC_TIME, "");
1198 if (!cmd.argument.empty())
1201 arg = lyxrc.date_insert_format;
1203 int const datetmp_len =
1204 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1206 for (int i = 0; i < datetmp_len; i++) {
1207 insertChar(bv, datetmp[i]);
1210 selection.cursor = cursor;
1211 moveCursorUpdate(bv, false);
1215 case LFUN_MOUSE_TRIPLE:
1218 if (bv_owner && bv->theLockingInset())
1220 if (cmd.button() == mouse_button::button1) {
1222 bv->screen().hideCursor();
1223 bv->screen().toggleSelection(this, bv);
1226 selection.cursor = cursor;
1230 bv->screen().toggleSelection(this, bv, false);
1232 bv->haveSelection(selection.set());
1236 case LFUN_MOUSE_DOUBLE:
1239 if (bv_owner && bv->theLockingInset())
1241 if (cmd.button() == mouse_button::button1) {
1243 bv->screen().hideCursor();
1244 bv->screen().toggleSelection(this, bv);
1245 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1246 bv->screen().toggleSelection(this, bv, false);
1248 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1251 bv->haveSelection(selection.set());
1255 case LFUN_MOUSE_MOTION:
1257 // Only use motion with button 1
1258 //if (ev.button() != mouse_button::button1)
1264 // Check for inset locking
1265 if (bv->theLockingInset()) {
1266 Inset * tli = bv->theLockingInset();
1267 LyXCursor cursor = bv->text->cursor;
1268 LyXFont font = bv->text->getFont(bv->buffer(),
1269 cursor.par(), cursor.pos());
1270 int width = tli->width(bv, font);
1271 int inset_x = font.isVisibleRightToLeft()
1272 ? cursor.ix() - width : cursor.ix();
1273 int start_x = inset_x + tli->scroll();
1274 FuncRequest cmd1 = cmd;
1275 cmd1.x = cmd.x - start_x;
1276 cmd1.y = cmd.y - cursor.iy() + bv->text->first_y;
1277 tli->localDispatch(cmd1);
1281 // The test for not selection possible is needed, that only motion
1282 // events are used, where the bottom press event was on
1283 // the drawing area too
1284 if (!selection_possible) {
1285 lyxerr[Debug::ACTION]
1286 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1290 bv->screen().hideCursor();
1292 Row * cursorrow = bv->text->cursor.row();
1293 bv->text->setCursorFromCoordinates(bv, cmd.x, cmd.y + bv->text->first_y);
1295 // sorry for this but I have a strange error that the y value jumps at
1296 // a certain point. This seems like an error in my xforms library or
1297 // in some other local environment, but I would like to leave this here
1298 // for the moment until I can remove this (Jug 20020418)
1299 if (y_before < bv->text->cursor.y())
1300 lyxerr << y_before << ':'
1301 << bv->text->cursor.y() << endl;
1303 // This is to allow jumping over large insets
1304 if (cursorrow == bv->text->cursor.row()) {
1305 if (cmd.y >= int(bv->workHeight()))
1306 bv->text->cursorDown(bv, false);
1308 bv->text->cursorUp(bv, false);
1311 // Maybe an empty line was deleted
1312 if (!bv->text->selection.set())
1313 bv->update(bv->text, BufferView::UPDATE);
1314 bv->text->setSelection(bv);
1315 bv->screen().toggleToggle(bv->text, bv);
1321 // Single-click on work area
1322 case LFUN_MOUSE_PRESS:
1327 // ok ok, this is a hack (for xforms)
1328 // We shouldn't go further down as we really should only do the
1329 // scrolling and be done with this. Otherwise we may open some
1330 // dialogs (Jug 20020424).
1331 if (cmd.button() == mouse_button::button4) {
1332 bv->scroll(-lyxrc.wheel_jump);
1335 if (cmd.button() == mouse_button::button5) {
1336 bv->scroll(lyxrc.wheel_jump);
1342 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1344 // Middle button press pastes if we have a selection
1345 // We do this here as if the selection was inside an inset
1346 // it could get cleared on the unlocking of the inset so
1347 // we have to check this first
1348 bool paste_internally = false;
1349 if (cmd.button() == mouse_button::button2 && selection.set()) {
1350 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1351 paste_internally = true;
1354 int const screen_first = bv->text->first_y;
1356 if (bv->theLockingInset()) {
1357 // We are in inset locking mode
1359 // Check whether the inset was hit. If not reset mode,
1360 // otherwise give the event to the inset
1361 if (inset_hit == bv->theLockingInset()) {
1362 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1363 bv->theLockingInset()->localDispatch(cmd1);
1366 bv->unlockInset(bv->theLockingInset());
1370 selection_possible = true;
1371 bv->screen().hideCursor();
1373 // Clear the selection
1374 bv->screen().toggleSelection(bv->text, bv);
1375 bv->text->clearSelection();
1376 bv->text->fullRebreak(bv);
1378 bv->updateScrollbar();
1380 // Single left click in math inset?
1381 if (isHighlyEditableInset(inset_hit)) {
1382 // Highly editable inset, like math
1383 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1384 selection_possible = false;
1385 bv->owner()->updateLayoutChoice();
1386 bv->owner()->message(inset->editMessage());
1387 //inset->edit(bv, x, y, cmd.button());
1388 // We just have to lock the inset before calling a PressEvent on it!
1389 // we don't need the edit() call here! (Jug20020329)
1390 if (!bv->lockInset(inset))
1391 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1392 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1393 inset->localDispatch(cmd1);
1396 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1398 // Right click on a footnote flag opens float menu
1399 if (cmd.button() == mouse_button::button3) {
1400 selection_possible = false;
1404 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1405 bv->text->setCursorFromCoordinates(bv, x, y + screen_first);
1407 bv->text->selection.cursor = bv->text->cursor;
1408 bv->text->cursor.x_fix(bv->text->cursor.x());
1410 bv->owner()->updateLayoutChoice();
1411 if (bv->fitCursor())
1412 selection_possible = false;
1414 // Insert primary selection with middle mouse
1415 // if there is a local selection in the current buffer,
1417 if (cmd.button() == mouse_button::button2) {
1418 if (paste_internally)
1419 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1421 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1422 selection_possible = false;
1427 case LFUN_MOUSE_RELEASE:
1429 // do nothing if we used the mouse wheel
1433 if (cmd.button() == mouse_button::button4
1434 || cmd.button() == mouse_button::button5)
1437 // If we hit an inset, we have the inset coordinates in these
1438 // and inset_hit points to the inset. If we do not hit an
1439 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1442 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1444 if (bv->theLockingInset()) {
1445 // We are in inset locking mode.
1447 // LyX does a kind of work-area grabbing for insets.
1448 // Only a ButtonPress FuncRequest outside the inset will
1449 // force a insetUnlock.
1450 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1451 bv->theLockingInset()->localDispatch(cmd1);
1455 selection_possible = false;
1457 if (cmd.button() == mouse_button::button2)
1461 if (cmd.button() == mouse_button::button1)
1462 bv->haveSelection(selection.set());
1465 bv->owner()->view_state_changed();
1466 bv->owner()->updateMenubar();
1467 bv->owner()->updateToolbar();
1469 // Did we hit an editable inset?
1471 selection_possible = false;
1473 // if we reach this point with a selection, it
1474 // must mean we are currently selecting.
1475 // But we don't want to open the inset
1476 // because that is annoying for the user.
1477 // So just pretend we didn't hit it.
1478 // this is OK because a "kosher" ButtonRelease
1479 // will follow a ButtonPress that clears
1481 // Note this also fixes selection drawing
1482 // problems if we end up opening an inset
1483 if (selection.set())
1486 // CHECK fix this proper in 0.13
1487 // well, maybe 13.0 !!!!!!!!!
1489 // Following a ref shouldn't issue
1490 // a push on the undo-stack
1491 // anylonger, now that we have
1492 // keybindings for following
1493 // references and returning from
1494 // references. IMHO though, it
1495 // should be the inset's own business
1496 // to push or not push on the undo
1497 // stack. They don't *have* to
1498 // alter the document...
1500 // ...or maybe the SetCursorParUndo()
1501 // below isn't necessary at all anylonger?
1502 if (inset_hit->lyxCode() == Inset::REF_CODE)
1503 setCursorParUndo(bv);
1505 bv->owner()->message(inset_hit->editMessage());
1507 if (isHighlyEditableInset(inset_hit)) {
1508 // Highly editable inset, like math
1509 UpdatableInset * inset = (UpdatableInset *) inset_hit;
1510 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1511 inset->localDispatch(cmd1);
1513 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1514 inset_hit->localDispatch(cmd1);
1515 // IMO this is a grosshack! Inset's should be changed so that
1516 // they call the actions they have to do with the insetButtonRel.
1517 // function and not in the edit(). This should be changed
1519 #ifdef WITH_WARNINGS
1520 #warning Please remove donot call inset->edit() here (Jug 20020812)
1522 inset_hit->edit(bv, x, y, cmd.button());
1527 // Maybe we want to edit a bibitem ale970302
1528 if (bv->text->cursor.par()->bibkey) {
1530 bv->text->cursor.par()->isRightToLeftPar(bv->buffer()->params);
1532 bibitemMaxWidth(bv, bv->buffer()->params.getLyXTextClass().defaultfont());
1533 if ((is_rtl && x > bv->text->workWidth(bv)-20-width) ||
1534 (!is_rtl && x < 20 + width)) {
1535 bv->text->cursor.par()->bibkey->edit(bv, 0, 0, mouse_button::none);
1541 case LFUN_SELFINSERT: {
1542 if (cmd.argument.empty())
1545 // Automatically delete the currently selected
1546 // text and replace it with what is being
1547 // typed in now. Depends on lyxrc settings
1548 // "auto_region_delete", which defaults to
1551 if (lyxrc.auto_region_delete) {
1552 if (selection.set()) {
1553 cutSelection(bv, false, false);
1556 bv->haveSelection(false);
1559 bv->beforeChange(this);
1560 LyXFont const old_font(real_current_font);
1562 string::const_iterator cit = cmd.argument.begin();
1563 string::const_iterator end = cmd.argument.end();
1564 for (; cit != end; ++cit)
1565 bv->owner()->getIntl().getTransManager().
1566 TranslateAndInsert(*cit, this);
1569 selection.cursor = cursor;
1570 moveCursorUpdate(bv, false);
1572 // real_current_font.number can change so we need to
1573 // update the minibuffer
1574 if (old_font != real_current_font)
1575 bv->owner()->view_state_changed();
1579 case LFUN_HTMLURL: {
1580 InsetCommandParams p("htmlurl");
1581 bv->owner()->getDialogs().createUrl(p.getAsString());
1586 InsetCommandParams p("url");
1587 bv->owner()->getDialogs().createUrl(p.getAsString());
1593 case LFUN_INSET_LIST:
1594 case LFUN_INSET_THEOREM:
1596 case LFUN_INSERT_NOTE:
1597 case LFUN_INSERT_URL:
1598 case LFUN_INSET_CAPTION:
1599 case LFUN_INSET_ERT:
1600 case LFUN_INSET_EXTERNAL:
1601 case LFUN_INSET_FLOAT:
1602 case LFUN_INSET_FOOTNOTE:
1603 case LFUN_INSET_MARGINAL:
1604 case LFUN_INSET_MINIPAGE:
1605 case LFUN_INSET_OPTARG:
1606 case LFUN_INSET_WIDE_FLOAT:
1607 case LFUN_INSET_WRAP:
1608 case LFUN_TABULAR_INSERT:
1609 case LFUN_INDEX_INSERT:
1610 case LFUN_INDEX_PRINT:
1611 case LFUN_PARENTINSERT:
1612 case LFUN_TOC_INSERT:
1614 Inset * inset = createInset(cmd);
1616 bool gotsel = false;
1617 if (selection.set()) {
1618 cutSelection(bv, true, false);
1621 if (bv->insertInset(inset)) {
1624 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
1634 return Inset::UNDISPATCHED;
1637 return Inset::DISPATCHED;