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