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