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