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 tmp->params().startOfAppendix(false);
389 par->params().startOfAppendix(start);
391 // we can set the refreshing parameters now
392 status(cmd.view(), LyXText::NEED_MORE_REFRESH);
394 refresh_row = 0; // not needed for full update
395 updateCounters(cmd.view());
396 setCursor(cmd.view(), cursor.par(), cursor.pos());
401 case LFUN_DELETE_WORD_FORWARD:
402 bv->beforeChange(this);
404 deleteWordForward(bv);
409 case LFUN_DELETE_WORD_BACKWARD:
410 bv->beforeChange(this);
412 deleteWordBackward(bv);
417 case LFUN_DELETE_LINE_FORWARD:
418 bv->beforeChange(this);
420 deleteLineForward(bv);
427 if (!selection.mark())
428 bv->beforeChange(this);
435 if (!selection.mark())
436 bv->beforeChange(this);
438 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
439 cursorLeftOneWord(bv);
441 cursorRightOneWord(bv);
446 if (!selection.mark())
447 bv->beforeChange(this);
449 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
450 cursorRightOneWord(bv);
452 cursorLeftOneWord(bv);
456 case LFUN_BEGINNINGBUF:
457 if (!selection.mark())
458 bv->beforeChange(this);
465 if (selection.mark())
466 bv->beforeChange(this);
474 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
478 finishChange(bv, true);
483 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
487 finishChange(bv, true);
493 finishChange(bv, true);
498 cursorDown(bv, true);
499 finishChange(bv, true);
502 case LFUN_UP_PARAGRAPHSEL:
504 cursorUpParagraph(bv);
505 finishChange(bv, true);
508 case LFUN_DOWN_PARAGRAPHSEL:
510 cursorDownParagraph(bv);
511 finishChange(bv, true);
517 finishChange(bv, true);
523 finishChange(bv, true);
529 finishChange(bv, true);
535 finishChange(bv, true);
538 case LFUN_WORDRIGHTSEL:
540 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
541 cursorLeftOneWord(bv);
543 cursorRightOneWord(bv);
544 finishChange(bv, true);
547 case LFUN_WORDLEFTSEL:
549 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
550 cursorRightOneWord(bv);
552 cursorLeftOneWord(bv);
553 finishChange(bv, true);
560 getWord(cur1, cur2, WHOLE_WORD);
561 setCursor(bv, cur1.par(), cur1.pos());
562 bv->beforeChange(this);
563 setCursor(bv, cur2.par(), cur2.pos());
564 finishChange(bv, true);
569 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
570 if (!selection.mark())
571 bv->beforeChange(this);
574 cursorLeft(bv, false);
575 if (cursor.pos() < cursor.par()->size()
576 && cursor.par()->isInset(cursor.pos())
577 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
578 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
579 cmd.message(tmpinset->editMessage());
580 tmpinset->edit(bv, !is_rtl);
584 cursorRight(bv, false);
590 // This is soooo ugly. Isn`t it possible to make
592 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
593 if (!selection.mark())
594 bv->beforeChange(this);
596 LyXCursor const cur = cursor;
598 cursorLeft(bv, false);
599 if ((is_rtl || cur != cursor) && // only if really moved!
600 cursor.pos() < cursor.par()->size() &&
601 cursor.par()->isInset(cursor.pos()) &&
602 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
603 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
604 cmd.message(tmpinset->editMessage());
605 tmpinset->edit(bv, is_rtl);
609 cursorRight(bv, false);
615 if (!selection.mark())
616 bv->beforeChange(this);
617 bv->update(this, BufferView::UPDATE);
623 if (!selection.mark())
624 bv->beforeChange(this);
625 bv->update(this, BufferView::UPDATE);
630 case LFUN_UP_PARAGRAPH:
631 if (!selection.mark())
632 bv->beforeChange(this);
633 bv->update(this, BufferView::UPDATE);
634 cursorUpParagraph(bv);
638 case LFUN_DOWN_PARAGRAPH:
639 if (!selection.mark())
640 bv->beforeChange(this);
641 bv->update(this, BufferView::UPDATE);
642 cursorDownParagraph(bv);
643 finishChange(bv, false);
647 if (!selection.mark())
648 bv->beforeChange(this);
649 bv->update(this, BufferView::UPDATE);
651 finishChange(bv, false);
654 // moveCursorUpdate(bv, false, false);
655 // owner_->view_state_changed();
659 if (!selection.mark())
660 bv->beforeChange(this);
661 bv->update(this, BufferView::UPDATE);
663 finishChange(bv, false);
667 if (!selection.mark())
668 bv->beforeChange(this);
671 finishChange(bv, false);
675 if (!selection.mark())
676 bv->beforeChange(this);
679 finishChange(bv, false);
683 bv->beforeChange(this);
684 insertChar(bv, Paragraph::META_NEWLINE);
686 setCursor(bv, cursor.par(), cursor.pos());
687 moveCursorUpdate(bv, false);
691 if (!selection.set()) {
693 selection.cursor = cursor;
695 // It is possible to make it a lot faster still
696 // just comment out the line below...
700 cutSelection(bv, true);
703 moveCursorUpdate(bv, false);
704 bv->owner()->view_state_changed();
708 case LFUN_DELETE_SKIP:
709 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
710 if (!selection.set()) {
711 LyXCursor cur = cursor;
712 if (cur.pos() == cur.par()->size()) {
716 && !(cur.par()->params().spaceTop()
717 == VSpace (VSpace::NONE))) {
719 cur.par()->params().lineTop(),
720 cur.par()->params().lineBottom(),
721 cur.par()->params().pagebreakTop(),
722 cur.par()->params().pagebreakBottom(),
723 VSpace(VSpace::NONE),
724 cur.par()->params().spaceBottom(),
725 cur.par()->params().spacing(),
726 cur.par()->params().align(),
727 cur.par()->params().labelWidthString(), 0);
733 selection.cursor = cursor;
737 selection.cursor = cursor;
741 cutSelection(bv, true);
748 if (!selection.set()) {
749 if (bv->owner()->getIntl().getTransManager().backspace()) {
751 selection.cursor = cursor;
753 // It is possible to make it a lot faster still
754 // just comment out the line below...
759 cutSelection(bv, true);
762 bv->owner()->view_state_changed();
766 case LFUN_BACKSPACE_SKIP:
767 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
768 if (!selection.set()) {
769 LyXCursor cur = cursor;
771 && !(cur.par()->params().spaceTop()
772 == VSpace (VSpace::NONE))) {
774 cur.par()->params().lineTop(),
775 cur.par()->params().lineBottom(),
776 cur.par()->params().pagebreakTop(),
777 cur.par()->params().pagebreakBottom(),
778 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
779 cur.par()->params().spacing(),
780 cur.par()->params().align(),
781 cur.par()->params().labelWidthString(), 0);
784 selection.cursor = cur;
788 cutSelection(bv, true);
793 case LFUN_BREAKPARAGRAPH:
794 bv->beforeChange(this);
795 breakParagraph(bv, 0);
797 selection.cursor = cursor;
799 bv->owner()->view_state_changed();
802 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
803 bv->beforeChange(this);
804 breakParagraph(bv, 1);
806 selection.cursor = cursor;
808 bv->owner()->view_state_changed();
811 case LFUN_BREAKPARAGRAPH_SKIP: {
812 // When at the beginning of a paragraph, remove
813 // indentation and add a "defskip" at the top.
814 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
815 LyXCursor cur = cursor;
816 bv->beforeChange(this);
817 if (cur.pos() == 0) {
818 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
820 cur.par()->params().lineTop(),
821 cur.par()->params().lineBottom(),
822 cur.par()->params().pagebreakTop(),
823 cur.par()->params().pagebreakBottom(),
824 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
825 cur.par()->params().spacing(),
826 cur.par()->params().align(),
827 cur.par()->params().labelWidthString(), 1);
832 breakParagraph(bv, 0);
836 selection.cursor = cur;
838 bv->owner()->view_state_changed();
842 case LFUN_PARAGRAPH_SPACING: {
843 Paragraph * par = cursor.par();
844 Spacing::Space cur_spacing = par->params().spacing().getSpace();
845 float cur_value = 1.0;
846 if (cur_spacing == Spacing::Other)
847 cur_value = par->params().spacing().getValue();
849 istringstream is(cmd.argument.c_str());
852 Spacing::Space new_spacing = cur_spacing;
853 float new_value = cur_value;
855 lyxerr << "Missing argument to `paragraph-spacing'"
857 } else if (tmp == "single") {
858 new_spacing = Spacing::Single;
859 } else if (tmp == "onehalf") {
860 new_spacing = Spacing::Onehalf;
861 } else if (tmp == "double") {
862 new_spacing = Spacing::Double;
863 } else if (tmp == "other") {
864 new_spacing = Spacing::Other;
867 lyxerr << "new_value = " << tmpval << endl;
870 } else if (tmp == "default") {
871 new_spacing = Spacing::Default;
873 lyxerr << _("Unknown spacing argument: ")
874 << cmd.argument << endl;
876 if (cur_spacing != new_spacing || cur_value != new_value) {
877 par->params().spacing(Spacing(new_spacing, new_value));
884 case LFUN_INSET_TOGGLE:
886 bv->beforeChange(this);
893 case LFUN_PROTECTEDSPACE:
894 if (cursor.par()->layout()->free_spacing) {
898 specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
900 moveCursorUpdate(bv, false);
903 case LFUN_HYPHENATION:
904 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
907 case LFUN_LIGATURE_BREAK:
908 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
912 specialChar(this, bv, InsetSpecialChar::LDOTS);
918 insertChar(bv, Paragraph::META_HFILL);
922 case LFUN_END_OF_SENTENCE:
923 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
926 case LFUN_MENU_SEPARATOR:
927 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
931 bv->beforeChange(this);
933 selection.cursor = cursor;
934 cmd.message(N_("Mark off"));
938 bv->beforeChange(this);
939 selection.mark(true);
941 selection.cursor = cursor;
942 cmd.message(N_("Mark on"));
946 bv->beforeChange(this);
947 if (selection.mark()) {
949 cmd.message(N_("Mark removed"));
951 selection.mark(true);
953 cmd.message(N_("Mark set"));
955 selection.cursor = cursor;
958 case LFUN_UPCASE_WORD:
960 changeCase(bv, LyXText::text_uppercase);
962 bv->updateInset(inset_owner, true);
966 case LFUN_LOWCASE_WORD:
968 changeCase(bv, LyXText::text_lowercase);
970 bv->updateInset(inset_owner, true);
974 case LFUN_CAPITALIZE_WORD:
976 changeCase(bv, LyXText::text_capitalization);
978 bv->updateInset(inset_owner, true);
982 case LFUN_TRANSPOSE_CHARS:
986 bv->updateInset(inset_owner, true);
991 cmd.message(_("Paste"));
993 // clear the selection
994 bv->toggleSelection();
998 clearSelection(); // bug 393
1007 cutSelection(bv, true);
1009 cmd.message(_("Cut"));
1014 cmd.message(_("Copy"));
1017 case LFUN_BEGINNINGBUFSEL:
1019 return Inset::UNDISPATCHED;
1022 finishChange(bv, true);
1025 case LFUN_ENDBUFSEL:
1027 return Inset::UNDISPATCHED;
1030 finishChange(bv, true);
1034 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1040 istringstream is(cmd.argument.c_str());
1043 lyxerr << "SETXY: Could not parse coordinates in '"
1044 << cmd.argument << std::endl;
1046 setCursorFromCoordinates(bv, x, y);
1051 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1053 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1059 case LFUN_GETLAYOUT:
1060 cmd.message(tostr(cursor.par()->layout()));
1064 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1065 << cmd.argument << endl;
1067 // This is not the good solution to the empty argument
1068 // problem, but it will hopefully suffice for 1.2.0.
1069 // The correct solution would be to augument the
1070 // function list/array with information about what
1071 // functions needs arguments and their type.
1072 if (cmd.argument.empty()) {
1073 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1077 // Derive layout number from given argument (string)
1078 // and current buffer's textclass (number)
1079 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1080 bool hasLayout = tclass.hasLayout(cmd.argument);
1081 string layout = cmd.argument;
1083 // If the entry is obsolete, use the new one instead.
1085 string const & obs = tclass[layout]->obsoleted_by();
1091 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1096 bool change_layout = (current_layout != layout);
1097 if (!change_layout && selection.set() &&
1098 selection.start.par() != selection.end.par())
1100 Paragraph * spar = selection.start.par();
1101 Paragraph * epar = selection.end.par()->next();
1102 while (spar != epar) {
1103 if (spar->layout()->name() != current_layout) {
1104 change_layout = true;
1107 spar = spar->next();
1110 if (change_layout) {
1112 current_layout = layout;
1114 setLayout(bv, layout);
1115 bv->owner()->setLayout(layout);
1122 case LFUN_PASTESELECTION: {
1126 // this was originally a beforeChange(bv->text), i.e
1127 // the outermost LyXText!
1128 bv->beforeChange(this);
1129 string const clip = bv->getClipboard();
1130 if (!clip.empty()) {
1131 if (cmd.argument == "paragraph")
1132 insertStringAsParagraphs(bv, clip);
1134 insertStringAsLines(bv, clip);
1141 case LFUN_GOTOERROR:
1142 gotoInset(bv, Inset::ERROR_CODE, false);
1146 gotoInset(bv, Inset::NOTE_CODE, false);
1149 case LFUN_REFERENCE_GOTO:
1151 vector<Inset::Code> tmp;
1152 tmp.push_back(Inset::LABEL_CODE);
1153 tmp.push_back(Inset::REF_CODE);
1154 gotoInset(bv, tmp, true);
1159 Paragraph const * par = cursor.par();
1160 lyx::pos_type pos = cursor.pos();
1164 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1167 c = par->getChar(pos - 1);
1170 LyXLayout_ptr const & style = par->layout();
1172 if (style->pass_thru ||
1173 par->getFontSettings(bv->buffer()->params,
1174 pos).language()->lang() == "hebrew" ||
1175 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1176 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1180 case LFUN_DATE_INSERT: {
1181 time_t now_time_t = time(NULL);
1182 struct tm * now_tm = localtime(&now_time_t);
1183 setlocale(LC_TIME, "");
1185 if (!cmd.argument.empty())
1188 arg = lyxrc.date_insert_format;
1190 int const datetmp_len =
1191 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1193 for (int i = 0; i < datetmp_len; i++) {
1194 insertChar(bv, datetmp[i]);
1197 selection.cursor = cursor;
1198 moveCursorUpdate(bv, false);
1202 case LFUN_MOUSE_TRIPLE:
1205 if (bv_owner && bv->theLockingInset())
1207 if (cmd.button() == mouse_button::button1) {
1209 bv->screen().hideCursor();
1210 bv->screen().toggleSelection(this, bv);
1213 selection.cursor = cursor;
1217 bv->screen().toggleSelection(this, bv, false);
1219 bv->haveSelection(selection.set());
1223 case LFUN_MOUSE_DOUBLE:
1226 if (bv_owner && bv->theLockingInset())
1228 if (cmd.button() == mouse_button::button1) {
1230 bv->screen().hideCursor();
1231 bv->screen().toggleSelection(this, bv);
1232 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1233 bv->screen().toggleSelection(this, bv, false);
1235 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1238 bv->haveSelection(selection.set());
1242 case LFUN_MOUSE_MOTION:
1244 // Only use motion with button 1
1245 //if (ev.button() != mouse_button::button1)
1251 // Check for inset locking
1252 if (bv->theLockingInset()) {
1253 Inset * tli = bv->theLockingInset();
1254 LyXCursor cursor = bv->text->cursor;
1255 LyXFont font = bv->text->getFont(bv->buffer(),
1256 cursor.par(), cursor.pos());
1257 int width = tli->width(bv, font);
1258 int inset_x = font.isVisibleRightToLeft()
1259 ? cursor.ix() - width : cursor.ix();
1260 int start_x = inset_x + tli->scroll();
1261 FuncRequest cmd1 = cmd;
1262 cmd1.x = cmd.x - start_x;
1263 cmd1.y = cmd.y - cursor.iy() + bv->text->first_y;
1264 tli->localDispatch(cmd1);
1268 // The test for not selection possible is needed, that only motion
1269 // events are used, where the bottom press event was on
1270 // the drawing area too
1271 if (!selection_possible) {
1272 lyxerr[Debug::ACTION]
1273 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1277 bv->screen().hideCursor();
1279 Row * cursorrow = bv->text->cursor.row();
1280 bv->text->setCursorFromCoordinates(bv, cmd.x, cmd.y + bv->text->first_y);
1282 // sorry for this but I have a strange error that the y value jumps at
1283 // a certain point. This seems like an error in my xforms library or
1284 // in some other local environment, but I would like to leave this here
1285 // for the moment until I can remove this (Jug 20020418)
1286 if (y_before < bv->text->cursor.y())
1287 lyxerr << y_before << ":" << bv->text->cursor.y() << endl;
1289 // This is to allow jumping over large insets
1290 if (cursorrow == bv->text->cursor.row()) {
1291 if (cmd.y >= int(bv->workHeight()))
1292 bv->text->cursorDown(bv, false);
1294 bv->text->cursorUp(bv, false);
1297 // Maybe an empty line was deleted
1298 if (!bv->text->selection.set())
1299 bv->update(bv->text, BufferView::UPDATE);
1300 bv->text->setSelection(bv);
1301 bv->screen().toggleToggle(bv->text, bv);
1307 // Single-click on work area
1308 case LFUN_MOUSE_PRESS:
1313 // ok ok, this is a hack (for xforms)
1314 // We shouldn't go further down as we really should only do the
1315 // scrolling and be done with this. Otherwise we may open some
1316 // dialogs (Jug 20020424).
1317 if (cmd.button() == mouse_button::button4) {
1318 bv->scroll(-lyxrc.wheel_jump);
1321 if (cmd.button() == mouse_button::button5) {
1322 bv->scroll(lyxrc.wheel_jump);
1328 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1330 // Middle button press pastes if we have a selection
1331 // We do this here as if the selection was inside an inset
1332 // it could get cleared on the unlocking of the inset so
1333 // we have to check this first
1334 bool paste_internally = false;
1335 if (cmd.button() == mouse_button::button2 && selection.set()) {
1336 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1337 paste_internally = true;
1340 int const screen_first = bv->text->first_y;
1342 if (bv->theLockingInset()) {
1343 // We are in inset locking mode
1345 // Check whether the inset was hit. If not reset mode,
1346 // otherwise give the event to the inset
1347 if (inset_hit == bv->theLockingInset()) {
1348 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1349 bv->theLockingInset()->localDispatch(cmd1);
1352 bv->unlockInset(bv->theLockingInset());
1356 selection_possible = true;
1357 bv->screen().hideCursor();
1359 // Clear the selection
1360 bv->screen().toggleSelection(bv->text, bv);
1361 bv->text->clearSelection();
1362 bv->text->fullRebreak(bv);
1364 bv->updateScrollbar();
1366 // Single left click in math inset?
1367 if (isHighlyEditableInset(inset_hit)) {
1368 // Highly editable inset, like math
1369 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1370 selection_possible = false;
1371 bv->owner()->updateLayoutChoice();
1372 bv->owner()->message(inset->editMessage());
1373 //inset->edit(bv, x, y, cmd.button());
1374 // We just have to lock the inset before calling a PressEvent on it!
1375 // we don't need the edit() call here! (Jug20020329)
1376 if (!bv->lockInset(inset))
1377 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1378 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1379 inset->localDispatch(cmd1);
1382 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1384 // Right click on a footnote flag opens float menu
1385 if (cmd.button() == mouse_button::button3) {
1386 selection_possible = false;
1390 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1391 bv->text->setCursorFromCoordinates(bv, x, y + screen_first);
1393 bv->text->selection.cursor = bv->text->cursor;
1394 bv->text->cursor.x_fix(bv->text->cursor.x());
1396 bv->owner()->updateLayoutChoice();
1397 if (bv->fitCursor())
1398 selection_possible = false;
1400 // Insert primary selection with middle mouse
1401 // if there is a local selection in the current buffer,
1403 if (cmd.button() == mouse_button::button2) {
1404 if (paste_internally)
1405 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1407 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1408 selection_possible = false;
1413 case LFUN_MOUSE_RELEASE:
1415 // do nothing if we used the mouse wheel
1419 if (cmd.button() == mouse_button::button4
1420 || cmd.button() == mouse_button::button5)
1423 // If we hit an inset, we have the inset coordinates in these
1424 // and inset_hit points to the inset. If we do not hit an
1425 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1428 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1430 if (bv->theLockingInset()) {
1431 // We are in inset locking mode.
1433 // LyX does a kind of work-area grabbing for insets.
1434 // Only a ButtonPress FuncRequest outside the inset will
1435 // force a insetUnlock.
1436 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1437 bv->theLockingInset()->localDispatch(cmd1);
1441 selection_possible = false;
1443 if (cmd.button() == mouse_button::button2)
1447 if (cmd.button() == mouse_button::button1)
1448 bv->haveSelection(selection.set());
1451 bv->owner()->view_state_changed();
1452 bv->owner()->updateMenubar();
1453 bv->owner()->updateToolbar();
1455 // Did we hit an editable inset?
1457 selection_possible = false;
1459 // if we reach this point with a selection, it
1460 // must mean we are currently selecting.
1461 // But we don't want to open the inset
1462 // because that is annoying for the user.
1463 // So just pretend we didn't hit it.
1464 // this is OK because a "kosher" ButtonRelease
1465 // will follow a ButtonPress that clears
1467 // Note this also fixes selection drawing
1468 // problems if we end up opening an inset
1469 if (selection.set())
1472 // CHECK fix this proper in 0.13
1473 // well, maybe 13.0 !!!!!!!!!
1475 // Following a ref shouldn't issue
1476 // a push on the undo-stack
1477 // anylonger, now that we have
1478 // keybindings for following
1479 // references and returning from
1480 // references. IMHO though, it
1481 // should be the inset's own business
1482 // to push or not push on the undo
1483 // stack. They don't *have* to
1484 // alter the document...
1486 // ...or maybe the SetCursorParUndo()
1487 // below isn't necessary at all anylonger?
1488 if (inset_hit->lyxCode() == Inset::REF_CODE)
1489 setCursorParUndo(bv);
1491 bv->owner()->message(inset_hit->editMessage());
1493 if (isHighlyEditableInset(inset_hit)) {
1494 // Highly editable inset, like math
1495 UpdatableInset * inset = (UpdatableInset *) inset_hit;
1496 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1497 inset->localDispatch(cmd1);
1499 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1500 inset_hit->localDispatch(cmd1);
1501 // IMO this is a grosshack! Inset's should be changed so that
1502 // they call the actions they have to do with the insetButtonRel.
1503 // function and not in the edit(). This should be changed
1505 #ifdef WITH_WARNINGS
1506 #warning Please remove donot call inset->edit() here (Jug 20020812)
1508 inset_hit->edit(bv, x, y, cmd.button());
1513 // Maybe we want to edit a bibitem ale970302
1514 if (bv->text->cursor.par()->bibkey) {
1516 bv->text->cursor.par()->isRightToLeftPar(bv->buffer()->params);
1518 bibitemMaxWidth(bv, bv->buffer()->params.getLyXTextClass().defaultfont());
1519 if ((is_rtl && x > bv->text->workWidth(bv)-20-width) ||
1520 (!is_rtl && x < 20 + width)) {
1521 bv->text->cursor.par()->bibkey->edit(bv, 0, 0, mouse_button::none);
1527 case LFUN_SELFINSERT: {
1528 if (cmd.argument.empty())
1531 // Automatically delete the currently selected
1532 // text and replace it with what is being
1533 // typed in now. Depends on lyxrc settings
1534 // "auto_region_delete", which defaults to
1537 if (lyxrc.auto_region_delete) {
1538 if (selection.set()) {
1539 cutSelection(bv, false, false);
1542 bv->haveSelection(false);
1545 bv->beforeChange(this);
1546 LyXFont const old_font(real_current_font);
1548 string::const_iterator cit = cmd.argument.begin();
1549 string::const_iterator end = cmd.argument.end();
1550 for (; cit != end; ++cit)
1551 bv->owner()->getIntl().getTransManager().
1552 TranslateAndInsert(*cit, this);
1555 selection.cursor = cursor;
1556 moveCursorUpdate(bv, false);
1558 // real_current_font.number can change so we need to
1559 // update the minibuffer
1560 if (old_font != real_current_font)
1561 bv->owner()->view_state_changed();
1565 case LFUN_HTMLURL: {
1566 InsetCommandParams p("htmlurl");
1567 bv->owner()->getDialogs().createUrl(p.getAsString());
1572 InsetCommandParams p("url");
1573 bv->owner()->getDialogs().createUrl(p.getAsString());
1579 case LFUN_INSET_LIST:
1580 case LFUN_INSET_THEOREM:
1582 case LFUN_INSERT_NOTE:
1583 case LFUN_INSERT_URL:
1584 case LFUN_INSET_CAPTION:
1585 case LFUN_INSET_ERT:
1586 case LFUN_INSET_EXTERNAL:
1587 case LFUN_INSET_FLOAT:
1588 case LFUN_INSET_FOOTNOTE:
1589 case LFUN_INSET_MARGINAL:
1590 case LFUN_INSET_MINIPAGE:
1591 case LFUN_INSET_OPTARG:
1592 case LFUN_INSET_WIDE_FLOAT:
1593 case LFUN_INSET_WRAP:
1594 case LFUN_TABULAR_INSERT:
1595 case LFUN_INDEX_INSERT:
1596 case LFUN_INDEX_PRINT:
1597 case LFUN_PARENTINSERT:
1598 case LFUN_TOC_INSERT:
1600 Inset * inset = createInset(cmd);
1602 bool gotsel = false;
1603 if (selection.set()) {
1604 cutSelection(bv, true, false);
1607 if (bv->insertInset(inset)) {
1610 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
1620 return Inset::UNDISPATCHED;
1623 return Inset::DISPATCHED;