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 "funcrequest.h"
31 #include "paragraph.h"
32 #include "ParagraphParameters.h"
33 #include "text_funcs.h"
37 #include "frontends/Dialogs.h"
38 #include "frontends/LyXView.h"
40 #include "insets/insetcommand.h"
41 #include "insets/insetnewline.h"
42 #include "insets/insetspecialchar.h"
43 #include "insets/insettext.h"
45 #include "support/lstrings.h"
46 #include "support/tostr.h"
48 #include "support/std_sstream.h"
51 using bv_funcs::replaceSelection;
55 using lyx::support::isStrUnsignedInt;
56 using lyx::support::strToUnsignedInt;
61 using std::istringstream;
65 extern string current_layout;
66 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
68 // the selection possible is needed, that only motion events are
69 // used, where the bottom press event was on the drawing area too
70 bool selection_possible = false;
75 void moveCursorUpdate(BufferView * bv, bool selecting)
77 LyXText * lt = bv->getLyXText();
79 //if (!lt->selection.set())
80 // lt->selection.cursor = lt->cursor;
82 if (selecting || lt->selection.mark())
85 if (!lt->selection.set())
86 bv->haveSelection(false);
93 void finishChange(BufferView * bv, bool selecting = false)
96 moveCursorUpdate(bv, selecting);
97 bv->owner()->view_state_changed();
100 // check if the given co-ordinates are inside an inset at the
101 // given cursor, if one exists. If so, the inset is returned,
102 // and the co-ordinates are made relative. Otherwise, 0 is returned.
103 InsetOld * checkInset(LyXText & text,
104 LyXCursor const & cur, int & x, int & y)
106 lyx::pos_type const pos = cur.pos();
107 ParagraphList::iterator par = text.getPar(cur);
109 if (pos >= par->size() || !par->isInset(pos))
112 InsetOld /*const*/ * inset = par->getInset(pos);
114 if (!isEditableInset(inset))
117 // get inset dimensions
118 BOOST_ASSERT(par->getInset(pos));
120 LyXFont const & font = text.getFont(par, pos);
122 int const width = inset->width();
123 int const inset_x = font.isVisibleRightToLeft()
124 ? (cur.x() - width) : cur.x();
127 inset_x + inset->scroll(),
129 cur.y() - inset->ascent(),
130 cur.y() + inset->descent()
133 if (!b.contains(x, y)) {
134 lyxerr[Debug::GUI] << "Missed inset at x,y "
136 << " box " << b << endl;
140 text.setCursor(cur.par(), pos, true);
143 // The origin of an inset is on the baseline
144 y -= text.cursor.y();
152 InsetOld * LyXText::checkInsetHit(int & x, int & y)
154 int y_tmp = y + bv_owner->top_y();
157 setCursorFromCoordinates(cur, x, y_tmp);
159 InsetOld * inset = checkInset(*this, cur, x, y_tmp);
165 // look at previous position
170 setCursor(cur, cur.par(), cur.pos() - 1, true);
172 inset = checkInset(*this, cur, x, y_tmp);
179 bool LyXText::gotoNextInset(vector<InsetOld::Code> const & codes,
180 string const & contents)
182 ParagraphList::iterator end = ownerParagraphs().end();
183 ParagraphList::iterator pit = cursorPar();
184 pos_type pos = cursor.pos();
188 if (pos + 1 < pit->size()) {
195 } while (pit != end &&
196 !(pit->isInset(pos) &&
197 (inset = pit->getInset(pos)) != 0 &&
198 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
200 static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
206 setCursor(parOffset(pit), pos, false);
211 void LyXText::gotoInset(vector<InsetOld::Code> const & codes,
214 bv()->beforeChange(this);
217 if (same_content && cursor.pos() < cursorPar()->size()
218 && cursorPar()->isInset(cursor.pos())) {
219 InsetOld const * inset = cursorPar()->getInset(cursor.pos());
220 if (find(codes.begin(), codes.end(), inset->lyxCode())
222 contents = static_cast<InsetCommand const *>(inset)->getContents();
225 if (!gotoNextInset(codes, contents)) {
226 if (cursor.pos() || cursorPar() != ownerParagraphs().begin()) {
227 LyXCursor tmp = cursor;
230 if (!gotoNextInset(codes, contents)) {
232 bv()->owner()->message(_("No more insets"));
235 bv()->owner()->message(_("No more insets"));
239 selection.cursor = cursor;
243 void LyXText::gotoInset(InsetOld::Code code, bool same_content)
245 gotoInset(vector<InsetOld::Code>(1, code), same_content);
249 void LyXText::cursorPrevious()
251 int y = bv_owner->top_y();
253 RowList::iterator rit = cursorRow();
255 if (rit == firstRow()) {
257 bv()->updateScrollbar();
261 setCursorFromCoordinates(cursor.x_fix(), y);
265 if (rit == bv()->text->cursorRow()) {
266 // we have a row which is taller than the workarea. The
267 // simplest solution is to move to the previous row instead.
270 // This is what we used to do, so we wouldn't skip right past
271 // tall rows, but it's not working right now.
274 new_y = bv()->text->cursor.y()
275 + bv()->theLockingInset()->insetInInsetY() + y
277 - bv()->workHeight() + 1;
282 - bv()->workHeight() + 1;
287 ParagraphList::iterator pit = cursorPar();
288 previousRow(pit, rit);
289 setCursor(cur, parOffset(pit), rit->pos(), false);
290 if (cur.y() > bv_owner->top_y())
292 bv()->updateScrollbar();
296 void LyXText::cursorNext()
298 int topy = bv_owner->top_y();
300 RowList::iterator rit = cursorRow();
301 if (rit == lastRow()) {
302 int y = cursor.y() - rit->baseline() + cursorRow()->height();
303 if (y > topy + bv()->workHeight())
304 bv_owner->updateScrollbar();
308 int y = topy + bv_owner->workHeight();
309 if (inset_owner && !topy) {
310 y -= (bv_owner->text->cursor.y()
312 + bv_owner->theLockingInset()->insetInInsetY());
315 ParagraphList::iterator dummypit;
316 Row const & rr = *getRowNearY(y, dummypit);
317 y = dummypit->y + rr.y_offset();
319 setCursorFromCoordinates(cursor.x_fix(), y);
320 // + bv->workHeight());
324 if (rit == bv_owner->text->cursorRow()) {
325 // we have a row which is taller than the workarea. The
326 // simplest solution is to move to the next row instead.
329 // This is what we used to do, so we wouldn't skip right past
330 // tall rows, but it's not working right now.
332 new_y = bv->top_y() + bv->workHeight();
336 new_y = bv()->text->cursor.y()
337 + bv()->theLockingInset()->insetInInsetY()
338 + y - rit->baseline();
340 new_y = cursor.y() - cursorRow()->baseline();
344 ParagraphList::iterator pit = cursorPar();
347 setCursor(cur, parOffset(pit), rit->pos(), false);
348 if (cur.y() < bv_owner->top_y() + bv()->workHeight())
350 bv()->updateScrollbar();
356 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
359 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
360 replaceSelection(lt);
361 if (!bv->insertInset(new_inset))
364 bv->updateInset(new_inset);
368 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
369 bool edit, bool pastesel)
371 InsetOld * inset = createInset(cmd);
372 BufferView * bv = cmd.view();
376 if (lt->selection.set()) {
377 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
380 if (bv->insertInset(inset)) {
382 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
383 inset->dispatch(cmd);
385 if (gotsel && pastesel)
386 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
396 dispatch_result LyXText::dispatch(FuncRequest const & cmd)
398 lyxerr[Debug::ACTION] << "LyXText::dispatch: action[" << cmd.action
399 <<"] arg[" << cmd.argument << ']' << "xy[" <<
400 cmd.x << ',' << cmd.y << ']' << endl;
402 BufferView * bv = cmd.view();
404 switch (cmd.action) {
406 case LFUN_APPENDIX: {
407 ParagraphList::iterator pit = cursorPar();
408 bool start = !pit->params().startOfAppendix();
410 // ensure that we have only one start_of_appendix in this document
411 ParagraphList::iterator tmp = ownerParagraphs().begin();
412 ParagraphList::iterator end = ownerParagraphs().end();
414 for (; tmp != end; ++tmp) {
415 if (tmp->params().startOfAppendix()) {
416 recUndo(parOffset(tmp));
417 tmp->params().startOfAppendix(false);
423 recUndo(parOffset(pit));
424 pit->params().startOfAppendix(start);
426 // we can set the refreshing parameters now
428 redoParagraph(cursorPar());
429 setCursor(cursorPar(), cursor.pos());
434 case LFUN_DELETE_WORD_FORWARD:
435 bv->beforeChange(this);
440 case LFUN_DELETE_WORD_BACKWARD:
441 bv->beforeChange(this);
442 deleteWordBackward();
446 case LFUN_DELETE_LINE_FORWARD:
447 bv->beforeChange(this);
453 if (!selection.mark())
454 bv->beforeChange(this);
455 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
458 cursorRightOneWord();
463 if (!selection.mark())
464 bv->beforeChange(this);
465 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
466 cursorRightOneWord();
472 case LFUN_BEGINNINGBUF:
473 if (!selection.mark())
474 bv->beforeChange(this);
480 if (selection.mark())
481 bv->beforeChange(this);
487 if (!selection.set())
488 selection.cursor = cursor;
489 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
493 finishChange(bv, true);
497 if (!selection.set())
498 selection.cursor = cursor;
499 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
503 finishChange(bv, true);
507 if (!selection.set())
508 selection.cursor = cursor;
510 finishChange(bv, true);
514 if (!selection.set())
515 selection.cursor = cursor;
517 finishChange(bv, true);
520 case LFUN_UP_PARAGRAPHSEL:
521 if (!selection.set())
522 selection.cursor = cursor;
524 finishChange(bv, true);
527 case LFUN_DOWN_PARAGRAPHSEL:
528 if (!selection.set())
529 selection.cursor = cursor;
530 cursorDownParagraph();
531 finishChange(bv, true);
535 if (!selection.set())
536 selection.cursor = cursor;
538 finishChange(bv, true);
542 if (!selection.set())
543 selection.cursor = cursor;
545 finishChange(bv, true);
549 if (!selection.set())
550 selection.cursor = cursor;
552 finishChange(bv, true);
556 if (!selection.set())
557 selection.cursor = cursor;
559 finishChange(bv, true);
562 case LFUN_WORDRIGHTSEL:
563 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
566 cursorRightOneWord();
567 finishChange(bv, true);
570 case LFUN_WORDLEFTSEL:
571 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
572 cursorRightOneWord();
575 finishChange(bv, true);
579 LyXCursor cur1 = cursor;
581 ::getWord(*this, cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs());
582 setCursor(cur1.par(), cur1.pos());
583 bv->beforeChange(this);
584 setCursor(cur2.par(), cur2.pos());
585 finishChange(bv, true);
590 bool is_rtl = cursorPar()->isRightToLeftPar(bv->buffer()->params());
591 if (!selection.mark())
592 bv->beforeChange(this);
595 if (cursor.pos() < cursorPar()->size()
596 && cursorPar()->isInset(cursor.pos())
597 && isHighlyEditableInset(cursorPar()->getInset(cursor.pos()))) {
598 InsetOld * tmpinset = cursorPar()->getInset(cursor.pos());
599 cmd.message(tmpinset->editMessage());
600 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
601 tmpinset->dispatch(cmd1);
611 // This is soooo ugly. Isn`t it possible to make
613 bool const is_rtl = cursorPar()->isRightToLeftPar(bv->buffer()->params());
614 if (!selection.mark())
615 bv->beforeChange(this);
616 LyXCursor const cur = cursor;
619 if ((is_rtl || cur != cursor) && // only if really moved!
620 cursor.pos() < cursorPar()->size() &&
621 cursorPar()->isInset(cursor.pos()) &&
622 isHighlyEditableInset(cursorPar()->getInset(cursor.pos()))) {
623 InsetOld * tmpinset = cursorPar()->getInset(cursor.pos());
624 cmd.message(tmpinset->editMessage());
625 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
626 tmpinset->dispatch(cmd1);
636 if (!selection.mark())
637 bv->beforeChange(this);
643 if (!selection.mark())
644 bv->beforeChange(this);
649 case LFUN_UP_PARAGRAPH:
650 if (!selection.mark())
651 bv->beforeChange(this);
656 case LFUN_DOWN_PARAGRAPH:
657 if (!selection.mark())
658 bv->beforeChange(this);
659 cursorDownParagraph();
660 finishChange(bv, false);
664 if (!selection.mark())
665 bv->beforeChange(this);
667 finishChange(bv, false);
671 if (!selection.mark())
672 bv->beforeChange(this);
674 finishChange(bv, false);
678 if (!selection.mark())
679 bv->beforeChange(this);
681 finishChange(bv, false);
685 if (!selection.mark())
686 bv->beforeChange(this);
688 finishChange(bv, false);
691 case LFUN_BREAKLINE: {
692 lyx::pos_type body = cursorPar()->beginningOfBody();
694 // Not allowed by LaTeX (labels or empty par)
695 if (cursor.pos() <= body)
698 replaceSelection(bv->getLyXText());
699 insertInset(new InsetNewline);
700 setCursor(cursorPar(), cursor.pos());
701 moveCursorUpdate(bv, false);
706 if (!selection.set()) {
708 selection.cursor = cursor;
709 // It is possible to make it a lot faster still
710 // just comment out the line below...
712 cutSelection(true, false);
714 moveCursorUpdate(bv, false);
715 bv->owner()->view_state_changed();
718 case LFUN_DELETE_SKIP:
719 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
720 if (!selection.set()) {
721 if (cursor.pos() == cursorPar()->size()) {
723 ParagraphParameters & params = cursorPar()->params();
724 if (cursor.pos() == 0
725 && !(params.spaceTop() == VSpace (VSpace::NONE))) {
729 params.pagebreakTop(),
730 params.pagebreakBottom(),
731 VSpace(VSpace::NONE),
732 params.spaceBottom(),
735 params.labelWidthString(), 0);
740 selection.cursor = cursor;
744 selection.cursor = cursor;
747 cutSelection(true, false);
754 if (!selection.set()) {
755 if (bv->owner()->getIntl().getTransManager().backspace()) {
757 selection.cursor = cursor;
758 // It is possible to make it a lot faster still
759 // just comment out the line below...
762 cutSelection(true, false);
764 bv->owner()->view_state_changed();
769 case LFUN_BACKSPACE_SKIP:
770 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
771 if (!selection.set()) {
772 ParagraphParameters & params = cursorPar()->params();
773 if (cursor.pos() == 0 && !(params.spaceTop() == VSpace(VSpace::NONE))) {
777 params.pagebreakTop(),
778 params.pagebreakBottom(),
779 VSpace(VSpace::NONE),
780 params.spaceBottom(),
783 params.labelWidthString(), 0);
785 LyXCursor cur = cursor;
787 selection.cursor = cur;
790 cutSelection(true, false);
795 case LFUN_BREAKPARAGRAPH:
796 replaceSelection(bv->getLyXText());
797 breakParagraph(bv->buffer()->paragraphs(), 0);
799 selection.cursor = cursor;
801 bv->owner()->view_state_changed();
804 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
805 replaceSelection(bv->getLyXText());
806 breakParagraph(bv->buffer()->paragraphs(), 1);
808 selection.cursor = cursor;
810 bv->owner()->view_state_changed();
813 case LFUN_BREAKPARAGRAPH_SKIP: {
814 // When at the beginning of a paragraph, remove
815 // indentation and add a "defskip" at the top.
816 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
817 LyXCursor cur = cursor;
818 replaceSelection(bv->getLyXText());
819 if (cur.pos() == 0) {
820 ParagraphParameters & params = getPar(cur)->params();
821 if (params.spaceTop() == VSpace(VSpace::NONE)) {
825 params.pagebreakTop(),
826 params.pagebreakBottom(),
827 VSpace(VSpace::DEFSKIP), params.spaceBottom(),
830 params.labelWidthString(), 1);
834 breakParagraph(bv->buffer()->paragraphs(), 0);
837 selection.cursor = cur;
839 bv->owner()->view_state_changed();
843 case LFUN_PARAGRAPH_SPACING: {
844 ParagraphList::iterator pit = cursorPar();
845 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
846 float cur_value = 1.0;
847 if (cur_spacing == Spacing::Other)
848 cur_value = pit->params().spacing().getValue();
850 istringstream is(cmd.argument);
853 Spacing::Space new_spacing = cur_spacing;
854 float new_value = cur_value;
856 lyxerr << "Missing argument to `paragraph-spacing'"
858 } else if (tmp == "single") {
859 new_spacing = Spacing::Single;
860 } else if (tmp == "onehalf") {
861 new_spacing = Spacing::Onehalf;
862 } else if (tmp == "double") {
863 new_spacing = Spacing::Double;
864 } else if (tmp == "other") {
865 new_spacing = Spacing::Other;
868 lyxerr << "new_value = " << tmpval << endl;
871 } else if (tmp == "default") {
872 new_spacing = Spacing::Default;
874 lyxerr << _("Unknown spacing argument: ")
875 << cmd.argument << endl;
877 if (cur_spacing != new_spacing || cur_value != new_value) {
878 pit->params().spacing(Spacing(new_spacing, new_value));
885 case LFUN_INSET_SETTINGS:
886 BOOST_ASSERT(bv->theLockingInset());
887 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
890 case LFUN_INSET_TOGGLE:
891 bv->beforeChange(this);
897 case LFUN_SPACE_INSERT:
898 if (cursorPar()->layout()->free_spacing)
901 doInsertInset(this, cmd, false, false);
902 moveCursorUpdate(bv, false);
905 case LFUN_HYPHENATION:
906 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
909 case LFUN_LIGATURE_BREAK:
910 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
914 specialChar(this, bv, InsetSpecialChar::LDOTS);
917 case LFUN_END_OF_SENTENCE:
918 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
921 case LFUN_MENU_SEPARATOR:
922 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
926 bv->beforeChange(this);
928 selection.cursor = cursor;
929 cmd.message(N_("Mark off"));
933 bv->beforeChange(this);
934 selection.mark(true);
936 selection.cursor = cursor;
937 cmd.message(N_("Mark on"));
941 bv->beforeChange(this);
942 if (selection.mark()) {
943 cmd.message(N_("Mark removed"));
945 selection.mark(true);
946 cmd.message(N_("Mark set"));
948 selection.cursor = cursor;
952 case LFUN_UPCASE_WORD:
953 changeCase(LyXText::text_uppercase);
957 case LFUN_LOWCASE_WORD:
958 changeCase(LyXText::text_lowercase);
962 case LFUN_CAPITALIZE_WORD:
963 changeCase(LyXText::text_capitalization);
967 case LFUN_TRANSPOSE_CHARS:
968 recUndo(cursor.par());
974 cmd.message(_("Paste"));
975 replaceSelection(bv->getLyXText());
976 #warning FIXME Check if the arg is in the domain of available selections.
977 if (isStrUnsignedInt(cmd.argument))
978 pasteSelection(strToUnsignedInt(cmd.argument));
981 clearSelection(); // bug 393
988 cutSelection(true, true);
989 cmd.message(_("Cut"));
995 cmd.message(_("Copy"));
998 case LFUN_BEGINNINGBUFSEL:
1000 return UNDISPATCHED;
1002 finishChange(bv, true);
1005 case LFUN_ENDBUFSEL:
1007 return UNDISPATCHED;
1009 finishChange(bv, true);
1013 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1019 istringstream is(cmd.argument);
1022 lyxerr << "SETXY: Could not parse coordinates in '"
1023 << cmd.argument << std::endl;
1025 setCursorFromCoordinates(x, y);
1030 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1032 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1038 case LFUN_GETLAYOUT:
1039 cmd.message(tostr(cursorPar()->layout()));
1043 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1044 << cmd.argument << endl;
1046 // This is not the good solution to the empty argument
1047 // problem, but it will hopefully suffice for 1.2.0.
1048 // The correct solution would be to augument the
1049 // function list/array with information about what
1050 // functions needs arguments and their type.
1051 if (cmd.argument.empty()) {
1052 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1056 // Derive layout number from given argument (string)
1057 // and current buffer's textclass (number)
1058 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1059 bool hasLayout = tclass.hasLayout(cmd.argument);
1060 string layout = cmd.argument;
1062 // If the entry is obsolete, use the new one instead.
1064 string const & obs = tclass[layout]->obsoleted_by();
1070 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1075 bool change_layout = (current_layout != layout);
1077 if (!change_layout && selection.set() &&
1078 selection.start.par() != selection.end.par())
1080 ParagraphList::iterator spit = getPar(selection.start);
1081 ParagraphList::iterator epit = boost::next(getPar(selection.end));
1082 while (spit != epit) {
1083 if (spit->layout()->name() != current_layout) {
1084 change_layout = true;
1091 if (change_layout) {
1092 current_layout = layout;
1094 bv->owner()->setLayout(layout);
1101 case LFUN_PASTESELECTION: {
1102 // this was originally a beforeChange(bv->text), i.e
1103 // the outermost LyXText!
1104 bv->beforeChange(this);
1105 string const clip = bv->getClipboard();
1106 if (!clip.empty()) {
1107 if (cmd.argument == "paragraph")
1108 insertStringAsParagraphs(clip);
1110 insertStringAsLines(clip);
1117 case LFUN_GOTOERROR:
1118 gotoInset(InsetOld::ERROR_CODE, false);
1122 gotoInset(InsetOld::NOTE_CODE, false);
1125 case LFUN_REFERENCE_GOTO:
1127 vector<InsetOld::Code> tmp;
1128 tmp.push_back(InsetOld::LABEL_CODE);
1129 tmp.push_back(InsetOld::REF_CODE);
1130 gotoInset(tmp, true);
1135 replaceSelection(bv->getLyXText());
1136 ParagraphList::iterator pit = cursorPar();
1137 lyx::pos_type pos = cursor.pos();
1141 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1144 c = pit->getChar(pos - 1);
1146 LyXLayout_ptr const & style = pit->layout();
1148 BufferParams const & bufparams = bv->buffer()->params();
1149 if (style->pass_thru ||
1150 pit->getFontSettings(bufparams,pos).language()->lang() == "hebrew" ||
1151 !bv->insertInset(new InsetQuotes(c, bufparams)))
1152 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1156 case LFUN_DATE_INSERT: {
1157 replaceSelection(bv->getLyXText());
1158 time_t now_time_t = time(NULL);
1159 struct tm * now_tm = localtime(&now_time_t);
1160 setlocale(LC_TIME, "");
1162 if (!cmd.argument.empty())
1165 arg = lyxrc.date_insert_format;
1167 int const datetmp_len =
1168 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1170 for (int i = 0; i < datetmp_len; i++)
1171 insertChar(datetmp[i]);
1173 selection.cursor = cursor;
1174 moveCursorUpdate(bv, false);
1178 case LFUN_MOUSE_TRIPLE:
1181 if (!isInInset() && bv->theLockingInset())
1183 if (cmd.button() == mouse_button::button1) {
1185 selection.cursor = cursor;
1189 bv->haveSelection(selection.set());
1193 case LFUN_MOUSE_DOUBLE:
1196 if (!isInInset() && bv->theLockingInset())
1198 if (cmd.button() == mouse_button::button1) {
1199 selectWord(lyx::WHOLE_WORD_STRICT);
1201 bv->haveSelection(selection.set());
1205 case LFUN_MOUSE_MOTION:
1207 // Only use motion with button 1
1208 //if (ev.button() != mouse_button::button1)
1214 // Check for inset locking
1215 if (bv->theLockingInset()) {
1216 InsetOld * tli = bv->theLockingInset();
1217 LyXCursor cursor = bv->text->cursor;
1218 LyXFont font = bv->text->getFont(bv->text->cursorPar(), cursor.pos());
1219 int width = tli->width();
1220 int inset_x = font.isVisibleRightToLeft()
1221 ? cursor.x() - width : cursor.x();
1222 int start_x = inset_x + tli->scroll();
1223 FuncRequest cmd1 = cmd;
1224 cmd1.x = cmd.x - start_x;
1225 cmd1.y = cmd.y - cursor.y() + bv->top_y();
1226 tli->dispatch(cmd1);
1230 // The test for not selection possible is needed, that only motion
1231 // events are used, where the bottom press event was on
1232 // the drawing area too
1233 if (!selection_possible) {
1234 lyxerr[Debug::ACTION]
1235 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1239 RowList::iterator cursorrow = bv->text->cursorRow();
1240 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->top_y());
1242 // sorry for this but I have a strange error that the y value jumps at
1243 // a certain point. This seems like an error in my xforms library or
1244 // in some other local environment, but I would like to leave this here
1245 // for the moment until I can remove this (Jug 20020418)
1246 if (y_before < bv->text->cursor.y())
1247 lyxerr << y_before << ':'
1248 << bv->text->cursor.y() << endl;
1250 // This is to allow jumping over large insets
1251 if (cursorrow == bv->text->cursorRow()) {
1252 if (cmd.y >= bv->workHeight())
1253 bv->text->cursorDown(false);
1255 bv->text->cursorUp(false);
1258 bv->text->setSelection();
1263 // Single-click on work area
1264 case LFUN_MOUSE_PRESS:
1269 // ok ok, this is a hack (for xforms)
1270 // We shouldn't go further down as we really should only do the
1271 // scrolling and be done with this. Otherwise we may open some
1272 // dialogs (Jug 20020424).
1273 if (cmd.button() == mouse_button::button4) {
1274 bv->scroll(-lyxrc.wheel_jump);
1277 if (cmd.button() == mouse_button::button5) {
1278 bv->scroll(lyxrc.wheel_jump);
1284 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1286 // Middle button press pastes if we have a selection
1287 // We do this here as if the selection was inside an inset
1288 // it could get cleared on the unlocking of the inset so
1289 // we have to check this first
1290 bool paste_internally = false;
1291 if (cmd.button() == mouse_button::button2 && selection.set()) {
1292 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1293 paste_internally = true;
1296 int const screen_first = bv->top_y();
1298 if (bv->theLockingInset()) {
1299 // We are in inset locking mode
1301 // Check whether the inset was hit. If not reset mode,
1302 // otherwise give the event to the inset
1303 if (inset_hit == bv->theLockingInset()) {
1304 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1305 bv->theLockingInset()->dispatch(cmd1);
1308 bv->unlockInset(bv->theLockingInset());
1312 selection_possible = true;
1314 // Clear the selection
1315 bv->text->clearSelection();
1317 bv->updateScrollbar();
1319 // Single left click in math inset?
1320 if (isHighlyEditableInset(inset_hit)) {
1321 // Highly editable inset, like math
1322 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1323 selection_possible = false;
1324 bv->owner()->message(inset->editMessage());
1325 // We just have to lock the inset before calling a PressEvent on it!
1326 if (!bv->lockInset(inset))
1327 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1328 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1329 inset->dispatch(cmd1);
1332 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1334 // Right click on a footnote flag opens float menu
1335 if (cmd.button() == mouse_button::button3) {
1336 selection_possible = false;
1340 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1341 bv->text->setCursorFromCoordinates(x, y + screen_first);
1343 bv->text->selection.cursor = bv->text->cursor;
1344 bv->text->cursor.x_fix(bv->text->cursor.x());
1346 if (bv->fitCursor())
1347 selection_possible = false;
1349 // Insert primary selection with middle mouse
1350 // if there is a local selection in the current buffer,
1352 if (cmd.button() == mouse_button::button2) {
1353 if (paste_internally)
1354 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1356 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1357 selection_possible = false;
1362 case LFUN_MOUSE_RELEASE:
1364 // do nothing if we used the mouse wheel
1368 if (cmd.button() == mouse_button::button4
1369 || cmd.button() == mouse_button::button5)
1372 // If we hit an inset, we have the inset coordinates in these
1373 // and inset_hit points to the inset. If we do not hit an
1374 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1377 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1379 if (bv->theLockingInset()) {
1380 // We are in inset locking mode.
1382 // LyX does a kind of work-area grabbing for insets.
1383 // Only a ButtonPress FuncRequest outside the inset will
1384 // force a insetUnlock.
1385 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1386 bv->theLockingInset()->dispatch(cmd1);
1390 selection_possible = false;
1392 if (cmd.button() == mouse_button::button2)
1396 if (cmd.button() == mouse_button::button1)
1397 bv->haveSelection(selection.set());
1400 bv->owner()->view_state_changed();
1401 bv->owner()->updateMenubar();
1402 bv->owner()->updateToolbar();
1404 // Did we hit an editable inset?
1406 selection_possible = false;
1408 // if we reach this point with a selection, it
1409 // must mean we are currently selecting.
1410 // But we don't want to open the inset
1411 // because that is annoying for the user.
1412 // So just pretend we didn't hit it.
1413 // this is OK because a "kosher" ButtonRelease
1414 // will follow a ButtonPress that clears
1416 // Note this also fixes selection drawing
1417 // problems if we end up opening an inset
1418 if (selection.set())
1421 // CHECK fix this proper in 0.13
1422 // well, maybe 13.0 !!!!!!!!!
1424 // Following a ref shouldn't issue
1425 // a push on the undo-stack
1426 // anylonger, now that we have
1427 // keybindings for following
1428 // references and returning from
1429 // references. IMHO though, it
1430 // should be the inset's own business
1431 // to push or not push on the undo
1432 // stack. They don't *have* to
1433 // alter the document...
1435 // ...or maybe the recordUndo()
1436 // below isn't necessary at all anylonger?
1437 if (inset_hit->lyxCode() == InsetOld::REF_CODE)
1438 recUndo(cursor.par());
1440 bv->owner()->message(inset_hit->editMessage());
1442 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1443 inset_hit->dispatch(cmd1);
1449 case LFUN_SELFINSERT: {
1450 if (cmd.argument.empty())
1453 // Automatically delete the currently selected
1454 // text and replace it with what is being
1455 // typed in now. Depends on lyxrc settings
1456 // "auto_region_delete", which defaults to
1459 if (lyxrc.auto_region_delete) {
1460 if (selection.set())
1461 cutSelection(false, false);
1462 bv->haveSelection(false);
1465 bv->beforeChange(this);
1466 LyXFont const old_font = real_current_font;
1468 string::const_iterator cit = cmd.argument.begin();
1469 string::const_iterator end = cmd.argument.end();
1470 for (; cit != end; ++cit)
1471 bv->owner()->getIntl().getTransManager().
1472 TranslateAndInsert(*cit, this);
1474 selection.cursor = cursor;
1475 moveCursorUpdate(bv, false);
1477 // real_current_font.number can change so we need to
1478 // update the minibuffer
1479 if (old_font != real_current_font)
1480 bv->owner()->view_state_changed();
1481 bv->updateScrollbar();
1485 case LFUN_HTMLURL: {
1486 InsetCommandParams p("htmlurl");
1487 string const data = InsetCommandMailer::params2string("url", p);
1488 bv->owner()->getDialogs().show("url", data, 0);
1493 InsetCommandParams p("url");
1494 string const data = InsetCommandMailer::params2string("url", p);
1495 bv->owner()->getDialogs().show("url", data, 0);
1501 case LFUN_INSET_LIST:
1502 case LFUN_INSET_THEOREM:
1503 case LFUN_INSET_CAPTION:
1505 case LFUN_INSERT_NOTE:
1506 case LFUN_INSERT_BOX:
1507 case LFUN_INSERT_BRANCH:
1508 case LFUN_INSERT_BIBITEM:
1509 case LFUN_INSET_ERT:
1510 case LFUN_INSET_FLOAT:
1511 case LFUN_INSET_FOOTNOTE:
1512 case LFUN_INSET_MARGINAL:
1513 case LFUN_INSET_MINIPAGE:
1514 case LFUN_INSET_OPTARG:
1515 case LFUN_INSET_WIDE_FLOAT:
1516 case LFUN_INSET_WRAP:
1517 case LFUN_TABULAR_INSERT:
1518 case LFUN_ENVIRONMENT_INSERT:
1519 // Open the inset, and move the current selection
1521 doInsertInset(this, cmd, true, true);
1524 case LFUN_INDEX_INSERT:
1525 // Just open the inset
1526 doInsertInset(this, cmd, true, false);
1529 case LFUN_INDEX_PRINT:
1530 case LFUN_TOC_INSERT:
1533 doInsertInset(this, cmd, false, false);
1536 case LFUN_DEPTH_MIN:
1537 bv_funcs::changeDepth(bv, this, bv_funcs::DEC_DEPTH, false);
1542 case LFUN_DEPTH_PLUS:
1544 bv_funcs::changeDepth(bv, this, bv_funcs::INC_DEPTH, false);
1550 return UNDISPATCHED;