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[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
200 << ": x: " << x << " y: " << y
201 << " pit: " << pit << " end: " << end << endl;
202 for (; pit != end; ++pit) {
203 InsetList::const_iterator iit = pars_[pit].insetlist.begin();
204 InsetList::const_iterator iend = pars_[pit].insetlist.end();
205 for (; iit != iend; ++iit) {
206 InsetBase * inset = iit->inset;
209 << "examining inset " << inset << endl;
210 if (theCoords.insets_.has(inset))
212 << " xo: " << inset->xo() << "..." << inset->xo() + inset->width()
213 << " yo: " << inset->yo() - inset->ascent() << "..."
214 << inset->yo() + inset->descent() << endl;
216 lyxerr << " inset has no cached position";
218 if (inset->covers(x, y)) {
220 << "Hit inset: " << inset << endl;
225 lyxerr[Debug::DEBUG] << "No inset hit. " << endl;
230 bool LyXText::gotoNextInset(LCursor & cur,
231 vector<InsetOld_code> const & codes, string const & contents)
233 BOOST_ASSERT(this == cur.text());
234 par_type end = paragraphs().size();
235 par_type pit = cur.par();
236 pos_type pos = cur.pos();
240 if (pos + 1 < pars_[pit].size()) {
247 } while (pit != end &&
248 !(pars_[pit].isInset(pos) &&
249 (inset = pars_[pit].getInset(pos)) != 0 &&
250 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
252 static_cast<InsetCommand *>(pars_[pit].getInset(pos))->getContents()
258 setCursor(cur, pit, pos, false);
263 void LyXText::gotoInset(LCursor & cur,
264 vector<InsetOld_code> const & codes, bool same_content)
266 cur.clearSelection();
270 && cur.pos() < cur.lastpos()
271 && cur.paragraph().isInset(cur.pos())) {
272 InsetBase const * inset = cur.paragraph().getInset(cur.pos());
273 if (find(codes.begin(), codes.end(), inset->lyxCode())
275 contents = static_cast<InsetCommand const *>(inset)->getContents();
278 if (!gotoNextInset(cur, codes, contents)) {
279 if (cur.pos() || cur.par() != 0) {
280 CursorSlice tmp = cur.top();
283 if (!gotoNextInset(cur, codes, contents)) {
285 cur.message(_("No more insets"));
288 cur.message(_("No more insets"));
295 void LyXText::gotoInset(LCursor & cur, InsetOld_code code, bool same_content)
297 gotoInset(cur, vector<InsetOld_code>(1, code), same_content);
301 void LyXText::cursorPrevious(LCursor & cur)
303 pos_type cpos = cur.pos();
304 lyx::par_type cpar = cur.par();
306 int x = cur.x_target();
307 int y = cur.bv().top_y();
308 setCursorFromCoordinates(cur, x, y);
310 if (cpar == cur.par() && cpos == cur.pos()) {
311 // we have a row which is taller than the workarea. The
312 // simplest solution is to move to the previous row instead.
316 cur.bv().updateScrollbar();
321 void LyXText::cursorNext(LCursor & cur)
323 pos_type cpos = cur.pos();
324 lyx::par_type cpar = cur.par();
326 int x = cur.x_target();
327 int y = cur.bv().top_y() + cur.bv().workHeight();
328 setCursorFromCoordinates(cur, x, y);
330 if (cpar == cur.par() && cpos == cur.pos()) {
331 // we have a row which is taller than the workarea. The
332 // simplest solution is to move to the next row instead.
336 cur.bv().updateScrollbar();
343 void specialChar(LCursor & cur, InsetSpecialChar::Kind kind)
345 lyx::cap::replaceSelection(cur);
346 cur.insert(new InsetSpecialChar(kind));
350 void doInsertInset(LCursor & cur, LyXText * text,
351 FuncRequest const & cmd, bool edit, bool pastesel)
353 InsetBase * inset = createInset(&cur.bv(), cmd);
359 if (cur.selection()) {
360 cur.bv().owner()->dispatch(FuncRequest(LFUN_CUT));
363 text->insertInset(cur, inset);
366 inset->edit(cur, true);
368 if (gotsel && pastesel)
369 cur.bv().owner()->dispatch(FuncRequest(LFUN_PASTE));
375 void LyXText::number(LCursor & cur)
377 LyXFont font(LyXFont::ALL_IGNORE);
378 font.setNumber(LyXFont::TOGGLE);
379 toggleAndShow(cur, this, font);
383 bool LyXText::isRTL(Paragraph const & par) const
385 return par.isRightToLeftPar(bv()->buffer()->params());
389 void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
391 lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
392 //lyxerr << "*** LyXText::dispatch: cmd: " << cmd << endl;
394 BOOST_ASSERT(cur.text() == this);
395 BufferView * bv = &cur.bv();
396 CursorSlice sl = cur.top();
397 bool sel = cur.selection();
400 switch (cmd.action) {
402 case LFUN_APPENDIX: {
403 Paragraph & par = cur.paragraph();
404 bool start = !par.params().startOfAppendix();
406 // ensure that we have only one start_of_appendix in this document
407 for (par_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
408 if (pars_[tmp].params().startOfAppendix()) {
410 pars_[tmp].params().startOfAppendix(false);
417 par.params().startOfAppendix(start);
419 // we can set the refreshing parameters now
425 case LFUN_DELETE_WORD_FORWARD:
426 cur.clearSelection();
427 deleteWordForward(cur);
428 finishChange(cur, false);
431 case LFUN_DELETE_WORD_BACKWARD:
432 cur.clearSelection();
433 deleteWordBackward(cur);
434 finishChange(cur, false);
437 case LFUN_DELETE_LINE_FORWARD:
438 cur.clearSelection();
439 deleteLineForward(cur);
440 finishChange(cur, false);
446 cur.clearSelection();
447 if (isRTL(cur.paragraph()))
448 cursorLeftOneWord(cur);
450 cursorRightOneWord(cur);
451 finishChange(cur, false);
457 cur.clearSelection();
458 if (isRTL(cur.paragraph()))
459 cursorRightOneWord(cur);
461 cursorLeftOneWord(cur);
462 finishChange(cur, false);
465 case LFUN_BEGINNINGBUF:
466 if (cur.size() == 1) {
468 cur.clearSelection();
470 finishChange(cur, false);
477 if (cur.size() == 1) {
479 cur.clearSelection();
481 finishChange(cur, false);
490 //lyxerr << "handle LFUN_RIGHT[SEL]:\n" << cur << endl;
491 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
492 if (isRTL(cur.paragraph()))
496 if (sl == cur.top()) {
498 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
505 //lyxerr << "handle LFUN_LEFT[SEL]:\n" << cur << endl;
506 cur.selHandle(cmd.action == LFUN_LEFTSEL);
507 if (isRTL(cur.paragraph()))
511 if (sl == cur.top()) {
513 cmd = FuncRequest(LFUN_FINISHED_LEFT);
520 //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
521 cur.selHandle(cmd.action == LFUN_UPSEL);
523 if (sl == cur.top()) {
525 cmd = FuncRequest(LFUN_FINISHED_UP);
532 //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
533 cur.selHandle(cmd.action == LFUN_DOWNSEL);
535 if (sl == cur.top()) {
537 cmd = FuncRequest(LFUN_FINISHED_DOWN);
541 case LFUN_UP_PARAGRAPHSEL:
542 if (!cur.selection())
544 cursorUpParagraph(cur);
545 finishChange(cur, true);
548 case LFUN_DOWN_PARAGRAPHSEL:
549 if (!cur.selection())
551 cursorDownParagraph(cur);
552 finishChange(cur, true);
556 if (!cur.selection())
559 finishChange(cur, true);
563 if (!cur.selection())
566 finishChange(cur, true);
570 if (!cur.selection())
573 finishChange(cur, true);
577 if (!cur.selection())
580 finishChange(cur, true);
583 case LFUN_WORDRIGHTSEL:
584 if (!cur.selection())
586 if (isRTL(cur.paragraph()))
587 cursorLeftOneWord(cur);
589 cursorRightOneWord(cur);
590 finishChange(cur, true);
593 case LFUN_WORDLEFTSEL:
594 if (!cur.selection())
596 if (isRTL(cur.paragraph()))
597 cursorRightOneWord(cur);
599 cursorLeftOneWord(cur);
600 finishChange(cur, true);
604 selectWord(cur, lyx::WHOLE_WORD);
605 finishChange(cur, true);
609 case LFUN_UP_PARAGRAPH:
612 cur.clearSelection();
613 cursorUpParagraph(cur);
614 finishChange(cur, false);
617 case LFUN_DOWN_PARAGRAPH:
620 cur.clearSelection();
621 cursorDownParagraph(cur);
622 finishChange(cur, false);
628 cur.clearSelection();
629 finishChange(cur, false);
630 if (cur.par() == 0 && cur.textRow().pos() == 0) {
632 cmd = FuncRequest(LFUN_FINISHED_UP);
641 cur.clearSelection();
642 finishChange(cur, false);
643 if (cur.par() == cur.lastpar()
644 && cur.textRow().endpos() == cur.lastpos()) {
646 cmd = FuncRequest(LFUN_FINISHED_DOWN);
654 cur.clearSelection();
656 finishChange(cur, false);
661 cur.clearSelection();
663 finishChange(cur, false);
666 case LFUN_BREAKLINE: {
667 // Not allowed by LaTeX (labels or empty par)
668 if (cur.pos() > cur.paragraph().beginOfBody()) {
669 lyx::cap::replaceSelection(cur);
670 cur.insert(new InsetNewline);
671 moveCursor(cur, false);
677 if (!cur.selection()) {
680 // It is possible to make it a lot faster still
681 // just comment out the line below...
683 cutSelection(cur, true, false);
685 moveCursor(cur, false);
688 case LFUN_DELETE_SKIP:
689 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
690 if (!cur.selection()) {
691 if (cur.pos() == cur.lastpos()) {
698 cutSelection(cur, true, false);
704 if (!cur.selection()) {
705 if (bv->owner()->getIntl().getTransManager().backspace()) {
708 // It is possible to make it a lot faster still
709 // just comment out the line below...
712 cutSelection(cur, true, false);
717 case LFUN_BACKSPACE_SKIP:
718 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
719 if (!cur.selection()) {
723 //CursorSlice cur = cursor();
727 cutSelection(cur, true, false);
731 case LFUN_BREAKPARAGRAPH:
732 lyx::cap::replaceSelection(cur);
733 breakParagraph(cur, 0);
738 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
739 lyx::cap::replaceSelection(cur);
740 breakParagraph(cur, 1);
745 case LFUN_BREAKPARAGRAPH_SKIP: {
746 // When at the beginning of a paragraph, remove
747 // indentation. Otherwise, do the same as LFUN_BREAKPARAGRAPH.
748 lyx::cap::replaceSelection(cur);
750 cur.paragraph().params().labelWidthString(string());
752 breakParagraph(cur, 0);
758 case LFUN_PARAGRAPH_SPACING: {
759 Paragraph & par = cur.paragraph();
760 Spacing::Space cur_spacing = par.params().spacing().getSpace();
761 float cur_value = 1.0;
762 if (cur_spacing == Spacing::Other)
763 cur_value = par.params().spacing().getValue();
765 istringstream is(cmd.argument);
768 Spacing::Space new_spacing = cur_spacing;
769 float new_value = cur_value;
771 lyxerr << "Missing argument to `paragraph-spacing'"
773 } else if (tmp == "single") {
774 new_spacing = Spacing::Single;
775 } else if (tmp == "onehalf") {
776 new_spacing = Spacing::Onehalf;
777 } else if (tmp == "double") {
778 new_spacing = Spacing::Double;
779 } else if (tmp == "other") {
780 new_spacing = Spacing::Other;
783 lyxerr << "new_value = " << tmpval << endl;
786 } else if (tmp == "default") {
787 new_spacing = Spacing::Default;
789 lyxerr << _("Unknown spacing argument: ")
790 << cmd.argument << endl;
792 if (cur_spacing != new_spacing || cur_value != new_value) {
793 par.params().spacing(Spacing(new_spacing, new_value));
799 case LFUN_INSET_APPLY: {
800 string const name = cmd.getArg(0);
801 InsetBase * inset = bv->owner()->getDialogs().getOpenInset(name);
803 FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
804 inset->dispatch(cur, fr);
806 FuncRequest fr(LFUN_INSET_INSERT, cmd.argument);
812 case LFUN_INSET_INSERT: {
814 InsetBase * inset = createInset(bv, cmd);
816 insertInset(cur, inset);
822 case LFUN_INSET_SETTINGS:
823 if (cur.inset().asUpdatableInset())
824 cur.inset().asUpdatableInset()->showInsetDialog(bv);
827 case LFUN_NEXT_INSET_TOGGLE: {
828 InsetBase * inset = cur.nextInset();
830 cur.clearSelection();
831 FuncRequest fr = cmd;
832 fr.action = LFUN_INSET_TOGGLE;
833 inset->dispatch(cur, fr);
838 case LFUN_KEYMAP_TOGGLE:
839 cur.clearSelection();
843 case LFUN_SPACE_INSERT:
844 if (cur.paragraph().layout()->free_spacing)
845 insertChar(cur, ' ');
847 doInsertInset(cur, this, cmd, false, false);
848 moveCursor(cur, false);
851 case LFUN_HYPHENATION:
852 specialChar(cur, InsetSpecialChar::HYPHENATION);
855 case LFUN_LIGATURE_BREAK:
856 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
860 specialChar(cur, InsetSpecialChar::LDOTS);
863 case LFUN_END_OF_SENTENCE:
864 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
867 case LFUN_MENU_SEPARATOR:
868 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
871 case LFUN_UPCASE_WORD:
872 changeCase(cur, LyXText::text_uppercase);
875 case LFUN_LOWCASE_WORD:
876 changeCase(cur, LyXText::text_lowercase);
879 case LFUN_CAPITALIZE_WORD:
880 changeCase(cur, LyXText::text_capitalization);
883 case LFUN_TRANSPOSE_CHARS:
889 cur.message(_("Paste"));
890 lyx::cap::replaceSelection(cur);
892 #warning FIXME Check if the arg is in the domain of available selections.
894 if (isStrUnsignedInt(cmd.argument))
895 pasteSelection(cur, strToUnsignedInt(cmd.argument));
897 pasteSelection(cur, 0);
898 cur.clearSelection(); // bug 393
904 cutSelection(cur, true, true);
905 cur.message(_("Cut"));
910 cur.message(_("Copy"));
914 cur.message(tostr(cursorX(cur.top())) + ' '
915 + tostr(cursorY(cur.top())));
921 istringstream is(cmd.argument);
924 lyxerr << "SETXY: Could not parse coordinates in '"
925 << cmd.argument << std::endl;
927 setCursorFromCoordinates(cur, x, y);
932 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
934 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
941 cur.message(cur.paragraph().layout()->name());
945 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
946 << cmd.argument << endl;
948 // This is not the good solution to the empty argument
949 // problem, but it will hopefully suffice for 1.2.0.
950 // The correct solution would be to augument the
951 // function list/array with information about what
952 // functions needs arguments and their type.
953 if (cmd.argument.empty()) {
954 cur.errorMessage(_("LyX function 'layout' needs an argument."));
958 // Derive layout number from given argument (string)
959 // and current buffer's textclass (number)
960 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
961 bool hasLayout = tclass.hasLayout(cmd.argument);
962 string layout = cmd.argument;
964 // If the entry is obsolete, use the new one instead.
966 string const & obs = tclass[layout]->obsoleted_by();
972 cur.errorMessage(string(N_("Layout ")) + cmd.argument +
977 bool change_layout = (current_layout != layout);
979 if (!change_layout && cur.selection() &&
980 cur.selBegin().par() != cur.selEnd().par())
982 par_type spit = cur.selBegin().par();
983 par_type epit = cur.selEnd().par() + 1;
984 while (spit != epit) {
985 if (pars_[spit].layout()->name() != current_layout) {
986 change_layout = true;
994 current_layout = layout;
995 setLayout(cur, layout);
996 bv->owner()->setLayout(layout);
1002 case LFUN_PASTESELECTION: {
1003 cur.clearSelection();
1004 string const clip = bv->getClipboard();
1005 if (!clip.empty()) {
1006 if (cmd.argument == "paragraph")
1007 insertStringAsParagraphs(cur, clip);
1009 insertStringAsLines(cur, clip);
1014 case LFUN_GOTOERROR:
1015 gotoInset(cur, InsetBase::ERROR_CODE, false);
1019 gotoInset(cur, InsetBase::NOTE_CODE, false);
1022 case LFUN_REFERENCE_GOTO: {
1023 vector<InsetOld_code> tmp;
1024 tmp.push_back(InsetBase::LABEL_CODE);
1025 tmp.push_back(InsetBase::REF_CODE);
1026 gotoInset(cur, tmp, true);
1031 lyx::cap::replaceSelection(cur);
1032 Paragraph & par = cur.paragraph();
1033 lyx::pos_type pos = cur.pos();
1037 else if (cur.prevInset() && cur.prevInset()->isSpace())
1040 c = par.getChar(pos - 1);
1042 LyXLayout_ptr const & style = par.layout();
1044 BufferParams const & bufparams = bv->buffer()->params();
1045 if (!style->pass_thru
1046 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1047 string arg = cmd.argument;
1048 if (arg == "single")
1049 cur.insert(new InsetQuotes(c,
1050 bufparams.quotes_language,
1051 InsetQuotes::SingleQ));
1052 else if (arg == "double")
1053 cur.insert(new InsetQuotes(c,
1054 bufparams.quotes_language,
1055 InsetQuotes::DoubleQ));
1057 cur.insert(new InsetQuotes(c, bufparams));
1061 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1065 case LFUN_DATE_INSERT: {
1066 lyx::cap::replaceSelection(cur);
1067 time_t now_time_t = time(NULL);
1068 struct tm * now_tm = localtime(&now_time_t);
1069 setlocale(LC_TIME, "");
1071 if (!cmd.argument.empty())
1074 arg = lyxrc.date_insert_format;
1076 int const datetmp_len =
1077 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1079 for (int i = 0; i < datetmp_len; i++)
1080 insertChar(cur, datetmp[i]);
1083 moveCursor(cur, false);
1087 case LFUN_MOUSE_TRIPLE:
1088 if (cmd.button() == mouse_button::button1) {
1089 selection_possible = true;
1094 bv->haveSelection(cur.selection());
1098 case LFUN_MOUSE_DOUBLE:
1099 if (cmd.button() == mouse_button::button1) {
1100 selection_possible = true;
1101 selectWord(cur, lyx::WHOLE_WORD_STRICT);
1102 bv->haveSelection(cur.selection());
1106 case LFUN_MOUSE_MOTION: {
1107 // Only use motion with button 1
1108 //if (cmd.button() != mouse_button::button1)
1110 // We want to use only motion events for which
1111 // the button press event was on the drawing area too.
1112 if (!selection_possible) {
1113 lyxerr[Debug::ACTION] << "BufferView::Pimpl::"
1114 "dispatch: no selection possible\n";
1115 lyxerr << "BufferView::Pimpl::dispatch: no selection possible\n";
1119 // ignore motions deeper nested than the real anchor
1120 LCursor & bvcur = cur.bv().cursor();
1121 if (bvcur.selection() && bvcur.anchor_.size() < cur.size())
1124 CursorSlice old = cur.top();
1125 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1127 // This is to allow jumping over large insets
1128 // FIXME: shouldn't be top-text-specific
1129 if (isMainText() && cur.top() == old) {
1130 if (cmd.y - bv->top_y() >= bv->workHeight())
1132 else if (cmd.y - bv->top_y() < 0)
1136 // don't set anchor_
1137 bv->cursor().setCursor(cur, true);
1138 lyxerr << "MOTION: " << bv->cursor() << endl;
1142 // Single-click on work area
1143 case LFUN_MOUSE_PRESS: {
1144 // Right click on a footnote flag opens float menu
1145 if (cmd.button() == mouse_button::button3) {
1146 cur.clearSelection();
1147 selection_possible = false;
1151 // Middle button press pastes if we have a selection
1152 // We do this here as if the selection was inside an inset
1153 // it could get cleared on the unlocking of the inset so
1154 // we have to check this first
1155 bool paste_internally = false;
1156 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1157 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1158 paste_internally = true;
1161 selection_possible = true;
1163 // Clear the selection
1164 cur.clearSelection();
1166 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1169 cur.x_target() = cursorX(cur.top());
1171 // Has the cursor just left the inset?
1172 if (bv->cursor().inMathed() && !cur.inMathed())
1173 bv->cursor().inset().notifyCursorLeaves(bv->cursor());
1178 // Don't allow selection after a big jump.
1179 if (bv->fitCursor())
1180 selection_possible = false;
1182 // Insert primary selection with middle mouse
1183 // if there is a local selection in the current buffer,
1185 if (cmd.button() == mouse_button::button2) {
1186 if (paste_internally)
1187 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1189 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1190 selection_possible = false;
1196 case LFUN_MOUSE_RELEASE: {
1197 selection_possible = false;
1199 if (cmd.button() == mouse_button::button2)
1203 if (cmd.button() == mouse_button::button1)
1204 bv->haveSelection(cur.selection());
1207 bv->owner()->updateMenubar();
1208 bv->owner()->updateToolbars();
1212 case LFUN_SELFINSERT: {
1213 if (cmd.argument.empty())
1216 // Automatically delete the currently selected
1217 // text and replace it with what is being
1218 // typed in now. Depends on lyxrc settings
1219 // "auto_region_delete", which defaults to
1222 if (lyxrc.auto_region_delete) {
1223 if (cur.selection())
1224 cutSelection(cur, false, false);
1225 bv->haveSelection(false);
1228 cur.clearSelection();
1229 LyXFont const old_font = real_current_font;
1231 string::const_iterator cit = cmd.argument.begin();
1232 string::const_iterator end = cmd.argument.end();
1233 for (; cit != end; ++cit)
1234 bv->owner()->getIntl().getTransManager().
1235 TranslateAndInsert(*cit, this);
1238 moveCursor(cur, false);
1240 // real_current_font.number can change so we need to
1241 // update the minibuffer
1242 if (old_font != real_current_font)
1243 bv->updateScrollbar();
1248 InsetCommandParams p("url");
1249 string const data = InsetCommandMailer::params2string("url", p);
1250 bv->owner()->getDialogs().show("url", data, 0);
1254 case LFUN_HTMLURL: {
1255 InsetCommandParams p("htmlurl");
1256 string const data = InsetCommandMailer::params2string("url", p);
1257 bv->owner()->getDialogs().show("url", data, 0);
1261 case LFUN_INSERT_LABEL: {
1262 // Try to generate a valid label
1263 string const contents = cmd.argument.empty() ?
1264 cur.getPossibleLabel() : cmd.argument;
1266 InsetCommandParams p("label", contents);
1267 string const data = InsetCommandMailer::params2string("label", p);
1269 if (cmd.argument.empty()) {
1270 bv->owner()->getDialogs().show("label", data, 0);
1272 FuncRequest fr(LFUN_INSET_INSERT, data);
1280 case LFUN_INSET_LIST:
1281 case LFUN_INSET_THEOREM:
1282 case LFUN_INSET_CAPTION:
1284 case LFUN_INSERT_NOTE:
1285 case LFUN_INSERT_CHARSTYLE:
1286 case LFUN_INSERT_BOX:
1287 case LFUN_INSERT_BRANCH:
1288 case LFUN_INSERT_BIBITEM:
1289 case LFUN_INSET_ERT:
1290 case LFUN_INSET_FLOAT:
1291 case LFUN_INSET_FOOTNOTE:
1292 case LFUN_INSET_MARGINAL:
1293 case LFUN_INSET_OPTARG:
1294 case LFUN_INSET_WIDE_FLOAT:
1295 case LFUN_INSET_WRAP:
1296 case LFUN_TABULAR_INSERT:
1297 case LFUN_ENVIRONMENT_INSERT:
1298 // Open the inset, and move the current selection
1300 doInsertInset(cur, this, cmd, true, true);
1304 case LFUN_INDEX_INSERT:
1305 // Just open the inset
1306 doInsertInset(cur, this, cmd, true, false);
1310 case LFUN_INDEX_PRINT:
1311 case LFUN_TOC_INSERT:
1313 case LFUN_INSERT_LINE:
1314 case LFUN_INSERT_PAGEBREAK:
1316 doInsertInset(cur, this, cmd, false, false);
1320 case LFUN_DEPTH_MIN:
1321 changeDepth(cur, DEC_DEPTH);
1324 case LFUN_DEPTH_PLUS:
1325 changeDepth(cur, INC_DEPTH);
1328 case LFUN_MATH_DISPLAY:
1329 mathDispatch(cur, cmd, true);
1332 case LFUN_MATH_IMPORT_SELECTION:
1333 case LFUN_MATH_MODE:
1334 mathDispatch(cur, cmd, false);
1337 case LFUN_MATH_MACRO:
1338 if (cmd.argument.empty())
1339 cur.errorMessage(N_("Missing argument"));
1341 string s = cmd.argument;
1342 string const s1 = token(s, ' ', 1);
1343 int const nargs = s1.empty() ? 0 : atoi(s1);
1344 string const s2 = token(s, ' ', 2);
1345 string const type = s2.empty() ? "newcommand" : s2;
1346 cur.insert(new MathMacroTemplate(token(s, ' ', 0), nargs, s2));
1347 //cur.nextInset()->edit(cur, true);
1351 case LFUN_INSERT_MATH:
1352 case LFUN_INSERT_MATRIX:
1353 case LFUN_MATH_DELIM: {
1354 cur.insert(new MathHullInset);
1355 cur.dispatch(FuncRequest(LFUN_RIGHT));
1356 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
1362 LyXFont font(LyXFont::ALL_IGNORE);
1363 font.setEmph(LyXFont::TOGGLE);
1364 toggleAndShow(cur, this, font);
1369 LyXFont font(LyXFont::ALL_IGNORE);
1370 font.setSeries(LyXFont::BOLD_SERIES);
1371 toggleAndShow(cur, this, font);
1376 LyXFont font(LyXFont::ALL_IGNORE);
1377 font.setNoun(LyXFont::TOGGLE);
1378 toggleAndShow(cur, this, font);
1383 LyXFont font(LyXFont::ALL_IGNORE);
1384 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1385 toggleAndShow(cur, this, font);
1390 LyXFont font(LyXFont::ALL_IGNORE);
1391 font.setFamily(LyXFont::SANS_FAMILY);
1392 toggleAndShow(cur, this, font);
1397 LyXFont font(LyXFont::ALL_IGNORE);
1398 font.setFamily(LyXFont::ROMAN_FAMILY);
1399 toggleAndShow(cur, this, font);
1403 case LFUN_DEFAULT: {
1404 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1405 toggleAndShow(cur, this, font);
1409 case LFUN_UNDERLINE: {
1410 LyXFont font(LyXFont::ALL_IGNORE);
1411 font.setUnderbar(LyXFont::TOGGLE);
1412 toggleAndShow(cur, this, font);
1416 case LFUN_FONT_SIZE: {
1417 LyXFont font(LyXFont::ALL_IGNORE);
1418 font.setLyXSize(cmd.argument);
1419 toggleAndShow(cur, this, font);
1423 case LFUN_LANGUAGE: {
1424 Language const * lang = languages.getLanguage(cmd.argument);
1427 LyXFont font(LyXFont::ALL_IGNORE);
1428 font.setLanguage(lang);
1429 toggleAndShow(cur, this, font);
1434 case LFUN_FREEFONT_APPLY:
1435 toggleAndShow(cur, this, freefont, toggleall);
1436 cur.message(_("Character set"));
1439 // Set the freefont using the contents of \param data dispatched from
1440 // the frontends and apply it at the current cursor location.
1441 case LFUN_FREEFONT_UPDATE: {
1444 if (bv_funcs::string2font(cmd.argument, font, toggle)) {
1447 toggleAndShow(cur, this, freefont, toggleall);
1448 cur.message(_("Character set"));
1453 case LFUN_FINISHED_LEFT:
1454 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1457 case LFUN_FINISHED_RIGHT:
1458 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1462 case LFUN_FINISHED_UP:
1463 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1467 case LFUN_FINISHED_DOWN:
1468 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1472 case LFUN_LAYOUT_PARAGRAPH: {
1474 params2string(cur.paragraph(), data);
1475 data = "show\n" + data;
1476 bv->owner()->getDialogs().show("paragraph", data);
1480 case LFUN_PARAGRAPH_UPDATE: {
1481 if (!bv->owner()->getDialogs().visible("paragraph"))
1484 params2string(cur.paragraph(), data);
1486 // Will the paragraph accept changes from the dialog?
1487 InsetBase & inset = cur.inset();
1488 bool const accept = !inset.forceDefaultParagraphs(&inset);
1490 data = "update " + tostr(accept) + '\n' + data;
1491 bv->owner()->getDialogs().update("paragraph", data);
1496 case LFUN_CIRCUMFLEX:
1506 case LFUN_SPECIAL_CARON:
1509 case LFUN_HUNG_UMLAUT:
1512 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1513 if (!cmd.argument.empty())
1514 bv->owner()->getIntl().getTransManager()
1515 .TranslateAndInsert(cmd.argument[0], this);
1518 case LFUN_FLOAT_LIST: {
1519 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1520 if (tclass.floats().typeExist(cmd.argument)) {
1521 // not quite sure if we want this...
1523 cur.clearSelection();
1524 breakParagraph(cur);
1526 if (cur.lastpos() != 0) {
1528 breakParagraph(cur);
1531 setLayout(cur, tclass.defaultLayoutName());
1532 setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1533 insertInset(cur, new InsetFloatList(cmd.argument));
1535 lyxerr << "Non-existent float type: "
1536 << cmd.argument << endl;
1541 case LFUN_ACCEPT_CHANGE: {
1546 case LFUN_REJECT_CHANGE: {
1551 case LFUN_THESAURUS_ENTRY: {
1552 string arg = cmd.argument;
1554 arg = cur.selectionAsString(false);
1556 if (arg.size() > 100 || arg.empty()) {
1557 // Get word or selection
1558 selectWordWhenUnderCursor(cur, lyx::WHOLE_WORD);
1559 arg = cur.selectionAsString(false);
1562 bv->owner()->getDialogs().show("thesaurus", arg);
1566 case LFUN_PARAGRAPH_APPLY: {
1567 // Given data, an encoding of the ParagraphParameters
1568 // generated in the Paragraph dialog, this function sets
1569 // the current paragraph appropriately.
1570 istringstream is(cmd.argument);
1573 ParagraphParameters params;
1578 params.labelWidthString(),
1580 cur.message(_("Paragraph layout set"));
1584 case LFUN_INSET_DIALOG_SHOW: {
1585 InsetBase * inset = cur.nextInset();
1587 FuncRequest fr(LFUN_INSET_DIALOG_SHOW);
1588 inset->dispatch(cur, fr);
1594 if (cur.selection()) {
1595 cur.selection() = false;
1598 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1607 // avoid to update when navigating
1609 && &sl.inset() == &cur.inset()
1610 && sl.idx() == cur.idx()
1612 && cur.selection() == false)
1617 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1618 FuncStatus & flag) const
1620 BOOST_ASSERT(cur.text() == this);
1621 LyXFont const & font = real_current_font;
1624 switch (cmd.action) {
1626 case LFUN_DEPTH_MIN:
1627 enable = changeDepthAllowed(cur, DEC_DEPTH);
1630 case LFUN_DEPTH_PLUS:
1631 enable = changeDepthAllowed(cur, INC_DEPTH);
1634 case LFUN_INSET_OPTARG:
1635 enable = cur.paragraph().layout()->optionalargs;
1639 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1643 // the functions which insert insets
1644 InsetOld::Code code = InsetOld::NO_CODE;
1645 switch (cmd.action) {
1646 case LFUN_DIALOG_SHOW_NEW_INSET:
1647 if (cmd.argument == "bibitem")
1648 code = InsetOld::BIBITEM_CODE;
1649 else if (cmd.argument == "bibtex")
1650 code = InsetOld::BIBTEX_CODE;
1651 else if (cmd.argument == "box")
1652 code = InsetOld::BOX_CODE;
1653 else if (cmd.argument == "branch")
1654 code = InsetOld::BRANCH_CODE;
1655 else if (cmd.argument == "citation")
1656 code = InsetOld::CITE_CODE;
1657 else if (cmd.argument == "ert")
1658 code = InsetOld::ERT_CODE;
1659 else if (cmd.argument == "external")
1660 code = InsetOld::EXTERNAL_CODE;
1661 else if (cmd.argument == "float")
1662 code = InsetOld::FLOAT_CODE;
1663 else if (cmd.argument == "graphics")
1664 code = InsetOld::GRAPHICS_CODE;
1665 else if (cmd.argument == "include")
1666 code = InsetOld::INCLUDE_CODE;
1667 else if (cmd.argument == "index")
1668 code = InsetOld::INDEX_CODE;
1669 else if (cmd.argument == "label")
1670 code = InsetOld::LABEL_CODE;
1671 else if (cmd.argument == "note")
1672 code = InsetOld::NOTE_CODE;
1673 else if (cmd.argument == "ref")
1674 code = InsetOld::REF_CODE;
1675 else if (cmd.argument == "toc")
1676 code = InsetOld::TOC_CODE;
1677 else if (cmd.argument == "url")
1678 code = InsetOld::URL_CODE;
1679 else if (cmd.argument == "vspace")
1680 code = InsetOld::VSPACE_CODE;
1681 else if (cmd.argument == "wrap")
1682 code = InsetOld::WRAP_CODE;
1685 case LFUN_INSET_ERT:
1686 code = InsetOld::ERT_CODE;
1688 case LFUN_INSET_FOOTNOTE:
1689 code = InsetOld::FOOT_CODE;
1691 case LFUN_TABULAR_INSERT:
1692 code = InsetOld::TABULAR_CODE;
1694 case LFUN_INSET_MARGINAL:
1695 code = InsetOld::MARGIN_CODE;
1697 case LFUN_INSET_FLOAT:
1698 case LFUN_INSET_WIDE_FLOAT:
1699 code = InsetOld::FLOAT_CODE;
1701 case LFUN_INSET_WRAP:
1702 code = InsetOld::WRAP_CODE;
1704 case LFUN_FLOAT_LIST:
1705 code = InsetOld::FLOAT_LIST_CODE;
1708 case LFUN_INSET_LIST:
1709 code = InsetOld::LIST_CODE;
1711 case LFUN_INSET_THEOREM:
1712 code = InsetOld::THEOREM_CODE;
1715 case LFUN_INSET_CAPTION:
1716 code = InsetOld::CAPTION_CODE;
1718 case LFUN_INSERT_NOTE:
1719 code = InsetOld::NOTE_CODE;
1721 case LFUN_INSERT_CHARSTYLE:
1722 code = InsetOld::CHARSTYLE_CODE;
1723 if (buf->params().getLyXTextClass().charstyles().empty())
1726 case LFUN_INSERT_BOX:
1727 code = InsetOld::BOX_CODE;
1729 case LFUN_INSERT_BRANCH:
1730 code = InsetOld::BRANCH_CODE;
1731 if (buf->params().branchlist().empty())
1734 case LFUN_INSERT_LABEL:
1735 code = InsetOld::LABEL_CODE;
1737 case LFUN_INSET_OPTARG:
1738 code = InsetOld::OPTARG_CODE;
1740 case LFUN_ENVIRONMENT_INSERT:
1741 code = InsetOld::BOX_CODE;
1743 case LFUN_INDEX_INSERT:
1744 code = InsetOld::INDEX_CODE;
1746 case LFUN_INDEX_PRINT:
1747 code = InsetOld::INDEX_PRINT_CODE;
1749 case LFUN_TOC_INSERT:
1750 code = InsetOld::TOC_CODE;
1754 code = InsetOld::URL_CODE;
1757 // always allow this, since we will inset a raw quote
1758 // if an inset is not allowed.
1760 case LFUN_HYPHENATION:
1761 case LFUN_LIGATURE_BREAK:
1763 case LFUN_MENU_SEPARATOR:
1765 case LFUN_END_OF_SENTENCE:
1766 code = InsetOld::SPECIALCHAR_CODE;
1768 case LFUN_SPACE_INSERT:
1769 // slight hack: we know this is allowed in math mode
1771 code = InsetOld::SPACE_CODE;
1773 case LFUN_INSET_DIALOG_SHOW: {
1774 InsetBase * inset = cur.nextInset();
1777 code = inset->lyxCode();
1778 if (!(code == InsetOld::INCLUDE_CODE
1779 || code == InsetOld::BIBTEX_CODE
1780 || code == InsetOld::FLOAT_LIST_CODE
1781 || code == InsetOld::TOC_CODE))
1790 if (code != InsetOld::NO_CODE
1791 && (cur.empty() || !cur.inset().insetAllowed(code)))
1796 case LFUN_DIALOG_SHOW_NEW_INSET:
1797 case LFUN_INSET_ERT:
1798 case LFUN_INSERT_BOX:
1799 case LFUN_INSERT_BRANCH:
1800 case LFUN_ENVIRONMENT_INSERT:
1801 case LFUN_INDEX_INSERT:
1802 case LFUN_INDEX_PRINT:
1803 case LFUN_TOC_INSERT:
1807 case LFUN_HYPHENATION:
1808 case LFUN_LIGATURE_BREAK:
1810 case LFUN_MENU_SEPARATOR:
1812 case LFUN_END_OF_SENTENCE:
1813 case LFUN_SPACE_INSERT:
1814 case LFUN_INSET_DIALOG_SHOW:
1818 flag.setOnOff(font.emph() == LyXFont::ON);
1822 flag.setOnOff(font.noun() == LyXFont::ON);
1826 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1830 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1834 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1838 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1841 case LFUN_DELETE_WORD_FORWARD:
1842 case LFUN_DELETE_WORD_BACKWARD:
1843 case LFUN_DELETE_LINE_FORWARD:
1844 case LFUN_WORDRIGHT:
1854 case LFUN_UP_PARAGRAPHSEL:
1855 case LFUN_DOWN_PARAGRAPHSEL:
1860 case LFUN_WORDRIGHTSEL:
1861 case LFUN_WORDLEFTSEL:
1863 case LFUN_UP_PARAGRAPH:
1864 case LFUN_DOWN_PARAGRAPH:
1869 case LFUN_BREAKLINE:
1871 case LFUN_DELETE_SKIP:
1872 case LFUN_BACKSPACE:
1873 case LFUN_BACKSPACE_SKIP:
1874 case LFUN_BREAKPARAGRAPH:
1875 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1876 case LFUN_BREAKPARAGRAPH_SKIP:
1877 case LFUN_PARAGRAPH_SPACING:
1878 case LFUN_INSET_APPLY:
1879 case LFUN_INSET_INSERT:
1880 case LFUN_NEXT_INSET_TOGGLE:
1881 case LFUN_UPCASE_WORD:
1882 case LFUN_LOWCASE_WORD:
1883 case LFUN_CAPITALIZE_WORD:
1884 case LFUN_TRANSPOSE_CHARS:
1891 case LFUN_GETLAYOUT:
1893 case LFUN_PASTESELECTION:
1894 case LFUN_GOTOERROR:
1896 case LFUN_REFERENCE_GOTO:
1897 case LFUN_DATE_INSERT:
1898 case LFUN_SELFINSERT:
1899 case LFUN_INSERT_LABEL:
1900 case LFUN_INSERT_NOTE:
1901 case LFUN_INSERT_CHARSTYLE:
1902 case LFUN_INSERT_BIBITEM:
1903 case LFUN_INSET_FLOAT:
1904 case LFUN_INSET_FOOTNOTE:
1905 case LFUN_INSET_MARGINAL:
1906 case LFUN_INSET_WIDE_FLOAT:
1907 case LFUN_INSET_WRAP:
1908 case LFUN_TABULAR_INSERT:
1909 case LFUN_INSERT_LINE:
1910 case LFUN_INSERT_PAGEBREAK:
1911 case LFUN_MATH_DISPLAY:
1912 case LFUN_MATH_IMPORT_SELECTION:
1913 case LFUN_MATH_MODE:
1914 case LFUN_MATH_MACRO:
1915 case LFUN_INSERT_MATH:
1916 case LFUN_INSERT_MATRIX:
1917 case LFUN_MATH_DELIM:
1919 case LFUN_UNDERLINE:
1920 case LFUN_FONT_SIZE:
1922 case LFUN_FREEFONT_APPLY:
1923 case LFUN_FREEFONT_UPDATE:
1924 case LFUN_LAYOUT_PARAGRAPH:
1925 case LFUN_PARAGRAPH_UPDATE:
1927 case LFUN_CIRCUMFLEX:
1937 case LFUN_SPECIAL_CARON:
1940 case LFUN_HUNG_UMLAUT:
1943 case LFUN_FLOAT_LIST:
1944 case LFUN_ACCEPT_CHANGE:
1945 case LFUN_REJECT_CHANGE:
1946 case LFUN_THESAURUS_ENTRY:
1947 case LFUN_PARAGRAPH_APPLY:
1949 case LFUN_KEYMAP_TOGGLE:
1950 // these are handled in our dispatch()
1955 case LFUN_BEGINNINGBUF:
1963 flag.enabled(enable);