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 "CutAndPaste.h"
28 #include "dispatchresult.h"
30 #include "funcrequest.h"
38 #include "paragraph.h"
39 #include "paragraph_funcs.h"
40 #include "ParagraphParameters.h"
44 #include "frontends/Dialogs.h"
45 #include "frontends/LyXView.h"
47 #include "insets/insetcommand.h"
48 #include "insets/insetfloatlist.h"
49 #include "insets/insetnewline.h"
50 #include "insets/insetquotes.h"
51 #include "insets/insetspecialchar.h"
52 #include "insets/insettext.h"
54 #include "support/lstrings.h"
55 #include "support/lyxlib.h"
56 #include "support/tostr.h"
57 #include "support/std_sstream.h"
59 #include "mathed/math_hullinset.h"
60 #include "mathed/formulamacro.h"
66 using lyx::cap::copySelection;
67 using lyx::cap::cutSelection;
68 using lyx::cap::pasteSelection;
69 using lyx::cap::replaceSelection;
71 using lyx::support::isStrUnsignedInt;
72 using lyx::support::strToUnsignedInt;
73 using lyx::support::atoi;
74 using lyx::support::token;
79 using std::istringstream;
83 extern string current_layout;
85 // the selection possible is needed, that only motion events are
86 // used, where the button press event was on the drawing area too
87 bool selection_possible = false;
93 LyXFont freefont(LyXFont::ALL_IGNORE);
94 bool toggleall = false;
97 void toggleAndShow(LCursor & cur, LyXText * text,
98 LyXFont const & font, bool toggleall = true)
100 text->toggleFree(cur, font, toggleall);
102 if (font.language() != ignore_language ||
103 font.number() != LyXFont::IGNORE) {
104 Paragraph & par = cur.paragraph();
105 text->bidi.computeTables(par, *cur.bv().buffer(), cur.textRow());
106 if (cur.boundary() !=
107 text->bidi.isBoundary(*cur.bv().buffer(), par,
109 text->real_current_font))
110 text->setCursor(cur, cur.par(), cur.pos(),
111 false, !cur.boundary());
116 void moveCursor(LCursor & cur, bool selecting)
118 if (selecting || cur.mark())
120 if (!cur.selection())
121 cur.bv().haveSelection(false);
122 cur.bv().switchKeyMap();
126 void finishChange(LCursor & cur, bool selecting)
129 moveCursor(cur, selecting);
133 void mathDispatch(LCursor & cur, FuncRequest const & cmd, bool display)
136 string sel = cur.selectionAsString(false);
137 lyxerr << "selection is: '" << sel << "'" << endl;
140 cur.insert(new MathHullInset);
141 cur.dispatch(FuncRequest(LFUN_RIGHT));
142 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
143 // don't do that also for LFUN_MATH_MODE unless you want end up with
144 // always changing to mathrm when opening an inlined inset
145 // -- I really hate "LyXfunc overloading"...
147 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
148 cur.dispatch(FuncRequest(LFUN_INSERT_MATH, cmd.argument));
150 // create a macro if we see "\\newcommand" somewhere, and an ordinary
152 cutSelection(cur, true, true);
153 if (sel.find("\\newcommand") == string::npos
154 && sel.find("\\def") == string::npos)
156 cur.insert(new MathHullInset);
157 cur.dispatch(FuncRequest(LFUN_RIGHT));
158 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
159 cur.dispatch(FuncRequest(LFUN_INSERT_MATH, sel));
161 cur.insert(new InsetFormulaMacro(sel));
162 cur.dispatch(FuncRequest(LFUN_RIGHT));
165 cur.message(N_("Math editor mode"));
174 string const freefont2string()
177 if (font2string(freefont, toggleall, data))
185 //takes absolute x,y coordinates
186 InsetBase * LyXText::checkInsetHit(int x, int y)
191 getParsInRange(paragraphs(),
193 bv()->top_y() - yo_ + bv()->workHeight(),
196 //lyxerr << "checkInsetHit: x: " << x << " y: " << y << endl;
197 for ( ; pit != end; ++pit) {
198 InsetList::iterator iit = pars_[pit].insetlist.begin();
199 InsetList::iterator iend = pars_[pit].insetlist.end();
200 for ( ; iit != iend; ++iit) {
201 InsetBase * inset = iit->inset;
203 lyxerr << "examining inset " << inset
204 << " xo: " << inset->xo() << "..." << inset->xo() + inset->width()
205 << " yo: " << inset->yo() - inset->ascent() << "..."
206 << inset->yo() + inset->descent() << endl;
208 if (inset->covers(x, y)) {
209 lyxerr << "Hit inset: " << inset << endl;
214 //lyxerr << "No inset hit. " << endl;
219 bool LyXText::gotoNextInset(LCursor & cur,
220 vector<InsetOld_code> const & codes, string const & contents)
222 BOOST_ASSERT(this == cur.text());
223 par_type end = paragraphs().size();
224 par_type pit = cur.par();
225 pos_type pos = cur.pos();
229 if (pos + 1 < pars_[pit].size()) {
236 } while (pit != end &&
237 !(pars_[pit].isInset(pos) &&
238 (inset = pars_[pit].getInset(pos)) != 0 &&
239 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
241 static_cast<InsetCommand *>(pars_[pit].getInset(pos))->getContents()
247 setCursor(cur, pit, pos, false);
252 void LyXText::gotoInset(LCursor & cur,
253 vector<InsetOld_code> const & codes, bool same_content)
255 cur.clearSelection();
259 && cur.pos() < cur.lastpos()
260 && cur.paragraph().isInset(cur.pos())) {
261 InsetBase const * inset = cur.paragraph().getInset(cur.pos());
262 if (find(codes.begin(), codes.end(), inset->lyxCode())
264 contents = static_cast<InsetCommand const *>(inset)->getContents();
267 if (!gotoNextInset(cur, codes, contents)) {
268 if (cur.pos() || cur.par() != 0) {
269 CursorSlice tmp = cur.top();
272 if (!gotoNextInset(cur, codes, contents)) {
274 cur.message(_("No more insets"));
277 cur.message(_("No more insets"));
284 void LyXText::gotoInset(LCursor & cur, InsetOld_code code, bool same_content)
286 gotoInset(cur, vector<InsetOld_code>(1, code), same_content);
290 void LyXText::cursorPrevious(LCursor & cur)
292 pos_type cpos = cur.pos();
293 lyx::par_type cpar = cur.par();
295 int x = cur.x_target();
296 int y = bv()->top_y();
297 setCursorFromCoordinates(cur, x, y);
299 if (cpar == cur.par() && cpos == cur.pos()) {
300 // we have a row which is taller than the workarea. The
301 // simplest solution is to move to the previous row instead.
305 bv()->updateScrollbar();
310 void LyXText::cursorNext(LCursor & cur)
312 pos_type cpos = cur.pos();
313 lyx::par_type cpar = cur.par();
315 int x = cur.x_target();
316 int y = bv()->top_y() + bv()->workHeight();
317 setCursorFromCoordinates(cur, x, y);
319 if (cpar == cur.par() && cpos == cur.pos()) {
320 // we have a row which is taller than the workarea. The
321 // simplest solution is to move to the next row instead.
325 bv()->updateScrollbar();
332 void specialChar(LCursor & cur, InsetSpecialChar::Kind kind)
334 lyx::cap::replaceSelection(cur);
335 cur.insert(new InsetSpecialChar(kind));
339 void doInsertInset(LCursor & cur, LyXText * text,
340 FuncRequest const & cmd, bool edit, bool pastesel)
342 InsetBase * inset = createInset(&cur.bv(), cmd);
348 if (cur.selection()) {
349 cur.bv().owner()->dispatch(FuncRequest(LFUN_CUT));
352 text->insertInset(cur, inset);
354 inset->edit(cur, true);
355 if (gotsel && pastesel)
356 cur.bv().owner()->dispatch(FuncRequest(LFUN_PASTE));
362 void LyXText::number(LCursor & cur)
364 LyXFont font(LyXFont::ALL_IGNORE);
365 font.setNumber(LyXFont::TOGGLE);
366 toggleAndShow(cur, this, font);
370 bool LyXText::isRTL(Paragraph const & par) const
372 return par.isRightToLeftPar(bv()->buffer()->params());
376 void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
378 lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
379 //lyxerr << "*** LyXText::dispatch: cmd: " << cmd << endl;
381 BOOST_ASSERT(cur.text() == this);
382 BufferView * bv = &cur.bv();
383 CursorSlice sl = cur.top();
385 switch (cmd.action) {
387 case LFUN_APPENDIX: {
388 Paragraph & par = cur.paragraph();
389 bool start = !par.params().startOfAppendix();
391 // ensure that we have only one start_of_appendix in this document
392 for (par_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
393 if (pars_[tmp].params().startOfAppendix()) {
395 pars_[tmp].params().startOfAppendix(false);
402 par.params().startOfAppendix(start);
404 // we can set the refreshing parameters now
410 case LFUN_DELETE_WORD_FORWARD:
411 cur.clearSelection();
412 deleteWordForward(cur);
413 finishChange(cur, false);
416 case LFUN_DELETE_WORD_BACKWARD:
417 cur.clearSelection();
418 deleteWordBackward(cur);
419 finishChange(cur, false);
422 case LFUN_DELETE_LINE_FORWARD:
423 cur.clearSelection();
424 deleteLineForward(cur);
425 finishChange(cur, false);
430 cur.clearSelection();
431 if (isRTL(cur.paragraph()))
432 cursorLeftOneWord(cur);
434 cursorRightOneWord(cur);
435 finishChange(cur, false);
440 cur.clearSelection();
441 if (isRTL(cur.paragraph()))
442 cursorRightOneWord(cur);
444 cursorLeftOneWord(cur);
445 finishChange(cur, false);
448 case LFUN_BEGINNINGBUF:
450 cur.clearSelection();
452 finishChange(cur, false);
457 cur.clearSelection();
459 finishChange(cur, false);
464 //lyxerr << "handle LFUN_RIGHT[SEL]:\n" << cur << endl;
465 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
466 if (isRTL(cur.paragraph()))
470 if (sl == cur.top()) {
472 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
478 //lyxerr << "handle LFUN_LEFT[SEL]:\n" << cur << endl;
479 cur.selHandle(cmd.action == LFUN_LEFTSEL);
480 if (isRTL(cur.paragraph()))
484 if (sl == cur.top()) {
486 cmd = FuncRequest(LFUN_FINISHED_LEFT);
492 //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
493 cur.selHandle(cmd.action == LFUN_UPSEL);
495 if (sl == cur.top()) {
497 cmd = FuncRequest(LFUN_FINISHED_UP);
503 //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
504 cur.selHandle(cmd.action == LFUN_DOWNSEL);
506 if (sl == cur.top()) {
508 cmd = FuncRequest(LFUN_FINISHED_DOWN);
512 case LFUN_UP_PARAGRAPHSEL:
513 if (!cur.selection())
515 cursorUpParagraph(cur);
516 finishChange(cur, true);
519 case LFUN_DOWN_PARAGRAPHSEL:
520 if (!cur.selection())
522 cursorDownParagraph(cur);
523 finishChange(cur, true);
527 if (!cur.selection())
530 finishChange(cur, true);
534 if (!cur.selection())
537 finishChange(cur, true);
541 if (!cur.selection())
544 finishChange(cur, true);
548 if (!cur.selection())
551 finishChange(cur, true);
554 case LFUN_WORDRIGHTSEL:
555 if (!cur.selection())
557 if (isRTL(cur.paragraph()))
558 cursorLeftOneWord(cur);
560 cursorRightOneWord(cur);
561 finishChange(cur, true);
564 case LFUN_WORDLEFTSEL:
565 if (!cur.selection())
567 if (isRTL(cur.paragraph()))
568 cursorRightOneWord(cur);
570 cursorLeftOneWord(cur);
571 finishChange(cur, true);
575 selectWord(cur, lyx::WHOLE_WORD);
576 finishChange(cur, true);
580 case LFUN_UP_PARAGRAPH:
582 cur.clearSelection();
583 cursorUpParagraph(cur);
584 finishChange(cur, false);
587 case LFUN_DOWN_PARAGRAPH:
589 cur.clearSelection();
590 cursorDownParagraph(cur);
591 finishChange(cur, false);
596 cur.clearSelection();
597 finishChange(cur, false);
598 if (cur.par() == 0 && cur.textRow().pos() == 0) {
600 cmd = FuncRequest(LFUN_FINISHED_UP);
608 cur.clearSelection();
609 finishChange(cur, false);
610 if (cur.par() == cur.lastpar()
611 && cur.textRow().endpos() == cur.lastpos()) {
613 cmd = FuncRequest(LFUN_FINISHED_DOWN);
621 cur.clearSelection();
623 finishChange(cur, false);
628 cur.clearSelection();
630 finishChange(cur, false);
633 case LFUN_BREAKLINE: {
634 // Not allowed by LaTeX (labels or empty par)
635 if (cur.pos() > cur.paragraph().beginOfBody()) {
636 lyx::cap::replaceSelection(cur);
637 cur.insert(new InsetNewline);
638 moveCursor(cur, false);
644 if (!cur.selection()) {
647 // It is possible to make it a lot faster still
648 // just comment out the line below...
650 cutSelection(cur, true, false);
652 moveCursor(cur, false);
655 case LFUN_DELETE_SKIP:
656 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
657 if (!cur.selection()) {
658 if (cur.pos() == cur.lastpos()) {
665 cutSelection(cur, true, false);
671 if (!cur.selection()) {
672 if (bv->owner()->getIntl().getTransManager().backspace()) {
675 // It is possible to make it a lot faster still
676 // just comment out the line below...
679 cutSelection(cur, true, false);
684 case LFUN_BACKSPACE_SKIP:
685 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
686 if (!cur.selection()) {
688 //CursorSlice cur = cursor();
692 cutSelection(cur, true, false);
696 case LFUN_BREAKPARAGRAPH:
697 lyx::cap::replaceSelection(cur);
698 breakParagraph(cur, 0);
703 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
704 lyx::cap::replaceSelection(cur);
705 breakParagraph(cur, 1);
710 case LFUN_BREAKPARAGRAPH_SKIP: {
711 // When at the beginning of a paragraph, remove
712 // indentation. Otherwise, do the same as LFUN_BREAKPARAGRAPH.
713 lyx::cap::replaceSelection(cur);
715 cur.paragraph().params().labelWidthString(string());
717 breakParagraph(cur, 0);
723 case LFUN_PARAGRAPH_SPACING: {
724 Paragraph & par = cur.paragraph();
725 Spacing::Space cur_spacing = par.params().spacing().getSpace();
726 float cur_value = 1.0;
727 if (cur_spacing == Spacing::Other)
728 cur_value = par.params().spacing().getValue();
730 istringstream is(cmd.argument);
733 Spacing::Space new_spacing = cur_spacing;
734 float new_value = cur_value;
736 lyxerr << "Missing argument to `paragraph-spacing'"
738 } else if (tmp == "single") {
739 new_spacing = Spacing::Single;
740 } else if (tmp == "onehalf") {
741 new_spacing = Spacing::Onehalf;
742 } else if (tmp == "double") {
743 new_spacing = Spacing::Double;
744 } else if (tmp == "other") {
745 new_spacing = Spacing::Other;
748 lyxerr << "new_value = " << tmpval << endl;
751 } else if (tmp == "default") {
752 new_spacing = Spacing::Default;
754 lyxerr << _("Unknown spacing argument: ")
755 << cmd.argument << endl;
757 if (cur_spacing != new_spacing || cur_value != new_value) {
758 par.params().spacing(Spacing(new_spacing, new_value));
764 case LFUN_INSET_APPLY: {
765 string const name = cmd.getArg(0);
766 InsetBase * inset = bv->owner()->getDialogs().getOpenInset(name);
768 FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
769 inset->dispatch(cur, fr);
771 FuncRequest fr(LFUN_INSET_INSERT, cmd.argument);
777 case LFUN_INSET_INSERT: {
779 InsetBase * inset = createInset(bv, cmd);
781 insertInset(cur, inset);
785 case LFUN_INSET_SETTINGS:
786 if (cur.inset().asUpdatableInset())
787 cur.inset().asUpdatableInset()->showInsetDialog(bv);
790 case LFUN_NEXT_INSET_TOGGLE: {
791 InsetBase * inset = cur.nextInset();
793 cur.clearSelection();
794 FuncRequest fr = cmd;
795 fr.action = LFUN_INSET_TOGGLE;
796 inset->dispatch(cur, fr);
801 case LFUN_KEYMAP_TOGGLE:
802 cur.clearSelection();
806 case LFUN_SPACE_INSERT:
807 if (cur.paragraph().layout()->free_spacing)
808 insertChar(cur, ' ');
810 doInsertInset(cur, this, cmd, false, false);
811 moveCursor(cur, false);
814 case LFUN_HYPHENATION:
815 specialChar(cur, InsetSpecialChar::HYPHENATION);
818 case LFUN_LIGATURE_BREAK:
819 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
823 specialChar(cur, InsetSpecialChar::LDOTS);
826 case LFUN_END_OF_SENTENCE:
827 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
830 case LFUN_MENU_SEPARATOR:
831 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
834 case LFUN_UPCASE_WORD:
835 changeCase(cur, LyXText::text_uppercase);
838 case LFUN_LOWCASE_WORD:
839 changeCase(cur, LyXText::text_lowercase);
842 case LFUN_CAPITALIZE_WORD:
843 changeCase(cur, LyXText::text_capitalization);
846 case LFUN_TRANSPOSE_CHARS:
852 cur.message(_("Paste"));
853 lyx::cap::replaceSelection(cur);
854 #warning FIXME Check if the arg is in the domain of available selections.
855 if (isStrUnsignedInt(cmd.argument))
856 pasteSelection(cur, strToUnsignedInt(cmd.argument));
858 pasteSelection(cur, 0);
859 cur.clearSelection(); // bug 393
865 cutSelection(cur, true, true);
866 cur.message(_("Cut"));
871 cur.message(_("Copy"));
875 cur.message(tostr(cursorX(cur.top())) + ' '
876 + tostr(cursorY(cur.top())));
882 istringstream is(cmd.argument);
885 lyxerr << "SETXY: Could not parse coordinates in '"
886 << cmd.argument << std::endl;
888 setCursorFromCoordinates(cur, x, y);
893 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
895 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
902 cur.message(cur.paragraph().layout()->name());
906 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
907 << cmd.argument << endl;
909 // This is not the good solution to the empty argument
910 // problem, but it will hopefully suffice for 1.2.0.
911 // The correct solution would be to augument the
912 // function list/array with information about what
913 // functions needs arguments and their type.
914 if (cmd.argument.empty()) {
915 cur.errorMessage(_("LyX function 'layout' needs an argument."));
919 // Derive layout number from given argument (string)
920 // and current buffer's textclass (number)
921 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
922 bool hasLayout = tclass.hasLayout(cmd.argument);
923 string layout = cmd.argument;
925 // If the entry is obsolete, use the new one instead.
927 string const & obs = tclass[layout]->obsoleted_by();
933 cur.errorMessage(string(N_("Layout ")) + cmd.argument +
938 bool change_layout = (current_layout != layout);
940 if (!change_layout && cur.selection() &&
941 cur.selBegin().par() != cur.selEnd().par())
943 par_type spit = cur.selBegin().par();
944 par_type epit = cur.selEnd().par() + 1;
945 while (spit != epit) {
946 if (pars_[spit].layout()->name() != current_layout) {
947 change_layout = true;
955 current_layout = layout;
956 setLayout(cur, layout);
957 bv->owner()->setLayout(layout);
963 case LFUN_PASTESELECTION: {
964 cur.clearSelection();
965 string const clip = bv->getClipboard();
967 if (cmd.argument == "paragraph")
968 insertStringAsParagraphs(cur, clip);
970 insertStringAsLines(cur, clip);
976 gotoInset(cur, InsetBase::ERROR_CODE, false);
980 gotoInset(cur, InsetBase::NOTE_CODE, false);
983 case LFUN_REFERENCE_GOTO: {
984 vector<InsetOld_code> tmp;
985 tmp.push_back(InsetBase::LABEL_CODE);
986 tmp.push_back(InsetBase::REF_CODE);
987 gotoInset(cur, tmp, true);
992 lyx::cap::replaceSelection(cur);
993 Paragraph & par = cur.paragraph();
994 lyx::pos_type pos = cur.pos();
998 else if (cur.prevInset() && cur.prevInset()->isSpace())
1001 c = par.getChar(pos - 1);
1003 LyXLayout_ptr const & style = par.layout();
1005 BufferParams const & bufparams = bv->buffer()->params();
1006 if (!style->pass_thru
1007 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1008 string arg = cmd.argument;
1009 if (arg == "single")
1010 cur.insert(new InsetQuotes(c,
1011 bufparams.quotes_language,
1012 InsetQuotes::SingleQ));
1013 else if (arg == "double")
1014 cur.insert(new InsetQuotes(c,
1015 bufparams.quotes_language,
1016 InsetQuotes::DoubleQ));
1018 cur.insert(new InsetQuotes(c, bufparams));
1021 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1025 case LFUN_DATE_INSERT: {
1026 lyx::cap::replaceSelection(cur);
1027 time_t now_time_t = time(NULL);
1028 struct tm * now_tm = localtime(&now_time_t);
1029 setlocale(LC_TIME, "");
1031 if (!cmd.argument.empty())
1034 arg = lyxrc.date_insert_format;
1036 int const datetmp_len =
1037 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1039 for (int i = 0; i < datetmp_len; i++)
1040 insertChar(cur, datetmp[i]);
1043 moveCursor(cur, false);
1047 case LFUN_MOUSE_TRIPLE:
1048 if (cmd.button() == mouse_button::button1) {
1049 selection_possible = true;
1054 bv->haveSelection(cur.selection());
1058 case LFUN_MOUSE_DOUBLE:
1059 if (cmd.button() == mouse_button::button1) {
1060 selection_possible = true;
1061 selectWord(cur, lyx::WHOLE_WORD_STRICT);
1062 bv->haveSelection(cur.selection());
1066 case LFUN_MOUSE_MOTION: {
1067 // Only use motion with button 1
1068 //if (cmd.button() != mouse_button::button1)
1070 // We want to use only motion events for which
1071 // the button press event was on the drawing area too.
1072 if (!selection_possible) {
1073 lyxerr[Debug::ACTION] << "BufferView::Pimpl::"
1074 "dispatch: no selection possible\n";
1075 lyxerr << "BufferView::Pimpl::dispatch: no selection possible\n";
1078 CursorSlice old = cur.top();
1079 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1081 // This is to allow jumping over large insets
1082 // FIXME: shouldn't be top-text-specific
1083 if (isMainText() && cur.top() == old) {
1084 if (cmd.y - bv->top_y() >= bv->workHeight())
1086 else if (cmd.y - bv->top_y() < 0)
1090 // don't set anchor_
1091 bv->cursor().setCursor(cur, true);
1092 lyxerr << "MOTION: " << bv->cursor() << endl;
1096 // Single-click on work area
1097 case LFUN_MOUSE_PRESS: {
1098 // ok ok, this is a hack (for xforms)
1099 // We shouldn't go further down as we really need to. Only do the
1100 // scrolling and be done with this. Otherwise we may open some
1101 // dialogs (Jug 20020424).
1102 if (cmd.button() == mouse_button::button4) {
1103 bv->scroll(-lyxrc.wheel_jump);
1107 if (cmd.button() == mouse_button::button5) {
1108 bv->scroll(lyxrc.wheel_jump);
1112 // Right click on a footnote flag opens float menu
1113 if (cmd.button() == mouse_button::button3) {
1114 cur.clearSelection();
1115 selection_possible = false;
1119 // Middle button press pastes if we have a selection
1120 // We do this here as if the selection was inside an inset
1121 // it could get cleared on the unlocking of the inset so
1122 // we have to check this first
1123 bool paste_internally = false;
1124 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1125 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1126 paste_internally = true;
1129 selection_possible = true;
1131 // Clear the selection
1132 cur.clearSelection();
1134 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1137 cur.x_target() = cursorX(cur.top());
1139 if (bv->fitCursor())
1140 selection_possible = false;
1142 // Insert primary selection with middle mouse
1143 // if there is a local selection in the current buffer,
1145 if (cmd.button() == mouse_button::button2) {
1146 if (paste_internally)
1147 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1149 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1150 selection_possible = false;
1155 case LFUN_MOUSE_RELEASE: {
1156 // do nothing if we used the mouse wheel
1157 if (cmd.button() == mouse_button::button4
1158 || cmd.button() == mouse_button::button5) {
1163 selection_possible = false;
1165 if (cmd.button() == mouse_button::button2)
1169 if (cmd.button() == mouse_button::button1)
1170 bv->haveSelection(cur.selection());
1173 bv->owner()->updateMenubar();
1174 bv->owner()->updateToolbar();
1178 case LFUN_SELFINSERT: {
1179 if (cmd.argument.empty())
1182 // Automatically delete the currently selected
1183 // text and replace it with what is being
1184 // typed in now. Depends on lyxrc settings
1185 // "auto_region_delete", which defaults to
1188 if (lyxrc.auto_region_delete) {
1189 if (cur.selection())
1190 cutSelection(cur, false, false);
1191 bv->haveSelection(false);
1194 cur.clearSelection();
1195 LyXFont const old_font = real_current_font;
1197 string::const_iterator cit = cmd.argument.begin();
1198 string::const_iterator end = cmd.argument.end();
1199 for (; cit != end; ++cit)
1200 bv->owner()->getIntl().getTransManager().
1201 TranslateAndInsert(*cit, this);
1204 moveCursor(cur, false);
1206 // real_current_font.number can change so we need to
1207 // update the minibuffer
1208 if (old_font != real_current_font)
1209 bv->updateScrollbar();
1214 InsetCommandParams p("url");
1215 string const data = InsetCommandMailer::params2string("url", p);
1216 bv->owner()->getDialogs().show("url", data, 0);
1220 case LFUN_HTMLURL: {
1221 InsetCommandParams p("htmlurl");
1222 string const data = InsetCommandMailer::params2string("url", p);
1223 bv->owner()->getDialogs().show("url", data, 0);
1227 case LFUN_INSERT_LABEL: {
1228 InsetCommandParams p("label");
1229 string const data = InsetCommandMailer::params2string("label", p);
1230 bv->owner()->getDialogs().show("label", data, 0);
1236 case LFUN_INSET_LIST:
1237 case LFUN_INSET_THEOREM:
1238 case LFUN_INSET_CAPTION:
1240 case LFUN_INSERT_NOTE:
1241 case LFUN_INSERT_CHARSTYLE:
1242 case LFUN_INSERT_BOX:
1243 case LFUN_INSERT_BRANCH:
1244 case LFUN_INSERT_BIBITEM:
1245 case LFUN_INSET_ERT:
1246 case LFUN_INSET_FLOAT:
1247 case LFUN_INSET_FOOTNOTE:
1248 case LFUN_INSET_MARGINAL:
1249 case LFUN_INSET_OPTARG:
1250 case LFUN_INSET_WIDE_FLOAT:
1251 case LFUN_INSET_WRAP:
1252 case LFUN_TABULAR_INSERT:
1253 case LFUN_ENVIRONMENT_INSERT:
1254 // Open the inset, and move the current selection
1256 doInsertInset(cur, this, cmd, true, true);
1259 case LFUN_INDEX_INSERT:
1260 // Just open the inset
1261 doInsertInset(cur, this, cmd, true, false);
1264 case LFUN_INDEX_PRINT:
1265 case LFUN_TOC_INSERT:
1267 case LFUN_INSERT_LINE:
1268 case LFUN_INSERT_PAGEBREAK:
1270 doInsertInset(cur, this, cmd, false, false);
1273 case LFUN_DEPTH_MIN:
1274 changeDepth(cur, DEC_DEPTH);
1277 case LFUN_DEPTH_PLUS:
1278 changeDepth(cur, INC_DEPTH);
1281 case LFUN_MATH_DISPLAY:
1282 mathDispatch(cur, cmd, true);
1285 case LFUN_MATH_IMPORT_SELECTION:
1286 case LFUN_MATH_MODE:
1287 mathDispatch(cur, cmd, false);
1290 case LFUN_MATH_MACRO:
1291 if (cmd.argument.empty())
1292 cur.errorMessage(N_("Missing argument"));
1294 string s = cmd.argument;
1295 string const s1 = token(s, ' ', 1);
1296 int const nargs = s1.empty() ? 0 : atoi(s1);
1297 string const s2 = token(s, ' ', 2);
1298 string const type = s2.empty() ? "newcommand" : s2;
1299 cur.insert(new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
1300 cur.nextInset()->edit(cur, true);
1304 case LFUN_INSERT_MATH:
1305 case LFUN_INSERT_MATRIX:
1306 case LFUN_MATH_DELIM: {
1307 cur.insert(new MathHullInset);
1308 cur.dispatch(FuncRequest(LFUN_RIGHT));
1309 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
1315 LyXFont font(LyXFont::ALL_IGNORE);
1316 font.setEmph(LyXFont::TOGGLE);
1317 toggleAndShow(cur, this, font);
1322 LyXFont font(LyXFont::ALL_IGNORE);
1323 font.setSeries(LyXFont::BOLD_SERIES);
1324 toggleAndShow(cur, this, font);
1329 LyXFont font(LyXFont::ALL_IGNORE);
1330 font.setNoun(LyXFont::TOGGLE);
1331 toggleAndShow(cur, this, font);
1336 LyXFont font(LyXFont::ALL_IGNORE);
1337 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1338 toggleAndShow(cur, this, font);
1343 LyXFont font(LyXFont::ALL_IGNORE);
1344 font.setFamily(LyXFont::SANS_FAMILY);
1345 toggleAndShow(cur, this, font);
1350 LyXFont font(LyXFont::ALL_IGNORE);
1351 font.setFamily(LyXFont::ROMAN_FAMILY);
1352 toggleAndShow(cur, this, font);
1356 case LFUN_DEFAULT: {
1357 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1358 toggleAndShow(cur, this, font);
1362 case LFUN_UNDERLINE: {
1363 LyXFont font(LyXFont::ALL_IGNORE);
1364 font.setUnderbar(LyXFont::TOGGLE);
1365 toggleAndShow(cur, this, font);
1369 case LFUN_FONT_SIZE: {
1370 LyXFont font(LyXFont::ALL_IGNORE);
1371 font.setLyXSize(cmd.argument);
1372 toggleAndShow(cur, this, font);
1376 case LFUN_LANGUAGE: {
1377 Language const * lang = languages.getLanguage(cmd.argument);
1380 LyXFont font(LyXFont::ALL_IGNORE);
1381 font.setLanguage(lang);
1382 toggleAndShow(cur, this, font);
1387 case LFUN_FREEFONT_APPLY:
1388 toggleAndShow(cur, this, freefont, toggleall);
1389 cur.message(_("Character set"));
1392 // Set the freefont using the contents of \param data dispatched from
1393 // the frontends and apply it at the current cursor location.
1394 case LFUN_FREEFONT_UPDATE: {
1397 if (bv_funcs::string2font(cmd.argument, font, toggle)) {
1400 toggleAndShow(cur, this, freefont, toggleall);
1401 cur.message(_("Character set"));
1406 case LFUN_FINISHED_LEFT:
1407 lyxerr << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1410 case LFUN_FINISHED_RIGHT:
1411 lyxerr << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1415 case LFUN_FINISHED_UP:
1416 lyxerr << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1420 case LFUN_FINISHED_DOWN:
1421 lyxerr << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1425 case LFUN_LAYOUT_PARAGRAPH: {
1427 params2string(cur.paragraph(), data);
1428 data = "show\n" + data;
1429 bv->owner()->getDialogs().show("paragraph", data);
1433 case LFUN_PARAGRAPH_UPDATE: {
1434 if (!bv->owner()->getDialogs().visible("paragraph"))
1437 params2string(cur.paragraph(), data);
1439 // Will the paragraph accept changes from the dialog?
1440 InsetBase & inset = cur.inset();
1441 bool const accept = !inset.forceDefaultParagraphs(&inset);
1443 data = "update " + tostr(accept) + '\n' + data;
1444 bv->owner()->getDialogs().update("paragraph", data);
1449 case LFUN_CIRCUMFLEX:
1459 case LFUN_SPECIAL_CARON:
1462 case LFUN_HUNG_UMLAUT:
1465 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1466 if (!cmd.argument.empty())
1467 bv->owner()->getIntl().getTransManager()
1468 .TranslateAndInsert(cmd.argument[0], this);
1471 case LFUN_FLOAT_LIST: {
1472 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1473 if (tclass.floats().typeExist(cmd.argument)) {
1474 // not quite sure if we want this...
1476 cur.clearSelection();
1477 breakParagraph(cur);
1479 if (cur.lastpos() != 0) {
1481 breakParagraph(cur);
1484 setLayout(cur, tclass.defaultLayoutName());
1485 setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1486 insertInset(cur, new InsetFloatList(cmd.argument));
1488 lyxerr << "Non-existent float type: "
1489 << cmd.argument << endl;
1494 case LFUN_ACCEPT_CHANGE: {
1499 case LFUN_REJECT_CHANGE: {
1504 case LFUN_THESAURUS_ENTRY: {
1505 string arg = cmd.argument;
1507 arg = cur.selectionAsString(false);
1509 if (arg.size() > 100 || arg.empty()) {
1510 // Get word or selection
1511 selectWordWhenUnderCursor(cur, lyx::WHOLE_WORD);
1512 arg = cur.selectionAsString(false);
1515 bv->owner()->getDialogs().show("thesaurus", arg);
1519 case LFUN_PARAGRAPH_APPLY: {
1520 // Given data, an encoding of the ParagraphParameters
1521 // generated in the Paragraph dialog, this function sets
1522 // the current paragraph appropriately.
1523 istringstream is(cmd.argument);
1526 ParagraphParameters params;
1531 params.labelWidthString(),
1533 cur.message(_("Paragraph layout set"));
1537 case LFUN_INSET_DIALOG_SHOW: {
1538 InsetBase * inset = cur.nextInset();
1540 FuncRequest fr(LFUN_INSET_DIALOG_SHOW);
1541 inset->dispatch(cur, fr);
1547 if (cur.selection()) {
1548 cur.selection() = false;
1551 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1562 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1563 FuncStatus & flag) const
1565 BOOST_ASSERT(cur.text() == this);
1566 LyXFont const & font = real_current_font;
1569 switch (cmd.action) {
1571 case LFUN_DEPTH_MIN:
1572 enable = changeDepthAllowed(cur, DEC_DEPTH);
1575 case LFUN_DEPTH_PLUS:
1576 enable = changeDepthAllowed(cur, INC_DEPTH);
1579 case LFUN_INSET_OPTARG:
1580 enable = cur.paragraph().layout()->optionalargs;
1584 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1588 // the functions which insert insets
1589 InsetOld::Code code = InsetOld::NO_CODE;
1590 switch (cmd.action) {
1591 case LFUN_DIALOG_SHOW_NEW_INSET:
1592 if (cmd.argument == "bibitem")
1593 code = InsetOld::BIBITEM_CODE;
1594 else if (cmd.argument == "bibtex")
1595 code = InsetOld::BIBTEX_CODE;
1596 else if (cmd.argument == "box")
1597 code = InsetOld::BOX_CODE;
1598 else if (cmd.argument == "branch")
1599 code = InsetOld::BRANCH_CODE;
1600 else if (cmd.argument == "citation")
1601 code = InsetOld::CITE_CODE;
1602 else if (cmd.argument == "ert")
1603 code = InsetOld::ERT_CODE;
1604 else if (cmd.argument == "external")
1605 code = InsetOld::EXTERNAL_CODE;
1606 else if (cmd.argument == "float")
1607 code = InsetOld::FLOAT_CODE;
1608 else if (cmd.argument == "graphics")
1609 code = InsetOld::GRAPHICS_CODE;
1610 else if (cmd.argument == "include")
1611 code = InsetOld::INCLUDE_CODE;
1612 else if (cmd.argument == "index")
1613 code = InsetOld::INDEX_CODE;
1614 else if (cmd.argument == "label")
1615 code = InsetOld::LABEL_CODE;
1616 else if (cmd.argument == "note")
1617 code = InsetOld::NOTE_CODE;
1618 else if (cmd.argument == "ref")
1619 code = InsetOld::REF_CODE;
1620 else if (cmd.argument == "toc")
1621 code = InsetOld::TOC_CODE;
1622 else if (cmd.argument == "url")
1623 code = InsetOld::URL_CODE;
1624 else if (cmd.argument == "vspace")
1625 code = InsetOld::VSPACE_CODE;
1626 else if (cmd.argument == "wrap")
1627 code = InsetOld::WRAP_CODE;
1630 case LFUN_INSET_ERT:
1631 code = InsetOld::ERT_CODE;
1633 case LFUN_INSET_FOOTNOTE:
1634 code = InsetOld::FOOT_CODE;
1636 case LFUN_TABULAR_INSERT:
1637 code = InsetOld::TABULAR_CODE;
1639 case LFUN_INSET_MARGINAL:
1640 code = InsetOld::MARGIN_CODE;
1642 case LFUN_INSET_FLOAT:
1643 case LFUN_INSET_WIDE_FLOAT:
1644 code = InsetOld::FLOAT_CODE;
1646 case LFUN_INSET_WRAP:
1647 code = InsetOld::WRAP_CODE;
1649 case LFUN_FLOAT_LIST:
1650 code = InsetOld::FLOAT_LIST_CODE;
1653 case LFUN_INSET_LIST:
1654 code = InsetOld::LIST_CODE;
1656 case LFUN_INSET_THEOREM:
1657 code = InsetOld::THEOREM_CODE;
1660 case LFUN_INSET_CAPTION:
1661 code = InsetOld::CAPTION_CODE;
1663 case LFUN_INSERT_NOTE:
1664 code = InsetOld::NOTE_CODE;
1666 case LFUN_INSERT_CHARSTYLE:
1667 code = InsetOld::CHARSTYLE_CODE;
1668 if (buf->params().getLyXTextClass().charstyles().empty())
1671 case LFUN_INSERT_BOX:
1672 code = InsetOld::BOX_CODE;
1674 case LFUN_INSERT_BRANCH:
1675 code = InsetOld::BRANCH_CODE;
1676 if (buf->params().branchlist().empty())
1679 case LFUN_INSERT_LABEL:
1680 code = InsetOld::LABEL_CODE;
1682 case LFUN_INSET_OPTARG:
1683 code = InsetOld::OPTARG_CODE;
1685 case LFUN_ENVIRONMENT_INSERT:
1686 code = InsetOld::BOX_CODE;
1688 case LFUN_INDEX_INSERT:
1689 code = InsetOld::INDEX_CODE;
1691 case LFUN_INDEX_PRINT:
1692 code = InsetOld::INDEX_PRINT_CODE;
1694 case LFUN_TOC_INSERT:
1695 code = InsetOld::TOC_CODE;
1699 code = InsetOld::URL_CODE;
1702 // always allow this, since we will inset a raw quote
1703 // if an inset is not allowed.
1705 case LFUN_HYPHENATION:
1706 case LFUN_LIGATURE_BREAK:
1708 case LFUN_MENU_SEPARATOR:
1710 case LFUN_END_OF_SENTENCE:
1711 code = InsetOld::SPECIALCHAR_CODE;
1713 case LFUN_SPACE_INSERT:
1714 // slight hack: we know this is allowed in math mode
1716 code = InsetOld::SPACE_CODE;
1718 case LFUN_INSET_DIALOG_SHOW: {
1719 InsetBase * inset = cur.nextInset();
1722 code = inset->lyxCode();
1723 if (!(code == InsetOld::INCLUDE_CODE
1724 || code == InsetOld::BIBTEX_CODE
1725 || code == InsetOld::FLOAT_LIST_CODE
1726 || code == InsetOld::TOC_CODE))
1735 if (code != InsetOld::NO_CODE
1736 && (cur.empty() || !cur.inset().insetAllowed(code)))
1741 case LFUN_DIALOG_SHOW_NEW_INSET:
1742 case LFUN_INSET_ERT:
1743 case LFUN_INSERT_BOX:
1744 case LFUN_INSERT_BRANCH:
1745 case LFUN_ENVIRONMENT_INSERT:
1746 case LFUN_INDEX_INSERT:
1747 case LFUN_INDEX_PRINT:
1748 case LFUN_TOC_INSERT:
1752 case LFUN_HYPHENATION:
1753 case LFUN_LIGATURE_BREAK:
1755 case LFUN_MENU_SEPARATOR:
1757 case LFUN_END_OF_SENTENCE:
1758 case LFUN_SPACE_INSERT:
1759 case LFUN_INSET_DIALOG_SHOW:
1763 flag.setOnOff(font.emph() == LyXFont::ON);
1767 flag.setOnOff(font.noun() == LyXFont::ON);
1771 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1775 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1779 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1783 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1786 case LFUN_DELETE_WORD_FORWARD:
1787 case LFUN_DELETE_WORD_BACKWARD:
1788 case LFUN_DELETE_LINE_FORWARD:
1789 case LFUN_WORDRIGHT:
1800 case LFUN_UP_PARAGRAPHSEL:
1801 case LFUN_DOWN_PARAGRAPHSEL:
1806 case LFUN_WORDRIGHTSEL:
1807 case LFUN_WORDLEFTSEL:
1809 case LFUN_UP_PARAGRAPH:
1810 case LFUN_DOWN_PARAGRAPH:
1815 case LFUN_BREAKLINE:
1817 case LFUN_DELETE_SKIP:
1818 case LFUN_BACKSPACE:
1819 case LFUN_BACKSPACE_SKIP:
1820 case LFUN_BREAKPARAGRAPH:
1821 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1822 case LFUN_BREAKPARAGRAPH_SKIP:
1823 case LFUN_PARAGRAPH_SPACING:
1824 case LFUN_INSET_APPLY:
1825 case LFUN_INSET_INSERT:
1826 case LFUN_NEXT_INSET_TOGGLE:
1827 case LFUN_UPCASE_WORD:
1828 case LFUN_LOWCASE_WORD:
1829 case LFUN_CAPITALIZE_WORD:
1830 case LFUN_TRANSPOSE_CHARS:
1837 case LFUN_GETLAYOUT:
1839 case LFUN_PASTESELECTION:
1840 case LFUN_GOTOERROR:
1842 case LFUN_REFERENCE_GOTO:
1843 case LFUN_DATE_INSERT:
1844 case LFUN_SELFINSERT:
1845 case LFUN_INSERT_LABEL:
1846 case LFUN_INSERT_NOTE:
1847 case LFUN_INSERT_CHARSTYLE:
1848 case LFUN_INSERT_BIBITEM:
1849 case LFUN_INSET_FLOAT:
1850 case LFUN_INSET_FOOTNOTE:
1851 case LFUN_INSET_MARGINAL:
1852 case LFUN_INSET_WIDE_FLOAT:
1853 case LFUN_INSET_WRAP:
1854 case LFUN_TABULAR_INSERT:
1855 case LFUN_INSERT_LINE:
1856 case LFUN_INSERT_PAGEBREAK:
1857 case LFUN_MATH_DISPLAY:
1858 case LFUN_MATH_IMPORT_SELECTION:
1859 case LFUN_MATH_MODE:
1860 case LFUN_MATH_MACRO:
1861 case LFUN_INSERT_MATH:
1862 case LFUN_INSERT_MATRIX:
1863 case LFUN_MATH_DELIM:
1865 case LFUN_UNDERLINE:
1866 case LFUN_FONT_SIZE:
1868 case LFUN_FREEFONT_APPLY:
1869 case LFUN_FREEFONT_UPDATE:
1870 case LFUN_LAYOUT_PARAGRAPH:
1871 case LFUN_PARAGRAPH_UPDATE:
1873 case LFUN_CIRCUMFLEX:
1883 case LFUN_SPECIAL_CARON:
1886 case LFUN_HUNG_UMLAUT:
1889 case LFUN_FLOAT_LIST:
1890 case LFUN_ACCEPT_CHANGE:
1891 case LFUN_REJECT_CHANGE:
1892 case LFUN_THESAURUS_ENTRY:
1893 case LFUN_PARAGRAPH_APPLY:
1895 case LFUN_KEYMAP_TOGGLE:
1896 // these are handled in our dispatch()
1904 flag.enabled(enable);