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 "insets/insetnewline.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);
67 if (lt->isTopLevel()) {
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 + top_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->top_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->top_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() > top_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 > top_y() + bv->workHeight()) {
302 bv->screen().draw(bv->text, bv, bv->text->top_y() + bv->workHeight());
303 bv->updateScrollbar();
308 int y = top_y() + bv->workHeight();
309 if (inset_owner && !top_y()) {
310 y -= (bv->text->cursor.iy()
312 + bv->theLockingInset()->insetInInsetY());
317 Row * cursorrow = cursor.row();
318 setCursorFromCoordinates(bv, cursor.x_fix(), y);
319 // + bv->workHeight());
323 if (cursorrow == bv->text->cursor.row()) {
324 // we have a row which is taller than the workarea. The
325 // simplest solution is to move to the next row instead.
326 cursorDown(bv, true);
328 // This is what we used to do, so we wouldn't skip right past
329 // tall rows, but it's not working right now.
331 new_y = bv->text->top_y() + bv->workHeight();
335 new_y = bv->text->cursor.iy()
336 + bv->theLockingInset()->insetInInsetY()
337 + y - cursor.row()->baseline();
339 new_y = cursor.y() - cursor.row()->baseline();
342 bv->screen().draw(bv->text, bv, new_y);
343 if (cursor.row()->next()) {
345 setCursor(bv, cur, cursor.row()->next()->par(),
346 cursor.row()->next()->pos(), false);
347 if (cur.y() < top_y() + bv->workHeight()) {
348 cursorDown(bv, true);
351 bv->updateScrollbar();
355 void LyXText::update(BufferView * bv, bool changed)
357 BufferView::UpdateCodes c = BufferView::SELECT | BufferView::FITCUR;
359 bv->update(this, c | BufferView::CHANGE);
366 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
370 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
371 if (!bv->insertInset(new_inset))
374 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));
403 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
405 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
406 <<"] arg[" << cmd.argument << ']' << endl;
408 BufferView * bv = cmd.view();
410 switch (cmd.action) {
412 case LFUN_APPENDIX: {
413 Paragraph * par = cursor.par();
414 bool start = !par->params().startOfAppendix();
416 // ensure that we have only one start_of_appendix in this document
417 Paragraph * tmp = ownerParagraph();
418 for (; tmp; tmp = tmp->next()) {
419 if (tmp->params().startOfAppendix()) {
420 setUndo(bv, Undo::EDIT, tmp, tmp->next());
421 tmp->params().startOfAppendix(false);
423 setHeightOfRow(bv, getRow(tmp, 0, tmpy));
428 setUndo(bv, Undo::EDIT, par, par->next());
429 par->params().startOfAppendix(start);
431 // we can set the refreshing parameters now
432 status(cmd.view(), LyXText::NEED_MORE_REFRESH);
433 updateCounters(cmd.view());
434 redoHeightOfParagraph(bv, cursor);
437 setCursor(cmd.view(), cursor.par(), cursor.pos());
442 case LFUN_DELETE_WORD_FORWARD:
443 bv->beforeChange(this);
445 deleteWordForward(bv);
450 case LFUN_DELETE_WORD_BACKWARD:
451 bv->beforeChange(this);
453 deleteWordBackward(bv);
458 case LFUN_DELETE_LINE_FORWARD:
459 bv->beforeChange(this);
461 deleteLineForward(bv);
468 if (!selection.mark())
469 bv->beforeChange(this);
476 if (!selection.mark())
477 bv->beforeChange(this);
479 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
480 cursorLeftOneWord(bv);
482 cursorRightOneWord(bv);
487 if (!selection.mark())
488 bv->beforeChange(this);
490 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
491 cursorRightOneWord(bv);
493 cursorLeftOneWord(bv);
497 case LFUN_BEGINNINGBUF:
498 if (!selection.mark())
499 bv->beforeChange(this);
506 if (selection.mark())
507 bv->beforeChange(this);
515 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
519 finishChange(bv, true);
524 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
528 finishChange(bv, true);
534 finishChange(bv, true);
539 cursorDown(bv, true);
540 finishChange(bv, true);
543 case LFUN_UP_PARAGRAPHSEL:
545 cursorUpParagraph(bv);
546 finishChange(bv, true);
549 case LFUN_DOWN_PARAGRAPHSEL:
551 cursorDownParagraph(bv);
552 finishChange(bv, true);
558 finishChange(bv, true);
564 finishChange(bv, true);
570 finishChange(bv, true);
576 finishChange(bv, true);
579 case LFUN_WORDRIGHTSEL:
581 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
582 cursorLeftOneWord(bv);
584 cursorRightOneWord(bv);
585 finishChange(bv, true);
588 case LFUN_WORDLEFTSEL:
590 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
591 cursorRightOneWord(bv);
593 cursorLeftOneWord(bv);
594 finishChange(bv, true);
601 getWord(cur1, cur2, WHOLE_WORD);
602 setCursor(bv, cur1.par(), cur1.pos());
603 bv->beforeChange(this);
604 setCursor(bv, cur2.par(), cur2.pos());
605 finishChange(bv, true);
610 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
611 if (!selection.mark())
612 bv->beforeChange(this);
615 cursorLeft(bv, false);
616 if (cursor.pos() < cursor.par()->size()
617 && cursor.par()->isInset(cursor.pos())
618 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
619 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
620 cmd.message(tmpinset->editMessage());
621 tmpinset->edit(bv, !is_rtl);
625 cursorRight(bv, false);
631 // This is soooo ugly. Isn`t it possible to make
633 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
634 if (!selection.mark())
635 bv->beforeChange(this);
637 LyXCursor const cur = cursor;
639 cursorLeft(bv, false);
640 if ((is_rtl || cur != cursor) && // only if really moved!
641 cursor.pos() < cursor.par()->size() &&
642 cursor.par()->isInset(cursor.pos()) &&
643 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
644 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
645 cmd.message(tmpinset->editMessage());
646 tmpinset->edit(bv, is_rtl);
650 cursorRight(bv, false);
656 if (!selection.mark())
657 bv->beforeChange(this);
658 bv->update(this, BufferView::UPDATE);
664 if (!selection.mark())
665 bv->beforeChange(this);
666 bv->update(this, BufferView::UPDATE);
671 case LFUN_UP_PARAGRAPH:
672 if (!selection.mark())
673 bv->beforeChange(this);
674 bv->update(this, BufferView::UPDATE);
675 cursorUpParagraph(bv);
679 case LFUN_DOWN_PARAGRAPH:
680 if (!selection.mark())
681 bv->beforeChange(this);
682 bv->update(this, BufferView::UPDATE);
683 cursorDownParagraph(bv);
684 finishChange(bv, false);
688 if (!selection.mark())
689 bv->beforeChange(this);
690 bv->update(this, BufferView::UPDATE);
692 finishChange(bv, false);
695 // moveCursorUpdate(bv, false, false);
696 // owner_->view_state_changed();
700 if (!selection.mark())
701 bv->beforeChange(this);
702 bv->update(this, BufferView::UPDATE);
704 finishChange(bv, false);
708 if (!selection.mark())
709 bv->beforeChange(this);
712 finishChange(bv, false);
716 if (!selection.mark())
717 bv->beforeChange(this);
720 finishChange(bv, false);
723 case LFUN_BREAKLINE: {
724 lyx::pos_type body = cursor.par()->beginningOfBody();
726 // Not allowed by LaTeX (labels or empty par)
727 if (cursor.pos() <= body)
730 bv->beforeChange(this);
731 insertInset(bv, new InsetNewline);
733 setCursor(bv, cursor.par(), cursor.pos());
734 moveCursorUpdate(bv, false);
739 if (!selection.set()) {
741 selection.cursor = cursor;
743 // It is possible to make it a lot faster still
744 // just comment out the line below...
748 cutSelection(bv, true);
751 moveCursorUpdate(bv, false);
752 bv->owner()->view_state_changed();
756 case LFUN_DELETE_SKIP:
757 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
758 if (!selection.set()) {
759 LyXCursor cur = cursor;
760 if (cur.pos() == cur.par()->size()) {
764 && !(cur.par()->params().spaceTop()
765 == VSpace (VSpace::NONE))) {
767 cur.par()->params().lineTop(),
768 cur.par()->params().lineBottom(),
769 cur.par()->params().pagebreakTop(),
770 cur.par()->params().pagebreakBottom(),
771 VSpace(VSpace::NONE),
772 cur.par()->params().spaceBottom(),
773 cur.par()->params().spacing(),
774 cur.par()->params().align(),
775 cur.par()->params().labelWidthString(), 0);
781 selection.cursor = cursor;
785 selection.cursor = cursor;
789 cutSelection(bv, true);
796 if (!selection.set()) {
797 if (bv->owner()->getIntl().getTransManager().backspace()) {
799 selection.cursor = cursor;
801 // It is possible to make it a lot faster still
802 // just comment out the line below...
807 cutSelection(bv, true);
810 bv->owner()->view_state_changed();
814 case LFUN_BACKSPACE_SKIP:
815 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
816 if (!selection.set()) {
817 LyXCursor cur = cursor;
819 && !(cur.par()->params().spaceTop()
820 == VSpace (VSpace::NONE))) {
822 cur.par()->params().lineTop(),
823 cur.par()->params().lineBottom(),
824 cur.par()->params().pagebreakTop(),
825 cur.par()->params().pagebreakBottom(),
826 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
827 cur.par()->params().spacing(),
828 cur.par()->params().align(),
829 cur.par()->params().labelWidthString(), 0);
832 selection.cursor = cur;
836 cutSelection(bv, true);
841 case LFUN_BREAKPARAGRAPH:
842 bv->beforeChange(this);
843 breakParagraph(bv, bv->buffer()->paragraphs, 0);
845 selection.cursor = cursor;
847 bv->owner()->view_state_changed();
850 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
851 bv->beforeChange(this);
852 breakParagraph(bv, bv->buffer()->paragraphs, 1);
854 selection.cursor = cursor;
856 bv->owner()->view_state_changed();
859 case LFUN_BREAKPARAGRAPH_SKIP: {
860 // When at the beginning of a paragraph, remove
861 // indentation and add a "defskip" at the top.
862 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
863 LyXCursor cur = cursor;
864 bv->beforeChange(this);
865 if (cur.pos() == 0) {
866 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
868 cur.par()->params().lineTop(),
869 cur.par()->params().lineBottom(),
870 cur.par()->params().pagebreakTop(),
871 cur.par()->params().pagebreakBottom(),
872 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
873 cur.par()->params().spacing(),
874 cur.par()->params().align(),
875 cur.par()->params().labelWidthString(), 1);
880 breakParagraph(bv, bv->buffer()->paragraphs, 0);
884 selection.cursor = cur;
886 bv->owner()->view_state_changed();
890 case LFUN_PARAGRAPH_SPACING: {
891 Paragraph * par = cursor.par();
892 Spacing::Space cur_spacing = par->params().spacing().getSpace();
893 float cur_value = 1.0;
894 if (cur_spacing == Spacing::Other)
895 cur_value = par->params().spacing().getValue();
897 istringstream is(cmd.argument.c_str());
900 Spacing::Space new_spacing = cur_spacing;
901 float new_value = cur_value;
903 lyxerr << "Missing argument to `paragraph-spacing'"
905 } else if (tmp == "single") {
906 new_spacing = Spacing::Single;
907 } else if (tmp == "onehalf") {
908 new_spacing = Spacing::Onehalf;
909 } else if (tmp == "double") {
910 new_spacing = Spacing::Double;
911 } else if (tmp == "other") {
912 new_spacing = Spacing::Other;
915 lyxerr << "new_value = " << tmpval << endl;
918 } else if (tmp == "default") {
919 new_spacing = Spacing::Default;
921 lyxerr << _("Unknown spacing argument: ")
922 << cmd.argument << endl;
924 if (cur_spacing != new_spacing || cur_value != new_value) {
925 par->params().spacing(Spacing(new_spacing, new_value));
932 case LFUN_INSET_TOGGLE:
934 bv->beforeChange(this);
941 case LFUN_PROTECTEDSPACE:
942 if (cursor.par()->layout()->free_spacing) {
946 specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
948 moveCursorUpdate(bv, false);
951 case LFUN_HYPHENATION:
952 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
955 case LFUN_LIGATURE_BREAK:
956 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
960 specialChar(this, bv, InsetSpecialChar::LDOTS);
963 case LFUN_END_OF_SENTENCE:
964 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
967 case LFUN_MENU_SEPARATOR:
968 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
972 bv->beforeChange(this);
974 selection.cursor = cursor;
975 cmd.message(N_("Mark off"));
979 bv->beforeChange(this);
980 selection.mark(true);
982 selection.cursor = cursor;
983 cmd.message(N_("Mark on"));
987 bv->beforeChange(this);
988 if (selection.mark()) {
990 cmd.message(N_("Mark removed"));
992 selection.mark(true);
994 cmd.message(N_("Mark set"));
996 selection.cursor = cursor;
999 case LFUN_UPCASE_WORD:
1001 changeCase(*bv, LyXText::text_uppercase);
1003 bv->updateInset(inset_owner, true);
1007 case LFUN_LOWCASE_WORD:
1009 changeCase(*bv, LyXText::text_lowercase);
1011 bv->updateInset(inset_owner, true);
1015 case LFUN_CAPITALIZE_WORD:
1017 changeCase(*bv, LyXText::text_capitalization);
1019 bv->updateInset(inset_owner, true);
1023 case LFUN_TRANSPOSE_CHARS:
1025 transposeChars(*bv);
1027 bv->updateInset(inset_owner, true);
1032 cmd.message(_("Paste"));
1034 // clear the selection
1035 bv->toggleSelection();
1039 clearSelection(); // bug 393
1048 cutSelection(bv, true);
1050 cmd.message(_("Cut"));
1055 cmd.message(_("Copy"));
1058 case LFUN_BEGINNINGBUFSEL:
1060 return UNDISPATCHED;
1063 finishChange(bv, true);
1066 case LFUN_ENDBUFSEL:
1068 return UNDISPATCHED;
1071 finishChange(bv, true);
1075 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1081 istringstream is(cmd.argument.c_str());
1084 lyxerr << "SETXY: Could not parse coordinates in '"
1085 << cmd.argument << std::endl;
1087 setCursorFromCoordinates(bv, x, y);
1092 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1094 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1100 case LFUN_GETLAYOUT:
1101 cmd.message(tostr(cursor.par()->layout()));
1105 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1106 << cmd.argument << endl;
1108 // This is not the good solution to the empty argument
1109 // problem, but it will hopefully suffice for 1.2.0.
1110 // The correct solution would be to augument the
1111 // function list/array with information about what
1112 // functions needs arguments and their type.
1113 if (cmd.argument.empty()) {
1114 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1118 // Derive layout number from given argument (string)
1119 // and current buffer's textclass (number)
1120 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1121 bool hasLayout = tclass.hasLayout(cmd.argument);
1122 string layout = cmd.argument;
1124 // If the entry is obsolete, use the new one instead.
1126 string const & obs = tclass[layout]->obsoleted_by();
1132 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1137 bool change_layout = (current_layout != layout);
1138 if (!change_layout && selection.set() &&
1139 selection.start.par() != selection.end.par())
1141 Paragraph * spar = selection.start.par();
1142 Paragraph * epar = selection.end.par()->next();
1143 while (spar != epar) {
1144 if (spar->layout()->name() != current_layout) {
1145 change_layout = true;
1148 spar = spar->next();
1151 if (change_layout) {
1153 current_layout = layout;
1155 setLayout(bv, layout);
1156 bv->owner()->setLayout(layout);
1163 case LFUN_PASTESELECTION: {
1167 // this was originally a beforeChange(bv->text), i.e
1168 // the outermost LyXText!
1169 bv->beforeChange(this);
1170 string const clip = bv->getClipboard();
1171 if (!clip.empty()) {
1172 if (cmd.argument == "paragraph")
1173 insertStringAsParagraphs(bv, clip);
1175 insertStringAsLines(bv, clip);
1182 case LFUN_GOTOERROR:
1183 gotoInset(bv, Inset::ERROR_CODE, false);
1187 gotoInset(bv, Inset::NOTE_CODE, false);
1190 case LFUN_REFERENCE_GOTO:
1192 vector<Inset::Code> tmp;
1193 tmp.push_back(Inset::LABEL_CODE);
1194 tmp.push_back(Inset::REF_CODE);
1195 gotoInset(bv, tmp, true);
1200 Paragraph const * par = cursor.par();
1201 lyx::pos_type pos = cursor.pos();
1205 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1208 c = par->getChar(pos - 1);
1211 LyXLayout_ptr const & style = par->layout();
1213 if (style->pass_thru ||
1214 par->getFontSettings(bv->buffer()->params,
1215 pos).language()->lang() == "hebrew" ||
1216 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1217 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1221 case LFUN_DATE_INSERT: {
1222 time_t now_time_t = time(NULL);
1223 struct tm * now_tm = localtime(&now_time_t);
1224 setlocale(LC_TIME, "");
1226 if (!cmd.argument.empty())
1229 arg = lyxrc.date_insert_format;
1231 int const datetmp_len =
1232 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1234 for (int i = 0; i < datetmp_len; i++) {
1235 insertChar(bv, datetmp[i]);
1238 selection.cursor = cursor;
1239 moveCursorUpdate(bv, false);
1243 case LFUN_MOUSE_TRIPLE:
1246 if (isTopLevel() && bv->theLockingInset())
1248 if (cmd.button() == mouse_button::button1) {
1250 bv->screen().hideCursor();
1251 bv->screen().toggleSelection(this, bv);
1254 selection.cursor = cursor;
1258 bv->screen().toggleSelection(this, bv, false);
1260 bv->haveSelection(selection.set());
1264 case LFUN_MOUSE_DOUBLE:
1267 if (isTopLevel() && bv->theLockingInset())
1269 if (cmd.button() == mouse_button::button1) {
1271 bv->screen().hideCursor();
1272 bv->screen().toggleSelection(this, bv);
1273 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1274 bv->screen().toggleSelection(this, bv, false);
1276 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1279 bv->haveSelection(selection.set());
1283 case LFUN_MOUSE_MOTION:
1285 // Only use motion with button 1
1286 //if (ev.button() != mouse_button::button1)
1292 // Check for inset locking
1293 if (bv->theLockingInset()) {
1294 Inset * tli = bv->theLockingInset();
1295 LyXCursor cursor = bv->text->cursor;
1296 LyXFont font = bv->text->getFont(bv->buffer(),
1297 cursor.par(), cursor.pos());
1298 int width = tli->width(bv, font);
1299 int inset_x = font.isVisibleRightToLeft()
1300 ? cursor.ix() - width : cursor.ix();
1301 int start_x = inset_x + tli->scroll();
1302 FuncRequest cmd1 = cmd;
1303 cmd1.x = cmd.x - start_x;
1304 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1305 tli->localDispatch(cmd1);
1309 // The test for not selection possible is needed, that only motion
1310 // events are used, where the bottom press event was on
1311 // the drawing area too
1312 if (!selection_possible) {
1313 lyxerr[Debug::ACTION]
1314 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1318 bv->screen().hideCursor();
1320 Row * cursorrow = bv->text->cursor.row();
1321 bv->text->setCursorFromCoordinates(bv, cmd.x, cmd.y + bv->text->top_y());
1323 // sorry for this but I have a strange error that the y value jumps at
1324 // a certain point. This seems like an error in my xforms library or
1325 // in some other local environment, but I would like to leave this here
1326 // for the moment until I can remove this (Jug 20020418)
1327 if (y_before < bv->text->cursor.y())
1328 lyxerr << y_before << ':'
1329 << bv->text->cursor.y() << endl;
1331 // This is to allow jumping over large insets
1332 if (cursorrow == bv->text->cursor.row()) {
1333 if (cmd.y >= bv->workHeight())
1334 bv->text->cursorDown(bv, false);
1336 bv->text->cursorUp(bv, false);
1339 // Maybe an empty line was deleted
1340 if (!bv->text->selection.set())
1341 bv->update(bv->text, BufferView::UPDATE);
1342 bv->text->setSelection(bv);
1343 bv->screen().toggleToggle(bv->text, bv);
1349 // Single-click on work area
1350 case LFUN_MOUSE_PRESS:
1355 // ok ok, this is a hack (for xforms)
1356 // We shouldn't go further down as we really should only do the
1357 // scrolling and be done with this. Otherwise we may open some
1358 // dialogs (Jug 20020424).
1359 if (cmd.button() == mouse_button::button4) {
1360 bv->scroll(-lyxrc.wheel_jump);
1363 if (cmd.button() == mouse_button::button5) {
1364 bv->scroll(lyxrc.wheel_jump);
1370 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1372 // Middle button press pastes if we have a selection
1373 // We do this here as if the selection was inside an inset
1374 // it could get cleared on the unlocking of the inset so
1375 // we have to check this first
1376 bool paste_internally = false;
1377 if (cmd.button() == mouse_button::button2 && selection.set()) {
1378 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1379 paste_internally = true;
1382 int const screen_first = bv->text->top_y();
1384 if (bv->theLockingInset()) {
1385 // We are in inset locking mode
1387 // Check whether the inset was hit. If not reset mode,
1388 // otherwise give the event to the inset
1389 if (inset_hit == bv->theLockingInset()) {
1390 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1391 bv->theLockingInset()->localDispatch(cmd1);
1394 bv->unlockInset(bv->theLockingInset());
1398 selection_possible = true;
1399 bv->screen().hideCursor();
1401 // Clear the selection
1402 bv->screen().toggleSelection(bv->text, bv);
1403 bv->text->clearSelection();
1404 bv->text->fullRebreak(bv);
1406 bv->updateScrollbar();
1408 // Single left click in math inset?
1409 if (isHighlyEditableInset(inset_hit)) {
1410 // Highly editable inset, like math
1411 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1412 selection_possible = false;
1413 bv->owner()->updateLayoutChoice();
1414 bv->owner()->message(inset->editMessage());
1415 //inset->edit(bv, x, y, cmd.button());
1416 // We just have to lock the inset before calling a PressEvent on it!
1417 // we don't need the edit() call here! (Jug20020329)
1418 if (!bv->lockInset(inset))
1419 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1420 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1421 inset->localDispatch(cmd1);
1424 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1426 // Right click on a footnote flag opens float menu
1427 if (cmd.button() == mouse_button::button3) {
1428 selection_possible = false;
1432 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1433 bv->text->setCursorFromCoordinates(bv, x, y + screen_first);
1435 bv->text->selection.cursor = bv->text->cursor;
1436 bv->text->cursor.x_fix(bv->text->cursor.x());
1438 bv->owner()->updateLayoutChoice();
1439 if (bv->fitCursor())
1440 selection_possible = false;
1442 // Insert primary selection with middle mouse
1443 // if there is a local selection in the current buffer,
1445 if (cmd.button() == mouse_button::button2) {
1446 if (paste_internally)
1447 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1449 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1450 selection_possible = false;
1455 case LFUN_MOUSE_RELEASE:
1457 // do nothing if we used the mouse wheel
1461 if (cmd.button() == mouse_button::button4
1462 || cmd.button() == mouse_button::button5)
1465 // If we hit an inset, we have the inset coordinates in these
1466 // and inset_hit points to the inset. If we do not hit an
1467 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1470 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1472 if (bv->theLockingInset()) {
1473 // We are in inset locking mode.
1475 // LyX does a kind of work-area grabbing for insets.
1476 // Only a ButtonPress FuncRequest outside the inset will
1477 // force a insetUnlock.
1478 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1479 bv->theLockingInset()->localDispatch(cmd1);
1483 selection_possible = false;
1485 if (cmd.button() == mouse_button::button2)
1489 if (cmd.button() == mouse_button::button1)
1490 bv->haveSelection(selection.set());
1493 bv->owner()->view_state_changed();
1494 bv->owner()->updateMenubar();
1495 bv->owner()->updateToolbar();
1497 // Did we hit an editable inset?
1499 selection_possible = false;
1501 // if we reach this point with a selection, it
1502 // must mean we are currently selecting.
1503 // But we don't want to open the inset
1504 // because that is annoying for the user.
1505 // So just pretend we didn't hit it.
1506 // this is OK because a "kosher" ButtonRelease
1507 // will follow a ButtonPress that clears
1509 // Note this also fixes selection drawing
1510 // problems if we end up opening an inset
1511 if (selection.set())
1514 // CHECK fix this proper in 0.13
1515 // well, maybe 13.0 !!!!!!!!!
1517 // Following a ref shouldn't issue
1518 // a push on the undo-stack
1519 // anylonger, now that we have
1520 // keybindings for following
1521 // references and returning from
1522 // references. IMHO though, it
1523 // should be the inset's own business
1524 // to push or not push on the undo
1525 // stack. They don't *have* to
1526 // alter the document...
1528 // ...or maybe the SetCursorParUndo()
1529 // below isn't necessary at all anylonger?
1530 if (inset_hit->lyxCode() == Inset::REF_CODE)
1531 setCursorParUndo(bv);
1533 bv->owner()->message(inset_hit->editMessage());
1535 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1536 inset_hit->localDispatch(cmd1);
1542 case LFUN_SELFINSERT: {
1543 if (cmd.argument.empty())
1546 // Automatically delete the currently selected
1547 // text and replace it with what is being
1548 // typed in now. Depends on lyxrc settings
1549 // "auto_region_delete", which defaults to
1552 if (lyxrc.auto_region_delete) {
1553 if (selection.set()) {
1554 cutSelection(bv, false, false);
1557 bv->haveSelection(false);
1560 bv->beforeChange(this);
1561 LyXFont const old_font(real_current_font);
1563 string::const_iterator cit = cmd.argument.begin();
1564 string::const_iterator end = cmd.argument.end();
1565 for (; cit != end; ++cit)
1566 bv->owner()->getIntl().getTransManager().
1567 TranslateAndInsert(*cit, this);
1570 selection.cursor = cursor;
1571 moveCursorUpdate(bv, false);
1573 // real_current_font.number can change so we need to
1574 // update the minibuffer
1575 if (old_font != real_current_font)
1576 bv->owner()->view_state_changed();
1580 case LFUN_HTMLURL: {
1581 InsetCommandParams p("htmlurl");
1582 string const data = InsetCommandMailer::params2string("url", p);
1583 bv->owner()->getDialogs().show("url", data, 0);
1588 InsetCommandParams p("url");
1589 string const data = InsetCommandMailer::params2string("url", p);
1590 bv->owner()->getDialogs().show("url", data, 0);
1596 case LFUN_INSET_LIST:
1597 case LFUN_INSET_THEOREM:
1598 case LFUN_INSET_CAPTION:
1600 case LFUN_INSERT_NOTE:
1601 case LFUN_INSERT_BIBITEM:
1602 case LFUN_INSET_ERT:
1603 case LFUN_INSET_FLOAT:
1604 case LFUN_INSET_FOOTNOTE:
1605 case LFUN_INSET_MARGINAL:
1606 case LFUN_INSET_MINIPAGE:
1607 case LFUN_INSET_OPTARG:
1608 case LFUN_INSET_WIDE_FLOAT:
1609 case LFUN_INSET_WRAP:
1610 case LFUN_TABULAR_INSERT:
1611 case LFUN_ENVIRONMENT_INSERT:
1612 // Open the inset, and move the current selection
1614 doInsertInset(this, cmd, true, true);
1617 case LFUN_INDEX_INSERT:
1618 // Just open the inset
1619 doInsertInset(this, cmd, true, false);
1622 case LFUN_INDEX_PRINT:
1623 case LFUN_PARENTINSERT:
1624 case LFUN_TOC_INSERT:
1627 doInsertInset(this, cmd, false, false);
1631 return UNDISPATCHED;