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()) {
65 bv->updateInset(lt->inset_owner, false);
67 if (lt->isTopLevel()) {
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 "
129 << " box " << b << endl;
133 text.setCursor(bv, &par, pos, true);
136 // The origin of an inset is on the baseline
137 y -= text.cursor.iy();
145 Inset * LyXText::checkInsetHit(BufferView * bv, int & x, int & y) const
147 int y_tmp = y + top_y();
150 setCursorFromCoordinates(bv, cur, x, y_tmp);
152 Inset * inset = checkInset(bv, *this, cur, x, y_tmp);
158 // look at previous position
163 setCursor(bv, cur, cur.par(), cur.pos() - 1, true);
165 inset = checkInset(bv, *this, cur, x, y_tmp);
172 bool LyXText::gotoNextInset(BufferView * bv,
173 vector<Inset::Code> const & codes, string const & contents) const
175 LyXCursor res = cursor;
178 if (res.pos() < res.par()->size() - 1) {
179 res.pos(res.pos() + 1);
181 res.par(res.par()->next());
185 } while (res.par() &&
186 !(res.par()->isInset(res.pos())
187 && (inset = res.par()->getInset(res.pos())) != 0
188 && find(codes.begin(), codes.end(), inset->lyxCode())
190 && (contents.empty() ||
191 static_cast<InsetCommand *>(
192 res.par()->getInset(res.pos()))->getContents()
196 setCursor(bv, res.par(), res.pos(), false);
203 void LyXText::gotoInset(BufferView * bv, vector<Inset::Code> const & codes,
207 bv->beforeChange(this);
211 if (same_content && cursor.par()->isInset(cursor.pos())) {
212 Inset const * inset = cursor.par()->getInset(cursor.pos());
213 if (find(codes.begin(), codes.end(), inset->lyxCode())
215 contents = static_cast<InsetCommand const *>(inset)->getContents();
218 if (!gotoNextInset(bv, codes, contents)) {
219 if (cursor.pos() || cursor.par() != ownerParagraph()) {
220 LyXCursor tmp = cursor;
221 cursor.par(ownerParagraph());
223 if (!gotoNextInset(bv, codes, contents)) {
225 bv->owner()->message(_("No more insets"));
228 bv->owner()->message(_("No more insets"));
232 selection.cursor = cursor;
236 void LyXText::gotoInset(BufferView * bv, Inset::Code code, bool same_content)
238 gotoInset(bv, vector<Inset::Code>(1, code), same_content);
242 void LyXText::cursorPrevious(BufferView * bv)
244 if (!cursor.row()->previous()) {
246 int new_y = bv->text->top_y() - bv->workHeight();
247 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
248 bv->updateScrollbar();
254 Row * cursorrow = cursor.row();
256 setCursorFromCoordinates(bv, cursor.x_fix(), y);
260 if (cursorrow == bv->text->cursor.row()) {
261 // we have a row which is taller than the workarea. The
262 // simplest solution is to move to the previous row instead.
265 // This is what we used to do, so we wouldn't skip right past
266 // tall rows, but it's not working right now.
268 new_y = bv->text->top_y() - bv->workHeight();
272 new_y = bv->text->cursor.iy()
273 + bv->theLockingInset()->insetInInsetY() + y
274 + cursor.row()->height()
275 - bv->workHeight() + 1;
278 - cursor.row()->baseline()
279 + cursor.row()->height()
280 - bv->workHeight() + 1;
283 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
284 if (cursor.row()->previous()) {
286 setCursor(bv, cur, cursor.row()->previous()->par(),
287 cursor.row()->previous()->pos(), false);
288 if (cur.y() > top_y()) {
292 bv->updateScrollbar();
296 void LyXText::cursorNext(BufferView * bv)
298 if (!cursor.row()->next()) {
299 int y = cursor.y() - cursor.row()->baseline() +
300 cursor.row()->height();
301 if (y > top_y() + bv->workHeight()) {
302 bv->screen().draw(bv->text, bv, bv->text->top_y() + bv->workHeight());
303 bv->updateScrollbar();
308 int y = top_y() + bv->workHeight();
309 if (inset_owner && !top_y()) {
310 y -= (bv->text->cursor.iy()
312 + bv->theLockingInset()->insetInInsetY());
317 Row * cursorrow = cursor.row();
318 setCursorFromCoordinates(bv, 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.
326 cursorDown(bv, true);
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(bv, cur, cursor.row()->next()->par(),
346 cursor.row()->next()->pos(), false);
347 if (cur.y() < top_y() + bv->workHeight()) {
348 cursorDown(bv, true);
351 bv->updateScrollbar();
355 void LyXText::update(BufferView * bv, bool changed)
357 BufferView::UpdateCodes c = BufferView::SELECT | BufferView::FITCUR;
359 bv->update(this, c | BufferView::CHANGE);
366 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
370 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
371 if (!bv->insertInset(new_inset))
374 bv->updateInset(new_inset, true);
378 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
379 bool edit, bool pastesel)
381 Inset * inset = createInset(cmd);
382 BufferView * bv = cmd.view();
386 if (lt->selection.set()) {
387 lt->cutSelection(bv, true, false);
390 if (bv->insertInset(inset)) {
393 if (gotsel && pastesel)
394 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
403 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
405 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
406 <<"] arg[" << cmd.argument << ']' << endl;
408 BufferView * bv = cmd.view();
410 switch (cmd.action) {
412 case LFUN_APPENDIX: {
413 Paragraph * par = cursor.par();
414 bool start = !par->params().startOfAppendix();
416 // ensure that we have only one start_of_appendix in this document
417 Paragraph * tmp = ownerParagraph();
418 for (; tmp; tmp = tmp->next()) {
419 if (tmp->params().startOfAppendix()) {
420 setUndo(bv, Undo::EDIT, tmp, tmp->next());
421 tmp->params().startOfAppendix(false);
426 setUndo(bv, Undo::EDIT, par, par->next());
427 par->params().startOfAppendix(start);
429 // we can set the refreshing parameters now
430 status(cmd.view(), LyXText::NEED_MORE_REFRESH);
432 refresh_row = 0; // not needed for full update
433 updateCounters(cmd.view());
434 setCursor(cmd.view(), cursor.par(), cursor.pos());
439 case LFUN_DELETE_WORD_FORWARD:
440 bv->beforeChange(this);
442 deleteWordForward(bv);
447 case LFUN_DELETE_WORD_BACKWARD:
448 bv->beforeChange(this);
450 deleteWordBackward(bv);
455 case LFUN_DELETE_LINE_FORWARD:
456 bv->beforeChange(this);
458 deleteLineForward(bv);
465 if (!selection.mark())
466 bv->beforeChange(this);
473 if (!selection.mark())
474 bv->beforeChange(this);
476 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
477 cursorLeftOneWord(bv);
479 cursorRightOneWord(bv);
484 if (!selection.mark())
485 bv->beforeChange(this);
487 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
488 cursorRightOneWord(bv);
490 cursorLeftOneWord(bv);
494 case LFUN_BEGINNINGBUF:
495 if (!selection.mark())
496 bv->beforeChange(this);
503 if (selection.mark())
504 bv->beforeChange(this);
512 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
516 finishChange(bv, true);
521 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
525 finishChange(bv, true);
531 finishChange(bv, true);
536 cursorDown(bv, true);
537 finishChange(bv, true);
540 case LFUN_UP_PARAGRAPHSEL:
542 cursorUpParagraph(bv);
543 finishChange(bv, true);
546 case LFUN_DOWN_PARAGRAPHSEL:
548 cursorDownParagraph(bv);
549 finishChange(bv, true);
555 finishChange(bv, true);
561 finishChange(bv, true);
567 finishChange(bv, true);
573 finishChange(bv, true);
576 case LFUN_WORDRIGHTSEL:
578 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
579 cursorLeftOneWord(bv);
581 cursorRightOneWord(bv);
582 finishChange(bv, true);
585 case LFUN_WORDLEFTSEL:
587 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
588 cursorRightOneWord(bv);
590 cursorLeftOneWord(bv);
591 finishChange(bv, true);
598 getWord(cur1, cur2, WHOLE_WORD);
599 setCursor(bv, cur1.par(), cur1.pos());
600 bv->beforeChange(this);
601 setCursor(bv, cur2.par(), cur2.pos());
602 finishChange(bv, true);
607 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
608 if (!selection.mark())
609 bv->beforeChange(this);
612 cursorLeft(bv, false);
613 if (cursor.pos() < cursor.par()->size()
614 && cursor.par()->isInset(cursor.pos())
615 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
616 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
617 cmd.message(tmpinset->editMessage());
618 tmpinset->edit(bv, !is_rtl);
622 cursorRight(bv, false);
628 // This is soooo ugly. Isn`t it possible to make
630 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
631 if (!selection.mark())
632 bv->beforeChange(this);
634 LyXCursor const cur = cursor;
636 cursorLeft(bv, false);
637 if ((is_rtl || cur != cursor) && // only if really moved!
638 cursor.pos() < cursor.par()->size() &&
639 cursor.par()->isInset(cursor.pos()) &&
640 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
641 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
642 cmd.message(tmpinset->editMessage());
643 tmpinset->edit(bv, is_rtl);
647 cursorRight(bv, false);
653 if (!selection.mark())
654 bv->beforeChange(this);
655 bv->update(this, BufferView::UPDATE);
661 if (!selection.mark())
662 bv->beforeChange(this);
663 bv->update(this, BufferView::UPDATE);
668 case LFUN_UP_PARAGRAPH:
669 if (!selection.mark())
670 bv->beforeChange(this);
671 bv->update(this, BufferView::UPDATE);
672 cursorUpParagraph(bv);
676 case LFUN_DOWN_PARAGRAPH:
677 if (!selection.mark())
678 bv->beforeChange(this);
679 bv->update(this, BufferView::UPDATE);
680 cursorDownParagraph(bv);
681 finishChange(bv, false);
685 if (!selection.mark())
686 bv->beforeChange(this);
687 bv->update(this, BufferView::UPDATE);
689 finishChange(bv, false);
692 // moveCursorUpdate(bv, false, false);
693 // owner_->view_state_changed();
697 if (!selection.mark())
698 bv->beforeChange(this);
699 bv->update(this, BufferView::UPDATE);
701 finishChange(bv, false);
705 if (!selection.mark())
706 bv->beforeChange(this);
709 finishChange(bv, false);
713 if (!selection.mark())
714 bv->beforeChange(this);
717 finishChange(bv, false);
720 case LFUN_BREAKLINE: {
721 lyx::pos_type body = cursor.par()->beginningOfBody();
723 // Not allowed by LaTeX (labels or empty par)
724 if (cursor.pos() <= body)
727 bv->beforeChange(this);
728 insertInset(bv, new InsetNewline);
730 setCursor(bv, cursor.par(), cursor.pos());
731 moveCursorUpdate(bv, false);
736 if (!selection.set()) {
738 selection.cursor = cursor;
740 // It is possible to make it a lot faster still
741 // just comment out the line below...
745 cutSelection(bv, true);
748 moveCursorUpdate(bv, false);
749 bv->owner()->view_state_changed();
753 case LFUN_DELETE_SKIP:
754 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
755 if (!selection.set()) {
756 LyXCursor cur = cursor;
757 if (cur.pos() == cur.par()->size()) {
761 && !(cur.par()->params().spaceTop()
762 == VSpace (VSpace::NONE))) {
764 cur.par()->params().lineTop(),
765 cur.par()->params().lineBottom(),
766 cur.par()->params().pagebreakTop(),
767 cur.par()->params().pagebreakBottom(),
768 VSpace(VSpace::NONE),
769 cur.par()->params().spaceBottom(),
770 cur.par()->params().spacing(),
771 cur.par()->params().align(),
772 cur.par()->params().labelWidthString(), 0);
778 selection.cursor = cursor;
782 selection.cursor = cursor;
786 cutSelection(bv, true);
793 if (!selection.set()) {
794 if (bv->owner()->getIntl().getTransManager().backspace()) {
796 selection.cursor = cursor;
798 // It is possible to make it a lot faster still
799 // just comment out the line below...
804 cutSelection(bv, true);
807 bv->owner()->view_state_changed();
811 case LFUN_BACKSPACE_SKIP:
812 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
813 if (!selection.set()) {
814 LyXCursor cur = cursor;
816 && !(cur.par()->params().spaceTop()
817 == VSpace (VSpace::NONE))) {
819 cur.par()->params().lineTop(),
820 cur.par()->params().lineBottom(),
821 cur.par()->params().pagebreakTop(),
822 cur.par()->params().pagebreakBottom(),
823 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
824 cur.par()->params().spacing(),
825 cur.par()->params().align(),
826 cur.par()->params().labelWidthString(), 0);
829 selection.cursor = cur;
833 cutSelection(bv, true);
838 case LFUN_BREAKPARAGRAPH:
839 bv->beforeChange(this);
840 breakParagraph(bv, bv->buffer()->paragraphs, 0);
842 selection.cursor = cursor;
844 bv->owner()->view_state_changed();
847 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
848 bv->beforeChange(this);
849 breakParagraph(bv, bv->buffer()->paragraphs, 1);
851 selection.cursor = cursor;
853 bv->owner()->view_state_changed();
856 case LFUN_BREAKPARAGRAPH_SKIP: {
857 // When at the beginning of a paragraph, remove
858 // indentation and add a "defskip" at the top.
859 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
860 LyXCursor cur = cursor;
861 bv->beforeChange(this);
862 if (cur.pos() == 0) {
863 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
865 cur.par()->params().lineTop(),
866 cur.par()->params().lineBottom(),
867 cur.par()->params().pagebreakTop(),
868 cur.par()->params().pagebreakBottom(),
869 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
870 cur.par()->params().spacing(),
871 cur.par()->params().align(),
872 cur.par()->params().labelWidthString(), 1);
877 breakParagraph(bv, bv->buffer()->paragraphs, 0);
881 selection.cursor = cur;
883 bv->owner()->view_state_changed();
887 case LFUN_PARAGRAPH_SPACING: {
888 Paragraph * par = cursor.par();
889 Spacing::Space cur_spacing = par->params().spacing().getSpace();
890 float cur_value = 1.0;
891 if (cur_spacing == Spacing::Other)
892 cur_value = par->params().spacing().getValue();
894 istringstream is(cmd.argument.c_str());
897 Spacing::Space new_spacing = cur_spacing;
898 float new_value = cur_value;
900 lyxerr << "Missing argument to `paragraph-spacing'"
902 } else if (tmp == "single") {
903 new_spacing = Spacing::Single;
904 } else if (tmp == "onehalf") {
905 new_spacing = Spacing::Onehalf;
906 } else if (tmp == "double") {
907 new_spacing = Spacing::Double;
908 } else if (tmp == "other") {
909 new_spacing = Spacing::Other;
912 lyxerr << "new_value = " << tmpval << endl;
915 } else if (tmp == "default") {
916 new_spacing = Spacing::Default;
918 lyxerr << _("Unknown spacing argument: ")
919 << cmd.argument << endl;
921 if (cur_spacing != new_spacing || cur_value != new_value) {
922 par->params().spacing(Spacing(new_spacing, new_value));
929 case LFUN_INSET_TOGGLE:
931 bv->beforeChange(this);
938 case LFUN_PROTECTEDSPACE:
939 if (cursor.par()->layout()->free_spacing) {
943 specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
945 moveCursorUpdate(bv, false);
948 case LFUN_HYPHENATION:
949 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
952 case LFUN_LIGATURE_BREAK:
953 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
957 specialChar(this, bv, InsetSpecialChar::LDOTS);
960 case LFUN_END_OF_SENTENCE:
961 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
964 case LFUN_MENU_SEPARATOR:
965 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
969 bv->beforeChange(this);
971 selection.cursor = cursor;
972 cmd.message(N_("Mark off"));
976 bv->beforeChange(this);
977 selection.mark(true);
979 selection.cursor = cursor;
980 cmd.message(N_("Mark on"));
984 bv->beforeChange(this);
985 if (selection.mark()) {
987 cmd.message(N_("Mark removed"));
989 selection.mark(true);
991 cmd.message(N_("Mark set"));
993 selection.cursor = cursor;
996 case LFUN_UPCASE_WORD:
998 changeCase(*bv, LyXText::text_uppercase);
1000 bv->updateInset(inset_owner, true);
1004 case LFUN_LOWCASE_WORD:
1006 changeCase(*bv, LyXText::text_lowercase);
1008 bv->updateInset(inset_owner, true);
1012 case LFUN_CAPITALIZE_WORD:
1014 changeCase(*bv, LyXText::text_capitalization);
1016 bv->updateInset(inset_owner, true);
1020 case LFUN_TRANSPOSE_CHARS:
1022 transposeChars(*bv);
1024 bv->updateInset(inset_owner, true);
1029 cmd.message(_("Paste"));
1031 // clear the selection
1032 bv->toggleSelection();
1036 clearSelection(); // bug 393
1045 cutSelection(bv, true);
1047 cmd.message(_("Cut"));
1052 cmd.message(_("Copy"));
1055 case LFUN_BEGINNINGBUFSEL:
1057 return UNDISPATCHED;
1060 finishChange(bv, true);
1063 case LFUN_ENDBUFSEL:
1065 return UNDISPATCHED;
1068 finishChange(bv, true);
1072 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1078 istringstream is(cmd.argument.c_str());
1081 lyxerr << "SETXY: Could not parse coordinates in '"
1082 << cmd.argument << std::endl;
1084 setCursorFromCoordinates(bv, x, y);
1089 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1091 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1097 case LFUN_GETLAYOUT:
1098 cmd.message(tostr(cursor.par()->layout()));
1102 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1103 << cmd.argument << endl;
1105 // This is not the good solution to the empty argument
1106 // problem, but it will hopefully suffice for 1.2.0.
1107 // The correct solution would be to augument the
1108 // function list/array with information about what
1109 // functions needs arguments and their type.
1110 if (cmd.argument.empty()) {
1111 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1115 // Derive layout number from given argument (string)
1116 // and current buffer's textclass (number)
1117 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1118 bool hasLayout = tclass.hasLayout(cmd.argument);
1119 string layout = cmd.argument;
1121 // If the entry is obsolete, use the new one instead.
1123 string const & obs = tclass[layout]->obsoleted_by();
1129 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1134 bool change_layout = (current_layout != layout);
1135 if (!change_layout && selection.set() &&
1136 selection.start.par() != selection.end.par())
1138 Paragraph * spar = selection.start.par();
1139 Paragraph * epar = selection.end.par()->next();
1140 while (spar != epar) {
1141 if (spar->layout()->name() != current_layout) {
1142 change_layout = true;
1145 spar = spar->next();
1148 if (change_layout) {
1150 current_layout = layout;
1152 setLayout(bv, layout);
1153 bv->owner()->setLayout(layout);
1160 case LFUN_PASTESELECTION: {
1164 // this was originally a beforeChange(bv->text), i.e
1165 // the outermost LyXText!
1166 bv->beforeChange(this);
1167 string const clip = bv->getClipboard();
1168 if (!clip.empty()) {
1169 if (cmd.argument == "paragraph")
1170 insertStringAsParagraphs(bv, clip);
1172 insertStringAsLines(bv, clip);
1179 case LFUN_GOTOERROR:
1180 gotoInset(bv, Inset::ERROR_CODE, false);
1184 gotoInset(bv, Inset::NOTE_CODE, false);
1187 case LFUN_REFERENCE_GOTO:
1189 vector<Inset::Code> tmp;
1190 tmp.push_back(Inset::LABEL_CODE);
1191 tmp.push_back(Inset::REF_CODE);
1192 gotoInset(bv, tmp, true);
1197 Paragraph const * par = cursor.par();
1198 lyx::pos_type pos = cursor.pos();
1202 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1205 c = par->getChar(pos - 1);
1208 LyXLayout_ptr const & style = par->layout();
1210 if (style->pass_thru ||
1211 par->getFontSettings(bv->buffer()->params,
1212 pos).language()->lang() == "hebrew" ||
1213 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1214 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1218 case LFUN_DATE_INSERT: {
1219 time_t now_time_t = time(NULL);
1220 struct tm * now_tm = localtime(&now_time_t);
1221 setlocale(LC_TIME, "");
1223 if (!cmd.argument.empty())
1226 arg = lyxrc.date_insert_format;
1228 int const datetmp_len =
1229 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1231 for (int i = 0; i < datetmp_len; i++) {
1232 insertChar(bv, datetmp[i]);
1235 selection.cursor = cursor;
1236 moveCursorUpdate(bv, false);
1240 case LFUN_MOUSE_TRIPLE:
1243 if (isTopLevel() && bv->theLockingInset())
1245 if (cmd.button() == mouse_button::button1) {
1247 bv->screen().hideCursor();
1248 bv->screen().toggleSelection(this, bv);
1251 selection.cursor = cursor;
1255 bv->screen().toggleSelection(this, bv, false);
1257 bv->haveSelection(selection.set());
1261 case LFUN_MOUSE_DOUBLE:
1264 if (isTopLevel() && bv->theLockingInset())
1266 if (cmd.button() == mouse_button::button1) {
1268 bv->screen().hideCursor();
1269 bv->screen().toggleSelection(this, bv);
1270 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1271 bv->screen().toggleSelection(this, bv, false);
1273 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1276 bv->haveSelection(selection.set());
1280 case LFUN_MOUSE_MOTION:
1282 // Only use motion with button 1
1283 //if (ev.button() != mouse_button::button1)
1289 // Check for inset locking
1290 if (bv->theLockingInset()) {
1291 Inset * tli = bv->theLockingInset();
1292 LyXCursor cursor = bv->text->cursor;
1293 LyXFont font = bv->text->getFont(bv->buffer(),
1294 cursor.par(), cursor.pos());
1295 int width = tli->width(bv, font);
1296 int inset_x = font.isVisibleRightToLeft()
1297 ? cursor.ix() - width : cursor.ix();
1298 int start_x = inset_x + tli->scroll();
1299 FuncRequest cmd1 = cmd;
1300 cmd1.x = cmd.x - start_x;
1301 cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1302 tli->localDispatch(cmd1);
1306 // The test for not selection possible is needed, that only motion
1307 // events are used, where the bottom press event was on
1308 // the drawing area too
1309 if (!selection_possible) {
1310 lyxerr[Debug::ACTION]
1311 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1315 bv->screen().hideCursor();
1317 Row * cursorrow = bv->text->cursor.row();
1318 bv->text->setCursorFromCoordinates(bv, cmd.x, cmd.y + bv->text->top_y());
1320 // sorry for this but I have a strange error that the y value jumps at
1321 // a certain point. This seems like an error in my xforms library or
1322 // in some other local environment, but I would like to leave this here
1323 // for the moment until I can remove this (Jug 20020418)
1324 if (y_before < bv->text->cursor.y())
1325 lyxerr << y_before << ':'
1326 << bv->text->cursor.y() << endl;
1328 // This is to allow jumping over large insets
1329 if (cursorrow == bv->text->cursor.row()) {
1330 if (cmd.y >= bv->workHeight())
1331 bv->text->cursorDown(bv, false);
1333 bv->text->cursorUp(bv, false);
1336 // Maybe an empty line was deleted
1337 if (!bv->text->selection.set())
1338 bv->update(bv->text, BufferView::UPDATE);
1339 bv->text->setSelection(bv);
1340 bv->screen().toggleToggle(bv->text, bv);
1346 // Single-click on work area
1347 case LFUN_MOUSE_PRESS:
1352 // ok ok, this is a hack (for xforms)
1353 // We shouldn't go further down as we really should only do the
1354 // scrolling and be done with this. Otherwise we may open some
1355 // dialogs (Jug 20020424).
1356 if (cmd.button() == mouse_button::button4) {
1357 bv->scroll(-lyxrc.wheel_jump);
1360 if (cmd.button() == mouse_button::button5) {
1361 bv->scroll(lyxrc.wheel_jump);
1367 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1369 // Middle button press pastes if we have a selection
1370 // We do this here as if the selection was inside an inset
1371 // it could get cleared on the unlocking of the inset so
1372 // we have to check this first
1373 bool paste_internally = false;
1374 if (cmd.button() == mouse_button::button2 && selection.set()) {
1375 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1376 paste_internally = true;
1379 int const screen_first = bv->text->top_y();
1381 if (bv->theLockingInset()) {
1382 // We are in inset locking mode
1384 // Check whether the inset was hit. If not reset mode,
1385 // otherwise give the event to the inset
1386 if (inset_hit == bv->theLockingInset()) {
1387 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1388 bv->theLockingInset()->localDispatch(cmd1);
1391 bv->unlockInset(bv->theLockingInset());
1395 selection_possible = true;
1396 bv->screen().hideCursor();
1398 // Clear the selection
1399 bv->screen().toggleSelection(bv->text, bv);
1400 bv->text->clearSelection();
1401 bv->text->fullRebreak(bv);
1403 bv->updateScrollbar();
1405 // Single left click in math inset?
1406 if (isHighlyEditableInset(inset_hit)) {
1407 // Highly editable inset, like math
1408 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1409 selection_possible = false;
1410 bv->owner()->updateLayoutChoice();
1411 bv->owner()->message(inset->editMessage());
1412 //inset->edit(bv, x, y, cmd.button());
1413 // We just have to lock the inset before calling a PressEvent on it!
1414 // we don't need the edit() call here! (Jug20020329)
1415 if (!bv->lockInset(inset))
1416 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1417 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1418 inset->localDispatch(cmd1);
1421 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1423 // Right click on a footnote flag opens float menu
1424 if (cmd.button() == mouse_button::button3) {
1425 selection_possible = false;
1429 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1430 bv->text->setCursorFromCoordinates(bv, x, y + screen_first);
1432 bv->text->selection.cursor = bv->text->cursor;
1433 bv->text->cursor.x_fix(bv->text->cursor.x());
1435 bv->owner()->updateLayoutChoice();
1436 if (bv->fitCursor())
1437 selection_possible = false;
1439 // Insert primary selection with middle mouse
1440 // if there is a local selection in the current buffer,
1442 if (cmd.button() == mouse_button::button2) {
1443 if (paste_internally)
1444 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1446 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1447 selection_possible = false;
1452 case LFUN_MOUSE_RELEASE:
1454 // do nothing if we used the mouse wheel
1458 if (cmd.button() == mouse_button::button4
1459 || cmd.button() == mouse_button::button5)
1462 // If we hit an inset, we have the inset coordinates in these
1463 // and inset_hit points to the inset. If we do not hit an
1464 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1467 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1469 if (bv->theLockingInset()) {
1470 // We are in inset locking mode.
1472 // LyX does a kind of work-area grabbing for insets.
1473 // Only a ButtonPress FuncRequest outside the inset will
1474 // force a insetUnlock.
1475 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1476 bv->theLockingInset()->localDispatch(cmd1);
1480 selection_possible = false;
1482 if (cmd.button() == mouse_button::button2)
1486 if (cmd.button() == mouse_button::button1)
1487 bv->haveSelection(selection.set());
1490 bv->owner()->view_state_changed();
1491 bv->owner()->updateMenubar();
1492 bv->owner()->updateToolbar();
1494 // Did we hit an editable inset?
1496 selection_possible = false;
1498 // if we reach this point with a selection, it
1499 // must mean we are currently selecting.
1500 // But we don't want to open the inset
1501 // because that is annoying for the user.
1502 // So just pretend we didn't hit it.
1503 // this is OK because a "kosher" ButtonRelease
1504 // will follow a ButtonPress that clears
1506 // Note this also fixes selection drawing
1507 // problems if we end up opening an inset
1508 if (selection.set())
1511 // CHECK fix this proper in 0.13
1512 // well, maybe 13.0 !!!!!!!!!
1514 // Following a ref shouldn't issue
1515 // a push on the undo-stack
1516 // anylonger, now that we have
1517 // keybindings for following
1518 // references and returning from
1519 // references. IMHO though, it
1520 // should be the inset's own business
1521 // to push or not push on the undo
1522 // stack. They don't *have* to
1523 // alter the document...
1525 // ...or maybe the SetCursorParUndo()
1526 // below isn't necessary at all anylonger?
1527 if (inset_hit->lyxCode() == Inset::REF_CODE)
1528 setCursorParUndo(bv);
1530 bv->owner()->message(inset_hit->editMessage());
1532 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1533 inset_hit->localDispatch(cmd1);
1539 case LFUN_SELFINSERT: {
1540 if (cmd.argument.empty())
1543 // Automatically delete the currently selected
1544 // text and replace it with what is being
1545 // typed in now. Depends on lyxrc settings
1546 // "auto_region_delete", which defaults to
1549 if (lyxrc.auto_region_delete) {
1550 if (selection.set()) {
1551 cutSelection(bv, false, false);
1554 bv->haveSelection(false);
1557 bv->beforeChange(this);
1558 LyXFont const old_font(real_current_font);
1560 string::const_iterator cit = cmd.argument.begin();
1561 string::const_iterator end = cmd.argument.end();
1562 for (; cit != end; ++cit)
1563 bv->owner()->getIntl().getTransManager().
1564 TranslateAndInsert(*cit, this);
1567 selection.cursor = cursor;
1568 moveCursorUpdate(bv, false);
1570 // real_current_font.number can change so we need to
1571 // update the minibuffer
1572 if (old_font != real_current_font)
1573 bv->owner()->view_state_changed();
1577 case LFUN_HTMLURL: {
1578 InsetCommandParams p("htmlurl");
1579 string const data = InsetCommandMailer::params2string("url", p);
1580 bv->owner()->getDialogs().show("url", data, 0);
1585 InsetCommandParams p("url");
1586 string const data = InsetCommandMailer::params2string("url", p);
1587 bv->owner()->getDialogs().show("url", data, 0);
1593 case LFUN_INSET_LIST:
1594 case LFUN_INSET_THEOREM:
1595 case LFUN_INSET_CAPTION:
1597 case LFUN_INSERT_NOTE:
1598 case LFUN_INSERT_BIBITEM:
1599 case LFUN_INSET_ERT:
1600 case LFUN_INSET_FLOAT:
1601 case LFUN_INSET_FOOTNOTE:
1602 case LFUN_INSET_MARGINAL:
1603 case LFUN_INSET_MINIPAGE:
1604 case LFUN_INSET_OPTARG:
1605 case LFUN_INSET_WIDE_FLOAT:
1606 case LFUN_INSET_WRAP:
1607 case LFUN_TABULAR_INSERT:
1608 case LFUN_ENVIRONMENT_INSERT:
1609 // Open the inset, and move the current selection
1611 doInsertInset(this, cmd, true, true);
1614 case LFUN_INDEX_INSERT:
1615 // Just open the inset
1616 doInsertInset(this, cmd, true, false);
1619 case LFUN_INDEX_PRINT:
1620 case LFUN_PARENTINSERT:
1621 case LFUN_TOC_INSERT:
1624 doInsertInset(this, cmd, false, false);
1628 return UNDISPATCHED;