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 "bufferview_funcs.h"
24 #include "ParagraphParameters.h"
30 #include "support/tostr.h"
31 #include "support/lstrings.h"
32 #include "frontends/LyXView.h"
33 #include "frontends/screen.h"
34 #include "frontends/Dialogs.h"
35 #include "insets/insetspecialchar.h"
36 #include "insets/insettext.h"
37 #include "insets/insetquotes.h"
38 #include "insets/insetcommand.h"
39 #include "insets/insetnewline.h"
40 #include "undo_funcs.h"
41 #include "text_funcs.h"
47 using namespace lyx::support;
48 using namespace bv_funcs;
55 extern string current_layout;
56 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
58 // the selection possible is needed, that only motion events are
59 // used, where the bottom press event was on the drawing area too
60 bool selection_possible = false;
65 void moveCursorUpdate(BufferView * bv, bool selecting)
67 LyXText * lt = bv->getLyXText();
69 if (selecting || lt->selection.mark()) {
72 bv->updateInset(lt->inset_owner);
76 if (!lt->isInInset()) {
77 bv->update(lt, BufferView::SELECT);
78 } else if (bv->text->needRefresh()) {
79 bv->update(BufferView::SELECT);
82 if (!lt->selection.set())
83 bv->haveSelection(false);
89 void finishChange(BufferView * bv, bool selecting = false)
92 moveCursorUpdate(bv, selecting);
93 bv->owner()->view_state_changed();
96 // check if the given co-ordinates are inside an inset at the
97 // given cursor, if one exists. If so, the inset is returned,
98 // and the co-ordinates are made relative. Otherwise, 0 is returned.
99 Inset * checkInset(BufferView * bv, LyXText & text,
100 LyXCursor const & cur, int & x, int & y)
102 lyx::pos_type const pos = cur.pos();
103 ParagraphList::iterator par = cur.par();
105 if (pos >= par->size() || !par->isInset(pos))
108 Inset /*const*/ * inset = par->getInset(pos);
110 if (!isEditableInset(inset))
113 // get inset dimensions
114 Assert(par->getInset(pos));
116 LyXFont const & font = text.getFont(bv->buffer(), par, pos);
118 int const width = inset->width();
119 int const inset_x = font.isVisibleRightToLeft()
120 ? (cur.ix() - width) : cur.ix();
123 inset_x + inset->scroll(),
125 cur.iy() - inset->ascent(),
126 cur.iy() + inset->descent()
129 if (!b.contained(x, y)) {
130 lyxerr[Debug::GUI] << "Missed inset at x,y "
132 << " box " << b << endl;
136 text.setCursor(par, pos, true);
139 // The origin of an inset is on the baseline
140 y -= text.cursor.iy();
148 Inset * LyXText::checkInsetHit(int & x, int & y)
150 int y_tmp = y + top_y();
153 setCursorFromCoordinates(cur, x, y_tmp);
155 Inset * inset = checkInset(bv(), *this, cur, x, y_tmp);
161 // look at previous position
166 setCursor(cur, cur.par(), cur.pos() - 1, true);
168 inset = checkInset(bv(), *this, cur, x, y_tmp);
175 bool LyXText::gotoNextInset(vector<Inset::Code> const & codes,
176 string const & contents)
178 ParagraphList::iterator end = ownerParagraphs().end();
179 ParagraphList::iterator pit = cursor.par();
180 pos_type pos = cursor.pos();
184 if (pos + 1 < pit->size()) {
191 } while (pit != end &&
192 !(pit->isInset(pos) &&
193 (inset = pit->getInset(pos)) != 0 &&
194 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
196 static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
200 setCursor(pit, pos, false);
207 void LyXText::gotoInset(vector<Inset::Code> const & codes,
210 bv()->beforeChange(this);
214 if (same_content && cursor.pos() < cursor.par()->size()
215 && cursor.par()->isInset(cursor.pos())) {
216 Inset const * inset = cursor.par()->getInset(cursor.pos());
217 if (find(codes.begin(), codes.end(), inset->lyxCode())
219 contents = static_cast<InsetCommand const *>(inset)->getContents();
222 if (!gotoNextInset(codes, contents)) {
223 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
224 LyXCursor tmp = cursor;
225 cursor.par(ownerParagraphs().begin());
227 if (!gotoNextInset(codes, contents)) {
229 bv()->owner()->message(_("No more insets"));
232 bv()->owner()->message(_("No more insets"));
236 selection.cursor = cursor;
240 void LyXText::gotoInset(Inset::Code code, bool same_content)
242 gotoInset(vector<Inset::Code>(1, code), same_content);
246 void LyXText::cursorPrevious()
250 if (cursorRow() == rows().begin()) {
252 int new_y = bv()->text->top_y() - bv()->workHeight();
253 bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
254 bv()->updateScrollbar();
259 RowList::iterator cursorrow = cursorRow();
261 setCursorFromCoordinates(cursor.x_fix(), y);
265 if (cursorrow == bv()->text->cursorRow()) {
266 // we have a row which is taller than the workarea. The
267 // simplest solution is to move to the previous row instead.
270 // This is what we used to do, so we wouldn't skip right past
271 // tall rows, but it's not working right now.
273 new_y = bv->text->top_y() - bv->workHeight();
277 new_y = bv()->text->cursor.iy()
278 + bv()->theLockingInset()->insetInInsetY() + y
279 + cursorRow()->height()
280 - bv()->workHeight() + 1;
283 - cursorRow()->baseline()
284 + cursorRow()->height()
285 - bv()->workHeight() + 1;
288 bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
289 if (cursorRow() != rows().begin()) {
291 setCursor(cur, boost::prior(cursorRow())->par(),
292 boost::prior(cursorRow())->pos(), false);
293 if (cur.y() > top_y()) {
297 bv()->updateScrollbar();
301 void LyXText::cursorNext()
305 if (boost::next(cursorRow()) == rows().end()) {
306 int y = cursor.y() - cursorRow()->baseline() +
307 cursorRow()->height();
308 if (y > topy + bv()->workHeight()) {
309 bv()->screen().draw(bv()->text, bv(), bv()->text->top_y() + bv()->workHeight());
310 bv()->updateScrollbar();
315 int y = topy + bv()->workHeight();
316 if (inset_owner && !topy) {
317 y -= (bv()->text->cursor.iy()
318 - bv()->text->top_y()
319 + bv()->theLockingInset()->insetInInsetY());
324 RowList::iterator cursorrow = cursorRow();
325 setCursorFromCoordinates(cursor.x_fix(), y);
326 // + bv->workHeight());
330 if (cursorrow == bv()->text->cursorRow()) {
331 // we have a row which is taller than the workarea. The
332 // simplest solution is to move to the next row instead.
335 // This is what we used to do, so we wouldn't skip right past
336 // tall rows, but it's not working right now.
338 new_y = bv->text->top_y() + bv->workHeight();
342 new_y = bv()->text->cursor.iy()
343 + bv()->theLockingInset()->insetInInsetY()
344 + y - cursorRow()->baseline();
346 new_y = cursor.y() - cursorRow()->baseline();
349 bv()->screen().draw(bv()->text, bv(), new_y);
351 RowList::iterator next_row = boost::next(cursorRow());
352 if (next_row != rows().end()) {
354 setCursor(cur, next_row->par(), next_row->pos(), false);
355 if (cur.y() < top_y() + bv()->workHeight()) {
359 bv()->updateScrollbar();
363 void LyXText::update()
365 bv()->update(this, BufferView::SELECT);
370 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
373 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
374 replaceSelection(lt);
375 if (!bv->insertInset(new_inset))
378 bv->updateInset(new_inset);
382 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
383 bool edit, bool pastesel)
385 Inset * inset = createInset(cmd);
386 BufferView * bv = cmd.view();
390 if (lt->selection.set()) {
391 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
394 if (bv->insertInset(inset)) {
396 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
397 inset->localDispatch(cmd);
399 if (gotsel && pastesel)
400 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
409 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
411 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
412 <<"] arg[" << cmd.argument << ']' << endl;
414 BufferView * bv = cmd.view();
416 switch (cmd.action) {
418 case LFUN_APPENDIX: {
419 ParagraphList::iterator pit = cursor.par();
420 bool start = !pit->params().startOfAppendix();
422 // ensure that we have only one start_of_appendix in this document
423 ParagraphList::iterator tmp = ownerParagraphs().begin();
424 ParagraphList::iterator end = ownerParagraphs().end();
426 for (; tmp != end; ++tmp) {
427 if (tmp->params().startOfAppendix()) {
428 recordUndo(bv, Undo::ATOMIC, tmp);
429 tmp->params().startOfAppendix(false);
431 setHeightOfRow(getRow(tmp, 0, tmpy));
436 recordUndo(bv, Undo::ATOMIC, pit);
437 pit->params().startOfAppendix(start);
439 // we can set the refreshing parameters now
441 redoHeightOfParagraph();
443 setCursor(cursor.par(), cursor.pos());
448 case LFUN_DELETE_WORD_FORWARD:
449 bv->beforeChange(this);
456 case LFUN_DELETE_WORD_BACKWARD:
457 bv->beforeChange(this);
459 deleteWordBackward();
464 case LFUN_DELETE_LINE_FORWARD:
465 bv->beforeChange(this);
473 if (!selection.mark())
474 bv->beforeChange(this);
476 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
479 cursorRightOneWord();
484 if (!selection.mark())
485 bv->beforeChange(this);
487 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
488 cursorRightOneWord();
494 case LFUN_BEGINNINGBUF:
495 if (!selection.mark())
496 bv->beforeChange(this);
503 if (selection.mark())
504 bv->beforeChange(this);
512 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
516 finishChange(bv, true);
521 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
525 finishChange(bv, true);
531 finishChange(bv, true);
537 finishChange(bv, true);
540 case LFUN_UP_PARAGRAPHSEL:
543 finishChange(bv, true);
546 case LFUN_DOWN_PARAGRAPHSEL:
548 cursorDownParagraph();
549 finishChange(bv, true);
555 finishChange(bv, true);
561 finishChange(bv, true);
567 finishChange(bv, true);
573 finishChange(bv, true);
576 case LFUN_WORDRIGHTSEL:
578 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
581 cursorRightOneWord();
582 finishChange(bv, true);
585 case LFUN_WORDLEFTSEL:
587 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
588 cursorRightOneWord();
591 finishChange(bv, true);
596 LyXCursor cur1 = cursor;
598 ::getWord(cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs());
599 setCursor(cur1.par(), cur1.pos());
600 bv->beforeChange(this);
601 setCursor(cur2.par(), cur2.pos());
602 finishChange(bv, true);
607 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
608 if (!selection.mark())
609 bv->beforeChange(this);
613 if (cursor.pos() < cursor.par()->size()
614 && cursor.par()->isInset(cursor.pos())
615 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
616 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
617 cmd.message(tmpinset->editMessage());
618 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
619 tmpinset->localDispatch(cmd1);
629 // This is soooo ugly. Isn`t it possible to make
631 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
632 if (!selection.mark())
633 bv->beforeChange(this);
635 LyXCursor const cur = cursor;
638 if ((is_rtl || cur != cursor) && // only if really moved!
639 cursor.pos() < cursor.par()->size() &&
640 cursor.par()->isInset(cursor.pos()) &&
641 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
642 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
643 cmd.message(tmpinset->editMessage());
644 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
645 tmpinset->localDispatch(cmd1);
655 if (!selection.mark())
656 bv->beforeChange(this);
657 bv->update(this, BufferView::UPDATE);
663 if (!selection.mark())
664 bv->beforeChange(this);
665 bv->update(this, BufferView::UPDATE);
670 case LFUN_UP_PARAGRAPH:
671 if (!selection.mark())
672 bv->beforeChange(this);
673 bv->update(this, BufferView::UPDATE);
678 case LFUN_DOWN_PARAGRAPH:
679 if (!selection.mark())
680 bv->beforeChange(this);
681 bv->update(this, BufferView::UPDATE);
682 cursorDownParagraph();
683 finishChange(bv, false);
687 if (!selection.mark())
688 bv->beforeChange(this);
689 bv->update(this, BufferView::UPDATE);
691 finishChange(bv, false);
694 // moveCursorUpdate(bv, false, false);
695 // owner_->view_state_changed();
699 if (!selection.mark())
700 bv->beforeChange(this);
701 bv->update(this, BufferView::UPDATE);
703 finishChange(bv, false);
707 if (!selection.mark())
708 bv->beforeChange(this);
711 finishChange(bv, false);
715 if (!selection.mark())
716 bv->beforeChange(this);
719 finishChange(bv, false);
722 case LFUN_BREAKLINE: {
723 lyx::pos_type body = cursor.par()->beginningOfBody();
725 // Not allowed by LaTeX (labels or empty par)
726 if (cursor.pos() <= body)
729 replaceSelection(bv->getLyXText());
730 insertInset(new InsetNewline);
732 setCursor(cursor.par(), cursor.pos());
733 moveCursorUpdate(bv, false);
738 if (!selection.set()) {
740 selection.cursor = cursor;
742 // It is possible to make it a lot faster still
743 // just comment out the line below...
746 cutSelection(true, false);
749 moveCursorUpdate(bv, false);
750 bv->owner()->view_state_changed();
754 case LFUN_DELETE_SKIP:
755 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
756 if (!selection.set()) {
757 LyXCursor cur = cursor;
758 if (cur.pos() == cur.par()->size()) {
762 && !(cur.par()->params().spaceTop()
763 == VSpace (VSpace::NONE))) {
765 cur.par()->params().lineTop(),
766 cur.par()->params().lineBottom(),
767 cur.par()->params().pagebreakTop(),
768 cur.par()->params().pagebreakBottom(),
769 VSpace(VSpace::NONE),
770 cur.par()->params().spaceBottom(),
771 cur.par()->params().spacing(),
772 cur.par()->params().align(),
773 cur.par()->params().labelWidthString(), 0);
779 selection.cursor = cursor;
783 selection.cursor = cursor;
787 cutSelection(true, false);
794 if (!selection.set()) {
795 if (bv->owner()->getIntl().getTransManager().backspace()) {
797 selection.cursor = cursor;
799 // It is possible to make it a lot faster still
800 // just comment out the line below...
804 cutSelection(true, false);
807 bv->owner()->view_state_changed();
811 case LFUN_BACKSPACE_SKIP:
812 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
813 if (!selection.set()) {
814 LyXCursor cur = cursor;
816 && !(cur.par()->params().spaceTop()
817 == VSpace (VSpace::NONE))) {
819 cur.par()->params().lineTop(),
820 cur.par()->params().lineBottom(),
821 cur.par()->params().pagebreakTop(),
822 cur.par()->params().pagebreakBottom(),
823 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
824 cur.par()->params().spacing(),
825 cur.par()->params().align(),
826 cur.par()->params().labelWidthString(), 0);
829 selection.cursor = cur;
833 cutSelection(true, false);
838 case LFUN_BREAKPARAGRAPH:
839 replaceSelection(bv->getLyXText());
840 breakParagraph(bv->buffer()->paragraphs, 0);
842 selection.cursor = cursor;
844 bv->owner()->view_state_changed();
847 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
848 replaceSelection(bv->getLyXText());
849 breakParagraph(bv->buffer()->paragraphs, 1);
851 selection.cursor = cursor;
853 bv->owner()->view_state_changed();
856 case LFUN_BREAKPARAGRAPH_SKIP: {
857 // When at the beginning of a paragraph, remove
858 // indentation and add a "defskip" at the top.
859 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
860 LyXCursor cur = cursor;
861 replaceSelection(bv->getLyXText());
862 if (cur.pos() == 0) {
863 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
865 cur.par()->params().lineTop(),
866 cur.par()->params().lineBottom(),
867 cur.par()->params().pagebreakTop(),
868 cur.par()->params().pagebreakBottom(),
869 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
870 cur.par()->params().spacing(),
871 cur.par()->params().align(),
872 cur.par()->params().labelWidthString(), 1);
876 breakParagraph(bv->buffer()->paragraphs, 0);
879 selection.cursor = cur;
881 bv->owner()->view_state_changed();
885 case LFUN_PARAGRAPH_SPACING: {
886 ParagraphList::iterator pit = cursor.par();
887 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
888 float cur_value = 1.0;
889 if (cur_spacing == Spacing::Other)
890 cur_value = pit->params().spacing().getValue();
892 istringstream is(STRCONV(cmd.argument));
895 Spacing::Space new_spacing = cur_spacing;
896 float new_value = cur_value;
898 lyxerr << "Missing argument to `paragraph-spacing'"
900 } else if (tmp == "single") {
901 new_spacing = Spacing::Single;
902 } else if (tmp == "onehalf") {
903 new_spacing = Spacing::Onehalf;
904 } else if (tmp == "double") {
905 new_spacing = Spacing::Double;
906 } else if (tmp == "other") {
907 new_spacing = Spacing::Other;
910 lyxerr << "new_value = " << tmpval << endl;
913 } else if (tmp == "default") {
914 new_spacing = Spacing::Default;
916 lyxerr << _("Unknown spacing argument: ")
917 << cmd.argument << endl;
919 if (cur_spacing != new_spacing || cur_value != new_value) {
920 pit->params().spacing(Spacing(new_spacing, new_value));
927 case LFUN_INSET_SETTINGS:
928 Assert(bv->theLockingInset());
929 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
932 case LFUN_INSET_TOGGLE:
933 bv->beforeChange(this);
940 case LFUN_SPACE_INSERT:
941 if (cursor.par()->layout()->free_spacing) {
945 doInsertInset(this, cmd, false, false);
947 moveCursorUpdate(bv, false);
950 case LFUN_HYPHENATION:
951 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
954 case LFUN_LIGATURE_BREAK:
955 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
959 specialChar(this, bv, InsetSpecialChar::LDOTS);
962 case LFUN_END_OF_SENTENCE:
963 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
966 case LFUN_MENU_SEPARATOR:
967 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
971 bv->beforeChange(this);
973 selection.cursor = cursor;
974 cmd.message(N_("Mark off"));
978 bv->beforeChange(this);
979 selection.mark(true);
981 selection.cursor = cursor;
982 cmd.message(N_("Mark on"));
986 bv->beforeChange(this);
987 if (selection.mark()) {
989 cmd.message(N_("Mark removed"));
991 selection.mark(true);
993 cmd.message(N_("Mark set"));
995 selection.cursor = cursor;
998 case LFUN_UPCASE_WORD:
1000 changeCase(LyXText::text_uppercase);
1002 bv->updateInset(inset_owner);
1006 case LFUN_LOWCASE_WORD:
1008 changeCase(LyXText::text_lowercase);
1010 bv->updateInset(inset_owner);
1014 case LFUN_CAPITALIZE_WORD:
1016 changeCase(LyXText::text_capitalization);
1018 bv->updateInset(inset_owner);
1022 case LFUN_TRANSPOSE_CHARS:
1024 recordUndo(bv, Undo::ATOMIC, cursor.par());
1025 if (transposeChars(cursor))
1026 checkParagraph(cursor.par(), cursor.pos());
1028 bv->updateInset(inset_owner);
1033 cmd.message(_("Paste"));
1034 replaceSelection(bv->getLyXText());
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 replaceSelection(bv->getLyXText());
1204 ParagraphList::iterator pit = cursor.par();
1205 lyx::pos_type pos = cursor.pos();
1209 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1212 c = pit->getChar(pos - 1);
1214 LyXLayout_ptr const & style = pit->layout();
1216 if (style->pass_thru ||
1217 pit->getFontSettings(bv->buffer()->params,
1218 pos).language()->lang() == "hebrew" ||
1219 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1220 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1224 case LFUN_DATE_INSERT: {
1225 replaceSelection(bv->getLyXText());
1226 time_t now_time_t = time(NULL);
1227 struct tm * now_tm = localtime(&now_time_t);
1228 setlocale(LC_TIME, "");
1230 if (!cmd.argument.empty())
1233 arg = lyxrc.date_insert_format;
1235 int const datetmp_len =
1236 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1238 for (int i = 0; i < datetmp_len; i++) {
1239 insertChar(datetmp[i]);
1242 selection.cursor = cursor;
1243 moveCursorUpdate(bv, false);
1247 case LFUN_MOUSE_TRIPLE:
1250 if (!isInInset() && bv->theLockingInset())
1252 if (cmd.button() == mouse_button::button1) {
1254 bv->screen().toggleSelection(this, bv);
1257 selection.cursor = cursor;
1261 bv->screen().toggleSelection(this, bv, false);
1263 bv->haveSelection(selection.set());
1267 case LFUN_MOUSE_DOUBLE:
1270 if (!isInInset() && bv->theLockingInset())
1272 if (cmd.button() == mouse_button::button1) {
1274 bv->screen().toggleSelection(this, bv);
1275 selectWord(lyx::WHOLE_WORD_STRICT);
1276 bv->screen().toggleSelection(this, bv, false);
1278 selectWord(lyx::WHOLE_WORD_STRICT);
1281 bv->haveSelection(selection.set());
1285 case LFUN_MOUSE_MOTION:
1287 // Only use motion with button 1
1288 //if (ev.button() != mouse_button::button1)
1294 // Check for inset locking
1295 if (bv->theLockingInset()) {
1296 Inset * tli = bv->theLockingInset();
1297 LyXCursor cursor = bv->text->cursor;
1298 LyXFont font = bv->text->getFont(bv->buffer(),
1299 cursor.par(), cursor.pos());
1300 int width = tli->width();
1301 int inset_x = font.isVisibleRightToLeft()
1302 ? cursor.ix() - width : cursor.ix();
1303 int start_x = inset_x + tli->scroll();
1304 FuncRequest cmd1 = cmd;
1305 cmd1.x = cmd.x - start_x;
1306 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1307 tli->localDispatch(cmd1);
1311 // The test for not selection possible is needed, that only motion
1312 // events are used, where the bottom press event was on
1313 // the drawing area too
1314 if (!selection_possible) {
1315 lyxerr[Debug::ACTION]
1316 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1320 RowList::iterator cursorrow = bv->text->cursorRow();
1321 bv->text->setCursorFromCoordinates(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->cursorRow()) {
1333 if (cmd.y >= bv->workHeight())
1334 bv->text->cursorDown(false);
1336 bv->text->cursorUp(false);
1339 // Maybe an empty line was deleted
1340 if (!bv->text->selection.set())
1341 bv->update(BufferView::UPDATE);
1342 bv->text->setSelection();
1343 bv->screen().toggleToggle(bv->text, bv);
1348 // Single-click on work area
1349 case LFUN_MOUSE_PRESS:
1354 // ok ok, this is a hack (for xforms)
1355 // We shouldn't go further down as we really should only do the
1356 // scrolling and be done with this. Otherwise we may open some
1357 // dialogs (Jug 20020424).
1358 if (cmd.button() == mouse_button::button4) {
1359 bv->scroll(-lyxrc.wheel_jump);
1362 if (cmd.button() == mouse_button::button5) {
1363 bv->scroll(lyxrc.wheel_jump);
1369 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1371 // Middle button press pastes if we have a selection
1372 // We do this here as if the selection was inside an inset
1373 // it could get cleared on the unlocking of the inset so
1374 // we have to check this first
1375 bool paste_internally = false;
1376 if (cmd.button() == mouse_button::button2 && selection.set()) {
1377 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1378 paste_internally = true;
1381 int const screen_first = bv->text->top_y();
1383 if (bv->theLockingInset()) {
1384 // We are in inset locking mode
1386 // Check whether the inset was hit. If not reset mode,
1387 // otherwise give the event to the inset
1388 if (inset_hit == bv->theLockingInset()) {
1389 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1390 bv->theLockingInset()->localDispatch(cmd1);
1393 bv->unlockInset(bv->theLockingInset());
1397 selection_possible = true;
1399 // Clear the selection
1400 bv->screen().toggleSelection(bv->text, bv);
1401 bv->text->clearSelection();
1402 bv->text->partialRebreak();
1404 bv->updateScrollbar();
1406 // Single left click in math inset?
1407 if (isHighlyEditableInset(inset_hit)) {
1408 // Highly editable inset, like math
1409 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1410 selection_possible = false;
1411 bv->owner()->message(inset->editMessage());
1412 // We just have to lock the inset before calling a PressEvent on it!
1413 if (!bv->lockInset(inset))
1414 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1415 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1416 inset->localDispatch(cmd1);
1419 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1421 // Right click on a footnote flag opens float menu
1422 if (cmd.button() == mouse_button::button3) {
1423 selection_possible = false;
1427 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1428 bv->text->setCursorFromCoordinates(x, y + screen_first);
1430 bv->text->selection.cursor = bv->text->cursor;
1431 bv->text->cursor.x_fix(bv->text->cursor.x());
1433 if (bv->fitCursor())
1434 selection_possible = false;
1436 // Insert primary selection with middle mouse
1437 // if there is a local selection in the current buffer,
1439 if (cmd.button() == mouse_button::button2) {
1440 if (paste_internally)
1441 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1443 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1444 selection_possible = false;
1449 case LFUN_MOUSE_RELEASE:
1451 // do nothing if we used the mouse wheel
1455 if (cmd.button() == mouse_button::button4
1456 || cmd.button() == mouse_button::button5)
1459 // If we hit an inset, we have the inset coordinates in these
1460 // and inset_hit points to the inset. If we do not hit an
1461 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1464 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1466 if (bv->theLockingInset()) {
1467 // We are in inset locking mode.
1469 // LyX does a kind of work-area grabbing for insets.
1470 // Only a ButtonPress FuncRequest outside the inset will
1471 // force a insetUnlock.
1472 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1473 bv->theLockingInset()->localDispatch(cmd1);
1477 selection_possible = false;
1479 if (cmd.button() == mouse_button::button2)
1483 if (cmd.button() == mouse_button::button1)
1484 bv->haveSelection(selection.set());
1487 bv->owner()->view_state_changed();
1488 bv->owner()->updateMenubar();
1489 bv->owner()->updateToolbar();
1491 // Did we hit an editable inset?
1493 selection_possible = false;
1495 // if we reach this point with a selection, it
1496 // must mean we are currently selecting.
1497 // But we don't want to open the inset
1498 // because that is annoying for the user.
1499 // So just pretend we didn't hit it.
1500 // this is OK because a "kosher" ButtonRelease
1501 // will follow a ButtonPress that clears
1503 // Note this also fixes selection drawing
1504 // problems if we end up opening an inset
1505 if (selection.set())
1508 // CHECK fix this proper in 0.13
1509 // well, maybe 13.0 !!!!!!!!!
1511 // Following a ref shouldn't issue
1512 // a push on the undo-stack
1513 // anylonger, now that we have
1514 // keybindings for following
1515 // references and returning from
1516 // references. IMHO though, it
1517 // should be the inset's own business
1518 // to push or not push on the undo
1519 // stack. They don't *have* to
1520 // alter the document...
1522 // ...or maybe the SetCursorParUndo()
1523 // below isn't necessary at all anylonger?
1524 if (inset_hit->lyxCode() == Inset::REF_CODE)
1525 recordUndo(bv, Undo::ATOMIC);
1527 bv->owner()->message(inset_hit->editMessage());
1529 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1530 inset_hit->localDispatch(cmd1);
1536 case LFUN_SELFINSERT: {
1537 if (cmd.argument.empty())
1540 // Automatically delete the currently selected
1541 // text and replace it with what is being
1542 // typed in now. Depends on lyxrc settings
1543 // "auto_region_delete", which defaults to
1546 if (lyxrc.auto_region_delete) {
1547 if (selection.set()) {
1548 cutSelection(false, false);
1551 bv->haveSelection(false);
1554 bv->beforeChange(this);
1555 LyXFont const old_font(real_current_font);
1557 string::const_iterator cit = cmd.argument.begin();
1558 string::const_iterator end = cmd.argument.end();
1559 for (; cit != end; ++cit)
1560 bv->owner()->getIntl().getTransManager().
1561 TranslateAndInsert(*cit, this);
1564 selection.cursor = cursor;
1565 moveCursorUpdate(bv, false);
1567 // real_current_font.number can change so we need to
1568 // update the minibuffer
1569 if (old_font != real_current_font)
1570 bv->owner()->view_state_changed();
1574 case LFUN_HTMLURL: {
1575 InsetCommandParams p("htmlurl");
1576 string const data = InsetCommandMailer::params2string("url", p);
1577 bv->owner()->getDialogs().show("url", data, 0);
1582 InsetCommandParams p("url");
1583 string const data = InsetCommandMailer::params2string("url", p);
1584 bv->owner()->getDialogs().show("url", data, 0);
1590 case LFUN_INSET_LIST:
1591 case LFUN_INSET_THEOREM:
1592 case LFUN_INSET_CAPTION:
1594 case LFUN_INSERT_NOTE:
1595 case LFUN_INSERT_BIBITEM:
1596 case LFUN_INSET_ERT:
1597 case LFUN_INSET_FLOAT:
1598 case LFUN_INSET_FOOTNOTE:
1599 case LFUN_INSET_MARGINAL:
1600 case LFUN_INSET_MINIPAGE:
1601 case LFUN_INSET_OPTARG:
1602 case LFUN_INSET_WIDE_FLOAT:
1603 case LFUN_INSET_WRAP:
1604 case LFUN_TABULAR_INSERT:
1605 case LFUN_ENVIRONMENT_INSERT:
1606 // Open the inset, and move the current selection
1608 doInsertInset(this, cmd, true, true);
1611 case LFUN_INDEX_INSERT:
1612 // Just open the inset
1613 doInsertInset(this, cmd, true, false);
1616 case LFUN_INDEX_PRINT:
1617 case LFUN_TOC_INSERT:
1620 doInsertInset(this, cmd, false, false);
1624 return UNDISPATCHED;