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.
275 new_y = bv()->text->cursor.y()
276 + bv()->theLockingInset()->insetInInsetY() + y
278 - bv()->workHeight() + 1;
283 - bv()->workHeight() + 1;
288 ParagraphList::iterator pit = cursorPar();
290 if (isFirstRow(pit, *rit))
293 previousRow(pit, rit);
294 setCursor(cur, parOffset(pit), rit->pos(), false);
295 if (cur.y() > bv_owner->top_y())
297 bv()->updateScrollbar();
301 void LyXText::cursorNext()
303 int topy = bv_owner->top_y();
305 RowList::iterator rit = cursorRow();
306 if (rit == lastRow()) {
307 int y = cursor.y() - rit->baseline() + cursorRow()->height();
308 if (y > topy + bv()->workHeight())
309 bv_owner->updateScrollbar();
313 int y = topy + bv_owner->workHeight();
314 if (inset_owner && !topy) {
315 y -= (bv_owner->text->cursor.y()
317 + bv_owner->theLockingInset()->insetInInsetY());
320 ParagraphList::iterator dummypit;
321 Row const & rr = *getRowNearY(y, dummypit);
322 y = dummypit->y + rr.y_offset();
324 setCursorFromCoordinates(cursor.x_fix(), y);
325 // + bv->workHeight());
329 if (rit == bv_owner->text->cursorRow()) {
330 // we have a row which is taller than the workarea. The
331 // simplest solution is to move to the next row instead.
334 // This is what we used to do, so we wouldn't skip right past
335 // tall rows, but it's not working right now.
337 new_y = bv->top_y() + bv->workHeight();
341 new_y = bv()->text->cursor.y()
342 + bv()->theLockingInset()->insetInInsetY()
343 + y - rit->baseline();
345 new_y = cursor.y() - cursorRow()->baseline();
349 ParagraphList::iterator pit = cursorPar();
353 setCursor(cur, parOffset(pit), rit->pos(), false);
354 if (cur.y() < bv_owner->top_y() + bv()->workHeight())
356 bv()->updateScrollbar();
362 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
365 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
366 replaceSelection(lt);
367 if (!bv->insertInset(new_inset))
370 bv->updateInset(new_inset);
374 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
375 bool edit, bool pastesel)
377 InsetOld * inset = createInset(cmd);
378 BufferView * bv = cmd.view();
382 if (lt->selection.set()) {
383 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
386 if (bv->insertInset(inset)) {
388 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
389 inset->dispatch(cmd);
391 if (gotsel && pastesel)
392 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
402 dispatch_result LyXText::dispatch(FuncRequest const & cmd)
404 lyxerr[Debug::ACTION] << "LyXText::dispatch: action[" << cmd.action
405 <<"] arg[" << cmd.argument << ']' << "xy[" <<
406 cmd.x << ',' << cmd.y << ']' << endl;
408 BufferView * bv = cmd.view();
410 switch (cmd.action) {
412 case LFUN_APPENDIX: {
413 ParagraphList::iterator pit = cursorPar();
414 bool start = !pit->params().startOfAppendix();
416 // ensure that we have only one start_of_appendix in this document
417 ParagraphList::iterator tmp = ownerParagraphs().begin();
418 ParagraphList::iterator end = ownerParagraphs().end();
420 for (; tmp != end; ++tmp) {
421 if (tmp->params().startOfAppendix()) {
422 recUndo(parOffset(tmp));
423 tmp->params().startOfAppendix(false);
429 recUndo(parOffset(pit));
430 pit->params().startOfAppendix(start);
432 // we can set the refreshing parameters now
434 redoParagraph(cursorPar());
435 setCursor(cursorPar(), cursor.pos());
440 case LFUN_DELETE_WORD_FORWARD:
441 bv->beforeChange(this);
446 case LFUN_DELETE_WORD_BACKWARD:
447 bv->beforeChange(this);
448 deleteWordBackward();
452 case LFUN_DELETE_LINE_FORWARD:
453 bv->beforeChange(this);
459 if (!selection.mark())
460 bv->beforeChange(this);
461 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
464 cursorRightOneWord();
469 if (!selection.mark())
470 bv->beforeChange(this);
471 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
472 cursorRightOneWord();
478 case LFUN_BEGINNINGBUF:
479 if (!selection.mark())
480 bv->beforeChange(this);
486 if (selection.mark())
487 bv->beforeChange(this);
493 if (!selection.set())
494 selection.cursor = cursor;
495 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
499 finishChange(bv, true);
503 if (!selection.set())
504 selection.cursor = cursor;
505 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
509 finishChange(bv, true);
513 if (!selection.set())
514 selection.cursor = cursor;
516 finishChange(bv, true);
520 if (!selection.set())
521 selection.cursor = cursor;
523 finishChange(bv, true);
526 case LFUN_UP_PARAGRAPHSEL:
527 if (!selection.set())
528 selection.cursor = cursor;
530 finishChange(bv, true);
533 case LFUN_DOWN_PARAGRAPHSEL:
534 if (!selection.set())
535 selection.cursor = cursor;
536 cursorDownParagraph();
537 finishChange(bv, true);
541 if (!selection.set())
542 selection.cursor = cursor;
544 finishChange(bv, true);
548 if (!selection.set())
549 selection.cursor = cursor;
551 finishChange(bv, true);
555 if (!selection.set())
556 selection.cursor = cursor;
558 finishChange(bv, true);
562 if (!selection.set())
563 selection.cursor = cursor;
565 finishChange(bv, true);
568 case LFUN_WORDRIGHTSEL:
569 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
572 cursorRightOneWord();
573 finishChange(bv, true);
576 case LFUN_WORDLEFTSEL:
577 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
578 cursorRightOneWord();
581 finishChange(bv, true);
585 LyXCursor cur1 = cursor;
587 ::getWord(*this, cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs());
588 setCursor(cur1.par(), cur1.pos());
589 bv->beforeChange(this);
590 setCursor(cur2.par(), cur2.pos());
591 finishChange(bv, true);
596 bool is_rtl = cursorPar()->isRightToLeftPar(bv->buffer()->params());
597 if (!selection.mark())
598 bv->beforeChange(this);
601 if (cursor.pos() < cursorPar()->size()
602 && cursorPar()->isInset(cursor.pos())
603 && isHighlyEditableInset(cursorPar()->getInset(cursor.pos()))) {
604 InsetOld * tmpinset = cursorPar()->getInset(cursor.pos());
605 cmd.message(tmpinset->editMessage());
606 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
607 tmpinset->dispatch(cmd1);
617 // This is soooo ugly. Isn`t it possible to make
619 bool const is_rtl = cursorPar()->isRightToLeftPar(bv->buffer()->params());
620 if (!selection.mark())
621 bv->beforeChange(this);
622 LyXCursor const cur = cursor;
625 if ((is_rtl || cur != cursor) && // only if really moved!
626 cursor.pos() < cursorPar()->size() &&
627 cursorPar()->isInset(cursor.pos()) &&
628 isHighlyEditableInset(cursorPar()->getInset(cursor.pos()))) {
629 InsetOld * tmpinset = cursorPar()->getInset(cursor.pos());
630 cmd.message(tmpinset->editMessage());
631 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
632 tmpinset->dispatch(cmd1);
642 if (!selection.mark())
643 bv->beforeChange(this);
649 if (!selection.mark())
650 bv->beforeChange(this);
655 case LFUN_UP_PARAGRAPH:
656 if (!selection.mark())
657 bv->beforeChange(this);
662 case LFUN_DOWN_PARAGRAPH:
663 if (!selection.mark())
664 bv->beforeChange(this);
665 cursorDownParagraph();
666 finishChange(bv, false);
670 if (!selection.mark())
671 bv->beforeChange(this);
673 finishChange(bv, false);
677 if (!selection.mark())
678 bv->beforeChange(this);
680 finishChange(bv, false);
684 if (!selection.mark())
685 bv->beforeChange(this);
687 finishChange(bv, false);
691 if (!selection.mark())
692 bv->beforeChange(this);
694 finishChange(bv, false);
697 case LFUN_BREAKLINE: {
698 lyx::pos_type body = cursorPar()->beginningOfBody();
700 // Not allowed by LaTeX (labels or empty par)
701 if (cursor.pos() <= body)
704 replaceSelection(bv->getLyXText());
705 insertInset(new InsetNewline);
706 setCursor(cursorPar(), cursor.pos());
707 moveCursorUpdate(bv, false);
712 if (!selection.set()) {
714 selection.cursor = cursor;
715 // It is possible to make it a lot faster still
716 // just comment out the line below...
718 cutSelection(true, false);
720 moveCursorUpdate(bv, false);
721 bv->owner()->view_state_changed();
724 case LFUN_DELETE_SKIP:
725 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
726 if (!selection.set()) {
727 if (cursor.pos() == cursorPar()->size()) {
729 ParagraphParameters & params = cursorPar()->params();
730 if (cursor.pos() == 0
731 && !(params.spaceTop() == VSpace (VSpace::NONE))) {
735 params.pagebreakTop(),
736 params.pagebreakBottom(),
737 VSpace(VSpace::NONE),
738 params.spaceBottom(),
741 params.labelWidthString(), 0);
746 selection.cursor = cursor;
750 selection.cursor = cursor;
753 cutSelection(true, false);
760 if (!selection.set()) {
761 if (bv->owner()->getIntl().getTransManager().backspace()) {
763 selection.cursor = cursor;
764 // It is possible to make it a lot faster still
765 // just comment out the line below...
768 cutSelection(true, false);
770 bv->owner()->view_state_changed();
775 case LFUN_BACKSPACE_SKIP:
776 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
777 if (!selection.set()) {
778 ParagraphParameters & params = cursorPar()->params();
779 if (cursor.pos() == 0 && !(params.spaceTop() == VSpace(VSpace::NONE))) {
783 params.pagebreakTop(),
784 params.pagebreakBottom(),
785 VSpace(VSpace::NONE),
786 params.spaceBottom(),
789 params.labelWidthString(), 0);
791 LyXCursor cur = cursor;
793 selection.cursor = cur;
796 cutSelection(true, false);
801 case LFUN_BREAKPARAGRAPH:
802 replaceSelection(bv->getLyXText());
803 breakParagraph(bv->buffer()->paragraphs(), 0);
805 selection.cursor = cursor;
807 bv->owner()->view_state_changed();
810 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
811 replaceSelection(bv->getLyXText());
812 breakParagraph(bv->buffer()->paragraphs(), 1);
814 selection.cursor = cursor;
816 bv->owner()->view_state_changed();
819 case LFUN_BREAKPARAGRAPH_SKIP: {
820 // When at the beginning of a paragraph, remove
821 // indentation and add a "defskip" at the top.
822 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
823 LyXCursor cur = cursor;
824 replaceSelection(bv->getLyXText());
825 if (cur.pos() == 0) {
826 ParagraphParameters & params = getPar(cur)->params();
827 if (params.spaceTop() == VSpace(VSpace::NONE)) {
831 params.pagebreakTop(),
832 params.pagebreakBottom(),
833 VSpace(VSpace::DEFSKIP), params.spaceBottom(),
836 params.labelWidthString(), 1);
840 breakParagraph(bv->buffer()->paragraphs(), 0);
843 selection.cursor = cur;
845 bv->owner()->view_state_changed();
849 case LFUN_PARAGRAPH_SPACING: {
850 ParagraphList::iterator pit = cursorPar();
851 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
852 float cur_value = 1.0;
853 if (cur_spacing == Spacing::Other)
854 cur_value = pit->params().spacing().getValue();
856 istringstream is(cmd.argument);
859 Spacing::Space new_spacing = cur_spacing;
860 float new_value = cur_value;
862 lyxerr << "Missing argument to `paragraph-spacing'"
864 } else if (tmp == "single") {
865 new_spacing = Spacing::Single;
866 } else if (tmp == "onehalf") {
867 new_spacing = Spacing::Onehalf;
868 } else if (tmp == "double") {
869 new_spacing = Spacing::Double;
870 } else if (tmp == "other") {
871 new_spacing = Spacing::Other;
874 lyxerr << "new_value = " << tmpval << endl;
877 } else if (tmp == "default") {
878 new_spacing = Spacing::Default;
880 lyxerr << _("Unknown spacing argument: ")
881 << cmd.argument << endl;
883 if (cur_spacing != new_spacing || cur_value != new_value) {
884 pit->params().spacing(Spacing(new_spacing, new_value));
891 case LFUN_INSET_SETTINGS:
892 BOOST_ASSERT(bv->theLockingInset());
893 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
896 case LFUN_INSET_TOGGLE:
897 bv->beforeChange(this);
903 case LFUN_SPACE_INSERT:
904 if (cursorPar()->layout()->free_spacing)
907 doInsertInset(this, cmd, false, false);
908 moveCursorUpdate(bv, false);
911 case LFUN_HYPHENATION:
912 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
915 case LFUN_LIGATURE_BREAK:
916 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
920 specialChar(this, bv, InsetSpecialChar::LDOTS);
923 case LFUN_END_OF_SENTENCE:
924 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
927 case LFUN_MENU_SEPARATOR:
928 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
932 bv->beforeChange(this);
934 selection.cursor = cursor;
935 cmd.message(N_("Mark off"));
939 bv->beforeChange(this);
940 selection.mark(true);
942 selection.cursor = cursor;
943 cmd.message(N_("Mark on"));
947 bv->beforeChange(this);
948 if (selection.mark()) {
949 cmd.message(N_("Mark removed"));
951 selection.mark(true);
952 cmd.message(N_("Mark set"));
954 selection.cursor = cursor;
958 case LFUN_UPCASE_WORD:
959 changeCase(LyXText::text_uppercase);
963 case LFUN_LOWCASE_WORD:
964 changeCase(LyXText::text_lowercase);
968 case LFUN_CAPITALIZE_WORD:
969 changeCase(LyXText::text_capitalization);
973 case LFUN_TRANSPOSE_CHARS:
974 recUndo(cursor.par());
980 cmd.message(_("Paste"));
981 replaceSelection(bv->getLyXText());
982 #warning FIXME Check if the arg is in the domain of available selections.
983 if (isStrUnsignedInt(cmd.argument))
984 pasteSelection(strToUnsignedInt(cmd.argument));
987 clearSelection(); // bug 393
994 cutSelection(true, true);
995 cmd.message(_("Cut"));
1001 cmd.message(_("Copy"));
1004 case LFUN_BEGINNINGBUFSEL:
1006 return UNDISPATCHED;
1008 finishChange(bv, true);
1011 case LFUN_ENDBUFSEL:
1013 return UNDISPATCHED;
1015 finishChange(bv, true);
1019 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1025 istringstream is(cmd.argument);
1028 lyxerr << "SETXY: Could not parse coordinates in '"
1029 << cmd.argument << std::endl;
1031 setCursorFromCoordinates(x, y);
1036 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1038 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1044 case LFUN_GETLAYOUT:
1045 cmd.message(tostr(cursorPar()->layout()));
1049 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1050 << cmd.argument << endl;
1052 // This is not the good solution to the empty argument
1053 // problem, but it will hopefully suffice for 1.2.0.
1054 // The correct solution would be to augument the
1055 // function list/array with information about what
1056 // functions needs arguments and their type.
1057 if (cmd.argument.empty()) {
1058 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1062 // Derive layout number from given argument (string)
1063 // and current buffer's textclass (number)
1064 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1065 bool hasLayout = tclass.hasLayout(cmd.argument);
1066 string layout = cmd.argument;
1068 // If the entry is obsolete, use the new one instead.
1070 string const & obs = tclass[layout]->obsoleted_by();
1076 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1081 bool change_layout = (current_layout != layout);
1083 if (!change_layout && selection.set() &&
1084 selection.start.par() != selection.end.par())
1086 ParagraphList::iterator spit = getPar(selection.start);
1087 ParagraphList::iterator epit = boost::next(getPar(selection.end));
1088 while (spit != epit) {
1089 if (spit->layout()->name() != current_layout) {
1090 change_layout = true;
1097 if (change_layout) {
1098 current_layout = layout;
1100 bv->owner()->setLayout(layout);
1107 case LFUN_PASTESELECTION: {
1108 // this was originally a beforeChange(bv->text), i.e
1109 // the outermost LyXText!
1110 bv->beforeChange(this);
1111 string const clip = bv->getClipboard();
1112 if (!clip.empty()) {
1113 if (cmd.argument == "paragraph")
1114 insertStringAsParagraphs(clip);
1116 insertStringAsLines(clip);
1123 case LFUN_GOTOERROR:
1124 gotoInset(InsetOld::ERROR_CODE, false);
1128 gotoInset(InsetOld::NOTE_CODE, false);
1131 case LFUN_REFERENCE_GOTO:
1133 vector<InsetOld::Code> tmp;
1134 tmp.push_back(InsetOld::LABEL_CODE);
1135 tmp.push_back(InsetOld::REF_CODE);
1136 gotoInset(tmp, true);
1141 replaceSelection(bv->getLyXText());
1142 ParagraphList::iterator pit = cursorPar();
1143 lyx::pos_type pos = cursor.pos();
1147 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1150 c = pit->getChar(pos - 1);
1152 LyXLayout_ptr const & style = pit->layout();
1154 BufferParams const & bufparams = bv->buffer()->params();
1155 if (style->pass_thru ||
1156 pit->getFontSettings(bufparams,pos).language()->lang() == "hebrew" ||
1157 !bv->insertInset(new InsetQuotes(c, bufparams)))
1158 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1162 case LFUN_DATE_INSERT: {
1163 replaceSelection(bv->getLyXText());
1164 time_t now_time_t = time(NULL);
1165 struct tm * now_tm = localtime(&now_time_t);
1166 setlocale(LC_TIME, "");
1168 if (!cmd.argument.empty())
1171 arg = lyxrc.date_insert_format;
1173 int const datetmp_len =
1174 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1176 for (int i = 0; i < datetmp_len; i++)
1177 insertChar(datetmp[i]);
1179 selection.cursor = cursor;
1180 moveCursorUpdate(bv, false);
1184 case LFUN_MOUSE_TRIPLE:
1187 if (!isInInset() && bv->theLockingInset())
1189 if (cmd.button() == mouse_button::button1) {
1191 selection.cursor = cursor;
1195 bv->haveSelection(selection.set());
1199 case LFUN_MOUSE_DOUBLE:
1202 if (!isInInset() && bv->theLockingInset())
1204 if (cmd.button() == mouse_button::button1) {
1205 selectWord(lyx::WHOLE_WORD_STRICT);
1207 bv->haveSelection(selection.set());
1211 case LFUN_MOUSE_MOTION:
1213 // Only use motion with button 1
1214 //if (ev.button() != mouse_button::button1)
1220 // Check for inset locking
1221 if (bv->theLockingInset()) {
1222 InsetOld * tli = bv->theLockingInset();
1223 LyXCursor cursor = bv->text->cursor;
1224 LyXFont font = bv->text->getFont(bv->text->cursorPar(), cursor.pos());
1225 int width = tli->width();
1226 int inset_x = font.isVisibleRightToLeft()
1227 ? cursor.x() - width : cursor.x();
1228 int start_x = inset_x + tli->scroll();
1229 FuncRequest cmd1 = cmd;
1230 cmd1.x = cmd.x - start_x;
1231 cmd1.y = cmd.y - cursor.y() + bv->top_y();
1232 tli->dispatch(cmd1);
1236 // The test for not selection possible is needed, that only motion
1237 // events are used, where the bottom press event was on
1238 // the drawing area too
1239 if (!selection_possible) {
1240 lyxerr[Debug::ACTION]
1241 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1245 RowList::iterator cursorrow = bv->text->cursorRow();
1246 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->top_y());
1248 // sorry for this but I have a strange error that the y value jumps at
1249 // a certain point. This seems like an error in my xforms library or
1250 // in some other local environment, but I would like to leave this here
1251 // for the moment until I can remove this (Jug 20020418)
1252 if (y_before < bv->text->cursor.y())
1253 lyxerr << y_before << ':'
1254 << bv->text->cursor.y() << endl;
1256 // This is to allow jumping over large insets
1257 if (cursorrow == bv->text->cursorRow()) {
1258 if (cmd.y >= bv->workHeight())
1259 bv->text->cursorDown(false);
1261 bv->text->cursorUp(false);
1264 bv->text->setSelection();
1269 // Single-click on work area
1270 case LFUN_MOUSE_PRESS:
1275 // ok ok, this is a hack (for xforms)
1276 // We shouldn't go further down as we really should only do the
1277 // scrolling and be done with this. Otherwise we may open some
1278 // dialogs (Jug 20020424).
1279 if (cmd.button() == mouse_button::button4) {
1280 bv->scroll(-lyxrc.wheel_jump);
1283 if (cmd.button() == mouse_button::button5) {
1284 bv->scroll(lyxrc.wheel_jump);
1290 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1292 // Middle button press pastes if we have a selection
1293 // We do this here as if the selection was inside an inset
1294 // it could get cleared on the unlocking of the inset so
1295 // we have to check this first
1296 bool paste_internally = false;
1297 if (cmd.button() == mouse_button::button2 && selection.set()) {
1298 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1299 paste_internally = true;
1302 int const screen_first = bv->top_y();
1304 if (bv->theLockingInset()) {
1305 // We are in inset locking mode
1307 // Check whether the inset was hit. If not reset mode,
1308 // otherwise give the event to the inset
1309 if (inset_hit == bv->theLockingInset()) {
1310 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1311 bv->theLockingInset()->dispatch(cmd1);
1314 bv->unlockInset(bv->theLockingInset());
1318 selection_possible = true;
1320 // Clear the selection
1321 bv->text->clearSelection();
1323 bv->updateScrollbar();
1325 // Single left click in math inset?
1326 if (isHighlyEditableInset(inset_hit)) {
1327 // Highly editable inset, like math
1328 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1329 selection_possible = false;
1330 bv->owner()->message(inset->editMessage());
1331 // We just have to lock the inset before calling a PressEvent on it!
1332 if (!bv->lockInset(inset))
1333 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1334 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1335 inset->dispatch(cmd1);
1338 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1340 // Right click on a footnote flag opens float menu
1341 if (cmd.button() == mouse_button::button3) {
1342 selection_possible = false;
1346 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1347 bv->text->setCursorFromCoordinates(x, y + screen_first);
1349 bv->text->selection.cursor = bv->text->cursor;
1350 bv->text->cursor.x_fix(bv->text->cursor.x());
1352 if (bv->fitCursor())
1353 selection_possible = false;
1355 // Insert primary selection with middle mouse
1356 // if there is a local selection in the current buffer,
1358 if (cmd.button() == mouse_button::button2) {
1359 if (paste_internally)
1360 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1362 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1363 selection_possible = false;
1368 case LFUN_MOUSE_RELEASE:
1370 // do nothing if we used the mouse wheel
1374 if (cmd.button() == mouse_button::button4
1375 || cmd.button() == mouse_button::button5)
1378 // If we hit an inset, we have the inset coordinates in these
1379 // and inset_hit points to the inset. If we do not hit an
1380 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1383 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1385 if (bv->theLockingInset()) {
1386 // We are in inset locking mode.
1388 // LyX does a kind of work-area grabbing for insets.
1389 // Only a ButtonPress FuncRequest outside the inset will
1390 // force a insetUnlock.
1391 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1392 bv->theLockingInset()->dispatch(cmd1);
1396 selection_possible = false;
1398 if (cmd.button() == mouse_button::button2)
1402 if (cmd.button() == mouse_button::button1)
1403 bv->haveSelection(selection.set());
1406 bv->owner()->view_state_changed();
1407 bv->owner()->updateMenubar();
1408 bv->owner()->updateToolbar();
1410 // Did we hit an editable inset?
1412 selection_possible = false;
1414 // if we reach this point with a selection, it
1415 // must mean we are currently selecting.
1416 // But we don't want to open the inset
1417 // because that is annoying for the user.
1418 // So just pretend we didn't hit it.
1419 // this is OK because a "kosher" ButtonRelease
1420 // will follow a ButtonPress that clears
1422 // Note this also fixes selection drawing
1423 // problems if we end up opening an inset
1424 if (selection.set())
1427 // CHECK fix this proper in 0.13
1428 // well, maybe 13.0 !!!!!!!!!
1430 // Following a ref shouldn't issue
1431 // a push on the undo-stack
1432 // anylonger, now that we have
1433 // keybindings for following
1434 // references and returning from
1435 // references. IMHO though, it
1436 // should be the inset's own business
1437 // to push or not push on the undo
1438 // stack. They don't *have* to
1439 // alter the document...
1441 // ...or maybe the recordUndo()
1442 // below isn't necessary at all anylonger?
1443 if (inset_hit->lyxCode() == InsetOld::REF_CODE)
1444 recUndo(cursor.par());
1446 bv->owner()->message(inset_hit->editMessage());
1448 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1449 inset_hit->dispatch(cmd1);
1455 case LFUN_SELFINSERT: {
1456 if (cmd.argument.empty())
1459 // Automatically delete the currently selected
1460 // text and replace it with what is being
1461 // typed in now. Depends on lyxrc settings
1462 // "auto_region_delete", which defaults to
1465 if (lyxrc.auto_region_delete) {
1466 if (selection.set())
1467 cutSelection(false, false);
1468 bv->haveSelection(false);
1471 bv->beforeChange(this);
1472 LyXFont const old_font = real_current_font;
1474 string::const_iterator cit = cmd.argument.begin();
1475 string::const_iterator end = cmd.argument.end();
1476 for (; cit != end; ++cit)
1477 bv->owner()->getIntl().getTransManager().
1478 TranslateAndInsert(*cit, this);
1480 selection.cursor = cursor;
1481 moveCursorUpdate(bv, false);
1483 // real_current_font.number can change so we need to
1484 // update the minibuffer
1485 if (old_font != real_current_font)
1486 bv->owner()->view_state_changed();
1487 bv->updateScrollbar();
1491 case LFUN_HTMLURL: {
1492 InsetCommandParams p("htmlurl");
1493 string const data = InsetCommandMailer::params2string("url", p);
1494 bv->owner()->getDialogs().show("url", data, 0);
1499 InsetCommandParams p("url");
1500 string const data = InsetCommandMailer::params2string("url", p);
1501 bv->owner()->getDialogs().show("url", data, 0);
1507 case LFUN_INSET_LIST:
1508 case LFUN_INSET_THEOREM:
1509 case LFUN_INSET_CAPTION:
1511 case LFUN_INSERT_NOTE:
1512 case LFUN_INSERT_BOX:
1513 case LFUN_INSERT_BRANCH:
1514 case LFUN_INSERT_BIBITEM:
1515 case LFUN_INSET_ERT:
1516 case LFUN_INSET_FLOAT:
1517 case LFUN_INSET_FOOTNOTE:
1518 case LFUN_INSET_MARGINAL:
1519 case LFUN_INSET_MINIPAGE:
1520 case LFUN_INSET_OPTARG:
1521 case LFUN_INSET_WIDE_FLOAT:
1522 case LFUN_INSET_WRAP:
1523 case LFUN_TABULAR_INSERT:
1524 case LFUN_ENVIRONMENT_INSERT:
1525 // Open the inset, and move the current selection
1527 doInsertInset(this, cmd, true, true);
1530 case LFUN_INDEX_INSERT:
1531 // Just open the inset
1532 doInsertInset(this, cmd, true, false);
1535 case LFUN_INDEX_PRINT:
1536 case LFUN_TOC_INSERT:
1539 doInsertInset(this, cmd, false, false);
1542 case LFUN_DEPTH_MIN:
1543 bv_funcs::changeDepth(bv, this, bv_funcs::DEC_DEPTH, false);
1548 case LFUN_DEPTH_PLUS:
1550 bv_funcs::changeDepth(bv, this, bv_funcs::INC_DEPTH, false);
1556 return UNDISPATCHED;