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 " << x << "," << y
128 << " box " << b << endl;
132 text.setCursor(bv, &par, pos, true);
135 // The origin of an inset is on the baseline
136 y -= text.cursor.iy();
144 Inset * LyXText::checkInsetHit(BufferView * bv, int & x, int & y) const
146 int y_tmp = y + first_y;
149 setCursorFromCoordinates(bv, cur, x, y_tmp);
151 Inset * inset = checkInset(bv, *this, cur, x, y_tmp);
157 // look at previous position
162 setCursor(bv, cur, cur.par(), cur.pos() - 1, true);
164 inset = checkInset(bv, *this, cur, x, y_tmp);
171 bool LyXText::gotoNextInset(BufferView * bv,
172 vector<Inset::Code> const & codes, string const & contents) const
174 LyXCursor res = cursor;
177 if (res.pos() < res.par()->size() - 1) {
178 res.pos(res.pos() + 1);
180 res.par(res.par()->next());
184 } while (res.par() &&
185 !(res.par()->isInset(res.pos())
186 && (inset = res.par()->getInset(res.pos())) != 0
187 && find(codes.begin(), codes.end(), inset->lyxCode())
189 && (contents.empty() ||
190 static_cast<InsetCommand *>(
191 res.par()->getInset(res.pos()))->getContents()
195 setCursor(bv, res.par(), res.pos(), false);
202 void LyXText::gotoInset(BufferView * bv, vector<Inset::Code> const & codes,
206 bv->beforeChange(this);
210 if (same_content && cursor.par()->isInset(cursor.pos())) {
211 Inset const * inset = cursor.par()->getInset(cursor.pos());
212 if (find(codes.begin(), codes.end(), inset->lyxCode())
214 contents = static_cast<InsetCommand const *>(inset)->getContents();
217 if (!gotoNextInset(bv, codes, contents)) {
218 if (cursor.pos() || cursor.par() != ownerParagraph()) {
219 LyXCursor tmp = cursor;
220 cursor.par(ownerParagraph());
222 if (!gotoNextInset(bv, codes, contents)) {
224 bv->owner()->message(_("No more insets"));
227 bv->owner()->message(_("No more insets"));
231 selection.cursor = cursor;
235 void LyXText::gotoInset(BufferView * bv, Inset::Code code, bool same_content)
237 gotoInset(bv, vector<Inset::Code>(1, code), same_content);
241 void LyXText::cursorPrevious(BufferView * bv)
243 if (!cursor.row()->previous()) {
245 int new_y = bv->text->first_y - bv->workHeight();
246 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
247 bv->updateScrollbar();
253 Row * cursorrow = cursor.row();
255 setCursorFromCoordinates(bv, cursor.x_fix(), y);
259 if (cursorrow == bv->text->cursor.row()) {
260 // we have a row which is higher than the workarea so we leave the
261 // cursor on the start of the row and move only the draw up as soon
262 // as we move the cursor or do something while inside the row (it may
263 // span several workarea-heights) we'll move to the top again, but this
264 // is better than just jump down and only display part of the row.
265 new_y = bv->text->first_y - bv->workHeight();
268 new_y = bv->text->cursor.iy()
269 + bv->theLockingInset()->insetInInsetY() + y
270 + cursor.row()->height()
271 - bv->workHeight() + 1;
274 - cursor.row()->baseline()
275 + cursor.row()->height()
276 - bv->workHeight() + 1;
279 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
280 if (cursor.row()->previous()) {
282 setCursor(bv, cur, cursor.row()->previous()->par(),
283 cursor.row()->previous()->pos(), false);
284 if (cur.y() > first_y) {
288 bv->updateScrollbar();
292 void LyXText::cursorNext(BufferView * bv)
294 if (!cursor.row()->next()) {
295 int y = cursor.y() - cursor.row()->baseline() +
296 cursor.row()->height();
297 if (y > int(first_y + bv->workHeight())) {
298 bv->screen().draw(bv->text, bv,
299 bv->text->first_y + bv->workHeight());
300 bv->updateScrollbar();
305 int y = first_y + bv->workHeight();
306 if (inset_owner && !first_y) {
307 y -= (bv->text->cursor.iy()
309 + bv->theLockingInset()->insetInInsetY());
314 Row * cursorrow = cursor.row();
315 setCursorFromCoordinates(bv, cursor.x_fix(), y);
316 // + bv->workHeight());
320 if (cursorrow == bv->text->cursor.row()) {
321 // we have a row which is higher than the workarea so we leave the
322 // cursor on the start of the row and move only the draw down as soon
323 // as we move the cursor or do something while inside the row (it may
324 // span several workarea-heights) we'll move to the top again, but this
325 // is better than just jump down and only display part of the row.
326 new_y = bv->text->first_y + bv->workHeight();
329 new_y = bv->text->cursor.iy()
330 + bv->theLockingInset()->insetInInsetY()
331 + y - cursor.row()->baseline();
333 new_y = cursor.y() - cursor.row()->baseline();
336 bv->screen().draw(bv->text, bv, new_y);
337 if (cursor.row()->next()) {
339 setCursor(bv, cur, cursor.row()->next()->par(),
340 cursor.row()->next()->pos(), false);
341 if (cur.y() < int(first_y + bv->workHeight())) {
342 cursorDown(bv, true);
345 bv->updateScrollbar();
349 void LyXText::update(BufferView * bv, bool changed)
351 BufferView::UpdateCodes c = BufferView::SELECT | BufferView::FITCUR;
353 bv->update(this, c | BufferView::CHANGE);
359 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
363 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
364 if (!bv->insertInset(new_inset))
367 bv->updateInset(new_inset, true);
371 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
373 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
374 <<"] arg[" << cmd.argument << "]" << endl;
376 BufferView * bv = cmd.view();
378 switch (cmd.action) {
380 case LFUN_APPENDIX: {
381 Paragraph * par = cursor.par();
382 bool start = !par->params().startOfAppendix();
384 // ensure that we have only one start_of_appendix in this document
385 Paragraph * tmp = ownerParagraph();
386 for (; tmp; tmp = tmp->next()) {
387 if (tmp->params().startOfAppendix()) {
388 setUndo(bv, Undo::EDIT, tmp, tmp->next());
389 tmp->params().startOfAppendix(false);
394 setUndo(bv, Undo::EDIT, par, par->next());
395 par->params().startOfAppendix(start);
397 // we can set the refreshing parameters now
398 status(cmd.view(), LyXText::NEED_MORE_REFRESH);
400 refresh_row = 0; // not needed for full update
401 updateCounters(cmd.view());
402 setCursor(cmd.view(), cursor.par(), cursor.pos());
407 case LFUN_DELETE_WORD_FORWARD:
408 bv->beforeChange(this);
410 deleteWordForward(bv);
415 case LFUN_DELETE_WORD_BACKWARD:
416 bv->beforeChange(this);
418 deleteWordBackward(bv);
423 case LFUN_DELETE_LINE_FORWARD:
424 bv->beforeChange(this);
426 deleteLineForward(bv);
433 if (!selection.mark())
434 bv->beforeChange(this);
441 if (!selection.mark())
442 bv->beforeChange(this);
444 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
445 cursorLeftOneWord(bv);
447 cursorRightOneWord(bv);
452 if (!selection.mark())
453 bv->beforeChange(this);
455 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
456 cursorRightOneWord(bv);
458 cursorLeftOneWord(bv);
462 case LFUN_BEGINNINGBUF:
463 if (!selection.mark())
464 bv->beforeChange(this);
471 if (selection.mark())
472 bv->beforeChange(this);
480 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
484 finishChange(bv, true);
489 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
493 finishChange(bv, true);
499 finishChange(bv, true);
504 cursorDown(bv, true);
505 finishChange(bv, true);
508 case LFUN_UP_PARAGRAPHSEL:
510 cursorUpParagraph(bv);
511 finishChange(bv, true);
514 case LFUN_DOWN_PARAGRAPHSEL:
516 cursorDownParagraph(bv);
517 finishChange(bv, true);
523 finishChange(bv, true);
529 finishChange(bv, true);
535 finishChange(bv, true);
541 finishChange(bv, true);
544 case LFUN_WORDRIGHTSEL:
546 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
547 cursorLeftOneWord(bv);
549 cursorRightOneWord(bv);
550 finishChange(bv, true);
553 case LFUN_WORDLEFTSEL:
555 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
556 cursorRightOneWord(bv);
558 cursorLeftOneWord(bv);
559 finishChange(bv, true);
566 getWord(cur1, cur2, WHOLE_WORD);
567 setCursor(bv, cur1.par(), cur1.pos());
568 bv->beforeChange(this);
569 setCursor(bv, cur2.par(), cur2.pos());
570 finishChange(bv, true);
575 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
576 if (!selection.mark())
577 bv->beforeChange(this);
580 cursorLeft(bv, false);
581 if (cursor.pos() < cursor.par()->size()
582 && cursor.par()->isInset(cursor.pos())
583 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
584 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
585 cmd.message(tmpinset->editMessage());
586 tmpinset->edit(bv, !is_rtl);
590 cursorRight(bv, false);
596 // This is soooo ugly. Isn`t it possible to make
598 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
599 if (!selection.mark())
600 bv->beforeChange(this);
602 LyXCursor const cur = cursor;
604 cursorLeft(bv, false);
605 if ((is_rtl || cur != cursor) && // only if really moved!
606 cursor.pos() < cursor.par()->size() &&
607 cursor.par()->isInset(cursor.pos()) &&
608 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
609 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
610 cmd.message(tmpinset->editMessage());
611 tmpinset->edit(bv, is_rtl);
615 cursorRight(bv, false);
621 if (!selection.mark())
622 bv->beforeChange(this);
623 bv->update(this, BufferView::UPDATE);
629 if (!selection.mark())
630 bv->beforeChange(this);
631 bv->update(this, BufferView::UPDATE);
636 case LFUN_UP_PARAGRAPH:
637 if (!selection.mark())
638 bv->beforeChange(this);
639 bv->update(this, BufferView::UPDATE);
640 cursorUpParagraph(bv);
644 case LFUN_DOWN_PARAGRAPH:
645 if (!selection.mark())
646 bv->beforeChange(this);
647 bv->update(this, BufferView::UPDATE);
648 cursorDownParagraph(bv);
649 finishChange(bv, false);
653 if (!selection.mark())
654 bv->beforeChange(this);
655 bv->update(this, BufferView::UPDATE);
657 finishChange(bv, false);
660 // moveCursorUpdate(bv, false, false);
661 // owner_->view_state_changed();
665 if (!selection.mark())
666 bv->beforeChange(this);
667 bv->update(this, BufferView::UPDATE);
669 finishChange(bv, false);
673 if (!selection.mark())
674 bv->beforeChange(this);
677 finishChange(bv, false);
681 if (!selection.mark())
682 bv->beforeChange(this);
685 finishChange(bv, false);
689 bv->beforeChange(this);
690 insertChar(bv, Paragraph::META_NEWLINE);
692 setCursor(bv, cursor.par(), cursor.pos());
693 moveCursorUpdate(bv, false);
697 if (!selection.set()) {
699 selection.cursor = cursor;
701 // It is possible to make it a lot faster still
702 // just comment out the line below...
706 cutSelection(bv, true);
709 moveCursorUpdate(bv, false);
710 bv->owner()->view_state_changed();
714 case LFUN_DELETE_SKIP:
715 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
716 if (!selection.set()) {
717 LyXCursor cur = cursor;
718 if (cur.pos() == cur.par()->size()) {
722 && !(cur.par()->params().spaceTop()
723 == VSpace (VSpace::NONE))) {
725 cur.par()->params().lineTop(),
726 cur.par()->params().lineBottom(),
727 cur.par()->params().pagebreakTop(),
728 cur.par()->params().pagebreakBottom(),
729 VSpace(VSpace::NONE),
730 cur.par()->params().spaceBottom(),
731 cur.par()->params().spacing(),
732 cur.par()->params().align(),
733 cur.par()->params().labelWidthString(), 0);
739 selection.cursor = cursor;
743 selection.cursor = cursor;
747 cutSelection(bv, true);
754 if (!selection.set()) {
755 if (bv->owner()->getIntl().getTransManager().backspace()) {
757 selection.cursor = cursor;
759 // It is possible to make it a lot faster still
760 // just comment out the line below...
765 cutSelection(bv, true);
768 bv->owner()->view_state_changed();
772 case LFUN_BACKSPACE_SKIP:
773 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
774 if (!selection.set()) {
775 LyXCursor cur = cursor;
777 && !(cur.par()->params().spaceTop()
778 == VSpace (VSpace::NONE))) {
780 cur.par()->params().lineTop(),
781 cur.par()->params().lineBottom(),
782 cur.par()->params().pagebreakTop(),
783 cur.par()->params().pagebreakBottom(),
784 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
785 cur.par()->params().spacing(),
786 cur.par()->params().align(),
787 cur.par()->params().labelWidthString(), 0);
790 selection.cursor = cur;
794 cutSelection(bv, true);
799 case LFUN_BREAKPARAGRAPH:
800 bv->beforeChange(this);
801 breakParagraph(bv, 0);
803 selection.cursor = cursor;
805 bv->owner()->view_state_changed();
808 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
809 bv->beforeChange(this);
810 breakParagraph(bv, 1);
812 selection.cursor = cursor;
814 bv->owner()->view_state_changed();
817 case LFUN_BREAKPARAGRAPH_SKIP: {
818 // When at the beginning of a paragraph, remove
819 // indentation and add a "defskip" at the top.
820 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
821 LyXCursor cur = cursor;
822 bv->beforeChange(this);
823 if (cur.pos() == 0) {
824 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
826 cur.par()->params().lineTop(),
827 cur.par()->params().lineBottom(),
828 cur.par()->params().pagebreakTop(),
829 cur.par()->params().pagebreakBottom(),
830 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
831 cur.par()->params().spacing(),
832 cur.par()->params().align(),
833 cur.par()->params().labelWidthString(), 1);
838 breakParagraph(bv, 0);
842 selection.cursor = cur;
844 bv->owner()->view_state_changed();
848 case LFUN_PARAGRAPH_SPACING: {
849 Paragraph * par = cursor.par();
850 Spacing::Space cur_spacing = par->params().spacing().getSpace();
851 float cur_value = 1.0;
852 if (cur_spacing == Spacing::Other)
853 cur_value = par->params().spacing().getValue();
855 istringstream is(cmd.argument.c_str());
858 Spacing::Space new_spacing = cur_spacing;
859 float new_value = cur_value;
861 lyxerr << "Missing argument to `paragraph-spacing'"
863 } else if (tmp == "single") {
864 new_spacing = Spacing::Single;
865 } else if (tmp == "onehalf") {
866 new_spacing = Spacing::Onehalf;
867 } else if (tmp == "double") {
868 new_spacing = Spacing::Double;
869 } else if (tmp == "other") {
870 new_spacing = Spacing::Other;
873 lyxerr << "new_value = " << tmpval << endl;
876 } else if (tmp == "default") {
877 new_spacing = Spacing::Default;
879 lyxerr << _("Unknown spacing argument: ")
880 << cmd.argument << endl;
882 if (cur_spacing != new_spacing || cur_value != new_value) {
883 par->params().spacing(Spacing(new_spacing, new_value));
890 case LFUN_INSET_TOGGLE:
892 bv->beforeChange(this);
899 case LFUN_PROTECTEDSPACE:
900 if (cursor.par()->layout()->free_spacing) {
904 specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
906 moveCursorUpdate(bv, false);
909 case LFUN_HYPHENATION:
910 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
913 case LFUN_LIGATURE_BREAK:
914 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
918 specialChar(this, bv, InsetSpecialChar::LDOTS);
924 insertChar(bv, Paragraph::META_HFILL);
928 case LFUN_END_OF_SENTENCE:
929 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
932 case LFUN_MENU_SEPARATOR:
933 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
937 bv->beforeChange(this);
939 selection.cursor = cursor;
940 cmd.message(N_("Mark off"));
944 bv->beforeChange(this);
945 selection.mark(true);
947 selection.cursor = cursor;
948 cmd.message(N_("Mark on"));
952 bv->beforeChange(this);
953 if (selection.mark()) {
955 cmd.message(N_("Mark removed"));
957 selection.mark(true);
959 cmd.message(N_("Mark set"));
961 selection.cursor = cursor;
964 case LFUN_UPCASE_WORD:
966 changeCase(bv, LyXText::text_uppercase);
968 bv->updateInset(inset_owner, true);
972 case LFUN_LOWCASE_WORD:
974 changeCase(bv, LyXText::text_lowercase);
976 bv->updateInset(inset_owner, true);
980 case LFUN_CAPITALIZE_WORD:
982 changeCase(bv, LyXText::text_capitalization);
984 bv->updateInset(inset_owner, true);
988 case LFUN_TRANSPOSE_CHARS:
992 bv->updateInset(inset_owner, true);
997 cmd.message(_("Paste"));
999 // clear the selection
1000 bv->toggleSelection();
1004 clearSelection(); // bug 393
1013 cutSelection(bv, true);
1015 cmd.message(_("Cut"));
1020 cmd.message(_("Copy"));
1023 case LFUN_BEGINNINGBUFSEL:
1025 return Inset::UNDISPATCHED;
1028 finishChange(bv, true);
1031 case LFUN_ENDBUFSEL:
1033 return Inset::UNDISPATCHED;
1036 finishChange(bv, true);
1040 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1046 istringstream is(cmd.argument.c_str());
1049 lyxerr << "SETXY: Could not parse coordinates in '"
1050 << cmd.argument << std::endl;
1052 setCursorFromCoordinates(bv, x, y);
1057 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1059 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1065 case LFUN_GETLAYOUT:
1066 cmd.message(tostr(cursor.par()->layout()));
1070 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1071 << cmd.argument << endl;
1073 // This is not the good solution to the empty argument
1074 // problem, but it will hopefully suffice for 1.2.0.
1075 // The correct solution would be to augument the
1076 // function list/array with information about what
1077 // functions needs arguments and their type.
1078 if (cmd.argument.empty()) {
1079 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1083 // Derive layout number from given argument (string)
1084 // and current buffer's textclass (number)
1085 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1086 bool hasLayout = tclass.hasLayout(cmd.argument);
1087 string layout = cmd.argument;
1089 // If the entry is obsolete, use the new one instead.
1091 string const & obs = tclass[layout]->obsoleted_by();
1097 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1102 bool change_layout = (current_layout != layout);
1103 if (!change_layout && selection.set() &&
1104 selection.start.par() != selection.end.par())
1106 Paragraph * spar = selection.start.par();
1107 Paragraph * epar = selection.end.par()->next();
1108 while (spar != epar) {
1109 if (spar->layout()->name() != current_layout) {
1110 change_layout = true;
1113 spar = spar->next();
1116 if (change_layout) {
1118 current_layout = layout;
1120 setLayout(bv, layout);
1121 bv->owner()->setLayout(layout);
1128 case LFUN_PASTESELECTION: {
1132 // this was originally a beforeChange(bv->text), i.e
1133 // the outermost LyXText!
1134 bv->beforeChange(this);
1135 string const clip = bv->getClipboard();
1136 if (!clip.empty()) {
1137 if (cmd.argument == "paragraph")
1138 insertStringAsParagraphs(bv, clip);
1140 insertStringAsLines(bv, clip);
1147 case LFUN_GOTOERROR:
1148 gotoInset(bv, Inset::ERROR_CODE, false);
1152 gotoInset(bv, Inset::NOTE_CODE, false);
1155 case LFUN_REFERENCE_GOTO:
1157 vector<Inset::Code> tmp;
1158 tmp.push_back(Inset::LABEL_CODE);
1159 tmp.push_back(Inset::REF_CODE);
1160 gotoInset(bv, tmp, true);
1165 Paragraph const * par = cursor.par();
1166 lyx::pos_type pos = cursor.pos();
1170 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1173 c = par->getChar(pos - 1);
1176 LyXLayout_ptr const & style = par->layout();
1178 if (style->pass_thru ||
1179 par->getFontSettings(bv->buffer()->params,
1180 pos).language()->lang() == "hebrew" ||
1181 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1182 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1186 case LFUN_DATE_INSERT: {
1187 time_t now_time_t = time(NULL);
1188 struct tm * now_tm = localtime(&now_time_t);
1189 setlocale(LC_TIME, "");
1191 if (!cmd.argument.empty())
1194 arg = lyxrc.date_insert_format;
1196 int const datetmp_len =
1197 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1199 for (int i = 0; i < datetmp_len; i++) {
1200 insertChar(bv, datetmp[i]);
1203 selection.cursor = cursor;
1204 moveCursorUpdate(bv, false);
1208 case LFUN_MOUSE_TRIPLE:
1211 if (bv_owner && bv->theLockingInset())
1213 if (cmd.button() == mouse_button::button1) {
1215 bv->screen().hideCursor();
1216 bv->screen().toggleSelection(this, bv);
1219 selection.cursor = cursor;
1223 bv->screen().toggleSelection(this, bv, false);
1225 bv->haveSelection(selection.set());
1229 case LFUN_MOUSE_DOUBLE:
1232 if (bv_owner && bv->theLockingInset())
1234 if (cmd.button() == mouse_button::button1) {
1236 bv->screen().hideCursor();
1237 bv->screen().toggleSelection(this, bv);
1238 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1239 bv->screen().toggleSelection(this, bv, false);
1241 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1244 bv->haveSelection(selection.set());
1248 case LFUN_MOUSE_MOTION:
1250 // Only use motion with button 1
1251 //if (ev.button() != mouse_button::button1)
1257 // Check for inset locking
1258 if (bv->theLockingInset()) {
1259 Inset * tli = bv->theLockingInset();
1260 LyXCursor cursor = bv->text->cursor;
1261 LyXFont font = bv->text->getFont(bv->buffer(),
1262 cursor.par(), cursor.pos());
1263 int width = tli->width(bv, font);
1264 int inset_x = font.isVisibleRightToLeft()
1265 ? cursor.ix() - width : cursor.ix();
1266 int start_x = inset_x + tli->scroll();
1267 FuncRequest cmd1 = cmd;
1268 cmd1.x = cmd.x - start_x;
1269 cmd1.y = cmd.y - cursor.iy() + bv->text->first_y;
1270 tli->localDispatch(cmd1);
1274 // The test for not selection possible is needed, that only motion
1275 // events are used, where the bottom press event was on
1276 // the drawing area too
1277 if (!selection_possible) {
1278 lyxerr[Debug::ACTION]
1279 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1283 bv->screen().hideCursor();
1285 Row * cursorrow = bv->text->cursor.row();
1286 bv->text->setCursorFromCoordinates(bv, cmd.x, cmd.y + bv->text->first_y);
1288 // sorry for this but I have a strange error that the y value jumps at
1289 // a certain point. This seems like an error in my xforms library or
1290 // in some other local environment, but I would like to leave this here
1291 // for the moment until I can remove this (Jug 20020418)
1292 if (y_before < bv->text->cursor.y())
1293 lyxerr << y_before << ":" << bv->text->cursor.y() << endl;
1295 // This is to allow jumping over large insets
1296 if (cursorrow == bv->text->cursor.row()) {
1297 if (cmd.y >= int(bv->workHeight()))
1298 bv->text->cursorDown(bv, false);
1300 bv->text->cursorUp(bv, false);
1303 // Maybe an empty line was deleted
1304 if (!bv->text->selection.set())
1305 bv->update(bv->text, BufferView::UPDATE);
1306 bv->text->setSelection(bv);
1307 bv->screen().toggleToggle(bv->text, bv);
1313 // Single-click on work area
1314 case LFUN_MOUSE_PRESS:
1319 // ok ok, this is a hack (for xforms)
1320 // We shouldn't go further down as we really should only do the
1321 // scrolling and be done with this. Otherwise we may open some
1322 // dialogs (Jug 20020424).
1323 if (cmd.button() == mouse_button::button4) {
1324 bv->scroll(-lyxrc.wheel_jump);
1327 if (cmd.button() == mouse_button::button5) {
1328 bv->scroll(lyxrc.wheel_jump);
1334 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1336 // Middle button press pastes if we have a selection
1337 // We do this here as if the selection was inside an inset
1338 // it could get cleared on the unlocking of the inset so
1339 // we have to check this first
1340 bool paste_internally = false;
1341 if (cmd.button() == mouse_button::button2 && selection.set()) {
1342 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1343 paste_internally = true;
1346 int const screen_first = bv->text->first_y;
1348 if (bv->theLockingInset()) {
1349 // We are in inset locking mode
1351 // Check whether the inset was hit. If not reset mode,
1352 // otherwise give the event to the inset
1353 if (inset_hit == bv->theLockingInset()) {
1354 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1355 bv->theLockingInset()->localDispatch(cmd1);
1358 bv->unlockInset(bv->theLockingInset());
1362 selection_possible = true;
1363 bv->screen().hideCursor();
1365 // Clear the selection
1366 bv->screen().toggleSelection(bv->text, bv);
1367 bv->text->clearSelection();
1368 bv->text->fullRebreak(bv);
1370 bv->updateScrollbar();
1372 // Single left click in math inset?
1373 if (isHighlyEditableInset(inset_hit)) {
1374 // Highly editable inset, like math
1375 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1376 selection_possible = false;
1377 bv->owner()->updateLayoutChoice();
1378 bv->owner()->message(inset->editMessage());
1379 //inset->edit(bv, x, y, cmd.button());
1380 // We just have to lock the inset before calling a PressEvent on it!
1381 // we don't need the edit() call here! (Jug20020329)
1382 if (!bv->lockInset(inset))
1383 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1384 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1385 inset->localDispatch(cmd1);
1388 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1390 // Right click on a footnote flag opens float menu
1391 if (cmd.button() == mouse_button::button3) {
1392 selection_possible = false;
1396 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1397 bv->text->setCursorFromCoordinates(bv, x, y + screen_first);
1399 bv->text->selection.cursor = bv->text->cursor;
1400 bv->text->cursor.x_fix(bv->text->cursor.x());
1402 bv->owner()->updateLayoutChoice();
1403 if (bv->fitCursor())
1404 selection_possible = false;
1406 // Insert primary selection with middle mouse
1407 // if there is a local selection in the current buffer,
1409 if (cmd.button() == mouse_button::button2) {
1410 if (paste_internally)
1411 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1413 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1414 selection_possible = false;
1419 case LFUN_MOUSE_RELEASE:
1421 // do nothing if we used the mouse wheel
1425 if (cmd.button() == mouse_button::button4
1426 || cmd.button() == mouse_button::button5)
1429 // If we hit an inset, we have the inset coordinates in these
1430 // and inset_hit points to the inset. If we do not hit an
1431 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1434 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1436 if (bv->theLockingInset()) {
1437 // We are in inset locking mode.
1439 // LyX does a kind of work-area grabbing for insets.
1440 // Only a ButtonPress FuncRequest outside the inset will
1441 // force a insetUnlock.
1442 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1443 bv->theLockingInset()->localDispatch(cmd1);
1447 selection_possible = false;
1449 if (cmd.button() == mouse_button::button2)
1453 if (cmd.button() == mouse_button::button1)
1454 bv->haveSelection(selection.set());
1457 bv->owner()->view_state_changed();
1458 bv->owner()->updateMenubar();
1459 bv->owner()->updateToolbar();
1461 // Did we hit an editable inset?
1463 selection_possible = false;
1465 // if we reach this point with a selection, it
1466 // must mean we are currently selecting.
1467 // But we don't want to open the inset
1468 // because that is annoying for the user.
1469 // So just pretend we didn't hit it.
1470 // this is OK because a "kosher" ButtonRelease
1471 // will follow a ButtonPress that clears
1473 // Note this also fixes selection drawing
1474 // problems if we end up opening an inset
1475 if (selection.set())
1478 // CHECK fix this proper in 0.13
1479 // well, maybe 13.0 !!!!!!!!!
1481 // Following a ref shouldn't issue
1482 // a push on the undo-stack
1483 // anylonger, now that we have
1484 // keybindings for following
1485 // references and returning from
1486 // references. IMHO though, it
1487 // should be the inset's own business
1488 // to push or not push on the undo
1489 // stack. They don't *have* to
1490 // alter the document...
1492 // ...or maybe the SetCursorParUndo()
1493 // below isn't necessary at all anylonger?
1494 if (inset_hit->lyxCode() == Inset::REF_CODE)
1495 setCursorParUndo(bv);
1497 bv->owner()->message(inset_hit->editMessage());
1499 if (isHighlyEditableInset(inset_hit)) {
1500 // Highly editable inset, like math
1501 UpdatableInset * inset = (UpdatableInset *) inset_hit;
1502 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1503 inset->localDispatch(cmd1);
1505 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1506 inset_hit->localDispatch(cmd1);
1507 // IMO this is a grosshack! Inset's should be changed so that
1508 // they call the actions they have to do with the insetButtonRel.
1509 // function and not in the edit(). This should be changed
1511 #ifdef WITH_WARNINGS
1512 #warning Please remove donot call inset->edit() here (Jug 20020812)
1514 inset_hit->edit(bv, x, y, cmd.button());
1519 // Maybe we want to edit a bibitem ale970302
1520 if (bv->text->cursor.par()->bibkey) {
1522 bv->text->cursor.par()->isRightToLeftPar(bv->buffer()->params);
1524 bibitemMaxWidth(bv, bv->buffer()->params.getLyXTextClass().defaultfont());
1525 if ((is_rtl && x > bv->text->workWidth(bv)-20-width) ||
1526 (!is_rtl && x < 20 + width)) {
1527 bv->text->cursor.par()->bibkey->edit(bv, 0, 0, mouse_button::none);
1533 case LFUN_SELFINSERT: {
1534 if (cmd.argument.empty())
1537 // Automatically delete the currently selected
1538 // text and replace it with what is being
1539 // typed in now. Depends on lyxrc settings
1540 // "auto_region_delete", which defaults to
1543 if (lyxrc.auto_region_delete) {
1544 if (selection.set()) {
1545 cutSelection(bv, false, false);
1548 bv->haveSelection(false);
1551 bv->beforeChange(this);
1552 LyXFont const old_font(real_current_font);
1554 string::const_iterator cit = cmd.argument.begin();
1555 string::const_iterator end = cmd.argument.end();
1556 for (; cit != end; ++cit)
1557 bv->owner()->getIntl().getTransManager().
1558 TranslateAndInsert(*cit, this);
1561 selection.cursor = cursor;
1562 moveCursorUpdate(bv, false);
1564 // real_current_font.number can change so we need to
1565 // update the minibuffer
1566 if (old_font != real_current_font)
1567 bv->owner()->view_state_changed();
1571 case LFUN_HTMLURL: {
1572 InsetCommandParams p("htmlurl");
1573 bv->owner()->getDialogs().createUrl(p.getAsString());
1578 InsetCommandParams p("url");
1579 bv->owner()->getDialogs().createUrl(p.getAsString());
1585 case LFUN_INSET_LIST:
1586 case LFUN_INSET_THEOREM:
1588 case LFUN_INSERT_NOTE:
1589 case LFUN_INSERT_URL:
1590 case LFUN_INSET_CAPTION:
1591 case LFUN_INSET_ERT:
1592 case LFUN_INSET_EXTERNAL:
1593 case LFUN_INSET_FLOAT:
1594 case LFUN_INSET_FOOTNOTE:
1595 case LFUN_INSET_MARGINAL:
1596 case LFUN_INSET_MINIPAGE:
1597 case LFUN_INSET_OPTARG:
1598 case LFUN_INSET_WIDE_FLOAT:
1599 case LFUN_INSET_WRAP:
1600 case LFUN_TABULAR_INSERT:
1601 case LFUN_INDEX_INSERT:
1602 case LFUN_INDEX_PRINT:
1603 case LFUN_PARENTINSERT:
1604 case LFUN_TOC_INSERT:
1606 Inset * inset = createInset(cmd);
1608 bool gotsel = false;
1609 if (selection.set()) {
1610 cutSelection(bv, true, false);
1613 if (bv->insertInset(inset)) {
1616 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
1626 return Inset::UNDISPATCHED;
1629 return Inset::DISPATCHED;