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