]> git.lyx.org Git - lyx.git/blob - src/text3.C
move mouse click handling to LyXText::dispatch
[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 "debug.h"
20 #include "bufferparams.h"
21 #include "buffer.h"
22 #include "ParagraphParameters.h"
23 #include "gettext.h"
24 #include "factory.h"
25 #include "intl.h"
26 #include "language.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"
37
38 #include <ctime>
39 #include <clocale>
40
41 using std::endl;
42
43 extern string current_layout;
44 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
45
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;
49
50
51 namespace {
52
53         void moveCursorUpdate(BufferView * bv, bool selecting)
54         {
55                 LyXText * lt = bv->getLyXText();
56
57                 if (selecting || lt->selection.mark()) {
58                         lt->setSelection(bv);
59                         if (lt->bv_owner)
60                                 bv->toggleToggle();
61                         else
62                                 bv->updateInset(lt->inset_owner, false);
63                 }
64                 if (lt->bv_owner) {
65                         //if (fitcur)
66                         //      bv->update(lt, BufferView::SELECT|BufferView::FITCUR);
67                         //else
68                         bv->update(lt, BufferView::SELECT|BufferView::FITCUR);
69                         bv->showCursor();
70                 } else if (bv->text->status() != LyXText::UNCHANGED) {
71                         bv->theLockingInset()->hideInsetCursor(bv);
72                         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
73                         bv->showCursor();
74                 }
75
76                 if (!lt->selection.set())
77                         bv->workarea().haveSelection(false);
78
79                 bv->switchKeyMap();
80         }
81
82
83         void finishChange(BufferView * bv, bool selecting = false)
84         {
85                 finishUndo();
86                 moveCursorUpdate(bv, selecting);
87                 bv->owner()->view_state_changed();
88         }
89
90 }
91
92
93 bool LyXText::gotoNextInset(BufferView * bv,
94         vector<Inset::Code> const & codes, string const & contents) const
95 {
96         LyXCursor res = cursor;
97         Inset * inset;
98         do {
99                 if (res.pos() < res.par()->size() - 1) {
100                         res.pos(res.pos() + 1);
101                 } else  {
102                         res.par(res.par()->next());
103                         res.pos(0);
104                 }
105
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())
110                    != codes.end()
111                    && (contents.empty() ||
112                        static_cast<InsetCommand *>(
113                                                         res.par()->getInset(res.pos()))->getContents()
114                        == contents)));
115
116         if (res.par()) {
117                 setCursor(bv, res.par(), res.pos(), false);
118                 return true;
119         }
120         return false;
121 }
122
123
124 void LyXText::gotoInset(BufferView * bv, vector<Inset::Code> const & codes,
125                                   bool same_content)
126 {
127         bv->hideCursor();
128         bv->beforeChange(this);
129         update(bv, false);
130
131         string contents;
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())
135                     != codes.end())
136                         contents = static_cast<InsetCommand const *>(inset)->getContents();
137         }
138
139         if (!gotoNextInset(bv, codes, contents)) {
140                 if (cursor.pos() || cursor.par() != ownerParagraph()) {
141                         LyXCursor tmp = cursor;
142                         cursor.par(ownerParagraph());
143                         cursor.pos(0);
144                         if (!gotoNextInset(bv, codes, contents)) {
145                                 cursor = tmp;
146                                 bv->owner()->message(_("No more insets"));
147                         }
148                 } else {
149                         bv->owner()->message(_("No more insets"));
150                 }
151         }
152         update(bv, false);
153         selection.cursor = cursor;
154 }
155
156
157 void LyXText::gotoInset(BufferView * bv, Inset::Code code, bool same_content)
158 {
159         gotoInset(bv, vector<Inset::Code>(1, code), same_content);
160 }
161
162
163 void LyXText::cursorPrevious(BufferView * bv)
164 {
165         if (!cursor.row()->previous()) {
166                 if (first_y > 0) {
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();
170                 }
171                 return;
172         }
173
174         int y = first_y;
175         Row * cursorrow = cursor.row();
176
177         setCursorFromCoordinates(bv, cursor.x_fix(), y);
178         finishUndo();
179
180         int new_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();
188         } else {
189                 if (inset_owner) {
190                         new_y = bv->text->cursor.iy()
191                                 + bv->theLockingInset()->insetInInsetY() + y
192                                 + cursor.row()->height()
193                                 - bv->workarea().workHeight() + 1;
194                 } else {
195                         new_y = cursor.y()
196                                 - cursor.row()->baseline()
197                                 + cursor.row()->height()
198                                 - bv->workarea().workHeight() + 1;
199                 }
200         }
201         bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
202         if (cursor.row()->previous()) {
203                 LyXCursor cur;
204                 setCursor(bv, cur, cursor.row()->previous()->par(),
205                                                 cursor.row()->previous()->pos(), false);
206                 if (cur.y() > first_y) {
207                         cursorUp(bv, true);
208                 }
209         }
210         bv->updateScrollbar();
211 }
212
213
214 void LyXText::cursorNext(BufferView * bv)
215 {
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();
223                 }
224                 return;
225         }
226
227         int y = first_y + bv->workarea().workHeight();
228         if (inset_owner && !first_y) {
229                 y -= (bv->text->cursor.iy()
230                           - bv->text->first_y
231                           + bv->theLockingInset()->insetInInsetY());
232         }
233
234         getRowNearY(y);
235
236         Row * cursorrow = cursor.row();
237         setCursorFromCoordinates(bv, cursor.x_fix(), y);
238         // + workarea().workHeight());
239         finishUndo();
240
241         int new_y;
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();
249         } else {
250                 if (inset_owner) {
251                         new_y = bv->text->cursor.iy()
252                                 + bv->theLockingInset()->insetInInsetY()
253                                 + y - cursor.row()->baseline();
254                 } else {
255                         new_y =  cursor.y() - cursor.row()->baseline();
256                 }
257         }
258         bv->screen().draw(bv->text, bv, new_y);
259         if (cursor.row()->next()) {
260                 LyXCursor cur;
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);
265                 }
266         }
267         bv->updateScrollbar();
268 }
269
270
271 void LyXText::update(BufferView * bv, bool changed)
272 {
273         BufferView::UpdateCodes c = BufferView::SELECT | BufferView::FITCUR;
274         if (changed)
275                 bv->update(this, c | BufferView::CHANGE);
276         else
277                 bv->update(this, c);
278 }
279
280
281 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
282 {
283         bv->hideCursor();
284         lt->update(bv);
285         InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
286         if (!bv->insertInset(new_inset))
287                 delete new_inset;
288         else
289                 bv->updateInset(new_inset, true);
290 }
291
292
293 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
294 {
295         lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
296                               <<"] arg[" << cmd.argument << "]" << endl;
297
298         BufferView * bv = cmd.view();
299
300         switch (cmd.action) {
301
302         case LFUN_APPENDIX: {
303                 Paragraph * par = cursor.par();
304                 bool start = !par->params().startOfAppendix();
305
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);
310
311                 par->params().startOfAppendix(start);
312
313                 // we can set the refreshing parameters now
314                 status(cmd.view(), LyXText::NEED_MORE_REFRESH);
315                 refresh_y = 0;
316                 refresh_row = 0; // not needed for full update
317                 updateCounters(cmd.view());
318                 setCursor(cmd.view(), cursor.par(), cursor.pos());
319                 update(bv);
320                 break;
321         }
322
323         case LFUN_DELETE_WORD_FORWARD:
324                 bv->beforeChange(this);
325                 update(bv, false);
326                 deleteWordForward(bv);
327                 update(bv);
328                 finishChange(bv);
329                 break;
330
331         case LFUN_DELETE_WORD_BACKWARD:
332                 bv->beforeChange(this);
333                 update(bv, false);
334                 deleteWordBackward(bv);
335                 update(bv, true);
336                 finishChange(bv);
337                 break;
338
339         case LFUN_DELETE_LINE_FORWARD:
340                 bv->beforeChange(this);
341                 update(bv, false);
342                 deleteLineForward(bv);
343                 update(bv);
344                 finishChange(bv);
345                 break;
346
347         case LFUN_SHIFT_TAB:
348         case LFUN_TAB:
349                 if (!selection.mark())
350                         bv->beforeChange(this);
351                 update(bv, false);
352                 cursorTab(bv);
353                 finishChange(bv);
354                 break;
355
356         case LFUN_WORDRIGHT:
357                 if (!selection.mark())
358                         bv->beforeChange(this);
359                 update(bv, false);
360                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
361                         cursorLeftOneWord(bv);
362                 else
363                         cursorRightOneWord(bv);
364                 finishChange(bv);
365                 break;
366
367         case LFUN_WORDLEFT:
368                 if (!selection.mark())
369                         bv->beforeChange(this);
370                 update(bv, false);
371                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
372                         cursorRightOneWord(bv);
373                 else
374                         cursorLeftOneWord(bv);
375                 finishChange(bv);
376                 break;
377
378         case LFUN_BEGINNINGBUF:
379                 if (!selection.mark())
380                         bv->beforeChange(this);
381                 update(bv, false);
382                 cursorTop(bv);
383                 finishChange(bv);
384                 break;
385
386         case LFUN_ENDBUF:
387                 if (selection.mark())
388                         bv->beforeChange(this);
389                 update(bv, false);
390                 cursorBottom(bv);
391                 finishChange(bv);
392                 break;
393
394         case LFUN_RIGHTSEL:
395                 update(bv, false);
396                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
397                         cursorLeft(bv);
398                 else
399                         cursorRight(bv);
400                 finishChange(bv, true);
401                 break;
402
403         case LFUN_LEFTSEL:
404                 update(bv, false);
405                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
406                         cursorRight(bv);
407                 else
408                         cursorLeft(bv);
409                 finishChange(bv, true);
410                 break;
411
412         case LFUN_UPSEL:
413                 update(bv, false);
414                 cursorUp(bv, true);
415                 finishChange(bv, true);
416                 break;
417
418         case LFUN_DOWNSEL:
419                 update(bv, false);
420                 cursorDown(bv, true);
421                 finishChange(bv, true);
422                 break;
423
424         case LFUN_UP_PARAGRAPHSEL:
425                 update(bv, false);
426                 cursorUpParagraph(bv);
427                 finishChange(bv, true);
428                 break;
429
430         case LFUN_DOWN_PARAGRAPHSEL:
431                 update(bv, false);
432                 cursorDownParagraph(bv);
433                 finishChange(bv, true);
434                 break;
435
436         case LFUN_PRIORSEL:
437                 update(bv, false);
438                 cursorPrevious(bv);
439                 finishChange(bv, true);
440                 break;
441
442         case LFUN_NEXTSEL:
443                 update(bv, false);
444                 cursorNext(bv);
445                 finishChange(bv, true);
446                 break;
447
448         case LFUN_HOMESEL:
449                 update(bv, false);
450                 cursorHome(bv);
451                 finishChange(bv, true);
452                 break;
453
454         case LFUN_ENDSEL:
455                 update(bv, false);
456                 cursorEnd(bv);
457                 finishChange(bv, true);
458                 break;
459
460         case LFUN_WORDRIGHTSEL:
461                 update(bv, false);
462                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
463                         cursorLeftOneWord(bv);
464                 else
465                         cursorRightOneWord(bv);
466                 finishChange(bv, true);
467                 break;
468
469         case LFUN_WORDLEFTSEL:
470                 update(bv, false);
471                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
472                         cursorRightOneWord(bv);
473                 else
474                         cursorLeftOneWord(bv);
475                 finishChange(bv, true);
476                 break;
477
478         case LFUN_RIGHT: {
479                 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
480                 if (!selection.mark())
481                         bv->beforeChange(this);
482                 update(bv);
483                 if (is_rtl)
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);
491                         break;
492                 }
493                 if (!is_rtl)
494                         cursorRight(bv, false);
495                 finishChange(bv);
496                 break;
497         }
498
499         case LFUN_LEFT: {
500                 // This is soooo ugly. Isn`t it possible to make
501                 // it simpler? (Lgb)
502                 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
503                 if (!selection.mark())
504                         bv->beforeChange(this);
505                 update(bv);
506                 LyXCursor const cur = cursor;
507                 if (!is_rtl)
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);
516                         break;
517                 }
518                 if (is_rtl)
519                         cursorRight(bv, false);
520                 finishChange(bv);
521                 break;
522         }
523
524         case LFUN_UP:
525                 if (!selection.mark())
526                         bv->beforeChange(this);
527                 bv->update(this, BufferView::UPDATE);
528                 cursorUp(bv);
529                 finishChange(bv);
530                 break;
531
532         case LFUN_DOWN:
533                 if (!selection.mark())
534                         bv->beforeChange(this);
535                 bv->update(this, BufferView::UPDATE);
536                 cursorDown(bv);
537                 finishChange(bv);
538                 break;
539
540         case LFUN_UP_PARAGRAPH:
541                 if (!selection.mark())
542                         bv->beforeChange(this);
543                 bv->update(this, BufferView::UPDATE);
544                 cursorUpParagraph(bv);
545                 finishChange(bv);
546                 break;
547
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);
554                 break;
555
556         case LFUN_PRIOR:
557                 if (!selection.mark())
558                         bv->beforeChange(this);
559                 bv->update(this, BufferView::UPDATE);
560                 cursorPrevious(bv);
561                 finishChange(bv, false);
562                 // was:
563                 // finishUndo();
564                 // moveCursorUpdate(bv, false, false);
565                 // owner_->view_state_changed();
566                 break;
567
568         case LFUN_NEXT:
569                 if (!selection.mark())
570                         bv->beforeChange(this);
571                 bv->update(this, BufferView::UPDATE);
572                 cursorNext(bv);
573                 finishChange(bv, false);
574                 break;
575
576         case LFUN_HOME:
577                 if (!selection.mark())
578                         bv->beforeChange(this);
579                 update(bv);
580                 cursorHome(bv);
581                 finishChange(bv, false);
582                 break;
583
584         case LFUN_END:
585                 if (!selection.mark())
586                         bv->beforeChange(this);
587                 update(bv);
588                 cursorEnd(bv);
589                 finishChange(bv, false);
590                 break;
591
592         case LFUN_BREAKLINE:
593                 bv->beforeChange(this);
594                 insertChar(bv, Paragraph::META_NEWLINE);
595                 update(bv, true);
596                 setCursor(bv, cursor.par(), cursor.pos());
597                 moveCursorUpdate(bv, false);
598                 break;
599
600         case LFUN_DELETE:
601                 if (!selection.set()) {
602                         Delete(bv);
603                         selection.cursor = cursor;
604                         update(bv);
605                         // It is possible to make it a lot faster still
606                         // just comment out the line below...
607                         bv->showCursor();
608                 } else {
609                         update(bv, false);
610                         cutSelection(bv, true);
611                         update(bv);
612                 }
613                 moveCursorUpdate(bv, false);
614                 bv->owner()->view_state_changed();
615                 bv->switchKeyMap();
616                 break;
617
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()) {
623                                 cursorRight(bv);
624                                 cur = cursor;
625                                 if (cur.pos() == 0
626                                     && !(cur.par()->params().spaceTop()
627                                          == VSpace (VSpace::NONE))) {
628                                         setParagraph(bv,
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);
638                                         cursorLeft(bv);
639                                         update(bv);
640                                 } else {
641                                         cursorLeft(bv);
642                                         Delete(bv);
643                                         selection.cursor = cursor;
644                                 }
645                         } else {
646                                 Delete(bv);
647                                 selection.cursor = cursor;
648                         }
649                 } else {
650                         update(bv, false);
651                         cutSelection(bv, true);
652                 }
653                 update(bv);
654                 break;
655
656
657         case LFUN_BACKSPACE:
658                 if (!selection.set()) {
659                         if (bv->owner()->getIntl().getTransManager().backspace()) {
660                                 backspace(bv);
661                                 selection.cursor = cursor;
662                                 update(bv);
663                                 // It is possible to make it a lot faster still
664                                 // just comment out the line below...
665                                 bv->showCursor();
666                         }
667                 } else {
668                         update(bv, false);
669                         cutSelection(bv, true);
670                         update(bv);
671                 }
672                 bv->owner()->view_state_changed();
673                 bv->switchKeyMap();
674                 break;
675
676         case LFUN_BACKSPACE_SKIP:
677                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
678                 if (!selection.set()) {
679                         LyXCursor cur = cursor;
680                         if (cur.pos() == 0
681                             && !(cur.par()->params().spaceTop()
682                                  == VSpace (VSpace::NONE))) {
683                                 setParagraph(bv,
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);
692                         } else {
693                                 backspace(bv);
694                                 selection.cursor = cur;
695                         }
696                 } else {
697                         update(bv, false);
698                         cutSelection(bv, true);
699                 }
700                 update(bv);
701                 break;
702
703         case LFUN_BREAKPARAGRAPH:
704                 bv->beforeChange(this);
705                 breakParagraph(bv, 0);
706                 update(bv);
707                 selection.cursor = cursor;
708                 bv->switchKeyMap();
709                 bv->owner()->view_state_changed();
710                 break;
711
712         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
713                 bv->beforeChange(this);
714                 breakParagraph(bv, 1);
715                 update(bv);
716                 selection.cursor = cursor;
717                 bv->switchKeyMap();
718                 bv->owner()->view_state_changed();
719                 break;
720
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)) {
729                                 setParagraph(bv,
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);
738                                 //update(bv);
739                         }
740                 }
741                 else {
742                         breakParagraph(bv, 0);
743                         //update(bv);
744                 }
745                 update(bv);
746                 selection.cursor = cur;
747                 bv->switchKeyMap();
748                 bv->owner()->view_state_changed();
749                 break;
750         }
751
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();
758
759                 istringstream is(cmd.argument.c_str());
760                 string tmp;
761                 is >> tmp;
762                 Spacing::Space new_spacing = cur_spacing;
763                 float new_value = cur_value;
764                 if (tmp.empty()) {
765                         lyxerr << "Missing argument to `paragraph-spacing'"
766                                << endl;
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;
775                         float tmpval = 0.0;
776                         is >> tmpval;
777                         lyxerr << "new_value = " << tmpval << endl;
778                         if (tmpval != 0.0)
779                                 new_value = tmpval;
780                 } else if (tmp == "default") {
781                         new_spacing = Spacing::Default;
782                 } else {
783                         lyxerr << _("Unknown spacing argument: ")
784                                << cmd.argument << endl;
785                 }
786                 if (cur_spacing != new_spacing || cur_value != new_value) {
787                         par->params().spacing(Spacing(new_spacing, new_value));
788                         redoParagraph(bv);
789                         update(bv);
790                 }
791                 break;
792         }
793
794         case LFUN_INSET_TOGGLE:
795                 bv->hideCursor();
796                 bv->beforeChange(this);
797                 update(bv, false);
798                 toggleInset(bv);
799                 update(bv, false);
800                 bv->switchKeyMap();
801                 break;
802
803         case LFUN_PROTECTEDSPACE:
804                 if (cursor.par()->layout()->free_spacing) {
805                         insertChar(bv, ' ');
806                         update(bv);
807                 } else {
808                         specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
809                 }
810                 moveCursorUpdate(bv, false);
811                 break;
812
813         case LFUN_HYPHENATION:
814                 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
815                 break;
816
817         case LFUN_LIGATURE_BREAK:
818                 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
819                 break;
820
821         case LFUN_LDOTS:
822                 specialChar(this, bv, InsetSpecialChar::LDOTS);
823                 break;
824
825         case LFUN_HFILL:
826                 bv->hideCursor();
827                 update(bv, false);
828                 insertChar(bv, Paragraph::META_HFILL);
829                 update(bv);
830                 break;
831
832         case LFUN_END_OF_SENTENCE:
833                 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
834                 break;
835
836         case LFUN_MENU_SEPARATOR:
837                 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
838                 break;
839
840         case LFUN_MARK_OFF:
841                 bv->beforeChange(this);
842                 update(bv, false);
843                 selection.cursor = cursor;
844                 cmd.message(N_("Mark off"));
845                 break;
846
847         case LFUN_MARK_ON:
848                 bv->beforeChange(this);
849                 selection.mark(true);
850                 update(bv, false);
851                 selection.cursor = cursor;
852                 cmd.message(N_("Mark on"));
853                 break;
854
855         case LFUN_SETMARK:
856                 bv->beforeChange(this);
857                 if (selection.mark()) {
858                         update(bv);
859                         cmd.message(N_("Mark removed"));
860                 } else {
861                         selection.mark(true);
862                         update(bv);
863                         cmd.message(N_("Mark set"));
864                 }
865                 selection.cursor = cursor;
866                 break;
867
868         case LFUN_UPCASE_WORD:
869                 update(bv, false);
870                 changeCase(bv, LyXText::text_uppercase);
871                 if (inset_owner)
872                         bv->updateInset(inset_owner, true);
873                 update(bv);
874                 break;
875
876         case LFUN_LOWCASE_WORD:
877                 update(bv, false);
878                 changeCase(bv, LyXText::text_lowercase);
879                 if (inset_owner)
880                         bv->updateInset(inset_owner, true);
881                 update(bv);
882                 break;
883
884         case LFUN_CAPITALIZE_WORD:
885                 update(bv, false);
886                 changeCase(bv, LyXText::text_capitalization);
887                 if (inset_owner)
888                         bv->updateInset(inset_owner, true);
889                 update(bv);
890                 break;
891
892         case LFUN_TRANSPOSE_CHARS:
893                 update(bv, false);
894                 transposeChars(*bv);
895                 if (inset_owner)
896                         bv->updateInset(inset_owner, true);
897                 update(bv);
898                 break;
899
900         case LFUN_PASTE:
901                 cmd.message(_("Paste"));
902                 bv->hideCursor();
903                 // clear the selection
904                 bv->toggleSelection();
905                 clearSelection();
906                 update(bv, false);
907                 pasteSelection(bv);
908                 clearSelection(); // bug 393
909                 update(bv, false);
910                 update(bv);
911                 bv->switchKeyMap();
912                 break;
913
914         case LFUN_CUT:
915                 bv->hideCursor();
916                 update(bv, false);
917                 cutSelection(bv, true);
918                 update(bv);
919                 cmd.message(_("Cut"));
920                 break;
921
922         case LFUN_COPY:
923                 copySelection(bv);
924                 cmd.message(_("Copy"));
925                 break;
926
927         case LFUN_BEGINNINGBUFSEL:
928                 if (inset_owner)
929                         return Inset::UNDISPATCHED;
930                 update(bv, false);
931                 cursorTop(bv);
932                 finishChange(bv, true);
933                 break;
934
935         case LFUN_ENDBUFSEL:
936                 if (inset_owner)
937                         return Inset::UNDISPATCHED;
938                 update(bv, false);
939                 cursorBottom(bv);
940                 finishChange(bv, true);
941                 break;
942
943         case LFUN_GETXY:
944                 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
945                 break;
946
947         case LFUN_SETXY: {
948                 int x = 0;
949                 int y = 0;
950                 istringstream is(cmd.argument.c_str());
951                 is >> x >> y;
952                 if (!is)
953                         lyxerr << "SETXY: Could not parse coordinates in '"
954                                << cmd.argument << std::endl;
955                 else 
956                         setCursorFromCoordinates(bv, x, y);
957                 break;
958         }
959
960         case LFUN_GETFONT:
961                 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
962                         cmd.message("E");
963                 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
964                         cmd.message("N");
965                 else
966                         cmd.message("0");
967                 break;
968
969         case LFUN_GETLAYOUT:
970                 cmd.message(tostr(cursor.par()->layout()));
971                 break;
972
973         case LFUN_LAYOUT: {
974                 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
975                   << cmd.argument << endl;
976
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."));
984                         break;
985                 }
986
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;
992
993                 // If the entry is obsolete, use the new one instead.
994                 if (hasLayout) {
995                         string const & obs = tclass[layout]->obsoleted_by();
996                         if (!obs.empty())
997                                 layout = obs;
998                 }
999
1000                 if (!hasLayout) {
1001                         cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1002                                 N_(" not known"));
1003                         break;
1004                 }
1005
1006                 bool change_layout = (current_layout != layout);
1007                 if (!change_layout && selection.set() &&
1008                         selection.start.par() != selection.end.par())
1009                 {
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;
1015                                         break;
1016                                 }
1017                         }
1018                 }
1019                 if (change_layout) {
1020                         bv->hideCursor();
1021                         current_layout = layout;
1022                         update(bv, false);
1023                         setLayout(bv, layout);
1024                         bv->owner()->setLayout(layout);
1025                         update(bv);
1026                         bv->switchKeyMap();
1027                 }
1028                 break;
1029         }
1030
1031         case LFUN_PASTESELECTION: {
1032                 if (!bv->buffer())
1033                         break;
1034                 bv->hideCursor();
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);
1042                         else
1043                                 insertStringAsLines(bv, clip);
1044                         clearSelection();
1045                         update(bv);
1046                 }
1047                 break;
1048         }
1049
1050         case LFUN_GOTOERROR:
1051                 gotoInset(bv, Inset::ERROR_CODE, false);
1052                 break;
1053
1054         case LFUN_GOTONOTE:
1055                 gotoInset(bv, Inset::NOTE_CODE, false);
1056                 break;
1057
1058         case LFUN_REFERENCE_GOTO:
1059         {
1060                 vector<Inset::Code> tmp;
1061                 tmp.push_back(Inset::LABEL_CODE);
1062                 tmp.push_back(Inset::REF_CODE);
1063                 gotoInset(bv, tmp, true);
1064                 break;
1065         }
1066
1067 #if 0
1068         case LFUN_INSET_LIST:
1069         case LFUN_INSET_THEOREM:
1070 #endif
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:
1081         {
1082                 Inset * inset = createInset(cmd);
1083                 if (inset) {
1084                         bool gotsel = false;
1085                         if (selection.set()) {
1086                                 cutSelection(bv, true, false);
1087                                 gotsel = true;
1088                         }
1089                         if (bv->insertInset(inset)) {
1090                                 inset->edit(bv);
1091                                 if (gotsel)
1092                                         bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
1093                         }
1094                         else
1095                                 delete inset;
1096                 }
1097                 break;
1098         }
1099
1100
1101         case LFUN_QUOTE: {
1102                 Paragraph const * par = cursor.par();
1103                 lyx::pos_type pos = cursor.pos();
1104                 char c;
1105                 if (!pos)
1106                         c = ' ';
1107                 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1108                         c = ' ';
1109                 else
1110                         c = par->getChar(pos - 1);
1111
1112                 bv->hideCursor();
1113                 LyXLayout_ptr const & style = par->layout();
1114
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, "\""));
1120                 break;
1121         }
1122
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, "");
1127                 string arg;
1128                 if (!cmd.argument.empty())
1129                         arg = cmd.argument;
1130                 else
1131                         arg = lyxrc.date_insert_format;
1132                 char datetmp[32];
1133                 int const datetmp_len =
1134                         ::strftime(datetmp, 32, arg.c_str(), now_tm);
1135
1136                 for (int i = 0; i < datetmp_len; i++) {
1137                         insertChar(bv, datetmp[i]);
1138                         update(bv, true);
1139                 }
1140                 selection.cursor = cursor;
1141                 moveCursorUpdate(bv, false);
1142                 break;
1143         }
1144
1145         case LFUN_MOUSE_TRIPLE:
1146                 if (!bv->buffer())
1147                         break;
1148                 if (bv_owner && bv->theLockingInset())
1149                         break;
1150                 if (cmd.button() == mouse_button::button1) {
1151                         if (bv_owner) {
1152                                 bv->screen().hideCursor();
1153                                 bv->screen().toggleSelection(this, bv);
1154                         }
1155                         cursorHome(bv);
1156                         selection.cursor = cursor;
1157                         cursorEnd(bv);
1158                         setSelection(bv);
1159                         if (bv_owner)
1160                                 bv->screen().toggleSelection(this, bv, false);
1161                         update(bv, false);
1162                         bv->workarea().haveSelection(selection.set());
1163                 }
1164                 break;
1165
1166         case LFUN_MOUSE_DOUBLE:
1167                 if (!bv->buffer())
1168                         break;
1169                 if (bv_owner && bv->theLockingInset())
1170                         break;
1171                 if (cmd.button() == mouse_button::button1) {
1172                         if (bv_owner) {
1173                                 bv->screen().hideCursor();
1174                                 bv->screen().toggleSelection(this, bv);
1175                                 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1176                                 bv->screen().toggleSelection(this, bv, false);
1177                         } else {
1178                                 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1179                         }
1180                         update(bv, false);
1181                         bv->workarea().haveSelection(selection.set());
1182                 }
1183                 break;
1184
1185         case LFUN_MOUSE_MOTION:
1186         {
1187                 // Only use motion with button 1
1188                 //if (ev.button() != mouse_button::button1)
1189                 //      return false;
1190
1191                 if (!bv->buffer())
1192                         break;
1193
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;
1205                         cmd1.x -= start_x;
1206                         cmd1.y -= cursor.iy() + bv->text->first_y;
1207                         tli->localDispatch(cmd1);
1208                         break;
1209                 }
1210
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";
1217                         break;
1218                 }
1219
1220                 bv->screen().hideCursor();
1221
1222                 Row * cursorrow = bv->text->cursor.row();
1223                 bv->text->setCursorFromCoordinates(bv, cmd.x, cmd.y + bv->text->first_y);
1224         #if 0
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;
1231         #endif
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);
1238                         }
1239                 }
1240
1241                 if (!bv->text->selection.set())
1242                         bv->update(bv->text, BufferView::UPDATE); // Maybe an empty line was deleted
1243
1244                 bv->text->setSelection(bv);
1245                 bv->screen().toggleToggle(bv->text, bv);
1246                 bv->fitCursor();
1247                 bv->showCursor();
1248                 break;
1249         }
1250
1251         // Single-click on work area
1252         case LFUN_MOUSE_PRESS:
1253         {
1254                 if (!bv->buffer())
1255                         break;
1256
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);
1263                         break;
1264                 }
1265                 if (cmd.button() == mouse_button::button5) {
1266                         bv->scroll(lyxrc.wheel_jump);
1267                         break;
1268                 }
1269
1270                 int x = cmd.x;
1271                 int y = cmd.y;
1272                 Inset * inset_hit = bv->checkInsetHit(bv->text, x, y);
1273
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;
1282                 }
1283
1284                 int const screen_first = bv->text->first_y;
1285
1286                 if (bv->theLockingInset()) {
1287                         // We are in inset locking mode
1288
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);
1294                                 break;
1295                         }
1296                         bv->unlockInset(bv->theLockingInset());
1297                 }
1298
1299                 if (!inset_hit)
1300                         selection_possible = true;
1301                 bv->screen().hideCursor();
1302
1303                 // Clear the selection
1304                 bv->screen().toggleSelection(bv->text, bv);
1305                 bv->text->clearSelection();
1306                 bv->text->fullRebreak(bv);
1307                 bv->update();
1308                 bv->updateScrollbar();
1309
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);
1324                         break;
1325                 }
1326                 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1327
1328                 // Right click on a footnote flag opens float menu
1329                 if (cmd.button() == mouse_button::button3) {
1330                         selection_possible = false;
1331                         break;
1332                 }
1333
1334                 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1335                         bv->text->setCursorFromCoordinates(bv, x, y + screen_first);
1336                 finishUndo();
1337                 bv->text->selection.cursor = bv->text->cursor;
1338                 bv->text->cursor.x_fix(bv->text->cursor.x());
1339
1340                 bv->owner()->updateLayoutChoice();
1341                 if (bv->fitCursor())
1342                         selection_possible = false;
1343
1344                 // Insert primary selection with middle mouse
1345                 // if there is a local selection in the current buffer,
1346                 // insert this
1347                 if (cmd.button() == mouse_button::button2) {
1348                         if (paste_internally)
1349                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1350                         else
1351                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1352                         selection_possible = false;
1353                 }
1354                 break;
1355         }
1356
1357         case LFUN_MOUSE_RELEASE:
1358         {
1359                 // do nothing if we used the mouse wheel
1360                 if (!bv->buffer())
1361                         break;
1362         
1363                 if (cmd.button() == mouse_button::button4
1364                  || cmd.button() == mouse_button::button5)
1365                         break;
1366
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.
1370                 int x = cmd.x;
1371                 int y = cmd.y;
1372                 Inset * inset_hit = bv->checkInsetHit(bv->text, x, y);
1373
1374                 if (bv->theLockingInset()) {
1375                         // We are in inset locking mode.
1376
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);
1382                         break;
1383                 }
1384
1385                 selection_possible = false;
1386
1387                 if (cmd.button() == mouse_button::button2)
1388                         break;
1389
1390                 // finish selection
1391                 if (cmd.button() == mouse_button::button1)
1392                         bv->workarea().haveSelection(selection.set());
1393
1394                 bv->switchKeyMap();
1395                 bv->owner()->view_state_changed();
1396                 bv->owner()->updateMenubar();
1397                 bv->owner()->updateToolbar();
1398
1399                 // Did we hit an editable inset?
1400                 if (inset_hit) {
1401                         selection_possible = false;
1402
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
1410                         // the selection.
1411                         // Note this also fixes selection drawing
1412                         // problems if we end up opening an inset
1413                         if (selection.set())
1414                                 break;
1415
1416                         // CHECK fix this proper in 0.13
1417                         // well, maybe 13.0 !!!!!!!!!
1418
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...
1429                         // (Joacim)
1430                         // ...or maybe the SetCursorParUndo()
1431                         // below isn't necessary at all anylonger?
1432                         if (inset_hit->lyxCode() == Inset::REF_CODE)
1433                                 setCursorParUndo(bv);
1434
1435                         bv->owner()->message(inset_hit->editMessage());
1436
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);
1442                         } else {
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
1448                                 // (Jug 20020329)
1449 #ifdef WITH_WARNINGS
1450 #warning Please remove donot call inset->edit() here (Jug 20020812)
1451 #endif
1452                                 inset_hit->edit(bv, x, y, cmd.button());
1453                         }
1454                         break;
1455                 }
1456
1457                 // Maybe we want to edit a bibitem ale970302
1458                 if (bv->text->cursor.par()->bibkey) {
1459                         bool const is_rtl =
1460                                 bv->text->cursor.par()->isRightToLeftPar(bv->buffer()->params);
1461                         int const width =
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);
1466                         }
1467                 }
1468                 break;
1469         }
1470
1471         case LFUN_SELFINSERT: {
1472                 if (cmd.argument.empty())
1473                         break;
1474
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
1479                 // true (on).
1480
1481                 if (lyxrc.auto_region_delete) {
1482                         if (selection.set()) {
1483                                 cutSelection(bv, false, false);
1484                                 update(bv);
1485                         }
1486                         bv->workarea().haveSelection(false);
1487                 }
1488
1489                 bv->beforeChange(this);
1490                 LyXFont const old_font(real_current_font);
1491
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);
1497
1498                 update(bv);
1499                 selection.cursor = cursor;
1500                 moveCursorUpdate(bv, false);
1501
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();
1506                 break;
1507         }
1508
1509         default:
1510                 return Inset::UNDISPATCHED;
1511         }
1512
1513         return Inset::DISPATCHED;
1514 }