1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2002 The LyX Team.
9 * ====================================================== */
15 #include "paragraph.h"
16 #include "BufferView.h"
17 #include "funcrequest.h"
20 #include "bufferparams.h"
22 #include "ParagraphParameters.h"
28 #include "support/lstrings.h"
29 #include "frontends/LyXView.h"
30 #include "frontends/screen.h"
31 #include "frontends/WorkArea.h"
32 #include "insets/insetspecialchar.h"
33 #include "insets/insettext.h"
34 #include "insets/insetbib.h"
35 #include "insets/insetquotes.h"
36 #include "insets/insetcommand.h"
37 #include "undo_funcs.h"
44 extern string current_layout;
45 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
47 // the selection possible is needed, that only motion events are
48 // used, where the bottom press event was on the drawing area too
49 bool selection_possible = false;
54 void moveCursorUpdate(BufferView * bv, bool selecting)
56 LyXText * lt = bv->getLyXText();
58 if (selecting || lt->selection.mark()) {
63 bv->updateInset(lt->inset_owner, false);
67 // bv->update(lt, BufferView::SELECT|BufferView::FITCUR);
69 bv->update(lt, BufferView::SELECT|BufferView::FITCUR);
71 } else if (bv->text->status() != LyXText::UNCHANGED) {
72 bv->theLockingInset()->hideInsetCursor(bv);
73 bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
77 if (!lt->selection.set())
78 bv->workarea().haveSelection(false);
84 void finishChange(BufferView * bv, bool selecting = false)
87 moveCursorUpdate(bv, selecting);
88 bv->owner()->view_state_changed();
91 // check if the given co-ordinates are inside an inset at the
92 // given cursor, if one exists. If so, the inset is returned,
93 // and the co-ordinates are made relative. Otherwise, 0 is returned.
94 Inset * checkInset(BufferView * bv, LyXText const & text,
95 LyXCursor const & cur, int & x, int & y)
97 lyx::pos_type const pos = cur.pos();
98 Paragraph /*const*/ & par = *cur.par();
100 if (pos >= par.size() || !par.isInset(pos))
103 Inset /*const*/ * inset = par.getInset(pos);
105 if (!isEditableInset(inset))
108 // get inset dimensions
109 lyx::Assert(par.getInset(pos));
111 LyXFont const & font = text.getFont(bv->buffer(), &par, pos);
113 int const width = inset->width(bv, font);
114 int const inset_x = font.isVisibleRightToLeft()
115 ? (cur.ix() - width) : cur.ix();
118 inset_x + inset->scroll(),
120 cur.iy() - inset->ascent(bv, font),
121 cur.iy() + inset->descent(bv, font)
124 if (!b.contained(x, y)) {
125 lyxerr[Debug::GUI] << "Missed inset at x,y " << x << "," << y
126 << " box " << b << endl;
130 text.setCursor(bv, &par, pos, true);
133 // The origin of an inset is on the baseline
134 y -= text.cursor.iy();
142 Inset * LyXText::checkInsetHit(BufferView * bv, int & x, int & y) const
144 int y_tmp = y + first_y;
147 setCursorFromCoordinates(bv, cur, x, y_tmp);
149 Inset * inset = checkInset(bv, *this, cur, x, y_tmp);
155 // look at previous position
160 setCursor(bv, cur, cur.par(), cur.pos() - 1, true);
162 inset = checkInset(bv, *this, cur, x, y_tmp);
169 bool LyXText::gotoNextInset(BufferView * bv,
170 vector<Inset::Code> const & codes, string const & contents) const
172 LyXCursor res = cursor;
175 if (res.pos() < res.par()->size() - 1) {
176 res.pos(res.pos() + 1);
178 res.par(res.par()->next());
182 } while (res.par() &&
183 !(res.par()->isInset(res.pos())
184 && (inset = res.par()->getInset(res.pos())) != 0
185 && find(codes.begin(), codes.end(), inset->lyxCode())
187 && (contents.empty() ||
188 static_cast<InsetCommand *>(
189 res.par()->getInset(res.pos()))->getContents()
193 setCursor(bv, res.par(), res.pos(), false);
200 void LyXText::gotoInset(BufferView * bv, vector<Inset::Code> const & codes,
204 bv->beforeChange(this);
208 if (same_content && cursor.par()->isInset(cursor.pos())) {
209 Inset const * inset = cursor.par()->getInset(cursor.pos());
210 if (find(codes.begin(), codes.end(), inset->lyxCode())
212 contents = static_cast<InsetCommand const *>(inset)->getContents();
215 if (!gotoNextInset(bv, codes, contents)) {
216 if (cursor.pos() || cursor.par() != ownerParagraph()) {
217 LyXCursor tmp = cursor;
218 cursor.par(ownerParagraph());
220 if (!gotoNextInset(bv, codes, contents)) {
222 bv->owner()->message(_("No more insets"));
225 bv->owner()->message(_("No more insets"));
229 selection.cursor = cursor;
233 void LyXText::gotoInset(BufferView * bv, Inset::Code code, bool same_content)
235 gotoInset(bv, vector<Inset::Code>(1, code), same_content);
239 void LyXText::cursorPrevious(BufferView * bv)
241 if (!cursor.row()->previous()) {
243 int new_y = bv->text->first_y - bv->workarea().workHeight();
244 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
245 bv->updateScrollbar();
251 Row * cursorrow = cursor.row();
253 setCursorFromCoordinates(bv, cursor.x_fix(), y);
257 if (cursorrow == bv->text->cursor.row()) {
258 // we have a row which is higher than the workarea so we leave the
259 // cursor on the start of the row and move only the draw up as soon
260 // as we move the cursor or do something while inside the row (it may
261 // span several workarea-heights) we'll move to the top again, but this
262 // is better than just jump down and only display part of the row.
263 new_y = bv->text->first_y - bv->workarea().workHeight();
266 new_y = bv->text->cursor.iy()
267 + bv->theLockingInset()->insetInInsetY() + y
268 + cursor.row()->height()
269 - bv->workarea().workHeight() + 1;
272 - cursor.row()->baseline()
273 + cursor.row()->height()
274 - bv->workarea().workHeight() + 1;
277 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
278 if (cursor.row()->previous()) {
280 setCursor(bv, cur, cursor.row()->previous()->par(),
281 cursor.row()->previous()->pos(), false);
282 if (cur.y() > first_y) {
286 bv->updateScrollbar();
290 void LyXText::cursorNext(BufferView * bv)
292 if (!cursor.row()->next()) {
293 int y = cursor.y() - cursor.row()->baseline() +
294 cursor.row()->height();
295 if (y > int(first_y + bv->workarea().workHeight())) {
296 bv->screen().draw(bv->text, bv,
297 bv->text->first_y + bv->workarea().workHeight());
298 bv->updateScrollbar();
303 int y = first_y + bv->workarea().workHeight();
304 if (inset_owner && !first_y) {
305 y -= (bv->text->cursor.iy()
307 + bv->theLockingInset()->insetInInsetY());
312 Row * cursorrow = cursor.row();
313 setCursorFromCoordinates(bv, cursor.x_fix(), y);
314 // + workarea().workHeight());
318 if (cursorrow == bv->text->cursor.row()) {
319 // we have a row which is higher than the workarea so we leave the
320 // cursor on the start of the row and move only the draw down as soon
321 // as we move the cursor or do something while inside the row (it may
322 // span several workarea-heights) we'll move to the top again, but this
323 // is better than just jump down and only display part of the row.
324 new_y = bv->text->first_y + bv->workarea().workHeight();
327 new_y = bv->text->cursor.iy()
328 + bv->theLockingInset()->insetInInsetY()
329 + y - cursor.row()->baseline();
331 new_y = cursor.y() - cursor.row()->baseline();
334 bv->screen().draw(bv->text, bv, new_y);
335 if (cursor.row()->next()) {
337 setCursor(bv, cur, cursor.row()->next()->par(),
338 cursor.row()->next()->pos(), false);
339 if (cur.y() < int(first_y + bv->workarea().workHeight())) {
340 cursorDown(bv, true);
343 bv->updateScrollbar();
347 void LyXText::update(BufferView * bv, bool changed)
349 BufferView::UpdateCodes c = BufferView::SELECT | BufferView::FITCUR;
351 bv->update(this, c | BufferView::CHANGE);
357 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
361 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
362 if (!bv->insertInset(new_inset))
365 bv->updateInset(new_inset, true);
369 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
371 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
372 <<"] arg[" << cmd.argument << "]" << endl;
374 BufferView * bv = cmd.view();
376 switch (cmd.action) {
378 case LFUN_APPENDIX: {
379 Paragraph * par = cursor.par();
380 bool start = !par->params().startOfAppendix();
382 // ensure that we have only one start_of_appendix in this document
383 Paragraph * tmp = ownerParagraph();
384 for (; tmp; tmp = tmp->next())
385 tmp->params().startOfAppendix(false);
387 par->params().startOfAppendix(start);
389 // we can set the refreshing parameters now
390 status(cmd.view(), LyXText::NEED_MORE_REFRESH);
392 refresh_row = 0; // not needed for full update
393 updateCounters(cmd.view());
394 setCursor(cmd.view(), cursor.par(), cursor.pos());
399 case LFUN_DELETE_WORD_FORWARD:
400 bv->beforeChange(this);
402 deleteWordForward(bv);
407 case LFUN_DELETE_WORD_BACKWARD:
408 bv->beforeChange(this);
410 deleteWordBackward(bv);
415 case LFUN_DELETE_LINE_FORWARD:
416 bv->beforeChange(this);
418 deleteLineForward(bv);
425 if (!selection.mark())
426 bv->beforeChange(this);
433 if (!selection.mark())
434 bv->beforeChange(this);
436 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
437 cursorLeftOneWord(bv);
439 cursorRightOneWord(bv);
444 if (!selection.mark())
445 bv->beforeChange(this);
447 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
448 cursorRightOneWord(bv);
450 cursorLeftOneWord(bv);
454 case LFUN_BEGINNINGBUF:
455 if (!selection.mark())
456 bv->beforeChange(this);
463 if (selection.mark())
464 bv->beforeChange(this);
472 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
476 finishChange(bv, true);
481 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
485 finishChange(bv, true);
491 finishChange(bv, true);
496 cursorDown(bv, true);
497 finishChange(bv, true);
500 case LFUN_UP_PARAGRAPHSEL:
502 cursorUpParagraph(bv);
503 finishChange(bv, true);
506 case LFUN_DOWN_PARAGRAPHSEL:
508 cursorDownParagraph(bv);
509 finishChange(bv, true);
515 finishChange(bv, true);
521 finishChange(bv, true);
527 finishChange(bv, true);
533 finishChange(bv, true);
536 case LFUN_WORDRIGHTSEL:
538 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
539 cursorLeftOneWord(bv);
541 cursorRightOneWord(bv);
542 finishChange(bv, true);
545 case LFUN_WORDLEFTSEL:
547 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
548 cursorRightOneWord(bv);
550 cursorLeftOneWord(bv);
551 finishChange(bv, true);
555 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
556 if (!selection.mark())
557 bv->beforeChange(this);
560 cursorLeft(bv, false);
561 if (cursor.pos() < cursor.par()->size()
562 && cursor.par()->isInset(cursor.pos())
563 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
564 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
565 cmd.message(tmpinset->editMessage());
566 tmpinset->edit(bv, !is_rtl);
570 cursorRight(bv, false);
576 // This is soooo ugly. Isn`t it possible to make
578 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
579 if (!selection.mark())
580 bv->beforeChange(this);
582 LyXCursor const cur = cursor;
584 cursorLeft(bv, false);
585 if ((is_rtl || cur != cursor) && // only if really moved!
586 cursor.pos() < cursor.par()->size() &&
587 cursor.par()->isInset(cursor.pos()) &&
588 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
589 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
590 cmd.message(tmpinset->editMessage());
591 tmpinset->edit(bv, is_rtl);
595 cursorRight(bv, false);
601 if (!selection.mark())
602 bv->beforeChange(this);
603 bv->update(this, BufferView::UPDATE);
609 if (!selection.mark())
610 bv->beforeChange(this);
611 bv->update(this, BufferView::UPDATE);
616 case LFUN_UP_PARAGRAPH:
617 if (!selection.mark())
618 bv->beforeChange(this);
619 bv->update(this, BufferView::UPDATE);
620 cursorUpParagraph(bv);
624 case LFUN_DOWN_PARAGRAPH:
625 if (!selection.mark())
626 bv->beforeChange(this);
627 bv->update(this, BufferView::UPDATE);
628 cursorDownParagraph(bv);
629 finishChange(bv, false);
633 if (!selection.mark())
634 bv->beforeChange(this);
635 bv->update(this, BufferView::UPDATE);
637 finishChange(bv, false);
640 // moveCursorUpdate(bv, false, false);
641 // owner_->view_state_changed();
645 if (!selection.mark())
646 bv->beforeChange(this);
647 bv->update(this, BufferView::UPDATE);
649 finishChange(bv, false);
653 if (!selection.mark())
654 bv->beforeChange(this);
657 finishChange(bv, false);
661 if (!selection.mark())
662 bv->beforeChange(this);
665 finishChange(bv, false);
669 bv->beforeChange(this);
670 insertChar(bv, Paragraph::META_NEWLINE);
672 setCursor(bv, cursor.par(), cursor.pos());
673 moveCursorUpdate(bv, false);
677 if (!selection.set()) {
679 selection.cursor = cursor;
681 // It is possible to make it a lot faster still
682 // just comment out the line below...
686 cutSelection(bv, true);
689 moveCursorUpdate(bv, false);
690 bv->owner()->view_state_changed();
694 case LFUN_DELETE_SKIP:
695 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
696 if (!selection.set()) {
697 LyXCursor cur = cursor;
698 if (cur.pos() == cur.par()->size()) {
702 && !(cur.par()->params().spaceTop()
703 == VSpace (VSpace::NONE))) {
705 cur.par()->params().lineTop(),
706 cur.par()->params().lineBottom(),
707 cur.par()->params().pagebreakTop(),
708 cur.par()->params().pagebreakBottom(),
709 VSpace(VSpace::NONE),
710 cur.par()->params().spaceBottom(),
711 cur.par()->params().spacing(),
712 cur.par()->params().align(),
713 cur.par()->params().labelWidthString(), 0);
719 selection.cursor = cursor;
723 selection.cursor = cursor;
727 cutSelection(bv, true);
734 if (!selection.set()) {
735 if (bv->owner()->getIntl().getTransManager().backspace()) {
737 selection.cursor = cursor;
739 // It is possible to make it a lot faster still
740 // just comment out the line below...
745 cutSelection(bv, true);
748 bv->owner()->view_state_changed();
752 case LFUN_BACKSPACE_SKIP:
753 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
754 if (!selection.set()) {
755 LyXCursor cur = cursor;
757 && !(cur.par()->params().spaceTop()
758 == VSpace (VSpace::NONE))) {
760 cur.par()->params().lineTop(),
761 cur.par()->params().lineBottom(),
762 cur.par()->params().pagebreakTop(),
763 cur.par()->params().pagebreakBottom(),
764 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
765 cur.par()->params().spacing(),
766 cur.par()->params().align(),
767 cur.par()->params().labelWidthString(), 0);
770 selection.cursor = cur;
774 cutSelection(bv, true);
779 case LFUN_BREAKPARAGRAPH:
780 bv->beforeChange(this);
781 breakParagraph(bv, 0);
783 selection.cursor = cursor;
785 bv->owner()->view_state_changed();
788 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
789 bv->beforeChange(this);
790 breakParagraph(bv, 1);
792 selection.cursor = cursor;
794 bv->owner()->view_state_changed();
797 case LFUN_BREAKPARAGRAPH_SKIP: {
798 // When at the beginning of a paragraph, remove
799 // indentation and add a "defskip" at the top.
800 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
801 LyXCursor cur = cursor;
802 bv->beforeChange(this);
803 if (cur.pos() == 0) {
804 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
806 cur.par()->params().lineTop(),
807 cur.par()->params().lineBottom(),
808 cur.par()->params().pagebreakTop(),
809 cur.par()->params().pagebreakBottom(),
810 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
811 cur.par()->params().spacing(),
812 cur.par()->params().align(),
813 cur.par()->params().labelWidthString(), 1);
818 breakParagraph(bv, 0);
822 selection.cursor = cur;
824 bv->owner()->view_state_changed();
828 case LFUN_PARAGRAPH_SPACING: {
829 Paragraph * par = cursor.par();
830 Spacing::Space cur_spacing = par->params().spacing().getSpace();
831 float cur_value = 1.0;
832 if (cur_spacing == Spacing::Other)
833 cur_value = par->params().spacing().getValue();
835 istringstream is(cmd.argument.c_str());
838 Spacing::Space new_spacing = cur_spacing;
839 float new_value = cur_value;
841 lyxerr << "Missing argument to `paragraph-spacing'"
843 } else if (tmp == "single") {
844 new_spacing = Spacing::Single;
845 } else if (tmp == "onehalf") {
846 new_spacing = Spacing::Onehalf;
847 } else if (tmp == "double") {
848 new_spacing = Spacing::Double;
849 } else if (tmp == "other") {
850 new_spacing = Spacing::Other;
853 lyxerr << "new_value = " << tmpval << endl;
856 } else if (tmp == "default") {
857 new_spacing = Spacing::Default;
859 lyxerr << _("Unknown spacing argument: ")
860 << cmd.argument << endl;
862 if (cur_spacing != new_spacing || cur_value != new_value) {
863 par->params().spacing(Spacing(new_spacing, new_value));
870 case LFUN_INSET_TOGGLE:
872 bv->beforeChange(this);
879 case LFUN_PROTECTEDSPACE:
880 if (cursor.par()->layout()->free_spacing) {
884 specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
886 moveCursorUpdate(bv, false);
889 case LFUN_HYPHENATION:
890 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
893 case LFUN_LIGATURE_BREAK:
894 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
898 specialChar(this, bv, InsetSpecialChar::LDOTS);
904 insertChar(bv, Paragraph::META_HFILL);
908 case LFUN_END_OF_SENTENCE:
909 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
912 case LFUN_MENU_SEPARATOR:
913 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
917 bv->beforeChange(this);
919 selection.cursor = cursor;
920 cmd.message(N_("Mark off"));
924 bv->beforeChange(this);
925 selection.mark(true);
927 selection.cursor = cursor;
928 cmd.message(N_("Mark on"));
932 bv->beforeChange(this);
933 if (selection.mark()) {
935 cmd.message(N_("Mark removed"));
937 selection.mark(true);
939 cmd.message(N_("Mark set"));
941 selection.cursor = cursor;
944 case LFUN_UPCASE_WORD:
946 changeCase(bv, LyXText::text_uppercase);
948 bv->updateInset(inset_owner, true);
952 case LFUN_LOWCASE_WORD:
954 changeCase(bv, LyXText::text_lowercase);
956 bv->updateInset(inset_owner, true);
960 case LFUN_CAPITALIZE_WORD:
962 changeCase(bv, LyXText::text_capitalization);
964 bv->updateInset(inset_owner, true);
968 case LFUN_TRANSPOSE_CHARS:
972 bv->updateInset(inset_owner, true);
977 cmd.message(_("Paste"));
979 // clear the selection
980 bv->toggleSelection();
984 clearSelection(); // bug 393
993 cutSelection(bv, true);
995 cmd.message(_("Cut"));
1000 cmd.message(_("Copy"));
1003 case LFUN_BEGINNINGBUFSEL:
1005 return Inset::UNDISPATCHED;
1008 finishChange(bv, true);
1011 case LFUN_ENDBUFSEL:
1013 return Inset::UNDISPATCHED;
1016 finishChange(bv, true);
1020 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1026 istringstream is(cmd.argument.c_str());
1029 lyxerr << "SETXY: Could not parse coordinates in '"
1030 << cmd.argument << std::endl;
1032 setCursorFromCoordinates(bv, x, y);
1037 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1039 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1045 case LFUN_GETLAYOUT:
1046 cmd.message(tostr(cursor.par()->layout()));
1050 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1051 << cmd.argument << endl;
1053 // This is not the good solution to the empty argument
1054 // problem, but it will hopefully suffice for 1.2.0.
1055 // The correct solution would be to augument the
1056 // function list/array with information about what
1057 // functions needs arguments and their type.
1058 if (cmd.argument.empty()) {
1059 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1063 // Derive layout number from given argument (string)
1064 // and current buffer's textclass (number)
1065 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1066 bool hasLayout = tclass.hasLayout(cmd.argument);
1067 string layout = cmd.argument;
1069 // If the entry is obsolete, use the new one instead.
1071 string const & obs = tclass[layout]->obsoleted_by();
1077 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1082 bool change_layout = (current_layout != layout);
1083 if (!change_layout && selection.set() &&
1084 selection.start.par() != selection.end.par())
1086 Paragraph * spar = selection.start.par();
1087 Paragraph * epar = selection.end.par()->next();
1088 while (spar != epar) {
1089 if (spar->layout()->name() != current_layout) {
1090 change_layout = true;
1095 if (change_layout) {
1097 current_layout = layout;
1099 setLayout(bv, layout);
1100 bv->owner()->setLayout(layout);
1107 case LFUN_PASTESELECTION: {
1111 // this was originally a beforeChange(bv->text), i.e
1112 // the outermost LyXText!
1113 bv->beforeChange(this);
1114 string const clip = bv->workarea().getClipboard();
1115 if (!clip.empty()) {
1116 if (cmd.argument == "paragraph")
1117 insertStringAsParagraphs(bv, clip);
1119 insertStringAsLines(bv, clip);
1126 case LFUN_GOTOERROR:
1127 gotoInset(bv, Inset::ERROR_CODE, false);
1131 gotoInset(bv, Inset::NOTE_CODE, false);
1134 case LFUN_REFERENCE_GOTO:
1136 vector<Inset::Code> tmp;
1137 tmp.push_back(Inset::LABEL_CODE);
1138 tmp.push_back(Inset::REF_CODE);
1139 gotoInset(bv, tmp, true);
1144 case LFUN_INSET_LIST:
1145 case LFUN_INSET_THEOREM:
1147 case LFUN_INSERT_NOTE:
1148 case LFUN_INSET_ERT:
1149 case LFUN_INSET_EXTERNAL:
1150 case LFUN_INSET_FLOAT:
1151 case LFUN_INSET_FOOTNOTE:
1152 case LFUN_INSET_MARGINAL:
1153 case LFUN_INSET_MINIPAGE:
1154 case LFUN_INSET_OPTARG:
1155 case LFUN_INSET_WIDE_FLOAT:
1156 case LFUN_TABULAR_INSERT:
1158 Inset * inset = createInset(cmd);
1160 bool gotsel = false;
1161 if (selection.set()) {
1162 cutSelection(bv, true, false);
1165 if (bv->insertInset(inset)) {
1168 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
1178 Paragraph const * par = cursor.par();
1179 lyx::pos_type pos = cursor.pos();
1183 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1186 c = par->getChar(pos - 1);
1189 LyXLayout_ptr const & style = par->layout();
1191 if (style->pass_thru ||
1192 par->getFontSettings(bv->buffer()->params,
1193 pos).language()->lang() == "hebrew" ||
1194 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1195 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1199 case LFUN_DATE_INSERT: {
1200 time_t now_time_t = time(NULL);
1201 struct tm * now_tm = localtime(&now_time_t);
1202 setlocale(LC_TIME, "");
1204 if (!cmd.argument.empty())
1207 arg = lyxrc.date_insert_format;
1209 int const datetmp_len =
1210 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1212 for (int i = 0; i < datetmp_len; i++) {
1213 insertChar(bv, datetmp[i]);
1216 selection.cursor = cursor;
1217 moveCursorUpdate(bv, false);
1221 case LFUN_MOUSE_TRIPLE:
1224 if (bv_owner && bv->theLockingInset())
1226 if (cmd.button() == mouse_button::button1) {
1228 bv->screen().hideCursor();
1229 bv->screen().toggleSelection(this, bv);
1232 selection.cursor = cursor;
1236 bv->screen().toggleSelection(this, bv, false);
1238 bv->workarea().haveSelection(selection.set());
1242 case LFUN_MOUSE_DOUBLE:
1245 if (bv_owner && bv->theLockingInset())
1247 if (cmd.button() == mouse_button::button1) {
1249 bv->screen().hideCursor();
1250 bv->screen().toggleSelection(this, bv);
1251 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1252 bv->screen().toggleSelection(this, bv, false);
1254 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1257 bv->workarea().haveSelection(selection.set());
1261 case LFUN_MOUSE_MOTION:
1263 // Only use motion with button 1
1264 //if (ev.button() != mouse_button::button1)
1270 // Check for inset locking
1271 if (bv->theLockingInset()) {
1272 Inset * tli = bv->theLockingInset();
1273 LyXCursor cursor = bv->text->cursor;
1274 LyXFont font = bv->text->getFont(bv->buffer(),
1275 cursor.par(), cursor.pos());
1276 int width = tli->width(bv, font);
1277 int inset_x = font.isVisibleRightToLeft()
1278 ? cursor.ix() - width : cursor.ix();
1279 int start_x = inset_x + tli->scroll();
1280 FuncRequest cmd1 = cmd;
1281 cmd1.x = cmd.x - start_x;
1282 cmd1.y = cmd.y - cursor.iy() + bv->text->first_y;
1283 tli->localDispatch(cmd1);
1287 // The test for not selection possible is needed, that only motion
1288 // events are used, where the bottom press event was on
1289 // the drawing area too
1290 if (!selection_possible) {
1291 lyxerr[Debug::ACTION]
1292 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1296 bv->screen().hideCursor();
1298 Row * cursorrow = bv->text->cursor.row();
1299 bv->text->setCursorFromCoordinates(bv, cmd.x, cmd.y + bv->text->first_y);
1301 // sorry for this but I have a strange error that the y value jumps at
1302 // a certain point. This seems like an error in my xforms library or
1303 // in some other local environment, but I would like to leave this here
1304 // for the moment until I can remove this (Jug 20020418)
1305 if (y_before < bv->text->cursor.y())
1306 lyxerr << y_before << ":" << bv->text->cursor.y() << endl;
1308 // This is to allow jumping over large insets
1309 if (cursorrow == bv->text->cursor.row()) {
1310 if (cmd.y >= int(bv->workarea().workHeight()))
1311 bv->text->cursorDown(bv, false);
1313 bv->text->cursorUp(bv, false);
1316 // Maybe an empty line was deleted
1317 if (!bv->text->selection.set())
1318 bv->update(bv->text, BufferView::UPDATE);
1319 bv->text->setSelection(bv);
1320 bv->screen().toggleToggle(bv->text, bv);
1326 // Single-click on work area
1327 case LFUN_MOUSE_PRESS:
1332 // ok ok, this is a hack (for xforms)
1333 // We shouldn't go further down as we really should only do the
1334 // scrolling and be done with this. Otherwise we may open some
1335 // dialogs (Jug 20020424).
1336 if (cmd.button() == mouse_button::button4) {
1337 bv->scroll(-lyxrc.wheel_jump);
1340 if (cmd.button() == mouse_button::button5) {
1341 bv->scroll(lyxrc.wheel_jump);
1347 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1349 // Middle button press pastes if we have a selection
1350 // We do this here as if the selection was inside an inset
1351 // it could get cleared on the unlocking of the inset so
1352 // we have to check this first
1353 bool paste_internally = false;
1354 if (cmd.button() == mouse_button::button2 && selection.set()) {
1355 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1356 paste_internally = true;
1359 int const screen_first = bv->text->first_y;
1361 if (bv->theLockingInset()) {
1362 // We are in inset locking mode
1364 // Check whether the inset was hit. If not reset mode,
1365 // otherwise give the event to the inset
1366 if (inset_hit == bv->theLockingInset()) {
1367 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1368 bv->theLockingInset()->localDispatch(cmd1);
1371 bv->unlockInset(bv->theLockingInset());
1375 selection_possible = true;
1376 bv->screen().hideCursor();
1378 // Clear the selection
1379 bv->screen().toggleSelection(bv->text, bv);
1380 bv->text->clearSelection();
1381 bv->text->fullRebreak(bv);
1383 bv->updateScrollbar();
1385 // Single left click in math inset?
1386 if (isHighlyEditableInset(inset_hit)) {
1387 // Highly editable inset, like math
1388 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1389 selection_possible = false;
1390 bv->owner()->updateLayoutChoice();
1391 bv->owner()->message(inset->editMessage());
1392 //inset->edit(bv, x, y, cmd.button());
1393 // We just have to lock the inset before calling a PressEvent on it!
1394 // we don't need the edit() call here! (Jug20020329)
1395 if (!bv->lockInset(inset))
1396 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1397 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1398 inset->localDispatch(cmd1);
1401 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1403 // Right click on a footnote flag opens float menu
1404 if (cmd.button() == mouse_button::button3) {
1405 selection_possible = false;
1409 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1410 bv->text->setCursorFromCoordinates(bv, x, y + screen_first);
1412 bv->text->selection.cursor = bv->text->cursor;
1413 bv->text->cursor.x_fix(bv->text->cursor.x());
1415 bv->owner()->updateLayoutChoice();
1416 if (bv->fitCursor())
1417 selection_possible = false;
1419 // Insert primary selection with middle mouse
1420 // if there is a local selection in the current buffer,
1422 if (cmd.button() == mouse_button::button2) {
1423 if (paste_internally)
1424 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1426 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1427 selection_possible = false;
1432 case LFUN_MOUSE_RELEASE:
1434 // do nothing if we used the mouse wheel
1438 if (cmd.button() == mouse_button::button4
1439 || cmd.button() == mouse_button::button5)
1442 // If we hit an inset, we have the inset coordinates in these
1443 // and inset_hit points to the inset. If we do not hit an
1444 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1447 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1449 if (bv->theLockingInset()) {
1450 // We are in inset locking mode.
1452 // LyX does a kind of work-area grabbing for insets.
1453 // Only a ButtonPress FuncRequest outside the inset will
1454 // force a insetUnlock.
1455 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1456 bv->theLockingInset()->localDispatch(cmd1);
1460 selection_possible = false;
1462 if (cmd.button() == mouse_button::button2)
1466 if (cmd.button() == mouse_button::button1)
1467 bv->workarea().haveSelection(selection.set());
1470 bv->owner()->view_state_changed();
1471 bv->owner()->updateMenubar();
1472 bv->owner()->updateToolbar();
1474 // Did we hit an editable inset?
1476 selection_possible = false;
1478 // if we reach this point with a selection, it
1479 // must mean we are currently selecting.
1480 // But we don't want to open the inset
1481 // because that is annoying for the user.
1482 // So just pretend we didn't hit it.
1483 // this is OK because a "kosher" ButtonRelease
1484 // will follow a ButtonPress that clears
1486 // Note this also fixes selection drawing
1487 // problems if we end up opening an inset
1488 if (selection.set())
1491 // CHECK fix this proper in 0.13
1492 // well, maybe 13.0 !!!!!!!!!
1494 // Following a ref shouldn't issue
1495 // a push on the undo-stack
1496 // anylonger, now that we have
1497 // keybindings for following
1498 // references and returning from
1499 // references. IMHO though, it
1500 // should be the inset's own business
1501 // to push or not push on the undo
1502 // stack. They don't *have* to
1503 // alter the document...
1505 // ...or maybe the SetCursorParUndo()
1506 // below isn't necessary at all anylonger?
1507 if (inset_hit->lyxCode() == Inset::REF_CODE)
1508 setCursorParUndo(bv);
1510 bv->owner()->message(inset_hit->editMessage());
1512 if (isHighlyEditableInset(inset_hit)) {
1513 // Highly editable inset, like math
1514 UpdatableInset * inset = (UpdatableInset *) inset_hit;
1515 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1516 inset->localDispatch(cmd1);
1518 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1519 inset_hit->localDispatch(cmd1);
1520 // IMO this is a grosshack! Inset's should be changed so that
1521 // they call the actions they have to do with the insetButtonRel.
1522 // function and not in the edit(). This should be changed
1524 #ifdef WITH_WARNINGS
1525 #warning Please remove donot call inset->edit() here (Jug 20020812)
1527 inset_hit->edit(bv, x, y, cmd.button());
1532 // Maybe we want to edit a bibitem ale970302
1533 if (bv->text->cursor.par()->bibkey) {
1535 bv->text->cursor.par()->isRightToLeftPar(bv->buffer()->params);
1537 bibitemMaxWidth(bv, bv->buffer()->params.getLyXTextClass().defaultfont());
1538 if ((is_rtl && x > bv->text->workWidth(bv)-20-width) ||
1539 (!is_rtl && x < 20 + width)) {
1540 bv->text->cursor.par()->bibkey->edit(bv, 0, 0, mouse_button::none);
1546 case LFUN_SELFINSERT: {
1547 if (cmd.argument.empty())
1550 // Automatically delete the currently selected
1551 // text and replace it with what is being
1552 // typed in now. Depends on lyxrc settings
1553 // "auto_region_delete", which defaults to
1556 if (lyxrc.auto_region_delete) {
1557 if (selection.set()) {
1558 cutSelection(bv, false, false);
1561 bv->workarea().haveSelection(false);
1564 bv->beforeChange(this);
1565 LyXFont const old_font(real_current_font);
1567 string::const_iterator cit = cmd.argument.begin();
1568 string::const_iterator end = cmd.argument.end();
1569 for (; cit != end; ++cit)
1570 bv->owner()->getIntl().getTransManager().
1571 TranslateAndInsert(*cit, this);
1574 selection.cursor = cursor;
1575 moveCursorUpdate(bv, false);
1577 // real_current_font.number can change so we need to
1578 // update the minibuffer
1579 if (old_font != real_current_font)
1580 bv->owner()->view_state_changed();
1585 return Inset::UNDISPATCHED;
1588 return Inset::DISPATCHED;