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/insetquotes.h"
35 #include "insets/insetcommand.h"
36 #include "undo_funcs.h"
45 extern string current_layout;
46 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
48 // the selection possible is needed, that only motion events are
49 // used, where the bottom press event was on the drawing area too
50 bool selection_possible = false;
55 void moveCursorUpdate(BufferView * bv, bool selecting)
57 LyXText * lt = bv->getLyXText();
59 if (selecting || lt->selection.mark()) {
64 bv->updateInset(lt->inset_owner, false);
66 if (lt->isTopLevel()) {
68 // bv->update(lt, BufferView::SELECT|BufferView::FITCUR);
70 bv->update(lt, BufferView::SELECT|BufferView::FITCUR);
72 } else if (bv->text->status() != LyXText::UNCHANGED) {
73 bv->theLockingInset()->hideInsetCursor(bv);
74 bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
78 if (!lt->selection.set())
79 bv->haveSelection(false);
85 void finishChange(BufferView * bv, bool selecting = false)
88 moveCursorUpdate(bv, selecting);
89 bv->owner()->view_state_changed();
92 // check if the given co-ordinates are inside an inset at the
93 // given cursor, if one exists. If so, the inset is returned,
94 // and the co-ordinates are made relative. Otherwise, 0 is returned.
95 Inset * checkInset(BufferView * bv, LyXText const & text,
96 LyXCursor const & cur, int & x, int & y)
98 lyx::pos_type const pos = cur.pos();
99 Paragraph /*const*/ & par = *cur.par();
101 if (pos >= par.size() || !par.isInset(pos))
104 Inset /*const*/ * inset = par.getInset(pos);
106 if (!isEditableInset(inset))
109 // get inset dimensions
110 lyx::Assert(par.getInset(pos));
112 LyXFont const & font = text.getFont(bv->buffer(), &par, pos);
114 int const width = inset->width(bv, font);
115 int const inset_x = font.isVisibleRightToLeft()
116 ? (cur.ix() - width) : cur.ix();
119 inset_x + inset->scroll(),
121 cur.iy() - inset->ascent(bv, font),
122 cur.iy() + inset->descent(bv, font)
125 if (!b.contained(x, y)) {
126 lyxerr[Debug::GUI] << "Missed inset at 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 + top_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->top_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 taller than the workarea. The
261 // simplest solution is to move to the previous row instead.
264 // This is what we used to do, so we wouldn't skip right past
265 // tall rows, but it's not working right now.
267 new_y = bv->text->top_y() - bv->workHeight();
271 new_y = bv->text->cursor.iy()
272 + bv->theLockingInset()->insetInInsetY() + y
273 + cursor.row()->height()
274 - bv->workHeight() + 1;
277 - cursor.row()->baseline()
278 + cursor.row()->height()
279 - bv->workHeight() + 1;
282 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
283 if (cursor.row()->previous()) {
285 setCursor(bv, cur, cursor.row()->previous()->par(),
286 cursor.row()->previous()->pos(), false);
287 if (cur.y() > top_y()) {
291 bv->updateScrollbar();
295 void LyXText::cursorNext(BufferView * bv)
297 if (!cursor.row()->next()) {
298 int y = cursor.y() - cursor.row()->baseline() +
299 cursor.row()->height();
300 if (y > top_y() + bv->workHeight()) {
301 bv->screen().draw(bv->text, bv, bv->text->top_y() + bv->workHeight());
302 bv->updateScrollbar();
307 int y = top_y() + bv->workHeight();
308 if (inset_owner && !top_y()) {
309 y -= (bv->text->cursor.iy()
311 + bv->theLockingInset()->insetInInsetY());
316 Row * cursorrow = cursor.row();
317 setCursorFromCoordinates(bv, cursor.x_fix(), y);
318 // + bv->workHeight());
322 if (cursorrow == bv->text->cursor.row()) {
323 // we have a row which is taller than the workarea. The
324 // simplest solution is to move to the next row instead.
325 cursorDown(bv, true);
327 // This is what we used to do, so we wouldn't skip right past
328 // tall rows, but it's not working right now.
330 new_y = bv->text->top_y() + bv->workHeight();
334 new_y = bv->text->cursor.iy()
335 + bv->theLockingInset()->insetInInsetY()
336 + y - cursor.row()->baseline();
338 new_y = cursor.y() - cursor.row()->baseline();
341 bv->screen().draw(bv->text, bv, new_y);
342 if (cursor.row()->next()) {
344 setCursor(bv, cur, cursor.row()->next()->par(),
345 cursor.row()->next()->pos(), false);
346 if (cur.y() < top_y() + bv->workHeight()) {
347 cursorDown(bv, true);
350 bv->updateScrollbar();
354 void LyXText::update(BufferView * bv, bool changed)
356 BufferView::UpdateCodes c = BufferView::SELECT | BufferView::FITCUR;
358 bv->update(this, c | BufferView::CHANGE);
365 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
369 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
370 if (!bv->insertInset(new_inset))
373 bv->updateInset(new_inset, true);
377 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
378 bool edit, bool pastesel)
380 Inset * inset = createInset(cmd);
381 BufferView * bv = cmd.view();
385 if (lt->selection.set()) {
386 lt->cutSelection(bv, true, false);
389 if (bv->insertInset(inset)) {
392 if (gotsel && pastesel)
393 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
402 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
404 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
405 <<"] arg[" << cmd.argument << ']' << endl;
407 BufferView * bv = cmd.view();
409 switch (cmd.action) {
411 case LFUN_APPENDIX: {
412 Paragraph * par = cursor.par();
413 bool start = !par->params().startOfAppendix();
415 // ensure that we have only one start_of_appendix in this document
416 Paragraph * tmp = ownerParagraph();
417 for (; tmp; tmp = tmp->next()) {
418 if (tmp->params().startOfAppendix()) {
419 setUndo(bv, Undo::EDIT, tmp, tmp->next());
420 tmp->params().startOfAppendix(false);
425 setUndo(bv, Undo::EDIT, par, par->next());
426 par->params().startOfAppendix(start);
428 // we can set the refreshing parameters now
429 status(cmd.view(), LyXText::NEED_MORE_REFRESH);
431 refresh_row = 0; // not needed for full update
432 updateCounters(cmd.view());
433 setCursor(cmd.view(), cursor.par(), cursor.pos());
438 case LFUN_DELETE_WORD_FORWARD:
439 bv->beforeChange(this);
441 deleteWordForward(bv);
446 case LFUN_DELETE_WORD_BACKWARD:
447 bv->beforeChange(this);
449 deleteWordBackward(bv);
454 case LFUN_DELETE_LINE_FORWARD:
455 bv->beforeChange(this);
457 deleteLineForward(bv);
464 if (!selection.mark())
465 bv->beforeChange(this);
472 if (!selection.mark())
473 bv->beforeChange(this);
475 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
476 cursorLeftOneWord(bv);
478 cursorRightOneWord(bv);
483 if (!selection.mark())
484 bv->beforeChange(this);
486 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
487 cursorRightOneWord(bv);
489 cursorLeftOneWord(bv);
493 case LFUN_BEGINNINGBUF:
494 if (!selection.mark())
495 bv->beforeChange(this);
502 if (selection.mark())
503 bv->beforeChange(this);
511 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
515 finishChange(bv, true);
520 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
524 finishChange(bv, true);
530 finishChange(bv, true);
535 cursorDown(bv, true);
536 finishChange(bv, true);
539 case LFUN_UP_PARAGRAPHSEL:
541 cursorUpParagraph(bv);
542 finishChange(bv, true);
545 case LFUN_DOWN_PARAGRAPHSEL:
547 cursorDownParagraph(bv);
548 finishChange(bv, true);
554 finishChange(bv, true);
560 finishChange(bv, true);
566 finishChange(bv, true);
572 finishChange(bv, true);
575 case LFUN_WORDRIGHTSEL:
577 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
578 cursorLeftOneWord(bv);
580 cursorRightOneWord(bv);
581 finishChange(bv, true);
584 case LFUN_WORDLEFTSEL:
586 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
587 cursorRightOneWord(bv);
589 cursorLeftOneWord(bv);
590 finishChange(bv, true);
597 getWord(cur1, cur2, WHOLE_WORD);
598 setCursor(bv, cur1.par(), cur1.pos());
599 bv->beforeChange(this);
600 setCursor(bv, cur2.par(), cur2.pos());
601 finishChange(bv, true);
606 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
607 if (!selection.mark())
608 bv->beforeChange(this);
611 cursorLeft(bv, false);
612 if (cursor.pos() < cursor.par()->size()
613 && cursor.par()->isInset(cursor.pos())
614 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
615 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
616 cmd.message(tmpinset->editMessage());
617 tmpinset->edit(bv, !is_rtl);
621 cursorRight(bv, false);
627 // This is soooo ugly. Isn`t it possible to make
629 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
630 if (!selection.mark())
631 bv->beforeChange(this);
633 LyXCursor const cur = cursor;
635 cursorLeft(bv, false);
636 if ((is_rtl || cur != cursor) && // only if really moved!
637 cursor.pos() < cursor.par()->size() &&
638 cursor.par()->isInset(cursor.pos()) &&
639 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
640 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
641 cmd.message(tmpinset->editMessage());
642 tmpinset->edit(bv, is_rtl);
646 cursorRight(bv, false);
652 if (!selection.mark())
653 bv->beforeChange(this);
654 bv->update(this, BufferView::UPDATE);
660 if (!selection.mark())
661 bv->beforeChange(this);
662 bv->update(this, BufferView::UPDATE);
667 case LFUN_UP_PARAGRAPH:
668 if (!selection.mark())
669 bv->beforeChange(this);
670 bv->update(this, BufferView::UPDATE);
671 cursorUpParagraph(bv);
675 case LFUN_DOWN_PARAGRAPH:
676 if (!selection.mark())
677 bv->beforeChange(this);
678 bv->update(this, BufferView::UPDATE);
679 cursorDownParagraph(bv);
680 finishChange(bv, false);
684 if (!selection.mark())
685 bv->beforeChange(this);
686 bv->update(this, BufferView::UPDATE);
688 finishChange(bv, false);
691 // moveCursorUpdate(bv, false, false);
692 // owner_->view_state_changed();
696 if (!selection.mark())
697 bv->beforeChange(this);
698 bv->update(this, BufferView::UPDATE);
700 finishChange(bv, false);
704 if (!selection.mark())
705 bv->beforeChange(this);
708 finishChange(bv, false);
712 if (!selection.mark())
713 bv->beforeChange(this);
716 finishChange(bv, false);
720 bv->beforeChange(this);
721 insertChar(bv, Paragraph::META_NEWLINE);
723 setCursor(bv, cursor.par(), cursor.pos());
724 moveCursorUpdate(bv, false);
728 if (!selection.set()) {
730 selection.cursor = cursor;
732 // It is possible to make it a lot faster still
733 // just comment out the line below...
737 cutSelection(bv, true);
740 moveCursorUpdate(bv, false);
741 bv->owner()->view_state_changed();
745 case LFUN_DELETE_SKIP:
746 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
747 if (!selection.set()) {
748 LyXCursor cur = cursor;
749 if (cur.pos() == cur.par()->size()) {
753 && !(cur.par()->params().spaceTop()
754 == VSpace (VSpace::NONE))) {
756 cur.par()->params().lineTop(),
757 cur.par()->params().lineBottom(),
758 cur.par()->params().pagebreakTop(),
759 cur.par()->params().pagebreakBottom(),
760 VSpace(VSpace::NONE),
761 cur.par()->params().spaceBottom(),
762 cur.par()->params().spacing(),
763 cur.par()->params().align(),
764 cur.par()->params().labelWidthString(), 0);
770 selection.cursor = cursor;
774 selection.cursor = cursor;
778 cutSelection(bv, true);
785 if (!selection.set()) {
786 if (bv->owner()->getIntl().getTransManager().backspace()) {
788 selection.cursor = cursor;
790 // It is possible to make it a lot faster still
791 // just comment out the line below...
796 cutSelection(bv, true);
799 bv->owner()->view_state_changed();
803 case LFUN_BACKSPACE_SKIP:
804 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
805 if (!selection.set()) {
806 LyXCursor cur = cursor;
808 && !(cur.par()->params().spaceTop()
809 == VSpace (VSpace::NONE))) {
811 cur.par()->params().lineTop(),
812 cur.par()->params().lineBottom(),
813 cur.par()->params().pagebreakTop(),
814 cur.par()->params().pagebreakBottom(),
815 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
816 cur.par()->params().spacing(),
817 cur.par()->params().align(),
818 cur.par()->params().labelWidthString(), 0);
821 selection.cursor = cur;
825 cutSelection(bv, true);
830 case LFUN_BREAKPARAGRAPH:
831 bv->beforeChange(this);
832 breakParagraph(bv, bv->buffer()->paragraphs, 0);
834 selection.cursor = cursor;
836 bv->owner()->view_state_changed();
839 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
840 bv->beforeChange(this);
841 breakParagraph(bv, bv->buffer()->paragraphs, 1);
843 selection.cursor = cursor;
845 bv->owner()->view_state_changed();
848 case LFUN_BREAKPARAGRAPH_SKIP: {
849 // When at the beginning of a paragraph, remove
850 // indentation and add a "defskip" at the top.
851 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
852 LyXCursor cur = cursor;
853 bv->beforeChange(this);
854 if (cur.pos() == 0) {
855 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
857 cur.par()->params().lineTop(),
858 cur.par()->params().lineBottom(),
859 cur.par()->params().pagebreakTop(),
860 cur.par()->params().pagebreakBottom(),
861 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
862 cur.par()->params().spacing(),
863 cur.par()->params().align(),
864 cur.par()->params().labelWidthString(), 1);
869 breakParagraph(bv, bv->buffer()->paragraphs, 0);
873 selection.cursor = cur;
875 bv->owner()->view_state_changed();
879 case LFUN_PARAGRAPH_SPACING: {
880 Paragraph * par = cursor.par();
881 Spacing::Space cur_spacing = par->params().spacing().getSpace();
882 float cur_value = 1.0;
883 if (cur_spacing == Spacing::Other)
884 cur_value = par->params().spacing().getValue();
886 istringstream is(cmd.argument.c_str());
889 Spacing::Space new_spacing = cur_spacing;
890 float new_value = cur_value;
892 lyxerr << "Missing argument to `paragraph-spacing'"
894 } else if (tmp == "single") {
895 new_spacing = Spacing::Single;
896 } else if (tmp == "onehalf") {
897 new_spacing = Spacing::Onehalf;
898 } else if (tmp == "double") {
899 new_spacing = Spacing::Double;
900 } else if (tmp == "other") {
901 new_spacing = Spacing::Other;
904 lyxerr << "new_value = " << tmpval << endl;
907 } else if (tmp == "default") {
908 new_spacing = Spacing::Default;
910 lyxerr << _("Unknown spacing argument: ")
911 << cmd.argument << endl;
913 if (cur_spacing != new_spacing || cur_value != new_value) {
914 par->params().spacing(Spacing(new_spacing, new_value));
921 case LFUN_INSET_TOGGLE:
923 bv->beforeChange(this);
930 case LFUN_PROTECTEDSPACE:
931 if (cursor.par()->layout()->free_spacing) {
935 specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
937 moveCursorUpdate(bv, false);
940 case LFUN_HYPHENATION:
941 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
944 case LFUN_LIGATURE_BREAK:
945 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
949 specialChar(this, bv, InsetSpecialChar::LDOTS);
952 case LFUN_END_OF_SENTENCE:
953 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
956 case LFUN_MENU_SEPARATOR:
957 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
961 bv->beforeChange(this);
963 selection.cursor = cursor;
964 cmd.message(N_("Mark off"));
968 bv->beforeChange(this);
969 selection.mark(true);
971 selection.cursor = cursor;
972 cmd.message(N_("Mark on"));
976 bv->beforeChange(this);
977 if (selection.mark()) {
979 cmd.message(N_("Mark removed"));
981 selection.mark(true);
983 cmd.message(N_("Mark set"));
985 selection.cursor = cursor;
988 case LFUN_UPCASE_WORD:
990 changeCase(*bv, LyXText::text_uppercase);
992 bv->updateInset(inset_owner, true);
996 case LFUN_LOWCASE_WORD:
998 changeCase(*bv, LyXText::text_lowercase);
1000 bv->updateInset(inset_owner, true);
1004 case LFUN_CAPITALIZE_WORD:
1006 changeCase(*bv, LyXText::text_capitalization);
1008 bv->updateInset(inset_owner, true);
1012 case LFUN_TRANSPOSE_CHARS:
1014 transposeChars(*bv);
1016 bv->updateInset(inset_owner, true);
1021 cmd.message(_("Paste"));
1023 // clear the selection
1024 bv->toggleSelection();
1028 clearSelection(); // bug 393
1037 cutSelection(bv, true);
1039 cmd.message(_("Cut"));
1044 cmd.message(_("Copy"));
1047 case LFUN_BEGINNINGBUFSEL:
1049 return UNDISPATCHED;
1052 finishChange(bv, true);
1055 case LFUN_ENDBUFSEL:
1057 return UNDISPATCHED;
1060 finishChange(bv, true);
1064 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1070 istringstream is(cmd.argument.c_str());
1073 lyxerr << "SETXY: Could not parse coordinates in '"
1074 << cmd.argument << std::endl;
1076 setCursorFromCoordinates(bv, x, y);
1081 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1083 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1089 case LFUN_GETLAYOUT:
1090 cmd.message(tostr(cursor.par()->layout()));
1094 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1095 << cmd.argument << endl;
1097 // This is not the good solution to the empty argument
1098 // problem, but it will hopefully suffice for 1.2.0.
1099 // The correct solution would be to augument the
1100 // function list/array with information about what
1101 // functions needs arguments and their type.
1102 if (cmd.argument.empty()) {
1103 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1107 // Derive layout number from given argument (string)
1108 // and current buffer's textclass (number)
1109 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1110 bool hasLayout = tclass.hasLayout(cmd.argument);
1111 string layout = cmd.argument;
1113 // If the entry is obsolete, use the new one instead.
1115 string const & obs = tclass[layout]->obsoleted_by();
1121 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1126 bool change_layout = (current_layout != layout);
1127 if (!change_layout && selection.set() &&
1128 selection.start.par() != selection.end.par())
1130 Paragraph * spar = selection.start.par();
1131 Paragraph * epar = selection.end.par()->next();
1132 while (spar != epar) {
1133 if (spar->layout()->name() != current_layout) {
1134 change_layout = true;
1137 spar = spar->next();
1140 if (change_layout) {
1142 current_layout = layout;
1144 setLayout(bv, layout);
1145 bv->owner()->setLayout(layout);
1152 case LFUN_PASTESELECTION: {
1156 // this was originally a beforeChange(bv->text), i.e
1157 // the outermost LyXText!
1158 bv->beforeChange(this);
1159 string const clip = bv->getClipboard();
1160 if (!clip.empty()) {
1161 if (cmd.argument == "paragraph")
1162 insertStringAsParagraphs(bv, clip);
1164 insertStringAsLines(bv, clip);
1171 case LFUN_GOTOERROR:
1172 gotoInset(bv, Inset::ERROR_CODE, false);
1176 gotoInset(bv, Inset::NOTE_CODE, false);
1179 case LFUN_REFERENCE_GOTO:
1181 vector<Inset::Code> tmp;
1182 tmp.push_back(Inset::LABEL_CODE);
1183 tmp.push_back(Inset::REF_CODE);
1184 gotoInset(bv, tmp, true);
1189 Paragraph const * par = cursor.par();
1190 lyx::pos_type pos = cursor.pos();
1194 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1197 c = par->getChar(pos - 1);
1200 LyXLayout_ptr const & style = par->layout();
1202 if (style->pass_thru ||
1203 par->getFontSettings(bv->buffer()->params,
1204 pos).language()->lang() == "hebrew" ||
1205 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1206 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1210 case LFUN_DATE_INSERT: {
1211 time_t now_time_t = time(NULL);
1212 struct tm * now_tm = localtime(&now_time_t);
1213 setlocale(LC_TIME, "");
1215 if (!cmd.argument.empty())
1218 arg = lyxrc.date_insert_format;
1220 int const datetmp_len =
1221 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1223 for (int i = 0; i < datetmp_len; i++) {
1224 insertChar(bv, datetmp[i]);
1227 selection.cursor = cursor;
1228 moveCursorUpdate(bv, false);
1232 case LFUN_MOUSE_TRIPLE:
1235 if (isTopLevel() && bv->theLockingInset())
1237 if (cmd.button() == mouse_button::button1) {
1239 bv->screen().hideCursor();
1240 bv->screen().toggleSelection(this, bv);
1243 selection.cursor = cursor;
1247 bv->screen().toggleSelection(this, bv, false);
1249 bv->haveSelection(selection.set());
1253 case LFUN_MOUSE_DOUBLE:
1256 if (isTopLevel() && bv->theLockingInset())
1258 if (cmd.button() == mouse_button::button1) {
1260 bv->screen().hideCursor();
1261 bv->screen().toggleSelection(this, bv);
1262 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1263 bv->screen().toggleSelection(this, bv, false);
1265 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1268 bv->haveSelection(selection.set());
1272 case LFUN_MOUSE_MOTION:
1274 // Only use motion with button 1
1275 //if (ev.button() != mouse_button::button1)
1281 // Check for inset locking
1282 if (bv->theLockingInset()) {
1283 Inset * tli = bv->theLockingInset();
1284 LyXCursor cursor = bv->text->cursor;
1285 LyXFont font = bv->text->getFont(bv->buffer(),
1286 cursor.par(), cursor.pos());
1287 int width = tli->width(bv, font);
1288 int inset_x = font.isVisibleRightToLeft()
1289 ? cursor.ix() - width : cursor.ix();
1290 int start_x = inset_x + tli->scroll();
1291 FuncRequest cmd1 = cmd;
1292 cmd1.x = cmd.x - start_x;
1293 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1294 tli->localDispatch(cmd1);
1298 // The test for not selection possible is needed, that only motion
1299 // events are used, where the bottom press event was on
1300 // the drawing area too
1301 if (!selection_possible) {
1302 lyxerr[Debug::ACTION]
1303 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1307 bv->screen().hideCursor();
1309 Row * cursorrow = bv->text->cursor.row();
1310 bv->text->setCursorFromCoordinates(bv, cmd.x, cmd.y + bv->text->top_y());
1312 // sorry for this but I have a strange error that the y value jumps at
1313 // a certain point. This seems like an error in my xforms library or
1314 // in some other local environment, but I would like to leave this here
1315 // for the moment until I can remove this (Jug 20020418)
1316 if (y_before < bv->text->cursor.y())
1317 lyxerr << y_before << ':'
1318 << bv->text->cursor.y() << endl;
1320 // This is to allow jumping over large insets
1321 if (cursorrow == bv->text->cursor.row()) {
1322 if (cmd.y >= bv->workHeight())
1323 bv->text->cursorDown(bv, false);
1325 bv->text->cursorUp(bv, false);
1328 // Maybe an empty line was deleted
1329 if (!bv->text->selection.set())
1330 bv->update(bv->text, BufferView::UPDATE);
1331 bv->text->setSelection(bv);
1332 bv->screen().toggleToggle(bv->text, bv);
1338 // Single-click on work area
1339 case LFUN_MOUSE_PRESS:
1344 // ok ok, this is a hack (for xforms)
1345 // We shouldn't go further down as we really should only do the
1346 // scrolling and be done with this. Otherwise we may open some
1347 // dialogs (Jug 20020424).
1348 if (cmd.button() == mouse_button::button4) {
1349 bv->scroll(-lyxrc.wheel_jump);
1352 if (cmd.button() == mouse_button::button5) {
1353 bv->scroll(lyxrc.wheel_jump);
1359 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1361 // Middle button press pastes if we have a selection
1362 // We do this here as if the selection was inside an inset
1363 // it could get cleared on the unlocking of the inset so
1364 // we have to check this first
1365 bool paste_internally = false;
1366 if (cmd.button() == mouse_button::button2 && selection.set()) {
1367 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1368 paste_internally = true;
1371 int const screen_first = bv->text->top_y();
1373 if (bv->theLockingInset()) {
1374 // We are in inset locking mode
1376 // Check whether the inset was hit. If not reset mode,
1377 // otherwise give the event to the inset
1378 if (inset_hit == bv->theLockingInset()) {
1379 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1380 bv->theLockingInset()->localDispatch(cmd1);
1383 bv->unlockInset(bv->theLockingInset());
1387 selection_possible = true;
1388 bv->screen().hideCursor();
1390 // Clear the selection
1391 bv->screen().toggleSelection(bv->text, bv);
1392 bv->text->clearSelection();
1393 bv->text->fullRebreak(bv);
1395 bv->updateScrollbar();
1397 // Single left click in math inset?
1398 if (isHighlyEditableInset(inset_hit)) {
1399 // Highly editable inset, like math
1400 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1401 selection_possible = false;
1402 bv->owner()->updateLayoutChoice();
1403 bv->owner()->message(inset->editMessage());
1404 //inset->edit(bv, x, y, cmd.button());
1405 // We just have to lock the inset before calling a PressEvent on it!
1406 // we don't need the edit() call here! (Jug20020329)
1407 if (!bv->lockInset(inset))
1408 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1409 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1410 inset->localDispatch(cmd1);
1413 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1415 // Right click on a footnote flag opens float menu
1416 if (cmd.button() == mouse_button::button3) {
1417 selection_possible = false;
1421 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1422 bv->text->setCursorFromCoordinates(bv, x, y + screen_first);
1424 bv->text->selection.cursor = bv->text->cursor;
1425 bv->text->cursor.x_fix(bv->text->cursor.x());
1427 bv->owner()->updateLayoutChoice();
1428 if (bv->fitCursor())
1429 selection_possible = false;
1431 // Insert primary selection with middle mouse
1432 // if there is a local selection in the current buffer,
1434 if (cmd.button() == mouse_button::button2) {
1435 if (paste_internally)
1436 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1438 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1439 selection_possible = false;
1444 case LFUN_MOUSE_RELEASE:
1446 // do nothing if we used the mouse wheel
1450 if (cmd.button() == mouse_button::button4
1451 || cmd.button() == mouse_button::button5)
1454 // If we hit an inset, we have the inset coordinates in these
1455 // and inset_hit points to the inset. If we do not hit an
1456 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1459 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1461 if (bv->theLockingInset()) {
1462 // We are in inset locking mode.
1464 // LyX does a kind of work-area grabbing for insets.
1465 // Only a ButtonPress FuncRequest outside the inset will
1466 // force a insetUnlock.
1467 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1468 bv->theLockingInset()->localDispatch(cmd1);
1472 selection_possible = false;
1474 if (cmd.button() == mouse_button::button2)
1478 if (cmd.button() == mouse_button::button1)
1479 bv->haveSelection(selection.set());
1482 bv->owner()->view_state_changed();
1483 bv->owner()->updateMenubar();
1484 bv->owner()->updateToolbar();
1486 // Did we hit an editable inset?
1488 selection_possible = false;
1490 // if we reach this point with a selection, it
1491 // must mean we are currently selecting.
1492 // But we don't want to open the inset
1493 // because that is annoying for the user.
1494 // So just pretend we didn't hit it.
1495 // this is OK because a "kosher" ButtonRelease
1496 // will follow a ButtonPress that clears
1498 // Note this also fixes selection drawing
1499 // problems if we end up opening an inset
1500 if (selection.set())
1503 // CHECK fix this proper in 0.13
1504 // well, maybe 13.0 !!!!!!!!!
1506 // Following a ref shouldn't issue
1507 // a push on the undo-stack
1508 // anylonger, now that we have
1509 // keybindings for following
1510 // references and returning from
1511 // references. IMHO though, it
1512 // should be the inset's own business
1513 // to push or not push on the undo
1514 // stack. They don't *have* to
1515 // alter the document...
1517 // ...or maybe the SetCursorParUndo()
1518 // below isn't necessary at all anylonger?
1519 if (inset_hit->lyxCode() == Inset::REF_CODE)
1520 setCursorParUndo(bv);
1522 bv->owner()->message(inset_hit->editMessage());
1524 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1525 inset_hit->localDispatch(cmd1);
1531 case LFUN_SELFINSERT: {
1532 if (cmd.argument.empty())
1535 // Automatically delete the currently selected
1536 // text and replace it with what is being
1537 // typed in now. Depends on lyxrc settings
1538 // "auto_region_delete", which defaults to
1541 if (lyxrc.auto_region_delete) {
1542 if (selection.set()) {
1543 cutSelection(bv, false, false);
1546 bv->haveSelection(false);
1549 bv->beforeChange(this);
1550 LyXFont const old_font(real_current_font);
1552 string::const_iterator cit = cmd.argument.begin();
1553 string::const_iterator end = cmd.argument.end();
1554 for (; cit != end; ++cit)
1555 bv->owner()->getIntl().getTransManager().
1556 TranslateAndInsert(*cit, this);
1559 selection.cursor = cursor;
1560 moveCursorUpdate(bv, false);
1562 // real_current_font.number can change so we need to
1563 // update the minibuffer
1564 if (old_font != real_current_font)
1565 bv->owner()->view_state_changed();
1569 case LFUN_HTMLURL: {
1570 InsetCommandParams p("htmlurl");
1571 string const data = InsetCommandMailer::params2string("url", p);
1572 bv->owner()->getDialogs().show("url", data, 0);
1577 InsetCommandParams p("url");
1578 string const data = InsetCommandMailer::params2string("url", p);
1579 bv->owner()->getDialogs().show("url", data, 0);
1585 case LFUN_INSET_LIST:
1586 case LFUN_INSET_THEOREM:
1587 case LFUN_INSET_CAPTION:
1589 case LFUN_INSERT_NOTE:
1590 case LFUN_INSERT_BIBITEM:
1591 case LFUN_INSET_ERT:
1592 case LFUN_INSET_FLOAT:
1593 case LFUN_INSET_FOOTNOTE:
1594 case LFUN_INSET_MARGINAL:
1595 case LFUN_INSET_MINIPAGE:
1596 case LFUN_INSET_OPTARG:
1597 case LFUN_INSET_WIDE_FLOAT:
1598 case LFUN_INSET_WRAP:
1599 case LFUN_TABULAR_INSERT:
1600 // Open the inset, and move the current selection
1602 doInsertInset(this, cmd, true, true);
1605 case LFUN_INDEX_INSERT:
1606 // Just open the inset
1607 doInsertInset(this, cmd, true, false);
1610 case LFUN_INDEX_PRINT:
1611 case LFUN_PARENTINSERT:
1612 case LFUN_TOC_INSERT:
1615 doInsertInset(this, cmd, false, false);
1619 return UNDISPATCHED;