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