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"
38 #include "text_funcs.h"
47 extern string current_layout;
48 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
50 // the selection possible is needed, that only motion events are
51 // used, where the bottom press event was on the drawing area too
52 bool selection_possible = false;
57 void moveCursorUpdate(BufferView * bv, bool selecting)
59 LyXText * lt = bv->getLyXText();
61 if (selecting || lt->selection.mark()) {
64 bv->updateInset(lt->inset_owner);
68 if (!lt->isInInset()) {
69 bv->update(lt, BufferView::SELECT);
71 } else if (bv->text->refreshStatus() != LyXText::REFRESH_NONE) {
72 bv->theLockingInset()->hideInsetCursor(bv);
73 bv->update(BufferView::SELECT);
77 if (!lt->selection.set())
78 bv->haveSelection(false);
84 void finishChange(BufferView * bv, bool selecting = false)
87 moveCursorUpdate(bv, selecting);
88 bv->owner()->view_state_changed();
91 // check if the given co-ordinates are inside an inset at the
92 // given cursor, if one exists. If so, the inset is returned,
93 // and the co-ordinates are made relative. Otherwise, 0 is returned.
94 Inset * checkInset(BufferView * bv, LyXText & text,
95 LyXCursor const & cur, int & x, int & y)
97 lyx::pos_type const pos = cur.pos();
98 Paragraph /*const*/ & par = *cur.par();
100 if (pos >= par.size() || !par.isInset(pos))
103 Inset /*const*/ * inset = par.getInset(pos);
105 if (!isEditableInset(inset))
108 // get inset dimensions
109 lyx::Assert(par.getInset(pos));
111 LyXFont const & font = text.getFont(bv->buffer(), &par, pos);
113 int const width = inset->width(bv, font);
114 int const inset_x = font.isVisibleRightToLeft()
115 ? (cur.ix() - width) : cur.ix();
118 inset_x + inset->scroll(),
120 cur.iy() - inset->ascent(bv, font),
121 cur.iy() + inset->descent(bv, font)
124 if (!b.contained(x, y)) {
125 lyxerr[Debug::GUI] << "Missed inset at x,y "
127 << " box " << b << endl;
131 text.setCursor(&par, pos, true);
134 // The origin of an inset is on the baseline
135 y -= text.cursor.iy();
143 Inset * LyXText::checkInsetHit(int & x, int & y)
145 int y_tmp = y + top_y();
148 setCursorFromCoordinates(cur, x, y_tmp);
150 Inset * inset = checkInset(bv(), *this, cur, x, y_tmp);
156 // look at previous position
161 setCursor(cur, cur.par(), cur.pos() - 1, true);
163 inset = checkInset(bv(), *this, cur, x, y_tmp);
170 bool LyXText::gotoNextInset(vector<Inset::Code> const & codes,
171 string const & contents)
173 LyXCursor res = cursor;
176 if (res.pos() < res.par()->size() - 1) {
177 res.pos(res.pos() + 1);
179 res.par(res.par()->next());
183 } while (res.par() &&
184 !(res.par()->isInset(res.pos())
185 && (inset = res.par()->getInset(res.pos())) != 0
186 && find(codes.begin(), codes.end(), inset->lyxCode())
188 && (contents.empty() ||
189 static_cast<InsetCommand *>(
190 res.par()->getInset(res.pos()))->getContents()
194 setCursor(res.par(), res.pos(), false);
201 void LyXText::gotoInset(vector<Inset::Code> const & codes,
205 bv()->beforeChange(this);
209 if (same_content && cursor.par()->isInset(cursor.pos())) {
210 Inset const * inset = cursor.par()->getInset(cursor.pos());
211 if (find(codes.begin(), codes.end(), inset->lyxCode())
213 contents = static_cast<InsetCommand const *>(inset)->getContents();
216 if (!gotoNextInset(codes, contents)) {
217 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
218 LyXCursor tmp = cursor;
219 cursor.par(&*ownerParagraphs().begin());
221 if (!gotoNextInset(codes, contents)) {
223 bv()->owner()->message(_("No more insets"));
226 bv()->owner()->message(_("No more insets"));
230 selection.cursor = cursor;
234 void LyXText::gotoInset(Inset::Code code, bool same_content)
236 gotoInset(vector<Inset::Code>(1, code), same_content);
240 void LyXText::cursorPrevious()
244 if (cursor.row() == rows().begin()) {
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();
253 RowList::iterator cursorrow = cursor.row();
255 setCursorFromCoordinates(cursor.x_fix(), y);
259 if (cursorrow == bv()->text->cursor.row()) {
260 // we have a row which is taller than the workarea. The
261 // simplest solution is to move to the previous row instead.
264 // This is what we used to do, so we wouldn't skip right past
265 // tall rows, but it's not working right now.
267 new_y = bv->text->top_y() - bv->workHeight();
271 new_y = bv()->text->cursor.iy()
272 + bv()->theLockingInset()->insetInInsetY() + y
273 + cursor.row()->height()
274 - bv()->workHeight() + 1;
277 - cursor.row()->baseline()
278 + cursor.row()->height()
279 - bv()->workHeight() + 1;
282 bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
283 if (cursor.row() != rows().begin()) {
285 setCursor(cur, boost::prior(cursor.row())->par(),
286 boost::prior(cursor.row())->pos(), false);
287 if (cur.y() > top_y()) {
291 bv()->updateScrollbar();
295 void LyXText::cursorNext()
299 if (boost::next(cursor.row()) == rows().end()) {
300 int y = cursor.y() - cursor.row()->baseline() +
301 cursor.row()->height();
302 if (y > topy + bv()->workHeight()) {
303 bv()->screen().draw(bv()->text, bv(), bv()->text->top_y() + bv()->workHeight());
304 bv()->updateScrollbar();
309 int y = topy + bv()->workHeight();
310 if (inset_owner && !topy) {
311 y -= (bv()->text->cursor.iy()
312 - bv()->text->top_y()
313 + bv()->theLockingInset()->insetInInsetY());
318 RowList::iterator cursorrow = cursor.row();
319 setCursorFromCoordinates(cursor.x_fix(), y);
320 // + bv->workHeight());
324 if (cursorrow == bv()->text->cursor.row()) {
325 // we have a row which is taller than the workarea. The
326 // simplest solution is to move to the next row instead.
329 // This is what we used to do, so we wouldn't skip right past
330 // tall rows, but it's not working right now.
332 new_y = bv->text->top_y() + bv->workHeight();
336 new_y = bv()->text->cursor.iy()
337 + bv()->theLockingInset()->insetInInsetY()
338 + y - cursor.row()->baseline();
340 new_y = cursor.y() - cursor.row()->baseline();
343 bv()->screen().draw(bv()->text, bv(), new_y);
344 if (boost::next(cursor.row()) != rows().end()) {
346 setCursor(cur, boost::next(cursor.row())->par(),
347 boost::next(cursor.row())->pos(), false);
348 if (cur.y() < top_y() + bv()->workHeight()) {
352 bv()->updateScrollbar();
356 void LyXText::update()
358 bv()->update(this, BufferView::SELECT);
363 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
367 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
368 if (!bv->insertInset(new_inset))
371 bv->updateInset(new_inset);
375 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
376 bool edit, bool pastesel)
378 Inset * inset = createInset(cmd);
379 BufferView * bv = cmd.view();
383 if (lt->selection.set()) {
384 lt->cutSelection(true, false);
387 if (bv->insertInset(inset)) {
390 if (gotsel && pastesel)
391 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
400 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
402 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
403 <<"] arg[" << cmd.argument << ']' << endl;
405 BufferView * bv = cmd.view();
407 switch (cmd.action) {
409 case LFUN_APPENDIX: {
410 Paragraph * par = cursor.par();
411 bool start = !par->params().startOfAppendix();
413 // ensure that we have only one start_of_appendix in this document
414 ParagraphList::iterator tmp = ownerParagraphs().begin();
415 ParagraphList::iterator end = ownerParagraphs().end();
417 for (; tmp != end; ++tmp) {
418 if (tmp->params().startOfAppendix()) {
419 setUndo(bv, Undo::EDIT, &*tmp, &*boost::next(tmp));
420 tmp->params().startOfAppendix(false);
422 setHeightOfRow(getRow(&*tmp, 0, tmpy));
427 setUndo(bv, Undo::EDIT, par, par->next());
428 par->params().startOfAppendix(start);
430 // we can set the refreshing parameters now
432 redoHeightOfParagraph();
434 setCursor(cursor.par(), cursor.pos());
439 case LFUN_DELETE_WORD_FORWARD:
440 bv->beforeChange(this);
447 case LFUN_DELETE_WORD_BACKWARD:
448 bv->beforeChange(this);
450 deleteWordBackward();
455 case LFUN_DELETE_LINE_FORWARD:
456 bv->beforeChange(this);
465 if (!selection.mark())
466 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);
598 getWord(cur1, cur2, WHOLE_WORD);
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 tmpinset->edit(bv, !is_rtl);
628 // This is soooo ugly. Isn`t it possible to make
630 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
631 if (!selection.mark())
632 bv->beforeChange(this);
634 LyXCursor const cur = cursor;
637 if ((is_rtl || cur != cursor) && // only if really moved!
638 cursor.pos() < cursor.par()->size() &&
639 cursor.par()->isInset(cursor.pos()) &&
640 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
641 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
642 cmd.message(tmpinset->editMessage());
643 tmpinset->edit(bv, is_rtl);
653 if (!selection.mark())
654 bv->beforeChange(this);
655 bv->update(this, BufferView::UPDATE);
661 if (!selection.mark())
662 bv->beforeChange(this);
663 bv->update(this, BufferView::UPDATE);
668 case LFUN_UP_PARAGRAPH:
669 if (!selection.mark())
670 bv->beforeChange(this);
671 bv->update(this, BufferView::UPDATE);
676 case LFUN_DOWN_PARAGRAPH:
677 if (!selection.mark())
678 bv->beforeChange(this);
679 bv->update(this, BufferView::UPDATE);
680 cursorDownParagraph();
681 finishChange(bv, false);
685 if (!selection.mark())
686 bv->beforeChange(this);
687 bv->update(this, BufferView::UPDATE);
689 finishChange(bv, false);
692 // moveCursorUpdate(bv, false, false);
693 // owner_->view_state_changed();
697 if (!selection.mark())
698 bv->beforeChange(this);
699 bv->update(this, BufferView::UPDATE);
701 finishChange(bv, false);
705 if (!selection.mark())
706 bv->beforeChange(this);
709 finishChange(bv, false);
713 if (!selection.mark())
714 bv->beforeChange(this);
717 finishChange(bv, false);
720 case LFUN_BREAKLINE: {
721 lyx::pos_type body = cursor.par()->beginningOfBody();
723 // Not allowed by LaTeX (labels or empty par)
724 if (cursor.pos() <= body)
727 bv->beforeChange(this);
728 insertInset(new InsetNewline);
730 setCursor(cursor.par(), cursor.pos());
731 moveCursorUpdate(bv, false);
736 if (!selection.set()) {
738 selection.cursor = cursor;
740 // It is possible to make it a lot faster still
741 // just comment out the line below...
745 cutSelection(bv, true);
748 moveCursorUpdate(bv, false);
749 bv->owner()->view_state_changed();
753 case LFUN_DELETE_SKIP:
754 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
755 if (!selection.set()) {
756 LyXCursor cur = cursor;
757 if (cur.pos() == cur.par()->size()) {
761 && !(cur.par()->params().spaceTop()
762 == VSpace (VSpace::NONE))) {
764 cur.par()->params().lineTop(),
765 cur.par()->params().lineBottom(),
766 cur.par()->params().pagebreakTop(),
767 cur.par()->params().pagebreakBottom(),
768 VSpace(VSpace::NONE),
769 cur.par()->params().spaceBottom(),
770 cur.par()->params().spacing(),
771 cur.par()->params().align(),
772 cur.par()->params().labelWidthString(), 0);
778 selection.cursor = cursor;
782 selection.cursor = cursor;
786 cutSelection(bv, true);
793 if (!selection.set()) {
794 if (bv->owner()->getIntl().getTransManager().backspace()) {
796 selection.cursor = cursor;
798 // It is possible to make it a lot faster still
799 // just comment out the line below...
804 cutSelection(bv, true);
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(bv, true);
838 case LFUN_BREAKPARAGRAPH:
839 bv->beforeChange(this);
840 breakParagraph(bv->buffer()->paragraphs, 0);
842 selection.cursor = cursor;
844 bv->owner()->view_state_changed();
847 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
848 bv->beforeChange(this);
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 bv->beforeChange(this);
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 Paragraph * par = cursor.par();
887 Spacing::Space cur_spacing = par->params().spacing().getSpace();
888 float cur_value = 1.0;
889 if (cur_spacing == Spacing::Other)
890 cur_value = par->params().spacing().getValue();
892 istringstream is(cmd.argument.c_str());
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 par->params().spacing(Spacing(new_spacing, new_value));
927 case LFUN_INSET_TOGGLE:
929 bv->beforeChange(this);
936 case LFUN_PROTECTEDSPACE:
937 if (cursor.par()->layout()->free_spacing) {
941 specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
943 moveCursorUpdate(bv, false);
946 case LFUN_HYPHENATION:
947 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
950 case LFUN_LIGATURE_BREAK:
951 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
955 specialChar(this, bv, InsetSpecialChar::LDOTS);
958 case LFUN_END_OF_SENTENCE:
959 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
962 case LFUN_MENU_SEPARATOR:
963 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
967 bv->beforeChange(this);
969 selection.cursor = cursor;
970 cmd.message(N_("Mark off"));
974 bv->beforeChange(this);
975 selection.mark(true);
977 selection.cursor = cursor;
978 cmd.message(N_("Mark on"));
982 bv->beforeChange(this);
983 if (selection.mark()) {
985 cmd.message(N_("Mark removed"));
987 selection.mark(true);
989 cmd.message(N_("Mark set"));
991 selection.cursor = cursor;
994 case LFUN_UPCASE_WORD:
996 changeCase(LyXText::text_uppercase);
998 bv->updateInset(inset_owner);
1002 case LFUN_LOWCASE_WORD:
1004 changeCase(LyXText::text_lowercase);
1006 bv->updateInset(inset_owner);
1010 case LFUN_CAPITALIZE_WORD:
1012 changeCase(LyXText::text_capitalization);
1014 bv->updateInset(inset_owner);
1018 case LFUN_TRANSPOSE_CHARS:
1020 transposeChars(*this, cursor);
1022 bv->updateInset(inset_owner);
1027 cmd.message(_("Paste"));
1029 // clear the selection
1030 bv->toggleSelection();
1034 clearSelection(); // bug 393
1042 cutSelection(bv, true);
1044 cmd.message(_("Cut"));
1049 cmd.message(_("Copy"));
1052 case LFUN_BEGINNINGBUFSEL:
1054 return UNDISPATCHED;
1057 finishChange(bv, true);
1060 case LFUN_ENDBUFSEL:
1062 return UNDISPATCHED;
1065 finishChange(bv, true);
1069 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1075 istringstream is(cmd.argument.c_str());
1078 lyxerr << "SETXY: Could not parse coordinates in '"
1079 << cmd.argument << std::endl;
1081 setCursorFromCoordinates(x, y);
1086 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1088 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1094 case LFUN_GETLAYOUT:
1095 cmd.message(tostr(cursor.par()->layout()));
1099 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1100 << cmd.argument << endl;
1102 // This is not the good solution to the empty argument
1103 // problem, but it will hopefully suffice for 1.2.0.
1104 // The correct solution would be to augument the
1105 // function list/array with information about what
1106 // functions needs arguments and their type.
1107 if (cmd.argument.empty()) {
1108 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1112 // Derive layout number from given argument (string)
1113 // and current buffer's textclass (number)
1114 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1115 bool hasLayout = tclass.hasLayout(cmd.argument);
1116 string layout = cmd.argument;
1118 // If the entry is obsolete, use the new one instead.
1120 string const & obs = tclass[layout]->obsoleted_by();
1126 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1131 bool change_layout = (current_layout != layout);
1132 if (!change_layout && selection.set() &&
1133 selection.start.par() != selection.end.par())
1135 Paragraph * spar = selection.start.par();
1136 Paragraph * epar = selection.end.par()->next();
1137 while (spar != epar) {
1138 if (spar->layout()->name() != current_layout) {
1139 change_layout = true;
1142 spar = spar->next();
1145 if (change_layout) {
1147 current_layout = layout;
1150 bv->owner()->setLayout(layout);
1157 case LFUN_PASTESELECTION: {
1161 // this was originally a beforeChange(bv->text), i.e
1162 // the outermost LyXText!
1163 bv->beforeChange(this);
1164 string const clip = bv->getClipboard();
1165 if (!clip.empty()) {
1166 if (cmd.argument == "paragraph")
1167 insertStringAsParagraphs(clip);
1169 insertStringAsLines(clip);
1176 case LFUN_GOTOERROR:
1177 gotoInset(Inset::ERROR_CODE, false);
1181 gotoInset(Inset::NOTE_CODE, false);
1184 case LFUN_REFERENCE_GOTO:
1186 vector<Inset::Code> tmp;
1187 tmp.push_back(Inset::LABEL_CODE);
1188 tmp.push_back(Inset::REF_CODE);
1189 gotoInset(tmp, true);
1194 Paragraph const * par = cursor.par();
1195 lyx::pos_type pos = cursor.pos();
1199 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1202 c = par->getChar(pos - 1);
1205 LyXLayout_ptr const & style = par->layout();
1207 if (style->pass_thru ||
1208 par->getFontSettings(bv->buffer()->params,
1209 pos).language()->lang() == "hebrew" ||
1210 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1211 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1215 case LFUN_DATE_INSERT: {
1216 time_t now_time_t = time(NULL);
1217 struct tm * now_tm = localtime(&now_time_t);
1218 setlocale(LC_TIME, "");
1220 if (!cmd.argument.empty())
1223 arg = lyxrc.date_insert_format;
1225 int const datetmp_len =
1226 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1228 for (int i = 0; i < datetmp_len; i++) {
1229 insertChar(datetmp[i]);
1232 selection.cursor = cursor;
1233 moveCursorUpdate(bv, false);
1237 case LFUN_MOUSE_TRIPLE:
1240 if (!isInInset() && bv->theLockingInset())
1242 if (cmd.button() == mouse_button::button1) {
1244 bv->screen().hideCursor();
1245 bv->screen().toggleSelection(this, bv);
1248 selection.cursor = cursor;
1252 bv->screen().toggleSelection(this, bv, false);
1254 bv->haveSelection(selection.set());
1258 case LFUN_MOUSE_DOUBLE:
1261 if (!isInInset() && bv->theLockingInset())
1263 if (cmd.button() == mouse_button::button1) {
1265 bv->screen().hideCursor();
1266 bv->screen().toggleSelection(this, bv);
1267 selectWord(LyXText::WHOLE_WORD_STRICT);
1268 bv->screen().toggleSelection(this, bv, false);
1270 selectWord(LyXText::WHOLE_WORD_STRICT);
1273 bv->haveSelection(selection.set());
1277 case LFUN_MOUSE_MOTION:
1279 // Only use motion with button 1
1280 //if (ev.button() != mouse_button::button1)
1286 // Check for inset locking
1287 if (bv->theLockingInset()) {
1288 Inset * tli = bv->theLockingInset();
1289 LyXCursor cursor = bv->text->cursor;
1290 LyXFont font = bv->text->getFont(bv->buffer(),
1291 cursor.par(), cursor.pos());
1292 int width = tli->width(bv, font);
1293 int inset_x = font.isVisibleRightToLeft()
1294 ? cursor.ix() - width : cursor.ix();
1295 int start_x = inset_x + tli->scroll();
1296 FuncRequest cmd1 = cmd;
1297 cmd1.x = cmd.x - start_x;
1298 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1299 tli->localDispatch(cmd1);
1303 // The test for not selection possible is needed, that only motion
1304 // events are used, where the bottom press event was on
1305 // the drawing area too
1306 if (!selection_possible) {
1307 lyxerr[Debug::ACTION]
1308 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1312 bv->screen().hideCursor();
1314 RowList::iterator cursorrow = bv->text->cursor.row();
1315 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->text->top_y());
1317 // sorry for this but I have a strange error that the y value jumps at
1318 // a certain point. This seems like an error in my xforms library or
1319 // in some other local environment, but I would like to leave this here
1320 // for the moment until I can remove this (Jug 20020418)
1321 if (y_before < bv->text->cursor.y())
1322 lyxerr << y_before << ':'
1323 << bv->text->cursor.y() << endl;
1325 // This is to allow jumping over large insets
1326 if (cursorrow == bv->text->cursor.row()) {
1327 if (cmd.y >= bv->workHeight())
1328 bv->text->cursorDown(false);
1330 bv->text->cursorUp(false);
1333 // Maybe an empty line was deleted
1334 if (!bv->text->selection.set())
1335 bv->update(BufferView::UPDATE);
1336 bv->text->setSelection();
1337 bv->screen().toggleToggle(bv->text, bv);
1343 // Single-click on work area
1344 case LFUN_MOUSE_PRESS:
1349 // ok ok, this is a hack (for xforms)
1350 // We shouldn't go further down as we really should only do the
1351 // scrolling and be done with this. Otherwise we may open some
1352 // dialogs (Jug 20020424).
1353 if (cmd.button() == mouse_button::button4) {
1354 bv->scroll(-lyxrc.wheel_jump);
1357 if (cmd.button() == mouse_button::button5) {
1358 bv->scroll(lyxrc.wheel_jump);
1364 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1366 // Middle button press pastes if we have a selection
1367 // We do this here as if the selection was inside an inset
1368 // it could get cleared on the unlocking of the inset so
1369 // we have to check this first
1370 bool paste_internally = false;
1371 if (cmd.button() == mouse_button::button2 && selection.set()) {
1372 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1373 paste_internally = true;
1376 int const screen_first = bv->text->top_y();
1378 if (bv->theLockingInset()) {
1379 // We are in inset locking mode
1381 // Check whether the inset was hit. If not reset mode,
1382 // otherwise give the event to the inset
1383 if (inset_hit == bv->theLockingInset()) {
1384 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1385 bv->theLockingInset()->localDispatch(cmd1);
1388 bv->unlockInset(bv->theLockingInset());
1392 selection_possible = true;
1393 bv->screen().hideCursor();
1395 // Clear the selection
1396 bv->screen().toggleSelection(bv->text, bv);
1397 bv->text->clearSelection();
1398 bv->text->fullRebreak();
1400 bv->updateScrollbar();
1402 // Single left click in math inset?
1403 if (isHighlyEditableInset(inset_hit)) {
1404 // Highly editable inset, like math
1405 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1406 selection_possible = false;
1407 bv->owner()->message(inset->editMessage());
1408 //inset->edit(bv, x, y, cmd.button());
1409 // We just have to lock the inset before calling a PressEvent on it!
1410 // we don't need the edit() call here! (Jug20020329)
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_PARENTINSERT:
1616 case LFUN_TOC_INSERT:
1619 doInsertInset(this, cmd, false, false);
1623 return UNDISPATCHED;