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"
21 #include "bufferparams.h"
23 #include "ParagraphParameters.h"
29 #include "support/tostr.h"
30 #include "support/lstrings.h"
31 #include "frontends/LyXView.h"
32 #include "frontends/screen.h"
33 #include "frontends/Dialogs.h"
34 #include "insets/insetspecialchar.h"
35 #include "insets/insettext.h"
36 #include "insets/insetquotes.h"
37 #include "insets/insetcommand.h"
38 #include "insets/insetnewline.h"
39 #include "undo_funcs.h"
40 #include "text_funcs.h"
46 using namespace lyx::support;
53 extern string current_layout;
54 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
56 // the selection possible is needed, that only motion events are
57 // used, where the bottom press event was on the drawing area too
58 bool selection_possible = false;
63 void moveCursorUpdate(BufferView * bv, bool selecting)
65 LyXText * lt = bv->getLyXText();
67 if (selecting || lt->selection.mark()) {
70 bv->updateInset(lt->inset_owner);
74 if (!lt->isInInset()) {
75 bv->update(lt, BufferView::SELECT);
76 } else if (bv->text->refreshStatus() != LyXText::REFRESH_NONE) {
77 bv->update(BufferView::SELECT);
80 if (!lt->selection.set())
81 bv->haveSelection(false);
87 void finishChange(BufferView * bv, bool selecting = false)
90 moveCursorUpdate(bv, selecting);
91 bv->owner()->view_state_changed();
94 // check if the given co-ordinates are inside an inset at the
95 // given cursor, if one exists. If so, the inset is returned,
96 // and the co-ordinates are made relative. Otherwise, 0 is returned.
97 Inset * checkInset(BufferView * bv, LyXText & text,
98 LyXCursor const & cur, int & x, int & y)
100 lyx::pos_type const pos = cur.pos();
101 ParagraphList::iterator par = cur.par();
103 if (pos >= par->size() || !par->isInset(pos))
106 Inset /*const*/ * inset = par->getInset(pos);
108 if (!isEditableInset(inset))
111 // get inset dimensions
112 Assert(par->getInset(pos));
114 LyXFont const & font = text.getFont(bv->buffer(), par, pos);
116 int const width = inset->width(bv, font);
117 int const inset_x = font.isVisibleRightToLeft()
118 ? (cur.ix() - width) : cur.ix();
121 inset_x + inset->scroll(),
123 cur.iy() - inset->ascent(bv, font),
124 cur.iy() + inset->descent(bv, font)
127 if (!b.contained(x, y)) {
128 lyxerr[Debug::GUI] << "Missed inset at x,y "
130 << " box " << b << endl;
134 text.setCursor(par, pos, true);
137 // The origin of an inset is on the baseline
138 y -= text.cursor.iy();
146 Inset * LyXText::checkInsetHit(int & x, int & y)
148 int y_tmp = y + top_y();
151 setCursorFromCoordinates(cur, x, y_tmp);
153 Inset * inset = checkInset(bv(), *this, cur, x, y_tmp);
159 // look at previous position
164 setCursor(cur, cur.par(), cur.pos() - 1, true);
166 inset = checkInset(bv(), *this, cur, x, y_tmp);
173 bool LyXText::gotoNextInset(vector<Inset::Code> const & codes,
174 string const & contents)
176 ParagraphList::iterator end = ownerParagraphs().end();
177 ParagraphList::iterator pit = cursor.par();
178 pos_type pos = cursor.pos();
182 if (pos + 1 < pit->size()) {
189 } while (pit != end &&
190 !(pit->isInset(pos) &&
191 (inset = pit->getInset(pos)) != 0 &&
192 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
194 static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
198 setCursor(pit, pos, false);
205 void LyXText::gotoInset(vector<Inset::Code> const & codes,
208 bv()->beforeChange(this);
212 if (same_content && cursor.pos() < cursor.par()->size()
213 && cursor.par()->isInset(cursor.pos())) {
214 Inset const * inset = cursor.par()->getInset(cursor.pos());
215 if (find(codes.begin(), codes.end(), inset->lyxCode())
217 contents = static_cast<InsetCommand const *>(inset)->getContents();
220 if (!gotoNextInset(codes, contents)) {
221 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
222 LyXCursor tmp = cursor;
223 cursor.par(ownerParagraphs().begin());
225 if (!gotoNextInset(codes, contents)) {
227 bv()->owner()->message(_("No more insets"));
230 bv()->owner()->message(_("No more insets"));
234 selection.cursor = cursor;
238 void LyXText::gotoInset(Inset::Code code, bool same_content)
240 gotoInset(vector<Inset::Code>(1, code), same_content);
244 void LyXText::cursorPrevious()
248 if (cursorRow() == rows().begin()) {
250 int new_y = bv()->text->top_y() - bv()->workHeight();
251 bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
252 bv()->updateScrollbar();
257 RowList::iterator cursorrow = cursorRow();
259 setCursorFromCoordinates(cursor.x_fix(), y);
263 if (cursorrow == bv()->text->cursorRow()) {
264 // we have a row which is taller than the workarea. The
265 // simplest solution is to move to the previous row instead.
268 // This is what we used to do, so we wouldn't skip right past
269 // tall rows, but it's not working right now.
271 new_y = bv->text->top_y() - bv->workHeight();
275 new_y = bv()->text->cursor.iy()
276 + bv()->theLockingInset()->insetInInsetY() + y
277 + cursorRow()->height()
278 - bv()->workHeight() + 1;
281 - cursorRow()->baseline()
282 + cursorRow()->height()
283 - bv()->workHeight() + 1;
286 bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
287 if (cursorRow() != rows().begin()) {
289 setCursor(cur, boost::prior(cursorRow())->par(),
290 boost::prior(cursorRow())->pos(), false);
291 if (cur.y() > top_y()) {
295 bv()->updateScrollbar();
299 void LyXText::cursorNext()
303 if (boost::next(cursorRow()) == rows().end()) {
304 int y = cursor.y() - cursorRow()->baseline() +
305 cursorRow()->height();
306 if (y > topy + bv()->workHeight()) {
307 bv()->screen().draw(bv()->text, bv(), bv()->text->top_y() + bv()->workHeight());
308 bv()->updateScrollbar();
313 int y = topy + bv()->workHeight();
314 if (inset_owner && !topy) {
315 y -= (bv()->text->cursor.iy()
316 - bv()->text->top_y()
317 + bv()->theLockingInset()->insetInInsetY());
322 RowList::iterator cursorrow = cursorRow();
323 setCursorFromCoordinates(cursor.x_fix(), y);
324 // + bv->workHeight());
328 if (cursorrow == bv()->text->cursorRow()) {
329 // we have a row which is taller than the workarea. The
330 // simplest solution is to move to the next row instead.
333 // This is what we used to do, so we wouldn't skip right past
334 // tall rows, but it's not working right now.
336 new_y = bv->text->top_y() + bv->workHeight();
340 new_y = bv()->text->cursor.iy()
341 + bv()->theLockingInset()->insetInInsetY()
342 + y - cursorRow()->baseline();
344 new_y = cursor.y() - cursorRow()->baseline();
347 bv()->screen().draw(bv()->text, bv(), new_y);
349 RowList::iterator next_row = boost::next(cursorRow());
350 if (next_row != rows().end()) {
352 setCursor(cur, next_row->par(), next_row->pos(), false);
353 if (cur.y() < top_y() + bv()->workHeight()) {
357 bv()->updateScrollbar();
361 void LyXText::update()
363 bv()->update(this, BufferView::SELECT);
368 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
371 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
372 if (!bv->insertInset(new_inset))
375 bv->updateInset(new_inset);
379 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
380 bool edit, bool pastesel)
382 Inset * inset = createInset(cmd);
383 BufferView * bv = cmd.view();
387 if (lt->selection.set()) {
388 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
391 if (bv->insertInset(inset)) {
393 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
394 inset->localDispatch(cmd);
396 if (gotsel && pastesel)
397 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
406 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
408 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
409 <<"] arg[" << cmd.argument << ']' << endl;
411 BufferView * bv = cmd.view();
413 switch (cmd.action) {
415 case LFUN_APPENDIX: {
416 ParagraphList::iterator pit = cursor.par();
417 bool start = !pit->params().startOfAppendix();
419 // ensure that we have only one start_of_appendix in this document
420 ParagraphList::iterator tmp = ownerParagraphs().begin();
421 ParagraphList::iterator end = ownerParagraphs().end();
423 for (; tmp != end; ++tmp) {
424 if (tmp->params().startOfAppendix()) {
425 setUndo(bv, Undo::EDIT, tmp);
426 tmp->params().startOfAppendix(false);
428 setHeightOfRow(getRow(tmp, 0, tmpy));
433 setUndo(bv, Undo::EDIT, pit);
434 pit->params().startOfAppendix(start);
436 // we can set the refreshing parameters now
438 redoHeightOfParagraph();
440 setCursor(cursor.par(), cursor.pos());
445 case LFUN_DELETE_WORD_FORWARD:
446 bv->beforeChange(this);
453 case LFUN_DELETE_WORD_BACKWARD:
454 bv->beforeChange(this);
456 deleteWordBackward();
461 case LFUN_DELETE_LINE_FORWARD:
462 bv->beforeChange(this);
470 if (!selection.mark())
471 bv->beforeChange(this);
473 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
476 cursorRightOneWord();
481 if (!selection.mark())
482 bv->beforeChange(this);
484 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
485 cursorRightOneWord();
491 case LFUN_BEGINNINGBUF:
492 if (!selection.mark())
493 bv->beforeChange(this);
500 if (selection.mark())
501 bv->beforeChange(this);
509 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
513 finishChange(bv, true);
518 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
522 finishChange(bv, true);
528 finishChange(bv, true);
534 finishChange(bv, true);
537 case LFUN_UP_PARAGRAPHSEL:
540 finishChange(bv, true);
543 case LFUN_DOWN_PARAGRAPHSEL:
545 cursorDownParagraph();
546 finishChange(bv, true);
552 finishChange(bv, true);
558 finishChange(bv, true);
564 finishChange(bv, true);
570 finishChange(bv, true);
573 case LFUN_WORDRIGHTSEL:
575 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
578 cursorRightOneWord();
579 finishChange(bv, true);
582 case LFUN_WORDLEFTSEL:
584 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
585 cursorRightOneWord();
588 finishChange(bv, true);
593 LyXCursor cur1 = cursor;
595 ::getWord(cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs());
596 setCursor(cur1.par(), cur1.pos());
597 bv->beforeChange(this);
598 setCursor(cur2.par(), cur2.pos());
599 finishChange(bv, true);
604 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
605 if (!selection.mark())
606 bv->beforeChange(this);
610 if (cursor.pos() < cursor.par()->size()
611 && cursor.par()->isInset(cursor.pos())
612 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
613 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
614 cmd.message(tmpinset->editMessage());
615 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
616 tmpinset->localDispatch(cmd1);
626 // This is soooo ugly. Isn`t it possible to make
628 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
629 if (!selection.mark())
630 bv->beforeChange(this);
632 LyXCursor const cur = cursor;
635 if ((is_rtl || cur != cursor) && // only if really moved!
636 cursor.pos() < cursor.par()->size() &&
637 cursor.par()->isInset(cursor.pos()) &&
638 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
639 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
640 cmd.message(tmpinset->editMessage());
641 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
642 tmpinset->localDispatch(cmd1);
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);
675 case LFUN_DOWN_PARAGRAPH:
676 if (!selection.mark())
677 bv->beforeChange(this);
678 bv->update(this, BufferView::UPDATE);
679 cursorDownParagraph();
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);
719 case LFUN_BREAKLINE: {
720 lyx::pos_type body = cursor.par()->beginningOfBody();
722 // Not allowed by LaTeX (labels or empty par)
723 if (cursor.pos() <= body)
726 bv->beforeChange(this);
727 insertInset(new InsetNewline);
729 setCursor(cursor.par(), cursor.pos());
730 moveCursorUpdate(bv, false);
735 if (!selection.set()) {
737 selection.cursor = cursor;
739 // It is possible to make it a lot faster still
740 // just comment out the line below...
743 cutSelection(true, false);
746 moveCursorUpdate(bv, false);
747 bv->owner()->view_state_changed();
751 case LFUN_DELETE_SKIP:
752 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
753 if (!selection.set()) {
754 LyXCursor cur = cursor;
755 if (cur.pos() == cur.par()->size()) {
759 && !(cur.par()->params().spaceTop()
760 == VSpace (VSpace::NONE))) {
762 cur.par()->params().lineTop(),
763 cur.par()->params().lineBottom(),
764 cur.par()->params().pagebreakTop(),
765 cur.par()->params().pagebreakBottom(),
766 VSpace(VSpace::NONE),
767 cur.par()->params().spaceBottom(),
768 cur.par()->params().spacing(),
769 cur.par()->params().align(),
770 cur.par()->params().labelWidthString(), 0);
776 selection.cursor = cursor;
780 selection.cursor = cursor;
784 cutSelection(true, false);
791 if (!selection.set()) {
792 if (bv->owner()->getIntl().getTransManager().backspace()) {
794 selection.cursor = cursor;
796 // It is possible to make it a lot faster still
797 // just comment out the line below...
801 cutSelection(true, false);
804 bv->owner()->view_state_changed();
808 case LFUN_BACKSPACE_SKIP:
809 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
810 if (!selection.set()) {
811 LyXCursor cur = cursor;
813 && !(cur.par()->params().spaceTop()
814 == VSpace (VSpace::NONE))) {
816 cur.par()->params().lineTop(),
817 cur.par()->params().lineBottom(),
818 cur.par()->params().pagebreakTop(),
819 cur.par()->params().pagebreakBottom(),
820 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
821 cur.par()->params().spacing(),
822 cur.par()->params().align(),
823 cur.par()->params().labelWidthString(), 0);
826 selection.cursor = cur;
830 cutSelection(true, false);
835 case LFUN_BREAKPARAGRAPH:
836 bv->beforeChange(this);
837 breakParagraph(bv->buffer()->paragraphs, 0);
839 selection.cursor = cursor;
841 bv->owner()->view_state_changed();
844 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
845 bv->beforeChange(this);
846 breakParagraph(bv->buffer()->paragraphs, 1);
848 selection.cursor = cursor;
850 bv->owner()->view_state_changed();
853 case LFUN_BREAKPARAGRAPH_SKIP: {
854 // When at the beginning of a paragraph, remove
855 // indentation and add a "defskip" at the top.
856 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
857 LyXCursor cur = cursor;
858 bv->beforeChange(this);
859 if (cur.pos() == 0) {
860 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
862 cur.par()->params().lineTop(),
863 cur.par()->params().lineBottom(),
864 cur.par()->params().pagebreakTop(),
865 cur.par()->params().pagebreakBottom(),
866 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
867 cur.par()->params().spacing(),
868 cur.par()->params().align(),
869 cur.par()->params().labelWidthString(), 1);
873 breakParagraph(bv->buffer()->paragraphs, 0);
876 selection.cursor = cur;
878 bv->owner()->view_state_changed();
882 case LFUN_PARAGRAPH_SPACING: {
883 ParagraphList::iterator pit = cursor.par();
884 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
885 float cur_value = 1.0;
886 if (cur_spacing == Spacing::Other)
887 cur_value = pit->params().spacing().getValue();
889 istringstream is(STRCONV(cmd.argument));
892 Spacing::Space new_spacing = cur_spacing;
893 float new_value = cur_value;
895 lyxerr << "Missing argument to `paragraph-spacing'"
897 } else if (tmp == "single") {
898 new_spacing = Spacing::Single;
899 } else if (tmp == "onehalf") {
900 new_spacing = Spacing::Onehalf;
901 } else if (tmp == "double") {
902 new_spacing = Spacing::Double;
903 } else if (tmp == "other") {
904 new_spacing = Spacing::Other;
907 lyxerr << "new_value = " << tmpval << endl;
910 } else if (tmp == "default") {
911 new_spacing = Spacing::Default;
913 lyxerr << _("Unknown spacing argument: ")
914 << cmd.argument << endl;
916 if (cur_spacing != new_spacing || cur_value != new_value) {
917 pit->params().spacing(Spacing(new_spacing, new_value));
924 case LFUN_INSET_SETTINGS:
925 Assert(bv->theLockingInset());
926 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
929 case LFUN_INSET_TOGGLE:
930 bv->beforeChange(this);
937 case LFUN_SPACE_INSERT:
938 if (cursor.par()->layout()->free_spacing) {
942 doInsertInset(this, cmd, false, false);
944 moveCursorUpdate(bv, false);
947 case LFUN_HYPHENATION:
948 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
951 case LFUN_LIGATURE_BREAK:
952 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
956 specialChar(this, bv, InsetSpecialChar::LDOTS);
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(LyXText::text_uppercase);
999 bv->updateInset(inset_owner);
1003 case LFUN_LOWCASE_WORD:
1005 changeCase(LyXText::text_lowercase);
1007 bv->updateInset(inset_owner);
1011 case LFUN_CAPITALIZE_WORD:
1013 changeCase(LyXText::text_capitalization);
1015 bv->updateInset(inset_owner);
1019 case LFUN_TRANSPOSE_CHARS:
1021 setUndo(bv, Undo::FINISH, cursor.par());
1022 if (transposeChars(cursor))
1023 checkParagraph(cursor.par(), cursor.pos());
1025 bv->updateInset(inset_owner);
1030 cmd.message(_("Paste"));
1031 // clear the selection
1032 bv->toggleSelection();
1035 size_t sel_index = 0;
1036 string const & arg = cmd.argument;
1037 if (isStrUnsignedInt(arg)) {
1038 size_t const paste_arg = strToUnsignedInt(arg);
1039 #warning FIXME Check if the arg is in the domain of available selections.
1040 sel_index = paste_arg;
1042 pasteSelection(sel_index);
1043 clearSelection(); // bug 393
1051 cutSelection(true, true);
1053 cmd.message(_("Cut"));
1058 cmd.message(_("Copy"));
1061 case LFUN_BEGINNINGBUFSEL:
1063 return UNDISPATCHED;
1066 finishChange(bv, true);
1069 case LFUN_ENDBUFSEL:
1071 return UNDISPATCHED;
1074 finishChange(bv, true);
1078 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1084 istringstream is(STRCONV(cmd.argument));
1087 lyxerr << "SETXY: Could not parse coordinates in '"
1088 << cmd.argument << std::endl;
1090 setCursorFromCoordinates(x, y);
1095 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1097 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1103 case LFUN_GETLAYOUT:
1104 cmd.message(tostr(cursor.par()->layout()));
1108 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1109 << cmd.argument << endl;
1111 // This is not the good solution to the empty argument
1112 // problem, but it will hopefully suffice for 1.2.0.
1113 // The correct solution would be to augument the
1114 // function list/array with information about what
1115 // functions needs arguments and their type.
1116 if (cmd.argument.empty()) {
1117 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1121 // Derive layout number from given argument (string)
1122 // and current buffer's textclass (number)
1123 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1124 bool hasLayout = tclass.hasLayout(cmd.argument);
1125 string layout = cmd.argument;
1127 // If the entry is obsolete, use the new one instead.
1129 string const & obs = tclass[layout]->obsoleted_by();
1135 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1140 bool change_layout = (current_layout != layout);
1142 if (!change_layout && selection.set() &&
1143 selection.start.par() != selection.end.par())
1145 ParagraphList::iterator spit = selection.start.par();
1146 ParagraphList::iterator epit = boost::next(selection.end.par());
1147 while (spit != epit) {
1148 if (spit->layout()->name() != current_layout) {
1149 change_layout = true;
1156 if (change_layout) {
1157 current_layout = layout;
1160 bv->owner()->setLayout(layout);
1167 case LFUN_PASTESELECTION: {
1170 // this was originally a beforeChange(bv->text), i.e
1171 // the outermost LyXText!
1172 bv->beforeChange(this);
1173 string const clip = bv->getClipboard();
1174 if (!clip.empty()) {
1175 if (cmd.argument == "paragraph")
1176 insertStringAsParagraphs(clip);
1178 insertStringAsLines(clip);
1185 case LFUN_GOTOERROR:
1186 gotoInset(Inset::ERROR_CODE, false);
1190 gotoInset(Inset::NOTE_CODE, false);
1193 case LFUN_REFERENCE_GOTO:
1195 vector<Inset::Code> tmp;
1196 tmp.push_back(Inset::LABEL_CODE);
1197 tmp.push_back(Inset::REF_CODE);
1198 gotoInset(tmp, true);
1203 ParagraphList::iterator pit = cursor.par();
1204 lyx::pos_type pos = cursor.pos();
1208 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1211 c = pit->getChar(pos - 1);
1213 LyXLayout_ptr const & style = pit->layout();
1215 if (style->pass_thru ||
1216 pit->getFontSettings(bv->buffer()->params,
1217 pos).language()->lang() == "hebrew" ||
1218 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1219 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1223 case LFUN_DATE_INSERT: {
1224 time_t now_time_t = time(NULL);
1225 struct tm * now_tm = localtime(&now_time_t);
1226 setlocale(LC_TIME, "");
1228 if (!cmd.argument.empty())
1231 arg = lyxrc.date_insert_format;
1233 int const datetmp_len =
1234 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1236 for (int i = 0; i < datetmp_len; i++) {
1237 insertChar(datetmp[i]);
1240 selection.cursor = cursor;
1241 moveCursorUpdate(bv, false);
1245 case LFUN_MOUSE_TRIPLE:
1248 if (!isInInset() && bv->theLockingInset())
1250 if (cmd.button() == mouse_button::button1) {
1252 bv->screen().toggleSelection(this, bv);
1255 selection.cursor = cursor;
1259 bv->screen().toggleSelection(this, bv, false);
1261 bv->haveSelection(selection.set());
1265 case LFUN_MOUSE_DOUBLE:
1268 if (!isInInset() && bv->theLockingInset())
1270 if (cmd.button() == mouse_button::button1) {
1272 bv->screen().toggleSelection(this, bv);
1273 selectWord(lyx::WHOLE_WORD_STRICT);
1274 bv->screen().toggleSelection(this, bv, false);
1276 selectWord(lyx::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 RowList::iterator cursorrow = bv->text->cursorRow();
1319 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->text->top_y());
1321 // sorry for this but I have a strange error that the y value jumps at
1322 // a certain point. This seems like an error in my xforms library or
1323 // in some other local environment, but I would like to leave this here
1324 // for the moment until I can remove this (Jug 20020418)
1325 if (y_before < bv->text->cursor.y())
1326 lyxerr << y_before << ':'
1327 << bv->text->cursor.y() << endl;
1329 // This is to allow jumping over large insets
1330 if (cursorrow == bv->text->cursorRow()) {
1331 if (cmd.y >= bv->workHeight())
1332 bv->text->cursorDown(false);
1334 bv->text->cursorUp(false);
1337 // Maybe an empty line was deleted
1338 if (!bv->text->selection.set())
1339 bv->update(BufferView::UPDATE);
1340 bv->text->setSelection();
1341 bv->screen().toggleToggle(bv->text, bv);
1346 // Single-click on work area
1347 case LFUN_MOUSE_PRESS:
1352 // ok ok, this is a hack (for xforms)
1353 // We shouldn't go further down as we really should only do the
1354 // scrolling and be done with this. Otherwise we may open some
1355 // dialogs (Jug 20020424).
1356 if (cmd.button() == mouse_button::button4) {
1357 bv->scroll(-lyxrc.wheel_jump);
1360 if (cmd.button() == mouse_button::button5) {
1361 bv->scroll(lyxrc.wheel_jump);
1367 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1369 // Middle button press pastes if we have a selection
1370 // We do this here as if the selection was inside an inset
1371 // it could get cleared on the unlocking of the inset so
1372 // we have to check this first
1373 bool paste_internally = false;
1374 if (cmd.button() == mouse_button::button2 && selection.set()) {
1375 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1376 paste_internally = true;
1379 int const screen_first = bv->text->top_y();
1381 if (bv->theLockingInset()) {
1382 // We are in inset locking mode
1384 // Check whether the inset was hit. If not reset mode,
1385 // otherwise give the event to the inset
1386 if (inset_hit == bv->theLockingInset()) {
1387 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1388 bv->theLockingInset()->localDispatch(cmd1);
1391 bv->unlockInset(bv->theLockingInset());
1395 selection_possible = true;
1397 // Clear the selection
1398 bv->screen().toggleSelection(bv->text, bv);
1399 bv->text->clearSelection();
1400 bv->text->partialRebreak();
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()->message(inset->editMessage());
1410 // We just have to lock the inset before calling a PressEvent on it!
1411 if (!bv->lockInset(inset))
1412 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1413 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1414 inset->localDispatch(cmd1);
1417 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1419 // Right click on a footnote flag opens float menu
1420 if (cmd.button() == mouse_button::button3) {
1421 selection_possible = false;
1425 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1426 bv->text->setCursorFromCoordinates(x, y + screen_first);
1428 bv->text->selection.cursor = bv->text->cursor;
1429 bv->text->cursor.x_fix(bv->text->cursor.x());
1431 if (bv->fitCursor())
1432 selection_possible = false;
1434 // Insert primary selection with middle mouse
1435 // if there is a local selection in the current buffer,
1437 if (cmd.button() == mouse_button::button2) {
1438 if (paste_internally)
1439 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1441 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1442 selection_possible = false;
1447 case LFUN_MOUSE_RELEASE:
1449 // do nothing if we used the mouse wheel
1453 if (cmd.button() == mouse_button::button4
1454 || cmd.button() == mouse_button::button5)
1457 // If we hit an inset, we have the inset coordinates in these
1458 // and inset_hit points to the inset. If we do not hit an
1459 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1462 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1464 if (bv->theLockingInset()) {
1465 // We are in inset locking mode.
1467 // LyX does a kind of work-area grabbing for insets.
1468 // Only a ButtonPress FuncRequest outside the inset will
1469 // force a insetUnlock.
1470 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1471 bv->theLockingInset()->localDispatch(cmd1);
1475 selection_possible = false;
1477 if (cmd.button() == mouse_button::button2)
1481 if (cmd.button() == mouse_button::button1)
1482 bv->haveSelection(selection.set());
1485 bv->owner()->view_state_changed();
1486 bv->owner()->updateMenubar();
1487 bv->owner()->updateToolbar();
1489 // Did we hit an editable inset?
1491 selection_possible = false;
1493 // if we reach this point with a selection, it
1494 // must mean we are currently selecting.
1495 // But we don't want to open the inset
1496 // because that is annoying for the user.
1497 // So just pretend we didn't hit it.
1498 // this is OK because a "kosher" ButtonRelease
1499 // will follow a ButtonPress that clears
1501 // Note this also fixes selection drawing
1502 // problems if we end up opening an inset
1503 if (selection.set())
1506 // CHECK fix this proper in 0.13
1507 // well, maybe 13.0 !!!!!!!!!
1509 // Following a ref shouldn't issue
1510 // a push on the undo-stack
1511 // anylonger, now that we have
1512 // keybindings for following
1513 // references and returning from
1514 // references. IMHO though, it
1515 // should be the inset's own business
1516 // to push or not push on the undo
1517 // stack. They don't *have* to
1518 // alter the document...
1520 // ...or maybe the SetCursorParUndo()
1521 // below isn't necessary at all anylonger?
1522 if (inset_hit->lyxCode() == Inset::REF_CODE)
1523 setCursorParUndo(bv);
1525 bv->owner()->message(inset_hit->editMessage());
1527 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1528 inset_hit->localDispatch(cmd1);
1534 case LFUN_SELFINSERT: {
1535 if (cmd.argument.empty())
1538 // Automatically delete the currently selected
1539 // text and replace it with what is being
1540 // typed in now. Depends on lyxrc settings
1541 // "auto_region_delete", which defaults to
1544 if (lyxrc.auto_region_delete) {
1545 if (selection.set()) {
1546 cutSelection(false, false);
1549 bv->haveSelection(false);
1552 bv->beforeChange(this);
1553 LyXFont const old_font(real_current_font);
1555 string::const_iterator cit = cmd.argument.begin();
1556 string::const_iterator end = cmd.argument.end();
1557 for (; cit != end; ++cit)
1558 bv->owner()->getIntl().getTransManager().
1559 TranslateAndInsert(*cit, this);
1562 selection.cursor = cursor;
1563 moveCursorUpdate(bv, false);
1565 // real_current_font.number can change so we need to
1566 // update the minibuffer
1567 if (old_font != real_current_font)
1568 bv->owner()->view_state_changed();
1572 case LFUN_HTMLURL: {
1573 InsetCommandParams p("htmlurl");
1574 string const data = InsetCommandMailer::params2string("url", p);
1575 bv->owner()->getDialogs().show("url", data, 0);
1580 InsetCommandParams p("url");
1581 string const data = InsetCommandMailer::params2string("url", p);
1582 bv->owner()->getDialogs().show("url", data, 0);
1588 case LFUN_INSET_LIST:
1589 case LFUN_INSET_THEOREM:
1590 case LFUN_INSET_CAPTION:
1592 case LFUN_INSERT_NOTE:
1593 case LFUN_INSERT_BIBITEM:
1594 case LFUN_INSET_ERT:
1595 case LFUN_INSET_FLOAT:
1596 case LFUN_INSET_FOOTNOTE:
1597 case LFUN_INSET_MARGINAL:
1598 case LFUN_INSET_MINIPAGE:
1599 case LFUN_INSET_OPTARG:
1600 case LFUN_INSET_WIDE_FLOAT:
1601 case LFUN_INSET_WRAP:
1602 case LFUN_TABULAR_INSERT:
1603 case LFUN_ENVIRONMENT_INSERT:
1604 // Open the inset, and move the current selection
1606 doInsertInset(this, cmd, true, true);
1609 case LFUN_INDEX_INSERT:
1610 // Just open the inset
1611 doInsertInset(this, cmd, true, false);
1614 case LFUN_INDEX_PRINT:
1615 case LFUN_TOC_INSERT:
1618 doInsertInset(this, cmd, false, false);
1622 return UNDISPATCHED;