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/WorkArea.h"
32 #include "insets/insetspecialchar.h"
33 #include "insets/insettext.h"
34 #include "insets/insetbib.h"
35 #include "insets/insetquotes.h"
36 #include "insets/insetcommand.h"
37 #include "undo_funcs.h"
44 extern string current_layout;
45 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
47 // the selection possible is needed, that only motion events are
48 // used, where the bottom press event was on the drawing area too
49 bool selection_possible = false;
54 void moveCursorUpdate(BufferView * bv, bool selecting)
56 LyXText * lt = bv->getLyXText();
58 if (selecting || lt->selection.mark()) {
63 bv->updateInset(lt->inset_owner, false);
67 // bv->update(lt, BufferView::SELECT|BufferView::FITCUR);
69 bv->update(lt, BufferView::SELECT|BufferView::FITCUR);
71 } else if (bv->text->status() != LyXText::UNCHANGED) {
72 bv->theLockingInset()->hideInsetCursor(bv);
73 bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
77 if (!lt->selection.set())
78 bv->workarea().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 const & 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 " << x << "," << y
126 << " box " << b << endl;
130 text.setCursor(bv, &par, pos, true);
133 // The origin of an inset is on the baseline
134 y -= text.cursor.iy();
142 Inset * LyXText::checkInsetHit(BufferView * bv, int & x, int & y) const
144 int y_tmp = y + first_y;
147 setCursorFromCoordinates(bv, cur, x, y_tmp);
149 Inset * inset = checkInset(bv, *this, cur, x, y_tmp);
155 // look at previous position
160 setCursor(bv, cur, cur.par(), cur.pos() - 1, true);
162 inset = checkInset(bv, *this, cur, x, y_tmp);
169 bool LyXText::gotoNextInset(BufferView * bv,
170 vector<Inset::Code> const & codes, string const & contents) const
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(bv, res.par(), res.pos(), false);
200 void LyXText::gotoInset(BufferView * bv, 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(bv, codes, contents)) {
216 if (cursor.pos() || cursor.par() != ownerParagraph()) {
217 LyXCursor tmp = cursor;
218 cursor.par(ownerParagraph());
220 if (!gotoNextInset(bv, codes, contents)) {
222 bv->owner()->message(_("No more insets"));
225 bv->owner()->message(_("No more insets"));
229 selection.cursor = cursor;
233 void LyXText::gotoInset(BufferView * bv, Inset::Code code, bool same_content)
235 gotoInset(bv, vector<Inset::Code>(1, code), same_content);
239 void LyXText::cursorPrevious(BufferView * bv)
241 if (!cursor.row()->previous()) {
243 int new_y = bv->text->first_y - bv->workarea().workHeight();
244 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
245 bv->updateScrollbar();
251 Row * cursorrow = cursor.row();
253 setCursorFromCoordinates(bv, cursor.x_fix(), y);
257 if (cursorrow == bv->text->cursor.row()) {
258 // we have a row which is higher than the workarea so we leave the
259 // cursor on the start of the row and move only the draw up as soon
260 // as we move the cursor or do something while inside the row (it may
261 // span several workarea-heights) we'll move to the top again, but this
262 // is better than just jump down and only display part of the row.
263 new_y = bv->text->first_y - bv->workarea().workHeight();
266 new_y = bv->text->cursor.iy()
267 + bv->theLockingInset()->insetInInsetY() + y
268 + cursor.row()->height()
269 - bv->workarea().workHeight() + 1;
272 - cursor.row()->baseline()
273 + cursor.row()->height()
274 - bv->workarea().workHeight() + 1;
277 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
278 if (cursor.row()->previous()) {
280 setCursor(bv, cur, cursor.row()->previous()->par(),
281 cursor.row()->previous()->pos(), false);
282 if (cur.y() > first_y) {
286 bv->updateScrollbar();
290 void LyXText::cursorNext(BufferView * bv)
292 if (!cursor.row()->next()) {
293 int y = cursor.y() - cursor.row()->baseline() +
294 cursor.row()->height();
295 if (y > int(first_y + bv->workarea().workHeight())) {
296 bv->screen().draw(bv->text, bv,
297 bv->text->first_y + bv->workarea().workHeight());
298 bv->updateScrollbar();
303 int y = first_y + bv->workarea().workHeight();
304 if (inset_owner && !first_y) {
305 y -= (bv->text->cursor.iy()
307 + bv->theLockingInset()->insetInInsetY());
312 Row * cursorrow = cursor.row();
313 setCursorFromCoordinates(bv, cursor.x_fix(), y);
314 // + workarea().workHeight());
318 if (cursorrow == bv->text->cursor.row()) {
319 // we have a row which is higher than the workarea so we leave the
320 // cursor on the start of the row and move only the draw down as soon
321 // as we move the cursor or do something while inside the row (it may
322 // span several workarea-heights) we'll move to the top again, but this
323 // is better than just jump down and only display part of the row.
324 new_y = bv->text->first_y + bv->workarea().workHeight();
327 new_y = bv->text->cursor.iy()
328 + bv->theLockingInset()->insetInInsetY()
329 + y - cursor.row()->baseline();
331 new_y = cursor.y() - cursor.row()->baseline();
334 bv->screen().draw(bv->text, bv, new_y);
335 if (cursor.row()->next()) {
337 setCursor(bv, cur, cursor.row()->next()->par(),
338 cursor.row()->next()->pos(), false);
339 if (cur.y() < int(first_y + bv->workarea().workHeight())) {
340 cursorDown(bv, true);
343 bv->updateScrollbar();
347 void LyXText::update(BufferView * bv, bool changed)
349 BufferView::UpdateCodes c = BufferView::SELECT | BufferView::FITCUR;
351 bv->update(this, c | BufferView::CHANGE);
357 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
361 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
362 if (!bv->insertInset(new_inset))
365 bv->updateInset(new_inset, true);
369 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
371 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
372 <<"] arg[" << cmd.argument << "]" << endl;
374 BufferView * bv = cmd.view();
376 switch (cmd.action) {
378 case LFUN_APPENDIX: {
379 Paragraph * par = cursor.par();
380 bool start = !par->params().startOfAppendix();
382 // ensure that we have only one start_of_appendix in this document
383 Paragraph * tmp = ownerParagraph();
384 for (; tmp; tmp = tmp->next())
385 tmp->params().startOfAppendix(false);
387 par->params().startOfAppendix(start);
389 // we can set the refreshing parameters now
390 status(cmd.view(), LyXText::NEED_MORE_REFRESH);
392 refresh_row = 0; // not needed for full update
393 updateCounters(cmd.view());
394 setCursor(cmd.view(), cursor.par(), cursor.pos());
399 case LFUN_DELETE_WORD_FORWARD:
400 bv->beforeChange(this);
402 deleteWordForward(bv);
407 case LFUN_DELETE_WORD_BACKWARD:
408 bv->beforeChange(this);
410 deleteWordBackward(bv);
415 case LFUN_DELETE_LINE_FORWARD:
416 bv->beforeChange(this);
418 deleteLineForward(bv);
425 if (!selection.mark())
426 bv->beforeChange(this);
433 if (!selection.mark())
434 bv->beforeChange(this);
436 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
437 cursorLeftOneWord(bv);
439 cursorRightOneWord(bv);
444 if (!selection.mark())
445 bv->beforeChange(this);
447 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
448 cursorRightOneWord(bv);
450 cursorLeftOneWord(bv);
454 case LFUN_BEGINNINGBUF:
455 if (!selection.mark())
456 bv->beforeChange(this);
463 if (selection.mark())
464 bv->beforeChange(this);
472 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
476 finishChange(bv, true);
481 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
485 finishChange(bv, true);
491 finishChange(bv, true);
496 cursorDown(bv, true);
497 finishChange(bv, true);
500 case LFUN_UP_PARAGRAPHSEL:
502 cursorUpParagraph(bv);
503 finishChange(bv, true);
506 case LFUN_DOWN_PARAGRAPHSEL:
508 cursorDownParagraph(bv);
509 finishChange(bv, true);
515 finishChange(bv, true);
521 finishChange(bv, true);
527 finishChange(bv, true);
533 finishChange(bv, true);
536 case LFUN_WORDRIGHTSEL:
538 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
539 cursorLeftOneWord(bv);
541 cursorRightOneWord(bv);
542 finishChange(bv, true);
545 case LFUN_WORDLEFTSEL:
547 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
548 cursorRightOneWord(bv);
550 cursorLeftOneWord(bv);
551 finishChange(bv, true);
555 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
556 if (!selection.mark())
557 bv->beforeChange(this);
560 cursorLeft(bv, false);
561 if (cursor.pos() < cursor.par()->size()
562 && cursor.par()->isInset(cursor.pos())
563 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
564 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
565 cmd.message(tmpinset->editMessage());
566 tmpinset->edit(bv, !is_rtl);
570 cursorRight(bv, false);
576 // This is soooo ugly. Isn`t it possible to make
578 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
579 if (!selection.mark())
580 bv->beforeChange(this);
582 LyXCursor const cur = cursor;
584 cursorLeft(bv, false);
585 if ((is_rtl || cur != cursor) && // only if really moved!
586 cursor.pos() < cursor.par()->size() &&
587 cursor.par()->isInset(cursor.pos()) &&
588 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
589 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
590 cmd.message(tmpinset->editMessage());
591 tmpinset->edit(bv, is_rtl);
595 cursorRight(bv, false);
601 if (!selection.mark())
602 bv->beforeChange(this);
603 bv->update(this, BufferView::UPDATE);
609 if (!selection.mark())
610 bv->beforeChange(this);
611 bv->update(this, BufferView::UPDATE);
616 case LFUN_UP_PARAGRAPH:
617 if (!selection.mark())
618 bv->beforeChange(this);
619 bv->update(this, BufferView::UPDATE);
620 cursorUpParagraph(bv);
624 case LFUN_DOWN_PARAGRAPH:
625 if (!selection.mark())
626 bv->beforeChange(this);
627 bv->update(this, BufferView::UPDATE);
628 cursorDownParagraph(bv);
629 finishChange(bv, false);
633 if (!selection.mark())
634 bv->beforeChange(this);
635 bv->update(this, BufferView::UPDATE);
637 finishChange(bv, false);
640 // moveCursorUpdate(bv, false, false);
641 // owner_->view_state_changed();
645 if (!selection.mark())
646 bv->beforeChange(this);
647 bv->update(this, BufferView::UPDATE);
649 finishChange(bv, false);
653 if (!selection.mark())
654 bv->beforeChange(this);
657 finishChange(bv, false);
661 if (!selection.mark())
662 bv->beforeChange(this);
665 finishChange(bv, false);
669 bv->beforeChange(this);
670 insertChar(bv, Paragraph::META_NEWLINE);
672 setCursor(bv, cursor.par(), cursor.pos());
673 moveCursorUpdate(bv, false);
677 if (!selection.set()) {
679 selection.cursor = cursor;
681 // It is possible to make it a lot faster still
682 // just comment out the line below...
686 cutSelection(bv, true);
689 moveCursorUpdate(bv, false);
690 bv->owner()->view_state_changed();
694 case LFUN_DELETE_SKIP:
695 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
696 if (!selection.set()) {
697 LyXCursor cur = cursor;
698 if (cur.pos() == cur.par()->size()) {
702 && !(cur.par()->params().spaceTop()
703 == VSpace (VSpace::NONE))) {
705 cur.par()->params().lineTop(),
706 cur.par()->params().lineBottom(),
707 cur.par()->params().pagebreakTop(),
708 cur.par()->params().pagebreakBottom(),
709 VSpace(VSpace::NONE),
710 cur.par()->params().spaceBottom(),
711 cur.par()->params().spacing(),
712 cur.par()->params().align(),
713 cur.par()->params().labelWidthString(), 0);
719 selection.cursor = cursor;
723 selection.cursor = cursor;
727 cutSelection(bv, true);
734 if (!selection.set()) {
735 if (bv->owner()->getIntl().getTransManager().backspace()) {
737 selection.cursor = cursor;
739 // It is possible to make it a lot faster still
740 // just comment out the line below...
745 cutSelection(bv, true);
748 bv->owner()->view_state_changed();
752 case LFUN_BACKSPACE_SKIP:
753 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
754 if (!selection.set()) {
755 LyXCursor cur = cursor;
757 && !(cur.par()->params().spaceTop()
758 == VSpace (VSpace::NONE))) {
760 cur.par()->params().lineTop(),
761 cur.par()->params().lineBottom(),
762 cur.par()->params().pagebreakTop(),
763 cur.par()->params().pagebreakBottom(),
764 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
765 cur.par()->params().spacing(),
766 cur.par()->params().align(),
767 cur.par()->params().labelWidthString(), 0);
770 selection.cursor = cur;
774 cutSelection(bv, true);
779 case LFUN_BREAKPARAGRAPH:
780 bv->beforeChange(this);
781 breakParagraph(bv, 0);
783 selection.cursor = cursor;
785 bv->owner()->view_state_changed();
788 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
789 bv->beforeChange(this);
790 breakParagraph(bv, 1);
792 selection.cursor = cursor;
794 bv->owner()->view_state_changed();
797 case LFUN_BREAKPARAGRAPH_SKIP: {
798 // When at the beginning of a paragraph, remove
799 // indentation and add a "defskip" at the top.
800 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
801 LyXCursor cur = cursor;
802 bv->beforeChange(this);
803 if (cur.pos() == 0) {
804 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
806 cur.par()->params().lineTop(),
807 cur.par()->params().lineBottom(),
808 cur.par()->params().pagebreakTop(),
809 cur.par()->params().pagebreakBottom(),
810 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
811 cur.par()->params().spacing(),
812 cur.par()->params().align(),
813 cur.par()->params().labelWidthString(), 1);
818 breakParagraph(bv, 0);
822 selection.cursor = cur;
824 bv->owner()->view_state_changed();
828 case LFUN_PARAGRAPH_SPACING: {
829 Paragraph * par = cursor.par();
830 Spacing::Space cur_spacing = par->params().spacing().getSpace();
831 float cur_value = 1.0;
832 if (cur_spacing == Spacing::Other)
833 cur_value = par->params().spacing().getValue();
835 istringstream is(cmd.argument.c_str());
838 Spacing::Space new_spacing = cur_spacing;
839 float new_value = cur_value;
841 lyxerr << "Missing argument to `paragraph-spacing'"
843 } else if (tmp == "single") {
844 new_spacing = Spacing::Single;
845 } else if (tmp == "onehalf") {
846 new_spacing = Spacing::Onehalf;
847 } else if (tmp == "double") {
848 new_spacing = Spacing::Double;
849 } else if (tmp == "other") {
850 new_spacing = Spacing::Other;
853 lyxerr << "new_value = " << tmpval << endl;
856 } else if (tmp == "default") {
857 new_spacing = Spacing::Default;
859 lyxerr << _("Unknown spacing argument: ")
860 << cmd.argument << endl;
862 if (cur_spacing != new_spacing || cur_value != new_value) {
863 par->params().spacing(Spacing(new_spacing, new_value));
870 case LFUN_INSET_TOGGLE:
872 bv->beforeChange(this);
879 case LFUN_PROTECTEDSPACE:
880 if (cursor.par()->layout()->free_spacing) {
884 specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
886 moveCursorUpdate(bv, false);
889 case LFUN_HYPHENATION:
890 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
893 case LFUN_LIGATURE_BREAK:
894 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
898 specialChar(this, bv, InsetSpecialChar::LDOTS);
904 insertChar(bv, Paragraph::META_HFILL);
908 case LFUN_END_OF_SENTENCE:
909 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
912 case LFUN_MENU_SEPARATOR:
913 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
917 bv->beforeChange(this);
919 selection.cursor = cursor;
920 cmd.message(N_("Mark off"));
924 bv->beforeChange(this);
925 selection.mark(true);
927 selection.cursor = cursor;
928 cmd.message(N_("Mark on"));
932 bv->beforeChange(this);
933 if (selection.mark()) {
935 cmd.message(N_("Mark removed"));
937 selection.mark(true);
939 cmd.message(N_("Mark set"));
941 selection.cursor = cursor;
944 case LFUN_UPCASE_WORD:
946 changeCase(bv, LyXText::text_uppercase);
948 bv->updateInset(inset_owner, true);
952 case LFUN_LOWCASE_WORD:
954 changeCase(bv, LyXText::text_lowercase);
956 bv->updateInset(inset_owner, true);
960 case LFUN_CAPITALIZE_WORD:
962 changeCase(bv, LyXText::text_capitalization);
964 bv->updateInset(inset_owner, true);
968 case LFUN_TRANSPOSE_CHARS:
972 bv->updateInset(inset_owner, true);
977 cmd.message(_("Paste"));
979 // clear the selection
980 bv->toggleSelection();
984 clearSelection(); // bug 393
993 cutSelection(bv, true);
995 cmd.message(_("Cut"));
1000 cmd.message(_("Copy"));
1003 case LFUN_BEGINNINGBUFSEL:
1005 return Inset::UNDISPATCHED;
1008 finishChange(bv, true);
1011 case LFUN_ENDBUFSEL:
1013 return Inset::UNDISPATCHED;
1016 finishChange(bv, true);
1020 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1026 istringstream is(cmd.argument.c_str());
1029 lyxerr << "SETXY: Could not parse coordinates in '"
1030 << cmd.argument << std::endl;
1032 setCursorFromCoordinates(bv, x, y);
1037 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1039 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1045 case LFUN_GETLAYOUT:
1046 cmd.message(tostr(cursor.par()->layout()));
1050 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1051 << cmd.argument << endl;
1053 // This is not the good solution to the empty argument
1054 // problem, but it will hopefully suffice for 1.2.0.
1055 // The correct solution would be to augument the
1056 // function list/array with information about what
1057 // functions needs arguments and their type.
1058 if (cmd.argument.empty()) {
1059 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1063 // Derive layout number from given argument (string)
1064 // and current buffer's textclass (number)
1065 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1066 bool hasLayout = tclass.hasLayout(cmd.argument);
1067 string layout = cmd.argument;
1069 // If the entry is obsolete, use the new one instead.
1071 string const & obs = tclass[layout]->obsoleted_by();
1077 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1082 bool change_layout = (current_layout != layout);
1083 if (!change_layout && selection.set() &&
1084 selection.start.par() != selection.end.par())
1086 Paragraph * spar = selection.start.par();
1087 Paragraph * epar = selection.end.par()->next();
1088 while (spar != epar) {
1089 if (spar->layout()->name() != current_layout) {
1090 change_layout = true;
1095 if (change_layout) {
1097 current_layout = layout;
1099 setLayout(bv, layout);
1100 bv->owner()->setLayout(layout);
1107 case LFUN_PASTESELECTION: {
1111 // this was originally a beforeChange(bv->text), i.e
1112 // the outermost LyXText!
1113 bv->beforeChange(this);
1114 string const clip = bv->workarea().getClipboard();
1115 if (!clip.empty()) {
1116 if (cmd.argument == "paragraph")
1117 insertStringAsParagraphs(bv, clip);
1119 insertStringAsLines(bv, clip);
1126 case LFUN_GOTOERROR:
1127 gotoInset(bv, Inset::ERROR_CODE, false);
1131 gotoInset(bv, Inset::NOTE_CODE, false);
1134 case LFUN_REFERENCE_GOTO:
1136 vector<Inset::Code> tmp;
1137 tmp.push_back(Inset::LABEL_CODE);
1138 tmp.push_back(Inset::REF_CODE);
1139 gotoInset(bv, tmp, true);
1144 Paragraph const * par = cursor.par();
1145 lyx::pos_type pos = cursor.pos();
1149 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1152 c = par->getChar(pos - 1);
1155 LyXLayout_ptr const & style = par->layout();
1157 if (style->pass_thru ||
1158 par->getFontSettings(bv->buffer()->params,
1159 pos).language()->lang() == "hebrew" ||
1160 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1161 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1165 case LFUN_DATE_INSERT: {
1166 time_t now_time_t = time(NULL);
1167 struct tm * now_tm = localtime(&now_time_t);
1168 setlocale(LC_TIME, "");
1170 if (!cmd.argument.empty())
1173 arg = lyxrc.date_insert_format;
1175 int const datetmp_len =
1176 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1178 for (int i = 0; i < datetmp_len; i++) {
1179 insertChar(bv, datetmp[i]);
1182 selection.cursor = cursor;
1183 moveCursorUpdate(bv, false);
1187 case LFUN_MOUSE_TRIPLE:
1190 if (bv_owner && bv->theLockingInset())
1192 if (cmd.button() == mouse_button::button1) {
1194 bv->screen().hideCursor();
1195 bv->screen().toggleSelection(this, bv);
1198 selection.cursor = cursor;
1202 bv->screen().toggleSelection(this, bv, false);
1204 bv->workarea().haveSelection(selection.set());
1208 case LFUN_MOUSE_DOUBLE:
1211 if (bv_owner && bv->theLockingInset())
1213 if (cmd.button() == mouse_button::button1) {
1215 bv->screen().hideCursor();
1216 bv->screen().toggleSelection(this, bv);
1217 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1218 bv->screen().toggleSelection(this, bv, false);
1220 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1223 bv->workarea().haveSelection(selection.set());
1227 case LFUN_MOUSE_MOTION:
1229 // Only use motion with button 1
1230 //if (ev.button() != mouse_button::button1)
1236 // Check for inset locking
1237 if (bv->theLockingInset()) {
1238 Inset * tli = bv->theLockingInset();
1239 LyXCursor cursor = bv->text->cursor;
1240 LyXFont font = bv->text->getFont(bv->buffer(),
1241 cursor.par(), cursor.pos());
1242 int width = tli->width(bv, font);
1243 int inset_x = font.isVisibleRightToLeft()
1244 ? cursor.ix() - width : cursor.ix();
1245 int start_x = inset_x + tli->scroll();
1246 FuncRequest cmd1 = cmd;
1247 cmd1.x = cmd.x - start_x;
1248 cmd1.y = cmd.y - cursor.iy() + bv->text->first_y;
1249 tli->localDispatch(cmd1);
1253 // The test for not selection possible is needed, that only motion
1254 // events are used, where the bottom press event was on
1255 // the drawing area too
1256 if (!selection_possible) {
1257 lyxerr[Debug::ACTION]
1258 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1262 bv->screen().hideCursor();
1264 Row * cursorrow = bv->text->cursor.row();
1265 bv->text->setCursorFromCoordinates(bv, cmd.x, cmd.y + bv->text->first_y);
1267 // sorry for this but I have a strange error that the y value jumps at
1268 // a certain point. This seems like an error in my xforms library or
1269 // in some other local environment, but I would like to leave this here
1270 // for the moment until I can remove this (Jug 20020418)
1271 if (y_before < bv->text->cursor.y())
1272 lyxerr << y_before << ":" << bv->text->cursor.y() << endl;
1274 // This is to allow jumping over large insets
1275 if (cursorrow == bv->text->cursor.row()) {
1276 if (cmd.y >= int(bv->workarea().workHeight()))
1277 bv->text->cursorDown(bv, false);
1279 bv->text->cursorUp(bv, false);
1282 // Maybe an empty line was deleted
1283 if (!bv->text->selection.set())
1284 bv->update(bv->text, BufferView::UPDATE);
1285 bv->text->setSelection(bv);
1286 bv->screen().toggleToggle(bv->text, bv);
1292 // Single-click on work area
1293 case LFUN_MOUSE_PRESS:
1298 // ok ok, this is a hack (for xforms)
1299 // We shouldn't go further down as we really should only do the
1300 // scrolling and be done with this. Otherwise we may open some
1301 // dialogs (Jug 20020424).
1302 if (cmd.button() == mouse_button::button4) {
1303 bv->scroll(-lyxrc.wheel_jump);
1306 if (cmd.button() == mouse_button::button5) {
1307 bv->scroll(lyxrc.wheel_jump);
1313 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1315 // Middle button press pastes if we have a selection
1316 // We do this here as if the selection was inside an inset
1317 // it could get cleared on the unlocking of the inset so
1318 // we have to check this first
1319 bool paste_internally = false;
1320 if (cmd.button() == mouse_button::button2 && selection.set()) {
1321 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1322 paste_internally = true;
1325 int const screen_first = bv->text->first_y;
1327 if (bv->theLockingInset()) {
1328 // We are in inset locking mode
1330 // Check whether the inset was hit. If not reset mode,
1331 // otherwise give the event to the inset
1332 if (inset_hit == bv->theLockingInset()) {
1333 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1334 bv->theLockingInset()->localDispatch(cmd1);
1337 bv->unlockInset(bv->theLockingInset());
1341 selection_possible = true;
1342 bv->screen().hideCursor();
1344 // Clear the selection
1345 bv->screen().toggleSelection(bv->text, bv);
1346 bv->text->clearSelection();
1347 bv->text->fullRebreak(bv);
1349 bv->updateScrollbar();
1351 // Single left click in math inset?
1352 if (isHighlyEditableInset(inset_hit)) {
1353 // Highly editable inset, like math
1354 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1355 selection_possible = false;
1356 bv->owner()->updateLayoutChoice();
1357 bv->owner()->message(inset->editMessage());
1358 //inset->edit(bv, x, y, cmd.button());
1359 // We just have to lock the inset before calling a PressEvent on it!
1360 // we don't need the edit() call here! (Jug20020329)
1361 if (!bv->lockInset(inset))
1362 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1363 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1364 inset->localDispatch(cmd1);
1367 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1369 // Right click on a footnote flag opens float menu
1370 if (cmd.button() == mouse_button::button3) {
1371 selection_possible = false;
1375 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1376 bv->text->setCursorFromCoordinates(bv, x, y + screen_first);
1378 bv->text->selection.cursor = bv->text->cursor;
1379 bv->text->cursor.x_fix(bv->text->cursor.x());
1381 bv->owner()->updateLayoutChoice();
1382 if (bv->fitCursor())
1383 selection_possible = false;
1385 // Insert primary selection with middle mouse
1386 // if there is a local selection in the current buffer,
1388 if (cmd.button() == mouse_button::button2) {
1389 if (paste_internally)
1390 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1392 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1393 selection_possible = false;
1398 case LFUN_MOUSE_RELEASE:
1400 // do nothing if we used the mouse wheel
1404 if (cmd.button() == mouse_button::button4
1405 || cmd.button() == mouse_button::button5)
1408 // If we hit an inset, we have the inset coordinates in these
1409 // and inset_hit points to the inset. If we do not hit an
1410 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1413 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1415 if (bv->theLockingInset()) {
1416 // We are in inset locking mode.
1418 // LyX does a kind of work-area grabbing for insets.
1419 // Only a ButtonPress FuncRequest outside the inset will
1420 // force a insetUnlock.
1421 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1422 bv->theLockingInset()->localDispatch(cmd1);
1426 selection_possible = false;
1428 if (cmd.button() == mouse_button::button2)
1432 if (cmd.button() == mouse_button::button1)
1433 bv->workarea().haveSelection(selection.set());
1436 bv->owner()->view_state_changed();
1437 bv->owner()->updateMenubar();
1438 bv->owner()->updateToolbar();
1440 // Did we hit an editable inset?
1442 selection_possible = false;
1444 // if we reach this point with a selection, it
1445 // must mean we are currently selecting.
1446 // But we don't want to open the inset
1447 // because that is annoying for the user.
1448 // So just pretend we didn't hit it.
1449 // this is OK because a "kosher" ButtonRelease
1450 // will follow a ButtonPress that clears
1452 // Note this also fixes selection drawing
1453 // problems if we end up opening an inset
1454 if (selection.set())
1457 // CHECK fix this proper in 0.13
1458 // well, maybe 13.0 !!!!!!!!!
1460 // Following a ref shouldn't issue
1461 // a push on the undo-stack
1462 // anylonger, now that we have
1463 // keybindings for following
1464 // references and returning from
1465 // references. IMHO though, it
1466 // should be the inset's own business
1467 // to push or not push on the undo
1468 // stack. They don't *have* to
1469 // alter the document...
1471 // ...or maybe the SetCursorParUndo()
1472 // below isn't necessary at all anylonger?
1473 if (inset_hit->lyxCode() == Inset::REF_CODE)
1474 setCursorParUndo(bv);
1476 bv->owner()->message(inset_hit->editMessage());
1478 if (isHighlyEditableInset(inset_hit)) {
1479 // Highly editable inset, like math
1480 UpdatableInset * inset = (UpdatableInset *) inset_hit;
1481 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1482 inset->localDispatch(cmd1);
1484 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1485 inset_hit->localDispatch(cmd1);
1486 // IMO this is a grosshack! Inset's should be changed so that
1487 // they call the actions they have to do with the insetButtonRel.
1488 // function and not in the edit(). This should be changed
1490 #ifdef WITH_WARNINGS
1491 #warning Please remove donot call inset->edit() here (Jug 20020812)
1493 inset_hit->edit(bv, x, y, cmd.button());
1498 // Maybe we want to edit a bibitem ale970302
1499 if (bv->text->cursor.par()->bibkey) {
1501 bv->text->cursor.par()->isRightToLeftPar(bv->buffer()->params);
1503 bibitemMaxWidth(bv, bv->buffer()->params.getLyXTextClass().defaultfont());
1504 if ((is_rtl && x > bv->text->workWidth(bv)-20-width) ||
1505 (!is_rtl && x < 20 + width)) {
1506 bv->text->cursor.par()->bibkey->edit(bv, 0, 0, mouse_button::none);
1512 case LFUN_SELFINSERT: {
1513 if (cmd.argument.empty())
1516 // Automatically delete the currently selected
1517 // text and replace it with what is being
1518 // typed in now. Depends on lyxrc settings
1519 // "auto_region_delete", which defaults to
1522 if (lyxrc.auto_region_delete) {
1523 if (selection.set()) {
1524 cutSelection(bv, false, false);
1527 bv->workarea().haveSelection(false);
1530 bv->beforeChange(this);
1531 LyXFont const old_font(real_current_font);
1533 string::const_iterator cit = cmd.argument.begin();
1534 string::const_iterator end = cmd.argument.end();
1535 for (; cit != end; ++cit)
1536 bv->owner()->getIntl().getTransManager().
1537 TranslateAndInsert(*cit, this);
1540 selection.cursor = cursor;
1541 moveCursorUpdate(bv, false);
1543 // real_current_font.number can change so we need to
1544 // update the minibuffer
1545 if (old_font != real_current_font)
1546 bv->owner()->view_state_changed();
1551 case LFUN_INSET_LIST:
1552 case LFUN_INSET_THEOREM:
1554 case LFUN_INSERT_NOTE:
1555 case LFUN_INSET_CAPTION:
1556 case LFUN_INSET_ERT:
1557 case LFUN_INSET_EXTERNAL:
1558 case LFUN_INSET_FLOAT:
1559 case LFUN_INSET_FOOTNOTE:
1560 case LFUN_INSET_MARGINAL:
1561 case LFUN_INSET_MINIPAGE:
1562 case LFUN_INSET_OPTARG:
1563 case LFUN_INSET_WIDE_FLOAT:
1564 case LFUN_TABULAR_INSERT:
1565 case LFUN_INDEX_INSERT:
1566 case LFUN_INDEX_PRINT:
1567 case LFUN_PARENTINSERT:
1568 case LFUN_TOC_INSERT:
1570 Inset * inset = createInset(cmd);
1572 bool gotsel = false;
1573 if (selection.set()) {
1574 cutSelection(bv, true, false);
1577 if (bv->insertInset(inset)) {
1580 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
1590 return Inset::UNDISPATCHED;
1593 return Inset::DISPATCHED;