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/insetbib.h"
35 #include "insets/insetquotes.h"
36 #include "insets/insetcommand.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()) {
65 bv->updateInset(lt->inset_owner, false);
69 // bv->update(lt, BufferView::SELECT|BufferView::FITCUR);
71 bv->update(lt, BufferView::SELECT|BufferView::FITCUR);
73 } else if (bv->text->status() != LyXText::UNCHANGED) {
74 bv->theLockingInset()->hideInsetCursor(bv);
75 bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
79 if (!lt->selection.set())
80 bv->haveSelection(false);
86 void finishChange(BufferView * bv, bool selecting = false)
89 moveCursorUpdate(bv, selecting);
90 bv->owner()->view_state_changed();
93 // check if the given co-ordinates are inside an inset at the
94 // given cursor, if one exists. If so, the inset is returned,
95 // and the co-ordinates are made relative. Otherwise, 0 is returned.
96 Inset * checkInset(BufferView * bv, LyXText const & text,
97 LyXCursor const & cur, int & x, int & y)
99 lyx::pos_type const pos = cur.pos();
100 Paragraph /*const*/ & par = *cur.par();
102 if (pos >= par.size() || !par.isInset(pos))
105 Inset /*const*/ * inset = par.getInset(pos);
107 if (!isEditableInset(inset))
110 // get inset dimensions
111 lyx::Assert(par.getInset(pos));
113 LyXFont const & font = text.getFont(bv->buffer(), &par, pos);
115 int const width = inset->width(bv, font);
116 int const inset_x = font.isVisibleRightToLeft()
117 ? (cur.ix() - width) : cur.ix();
120 inset_x + inset->scroll(),
122 cur.iy() - inset->ascent(bv, font),
123 cur.iy() + inset->descent(bv, font)
126 if (!b.contained(x, y)) {
127 lyxerr[Debug::GUI] << "Missed inset at x,y " << x << "," << y
128 << " box " << b << endl;
132 text.setCursor(bv, &par, pos, true);
135 // The origin of an inset is on the baseline
136 y -= text.cursor.iy();
144 Inset * LyXText::checkInsetHit(BufferView * bv, int & x, int & y) const
146 int y_tmp = y + first_y;
149 setCursorFromCoordinates(bv, cur, x, y_tmp);
151 Inset * inset = checkInset(bv, *this, cur, x, y_tmp);
157 // look at previous position
162 setCursor(bv, cur, cur.par(), cur.pos() - 1, true);
164 inset = checkInset(bv, *this, cur, x, y_tmp);
171 bool LyXText::gotoNextInset(BufferView * bv,
172 vector<Inset::Code> const & codes, string const & contents) const
174 LyXCursor res = cursor;
177 if (res.pos() < res.par()->size() - 1) {
178 res.pos(res.pos() + 1);
180 res.par(res.par()->next());
184 } while (res.par() &&
185 !(res.par()->isInset(res.pos())
186 && (inset = res.par()->getInset(res.pos())) != 0
187 && find(codes.begin(), codes.end(), inset->lyxCode())
189 && (contents.empty() ||
190 static_cast<InsetCommand *>(
191 res.par()->getInset(res.pos()))->getContents()
195 setCursor(bv, res.par(), res.pos(), false);
202 void LyXText::gotoInset(BufferView * bv, vector<Inset::Code> const & codes,
206 bv->beforeChange(this);
210 if (same_content && cursor.par()->isInset(cursor.pos())) {
211 Inset const * inset = cursor.par()->getInset(cursor.pos());
212 if (find(codes.begin(), codes.end(), inset->lyxCode())
214 contents = static_cast<InsetCommand const *>(inset)->getContents();
217 if (!gotoNextInset(bv, codes, contents)) {
218 if (cursor.pos() || cursor.par() != ownerParagraph()) {
219 LyXCursor tmp = cursor;
220 cursor.par(ownerParagraph());
222 if (!gotoNextInset(bv, codes, contents)) {
224 bv->owner()->message(_("No more insets"));
227 bv->owner()->message(_("No more insets"));
231 selection.cursor = cursor;
235 void LyXText::gotoInset(BufferView * bv, Inset::Code code, bool same_content)
237 gotoInset(bv, vector<Inset::Code>(1, code), same_content);
241 void LyXText::cursorPrevious(BufferView * bv)
243 if (!cursor.row()->previous()) {
245 int new_y = bv->text->first_y - bv->workHeight();
246 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
247 bv->updateScrollbar();
253 Row * cursorrow = cursor.row();
255 setCursorFromCoordinates(bv, cursor.x_fix(), y);
259 if (cursorrow == bv->text->cursor.row()) {
260 // we have a row which is higher than the workarea so we leave the
261 // cursor on the start of the row and move only the draw up as soon
262 // as we move the cursor or do something while inside the row (it may
263 // span several workarea-heights) we'll move to the top again, but this
264 // is better than just jump down and only display part of the row.
265 new_y = bv->text->first_y - bv->workHeight();
268 new_y = bv->text->cursor.iy()
269 + bv->theLockingInset()->insetInInsetY() + y
270 + cursor.row()->height()
271 - bv->workHeight() + 1;
274 - cursor.row()->baseline()
275 + cursor.row()->height()
276 - bv->workHeight() + 1;
279 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
280 if (cursor.row()->previous()) {
282 setCursor(bv, cur, cursor.row()->previous()->par(),
283 cursor.row()->previous()->pos(), false);
284 if (cur.y() > first_y) {
288 bv->updateScrollbar();
292 void LyXText::cursorNext(BufferView * bv)
294 if (!cursor.row()->next()) {
295 int y = cursor.y() - cursor.row()->baseline() +
296 cursor.row()->height();
297 if (y > int(first_y + bv->workHeight())) {
298 bv->screen().draw(bv->text, bv,
299 bv->text->first_y + bv->workHeight());
300 bv->updateScrollbar();
305 int y = first_y + bv->workHeight();
306 if (inset_owner && !first_y) {
307 y -= (bv->text->cursor.iy()
309 + bv->theLockingInset()->insetInInsetY());
314 Row * cursorrow = cursor.row();
315 setCursorFromCoordinates(bv, cursor.x_fix(), y);
316 // + bv->workHeight());
320 if (cursorrow == bv->text->cursor.row()) {
321 // we have a row which is higher than the workarea so we leave the
322 // cursor on the start of the row and move only the draw down as soon
323 // as we move the cursor or do something while inside the row (it may
324 // span several workarea-heights) we'll move to the top again, but this
325 // is better than just jump down and only display part of the row.
326 new_y = bv->text->first_y + bv->workHeight();
329 new_y = bv->text->cursor.iy()
330 + bv->theLockingInset()->insetInInsetY()
331 + y - cursor.row()->baseline();
333 new_y = cursor.y() - cursor.row()->baseline();
336 bv->screen().draw(bv->text, bv, new_y);
337 if (cursor.row()->next()) {
339 setCursor(bv, cur, cursor.row()->next()->par(),
340 cursor.row()->next()->pos(), false);
341 if (cur.y() < int(first_y + bv->workHeight())) {
342 cursorDown(bv, true);
345 bv->updateScrollbar();
349 void LyXText::update(BufferView * bv, bool changed)
351 BufferView::UpdateCodes c = BufferView::SELECT | BufferView::FITCUR;
353 bv->update(this, c | BufferView::CHANGE);
359 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
363 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
364 if (!bv->insertInset(new_inset))
367 bv->updateInset(new_inset, true);
371 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
373 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
374 <<"] arg[" << cmd.argument << "]" << endl;
376 BufferView * bv = cmd.view();
378 switch (cmd.action) {
380 case LFUN_APPENDIX: {
381 Paragraph * par = cursor.par();
382 bool start = !par->params().startOfAppendix();
384 // ensure that we have only one start_of_appendix in this document
385 Paragraph * tmp = ownerParagraph();
386 for (; tmp; tmp = tmp->next())
387 tmp->params().startOfAppendix(false);
389 par->params().startOfAppendix(start);
391 // we can set the refreshing parameters now
392 status(cmd.view(), LyXText::NEED_MORE_REFRESH);
394 refresh_row = 0; // not needed for full update
395 updateCounters(cmd.view());
396 setCursor(cmd.view(), cursor.par(), cursor.pos());
401 case LFUN_DELETE_WORD_FORWARD:
402 bv->beforeChange(this);
404 deleteWordForward(bv);
409 case LFUN_DELETE_WORD_BACKWARD:
410 bv->beforeChange(this);
412 deleteWordBackward(bv);
417 case LFUN_DELETE_LINE_FORWARD:
418 bv->beforeChange(this);
420 deleteLineForward(bv);
427 if (!selection.mark())
428 bv->beforeChange(this);
435 if (!selection.mark())
436 bv->beforeChange(this);
438 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
439 cursorLeftOneWord(bv);
441 cursorRightOneWord(bv);
446 if (!selection.mark())
447 bv->beforeChange(this);
449 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
450 cursorRightOneWord(bv);
452 cursorLeftOneWord(bv);
456 case LFUN_BEGINNINGBUF:
457 if (!selection.mark())
458 bv->beforeChange(this);
465 if (selection.mark())
466 bv->beforeChange(this);
474 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
478 finishChange(bv, true);
483 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
487 finishChange(bv, true);
493 finishChange(bv, true);
498 cursorDown(bv, true);
499 finishChange(bv, true);
502 case LFUN_UP_PARAGRAPHSEL:
504 cursorUpParagraph(bv);
505 finishChange(bv, true);
508 case LFUN_DOWN_PARAGRAPHSEL:
510 cursorDownParagraph(bv);
511 finishChange(bv, true);
517 finishChange(bv, true);
523 finishChange(bv, true);
529 finishChange(bv, true);
535 finishChange(bv, true);
538 case LFUN_WORDRIGHTSEL:
540 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
541 cursorLeftOneWord(bv);
543 cursorRightOneWord(bv);
544 finishChange(bv, true);
547 case LFUN_WORDLEFTSEL:
549 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
550 cursorRightOneWord(bv);
552 cursorLeftOneWord(bv);
553 finishChange(bv, true);
557 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
558 if (!selection.mark())
559 bv->beforeChange(this);
562 cursorLeft(bv, false);
563 if (cursor.pos() < cursor.par()->size()
564 && cursor.par()->isInset(cursor.pos())
565 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
566 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
567 cmd.message(tmpinset->editMessage());
568 tmpinset->edit(bv, !is_rtl);
572 cursorRight(bv, false);
578 // This is soooo ugly. Isn`t it possible to make
580 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
581 if (!selection.mark())
582 bv->beforeChange(this);
584 LyXCursor const cur = cursor;
586 cursorLeft(bv, false);
587 if ((is_rtl || cur != cursor) && // only if really moved!
588 cursor.pos() < cursor.par()->size() &&
589 cursor.par()->isInset(cursor.pos()) &&
590 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
591 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
592 cmd.message(tmpinset->editMessage());
593 tmpinset->edit(bv, is_rtl);
597 cursorRight(bv, false);
603 if (!selection.mark())
604 bv->beforeChange(this);
605 bv->update(this, BufferView::UPDATE);
611 if (!selection.mark())
612 bv->beforeChange(this);
613 bv->update(this, BufferView::UPDATE);
618 case LFUN_UP_PARAGRAPH:
619 if (!selection.mark())
620 bv->beforeChange(this);
621 bv->update(this, BufferView::UPDATE);
622 cursorUpParagraph(bv);
626 case LFUN_DOWN_PARAGRAPH:
627 if (!selection.mark())
628 bv->beforeChange(this);
629 bv->update(this, BufferView::UPDATE);
630 cursorDownParagraph(bv);
631 finishChange(bv, false);
635 if (!selection.mark())
636 bv->beforeChange(this);
637 bv->update(this, BufferView::UPDATE);
639 finishChange(bv, false);
642 // moveCursorUpdate(bv, false, false);
643 // owner_->view_state_changed();
647 if (!selection.mark())
648 bv->beforeChange(this);
649 bv->update(this, BufferView::UPDATE);
651 finishChange(bv, false);
655 if (!selection.mark())
656 bv->beforeChange(this);
659 finishChange(bv, false);
663 if (!selection.mark())
664 bv->beforeChange(this);
667 finishChange(bv, false);
671 bv->beforeChange(this);
672 insertChar(bv, Paragraph::META_NEWLINE);
674 setCursor(bv, cursor.par(), cursor.pos());
675 moveCursorUpdate(bv, false);
679 if (!selection.set()) {
681 selection.cursor = cursor;
683 // It is possible to make it a lot faster still
684 // just comment out the line below...
688 cutSelection(bv, true);
691 moveCursorUpdate(bv, false);
692 bv->owner()->view_state_changed();
696 case LFUN_DELETE_SKIP:
697 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
698 if (!selection.set()) {
699 LyXCursor cur = cursor;
700 if (cur.pos() == cur.par()->size()) {
704 && !(cur.par()->params().spaceTop()
705 == VSpace (VSpace::NONE))) {
707 cur.par()->params().lineTop(),
708 cur.par()->params().lineBottom(),
709 cur.par()->params().pagebreakTop(),
710 cur.par()->params().pagebreakBottom(),
711 VSpace(VSpace::NONE),
712 cur.par()->params().spaceBottom(),
713 cur.par()->params().spacing(),
714 cur.par()->params().align(),
715 cur.par()->params().labelWidthString(), 0);
721 selection.cursor = cursor;
725 selection.cursor = cursor;
729 cutSelection(bv, true);
736 if (!selection.set()) {
737 if (bv->owner()->getIntl().getTransManager().backspace()) {
739 selection.cursor = cursor;
741 // It is possible to make it a lot faster still
742 // just comment out the line below...
747 cutSelection(bv, true);
750 bv->owner()->view_state_changed();
754 case LFUN_BACKSPACE_SKIP:
755 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
756 if (!selection.set()) {
757 LyXCursor cur = cursor;
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), cur.par()->params().spaceBottom(),
767 cur.par()->params().spacing(),
768 cur.par()->params().align(),
769 cur.par()->params().labelWidthString(), 0);
772 selection.cursor = cur;
776 cutSelection(bv, true);
781 case LFUN_BREAKPARAGRAPH:
782 bv->beforeChange(this);
783 breakParagraph(bv, 0);
785 selection.cursor = cursor;
787 bv->owner()->view_state_changed();
790 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
791 bv->beforeChange(this);
792 breakParagraph(bv, 1);
794 selection.cursor = cursor;
796 bv->owner()->view_state_changed();
799 case LFUN_BREAKPARAGRAPH_SKIP: {
800 // When at the beginning of a paragraph, remove
801 // indentation and add a "defskip" at the top.
802 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
803 LyXCursor cur = cursor;
804 bv->beforeChange(this);
805 if (cur.pos() == 0) {
806 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
808 cur.par()->params().lineTop(),
809 cur.par()->params().lineBottom(),
810 cur.par()->params().pagebreakTop(),
811 cur.par()->params().pagebreakBottom(),
812 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
813 cur.par()->params().spacing(),
814 cur.par()->params().align(),
815 cur.par()->params().labelWidthString(), 1);
820 breakParagraph(bv, 0);
824 selection.cursor = cur;
826 bv->owner()->view_state_changed();
830 case LFUN_PARAGRAPH_SPACING: {
831 Paragraph * par = cursor.par();
832 Spacing::Space cur_spacing = par->params().spacing().getSpace();
833 float cur_value = 1.0;
834 if (cur_spacing == Spacing::Other)
835 cur_value = par->params().spacing().getValue();
837 istringstream is(cmd.argument.c_str());
840 Spacing::Space new_spacing = cur_spacing;
841 float new_value = cur_value;
843 lyxerr << "Missing argument to `paragraph-spacing'"
845 } else if (tmp == "single") {
846 new_spacing = Spacing::Single;
847 } else if (tmp == "onehalf") {
848 new_spacing = Spacing::Onehalf;
849 } else if (tmp == "double") {
850 new_spacing = Spacing::Double;
851 } else if (tmp == "other") {
852 new_spacing = Spacing::Other;
855 lyxerr << "new_value = " << tmpval << endl;
858 } else if (tmp == "default") {
859 new_spacing = Spacing::Default;
861 lyxerr << _("Unknown spacing argument: ")
862 << cmd.argument << endl;
864 if (cur_spacing != new_spacing || cur_value != new_value) {
865 par->params().spacing(Spacing(new_spacing, new_value));
872 case LFUN_INSET_TOGGLE:
874 bv->beforeChange(this);
881 case LFUN_PROTECTEDSPACE:
882 if (cursor.par()->layout()->free_spacing) {
886 specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
888 moveCursorUpdate(bv, false);
891 case LFUN_HYPHENATION:
892 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
895 case LFUN_LIGATURE_BREAK:
896 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
900 specialChar(this, bv, InsetSpecialChar::LDOTS);
906 insertChar(bv, Paragraph::META_HFILL);
910 case LFUN_END_OF_SENTENCE:
911 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
914 case LFUN_MENU_SEPARATOR:
915 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
919 bv->beforeChange(this);
921 selection.cursor = cursor;
922 cmd.message(N_("Mark off"));
926 bv->beforeChange(this);
927 selection.mark(true);
929 selection.cursor = cursor;
930 cmd.message(N_("Mark on"));
934 bv->beforeChange(this);
935 if (selection.mark()) {
937 cmd.message(N_("Mark removed"));
939 selection.mark(true);
941 cmd.message(N_("Mark set"));
943 selection.cursor = cursor;
946 case LFUN_UPCASE_WORD:
948 changeCase(bv, LyXText::text_uppercase);
950 bv->updateInset(inset_owner, true);
954 case LFUN_LOWCASE_WORD:
956 changeCase(bv, LyXText::text_lowercase);
958 bv->updateInset(inset_owner, true);
962 case LFUN_CAPITALIZE_WORD:
964 changeCase(bv, LyXText::text_capitalization);
966 bv->updateInset(inset_owner, true);
970 case LFUN_TRANSPOSE_CHARS:
974 bv->updateInset(inset_owner, true);
979 cmd.message(_("Paste"));
981 // clear the selection
982 bv->toggleSelection();
986 clearSelection(); // bug 393
995 cutSelection(bv, true);
997 cmd.message(_("Cut"));
1002 cmd.message(_("Copy"));
1005 case LFUN_BEGINNINGBUFSEL:
1007 return Inset::UNDISPATCHED;
1010 finishChange(bv, true);
1013 case LFUN_ENDBUFSEL:
1015 return Inset::UNDISPATCHED;
1018 finishChange(bv, true);
1022 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1028 istringstream is(cmd.argument.c_str());
1031 lyxerr << "SETXY: Could not parse coordinates in '"
1032 << cmd.argument << std::endl;
1034 setCursorFromCoordinates(bv, x, y);
1039 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1041 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1047 case LFUN_GETLAYOUT:
1048 cmd.message(tostr(cursor.par()->layout()));
1052 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1053 << cmd.argument << endl;
1055 // This is not the good solution to the empty argument
1056 // problem, but it will hopefully suffice for 1.2.0.
1057 // The correct solution would be to augument the
1058 // function list/array with information about what
1059 // functions needs arguments and their type.
1060 if (cmd.argument.empty()) {
1061 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1065 // Derive layout number from given argument (string)
1066 // and current buffer's textclass (number)
1067 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1068 bool hasLayout = tclass.hasLayout(cmd.argument);
1069 string layout = cmd.argument;
1071 // If the entry is obsolete, use the new one instead.
1073 string const & obs = tclass[layout]->obsoleted_by();
1079 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1084 bool change_layout = (current_layout != layout);
1085 if (!change_layout && selection.set() &&
1086 selection.start.par() != selection.end.par())
1088 Paragraph * spar = selection.start.par();
1089 Paragraph * epar = selection.end.par()->next();
1090 while (spar != epar) {
1091 if (spar->layout()->name() != current_layout) {
1092 change_layout = true;
1097 if (change_layout) {
1099 current_layout = layout;
1101 setLayout(bv, layout);
1102 bv->owner()->setLayout(layout);
1109 case LFUN_PASTESELECTION: {
1113 // this was originally a beforeChange(bv->text), i.e
1114 // the outermost LyXText!
1115 bv->beforeChange(this);
1116 string const clip = bv->getClipboard();
1117 if (!clip.empty()) {
1118 if (cmd.argument == "paragraph")
1119 insertStringAsParagraphs(bv, clip);
1121 insertStringAsLines(bv, clip);
1128 case LFUN_GOTOERROR:
1129 gotoInset(bv, Inset::ERROR_CODE, false);
1133 gotoInset(bv, Inset::NOTE_CODE, false);
1136 case LFUN_REFERENCE_GOTO:
1138 vector<Inset::Code> tmp;
1139 tmp.push_back(Inset::LABEL_CODE);
1140 tmp.push_back(Inset::REF_CODE);
1141 gotoInset(bv, tmp, true);
1146 Paragraph const * par = cursor.par();
1147 lyx::pos_type pos = cursor.pos();
1151 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1154 c = par->getChar(pos - 1);
1157 LyXLayout_ptr const & style = par->layout();
1159 if (style->pass_thru ||
1160 par->getFontSettings(bv->buffer()->params,
1161 pos).language()->lang() == "hebrew" ||
1162 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1163 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1167 case LFUN_DATE_INSERT: {
1168 time_t now_time_t = time(NULL);
1169 struct tm * now_tm = localtime(&now_time_t);
1170 setlocale(LC_TIME, "");
1172 if (!cmd.argument.empty())
1175 arg = lyxrc.date_insert_format;
1177 int const datetmp_len =
1178 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1180 for (int i = 0; i < datetmp_len; i++) {
1181 insertChar(bv, datetmp[i]);
1184 selection.cursor = cursor;
1185 moveCursorUpdate(bv, false);
1189 case LFUN_MOUSE_TRIPLE:
1192 if (bv_owner && bv->theLockingInset())
1194 if (cmd.button() == mouse_button::button1) {
1196 bv->screen().hideCursor();
1197 bv->screen().toggleSelection(this, bv);
1200 selection.cursor = cursor;
1204 bv->screen().toggleSelection(this, bv, false);
1206 bv->haveSelection(selection.set());
1210 case LFUN_MOUSE_DOUBLE:
1213 if (bv_owner && bv->theLockingInset())
1215 if (cmd.button() == mouse_button::button1) {
1217 bv->screen().hideCursor();
1218 bv->screen().toggleSelection(this, bv);
1219 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1220 bv->screen().toggleSelection(this, bv, false);
1222 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1225 bv->haveSelection(selection.set());
1229 case LFUN_MOUSE_MOTION:
1231 // Only use motion with button 1
1232 //if (ev.button() != mouse_button::button1)
1238 // Check for inset locking
1239 if (bv->theLockingInset()) {
1240 Inset * tli = bv->theLockingInset();
1241 LyXCursor cursor = bv->text->cursor;
1242 LyXFont font = bv->text->getFont(bv->buffer(),
1243 cursor.par(), cursor.pos());
1244 int width = tli->width(bv, font);
1245 int inset_x = font.isVisibleRightToLeft()
1246 ? cursor.ix() - width : cursor.ix();
1247 int start_x = inset_x + tli->scroll();
1248 FuncRequest cmd1 = cmd;
1249 cmd1.x = cmd.x - start_x;
1250 cmd1.y = cmd.y - cursor.iy() + bv->text->first_y;
1251 tli->localDispatch(cmd1);
1255 // The test for not selection possible is needed, that only motion
1256 // events are used, where the bottom press event was on
1257 // the drawing area too
1258 if (!selection_possible) {
1259 lyxerr[Debug::ACTION]
1260 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1264 bv->screen().hideCursor();
1266 Row * cursorrow = bv->text->cursor.row();
1267 bv->text->setCursorFromCoordinates(bv, cmd.x, cmd.y + bv->text->first_y);
1269 // sorry for this but I have a strange error that the y value jumps at
1270 // a certain point. This seems like an error in my xforms library or
1271 // in some other local environment, but I would like to leave this here
1272 // for the moment until I can remove this (Jug 20020418)
1273 if (y_before < bv->text->cursor.y())
1274 lyxerr << y_before << ":" << bv->text->cursor.y() << endl;
1276 // This is to allow jumping over large insets
1277 if (cursorrow == bv->text->cursor.row()) {
1278 if (cmd.y >= int(bv->workHeight()))
1279 bv->text->cursorDown(bv, false);
1281 bv->text->cursorUp(bv, false);
1284 // Maybe an empty line was deleted
1285 if (!bv->text->selection.set())
1286 bv->update(bv->text, BufferView::UPDATE);
1287 bv->text->setSelection(bv);
1288 bv->screen().toggleToggle(bv->text, bv);
1294 // Single-click on work area
1295 case LFUN_MOUSE_PRESS:
1300 // ok ok, this is a hack (for xforms)
1301 // We shouldn't go further down as we really should only do the
1302 // scrolling and be done with this. Otherwise we may open some
1303 // dialogs (Jug 20020424).
1304 if (cmd.button() == mouse_button::button4) {
1305 bv->scroll(-lyxrc.wheel_jump);
1308 if (cmd.button() == mouse_button::button5) {
1309 bv->scroll(lyxrc.wheel_jump);
1315 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1317 // Middle button press pastes if we have a selection
1318 // We do this here as if the selection was inside an inset
1319 // it could get cleared on the unlocking of the inset so
1320 // we have to check this first
1321 bool paste_internally = false;
1322 if (cmd.button() == mouse_button::button2 && selection.set()) {
1323 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1324 paste_internally = true;
1327 int const screen_first = bv->text->first_y;
1329 if (bv->theLockingInset()) {
1330 // We are in inset locking mode
1332 // Check whether the inset was hit. If not reset mode,
1333 // otherwise give the event to the inset
1334 if (inset_hit == bv->theLockingInset()) {
1335 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1336 bv->theLockingInset()->localDispatch(cmd1);
1339 bv->unlockInset(bv->theLockingInset());
1343 selection_possible = true;
1344 bv->screen().hideCursor();
1346 // Clear the selection
1347 bv->screen().toggleSelection(bv->text, bv);
1348 bv->text->clearSelection();
1349 bv->text->fullRebreak(bv);
1351 bv->updateScrollbar();
1353 // Single left click in math inset?
1354 if (isHighlyEditableInset(inset_hit)) {
1355 // Highly editable inset, like math
1356 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1357 selection_possible = false;
1358 bv->owner()->updateLayoutChoice();
1359 bv->owner()->message(inset->editMessage());
1360 //inset->edit(bv, x, y, cmd.button());
1361 // We just have to lock the inset before calling a PressEvent on it!
1362 // we don't need the edit() call here! (Jug20020329)
1363 if (!bv->lockInset(inset))
1364 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1365 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1366 inset->localDispatch(cmd1);
1369 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1371 // Right click on a footnote flag opens float menu
1372 if (cmd.button() == mouse_button::button3) {
1373 selection_possible = false;
1377 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1378 bv->text->setCursorFromCoordinates(bv, x, y + screen_first);
1380 bv->text->selection.cursor = bv->text->cursor;
1381 bv->text->cursor.x_fix(bv->text->cursor.x());
1383 bv->owner()->updateLayoutChoice();
1384 if (bv->fitCursor())
1385 selection_possible = false;
1387 // Insert primary selection with middle mouse
1388 // if there is a local selection in the current buffer,
1390 if (cmd.button() == mouse_button::button2) {
1391 if (paste_internally)
1392 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1394 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1395 selection_possible = false;
1400 case LFUN_MOUSE_RELEASE:
1402 // do nothing if we used the mouse wheel
1406 if (cmd.button() == mouse_button::button4
1407 || cmd.button() == mouse_button::button5)
1410 // If we hit an inset, we have the inset coordinates in these
1411 // and inset_hit points to the inset. If we do not hit an
1412 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1415 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1417 if (bv->theLockingInset()) {
1418 // We are in inset locking mode.
1420 // LyX does a kind of work-area grabbing for insets.
1421 // Only a ButtonPress FuncRequest outside the inset will
1422 // force a insetUnlock.
1423 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1424 bv->theLockingInset()->localDispatch(cmd1);
1428 selection_possible = false;
1430 if (cmd.button() == mouse_button::button2)
1434 if (cmd.button() == mouse_button::button1)
1435 bv->haveSelection(selection.set());
1438 bv->owner()->view_state_changed();
1439 bv->owner()->updateMenubar();
1440 bv->owner()->updateToolbar();
1442 // Did we hit an editable inset?
1444 selection_possible = false;
1446 // if we reach this point with a selection, it
1447 // must mean we are currently selecting.
1448 // But we don't want to open the inset
1449 // because that is annoying for the user.
1450 // So just pretend we didn't hit it.
1451 // this is OK because a "kosher" ButtonRelease
1452 // will follow a ButtonPress that clears
1454 // Note this also fixes selection drawing
1455 // problems if we end up opening an inset
1456 if (selection.set())
1459 // CHECK fix this proper in 0.13
1460 // well, maybe 13.0 !!!!!!!!!
1462 // Following a ref shouldn't issue
1463 // a push on the undo-stack
1464 // anylonger, now that we have
1465 // keybindings for following
1466 // references and returning from
1467 // references. IMHO though, it
1468 // should be the inset's own business
1469 // to push or not push on the undo
1470 // stack. They don't *have* to
1471 // alter the document...
1473 // ...or maybe the SetCursorParUndo()
1474 // below isn't necessary at all anylonger?
1475 if (inset_hit->lyxCode() == Inset::REF_CODE)
1476 setCursorParUndo(bv);
1478 bv->owner()->message(inset_hit->editMessage());
1480 if (isHighlyEditableInset(inset_hit)) {
1481 // Highly editable inset, like math
1482 UpdatableInset * inset = (UpdatableInset *) inset_hit;
1483 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1484 inset->localDispatch(cmd1);
1486 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1487 inset_hit->localDispatch(cmd1);
1488 // IMO this is a grosshack! Inset's should be changed so that
1489 // they call the actions they have to do with the insetButtonRel.
1490 // function and not in the edit(). This should be changed
1492 #ifdef WITH_WARNINGS
1493 #warning Please remove donot call inset->edit() here (Jug 20020812)
1495 inset_hit->edit(bv, x, y, cmd.button());
1500 // Maybe we want to edit a bibitem ale970302
1501 if (bv->text->cursor.par()->bibkey) {
1503 bv->text->cursor.par()->isRightToLeftPar(bv->buffer()->params);
1505 bibitemMaxWidth(bv, bv->buffer()->params.getLyXTextClass().defaultfont());
1506 if ((is_rtl && x > bv->text->workWidth(bv)-20-width) ||
1507 (!is_rtl && x < 20 + width)) {
1508 bv->text->cursor.par()->bibkey->edit(bv, 0, 0, mouse_button::none);
1514 case LFUN_SELFINSERT: {
1515 if (cmd.argument.empty())
1518 // Automatically delete the currently selected
1519 // text and replace it with what is being
1520 // typed in now. Depends on lyxrc settings
1521 // "auto_region_delete", which defaults to
1524 if (lyxrc.auto_region_delete) {
1525 if (selection.set()) {
1526 cutSelection(bv, false, false);
1529 bv->haveSelection(false);
1532 bv->beforeChange(this);
1533 LyXFont const old_font(real_current_font);
1535 string::const_iterator cit = cmd.argument.begin();
1536 string::const_iterator end = cmd.argument.end();
1537 for (; cit != end; ++cit)
1538 bv->owner()->getIntl().getTransManager().
1539 TranslateAndInsert(*cit, this);
1542 selection.cursor = cursor;
1543 moveCursorUpdate(bv, false);
1545 // real_current_font.number can change so we need to
1546 // update the minibuffer
1547 if (old_font != real_current_font)
1548 bv->owner()->view_state_changed();
1552 case LFUN_HTMLURL: {
1553 InsetCommandParams p("htmlurl");
1554 bv->owner()->getDialogs().createUrl(p.getAsString());
1559 InsetCommandParams p("url");
1560 bv->owner()->getDialogs().createUrl(p.getAsString());
1566 case LFUN_INSET_LIST:
1567 case LFUN_INSET_THEOREM:
1569 case LFUN_INSERT_NOTE:
1570 case LFUN_INSERT_URL:
1571 case LFUN_INSET_CAPTION:
1572 case LFUN_INSET_ERT:
1573 case LFUN_INSET_EXTERNAL:
1574 case LFUN_INSET_FLOAT:
1575 case LFUN_INSET_FOOTNOTE:
1576 case LFUN_INSET_MARGINAL:
1577 case LFUN_INSET_MINIPAGE:
1578 case LFUN_INSET_OPTARG:
1579 case LFUN_INSET_WIDE_FLOAT:
1580 case LFUN_INSET_WRAP:
1581 case LFUN_TABULAR_INSERT:
1582 case LFUN_INDEX_INSERT:
1583 case LFUN_INDEX_PRINT:
1584 case LFUN_PARENTINSERT:
1585 case LFUN_TOC_INSERT:
1587 Inset * inset = createInset(cmd);
1589 bool gotsel = false;
1590 if (selection.set()) {
1591 cutSelection(bv, true, false);
1594 if (bv->insertInset(inset)) {
1597 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
1607 return Inset::UNDISPATCHED;
1610 return Inset::DISPATCHED;