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"
27 #include "support/lstrings.h"
28 #include "frontends/LyXView.h"
29 #include "frontends/screen.h"
30 #include "frontends/WorkArea.h"
31 #include "insets/insetspecialchar.h"
32 #include "insets/insettext.h"
33 #include "insets/insetbib.h"
34 #include "insets/insetquotes.h"
35 #include "insets/insetcommand.h"
36 #include "undo_funcs.h"
43 extern string current_layout;
44 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
46 // the selection possible is needed, that only motion events are
47 // used, where the bottom press event was on the drawing area too
48 bool selection_possible = false;
53 void moveCursorUpdate(BufferView * bv, bool selecting)
55 LyXText * lt = bv->getLyXText();
57 if (selecting || lt->selection.mark()) {
62 bv->updateInset(lt->inset_owner, false);
66 // bv->update(lt, BufferView::SELECT|BufferView::FITCUR);
68 bv->update(lt, BufferView::SELECT|BufferView::FITCUR);
70 } else if (bv->text->status() != LyXText::UNCHANGED) {
71 bv->theLockingInset()->hideInsetCursor(bv);
72 bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
76 if (!lt->selection.set())
77 bv->workarea().haveSelection(false);
83 void finishChange(BufferView * bv, bool selecting = false)
86 moveCursorUpdate(bv, selecting);
87 bv->owner()->view_state_changed();
93 bool LyXText::gotoNextInset(BufferView * bv,
94 vector<Inset::Code> const & codes, string const & contents) const
96 LyXCursor res = cursor;
99 if (res.pos() < res.par()->size() - 1) {
100 res.pos(res.pos() + 1);
102 res.par(res.par()->next());
106 } while (res.par() &&
107 !(res.par()->isInset(res.pos())
108 && (inset = res.par()->getInset(res.pos())) != 0
109 && find(codes.begin(), codes.end(), inset->lyxCode())
111 && (contents.empty() ||
112 static_cast<InsetCommand *>(
113 res.par()->getInset(res.pos()))->getContents()
117 setCursor(bv, res.par(), res.pos(), false);
124 void LyXText::gotoInset(BufferView * bv, vector<Inset::Code> const & codes,
128 bv->beforeChange(this);
132 if (same_content && cursor.par()->isInset(cursor.pos())) {
133 Inset const * inset = cursor.par()->getInset(cursor.pos());
134 if (find(codes.begin(), codes.end(), inset->lyxCode())
136 contents = static_cast<InsetCommand const *>(inset)->getContents();
139 if (!gotoNextInset(bv, codes, contents)) {
140 if (cursor.pos() || cursor.par() != ownerParagraph()) {
141 LyXCursor tmp = cursor;
142 cursor.par(ownerParagraph());
144 if (!gotoNextInset(bv, codes, contents)) {
146 bv->owner()->message(_("No more insets"));
149 bv->owner()->message(_("No more insets"));
153 selection.cursor = cursor;
157 void LyXText::gotoInset(BufferView * bv, Inset::Code code, bool same_content)
159 gotoInset(bv, vector<Inset::Code>(1, code), same_content);
163 void LyXText::cursorPrevious(BufferView * bv)
165 if (!cursor.row()->previous()) {
167 int new_y = bv->text->first_y - bv->workarea().workHeight();
168 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
169 bv->updateScrollbar();
175 Row * cursorrow = cursor.row();
177 setCursorFromCoordinates(bv, cursor.x_fix(), y);
181 if (cursorrow == bv->text->cursor.row()) {
182 // we have a row which is higher than the workarea so we leave the
183 // cursor on the start of the row and move only the draw up as soon
184 // as we move the cursor or do something while inside the row (it may
185 // span several workarea-heights) we'll move to the top again, but this
186 // is better than just jump down and only display part of the row.
187 new_y = bv->text->first_y - bv->workarea().workHeight();
190 new_y = bv->text->cursor.iy()
191 + bv->theLockingInset()->insetInInsetY() + y
192 + cursor.row()->height()
193 - bv->workarea().workHeight() + 1;
196 - cursor.row()->baseline()
197 + cursor.row()->height()
198 - bv->workarea().workHeight() + 1;
201 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
202 if (cursor.row()->previous()) {
204 setCursor(bv, cur, cursor.row()->previous()->par(),
205 cursor.row()->previous()->pos(), false);
206 if (cur.y() > first_y) {
210 bv->updateScrollbar();
214 void LyXText::cursorNext(BufferView * bv)
216 if (!cursor.row()->next()) {
217 int y = cursor.y() - cursor.row()->baseline() +
218 cursor.row()->height();
219 if (y > int(first_y + bv->workarea().workHeight())) {
220 bv->screen().draw(bv->text, bv,
221 bv->text->first_y + bv->workarea().workHeight());
222 bv->updateScrollbar();
227 int y = first_y + bv->workarea().workHeight();
228 if (inset_owner && !first_y) {
229 y -= (bv->text->cursor.iy()
231 + bv->theLockingInset()->insetInInsetY());
236 Row * cursorrow = cursor.row();
237 setCursorFromCoordinates(bv, cursor.x_fix(), y);
238 // + workarea().workHeight());
242 if (cursorrow == bv->text->cursor.row()) {
243 // we have a row which is higher than the workarea so we leave the
244 // cursor on the start of the row and move only the draw down as soon
245 // as we move the cursor or do something while inside the row (it may
246 // span several workarea-heights) we'll move to the top again, but this
247 // is better than just jump down and only display part of the row.
248 new_y = bv->text->first_y + bv->workarea().workHeight();
251 new_y = bv->text->cursor.iy()
252 + bv->theLockingInset()->insetInInsetY()
253 + y - cursor.row()->baseline();
255 new_y = cursor.y() - cursor.row()->baseline();
258 bv->screen().draw(bv->text, bv, new_y);
259 if (cursor.row()->next()) {
261 setCursor(bv, cur, cursor.row()->next()->par(),
262 cursor.row()->next()->pos(), false);
263 if (cur.y() < int(first_y + bv->workarea().workHeight())) {
264 cursorDown(bv, true);
267 bv->updateScrollbar();
271 void LyXText::update(BufferView * bv, bool changed)
273 BufferView::UpdateCodes c = BufferView::SELECT | BufferView::FITCUR;
275 bv->update(this, c | BufferView::CHANGE);
281 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
285 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
286 if (!bv->insertInset(new_inset))
289 bv->updateInset(new_inset, true);
293 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
295 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
296 <<"] arg[" << cmd.argument << "]" << endl;
298 BufferView * bv = cmd.view();
300 switch (cmd.action) {
302 case LFUN_APPENDIX: {
303 Paragraph * par = cursor.par();
304 bool start = !par->params().startOfAppendix();
306 // ensure that we have only one start_of_appendix in this document
307 Paragraph * tmp = ownerParagraph();
308 for (; tmp; tmp = tmp->next())
309 tmp->params().startOfAppendix(false);
311 par->params().startOfAppendix(start);
313 // we can set the refreshing parameters now
314 status(cmd.view(), LyXText::NEED_MORE_REFRESH);
316 refresh_row = 0; // not needed for full update
317 updateCounters(cmd.view());
318 setCursor(cmd.view(), cursor.par(), cursor.pos());
323 case LFUN_DELETE_WORD_FORWARD:
324 bv->beforeChange(this);
326 deleteWordForward(bv);
331 case LFUN_DELETE_WORD_BACKWARD:
332 bv->beforeChange(this);
334 deleteWordBackward(bv);
339 case LFUN_DELETE_LINE_FORWARD:
340 bv->beforeChange(this);
342 deleteLineForward(bv);
349 if (!selection.mark())
350 bv->beforeChange(this);
357 if (!selection.mark())
358 bv->beforeChange(this);
360 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
361 cursorLeftOneWord(bv);
363 cursorRightOneWord(bv);
368 if (!selection.mark())
369 bv->beforeChange(this);
371 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
372 cursorRightOneWord(bv);
374 cursorLeftOneWord(bv);
378 case LFUN_BEGINNINGBUF:
379 if (!selection.mark())
380 bv->beforeChange(this);
387 if (selection.mark())
388 bv->beforeChange(this);
396 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
400 finishChange(bv, true);
405 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
409 finishChange(bv, true);
415 finishChange(bv, true);
420 cursorDown(bv, true);
421 finishChange(bv, true);
424 case LFUN_UP_PARAGRAPHSEL:
426 cursorUpParagraph(bv);
427 finishChange(bv, true);
430 case LFUN_DOWN_PARAGRAPHSEL:
432 cursorDownParagraph(bv);
433 finishChange(bv, true);
439 finishChange(bv, true);
445 finishChange(bv, true);
451 finishChange(bv, true);
457 finishChange(bv, true);
460 case LFUN_WORDRIGHTSEL:
462 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
463 cursorLeftOneWord(bv);
465 cursorRightOneWord(bv);
466 finishChange(bv, true);
469 case LFUN_WORDLEFTSEL:
471 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
472 cursorRightOneWord(bv);
474 cursorLeftOneWord(bv);
475 finishChange(bv, true);
479 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
480 if (!selection.mark())
481 bv->beforeChange(this);
484 cursorLeft(bv, false);
485 if (cursor.pos() < cursor.par()->size()
486 && cursor.par()->isInset(cursor.pos())
487 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
488 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
489 cmd.message(tmpinset->editMessage());
490 tmpinset->edit(bv, !is_rtl);
494 cursorRight(bv, false);
500 // This is soooo ugly. Isn`t it possible to make
502 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
503 if (!selection.mark())
504 bv->beforeChange(this);
506 LyXCursor const cur = cursor;
508 cursorLeft(bv, false);
509 if ((is_rtl || cur != cursor) && // only if really moved!
510 cursor.pos() < cursor.par()->size() &&
511 cursor.par()->isInset(cursor.pos()) &&
512 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
513 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
514 cmd.message(tmpinset->editMessage());
515 tmpinset->edit(bv, is_rtl);
519 cursorRight(bv, false);
525 if (!selection.mark())
526 bv->beforeChange(this);
527 bv->update(this, BufferView::UPDATE);
533 if (!selection.mark())
534 bv->beforeChange(this);
535 bv->update(this, BufferView::UPDATE);
540 case LFUN_UP_PARAGRAPH:
541 if (!selection.mark())
542 bv->beforeChange(this);
543 bv->update(this, BufferView::UPDATE);
544 cursorUpParagraph(bv);
548 case LFUN_DOWN_PARAGRAPH:
549 if (!selection.mark())
550 bv->beforeChange(this);
551 bv->update(this, BufferView::UPDATE);
552 cursorDownParagraph(bv);
553 finishChange(bv, false);
557 if (!selection.mark())
558 bv->beforeChange(this);
559 bv->update(this, BufferView::UPDATE);
561 finishChange(bv, false);
564 // moveCursorUpdate(bv, false, false);
565 // owner_->view_state_changed();
569 if (!selection.mark())
570 bv->beforeChange(this);
571 bv->update(this, BufferView::UPDATE);
573 finishChange(bv, false);
577 if (!selection.mark())
578 bv->beforeChange(this);
581 finishChange(bv, false);
585 if (!selection.mark())
586 bv->beforeChange(this);
589 finishChange(bv, false);
593 bv->beforeChange(this);
594 insertChar(bv, Paragraph::META_NEWLINE);
596 setCursor(bv, cursor.par(), cursor.pos());
597 moveCursorUpdate(bv, false);
601 if (!selection.set()) {
603 selection.cursor = cursor;
605 // It is possible to make it a lot faster still
606 // just comment out the line below...
610 cutSelection(bv, true);
613 moveCursorUpdate(bv, false);
614 bv->owner()->view_state_changed();
618 case LFUN_DELETE_SKIP:
619 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
620 if (!selection.set()) {
621 LyXCursor cur = cursor;
622 if (cur.pos() == cur.par()->size()) {
626 && !(cur.par()->params().spaceTop()
627 == VSpace (VSpace::NONE))) {
629 cur.par()->params().lineTop(),
630 cur.par()->params().lineBottom(),
631 cur.par()->params().pagebreakTop(),
632 cur.par()->params().pagebreakBottom(),
633 VSpace(VSpace::NONE),
634 cur.par()->params().spaceBottom(),
635 cur.par()->params().spacing(),
636 cur.par()->params().align(),
637 cur.par()->params().labelWidthString(), 0);
643 selection.cursor = cursor;
647 selection.cursor = cursor;
651 cutSelection(bv, true);
658 if (!selection.set()) {
659 if (bv->owner()->getIntl().getTransManager().backspace()) {
661 selection.cursor = cursor;
663 // It is possible to make it a lot faster still
664 // just comment out the line below...
669 cutSelection(bv, true);
672 bv->owner()->view_state_changed();
676 case LFUN_BACKSPACE_SKIP:
677 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
678 if (!selection.set()) {
679 LyXCursor cur = cursor;
681 && !(cur.par()->params().spaceTop()
682 == VSpace (VSpace::NONE))) {
684 cur.par()->params().lineTop(),
685 cur.par()->params().lineBottom(),
686 cur.par()->params().pagebreakTop(),
687 cur.par()->params().pagebreakBottom(),
688 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
689 cur.par()->params().spacing(),
690 cur.par()->params().align(),
691 cur.par()->params().labelWidthString(), 0);
694 selection.cursor = cur;
698 cutSelection(bv, true);
703 case LFUN_BREAKPARAGRAPH:
704 bv->beforeChange(this);
705 breakParagraph(bv, 0);
707 selection.cursor = cursor;
709 bv->owner()->view_state_changed();
712 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
713 bv->beforeChange(this);
714 breakParagraph(bv, 1);
716 selection.cursor = cursor;
718 bv->owner()->view_state_changed();
721 case LFUN_BREAKPARAGRAPH_SKIP: {
722 // When at the beginning of a paragraph, remove
723 // indentation and add a "defskip" at the top.
724 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
725 LyXCursor cur = cursor;
726 bv->beforeChange(this);
727 if (cur.pos() == 0) {
728 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
730 cur.par()->params().lineTop(),
731 cur.par()->params().lineBottom(),
732 cur.par()->params().pagebreakTop(),
733 cur.par()->params().pagebreakBottom(),
734 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
735 cur.par()->params().spacing(),
736 cur.par()->params().align(),
737 cur.par()->params().labelWidthString(), 1);
742 breakParagraph(bv, 0);
746 selection.cursor = cur;
748 bv->owner()->view_state_changed();
752 case LFUN_PARAGRAPH_SPACING: {
753 Paragraph * par = cursor.par();
754 Spacing::Space cur_spacing = par->params().spacing().getSpace();
755 float cur_value = 1.0;
756 if (cur_spacing == Spacing::Other)
757 cur_value = par->params().spacing().getValue();
759 istringstream is(cmd.argument.c_str());
762 Spacing::Space new_spacing = cur_spacing;
763 float new_value = cur_value;
765 lyxerr << "Missing argument to `paragraph-spacing'"
767 } else if (tmp == "single") {
768 new_spacing = Spacing::Single;
769 } else if (tmp == "onehalf") {
770 new_spacing = Spacing::Onehalf;
771 } else if (tmp == "double") {
772 new_spacing = Spacing::Double;
773 } else if (tmp == "other") {
774 new_spacing = Spacing::Other;
777 lyxerr << "new_value = " << tmpval << endl;
780 } else if (tmp == "default") {
781 new_spacing = Spacing::Default;
783 lyxerr << _("Unknown spacing argument: ")
784 << cmd.argument << endl;
786 if (cur_spacing != new_spacing || cur_value != new_value) {
787 par->params().spacing(Spacing(new_spacing, new_value));
794 case LFUN_INSET_TOGGLE:
796 bv->beforeChange(this);
803 case LFUN_PROTECTEDSPACE:
804 if (cursor.par()->layout()->free_spacing) {
808 specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
810 moveCursorUpdate(bv, false);
813 case LFUN_HYPHENATION:
814 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
817 case LFUN_LIGATURE_BREAK:
818 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
822 specialChar(this, bv, InsetSpecialChar::LDOTS);
828 insertChar(bv, Paragraph::META_HFILL);
832 case LFUN_END_OF_SENTENCE:
833 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
836 case LFUN_MENU_SEPARATOR:
837 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
841 bv->beforeChange(this);
843 selection.cursor = cursor;
844 cmd.message(N_("Mark off"));
848 bv->beforeChange(this);
849 selection.mark(true);
851 selection.cursor = cursor;
852 cmd.message(N_("Mark on"));
856 bv->beforeChange(this);
857 if (selection.mark()) {
859 cmd.message(N_("Mark removed"));
861 selection.mark(true);
863 cmd.message(N_("Mark set"));
865 selection.cursor = cursor;
868 case LFUN_UPCASE_WORD:
870 changeCase(bv, LyXText::text_uppercase);
872 bv->updateInset(inset_owner, true);
876 case LFUN_LOWCASE_WORD:
878 changeCase(bv, LyXText::text_lowercase);
880 bv->updateInset(inset_owner, true);
884 case LFUN_CAPITALIZE_WORD:
886 changeCase(bv, LyXText::text_capitalization);
888 bv->updateInset(inset_owner, true);
892 case LFUN_TRANSPOSE_CHARS:
896 bv->updateInset(inset_owner, true);
901 cmd.message(_("Paste"));
903 // clear the selection
904 bv->toggleSelection();
908 clearSelection(); // bug 393
917 cutSelection(bv, true);
919 cmd.message(_("Cut"));
924 cmd.message(_("Copy"));
927 case LFUN_BEGINNINGBUFSEL:
929 return Inset::UNDISPATCHED;
932 finishChange(bv, true);
937 return Inset::UNDISPATCHED;
940 finishChange(bv, true);
944 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
950 istringstream is(cmd.argument.c_str());
953 lyxerr << "SETXY: Could not parse coordinates in '"
954 << cmd.argument << std::endl;
956 setCursorFromCoordinates(bv, x, y);
961 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
963 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
970 cmd.message(tostr(cursor.par()->layout()));
974 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
975 << cmd.argument << endl;
977 // This is not the good solution to the empty argument
978 // problem, but it will hopefully suffice for 1.2.0.
979 // The correct solution would be to augument the
980 // function list/array with information about what
981 // functions needs arguments and their type.
982 if (cmd.argument.empty()) {
983 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
987 // Derive layout number from given argument (string)
988 // and current buffer's textclass (number)
989 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
990 bool hasLayout = tclass.hasLayout(cmd.argument);
991 string layout = cmd.argument;
993 // If the entry is obsolete, use the new one instead.
995 string const & obs = tclass[layout]->obsoleted_by();
1001 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1006 bool change_layout = (current_layout != layout);
1007 if (!change_layout && selection.set() &&
1008 selection.start.par() != selection.end.par())
1010 Paragraph * spar = selection.start.par();
1011 Paragraph * epar = selection.end.par()->next();
1012 while (spar != epar) {
1013 if (spar->layout()->name() != current_layout) {
1014 change_layout = true;
1019 if (change_layout) {
1021 current_layout = layout;
1023 setLayout(bv, layout);
1024 bv->owner()->setLayout(layout);
1031 case LFUN_PASTESELECTION: {
1035 // this was originally a beforeChange(bv->text), i.e
1036 // the outermost LyXText!
1037 bv->beforeChange(this);
1038 string const clip = bv->workarea().getClipboard();
1039 if (!clip.empty()) {
1040 if (cmd.argument == "paragraph")
1041 insertStringAsParagraphs(bv, clip);
1043 insertStringAsLines(bv, clip);
1050 case LFUN_GOTOERROR:
1051 gotoInset(bv, Inset::ERROR_CODE, false);
1055 gotoInset(bv, Inset::NOTE_CODE, false);
1058 case LFUN_REFERENCE_GOTO:
1060 vector<Inset::Code> tmp;
1061 tmp.push_back(Inset::LABEL_CODE);
1062 tmp.push_back(Inset::REF_CODE);
1063 gotoInset(bv, tmp, true);
1068 case LFUN_INSET_LIST:
1069 case LFUN_INSET_THEOREM:
1071 case LFUN_INSERT_NOTE:
1072 case LFUN_INSET_ERT:
1073 case LFUN_INSET_EXTERNAL:
1074 case LFUN_INSET_FLOAT:
1075 case LFUN_INSET_FOOTNOTE:
1076 case LFUN_INSET_MARGINAL:
1077 case LFUN_INSET_MINIPAGE:
1078 case LFUN_INSET_OPTARG:
1079 case LFUN_INSET_WIDE_FLOAT:
1080 case LFUN_TABULAR_INSERT:
1082 Inset * inset = createInset(cmd);
1084 bool gotsel = false;
1085 if (selection.set()) {
1086 cutSelection(bv, true, false);
1089 if (bv->insertInset(inset)) {
1092 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
1102 Paragraph const * par = cursor.par();
1103 lyx::pos_type pos = cursor.pos();
1107 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1110 c = par->getChar(pos - 1);
1113 LyXLayout_ptr const & style = par->layout();
1115 if (style->pass_thru ||
1116 par->getFontSettings(bv->buffer()->params,
1117 pos).language()->lang() == "hebrew" ||
1118 (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1119 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1123 case LFUN_DATE_INSERT: {
1124 time_t now_time_t = time(NULL);
1125 struct tm * now_tm = localtime(&now_time_t);
1126 setlocale(LC_TIME, "");
1128 if (!cmd.argument.empty())
1131 arg = lyxrc.date_insert_format;
1133 int const datetmp_len =
1134 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1136 for (int i = 0; i < datetmp_len; i++) {
1137 insertChar(bv, datetmp[i]);
1140 selection.cursor = cursor;
1141 moveCursorUpdate(bv, false);
1145 case LFUN_MOUSE_TRIPLE:
1148 if (bv_owner && bv->theLockingInset())
1150 if (cmd.button() == mouse_button::button1) {
1152 bv->screen().hideCursor();
1153 bv->screen().toggleSelection(this, bv);
1156 selection.cursor = cursor;
1160 bv->screen().toggleSelection(this, bv, false);
1162 bv->workarea().haveSelection(selection.set());
1166 case LFUN_MOUSE_DOUBLE:
1169 if (bv_owner && bv->theLockingInset())
1171 if (cmd.button() == mouse_button::button1) {
1173 bv->screen().hideCursor();
1174 bv->screen().toggleSelection(this, bv);
1175 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1176 bv->screen().toggleSelection(this, bv, false);
1178 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1181 bv->workarea().haveSelection(selection.set());
1185 case LFUN_MOUSE_MOTION:
1187 // Only use motion with button 1
1188 //if (ev.button() != mouse_button::button1)
1194 // Check for inset locking
1195 if (bv->theLockingInset()) {
1196 Inset * tli = bv->theLockingInset();
1197 LyXCursor cursor = bv->text->cursor;
1198 LyXFont font = bv->text->getFont(bv->buffer(),
1199 cursor.par(), cursor.pos());
1200 int width = tli->width(bv, font);
1201 int inset_x = font.isVisibleRightToLeft()
1202 ? cursor.ix() - width : cursor.ix();
1203 int start_x = inset_x + tli->scroll();
1204 FuncRequest cmd1 = cmd;
1206 cmd1.y -= cursor.iy() + bv->text->first_y;
1207 tli->localDispatch(cmd1);
1211 // The test for not selection possible is needed, that only motion
1212 // events are used, where the bottom press event was on
1213 // the drawing area too
1214 if (!selection_possible) {
1215 lyxerr[Debug::ACTION]
1216 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1220 bv->screen().hideCursor();
1222 Row * cursorrow = bv->text->cursor.row();
1223 bv->text->setCursorFromCoordinates(bv, cmd.x, cmd.y + bv->text->first_y);
1225 // sorry for this but I have a strange error that the y value jumps at
1226 // a certain point. This seems like an error in my xforms library or
1227 // in some other local environment, but I would like to leave this here
1228 // for the moment until I can remove this (Jug 20020418)
1229 if (y_before < bv->text->cursor.y())
1230 lyxerr << y_before << ":" << bv->text->cursor.y() << endl;
1232 // This is to allow jumping over large insets
1233 if (cursorrow == bv->text->cursor.row()) {
1234 if (cmd.y >= int(bv->workarea().workHeight())) {
1235 bv->text->cursorDown(bv, false);
1236 } else if (cmd.y < 0) {
1237 bv->text->cursorUp(bv, false);
1241 if (!bv->text->selection.set())
1242 bv->update(bv->text, BufferView::UPDATE); // Maybe an empty line was deleted
1244 bv->text->setSelection(bv);
1245 bv->screen().toggleToggle(bv->text, bv);
1251 // Single-click on work area
1252 case LFUN_MOUSE_PRESS:
1257 // ok ok, this is a hack (for xforms)
1258 // We shouldn't go further down as we really should only do the
1259 // scrolling and be done with this. Otherwise we may open some
1260 // dialogs (Jug 20020424).
1261 if (cmd.button() == mouse_button::button4) {
1262 bv->scroll(-lyxrc.wheel_jump);
1265 if (cmd.button() == mouse_button::button5) {
1266 bv->scroll(lyxrc.wheel_jump);
1272 Inset * inset_hit = bv->checkInsetHit(bv->text, x, y);
1274 // Middle button press pastes if we have a selection
1275 // We do this here as if the selection was inside an inset
1276 // it could get cleared on the unlocking of the inset so
1277 // we have to check this first
1278 bool paste_internally = false;
1279 if (cmd.button() == mouse_button::button2 && selection.set()) {
1280 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1281 paste_internally = true;
1284 int const screen_first = bv->text->first_y;
1286 if (bv->theLockingInset()) {
1287 // We are in inset locking mode
1289 // Check whether the inset was hit. If not reset mode,
1290 // otherwise give the event to the inset
1291 if (inset_hit == bv->theLockingInset()) {
1292 FuncRequest cmd(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1293 bv->theLockingInset()->localDispatch(cmd);
1296 bv->unlockInset(bv->theLockingInset());
1300 selection_possible = true;
1301 bv->screen().hideCursor();
1303 // Clear the selection
1304 bv->screen().toggleSelection(bv->text, bv);
1305 bv->text->clearSelection();
1306 bv->text->fullRebreak(bv);
1308 bv->updateScrollbar();
1310 // Single left click in math inset?
1311 if (isHighlyEditableInset(inset_hit)) {
1312 // Highly editable inset, like math
1313 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1314 selection_possible = false;
1315 bv->owner()->updateLayoutChoice();
1316 bv->owner()->message(inset->editMessage());
1317 //inset->edit(bv, x, y, cmd.button());
1318 // We just have to lock the inset before calling a PressEvent on it!
1319 // we don't need the edit() call here! (Jug20020329)
1320 if (!bv->lockInset(inset))
1321 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1322 FuncRequest cmd(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1323 inset->localDispatch(cmd);
1326 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1328 // Right click on a footnote flag opens float menu
1329 if (cmd.button() == mouse_button::button3) {
1330 selection_possible = false;
1334 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1335 bv->text->setCursorFromCoordinates(bv, x, y + screen_first);
1337 bv->text->selection.cursor = bv->text->cursor;
1338 bv->text->cursor.x_fix(bv->text->cursor.x());
1340 bv->owner()->updateLayoutChoice();
1341 if (bv->fitCursor())
1342 selection_possible = false;
1344 // Insert primary selection with middle mouse
1345 // if there is a local selection in the current buffer,
1347 if (cmd.button() == mouse_button::button2) {
1348 if (paste_internally)
1349 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1351 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1352 selection_possible = false;
1357 case LFUN_MOUSE_RELEASE:
1359 // do nothing if we used the mouse wheel
1363 if (cmd.button() == mouse_button::button4
1364 || cmd.button() == mouse_button::button5)
1367 // If we hit an inset, we have the inset coordinates in these
1368 // and inset_hit points to the inset. If we do not hit an
1369 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1372 Inset * inset_hit = bv->checkInsetHit(bv->text, x, y);
1374 if (bv->theLockingInset()) {
1375 // We are in inset locking mode.
1377 // LyX does a kind of work-area grabbing for insets.
1378 // Only a ButtonPress FuncRequest outside the inset will
1379 // force a insetUnlock.
1380 FuncRequest cmd(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1381 bv->theLockingInset()->localDispatch(cmd);
1385 selection_possible = false;
1387 if (cmd.button() == mouse_button::button2)
1391 if (cmd.button() == mouse_button::button1)
1392 bv->workarea().haveSelection(selection.set());
1395 bv->owner()->view_state_changed();
1396 bv->owner()->updateMenubar();
1397 bv->owner()->updateToolbar();
1399 // Did we hit an editable inset?
1401 selection_possible = false;
1403 // if we reach this point with a selection, it
1404 // must mean we are currently selecting.
1405 // But we don't want to open the inset
1406 // because that is annoying for the user.
1407 // So just pretend we didn't hit it.
1408 // this is OK because a "kosher" ButtonRelease
1409 // will follow a ButtonPress that clears
1411 // Note this also fixes selection drawing
1412 // problems if we end up opening an inset
1413 if (selection.set())
1416 // CHECK fix this proper in 0.13
1417 // well, maybe 13.0 !!!!!!!!!
1419 // Following a ref shouldn't issue
1420 // a push on the undo-stack
1421 // anylonger, now that we have
1422 // keybindings for following
1423 // references and returning from
1424 // references. IMHO though, it
1425 // should be the inset's own business
1426 // to push or not push on the undo
1427 // stack. They don't *have* to
1428 // alter the document...
1430 // ...or maybe the SetCursorParUndo()
1431 // below isn't necessary at all anylonger?
1432 if (inset_hit->lyxCode() == Inset::REF_CODE)
1433 setCursorParUndo(bv);
1435 bv->owner()->message(inset_hit->editMessage());
1437 if (isHighlyEditableInset(inset_hit)) {
1438 // Highly editable inset, like math
1439 UpdatableInset * inset = (UpdatableInset *) inset_hit;
1440 FuncRequest cmd(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1441 inset->localDispatch(cmd);
1443 FuncRequest cmd(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1444 inset_hit->localDispatch(cmd);
1445 // IMO this is a grosshack! Inset's should be changed so that
1446 // they call the actions they have to do with the insetButtonRel.
1447 // function and not in the edit(). This should be changed
1449 #ifdef WITH_WARNINGS
1450 #warning Please remove donot call inset->edit() here (Jug 20020812)
1452 inset_hit->edit(bv, x, y, cmd.button());
1457 // Maybe we want to edit a bibitem ale970302
1458 if (bv->text->cursor.par()->bibkey) {
1460 bv->text->cursor.par()->isRightToLeftPar(bv->buffer()->params);
1462 bibitemMaxWidth(bv, bv->buffer()->params.getLyXTextClass().defaultfont());
1463 if ((is_rtl && x > bv->text->workWidth(bv)-20-width) ||
1464 (!is_rtl && x < 20 + width)) {
1465 bv->text->cursor.par()->bibkey->edit(bv, 0, 0, mouse_button::none);
1471 case LFUN_SELFINSERT: {
1472 if (cmd.argument.empty())
1475 // Automatically delete the currently selected
1476 // text and replace it with what is being
1477 // typed in now. Depends on lyxrc settings
1478 // "auto_region_delete", which defaults to
1481 if (lyxrc.auto_region_delete) {
1482 if (selection.set()) {
1483 cutSelection(bv, false, false);
1486 bv->workarea().haveSelection(false);
1489 bv->beforeChange(this);
1490 LyXFont const old_font(real_current_font);
1492 string::const_iterator cit = cmd.argument.begin();
1493 string::const_iterator end = cmd.argument.end();
1494 for (; cit != end; ++cit)
1495 bv->owner()->getIntl().getTransManager().
1496 TranslateAndInsert(*cit, this);
1499 selection.cursor = cursor;
1500 moveCursorUpdate(bv, false);
1502 // real_current_font.number can change so we need to
1503 // update the minibuffer
1504 if (old_font != real_current_font)
1505 bv->owner()->view_state_changed();
1510 return Inset::UNDISPATCHED;
1513 return Inset::DISPATCHED;