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 "ParagraphParameters.h"
35 #include "text_funcs.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 moveCursorUpdate(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);
235 void finishChange(BufferView * bv, bool selecting = false)
238 moveCursorUpdate(bv, selecting);
239 bv->owner()->view_state_changed();
242 // check if the given co-ordinates are inside an inset at the
243 // given cursor, if one exists. If so, the inset is returned,
244 // and the co-ordinates are made relative. Otherwise, 0 is returned.
245 InsetOld * checkInset(LyXText & text,
246 LyXCursor const & cur, int & x, int & y)
248 lyx::pos_type const pos = cur.pos();
249 ParagraphList::iterator par = text.getPar(cur);
251 if (pos >= par->size() || !par->isInset(pos))
254 InsetOld /*const*/ * inset = par->getInset(pos);
256 if (!isEditableInset(inset))
259 // get inset dimensions
260 BOOST_ASSERT(par->getInset(pos));
262 LyXFont const & font = text.getFont(par, pos);
264 int const width = inset->width();
265 int const inset_x = font.isVisibleRightToLeft()
266 ? (cur.x() - width) : cur.x();
269 inset_x + inset->scroll(),
271 cur.y() - inset->ascent(),
272 cur.y() + inset->descent()
275 if (!b.contains(x, y)) {
276 lyxerr[Debug::GUI] << "Missed inset at x,y "
278 << " box " << b << endl;
282 text.setCursor(cur.par(), pos, true);
285 // The origin of an inset is on the baseline
286 y -= text.cursor.y();
296 string const freefont2string()
299 if (font2string(freefont, toggleall, data))
308 InsetOld * LyXText::checkInsetHit(int & x, int & y)
310 int y_tmp = y + bv_owner->top_y();
313 setCursorFromCoordinates(cur, x, y_tmp);
315 InsetOld * inset = checkInset(*this, cur, x, y_tmp);
321 // look at previous position
326 setCursor(cur, cur.par(), cur.pos() - 1, true);
328 inset = checkInset(*this, cur, x, y_tmp);
335 bool LyXText::gotoNextInset(vector<InsetOld::Code> const & codes,
336 string const & contents)
338 ParagraphList::iterator end = ownerParagraphs().end();
339 ParagraphList::iterator pit = cursorPar();
340 pos_type pos = cursor.pos();
344 if (pos + 1 < pit->size()) {
351 } while (pit != end &&
352 !(pit->isInset(pos) &&
353 (inset = pit->getInset(pos)) != 0 &&
354 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
356 static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
362 setCursor(parOffset(pit), pos, false);
367 void LyXText::gotoInset(vector<InsetOld::Code> const & codes,
373 if (same_content && cursor.pos() < cursorPar()->size()
374 && cursorPar()->isInset(cursor.pos())) {
375 InsetOld const * inset = cursorPar()->getInset(cursor.pos());
376 if (find(codes.begin(), codes.end(), inset->lyxCode())
378 contents = static_cast<InsetCommand const *>(inset)->getContents();
381 if (!gotoNextInset(codes, contents)) {
382 if (cursor.pos() || cursorPar() != ownerParagraphs().begin()) {
383 LyXCursor tmp = cursor;
386 if (!gotoNextInset(codes, contents)) {
388 bv()->owner()->message(_("No more insets"));
391 bv()->owner()->message(_("No more insets"));
395 selection.cursor = cursor;
399 void LyXText::gotoInset(InsetOld::Code code, bool same_content)
401 gotoInset(vector<InsetOld::Code>(1, code), same_content);
405 void LyXText::cursorPrevious()
407 int y = bv()->top_y();
409 ParagraphList::iterator cpit = cursorPar();
410 RowList::iterator crit = cpit->getRow(cursor.pos());
412 if (isFirstRow(cpit, *crit)) {
414 bv()->updateScrollbar();
418 setCursorFromCoordinates(bv()->x_target(), y);
421 if (crit == bv()->text->cursorRow()) {
422 // we have a row which is taller than the workarea. The
423 // simplest solution is to move to the previous row instead.
428 int new_y = + crit->height() - bv()->workHeight() + 1;
431 new_y += bv()->text->cursor.y()
432 + bv()->cursor().innerInset()->insetInInsetY()
435 new_y += cursor.y() - crit->baseline();
438 previousRow(cpit, crit);
440 setCursor(cur, parOffset(cpit), crit->pos(), false);
441 if (cur.y() > bv()->top_y())
443 bv()->updateScrollbar();
447 void LyXText::cursorNext()
449 int topy = bv()->top_y();
451 ParagraphList::iterator cpit = cursorPar();
452 RowList::iterator crit = cpit->getRow(cursor.pos());
454 if (isLastRow(cpit, *crit)) {
455 int y = cursor.y() - crit->baseline() + crit->height();
456 if (y > topy + bv()->workHeight())
457 bv()->updateScrollbar();
461 int y = topy + bv()->workHeight();
462 if (inset_owner && !topy) {
463 y += - bv()->text->cursor.y()
465 - bv()->cursor().innerInset()->insetInInsetY();
468 ParagraphList::iterator dummypit;
469 Row const & row = *getRowNearY(y, dummypit);
470 y = dummypit->y + row.y_offset();
472 setCursorFromCoordinates(bv()->x_target(), y);
473 // + bv->workHeight());
477 if (crit == bv()->text->cursorRow()) {
478 // we have a row which is taller than the workarea. The
479 // simplest solution is to move to the next row instead.
482 // This is what we used to do, so we wouldn't skip right past
483 // tall rows, but it's not working right now.
485 new_y = bv->top_y() + bv->workHeight();
490 new_y = bv()->text->cursor.y()
491 + bv()->cursor().innerInset()->insetInInsetY()
492 + y - crit->baseline();
494 new_y = cursor.y() - crit->baseline();
500 setCursor(cur, parOffset(cpit), crit->pos(), false);
501 if (cur.y() < bv_owner->top_y() + bv()->workHeight())
503 bv()->updateScrollbar();
509 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
512 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
513 replaceSelection(lt);
514 if (!bv->insertInset(new_inset))
517 bv->updateInset(new_inset);
521 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
522 bool edit, bool pastesel)
524 InsetOld * inset = createInset(cmd);
525 BufferView * bv = cmd.view();
529 if (lt->selection.set()) {
530 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
533 if (bv->insertInset(inset)) {
535 inset->edit(bv, true);
536 if (gotsel && pastesel)
537 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
547 void LyXText::number()
549 LyXFont font(LyXFont::ALL_IGNORE);
550 font.setNumber(LyXFont::TOGGLE);
551 toggleAndShow(bv(), this, font);
555 bool LyXText::rtl() const
557 return cursorPar()->isRightToLeftPar(bv()->buffer()->params());
561 DispatchResult LyXText::dispatch(FuncRequest const & cmd)
563 //lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
564 lyxerr << "LyXText::dispatch: cmd: " << cmd << endl;
566 BufferView * bv = cmd.view();
568 switch (cmd.action) {
570 case LFUN_APPENDIX: {
571 ParagraphList::iterator pit = cursorPar();
572 bool start = !pit->params().startOfAppendix();
574 // ensure that we have only one start_of_appendix in this document
575 ParagraphList::iterator tmp = ownerParagraphs().begin();
576 ParagraphList::iterator end = ownerParagraphs().end();
578 for (; tmp != end; ++tmp) {
579 if (tmp->params().startOfAppendix()) {
580 recUndo(parOffset(tmp));
581 tmp->params().startOfAppendix(false);
587 recUndo(parOffset(pit));
588 pit->params().startOfAppendix(start);
590 // we can set the refreshing parameters now
592 redoParagraph(cursorPar());
593 setCursor(cursorPar(), cursor.pos());
598 case LFUN_DELETE_WORD_FORWARD:
604 case LFUN_DELETE_WORD_BACKWARD:
606 deleteWordBackward();
610 case LFUN_DELETE_LINE_FORWARD:
617 if (!selection.mark())
622 cursorRightOneWord();
627 if (!selection.mark())
630 cursorRightOneWord();
636 case LFUN_BEGINNINGBUF:
637 if (!selection.mark())
644 if (selection.mark())
651 if (!selection.set())
652 selection.cursor = cursor;
657 finishChange(bv, true);
661 if (!selection.set())
662 selection.cursor = cursor;
667 finishChange(bv, true);
671 if (!selection.set())
672 selection.cursor = cursor;
674 finishChange(bv, true);
678 if (!selection.set())
679 selection.cursor = cursor;
681 finishChange(bv, true);
684 case LFUN_UP_PARAGRAPHSEL:
685 if (!selection.set())
686 selection.cursor = cursor;
688 finishChange(bv, true);
691 case LFUN_DOWN_PARAGRAPHSEL:
692 if (!selection.set())
693 selection.cursor = cursor;
694 cursorDownParagraph();
695 finishChange(bv, true);
699 if (!selection.set())
700 selection.cursor = cursor;
702 finishChange(bv, true);
706 if (!selection.set())
707 selection.cursor = cursor;
709 finishChange(bv, true);
713 if (!selection.set())
714 selection.cursor = cursor;
716 finishChange(bv, true);
720 if (!selection.set())
721 selection.cursor = cursor;
723 finishChange(bv, true);
726 case LFUN_WORDRIGHTSEL:
730 cursorRightOneWord();
731 finishChange(bv, true);
734 case LFUN_WORDLEFTSEL:
736 cursorRightOneWord();
739 finishChange(bv, true);
743 LyXCursor cur1 = cursor;
745 ::getWord(*this, cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs());
746 setCursor(cur1.par(), cur1.pos());
748 setCursor(cur2.par(), cur2.pos());
749 finishChange(bv, true);
755 if (!selection.mark())
759 if (cursor.pos() < cursorPar()->size()
760 && cursorPar()->isInset(cursor.pos())
761 && isHighlyEditableInset(cursorPar()->getInset(cursor.pos()))) {
762 InsetOld * tmpinset = cursorPar()->getInset(cursor.pos());
763 cmd.message(tmpinset->editMessage());
764 tmpinset->edit(bv, !is_rtl);
774 // This is soooo ugly. Isn't it possible to make
776 bool const is_rtl = rtl();
777 if (!selection.mark())
779 LyXCursor const cur = cursor;
782 if ((is_rtl || cur != cursor) && // only if really moved!
783 cursor.pos() < cursorPar()->size() &&
784 cursorPar()->isInset(cursor.pos()) &&
785 isHighlyEditableInset(cursorPar()->getInset(cursor.pos()))) {
786 InsetOld * tmpinset = cursorPar()->getInset(cursor.pos());
787 cmd.message(tmpinset->editMessage());
788 tmpinset->edit(bv, is_rtl);
798 if (!selection.mark())
805 if (!selection.mark())
811 case LFUN_UP_PARAGRAPH:
812 if (!selection.mark())
818 case LFUN_DOWN_PARAGRAPH:
819 if (!selection.mark())
821 cursorDownParagraph();
822 finishChange(bv, false);
826 if (!selection.mark())
829 finishChange(bv, false);
833 if (!selection.mark())
836 finishChange(bv, false);
840 if (!selection.mark())
843 finishChange(bv, false);
847 if (!selection.mark())
850 finishChange(bv, false);
853 case LFUN_BREAKLINE: {
854 lyx::pos_type body = cursorPar()->beginningOfBody();
856 // Not allowed by LaTeX (labels or empty par)
857 if (cursor.pos() <= body)
860 replaceSelection(bv->getLyXText());
861 insertInset(new InsetNewline);
862 setCursor(cursorPar(), cursor.pos());
863 moveCursorUpdate(bv, false);
868 if (!selection.set()) {
870 selection.cursor = cursor;
871 // It is possible to make it a lot faster still
872 // just comment out the line below...
874 cutSelection(true, false);
876 moveCursorUpdate(bv, false);
877 bv->owner()->view_state_changed();
880 case LFUN_DELETE_SKIP:
881 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
882 if (!selection.set()) {
883 if (cursor.pos() == cursorPar()->size()) {
885 ParagraphParameters & params = cursorPar()->params();
886 if (cursor.pos() == 0
887 && !(params.spaceTop() == VSpace (VSpace::NONE))) {
889 VSpace(VSpace::NONE),
890 params.spaceBottom(),
893 params.labelWidthString(), 0);
898 selection.cursor = cursor;
902 selection.cursor = cursor;
905 cutSelection(true, false);
912 if (!selection.set()) {
913 if (bv->owner()->getIntl().getTransManager().backspace()) {
915 selection.cursor = cursor;
916 // It is possible to make it a lot faster still
917 // just comment out the line below...
920 cutSelection(true, false);
922 bv->owner()->view_state_changed();
927 case LFUN_BACKSPACE_SKIP:
928 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
929 if (!selection.set()) {
930 ParagraphParameters & params = cursorPar()->params();
931 if (cursor.pos() == 0 && !(params.spaceTop() == VSpace(VSpace::NONE))) {
933 VSpace(VSpace::NONE),
934 params.spaceBottom(),
937 params.labelWidthString(), 0);
939 LyXCursor cur = cursor;
941 selection.cursor = cur;
944 cutSelection(true, false);
949 case LFUN_BREAKPARAGRAPH:
950 replaceSelection(bv->getLyXText());
951 breakParagraph(bv->buffer()->paragraphs(), 0);
953 selection.cursor = cursor;
955 bv->owner()->view_state_changed();
958 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
959 replaceSelection(bv->getLyXText());
960 breakParagraph(bv->buffer()->paragraphs(), 1);
962 selection.cursor = cursor;
964 bv->owner()->view_state_changed();
967 case LFUN_BREAKPARAGRAPH_SKIP: {
968 // When at the beginning of a paragraph, remove
969 // indentation and add a "defskip" at the top.
970 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
971 LyXCursor cur = cursor;
972 replaceSelection(bv->getLyXText());
973 if (cur.pos() == 0) {
974 ParagraphParameters & params = getPar(cur)->params();
975 if (params.spaceTop() == VSpace(VSpace::NONE)) {
977 VSpace(VSpace::DEFSKIP), params.spaceBottom(),
980 params.labelWidthString(), 1);
984 breakParagraph(bv->buffer()->paragraphs(), 0);
987 selection.cursor = cur;
989 bv->owner()->view_state_changed();
993 case LFUN_PARAGRAPH_SPACING: {
994 ParagraphList::iterator pit = cursorPar();
995 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
996 float cur_value = 1.0;
997 if (cur_spacing == Spacing::Other)
998 cur_value = pit->params().spacing().getValue();
1000 istringstream is(cmd.argument);
1003 Spacing::Space new_spacing = cur_spacing;
1004 float new_value = cur_value;
1006 lyxerr << "Missing argument to `paragraph-spacing'"
1008 } else if (tmp == "single") {
1009 new_spacing = Spacing::Single;
1010 } else if (tmp == "onehalf") {
1011 new_spacing = Spacing::Onehalf;
1012 } else if (tmp == "double") {
1013 new_spacing = Spacing::Double;
1014 } else if (tmp == "other") {
1015 new_spacing = Spacing::Other;
1018 lyxerr << "new_value = " << tmpval << endl;
1021 } else if (tmp == "default") {
1022 new_spacing = Spacing::Default;
1024 lyxerr << _("Unknown spacing argument: ")
1025 << cmd.argument << endl;
1027 if (cur_spacing != new_spacing || cur_value != new_value) {
1028 pit->params().spacing(Spacing(new_spacing, new_value));
1035 case LFUN_INSET_SETTINGS:
1036 bv->cursor().innerInset()->showInsetDialog(bv);
1039 case LFUN_INSET_TOGGLE:
1046 case LFUN_SPACE_INSERT:
1047 if (cursorPar()->layout()->free_spacing)
1050 doInsertInset(this, cmd, false, false);
1051 moveCursorUpdate(bv, false);
1054 case LFUN_HYPHENATION:
1055 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
1058 case LFUN_LIGATURE_BREAK:
1059 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
1063 specialChar(this, bv, InsetSpecialChar::LDOTS);
1066 case LFUN_END_OF_SENTENCE:
1067 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
1070 case LFUN_MENU_SEPARATOR:
1071 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
1077 selection.cursor = cursor;
1078 cmd.message(N_("Mark off"));
1083 selection.mark(true);
1085 selection.cursor = cursor;
1086 cmd.message(N_("Mark on"));
1091 if (selection.mark()) {
1092 cmd.message(N_("Mark removed"));
1094 selection.mark(true);
1095 cmd.message(N_("Mark set"));
1097 selection.cursor = cursor;
1101 case LFUN_UPCASE_WORD:
1102 changeCase(LyXText::text_uppercase);
1106 case LFUN_LOWCASE_WORD:
1107 changeCase(LyXText::text_lowercase);
1111 case LFUN_CAPITALIZE_WORD:
1112 changeCase(LyXText::text_capitalization);
1116 case LFUN_TRANSPOSE_CHARS:
1117 recUndo(cursor.par());
1123 cmd.message(_("Paste"));
1124 replaceSelection(bv->getLyXText());
1125 #warning FIXME Check if the arg is in the domain of available selections.
1126 if (isStrUnsignedInt(cmd.argument))
1127 pasteSelection(strToUnsignedInt(cmd.argument));
1130 clearSelection(); // bug 393
1137 cutSelection(true, true);
1138 cmd.message(_("Cut"));
1144 cmd.message(_("Copy"));
1147 case LFUN_BEGINNINGBUFSEL:
1149 return DispatchResult(false);
1151 finishChange(bv, true);
1154 case LFUN_ENDBUFSEL:
1156 return DispatchResult(false);
1158 finishChange(bv, true);
1162 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1168 istringstream is(cmd.argument);
1171 lyxerr << "SETXY: Could not parse coordinates in '"
1172 << cmd.argument << std::endl;
1174 setCursorFromCoordinates(x, y);
1179 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1181 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1187 case LFUN_GETLAYOUT:
1188 cmd.message(tostr(cursorPar()->layout()));
1192 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1193 << cmd.argument << endl;
1195 // This is not the good solution to the empty argument
1196 // problem, but it will hopefully suffice for 1.2.0.
1197 // The correct solution would be to augument the
1198 // function list/array with information about what
1199 // functions needs arguments and their type.
1200 if (cmd.argument.empty()) {
1201 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1205 // Derive layout number from given argument (string)
1206 // and current buffer's textclass (number)
1207 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1208 bool hasLayout = tclass.hasLayout(cmd.argument);
1209 string layout = cmd.argument;
1211 // If the entry is obsolete, use the new one instead.
1213 string const & obs = tclass[layout]->obsoleted_by();
1219 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1224 bool change_layout = (current_layout != layout);
1226 if (!change_layout && selection.set() &&
1227 selection.start.par() != selection.end.par())
1229 ParagraphList::iterator spit = getPar(selection.start);
1230 ParagraphList::iterator epit = boost::next(getPar(selection.end));
1231 while (spit != epit) {
1232 if (spit->layout()->name() != current_layout) {
1233 change_layout = true;
1240 if (change_layout) {
1241 current_layout = layout;
1243 bv->owner()->setLayout(layout);
1250 case LFUN_PASTESELECTION: {
1251 // this was originally a bv->text->clearSelection(), i.e
1252 // the outermost LyXText!
1254 string const clip = bv->getClipboard();
1255 if (!clip.empty()) {
1256 if (cmd.argument == "paragraph")
1257 insertStringAsParagraphs(clip);
1259 insertStringAsLines(clip);
1265 case LFUN_GOTOERROR:
1266 gotoInset(InsetOld::ERROR_CODE, false);
1270 gotoInset(InsetOld::NOTE_CODE, false);
1273 case LFUN_REFERENCE_GOTO:
1275 vector<InsetOld::Code> tmp;
1276 tmp.push_back(InsetOld::LABEL_CODE);
1277 tmp.push_back(InsetOld::REF_CODE);
1278 gotoInset(tmp, true);
1283 replaceSelection(bv->getLyXText());
1284 ParagraphList::iterator pit = cursorPar();
1285 lyx::pos_type pos = cursor.pos();
1289 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1292 c = pit->getChar(pos - 1);
1294 LyXLayout_ptr const & style = pit->layout();
1296 BufferParams const & bufparams = bv->buffer()->params();
1297 if (style->pass_thru ||
1298 pit->getFontSettings(bufparams,pos).language()->lang() == "hebrew" ||
1299 !bv->insertInset(new InsetQuotes(c, bufparams)))
1300 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1304 case LFUN_DATE_INSERT: {
1305 replaceSelection(bv->getLyXText());
1306 time_t now_time_t = time(NULL);
1307 struct tm * now_tm = localtime(&now_time_t);
1308 setlocale(LC_TIME, "");
1310 if (!cmd.argument.empty())
1313 arg = lyxrc.date_insert_format;
1315 int const datetmp_len =
1316 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1318 for (int i = 0; i < datetmp_len; i++)
1319 insertChar(datetmp[i]);
1321 selection.cursor = cursor;
1322 moveCursorUpdate(bv, false);
1326 case LFUN_MOUSE_TRIPLE:
1329 if (cmd.button() == mouse_button::button1) {
1331 selection.cursor = cursor;
1335 bv->haveSelection(selection.set());
1339 case LFUN_MOUSE_DOUBLE:
1342 if (cmd.button() == mouse_button::button1) {
1343 selectWord(lyx::WHOLE_WORD_STRICT);
1345 bv->haveSelection(selection.set());
1349 case LFUN_MOUSE_MOTION: {
1350 // Only use motion with button 1
1351 //if (ev.button() != mouse_button::button1)
1357 // Check for inset locking
1359 if (bv->innerInset()) {
1360 InsetOld * tli = bv->innerInset();
1361 LyXCursor cursor = bv->text->cursor;
1362 LyXFont font = bv->text->getFont(bv->text->cursorPar(), cursor.pos());
1363 int width = tli->width();
1364 int inset_x = font.isVisibleRightToLeft()
1365 ? cursor.x() - width : cursor.x();
1366 int start_x = inset_x + tli->scroll();
1367 FuncRequest cmd1 = cmd;
1368 cmd1.x = cmd.x - start_x;
1369 cmd1.y = cmd.y - cursor.y() + bv->top_y();
1370 tli->dispatch(cmd1);
1375 // The test for not selection possible is needed, that only motion
1376 // events are used, where the bottom press event was on
1377 // the drawing area too
1378 if (!selection_possible) {
1379 lyxerr[Debug::ACTION]
1380 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1384 RowList::iterator cursorrow = bv->text->cursorRow();
1385 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->top_y());
1387 // sorry for this but I have a strange error that the y value jumps at
1388 // a certain point. This seems like an error in my xforms library or
1389 // in some other local environment, but I would like to leave this here
1390 // for the moment until I can remove this (Jug 20020418)
1391 if (y_before < bv->text->cursor.y())
1392 lyxerr << y_before << ':'
1393 << bv->text->cursor.y() << endl;
1395 // This is to allow jumping over large insets
1396 if (cursorrow == bv->text->cursorRow()) {
1397 if (cmd.y >= bv->workHeight())
1398 bv->text->cursorDown(false);
1400 bv->text->cursorUp(false);
1403 bv->text->setSelection();
1408 // Single-click on work area
1409 case LFUN_MOUSE_PRESS: {
1413 // ok ok, this is a hack (for xforms)
1414 // We shouldn't go further down as we really should only do the
1415 // scrolling and be done with this. Otherwise we may open some
1416 // dialogs (Jug 20020424).
1417 if (cmd.button() == mouse_button::button4) {
1418 bv->scroll(-lyxrc.wheel_jump);
1421 if (cmd.button() == mouse_button::button5) {
1422 bv->scroll(lyxrc.wheel_jump);
1426 // Middle button press pastes if we have a selection
1427 // We do this here as if the selection was inside an inset
1428 // it could get cleared on the unlocking of the inset so
1429 // we have to check this first
1430 bool paste_internally = false;
1431 if (cmd.button() == mouse_button::button2 && selection.set()) {
1432 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1433 paste_internally = true;
1436 int const screen_first = bv->top_y();
1437 selection_possible = true;
1439 // Clear the selection
1440 bv->text->clearSelection();
1442 bv->updateScrollbar();
1444 // Right click on a footnote flag opens float menu
1445 if (cmd.button() == mouse_button::button3) {
1446 selection_possible = false;
1450 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + screen_first);
1452 bv->text->selection.cursor = bv->text->cursor;
1453 bv->x_target(bv->text->cursor.x());
1455 if (bv->fitCursor())
1456 selection_possible = false;
1458 // Insert primary selection with middle mouse
1459 // if there is a local selection in the current buffer,
1461 if (cmd.button() == mouse_button::button2) {
1462 if (paste_internally)
1463 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1465 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1466 selection_possible = false;
1471 case LFUN_MOUSE_RELEASE: {
1472 // do nothing if we used the mouse wheel
1476 if (cmd.button() == mouse_button::button4
1477 || cmd.button() == mouse_button::button5)
1480 selection_possible = false;
1482 if (cmd.button() == mouse_button::button2)
1486 if (cmd.button() == mouse_button::button1)
1487 bv->haveSelection(selection.set());
1490 bv->owner()->view_state_changed();
1491 bv->owner()->updateMenubar();
1492 bv->owner()->updateToolbar();
1496 case LFUN_SELFINSERT: {
1497 if (cmd.argument.empty())
1500 // Automatically delete the currently selected
1501 // text and replace it with what is being
1502 // typed in now. Depends on lyxrc settings
1503 // "auto_region_delete", which defaults to
1506 if (lyxrc.auto_region_delete) {
1507 if (selection.set())
1508 cutSelection(false, false);
1509 bv->haveSelection(false);
1513 LyXFont const old_font = real_current_font;
1515 string::const_iterator cit = cmd.argument.begin();
1516 string::const_iterator end = cmd.argument.end();
1517 for (; cit != end; ++cit)
1518 bv->owner()->getIntl().getTransManager().
1519 TranslateAndInsert(*cit, this);
1521 selection.cursor = cursor;
1522 moveCursorUpdate(bv, false);
1524 // real_current_font.number can change so we need to
1525 // update the minibuffer
1526 if (old_font != real_current_font)
1527 bv->owner()->view_state_changed();
1528 bv->updateScrollbar();
1532 case LFUN_HTMLURL: {
1533 InsetCommandParams p("htmlurl");
1534 string const data = InsetCommandMailer::params2string("url", p);
1535 bv->owner()->getDialogs().show("url", data, 0);
1540 InsetCommandParams p("url");
1541 string const data = InsetCommandMailer::params2string("url", p);
1542 bv->owner()->getDialogs().show("url", data, 0);
1548 case LFUN_INSET_LIST:
1549 case LFUN_INSET_THEOREM:
1550 case LFUN_INSET_CAPTION:
1552 case LFUN_INSERT_NOTE:
1553 case LFUN_INSERT_BOX:
1554 case LFUN_INSERT_BRANCH:
1555 case LFUN_INSERT_BIBITEM:
1556 case LFUN_INSET_ERT:
1557 case LFUN_INSET_FLOAT:
1558 case LFUN_INSET_FOOTNOTE:
1559 case LFUN_INSET_MARGINAL:
1560 case LFUN_INSET_MINIPAGE:
1561 case LFUN_INSET_OPTARG:
1562 case LFUN_INSET_WIDE_FLOAT:
1563 case LFUN_INSET_WRAP:
1564 case LFUN_TABULAR_INSERT:
1565 case LFUN_ENVIRONMENT_INSERT:
1566 // Open the inset, and move the current selection
1568 doInsertInset(this, cmd, true, true);
1571 case LFUN_INDEX_INSERT:
1572 // Just open the inset
1573 doInsertInset(this, cmd, true, false);
1576 case LFUN_INDEX_PRINT:
1577 case LFUN_TOC_INSERT:
1579 case LFUN_INSERT_LINE:
1580 case LFUN_INSERT_PAGEBREAK:
1582 doInsertInset(this, cmd, false, false);
1585 case LFUN_DEPTH_MIN:
1587 bv_funcs::changeDepth(bv, this, bv_funcs::DEC_DEPTH, false);
1591 case LFUN_DEPTH_PLUS:
1593 bv_funcs::changeDepth(bv, this, bv_funcs::INC_DEPTH, false);
1597 case LFUN_MATH_DELIM:
1598 case LFUN_MATH_DISPLAY:
1599 case LFUN_INSERT_MATH:
1600 case LFUN_MATH_LIMITS:
1601 case LFUN_MATH_MACRO:
1602 case LFUN_MATH_MUTATE:
1603 case LFUN_MATH_SPACE:
1604 case LFUN_MATH_IMPORT_SELECTION:
1605 case LFUN_MATH_MODE:
1606 case LFUN_MATH_NONUMBER:
1607 case LFUN_MATH_NUMBER:
1608 case LFUN_MATH_EXTERN:
1609 case LFUN_MATH_SIZE:
1615 bv->owner()->view_state_changed();
1620 bv->owner()->view_state_changed();
1625 bv->owner()->view_state_changed();
1630 bv->owner()->view_state_changed();
1635 bv->owner()->view_state_changed();
1640 bv->owner()->view_state_changed();
1644 styleReset(bv, this);
1645 bv->owner()->view_state_changed();
1648 case LFUN_UNDERLINE:
1649 underline(bv, this);
1650 bv->owner()->view_state_changed();
1653 case LFUN_FONT_SIZE:
1654 fontSize(bv, cmd.argument, this);
1655 bv->owner()->view_state_changed();
1659 lang(bv, cmd.argument, this);
1661 bv->owner()->view_state_changed();
1664 case LFUN_FREEFONT_APPLY:
1665 apply_freefont(bv, this);
1668 case LFUN_FREEFONT_UPDATE:
1669 update_and_apply_freefont(bv, this, cmd.argument);
1672 case LFUN_FINISHED_LEFT:
1673 lyxerr << "swallow LFUN_FINISHED_LEFT" << endl;
1678 case LFUN_FINISHED_RIGHT:
1679 lyxerr << "swallow LFUN_FINISHED_RIGHT" << endl;
1684 case LFUN_FINISHED_UP:
1685 lyxerr << "swallow LFUN_FINISHED_UP" << endl;
1688 case LFUN_FINISHED_DOWN:
1689 lyxerr << "swallow LFUN_FINISHED_DOWN" << endl;
1694 return DispatchResult(false);
1697 return DispatchResult(true, true);