1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2002 The LyX Team.
9 * ====================================================== */
15 #include "paragraph.h"
16 #include "BufferView.h"
17 #include "funcrequest.h"
20 #include "bufferparams.h"
22 #include "ParagraphParameters.h"
28 #include "support/lstrings.h"
29 #include "frontends/LyXView.h"
30 #include "frontends/screen.h"
31 #include "frontends/Dialogs.h"
32 #include "insets/insetspecialchar.h"
33 #include "insets/insettext.h"
34 #include "insets/insetbib.h"
35 #include "insets/insetquotes.h"
36 #include "insets/insetcommand.h"
37 #include "undo_funcs.h"
46 extern string current_layout;
47 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
49 // the selection possible is needed, that only motion events are
50 // used, where the bottom press event was on the drawing area too
51 bool selection_possible = false;
56 void moveCursorUpdate(BufferView * bv, bool selecting)
58 LyXText * lt = bv->getLyXText();
60 if (selecting || lt->selection.mark()) {
65 bv->updateInset(lt->inset_owner, false);
69 // bv->update(lt, BufferView::SELECT|BufferView::FITCUR);
71 bv->update(lt, BufferView::SELECT|BufferView::FITCUR);
73 } else if (bv->text->status() != LyXText::UNCHANGED) {
74 bv->theLockingInset()->hideInsetCursor(bv);
75 bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
79 if (!lt->selection.set())
80 bv->haveSelection(false);
86 void finishChange(BufferView * bv, bool selecting = false)
89 moveCursorUpdate(bv, selecting);
90 bv->owner()->view_state_changed();
93 // check if the given co-ordinates are inside an inset at the
94 // given cursor, if one exists. If so, the inset is returned,
95 // and the co-ordinates are made relative. Otherwise, 0 is returned.
96 Inset * checkInset(BufferView * bv, LyXText const & text,
97 LyXCursor const & cur, int & x, int & y)
99 lyx::pos_type const pos = cur.pos();
100 Paragraph /*const*/ & par = *cur.par();
102 if (pos >= par.size() || !par.isInset(pos))
105 Inset /*const*/ * inset = par.getInset(pos);
107 if (!isEditableInset(inset))
110 // get inset dimensions
111 lyx::Assert(par.getInset(pos));
113 LyXFont const & font = text.getFont(bv->buffer(), &par, pos);
115 int const width = inset->width(bv, font);
116 int const inset_x = font.isVisibleRightToLeft()
117 ? (cur.ix() - width) : cur.ix();
120 inset_x + inset->scroll(),
122 cur.iy() - inset->ascent(bv, font),
123 cur.iy() + inset->descent(bv, font)
126 if (!b.contained(x, y)) {
127 lyxerr[Debug::GUI] << "Missed inset at x,y "
129 << " box " << b << endl;
133 text.setCursor(bv, &par, pos, true);
136 // The origin of an inset is on the baseline
137 y -= text.cursor.iy();
145 Inset * LyXText::checkInsetHit(BufferView * bv, int & x, int & y) const
147 int y_tmp = y + first_y;
150 setCursorFromCoordinates(bv, cur, x, y_tmp);
152 Inset * inset = checkInset(bv, *this, cur, x, y_tmp);
158 // look at previous position
163 setCursor(bv, cur, cur.par(), cur.pos() - 1, true);
165 inset = checkInset(bv, *this, cur, x, y_tmp);
172 bool LyXText::gotoNextInset(BufferView * bv,
173 vector<Inset::Code> const & codes, string const & contents) const
175 LyXCursor res = cursor;
178 if (res.pos() < res.par()->size() - 1) {
179 res.pos(res.pos() + 1);
181 res.par(res.par()->next());
185 } while (res.par() &&
186 !(res.par()->isInset(res.pos())
187 && (inset = res.par()->getInset(res.pos())) != 0
188 && find(codes.begin(), codes.end(), inset->lyxCode())
190 && (contents.empty() ||
191 static_cast<InsetCommand *>(
192 res.par()->getInset(res.pos()))->getContents()
196 setCursor(bv, res.par(), res.pos(), false);
203 void LyXText::gotoInset(BufferView * bv, vector<Inset::Code> const & codes,
207 bv->beforeChange(this);
211 if (same_content && cursor.par()->isInset(cursor.pos())) {
212 Inset const * inset = cursor.par()->getInset(cursor.pos());
213 if (find(codes.begin(), codes.end(), inset->lyxCode())
215 contents = static_cast<InsetCommand const *>(inset)->getContents();
218 if (!gotoNextInset(bv, codes, contents)) {
219 if (cursor.pos() || cursor.par() != ownerParagraph()) {
220 LyXCursor tmp = cursor;
221 cursor.par(ownerParagraph());
223 if (!gotoNextInset(bv, codes, contents)) {
225 bv->owner()->message(_("No more insets"));
228 bv->owner()->message(_("No more insets"));
232 selection.cursor = cursor;
236 void LyXText::gotoInset(BufferView * bv, Inset::Code code, bool same_content)
238 gotoInset(bv, vector<Inset::Code>(1, code), same_content);
242 void LyXText::cursorPrevious(BufferView * bv)
244 if (!cursor.row()->previous()) {
246 int new_y = bv->text->first_y - bv->workHeight();
247 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
248 bv->updateScrollbar();
254 Row * cursorrow = cursor.row();
256 setCursorFromCoordinates(bv, cursor.x_fix(), y);
260 if (cursorrow == bv->text->cursor.row()) {
261 // we have a row which is taller than the workarea. The
262 // simplest solution is to move to the previous row instead.
265 // This is what we used to do, so we wouldn't skip right past
266 // tall rows, but it's not working right now.
268 new_y = bv->text->first_y - bv->workHeight();
272 new_y = bv->text->cursor.iy()
273 + bv->theLockingInset()->insetInInsetY() + y
274 + cursor.row()->height()
275 - bv->workHeight() + 1;
278 - cursor.row()->baseline()
279 + cursor.row()->height()
280 - bv->workHeight() + 1;
283 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
284 if (cursor.row()->previous()) {
286 setCursor(bv, cur, cursor.row()->previous()->par(),
287 cursor.row()->previous()->pos(), false);
288 if (cur.y() > first_y) {
292 bv->updateScrollbar();
296 void LyXText::cursorNext(BufferView * bv)
298 if (!cursor.row()->next()) {
299 int y = cursor.y() - cursor.row()->baseline() +
300 cursor.row()->height();
301 if (y > int(first_y + bv->workHeight())) {
302 bv->screen().draw(bv->text, bv,
303 bv->text->first_y + bv->workHeight());
304 bv->updateScrollbar();
309 int y = first_y + bv->workHeight();
310 if (inset_owner && !first_y) {
311 y -= (bv->text->cursor.iy()
313 + bv->theLockingInset()->insetInInsetY());
318 Row * cursorrow = cursor.row();
319 setCursorFromCoordinates(bv, cursor.x_fix(), y);
320 // + bv->workHeight());
324 if (cursorrow == bv->text->cursor.row()) {
325 // we have a row which is taller than the workarea. The
326 // simplest solution is to move to the next row instead.
327 cursorDown(bv, true);
329 // This is what we used to do, so we wouldn't skip right past
330 // tall rows, but it's not working right now.
332 new_y = bv->text->first_y + bv->workHeight();
336 new_y = bv->text->cursor.iy()
337 + bv->theLockingInset()->insetInInsetY()
338 + y - cursor.row()->baseline();
340 new_y = cursor.y() - cursor.row()->baseline();
343 bv->screen().draw(bv->text, bv, new_y);
344 if (cursor.row()->next()) {
346 setCursor(bv, cur, cursor.row()->next()->par(),
347 cursor.row()->next()->pos(), false);
348 if (cur.y() < int(first_y + bv->workHeight())) {
349 cursorDown(bv, true);
352 bv->updateScrollbar();
356 void LyXText::update(BufferView * bv, bool changed)
358 BufferView::UpdateCodes c = BufferView::SELECT | BufferView::FITCUR;
360 bv->update(this, c | BufferView::CHANGE);
367 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
371 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
372 if (!bv->insertInset(new_inset))
375 bv->updateInset(new_inset, true);
378 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
379 bool edit, bool pastesel)
381 Inset * inset = createInset(cmd);
382 BufferView * bv = cmd.view();
386 if (lt->selection.set()) {
387 lt->cutSelection(bv, true, false);
390 if (bv->insertInset(inset)) {
393 if (gotsel && pastesel)
394 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
404 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
406 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
407 <<"] arg[" << cmd.argument << ']' << endl;
409 BufferView * bv = cmd.view();
411 switch (cmd.action) {
413 case LFUN_APPENDIX: {
414 Paragraph * par = cursor.par();
415 bool start = !par->params().startOfAppendix();
417 // ensure that we have only one start_of_appendix in this document
418 Paragraph * tmp = ownerParagraph();
419 for (; tmp; tmp = tmp->next()) {
420 if (tmp->params().startOfAppendix()) {
421 setUndo(bv, Undo::EDIT, tmp, tmp->next());
422 tmp->params().startOfAppendix(false);
427 setUndo(bv, Undo::EDIT, par, par->next());
428 par->params().startOfAppendix(start);
430 // we can set the refreshing parameters now
431 status(cmd.view(), LyXText::NEED_MORE_REFRESH);
433 refresh_row = 0; // not needed for full update
434 updateCounters(cmd.view());
435 setCursor(cmd.view(), cursor.par(), cursor.pos());
440 case LFUN_DELETE_WORD_FORWARD:
441 bv->beforeChange(this);
443 deleteWordForward(bv);
448 case LFUN_DELETE_WORD_BACKWARD:
449 bv->beforeChange(this);
451 deleteWordBackward(bv);
456 case LFUN_DELETE_LINE_FORWARD:
457 bv->beforeChange(this);
459 deleteLineForward(bv);
466 if (!selection.mark())
467 bv->beforeChange(this);
474 if (!selection.mark())
475 bv->beforeChange(this);
477 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
478 cursorLeftOneWord(bv);
480 cursorRightOneWord(bv);
485 if (!selection.mark())
486 bv->beforeChange(this);
488 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
489 cursorRightOneWord(bv);
491 cursorLeftOneWord(bv);
495 case LFUN_BEGINNINGBUF:
496 if (!selection.mark())
497 bv->beforeChange(this);
504 if (selection.mark())
505 bv->beforeChange(this);
513 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
517 finishChange(bv, true);
522 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
526 finishChange(bv, true);
532 finishChange(bv, true);
537 cursorDown(bv, true);
538 finishChange(bv, true);
541 case LFUN_UP_PARAGRAPHSEL:
543 cursorUpParagraph(bv);
544 finishChange(bv, true);
547 case LFUN_DOWN_PARAGRAPHSEL:
549 cursorDownParagraph(bv);
550 finishChange(bv, true);
556 finishChange(bv, true);
562 finishChange(bv, true);
568 finishChange(bv, true);
574 finishChange(bv, true);
577 case LFUN_WORDRIGHTSEL:
579 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
580 cursorLeftOneWord(bv);
582 cursorRightOneWord(bv);
583 finishChange(bv, true);
586 case LFUN_WORDLEFTSEL:
588 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
589 cursorRightOneWord(bv);
591 cursorLeftOneWord(bv);
592 finishChange(bv, true);
599 getWord(cur1, cur2, WHOLE_WORD);
600 setCursor(bv, cur1.par(), cur1.pos());
601 bv->beforeChange(this);
602 setCursor(bv, cur2.par(), cur2.pos());
603 finishChange(bv, true);
608 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
609 if (!selection.mark())
610 bv->beforeChange(this);
613 cursorLeft(bv, false);
614 if (cursor.pos() < cursor.par()->size()
615 && cursor.par()->isInset(cursor.pos())
616 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
617 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
618 cmd.message(tmpinset->editMessage());
619 tmpinset->edit(bv, !is_rtl);
623 cursorRight(bv, false);
629 // This is soooo ugly. Isn`t it possible to make
631 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
632 if (!selection.mark())
633 bv->beforeChange(this);
635 LyXCursor const cur = cursor;
637 cursorLeft(bv, false);
638 if ((is_rtl || cur != cursor) && // only if really moved!
639 cursor.pos() < cursor.par()->size() &&
640 cursor.par()->isInset(cursor.pos()) &&
641 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
642 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
643 cmd.message(tmpinset->editMessage());
644 tmpinset->edit(bv, is_rtl);
648 cursorRight(bv, false);
654 if (!selection.mark())
655 bv->beforeChange(this);
656 bv->update(this, BufferView::UPDATE);
662 if (!selection.mark())
663 bv->beforeChange(this);
664 bv->update(this, BufferView::UPDATE);
669 case LFUN_UP_PARAGRAPH:
670 if (!selection.mark())
671 bv->beforeChange(this);
672 bv->update(this, BufferView::UPDATE);
673 cursorUpParagraph(bv);
677 case LFUN_DOWN_PARAGRAPH:
678 if (!selection.mark())
679 bv->beforeChange(this);
680 bv->update(this, BufferView::UPDATE);
681 cursorDownParagraph(bv);
682 finishChange(bv, false);
686 if (!selection.mark())
687 bv->beforeChange(this);
688 bv->update(this, BufferView::UPDATE);
690 finishChange(bv, false);
693 // moveCursorUpdate(bv, false, false);
694 // owner_->view_state_changed();
698 if (!selection.mark())
699 bv->beforeChange(this);
700 bv->update(this, BufferView::UPDATE);
702 finishChange(bv, false);
706 if (!selection.mark())
707 bv->beforeChange(this);
710 finishChange(bv, false);
714 if (!selection.mark())
715 bv->beforeChange(this);
718 finishChange(bv, false);
722 bv->beforeChange(this);
723 insertChar(bv, Paragraph::META_NEWLINE);
725 setCursor(bv, cursor.par(), cursor.pos());
726 moveCursorUpdate(bv, false);
730 if (!selection.set()) {
732 selection.cursor = cursor;
734 // It is possible to make it a lot faster still
735 // just comment out the line below...
739 cutSelection(bv, true);
742 moveCursorUpdate(bv, false);
743 bv->owner()->view_state_changed();
747 case LFUN_DELETE_SKIP:
748 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
749 if (!selection.set()) {
750 LyXCursor cur = cursor;
751 if (cur.pos() == cur.par()->size()) {
755 && !(cur.par()->params().spaceTop()
756 == VSpace (VSpace::NONE))) {
758 cur.par()->params().lineTop(),
759 cur.par()->params().lineBottom(),
760 cur.par()->params().pagebreakTop(),
761 cur.par()->params().pagebreakBottom(),
762 VSpace(VSpace::NONE),
763 cur.par()->params().spaceBottom(),
764 cur.par()->params().spacing(),
765 cur.par()->params().align(),
766 cur.par()->params().labelWidthString(), 0);
772 selection.cursor = cursor;
776 selection.cursor = cursor;
780 cutSelection(bv, true);
787 if (!selection.set()) {
788 if (bv->owner()->getIntl().getTransManager().backspace()) {
790 selection.cursor = cursor;
792 // It is possible to make it a lot faster still
793 // just comment out the line below...
798 cutSelection(bv, true);
801 bv->owner()->view_state_changed();
805 case LFUN_BACKSPACE_SKIP:
806 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
807 if (!selection.set()) {
808 LyXCursor cur = cursor;
810 && !(cur.par()->params().spaceTop()
811 == VSpace (VSpace::NONE))) {
813 cur.par()->params().lineTop(),
814 cur.par()->params().lineBottom(),
815 cur.par()->params().pagebreakTop(),
816 cur.par()->params().pagebreakBottom(),
817 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
818 cur.par()->params().spacing(),
819 cur.par()->params().align(),
820 cur.par()->params().labelWidthString(), 0);
823 selection.cursor = cur;
827 cutSelection(bv, true);
832 case LFUN_BREAKPARAGRAPH:
833 bv->beforeChange(this);
834 breakParagraph(bv, 0);
836 selection.cursor = cursor;
838 bv->owner()->view_state_changed();
841 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
842 bv->beforeChange(this);
843 breakParagraph(bv, 1);
845 selection.cursor = cursor;
847 bv->owner()->view_state_changed();
850 case LFUN_BREAKPARAGRAPH_SKIP: {
851 // When at the beginning of a paragraph, remove
852 // indentation and add a "defskip" at the top.
853 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
854 LyXCursor cur = cursor;
855 bv->beforeChange(this);
856 if (cur.pos() == 0) {
857 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
859 cur.par()->params().lineTop(),
860 cur.par()->params().lineBottom(),
861 cur.par()->params().pagebreakTop(),
862 cur.par()->params().pagebreakBottom(),
863 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
864 cur.par()->params().spacing(),
865 cur.par()->params().align(),
866 cur.par()->params().labelWidthString(), 1);
871 breakParagraph(bv, 0);
875 selection.cursor = cur;
877 bv->owner()->view_state_changed();
881 case LFUN_PARAGRAPH_SPACING: {
882 Paragraph * par = cursor.par();
883 Spacing::Space cur_spacing = par->params().spacing().getSpace();
884 float cur_value = 1.0;
885 if (cur_spacing == Spacing::Other)
886 cur_value = par->params().spacing().getValue();
888 istringstream is(cmd.argument.c_str());
891 Spacing::Space new_spacing = cur_spacing;
892 float new_value = cur_value;
894 lyxerr << "Missing argument to `paragraph-spacing'"
896 } else if (tmp == "single") {
897 new_spacing = Spacing::Single;
898 } else if (tmp == "onehalf") {
899 new_spacing = Spacing::Onehalf;
900 } else if (tmp == "double") {
901 new_spacing = Spacing::Double;
902 } else if (tmp == "other") {
903 new_spacing = Spacing::Other;
906 lyxerr << "new_value = " << tmpval << endl;
909 } else if (tmp == "default") {
910 new_spacing = Spacing::Default;
912 lyxerr << _("Unknown spacing argument: ")
913 << cmd.argument << endl;
915 if (cur_spacing != new_spacing || cur_value != new_value) {
916 par->params().spacing(Spacing(new_spacing, new_value));
923 case LFUN_INSET_TOGGLE:
925 bv->beforeChange(this);
932 case LFUN_PROTECTEDSPACE:
933 if (cursor.par()->layout()->free_spacing) {
937 specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
939 moveCursorUpdate(bv, false);
942 case LFUN_HYPHENATION:
943 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
946 case LFUN_LIGATURE_BREAK:
947 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
951 specialChar(this, bv, InsetSpecialChar::LDOTS);
957 insertChar(bv, Paragraph::META_HFILL);
961 case LFUN_END_OF_SENTENCE:
962 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
965 case LFUN_MENU_SEPARATOR:
966 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
970 bv->beforeChange(this);
972 selection.cursor = cursor;
973 cmd.message(N_("Mark off"));
977 bv->beforeChange(this);
978 selection.mark(true);
980 selection.cursor = cursor;
981 cmd.message(N_("Mark on"));
985 bv->beforeChange(this);
986 if (selection.mark()) {
988 cmd.message(N_("Mark removed"));
990 selection.mark(true);
992 cmd.message(N_("Mark set"));
994 selection.cursor = cursor;
997 case LFUN_UPCASE_WORD:
999 changeCase(bv, LyXText::text_uppercase);
1001 bv->updateInset(inset_owner, true);
1005 case LFUN_LOWCASE_WORD:
1007 changeCase(bv, LyXText::text_lowercase);
1009 bv->updateInset(inset_owner, true);
1013 case LFUN_CAPITALIZE_WORD:
1015 changeCase(bv, LyXText::text_capitalization);
1017 bv->updateInset(inset_owner, true);
1021 case LFUN_TRANSPOSE_CHARS:
1023 transposeChars(*bv);
1025 bv->updateInset(inset_owner, true);
1030 cmd.message(_("Paste"));
1032 // clear the selection
1033 bv->toggleSelection();
1037 clearSelection(); // bug 393
1046 cutSelection(bv, true);
1048 cmd.message(_("Cut"));
1053 cmd.message(_("Copy"));
1056 case LFUN_BEGINNINGBUFSEL:
1058 return Inset::UNDISPATCHED;
1061 finishChange(bv, true);
1064 case LFUN_ENDBUFSEL:
1066 return Inset::UNDISPATCHED;
1069 finishChange(bv, true);
1073 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1079 istringstream is(cmd.argument.c_str());
1082 lyxerr << "SETXY: Could not parse coordinates in '"
1083 << cmd.argument << std::endl;
1085 setCursorFromCoordinates(bv, x, y);
1090 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1092 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1098 case LFUN_GETLAYOUT:
1099 cmd.message(tostr(cursor.par()->layout()));
1103 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1104 << cmd.argument << endl;
1106 // This is not the good solution to the empty argument
1107 // problem, but it will hopefully suffice for 1.2.0.
1108 // The correct solution would be to augument the
1109 // function list/array with information about what
1110 // functions needs arguments and their type.
1111 if (cmd.argument.empty()) {
1112 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1116 // Derive layout number from given argument (string)
1117 // and current buffer's textclass (number)
1118 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1119 bool hasLayout = tclass.hasLayout(cmd.argument);
1120 string layout = cmd.argument;
1122 // If the entry is obsolete, use the new one instead.
1124 string const & obs = tclass[layout]->obsoleted_by();
1130 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1135 bool change_layout = (current_layout != layout);
1136 if (!change_layout && selection.set() &&
1137 selection.start.par() != selection.end.par())
1139 Paragraph * spar = selection.start.par();
1140 Paragraph * epar = selection.end.par()->next();
1141 while (spar != epar) {
1142 if (spar->layout()->name() != current_layout) {
1143 change_layout = true;
1146 spar = spar->next();
1149 if (change_layout) {
1151 current_layout = layout;
1153 setLayout(bv, layout);
1154 bv->owner()->setLayout(layout);
1161 case LFUN_PASTESELECTION: {
1165 // this was originally a beforeChange(bv->text), i.e
1166 // the outermost LyXText!
1167 bv->beforeChange(this);
1168 string const clip = bv->getClipboard();
1169 if (!clip.empty()) {
1170 if (cmd.argument == "paragraph")
1171 insertStringAsParagraphs(bv, clip);
1173 insertStringAsLines(bv, clip);
1180 case LFUN_GOTOERROR:
1181 gotoInset(bv, Inset::ERROR_CODE, false);
1185 gotoInset(bv, Inset::NOTE_CODE, false);
1188 case LFUN_REFERENCE_GOTO:
1190 vector<Inset::Code> tmp;
1191 tmp.push_back(Inset::LABEL_CODE);
1192 tmp.push_back(Inset::REF_CODE);
1193 gotoInset(bv, tmp, true);
1198 Paragraph const * par = cursor.par();
1199 lyx::pos_type pos = cursor.pos();
1203 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1206 c = par->getChar(pos - 1);
1209 LyXLayout_ptr const & style = par->layout();
1211 if (style->pass_thru ||
1212 par->getFontSettings(bv->buffer()->params,
1213 pos).language()->lang() == "hebrew" ||
1214 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1215 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1219 case LFUN_DATE_INSERT: {
1220 time_t now_time_t = time(NULL);
1221 struct tm * now_tm = localtime(&now_time_t);
1222 setlocale(LC_TIME, "");
1224 if (!cmd.argument.empty())
1227 arg = lyxrc.date_insert_format;
1229 int const datetmp_len =
1230 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1232 for (int i = 0; i < datetmp_len; i++) {
1233 insertChar(bv, datetmp[i]);
1236 selection.cursor = cursor;
1237 moveCursorUpdate(bv, false);
1241 case LFUN_MOUSE_TRIPLE:
1244 if (bv_owner && bv->theLockingInset())
1246 if (cmd.button() == mouse_button::button1) {
1248 bv->screen().hideCursor();
1249 bv->screen().toggleSelection(this, bv);
1252 selection.cursor = cursor;
1256 bv->screen().toggleSelection(this, bv, false);
1258 bv->haveSelection(selection.set());
1262 case LFUN_MOUSE_DOUBLE:
1265 if (bv_owner && bv->theLockingInset())
1267 if (cmd.button() == mouse_button::button1) {
1269 bv->screen().hideCursor();
1270 bv->screen().toggleSelection(this, bv);
1271 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1272 bv->screen().toggleSelection(this, bv, false);
1274 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1277 bv->haveSelection(selection.set());
1281 case LFUN_MOUSE_MOTION:
1283 // Only use motion with button 1
1284 //if (ev.button() != mouse_button::button1)
1290 // Check for inset locking
1291 if (bv->theLockingInset()) {
1292 Inset * tli = bv->theLockingInset();
1293 LyXCursor cursor = bv->text->cursor;
1294 LyXFont font = bv->text->getFont(bv->buffer(),
1295 cursor.par(), cursor.pos());
1296 int width = tli->width(bv, font);
1297 int inset_x = font.isVisibleRightToLeft()
1298 ? cursor.ix() - width : cursor.ix();
1299 int start_x = inset_x + tli->scroll();
1300 FuncRequest cmd1 = cmd;
1301 cmd1.x = cmd.x - start_x;
1302 cmd1.y = cmd.y - cursor.iy() + bv->text->first_y;
1303 tli->localDispatch(cmd1);
1307 // The test for not selection possible is needed, that only motion
1308 // events are used, where the bottom press event was on
1309 // the drawing area too
1310 if (!selection_possible) {
1311 lyxerr[Debug::ACTION]
1312 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1316 bv->screen().hideCursor();
1318 Row * cursorrow = bv->text->cursor.row();
1319 bv->text->setCursorFromCoordinates(bv, cmd.x, cmd.y + bv->text->first_y);
1321 // sorry for this but I have a strange error that the y value jumps at
1322 // a certain point. This seems like an error in my xforms library or
1323 // in some other local environment, but I would like to leave this here
1324 // for the moment until I can remove this (Jug 20020418)
1325 if (y_before < bv->text->cursor.y())
1326 lyxerr << y_before << ':'
1327 << bv->text->cursor.y() << endl;
1329 // This is to allow jumping over large insets
1330 if (cursorrow == bv->text->cursor.row()) {
1331 if (cmd.y >= int(bv->workHeight()))
1332 bv->text->cursorDown(bv, false);
1334 bv->text->cursorUp(bv, false);
1337 // Maybe an empty line was deleted
1338 if (!bv->text->selection.set())
1339 bv->update(bv->text, BufferView::UPDATE);
1340 bv->text->setSelection(bv);
1341 bv->screen().toggleToggle(bv->text, bv);
1347 // Single-click on work area
1348 case LFUN_MOUSE_PRESS:
1353 // ok ok, this is a hack (for xforms)
1354 // We shouldn't go further down as we really should only do the
1355 // scrolling and be done with this. Otherwise we may open some
1356 // dialogs (Jug 20020424).
1357 if (cmd.button() == mouse_button::button4) {
1358 bv->scroll(-lyxrc.wheel_jump);
1361 if (cmd.button() == mouse_button::button5) {
1362 bv->scroll(lyxrc.wheel_jump);
1368 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1370 // Middle button press pastes if we have a selection
1371 // We do this here as if the selection was inside an inset
1372 // it could get cleared on the unlocking of the inset so
1373 // we have to check this first
1374 bool paste_internally = false;
1375 if (cmd.button() == mouse_button::button2 && selection.set()) {
1376 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1377 paste_internally = true;
1380 int const screen_first = bv->text->first_y;
1382 if (bv->theLockingInset()) {
1383 // We are in inset locking mode
1385 // Check whether the inset was hit. If not reset mode,
1386 // otherwise give the event to the inset
1387 if (inset_hit == bv->theLockingInset()) {
1388 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1389 bv->theLockingInset()->localDispatch(cmd1);
1392 bv->unlockInset(bv->theLockingInset());
1396 selection_possible = true;
1397 bv->screen().hideCursor();
1399 // Clear the selection
1400 bv->screen().toggleSelection(bv->text, bv);
1401 bv->text->clearSelection();
1402 bv->text->fullRebreak(bv);
1404 bv->updateScrollbar();
1406 // Single left click in math inset?
1407 if (isHighlyEditableInset(inset_hit)) {
1408 // Highly editable inset, like math
1409 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1410 selection_possible = false;
1411 bv->owner()->updateLayoutChoice();
1412 bv->owner()->message(inset->editMessage());
1413 //inset->edit(bv, x, y, cmd.button());
1414 // We just have to lock the inset before calling a PressEvent on it!
1415 // we don't need the edit() call here! (Jug20020329)
1416 if (!bv->lockInset(inset))
1417 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1418 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1419 inset->localDispatch(cmd1);
1422 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1424 // Right click on a footnote flag opens float menu
1425 if (cmd.button() == mouse_button::button3) {
1426 selection_possible = false;
1430 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1431 bv->text->setCursorFromCoordinates(bv, x, y + screen_first);
1433 bv->text->selection.cursor = bv->text->cursor;
1434 bv->text->cursor.x_fix(bv->text->cursor.x());
1436 bv->owner()->updateLayoutChoice();
1437 if (bv->fitCursor())
1438 selection_possible = false;
1440 // Insert primary selection with middle mouse
1441 // if there is a local selection in the current buffer,
1443 if (cmd.button() == mouse_button::button2) {
1444 if (paste_internally)
1445 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1447 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1448 selection_possible = false;
1453 case LFUN_MOUSE_RELEASE:
1455 // do nothing if we used the mouse wheel
1459 if (cmd.button() == mouse_button::button4
1460 || cmd.button() == mouse_button::button5)
1463 // If we hit an inset, we have the inset coordinates in these
1464 // and inset_hit points to the inset. If we do not hit an
1465 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1468 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1470 if (bv->theLockingInset()) {
1471 // We are in inset locking mode.
1473 // LyX does a kind of work-area grabbing for insets.
1474 // Only a ButtonPress FuncRequest outside the inset will
1475 // force a insetUnlock.
1476 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1477 bv->theLockingInset()->localDispatch(cmd1);
1481 selection_possible = false;
1483 if (cmd.button() == mouse_button::button2)
1487 if (cmd.button() == mouse_button::button1)
1488 bv->haveSelection(selection.set());
1491 bv->owner()->view_state_changed();
1492 bv->owner()->updateMenubar();
1493 bv->owner()->updateToolbar();
1495 // Did we hit an editable inset?
1497 selection_possible = false;
1499 // if we reach this point with a selection, it
1500 // must mean we are currently selecting.
1501 // But we don't want to open the inset
1502 // because that is annoying for the user.
1503 // So just pretend we didn't hit it.
1504 // this is OK because a "kosher" ButtonRelease
1505 // will follow a ButtonPress that clears
1507 // Note this also fixes selection drawing
1508 // problems if we end up opening an inset
1509 if (selection.set())
1512 // CHECK fix this proper in 0.13
1513 // well, maybe 13.0 !!!!!!!!!
1515 // Following a ref shouldn't issue
1516 // a push on the undo-stack
1517 // anylonger, now that we have
1518 // keybindings for following
1519 // references and returning from
1520 // references. IMHO though, it
1521 // should be the inset's own business
1522 // to push or not push on the undo
1523 // stack. They don't *have* to
1524 // alter the document...
1526 // ...or maybe the SetCursorParUndo()
1527 // below isn't necessary at all anylonger?
1528 if (inset_hit->lyxCode() == Inset::REF_CODE)
1529 setCursorParUndo(bv);
1531 bv->owner()->message(inset_hit->editMessage());
1533 if (isHighlyEditableInset(inset_hit)) {
1534 // Highly editable inset, like math
1535 UpdatableInset * inset = (UpdatableInset *) inset_hit;
1536 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1537 inset->localDispatch(cmd1);
1539 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1540 inset_hit->localDispatch(cmd1);
1541 // IMO this is a grosshack! Inset's should be changed so that
1542 // they call the actions they have to do with the insetButtonRel.
1543 // function and not in the edit(). This should be changed
1545 #ifdef WITH_WARNINGS
1546 #warning Please remove donot call inset->edit() here (Jug 20020812)
1548 inset_hit->edit(bv, x, y, cmd.button());
1553 // Maybe we want to edit a bibitem ale970302
1554 if (bv->text->cursor.par()->bibkey) {
1556 bv->text->cursor.par()->isRightToLeftPar(bv->buffer()->params);
1558 bibitemMaxWidth(bv, bv->buffer()->params.getLyXTextClass().defaultfont());
1559 if ((is_rtl && x > bv->text->workWidth(bv)-20-width) ||
1560 (!is_rtl && x < 20 + width)) {
1561 bv->text->cursor.par()->bibkey->edit(bv, 0, 0, mouse_button::none);
1567 case LFUN_SELFINSERT: {
1568 if (cmd.argument.empty())
1571 // Automatically delete the currently selected
1572 // text and replace it with what is being
1573 // typed in now. Depends on lyxrc settings
1574 // "auto_region_delete", which defaults to
1577 if (lyxrc.auto_region_delete) {
1578 if (selection.set()) {
1579 cutSelection(bv, false, false);
1582 bv->haveSelection(false);
1585 bv->beforeChange(this);
1586 LyXFont const old_font(real_current_font);
1588 string::const_iterator cit = cmd.argument.begin();
1589 string::const_iterator end = cmd.argument.end();
1590 for (; cit != end; ++cit)
1591 bv->owner()->getIntl().getTransManager().
1592 TranslateAndInsert(*cit, this);
1595 selection.cursor = cursor;
1596 moveCursorUpdate(bv, false);
1598 // real_current_font.number can change so we need to
1599 // update the minibuffer
1600 if (old_font != real_current_font)
1601 bv->owner()->view_state_changed();
1605 case LFUN_HTMLURL: {
1606 InsetCommandParams p("htmlurl");
1607 bv->owner()->getDialogs().createUrl(p.getAsString());
1612 InsetCommandParams p("url");
1613 bv->owner()->getDialogs().createUrl(p.getAsString());
1619 case LFUN_INSET_LIST:
1620 case LFUN_INSET_THEOREM:
1621 case LFUN_INSET_CAPTION:
1623 case LFUN_INSERT_NOTE:
1624 case LFUN_INSET_ERT:
1625 case LFUN_INSET_FLOAT:
1626 case LFUN_INSET_FOOTNOTE:
1627 case LFUN_INSET_MARGINAL:
1628 case LFUN_INSET_MINIPAGE:
1629 case LFUN_INSET_OPTARG:
1630 case LFUN_INSET_WIDE_FLOAT:
1631 case LFUN_INSET_WRAP:
1632 case LFUN_TABULAR_INSERT:
1633 // Open the inset, and move the current selection
1635 doInsertInset(this, cmd, true, true);
1638 case LFUN_INSERT_URL:
1639 case LFUN_INSET_EXTERNAL:
1640 case LFUN_INDEX_INSERT:
1641 // Just open the inset
1642 doInsertInset(this, cmd, true, false);
1645 case LFUN_INDEX_PRINT:
1646 case LFUN_PARENTINSERT:
1647 case LFUN_TOC_INSERT:
1649 doInsertInset(this, cmd, false, false);
1653 return Inset::UNDISPATCHED;
1656 return Inset::DISPATCHED;