]> git.lyx.org Git - lyx.git/blob - src/text3.C
Alfredo's patch.
[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/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 = 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, 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_HFILL:
953                 bv->hideCursor();
954                 update(bv, false);
955                 insertChar(bv, Paragraph::META_HFILL);
956                 update(bv);
957                 break;
958
959         case LFUN_END_OF_SENTENCE:
960                 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
961                 break;
962
963         case LFUN_MENU_SEPARATOR:
964                 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
965                 break;
966
967         case LFUN_MARK_OFF:
968                 bv->beforeChange(this);
969                 update(bv, false);
970                 selection.cursor = cursor;
971                 cmd.message(N_("Mark off"));
972                 break;
973
974         case LFUN_MARK_ON:
975                 bv->beforeChange(this);
976                 selection.mark(true);
977                 update(bv, false);
978                 selection.cursor = cursor;
979                 cmd.message(N_("Mark on"));
980                 break;
981
982         case LFUN_SETMARK:
983                 bv->beforeChange(this);
984                 if (selection.mark()) {
985                         update(bv);
986                         cmd.message(N_("Mark removed"));
987                 } else {
988                         selection.mark(true);
989                         update(bv);
990                         cmd.message(N_("Mark set"));
991                 }
992                 selection.cursor = cursor;
993                 break;
994
995         case LFUN_UPCASE_WORD:
996                 update(bv, false);
997                 changeCase(*bv, LyXText::text_uppercase);
998                 if (inset_owner)
999                         bv->updateInset(inset_owner, true);
1000                 update(bv);
1001                 break;
1002
1003         case LFUN_LOWCASE_WORD:
1004                 update(bv, false);
1005                 changeCase(*bv, LyXText::text_lowercase);
1006                 if (inset_owner)
1007                         bv->updateInset(inset_owner, true);
1008                 update(bv);
1009                 break;
1010
1011         case LFUN_CAPITALIZE_WORD:
1012                 update(bv, false);
1013                 changeCase(*bv, LyXText::text_capitalization);
1014                 if (inset_owner)
1015                         bv->updateInset(inset_owner, true);
1016                 update(bv);
1017                 break;
1018
1019         case LFUN_TRANSPOSE_CHARS:
1020                 update(bv, false);
1021                 transposeChars(*bv);
1022                 if (inset_owner)
1023                         bv->updateInset(inset_owner, true);
1024                 update(bv);
1025                 break;
1026
1027         case LFUN_PASTE:
1028                 cmd.message(_("Paste"));
1029                 bv->hideCursor();
1030                 // clear the selection
1031                 bv->toggleSelection();
1032                 clearSelection();
1033                 update(bv, false);
1034                 pasteSelection(bv);
1035                 clearSelection(); // bug 393
1036                 update(bv, false);
1037                 update(bv);
1038                 bv->switchKeyMap();
1039                 break;
1040
1041         case LFUN_CUT:
1042                 bv->hideCursor();
1043                 update(bv, false);
1044                 cutSelection(bv, true);
1045                 update(bv);
1046                 cmd.message(_("Cut"));
1047                 break;
1048
1049         case LFUN_COPY:
1050                 copySelection(bv);
1051                 cmd.message(_("Copy"));
1052                 break;
1053
1054         case LFUN_BEGINNINGBUFSEL:
1055                 if (inset_owner)
1056                         return UNDISPATCHED;
1057                 update(bv, false);
1058                 cursorTop(bv);
1059                 finishChange(bv, true);
1060                 break;
1061
1062         case LFUN_ENDBUFSEL:
1063                 if (inset_owner)
1064                         return UNDISPATCHED;
1065                 update(bv, false);
1066                 cursorBottom(bv);
1067                 finishChange(bv, true);
1068                 break;
1069
1070         case LFUN_GETXY:
1071                 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1072                 break;
1073
1074         case LFUN_SETXY: {
1075                 int x = 0;
1076                 int y = 0;
1077                 istringstream is(cmd.argument.c_str());
1078                 is >> x >> y;
1079                 if (!is)
1080                         lyxerr << "SETXY: Could not parse coordinates in '"
1081                                << cmd.argument << std::endl;
1082                 else
1083                         setCursorFromCoordinates(bv, x, y);
1084                 break;
1085         }
1086
1087         case LFUN_GETFONT:
1088                 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1089                         cmd.message("E");
1090                 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1091                         cmd.message("N");
1092                 else
1093                         cmd.message("0");
1094                 break;
1095
1096         case LFUN_GETLAYOUT:
1097                 cmd.message(tostr(cursor.par()->layout()));
1098                 break;
1099
1100         case LFUN_LAYOUT: {
1101                 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1102                   << cmd.argument << endl;
1103
1104                 // This is not the good solution to the empty argument
1105                 // problem, but it will hopefully suffice for 1.2.0.
1106                 // The correct solution would be to augument the
1107                 // function list/array with information about what
1108                 // functions needs arguments and their type.
1109                 if (cmd.argument.empty()) {
1110                         cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1111                         break;
1112                 }
1113
1114                 // Derive layout number from given argument (string)
1115                 // and current buffer's textclass (number)
1116                 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1117                 bool hasLayout = tclass.hasLayout(cmd.argument);
1118                 string layout = cmd.argument;
1119
1120                 // If the entry is obsolete, use the new one instead.
1121                 if (hasLayout) {
1122                         string const & obs = tclass[layout]->obsoleted_by();
1123                         if (!obs.empty())
1124                                 layout = obs;
1125                 }
1126
1127                 if (!hasLayout) {
1128                         cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1129                                 N_(" not known"));
1130                         break;
1131                 }
1132
1133                 bool change_layout = (current_layout != layout);
1134                 if (!change_layout && selection.set() &&
1135                         selection.start.par() != selection.end.par())
1136                 {
1137                         Paragraph * spar = selection.start.par();
1138                         Paragraph * epar = selection.end.par()->next();
1139                         while (spar != epar) {
1140                                 if (spar->layout()->name() != current_layout) {
1141                                         change_layout = true;
1142                                         break;
1143                                 }
1144                                 spar = spar->next();
1145                         }
1146                 }
1147                 if (change_layout) {
1148                         bv->hideCursor();
1149                         current_layout = layout;
1150                         update(bv, false);
1151                         setLayout(bv, layout);
1152                         bv->owner()->setLayout(layout);
1153                         update(bv);
1154                         bv->switchKeyMap();
1155                 }
1156                 break;
1157         }
1158
1159         case LFUN_PASTESELECTION: {
1160                 if (!bv->buffer())
1161                         break;
1162                 bv->hideCursor();
1163                 // this was originally a beforeChange(bv->text), i.e
1164                 // the outermost LyXText!
1165                 bv->beforeChange(this);
1166                 string const clip = bv->getClipboard();
1167                 if (!clip.empty()) {
1168                         if (cmd.argument == "paragraph")
1169                                 insertStringAsParagraphs(bv, clip);
1170                         else
1171                                 insertStringAsLines(bv, clip);
1172                         clearSelection();
1173                         update(bv);
1174                 }
1175                 break;
1176         }
1177
1178         case LFUN_GOTOERROR:
1179                 gotoInset(bv, Inset::ERROR_CODE, false);
1180                 break;
1181
1182         case LFUN_GOTONOTE:
1183                 gotoInset(bv, Inset::NOTE_CODE, false);
1184                 break;
1185
1186         case LFUN_REFERENCE_GOTO:
1187         {
1188                 vector<Inset::Code> tmp;
1189                 tmp.push_back(Inset::LABEL_CODE);
1190                 tmp.push_back(Inset::REF_CODE);
1191                 gotoInset(bv, tmp, true);
1192                 break;
1193         }
1194
1195         case LFUN_QUOTE: {
1196                 Paragraph const * par = cursor.par();
1197                 lyx::pos_type pos = cursor.pos();
1198                 char c;
1199                 if (!pos)
1200                         c = ' ';
1201                 else if (par->isInset(pos - 1) && par->getInset(pos - 1)->isSpace())
1202                         c = ' ';
1203                 else
1204                         c = par->getChar(pos - 1);
1205
1206                 bv->hideCursor();
1207                 LyXLayout_ptr const & style = par->layout();
1208
1209                 if (style->pass_thru ||
1210                                 par->getFontSettings(bv->buffer()->params,
1211                                          pos).language()->lang() == "hebrew" ||
1212                         (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1213                         bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1214                 break;
1215         }
1216
1217         case LFUN_DATE_INSERT: {
1218                 time_t now_time_t = time(NULL);
1219                 struct tm * now_tm = localtime(&now_time_t);
1220                 setlocale(LC_TIME, "");
1221                 string arg;
1222                 if (!cmd.argument.empty())
1223                         arg = cmd.argument;
1224                 else
1225                         arg = lyxrc.date_insert_format;
1226                 char datetmp[32];
1227                 int const datetmp_len =
1228                         ::strftime(datetmp, 32, arg.c_str(), now_tm);
1229
1230                 for (int i = 0; i < datetmp_len; i++) {
1231                         insertChar(bv, datetmp[i]);
1232                         update(bv, true);
1233                 }
1234                 selection.cursor = cursor;
1235                 moveCursorUpdate(bv, false);
1236                 break;
1237         }
1238
1239         case LFUN_MOUSE_TRIPLE:
1240                 if (!bv->buffer())
1241                         break;
1242                 if (isTopLevel() && bv->theLockingInset())
1243                         break;
1244                 if (cmd.button() == mouse_button::button1) {
1245                         if (isTopLevel()) {
1246                                 bv->screen().hideCursor();
1247                                 bv->screen().toggleSelection(this, bv);
1248                         }
1249                         cursorHome(bv);
1250                         selection.cursor = cursor;
1251                         cursorEnd(bv);
1252                         setSelection(bv);
1253                         if (isTopLevel())
1254                                 bv->screen().toggleSelection(this, bv, false);
1255                         update(bv, false);
1256                         bv->haveSelection(selection.set());
1257                 }
1258                 break;
1259
1260         case LFUN_MOUSE_DOUBLE:
1261                 if (!bv->buffer())
1262                         break;
1263                 if (isTopLevel() && bv->theLockingInset())
1264                         break;
1265                 if (cmd.button() == mouse_button::button1) {
1266                         if (isTopLevel()) {
1267                                 bv->screen().hideCursor();
1268                                 bv->screen().toggleSelection(this, bv);
1269                                 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1270                                 bv->screen().toggleSelection(this, bv, false);
1271                         } else {
1272                                 selectWord(bv, LyXText::WHOLE_WORD_STRICT);
1273                         }
1274                         update(bv, false);
1275                         bv->haveSelection(selection.set());
1276                 }
1277                 break;
1278
1279         case LFUN_MOUSE_MOTION:
1280         {
1281                 // Only use motion with button 1
1282                 //if (ev.button() != mouse_button::button1)
1283                 //      return false;
1284
1285                 if (!bv->buffer())
1286                         break;
1287
1288                 // Check for inset locking
1289                 if (bv->theLockingInset()) {
1290                         Inset * tli = bv->theLockingInset();
1291                         LyXCursor cursor = bv->text->cursor;
1292                         LyXFont font = bv->text->getFont(bv->buffer(),
1293                                                                 cursor.par(), cursor.pos());
1294                         int width = tli->width(bv, font);
1295                         int inset_x = font.isVisibleRightToLeft()
1296                                 ? cursor.ix() - width : cursor.ix();
1297                         int start_x = inset_x + tli->scroll();
1298                         FuncRequest cmd1 = cmd;
1299                         cmd1.x = cmd.x - start_x;
1300                         cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1301                         tli->localDispatch(cmd1);
1302                         break;
1303                 }
1304
1305                 // The test for not selection possible is needed, that only motion
1306                 // events are used, where the bottom press event was on
1307                 //  the drawing area too
1308                 if (!selection_possible) {
1309                         lyxerr[Debug::ACTION]
1310                                 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1311                         break;
1312                 }
1313
1314                 bv->screen().hideCursor();
1315
1316                 Row * cursorrow = bv->text->cursor.row();
1317                 bv->text->setCursorFromCoordinates(bv, cmd.x, cmd.y + bv->text->top_y());
1318         #if 0
1319                 // sorry for this but I have a strange error that the y value jumps at
1320                 // a certain point. This seems like an error in my xforms library or
1321                 // in some other local environment, but I would like to leave this here
1322                 // for the moment until I can remove this (Jug 20020418)
1323                 if (y_before < bv->text->cursor.y())
1324                         lyxerr << y_before << ':'
1325                                << bv->text->cursor.y() << endl;
1326         #endif
1327                 // This is to allow jumping over large insets
1328                 if (cursorrow == bv->text->cursor.row()) {
1329                         if (cmd.y >= bv->workHeight())
1330                                 bv->text->cursorDown(bv, false);
1331                         else if (cmd.y < 0)
1332                                 bv->text->cursorUp(bv, false);
1333                 }
1334
1335                 // Maybe an empty line was deleted
1336                 if (!bv->text->selection.set())
1337                         bv->update(bv->text, BufferView::UPDATE);
1338                 bv->text->setSelection(bv);
1339                 bv->screen().toggleToggle(bv->text, bv);
1340                 bv->fitCursor();
1341                 bv->showCursor();
1342                 break;
1343         }
1344
1345         // Single-click on work area
1346         case LFUN_MOUSE_PRESS:
1347         {
1348                 if (!bv->buffer())
1349                         break;
1350
1351                 // ok ok, this is a hack (for xforms)
1352                 // We shouldn't go further down as we really should only do the
1353                 // scrolling and be done with this. Otherwise we may open some
1354                 // dialogs (Jug 20020424).
1355                 if (cmd.button() == mouse_button::button4) {
1356                         bv->scroll(-lyxrc.wheel_jump);
1357                         break;
1358                 }
1359                 if (cmd.button() == mouse_button::button5) {
1360                         bv->scroll(lyxrc.wheel_jump);
1361                         break;
1362                 }
1363
1364                 int x = cmd.x;
1365                 int y = cmd.y;
1366                 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1367
1368                 // Middle button press pastes if we have a selection
1369                 // We do this here as if the selection was inside an inset
1370                 // it could get cleared on the unlocking of the inset so
1371                 // we have to check this first
1372                 bool paste_internally = false;
1373                 if (cmd.button() == mouse_button::button2 && selection.set()) {
1374                         bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1375                         paste_internally = true;
1376                 }
1377
1378                 int const screen_first = bv->text->top_y();
1379
1380                 if (bv->theLockingInset()) {
1381                         // We are in inset locking mode
1382
1383                         // Check whether the inset was hit. If not reset mode,
1384                         // otherwise give the event to the inset
1385                         if (inset_hit == bv->theLockingInset()) {
1386                                 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1387                                 bv->theLockingInset()->localDispatch(cmd1);
1388                                 break;
1389                         }
1390                         bv->unlockInset(bv->theLockingInset());
1391                 }
1392
1393                 if (!inset_hit)
1394                         selection_possible = true;
1395                 bv->screen().hideCursor();
1396
1397                 // Clear the selection
1398                 bv->screen().toggleSelection(bv->text, bv);
1399                 bv->text->clearSelection();
1400                 bv->text->fullRebreak(bv);
1401                 bv->update();
1402                 bv->updateScrollbar();
1403
1404                 // Single left click in math inset?
1405                 if (isHighlyEditableInset(inset_hit)) {
1406                         // Highly editable inset, like math
1407                         UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1408                         selection_possible = false;
1409                         bv->owner()->updateLayoutChoice();
1410                         bv->owner()->message(inset->editMessage());
1411                         //inset->edit(bv, x, y, cmd.button());
1412                         // We just have to lock the inset before calling a PressEvent on it!
1413                         // we don't need the edit() call here! (Jug20020329)
1414                         if (!bv->lockInset(inset))
1415                                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1416                         FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1417                         inset->localDispatch(cmd1);
1418                         break;
1419                 }
1420                 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1421
1422                 // Right click on a footnote flag opens float menu
1423                 if (cmd.button() == mouse_button::button3) {
1424                         selection_possible = false;
1425                         break;
1426                 }
1427
1428                 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1429                         bv->text->setCursorFromCoordinates(bv, x, y + screen_first);
1430                 finishUndo();
1431                 bv->text->selection.cursor = bv->text->cursor;
1432                 bv->text->cursor.x_fix(bv->text->cursor.x());
1433
1434                 bv->owner()->updateLayoutChoice();
1435                 if (bv->fitCursor())
1436                         selection_possible = false;
1437
1438                 // Insert primary selection with middle mouse
1439                 // if there is a local selection in the current buffer,
1440                 // insert this
1441                 if (cmd.button() == mouse_button::button2) {
1442                         if (paste_internally)
1443                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1444                         else
1445                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1446                         selection_possible = false;
1447                 }
1448                 break;
1449         }
1450
1451         case LFUN_MOUSE_RELEASE:
1452         {
1453                 // do nothing if we used the mouse wheel
1454                 if (!bv->buffer())
1455                         break;
1456
1457                 if (cmd.button() == mouse_button::button4
1458                  || cmd.button() == mouse_button::button5)
1459                         break;
1460
1461                 // If we hit an inset, we have the inset coordinates in these
1462                 // and inset_hit points to the inset.  If we do not hit an
1463                 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1464                 int x = cmd.x;
1465                 int y = cmd.y;
1466                 Inset * inset_hit = bv->text->checkInsetHit(bv, x, y);
1467
1468                 if (bv->theLockingInset()) {
1469                         // We are in inset locking mode.
1470
1471                         // LyX does a kind of work-area grabbing for insets.
1472                         // Only a ButtonPress FuncRequest outside the inset will
1473                         // force a insetUnlock.
1474                         FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1475                         bv->theLockingInset()->localDispatch(cmd1);
1476                         break;
1477                 }
1478
1479                 selection_possible = false;
1480
1481                 if (cmd.button() == mouse_button::button2)
1482                         break;
1483
1484                 // finish selection
1485                 if (cmd.button() == mouse_button::button1)
1486                         bv->haveSelection(selection.set());
1487
1488                 bv->switchKeyMap();
1489                 bv->owner()->view_state_changed();
1490                 bv->owner()->updateMenubar();
1491                 bv->owner()->updateToolbar();
1492
1493                 // Did we hit an editable inset?
1494                 if (inset_hit) {
1495                         selection_possible = false;
1496
1497                         // if we reach this point with a selection, it
1498                         // must mean we are currently selecting.
1499                         // But we don't want to open the inset
1500                         // because that is annoying for the user.
1501                         // So just pretend we didn't hit it.
1502                         // this is OK because a "kosher" ButtonRelease
1503                         // will follow a ButtonPress that clears
1504                         // the selection.
1505                         // Note this also fixes selection drawing
1506                         // problems if we end up opening an inset
1507                         if (selection.set())
1508                                 break;
1509
1510                         // CHECK fix this proper in 0.13
1511                         // well, maybe 13.0 !!!!!!!!!
1512
1513                         // Following a ref shouldn't issue
1514                         // a push on the undo-stack
1515                         // anylonger, now that we have
1516                         // keybindings for following
1517                         // references and returning from
1518                         // references.  IMHO though, it
1519                         // should be the inset's own business
1520                         // to push or not push on the undo
1521                         // stack. They don't *have* to
1522                         // alter the document...
1523                         // (Joacim)
1524                         // ...or maybe the SetCursorParUndo()
1525                         // below isn't necessary at all anylonger?
1526                         if (inset_hit->lyxCode() == Inset::REF_CODE)
1527                                 setCursorParUndo(bv);
1528
1529                         bv->owner()->message(inset_hit->editMessage());
1530
1531                         if (isHighlyEditableInset(inset_hit)) {
1532                                 // Highly editable inset, like math
1533                                 UpdatableInset * inset = (UpdatableInset *) inset_hit;
1534                                 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1535                                 inset->localDispatch(cmd1);
1536                         } else {
1537                                 FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1538                                 inset_hit->localDispatch(cmd1);
1539                                 // IMO this is a gross hack! Insets should be changed so that
1540                                 // they call the actions they have to do with the insetButtonRel.
1541                                 // function and not in the edit(). This should be changed
1542                                 // (Jug 20020329)
1543 #ifdef WITH_WARNINGS
1544 #warning Please remove donot call inset->edit() here (Jug 20020812)
1545 #endif
1546                                 inset_hit->edit(bv, x, y, cmd.button());
1547                         }
1548                         break;
1549                 }
1550
1551                 break;
1552         }
1553
1554         case LFUN_SELFINSERT: {
1555                 if (cmd.argument.empty())
1556                         break;
1557
1558                 // Automatically delete the currently selected
1559                 // text and replace it with what is being
1560                 // typed in now. Depends on lyxrc settings
1561                 // "auto_region_delete", which defaults to
1562                 // true (on).
1563
1564                 if (lyxrc.auto_region_delete) {
1565                         if (selection.set()) {
1566                                 cutSelection(bv, false, false);
1567                                 update(bv);
1568                         }
1569                         bv->haveSelection(false);
1570                 }
1571
1572                 bv->beforeChange(this);
1573                 LyXFont const old_font(real_current_font);
1574
1575                 string::const_iterator cit = cmd.argument.begin();
1576                 string::const_iterator end = cmd.argument.end();
1577                 for (; cit != end; ++cit)
1578                         bv->owner()->getIntl().getTransManager().
1579                                 TranslateAndInsert(*cit, this);
1580
1581                 update(bv);
1582                 selection.cursor = cursor;
1583                 moveCursorUpdate(bv, false);
1584
1585                 // real_current_font.number can change so we need to
1586                 // update the minibuffer
1587                 if (old_font != real_current_font)
1588                         bv->owner()->view_state_changed();
1589                 break;
1590         }
1591
1592         case LFUN_HTMLURL: {
1593                 InsetCommandParams p("htmlurl");
1594                 string const data = InsetCommandMailer::params2string("url", p);
1595                 bv->owner()->getDialogs().show("url", data, 0);
1596                 break;
1597         }
1598
1599         case LFUN_URL: {
1600                 InsetCommandParams p("url");
1601                 string const data = InsetCommandMailer::params2string("url", p);
1602                 bv->owner()->getDialogs().show("url", data, 0);
1603                 break;
1604         }
1605
1606
1607 #if 0
1608         case LFUN_INSET_LIST:
1609         case LFUN_INSET_THEOREM:
1610         case LFUN_INSET_CAPTION:
1611 #endif
1612         case LFUN_INSERT_NOTE:
1613         case LFUN_INSERT_BIBITEM:
1614         case LFUN_INSET_ERT:
1615         case LFUN_INSET_FLOAT:
1616         case LFUN_INSET_FOOTNOTE:
1617         case LFUN_INSET_MARGINAL:
1618         case LFUN_INSET_MINIPAGE:
1619         case LFUN_INSET_OPTARG:
1620         case LFUN_INSET_WIDE_FLOAT:
1621         case LFUN_INSET_WRAP:
1622         case LFUN_TABULAR_INSERT:
1623                 // Open the inset, and move the current selection
1624                 // inside it.
1625                 doInsertInset(this, cmd, true, true);
1626                 break;
1627
1628         case LFUN_INDEX_INSERT:
1629                 // Just open the inset
1630                 doInsertInset(this, cmd, true, false);
1631                 break;
1632
1633         case LFUN_INDEX_PRINT:
1634         case LFUN_PARENTINSERT:
1635         case LFUN_TOC_INSERT:
1636                 // do nothing fancy
1637                 doInsertInset(this, cmd, false, false);
1638                 break;
1639
1640         default:
1641                 return UNDISPATCHED;
1642         }
1643
1644         return DISPATCHED;
1645 }