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() != ownerParagraph()) {
218 LyXCursor tmp = cursor;
219 cursor.par(ownerParagraph());
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 Paragraph * tmp = ownerParagraph();
415 for (; tmp; tmp = tmp->next()) {
416 if (tmp->params().startOfAppendix()) {
417 setUndo(bv, Undo::EDIT, tmp, tmp->next());
418 tmp->params().startOfAppendix(false);
420 setHeightOfRow(getRow(tmp, 0, tmpy));
425 setUndo(bv, Undo::EDIT, par, par->next());
426 par->params().startOfAppendix(start);
428 // we can set the refreshing parameters now
430 redoHeightOfParagraph();
432 setCursor(cursor.par(), cursor.pos());
437 case LFUN_DELETE_WORD_FORWARD:
438 bv->beforeChange(this);
445 case LFUN_DELETE_WORD_BACKWARD:
446 bv->beforeChange(this);
448 deleteWordBackward();
453 case LFUN_DELETE_LINE_FORWARD:
454 bv->beforeChange(this);
463 if (!selection.mark())
464 bv->beforeChange(this);
471 if (!selection.mark())
472 bv->beforeChange(this);
474 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
477 cursorRightOneWord();
482 if (!selection.mark())
483 bv->beforeChange(this);
485 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
486 cursorRightOneWord();
492 case LFUN_BEGINNINGBUF:
493 if (!selection.mark())
494 bv->beforeChange(this);
501 if (selection.mark())
502 bv->beforeChange(this);
510 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
514 finishChange(bv, true);
519 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
523 finishChange(bv, true);
529 finishChange(bv, true);
535 finishChange(bv, true);
538 case LFUN_UP_PARAGRAPHSEL:
541 finishChange(bv, true);
544 case LFUN_DOWN_PARAGRAPHSEL:
546 cursorDownParagraph();
547 finishChange(bv, true);
553 finishChange(bv, true);
559 finishChange(bv, true);
565 finishChange(bv, true);
571 finishChange(bv, true);
574 case LFUN_WORDRIGHTSEL:
576 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
579 cursorRightOneWord();
580 finishChange(bv, true);
583 case LFUN_WORDLEFTSEL:
585 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
586 cursorRightOneWord();
589 finishChange(bv, true);
596 getWord(cur1, cur2, WHOLE_WORD);
597 setCursor(cur1.par(), cur1.pos());
598 bv->beforeChange(this);
599 setCursor(cur2.par(), cur2.pos());
600 finishChange(bv, true);
605 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
606 if (!selection.mark())
607 bv->beforeChange(this);
611 if (cursor.pos() < cursor.par()->size()
612 && cursor.par()->isInset(cursor.pos())
613 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
614 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
615 cmd.message(tmpinset->editMessage());
616 tmpinset->edit(bv, !is_rtl);
626 // This is soooo ugly. Isn`t it possible to make
628 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
629 if (!selection.mark())
630 bv->beforeChange(this);
632 LyXCursor const cur = cursor;
635 if ((is_rtl || cur != cursor) && // only if really moved!
636 cursor.pos() < cursor.par()->size() &&
637 cursor.par()->isInset(cursor.pos()) &&
638 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
639 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
640 cmd.message(tmpinset->editMessage());
641 tmpinset->edit(bv, is_rtl);
651 if (!selection.mark())
652 bv->beforeChange(this);
653 bv->update(this, BufferView::UPDATE);
659 if (!selection.mark())
660 bv->beforeChange(this);
661 bv->update(this, BufferView::UPDATE);
666 case LFUN_UP_PARAGRAPH:
667 if (!selection.mark())
668 bv->beforeChange(this);
669 bv->update(this, BufferView::UPDATE);
674 case LFUN_DOWN_PARAGRAPH:
675 if (!selection.mark())
676 bv->beforeChange(this);
677 bv->update(this, BufferView::UPDATE);
678 cursorDownParagraph();
679 finishChange(bv, false);
683 if (!selection.mark())
684 bv->beforeChange(this);
685 bv->update(this, BufferView::UPDATE);
687 finishChange(bv, false);
690 // moveCursorUpdate(bv, false, false);
691 // owner_->view_state_changed();
695 if (!selection.mark())
696 bv->beforeChange(this);
697 bv->update(this, BufferView::UPDATE);
699 finishChange(bv, false);
703 if (!selection.mark())
704 bv->beforeChange(this);
707 finishChange(bv, false);
711 if (!selection.mark())
712 bv->beforeChange(this);
715 finishChange(bv, false);
718 case LFUN_BREAKLINE: {
719 lyx::pos_type body = cursor.par()->beginningOfBody();
721 // Not allowed by LaTeX (labels or empty par)
722 if (cursor.pos() <= body)
725 bv->beforeChange(this);
726 insertInset(new InsetNewline);
728 setCursor(cursor.par(), cursor.pos());
729 moveCursorUpdate(bv, false);
734 if (!selection.set()) {
736 selection.cursor = cursor;
738 // It is possible to make it a lot faster still
739 // just comment out the line below...
743 cutSelection(bv, true);
746 moveCursorUpdate(bv, false);
747 bv->owner()->view_state_changed();
751 case LFUN_DELETE_SKIP:
752 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
753 if (!selection.set()) {
754 LyXCursor cur = cursor;
755 if (cur.pos() == cur.par()->size()) {
759 && !(cur.par()->params().spaceTop()
760 == VSpace (VSpace::NONE))) {
762 cur.par()->params().lineTop(),
763 cur.par()->params().lineBottom(),
764 cur.par()->params().pagebreakTop(),
765 cur.par()->params().pagebreakBottom(),
766 VSpace(VSpace::NONE),
767 cur.par()->params().spaceBottom(),
768 cur.par()->params().spacing(),
769 cur.par()->params().align(),
770 cur.par()->params().labelWidthString(), 0);
776 selection.cursor = cursor;
780 selection.cursor = cursor;
784 cutSelection(bv, true);
791 if (!selection.set()) {
792 if (bv->owner()->getIntl().getTransManager().backspace()) {
794 selection.cursor = cursor;
796 // It is possible to make it a lot faster still
797 // just comment out the line below...
802 cutSelection(bv, true);
805 bv->owner()->view_state_changed();
809 case LFUN_BACKSPACE_SKIP:
810 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
811 if (!selection.set()) {
812 LyXCursor cur = cursor;
814 && !(cur.par()->params().spaceTop()
815 == VSpace (VSpace::NONE))) {
817 cur.par()->params().lineTop(),
818 cur.par()->params().lineBottom(),
819 cur.par()->params().pagebreakTop(),
820 cur.par()->params().pagebreakBottom(),
821 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
822 cur.par()->params().spacing(),
823 cur.par()->params().align(),
824 cur.par()->params().labelWidthString(), 0);
827 selection.cursor = cur;
831 cutSelection(bv, true);
836 case LFUN_BREAKPARAGRAPH:
837 bv->beforeChange(this);
838 breakParagraph(bv->buffer()->paragraphs, 0);
840 selection.cursor = cursor;
842 bv->owner()->view_state_changed();
845 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
846 bv->beforeChange(this);
847 breakParagraph(bv->buffer()->paragraphs, 1);
849 selection.cursor = cursor;
851 bv->owner()->view_state_changed();
854 case LFUN_BREAKPARAGRAPH_SKIP: {
855 // When at the beginning of a paragraph, remove
856 // indentation and add a "defskip" at the top.
857 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
858 LyXCursor cur = cursor;
859 bv->beforeChange(this);
860 if (cur.pos() == 0) {
861 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
863 cur.par()->params().lineTop(),
864 cur.par()->params().lineBottom(),
865 cur.par()->params().pagebreakTop(),
866 cur.par()->params().pagebreakBottom(),
867 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
868 cur.par()->params().spacing(),
869 cur.par()->params().align(),
870 cur.par()->params().labelWidthString(), 1);
874 breakParagraph(bv->buffer()->paragraphs, 0);
877 selection.cursor = cur;
879 bv->owner()->view_state_changed();
883 case LFUN_PARAGRAPH_SPACING: {
884 Paragraph * par = cursor.par();
885 Spacing::Space cur_spacing = par->params().spacing().getSpace();
886 float cur_value = 1.0;
887 if (cur_spacing == Spacing::Other)
888 cur_value = par->params().spacing().getValue();
890 istringstream is(cmd.argument.c_str());
893 Spacing::Space new_spacing = cur_spacing;
894 float new_value = cur_value;
896 lyxerr << "Missing argument to `paragraph-spacing'"
898 } else if (tmp == "single") {
899 new_spacing = Spacing::Single;
900 } else if (tmp == "onehalf") {
901 new_spacing = Spacing::Onehalf;
902 } else if (tmp == "double") {
903 new_spacing = Spacing::Double;
904 } else if (tmp == "other") {
905 new_spacing = Spacing::Other;
908 lyxerr << "new_value = " << tmpval << endl;
911 } else if (tmp == "default") {
912 new_spacing = Spacing::Default;
914 lyxerr << _("Unknown spacing argument: ")
915 << cmd.argument << endl;
917 if (cur_spacing != new_spacing || cur_value != new_value) {
918 par->params().spacing(Spacing(new_spacing, new_value));
925 case LFUN_INSET_TOGGLE:
927 bv->beforeChange(this);
934 case LFUN_PROTECTEDSPACE:
935 if (cursor.par()->layout()->free_spacing) {
939 specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
941 moveCursorUpdate(bv, false);
944 case LFUN_HYPHENATION:
945 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
948 case LFUN_LIGATURE_BREAK:
949 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
953 specialChar(this, bv, InsetSpecialChar::LDOTS);
956 case LFUN_END_OF_SENTENCE:
957 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
960 case LFUN_MENU_SEPARATOR:
961 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
965 bv->beforeChange(this);
967 selection.cursor = cursor;
968 cmd.message(N_("Mark off"));
972 bv->beforeChange(this);
973 selection.mark(true);
975 selection.cursor = cursor;
976 cmd.message(N_("Mark on"));
980 bv->beforeChange(this);
981 if (selection.mark()) {
983 cmd.message(N_("Mark removed"));
985 selection.mark(true);
987 cmd.message(N_("Mark set"));
989 selection.cursor = cursor;
992 case LFUN_UPCASE_WORD:
994 changeCase(LyXText::text_uppercase);
996 bv->updateInset(inset_owner);
1000 case LFUN_LOWCASE_WORD:
1002 changeCase(LyXText::text_lowercase);
1004 bv->updateInset(inset_owner);
1008 case LFUN_CAPITALIZE_WORD:
1010 changeCase(LyXText::text_capitalization);
1012 bv->updateInset(inset_owner);
1016 case LFUN_TRANSPOSE_CHARS:
1018 transposeChars(*this, cursor);
1020 bv->updateInset(inset_owner);
1025 cmd.message(_("Paste"));
1027 // clear the selection
1028 bv->toggleSelection();
1032 clearSelection(); // bug 393
1040 cutSelection(bv, true);
1042 cmd.message(_("Cut"));
1047 cmd.message(_("Copy"));
1050 case LFUN_BEGINNINGBUFSEL:
1052 return UNDISPATCHED;
1055 finishChange(bv, true);
1058 case LFUN_ENDBUFSEL:
1060 return UNDISPATCHED;
1063 finishChange(bv, true);
1067 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1073 istringstream is(cmd.argument.c_str());
1076 lyxerr << "SETXY: Could not parse coordinates in '"
1077 << cmd.argument << std::endl;
1079 setCursorFromCoordinates(x, y);
1084 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1086 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1092 case LFUN_GETLAYOUT:
1093 cmd.message(tostr(cursor.par()->layout()));
1097 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1098 << cmd.argument << endl;
1100 // This is not the good solution to the empty argument
1101 // problem, but it will hopefully suffice for 1.2.0.
1102 // The correct solution would be to augument the
1103 // function list/array with information about what
1104 // functions needs arguments and their type.
1105 if (cmd.argument.empty()) {
1106 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1110 // Derive layout number from given argument (string)
1111 // and current buffer's textclass (number)
1112 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1113 bool hasLayout = tclass.hasLayout(cmd.argument);
1114 string layout = cmd.argument;
1116 // If the entry is obsolete, use the new one instead.
1118 string const & obs = tclass[layout]->obsoleted_by();
1124 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1129 bool change_layout = (current_layout != layout);
1130 if (!change_layout && selection.set() &&
1131 selection.start.par() != selection.end.par())
1133 Paragraph * spar = selection.start.par();
1134 Paragraph * epar = selection.end.par()->next();
1135 while (spar != epar) {
1136 if (spar->layout()->name() != current_layout) {
1137 change_layout = true;
1140 spar = spar->next();
1143 if (change_layout) {
1145 current_layout = layout;
1148 bv->owner()->setLayout(layout);
1155 case LFUN_PASTESELECTION: {
1159 // this was originally a beforeChange(bv->text), i.e
1160 // the outermost LyXText!
1161 bv->beforeChange(this);
1162 string const clip = bv->getClipboard();
1163 if (!clip.empty()) {
1164 if (cmd.argument == "paragraph")
1165 insertStringAsParagraphs(clip);
1167 insertStringAsLines(clip);
1174 case LFUN_GOTOERROR:
1175 gotoInset(Inset::ERROR_CODE, false);
1179 gotoInset(Inset::NOTE_CODE, false);
1182 case LFUN_REFERENCE_GOTO:
1184 vector<Inset::Code> tmp;
1185 tmp.push_back(Inset::LABEL_CODE);
1186 tmp.push_back(Inset::REF_CODE);
1187 gotoInset(tmp, true);
1192 Paragraph const * par = cursor.par();
1193 lyx::pos_type pos = cursor.pos();
1197 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1200 c = par->getChar(pos - 1);
1203 LyXLayout_ptr const & style = par->layout();
1205 if (style->pass_thru ||
1206 par->getFontSettings(bv->buffer()->params,
1207 pos).language()->lang() == "hebrew" ||
1208 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1209 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1213 case LFUN_DATE_INSERT: {
1214 time_t now_time_t = time(NULL);
1215 struct tm * now_tm = localtime(&now_time_t);
1216 setlocale(LC_TIME, "");
1218 if (!cmd.argument.empty())
1221 arg = lyxrc.date_insert_format;
1223 int const datetmp_len =
1224 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1226 for (int i = 0; i < datetmp_len; i++) {
1227 insertChar(datetmp[i]);
1230 selection.cursor = cursor;
1231 moveCursorUpdate(bv, false);
1235 case LFUN_MOUSE_TRIPLE:
1238 if (!isInInset() && bv->theLockingInset())
1240 if (cmd.button() == mouse_button::button1) {
1242 bv->screen().hideCursor();
1243 bv->screen().toggleSelection(this, bv);
1246 selection.cursor = cursor;
1250 bv->screen().toggleSelection(this, bv, false);
1252 bv->haveSelection(selection.set());
1256 case LFUN_MOUSE_DOUBLE:
1259 if (!isInInset() && bv->theLockingInset())
1261 if (cmd.button() == mouse_button::button1) {
1263 bv->screen().hideCursor();
1264 bv->screen().toggleSelection(this, bv);
1265 selectWord(LyXText::WHOLE_WORD_STRICT);
1266 bv->screen().toggleSelection(this, bv, false);
1268 selectWord(LyXText::WHOLE_WORD_STRICT);
1271 bv->haveSelection(selection.set());
1275 case LFUN_MOUSE_MOTION:
1277 // Only use motion with button 1
1278 //if (ev.button() != mouse_button::button1)
1284 // Check for inset locking
1285 if (bv->theLockingInset()) {
1286 Inset * tli = bv->theLockingInset();
1287 LyXCursor cursor = bv->text->cursor;
1288 LyXFont font = bv->text->getFont(bv->buffer(),
1289 cursor.par(), cursor.pos());
1290 int width = tli->width(bv, font);
1291 int inset_x = font.isVisibleRightToLeft()
1292 ? cursor.ix() - width : cursor.ix();
1293 int start_x = inset_x + tli->scroll();
1294 FuncRequest cmd1 = cmd;
1295 cmd1.x = cmd.x - start_x;
1296 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1297 tli->localDispatch(cmd1);
1301 // The test for not selection possible is needed, that only motion
1302 // events are used, where the bottom press event was on
1303 // the drawing area too
1304 if (!selection_possible) {
1305 lyxerr[Debug::ACTION]
1306 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1310 bv->screen().hideCursor();
1312 RowList::iterator cursorrow = bv->text->cursor.row();
1313 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->text->top_y());
1315 // sorry for this but I have a strange error that the y value jumps at
1316 // a certain point. This seems like an error in my xforms library or
1317 // in some other local environment, but I would like to leave this here
1318 // for the moment until I can remove this (Jug 20020418)
1319 if (y_before < bv->text->cursor.y())
1320 lyxerr << y_before << ':'
1321 << bv->text->cursor.y() << endl;
1323 // This is to allow jumping over large insets
1324 if (cursorrow == bv->text->cursor.row()) {
1325 if (cmd.y >= bv->workHeight())
1326 bv->text->cursorDown(false);
1328 bv->text->cursorUp(false);
1331 // Maybe an empty line was deleted
1332 if (!bv->text->selection.set())
1333 bv->update(BufferView::UPDATE);
1334 bv->text->setSelection();
1335 bv->screen().toggleToggle(bv->text, bv);
1341 // Single-click on work area
1342 case LFUN_MOUSE_PRESS:
1347 // ok ok, this is a hack (for xforms)
1348 // We shouldn't go further down as we really should only do the
1349 // scrolling and be done with this. Otherwise we may open some
1350 // dialogs (Jug 20020424).
1351 if (cmd.button() == mouse_button::button4) {
1352 bv->scroll(-lyxrc.wheel_jump);
1355 if (cmd.button() == mouse_button::button5) {
1356 bv->scroll(lyxrc.wheel_jump);
1362 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1364 // Middle button press pastes if we have a selection
1365 // We do this here as if the selection was inside an inset
1366 // it could get cleared on the unlocking of the inset so
1367 // we have to check this first
1368 bool paste_internally = false;
1369 if (cmd.button() == mouse_button::button2 && selection.set()) {
1370 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1371 paste_internally = true;
1374 int const screen_first = bv->text->top_y();
1376 if (bv->theLockingInset()) {
1377 // We are in inset locking mode
1379 // Check whether the inset was hit. If not reset mode,
1380 // otherwise give the event to the inset
1381 if (inset_hit == bv->theLockingInset()) {
1382 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1383 bv->theLockingInset()->localDispatch(cmd1);
1386 bv->unlockInset(bv->theLockingInset());
1390 selection_possible = true;
1391 bv->screen().hideCursor();
1393 // Clear the selection
1394 bv->screen().toggleSelection(bv->text, bv);
1395 bv->text->clearSelection();
1396 bv->text->fullRebreak();
1398 bv->updateScrollbar();
1400 // Single left click in math inset?
1401 if (isHighlyEditableInset(inset_hit)) {
1402 // Highly editable inset, like math
1403 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1404 selection_possible = false;
1405 bv->owner()->message(inset->editMessage());
1406 //inset->edit(bv, x, y, cmd.button());
1407 // We just have to lock the inset before calling a PressEvent on it!
1408 // we don't need the edit() call here! (Jug20020329)
1409 if (!bv->lockInset(inset))
1410 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1411 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1412 inset->localDispatch(cmd1);
1415 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1417 // Right click on a footnote flag opens float menu
1418 if (cmd.button() == mouse_button::button3) {
1419 selection_possible = false;
1423 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1424 bv->text->setCursorFromCoordinates(x, y + screen_first);
1426 bv->text->selection.cursor = bv->text->cursor;
1427 bv->text->cursor.x_fix(bv->text->cursor.x());
1429 if (bv->fitCursor())
1430 selection_possible = false;
1432 // Insert primary selection with middle mouse
1433 // if there is a local selection in the current buffer,
1435 if (cmd.button() == mouse_button::button2) {
1436 if (paste_internally)
1437 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1439 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1440 selection_possible = false;
1445 case LFUN_MOUSE_RELEASE:
1447 // do nothing if we used the mouse wheel
1451 if (cmd.button() == mouse_button::button4
1452 || cmd.button() == mouse_button::button5)
1455 // If we hit an inset, we have the inset coordinates in these
1456 // and inset_hit points to the inset. If we do not hit an
1457 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1460 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1462 if (bv->theLockingInset()) {
1463 // We are in inset locking mode.
1465 // LyX does a kind of work-area grabbing for insets.
1466 // Only a ButtonPress FuncRequest outside the inset will
1467 // force a insetUnlock.
1468 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1469 bv->theLockingInset()->localDispatch(cmd1);
1473 selection_possible = false;
1475 if (cmd.button() == mouse_button::button2)
1479 if (cmd.button() == mouse_button::button1)
1480 bv->haveSelection(selection.set());
1483 bv->owner()->view_state_changed();
1484 bv->owner()->updateMenubar();
1485 bv->owner()->updateToolbar();
1487 // Did we hit an editable inset?
1489 selection_possible = false;
1491 // if we reach this point with a selection, it
1492 // must mean we are currently selecting.
1493 // But we don't want to open the inset
1494 // because that is annoying for the user.
1495 // So just pretend we didn't hit it.
1496 // this is OK because a "kosher" ButtonRelease
1497 // will follow a ButtonPress that clears
1499 // Note this also fixes selection drawing
1500 // problems if we end up opening an inset
1501 if (selection.set())
1504 // CHECK fix this proper in 0.13
1505 // well, maybe 13.0 !!!!!!!!!
1507 // Following a ref shouldn't issue
1508 // a push on the undo-stack
1509 // anylonger, now that we have
1510 // keybindings for following
1511 // references and returning from
1512 // references. IMHO though, it
1513 // should be the inset's own business
1514 // to push or not push on the undo
1515 // stack. They don't *have* to
1516 // alter the document...
1518 // ...or maybe the SetCursorParUndo()
1519 // below isn't necessary at all anylonger?
1520 if (inset_hit->lyxCode() == Inset::REF_CODE)
1521 setCursorParUndo(bv);
1523 bv->owner()->message(inset_hit->editMessage());
1525 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1526 inset_hit->localDispatch(cmd1);
1532 case LFUN_SELFINSERT: {
1533 if (cmd.argument.empty())
1536 // Automatically delete the currently selected
1537 // text and replace it with what is being
1538 // typed in now. Depends on lyxrc settings
1539 // "auto_region_delete", which defaults to
1542 if (lyxrc.auto_region_delete) {
1543 if (selection.set()) {
1544 cutSelection(false, false);
1547 bv->haveSelection(false);
1550 bv->beforeChange(this);
1551 LyXFont const old_font(real_current_font);
1553 string::const_iterator cit = cmd.argument.begin();
1554 string::const_iterator end = cmd.argument.end();
1555 for (; cit != end; ++cit)
1556 bv->owner()->getIntl().getTransManager().
1557 TranslateAndInsert(*cit, this);
1560 selection.cursor = cursor;
1561 moveCursorUpdate(bv, false);
1563 // real_current_font.number can change so we need to
1564 // update the minibuffer
1565 if (old_font != real_current_font)
1566 bv->owner()->view_state_changed();
1570 case LFUN_HTMLURL: {
1571 InsetCommandParams p("htmlurl");
1572 string const data = InsetCommandMailer::params2string("url", p);
1573 bv->owner()->getDialogs().show("url", data, 0);
1578 InsetCommandParams p("url");
1579 string const data = InsetCommandMailer::params2string("url", p);
1580 bv->owner()->getDialogs().show("url", data, 0);
1586 case LFUN_INSET_LIST:
1587 case LFUN_INSET_THEOREM:
1588 case LFUN_INSET_CAPTION:
1590 case LFUN_INSERT_NOTE:
1591 case LFUN_INSERT_BIBITEM:
1592 case LFUN_INSET_ERT:
1593 case LFUN_INSET_FLOAT:
1594 case LFUN_INSET_FOOTNOTE:
1595 case LFUN_INSET_MARGINAL:
1596 case LFUN_INSET_MINIPAGE:
1597 case LFUN_INSET_OPTARG:
1598 case LFUN_INSET_WIDE_FLOAT:
1599 case LFUN_INSET_WRAP:
1600 case LFUN_TABULAR_INSERT:
1601 case LFUN_ENVIRONMENT_INSERT:
1602 // Open the inset, and move the current selection
1604 doInsertInset(this, cmd, true, true);
1607 case LFUN_INDEX_INSERT:
1608 // Just open the inset
1609 doInsertInset(this, cmd, true, false);
1612 case LFUN_INDEX_PRINT:
1613 case LFUN_PARENTINSERT:
1614 case LFUN_TOC_INSERT:
1617 doInsertInset(this, cmd, false, false);
1621 return UNDISPATCHED;