3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Alfredo Braunstein
9 * \author Angus Leeming
11 * \author André Pönitz
13 * Full author contact details are available in file CREDITS.
20 #include "FloatList.h"
21 #include "FuncStatus.h"
23 #include "bufferparams.h"
24 #include "BufferView.h"
26 #include "coordcache.h"
27 #include "CutAndPaste.h"
29 #include "dispatchresult.h"
31 #include "funcrequest.h"
39 #include "paragraph.h"
40 #include "paragraph_funcs.h"
41 #include "ParagraphParameters.h"
45 #include "frontends/Dialogs.h"
46 #include "frontends/LyXView.h"
48 #include "insets/insetcommand.h"
49 #include "insets/insetfloatlist.h"
50 #include "insets/insetnewline.h"
51 #include "insets/insetquotes.h"
52 #include "insets/insetspecialchar.h"
53 #include "insets/insettext.h"
55 #include "support/lstrings.h"
56 #include "support/lyxlib.h"
57 #include "support/tostr.h"
59 #include "mathed/math_hullinset.h"
60 #include "mathed/math_macrotemplate.h"
67 using lyx::cap::copySelection;
68 using lyx::cap::cutSelection;
69 using lyx::cap::pasteSelection;
70 using lyx::cap::replaceSelection;
72 using lyx::support::isStrUnsignedInt;
73 using lyx::support::strToUnsignedInt;
74 using lyx::support::atoi;
75 using lyx::support::token;
80 using std::istringstream;
84 extern string current_layout;
86 // the selection possible is needed, that only motion events are
87 // used, where the button press event was on the drawing area too
88 bool selection_possible = false;
94 LyXFont freefont(LyXFont::ALL_IGNORE);
95 bool toggleall = false;
98 void toggleAndShow(LCursor & cur, LyXText * text,
99 LyXFont const & font, bool toggleall = true)
101 text->toggleFree(cur, font, toggleall);
103 if (font.language() != ignore_language ||
104 font.number() != LyXFont::IGNORE) {
105 Paragraph & par = cur.paragraph();
106 text->bidi.computeTables(par, cur.buffer(), cur.textRow());
107 if (cur.boundary() !=
108 text->bidi.isBoundary(cur.buffer(), par,
110 text->real_current_font))
111 text->setCursor(cur, cur.par(), cur.pos(),
112 false, !cur.boundary());
117 void moveCursor(LCursor & cur, bool selecting)
119 if (selecting || cur.mark())
121 if (!cur.selection())
122 cur.bv().haveSelection(false);
123 cur.bv().switchKeyMap();
127 void finishChange(LCursor & cur, bool selecting)
130 moveCursor(cur, selecting);
134 void mathDispatch(LCursor & cur, FuncRequest const & cmd, bool display)
137 string sel = cur.selectionAsString(false);
138 lyxerr << "selection is: '" << sel << "'" << endl;
141 const int old_pos = cur.pos();
142 cur.insert(new MathHullInset);
143 BOOST_ASSERT(old_pos == cur.pos());
144 cur.nextInset()->edit(cur, true);
145 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
146 // don't do that also for LFUN_MATH_MODE unless you want end up with
147 // always changing to mathrm when opening an inlined inset
148 // -- I really hate "LyXfunc overloading"...
150 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
151 cur.dispatch(FuncRequest(LFUN_INSERT_MATH, cmd.argument));
153 // create a macro if we see "\\newcommand" somewhere, and an ordinary
155 cutSelection(cur, true, true);
156 if (sel.find("\\newcommand") == string::npos
157 && sel.find("\\def") == string::npos)
159 cur.insert(new MathHullInset);
160 cur.dispatch(FuncRequest(LFUN_RIGHT));
161 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
162 cur.dispatch(FuncRequest(LFUN_INSERT_MATH, sel));
164 istringstream is(sel);
165 cur.insert(new MathMacroTemplate(is));
168 cur.message(N_("Math editor mode"));
177 string const freefont2string()
180 if (font2string(freefont, toggleall, data))
188 // takes absolute x,y coordinates
189 InsetBase * LyXText::checkInsetHit(int x, int y) const
194 getParsInRange(paragraphs(),
196 bv()->top_y() - yo_ + bv()->workHeight(),
199 lyxerr << "checkInsetHit: x: " << x << " y: " << y << endl;
200 lyxerr << " pit: " << pit << " end: " << end << endl;
201 for (; pit != end; ++pit) {
202 InsetList::const_iterator iit = pars_[pit].insetlist.begin();
203 InsetList::const_iterator iend = pars_[pit].insetlist.end();
204 for (; iit != iend; ++iit) {
205 InsetBase * inset = iit->inset;
207 lyxerr << "examining inset " << inset << endl;
208 if (theCoords.insets_.has(inset))
210 << " xo: " << inset->xo() << "..." << inset->xo() + inset->width()
211 << " yo: " << inset->yo() - inset->ascent() << "..."
212 << inset->yo() + inset->descent() << endl;
214 lyxerr << " inset has no cached position";
216 if (inset->covers(x, y)) {
217 lyxerr << "Hit inset: " << inset << endl;
222 lyxerr << "No inset hit. " << endl;
227 bool LyXText::gotoNextInset(LCursor & cur,
228 vector<InsetOld_code> const & codes, string const & contents)
230 BOOST_ASSERT(this == cur.text());
231 par_type end = paragraphs().size();
232 par_type pit = cur.par();
233 pos_type pos = cur.pos();
237 if (pos + 1 < pars_[pit].size()) {
244 } while (pit != end &&
245 !(pars_[pit].isInset(pos) &&
246 (inset = pars_[pit].getInset(pos)) != 0 &&
247 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
249 static_cast<InsetCommand *>(pars_[pit].getInset(pos))->getContents()
255 setCursor(cur, pit, pos, false);
260 void LyXText::gotoInset(LCursor & cur,
261 vector<InsetOld_code> const & codes, bool same_content)
263 cur.clearSelection();
267 && cur.pos() < cur.lastpos()
268 && cur.paragraph().isInset(cur.pos())) {
269 InsetBase const * inset = cur.paragraph().getInset(cur.pos());
270 if (find(codes.begin(), codes.end(), inset->lyxCode())
272 contents = static_cast<InsetCommand const *>(inset)->getContents();
275 if (!gotoNextInset(cur, codes, contents)) {
276 if (cur.pos() || cur.par() != 0) {
277 CursorSlice tmp = cur.top();
280 if (!gotoNextInset(cur, codes, contents)) {
282 cur.message(_("No more insets"));
285 cur.message(_("No more insets"));
292 void LyXText::gotoInset(LCursor & cur, InsetOld_code code, bool same_content)
294 gotoInset(cur, vector<InsetOld_code>(1, code), same_content);
298 void LyXText::cursorPrevious(LCursor & cur)
300 pos_type cpos = cur.pos();
301 lyx::par_type cpar = cur.par();
303 int x = cur.x_target();
304 int y = cur.bv().top_y();
305 setCursorFromCoordinates(cur, x, y);
307 if (cpar == cur.par() && cpos == cur.pos()) {
308 // we have a row which is taller than the workarea. The
309 // simplest solution is to move to the previous row instead.
313 cur.bv().updateScrollbar();
318 void LyXText::cursorNext(LCursor & cur)
320 pos_type cpos = cur.pos();
321 lyx::par_type cpar = cur.par();
323 int x = cur.x_target();
324 int y = cur.bv().top_y() + cur.bv().workHeight();
325 setCursorFromCoordinates(cur, x, y);
327 if (cpar == cur.par() && cpos == cur.pos()) {
328 // we have a row which is taller than the workarea. The
329 // simplest solution is to move to the next row instead.
333 cur.bv().updateScrollbar();
340 void specialChar(LCursor & cur, InsetSpecialChar::Kind kind)
342 lyx::cap::replaceSelection(cur);
343 cur.insert(new InsetSpecialChar(kind));
347 void doInsertInset(LCursor & cur, LyXText * text,
348 FuncRequest const & cmd, bool edit, bool pastesel)
350 InsetBase * inset = createInset(&cur.bv(), cmd);
356 if (cur.selection()) {
357 cur.bv().owner()->dispatch(FuncRequest(LFUN_CUT));
360 text->insertInset(cur, inset);
363 inset->edit(cur, true);
365 if (gotsel && pastesel)
366 cur.bv().owner()->dispatch(FuncRequest(LFUN_PASTE));
372 void LyXText::number(LCursor & cur)
374 LyXFont font(LyXFont::ALL_IGNORE);
375 font.setNumber(LyXFont::TOGGLE);
376 toggleAndShow(cur, this, font);
380 bool LyXText::isRTL(Paragraph const & par) const
382 return par.isRightToLeftPar(bv()->buffer()->params());
386 void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
388 lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
389 //lyxerr << "*** LyXText::dispatch: cmd: " << cmd << endl;
391 BOOST_ASSERT(cur.text() == this);
392 BufferView * bv = &cur.bv();
393 CursorSlice sl = cur.top();
394 bool sel = cur.selection();
397 switch (cmd.action) {
399 case LFUN_APPENDIX: {
400 Paragraph & par = cur.paragraph();
401 bool start = !par.params().startOfAppendix();
403 // ensure that we have only one start_of_appendix in this document
404 for (par_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
405 if (pars_[tmp].params().startOfAppendix()) {
407 pars_[tmp].params().startOfAppendix(false);
414 par.params().startOfAppendix(start);
416 // we can set the refreshing parameters now
422 case LFUN_DELETE_WORD_FORWARD:
423 cur.clearSelection();
424 deleteWordForward(cur);
425 finishChange(cur, false);
428 case LFUN_DELETE_WORD_BACKWARD:
429 cur.clearSelection();
430 deleteWordBackward(cur);
431 finishChange(cur, false);
434 case LFUN_DELETE_LINE_FORWARD:
435 cur.clearSelection();
436 deleteLineForward(cur);
437 finishChange(cur, false);
443 cur.clearSelection();
444 if (isRTL(cur.paragraph()))
445 cursorLeftOneWord(cur);
447 cursorRightOneWord(cur);
448 finishChange(cur, false);
454 cur.clearSelection();
455 if (isRTL(cur.paragraph()))
456 cursorRightOneWord(cur);
458 cursorLeftOneWord(cur);
459 finishChange(cur, false);
462 case LFUN_BEGINNINGBUF:
463 if (cur.size() == 1) {
465 cur.clearSelection();
467 finishChange(cur, false);
474 if (cur.size() == 1) {
476 cur.clearSelection();
478 finishChange(cur, false);
487 //lyxerr << "handle LFUN_RIGHT[SEL]:\n" << cur << endl;
488 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
489 if (isRTL(cur.paragraph()))
493 if (sl == cur.top()) {
495 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
502 //lyxerr << "handle LFUN_LEFT[SEL]:\n" << cur << endl;
503 cur.selHandle(cmd.action == LFUN_LEFTSEL);
504 if (isRTL(cur.paragraph()))
508 if (sl == cur.top()) {
510 cmd = FuncRequest(LFUN_FINISHED_LEFT);
517 //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
518 cur.selHandle(cmd.action == LFUN_UPSEL);
520 if (sl == cur.top()) {
522 cmd = FuncRequest(LFUN_FINISHED_UP);
529 //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
530 cur.selHandle(cmd.action == LFUN_DOWNSEL);
532 if (sl == cur.top()) {
534 cmd = FuncRequest(LFUN_FINISHED_DOWN);
538 case LFUN_UP_PARAGRAPHSEL:
539 if (!cur.selection())
541 cursorUpParagraph(cur);
542 finishChange(cur, true);
545 case LFUN_DOWN_PARAGRAPHSEL:
546 if (!cur.selection())
548 cursorDownParagraph(cur);
549 finishChange(cur, true);
553 if (!cur.selection())
556 finishChange(cur, true);
560 if (!cur.selection())
563 finishChange(cur, true);
567 if (!cur.selection())
570 finishChange(cur, true);
574 if (!cur.selection())
577 finishChange(cur, true);
580 case LFUN_WORDRIGHTSEL:
581 if (!cur.selection())
583 if (isRTL(cur.paragraph()))
584 cursorLeftOneWord(cur);
586 cursorRightOneWord(cur);
587 finishChange(cur, true);
590 case LFUN_WORDLEFTSEL:
591 if (!cur.selection())
593 if (isRTL(cur.paragraph()))
594 cursorRightOneWord(cur);
596 cursorLeftOneWord(cur);
597 finishChange(cur, true);
601 selectWord(cur, lyx::WHOLE_WORD);
602 finishChange(cur, true);
606 case LFUN_UP_PARAGRAPH:
609 cur.clearSelection();
610 cursorUpParagraph(cur);
611 finishChange(cur, false);
614 case LFUN_DOWN_PARAGRAPH:
617 cur.clearSelection();
618 cursorDownParagraph(cur);
619 finishChange(cur, false);
625 cur.clearSelection();
626 finishChange(cur, false);
627 if (cur.par() == 0 && cur.textRow().pos() == 0) {
629 cmd = FuncRequest(LFUN_FINISHED_UP);
638 cur.clearSelection();
639 finishChange(cur, false);
640 if (cur.par() == cur.lastpar()
641 && cur.textRow().endpos() == cur.lastpos()) {
643 cmd = FuncRequest(LFUN_FINISHED_DOWN);
651 cur.clearSelection();
653 finishChange(cur, false);
658 cur.clearSelection();
660 finishChange(cur, false);
663 case LFUN_BREAKLINE: {
664 // Not allowed by LaTeX (labels or empty par)
665 if (cur.pos() > cur.paragraph().beginOfBody()) {
666 lyx::cap::replaceSelection(cur);
667 cur.insert(new InsetNewline);
668 moveCursor(cur, false);
674 if (!cur.selection()) {
677 // It is possible to make it a lot faster still
678 // just comment out the line below...
680 cutSelection(cur, true, false);
682 moveCursor(cur, false);
685 case LFUN_DELETE_SKIP:
686 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
687 if (!cur.selection()) {
688 if (cur.pos() == cur.lastpos()) {
695 cutSelection(cur, true, false);
701 if (!cur.selection()) {
702 if (bv->owner()->getIntl().getTransManager().backspace()) {
705 // It is possible to make it a lot faster still
706 // just comment out the line below...
709 cutSelection(cur, true, false);
714 case LFUN_BACKSPACE_SKIP:
715 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
716 if (!cur.selection()) {
720 //CursorSlice cur = cursor();
724 cutSelection(cur, true, false);
728 case LFUN_BREAKPARAGRAPH:
729 lyx::cap::replaceSelection(cur);
730 breakParagraph(cur, 0);
735 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
736 lyx::cap::replaceSelection(cur);
737 breakParagraph(cur, 1);
742 case LFUN_BREAKPARAGRAPH_SKIP: {
743 // When at the beginning of a paragraph, remove
744 // indentation. Otherwise, do the same as LFUN_BREAKPARAGRAPH.
745 lyx::cap::replaceSelection(cur);
747 cur.paragraph().params().labelWidthString(string());
749 breakParagraph(cur, 0);
755 case LFUN_PARAGRAPH_SPACING: {
756 Paragraph & par = cur.paragraph();
757 Spacing::Space cur_spacing = par.params().spacing().getSpace();
758 float cur_value = 1.0;
759 if (cur_spacing == Spacing::Other)
760 cur_value = par.params().spacing().getValue();
762 istringstream is(cmd.argument);
765 Spacing::Space new_spacing = cur_spacing;
766 float new_value = cur_value;
768 lyxerr << "Missing argument to `paragraph-spacing'"
770 } else if (tmp == "single") {
771 new_spacing = Spacing::Single;
772 } else if (tmp == "onehalf") {
773 new_spacing = Spacing::Onehalf;
774 } else if (tmp == "double") {
775 new_spacing = Spacing::Double;
776 } else if (tmp == "other") {
777 new_spacing = Spacing::Other;
780 lyxerr << "new_value = " << tmpval << endl;
783 } else if (tmp == "default") {
784 new_spacing = Spacing::Default;
786 lyxerr << _("Unknown spacing argument: ")
787 << cmd.argument << endl;
789 if (cur_spacing != new_spacing || cur_value != new_value) {
790 par.params().spacing(Spacing(new_spacing, new_value));
796 case LFUN_INSET_APPLY: {
797 string const name = cmd.getArg(0);
798 InsetBase * inset = bv->owner()->getDialogs().getOpenInset(name);
800 FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
801 inset->dispatch(cur, fr);
803 FuncRequest fr(LFUN_INSET_INSERT, cmd.argument);
809 case LFUN_INSET_INSERT: {
811 InsetBase * inset = createInset(bv, cmd);
813 insertInset(cur, inset);
819 case LFUN_INSET_SETTINGS:
820 if (cur.inset().asUpdatableInset())
821 cur.inset().asUpdatableInset()->showInsetDialog(bv);
824 case LFUN_NEXT_INSET_TOGGLE: {
825 InsetBase * inset = cur.nextInset();
827 cur.clearSelection();
828 FuncRequest fr = cmd;
829 fr.action = LFUN_INSET_TOGGLE;
830 inset->dispatch(cur, fr);
835 case LFUN_KEYMAP_TOGGLE:
836 cur.clearSelection();
840 case LFUN_SPACE_INSERT:
841 if (cur.paragraph().layout()->free_spacing)
842 insertChar(cur, ' ');
844 doInsertInset(cur, this, cmd, false, false);
845 moveCursor(cur, false);
848 case LFUN_HYPHENATION:
849 specialChar(cur, InsetSpecialChar::HYPHENATION);
852 case LFUN_LIGATURE_BREAK:
853 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
857 specialChar(cur, InsetSpecialChar::LDOTS);
860 case LFUN_END_OF_SENTENCE:
861 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
864 case LFUN_MENU_SEPARATOR:
865 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
868 case LFUN_UPCASE_WORD:
869 changeCase(cur, LyXText::text_uppercase);
872 case LFUN_LOWCASE_WORD:
873 changeCase(cur, LyXText::text_lowercase);
876 case LFUN_CAPITALIZE_WORD:
877 changeCase(cur, LyXText::text_capitalization);
880 case LFUN_TRANSPOSE_CHARS:
886 cur.message(_("Paste"));
887 lyx::cap::replaceSelection(cur);
889 #warning FIXME Check if the arg is in the domain of available selections.
891 if (isStrUnsignedInt(cmd.argument))
892 pasteSelection(cur, strToUnsignedInt(cmd.argument));
894 pasteSelection(cur, 0);
895 cur.clearSelection(); // bug 393
901 cutSelection(cur, true, true);
902 cur.message(_("Cut"));
907 cur.message(_("Copy"));
911 cur.message(tostr(cursorX(cur.top())) + ' '
912 + tostr(cursorY(cur.top())));
918 istringstream is(cmd.argument);
921 lyxerr << "SETXY: Could not parse coordinates in '"
922 << cmd.argument << std::endl;
924 setCursorFromCoordinates(cur, x, y);
929 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
931 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
938 cur.message(cur.paragraph().layout()->name());
942 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
943 << cmd.argument << endl;
945 // This is not the good solution to the empty argument
946 // problem, but it will hopefully suffice for 1.2.0.
947 // The correct solution would be to augument the
948 // function list/array with information about what
949 // functions needs arguments and their type.
950 if (cmd.argument.empty()) {
951 cur.errorMessage(_("LyX function 'layout' needs an argument."));
955 // Derive layout number from given argument (string)
956 // and current buffer's textclass (number)
957 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
958 bool hasLayout = tclass.hasLayout(cmd.argument);
959 string layout = cmd.argument;
961 // If the entry is obsolete, use the new one instead.
963 string const & obs = tclass[layout]->obsoleted_by();
969 cur.errorMessage(string(N_("Layout ")) + cmd.argument +
974 bool change_layout = (current_layout != layout);
976 if (!change_layout && cur.selection() &&
977 cur.selBegin().par() != cur.selEnd().par())
979 par_type spit = cur.selBegin().par();
980 par_type epit = cur.selEnd().par() + 1;
981 while (spit != epit) {
982 if (pars_[spit].layout()->name() != current_layout) {
983 change_layout = true;
991 current_layout = layout;
992 setLayout(cur, layout);
993 bv->owner()->setLayout(layout);
999 case LFUN_PASTESELECTION: {
1000 cur.clearSelection();
1001 string const clip = bv->getClipboard();
1002 if (!clip.empty()) {
1003 if (cmd.argument == "paragraph")
1004 insertStringAsParagraphs(cur, clip);
1006 insertStringAsLines(cur, clip);
1011 case LFUN_GOTOERROR:
1012 gotoInset(cur, InsetBase::ERROR_CODE, false);
1016 gotoInset(cur, InsetBase::NOTE_CODE, false);
1019 case LFUN_REFERENCE_GOTO: {
1020 vector<InsetOld_code> tmp;
1021 tmp.push_back(InsetBase::LABEL_CODE);
1022 tmp.push_back(InsetBase::REF_CODE);
1023 gotoInset(cur, tmp, true);
1028 lyx::cap::replaceSelection(cur);
1029 Paragraph & par = cur.paragraph();
1030 lyx::pos_type pos = cur.pos();
1034 else if (cur.prevInset() && cur.prevInset()->isSpace())
1037 c = par.getChar(pos - 1);
1039 LyXLayout_ptr const & style = par.layout();
1041 BufferParams const & bufparams = bv->buffer()->params();
1042 if (!style->pass_thru
1043 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1044 string arg = cmd.argument;
1045 if (arg == "single")
1046 cur.insert(new InsetQuotes(c,
1047 bufparams.quotes_language,
1048 InsetQuotes::SingleQ));
1049 else if (arg == "double")
1050 cur.insert(new InsetQuotes(c,
1051 bufparams.quotes_language,
1052 InsetQuotes::DoubleQ));
1054 cur.insert(new InsetQuotes(c, bufparams));
1058 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1062 case LFUN_DATE_INSERT: {
1063 lyx::cap::replaceSelection(cur);
1064 time_t now_time_t = time(NULL);
1065 struct tm * now_tm = localtime(&now_time_t);
1066 setlocale(LC_TIME, "");
1068 if (!cmd.argument.empty())
1071 arg = lyxrc.date_insert_format;
1073 int const datetmp_len =
1074 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1076 for (int i = 0; i < datetmp_len; i++)
1077 insertChar(cur, datetmp[i]);
1080 moveCursor(cur, false);
1084 case LFUN_MOUSE_TRIPLE:
1085 if (cmd.button() == mouse_button::button1) {
1086 selection_possible = true;
1091 bv->haveSelection(cur.selection());
1095 case LFUN_MOUSE_DOUBLE:
1096 if (cmd.button() == mouse_button::button1) {
1097 selection_possible = true;
1098 selectWord(cur, lyx::WHOLE_WORD_STRICT);
1099 bv->haveSelection(cur.selection());
1103 case LFUN_MOUSE_MOTION: {
1104 // Only use motion with button 1
1105 //if (cmd.button() != mouse_button::button1)
1107 // We want to use only motion events for which
1108 // the button press event was on the drawing area too.
1109 if (!selection_possible) {
1110 lyxerr[Debug::ACTION] << "BufferView::Pimpl::"
1111 "dispatch: no selection possible\n";
1112 lyxerr << "BufferView::Pimpl::dispatch: no selection possible\n";
1116 // ignore motions deeper nested than the real anchor
1117 LCursor & bvcur = cur.bv().cursor();
1118 if (bvcur.selection() && bvcur.anchor_.size() < cur.size())
1121 CursorSlice old = cur.top();
1122 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1124 // This is to allow jumping over large insets
1125 // FIXME: shouldn't be top-text-specific
1126 if (isMainText() && cur.top() == old) {
1127 if (cmd.y - bv->top_y() >= bv->workHeight())
1129 else if (cmd.y - bv->top_y() < 0)
1133 // don't set anchor_
1134 bv->cursor().setCursor(cur, true);
1135 lyxerr << "MOTION: " << bv->cursor() << endl;
1139 // Single-click on work area
1140 case LFUN_MOUSE_PRESS: {
1141 // Right click on a footnote flag opens float menu
1142 if (cmd.button() == mouse_button::button3) {
1143 cur.clearSelection();
1144 selection_possible = false;
1148 // Middle button press pastes if we have a selection
1149 // We do this here as if the selection was inside an inset
1150 // it could get cleared on the unlocking of the inset so
1151 // we have to check this first
1152 bool paste_internally = false;
1153 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1154 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1155 paste_internally = true;
1158 selection_possible = true;
1160 // Clear the selection
1161 cur.clearSelection();
1163 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1166 cur.x_target() = cursorX(cur.top());
1168 // Has the cursor just left the inset?
1169 if (bv->cursor().inMathed() && !cur.inMathed())
1170 bv->cursor().inset().notifyCursorLeaves(bv->cursor());
1175 // Don't allow selection after a big jump.
1176 if (bv->fitCursor())
1177 selection_possible = false;
1179 // Insert primary selection with middle mouse
1180 // if there is a local selection in the current buffer,
1182 if (cmd.button() == mouse_button::button2) {
1183 if (paste_internally)
1184 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1186 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1187 selection_possible = false;
1193 case LFUN_MOUSE_RELEASE: {
1194 selection_possible = false;
1196 if (cmd.button() == mouse_button::button2)
1200 if (cmd.button() == mouse_button::button1)
1201 bv->haveSelection(cur.selection());
1204 bv->owner()->updateMenubar();
1205 bv->owner()->updateToolbars();
1209 case LFUN_SELFINSERT: {
1210 if (cmd.argument.empty())
1213 // Automatically delete the currently selected
1214 // text and replace it with what is being
1215 // typed in now. Depends on lyxrc settings
1216 // "auto_region_delete", which defaults to
1219 if (lyxrc.auto_region_delete) {
1220 if (cur.selection())
1221 cutSelection(cur, false, false);
1222 bv->haveSelection(false);
1225 cur.clearSelection();
1226 LyXFont const old_font = real_current_font;
1228 string::const_iterator cit = cmd.argument.begin();
1229 string::const_iterator end = cmd.argument.end();
1230 for (; cit != end; ++cit)
1231 bv->owner()->getIntl().getTransManager().
1232 TranslateAndInsert(*cit, this);
1235 moveCursor(cur, false);
1237 // real_current_font.number can change so we need to
1238 // update the minibuffer
1239 if (old_font != real_current_font)
1240 bv->updateScrollbar();
1245 InsetCommandParams p("url");
1246 string const data = InsetCommandMailer::params2string("url", p);
1247 bv->owner()->getDialogs().show("url", data, 0);
1251 case LFUN_HTMLURL: {
1252 InsetCommandParams p("htmlurl");
1253 string const data = InsetCommandMailer::params2string("url", p);
1254 bv->owner()->getDialogs().show("url", data, 0);
1258 case LFUN_INSERT_LABEL: {
1259 // Try to generate a valid label
1260 string const contents = cmd.argument.empty() ?
1261 cur.getPossibleLabel() : cmd.argument;
1263 InsetCommandParams p("label", contents);
1264 string const data = InsetCommandMailer::params2string("label", p);
1266 if (cmd.argument.empty()) {
1267 bv->owner()->getDialogs().show("label", data, 0);
1269 FuncRequest fr(LFUN_INSET_INSERT, data);
1277 case LFUN_INSET_LIST:
1278 case LFUN_INSET_THEOREM:
1279 case LFUN_INSET_CAPTION:
1281 case LFUN_INSERT_NOTE:
1282 case LFUN_INSERT_CHARSTYLE:
1283 case LFUN_INSERT_BOX:
1284 case LFUN_INSERT_BRANCH:
1285 case LFUN_INSERT_BIBITEM:
1286 case LFUN_INSET_ERT:
1287 case LFUN_INSET_FLOAT:
1288 case LFUN_INSET_FOOTNOTE:
1289 case LFUN_INSET_MARGINAL:
1290 case LFUN_INSET_OPTARG:
1291 case LFUN_INSET_WIDE_FLOAT:
1292 case LFUN_INSET_WRAP:
1293 case LFUN_TABULAR_INSERT:
1294 case LFUN_ENVIRONMENT_INSERT:
1295 // Open the inset, and move the current selection
1297 doInsertInset(cur, this, cmd, true, true);
1301 case LFUN_INDEX_INSERT:
1302 // Just open the inset
1303 doInsertInset(cur, this, cmd, true, false);
1307 case LFUN_INDEX_PRINT:
1308 case LFUN_TOC_INSERT:
1310 case LFUN_INSERT_LINE:
1311 case LFUN_INSERT_PAGEBREAK:
1313 doInsertInset(cur, this, cmd, false, false);
1317 case LFUN_DEPTH_MIN:
1318 changeDepth(cur, DEC_DEPTH);
1321 case LFUN_DEPTH_PLUS:
1322 changeDepth(cur, INC_DEPTH);
1325 case LFUN_MATH_DISPLAY:
1326 mathDispatch(cur, cmd, true);
1329 case LFUN_MATH_IMPORT_SELECTION:
1330 case LFUN_MATH_MODE:
1331 mathDispatch(cur, cmd, false);
1334 case LFUN_MATH_MACRO:
1335 if (cmd.argument.empty())
1336 cur.errorMessage(N_("Missing argument"));
1338 string s = cmd.argument;
1339 string const s1 = token(s, ' ', 1);
1340 int const nargs = s1.empty() ? 0 : atoi(s1);
1341 string const s2 = token(s, ' ', 2);
1342 string const type = s2.empty() ? "newcommand" : s2;
1343 cur.insert(new MathMacroTemplate(token(s, ' ', 0), nargs, s2));
1344 //cur.nextInset()->edit(cur, true);
1348 case LFUN_INSERT_MATH:
1349 case LFUN_INSERT_MATRIX:
1350 case LFUN_MATH_DELIM: {
1351 cur.insert(new MathHullInset);
1352 cur.dispatch(FuncRequest(LFUN_RIGHT));
1353 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
1359 LyXFont font(LyXFont::ALL_IGNORE);
1360 font.setEmph(LyXFont::TOGGLE);
1361 toggleAndShow(cur, this, font);
1366 LyXFont font(LyXFont::ALL_IGNORE);
1367 font.setSeries(LyXFont::BOLD_SERIES);
1368 toggleAndShow(cur, this, font);
1373 LyXFont font(LyXFont::ALL_IGNORE);
1374 font.setNoun(LyXFont::TOGGLE);
1375 toggleAndShow(cur, this, font);
1380 LyXFont font(LyXFont::ALL_IGNORE);
1381 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1382 toggleAndShow(cur, this, font);
1387 LyXFont font(LyXFont::ALL_IGNORE);
1388 font.setFamily(LyXFont::SANS_FAMILY);
1389 toggleAndShow(cur, this, font);
1394 LyXFont font(LyXFont::ALL_IGNORE);
1395 font.setFamily(LyXFont::ROMAN_FAMILY);
1396 toggleAndShow(cur, this, font);
1400 case LFUN_DEFAULT: {
1401 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1402 toggleAndShow(cur, this, font);
1406 case LFUN_UNDERLINE: {
1407 LyXFont font(LyXFont::ALL_IGNORE);
1408 font.setUnderbar(LyXFont::TOGGLE);
1409 toggleAndShow(cur, this, font);
1413 case LFUN_FONT_SIZE: {
1414 LyXFont font(LyXFont::ALL_IGNORE);
1415 font.setLyXSize(cmd.argument);
1416 toggleAndShow(cur, this, font);
1420 case LFUN_LANGUAGE: {
1421 Language const * lang = languages.getLanguage(cmd.argument);
1424 LyXFont font(LyXFont::ALL_IGNORE);
1425 font.setLanguage(lang);
1426 toggleAndShow(cur, this, font);
1431 case LFUN_FREEFONT_APPLY:
1432 toggleAndShow(cur, this, freefont, toggleall);
1433 cur.message(_("Character set"));
1436 // Set the freefont using the contents of \param data dispatched from
1437 // the frontends and apply it at the current cursor location.
1438 case LFUN_FREEFONT_UPDATE: {
1441 if (bv_funcs::string2font(cmd.argument, font, toggle)) {
1444 toggleAndShow(cur, this, freefont, toggleall);
1445 cur.message(_("Character set"));
1450 case LFUN_FINISHED_LEFT:
1451 lyxerr << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1454 case LFUN_FINISHED_RIGHT:
1455 lyxerr << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1459 case LFUN_FINISHED_UP:
1460 lyxerr << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1464 case LFUN_FINISHED_DOWN:
1465 lyxerr << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1469 case LFUN_LAYOUT_PARAGRAPH: {
1471 params2string(cur.paragraph(), data);
1472 data = "show\n" + data;
1473 bv->owner()->getDialogs().show("paragraph", data);
1477 case LFUN_PARAGRAPH_UPDATE: {
1478 if (!bv->owner()->getDialogs().visible("paragraph"))
1481 params2string(cur.paragraph(), data);
1483 // Will the paragraph accept changes from the dialog?
1484 InsetBase & inset = cur.inset();
1485 bool const accept = !inset.forceDefaultParagraphs(&inset);
1487 data = "update " + tostr(accept) + '\n' + data;
1488 bv->owner()->getDialogs().update("paragraph", data);
1493 case LFUN_CIRCUMFLEX:
1503 case LFUN_SPECIAL_CARON:
1506 case LFUN_HUNG_UMLAUT:
1509 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1510 if (!cmd.argument.empty())
1511 bv->owner()->getIntl().getTransManager()
1512 .TranslateAndInsert(cmd.argument[0], this);
1515 case LFUN_FLOAT_LIST: {
1516 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1517 if (tclass.floats().typeExist(cmd.argument)) {
1518 // not quite sure if we want this...
1520 cur.clearSelection();
1521 breakParagraph(cur);
1523 if (cur.lastpos() != 0) {
1525 breakParagraph(cur);
1528 setLayout(cur, tclass.defaultLayoutName());
1529 setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1530 insertInset(cur, new InsetFloatList(cmd.argument));
1532 lyxerr << "Non-existent float type: "
1533 << cmd.argument << endl;
1538 case LFUN_ACCEPT_CHANGE: {
1543 case LFUN_REJECT_CHANGE: {
1548 case LFUN_THESAURUS_ENTRY: {
1549 string arg = cmd.argument;
1551 arg = cur.selectionAsString(false);
1553 if (arg.size() > 100 || arg.empty()) {
1554 // Get word or selection
1555 selectWordWhenUnderCursor(cur, lyx::WHOLE_WORD);
1556 arg = cur.selectionAsString(false);
1559 bv->owner()->getDialogs().show("thesaurus", arg);
1563 case LFUN_PARAGRAPH_APPLY: {
1564 // Given data, an encoding of the ParagraphParameters
1565 // generated in the Paragraph dialog, this function sets
1566 // the current paragraph appropriately.
1567 istringstream is(cmd.argument);
1570 ParagraphParameters params;
1575 params.labelWidthString(),
1577 cur.message(_("Paragraph layout set"));
1581 case LFUN_INSET_DIALOG_SHOW: {
1582 InsetBase * inset = cur.nextInset();
1584 FuncRequest fr(LFUN_INSET_DIALOG_SHOW);
1585 inset->dispatch(cur, fr);
1591 if (cur.selection()) {
1592 cur.selection() = false;
1595 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1604 // avoid to update when navigating
1606 && &sl.inset() == &cur.inset()
1607 && sl.idx() == cur.idx()
1609 && cur.selection() == false)
1614 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1615 FuncStatus & flag) const
1617 BOOST_ASSERT(cur.text() == this);
1618 LyXFont const & font = real_current_font;
1621 switch (cmd.action) {
1623 case LFUN_DEPTH_MIN:
1624 enable = changeDepthAllowed(cur, DEC_DEPTH);
1627 case LFUN_DEPTH_PLUS:
1628 enable = changeDepthAllowed(cur, INC_DEPTH);
1631 case LFUN_INSET_OPTARG:
1632 enable = cur.paragraph().layout()->optionalargs;
1636 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1640 // the functions which insert insets
1641 InsetOld::Code code = InsetOld::NO_CODE;
1642 switch (cmd.action) {
1643 case LFUN_DIALOG_SHOW_NEW_INSET:
1644 if (cmd.argument == "bibitem")
1645 code = InsetOld::BIBITEM_CODE;
1646 else if (cmd.argument == "bibtex")
1647 code = InsetOld::BIBTEX_CODE;
1648 else if (cmd.argument == "box")
1649 code = InsetOld::BOX_CODE;
1650 else if (cmd.argument == "branch")
1651 code = InsetOld::BRANCH_CODE;
1652 else if (cmd.argument == "citation")
1653 code = InsetOld::CITE_CODE;
1654 else if (cmd.argument == "ert")
1655 code = InsetOld::ERT_CODE;
1656 else if (cmd.argument == "external")
1657 code = InsetOld::EXTERNAL_CODE;
1658 else if (cmd.argument == "float")
1659 code = InsetOld::FLOAT_CODE;
1660 else if (cmd.argument == "graphics")
1661 code = InsetOld::GRAPHICS_CODE;
1662 else if (cmd.argument == "include")
1663 code = InsetOld::INCLUDE_CODE;
1664 else if (cmd.argument == "index")
1665 code = InsetOld::INDEX_CODE;
1666 else if (cmd.argument == "label")
1667 code = InsetOld::LABEL_CODE;
1668 else if (cmd.argument == "note")
1669 code = InsetOld::NOTE_CODE;
1670 else if (cmd.argument == "ref")
1671 code = InsetOld::REF_CODE;
1672 else if (cmd.argument == "toc")
1673 code = InsetOld::TOC_CODE;
1674 else if (cmd.argument == "url")
1675 code = InsetOld::URL_CODE;
1676 else if (cmd.argument == "vspace")
1677 code = InsetOld::VSPACE_CODE;
1678 else if (cmd.argument == "wrap")
1679 code = InsetOld::WRAP_CODE;
1682 case LFUN_INSET_ERT:
1683 code = InsetOld::ERT_CODE;
1685 case LFUN_INSET_FOOTNOTE:
1686 code = InsetOld::FOOT_CODE;
1688 case LFUN_TABULAR_INSERT:
1689 code = InsetOld::TABULAR_CODE;
1691 case LFUN_INSET_MARGINAL:
1692 code = InsetOld::MARGIN_CODE;
1694 case LFUN_INSET_FLOAT:
1695 case LFUN_INSET_WIDE_FLOAT:
1696 code = InsetOld::FLOAT_CODE;
1698 case LFUN_INSET_WRAP:
1699 code = InsetOld::WRAP_CODE;
1701 case LFUN_FLOAT_LIST:
1702 code = InsetOld::FLOAT_LIST_CODE;
1705 case LFUN_INSET_LIST:
1706 code = InsetOld::LIST_CODE;
1708 case LFUN_INSET_THEOREM:
1709 code = InsetOld::THEOREM_CODE;
1712 case LFUN_INSET_CAPTION:
1713 code = InsetOld::CAPTION_CODE;
1715 case LFUN_INSERT_NOTE:
1716 code = InsetOld::NOTE_CODE;
1718 case LFUN_INSERT_CHARSTYLE:
1719 code = InsetOld::CHARSTYLE_CODE;
1720 if (buf->params().getLyXTextClass().charstyles().empty())
1723 case LFUN_INSERT_BOX:
1724 code = InsetOld::BOX_CODE;
1726 case LFUN_INSERT_BRANCH:
1727 code = InsetOld::BRANCH_CODE;
1728 if (buf->params().branchlist().empty())
1731 case LFUN_INSERT_LABEL:
1732 code = InsetOld::LABEL_CODE;
1734 case LFUN_INSET_OPTARG:
1735 code = InsetOld::OPTARG_CODE;
1737 case LFUN_ENVIRONMENT_INSERT:
1738 code = InsetOld::BOX_CODE;
1740 case LFUN_INDEX_INSERT:
1741 code = InsetOld::INDEX_CODE;
1743 case LFUN_INDEX_PRINT:
1744 code = InsetOld::INDEX_PRINT_CODE;
1746 case LFUN_TOC_INSERT:
1747 code = InsetOld::TOC_CODE;
1751 code = InsetOld::URL_CODE;
1754 // always allow this, since we will inset a raw quote
1755 // if an inset is not allowed.
1757 case LFUN_HYPHENATION:
1758 case LFUN_LIGATURE_BREAK:
1760 case LFUN_MENU_SEPARATOR:
1762 case LFUN_END_OF_SENTENCE:
1763 code = InsetOld::SPECIALCHAR_CODE;
1765 case LFUN_SPACE_INSERT:
1766 // slight hack: we know this is allowed in math mode
1768 code = InsetOld::SPACE_CODE;
1770 case LFUN_INSET_DIALOG_SHOW: {
1771 InsetBase * inset = cur.nextInset();
1774 code = inset->lyxCode();
1775 if (!(code == InsetOld::INCLUDE_CODE
1776 || code == InsetOld::BIBTEX_CODE
1777 || code == InsetOld::FLOAT_LIST_CODE
1778 || code == InsetOld::TOC_CODE))
1787 if (code != InsetOld::NO_CODE
1788 && (cur.empty() || !cur.inset().insetAllowed(code)))
1793 case LFUN_DIALOG_SHOW_NEW_INSET:
1794 case LFUN_INSET_ERT:
1795 case LFUN_INSERT_BOX:
1796 case LFUN_INSERT_BRANCH:
1797 case LFUN_ENVIRONMENT_INSERT:
1798 case LFUN_INDEX_INSERT:
1799 case LFUN_INDEX_PRINT:
1800 case LFUN_TOC_INSERT:
1804 case LFUN_HYPHENATION:
1805 case LFUN_LIGATURE_BREAK:
1807 case LFUN_MENU_SEPARATOR:
1809 case LFUN_END_OF_SENTENCE:
1810 case LFUN_SPACE_INSERT:
1811 case LFUN_INSET_DIALOG_SHOW:
1815 flag.setOnOff(font.emph() == LyXFont::ON);
1819 flag.setOnOff(font.noun() == LyXFont::ON);
1823 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1827 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1831 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1835 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1838 case LFUN_DELETE_WORD_FORWARD:
1839 case LFUN_DELETE_WORD_BACKWARD:
1840 case LFUN_DELETE_LINE_FORWARD:
1841 case LFUN_WORDRIGHT:
1851 case LFUN_UP_PARAGRAPHSEL:
1852 case LFUN_DOWN_PARAGRAPHSEL:
1857 case LFUN_WORDRIGHTSEL:
1858 case LFUN_WORDLEFTSEL:
1860 case LFUN_UP_PARAGRAPH:
1861 case LFUN_DOWN_PARAGRAPH:
1866 case LFUN_BREAKLINE:
1868 case LFUN_DELETE_SKIP:
1869 case LFUN_BACKSPACE:
1870 case LFUN_BACKSPACE_SKIP:
1871 case LFUN_BREAKPARAGRAPH:
1872 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1873 case LFUN_BREAKPARAGRAPH_SKIP:
1874 case LFUN_PARAGRAPH_SPACING:
1875 case LFUN_INSET_APPLY:
1876 case LFUN_INSET_INSERT:
1877 case LFUN_NEXT_INSET_TOGGLE:
1878 case LFUN_UPCASE_WORD:
1879 case LFUN_LOWCASE_WORD:
1880 case LFUN_CAPITALIZE_WORD:
1881 case LFUN_TRANSPOSE_CHARS:
1888 case LFUN_GETLAYOUT:
1890 case LFUN_PASTESELECTION:
1891 case LFUN_GOTOERROR:
1893 case LFUN_REFERENCE_GOTO:
1894 case LFUN_DATE_INSERT:
1895 case LFUN_SELFINSERT:
1896 case LFUN_INSERT_LABEL:
1897 case LFUN_INSERT_NOTE:
1898 case LFUN_INSERT_CHARSTYLE:
1899 case LFUN_INSERT_BIBITEM:
1900 case LFUN_INSET_FLOAT:
1901 case LFUN_INSET_FOOTNOTE:
1902 case LFUN_INSET_MARGINAL:
1903 case LFUN_INSET_WIDE_FLOAT:
1904 case LFUN_INSET_WRAP:
1905 case LFUN_TABULAR_INSERT:
1906 case LFUN_INSERT_LINE:
1907 case LFUN_INSERT_PAGEBREAK:
1908 case LFUN_MATH_DISPLAY:
1909 case LFUN_MATH_IMPORT_SELECTION:
1910 case LFUN_MATH_MODE:
1911 case LFUN_MATH_MACRO:
1912 case LFUN_INSERT_MATH:
1913 case LFUN_INSERT_MATRIX:
1914 case LFUN_MATH_DELIM:
1916 case LFUN_UNDERLINE:
1917 case LFUN_FONT_SIZE:
1919 case LFUN_FREEFONT_APPLY:
1920 case LFUN_FREEFONT_UPDATE:
1921 case LFUN_LAYOUT_PARAGRAPH:
1922 case LFUN_PARAGRAPH_UPDATE:
1924 case LFUN_CIRCUMFLEX:
1934 case LFUN_SPECIAL_CARON:
1937 case LFUN_HUNG_UMLAUT:
1940 case LFUN_FLOAT_LIST:
1941 case LFUN_ACCEPT_CHANGE:
1942 case LFUN_REJECT_CHANGE:
1943 case LFUN_THESAURUS_ENTRY:
1944 case LFUN_PARAGRAPH_APPLY:
1946 case LFUN_KEYMAP_TOGGLE:
1947 // these are handled in our dispatch()
1952 case LFUN_BEGINNINGBUF:
1960 flag.enabled(enable);