]> git.lyx.org Git - lyx.git/blob - src/text3.C
simplify InsetTabular::calculate_dimensions_of_cells a bit
[lyx.git] / src / text3.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2002 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #include "lyxtext.h"
14 #include "lyxrow.h"
15 #include "paragraph.h"
16 #include "BufferView.h"
17 #include "funcrequest.h"
18 #include "lyxrc.h"
19 #include "Lsstream.h"
20 #include "debug.h"
21 #include "bufferparams.h"
22 #include "buffer.h"
23 #include "bufferview_funcs.h"
24 #include "ParagraphParameters.h"
25 #include "gettext.h"
26 #include "factory.h"
27 #include "intl.h"
28 #include "box.h"
29 #include "language.h"
30 #include "support/tostr.h"
31 #include "support/lstrings.h"
32 #include "frontends/LyXView.h"
33 #include "frontends/screen.h"
34 #include "frontends/Dialogs.h"
35 #include "insets/insetspecialchar.h"
36 #include "insets/insettext.h"
37 #include "insets/insetquotes.h"
38 #include "insets/insetcommand.h"
39 #include "insets/insetnewline.h"
40 #include "undo_funcs.h"
41 #include "text_funcs.h"
42 #include "Lsstream.h"
43
44 #include <ctime>
45 #include <clocale>
46
47 using namespace lyx::support;
48 using namespace bv_funcs;
49
50 using std::endl;
51 using std::find;
52 using std::vector;
53 using lyx::pos_type;
54
55 extern string current_layout;
56 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
57
58 // the selection possible is needed, that only motion events are
59 // used, where the bottom press event was on the drawing area too
60 bool selection_possible = false;
61
62
63 namespace {
64
65         void moveCursorUpdate(BufferView * bv, bool selecting)
66         {
67                 LyXText * lt = bv->getLyXText();
68
69                 if (selecting || lt->selection.mark()) {
70                         lt->setSelection();
71                         if (lt->isInInset())
72                                 bv->updateInset(lt->inset_owner);
73                         else
74                                 bv->toggleToggle();
75                 }
76                 if (!lt->isInInset()) {
77                         bv->update(lt, BufferView::SELECT);
78                 } else if (bv->text->needRefresh()) {
79                         bv->update(BufferView::SELECT);
80                 }
81
82                 if (!lt->selection.set())
83                         bv->haveSelection(false);
84
85                 bv->switchKeyMap();
86         }
87
88
89         void finishChange(BufferView * bv, bool selecting = false)
90         {
91                 finishUndo();
92                 moveCursorUpdate(bv, selecting);
93                 bv->owner()->view_state_changed();
94         }
95
96         // check if the given co-ordinates are inside an inset at the
97         // given cursor, if one exists. If so, the inset is returned,
98         // and the co-ordinates are made relative. Otherwise, 0 is returned.
99         Inset * checkInset(BufferView * bv, LyXText & text,
100                 LyXCursor const & cur, int & x, int & y)
101         {
102                 lyx::pos_type const pos = cur.pos();
103                 ParagraphList::iterator par = cur.par();
104
105                 if (pos >= par->size() || !par->isInset(pos))
106                         return 0;
107
108                 Inset /*const*/ * inset = par->getInset(pos);
109
110                 if (!isEditableInset(inset))
111                         return 0;
112
113                 // get inset dimensions
114                 Assert(par->getInset(pos));
115
116                 LyXFont const & font = text.getFont(bv->buffer(), par, pos);
117
118                 int const width = inset->width(bv, font);
119                 int const inset_x = font.isVisibleRightToLeft()
120                         ? (cur.ix() - width) : cur.ix();
121
122                 Box b(
123                         inset_x + inset->scroll(),
124                         inset_x + width,
125                         cur.iy() - inset->ascent(bv, font),
126                         cur.iy() + inset->descent(bv, font)
127                 );
128
129                 if (!b.contained(x, y)) {
130                         lyxerr[Debug::GUI] << "Missed inset at x,y "
131                                            << x << ',' << y
132                                            << " box " << b << endl;
133                         return 0;
134                 }
135
136                 text.setCursor(par, pos, true);
137
138                 x -= b.x1;
139                 // The origin of an inset is on the baseline
140                 y -= text.cursor.iy();
141
142                 return inset;
143         }
144
145 } // anon namespace
146
147
148 Inset * LyXText::checkInsetHit(int & x, int & y)
149 {
150         int y_tmp = y + top_y();
151
152         LyXCursor cur;
153         setCursorFromCoordinates(cur, x, y_tmp);
154
155         Inset * inset = checkInset(bv(), *this, cur, x, y_tmp);
156         if (inset) {
157                 y = y_tmp;
158                 return inset;
159         }
160
161         // look at previous position
162         if (cur.pos() == 0)
163                 return 0;
164
165         // move back one
166         setCursor(cur, cur.par(), cur.pos() - 1, true);
167
168         inset = checkInset(bv(), *this, cur, x, y_tmp);
169         if (inset)
170                 y = y_tmp;
171         return inset;
172 }
173
174
175 bool LyXText::gotoNextInset(vector<Inset::Code> const & codes,
176                             string const & contents)
177 {
178         ParagraphList::iterator end = ownerParagraphs().end();
179         ParagraphList::iterator pit = cursor.par();
180         pos_type pos = cursor.pos();
181
182         Inset * inset;
183         do {
184                 if (pos + 1 < pit->size()) {
185                         ++pos;
186                 } else  {
187                         ++pit;
188                         pos = 0;
189                 }
190
191         } while (pit != end &&
192                  !(pit->isInset(pos) &&
193                    (inset = pit->getInset(pos)) != 0 &&
194                    find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
195                    (contents.empty() ||
196                     static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
197                     == contents)));
198
199         if (pit != end) {
200                 setCursor(pit, pos, false);
201                 return true;
202         }
203         return false;
204 }
205
206
207 void LyXText::gotoInset(vector<Inset::Code> const & codes,
208                         bool same_content)
209 {
210         bv()->beforeChange(this);
211         update();
212
213         string contents;
214         if (same_content && cursor.pos() < cursor.par()->size()
215             && cursor.par()->isInset(cursor.pos())) {
216                 Inset const * inset = cursor.par()->getInset(cursor.pos());
217                 if (find(codes.begin(), codes.end(), inset->lyxCode())
218                     != codes.end())
219                         contents = static_cast<InsetCommand const *>(inset)->getContents();
220         }
221
222         if (!gotoNextInset(codes, contents)) {
223                 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
224                         LyXCursor tmp = cursor;
225                         cursor.par(ownerParagraphs().begin());
226                         cursor.pos(0);
227                         if (!gotoNextInset(codes, contents)) {
228                                 cursor = tmp;
229                                 bv()->owner()->message(_("No more insets"));
230                         }
231                 } else {
232                         bv()->owner()->message(_("No more insets"));
233                 }
234         }
235         update();
236         selection.cursor = cursor;
237 }
238
239
240 void LyXText::gotoInset(Inset::Code code, bool same_content)
241 {
242         gotoInset(vector<Inset::Code>(1, code), same_content);
243 }
244
245
246 void LyXText::cursorPrevious()
247 {
248         int y = top_y();
249
250         if (cursorRow() == rows().begin()) {
251                 if (y > 0) {
252                         int new_y = bv()->text->top_y() - bv()->workHeight();
253                         bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
254                         bv()->updateScrollbar();
255                 }
256                 return;
257         }
258
259         RowList::iterator cursorrow = cursorRow();
260
261         setCursorFromCoordinates(cursor.x_fix(), y);
262         finishUndo();
263
264         int new_y;
265         if (cursorrow == bv()->text->cursorRow()) {
266                 // we have a row which is taller than the workarea. The
267                 // simplest solution is to move to the previous row instead.
268                 cursorUp(true);
269                 return;
270                 // This is what we used to do, so we wouldn't skip right past
271                 // tall rows, but it's not working right now.
272 #if 0
273                 new_y = bv->text->top_y() - bv->workHeight();
274 #endif
275         } else {
276                 if (inset_owner) {
277                         new_y = bv()->text->cursor.iy()
278                                 + bv()->theLockingInset()->insetInInsetY() + y
279                                 + cursorRow()->height()
280                                 - bv()->workHeight() + 1;
281                 } else {
282                         new_y = cursor.y()
283                                 - cursorRow()->baseline()
284                                 + cursorRow()->height()
285                                 - bv()->workHeight() + 1;
286                 }
287         }
288         bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
289         if (cursorRow() != rows().begin()) {
290                 LyXCursor cur;
291                 setCursor(cur, boost::prior(cursorRow())->par(),
292                           boost::prior(cursorRow())->pos(), false);
293                 if (cur.y() > top_y()) {
294                         cursorUp(true);
295                 }
296         }
297         bv()->updateScrollbar();
298 }
299
300
301 void LyXText::cursorNext()
302 {
303         int topy = top_y();
304
305         if (boost::next(cursorRow()) == rows().end()) {
306                 int y = cursor.y() - cursorRow()->baseline() +
307                         cursorRow()->height();
308                 if (y > topy + bv()->workHeight()) {
309                         bv()->screen().draw(bv()->text, bv(), bv()->text->top_y() + bv()->workHeight());
310                         bv()->updateScrollbar();
311                 }
312                 return;
313         }
314
315         int y = topy + bv()->workHeight();
316         if (inset_owner && !topy) {
317                 y -= (bv()->text->cursor.iy()
318                           - bv()->text->top_y()
319                           + bv()->theLockingInset()->insetInInsetY());
320         }
321
322         getRowNearY(y);
323
324         RowList::iterator cursorrow = cursorRow();
325         setCursorFromCoordinates(cursor.x_fix(), y);
326         // + bv->workHeight());
327         finishUndo();
328
329         int new_y;
330         if (cursorrow == bv()->text->cursorRow()) {
331                 // we have a row which is taller than the workarea. The
332                 // simplest solution is to move to the next row instead.
333                 cursorDown(true);
334                 return;
335                 // This is what we used to do, so we wouldn't skip right past
336                 // tall rows, but it's not working right now.
337 #if 0
338                 new_y = bv->text->top_y() + bv->workHeight();
339 #endif
340         } else {
341                 if (inset_owner) {
342                         new_y = bv()->text->cursor.iy()
343                                 + bv()->theLockingInset()->insetInInsetY()
344                                 + y - cursorRow()->baseline();
345                 } else {
346                         new_y =  cursor.y() - cursorRow()->baseline();
347                 }
348         }
349         bv()->screen().draw(bv()->text, bv(), new_y);
350
351         RowList::iterator next_row = boost::next(cursorRow());
352         if (next_row != rows().end()) {
353                 LyXCursor cur;
354                 setCursor(cur, next_row->par(), next_row->pos(), false);
355                 if (cur.y() < top_y() + bv()->workHeight()) {
356                         cursorDown(true);
357                 }
358         }
359         bv()->updateScrollbar();
360 }
361
362
363 void LyXText::update()
364 {
365         bv()->update(this, BufferView::SELECT);
366 }
367
368 namespace {
369
370 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
371 {
372         lt->update();
373         InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
374         replaceSelection(lt);
375         if (!bv->insertInset(new_inset))
376                 delete new_inset;
377         else
378                 bv->updateInset(new_inset);
379 }
380
381
382 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
383                    bool edit, bool pastesel)
384 {
385         Inset * inset = createInset(cmd);
386         BufferView * bv = cmd.view();
387
388         if (inset) {
389                 bool gotsel = false;
390                 if (lt->selection.set()) {
391                         bv->owner()->dispatch(FuncRequest(LFUN_CUT));
392                         gotsel = true;
393                 }
394                 if (bv->insertInset(inset)) {
395                         if (edit) {
396                                 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
397                                 inset->localDispatch(cmd);
398                         }
399                         if (gotsel && pastesel)
400                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
401                 }
402                 else
403                         delete inset;
404         }
405 }
406
407 }
408
409 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
410 {
411         lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
412                               <<"] arg[" << cmd.argument << ']' << endl;
413
414         BufferView * bv = cmd.view();
415
416         switch (cmd.action) {
417
418         case LFUN_APPENDIX: {
419                 ParagraphList::iterator pit = cursor.par();
420                 bool start = !pit->params().startOfAppendix();
421
422                 // ensure that we have only one start_of_appendix in this document
423                 ParagraphList::iterator tmp = ownerParagraphs().begin();
424                 ParagraphList::iterator end = ownerParagraphs().end();
425
426                 for (; tmp != end; ++tmp) {
427                         if (tmp->params().startOfAppendix()) {
428                                 setUndo(bv, Undo::EDIT, tmp);
429                                 tmp->params().startOfAppendix(false);
430                                 int tmpy;
431                                 setHeightOfRow(getRow(tmp, 0, tmpy));
432                                 break;
433                         }
434                 }
435
436                 setUndo(bv, Undo::EDIT, pit);
437                 pit->params().startOfAppendix(start);
438
439                 // we can set the refreshing parameters now
440                 updateCounters();
441                 redoHeightOfParagraph();
442                 postPaint();
443                 setCursor(cursor.par(), cursor.pos());
444                 update();
445                 break;
446         }
447
448         case LFUN_DELETE_WORD_FORWARD:
449                 bv->beforeChange(this);
450                 update();
451                 deleteWordForward();
452                 update();
453                 finishChange(bv);
454                 break;
455
456         case LFUN_DELETE_WORD_BACKWARD:
457                 bv->beforeChange(this);
458                 update();
459                 deleteWordBackward();
460                 update();
461                 finishChange(bv);
462                 break;
463
464         case LFUN_DELETE_LINE_FORWARD:
465                 bv->beforeChange(this);
466                 update();
467                 deleteLineForward();
468                 update();
469                 finishChange(bv);
470                 break;
471
472         case LFUN_WORDRIGHT:
473                 if (!selection.mark())
474                         bv->beforeChange(this);
475                 update();
476                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
477                         cursorLeftOneWord();
478                 else
479                         cursorRightOneWord();
480                 finishChange(bv);
481                 break;
482
483         case LFUN_WORDLEFT:
484                 if (!selection.mark())
485                         bv->beforeChange(this);
486                 update();
487                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
488                         cursorRightOneWord();
489                 else
490                         cursorLeftOneWord();
491                 finishChange(bv);
492                 break;
493
494         case LFUN_BEGINNINGBUF:
495                 if (!selection.mark())
496                         bv->beforeChange(this);
497                 update();
498                 cursorTop();
499                 finishChange(bv);
500                 break;
501
502         case LFUN_ENDBUF:
503                 if (selection.mark())
504                         bv->beforeChange(this);
505                 update();
506                 cursorBottom();
507                 finishChange(bv);
508                 break;
509
510         case LFUN_RIGHTSEL:
511                 update();
512                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
513                         cursorLeft(bv);
514                 else
515                         cursorRight(bv);
516                 finishChange(bv, true);
517                 break;
518
519         case LFUN_LEFTSEL:
520                 update();
521                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
522                         cursorRight(bv);
523                 else
524                         cursorLeft(bv);
525                 finishChange(bv, true);
526                 break;
527
528         case LFUN_UPSEL:
529                 update();
530                 cursorUp(true);
531                 finishChange(bv, true);
532                 break;
533
534         case LFUN_DOWNSEL:
535                 update();
536                 cursorDown(true);
537                 finishChange(bv, true);
538                 break;
539
540         case LFUN_UP_PARAGRAPHSEL:
541                 update();
542                 cursorUpParagraph();
543                 finishChange(bv, true);
544                 break;
545
546         case LFUN_DOWN_PARAGRAPHSEL:
547                 update();
548                 cursorDownParagraph();
549                 finishChange(bv, true);
550                 break;
551
552         case LFUN_PRIORSEL:
553                 update();
554                 cursorPrevious();
555                 finishChange(bv, true);
556                 break;
557
558         case LFUN_NEXTSEL:
559                 update();
560                 cursorNext();
561                 finishChange(bv, true);
562                 break;
563
564         case LFUN_HOMESEL:
565                 update();
566                 cursorHome();
567                 finishChange(bv, true);
568                 break;
569
570         case LFUN_ENDSEL:
571                 update();
572                 cursorEnd();
573                 finishChange(bv, true);
574                 break;
575
576         case LFUN_WORDRIGHTSEL:
577                 update();
578                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
579                         cursorLeftOneWord();
580                 else
581                         cursorRightOneWord();
582                 finishChange(bv, true);
583                 break;
584
585         case LFUN_WORDLEFTSEL:
586                 update();
587                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
588                         cursorRightOneWord();
589                 else
590                         cursorLeftOneWord();
591                 finishChange(bv, true);
592                 break;
593
594         case LFUN_WORDSEL: {
595                 update();
596                 LyXCursor cur1 = cursor;
597                 LyXCursor cur2;
598                 ::getWord(cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs());
599                 setCursor(cur1.par(), cur1.pos());
600                 bv->beforeChange(this);
601                 setCursor(cur2.par(), cur2.pos());
602                 finishChange(bv, true);
603                 break;
604         }
605
606         case LFUN_RIGHT: {
607                 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
608                 if (!selection.mark())
609                         bv->beforeChange(this);
610                 update();
611                 if (is_rtl)
612                         cursorLeft(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                         FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
619                         tmpinset->localDispatch(cmd1);
620                         break;
621                 }
622                 if (!is_rtl)
623                         cursorRight(false);
624                 finishChange(bv);
625                 break;
626         }
627
628         case LFUN_LEFT: {
629                 // This is soooo ugly. Isn`t it possible to make
630                 // it simpler? (Lgb)
631                 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
632                 if (!selection.mark())
633                         bv->beforeChange(this);
634                 update();
635                 LyXCursor const cur = cursor;
636                 if (!is_rtl)
637                         cursorLeft(false);
638                 if ((is_rtl || cur != cursor) && // only if really moved!
639                     cursor.pos() < cursor.par()->size() &&
640                     cursor.par()->isInset(cursor.pos()) &&
641                     isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
642                         Inset * tmpinset = cursor.par()->getInset(cursor.pos());
643                         cmd.message(tmpinset->editMessage());
644                         FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
645                         tmpinset->localDispatch(cmd1);
646                         break;
647                 }
648                 if (is_rtl)
649                         cursorRight(false);
650                 finishChange(bv);
651                 break;
652         }
653
654         case LFUN_UP:
655                 if (!selection.mark())
656                         bv->beforeChange(this);
657                 bv->update(this, BufferView::UPDATE);
658                 cursorUp(false);
659                 finishChange(bv);
660                 break;
661
662         case LFUN_DOWN:
663                 if (!selection.mark())
664                         bv->beforeChange(this);
665                 bv->update(this, BufferView::UPDATE);
666                 cursorDown(false);
667                 finishChange(bv);
668                 break;
669
670         case LFUN_UP_PARAGRAPH:
671                 if (!selection.mark())
672                         bv->beforeChange(this);
673                 bv->update(this, BufferView::UPDATE);
674                 cursorUpParagraph();
675                 finishChange(bv);
676                 break;
677
678         case LFUN_DOWN_PARAGRAPH:
679                 if (!selection.mark())
680                         bv->beforeChange(this);
681                 bv->update(this, BufferView::UPDATE);
682                 cursorDownParagraph();
683                 finishChange(bv, false);
684                 break;
685
686         case LFUN_PRIOR:
687                 if (!selection.mark())
688                         bv->beforeChange(this);
689                 bv->update(this, BufferView::UPDATE);
690                 cursorPrevious();
691                 finishChange(bv, false);
692                 // was:
693                 // finishUndo();
694                 // moveCursorUpdate(bv, false, false);
695                 // owner_->view_state_changed();
696                 break;
697
698         case LFUN_NEXT:
699                 if (!selection.mark())
700                         bv->beforeChange(this);
701                 bv->update(this, BufferView::UPDATE);
702                 cursorNext();
703                 finishChange(bv, false);
704                 break;
705
706         case LFUN_HOME:
707                 if (!selection.mark())
708                         bv->beforeChange(this);
709                 update();
710                 cursorHome();
711                 finishChange(bv, false);
712                 break;
713
714         case LFUN_END:
715                 if (!selection.mark())
716                         bv->beforeChange(this);
717                 update();
718                 cursorEnd();
719                 finishChange(bv, false);
720                 break;
721
722         case LFUN_BREAKLINE: {
723                 lyx::pos_type body = cursor.par()->beginningOfBody();
724
725                 // Not allowed by LaTeX (labels or empty par)
726                 if (cursor.pos() <= body)
727                         break;
728
729                 replaceSelection(bv->getLyXText());
730                 insertInset(new InsetNewline);
731                 update();
732                 setCursor(cursor.par(), cursor.pos());
733                 moveCursorUpdate(bv, false);
734                 break;
735         }
736
737         case LFUN_DELETE:
738                 if (!selection.set()) {
739                         Delete();
740                         selection.cursor = cursor;
741                         update();
742                         // It is possible to make it a lot faster still
743                         // just comment out the line below...
744                 } else {
745                         update();
746                         cutSelection(true, false);
747                         update();
748                 }
749                 moveCursorUpdate(bv, false);
750                 bv->owner()->view_state_changed();
751                 bv->switchKeyMap();
752                 break;
753
754         case LFUN_DELETE_SKIP:
755                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
756                 if (!selection.set()) {
757                         LyXCursor cur = cursor;
758                         if (cur.pos() == cur.par()->size()) {
759                                 cursorRight(bv);
760                                 cur = cursor;
761                                 if (cur.pos() == 0
762                                     && !(cur.par()->params().spaceTop()
763                                          == VSpace (VSpace::NONE))) {
764                                         setParagraph(
765                                                  cur.par()->params().lineTop(),
766                                                  cur.par()->params().lineBottom(),
767                                                  cur.par()->params().pagebreakTop(),
768                                                  cur.par()->params().pagebreakBottom(),
769                                                  VSpace(VSpace::NONE),
770                                                  cur.par()->params().spaceBottom(),
771                                                  cur.par()->params().spacing(),
772                                                  cur.par()->params().align(),
773                                                  cur.par()->params().labelWidthString(), 0);
774                                         cursorLeft(bv);
775                                         update();
776                                 } else {
777                                         cursorLeft(bv);
778                                         Delete();
779                                         selection.cursor = cursor;
780                                 }
781                         } else {
782                                 Delete();
783                                 selection.cursor = cursor;
784                         }
785                 } else {
786                         update();
787                         cutSelection(true, false);
788                 }
789                 update();
790                 break;
791
792
793         case LFUN_BACKSPACE:
794                 if (!selection.set()) {
795                         if (bv->owner()->getIntl().getTransManager().backspace()) {
796                                 backspace();
797                                 selection.cursor = cursor;
798                                 update();
799                                 // It is possible to make it a lot faster still
800                                 // just comment out the line below...
801                         }
802                 } else {
803                         update();
804                         cutSelection(true, false);
805                         update();
806                 }
807                 bv->owner()->view_state_changed();
808                 bv->switchKeyMap();
809                 break;
810
811         case LFUN_BACKSPACE_SKIP:
812                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
813                 if (!selection.set()) {
814                         LyXCursor cur = cursor;
815                         if (cur.pos() == 0
816                             && !(cur.par()->params().spaceTop()
817                                  == VSpace (VSpace::NONE))) {
818                                 setParagraph(
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);
827                         } else {
828                                 backspace();
829                                 selection.cursor = cur;
830                         }
831                 } else {
832                         update();
833                         cutSelection(true, false);
834                 }
835                 update();
836                 break;
837
838         case LFUN_BREAKPARAGRAPH:
839                 replaceSelection(bv->getLyXText());
840                 breakParagraph(bv->buffer()->paragraphs, 0);
841                 update();
842                 selection.cursor = cursor;
843                 bv->switchKeyMap();
844                 bv->owner()->view_state_changed();
845                 break;
846
847         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
848                 replaceSelection(bv->getLyXText());
849                 breakParagraph(bv->buffer()->paragraphs, 1);
850                 update();
851                 selection.cursor = cursor;
852                 bv->switchKeyMap();
853                 bv->owner()->view_state_changed();
854                 break;
855
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                 replaceSelection(bv->getLyXText());
862                 if (cur.pos() == 0) {
863                         if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
864                                 setParagraph(
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);
873                         }
874                 }
875                 else {
876                         breakParagraph(bv->buffer()->paragraphs, 0);
877                 }
878                 update();
879                 selection.cursor = cur;
880                 bv->switchKeyMap();
881                 bv->owner()->view_state_changed();
882                 break;
883         }
884
885         case LFUN_PARAGRAPH_SPACING: {
886                 ParagraphList::iterator pit = cursor.par();
887                 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
888                 float cur_value = 1.0;
889                 if (cur_spacing == Spacing::Other)
890                         cur_value = pit->params().spacing().getValue();
891
892                 istringstream is(STRCONV(cmd.argument));
893                 string tmp;
894                 is >> tmp;
895                 Spacing::Space new_spacing = cur_spacing;
896                 float new_value = cur_value;
897                 if (tmp.empty()) {
898                         lyxerr << "Missing argument to `paragraph-spacing'"
899                                << endl;
900                 } else if (tmp == "single") {
901                         new_spacing = Spacing::Single;
902                 } else if (tmp == "onehalf") {
903                         new_spacing = Spacing::Onehalf;
904                 } else if (tmp == "double") {
905                         new_spacing = Spacing::Double;
906                 } else if (tmp == "other") {
907                         new_spacing = Spacing::Other;
908                         float tmpval = 0.0;
909                         is >> tmpval;
910                         lyxerr << "new_value = " << tmpval << endl;
911                         if (tmpval != 0.0)
912                                 new_value = tmpval;
913                 } else if (tmp == "default") {
914                         new_spacing = Spacing::Default;
915                 } else {
916                         lyxerr << _("Unknown spacing argument: ")
917                                << cmd.argument << endl;
918                 }
919                 if (cur_spacing != new_spacing || cur_value != new_value) {
920                         pit->params().spacing(Spacing(new_spacing, new_value));
921                         redoParagraph();
922                         update();
923                 }
924                 break;
925         }
926
927         case LFUN_INSET_SETTINGS:
928                 Assert(bv->theLockingInset());
929                 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
930                 break;
931
932         case LFUN_INSET_TOGGLE:
933                 bv->beforeChange(this);
934                 update();
935                 toggleInset();
936                 update();
937                 bv->switchKeyMap();
938                 break;
939
940         case LFUN_SPACE_INSERT:
941                 if (cursor.par()->layout()->free_spacing) {
942                         insertChar(' ');
943                         update();
944                 } else {
945                         doInsertInset(this, cmd, false, false);
946                 }
947                 moveCursorUpdate(bv, false);
948                 break;
949
950         case LFUN_HYPHENATION:
951                 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
952                 break;
953
954         case LFUN_LIGATURE_BREAK:
955                 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
956                 break;
957
958         case LFUN_LDOTS:
959                 specialChar(this, bv, InsetSpecialChar::LDOTS);
960                 break;
961
962         case LFUN_END_OF_SENTENCE:
963                 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
964                 break;
965
966         case LFUN_MENU_SEPARATOR:
967                 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
968                 break;
969
970         case LFUN_MARK_OFF:
971                 bv->beforeChange(this);
972                 update();
973                 selection.cursor = cursor;
974                 cmd.message(N_("Mark off"));
975                 break;
976
977         case LFUN_MARK_ON:
978                 bv->beforeChange(this);
979                 selection.mark(true);
980                 update();
981                 selection.cursor = cursor;
982                 cmd.message(N_("Mark on"));
983                 break;
984
985         case LFUN_SETMARK:
986                 bv->beforeChange(this);
987                 if (selection.mark()) {
988                         update();
989                         cmd.message(N_("Mark removed"));
990                 } else {
991                         selection.mark(true);
992                         update();
993                         cmd.message(N_("Mark set"));
994                 }
995                 selection.cursor = cursor;
996                 break;
997
998         case LFUN_UPCASE_WORD:
999                 update();
1000                 changeCase(LyXText::text_uppercase);
1001                 if (inset_owner)
1002                         bv->updateInset(inset_owner);
1003                 update();
1004                 break;
1005
1006         case LFUN_LOWCASE_WORD:
1007                 update();
1008                 changeCase(LyXText::text_lowercase);
1009                 if (inset_owner)
1010                         bv->updateInset(inset_owner);
1011                 update();
1012                 break;
1013
1014         case LFUN_CAPITALIZE_WORD:
1015                 update();
1016                 changeCase(LyXText::text_capitalization);
1017                 if (inset_owner)
1018                         bv->updateInset(inset_owner);
1019                 update();
1020                 break;
1021
1022         case LFUN_TRANSPOSE_CHARS:
1023                 update();
1024                 setUndo(bv, Undo::FINISH, cursor.par());
1025                 if (transposeChars(cursor))
1026                         checkParagraph(cursor.par(), cursor.pos());
1027                 if (inset_owner)
1028                         bv->updateInset(inset_owner);
1029                 update();
1030                 break;
1031
1032         case LFUN_PASTE: {
1033                 cmd.message(_("Paste"));
1034                 replaceSelection(bv->getLyXText());
1035                 size_t sel_index = 0;
1036                 string const & arg = cmd.argument;
1037                 if (isStrUnsignedInt(arg)) {
1038                         size_t const paste_arg = strToUnsignedInt(arg);
1039 #warning FIXME Check if the arg is in the domain of available selections.
1040                         sel_index = paste_arg;
1041                 }
1042                 pasteSelection(sel_index);
1043                 clearSelection(); // bug 393
1044                 update();
1045                 bv->switchKeyMap();
1046                 break;
1047         }
1048
1049         case LFUN_CUT:
1050                 update();
1051                 cutSelection(true, true);
1052                 update();
1053                 cmd.message(_("Cut"));
1054                 break;
1055
1056         case LFUN_COPY:
1057                 copySelection();
1058                 cmd.message(_("Copy"));
1059                 break;
1060
1061         case LFUN_BEGINNINGBUFSEL:
1062                 if (inset_owner)
1063                         return UNDISPATCHED;
1064                 update();
1065                 cursorTop();
1066                 finishChange(bv, true);
1067                 break;
1068
1069         case LFUN_ENDBUFSEL:
1070                 if (inset_owner)
1071                         return UNDISPATCHED;
1072                 update();
1073                 cursorBottom();
1074                 finishChange(bv, true);
1075                 break;
1076
1077         case LFUN_GETXY:
1078                 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1079                 break;
1080
1081         case LFUN_SETXY: {
1082                 int x = 0;
1083                 int y = 0;
1084                 istringstream is(STRCONV(cmd.argument));
1085                 is >> x >> y;
1086                 if (!is)
1087                         lyxerr << "SETXY: Could not parse coordinates in '"
1088                                << cmd.argument << std::endl;
1089                 else
1090                         setCursorFromCoordinates(x, y);
1091                 break;
1092         }
1093
1094         case LFUN_GETFONT:
1095                 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1096                         cmd.message("E");
1097                 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1098                         cmd.message("N");
1099                 else
1100                         cmd.message("0");
1101                 break;
1102
1103         case LFUN_GETLAYOUT:
1104                 cmd.message(tostr(cursor.par()->layout()));
1105                 break;
1106
1107         case LFUN_LAYOUT: {
1108                 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1109                   << cmd.argument << endl;
1110
1111                 // This is not the good solution to the empty argument
1112                 // problem, but it will hopefully suffice for 1.2.0.
1113                 // The correct solution would be to augument the
1114                 // function list/array with information about what
1115                 // functions needs arguments and their type.
1116                 if (cmd.argument.empty()) {
1117                         cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1118                         break;
1119                 }
1120
1121                 // Derive layout number from given argument (string)
1122                 // and current buffer's textclass (number)
1123                 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1124                 bool hasLayout = tclass.hasLayout(cmd.argument);
1125                 string layout = cmd.argument;
1126
1127                 // If the entry is obsolete, use the new one instead.
1128                 if (hasLayout) {
1129                         string const & obs = tclass[layout]->obsoleted_by();
1130                         if (!obs.empty())
1131                                 layout = obs;
1132                 }
1133
1134                 if (!hasLayout) {
1135                         cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1136                                 N_(" not known"));
1137                         break;
1138                 }
1139
1140                 bool change_layout = (current_layout != layout);
1141
1142                 if (!change_layout && selection.set() &&
1143                         selection.start.par() != selection.end.par())
1144                 {
1145                         ParagraphList::iterator spit = selection.start.par();
1146                         ParagraphList::iterator epit = boost::next(selection.end.par());
1147                         while (spit != epit) {
1148                                 if (spit->layout()->name() != current_layout) {
1149                                         change_layout = true;
1150                                         break;
1151                                 }
1152                                 ++spit;
1153                         }
1154                 }
1155
1156                 if (change_layout) {
1157                         current_layout = layout;
1158                         update();
1159                         setLayout(layout);
1160                         bv->owner()->setLayout(layout);
1161                         update();
1162                         bv->switchKeyMap();
1163                 }
1164                 break;
1165         }
1166
1167         case LFUN_PASTESELECTION: {
1168                 if (!bv->buffer())
1169                         break;
1170                 // this was originally a beforeChange(bv->text), i.e
1171                 // the outermost LyXText!
1172                 bv->beforeChange(this);
1173                 string const clip = bv->getClipboard();
1174                 if (!clip.empty()) {
1175                         if (cmd.argument == "paragraph")
1176                                 insertStringAsParagraphs(clip);
1177                         else
1178                                 insertStringAsLines(clip);
1179                         clearSelection();
1180                         update();
1181                 }
1182                 break;
1183         }
1184
1185         case LFUN_GOTOERROR:
1186                 gotoInset(Inset::ERROR_CODE, false);
1187                 break;
1188
1189         case LFUN_GOTONOTE:
1190                 gotoInset(Inset::NOTE_CODE, false);
1191                 break;
1192
1193         case LFUN_REFERENCE_GOTO:
1194         {
1195                 vector<Inset::Code> tmp;
1196                 tmp.push_back(Inset::LABEL_CODE);
1197                 tmp.push_back(Inset::REF_CODE);
1198                 gotoInset(tmp, true);
1199                 break;
1200         }
1201
1202         case LFUN_QUOTE: {
1203                 replaceSelection(bv->getLyXText());
1204                 ParagraphList::iterator pit = cursor.par();
1205                 lyx::pos_type pos = cursor.pos();
1206                 char c;
1207                 if (!pos)
1208                         c = ' ';
1209                 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1210                         c = ' ';
1211                 else
1212                         c = pit->getChar(pos - 1);
1213
1214                 LyXLayout_ptr const & style = pit->layout();
1215
1216                 if (style->pass_thru ||
1217                                 pit->getFontSettings(bv->buffer()->params,
1218                                          pos).language()->lang() == "hebrew" ||
1219                         (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1220                         bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1221                 break;
1222         }
1223
1224         case LFUN_DATE_INSERT: {
1225                 replaceSelection(bv->getLyXText());
1226                 time_t now_time_t = time(NULL);
1227                 struct tm * now_tm = localtime(&now_time_t);
1228                 setlocale(LC_TIME, "");
1229                 string arg;
1230                 if (!cmd.argument.empty())
1231                         arg = cmd.argument;
1232                 else
1233                         arg = lyxrc.date_insert_format;
1234                 char datetmp[32];
1235                 int const datetmp_len =
1236                         ::strftime(datetmp, 32, arg.c_str(), now_tm);
1237
1238                 for (int i = 0; i < datetmp_len; i++) {
1239                         insertChar(datetmp[i]);
1240                         update();
1241                 }
1242                 selection.cursor = cursor;
1243                 moveCursorUpdate(bv, false);
1244                 break;
1245         }
1246
1247         case LFUN_MOUSE_TRIPLE:
1248                 if (!bv->buffer())
1249                         break;
1250                 if (!isInInset() && bv->theLockingInset())
1251                         break;
1252                 if (cmd.button() == mouse_button::button1) {
1253                         if (!isInInset()) {
1254                                 bv->screen().toggleSelection(this, bv);
1255                         }
1256                         cursorHome();
1257                         selection.cursor = cursor;
1258                         cursorEnd();
1259                         setSelection();
1260                         if (!isInInset())
1261                                 bv->screen().toggleSelection(this, bv, false);
1262                         update();
1263                         bv->haveSelection(selection.set());
1264                 }
1265                 break;
1266
1267         case LFUN_MOUSE_DOUBLE:
1268                 if (!bv->buffer())
1269                         break;
1270                 if (!isInInset() && bv->theLockingInset())
1271                         break;
1272                 if (cmd.button() == mouse_button::button1) {
1273                         if (!isInInset()) {
1274                                 bv->screen().toggleSelection(this, bv);
1275                                 selectWord(lyx::WHOLE_WORD_STRICT);
1276                                 bv->screen().toggleSelection(this, bv, false);
1277                         } else {
1278                                 selectWord(lyx::WHOLE_WORD_STRICT);
1279                         }
1280                         update();
1281                         bv->haveSelection(selection.set());
1282                 }
1283                 break;
1284
1285         case LFUN_MOUSE_MOTION:
1286         {
1287                 // Only use motion with button 1
1288                 //if (ev.button() != mouse_button::button1)
1289                 //      return false;
1290
1291                 if (!bv->buffer())
1292                         break;
1293
1294                 // Check for inset locking
1295                 if (bv->theLockingInset()) {
1296                         Inset * tli = bv->theLockingInset();
1297                         LyXCursor cursor = bv->text->cursor;
1298                         LyXFont font = bv->text->getFont(bv->buffer(),
1299                                                          cursor.par(), cursor.pos());
1300                         int width = tli->width(bv, font);
1301                         int inset_x = font.isVisibleRightToLeft()
1302                                 ? cursor.ix() - width : cursor.ix();
1303                         int start_x = inset_x + tli->scroll();
1304                         FuncRequest cmd1 = cmd;
1305                         cmd1.x = cmd.x - start_x;
1306                         cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1307                         tli->localDispatch(cmd1);
1308                         break;
1309                 }
1310
1311                 // The test for not selection possible is needed, that only motion
1312                 // events are used, where the bottom press event was on
1313                 //  the drawing area too
1314                 if (!selection_possible) {
1315                         lyxerr[Debug::ACTION]
1316                                 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1317                         break;
1318                 }
1319
1320                 RowList::iterator cursorrow = bv->text->cursorRow();
1321                 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->text->top_y());
1322         #if 0
1323                 // sorry for this but I have a strange error that the y value jumps at
1324                 // a certain point. This seems like an error in my xforms library or
1325                 // in some other local environment, but I would like to leave this here
1326                 // for the moment until I can remove this (Jug 20020418)
1327                 if (y_before < bv->text->cursor.y())
1328                         lyxerr << y_before << ':'
1329                                << bv->text->cursor.y() << endl;
1330         #endif
1331                 // This is to allow jumping over large insets
1332                 if (cursorrow == bv->text->cursorRow()) {
1333                         if (cmd.y >= bv->workHeight())
1334                                 bv->text->cursorDown(false);
1335                         else if (cmd.y < 0)
1336                                 bv->text->cursorUp(false);
1337                 }
1338
1339                 // Maybe an empty line was deleted
1340                 if (!bv->text->selection.set())
1341                         bv->update(BufferView::UPDATE);
1342                 bv->text->setSelection();
1343                 bv->screen().toggleToggle(bv->text, bv);
1344                 bv->fitCursor();
1345                 break;
1346         }
1347
1348         // Single-click on work area
1349         case LFUN_MOUSE_PRESS:
1350         {
1351                 if (!bv->buffer())
1352                         break;
1353
1354                 // ok ok, this is a hack (for xforms)
1355                 // We shouldn't go further down as we really should only do the
1356                 // scrolling and be done with this. Otherwise we may open some
1357                 // dialogs (Jug 20020424).
1358                 if (cmd.button() == mouse_button::button4) {
1359                         bv->scroll(-lyxrc.wheel_jump);
1360                         break;
1361                 }
1362                 if (cmd.button() == mouse_button::button5) {
1363                         bv->scroll(lyxrc.wheel_jump);
1364                         break;
1365                 }
1366
1367                 int x = cmd.x;
1368                 int y = cmd.y;
1369                 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1370
1371                 // Middle button press pastes if we have a selection
1372                 // We do this here as if the selection was inside an inset
1373                 // it could get cleared on the unlocking of the inset so
1374                 // we have to check this first
1375                 bool paste_internally = false;
1376                 if (cmd.button() == mouse_button::button2 && selection.set()) {
1377                         bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1378                         paste_internally = true;
1379                 }
1380
1381                 int const screen_first = bv->text->top_y();
1382
1383                 if (bv->theLockingInset()) {
1384                         // We are in inset locking mode
1385
1386                         // Check whether the inset was hit. If not reset mode,
1387                         // otherwise give the event to the inset
1388                         if (inset_hit == bv->theLockingInset()) {
1389                                 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1390                                 bv->theLockingInset()->localDispatch(cmd1);
1391                                 break;
1392                         }
1393                         bv->unlockInset(bv->theLockingInset());
1394                 }
1395
1396                 if (!inset_hit)
1397                         selection_possible = true;
1398
1399                 // Clear the selection
1400                 bv->screen().toggleSelection(bv->text, bv);
1401                 bv->text->clearSelection();
1402                 bv->text->partialRebreak();
1403                 bv->update();
1404                 bv->updateScrollbar();
1405
1406                 // Single left click in math inset?
1407                 if (isHighlyEditableInset(inset_hit)) {
1408                         // Highly editable inset, like math
1409                         UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1410                         selection_possible = false;
1411                         bv->owner()->message(inset->editMessage());
1412                         // We just have to lock the inset before calling a PressEvent on it!
1413                         if (!bv->lockInset(inset))
1414                                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1415                         FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1416                         inset->localDispatch(cmd1);
1417                         break;
1418                 }
1419                 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1420
1421                 // Right click on a footnote flag opens float menu
1422                 if (cmd.button() == mouse_button::button3) {
1423                         selection_possible = false;
1424                         break;
1425                 }
1426
1427                 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1428                         bv->text->setCursorFromCoordinates(x, y + screen_first);
1429                 finishUndo();
1430                 bv->text->selection.cursor = bv->text->cursor;
1431                 bv->text->cursor.x_fix(bv->text->cursor.x());
1432
1433                 if (bv->fitCursor())
1434                         selection_possible = false;
1435
1436                 // Insert primary selection with middle mouse
1437                 // if there is a local selection in the current buffer,
1438                 // insert this
1439                 if (cmd.button() == mouse_button::button2) {
1440                         if (paste_internally)
1441                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1442                         else
1443                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1444                         selection_possible = false;
1445                 }
1446                 break;
1447         }
1448
1449         case LFUN_MOUSE_RELEASE:
1450         {
1451                 // do nothing if we used the mouse wheel
1452                 if (!bv->buffer())
1453                         break;
1454
1455                 if (cmd.button() == mouse_button::button4
1456                  || cmd.button() == mouse_button::button5)
1457                         break;
1458
1459                 // If we hit an inset, we have the inset coordinates in these
1460                 // and inset_hit points to the inset.  If we do not hit an
1461                 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1462                 int x = cmd.x;
1463                 int y = cmd.y;
1464                 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1465
1466                 if (bv->theLockingInset()) {
1467                         // We are in inset locking mode.
1468
1469                         // LyX does a kind of work-area grabbing for insets.
1470                         // Only a ButtonPress FuncRequest outside the inset will
1471                         // force a insetUnlock.
1472                         FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1473                         bv->theLockingInset()->localDispatch(cmd1);
1474                         break;
1475                 }
1476
1477                 selection_possible = false;
1478
1479                 if (cmd.button() == mouse_button::button2)
1480                         break;
1481
1482                 // finish selection
1483                 if (cmd.button() == mouse_button::button1)
1484                         bv->haveSelection(selection.set());
1485
1486                 bv->switchKeyMap();
1487                 bv->owner()->view_state_changed();
1488                 bv->owner()->updateMenubar();
1489                 bv->owner()->updateToolbar();
1490
1491                 // Did we hit an editable inset?
1492                 if (inset_hit) {
1493                         selection_possible = false;
1494
1495                         // if we reach this point with a selection, it
1496                         // must mean we are currently selecting.
1497                         // But we don't want to open the inset
1498                         // because that is annoying for the user.
1499                         // So just pretend we didn't hit it.
1500                         // this is OK because a "kosher" ButtonRelease
1501                         // will follow a ButtonPress that clears
1502                         // the selection.
1503                         // Note this also fixes selection drawing
1504                         // problems if we end up opening an inset
1505                         if (selection.set())
1506                                 break;
1507
1508                         // CHECK fix this proper in 0.13
1509                         // well, maybe 13.0 !!!!!!!!!
1510
1511                         // Following a ref shouldn't issue
1512                         // a push on the undo-stack
1513                         // anylonger, now that we have
1514                         // keybindings for following
1515                         // references and returning from
1516                         // references.  IMHO though, it
1517                         // should be the inset's own business
1518                         // to push or not push on the undo
1519                         // stack. They don't *have* to
1520                         // alter the document...
1521                         // (Joacim)
1522                         // ...or maybe the SetCursorParUndo()
1523                         // below isn't necessary at all anylonger?
1524                         if (inset_hit->lyxCode() == Inset::REF_CODE)
1525                                 setCursorParUndo(bv);
1526
1527                         bv->owner()->message(inset_hit->editMessage());
1528
1529                         FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1530                         inset_hit->localDispatch(cmd1);
1531                 }
1532
1533                 break;
1534         }
1535
1536         case LFUN_SELFINSERT: {
1537                 if (cmd.argument.empty())
1538                         break;
1539
1540                 // Automatically delete the currently selected
1541                 // text and replace it with what is being
1542                 // typed in now. Depends on lyxrc settings
1543                 // "auto_region_delete", which defaults to
1544                 // true (on).
1545
1546                 if (lyxrc.auto_region_delete) {
1547                         if (selection.set()) {
1548                                 cutSelection(false, false);
1549                                 update();
1550                         }
1551                         bv->haveSelection(false);
1552                 }
1553
1554                 bv->beforeChange(this);
1555                 LyXFont const old_font(real_current_font);
1556
1557                 string::const_iterator cit = cmd.argument.begin();
1558                 string::const_iterator end = cmd.argument.end();
1559                 for (; cit != end; ++cit)
1560                         bv->owner()->getIntl().getTransManager().
1561                                 TranslateAndInsert(*cit, this);
1562
1563                 update();
1564                 selection.cursor = cursor;
1565                 moveCursorUpdate(bv, false);
1566
1567                 // real_current_font.number can change so we need to
1568                 // update the minibuffer
1569                 if (old_font != real_current_font)
1570                         bv->owner()->view_state_changed();
1571                 break;
1572         }
1573
1574         case LFUN_HTMLURL: {
1575                 InsetCommandParams p("htmlurl");
1576                 string const data = InsetCommandMailer::params2string("url", p);
1577                 bv->owner()->getDialogs().show("url", data, 0);
1578                 break;
1579         }
1580
1581         case LFUN_URL: {
1582                 InsetCommandParams p("url");
1583                 string const data = InsetCommandMailer::params2string("url", p);
1584                 bv->owner()->getDialogs().show("url", data, 0);
1585                 break;
1586         }
1587
1588
1589 #if 0
1590         case LFUN_INSET_LIST:
1591         case LFUN_INSET_THEOREM:
1592         case LFUN_INSET_CAPTION:
1593 #endif
1594         case LFUN_INSERT_NOTE:
1595         case LFUN_INSERT_BIBITEM:
1596         case LFUN_INSET_ERT:
1597         case LFUN_INSET_FLOAT:
1598         case LFUN_INSET_FOOTNOTE:
1599         case LFUN_INSET_MARGINAL:
1600         case LFUN_INSET_MINIPAGE:
1601         case LFUN_INSET_OPTARG:
1602         case LFUN_INSET_WIDE_FLOAT:
1603         case LFUN_INSET_WRAP:
1604         case LFUN_TABULAR_INSERT:
1605         case LFUN_ENVIRONMENT_INSERT:
1606                 // Open the inset, and move the current selection
1607                 // inside it.
1608                 doInsertInset(this, cmd, true, true);
1609                 break;
1610
1611         case LFUN_INDEX_INSERT:
1612                 // Just open the inset
1613                 doInsertInset(this, cmd, true, false);
1614                 break;
1615
1616         case LFUN_INDEX_PRINT:
1617         case LFUN_TOC_INSERT:
1618         case LFUN_HFILL:
1619                 // do nothing fancy
1620                 doInsertInset(this, cmd, false, false);
1621                 break;
1622
1623         default:
1624                 return UNDISPATCHED;
1625         }
1626
1627         return DISPATCHED;
1628 }