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