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);
955 insertChar(bv, Paragraph::META_HFILL);
959 case LFUN_END_OF_SENTENCE:
960 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
963 case LFUN_MENU_SEPARATOR:
964 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
968 bv->beforeChange(this);
970 selection.cursor = cursor;
971 cmd.message(N_("Mark off"));
975 bv->beforeChange(this);
976 selection.mark(true);
978 selection.cursor = cursor;
979 cmd.message(N_("Mark on"));
983 bv->beforeChange(this);
984 if (selection.mark()) {
986 cmd.message(N_("Mark removed"));
988 selection.mark(true);
990 cmd.message(N_("Mark set"));
992 selection.cursor = cursor;
995 case LFUN_UPCASE_WORD:
997 changeCase(*bv, LyXText::text_uppercase);
999 bv->updateInset(inset_owner, true);
1003 case LFUN_LOWCASE_WORD:
1005 changeCase(*bv, LyXText::text_lowercase);
1007 bv->updateInset(inset_owner, true);
1011 case LFUN_CAPITALIZE_WORD:
1013 changeCase(*bv, LyXText::text_capitalization);
1015 bv->updateInset(inset_owner, true);
1019 case LFUN_TRANSPOSE_CHARS:
1021 transposeChars(*bv);
1023 bv->updateInset(inset_owner, true);
1028 cmd.message(_("Paste"));
1030 // clear the selection
1031 bv->toggleSelection();
1035 clearSelection(); // bug 393
1044 cutSelection(bv, true);
1046 cmd.message(_("Cut"));
1051 cmd.message(_("Copy"));
1054 case LFUN_BEGINNINGBUFSEL:
1056 return UNDISPATCHED;
1059 finishChange(bv, true);
1062 case LFUN_ENDBUFSEL:
1064 return UNDISPATCHED;
1067 finishChange(bv, true);
1071 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1077 istringstream is(cmd.argument.c_str());
1080 lyxerr << "SETXY: Could not parse coordinates in '"
1081 << cmd.argument << std::endl;
1083 setCursorFromCoordinates(bv, x, y);
1088 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1090 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1096 case LFUN_GETLAYOUT:
1097 cmd.message(tostr(cursor.par()->layout()));
1101 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1102 << cmd.argument << endl;
1104 // This is not the good solution to the empty argument
1105 // problem, but it will hopefully suffice for 1.2.0.
1106 // The correct solution would be to augument the
1107 // function list/array with information about what
1108 // functions needs arguments and their type.
1109 if (cmd.argument.empty()) {
1110 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1114 // Derive layout number from given argument (string)
1115 // and current buffer's textclass (number)
1116 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1117 bool hasLayout = tclass.hasLayout(cmd.argument);
1118 string layout = cmd.argument;
1120 // If the entry is obsolete, use the new one instead.
1122 string const & obs = tclass[layout]->obsoleted_by();
1128 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1133 bool change_layout = (current_layout != layout);
1134 if (!change_layout && selection.set() &&
1135 selection.start.par() != selection.end.par())
1137 Paragraph * spar = selection.start.par();
1138 Paragraph * epar = selection.end.par()->next();
1139 while (spar != epar) {
1140 if (spar->layout()->name() != current_layout) {
1141 change_layout = true;
1144 spar = spar->next();
1147 if (change_layout) {
1149 current_layout = layout;
1151 setLayout(bv, layout);
1152 bv->owner()->setLayout(layout);
1159 case LFUN_PASTESELECTION: {
1163 // this was originally a beforeChange(bv->text), i.e
1164 // the outermost LyXText!
1165 bv->beforeChange(this);
1166 string const clip = bv->getClipboard();
1167 if (!clip.empty()) {
1168 if (cmd.argument == "paragraph")
1169 insertStringAsParagraphs(bv, clip);
1171 insertStringAsLines(bv, clip);
1178 case LFUN_GOTOERROR:
1179 gotoInset(bv, Inset::ERROR_CODE, false);
1183 gotoInset(bv, Inset::NOTE_CODE, false);
1186 case LFUN_REFERENCE_GOTO:
1188 vector<Inset::Code> tmp;
1189 tmp.push_back(Inset::LABEL_CODE);
1190 tmp.push_back(Inset::REF_CODE);
1191 gotoInset(bv, tmp, true);
1196 Paragraph const * par = cursor.par();
1197 lyx::pos_type pos = cursor.pos();
1201 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1204 c = par->getChar(pos - 1);
1207 LyXLayout_ptr const & style = par->layout();
1209 if (style->pass_thru ||
1210 par->getFontSettings(bv->buffer()->params,
1211 pos).language()->lang() == "hebrew" ||
1212 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1213 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1217 case LFUN_DATE_INSERT: {
1218 time_t now_time_t = time(NULL);
1219 struct tm * now_tm = localtime(&now_time_t);
1220 setlocale(LC_TIME, "");
1222 if (!cmd.argument.empty())
1225 arg = lyxrc.date_insert_format;
1227 int const datetmp_len =
1228 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1230 for (int i = 0; i < datetmp_len; i++) {
1231 insertChar(bv, datetmp[i]);
1234 selection.cursor = cursor;
1235 moveCursorUpdate(bv, false);
1239 case LFUN_MOUSE_TRIPLE:
1242 if (isTopLevel() && bv->theLockingInset())
1244 if (cmd.button() == mouse_button::button1) {
1246 bv->screen().hideCursor();
1247 bv->screen().toggleSelection(this, bv);
1250 selection.cursor = cursor;
1254 bv->screen().toggleSelection(this, bv, false);
1256 bv->haveSelection(selection.set());
1260 case LFUN_MOUSE_DOUBLE:
1263 if (isTopLevel() && bv->theLockingInset())
1265 if (cmd.button() == mouse_button::button1) {
1267 bv->screen().hideCursor();
1268 bv->screen().toggleSelection(this, bv);
1269 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1270 bv->screen().toggleSelection(this, bv, false);
1272 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1275 bv->haveSelection(selection.set());
1279 case LFUN_MOUSE_MOTION:
1281 // Only use motion with button 1
1282 //if (ev.button() != mouse_button::button1)
1288 // Check for inset locking
1289 if (bv->theLockingInset()) {
1290 Inset * tli = bv->theLockingInset();
1291 LyXCursor cursor = bv->text->cursor;
1292 LyXFont font = bv->text->getFont(bv->buffer(),
1293 cursor.par(), cursor.pos());
1294 int width = tli->width(bv, font);
1295 int inset_x = font.isVisibleRightToLeft()
1296 ? cursor.ix() - width : cursor.ix();
1297 int start_x = inset_x + tli->scroll();
1298 FuncRequest cmd1 = cmd;
1299 cmd1.x = cmd.x - start_x;
1300 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1301 tli->localDispatch(cmd1);
1305 // The test for not selection possible is needed, that only motion
1306 // events are used, where the bottom press event was on
1307 // the drawing area too
1308 if (!selection_possible) {
1309 lyxerr[Debug::ACTION]
1310 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1314 bv->screen().hideCursor();
1316 Row * cursorrow = bv->text->cursor.row();
1317 bv->text->setCursorFromCoordinates(bv, cmd.x, cmd.y + bv->text->top_y());
1319 // sorry for this but I have a strange error that the y value jumps at
1320 // a certain point. This seems like an error in my xforms library or
1321 // in some other local environment, but I would like to leave this here
1322 // for the moment until I can remove this (Jug 20020418)
1323 if (y_before < bv->text->cursor.y())
1324 lyxerr << y_before << ':'
1325 << bv->text->cursor.y() << endl;
1327 // This is to allow jumping over large insets
1328 if (cursorrow == bv->text->cursor.row()) {
1329 if (cmd.y >= bv->workHeight())
1330 bv->text->cursorDown(bv, false);
1332 bv->text->cursorUp(bv, false);
1335 // Maybe an empty line was deleted
1336 if (!bv->text->selection.set())
1337 bv->update(bv->text, BufferView::UPDATE);
1338 bv->text->setSelection(bv);
1339 bv->screen().toggleToggle(bv->text, bv);
1345 // Single-click on work area
1346 case LFUN_MOUSE_PRESS:
1351 // ok ok, this is a hack (for xforms)
1352 // We shouldn't go further down as we really should only do the
1353 // scrolling and be done with this. Otherwise we may open some
1354 // dialogs (Jug 20020424).
1355 if (cmd.button() == mouse_button::button4) {
1356 bv->scroll(-lyxrc.wheel_jump);
1359 if (cmd.button() == mouse_button::button5) {
1360 bv->scroll(lyxrc.wheel_jump);
1366 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1368 // Middle button press pastes if we have a selection
1369 // We do this here as if the selection was inside an inset
1370 // it could get cleared on the unlocking of the inset so
1371 // we have to check this first
1372 bool paste_internally = false;
1373 if (cmd.button() == mouse_button::button2 && selection.set()) {
1374 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1375 paste_internally = true;
1378 int const screen_first = bv->text->top_y();
1380 if (bv->theLockingInset()) {
1381 // We are in inset locking mode
1383 // Check whether the inset was hit. If not reset mode,
1384 // otherwise give the event to the inset
1385 if (inset_hit == bv->theLockingInset()) {
1386 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1387 bv->theLockingInset()->localDispatch(cmd1);
1390 bv->unlockInset(bv->theLockingInset());
1394 selection_possible = true;
1395 bv->screen().hideCursor();
1397 // Clear the selection
1398 bv->screen().toggleSelection(bv->text, bv);
1399 bv->text->clearSelection();
1400 bv->text->fullRebreak(bv);
1402 bv->updateScrollbar();
1404 // Single left click in math inset?
1405 if (isHighlyEditableInset(inset_hit)) {
1406 // Highly editable inset, like math
1407 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1408 selection_possible = false;
1409 bv->owner()->updateLayoutChoice();
1410 bv->owner()->message(inset->editMessage());
1411 //inset->edit(bv, x, y, cmd.button());
1412 // We just have to lock the inset before calling a PressEvent on it!
1413 // we don't need the edit() call here! (Jug20020329)
1414 if (!bv->lockInset(inset))
1415 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1416 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1417 inset->localDispatch(cmd1);
1420 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1422 // Right click on a footnote flag opens float menu
1423 if (cmd.button() == mouse_button::button3) {
1424 selection_possible = false;
1428 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1429 bv->text->setCursorFromCoordinates(bv, x, y + screen_first);
1431 bv->text->selection.cursor = bv->text->cursor;
1432 bv->text->cursor.x_fix(bv->text->cursor.x());
1434 bv->owner()->updateLayoutChoice();
1435 if (bv->fitCursor())
1436 selection_possible = false;
1438 // Insert primary selection with middle mouse
1439 // if there is a local selection in the current buffer,
1441 if (cmd.button() == mouse_button::button2) {
1442 if (paste_internally)
1443 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1445 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1446 selection_possible = false;
1451 case LFUN_MOUSE_RELEASE:
1453 // do nothing if we used the mouse wheel
1457 if (cmd.button() == mouse_button::button4
1458 || cmd.button() == mouse_button::button5)
1461 // If we hit an inset, we have the inset coordinates in these
1462 // and inset_hit points to the inset. If we do not hit an
1463 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1466 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1468 if (bv->theLockingInset()) {
1469 // We are in inset locking mode.
1471 // LyX does a kind of work-area grabbing for insets.
1472 // Only a ButtonPress FuncRequest outside the inset will
1473 // force a insetUnlock.
1474 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1475 bv->theLockingInset()->localDispatch(cmd1);
1479 selection_possible = false;
1481 if (cmd.button() == mouse_button::button2)
1485 if (cmd.button() == mouse_button::button1)
1486 bv->haveSelection(selection.set());
1489 bv->owner()->view_state_changed();
1490 bv->owner()->updateMenubar();
1491 bv->owner()->updateToolbar();
1493 // Did we hit an editable inset?
1495 selection_possible = false;
1497 // if we reach this point with a selection, it
1498 // must mean we are currently selecting.
1499 // But we don't want to open the inset
1500 // because that is annoying for the user.
1501 // So just pretend we didn't hit it.
1502 // this is OK because a "kosher" ButtonRelease
1503 // will follow a ButtonPress that clears
1505 // Note this also fixes selection drawing
1506 // problems if we end up opening an inset
1507 if (selection.set())
1510 // CHECK fix this proper in 0.13
1511 // well, maybe 13.0 !!!!!!!!!
1513 // Following a ref shouldn't issue
1514 // a push on the undo-stack
1515 // anylonger, now that we have
1516 // keybindings for following
1517 // references and returning from
1518 // references. IMHO though, it
1519 // should be the inset's own business
1520 // to push or not push on the undo
1521 // stack. They don't *have* to
1522 // alter the document...
1524 // ...or maybe the SetCursorParUndo()
1525 // below isn't necessary at all anylonger?
1526 if (inset_hit->lyxCode() == Inset::REF_CODE)
1527 setCursorParUndo(bv);
1529 bv->owner()->message(inset_hit->editMessage());
1531 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1532 inset_hit->localDispatch(cmd1);
1538 case LFUN_SELFINSERT: {
1539 if (cmd.argument.empty())
1542 // Automatically delete the currently selected
1543 // text and replace it with what is being
1544 // typed in now. Depends on lyxrc settings
1545 // "auto_region_delete", which defaults to
1548 if (lyxrc.auto_region_delete) {
1549 if (selection.set()) {
1550 cutSelection(bv, false, false);
1553 bv->haveSelection(false);
1556 bv->beforeChange(this);
1557 LyXFont const old_font(real_current_font);
1559 string::const_iterator cit = cmd.argument.begin();
1560 string::const_iterator end = cmd.argument.end();
1561 for (; cit != end; ++cit)
1562 bv->owner()->getIntl().getTransManager().
1563 TranslateAndInsert(*cit, this);
1566 selection.cursor = cursor;
1567 moveCursorUpdate(bv, false);
1569 // real_current_font.number can change so we need to
1570 // update the minibuffer
1571 if (old_font != real_current_font)
1572 bv->owner()->view_state_changed();
1576 case LFUN_HTMLURL: {
1577 InsetCommandParams p("htmlurl");
1578 string const data = InsetCommandMailer::params2string("url", p);
1579 bv->owner()->getDialogs().show("url", data, 0);
1584 InsetCommandParams p("url");
1585 string const data = InsetCommandMailer::params2string("url", p);
1586 bv->owner()->getDialogs().show("url", data, 0);
1592 case LFUN_INSET_LIST:
1593 case LFUN_INSET_THEOREM:
1594 case LFUN_INSET_CAPTION:
1596 case LFUN_INSERT_NOTE:
1597 case LFUN_INSERT_BIBITEM:
1598 case LFUN_INSET_ERT:
1599 case LFUN_INSET_FLOAT:
1600 case LFUN_INSET_FOOTNOTE:
1601 case LFUN_INSET_MARGINAL:
1602 case LFUN_INSET_MINIPAGE:
1603 case LFUN_INSET_OPTARG:
1604 case LFUN_INSET_WIDE_FLOAT:
1605 case LFUN_INSET_WRAP:
1606 case LFUN_TABULAR_INSERT:
1607 // Open the inset, and move the current selection
1609 doInsertInset(this, cmd, true, true);
1612 case LFUN_INDEX_INSERT:
1613 // Just open the inset
1614 doInsertInset(this, cmd, true, false);
1617 case LFUN_INDEX_PRINT:
1618 case LFUN_PARENTINSERT:
1619 case LFUN_TOC_INSERT:
1621 doInsertInset(this, cmd, false, false);
1625 return UNDISPATCHED;