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.
21 #include "bufferparams.h"
22 #include "BufferView.h"
25 #include "dispatchresult.h"
27 #include "funcrequest.h"
33 #include "paragraph.h"
34 #include "paragraph_funcs.h"
35 #include "ParagraphParameters.h"
39 #include "frontends/Dialogs.h"
40 #include "frontends/LyXView.h"
42 #include "insets/insetcommand.h"
43 #include "insets/insetnewline.h"
44 #include "insets/insetspecialchar.h"
45 #include "insets/insettext.h"
47 #include "support/lstrings.h"
48 #include "support/tostr.h"
49 #include "support/std_sstream.h"
51 #include "mathed/formulabase.h"
55 using bv_funcs::replaceSelection;
59 using lyx::support::isStrUnsignedInt;
60 using lyx::support::strToUnsignedInt;
65 using std::istringstream;
69 extern string current_layout;
70 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
72 // the selection possible is needed, that only motion events are
73 // used, where the bottom press event was on the drawing area too
74 bool selection_possible = false;
80 LyXFont freefont(LyXFont::ALL_IGNORE);
81 bool toggleall = false;
84 void toggleAndShow(BufferView * bv, LyXText * text,
85 LyXFont const & font, bool toggleall = true)
90 text->toggleFree(font, toggleall);
93 if (font.language() != ignore_language ||
94 font.number() != LyXFont::IGNORE) {
95 LyXCursor & cursor = text->cursor;
96 Paragraph & par = *text->cursorPar();
97 text->bidi.computeTables(par, *bv->buffer(),
98 *par.getRow(cursor.pos()));
99 if (cursor.boundary() !=
100 text->bidi.isBoundary(*bv->buffer(), par,
102 text->real_current_font))
103 text->setCursor(cursor.par(), cursor.pos(),
104 false, !cursor.boundary());
109 /// Apply the contents of freefont at the current cursor location.
110 void apply_freefont(BufferView * bv, LyXText * text)
112 toggleAndShow(bv, text, freefont, toggleall);
113 bv->owner()->view_state_changed();
114 bv->owner()->message(_("Character set"));
118 /** Set the freefont using the contents of \param data dispatched from
119 * the frontends and apply it at the current cursor location.
121 void update_and_apply_freefont(BufferView * bv, LyXText * text,
126 if (bv_funcs::string2font(data, font, toggle)) {
129 apply_freefont(bv, text);
134 void emph(BufferView * bv, LyXText * text)
136 LyXFont font(LyXFont::ALL_IGNORE);
137 font.setEmph(LyXFont::TOGGLE);
138 toggleAndShow(bv, text, font);
142 void bold(BufferView * bv, LyXText * text)
144 LyXFont font(LyXFont::ALL_IGNORE);
145 font.setSeries(LyXFont::BOLD_SERIES);
146 toggleAndShow(bv, text, font);
150 void noun(BufferView * bv, LyXText * text)
152 LyXFont font(LyXFont::ALL_IGNORE);
153 font.setNoun(LyXFont::TOGGLE);
154 toggleAndShow(bv, text, font);
158 void lang(BufferView * bv, string const & l, LyXText * text)
160 Language const * lang = languages.getLanguage(l);
164 LyXFont font(LyXFont::ALL_IGNORE);
165 font.setLanguage(lang);
166 toggleAndShow(bv, text, font);
170 void code(BufferView * bv, LyXText * text)
172 LyXFont font(LyXFont::ALL_IGNORE);
173 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
174 toggleAndShow(bv, text, font);
178 void sans(BufferView * bv, LyXText * text)
180 LyXFont font(LyXFont::ALL_IGNORE);
181 font.setFamily(LyXFont::SANS_FAMILY);
182 toggleAndShow(bv, text, font);
186 void roman(BufferView * bv, LyXText * text)
188 LyXFont font(LyXFont::ALL_IGNORE);
189 font.setFamily(LyXFont::ROMAN_FAMILY);
190 toggleAndShow(bv, text, font);
194 void styleReset(BufferView * bv, LyXText * text)
196 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
197 toggleAndShow(bv, text, font);
201 void underline(BufferView * bv, LyXText * text)
203 LyXFont font(LyXFont::ALL_IGNORE);
204 font.setUnderbar(LyXFont::TOGGLE);
205 toggleAndShow(bv, text, font);
209 void fontSize(BufferView * bv, string const & size, LyXText * text)
211 LyXFont font(LyXFont::ALL_IGNORE);
212 font.setLyXSize(size);
213 toggleAndShow(bv, text, font);
217 void moveCursor(BufferView * bv, bool selecting)
219 LyXText * lt = bv->getLyXText();
221 // if (!lt->selection.set())
222 // lt->selection.cursor = lt->cursor;
224 if (selecting || lt->selection.mark())
227 if (!lt->selection.set())
228 bv->haveSelection(false);
233 void finishChange(BufferView * bv, bool selecting = false)
236 moveCursor(bv, selecting);
237 bv->owner()->view_state_changed();
245 string const freefont2string()
248 if (font2string(freefont, toggleall, data))
257 InsetOld * LyXText::checkInsetHit(int x, int y)
259 ParagraphList::iterator pit;
260 ParagraphList::iterator end;
262 getParsInRange(ownerParagraphs(),
263 bv()->top_y(), bv()->top_y() + bv()->workHeight(),
266 lyxerr << "checkInsetHit: x: " << x << " y: " << y << endl;
267 for ( ; pit != end; ++pit) {
268 InsetList::iterator iit = pit->insetlist.begin();
269 InsetList::iterator iend = pit->insetlist.end();
270 for ( ; iit != iend; ++iit) {
271 InsetOld * inset = iit->inset;
272 //lyxerr << "examining inset " << inset
273 // << " xy: " << inset->x() << "/" << inset->y()
274 // << " x: " << inset->x() << "..." << inset->x() + inset->width()
275 // << " y: " << inset->y() - inset->ascent() << "..."
276 // << inset->y() + inset->descent()
279 && x <= inset->x() + inset->width()
280 && y >= inset->y() - inset->ascent()
281 && y <= inset->y() + inset->descent())
283 lyxerr << "Hit inset: " << inset << endl;
288 lyxerr << "No inset hit. " << endl;
293 bool LyXText::gotoNextInset(vector<InsetOld::Code> const & codes,
294 string const & contents)
296 ParagraphList::iterator end = ownerParagraphs().end();
297 ParagraphList::iterator pit = cursorPar();
298 pos_type pos = cursor.pos();
302 if (pos + 1 < pit->size()) {
309 } while (pit != end &&
310 !(pit->isInset(pos) &&
311 (inset = pit->getInset(pos)) != 0 &&
312 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
314 static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
320 setCursor(parOffset(pit), pos, false);
325 void LyXText::gotoInset(vector<InsetOld::Code> const & codes,
331 if (same_content && cursor.pos() < cursorPar()->size()
332 && cursorPar()->isInset(cursor.pos())) {
333 InsetOld const * inset = cursorPar()->getInset(cursor.pos());
334 if (find(codes.begin(), codes.end(), inset->lyxCode())
336 contents = static_cast<InsetCommand const *>(inset)->getContents();
339 if (!gotoNextInset(codes, contents)) {
340 if (cursor.pos() || cursorPar() != ownerParagraphs().begin()) {
341 LyXCursor tmp = cursor;
344 if (!gotoNextInset(codes, contents)) {
346 bv()->owner()->message(_("No more insets"));
349 bv()->owner()->message(_("No more insets"));
353 selection.cursor = cursor;
357 void LyXText::gotoInset(InsetOld::Code code, bool same_content)
359 gotoInset(vector<InsetOld::Code>(1, code), same_content);
363 void LyXText::cursorPrevious()
365 int y = bv()->top_y();
367 ParagraphList::iterator cpit = cursorPar();
368 RowList::iterator crit = cpit->getRow(cursor.pos());
370 if (isFirstRow(cpit, *crit)) {
372 bv()->updateScrollbar();
376 setCursorFromCoordinates(bv()->x_target(), y);
379 if (crit == bv()->text->cursorRow()) {
380 // we have a row which is taller than the workarea. The
381 // simplest solution is to move to the previous row instead.
386 int new_y = + crit->height() - bv()->workHeight() + 1;
389 new_y += bv()->text->cursor.y()
390 + bv()->cursor().innerInset()->insetInInsetY()
393 new_y += cursor.y() - crit->baseline();
396 previousRow(cpit, crit);
398 setCursor(cur, parOffset(cpit), crit->pos(), false);
399 if (cur.y() > bv()->top_y())
401 bv()->updateScrollbar();
405 void LyXText::cursorNext()
407 int topy = bv()->top_y();
409 ParagraphList::iterator cpit = cursorPar();
410 RowList::iterator crit = cpit->getRow(cursor.pos());
412 if (isLastRow(cpit, *crit)) {
413 int y = cursor.y() - crit->baseline() + crit->height();
414 if (y > topy + bv()->workHeight())
415 bv()->updateScrollbar();
419 int y = topy + bv()->workHeight();
420 if (inset_owner && !topy) {
421 y += - bv()->text->cursor.y()
423 - bv()->cursor().innerInset()->insetInInsetY();
426 ParagraphList::iterator dummypit;
427 Row const & row = *getRowNearY(y, dummypit);
428 y = dummypit->y + row.y_offset();
430 setCursorFromCoordinates(bv()->x_target(), y);
431 // + bv->workHeight());
435 if (crit == bv()->text->cursorRow()) {
436 // we have a row which is taller than the workarea. The
437 // simplest solution is to move to the next row instead.
440 // This is what we used to do, so we wouldn't skip right past
441 // tall rows, but it's not working right now.
443 new_y = bv->top_y() + bv->workHeight();
448 new_y = bv()->text->cursor.y()
449 + bv()->cursor().innerInset()->insetInInsetY()
450 + y - crit->baseline();
452 new_y = cursor.y() - crit->baseline();
458 setCursor(cur, parOffset(cpit), crit->pos(), false);
459 if (cur.y() < bv()->top_y() + bv()->workHeight())
461 bv()->updateScrollbar();
467 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
470 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
471 replaceSelection(lt);
472 if (!bv->insertInset(new_inset))
479 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
480 bool edit, bool pastesel)
482 InsetOld * inset = createInset(cmd);
483 BufferView * bv = cmd.view();
487 if (lt->selection.set()) {
488 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
491 if (bv->insertInset(inset)) {
493 inset->edit(bv, true);
494 if (gotsel && pastesel)
495 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
504 void LyXText::number()
506 LyXFont font(LyXFont::ALL_IGNORE);
507 font.setNumber(LyXFont::TOGGLE);
508 toggleAndShow(bv(), this, font);
512 bool LyXText::rtl() const
514 return cursorPar()->isRightToLeftPar(bv()->buffer()->params());
518 DispatchResult LyXText::dispatch(FuncRequest const & cmd)
520 //lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
521 lyxerr << "LyXText::dispatch: cmd: " << cmd << endl;
523 BufferView * bv = cmd.view();
525 switch (cmd.action) {
527 case LFUN_APPENDIX: {
528 ParagraphList::iterator pit = cursorPar();
529 bool start = !pit->params().startOfAppendix();
531 // ensure that we have only one start_of_appendix in this document
532 ParagraphList::iterator tmp = ownerParagraphs().begin();
533 ParagraphList::iterator end = ownerParagraphs().end();
535 for (; tmp != end; ++tmp) {
536 if (tmp->params().startOfAppendix()) {
537 recUndo(parOffset(tmp));
538 tmp->params().startOfAppendix(false);
544 recUndo(parOffset(pit));
545 pit->params().startOfAppendix(start);
547 // we can set the refreshing parameters now
549 redoParagraph(cursorPar());
550 setCursor(cursorPar(), cursor.pos());
555 case LFUN_DELETE_WORD_FORWARD:
561 case LFUN_DELETE_WORD_BACKWARD:
563 deleteWordBackward();
567 case LFUN_DELETE_LINE_FORWARD:
574 if (!selection.mark())
579 cursorRightOneWord();
584 if (!selection.mark())
587 cursorRightOneWord();
593 case LFUN_BEGINNINGBUF:
594 if (!selection.mark())
601 if (selection.mark())
608 if (!selection.set())
609 selection.cursor = cursor;
614 finishChange(bv, true);
618 if (!selection.set())
619 selection.cursor = cursor;
624 finishChange(bv, true);
628 if (!selection.set())
629 selection.cursor = cursor;
631 finishChange(bv, true);
635 if (!selection.set())
636 selection.cursor = cursor;
638 finishChange(bv, true);
641 case LFUN_UP_PARAGRAPHSEL:
642 if (!selection.set())
643 selection.cursor = cursor;
645 finishChange(bv, true);
648 case LFUN_DOWN_PARAGRAPHSEL:
649 if (!selection.set())
650 selection.cursor = cursor;
651 cursorDownParagraph();
652 finishChange(bv, true);
656 if (!selection.set())
657 selection.cursor = cursor;
659 finishChange(bv, true);
663 if (!selection.set())
664 selection.cursor = cursor;
666 finishChange(bv, true);
670 if (!selection.set())
671 selection.cursor = cursor;
673 finishChange(bv, true);
677 if (!selection.set())
678 selection.cursor = cursor;
680 finishChange(bv, true);
683 case LFUN_WORDRIGHTSEL:
687 cursorRightOneWord();
688 finishChange(bv, true);
691 case LFUN_WORDLEFTSEL:
693 cursorRightOneWord();
696 finishChange(bv, true);
700 LyXCursor cur1 = cursor;
702 getWord(cur1, cur2, lyx::WHOLE_WORD);
703 setCursor(cur1.par(), cur1.pos());
705 setCursor(cur2.par(), cur2.pos());
706 finishChange(bv, true);
712 if (!selection.mark())
716 if (cursor.pos() < cursorPar()->size()
717 && cursorPar()->isInset(cursor.pos())
718 && isHighlyEditableInset(cursorPar()->getInset(cursor.pos()))) {
719 InsetOld * tmpinset = cursorPar()->getInset(cursor.pos());
720 cmd.message(tmpinset->editMessage());
721 tmpinset->edit(bv, !is_rtl);
731 // This is soooo ugly. Isn't it possible to make
733 bool const is_rtl = rtl();
734 if (!selection.mark())
736 LyXCursor const cur = cursor;
739 if ((is_rtl || cur != cursor) && // only if really moved!
740 cursor.pos() < cursorPar()->size() &&
741 cursorPar()->isInset(cursor.pos()) &&
742 isHighlyEditableInset(cursorPar()->getInset(cursor.pos()))) {
743 InsetOld * tmpinset = cursorPar()->getInset(cursor.pos());
744 cmd.message(tmpinset->editMessage());
745 tmpinset->edit(bv, is_rtl);
755 if (!selection.mark())
762 if (!selection.mark())
768 case LFUN_UP_PARAGRAPH:
769 if (!selection.mark())
775 case LFUN_DOWN_PARAGRAPH:
776 if (!selection.mark())
778 cursorDownParagraph();
779 finishChange(bv, false);
783 if (!selection.mark())
786 finishChange(bv, false);
790 if (!selection.mark())
793 finishChange(bv, false);
797 if (!selection.mark())
800 finishChange(bv, false);
804 if (!selection.mark())
807 finishChange(bv, false);
810 case LFUN_BREAKLINE: {
811 lyx::pos_type body = cursorPar()->beginOfBody();
813 // Not allowed by LaTeX (labels or empty par)
814 if (cursor.pos() <= body)
817 replaceSelection(bv->getLyXText());
818 insertInset(new InsetNewline);
819 setCursor(cursorPar(), cursor.pos());
820 moveCursor(bv, false);
825 if (!selection.set()) {
827 selection.cursor = cursor;
828 // It is possible to make it a lot faster still
829 // just comment out the line below...
831 cutSelection(true, false);
833 moveCursor(bv, false);
834 bv->owner()->view_state_changed();
837 case LFUN_DELETE_SKIP:
838 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
839 if (!selection.set()) {
840 if (cursor.pos() == cursorPar()->size()) {
842 ParagraphParameters & params = cursorPar()->params();
843 if (cursor.pos() == 0
844 && !(params.spaceTop() == VSpace (VSpace::NONE))) {
846 VSpace(VSpace::NONE),
847 params.spaceBottom(),
850 params.labelWidthString(), 0);
855 selection.cursor = cursor;
859 selection.cursor = cursor;
862 cutSelection(true, false);
869 if (!selection.set()) {
870 if (bv->owner()->getIntl().getTransManager().backspace()) {
872 selection.cursor = cursor;
873 // It is possible to make it a lot faster still
874 // just comment out the line below...
877 cutSelection(true, false);
879 bv->owner()->view_state_changed();
884 case LFUN_BACKSPACE_SKIP:
885 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
886 if (!selection.set()) {
887 ParagraphParameters & params = cursorPar()->params();
888 if (cursor.pos() == 0 && !(params.spaceTop() == VSpace(VSpace::NONE))) {
890 VSpace(VSpace::NONE),
891 params.spaceBottom(),
894 params.labelWidthString(), 0);
896 LyXCursor cur = cursor;
898 selection.cursor = cur;
901 cutSelection(true, false);
906 case LFUN_BREAKPARAGRAPH:
907 replaceSelection(bv->getLyXText());
908 breakParagraph(bv->buffer()->paragraphs(), 0);
910 selection.cursor = cursor;
912 bv->owner()->view_state_changed();
915 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
916 replaceSelection(bv->getLyXText());
917 breakParagraph(bv->buffer()->paragraphs(), 1);
919 selection.cursor = cursor;
921 bv->owner()->view_state_changed();
924 case LFUN_BREAKPARAGRAPH_SKIP: {
925 // When at the beginning of a paragraph, remove
926 // indentation and add a "defskip" at the top.
927 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
928 LyXCursor cur = cursor;
929 replaceSelection(bv->getLyXText());
930 if (cur.pos() == 0) {
931 ParagraphParameters & params = getPar(cur)->params();
932 if (params.spaceTop() == VSpace(VSpace::NONE)) {
934 VSpace(VSpace::DEFSKIP), params.spaceBottom(),
937 params.labelWidthString(), 1);
941 breakParagraph(bv->buffer()->paragraphs(), 0);
944 selection.cursor = cur;
946 bv->owner()->view_state_changed();
950 case LFUN_PARAGRAPH_SPACING: {
951 ParagraphList::iterator pit = cursorPar();
952 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
953 float cur_value = 1.0;
954 if (cur_spacing == Spacing::Other)
955 cur_value = pit->params().spacing().getValue();
957 istringstream is(cmd.argument);
960 Spacing::Space new_spacing = cur_spacing;
961 float new_value = cur_value;
963 lyxerr << "Missing argument to `paragraph-spacing'"
965 } else if (tmp == "single") {
966 new_spacing = Spacing::Single;
967 } else if (tmp == "onehalf") {
968 new_spacing = Spacing::Onehalf;
969 } else if (tmp == "double") {
970 new_spacing = Spacing::Double;
971 } else if (tmp == "other") {
972 new_spacing = Spacing::Other;
975 lyxerr << "new_value = " << tmpval << endl;
978 } else if (tmp == "default") {
979 new_spacing = Spacing::Default;
981 lyxerr << _("Unknown spacing argument: ")
982 << cmd.argument << endl;
984 if (cur_spacing != new_spacing || cur_value != new_value) {
985 pit->params().spacing(Spacing(new_spacing, new_value));
992 case LFUN_INSET_APPLY: {
993 string const name = cmd.getArg(0);
994 InsetBase * inset = bv->owner()->getDialogs().getOpenInset(name);
996 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
999 FuncRequest fr(bv, LFUN_INSET_INSERT, cmd.argument);
1005 case LFUN_INSET_INSERT: {
1006 InsetOld * inset = createInset(cmd);
1007 if (!inset || !bv->insertInset(inset))
1012 case LFUN_INSET_SETTINGS:
1013 bv->cursor().innerInset()->showInsetDialog(bv);
1016 case LFUN_INSET_TOGGLE:
1023 case LFUN_SPACE_INSERT:
1024 if (cursorPar()->layout()->free_spacing)
1027 doInsertInset(this, cmd, false, false);
1028 moveCursor(bv, false);
1031 case LFUN_HYPHENATION:
1032 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
1035 case LFUN_LIGATURE_BREAK:
1036 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
1040 specialChar(this, bv, InsetSpecialChar::LDOTS);
1043 case LFUN_END_OF_SENTENCE:
1044 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
1047 case LFUN_MENU_SEPARATOR:
1048 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
1054 selection.cursor = cursor;
1055 cmd.message(N_("Mark off"));
1060 selection.mark(true);
1062 selection.cursor = cursor;
1063 cmd.message(N_("Mark on"));
1068 if (selection.mark()) {
1069 cmd.message(N_("Mark removed"));
1071 selection.mark(true);
1072 cmd.message(N_("Mark set"));
1074 selection.cursor = cursor;
1078 case LFUN_UPCASE_WORD:
1079 changeCase(LyXText::text_uppercase);
1083 case LFUN_LOWCASE_WORD:
1084 changeCase(LyXText::text_lowercase);
1088 case LFUN_CAPITALIZE_WORD:
1089 changeCase(LyXText::text_capitalization);
1093 case LFUN_TRANSPOSE_CHARS:
1094 recUndo(cursor.par());
1100 cmd.message(_("Paste"));
1101 replaceSelection(bv->getLyXText());
1102 #warning FIXME Check if the arg is in the domain of available selections.
1103 if (isStrUnsignedInt(cmd.argument))
1104 pasteSelection(strToUnsignedInt(cmd.argument));
1107 clearSelection(); // bug 393
1114 cutSelection(true, true);
1115 cmd.message(_("Cut"));
1121 cmd.message(_("Copy"));
1124 case LFUN_BEGINNINGBUFSEL:
1126 return DispatchResult(false);
1128 finishChange(bv, true);
1131 case LFUN_ENDBUFSEL:
1133 return DispatchResult(false);
1135 finishChange(bv, true);
1139 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1145 istringstream is(cmd.argument);
1148 lyxerr << "SETXY: Could not parse coordinates in '"
1149 << cmd.argument << std::endl;
1151 setCursorFromCoordinates(x, y);
1156 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1158 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1164 case LFUN_GETLAYOUT:
1165 cmd.message(tostr(cursorPar()->layout()));
1169 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1170 << cmd.argument << endl;
1172 // This is not the good solution to the empty argument
1173 // problem, but it will hopefully suffice for 1.2.0.
1174 // The correct solution would be to augument the
1175 // function list/array with information about what
1176 // functions needs arguments and their type.
1177 if (cmd.argument.empty()) {
1178 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1182 // Derive layout number from given argument (string)
1183 // and current buffer's textclass (number)
1184 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1185 bool hasLayout = tclass.hasLayout(cmd.argument);
1186 string layout = cmd.argument;
1188 // If the entry is obsolete, use the new one instead.
1190 string const & obs = tclass[layout]->obsoleted_by();
1196 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1201 bool change_layout = (current_layout != layout);
1203 if (!change_layout && selection.set() &&
1204 selection.start.par() != selection.end.par())
1206 ParagraphList::iterator spit = getPar(selection.start);
1207 ParagraphList::iterator epit = boost::next(getPar(selection.end));
1208 while (spit != epit) {
1209 if (spit->layout()->name() != current_layout) {
1210 change_layout = true;
1217 if (change_layout) {
1218 current_layout = layout;
1220 bv->owner()->setLayout(layout);
1227 case LFUN_PASTESELECTION: {
1228 // this was originally a bv->text->clearSelection(), i.e
1229 // the outermost LyXText!
1231 string const clip = bv->getClipboard();
1232 if (!clip.empty()) {
1233 if (cmd.argument == "paragraph")
1234 insertStringAsParagraphs(clip);
1236 insertStringAsLines(clip);
1242 case LFUN_GOTOERROR:
1243 gotoInset(InsetOld::ERROR_CODE, false);
1247 gotoInset(InsetOld::NOTE_CODE, false);
1250 case LFUN_REFERENCE_GOTO: {
1251 vector<InsetOld::Code> tmp;
1252 tmp.push_back(InsetOld::LABEL_CODE);
1253 tmp.push_back(InsetOld::REF_CODE);
1254 gotoInset(tmp, true);
1259 replaceSelection(bv->getLyXText());
1260 ParagraphList::iterator pit = cursorPar();
1261 lyx::pos_type pos = cursor.pos();
1265 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1268 c = pit->getChar(pos - 1);
1270 LyXLayout_ptr const & style = pit->layout();
1272 BufferParams const & bufparams = bv->buffer()->params();
1273 if (style->pass_thru ||
1274 pit->getFontSettings(bufparams,pos).language()->lang() == "hebrew" ||
1275 !bv->insertInset(new InsetQuotes(c, bufparams)))
1276 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1280 case LFUN_DATE_INSERT: {
1281 replaceSelection(bv->getLyXText());
1282 time_t now_time_t = time(NULL);
1283 struct tm * now_tm = localtime(&now_time_t);
1284 setlocale(LC_TIME, "");
1286 if (!cmd.argument.empty())
1289 arg = lyxrc.date_insert_format;
1291 int const datetmp_len =
1292 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1294 for (int i = 0; i < datetmp_len; i++)
1295 insertChar(datetmp[i]);
1297 selection.cursor = cursor;
1298 moveCursor(bv, false);
1302 case LFUN_MOUSE_TRIPLE:
1305 if (cmd.button() == mouse_button::button1) {
1307 selection.cursor = cursor;
1310 bv->haveSelection(selection.set());
1314 case LFUN_MOUSE_DOUBLE:
1317 if (cmd.button() == mouse_button::button1) {
1318 selectWord(lyx::WHOLE_WORD_STRICT);
1319 bv->haveSelection(selection.set());
1323 case LFUN_MOUSE_MOTION: {
1324 // Only use motion with button 1
1325 //if (ev.button() != mouse_button::button1)
1331 // Check for inset locking
1333 if (bv->innerInset()) {
1334 InsetOld * tli = bv->innerInset();
1335 LyXCursor cursor = bv->text->cursor;
1336 LyXFont font = bv->text->getFont(bv->text->cursorPar(), cursor.pos());
1337 int width = tli->width();
1338 int inset_x = font.isVisibleRightToLeft()
1339 ? cursor.x() - width : cursor.x();
1340 int start_x = inset_x + tli->scroll();
1341 FuncRequest cmd1 = cmd;
1342 cmd1.x = cmd.x - start_x;
1343 cmd1.y = cmd.y - cursor.y() + bv->top_y();
1344 tli->dispatch(cmd1);
1349 // The test for not selection possible is needed, that only motion
1350 // events are used, where the bottom press event was on
1351 // the drawing area too
1352 if (!selection_possible) {
1353 lyxerr[Debug::ACTION]
1354 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1358 RowList::iterator cursorrow = bv->text->cursorRow();
1359 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->top_y());
1361 // sorry for this but I have a strange error that the y value jumps at
1362 // a certain point. This seems like an error in my xforms library or
1363 // in some other local environment, but I would like to leave this here
1364 // for the moment until I can remove this (Jug 20020418)
1365 if (y_before < bv->text->cursor.y())
1366 lyxerr << y_before << ':'
1367 << bv->text->cursor.y() << endl;
1369 // This is to allow jumping over large insets
1370 if (cursorrow == cursorRow()) {
1371 if (cmd.y >= bv->workHeight())
1377 bv->text->setSelection();
1382 // Single-click on work area
1383 case LFUN_MOUSE_PRESS: {
1387 // ok ok, this is a hack (for xforms)
1388 // We shouldn't go further down as we really should only do the
1389 // scrolling and be done with this. Otherwise we may open some
1390 // dialogs (Jug 20020424).
1391 if (cmd.button() == mouse_button::button4) {
1392 bv->scroll(-lyxrc.wheel_jump);
1395 if (cmd.button() == mouse_button::button5) {
1396 bv->scroll(lyxrc.wheel_jump);
1400 // Middle button press pastes if we have a selection
1401 // We do this here as if the selection was inside an inset
1402 // it could get cleared on the unlocking of the inset so
1403 // we have to check this first
1404 bool paste_internally = false;
1405 if (cmd.button() == mouse_button::button2 && selection.set()) {
1406 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1407 paste_internally = true;
1410 int const screen_first = bv->top_y();
1411 selection_possible = true;
1413 // Clear the selection
1414 bv->text->clearSelection();
1416 bv->updateScrollbar();
1418 // Right click on a footnote flag opens float menu
1419 if (cmd.button() == mouse_button::button3) {
1420 selection_possible = false;
1424 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + screen_first);
1426 bv->text->selection.cursor = bv->text->cursor;
1427 bv->x_target(bv->text->cursor.x());
1429 if (bv->fitCursor())
1430 selection_possible = false;
1432 // Insert primary selection with middle mouse
1433 // if there is a local selection in the current buffer,
1435 if (cmd.button() == mouse_button::button2) {
1436 if (paste_internally)
1437 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1439 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1440 selection_possible = false;
1445 case LFUN_MOUSE_RELEASE: {
1449 // do nothing if we used the mouse wheel
1450 if (cmd.button() == mouse_button::button4
1451 || cmd.button() == mouse_button::button5)
1452 return DispatchResult(true, false);
1454 selection_possible = false;
1456 if (cmd.button() == mouse_button::button2)
1460 if (cmd.button() == mouse_button::button1)
1461 bv->haveSelection(selection.set());
1464 bv->owner()->view_state_changed();
1465 bv->owner()->updateMenubar();
1466 bv->owner()->updateToolbar();
1470 case LFUN_SELFINSERT: {
1471 if (cmd.argument.empty())
1474 // Automatically delete the currently selected
1475 // text and replace it with what is being
1476 // typed in now. Depends on lyxrc settings
1477 // "auto_region_delete", which defaults to
1480 if (lyxrc.auto_region_delete) {
1481 if (selection.set())
1482 cutSelection(false, false);
1483 bv->haveSelection(false);
1487 LyXFont const old_font = real_current_font;
1489 string::const_iterator cit = cmd.argument.begin();
1490 string::const_iterator end = cmd.argument.end();
1491 for (; cit != end; ++cit)
1492 bv->owner()->getIntl().getTransManager().
1493 TranslateAndInsert(*cit, this);
1495 selection.cursor = cursor;
1496 moveCursor(bv, false);
1498 // real_current_font.number can change so we need to
1499 // update the minibuffer
1500 if (old_font != real_current_font)
1501 bv->owner()->view_state_changed();
1502 bv->updateScrollbar();
1507 InsetCommandParams p("url");
1508 string const data = InsetCommandMailer::params2string("url", p);
1509 bv->owner()->getDialogs().show("url", data, 0);
1513 case LFUN_HTMLURL: {
1514 InsetCommandParams p("htmlurl");
1515 string const data = InsetCommandMailer::params2string("url", p);
1516 bv->owner()->getDialogs().show("url", data, 0);
1520 case LFUN_INSERT_LABEL: {
1521 InsetCommandParams p("label");
1522 string const data = InsetCommandMailer::params2string("label", p);
1523 bv->owner()->getDialogs().show("label", data, 0);
1529 case LFUN_INSET_LIST:
1530 case LFUN_INSET_THEOREM:
1531 case LFUN_INSET_CAPTION:
1533 case LFUN_INSERT_NOTE:
1534 case LFUN_INSERT_CHARSTYLE:
1535 case LFUN_INSERT_BOX:
1536 case LFUN_INSERT_BRANCH:
1537 case LFUN_INSERT_BIBITEM:
1538 case LFUN_INSET_ERT:
1539 case LFUN_INSET_FLOAT:
1540 case LFUN_INSET_FOOTNOTE:
1541 case LFUN_INSET_MARGINAL:
1542 case LFUN_INSET_MINIPAGE:
1543 case LFUN_INSET_OPTARG:
1544 case LFUN_INSET_WIDE_FLOAT:
1545 case LFUN_INSET_WRAP:
1546 case LFUN_TABULAR_INSERT:
1547 case LFUN_ENVIRONMENT_INSERT:
1548 // Open the inset, and move the current selection
1550 doInsertInset(this, cmd, true, true);
1553 case LFUN_INDEX_INSERT:
1554 // Just open the inset
1555 doInsertInset(this, cmd, true, false);
1558 case LFUN_INDEX_PRINT:
1559 case LFUN_TOC_INSERT:
1561 case LFUN_INSERT_LINE:
1562 case LFUN_INSERT_PAGEBREAK:
1564 doInsertInset(this, cmd, false, false);
1567 case LFUN_DEPTH_MIN:
1569 bv_funcs::changeDepth(bv, this, bv_funcs::DEC_DEPTH, false);
1573 case LFUN_DEPTH_PLUS:
1575 bv_funcs::changeDepth(bv, this, bv_funcs::INC_DEPTH, false);
1579 case LFUN_MATH_DELIM:
1580 case LFUN_MATH_DISPLAY:
1581 case LFUN_INSERT_MATH:
1582 case LFUN_MATH_LIMITS:
1583 case LFUN_MATH_MACRO:
1584 case LFUN_MATH_MUTATE:
1585 case LFUN_MATH_SPACE:
1586 case LFUN_MATH_IMPORT_SELECTION:
1587 case LFUN_MATH_MODE:
1588 case LFUN_MATH_NONUMBER:
1589 case LFUN_MATH_NUMBER:
1590 case LFUN_MATH_EXTERN:
1591 case LFUN_MATH_SIZE:
1597 bv->owner()->view_state_changed();
1602 bv->owner()->view_state_changed();
1607 bv->owner()->view_state_changed();
1612 bv->owner()->view_state_changed();
1617 bv->owner()->view_state_changed();
1622 bv->owner()->view_state_changed();
1626 styleReset(bv, this);
1627 bv->owner()->view_state_changed();
1630 case LFUN_UNDERLINE:
1631 underline(bv, this);
1632 bv->owner()->view_state_changed();
1635 case LFUN_FONT_SIZE:
1636 fontSize(bv, cmd.argument, this);
1637 bv->owner()->view_state_changed();
1641 lang(bv, cmd.argument, this);
1643 bv->owner()->view_state_changed();
1646 case LFUN_FREEFONT_APPLY:
1647 apply_freefont(bv, this);
1650 case LFUN_FREEFONT_UPDATE:
1651 update_and_apply_freefont(bv, this, cmd.argument);
1654 case LFUN_FINISHED_LEFT:
1655 lyxerr << "swallow LFUN_FINISHED_LEFT" << endl;
1660 case LFUN_FINISHED_RIGHT:
1661 lyxerr << "swallow LFUN_FINISHED_RIGHT" << endl;
1666 case LFUN_FINISHED_UP:
1667 lyxerr << "swallow LFUN_FINISHED_UP" << endl;
1670 case LFUN_FINISHED_DOWN:
1671 lyxerr << "swallow LFUN_FINISHED_DOWN" << endl;
1676 return DispatchResult(false);
1679 return DispatchResult(true, true);