1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2002 The LyX Team.
9 * ====================================================== */
15 #include "paragraph.h"
16 #include "BufferView.h"
17 #include "funcrequest.h"
20 #include "bufferparams.h"
22 #include "ParagraphParameters.h"
28 #include "support/lstrings.h"
29 #include "frontends/LyXView.h"
30 #include "frontends/screen.h"
31 #include "frontends/Dialogs.h"
32 #include "insets/insetspecialchar.h"
33 #include "insets/insettext.h"
34 #include "insets/insetquotes.h"
35 #include "insets/insetcommand.h"
36 #include "insets/insetnewline.h"
37 #include "undo_funcs.h"
38 #include "text_funcs.h"
47 extern string current_layout;
48 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
50 // the selection possible is needed, that only motion events are
51 // used, where the bottom press event was on the drawing area too
52 bool selection_possible = false;
57 void moveCursorUpdate(BufferView * bv, bool selecting)
59 LyXText * lt = bv->getLyXText();
61 if (selecting || lt->selection.mark()) {
64 bv->updateInset(lt->inset_owner);
68 if (!lt->isInInset()) {
69 bv->update(lt, BufferView::SELECT);
71 } else if (bv->text->refreshStatus() != LyXText::REFRESH_NONE) {
72 bv->theLockingInset()->hideInsetCursor(bv);
73 bv->update(BufferView::SELECT);
77 if (!lt->selection.set())
78 bv->haveSelection(false);
84 void finishChange(BufferView * bv, bool selecting = false)
87 moveCursorUpdate(bv, selecting);
88 bv->owner()->view_state_changed();
91 // check if the given co-ordinates are inside an inset at the
92 // given cursor, if one exists. If so, the inset is returned,
93 // and the co-ordinates are made relative. Otherwise, 0 is returned.
94 Inset * checkInset(BufferView * bv, LyXText & text,
95 LyXCursor const & cur, int & x, int & y)
97 lyx::pos_type const pos = cur.pos();
98 Paragraph /*const*/ & par = *cur.par();
100 if (pos >= par.size() || !par.isInset(pos))
103 Inset /*const*/ * inset = par.getInset(pos);
105 if (!isEditableInset(inset))
108 // get inset dimensions
109 lyx::Assert(par.getInset(pos));
111 LyXFont const & font = text.getFont(bv->buffer(), &par, pos);
113 int const width = inset->width(bv, font);
114 int const inset_x = font.isVisibleRightToLeft()
115 ? (cur.ix() - width) : cur.ix();
118 inset_x + inset->scroll(),
120 cur.iy() - inset->ascent(bv, font),
121 cur.iy() + inset->descent(bv, font)
124 if (!b.contained(x, y)) {
125 lyxerr[Debug::GUI] << "Missed inset at x,y "
127 << " box " << b << endl;
131 text.setCursor(&par, pos, true);
134 // The origin of an inset is on the baseline
135 y -= text.cursor.iy();
143 Inset * LyXText::checkInsetHit(int & x, int & y)
145 int y_tmp = y + top_y();
148 setCursorFromCoordinates(cur, x, y_tmp);
150 Inset * inset = checkInset(bv(), *this, cur, x, y_tmp);
156 // look at previous position
161 setCursor(cur, cur.par(), cur.pos() - 1, true);
163 inset = checkInset(bv(), *this, cur, x, y_tmp);
170 bool LyXText::gotoNextInset(vector<Inset::Code> const & codes,
171 string const & contents)
173 LyXCursor res = cursor;
176 if (res.pos() < res.par()->size() - 1) {
177 res.pos(res.pos() + 1);
179 res.par(res.par()->next());
183 } while (res.par() &&
184 !(res.par()->isInset(res.pos())
185 && (inset = res.par()->getInset(res.pos())) != 0
186 && find(codes.begin(), codes.end(), inset->lyxCode())
188 && (contents.empty() ||
189 static_cast<InsetCommand *>(
190 res.par()->getInset(res.pos()))->getContents()
194 setCursor(res.par(), res.pos(), false);
201 void LyXText::gotoInset(vector<Inset::Code> const & codes,
205 bv()->beforeChange(this);
209 if (same_content && cursor.par()->isInset(cursor.pos())) {
210 Inset const * inset = cursor.par()->getInset(cursor.pos());
211 if (find(codes.begin(), codes.end(), inset->lyxCode())
213 contents = static_cast<InsetCommand const *>(inset)->getContents();
216 if (!gotoNextInset(codes, contents)) {
217 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
218 LyXCursor tmp = cursor;
219 cursor.par(&*ownerParagraphs().begin());
221 if (!gotoNextInset(codes, contents)) {
223 bv()->owner()->message(_("No more insets"));
226 bv()->owner()->message(_("No more insets"));
230 selection.cursor = cursor;
234 void LyXText::gotoInset(Inset::Code code, bool same_content)
236 gotoInset(vector<Inset::Code>(1, code), same_content);
240 void LyXText::cursorPrevious()
244 if (cursor.row() == rows().begin()) {
246 int new_y = bv()->text->top_y() - bv()->workHeight();
247 bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
248 bv()->updateScrollbar();
253 RowList::iterator cursorrow = cursor.row();
255 setCursorFromCoordinates(cursor.x_fix(), y);
259 if (cursorrow == bv()->text->cursor.row()) {
260 // we have a row which is taller than the workarea. The
261 // simplest solution is to move to the previous row instead.
264 // This is what we used to do, so we wouldn't skip right past
265 // tall rows, but it's not working right now.
267 new_y = bv->text->top_y() - bv->workHeight();
271 new_y = bv()->text->cursor.iy()
272 + bv()->theLockingInset()->insetInInsetY() + y
273 + cursor.row()->height()
274 - bv()->workHeight() + 1;
277 - cursor.row()->baseline()
278 + cursor.row()->height()
279 - bv()->workHeight() + 1;
282 bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
283 if (cursor.row() != rows().begin()) {
285 setCursor(cur, &*boost::prior(cursor.row())->par(),
286 boost::prior(cursor.row())->pos(), false);
287 if (cur.y() > top_y()) {
291 bv()->updateScrollbar();
295 void LyXText::cursorNext()
299 if (boost::next(cursor.row()) == rows().end()) {
300 int y = cursor.y() - cursor.row()->baseline() +
301 cursor.row()->height();
302 if (y > topy + bv()->workHeight()) {
303 bv()->screen().draw(bv()->text, bv(), bv()->text->top_y() + bv()->workHeight());
304 bv()->updateScrollbar();
309 int y = topy + bv()->workHeight();
310 if (inset_owner && !topy) {
311 y -= (bv()->text->cursor.iy()
312 - bv()->text->top_y()
313 + bv()->theLockingInset()->insetInInsetY());
318 RowList::iterator cursorrow = cursor.row();
319 setCursorFromCoordinates(cursor.x_fix(), y);
320 // + bv->workHeight());
324 if (cursorrow == bv()->text->cursor.row()) {
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->text->top_y() + bv->workHeight();
336 new_y = bv()->text->cursor.iy()
337 + bv()->theLockingInset()->insetInInsetY()
338 + y - cursor.row()->baseline();
340 new_y = cursor.y() - cursor.row()->baseline();
343 bv()->screen().draw(bv()->text, bv(), new_y);
344 if (boost::next(cursor.row()) != rows().end()) {
346 setCursor(cur, &*boost::next(cursor.row())->par(),
347 boost::next(cursor.row())->pos(), false);
348 if (cur.y() < top_y() + bv()->workHeight()) {
352 bv()->updateScrollbar();
356 void LyXText::update()
358 bv()->update(this, BufferView::SELECT);
363 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
367 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
368 if (!bv->insertInset(new_inset))
371 bv->updateInset(new_inset);
375 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
376 bool edit, bool pastesel)
378 Inset * inset = createInset(cmd);
379 BufferView * bv = cmd.view();
383 if (lt->selection.set()) {
384 lt->cutSelection(true, false);
387 if (bv->insertInset(inset)) {
390 if (gotsel && pastesel)
391 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
400 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
402 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
403 <<"] arg[" << cmd.argument << ']' << endl;
405 BufferView * bv = cmd.view();
407 switch (cmd.action) {
409 case LFUN_APPENDIX: {
410 Paragraph * par = cursor.par();
411 bool start = !par->params().startOfAppendix();
413 // ensure that we have only one start_of_appendix in this document
414 ParagraphList::iterator tmp = ownerParagraphs().begin();
415 ParagraphList::iterator end = ownerParagraphs().end();
417 for (; tmp != end; ++tmp) {
418 if (tmp->params().startOfAppendix()) {
419 setUndo(bv, Undo::EDIT, &*tmp, &*boost::next(tmp));
420 tmp->params().startOfAppendix(false);
422 setHeightOfRow(getRow(&*tmp, 0, tmpy));
427 setUndo(bv, Undo::EDIT, par, par->next());
428 par->params().startOfAppendix(start);
430 // we can set the refreshing parameters now
432 redoHeightOfParagraph();
434 setCursor(cursor.par(), cursor.pos());
439 case LFUN_DELETE_WORD_FORWARD:
440 bv->beforeChange(this);
447 case LFUN_DELETE_WORD_BACKWARD:
448 bv->beforeChange(this);
450 deleteWordBackward();
455 case LFUN_DELETE_LINE_FORWARD:
456 bv->beforeChange(this);
464 if (!selection.mark())
465 bv->beforeChange(this);
467 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
470 cursorRightOneWord();
475 if (!selection.mark())
476 bv->beforeChange(this);
478 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
479 cursorRightOneWord();
485 case LFUN_BEGINNINGBUF:
486 if (!selection.mark())
487 bv->beforeChange(this);
494 if (selection.mark())
495 bv->beforeChange(this);
503 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
507 finishChange(bv, true);
512 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
516 finishChange(bv, true);
522 finishChange(bv, true);
528 finishChange(bv, true);
531 case LFUN_UP_PARAGRAPHSEL:
534 finishChange(bv, true);
537 case LFUN_DOWN_PARAGRAPHSEL:
539 cursorDownParagraph();
540 finishChange(bv, true);
546 finishChange(bv, true);
552 finishChange(bv, true);
558 finishChange(bv, true);
564 finishChange(bv, true);
567 case LFUN_WORDRIGHTSEL:
569 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
572 cursorRightOneWord();
573 finishChange(bv, true);
576 case LFUN_WORDLEFTSEL:
578 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
579 cursorRightOneWord();
582 finishChange(bv, true);
589 getWord(cur1, cur2, WHOLE_WORD);
590 setCursor(cur1.par(), cur1.pos());
591 bv->beforeChange(this);
592 setCursor(cur2.par(), cur2.pos());
593 finishChange(bv, true);
598 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
599 if (!selection.mark())
600 bv->beforeChange(this);
604 if (cursor.pos() < cursor.par()->size()
605 && cursor.par()->isInset(cursor.pos())
606 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
607 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
608 cmd.message(tmpinset->editMessage());
609 tmpinset->edit(bv, !is_rtl);
619 // This is soooo ugly. Isn`t it possible to make
621 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
622 if (!selection.mark())
623 bv->beforeChange(this);
625 LyXCursor const cur = cursor;
628 if ((is_rtl || cur != cursor) && // only if really moved!
629 cursor.pos() < cursor.par()->size() &&
630 cursor.par()->isInset(cursor.pos()) &&
631 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
632 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
633 cmd.message(tmpinset->editMessage());
634 tmpinset->edit(bv, is_rtl);
644 if (!selection.mark())
645 bv->beforeChange(this);
646 bv->update(this, BufferView::UPDATE);
652 if (!selection.mark())
653 bv->beforeChange(this);
654 bv->update(this, BufferView::UPDATE);
659 case LFUN_UP_PARAGRAPH:
660 if (!selection.mark())
661 bv->beforeChange(this);
662 bv->update(this, BufferView::UPDATE);
667 case LFUN_DOWN_PARAGRAPH:
668 if (!selection.mark())
669 bv->beforeChange(this);
670 bv->update(this, BufferView::UPDATE);
671 cursorDownParagraph();
672 finishChange(bv, false);
676 if (!selection.mark())
677 bv->beforeChange(this);
678 bv->update(this, BufferView::UPDATE);
680 finishChange(bv, false);
683 // moveCursorUpdate(bv, false, false);
684 // owner_->view_state_changed();
688 if (!selection.mark())
689 bv->beforeChange(this);
690 bv->update(this, BufferView::UPDATE);
692 finishChange(bv, false);
696 if (!selection.mark())
697 bv->beforeChange(this);
700 finishChange(bv, false);
704 if (!selection.mark())
705 bv->beforeChange(this);
708 finishChange(bv, false);
711 case LFUN_BREAKLINE: {
712 lyx::pos_type body = cursor.par()->beginningOfBody();
714 // Not allowed by LaTeX (labels or empty par)
715 if (cursor.pos() <= body)
718 bv->beforeChange(this);
719 insertInset(new InsetNewline);
721 setCursor(cursor.par(), cursor.pos());
722 moveCursorUpdate(bv, false);
727 if (!selection.set()) {
729 selection.cursor = cursor;
731 // It is possible to make it a lot faster still
732 // just comment out the line below...
736 cutSelection(bv, true);
739 moveCursorUpdate(bv, false);
740 bv->owner()->view_state_changed();
744 case LFUN_DELETE_SKIP:
745 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
746 if (!selection.set()) {
747 LyXCursor cur = cursor;
748 if (cur.pos() == cur.par()->size()) {
752 && !(cur.par()->params().spaceTop()
753 == VSpace (VSpace::NONE))) {
755 cur.par()->params().lineTop(),
756 cur.par()->params().lineBottom(),
757 cur.par()->params().pagebreakTop(),
758 cur.par()->params().pagebreakBottom(),
759 VSpace(VSpace::NONE),
760 cur.par()->params().spaceBottom(),
761 cur.par()->params().spacing(),
762 cur.par()->params().align(),
763 cur.par()->params().labelWidthString(), 0);
769 selection.cursor = cursor;
773 selection.cursor = cursor;
777 cutSelection(bv, true);
784 if (!selection.set()) {
785 if (bv->owner()->getIntl().getTransManager().backspace()) {
787 selection.cursor = cursor;
789 // It is possible to make it a lot faster still
790 // just comment out the line below...
795 cutSelection(bv, true);
798 bv->owner()->view_state_changed();
802 case LFUN_BACKSPACE_SKIP:
803 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
804 if (!selection.set()) {
805 LyXCursor cur = cursor;
807 && !(cur.par()->params().spaceTop()
808 == VSpace (VSpace::NONE))) {
810 cur.par()->params().lineTop(),
811 cur.par()->params().lineBottom(),
812 cur.par()->params().pagebreakTop(),
813 cur.par()->params().pagebreakBottom(),
814 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
815 cur.par()->params().spacing(),
816 cur.par()->params().align(),
817 cur.par()->params().labelWidthString(), 0);
820 selection.cursor = cur;
824 cutSelection(bv, true);
829 case LFUN_BREAKPARAGRAPH:
830 bv->beforeChange(this);
831 breakParagraph(bv->buffer()->paragraphs, 0);
833 selection.cursor = cursor;
835 bv->owner()->view_state_changed();
838 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
839 bv->beforeChange(this);
840 breakParagraph(bv->buffer()->paragraphs, 1);
842 selection.cursor = cursor;
844 bv->owner()->view_state_changed();
847 case LFUN_BREAKPARAGRAPH_SKIP: {
848 // When at the beginning of a paragraph, remove
849 // indentation and add a "defskip" at the top.
850 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
851 LyXCursor cur = cursor;
852 bv->beforeChange(this);
853 if (cur.pos() == 0) {
854 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
856 cur.par()->params().lineTop(),
857 cur.par()->params().lineBottom(),
858 cur.par()->params().pagebreakTop(),
859 cur.par()->params().pagebreakBottom(),
860 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
861 cur.par()->params().spacing(),
862 cur.par()->params().align(),
863 cur.par()->params().labelWidthString(), 1);
867 breakParagraph(bv->buffer()->paragraphs, 0);
870 selection.cursor = cur;
872 bv->owner()->view_state_changed();
876 case LFUN_PARAGRAPH_SPACING: {
877 Paragraph * par = cursor.par();
878 Spacing::Space cur_spacing = par->params().spacing().getSpace();
879 float cur_value = 1.0;
880 if (cur_spacing == Spacing::Other)
881 cur_value = par->params().spacing().getValue();
883 istringstream is(cmd.argument.c_str());
886 Spacing::Space new_spacing = cur_spacing;
887 float new_value = cur_value;
889 lyxerr << "Missing argument to `paragraph-spacing'"
891 } else if (tmp == "single") {
892 new_spacing = Spacing::Single;
893 } else if (tmp == "onehalf") {
894 new_spacing = Spacing::Onehalf;
895 } else if (tmp == "double") {
896 new_spacing = Spacing::Double;
897 } else if (tmp == "other") {
898 new_spacing = Spacing::Other;
901 lyxerr << "new_value = " << tmpval << endl;
904 } else if (tmp == "default") {
905 new_spacing = Spacing::Default;
907 lyxerr << _("Unknown spacing argument: ")
908 << cmd.argument << endl;
910 if (cur_spacing != new_spacing || cur_value != new_value) {
911 par->params().spacing(Spacing(new_spacing, new_value));
918 case LFUN_INSET_TOGGLE:
920 bv->beforeChange(this);
927 case LFUN_PROTECTEDSPACE:
928 if (cursor.par()->layout()->free_spacing) {
932 specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
934 moveCursorUpdate(bv, false);
937 case LFUN_HYPHENATION:
938 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
941 case LFUN_LIGATURE_BREAK:
942 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
946 specialChar(this, bv, InsetSpecialChar::LDOTS);
949 case LFUN_END_OF_SENTENCE:
950 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
953 case LFUN_MENU_SEPARATOR:
954 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
958 bv->beforeChange(this);
960 selection.cursor = cursor;
961 cmd.message(N_("Mark off"));
965 bv->beforeChange(this);
966 selection.mark(true);
968 selection.cursor = cursor;
969 cmd.message(N_("Mark on"));
973 bv->beforeChange(this);
974 if (selection.mark()) {
976 cmd.message(N_("Mark removed"));
978 selection.mark(true);
980 cmd.message(N_("Mark set"));
982 selection.cursor = cursor;
985 case LFUN_UPCASE_WORD:
987 changeCase(LyXText::text_uppercase);
989 bv->updateInset(inset_owner);
993 case LFUN_LOWCASE_WORD:
995 changeCase(LyXText::text_lowercase);
997 bv->updateInset(inset_owner);
1001 case LFUN_CAPITALIZE_WORD:
1003 changeCase(LyXText::text_capitalization);
1005 bv->updateInset(inset_owner);
1009 case LFUN_TRANSPOSE_CHARS:
1011 transposeChars(*this, cursor);
1013 bv->updateInset(inset_owner);
1018 cmd.message(_("Paste"));
1020 // clear the selection
1021 bv->toggleSelection();
1025 clearSelection(); // bug 393
1033 cutSelection(bv, true);
1035 cmd.message(_("Cut"));
1040 cmd.message(_("Copy"));
1043 case LFUN_BEGINNINGBUFSEL:
1045 return UNDISPATCHED;
1048 finishChange(bv, true);
1051 case LFUN_ENDBUFSEL:
1053 return UNDISPATCHED;
1056 finishChange(bv, true);
1060 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1066 istringstream is(cmd.argument.c_str());
1069 lyxerr << "SETXY: Could not parse coordinates in '"
1070 << cmd.argument << std::endl;
1072 setCursorFromCoordinates(x, y);
1077 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1079 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1085 case LFUN_GETLAYOUT:
1086 cmd.message(tostr(cursor.par()->layout()));
1090 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1091 << cmd.argument << endl;
1093 // This is not the good solution to the empty argument
1094 // problem, but it will hopefully suffice for 1.2.0.
1095 // The correct solution would be to augument the
1096 // function list/array with information about what
1097 // functions needs arguments and their type.
1098 if (cmd.argument.empty()) {
1099 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1103 // Derive layout number from given argument (string)
1104 // and current buffer's textclass (number)
1105 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1106 bool hasLayout = tclass.hasLayout(cmd.argument);
1107 string layout = cmd.argument;
1109 // If the entry is obsolete, use the new one instead.
1111 string const & obs = tclass[layout]->obsoleted_by();
1117 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1122 bool change_layout = (current_layout != layout);
1123 if (!change_layout && selection.set() &&
1124 selection.start.par() != selection.end.par())
1126 Paragraph * spar = selection.start.par();
1127 Paragraph * epar = selection.end.par()->next();
1128 while (spar != epar) {
1129 if (spar->layout()->name() != current_layout) {
1130 change_layout = true;
1133 spar = spar->next();
1136 if (change_layout) {
1138 current_layout = layout;
1141 bv->owner()->setLayout(layout);
1148 case LFUN_PASTESELECTION: {
1152 // this was originally a beforeChange(bv->text), i.e
1153 // the outermost LyXText!
1154 bv->beforeChange(this);
1155 string const clip = bv->getClipboard();
1156 if (!clip.empty()) {
1157 if (cmd.argument == "paragraph")
1158 insertStringAsParagraphs(clip);
1160 insertStringAsLines(clip);
1167 case LFUN_GOTOERROR:
1168 gotoInset(Inset::ERROR_CODE, false);
1172 gotoInset(Inset::NOTE_CODE, false);
1175 case LFUN_REFERENCE_GOTO:
1177 vector<Inset::Code> tmp;
1178 tmp.push_back(Inset::LABEL_CODE);
1179 tmp.push_back(Inset::REF_CODE);
1180 gotoInset(tmp, true);
1185 Paragraph const * par = cursor.par();
1186 lyx::pos_type pos = cursor.pos();
1190 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1193 c = par->getChar(pos - 1);
1196 LyXLayout_ptr const & style = par->layout();
1198 if (style->pass_thru ||
1199 par->getFontSettings(bv->buffer()->params,
1200 pos).language()->lang() == "hebrew" ||
1201 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1202 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1206 case LFUN_DATE_INSERT: {
1207 time_t now_time_t = time(NULL);
1208 struct tm * now_tm = localtime(&now_time_t);
1209 setlocale(LC_TIME, "");
1211 if (!cmd.argument.empty())
1214 arg = lyxrc.date_insert_format;
1216 int const datetmp_len =
1217 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1219 for (int i = 0; i < datetmp_len; i++) {
1220 insertChar(datetmp[i]);
1223 selection.cursor = cursor;
1224 moveCursorUpdate(bv, false);
1228 case LFUN_MOUSE_TRIPLE:
1231 if (!isInInset() && bv->theLockingInset())
1233 if (cmd.button() == mouse_button::button1) {
1235 bv->screen().hideCursor();
1236 bv->screen().toggleSelection(this, bv);
1239 selection.cursor = cursor;
1243 bv->screen().toggleSelection(this, bv, false);
1245 bv->haveSelection(selection.set());
1249 case LFUN_MOUSE_DOUBLE:
1252 if (!isInInset() && bv->theLockingInset())
1254 if (cmd.button() == mouse_button::button1) {
1256 bv->screen().hideCursor();
1257 bv->screen().toggleSelection(this, bv);
1258 selectWord(LyXText::WHOLE_WORD_STRICT);
1259 bv->screen().toggleSelection(this, bv, false);
1261 selectWord(LyXText::WHOLE_WORD_STRICT);
1264 bv->haveSelection(selection.set());
1268 case LFUN_MOUSE_MOTION:
1270 // Only use motion with button 1
1271 //if (ev.button() != mouse_button::button1)
1277 // Check for inset locking
1278 if (bv->theLockingInset()) {
1279 Inset * tli = bv->theLockingInset();
1280 LyXCursor cursor = bv->text->cursor;
1281 LyXFont font = bv->text->getFont(bv->buffer(),
1282 cursor.par(), cursor.pos());
1283 int width = tli->width(bv, font);
1284 int inset_x = font.isVisibleRightToLeft()
1285 ? cursor.ix() - width : cursor.ix();
1286 int start_x = inset_x + tli->scroll();
1287 FuncRequest cmd1 = cmd;
1288 cmd1.x = cmd.x - start_x;
1289 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1290 tli->localDispatch(cmd1);
1294 // The test for not selection possible is needed, that only motion
1295 // events are used, where the bottom press event was on
1296 // the drawing area too
1297 if (!selection_possible) {
1298 lyxerr[Debug::ACTION]
1299 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1303 bv->screen().hideCursor();
1305 RowList::iterator cursorrow = bv->text->cursor.row();
1306 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->text->top_y());
1308 // sorry for this but I have a strange error that the y value jumps at
1309 // a certain point. This seems like an error in my xforms library or
1310 // in some other local environment, but I would like to leave this here
1311 // for the moment until I can remove this (Jug 20020418)
1312 if (y_before < bv->text->cursor.y())
1313 lyxerr << y_before << ':'
1314 << bv->text->cursor.y() << endl;
1316 // This is to allow jumping over large insets
1317 if (cursorrow == bv->text->cursor.row()) {
1318 if (cmd.y >= bv->workHeight())
1319 bv->text->cursorDown(false);
1321 bv->text->cursorUp(false);
1324 // Maybe an empty line was deleted
1325 if (!bv->text->selection.set())
1326 bv->update(BufferView::UPDATE);
1327 bv->text->setSelection();
1328 bv->screen().toggleToggle(bv->text, bv);
1334 // Single-click on work area
1335 case LFUN_MOUSE_PRESS:
1340 // ok ok, this is a hack (for xforms)
1341 // We shouldn't go further down as we really should only do the
1342 // scrolling and be done with this. Otherwise we may open some
1343 // dialogs (Jug 20020424).
1344 if (cmd.button() == mouse_button::button4) {
1345 bv->scroll(-lyxrc.wheel_jump);
1348 if (cmd.button() == mouse_button::button5) {
1349 bv->scroll(lyxrc.wheel_jump);
1355 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1357 // Middle button press pastes if we have a selection
1358 // We do this here as if the selection was inside an inset
1359 // it could get cleared on the unlocking of the inset so
1360 // we have to check this first
1361 bool paste_internally = false;
1362 if (cmd.button() == mouse_button::button2 && selection.set()) {
1363 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1364 paste_internally = true;
1367 int const screen_first = bv->text->top_y();
1369 if (bv->theLockingInset()) {
1370 // We are in inset locking mode
1372 // Check whether the inset was hit. If not reset mode,
1373 // otherwise give the event to the inset
1374 if (inset_hit == bv->theLockingInset()) {
1375 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1376 bv->theLockingInset()->localDispatch(cmd1);
1379 bv->unlockInset(bv->theLockingInset());
1383 selection_possible = true;
1384 bv->screen().hideCursor();
1386 // Clear the selection
1387 bv->screen().toggleSelection(bv->text, bv);
1388 bv->text->clearSelection();
1389 bv->text->fullRebreak();
1391 bv->updateScrollbar();
1393 // Single left click in math inset?
1394 if (isHighlyEditableInset(inset_hit)) {
1395 // Highly editable inset, like math
1396 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1397 selection_possible = false;
1398 bv->owner()->message(inset->editMessage());
1399 //inset->edit(bv, x, y, cmd.button());
1400 // We just have to lock the inset before calling a PressEvent on it!
1401 // we don't need the edit() call here! (Jug20020329)
1402 if (!bv->lockInset(inset))
1403 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1404 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1405 inset->localDispatch(cmd1);
1408 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1410 // Right click on a footnote flag opens float menu
1411 if (cmd.button() == mouse_button::button3) {
1412 selection_possible = false;
1416 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1417 bv->text->setCursorFromCoordinates(x, y + screen_first);
1419 bv->text->selection.cursor = bv->text->cursor;
1420 bv->text->cursor.x_fix(bv->text->cursor.x());
1422 if (bv->fitCursor())
1423 selection_possible = false;
1425 // Insert primary selection with middle mouse
1426 // if there is a local selection in the current buffer,
1428 if (cmd.button() == mouse_button::button2) {
1429 if (paste_internally)
1430 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1432 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1433 selection_possible = false;
1438 case LFUN_MOUSE_RELEASE:
1440 // do nothing if we used the mouse wheel
1444 if (cmd.button() == mouse_button::button4
1445 || cmd.button() == mouse_button::button5)
1448 // If we hit an inset, we have the inset coordinates in these
1449 // and inset_hit points to the inset. If we do not hit an
1450 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1453 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1455 if (bv->theLockingInset()) {
1456 // We are in inset locking mode.
1458 // LyX does a kind of work-area grabbing for insets.
1459 // Only a ButtonPress FuncRequest outside the inset will
1460 // force a insetUnlock.
1461 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1462 bv->theLockingInset()->localDispatch(cmd1);
1466 selection_possible = false;
1468 if (cmd.button() == mouse_button::button2)
1472 if (cmd.button() == mouse_button::button1)
1473 bv->haveSelection(selection.set());
1476 bv->owner()->view_state_changed();
1477 bv->owner()->updateMenubar();
1478 bv->owner()->updateToolbar();
1480 // Did we hit an editable inset?
1482 selection_possible = false;
1484 // if we reach this point with a selection, it
1485 // must mean we are currently selecting.
1486 // But we don't want to open the inset
1487 // because that is annoying for the user.
1488 // So just pretend we didn't hit it.
1489 // this is OK because a "kosher" ButtonRelease
1490 // will follow a ButtonPress that clears
1492 // Note this also fixes selection drawing
1493 // problems if we end up opening an inset
1494 if (selection.set())
1497 // CHECK fix this proper in 0.13
1498 // well, maybe 13.0 !!!!!!!!!
1500 // Following a ref shouldn't issue
1501 // a push on the undo-stack
1502 // anylonger, now that we have
1503 // keybindings for following
1504 // references and returning from
1505 // references. IMHO though, it
1506 // should be the inset's own business
1507 // to push or not push on the undo
1508 // stack. They don't *have* to
1509 // alter the document...
1511 // ...or maybe the SetCursorParUndo()
1512 // below isn't necessary at all anylonger?
1513 if (inset_hit->lyxCode() == Inset::REF_CODE)
1514 setCursorParUndo(bv);
1516 bv->owner()->message(inset_hit->editMessage());
1518 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1519 inset_hit->localDispatch(cmd1);
1525 case LFUN_SELFINSERT: {
1526 if (cmd.argument.empty())
1529 // Automatically delete the currently selected
1530 // text and replace it with what is being
1531 // typed in now. Depends on lyxrc settings
1532 // "auto_region_delete", which defaults to
1535 if (lyxrc.auto_region_delete) {
1536 if (selection.set()) {
1537 cutSelection(false, false);
1540 bv->haveSelection(false);
1543 bv->beforeChange(this);
1544 LyXFont const old_font(real_current_font);
1546 string::const_iterator cit = cmd.argument.begin();
1547 string::const_iterator end = cmd.argument.end();
1548 for (; cit != end; ++cit)
1549 bv->owner()->getIntl().getTransManager().
1550 TranslateAndInsert(*cit, this);
1553 selection.cursor = cursor;
1554 moveCursorUpdate(bv, false);
1556 // real_current_font.number can change so we need to
1557 // update the minibuffer
1558 if (old_font != real_current_font)
1559 bv->owner()->view_state_changed();
1563 case LFUN_HTMLURL: {
1564 InsetCommandParams p("htmlurl");
1565 string const data = InsetCommandMailer::params2string("url", p);
1566 bv->owner()->getDialogs().show("url", data, 0);
1571 InsetCommandParams p("url");
1572 string const data = InsetCommandMailer::params2string("url", p);
1573 bv->owner()->getDialogs().show("url", data, 0);
1579 case LFUN_INSET_LIST:
1580 case LFUN_INSET_THEOREM:
1581 case LFUN_INSET_CAPTION:
1583 case LFUN_INSERT_NOTE:
1584 case LFUN_INSERT_BIBITEM:
1585 case LFUN_INSET_ERT:
1586 case LFUN_INSET_FLOAT:
1587 case LFUN_INSET_FOOTNOTE:
1588 case LFUN_INSET_MARGINAL:
1589 case LFUN_INSET_MINIPAGE:
1590 case LFUN_INSET_OPTARG:
1591 case LFUN_INSET_WIDE_FLOAT:
1592 case LFUN_INSET_WRAP:
1593 case LFUN_TABULAR_INSERT:
1594 case LFUN_ENVIRONMENT_INSERT:
1595 // Open the inset, and move the current selection
1597 doInsertInset(this, cmd, true, true);
1600 case LFUN_INDEX_INSERT:
1601 // Just open the inset
1602 doInsertInset(this, cmd, true, false);
1605 case LFUN_INDEX_PRINT:
1606 case LFUN_PARENTINSERT:
1607 case LFUN_TOC_INSERT:
1610 doInsertInset(this, cmd, false, false);
1614 return UNDISPATCHED;