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