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;
1109 if (change_layout) {
1111 current_layout = layout;
1113 setLayout(bv, layout);
1114 bv->owner()->setLayout(layout);
1121 case LFUN_PASTESELECTION: {
1125 // this was originally a beforeChange(bv->text), i.e
1126 // the outermost LyXText!
1127 bv->beforeChange(this);
1128 string const clip = bv->getClipboard();
1129 if (!clip.empty()) {
1130 if (cmd.argument == "paragraph")
1131 insertStringAsParagraphs(bv, clip);
1133 insertStringAsLines(bv, clip);
1140 case LFUN_GOTOERROR:
1141 gotoInset(bv, Inset::ERROR_CODE, false);
1145 gotoInset(bv, Inset::NOTE_CODE, false);
1148 case LFUN_REFERENCE_GOTO:
1150 vector<Inset::Code> tmp;
1151 tmp.push_back(Inset::LABEL_CODE);
1152 tmp.push_back(Inset::REF_CODE);
1153 gotoInset(bv, tmp, true);
1158 Paragraph const * par = cursor.par();
1159 lyx::pos_type pos = cursor.pos();
1163 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1166 c = par->getChar(pos - 1);
1169 LyXLayout_ptr const & style = par->layout();
1171 if (style->pass_thru ||
1172 par->getFontSettings(bv->buffer()->params,
1173 pos).language()->lang() == "hebrew" ||
1174 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1175 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1179 case LFUN_DATE_INSERT: {
1180 time_t now_time_t = time(NULL);
1181 struct tm * now_tm = localtime(&now_time_t);
1182 setlocale(LC_TIME, "");
1184 if (!cmd.argument.empty())
1187 arg = lyxrc.date_insert_format;
1189 int const datetmp_len =
1190 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1192 for (int i = 0; i < datetmp_len; i++) {
1193 insertChar(bv, datetmp[i]);
1196 selection.cursor = cursor;
1197 moveCursorUpdate(bv, false);
1201 case LFUN_MOUSE_TRIPLE:
1204 if (bv_owner && bv->theLockingInset())
1206 if (cmd.button() == mouse_button::button1) {
1208 bv->screen().hideCursor();
1209 bv->screen().toggleSelection(this, bv);
1212 selection.cursor = cursor;
1216 bv->screen().toggleSelection(this, bv, false);
1218 bv->haveSelection(selection.set());
1222 case LFUN_MOUSE_DOUBLE:
1225 if (bv_owner && bv->theLockingInset())
1227 if (cmd.button() == mouse_button::button1) {
1229 bv->screen().hideCursor();
1230 bv->screen().toggleSelection(this, bv);
1231 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1232 bv->screen().toggleSelection(this, bv, false);
1234 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1237 bv->haveSelection(selection.set());
1241 case LFUN_MOUSE_MOTION:
1243 // Only use motion with button 1
1244 //if (ev.button() != mouse_button::button1)
1250 // Check for inset locking
1251 if (bv->theLockingInset()) {
1252 Inset * tli = bv->theLockingInset();
1253 LyXCursor cursor = bv->text->cursor;
1254 LyXFont font = bv->text->getFont(bv->buffer(),
1255 cursor.par(), cursor.pos());
1256 int width = tli->width(bv, font);
1257 int inset_x = font.isVisibleRightToLeft()
1258 ? cursor.ix() - width : cursor.ix();
1259 int start_x = inset_x + tli->scroll();
1260 FuncRequest cmd1 = cmd;
1261 cmd1.x = cmd.x - start_x;
1262 cmd1.y = cmd.y - cursor.iy() + bv->text->first_y;
1263 tli->localDispatch(cmd1);
1267 // The test for not selection possible is needed, that only motion
1268 // events are used, where the bottom press event was on
1269 // the drawing area too
1270 if (!selection_possible) {
1271 lyxerr[Debug::ACTION]
1272 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1276 bv->screen().hideCursor();
1278 Row * cursorrow = bv->text->cursor.row();
1279 bv->text->setCursorFromCoordinates(bv, cmd.x, cmd.y + bv->text->first_y);
1281 // sorry for this but I have a strange error that the y value jumps at
1282 // a certain point. This seems like an error in my xforms library or
1283 // in some other local environment, but I would like to leave this here
1284 // for the moment until I can remove this (Jug 20020418)
1285 if (y_before < bv->text->cursor.y())
1286 lyxerr << y_before << ":" << bv->text->cursor.y() << endl;
1288 // This is to allow jumping over large insets
1289 if (cursorrow == bv->text->cursor.row()) {
1290 if (cmd.y >= int(bv->workHeight()))
1291 bv->text->cursorDown(bv, false);
1293 bv->text->cursorUp(bv, false);
1296 // Maybe an empty line was deleted
1297 if (!bv->text->selection.set())
1298 bv->update(bv->text, BufferView::UPDATE);
1299 bv->text->setSelection(bv);
1300 bv->screen().toggleToggle(bv->text, bv);
1306 // Single-click on work area
1307 case LFUN_MOUSE_PRESS:
1312 // ok ok, this is a hack (for xforms)
1313 // We shouldn't go further down as we really should only do the
1314 // scrolling and be done with this. Otherwise we may open some
1315 // dialogs (Jug 20020424).
1316 if (cmd.button() == mouse_button::button4) {
1317 bv->scroll(-lyxrc.wheel_jump);
1320 if (cmd.button() == mouse_button::button5) {
1321 bv->scroll(lyxrc.wheel_jump);
1327 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1329 // Middle button press pastes if we have a selection
1330 // We do this here as if the selection was inside an inset
1331 // it could get cleared on the unlocking of the inset so
1332 // we have to check this first
1333 bool paste_internally = false;
1334 if (cmd.button() == mouse_button::button2 && selection.set()) {
1335 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1336 paste_internally = true;
1339 int const screen_first = bv->text->first_y;
1341 if (bv->theLockingInset()) {
1342 // We are in inset locking mode
1344 // Check whether the inset was hit. If not reset mode,
1345 // otherwise give the event to the inset
1346 if (inset_hit == bv->theLockingInset()) {
1347 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1348 bv->theLockingInset()->localDispatch(cmd1);
1351 bv->unlockInset(bv->theLockingInset());
1355 selection_possible = true;
1356 bv->screen().hideCursor();
1358 // Clear the selection
1359 bv->screen().toggleSelection(bv->text, bv);
1360 bv->text->clearSelection();
1361 bv->text->fullRebreak(bv);
1363 bv->updateScrollbar();
1365 // Single left click in math inset?
1366 if (isHighlyEditableInset(inset_hit)) {
1367 // Highly editable inset, like math
1368 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1369 selection_possible = false;
1370 bv->owner()->updateLayoutChoice();
1371 bv->owner()->message(inset->editMessage());
1372 //inset->edit(bv, x, y, cmd.button());
1373 // We just have to lock the inset before calling a PressEvent on it!
1374 // we don't need the edit() call here! (Jug20020329)
1375 if (!bv->lockInset(inset))
1376 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1377 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1378 inset->localDispatch(cmd1);
1381 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1383 // Right click on a footnote flag opens float menu
1384 if (cmd.button() == mouse_button::button3) {
1385 selection_possible = false;
1389 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1390 bv->text->setCursorFromCoordinates(bv, x, y + screen_first);
1392 bv->text->selection.cursor = bv->text->cursor;
1393 bv->text->cursor.x_fix(bv->text->cursor.x());
1395 bv->owner()->updateLayoutChoice();
1396 if (bv->fitCursor())
1397 selection_possible = false;
1399 // Insert primary selection with middle mouse
1400 // if there is a local selection in the current buffer,
1402 if (cmd.button() == mouse_button::button2) {
1403 if (paste_internally)
1404 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1406 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1407 selection_possible = false;
1412 case LFUN_MOUSE_RELEASE:
1414 // do nothing if we used the mouse wheel
1418 if (cmd.button() == mouse_button::button4
1419 || cmd.button() == mouse_button::button5)
1422 // If we hit an inset, we have the inset coordinates in these
1423 // and inset_hit points to the inset. If we do not hit an
1424 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1427 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1429 if (bv->theLockingInset()) {
1430 // We are in inset locking mode.
1432 // LyX does a kind of work-area grabbing for insets.
1433 // Only a ButtonPress FuncRequest outside the inset will
1434 // force a insetUnlock.
1435 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1436 bv->theLockingInset()->localDispatch(cmd1);
1440 selection_possible = false;
1442 if (cmd.button() == mouse_button::button2)
1446 if (cmd.button() == mouse_button::button1)
1447 bv->haveSelection(selection.set());
1450 bv->owner()->view_state_changed();
1451 bv->owner()->updateMenubar();
1452 bv->owner()->updateToolbar();
1454 // Did we hit an editable inset?
1456 selection_possible = false;
1458 // if we reach this point with a selection, it
1459 // must mean we are currently selecting.
1460 // But we don't want to open the inset
1461 // because that is annoying for the user.
1462 // So just pretend we didn't hit it.
1463 // this is OK because a "kosher" ButtonRelease
1464 // will follow a ButtonPress that clears
1466 // Note this also fixes selection drawing
1467 // problems if we end up opening an inset
1468 if (selection.set())
1471 // CHECK fix this proper in 0.13
1472 // well, maybe 13.0 !!!!!!!!!
1474 // Following a ref shouldn't issue
1475 // a push on the undo-stack
1476 // anylonger, now that we have
1477 // keybindings for following
1478 // references and returning from
1479 // references. IMHO though, it
1480 // should be the inset's own business
1481 // to push or not push on the undo
1482 // stack. They don't *have* to
1483 // alter the document...
1485 // ...or maybe the SetCursorParUndo()
1486 // below isn't necessary at all anylonger?
1487 if (inset_hit->lyxCode() == Inset::REF_CODE)
1488 setCursorParUndo(bv);
1490 bv->owner()->message(inset_hit->editMessage());
1492 if (isHighlyEditableInset(inset_hit)) {
1493 // Highly editable inset, like math
1494 UpdatableInset * inset = (UpdatableInset *) inset_hit;
1495 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1496 inset->localDispatch(cmd1);
1498 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1499 inset_hit->localDispatch(cmd1);
1500 // IMO this is a grosshack! Inset's should be changed so that
1501 // they call the actions they have to do with the insetButtonRel.
1502 // function and not in the edit(). This should be changed
1504 #ifdef WITH_WARNINGS
1505 #warning Please remove donot call inset->edit() here (Jug 20020812)
1507 inset_hit->edit(bv, x, y, cmd.button());
1512 // Maybe we want to edit a bibitem ale970302
1513 if (bv->text->cursor.par()->bibkey) {
1515 bv->text->cursor.par()->isRightToLeftPar(bv->buffer()->params);
1517 bibitemMaxWidth(bv, bv->buffer()->params.getLyXTextClass().defaultfont());
1518 if ((is_rtl && x > bv->text->workWidth(bv)-20-width) ||
1519 (!is_rtl && x < 20 + width)) {
1520 bv->text->cursor.par()->bibkey->edit(bv, 0, 0, mouse_button::none);
1526 case LFUN_SELFINSERT: {
1527 if (cmd.argument.empty())
1530 // Automatically delete the currently selected
1531 // text and replace it with what is being
1532 // typed in now. Depends on lyxrc settings
1533 // "auto_region_delete", which defaults to
1536 if (lyxrc.auto_region_delete) {
1537 if (selection.set()) {
1538 cutSelection(bv, false, false);
1541 bv->haveSelection(false);
1544 bv->beforeChange(this);
1545 LyXFont const old_font(real_current_font);
1547 string::const_iterator cit = cmd.argument.begin();
1548 string::const_iterator end = cmd.argument.end();
1549 for (; cit != end; ++cit)
1550 bv->owner()->getIntl().getTransManager().
1551 TranslateAndInsert(*cit, this);
1554 selection.cursor = cursor;
1555 moveCursorUpdate(bv, false);
1557 // real_current_font.number can change so we need to
1558 // update the minibuffer
1559 if (old_font != real_current_font)
1560 bv->owner()->view_state_changed();
1564 case LFUN_HTMLURL: {
1565 InsetCommandParams p("htmlurl");
1566 bv->owner()->getDialogs().createUrl(p.getAsString());
1571 InsetCommandParams p("url");
1572 bv->owner()->getDialogs().createUrl(p.getAsString());
1578 case LFUN_INSET_LIST:
1579 case LFUN_INSET_THEOREM:
1581 case LFUN_INSERT_NOTE:
1582 case LFUN_INSERT_URL:
1583 case LFUN_INSET_CAPTION:
1584 case LFUN_INSET_ERT:
1585 case LFUN_INSET_EXTERNAL:
1586 case LFUN_INSET_FLOAT:
1587 case LFUN_INSET_FOOTNOTE:
1588 case LFUN_INSET_MARGINAL:
1589 case LFUN_INSET_MINIPAGE:
1590 case LFUN_INSET_OPTARG:
1591 case LFUN_INSET_WIDE_FLOAT:
1592 case LFUN_INSET_WRAP:
1593 case LFUN_TABULAR_INSERT:
1594 case LFUN_INDEX_INSERT:
1595 case LFUN_INDEX_PRINT:
1596 case LFUN_PARENTINSERT:
1597 case LFUN_TOC_INSERT:
1599 Inset * inset = createInset(cmd);
1601 bool gotsel = false;
1602 if (selection.set()) {
1603 cutSelection(bv, true, false);
1606 if (bv->insertInset(inset)) {
1609 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
1619 return Inset::UNDISPATCHED;
1622 return Inset::DISPATCHED;