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"
34 #include "undo_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 y = getRowNearY(y, dummypit)->y();
318 setCursorFromCoordinates(cursor.x_fix(), y);
319 // + bv->workHeight());
323 if (rit == bv_owner->text->cursorRow()) {
324 // we have a row which is taller than the workarea. The
325 // simplest solution is to move to the next row instead.
328 // This is what we used to do, so we wouldn't skip right past
329 // tall rows, but it's not working right now.
331 new_y = bv->top_y() + bv->workHeight();
335 new_y = bv()->text->cursor.y()
336 + bv()->theLockingInset()->insetInInsetY()
337 + y - rit->baseline();
339 new_y = cursor.y() - cursorRow()->baseline();
343 ParagraphList::iterator pit = cursorPar();
346 setCursor(cur, parOffset(pit), rit->pos(), false);
347 if (cur.y() < bv_owner->top_y() + bv()->workHeight())
349 bv()->updateScrollbar();
355 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
358 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
359 replaceSelection(lt);
360 if (!bv->insertInset(new_inset))
363 bv->updateInset(new_inset);
367 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
368 bool edit, bool pastesel)
370 InsetOld * inset = createInset(cmd);
371 BufferView * bv = cmd.view();
375 if (lt->selection.set()) {
376 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
379 if (bv->insertInset(inset)) {
381 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
382 inset->localDispatch(cmd);
384 if (gotsel && pastesel)
385 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
394 InsetOld::RESULT LyXText::dispatch(FuncRequest const & cmd)
396 lyxerr[Debug::ACTION] << "LyXText::dispatch: action[" << cmd.action
397 <<"] arg[" << cmd.argument << ']' << "xy[" <<
398 cmd.x << ',' << cmd.y << ']' << endl;
400 BufferView * bv = cmd.view();
402 switch (cmd.action) {
404 case LFUN_APPENDIX: {
405 ParagraphList::iterator pit = cursorPar();
406 bool start = !pit->params().startOfAppendix();
408 // ensure that we have only one start_of_appendix in this document
409 ParagraphList::iterator tmp = ownerParagraphs().begin();
410 ParagraphList::iterator end = ownerParagraphs().end();
412 for (; tmp != end; ++tmp) {
413 if (tmp->params().startOfAppendix()) {
414 recordUndo(bv, Undo::ATOMIC, tmp);
415 tmp->params().startOfAppendix(false);
421 recordUndo(bv, Undo::ATOMIC, pit);
422 pit->params().startOfAppendix(start);
424 // we can set the refreshing parameters now
426 redoParagraph(cursorPar());
427 setCursor(cursorPar(), cursor.pos());
432 case LFUN_DELETE_WORD_FORWARD:
433 bv->beforeChange(this);
438 case LFUN_DELETE_WORD_BACKWARD:
439 bv->beforeChange(this);
440 deleteWordBackward();
444 case LFUN_DELETE_LINE_FORWARD:
445 bv->beforeChange(this);
451 if (!selection.mark())
452 bv->beforeChange(this);
453 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
456 cursorRightOneWord();
461 if (!selection.mark())
462 bv->beforeChange(this);
463 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
464 cursorRightOneWord();
470 case LFUN_BEGINNINGBUF:
471 if (!selection.mark())
472 bv->beforeChange(this);
478 if (selection.mark())
479 bv->beforeChange(this);
485 if (!selection.set())
486 selection.cursor = cursor;
487 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
491 finishChange(bv, true);
495 if (!selection.set())
496 selection.cursor = cursor;
497 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
501 finishChange(bv, true);
505 if (!selection.set())
506 selection.cursor = cursor;
508 finishChange(bv, true);
512 if (!selection.set())
513 selection.cursor = cursor;
515 finishChange(bv, true);
518 case LFUN_UP_PARAGRAPHSEL:
519 if (!selection.set())
520 selection.cursor = cursor;
522 finishChange(bv, true);
525 case LFUN_DOWN_PARAGRAPHSEL:
526 if (!selection.set())
527 selection.cursor = cursor;
528 cursorDownParagraph();
529 finishChange(bv, true);
533 if (!selection.set())
534 selection.cursor = cursor;
536 finishChange(bv, true);
540 if (!selection.set())
541 selection.cursor = cursor;
543 finishChange(bv, true);
547 if (!selection.set())
548 selection.cursor = cursor;
550 finishChange(bv, true);
554 if (!selection.set())
555 selection.cursor = cursor;
557 finishChange(bv, true);
560 case LFUN_WORDRIGHTSEL:
561 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
564 cursorRightOneWord();
565 finishChange(bv, true);
568 case LFUN_WORDLEFTSEL:
569 if (cursorPar()->isRightToLeftPar(bv->buffer()->params()))
570 cursorRightOneWord();
573 finishChange(bv, true);
577 LyXCursor cur1 = cursor;
579 ::getWord(*this, cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs());
580 setCursor(cur1.par(), cur1.pos());
581 bv->beforeChange(this);
582 setCursor(cur2.par(), cur2.pos());
583 finishChange(bv, true);
588 bool is_rtl = cursorPar()->isRightToLeftPar(bv->buffer()->params());
589 if (!selection.mark())
590 bv->beforeChange(this);
593 if (cursor.pos() < cursorPar()->size()
594 && cursorPar()->isInset(cursor.pos())
595 && isHighlyEditableInset(cursorPar()->getInset(cursor.pos()))) {
596 InsetOld * tmpinset = cursorPar()->getInset(cursor.pos());
597 cmd.message(tmpinset->editMessage());
598 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
599 tmpinset->localDispatch(cmd1);
609 // This is soooo ugly. Isn`t it possible to make
611 bool const is_rtl = cursorPar()->isRightToLeftPar(bv->buffer()->params());
612 if (!selection.mark())
613 bv->beforeChange(this);
614 LyXCursor const cur = cursor;
617 if ((is_rtl || cur != cursor) && // only if really moved!
618 cursor.pos() < cursorPar()->size() &&
619 cursorPar()->isInset(cursor.pos()) &&
620 isHighlyEditableInset(cursorPar()->getInset(cursor.pos()))) {
621 InsetOld * tmpinset = cursorPar()->getInset(cursor.pos());
622 cmd.message(tmpinset->editMessage());
623 FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
624 tmpinset->localDispatch(cmd1);
634 if (!selection.mark())
635 bv->beforeChange(this);
641 if (!selection.mark())
642 bv->beforeChange(this);
647 case LFUN_UP_PARAGRAPH:
648 if (!selection.mark())
649 bv->beforeChange(this);
654 case LFUN_DOWN_PARAGRAPH:
655 if (!selection.mark())
656 bv->beforeChange(this);
657 cursorDownParagraph();
658 finishChange(bv, false);
662 if (!selection.mark())
663 bv->beforeChange(this);
665 finishChange(bv, false);
669 if (!selection.mark())
670 bv->beforeChange(this);
672 finishChange(bv, false);
676 if (!selection.mark())
677 bv->beforeChange(this);
679 finishChange(bv, false);
683 if (!selection.mark())
684 bv->beforeChange(this);
686 finishChange(bv, false);
689 case LFUN_BREAKLINE: {
690 lyx::pos_type body = cursorPar()->beginningOfBody();
692 // Not allowed by LaTeX (labels or empty par)
693 if (cursor.pos() <= body)
696 replaceSelection(bv->getLyXText());
697 insertInset(new InsetNewline);
698 setCursor(cursorPar(), cursor.pos());
699 moveCursorUpdate(bv, false);
704 if (!selection.set()) {
706 selection.cursor = cursor;
707 // It is possible to make it a lot faster still
708 // just comment out the line below...
710 cutSelection(true, false);
712 moveCursorUpdate(bv, false);
713 bv->owner()->view_state_changed();
716 case LFUN_DELETE_SKIP:
717 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
718 if (!selection.set()) {
719 if (cursor.pos() == cursorPar()->size()) {
721 ParagraphParameters & params = cursorPar()->params();
722 if (cursor.pos() == 0
723 && !(params.spaceTop() == VSpace (VSpace::NONE))) {
727 params.pagebreakTop(),
728 params.pagebreakBottom(),
729 VSpace(VSpace::NONE),
730 params.spaceBottom(),
733 params.labelWidthString(), 0);
738 selection.cursor = cursor;
742 selection.cursor = cursor;
745 cutSelection(true, false);
752 if (!selection.set()) {
753 if (bv->owner()->getIntl().getTransManager().backspace()) {
755 selection.cursor = cursor;
756 // It is possible to make it a lot faster still
757 // just comment out the line below...
760 cutSelection(true, false);
762 bv->owner()->view_state_changed();
767 case LFUN_BACKSPACE_SKIP:
768 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
769 if (!selection.set()) {
770 ParagraphParameters & params = cursorPar()->params();
771 if (cursor.pos() == 0 && !(params.spaceTop() == VSpace(VSpace::NONE))) {
775 params.pagebreakTop(),
776 params.pagebreakBottom(),
777 VSpace(VSpace::NONE),
778 params.spaceBottom(),
781 params.labelWidthString(), 0);
783 LyXCursor cur = cursor;
785 selection.cursor = cur;
788 cutSelection(true, false);
793 case LFUN_BREAKPARAGRAPH:
794 replaceSelection(bv->getLyXText());
795 breakParagraph(bv->buffer()->paragraphs(), 0);
797 selection.cursor = cursor;
799 bv->owner()->view_state_changed();
802 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
803 replaceSelection(bv->getLyXText());
804 breakParagraph(bv->buffer()->paragraphs(), 1);
806 selection.cursor = cursor;
808 bv->owner()->view_state_changed();
811 case LFUN_BREAKPARAGRAPH_SKIP: {
812 // When at the beginning of a paragraph, remove
813 // indentation and add a "defskip" at the top.
814 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
815 LyXCursor cur = cursor;
816 replaceSelection(bv->getLyXText());
817 if (cur.pos() == 0) {
818 ParagraphParameters & params = getPar(cur)->params();
819 if (params.spaceTop() == VSpace(VSpace::NONE)) {
823 params.pagebreakTop(),
824 params.pagebreakBottom(),
825 VSpace(VSpace::DEFSKIP), params.spaceBottom(),
828 params.labelWidthString(), 1);
832 breakParagraph(bv->buffer()->paragraphs(), 0);
835 selection.cursor = cur;
837 bv->owner()->view_state_changed();
841 case LFUN_PARAGRAPH_SPACING: {
842 ParagraphList::iterator pit = cursorPar();
843 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
844 float cur_value = 1.0;
845 if (cur_spacing == Spacing::Other)
846 cur_value = pit->params().spacing().getValue();
848 istringstream is(cmd.argument);
851 Spacing::Space new_spacing = cur_spacing;
852 float new_value = cur_value;
854 lyxerr << "Missing argument to `paragraph-spacing'"
856 } else if (tmp == "single") {
857 new_spacing = Spacing::Single;
858 } else if (tmp == "onehalf") {
859 new_spacing = Spacing::Onehalf;
860 } else if (tmp == "double") {
861 new_spacing = Spacing::Double;
862 } else if (tmp == "other") {
863 new_spacing = Spacing::Other;
866 lyxerr << "new_value = " << tmpval << endl;
869 } else if (tmp == "default") {
870 new_spacing = Spacing::Default;
872 lyxerr << _("Unknown spacing argument: ")
873 << cmd.argument << endl;
875 if (cur_spacing != new_spacing || cur_value != new_value) {
876 pit->params().spacing(Spacing(new_spacing, new_value));
883 case LFUN_INSET_SETTINGS:
884 BOOST_ASSERT(bv->theLockingInset());
885 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
888 case LFUN_INSET_TOGGLE:
889 bv->beforeChange(this);
895 case LFUN_SPACE_INSERT:
896 if (cursorPar()->layout()->free_spacing)
899 doInsertInset(this, cmd, false, false);
900 moveCursorUpdate(bv, false);
903 case LFUN_HYPHENATION:
904 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
907 case LFUN_LIGATURE_BREAK:
908 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
912 specialChar(this, bv, InsetSpecialChar::LDOTS);
915 case LFUN_END_OF_SENTENCE:
916 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
919 case LFUN_MENU_SEPARATOR:
920 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
924 bv->beforeChange(this);
926 selection.cursor = cursor;
927 cmd.message(N_("Mark off"));
931 bv->beforeChange(this);
932 selection.mark(true);
934 selection.cursor = cursor;
935 cmd.message(N_("Mark on"));
939 bv->beforeChange(this);
940 if (selection.mark()) {
941 cmd.message(N_("Mark removed"));
943 selection.mark(true);
944 cmd.message(N_("Mark set"));
946 selection.cursor = cursor;
950 case LFUN_UPCASE_WORD:
951 changeCase(LyXText::text_uppercase);
955 case LFUN_LOWCASE_WORD:
956 changeCase(LyXText::text_lowercase);
960 case LFUN_CAPITALIZE_WORD:
961 changeCase(LyXText::text_capitalization);
965 case LFUN_TRANSPOSE_CHARS:
966 recordUndo(bv, Undo::ATOMIC, cursorPar());
972 cmd.message(_("Paste"));
973 replaceSelection(bv->getLyXText());
974 #warning FIXME Check if the arg is in the domain of available selections.
975 if (isStrUnsignedInt(cmd.argument))
976 pasteSelection(strToUnsignedInt(cmd.argument));
979 clearSelection(); // bug 393
986 cutSelection(true, true);
987 cmd.message(_("Cut"));
993 cmd.message(_("Copy"));
996 case LFUN_BEGINNINGBUFSEL:
1000 finishChange(bv, true);
1003 case LFUN_ENDBUFSEL:
1005 return UNDISPATCHED;
1007 finishChange(bv, true);
1011 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1017 istringstream is(cmd.argument);
1020 lyxerr << "SETXY: Could not parse coordinates in '"
1021 << cmd.argument << std::endl;
1023 setCursorFromCoordinates(x, y);
1028 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1030 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1036 case LFUN_GETLAYOUT:
1037 cmd.message(tostr(cursorPar()->layout()));
1041 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1042 << cmd.argument << endl;
1044 // This is not the good solution to the empty argument
1045 // problem, but it will hopefully suffice for 1.2.0.
1046 // The correct solution would be to augument the
1047 // function list/array with information about what
1048 // functions needs arguments and their type.
1049 if (cmd.argument.empty()) {
1050 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1054 // Derive layout number from given argument (string)
1055 // and current buffer's textclass (number)
1056 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1057 bool hasLayout = tclass.hasLayout(cmd.argument);
1058 string layout = cmd.argument;
1060 // If the entry is obsolete, use the new one instead.
1062 string const & obs = tclass[layout]->obsoleted_by();
1068 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1073 bool change_layout = (current_layout != layout);
1075 if (!change_layout && selection.set() &&
1076 selection.start.par() != selection.end.par())
1078 ParagraphList::iterator spit = getPar(selection.start);
1079 ParagraphList::iterator epit = boost::next(getPar(selection.end));
1080 while (spit != epit) {
1081 if (spit->layout()->name() != current_layout) {
1082 change_layout = true;
1089 if (change_layout) {
1090 current_layout = layout;
1092 bv->owner()->setLayout(layout);
1099 case LFUN_PASTESELECTION: {
1100 // this was originally a beforeChange(bv->text), i.e
1101 // the outermost LyXText!
1102 bv->beforeChange(this);
1103 string const clip = bv->getClipboard();
1104 if (!clip.empty()) {
1105 if (cmd.argument == "paragraph")
1106 insertStringAsParagraphs(clip);
1108 insertStringAsLines(clip);
1115 case LFUN_GOTOERROR:
1116 gotoInset(InsetOld::ERROR_CODE, false);
1120 gotoInset(InsetOld::NOTE_CODE, false);
1123 case LFUN_REFERENCE_GOTO:
1125 vector<InsetOld::Code> tmp;
1126 tmp.push_back(InsetOld::LABEL_CODE);
1127 tmp.push_back(InsetOld::REF_CODE);
1128 gotoInset(tmp, true);
1133 replaceSelection(bv->getLyXText());
1134 ParagraphList::iterator pit = cursorPar();
1135 lyx::pos_type pos = cursor.pos();
1139 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1142 c = pit->getChar(pos - 1);
1144 LyXLayout_ptr const & style = pit->layout();
1146 BufferParams const & bufparams = bv->buffer()->params();
1147 if (style->pass_thru ||
1148 pit->getFontSettings(bufparams,pos).language()->lang() == "hebrew" ||
1149 !bv->insertInset(new InsetQuotes(c, bufparams)))
1150 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1154 case LFUN_DATE_INSERT: {
1155 replaceSelection(bv->getLyXText());
1156 time_t now_time_t = time(NULL);
1157 struct tm * now_tm = localtime(&now_time_t);
1158 setlocale(LC_TIME, "");
1160 if (!cmd.argument.empty())
1163 arg = lyxrc.date_insert_format;
1165 int const datetmp_len =
1166 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1168 for (int i = 0; i < datetmp_len; i++)
1169 insertChar(datetmp[i]);
1171 selection.cursor = cursor;
1172 moveCursorUpdate(bv, false);
1176 case LFUN_MOUSE_TRIPLE:
1179 if (!isInInset() && bv->theLockingInset())
1181 if (cmd.button() == mouse_button::button1) {
1183 selection.cursor = cursor;
1187 bv->haveSelection(selection.set());
1191 case LFUN_MOUSE_DOUBLE:
1194 if (!isInInset() && bv->theLockingInset())
1196 if (cmd.button() == mouse_button::button1) {
1197 selectWord(lyx::WHOLE_WORD_STRICT);
1199 bv->haveSelection(selection.set());
1203 case LFUN_MOUSE_MOTION:
1205 // Only use motion with button 1
1206 //if (ev.button() != mouse_button::button1)
1212 // Check for inset locking
1213 if (bv->theLockingInset()) {
1214 InsetOld * tli = bv->theLockingInset();
1215 LyXCursor cursor = bv->text->cursor;
1216 LyXFont font = bv->text->getFont(cursorPar(), cursor.pos());
1217 int width = tli->width();
1218 int inset_x = font.isVisibleRightToLeft()
1219 ? cursor.x() - width : cursor.x();
1220 int start_x = inset_x + tli->scroll();
1221 FuncRequest cmd1 = cmd;
1222 cmd1.x = cmd.x - start_x;
1223 cmd1.y = cmd.y - cursor.y() + bv->top_y();
1224 tli->localDispatch(cmd1);
1228 // The test for not selection possible is needed, that only motion
1229 // events are used, where the bottom press event was on
1230 // the drawing area too
1231 if (!selection_possible) {
1232 lyxerr[Debug::ACTION]
1233 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1237 RowList::iterator cursorrow = bv->text->cursorRow();
1238 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->top_y());
1240 // sorry for this but I have a strange error that the y value jumps at
1241 // a certain point. This seems like an error in my xforms library or
1242 // in some other local environment, but I would like to leave this here
1243 // for the moment until I can remove this (Jug 20020418)
1244 if (y_before < bv->text->cursor.y())
1245 lyxerr << y_before << ':'
1246 << bv->text->cursor.y() << endl;
1248 // This is to allow jumping over large insets
1249 if (cursorrow == bv->text->cursorRow()) {
1250 if (cmd.y >= bv->workHeight())
1251 bv->text->cursorDown(false);
1253 bv->text->cursorUp(false);
1256 bv->text->setSelection();
1261 // Single-click on work area
1262 case LFUN_MOUSE_PRESS:
1267 // ok ok, this is a hack (for xforms)
1268 // We shouldn't go further down as we really should only do the
1269 // scrolling and be done with this. Otherwise we may open some
1270 // dialogs (Jug 20020424).
1271 if (cmd.button() == mouse_button::button4) {
1272 bv->scroll(-lyxrc.wheel_jump);
1275 if (cmd.button() == mouse_button::button5) {
1276 bv->scroll(lyxrc.wheel_jump);
1282 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1284 // Middle button press pastes if we have a selection
1285 // We do this here as if the selection was inside an inset
1286 // it could get cleared on the unlocking of the inset so
1287 // we have to check this first
1288 bool paste_internally = false;
1289 if (cmd.button() == mouse_button::button2 && selection.set()) {
1290 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1291 paste_internally = true;
1294 int const screen_first = bv->top_y();
1296 if (bv->theLockingInset()) {
1297 // We are in inset locking mode
1299 // Check whether the inset was hit. If not reset mode,
1300 // otherwise give the event to the inset
1301 if (inset_hit == bv->theLockingInset()) {
1302 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1303 bv->theLockingInset()->localDispatch(cmd1);
1306 bv->unlockInset(bv->theLockingInset());
1310 selection_possible = true;
1312 // Clear the selection
1313 bv->text->clearSelection();
1315 bv->updateScrollbar();
1317 // Single left click in math inset?
1318 if (isHighlyEditableInset(inset_hit)) {
1319 // Highly editable inset, like math
1320 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1321 selection_possible = false;
1322 bv->owner()->message(inset->editMessage());
1323 // We just have to lock the inset before calling a PressEvent on it!
1324 if (!bv->lockInset(inset))
1325 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1326 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1327 inset->localDispatch(cmd1);
1330 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1332 // Right click on a footnote flag opens float menu
1333 if (cmd.button() == mouse_button::button3) {
1334 selection_possible = false;
1338 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1339 bv->text->setCursorFromCoordinates(x, y + screen_first);
1341 bv->text->selection.cursor = bv->text->cursor;
1342 bv->text->cursor.x_fix(bv->text->cursor.x());
1344 if (bv->fitCursor())
1345 selection_possible = false;
1347 // Insert primary selection with middle mouse
1348 // if there is a local selection in the current buffer,
1350 if (cmd.button() == mouse_button::button2) {
1351 if (paste_internally)
1352 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1354 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1355 selection_possible = false;
1360 case LFUN_MOUSE_RELEASE:
1362 // do nothing if we used the mouse wheel
1366 if (cmd.button() == mouse_button::button4
1367 || cmd.button() == mouse_button::button5)
1370 // If we hit an inset, we have the inset coordinates in these
1371 // and inset_hit points to the inset. If we do not hit an
1372 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1375 InsetOld * inset_hit = bv->text->checkInsetHit(x, y);
1377 if (bv->theLockingInset()) {
1378 // We are in inset locking mode.
1380 // LyX does a kind of work-area grabbing for insets.
1381 // Only a ButtonPress FuncRequest outside the inset will
1382 // force a insetUnlock.
1383 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1384 bv->theLockingInset()->localDispatch(cmd1);
1388 selection_possible = false;
1390 if (cmd.button() == mouse_button::button2)
1394 if (cmd.button() == mouse_button::button1)
1395 bv->haveSelection(selection.set());
1398 bv->owner()->view_state_changed();
1399 bv->owner()->updateMenubar();
1400 bv->owner()->updateToolbar();
1402 // Did we hit an editable inset?
1404 selection_possible = false;
1406 // if we reach this point with a selection, it
1407 // must mean we are currently selecting.
1408 // But we don't want to open the inset
1409 // because that is annoying for the user.
1410 // So just pretend we didn't hit it.
1411 // this is OK because a "kosher" ButtonRelease
1412 // will follow a ButtonPress that clears
1414 // Note this also fixes selection drawing
1415 // problems if we end up opening an inset
1416 if (selection.set())
1419 // CHECK fix this proper in 0.13
1420 // well, maybe 13.0 !!!!!!!!!
1422 // Following a ref shouldn't issue
1423 // a push on the undo-stack
1424 // anylonger, now that we have
1425 // keybindings for following
1426 // references and returning from
1427 // references. IMHO though, it
1428 // should be the inset's own business
1429 // to push or not push on the undo
1430 // stack. They don't *have* to
1431 // alter the document...
1433 // ...or maybe the recordUndo()
1434 // below isn't necessary at all anylonger?
1435 if (inset_hit->lyxCode() == InsetOld::REF_CODE)
1436 recordUndo(bv, Undo::ATOMIC);
1438 bv->owner()->message(inset_hit->editMessage());
1440 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1441 inset_hit->localDispatch(cmd1);
1447 case LFUN_SELFINSERT: {
1448 if (cmd.argument.empty())
1451 // Automatically delete the currently selected
1452 // text and replace it with what is being
1453 // typed in now. Depends on lyxrc settings
1454 // "auto_region_delete", which defaults to
1457 if (lyxrc.auto_region_delete) {
1458 if (selection.set())
1459 cutSelection(false, false);
1460 bv->haveSelection(false);
1463 bv->beforeChange(this);
1464 LyXFont const old_font = real_current_font;
1466 string::const_iterator cit = cmd.argument.begin();
1467 string::const_iterator end = cmd.argument.end();
1468 for (; cit != end; ++cit)
1469 bv->owner()->getIntl().getTransManager().
1470 TranslateAndInsert(*cit, this);
1472 selection.cursor = cursor;
1473 moveCursorUpdate(bv, false);
1475 // real_current_font.number can change so we need to
1476 // update the minibuffer
1477 if (old_font != real_current_font)
1478 bv->owner()->view_state_changed();
1479 bv->updateScrollbar();
1483 case LFUN_HTMLURL: {
1484 InsetCommandParams p("htmlurl");
1485 string const data = InsetCommandMailer::params2string("url", p);
1486 bv->owner()->getDialogs().show("url", data, 0);
1491 InsetCommandParams p("url");
1492 string const data = InsetCommandMailer::params2string("url", p);
1493 bv->owner()->getDialogs().show("url", data, 0);
1499 case LFUN_INSET_LIST:
1500 case LFUN_INSET_THEOREM:
1501 case LFUN_INSET_CAPTION:
1503 case LFUN_INSERT_NOTE:
1504 case LFUN_INSERT_BOX:
1505 case LFUN_INSERT_BRANCH:
1506 case LFUN_INSERT_BIBITEM:
1507 case LFUN_INSET_ERT:
1508 case LFUN_INSET_FLOAT:
1509 case LFUN_INSET_FOOTNOTE:
1510 case LFUN_INSET_MARGINAL:
1511 case LFUN_INSET_MINIPAGE:
1512 case LFUN_INSET_OPTARG:
1513 case LFUN_INSET_WIDE_FLOAT:
1514 case LFUN_INSET_WRAP:
1515 case LFUN_TABULAR_INSERT:
1516 case LFUN_ENVIRONMENT_INSERT:
1517 // Open the inset, and move the current selection
1519 doInsertInset(this, cmd, true, true);
1522 case LFUN_INDEX_INSERT:
1523 // Just open the inset
1524 doInsertInset(this, cmd, true, false);
1527 case LFUN_INDEX_PRINT:
1528 case LFUN_TOC_INSERT:
1531 doInsertInset(this, cmd, false, false);
1534 case LFUN_DEPTH_MIN:
1535 bv_funcs::changeDepth(bv, this, bv_funcs::DEC_DEPTH, false);
1540 case LFUN_DEPTH_PLUS:
1542 bv_funcs::changeDepth(bv, this, bv_funcs::INC_DEPTH, false);
1548 return UNDISPATCHED;