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"
46 extern string current_layout;
47 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
49 // the selection possible is needed, that only motion events are
50 // used, where the bottom press event was on the drawing area too
51 bool selection_possible = false;
56 void moveCursorUpdate(BufferView * bv, bool selecting)
58 LyXText * lt = bv->getLyXText();
60 if (selecting || lt->selection.mark()) {
63 bv->updateInset(lt->inset_owner);
67 if (!lt->isInInset()) {
68 bv->update(lt, BufferView::SELECT);
70 } else if (bv->text->status() != LyXText::UNCHANGED) {
71 bv->theLockingInset()->hideInsetCursor(bv);
72 bv->update(BufferView::SELECT);
76 if (!lt->selection.set())
77 bv->haveSelection(false);
83 void finishChange(BufferView * bv, bool selecting = false)
86 moveCursorUpdate(bv, selecting);
87 bv->owner()->view_state_changed();
90 // check if the given co-ordinates are inside an inset at the
91 // given cursor, if one exists. If so, the inset is returned,
92 // and the co-ordinates are made relative. Otherwise, 0 is returned.
93 Inset * checkInset(BufferView * bv, LyXText & text,
94 LyXCursor const & cur, int & x, int & y)
96 lyx::pos_type const pos = cur.pos();
97 Paragraph /*const*/ & par = *cur.par();
99 if (pos >= par.size() || !par.isInset(pos))
102 Inset /*const*/ * inset = par.getInset(pos);
104 if (!isEditableInset(inset))
107 // get inset dimensions
108 lyx::Assert(par.getInset(pos));
110 LyXFont const & font = text.getFont(bv->buffer(), &par, pos);
112 int const width = inset->width(bv, font);
113 int const inset_x = font.isVisibleRightToLeft()
114 ? (cur.ix() - width) : cur.ix();
117 inset_x + inset->scroll(),
119 cur.iy() - inset->ascent(bv, font),
120 cur.iy() + inset->descent(bv, font)
123 if (!b.contained(x, y)) {
124 lyxerr[Debug::GUI] << "Missed inset at x,y "
126 << " box " << b << endl;
130 text.setCursor(&par, pos, true);
133 // The origin of an inset is on the baseline
134 y -= text.cursor.iy();
142 Inset * LyXText::checkInsetHit(int & x, int & y)
144 int y_tmp = y + top_y();
147 setCursorFromCoordinates(cur, x, y_tmp);
149 Inset * inset = checkInset(bv(), *this, cur, x, y_tmp);
155 // look at previous position
160 setCursor(cur, cur.par(), cur.pos() - 1, true);
162 inset = checkInset(bv(), *this, cur, x, y_tmp);
169 bool LyXText::gotoNextInset(vector<Inset::Code> const & codes,
170 string const & contents)
172 LyXCursor res = cursor;
175 if (res.pos() < res.par()->size() - 1) {
176 res.pos(res.pos() + 1);
178 res.par(res.par()->next());
182 } while (res.par() &&
183 !(res.par()->isInset(res.pos())
184 && (inset = res.par()->getInset(res.pos())) != 0
185 && find(codes.begin(), codes.end(), inset->lyxCode())
187 && (contents.empty() ||
188 static_cast<InsetCommand *>(
189 res.par()->getInset(res.pos()))->getContents()
193 setCursor(res.par(), res.pos(), false);
200 void LyXText::gotoInset(vector<Inset::Code> const & codes,
204 bv()->beforeChange(this);
208 if (same_content && cursor.par()->isInset(cursor.pos())) {
209 Inset const * inset = cursor.par()->getInset(cursor.pos());
210 if (find(codes.begin(), codes.end(), inset->lyxCode())
212 contents = static_cast<InsetCommand const *>(inset)->getContents();
215 if (!gotoNextInset(codes, contents)) {
216 if (cursor.pos() || cursor.par() != ownerParagraph()) {
217 LyXCursor tmp = cursor;
218 cursor.par(ownerParagraph());
220 if (!gotoNextInset(codes, contents)) {
222 bv()->owner()->message(_("No more insets"));
225 bv()->owner()->message(_("No more insets"));
229 selection.cursor = cursor;
233 void LyXText::gotoInset(Inset::Code code, bool same_content)
235 gotoInset(vector<Inset::Code>(1, code), same_content);
239 void LyXText::cursorPrevious()
243 if (!cursor.row()->previous()) {
245 int new_y = bv()->text->top_y() - bv()->workHeight();
246 bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
247 bv()->updateScrollbar();
252 Row * cursorrow = cursor.row();
254 setCursorFromCoordinates(cursor.x_fix(), y);
258 if (cursorrow == bv()->text->cursor.row()) {
259 // we have a row which is taller than the workarea. The
260 // simplest solution is to move to the previous row instead.
263 // This is what we used to do, so we wouldn't skip right past
264 // tall rows, but it's not working right now.
266 new_y = bv->text->top_y() - bv->workHeight();
270 new_y = bv()->text->cursor.iy()
271 + bv()->theLockingInset()->insetInInsetY() + y
272 + cursor.row()->height()
273 - bv()->workHeight() + 1;
276 - cursor.row()->baseline()
277 + cursor.row()->height()
278 - bv()->workHeight() + 1;
281 bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
282 if (cursor.row()->previous()) {
284 setCursor(cur, cursor.row()->previous()->par(),
285 cursor.row()->previous()->pos(), false);
286 if (cur.y() > top_y()) {
290 bv()->updateScrollbar();
294 void LyXText::cursorNext()
298 if (!cursor.row()->next()) {
299 int y = cursor.y() - cursor.row()->baseline() +
300 cursor.row()->height();
301 if (y > topy + bv()->workHeight()) {
302 bv()->screen().draw(bv()->text, bv(), bv()->text->top_y() + bv()->workHeight());
303 bv()->updateScrollbar();
308 int y = topy + bv()->workHeight();
309 if (inset_owner && !topy) {
310 y -= (bv()->text->cursor.iy()
311 - bv()->text->top_y()
312 + bv()->theLockingInset()->insetInInsetY());
317 Row * cursorrow = cursor.row();
318 setCursorFromCoordinates(cursor.x_fix(), y);
319 // + bv->workHeight());
323 if (cursorrow == bv()->text->cursor.row()) {
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->text->top_y() + bv->workHeight();
335 new_y = bv()->text->cursor.iy()
336 + bv()->theLockingInset()->insetInInsetY()
337 + y - cursor.row()->baseline();
339 new_y = cursor.y() - cursor.row()->baseline();
342 bv()->screen().draw(bv()->text, bv(), new_y);
343 if (cursor.row()->next()) {
345 setCursor(cur, cursor.row()->next()->par(),
346 cursor.row()->next()->pos(), false);
347 if (cur.y() < top_y() + bv()->workHeight()) {
351 bv()->updateScrollbar();
355 void LyXText::update()
357 bv()->update(this, BufferView::SELECT);
362 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
366 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
367 if (!bv->insertInset(new_inset))
370 bv->updateInset(new_inset);
374 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
375 bool edit, bool pastesel)
377 Inset * inset = createInset(cmd);
378 BufferView * bv = cmd.view();
382 if (lt->selection.set()) {
383 lt->cutSelection(true, false);
386 if (bv->insertInset(inset)) {
389 if (gotsel && pastesel)
390 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
399 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
401 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
402 <<"] arg[" << cmd.argument << ']' << endl;
404 BufferView * bv = cmd.view();
406 switch (cmd.action) {
408 case LFUN_APPENDIX: {
409 Paragraph * par = cursor.par();
410 bool start = !par->params().startOfAppendix();
412 // ensure that we have only one start_of_appendix in this document
413 Paragraph * tmp = ownerParagraph();
414 for (; tmp; tmp = tmp->next()) {
415 if (tmp->params().startOfAppendix()) {
416 setUndo(bv, Undo::EDIT, tmp, tmp->next());
417 tmp->params().startOfAppendix(false);
419 setHeightOfRow(getRow(tmp, 0, tmpy));
424 setUndo(bv, Undo::EDIT, par, par->next());
425 par->params().startOfAppendix(start);
427 // we can set the refreshing parameters now
429 redoHeightOfParagraph();
431 setCursor(cursor.par(), cursor.pos());
436 case LFUN_DELETE_WORD_FORWARD:
437 bv->beforeChange(this);
444 case LFUN_DELETE_WORD_BACKWARD:
445 bv->beforeChange(this);
447 deleteWordBackward();
452 case LFUN_DELETE_LINE_FORWARD:
453 bv->beforeChange(this);
462 if (!selection.mark())
463 bv->beforeChange(this);
470 if (!selection.mark())
471 bv->beforeChange(this);
473 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
476 cursorRightOneWord();
481 if (!selection.mark())
482 bv->beforeChange(this);
484 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
485 cursorRightOneWord();
491 case LFUN_BEGINNINGBUF:
492 if (!selection.mark())
493 bv->beforeChange(this);
500 if (selection.mark())
501 bv->beforeChange(this);
509 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
513 finishChange(bv, true);
518 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
522 finishChange(bv, true);
528 finishChange(bv, true);
534 finishChange(bv, true);
537 case LFUN_UP_PARAGRAPHSEL:
540 finishChange(bv, true);
543 case LFUN_DOWN_PARAGRAPHSEL:
545 cursorDownParagraph();
546 finishChange(bv, true);
552 finishChange(bv, true);
558 finishChange(bv, true);
564 finishChange(bv, true);
570 finishChange(bv, true);
573 case LFUN_WORDRIGHTSEL:
575 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
578 cursorRightOneWord();
579 finishChange(bv, true);
582 case LFUN_WORDLEFTSEL:
584 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
585 cursorRightOneWord();
588 finishChange(bv, true);
595 getWord(cur1, cur2, WHOLE_WORD);
596 setCursor(cur1.par(), cur1.pos());
597 bv->beforeChange(this);
598 setCursor(cur2.par(), cur2.pos());
599 finishChange(bv, true);
604 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
605 if (!selection.mark())
606 bv->beforeChange(this);
610 if (cursor.pos() < cursor.par()->size()
611 && cursor.par()->isInset(cursor.pos())
612 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
613 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
614 cmd.message(tmpinset->editMessage());
615 tmpinset->edit(bv, !is_rtl);
625 // This is soooo ugly. Isn`t it possible to make
627 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
628 if (!selection.mark())
629 bv->beforeChange(this);
631 LyXCursor const cur = cursor;
634 if ((is_rtl || cur != cursor) && // only if really moved!
635 cursor.pos() < cursor.par()->size() &&
636 cursor.par()->isInset(cursor.pos()) &&
637 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
638 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
639 cmd.message(tmpinset->editMessage());
640 tmpinset->edit(bv, is_rtl);
650 if (!selection.mark())
651 bv->beforeChange(this);
652 bv->update(this, BufferView::UPDATE);
658 if (!selection.mark())
659 bv->beforeChange(this);
660 bv->update(this, BufferView::UPDATE);
665 case LFUN_UP_PARAGRAPH:
666 if (!selection.mark())
667 bv->beforeChange(this);
668 bv->update(this, BufferView::UPDATE);
673 case LFUN_DOWN_PARAGRAPH:
674 if (!selection.mark())
675 bv->beforeChange(this);
676 bv->update(this, BufferView::UPDATE);
677 cursorDownParagraph();
678 finishChange(bv, false);
682 if (!selection.mark())
683 bv->beforeChange(this);
684 bv->update(this, BufferView::UPDATE);
686 finishChange(bv, false);
689 // moveCursorUpdate(bv, false, false);
690 // owner_->view_state_changed();
694 if (!selection.mark())
695 bv->beforeChange(this);
696 bv->update(this, BufferView::UPDATE);
698 finishChange(bv, false);
702 if (!selection.mark())
703 bv->beforeChange(this);
706 finishChange(bv, false);
710 if (!selection.mark())
711 bv->beforeChange(this);
714 finishChange(bv, false);
717 case LFUN_BREAKLINE: {
718 lyx::pos_type body = cursor.par()->beginningOfBody();
720 // Not allowed by LaTeX (labels or empty par)
721 if (cursor.pos() <= body)
724 bv->beforeChange(this);
725 insertInset(new InsetNewline);
727 setCursor(cursor.par(), cursor.pos());
728 moveCursorUpdate(bv, false);
733 if (!selection.set()) {
735 selection.cursor = cursor;
737 // It is possible to make it a lot faster still
738 // just comment out the line below...
742 cutSelection(bv, true);
745 moveCursorUpdate(bv, false);
746 bv->owner()->view_state_changed();
750 case LFUN_DELETE_SKIP:
751 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
752 if (!selection.set()) {
753 LyXCursor cur = cursor;
754 if (cur.pos() == cur.par()->size()) {
758 && !(cur.par()->params().spaceTop()
759 == VSpace (VSpace::NONE))) {
761 cur.par()->params().lineTop(),
762 cur.par()->params().lineBottom(),
763 cur.par()->params().pagebreakTop(),
764 cur.par()->params().pagebreakBottom(),
765 VSpace(VSpace::NONE),
766 cur.par()->params().spaceBottom(),
767 cur.par()->params().spacing(),
768 cur.par()->params().align(),
769 cur.par()->params().labelWidthString(), 0);
775 selection.cursor = cursor;
779 selection.cursor = cursor;
783 cutSelection(bv, true);
790 if (!selection.set()) {
791 if (bv->owner()->getIntl().getTransManager().backspace()) {
793 selection.cursor = cursor;
795 // It is possible to make it a lot faster still
796 // just comment out the line below...
801 cutSelection(bv, true);
804 bv->owner()->view_state_changed();
808 case LFUN_BACKSPACE_SKIP:
809 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
810 if (!selection.set()) {
811 LyXCursor cur = cursor;
813 && !(cur.par()->params().spaceTop()
814 == VSpace (VSpace::NONE))) {
816 cur.par()->params().lineTop(),
817 cur.par()->params().lineBottom(),
818 cur.par()->params().pagebreakTop(),
819 cur.par()->params().pagebreakBottom(),
820 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
821 cur.par()->params().spacing(),
822 cur.par()->params().align(),
823 cur.par()->params().labelWidthString(), 0);
826 selection.cursor = cur;
830 cutSelection(bv, true);
835 case LFUN_BREAKPARAGRAPH:
836 bv->beforeChange(this);
837 breakParagraph(bv->buffer()->paragraphs, 0);
839 selection.cursor = cursor;
841 bv->owner()->view_state_changed();
844 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
845 bv->beforeChange(this);
846 breakParagraph(bv->buffer()->paragraphs, 1);
848 selection.cursor = cursor;
850 bv->owner()->view_state_changed();
853 case LFUN_BREAKPARAGRAPH_SKIP: {
854 // When at the beginning of a paragraph, remove
855 // indentation and add a "defskip" at the top.
856 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
857 LyXCursor cur = cursor;
858 bv->beforeChange(this);
859 if (cur.pos() == 0) {
860 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
862 cur.par()->params().lineTop(),
863 cur.par()->params().lineBottom(),
864 cur.par()->params().pagebreakTop(),
865 cur.par()->params().pagebreakBottom(),
866 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
867 cur.par()->params().spacing(),
868 cur.par()->params().align(),
869 cur.par()->params().labelWidthString(), 1);
873 breakParagraph(bv->buffer()->paragraphs, 0);
876 selection.cursor = cur;
878 bv->owner()->view_state_changed();
882 case LFUN_PARAGRAPH_SPACING: {
883 Paragraph * par = cursor.par();
884 Spacing::Space cur_spacing = par->params().spacing().getSpace();
885 float cur_value = 1.0;
886 if (cur_spacing == Spacing::Other)
887 cur_value = par->params().spacing().getValue();
889 istringstream is(cmd.argument.c_str());
892 Spacing::Space new_spacing = cur_spacing;
893 float new_value = cur_value;
895 lyxerr << "Missing argument to `paragraph-spacing'"
897 } else if (tmp == "single") {
898 new_spacing = Spacing::Single;
899 } else if (tmp == "onehalf") {
900 new_spacing = Spacing::Onehalf;
901 } else if (tmp == "double") {
902 new_spacing = Spacing::Double;
903 } else if (tmp == "other") {
904 new_spacing = Spacing::Other;
907 lyxerr << "new_value = " << tmpval << endl;
910 } else if (tmp == "default") {
911 new_spacing = Spacing::Default;
913 lyxerr << _("Unknown spacing argument: ")
914 << cmd.argument << endl;
916 if (cur_spacing != new_spacing || cur_value != new_value) {
917 par->params().spacing(Spacing(new_spacing, new_value));
924 case LFUN_INSET_TOGGLE:
926 bv->beforeChange(this);
933 case LFUN_PROTECTEDSPACE:
934 if (cursor.par()->layout()->free_spacing) {
938 specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
940 moveCursorUpdate(bv, false);
943 case LFUN_HYPHENATION:
944 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
947 case LFUN_LIGATURE_BREAK:
948 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
952 specialChar(this, bv, InsetSpecialChar::LDOTS);
955 case LFUN_END_OF_SENTENCE:
956 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
959 case LFUN_MENU_SEPARATOR:
960 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
964 bv->beforeChange(this);
966 selection.cursor = cursor;
967 cmd.message(N_("Mark off"));
971 bv->beforeChange(this);
972 selection.mark(true);
974 selection.cursor = cursor;
975 cmd.message(N_("Mark on"));
979 bv->beforeChange(this);
980 if (selection.mark()) {
982 cmd.message(N_("Mark removed"));
984 selection.mark(true);
986 cmd.message(N_("Mark set"));
988 selection.cursor = cursor;
991 case LFUN_UPCASE_WORD:
993 changeCase(LyXText::text_uppercase);
995 bv->updateInset(inset_owner);
999 case LFUN_LOWCASE_WORD:
1001 changeCase(LyXText::text_lowercase);
1003 bv->updateInset(inset_owner);
1007 case LFUN_CAPITALIZE_WORD:
1009 changeCase(LyXText::text_capitalization);
1011 bv->updateInset(inset_owner);
1015 case LFUN_TRANSPOSE_CHARS:
1019 bv->updateInset(inset_owner);
1024 cmd.message(_("Paste"));
1026 // clear the selection
1027 bv->toggleSelection();
1031 clearSelection(); // bug 393
1039 cutSelection(bv, true);
1041 cmd.message(_("Cut"));
1046 cmd.message(_("Copy"));
1049 case LFUN_BEGINNINGBUFSEL:
1051 return UNDISPATCHED;
1054 finishChange(bv, true);
1057 case LFUN_ENDBUFSEL:
1059 return UNDISPATCHED;
1062 finishChange(bv, true);
1066 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1072 istringstream is(cmd.argument.c_str());
1075 lyxerr << "SETXY: Could not parse coordinates in '"
1076 << cmd.argument << std::endl;
1078 setCursorFromCoordinates(x, y);
1083 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1085 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1091 case LFUN_GETLAYOUT:
1092 cmd.message(tostr(cursor.par()->layout()));
1096 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1097 << cmd.argument << endl;
1099 // This is not the good solution to the empty argument
1100 // problem, but it will hopefully suffice for 1.2.0.
1101 // The correct solution would be to augument the
1102 // function list/array with information about what
1103 // functions needs arguments and their type.
1104 if (cmd.argument.empty()) {
1105 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1109 // Derive layout number from given argument (string)
1110 // and current buffer's textclass (number)
1111 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1112 bool hasLayout = tclass.hasLayout(cmd.argument);
1113 string layout = cmd.argument;
1115 // If the entry is obsolete, use the new one instead.
1117 string const & obs = tclass[layout]->obsoleted_by();
1123 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1128 bool change_layout = (current_layout != layout);
1129 if (!change_layout && selection.set() &&
1130 selection.start.par() != selection.end.par())
1132 Paragraph * spar = selection.start.par();
1133 Paragraph * epar = selection.end.par()->next();
1134 while (spar != epar) {
1135 if (spar->layout()->name() != current_layout) {
1136 change_layout = true;
1139 spar = spar->next();
1142 if (change_layout) {
1144 current_layout = layout;
1147 bv->owner()->setLayout(layout);
1154 case LFUN_PASTESELECTION: {
1158 // this was originally a beforeChange(bv->text), i.e
1159 // the outermost LyXText!
1160 bv->beforeChange(this);
1161 string const clip = bv->getClipboard();
1162 if (!clip.empty()) {
1163 if (cmd.argument == "paragraph")
1164 insertStringAsParagraphs(clip);
1166 insertStringAsLines(clip);
1173 case LFUN_GOTOERROR:
1174 gotoInset(Inset::ERROR_CODE, false);
1178 gotoInset(Inset::NOTE_CODE, false);
1181 case LFUN_REFERENCE_GOTO:
1183 vector<Inset::Code> tmp;
1184 tmp.push_back(Inset::LABEL_CODE);
1185 tmp.push_back(Inset::REF_CODE);
1186 gotoInset(tmp, true);
1191 Paragraph const * par = cursor.par();
1192 lyx::pos_type pos = cursor.pos();
1196 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1199 c = par->getChar(pos - 1);
1202 LyXLayout_ptr const & style = par->layout();
1204 if (style->pass_thru ||
1205 par->getFontSettings(bv->buffer()->params,
1206 pos).language()->lang() == "hebrew" ||
1207 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1208 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1212 case LFUN_DATE_INSERT: {
1213 time_t now_time_t = time(NULL);
1214 struct tm * now_tm = localtime(&now_time_t);
1215 setlocale(LC_TIME, "");
1217 if (!cmd.argument.empty())
1220 arg = lyxrc.date_insert_format;
1222 int const datetmp_len =
1223 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1225 for (int i = 0; i < datetmp_len; i++) {
1226 insertChar(datetmp[i]);
1229 selection.cursor = cursor;
1230 moveCursorUpdate(bv, false);
1234 case LFUN_MOUSE_TRIPLE:
1237 if (!isInInset() && bv->theLockingInset())
1239 if (cmd.button() == mouse_button::button1) {
1241 bv->screen().hideCursor();
1242 bv->screen().toggleSelection(this, bv);
1245 selection.cursor = cursor;
1249 bv->screen().toggleSelection(this, bv, false);
1251 bv->haveSelection(selection.set());
1255 case LFUN_MOUSE_DOUBLE:
1258 if (!isInInset() && bv->theLockingInset())
1260 if (cmd.button() == mouse_button::button1) {
1262 bv->screen().hideCursor();
1263 bv->screen().toggleSelection(this, bv);
1264 selectWord(LyXText::WHOLE_WORD_STRICT);
1265 bv->screen().toggleSelection(this, bv, false);
1267 selectWord(LyXText::WHOLE_WORD_STRICT);
1270 bv->haveSelection(selection.set());
1274 case LFUN_MOUSE_MOTION:
1276 // Only use motion with button 1
1277 //if (ev.button() != mouse_button::button1)
1283 // Check for inset locking
1284 if (bv->theLockingInset()) {
1285 Inset * tli = bv->theLockingInset();
1286 LyXCursor cursor = bv->text->cursor;
1287 LyXFont font = bv->text->getFont(bv->buffer(),
1288 cursor.par(), cursor.pos());
1289 int width = tli->width(bv, font);
1290 int inset_x = font.isVisibleRightToLeft()
1291 ? cursor.ix() - width : cursor.ix();
1292 int start_x = inset_x + tli->scroll();
1293 FuncRequest cmd1 = cmd;
1294 cmd1.x = cmd.x - start_x;
1295 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1296 tli->localDispatch(cmd1);
1300 // The test for not selection possible is needed, that only motion
1301 // events are used, where the bottom press event was on
1302 // the drawing area too
1303 if (!selection_possible) {
1304 lyxerr[Debug::ACTION]
1305 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1309 bv->screen().hideCursor();
1311 Row * cursorrow = bv->text->cursor.row();
1312 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->text->top_y());
1314 // sorry for this but I have a strange error that the y value jumps at
1315 // a certain point. This seems like an error in my xforms library or
1316 // in some other local environment, but I would like to leave this here
1317 // for the moment until I can remove this (Jug 20020418)
1318 if (y_before < bv->text->cursor.y())
1319 lyxerr << y_before << ':'
1320 << bv->text->cursor.y() << endl;
1322 // This is to allow jumping over large insets
1323 if (cursorrow == bv->text->cursor.row()) {
1324 if (cmd.y >= bv->workHeight())
1325 bv->text->cursorDown(false);
1327 bv->text->cursorUp(false);
1330 // Maybe an empty line was deleted
1331 if (!bv->text->selection.set())
1332 bv->update(BufferView::UPDATE);
1333 bv->text->setSelection();
1334 bv->screen().toggleToggle(bv->text, bv);
1340 // Single-click on work area
1341 case LFUN_MOUSE_PRESS:
1346 // ok ok, this is a hack (for xforms)
1347 // We shouldn't go further down as we really should only do the
1348 // scrolling and be done with this. Otherwise we may open some
1349 // dialogs (Jug 20020424).
1350 if (cmd.button() == mouse_button::button4) {
1351 bv->scroll(-lyxrc.wheel_jump);
1354 if (cmd.button() == mouse_button::button5) {
1355 bv->scroll(lyxrc.wheel_jump);
1361 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1363 // Middle button press pastes if we have a selection
1364 // We do this here as if the selection was inside an inset
1365 // it could get cleared on the unlocking of the inset so
1366 // we have to check this first
1367 bool paste_internally = false;
1368 if (cmd.button() == mouse_button::button2 && selection.set()) {
1369 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1370 paste_internally = true;
1373 int const screen_first = bv->text->top_y();
1375 if (bv->theLockingInset()) {
1376 // We are in inset locking mode
1378 // Check whether the inset was hit. If not reset mode,
1379 // otherwise give the event to the inset
1380 if (inset_hit == bv->theLockingInset()) {
1381 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1382 bv->theLockingInset()->localDispatch(cmd1);
1385 bv->unlockInset(bv->theLockingInset());
1389 selection_possible = true;
1390 bv->screen().hideCursor();
1392 // Clear the selection
1393 bv->screen().toggleSelection(bv->text, bv);
1394 bv->text->clearSelection();
1395 bv->text->fullRebreak();
1397 bv->updateScrollbar();
1399 // Single left click in math inset?
1400 if (isHighlyEditableInset(inset_hit)) {
1401 // Highly editable inset, like math
1402 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1403 selection_possible = false;
1404 bv->owner()->message(inset->editMessage());
1405 //inset->edit(bv, x, y, cmd.button());
1406 // We just have to lock the inset before calling a PressEvent on it!
1407 // we don't need the edit() call here! (Jug20020329)
1408 if (!bv->lockInset(inset))
1409 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1410 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1411 inset->localDispatch(cmd1);
1414 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1416 // Right click on a footnote flag opens float menu
1417 if (cmd.button() == mouse_button::button3) {
1418 selection_possible = false;
1422 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1423 bv->text->setCursorFromCoordinates(x, y + screen_first);
1425 bv->text->selection.cursor = bv->text->cursor;
1426 bv->text->cursor.x_fix(bv->text->cursor.x());
1428 if (bv->fitCursor())
1429 selection_possible = false;
1431 // Insert primary selection with middle mouse
1432 // if there is a local selection in the current buffer,
1434 if (cmd.button() == mouse_button::button2) {
1435 if (paste_internally)
1436 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1438 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1439 selection_possible = false;
1444 case LFUN_MOUSE_RELEASE:
1446 // do nothing if we used the mouse wheel
1450 if (cmd.button() == mouse_button::button4
1451 || cmd.button() == mouse_button::button5)
1454 // If we hit an inset, we have the inset coordinates in these
1455 // and inset_hit points to the inset. If we do not hit an
1456 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1459 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1461 if (bv->theLockingInset()) {
1462 // We are in inset locking mode.
1464 // LyX does a kind of work-area grabbing for insets.
1465 // Only a ButtonPress FuncRequest outside the inset will
1466 // force a insetUnlock.
1467 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1468 bv->theLockingInset()->localDispatch(cmd1);
1472 selection_possible = false;
1474 if (cmd.button() == mouse_button::button2)
1478 if (cmd.button() == mouse_button::button1)
1479 bv->haveSelection(selection.set());
1482 bv->owner()->view_state_changed();
1483 bv->owner()->updateMenubar();
1484 bv->owner()->updateToolbar();
1486 // Did we hit an editable inset?
1488 selection_possible = false;
1490 // if we reach this point with a selection, it
1491 // must mean we are currently selecting.
1492 // But we don't want to open the inset
1493 // because that is annoying for the user.
1494 // So just pretend we didn't hit it.
1495 // this is OK because a "kosher" ButtonRelease
1496 // will follow a ButtonPress that clears
1498 // Note this also fixes selection drawing
1499 // problems if we end up opening an inset
1500 if (selection.set())
1503 // CHECK fix this proper in 0.13
1504 // well, maybe 13.0 !!!!!!!!!
1506 // Following a ref shouldn't issue
1507 // a push on the undo-stack
1508 // anylonger, now that we have
1509 // keybindings for following
1510 // references and returning from
1511 // references. IMHO though, it
1512 // should be the inset's own business
1513 // to push or not push on the undo
1514 // stack. They don't *have* to
1515 // alter the document...
1517 // ...or maybe the SetCursorParUndo()
1518 // below isn't necessary at all anylonger?
1519 if (inset_hit->lyxCode() == Inset::REF_CODE)
1520 setCursorParUndo(bv);
1522 bv->owner()->message(inset_hit->editMessage());
1524 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1525 inset_hit->localDispatch(cmd1);
1531 case LFUN_SELFINSERT: {
1532 if (cmd.argument.empty())
1535 // Automatically delete the currently selected
1536 // text and replace it with what is being
1537 // typed in now. Depends on lyxrc settings
1538 // "auto_region_delete", which defaults to
1541 if (lyxrc.auto_region_delete) {
1542 if (selection.set()) {
1543 cutSelection(false, false);
1546 bv->haveSelection(false);
1549 bv->beforeChange(this);
1550 LyXFont const old_font(real_current_font);
1552 string::const_iterator cit = cmd.argument.begin();
1553 string::const_iterator end = cmd.argument.end();
1554 for (; cit != end; ++cit)
1555 bv->owner()->getIntl().getTransManager().
1556 TranslateAndInsert(*cit, this);
1559 selection.cursor = cursor;
1560 moveCursorUpdate(bv, false);
1562 // real_current_font.number can change so we need to
1563 // update the minibuffer
1564 if (old_font != real_current_font)
1565 bv->owner()->view_state_changed();
1569 case LFUN_HTMLURL: {
1570 InsetCommandParams p("htmlurl");
1571 string const data = InsetCommandMailer::params2string("url", p);
1572 bv->owner()->getDialogs().show("url", data, 0);
1577 InsetCommandParams p("url");
1578 string const data = InsetCommandMailer::params2string("url", p);
1579 bv->owner()->getDialogs().show("url", data, 0);
1585 case LFUN_INSET_LIST:
1586 case LFUN_INSET_THEOREM:
1587 case LFUN_INSET_CAPTION:
1589 case LFUN_INSERT_NOTE:
1590 case LFUN_INSERT_BIBITEM:
1591 case LFUN_INSET_ERT:
1592 case LFUN_INSET_FLOAT:
1593 case LFUN_INSET_FOOTNOTE:
1594 case LFUN_INSET_MARGINAL:
1595 case LFUN_INSET_MINIPAGE:
1596 case LFUN_INSET_OPTARG:
1597 case LFUN_INSET_WIDE_FLOAT:
1598 case LFUN_INSET_WRAP:
1599 case LFUN_TABULAR_INSERT:
1600 case LFUN_ENVIRONMENT_INSERT:
1601 // Open the inset, and move the current selection
1603 doInsertInset(this, cmd, true, true);
1606 case LFUN_INDEX_INSERT:
1607 // Just open the inset
1608 doInsertInset(this, cmd, true, false);
1611 case LFUN_INDEX_PRINT:
1612 case LFUN_PARENTINSERT:
1613 case LFUN_TOC_INSERT:
1616 doInsertInset(this, cmd, false, false);
1620 return UNDISPATCHED;