]> git.lyx.org Git - lyx.git/blob - src/text3.C
fix bug #652
[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                                 spar = spar->next();
1108                         }
1109                 }
1110                 if (change_layout) {
1111                         bv->hideCursor();
1112                         current_layout = layout;
1113                         update(bv, false);
1114                         setLayout(bv, layout);
1115                         bv->owner()->setLayout(layout);
1116                         update(bv);
1117                         bv->switchKeyMap();
1118                 }
1119                 break;
1120         }
1121
1122         case LFUN_PASTESELECTION: {
1123                 if (!bv->buffer())
1124                         break;
1125                 bv->hideCursor();
1126                 // this was originally a beforeChange(bv->text), i.e
1127                 // the outermost LyXText!
1128                 bv->beforeChange(this);
1129                 string const clip = bv->getClipboard();
1130                 if (!clip.empty()) {
1131                         if (cmd.argument == "paragraph")
1132                                 insertStringAsParagraphs(bv, clip);
1133                         else
1134                                 insertStringAsLines(bv, clip);
1135                         clearSelection();
1136                         update(bv);
1137                 }
1138                 break;
1139         }
1140
1141         case LFUN_GOTOERROR:
1142                 gotoInset(bv, Inset::ERROR_CODE, false);
1143                 break;
1144
1145         case LFUN_GOTONOTE:
1146                 gotoInset(bv, Inset::NOTE_CODE, false);
1147                 break;
1148
1149         case LFUN_REFERENCE_GOTO:
1150         {
1151                 vector<Inset::Code> tmp;
1152                 tmp.push_back(Inset::LABEL_CODE);
1153                 tmp.push_back(Inset::REF_CODE);
1154                 gotoInset(bv, tmp, true);
1155                 break;
1156         }
1157
1158         case LFUN_QUOTE: {
1159                 Paragraph const * par = cursor.par();
1160                 lyx::pos_type pos = cursor.pos();
1161                 char c;
1162                 if (!pos)
1163                         c = ' ';
1164                 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1165                         c = ' ';
1166                 else
1167                         c = par->getChar(pos - 1);
1168
1169                 bv->hideCursor();
1170                 LyXLayout_ptr const & style = par->layout();
1171
1172                 if (style->pass_thru ||
1173                                 par->getFontSettings(bv->buffer()->params,
1174                                          pos).language()->lang() == "hebrew" ||
1175                         (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1176                         bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1177                 break;
1178         }
1179
1180         case LFUN_DATE_INSERT: {
1181                 time_t now_time_t = time(NULL);
1182                 struct tm * now_tm = localtime(&now_time_t);
1183                 setlocale(LC_TIME, "");
1184                 string arg;
1185                 if (!cmd.argument.empty())
1186                         arg = cmd.argument;
1187                 else
1188                         arg = lyxrc.date_insert_format;
1189                 char datetmp[32];
1190                 int const datetmp_len =
1191                         ::strftime(datetmp, 32, arg.c_str(), now_tm);
1192
1193                 for (int i = 0; i < datetmp_len; i++) {
1194                         insertChar(bv, datetmp[i]);
1195                         update(bv, true);
1196                 }
1197                 selection.cursor = cursor;
1198                 moveCursorUpdate(bv, false);
1199                 break;
1200         }
1201
1202         case LFUN_MOUSE_TRIPLE:
1203                 if (!bv->buffer())
1204                         break;
1205                 if (bv_owner && bv->theLockingInset())
1206                         break;
1207                 if (cmd.button() == mouse_button::button1) {
1208                         if (bv_owner) {
1209                                 bv->screen().hideCursor();
1210                                 bv->screen().toggleSelection(this, bv);
1211                         }
1212                         cursorHome(bv);
1213                         selection.cursor = cursor;
1214                         cursorEnd(bv);
1215                         setSelection(bv);
1216                         if (bv_owner)
1217                                 bv->screen().toggleSelection(this, bv, false);
1218                         update(bv, false);
1219                         bv->haveSelection(selection.set());
1220                 }
1221                 break;
1222
1223         case LFUN_MOUSE_DOUBLE:
1224                 if (!bv->buffer())
1225                         break;
1226                 if (bv_owner && bv->theLockingInset())
1227                         break;
1228                 if (cmd.button() == mouse_button::button1) {
1229                         if (bv_owner) {
1230                                 bv->screen().hideCursor();
1231                                 bv->screen().toggleSelection(this, bv);
1232                                 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1233                                 bv->screen().toggleSelection(this, bv, false);
1234                         } else {
1235                                 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1236                         }
1237                         update(bv, false);
1238                         bv->haveSelection(selection.set());
1239                 }
1240                 break;
1241
1242         case LFUN_MOUSE_MOTION:
1243         {
1244                 // Only use motion with button 1
1245                 //if (ev.button() != mouse_button::button1)
1246                 //      return false;
1247
1248                 if (!bv->buffer())
1249                         break;
1250
1251                 // Check for inset locking
1252                 if (bv->theLockingInset()) {
1253                         Inset * tli = bv->theLockingInset();
1254                         LyXCursor cursor = bv->text->cursor;
1255                         LyXFont font = bv->text->getFont(bv->buffer(),
1256                                                                 cursor.par(), cursor.pos());
1257                         int width = tli->width(bv, font);
1258                         int inset_x = font.isVisibleRightToLeft()
1259                                 ? cursor.ix() - width : cursor.ix();
1260                         int start_x = inset_x + tli->scroll();
1261                         FuncRequest cmd1 = cmd;
1262                         cmd1.x = cmd.x - start_x;
1263                         cmd1.y = cmd.y - cursor.iy() + bv->text->first_y;
1264                         tli->localDispatch(cmd1);
1265                         break;
1266                 }
1267
1268                 // The test for not selection possible is needed, that only motion
1269                 // events are used, where the bottom press event was on
1270                 //  the drawing area too
1271                 if (!selection_possible) {
1272                         lyxerr[Debug::ACTION]
1273                                 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1274                         break;
1275                 }
1276
1277                 bv->screen().hideCursor();
1278
1279                 Row * cursorrow = bv->text->cursor.row();
1280                 bv->text->setCursorFromCoordinates(bv, cmd.x, cmd.y + bv->text->first_y);
1281         #if 0
1282                 // sorry for this but I have a strange error that the y value jumps at
1283                 // a certain point. This seems like an error in my xforms library or
1284                 // in some other local environment, but I would like to leave this here
1285                 // for the moment until I can remove this (Jug 20020418)
1286                 if (y_before < bv->text->cursor.y())
1287                         lyxerr << y_before << ":" << bv->text->cursor.y() << endl;
1288         #endif
1289                 // This is to allow jumping over large insets
1290                 if (cursorrow == bv->text->cursor.row()) {
1291                         if (cmd.y >= int(bv->workHeight()))
1292                                 bv->text->cursorDown(bv, false);
1293                         else if (cmd.y < 0)
1294                                 bv->text->cursorUp(bv, false);
1295                 }
1296
1297                 // Maybe an empty line was deleted
1298                 if (!bv->text->selection.set())
1299                         bv->update(bv->text, BufferView::UPDATE);
1300                 bv->text->setSelection(bv);
1301                 bv->screen().toggleToggle(bv->text, bv);
1302                 bv->fitCursor();
1303                 bv->showCursor();
1304                 break;
1305         }
1306
1307         // Single-click on work area
1308         case LFUN_MOUSE_PRESS:
1309         {
1310                 if (!bv->buffer())
1311                         break;
1312
1313                 // ok ok, this is a hack (for xforms)
1314                 // We shouldn't go further down as we really should only do the
1315                 // scrolling and be done with this. Otherwise we may open some
1316                 // dialogs (Jug 20020424).
1317                 if (cmd.button() == mouse_button::button4) {
1318                         bv->scroll(-lyxrc.wheel_jump);
1319                         break;
1320                 }
1321                 if (cmd.button() == mouse_button::button5) {
1322                         bv->scroll(lyxrc.wheel_jump);
1323                         break;
1324                 }
1325
1326                 int x = cmd.x;
1327                 int y = cmd.y;
1328                 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1329
1330                 // Middle button press pastes if we have a selection
1331                 // We do this here as if the selection was inside an inset
1332                 // it could get cleared on the unlocking of the inset so
1333                 // we have to check this first
1334                 bool paste_internally = false;
1335                 if (cmd.button() == mouse_button::button2 && selection.set()) {
1336                         bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1337                         paste_internally = true;
1338                 }
1339
1340                 int const screen_first = bv->text->first_y;
1341
1342                 if (bv->theLockingInset()) {
1343                         // We are in inset locking mode
1344
1345                         // Check whether the inset was hit. If not reset mode,
1346                         // otherwise give the event to the inset
1347                         if (inset_hit == bv->theLockingInset()) {
1348                                 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1349                                 bv->theLockingInset()->localDispatch(cmd1);
1350                                 break;
1351                         }
1352                         bv->unlockInset(bv->theLockingInset());
1353                 }
1354
1355                 if (!inset_hit)
1356                         selection_possible = true;
1357                 bv->screen().hideCursor();
1358
1359                 // Clear the selection
1360                 bv->screen().toggleSelection(bv->text, bv);
1361                 bv->text->clearSelection();
1362                 bv->text->fullRebreak(bv);
1363                 bv->update();
1364                 bv->updateScrollbar();
1365
1366                 // Single left click in math inset?
1367                 if (isHighlyEditableInset(inset_hit)) {
1368                         // Highly editable inset, like math
1369                         UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1370                         selection_possible = false;
1371                         bv->owner()->updateLayoutChoice();
1372                         bv->owner()->message(inset->editMessage());
1373                         //inset->edit(bv, x, y, cmd.button());
1374                         // We just have to lock the inset before calling a PressEvent on it!
1375                         // we don't need the edit() call here! (Jug20020329)
1376                         if (!bv->lockInset(inset))
1377                                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1378                         FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1379                         inset->localDispatch(cmd1);
1380                         break;
1381                 }
1382                 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1383
1384                 // Right click on a footnote flag opens float menu
1385                 if (cmd.button() == mouse_button::button3) {
1386                         selection_possible = false;
1387                         break;
1388                 }
1389
1390                 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1391                         bv->text->setCursorFromCoordinates(bv, x, y + screen_first);
1392                 finishUndo();
1393                 bv->text->selection.cursor = bv->text->cursor;
1394                 bv->text->cursor.x_fix(bv->text->cursor.x());
1395
1396                 bv->owner()->updateLayoutChoice();
1397                 if (bv->fitCursor())
1398                         selection_possible = false;
1399
1400                 // Insert primary selection with middle mouse
1401                 // if there is a local selection in the current buffer,
1402                 // insert this
1403                 if (cmd.button() == mouse_button::button2) {
1404                         if (paste_internally)
1405                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1406                         else
1407                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1408                         selection_possible = false;
1409                 }
1410                 break;
1411         }
1412
1413         case LFUN_MOUSE_RELEASE:
1414         {
1415                 // do nothing if we used the mouse wheel
1416                 if (!bv->buffer())
1417                         break;
1418         
1419                 if (cmd.button() == mouse_button::button4
1420                  || cmd.button() == mouse_button::button5)
1421                         break;
1422
1423                 // If we hit an inset, we have the inset coordinates in these
1424                 // and inset_hit points to the inset.  If we do not hit an
1425                 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1426                 int x = cmd.x;
1427                 int y = cmd.y;
1428                 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1429
1430                 if (bv->theLockingInset()) {
1431                         // We are in inset locking mode.
1432
1433                         // LyX does a kind of work-area grabbing for insets.
1434                         // Only a ButtonPress FuncRequest outside the inset will
1435                         // force a insetUnlock.
1436                         FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1437                         bv->theLockingInset()->localDispatch(cmd1);
1438                         break;
1439                 }
1440
1441                 selection_possible = false;
1442
1443                 if (cmd.button() == mouse_button::button2)
1444                         break;
1445
1446                 // finish selection
1447                 if (cmd.button() == mouse_button::button1)
1448                         bv->haveSelection(selection.set());
1449
1450                 bv->switchKeyMap();
1451                 bv->owner()->view_state_changed();
1452                 bv->owner()->updateMenubar();
1453                 bv->owner()->updateToolbar();
1454
1455                 // Did we hit an editable inset?
1456                 if (inset_hit) {
1457                         selection_possible = false;
1458
1459                         // if we reach this point with a selection, it
1460                         // must mean we are currently selecting.
1461                         // But we don't want to open the inset
1462                         // because that is annoying for the user.
1463                         // So just pretend we didn't hit it.
1464                         // this is OK because a "kosher" ButtonRelease
1465                         // will follow a ButtonPress that clears
1466                         // the selection.
1467                         // Note this also fixes selection drawing
1468                         // problems if we end up opening an inset
1469                         if (selection.set())
1470                                 break;
1471
1472                         // CHECK fix this proper in 0.13
1473                         // well, maybe 13.0 !!!!!!!!!
1474
1475                         // Following a ref shouldn't issue
1476                         // a push on the undo-stack
1477                         // anylonger, now that we have
1478                         // keybindings for following
1479                         // references and returning from
1480                         // references.  IMHO though, it
1481                         // should be the inset's own business
1482                         // to push or not push on the undo
1483                         // stack. They don't *have* to
1484                         // alter the document...
1485                         // (Joacim)
1486                         // ...or maybe the SetCursorParUndo()
1487                         // below isn't necessary at all anylonger?
1488                         if (inset_hit->lyxCode() == Inset::REF_CODE)
1489                                 setCursorParUndo(bv);
1490
1491                         bv->owner()->message(inset_hit->editMessage());
1492
1493                         if (isHighlyEditableInset(inset_hit)) {
1494                                 // Highly editable inset, like math
1495                                 UpdatableInset * inset = (UpdatableInset *) inset_hit;
1496                                 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1497                                 inset->localDispatch(cmd1);
1498                         } else {
1499                                 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1500                                 inset_hit->localDispatch(cmd1);
1501                                 // IMO this is a grosshack! Inset's should be changed so that
1502                                 // they call the actions they have to do with the insetButtonRel.
1503                                 // function and not in the edit(). This should be changed
1504                                 // (Jug 20020329)
1505 #ifdef WITH_WARNINGS
1506 #warning Please remove donot call inset->edit() here (Jug 20020812)
1507 #endif
1508                                 inset_hit->edit(bv, x, y, cmd.button());
1509                         }
1510                         break;
1511                 }
1512
1513                 // Maybe we want to edit a bibitem ale970302
1514                 if (bv->text->cursor.par()->bibkey) {
1515                         bool const is_rtl =
1516                                 bv->text->cursor.par()->isRightToLeftPar(bv->buffer()->params);
1517                         int const width =
1518                                 bibitemMaxWidth(bv, bv->buffer()->params.getLyXTextClass().defaultfont());
1519                         if ((is_rtl && x > bv->text->workWidth(bv)-20-width) ||
1520                                         (!is_rtl && x < 20 + width)) {
1521                                 bv->text->cursor.par()->bibkey->edit(bv, 0, 0, mouse_button::none);
1522                         }
1523                 }
1524                 break;
1525         }
1526
1527         case LFUN_SELFINSERT: {
1528                 if (cmd.argument.empty())
1529                         break;
1530
1531                 // Automatically delete the currently selected
1532                 // text and replace it with what is being
1533                 // typed in now. Depends on lyxrc settings
1534                 // "auto_region_delete", which defaults to
1535                 // true (on).
1536
1537                 if (lyxrc.auto_region_delete) {
1538                         if (selection.set()) {
1539                                 cutSelection(bv, false, false);
1540                                 update(bv);
1541                         }
1542                         bv->haveSelection(false);
1543                 }
1544
1545                 bv->beforeChange(this);
1546                 LyXFont const old_font(real_current_font);
1547
1548                 string::const_iterator cit = cmd.argument.begin();
1549                 string::const_iterator end = cmd.argument.end();
1550                 for (; cit != end; ++cit)
1551                         bv->owner()->getIntl().getTransManager().
1552                                 TranslateAndInsert(*cit, this);
1553
1554                 update(bv);
1555                 selection.cursor = cursor;
1556                 moveCursorUpdate(bv, false);
1557
1558                 // real_current_font.number can change so we need to
1559                 // update the minibuffer
1560                 if (old_font != real_current_font)
1561                         bv->owner()->view_state_changed();
1562                 break;
1563         }
1564
1565         case LFUN_HTMLURL: {
1566                 InsetCommandParams p("htmlurl");
1567                 bv->owner()->getDialogs().createUrl(p.getAsString());
1568                 break;
1569         }
1570
1571         case LFUN_URL: {
1572                 InsetCommandParams p("url");
1573                 bv->owner()->getDialogs().createUrl(p.getAsString());
1574                 break;
1575         }
1576
1577
1578 #if 0
1579         case LFUN_INSET_LIST:
1580         case LFUN_INSET_THEOREM:
1581 #endif
1582         case LFUN_INSERT_NOTE:
1583         case LFUN_INSERT_URL:
1584         case LFUN_INSET_CAPTION:
1585         case LFUN_INSET_ERT:
1586         case LFUN_INSET_EXTERNAL:
1587         case LFUN_INSET_FLOAT:
1588         case LFUN_INSET_FOOTNOTE:
1589         case LFUN_INSET_MARGINAL:
1590         case LFUN_INSET_MINIPAGE:
1591         case LFUN_INSET_OPTARG:
1592         case LFUN_INSET_WIDE_FLOAT:
1593         case LFUN_INSET_WRAP:
1594         case LFUN_TABULAR_INSERT:
1595         case LFUN_INDEX_INSERT:
1596         case LFUN_INDEX_PRINT: 
1597         case LFUN_PARENTINSERT:
1598         case LFUN_TOC_INSERT:
1599         {
1600                 Inset * inset = createInset(cmd);
1601                 if (inset) {
1602                         bool gotsel = false;
1603                         if (selection.set()) {
1604                                 cutSelection(bv, true, false);
1605                                 gotsel = true;
1606                         }
1607                         if (bv->insertInset(inset)) {
1608                                 inset->edit(bv);
1609                                 if (gotsel)
1610                                         bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION));
1611                         }
1612                         else
1613                                 delete inset;
1614                 }
1615                 break;
1616         }
1617
1618
1619         default:
1620                 return Inset::UNDISPATCHED;
1621         }
1622
1623         return Inset::DISPATCHED;
1624 }