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