]> git.lyx.org Git - lyx.git/blob - src/BufferView_pimpl.C
a6f510b7e2279b96d1d0ae1e54a8c6ce4235153b
[lyx.git] / src / BufferView_pimpl.C
1 #include <config.h>
2
3 #include <ctime>
4 #include <unistd.h>
5 #include <sys/wait.h>
6 #include <locale.h>
7
8 #ifdef __GNUG__
9 #pragma implementation
10 #endif
11
12 #include "BufferView_pimpl.h"
13 #include "WorkArea.h"
14 #include "lyxscreen.h"
15 #include "lyxtext.h"
16 #include "lyxrow.h"
17 #include "LyXView.h"
18 #include "commandtags.h"
19 #include "font.h"
20 #include "bufferview_funcs.h"
21 #include "TextCache.h"
22 #include "bufferlist.h"
23 #include "lyx_gui_misc.h"
24 #include "lyxrc.h"
25 #include "intl.h"
26 #include "support/LAssert.h"
27 #include "frontends/Dialogs.h"
28 #include "insets/insetbib.h"
29 #include "insets/insettext.h"
30 /// added for Dispatch functions
31 #include "lyx_cb.h"
32 #include "frontends/FileDialog.h"
33 #include "lyx_main.h"
34 #include "FloatList.h"
35 #include "support/filetools.h"
36 #include "support/lyxfunctional.h"
37 #include "insets/inseturl.h"
38 #include "insets/insetlatexaccent.h"
39 #include "insets/insettoc.h"
40 #include "insets/insetref.h"
41 #include "insets/insetparent.h"
42 #include "insets/insetindex.h"
43 #include "insets/insetinclude.h"
44 #include "insets/insetcite.h"
45 #include "insets/insetert.h"
46 #include "insets/insetexternal.h"
47 #include "insets/insetgraphics.h"
48 #include "insets/insetfoot.h"
49 #include "insets/insetmarginal.h"
50 #include "insets/insetminipage.h"
51 #include "insets/insetfloat.h"
52 #include "insets/insetlist.h"
53 #include "insets/insettabular.h"
54 #include "insets/insettheorem.h"
55 #include "insets/insetcaption.h"
56 #include "mathed/formulamacro.h"
57 #include "gettext.h"
58
59 extern LyXTextClass::size_type current_layout;
60 extern int greek_kb_flag;
61
62 using std::vector;
63 using std::find_if;
64 using std::pair;
65 using std::endl;
66 using std::make_pair;
67 using std::min;
68 using SigC::slot;
69
70 /* the selection possible is needed, that only motion events are 
71  * used, where the bottom press event was on the drawing area too */
72 bool selection_possible = false;
73
74 extern BufferList bufferlist;
75 extern char ascii_type;
76
77 extern bool math_insert_greek(BufferView *, char);
78 extern void sigchldhandler(pid_t pid, int * status);
79 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
80
81 const unsigned int saved_positions_num = 20;
82
83 namespace {
84
85 inline
86 void waitForX()
87 {
88         XSync(fl_get_display(), 0);
89 }
90
91
92 void SetXtermCursor(Window win)
93 {
94         static Cursor cursor;
95         static bool cursor_undefined = true;
96         if (cursor_undefined){
97                 cursor = XCreateFontCursor(fl_get_display(), XC_xterm);
98                 XFlush(fl_get_display());
99                 cursor_undefined = false;
100         }
101         XDefineCursor(fl_get_display(), win, cursor);
102         XFlush(fl_get_display());
103 }
104
105 } // anon namespace
106
107
108 BufferView::Pimpl::Pimpl(BufferView * b, LyXView * o,
109              int xpos, int ypos, int width, int height)
110         : bv_(b), owner_(o), buffer_(0),
111           current_scrollbar_value(0), cursor_timeout(400),
112           workarea_(xpos, ypos, width, height), using_xterm_cursor(false)
113 {
114         // Setup the signals
115         workarea_.scrollCB.connect(slot(this, &BufferView::Pimpl::scrollCB));
116         workarea_.workAreaExpose
117                 .connect(slot(this, &BufferView::Pimpl::workAreaExpose));
118         workarea_.workAreaEnter
119                 .connect(slot(this, &BufferView::Pimpl::enterView));
120         workarea_.workAreaLeave
121                 .connect(slot(this, &BufferView::Pimpl::leaveView));
122         workarea_.workAreaButtonPress
123                 .connect(slot(this, &BufferView::Pimpl::workAreaButtonPress));
124         workarea_.workAreaButtonRelease
125                 .connect(slot(this,
126                               &BufferView::Pimpl::workAreaButtonRelease));
127         workarea_.workAreaMotionNotify
128                 .connect(slot(this, &BufferView::Pimpl::workAreaMotionNotify));
129         workarea_.workAreaDoubleClick
130                 .connect(slot(this, &BufferView::Pimpl::doubleClick));
131         workarea_.workAreaTripleClick
132                 .connect(slot(this, &BufferView::Pimpl::tripleClick));
133         workarea_.workAreaKeyPress
134                 .connect(slot(this, &BufferView::Pimpl::workAreaKeyPress));
135         
136         //screen_ = 0;
137
138         cursor_timeout.timeout.connect(slot(this,
139                                             &BufferView::Pimpl::cursorToggle));
140         //current_scrollbar_value = 0;
141         cursor_timeout.start();
142         workarea_.setFocus();
143         //using_xterm_cursor = false;
144         saved_positions.resize(saved_positions_num);
145 }
146
147
148 Painter & BufferView::Pimpl::painter() 
149 {
150         return workarea_.getPainter();
151 }
152
153
154 void BufferView::Pimpl::buffer(Buffer * b)
155 {
156         lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
157                             << b << ")" << endl;
158         if (buffer_) {
159                 bv_->insetSleep();
160                 buffer_->delUser(bv_);
161
162                 // Put the old text into the TextCache, but
163                 // only if the buffer is still loaded.
164                 // Also set the owner of the test to 0
165                 //              bv_->text->owner(0);
166                 textcache.add(buffer_, workarea_.workWidth(), bv_->text);
167                 if (lyxerr.debugging())
168                         textcache.show(lyxerr, "BufferView::buffer");
169                 
170                 bv_->text = 0;
171         }
172
173         // Set current buffer
174         buffer_ = b;
175
176         if (bufferlist.getState() == BufferList::CLOSING) return;
177         
178         // Nuke old image
179         // screen is always deleted when the buffer is changed.
180         screen_.reset(0);
181         //delete screen_;
182         //screen_ = 0;
183
184         // If we are closing the buffer, use the first buffer as current
185         if (!buffer_) {
186                 buffer_ = bufferlist.first();
187         }
188
189         if (buffer_) {
190                 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
191                 buffer_->addUser(bv_);
192                 // If we don't have a text object for this, we make one
193                 if (bv_->text == 0) {
194                         resizeCurrentBuffer();
195                 } else {
196                         updateScreen();
197                         updateScrollbar();
198                 }
199                 bv_->text->first = screen_->TopCursorVisible(bv_->text);
200                 owner_->updateMenubar();
201                 owner_->updateToolbar();
202                 // Similarly, buffer-dependent dialogs should be updated or
203                 // hidden. This should go here because some dialogs (eg ToC)
204                 // require bv_->text.
205                 owner_->getDialogs()->updateBufferDependent(true);
206                 redraw();
207                 bv_->insetWakeup();
208         } else {
209                 lyxerr[Debug::INFO] << "  No Buffer!" << endl;
210                 owner_->updateMenubar();
211                 owner_->updateToolbar();
212                 owner_->getDialogs()->hideBufferDependent();
213                 updateScrollbar();
214                 workarea_.redraw();
215
216                 // Also remove all remaining text's from the testcache.
217                 // (there should not be any!) (if there is any it is a
218                 // bug!)
219                 if (lyxerr.debugging())
220                         textcache.show(lyxerr, "buffer delete all");
221                 textcache.clear();
222         }
223         // should update layoutchoice even if we don't have a buffer.
224         owner_->updateLayoutChoice();
225
226         owner_->updateWindowTitle();
227 }
228
229
230 void BufferView::Pimpl::resize(int xpos, int ypos, int width, int height)
231 {
232         workarea_.resize(xpos, ypos, width, height);
233         update(bv_->text, SELECT);
234         redraw();
235 }
236
237
238 void BufferView::Pimpl::resize()
239 {
240         if (buffer_)
241                 resizeCurrentBuffer();
242 }
243
244
245 void BufferView::Pimpl::redraw()
246 {
247         lyxerr[Debug::INFO] << "BufferView::redraw()" << endl;
248         workarea_.redraw();
249 }
250
251
252 bool BufferView::Pimpl::fitCursor(LyXText * text)
253 {
254         lyx::Assert(screen_.get());
255  
256         bool ret = screen_->FitCursor(text, bv_);
257         if (ret)
258             updateScrollbar();
259         return ret;
260 }
261
262
263 void BufferView::Pimpl::redoCurrentBuffer()
264 {
265         lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
266         if (buffer_ && bv_->text) {
267                 resize();
268                 owner_->updateLayoutChoice();
269         }
270 }
271
272
273 int BufferView::Pimpl::resizeCurrentBuffer()
274 {
275         lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
276         
277         LyXParagraph * par = 0;
278         LyXParagraph * selstartpar = 0;
279         LyXParagraph * selendpar = 0;
280         UpdatableInset * the_locking_inset = 0;
281         
282         LyXParagraph::size_type pos = 0;
283         LyXParagraph::size_type selstartpos = 0;
284         LyXParagraph::size_type selendpos = 0;
285         bool selection = false;
286         bool mark_set  = false;
287
288         ProhibitInput(bv_);
289
290         owner_->message(_("Formatting document..."));
291
292         if (bv_->text) {
293                 par = bv_->text->cursor.par();
294                 pos = bv_->text->cursor.pos();
295                 selstartpar = bv_->text->sel_start_cursor.par();
296                 selstartpos = bv_->text->sel_start_cursor.pos();
297                 selendpar = bv_->text->sel_end_cursor.par();
298                 selendpos = bv_->text->sel_end_cursor.pos();
299                 selection = bv_->text->selection;
300                 mark_set = bv_->text->mark_set;
301                 the_locking_inset = bv_->text->the_locking_inset;
302                 delete bv_->text;
303                 bv_->text = new LyXText(bv_);
304         } else {
305                 // See if we have a text in TextCache that fits
306                 // the new buffer_ with the correct width.
307                 bv_->text = textcache.findFit(buffer_, workarea_.workWidth());
308                 if (bv_->text) {
309                         if (lyxerr.debugging()) {
310                                 lyxerr << "Found a LyXText that fits:\n";
311                                 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea_.workWidth(), bv_->text)));
312                         }
313                         // Set the owner of the newly found text
314                         //      bv_->text->owner(bv_);
315                         if (lyxerr.debugging())
316                                 textcache.show(lyxerr, "resizeCurrentBuffer");
317                 } else {
318                         bv_->text = new LyXText(bv_);
319                 }
320         }
321         updateScreen();
322
323         if (par) {
324                 bv_->text->selection = true;
325                 /* at this point just to avoid the Delete-Empty-Paragraph
326                  * Mechanism when setting the cursor */
327                 bv_->text->mark_set = mark_set;
328                 if (selection) {
329                         bv_->text->SetCursor(bv_, selstartpar, selstartpos);
330                         bv_->text->sel_cursor = bv_->text->cursor;
331                         bv_->text->SetCursor(bv_, selendpar, selendpos);
332                         bv_->text->SetSelection(bv_);
333                         bv_->text->SetCursor(bv_, par, pos);
334                 } else {
335                         bv_->text->SetCursor(bv_, par, pos);
336                         bv_->text->sel_cursor = bv_->text->cursor;
337                         bv_->text->selection = false;
338                 }
339                 // remake the inset locking
340                 bv_->text->the_locking_inset = the_locking_inset;
341         }
342         bv_->text->first = screen_->TopCursorVisible(bv_->text);
343         buffer_->resizeInsets(bv_);
344         // this will scroll the screen such that the cursor becomes visible
345         updateScrollbar();
346         redraw();
347
348         bv_->setState();
349         AllowInput(bv_);
350
351         /// get rid of the splash screen if it's not gone already
352         owner_->getDialogs()->destroySplash();
353  
354         return 0;
355 }
356
357
358 void BufferView::Pimpl::updateScreen()
359 {
360         // Regenerate the screen.
361         screen_.reset(new LyXScreen(workarea_));
362 }
363
364
365 void BufferView::Pimpl::updateScrollbar()
366 {
367         /* If the text is smaller than the working area, the scrollbar
368          * maximum must be the working area height. No scrolling will 
369          * be possible */
370
371         if (!buffer_) {
372                 workarea_.setScrollbar(0, 1.0);
373                 return;
374         }
375         
376         static unsigned long max2 = 0;
377         static unsigned long height2 = 0;
378
379         unsigned long cbth = 0;
380         long cbsf = 0;
381
382         if (bv_->text) {
383                 cbth = bv_->text->height;
384                 cbsf = bv_->text->first;
385         }
386
387         // check if anything has changed.
388         if (max2 == cbth &&
389             height2 == workarea_.height() &&
390             current_scrollbar_value == cbsf)
391                 return; // no
392         max2 = cbth;
393         height2 = workarea_.height();
394         current_scrollbar_value = cbsf;
395
396         if (cbth <= height2) { // text is smaller than screen
397                 workarea_.setScrollbar(0, 1.0); // right?
398                 return;
399         }
400
401         long maximum_height = workarea_.height() * 3 / 4 + cbth;
402         long value = cbsf;
403
404         // set the scrollbar
405         double hfloat = workarea_.height();
406         double maxfloat = maximum_height;
407
408         float slider_size = 0.0;
409         int slider_value = value;
410
411         workarea_.setScrollbarBounds(0, bv_->text->height - workarea_.height());
412         double const lineh = bv_->text->DefaultHeight();
413         workarea_.setScrollbarIncrements(lineh);
414         if (maxfloat > 0.0) {
415                 if ((hfloat / maxfloat) * float(height2) < 3)
416                         slider_size = 3.0/float(height2);
417                 else
418                         slider_size = hfloat / maxfloat;
419         } else
420                 slider_size = hfloat;
421
422         workarea_.setScrollbar(slider_value, slider_size / workarea_.height());
423 }
424
425
426 // Callback for scrollbar slider
427 void BufferView::Pimpl::scrollCB(double value)
428 {
429         if (!buffer_) return;
430
431         current_scrollbar_value = long(value);
432         if (current_scrollbar_value < 0)
433                 current_scrollbar_value = 0;
434    
435         if (!screen_.get())
436                 return;
437
438         screen_->Draw(bv_->text, bv_, current_scrollbar_value);
439
440         if (!lyxrc.cursor_follows_scrollbar) {
441                 waitForX();
442                 return;
443         }
444  
445         LyXText * vbt = bv_->text;
446  
447         int const height = vbt->DefaultHeight();
448         int const first = static_cast<int>((bv_->text->first + height));
449         int const last = static_cast<int>((bv_->text->first + workarea_.height() - height));
450
451         if (vbt->cursor.y() < first)
452                 vbt->SetCursorFromCoordinates(bv_, 0, first);
453         else if (vbt->cursor.y() > last)
454                 vbt->SetCursorFromCoordinates(bv_, 0, last);
455
456         waitForX();
457 }
458
459
460 int BufferView::Pimpl::scrollUp(long time)
461 {
462         if (!buffer_) return 0;
463         if (!screen_.get()) return 0;
464    
465         double value = workarea_.getScrollbarValue();
466    
467         if (value == 0) return 0;
468
469         float add_value =  (bv_->text->DefaultHeight()
470                             + float(time) * float(time) * 0.125);
471    
472         if (add_value > workarea_.height())
473                 add_value = float(workarea_.height() -
474                                   bv_->text->DefaultHeight());
475    
476         value -= add_value;
477
478         if (value < 0)
479                 value = 0;
480    
481         workarea_.setScrollbarValue(value);
482    
483         scrollCB(value); 
484         return 0;
485 }
486
487
488 int BufferView::Pimpl::scrollDown(long time)
489 {
490         if (!buffer_) return 0;
491         if (!screen_.get()) return 0;
492    
493         double value = workarea_.getScrollbarValue();
494         pair<float, float> p = workarea_.getScrollbarBounds();
495         double max = p.second;
496         
497         if (value == max) return 0;
498
499         float add_value =  (bv_->text->DefaultHeight()
500                             + float(time) * float(time) * 0.125);
501    
502         if (add_value > workarea_.height())
503                 add_value = float(workarea_.height() -
504                                   bv_->text->DefaultHeight());
505    
506         value += add_value;
507    
508         if (value > max)
509                 value = max;
510
511         workarea_.setScrollbarValue(value);
512         
513         scrollCB(value); 
514         return 0;
515 }
516
517
518 void BufferView::Pimpl::workAreaKeyPress(KeySym keysym, unsigned int state)
519 {
520         bv_->owner()->getLyXFunc()->processKeySym(keysym, state);
521 }
522
523
524 void BufferView::Pimpl::workAreaMotionNotify(int x, int y, unsigned int state)
525 {
526         // Only use motion with button 1
527         if (!(state & Button1MotionMask))
528                 return;
529
530         if (!buffer_ || !screen_.get()) return;
531
532         // Check for inset locking
533         if (bv_->theLockingInset()) {
534                 LyXCursor cursor = bv_->text->cursor;
535                 LyXFont font = bv_->text->GetFont(bv_->buffer(),
536                                                   cursor.par(), cursor.pos());
537                 int width = bv_->theLockingInset()->width(bv_, font);
538                 int inset_x = font.isVisibleRightToLeft()
539                         ? cursor.x() - width : cursor.x();
540                 int start_x = inset_x + bv_->theLockingInset()->scroll();
541                 bv_->theLockingInset()->
542                         InsetMotionNotify(bv_,
543                                           x - start_x,
544                                           y - cursor.y() + bv_->text->first,
545                                           state);
546                 return;
547         }
548    
549         /* The test for not selection possible is needed, that only motion events are 
550          * used, where the bottom press event was on the drawing area too */
551         if (!selection_possible)
552                 return;
553  
554         screen_->HideCursor();
555
556         bv_->text->SetCursorFromCoordinates(bv_, x, y + bv_->text->first);
557       
558         if (!bv_->text->selection)
559                 update(bv_->text, BufferView::UPDATE); // Maybe an empty line was deleted
560       
561         bv_->text->SetSelection(bv_);
562         screen_->ToggleToggle(bv_->text, bv_);
563         fitCursor(bv_->text);
564         screen_->ShowCursor(bv_->text, bv_);
565 }
566
567
568 // Single-click on work area
569 void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
570                                             unsigned int button)
571 {
572         last_click_x = -1;
573         last_click_y = -1;
574
575         if (!buffer_ || !screen_.get()) return;
576
577         Inset * inset_hit = checkInsetHit(bv_->text, xpos, ypos, button);
578
579         // ok ok, this is a hack.
580         if (button == 4 || button == 5) {
581                 switch (button) {
582                 case 4:
583                         scrollUp(lyxrc.wheel_jump); // default 100, set in lyxrc
584                         break;
585                 case 5:
586                         scrollDown(lyxrc.wheel_jump);
587                         break;
588                 }
589         }
590         
591         if (bv_->theLockingInset()) {
592                 // We are in inset locking mode
593                 
594                 /* Check whether the inset was hit. If not reset mode,
595                    otherwise give the event to the inset */
596                 if (inset_hit == bv_->theLockingInset()) {
597                         bv_->theLockingInset()->
598                                 InsetButtonPress(bv_,
599                                                  xpos, ypos,
600                                                  button);
601                         return;
602                 } else {
603                         bv_->unlockInset(bv_->theLockingInset());
604                 }
605         }
606         
607         if (!inset_hit)
608                 selection_possible = true;
609         screen_->HideCursor();
610
611         int const screen_first = bv_->text->first;
612         
613         // Middle button press pastes if we have a selection
614         bool paste_internally = false;
615         if (button == 2
616             && bv_->text->selection) {
617                 owner_->getLyXFunc()->Dispatch(LFUN_COPY);
618                 paste_internally = true;
619         }
620         
621         // Clear the selection
622         screen_->ToggleSelection(bv_->text, bv_);
623         bv_->text->ClearSelection(bv_);
624         bv_->text->FullRebreak(bv_);
625         screen_->Update(bv_->text, bv_);
626         updateScrollbar();
627         
628         // Single left click in math inset?
629         if ((inset_hit != 0) &&
630             (inset_hit->Editable()==Inset::HIGHLY_EDITABLE)) {
631                 // Highly editable inset, like math
632                 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
633                 selection_possible = false;
634                 owner_->updateLayoutChoice();
635                 owner_->message(inset->EditMessage());
636                 inset->InsetButtonPress(bv_, xpos, ypos, button);
637                 inset->Edit(bv_, xpos, ypos, button);
638                 return;
639         } 
640         
641         // Right click on a footnote flag opens float menu
642         if (button == 3) { 
643                 selection_possible = false;
644                 return;
645         }
646         
647         if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
648                 bv_->text->SetCursorFromCoordinates(bv_, xpos, ypos + screen_first);
649         bv_->text->FinishUndo();
650         bv_->text->sel_cursor = bv_->text->cursor;
651         bv_->text->cursor.x_fix(bv_->text->cursor.x());
652         
653         owner_->updateLayoutChoice();
654         if (fitCursor(bv_->text)) {
655                 selection_possible = false;
656         }
657         
658         // Insert primary selection with middle mouse
659         // if there is a local selection in the current buffer,
660         // insert this
661         if (button == 2) {
662                 if (paste_internally)
663                         owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
664                 else
665                         owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
666                                                        "paragraph");
667                 selection_possible = false;
668                 return;
669         }
670 }
671
672
673 void BufferView::Pimpl::doubleClick(int /*x*/, int /*y*/, unsigned int button) 
674 {
675         // select a word
676         if (!buffer_)
677             return;
678
679         LyXText * text = bv_->getLyXText();
680
681         if (text->bv_owner && bv_->theLockingInset())
682             return;
683
684         if (screen_.get() && button == 1) {
685             if (text->bv_owner) {
686                 screen_->HideCursor();
687                 screen_->ToggleSelection(text, bv_);
688                 text->SelectWord(bv_);
689                 screen_->ToggleSelection(text, bv_, false);
690             } else {
691                 text->SelectWord(bv_);
692             }
693             /* This will fit the cursor on the screen
694              * if necessary */
695             update(text, BufferView::SELECT|BufferView::FITCUR);
696         }
697 }
698
699
700 void BufferView::Pimpl::tripleClick(int /*x*/, int /*y*/, unsigned int button)
701 {
702         // select a line
703         if (buffer_)
704                 return;
705
706         LyXText * text = bv_->getLyXText();
707
708         if (text->bv_owner && bv_->theLockingInset())
709             return;
710
711         if (screen_.get() && (button == 1)) {
712                 screen_->HideCursor();
713                 screen_->ToggleSelection(text, bv_);
714                 text->CursorHome(bv_);
715                 text->sel_cursor = text->cursor;
716                 text->CursorEnd(bv_);
717                 text->SetSelection(bv_);
718                 screen_->ToggleSelection(text, bv_, false);
719                 /* This will fit the cursor on the screen
720                  * if necessary */
721                 update(text, BufferView::SELECT|BufferView::FITCUR);
722         }
723 }
724
725
726 void BufferView::Pimpl::enterView()
727 {
728         if (active() && available()) {
729                 SetXtermCursor(workarea_.getWin());
730                 using_xterm_cursor = true;
731         }
732 }
733
734
735 void BufferView::Pimpl::leaveView()
736 {
737         if (using_xterm_cursor) {
738                 XUndefineCursor(fl_get_display(), workarea_.getWin());
739                 using_xterm_cursor = false;
740         }
741 }
742
743
744 void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
745                                               unsigned int button)
746 {
747         if (!buffer_ || !screen_.get()) return;
748
749         // If we hit an inset, we have the inset coordinates in these
750         // and inset_hit points to the inset.  If we do not hit an
751         // inset, inset_hit is 0, and inset_x == x, inset_y == y.
752         Inset * inset_hit = checkInsetHit(bv_->text, x, y, button);
753
754         if (bv_->theLockingInset()) {
755                 // We are in inset locking mode.
756
757                 /* LyX does a kind of work-area grabbing for insets.
758                    Only a ButtonPress Event outside the inset will 
759                    force a InsetUnlock. */
760                 bv_->theLockingInset()->
761                         InsetButtonRelease(bv_, x, y, button);
762                 return;
763         }
764         
765         selection_possible = false;
766         
767         if (button == 2)
768                 return;
769
770         bv_->setState();
771         owner_->showState();
772
773         // Did we hit an editable inset?
774         if (inset_hit) {
775                 // Inset like error, notes and figures
776                 selection_possible = false;
777
778                 // CHECK fix this proper in 0.13
779
780                 // Following a ref shouldn't issue
781                 // a push on the undo-stack
782                 // anylonger, now that we have
783                 // keybindings for following
784                 // references and returning from
785                 // references.  IMHO though, it
786                 // should be the inset's own business
787                 // to push or not push on the undo
788                 // stack. They don't *have* to
789                 // alter the document...
790                 // (Joacim)
791                 // ...or maybe the SetCursorParUndo()
792                 // below isn't necessary at all anylonger?
793                 if (inset_hit->LyxCode() == Inset::REF_CODE) {
794                         bv_->text->SetCursorParUndo(bv_->buffer());
795                 }
796
797                 owner_->message(inset_hit->EditMessage());
798
799                 if (inset_hit->Editable()==Inset::HIGHLY_EDITABLE) {
800                         // Highly editable inset, like math
801                         UpdatableInset *inset = (UpdatableInset *)inset_hit;
802                         inset->InsetButtonRelease(bv_, x, y, button);
803                 } else {
804                         inset_hit->InsetButtonRelease(bv_, x, y, button);
805                         inset_hit->Edit(bv_, x, y, button);
806                 }
807                 return;
808         }
809
810 #ifndef NEW_INSETS
811         // check whether we want to open a float
812         if (bv_->text) {
813                 bool hit = false;
814                 char c = ' ';
815                 if (bv_->text->cursor.pos() <
816                     bv_->text->cursor.par()->Last()) {
817                         c = bv_->text->cursor.par()->
818                                 GetChar(bv_->text->cursor.pos());
819                 }
820                if(!bv_->text->selection)
821                 if (c == LyXParagraph::META_FOOTNOTE
822                     || c == LyXParagraph::META_MARGIN
823                     || c == LyXParagraph::META_FIG
824                     || c == LyXParagraph::META_TAB
825                     || c == LyXParagraph::META_WIDE_FIG
826                     || c == LyXParagraph::META_WIDE_TAB
827                     || c == LyXParagraph::META_ALGORITHM){
828                         hit = true;
829                 } else
830                         if (bv_->text->cursor.pos() - 1 >= 0) {
831                         c = bv_->text->cursor.par()->
832                                 GetChar(bv_->text->cursor.pos() - 1);
833                         if (c == LyXParagraph::META_FOOTNOTE
834                             || c == LyXParagraph::META_MARGIN
835                             || c == LyXParagraph::META_FIG
836                             || c == LyXParagraph::META_TAB
837                             || c == LyXParagraph::META_WIDE_FIG 
838                             || c == LyXParagraph::META_WIDE_TAB
839                             || c == LyXParagraph::META_ALGORITHM){
840                                 // We are one step too far to the right
841                                 bv_->text->CursorLeft(bv_);
842                                 hit = true;
843                         }
844                 }
845                 if (hit == true) {
846                         bv_->toggleFloat();
847                         selection_possible = false;
848                         return;
849                 }
850         }
851         // Do we want to close a float? (click on the float-label)
852         if (bv_->text->cursor.row()->par()->footnoteflag == 
853             LyXParagraph::OPEN_FOOTNOTE
854             && bv_->text->cursor.row()->previous() &&
855             bv_->text->cursor.row()->previous()->par()->
856             footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
857                 LyXFont font(LyXFont::ALL_SANE);
858                 font.setSize(LyXFont::SIZE_FOOTNOTE);
859
860                 int box_x = 20; // LYX_PAPER_MARGIN;
861                 box_x += lyxfont::width(" wide-tab ", font);
862
863                 unsigned int screen_first = bv_->text->first;
864
865                 if (x < box_x
866                     && y + screen_first > bv_->text->cursor.y() -
867                     bv_->text->cursor.row()->baseline()
868                     && y + screen_first < bv_->text->cursor.y() -
869                     bv_->text->cursor.row()->baseline()
870                     + lyxfont::maxAscent(font) * 1.2 + lyxfont::maxDescent(font) * 1.2) {
871                         bv_->toggleFloat();
872                         selection_possible = false;
873                         return;
874                 }
875         }
876 #else
877         // check whether we want to open a float
878         if (bv_->text) {
879                 bool hit = false;
880                 char c = ' ';
881                 if (bv_->text->cursor.pos() <
882                     bv_->text->cursor.par()->size()) {
883                         c = bv_->text->cursor.par()->
884                                 GetChar(bv_->text->cursor.pos());
885                 }
886                         if (bv_->text->cursor.pos() - 1 >= 0) {
887                         c = bv_->text->cursor.par()->
888                                 GetChar(bv_->text->cursor.pos() - 1);
889                 }
890                 if (hit == true) {
891                         selection_possible = false;
892                         return;
893                 }
894         }
895 #endif
896         // Maybe we want to edit a bibitem ale970302
897         if (bv_->text->cursor.par()->bibkey && x < 20 + 
898             bibitemMaxWidth(bv_, textclasslist.
899                             TextClass(buffer_->
900                                       params.textclass).defaultfont())) {
901                 bv_->text->cursor.par()->bibkey->Edit(bv_, 0, 0, 0);
902         }
903
904         return;
905 }
906
907
908 /* 
909  * Returns an inset if inset was hit. 0 otherwise.
910  * If hit, the coordinates are changed relative to the inset. 
911  * Otherwise coordinates are not changed, and false is returned.
912  */
913 Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y,
914                                          unsigned int /* button */)
915 {
916         if (!screen_.get())
917                 return 0;
918   
919         int y_tmp = y + text->first;
920   
921         LyXCursor cursor;
922         text->SetCursorFromCoordinates(bv_, cursor, x, y_tmp);
923         text->SetCursor(bv_, cursor, cursor.par(),cursor.pos(),true);
924
925
926 #ifndef NEW_INSETS
927         if (cursor.pos() < cursor.par()->Last()
928 #else
929         if (cursor.pos() < cursor.par()->size()
930 #endif
931             && cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
932             && cursor.par()->GetInset(cursor.pos())
933             && cursor.par()->GetInset(cursor.pos())->Editable()) {
934
935                 // Check whether the inset really was hit
936                 Inset * tmpinset = cursor.par()->GetInset(cursor.pos());
937                 LyXFont font = text->GetFont(bv_->buffer(),
938                                                   cursor.par(), cursor.pos());
939                 int const width = tmpinset->width(bv_, font);
940                 int const inset_x = font.isVisibleRightToLeft()
941                         ? cursor.x() - width : cursor.x();
942                 int const start_x = inset_x + tmpinset->scroll();
943                 int const end_x = inset_x + width;
944
945                 if (x > start_x && x < end_x
946                     && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
947                     && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
948                         text->SetCursor(bv_, cursor.par(),cursor.pos(), true);
949                         x = x - start_x;
950                         // The origin of an inset is on the baseline
951                         y = y_tmp - (text->cursor.y()); 
952                         return tmpinset;
953                 }
954         }
955
956         if ((cursor.pos() - 1 >= 0) &&
957             (cursor.par()->GetChar(cursor.pos()-1) == LyXParagraph::META_INSET) &&
958             (cursor.par()->GetInset(cursor.pos() - 1)) &&
959             (cursor.par()->GetInset(cursor.pos() - 1)->Editable())) {
960                 Inset * tmpinset = cursor.par()->GetInset(cursor.pos()-1);
961                 LyXFont font = text->GetFont(bv_->buffer(), cursor.par(),
962                                                   cursor.pos()-1);
963                 int const width = tmpinset->width(bv_, font);
964                 int const inset_x = font.isVisibleRightToLeft()
965                         ? cursor.x() : cursor.x() - width;
966                 int const start_x = inset_x + tmpinset->scroll();
967                 int const end_x = inset_x + width;
968
969                 if (x > start_x && x < end_x
970                     && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
971                     && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
972 #if 0
973                         if (move_cursor && (tmpinset != bv_->theLockingInset()))
974 #endif
975                                 text->SetCursor(bv_, cursor.par(),cursor.pos()-1,true);
976                         x = x - start_x;
977                         // The origin of an inset is on the baseline
978                         y = y_tmp - (text->cursor.y()); 
979                         return tmpinset;
980                 }
981         }
982         return 0;
983 }
984
985
986 void BufferView::Pimpl::workAreaExpose()
987 {
988         static int work_area_width = 0;
989         static unsigned int work_area_height = 0;
990
991         bool const widthChange = workarea_.workWidth() != work_area_width;
992         bool const heightChange = workarea_.height() != work_area_height;
993
994         // update from work area
995         work_area_width = workarea_.workWidth();
996         work_area_height = workarea_.height();
997         if (buffer_ != 0) {
998                 if (widthChange) {
999                         // All buffers need a resize
1000                         bufferlist.resize();
1001
1002                         // Remove all texts from the textcache
1003                         // This is not _really_ what we want to do. What
1004                         // we really want to do is to delete in textcache
1005                         // that does not have a BufferView with matching
1006                         // width, but as long as we have only one BufferView
1007                         // deleting all gives the same result.
1008                         if (lyxerr.debugging())
1009                                 textcache.show(lyxerr, "Expose delete all");
1010                         textcache.clear();
1011                 } else if (heightChange) {
1012                         // Rebuild image of current screen
1013                         updateScreen();
1014                         // fitCursor() ensures we don't jump back
1015                         // to the start of the document on vertical
1016                         // resize
1017                         fitCursor(bv_->text);
1018
1019                         // The main window size has changed, repaint most stuff
1020                         redraw();
1021                 } else if (screen_.get())
1022                     screen_->Redraw(bv_->text, bv_);
1023         } else {
1024                 // Grey box when we don't have a buffer
1025                 workarea_.greyOut();
1026         }
1027
1028         // always make sure that the scrollbar is sane.
1029         updateScrollbar();
1030         owner_->updateLayoutChoice();
1031         return;
1032 }
1033
1034
1035 void BufferView::Pimpl::update()
1036 {
1037         if (screen_.get()) screen_->Update(bv_->text, bv_);
1038 }
1039
1040 // Values used when calling update:
1041 // -3 - update
1042 // -2 - update, move sel_cursor if selection, fitcursor
1043 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
1044 //  0 - update, move sel_cursor if selection, fitcursor 
1045 //  1 - update, move sel_cursor if selection, fitcursor, mark dirty
1046 //  3 - update, move sel_cursor if selection
1047 //
1048 // update -
1049 // a simple redraw of the parts that need refresh
1050 //
1051 // move sel_cursor if selection -
1052 // the text's sel_cursor is moved if there is selection is progress
1053 //
1054 // fitcursor -
1055 // fitCursor() is called and the scrollbar updated
1056 //
1057 // mark dirty -
1058 // the buffer is marked dirty.
1059 //
1060 // enum {
1061 //       UPDATE = 0,
1062 //       SELECT = 1,
1063 //       FITCUR = 2,
1064 //       CHANGE = 4 
1065 // };
1066 //
1067 // UPDATE_ONLY = UPDATE;
1068 // UPDATE_SELECT = UPDATE | SELECT;
1069 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
1070 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
1071 //
1072 // update(-3) -> update(0)         -> update(0) -> update(UPDATE)
1073 // update(-2) -> update(1 + 2)     -> update(3) -> update(SELECT|FITCUR)
1074 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1075 // update(1)  -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1076 // update(3)  -> update(1)         -> update(1) -> update(SELECT)
1077
1078 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
1079 {
1080         owner_->updateLayoutChoice();
1081
1082         if (!text->selection && (f & SELECT)) {
1083                 text->sel_cursor = text->cursor;
1084         }
1085
1086         text->FullRebreak(bv_);
1087
1088         if (text->inset_owner) {
1089             text->inset_owner->SetUpdateStatus(bv_, InsetText::NONE);
1090             bv_->updateInset(text->inset_owner, true);
1091         } else
1092             update();
1093
1094         if ((f & FITCUR)) {
1095                 fitCursor(text);
1096         }
1097
1098         if ((f & CHANGE)) {
1099                 buffer_->markDirty();
1100         }
1101 }
1102
1103
1104 // Callback for cursor timer
1105 void BufferView::Pimpl::cursorToggle()
1106 {
1107         // Quite a nice place for asyncron Inset updating, isn't it?
1108         // Actually no! This is run even if no buffer exist... so (Lgb)
1109         if (!buffer_) {
1110                 cursor_timeout.restart();
1111                 return;
1112         }
1113  
1114         int status = 1;
1115         int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1116         if (pid == -1) // error find out what is wrong
1117                 ; // ignore it for now.
1118         else if (pid > 0)
1119                 sigchldhandler(pid, &status);
1120
1121         updatelist.update(bv_);
1122         
1123         if (!screen_.get()) {
1124                 cursor_timeout.restart();
1125                 return;
1126         }
1127
1128         if (!bv_->theLockingInset()) {
1129                 screen_->CursorToggle(bv_->text, bv_);
1130         } else {
1131                 bv_->theLockingInset()->ToggleInsetCursor(bv_);
1132         }
1133         
1134         cursor_timeout.restart();
1135 }
1136
1137
1138 void BufferView::Pimpl::cursorPrevious(LyXText * text)
1139 {
1140         if (!text->cursor.row()->previous())
1141                 return;
1142         
1143         int y = text->first;
1144         if (text->inset_owner)
1145                 y += bv_->text->first;
1146         Row * cursorrow = text->cursor.row();
1147         text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y);
1148         bv_->text->FinishUndo();
1149         // This is to allow jumping over large insets
1150         if ((cursorrow == text->cursor.row()))
1151                 text->CursorUp(bv_);
1152         
1153         if (text->inset_owner ||
1154             text->cursor.row()->height() < workarea_.height())
1155                 screen_->Draw(bv_->text, bv_,
1156                               text->cursor.y()
1157                               - text->cursor.row()->baseline()
1158                               + text->cursor.row()->height()
1159                               - workarea_.height() + 1 );
1160         updateScrollbar();
1161 }
1162
1163
1164 void BufferView::Pimpl::cursorNext(LyXText * text)
1165 {
1166         if (!text->cursor.row()->next())
1167                 return;
1168         
1169         int y = text->first + workarea_.height();
1170 //      if (text->inset_owner)
1171 //              y += bv_->text->first;
1172         text->GetRowNearY(y);
1173     
1174         Row * cursorrow = text->cursor.row();
1175         text->SetCursorFromCoordinates(bv_, text->cursor.x_fix(), y); // + workarea_->height());
1176         bv_->text->FinishUndo();
1177         // This is to allow jumping over large insets
1178         if ((cursorrow == bv_->text->cursor.row()))
1179                 text->CursorDown(bv_);
1180         
1181         if (text->inset_owner ||
1182             text->cursor.row()->height() < workarea_.height())
1183                 screen_->Draw(bv_->text, bv_, text->cursor.y() -
1184                               text->cursor.row()->baseline());
1185         updateScrollbar();
1186 }
1187
1188
1189 bool BufferView::Pimpl::available() const
1190 {
1191         if (buffer_ && bv_->text) return true;
1192         return false;
1193 }
1194
1195
1196 void BufferView::Pimpl::beforeChange(LyXText * text)
1197 {
1198         toggleSelection();
1199         text->ClearSelection(bv_);
1200 }
1201
1202
1203 void BufferView::Pimpl::savePosition(unsigned int i)
1204 {
1205         if (i >= saved_positions_num)
1206                 return;
1207         saved_positions[i] = Position(buffer_->fileName(),
1208                                       bv_->text->cursor.par()->id(),
1209                                       bv_->text->cursor.pos());
1210         if (i > 0) {
1211                 string const str = _("Saved bookmark") + ' ' + tostr(i);
1212                 owner_->message(str);
1213         }
1214 }
1215
1216
1217 void BufferView::Pimpl::restorePosition(unsigned int i)
1218 {
1219         if (i >= saved_positions_num)
1220                 return;
1221
1222         string fname = saved_positions[i].filename;
1223
1224         beforeChange(bv_->text);
1225
1226         if (fname != buffer_->fileName()) {
1227                 Buffer * b = bufferlist.exists(fname) ?
1228                         bufferlist.getBuffer(fname) :
1229                         bufferlist.loadLyXFile(fname); // don't ask, just load it
1230                 if (b != 0 ) buffer(b);
1231         }
1232
1233         LyXParagraph * par = bv_->text->GetParFromID(saved_positions[i].par_id);
1234         if (!par)
1235                 return;
1236
1237 #ifndef NEW_INSETS
1238         bv_->text->SetCursor(bv_, par,
1239                              min(par->Last(), saved_positions[i].par_pos));
1240 #else
1241         bv_->text->SetCursor(bv_, par,
1242                              min(par->size(), saved_positions[i].par_pos));
1243 #endif
1244         update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1245         if (i > 0) {
1246                 string const str = _("Moved to bookmark") + ' ' + tostr(i);
1247                 owner_->message(str);
1248         }
1249 }
1250
1251
1252 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
1253 {
1254         if (i >= saved_positions_num)
1255                 return false;
1256
1257         return !saved_positions[i].filename.empty();
1258 }
1259
1260
1261 void BufferView::Pimpl::setState()
1262 {
1263         if (!lyxrc.rtl_support)
1264                 return;
1265
1266         LyXText * text = bv_->getLyXText();
1267         if (text->real_current_font.isRightToLeft() &&
1268             text->real_current_font.latex() != LyXFont::ON) {
1269                 if (owner_->getIntl()->keymap == Intl::PRIMARY)
1270                         owner_->getIntl()->KeyMapSec();
1271         } else {
1272                 if (owner_->getIntl()->keymap == Intl::SECONDARY)
1273                         owner_->getIntl()->KeyMapPrim();
1274         }
1275 }
1276
1277
1278 void BufferView::Pimpl::insetSleep()
1279 {
1280         if (bv_->theLockingInset() && !bv_->inset_slept) {
1281                 bv_->theLockingInset()->GetCursorPos(bv_, bv_->slx, bv_->sly);
1282                 bv_->theLockingInset()->InsetUnlock(bv_);
1283                 bv_->inset_slept = true;
1284         }
1285 }
1286
1287
1288 void BufferView::Pimpl::insetWakeup()
1289 {
1290         if (bv_->theLockingInset() && bv_->inset_slept) {
1291                 bv_->theLockingInset()->Edit(bv_, bv_->slx, bv_->sly, 0);
1292                 bv_->inset_slept = false;
1293         }
1294 }
1295
1296
1297 void BufferView::Pimpl::insetUnlock()
1298 {
1299         if (bv_->theLockingInset()) {
1300                 if (!bv_->inset_slept)
1301                         bv_->theLockingInset()->InsetUnlock(bv_);
1302                 bv_->theLockingInset(0);
1303                 bv_->text->FinishUndo();
1304                 bv_->inset_slept = false;
1305         }
1306 }
1307
1308
1309 bool BufferView::Pimpl::focus() const
1310 {
1311         return workarea_.hasFocus();
1312 }
1313
1314
1315 void BufferView::Pimpl::focus(bool f)
1316 {
1317         if (f) workarea_.setFocus();
1318 }
1319
1320
1321 bool BufferView::Pimpl::active() const
1322 {
1323         return workarea_.active();
1324 }
1325
1326
1327 bool BufferView::Pimpl::belowMouse() const 
1328 {
1329         return workarea_.belowMouse();
1330 }
1331
1332
1333 void BufferView::Pimpl::showCursor()
1334 {
1335         if (screen_.get())
1336                 screen_->ShowCursor(bv_->text, bv_);
1337 }
1338
1339
1340 void BufferView::Pimpl::hideCursor()
1341 {
1342         if (screen_.get())
1343                 screen_->HideCursor();
1344 }
1345
1346
1347 void BufferView::Pimpl::toggleSelection(bool b)
1348 {
1349         if (screen_.get())
1350                 screen_->ToggleSelection(bv_->text, bv_, b);
1351 }
1352
1353
1354 void BufferView::Pimpl::toggleToggle()
1355 {
1356         if (screen_.get())
1357                 screen_->ToggleToggle(bv_->text, bv_);
1358 }
1359
1360
1361 void BufferView::Pimpl::center() 
1362 {
1363         beforeChange(bv_->text);
1364         if (bv_->text->cursor.y() > static_cast<int>((workarea_.height() / 2))) {
1365                 screen_->Draw(bv_->text, bv_, bv_->text->cursor.y() - workarea_.height() / 2);
1366         } else {
1367                 screen_->Draw(bv_->text, bv_, 0);
1368         }
1369         update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1370         redraw();
1371 }
1372
1373
1374 void BufferView::Pimpl::pasteClipboard(bool asPara) 
1375 {
1376         if (!buffer_) return;
1377
1378         screen_->HideCursor();
1379         beforeChange(bv_->text);
1380         
1381         string const clip(workarea_.getClipboard());
1382         
1383         if (clip.empty()) return;
1384
1385         if (asPara) {
1386                 bv_->text->InsertStringB(bv_, clip);
1387         } else {
1388                 bv_->text->InsertStringA(bv_, clip);
1389         }
1390         update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1391 }
1392
1393
1394 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
1395 {
1396         workarea_.putClipboard(stuff);
1397 }
1398
1399
1400 /*
1401  * Dispatch functions for actions which can be valid for BufferView->text
1402  * and/or InsetText->text!!!
1403  */
1404 static LyXText * TEXT(BufferView * bv) { return bv->getLyXText(); }
1405
1406 inline
1407 void BufferView::Pimpl::moveCursorUpdate(bool selecting)
1408 {
1409         if (selecting || TEXT(bv_)->mark_set) {
1410                 TEXT(bv_)->SetSelection(bv_);
1411                 if (TEXT(bv_)->bv_owner)
1412                         bv_->toggleToggle();
1413         }
1414         update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1415         showCursor();
1416         
1417         /* ---> Everytime the cursor is moved, show the current font state. */
1418         // should this too me moved out of this func?
1419         //owner->showState();
1420         bv_->setState();
1421 }
1422
1423
1424 Inset * BufferView::Pimpl::getInsetByCode(Inset::Code code)
1425 {
1426         LyXCursor cursor = TEXT(bv_)->cursor;
1427         Buffer::inset_iterator it =
1428                 find_if(Buffer::inset_iterator(
1429                         cursor.par(), cursor.pos()),
1430                         buffer_->inset_iterator_end(),
1431                         lyx::compare_memfun(&Inset::LyxCode, code)
1432                         );
1433         return it != buffer_->inset_iterator_end() ? (*it) : 0;
1434 }
1435
1436
1437 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
1438 {
1439         string filename = filen;
1440
1441         if (filename.empty()) {
1442                 // Launch a file browser
1443                 string initpath = lyxrc.document_path;
1444
1445                 if (available()) {
1446                         string const trypath = owner_->buffer()->filepath;
1447                         // If directory is writeable, use this as default.
1448                         if (IsDirWriteable(trypath) == 1)
1449                                 initpath = trypath;
1450                 }
1451
1452                 FileDialog fileDlg(bv_->owner(), _("Select LyX document to insert"),
1453                         LFUN_FILE_INSERT,
1454                         make_pair(string(_("Documents")), string(lyxrc.document_path)),
1455                         make_pair(string(_("Examples")), string(AddPath(system_lyxdir, "examples"))));
1456
1457                 FileDialog::Result result = fileDlg.Select(initpath, _("*.lyx| LyX Documents (*.lyx)"));
1458  
1459                 if (result.first == FileDialog::Later)
1460                         return;
1461
1462                 filename = result.second;
1463
1464                 // check selected filename
1465                 if (filename.empty()) {
1466                         owner_->message(_("Canceled."));
1467                         return;
1468                 }
1469         }
1470
1471         // get absolute path of file and make sure the filename ends
1472         // with .lyx
1473         filename = MakeAbsPath(filename);
1474         if (!IsLyXFilename(filename))
1475                 filename += ".lyx";
1476
1477         // Inserts document
1478         string const s1 = _("Inserting document") + ' '
1479                 + MakeDisplayPath(filename) + " ...";
1480         owner_->message(s1);
1481         bool const res = bv_->insertLyXFile(filename);
1482         if (res) {
1483                 string const str = _("Document") + ' '
1484                         + MakeDisplayPath(filename) + ' ' + _("inserted.");
1485                 owner_->message(str);
1486         } else {
1487                 string const str = _("Could not insert document") + ' '
1488                         + MakeDisplayPath(filename);
1489                 owner_->message(str);
1490         }
1491 }
1492
1493
1494 bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
1495 {
1496         switch (action) {
1497                 // --- Misc -------------------------------------------
1498         case LFUN_APPENDIX:
1499                 if (available()) {
1500                         TEXT(bv_)->toggleAppendix(bv_);
1501                         update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1502                 }
1503                 break;
1504
1505         case LFUN_TOC_INSERT:
1506         case LFUN_LOA_INSERT:
1507         case LFUN_LOF_INSERT:
1508         case LFUN_LOT_INSERT:
1509         {
1510                 InsetCommandParams p;
1511                 
1512                 if (action == LFUN_TOC_INSERT )
1513                         p.setCmdName( "tableofcontents" );
1514                 else if (action == LFUN_LOA_INSERT )
1515                         p.setCmdName( "listofalgorithms" );
1516                 else if (action == LFUN_LOF_INSERT )
1517                         p.setCmdName( "listoffigures" );
1518                 else
1519                         p.setCmdName( "listoftables" );
1520
1521                 Inset * inset = new InsetTOC( p );
1522                 if (!bv_->insertInset( inset, "Standard", true ) )
1523                         delete inset;
1524                 break;
1525         }
1526                 
1527         case LFUN_TABULAR_FEATURE:
1528         case LFUN_SCROLL_INSET:
1529                 // this is not handled here as this funktion is only aktive
1530                 // if we have a locking_inset and that one is (or contains)
1531                 // a tabular-inset
1532                 break;
1533
1534         case LFUN_INSET_GRAPHICS:
1535         {
1536                 Inset * new_inset = new InsetGraphics;
1537                 if (!bv_->insertInset(new_inset)) {
1538                         delete new_inset;
1539                 } else {
1540                         // this is need because you don't use a inset->Edit()
1541                         bv_->updateInset(new_inset, true);
1542                         new_inset->Edit(bv_, 0, 0, 0);
1543                 }
1544                 break;
1545         }
1546                 
1547         case LFUN_PASTE:
1548                 bv_->paste();
1549                 setState();
1550                 break;
1551                 
1552         case LFUN_PASTESELECTION:
1553         {
1554                 bool asPara = false;
1555                 if (argument == "paragraph") asPara = true;
1556                 pasteClipboard(asPara);
1557         }
1558         break;
1559         
1560         case LFUN_CUT:
1561                 bv_->cut();
1562                 break;
1563                 
1564         case LFUN_COPY:
1565                 bv_->copy();
1566                 break;
1567                 
1568         case LFUN_LAYOUT_COPY:
1569                 bv_->copyEnvironment();
1570                 break;
1571                 
1572         case LFUN_LAYOUT_PASTE:
1573                 bv_->pasteEnvironment();
1574                 setState();
1575                 break;
1576                 
1577         case LFUN_GOTOERROR:
1578                 bv_->gotoInset(Inset::ERROR_CODE, false);
1579                 break;
1580                 
1581         case LFUN_GOTONOTE:
1582                 bv_->gotoInset(Inset::IGNORE_CODE, false);
1583                 break;
1584
1585         case LFUN_REFERENCE_GOTO:
1586         {
1587                 vector<Inset::Code> tmp;
1588                 tmp.push_back(Inset::LABEL_CODE);
1589                 tmp.push_back(Inset::REF_CODE);
1590                 bv_->gotoInset(tmp, true);
1591                 break;
1592         }
1593
1594         case LFUN_HYPHENATION:
1595                 bv_->hyphenationPoint();
1596                 break;
1597                 
1598         case LFUN_LDOTS:
1599                 bv_->ldots();
1600                 break;
1601                 
1602         case LFUN_END_OF_SENTENCE:
1603                 bv_->endOfSentenceDot();
1604                 break;
1605
1606         case LFUN_MENU_SEPARATOR:
1607                 bv_->menuSeparator();
1608                 break;
1609                 
1610         case LFUN_HFILL:
1611                 bv_->hfill();
1612                 break;
1613                 
1614         case LFUN_DEPTH:
1615                 changeDepth(bv_, TEXT(bv_), 0);
1616                 break;
1617                 
1618         case LFUN_DEPTH_MIN:
1619                 changeDepth(bv_, TEXT(bv_), -1);
1620                 break;
1621                 
1622         case LFUN_DEPTH_PLUS:
1623                 changeDepth(bv_, TEXT(bv_), 1);
1624                 break;
1625                 
1626         case LFUN_FREE:
1627                 owner_->getDialogs()->setUserFreeFont();
1628                 break;
1629                 
1630         case LFUN_TEX:
1631                 Tex(bv_);
1632                 setState();
1633                 owner_->showState();
1634                 break;
1635
1636         case LFUN_FILE_INSERT:
1637         {
1638                 MenuInsertLyXFile(argument);
1639         }
1640         break;
1641         
1642         case LFUN_FILE_INSERT_ASCII_PARA:
1643                 InsertAsciiFile(bv_, argument, true);
1644                 break;
1645
1646         case LFUN_FILE_INSERT_ASCII:
1647                 InsertAsciiFile(bv_, argument, false);
1648                 break;
1649                 
1650         case LFUN_LAYOUT:
1651         {
1652                 lyxerr.debug() << "LFUN_LAYOUT: (arg) "
1653                                << argument << endl;
1654                 
1655                 // Derive layout number from given argument (string)
1656                 // and current buffer's textclass (number). */    
1657                 LyXTextClassList::ClassList::size_type tclass =
1658                         buffer_->params.textclass;
1659                 pair <bool, LyXTextClass::size_type> layout = 
1660                         textclasslist.NumberOfLayout(tclass, argument);
1661
1662                 // If the entry is obsolete, use the new one instead.
1663                 if (layout.first) {
1664                         string obs = textclasslist.Style(tclass,layout.second)
1665                                 .obsoleted_by();
1666                         if (!obs.empty()) 
1667                                 layout = 
1668                                         textclasslist.NumberOfLayout(tclass, obs);
1669                 }
1670
1671                 // see if we found the layout number:
1672                 if (!layout.first) {
1673                         owner_->getLyXFunc()->setErrorMessage(
1674                                 string(N_("Layout ")) + argument +
1675                                 N_(" not known"));
1676                         break;
1677                 }
1678
1679                 if (current_layout != layout.second) {
1680                         hideCursor();
1681                         current_layout = layout.second;
1682                         update(TEXT(bv_),
1683                                BufferView::SELECT|BufferView::FITCUR);
1684                         TEXT(bv_)->SetLayout(bv_, layout.second);
1685                         owner_->setLayout(layout.second);
1686                         update(TEXT(bv_),
1687                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1688                         setState();
1689                 }
1690         }
1691         break;
1692
1693         case LFUN_LANGUAGE:
1694                 Lang(bv_, argument);
1695                 setState();
1696                 owner_->showState();
1697                 break;
1698
1699         case LFUN_EMPH:
1700                 Emph(bv_);
1701                 owner_->showState();
1702                 break;
1703
1704         case LFUN_BOLD:
1705                 Bold(bv_);
1706                 owner_->showState();
1707                 break;
1708                 
1709         case LFUN_NOUN:
1710                 Noun(bv_);
1711                 owner_->showState();
1712                 break;
1713                 
1714         case LFUN_CODE:
1715                 Code(bv_);
1716                 owner_->showState();
1717                 break;
1718                 
1719         case LFUN_SANS:
1720                 Sans(bv_);
1721                 owner_->showState();
1722                 break;
1723                 
1724         case LFUN_ROMAN:
1725                 Roman(bv_);
1726                 owner_->showState();
1727                 break;
1728                 
1729         case LFUN_DEFAULT:
1730                 StyleReset(bv_);
1731                 owner_->showState();
1732                 break;
1733                 
1734         case LFUN_UNDERLINE:
1735                 Underline(bv_);
1736                 owner_->showState();
1737                 break;
1738                 
1739         case LFUN_FONT_SIZE:
1740                 FontSize(bv_, argument);
1741                 owner_->showState();
1742                 break;
1743                 
1744         case LFUN_FONT_STATE:
1745                 owner_->getLyXFunc()->setMessage(CurrentState(bv_));
1746                 break;
1747                 
1748         case LFUN_UPCASE_WORD:
1749                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1750                 TEXT(bv_)->ChangeWordCase(bv_, LyXText::text_uppercase);
1751                 if (TEXT(bv_)->inset_owner)
1752                         bv_->updateInset(TEXT(bv_)->inset_owner, true);
1753                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1754                 break;
1755                 
1756         case LFUN_LOWCASE_WORD:
1757                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1758                 TEXT(bv_)->ChangeWordCase(bv_, LyXText::text_lowercase);
1759                 if (TEXT(bv_)->inset_owner)
1760                         bv_->updateInset(TEXT(bv_)->inset_owner, true);
1761                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1762                 break;
1763                 
1764         case LFUN_CAPITALIZE_WORD:
1765                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1766                 TEXT(bv_)->ChangeWordCase(bv_,
1767                                              LyXText::text_capitalization);
1768                 if (TEXT(bv_)->inset_owner)
1769                         bv_->updateInset(TEXT(bv_)->inset_owner, true);
1770                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1771                 break;
1772
1773         case LFUN_TRANSPOSE_CHARS:
1774                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1775                 TEXT(bv_)->TransposeChars(*bv_);
1776                 if (TEXT(bv_)->inset_owner)
1777                         bv_->updateInset(TEXT(bv_)->inset_owner, true);
1778                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1779                 break;
1780                 
1781                                           
1782         case LFUN_INSERT_LABEL:
1783                 MenuInsertLabel(bv_, argument);
1784                 break;
1785                 
1786         case LFUN_REF_INSERT:
1787                 if (argument.empty()) {
1788                         InsetCommandParams p("ref");
1789                         owner_->getDialogs()->createRef(p.getAsString());
1790                 } else {
1791                         InsetCommandParams p;
1792                         p.setFromString(argument);
1793
1794                         InsetRef * inset = new InsetRef(p, *buffer_);
1795                         if (!bv_->insertInset(inset))
1796                                 delete inset;
1797                         else
1798                                 bv_->updateInset(inset, true);
1799                 }
1800                 break;
1801
1802         case LFUN_BOOKMARK_SAVE:
1803                 savePosition(strToUnsignedInt(argument));
1804                 break;
1805
1806         case LFUN_BOOKMARK_GOTO:
1807                 restorePosition(strToUnsignedInt(argument));
1808                 break;
1809
1810         case LFUN_REF_GOTO:
1811         {
1812                 string label(argument);
1813                 if (label.empty()) {
1814                         InsetRef * inset = 
1815                                 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1816                         if (inset) {
1817                                 label = inset->getContents();
1818                                 savePosition(0);
1819                         }
1820                 }
1821                 
1822                 if (!label.empty()) {
1823                         //bv_->savePosition(0);
1824                         if (!bv_->gotoLabel(label))
1825                                 WriteAlert(_("Error"), 
1826                                            _("Couldn't find this label"), 
1827                                            _("in current document."));
1828                 }
1829         }
1830         break;
1831                 
1832                 // --- Cursor Movements -----------------------------
1833         case LFUN_RIGHT:
1834         {
1835                 bool is_rtl = TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params);
1836                 if (!TEXT(bv_)->mark_set)
1837                         beforeChange(TEXT(bv_));
1838                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1839                 if (is_rtl)
1840                         TEXT(bv_)->CursorLeft(bv_, false);
1841 #ifndef NEW_INSETS
1842                 if (TEXT(bv_)->cursor.pos() < TEXT(bv_)->cursor.par()->Last()
1843 #else
1844                 if (TEXT(bv_)->cursor.pos() < TEXT(bv_)->cursor.par()->size()
1845 #endif
1846                     && TEXT(bv_)->cursor.par()->GetChar(TEXT(bv_)->cursor.pos())
1847                     == LyXParagraph::META_INSET
1848                     && TEXT(bv_)->cursor.par()->GetInset(TEXT(bv_)->cursor.pos())
1849                     && TEXT(bv_)->cursor.par()->GetInset(TEXT(bv_)->cursor.pos())->Editable() == Inset::HIGHLY_EDITABLE){
1850                         Inset * tmpinset = TEXT(bv_)->cursor.par()->GetInset(TEXT(bv_)->cursor.pos());
1851                         owner_->getLyXFunc()->setMessage(tmpinset->EditMessage());
1852                         int y = 0;
1853                         if (is_rtl) {
1854                                 LyXFont font = 
1855                                         TEXT(bv_)->GetFont(buffer_,
1856                                                               TEXT(bv_)->cursor.par(),
1857                                                               TEXT(bv_)->cursor.pos()); 
1858                                 y = tmpinset->descent(bv_,font);
1859                         }
1860                         tmpinset->Edit(bv_, 0, y, 0);
1861                         break;
1862                 }
1863                 if (!is_rtl)
1864                         TEXT(bv_)->CursorRight(bv_, false);
1865                 TEXT(bv_)->FinishUndo();
1866                 moveCursorUpdate(false);
1867                 owner_->showState();
1868         }
1869         break;
1870                 
1871         case LFUN_LEFT:
1872         {
1873                 // This is soooo ugly. Isn`t it possible to make
1874                 // it simpler? (Lgb)
1875                 bool is_rtl = TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params);
1876                 if (!TEXT(bv_)->mark_set)
1877                         beforeChange(TEXT(bv_));
1878                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1879                 LyXCursor cur = TEXT(bv_)->cursor;
1880                 if (!is_rtl)
1881                         TEXT(bv_)->CursorLeft(bv_, false);
1882                 if ((is_rtl || cur != TEXT(bv_)->cursor) && // only if really moved!
1883 #ifndef NEW_INSETS
1884                     TEXT(bv_)->cursor.pos() < TEXT(bv_)->cursor.par()->Last() &&
1885 #else
1886                     TEXT(bv_)->cursor.pos() < TEXT(bv_)->cursor.par()->size() &&
1887 #endif
1888                     (TEXT(bv_)->cursor.par()->GetChar(TEXT(bv_)->cursor.pos()) ==
1889                      LyXParagraph::META_INSET) &&
1890                     TEXT(bv_)->cursor.par()->GetInset(TEXT(bv_)->cursor.pos()) &&
1891                     (TEXT(bv_)->cursor.par()->GetInset(TEXT(bv_)->cursor.pos())->Editable()
1892                      == Inset::HIGHLY_EDITABLE))
1893                 {
1894                         Inset * tmpinset = TEXT(bv_)->cursor.par()->GetInset(TEXT(bv_)->cursor.pos());
1895                         owner_->getLyXFunc()->setMessage(tmpinset->EditMessage());
1896                         LyXFont font = TEXT(bv_)->GetFont(buffer_,
1897                                                              TEXT(bv_)->cursor.par(),
1898                                                              TEXT(bv_)->cursor.pos());
1899                         int y = is_rtl ? 0 
1900                                 : tmpinset->descent(bv_,font);
1901                         tmpinset->Edit(bv_,
1902                                        tmpinset->x() +
1903                                        tmpinset->width(bv_,font),
1904                                        y, 0);
1905                         break;
1906                 }
1907                 if  (is_rtl)
1908                         TEXT(bv_)->CursorRight(bv_, false);
1909
1910                 TEXT(bv_)->FinishUndo();
1911                 moveCursorUpdate(false);
1912                 owner_->showState();
1913         }
1914         break;
1915                 
1916         case LFUN_UP:
1917                 if (!TEXT(bv_)->mark_set)
1918                         beforeChange(TEXT(bv_));
1919                 update(TEXT(bv_), BufferView::UPDATE);
1920                 TEXT(bv_)->CursorUp(bv_);
1921                 TEXT(bv_)->FinishUndo();
1922                 moveCursorUpdate(false);
1923                 owner_->showState();
1924                 break;
1925                 
1926         case LFUN_DOWN:
1927                 if (!TEXT(bv_)->mark_set)
1928                         beforeChange(TEXT(bv_));
1929                 update(TEXT(bv_), BufferView::UPDATE);
1930                 TEXT(bv_)->CursorDown(bv_);
1931                 TEXT(bv_)->FinishUndo();
1932                 moveCursorUpdate(false);
1933                 owner_->showState();
1934                 break;
1935
1936         case LFUN_UP_PARAGRAPH:
1937                 if (!TEXT(bv_)->mark_set)
1938                         beforeChange(TEXT(bv_));
1939                 update(TEXT(bv_), BufferView::UPDATE);
1940                 TEXT(bv_)->CursorUpParagraph(bv_);
1941                 TEXT(bv_)->FinishUndo();
1942                 moveCursorUpdate(false);
1943                 owner_->showState();
1944                 break;
1945                 
1946         case LFUN_DOWN_PARAGRAPH:
1947                 if (!TEXT(bv_)->mark_set)
1948                         beforeChange(TEXT(bv_));
1949                 update(TEXT(bv_), BufferView::UPDATE);
1950                 TEXT(bv_)->CursorDownParagraph(bv_);
1951                 TEXT(bv_)->FinishUndo();
1952                 moveCursorUpdate(false);
1953                 owner_->showState();
1954                 break;
1955                 
1956         case LFUN_PRIOR:
1957                 if (!TEXT(bv_)->mark_set)
1958                         beforeChange(TEXT(bv_));
1959                 update(TEXT(bv_), BufferView::UPDATE);
1960                 cursorPrevious(TEXT(bv_));
1961                 TEXT(bv_)->FinishUndo();
1962                 moveCursorUpdate(false);
1963                 owner_->showState();
1964                 break;
1965                 
1966         case LFUN_NEXT:
1967                 if (!TEXT(bv_)->mark_set)
1968                         beforeChange(TEXT(bv_));
1969                 update(TEXT(bv_), BufferView::UPDATE);
1970                 cursorNext(TEXT(bv_));
1971                 TEXT(bv_)->FinishUndo();
1972                 moveCursorUpdate(false);
1973                 owner_->showState();
1974                 break;
1975                 
1976         case LFUN_HOME:
1977                 if (!TEXT(bv_)->mark_set)
1978                         beforeChange(TEXT(bv_));
1979                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1980                 TEXT(bv_)->CursorHome(bv_);
1981                 TEXT(bv_)->FinishUndo();
1982                 moveCursorUpdate(false);
1983                 owner_->showState();
1984                 break;
1985                 
1986         case LFUN_END:
1987                 if (!TEXT(bv_)->mark_set)
1988                         beforeChange(TEXT(bv_));
1989                 update(TEXT(bv_),
1990                        BufferView::SELECT|BufferView::FITCUR);
1991                 TEXT(bv_)->CursorEnd(bv_);
1992                 TEXT(bv_)->FinishUndo();
1993                 moveCursorUpdate(false);
1994                 owner_->showState();
1995                 break;
1996                 
1997         case LFUN_SHIFT_TAB:
1998         case LFUN_TAB:
1999                 if (!TEXT(bv_)->mark_set)
2000                         beforeChange(TEXT(bv_));
2001                 update(TEXT(bv_),
2002                        BufferView::SELECT|BufferView::FITCUR);
2003                 TEXT(bv_)->CursorTab(bv_);
2004                 TEXT(bv_)->FinishUndo();
2005                 moveCursorUpdate(false);
2006                 owner_->showState();
2007                 break;
2008                 
2009         case LFUN_WORDRIGHT:
2010                 if (!TEXT(bv_)->mark_set)
2011                         beforeChange(TEXT(bv_));
2012                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2013                 if (TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params))
2014                         TEXT(bv_)->CursorLeftOneWord(bv_);
2015                 else
2016                         TEXT(bv_)->CursorRightOneWord(bv_);
2017                 TEXT(bv_)->FinishUndo();
2018                 moveCursorUpdate(false);
2019                 owner_->showState();
2020                 break;
2021                 
2022         case LFUN_WORDLEFT:
2023                 if (!TEXT(bv_)->mark_set)
2024                         beforeChange(TEXT(bv_));
2025                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2026                 if (TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params))
2027                         TEXT(bv_)->CursorRightOneWord(bv_);
2028                 else
2029                         TEXT(bv_)->CursorLeftOneWord(bv_);
2030                 TEXT(bv_)->FinishUndo();
2031                 moveCursorUpdate(false);
2032                 owner_->showState();
2033                 break;
2034                 
2035         case LFUN_BEGINNINGBUF:
2036                 if (!TEXT(bv_)->mark_set)
2037                         beforeChange(TEXT(bv_));
2038                 update(TEXT(bv_),
2039                        BufferView::SELECT|BufferView::FITCUR);
2040                 TEXT(bv_)->CursorTop(bv_);
2041                 TEXT(bv_)->FinishUndo();
2042                 moveCursorUpdate(false);
2043                 owner_->showState();
2044                 break;
2045                 
2046         case LFUN_ENDBUF:
2047                 if (!TEXT(bv_)->mark_set)
2048                         beforeChange(TEXT(bv_));
2049                 update(TEXT(bv_),
2050                        BufferView::SELECT|BufferView::FITCUR);
2051                 TEXT(bv_)->CursorBottom(bv_);
2052                 TEXT(bv_)->FinishUndo();
2053                 moveCursorUpdate(false);
2054                 owner_->showState();
2055                 break;
2056
2057       
2058                 /* cursor selection ---------------------------- */
2059         case LFUN_RIGHTSEL:
2060                 update(TEXT(bv_),
2061                        BufferView::SELECT|BufferView::FITCUR);
2062                 if (TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params))
2063                         TEXT(bv_)->CursorLeft(bv_);
2064                 else
2065                         TEXT(bv_)->CursorRight(bv_);
2066                 TEXT(bv_)->FinishUndo();
2067                 moveCursorUpdate(true);
2068                 owner_->showState();
2069                 break;
2070                 
2071         case LFUN_LEFTSEL:
2072                 update(TEXT(bv_),
2073                        BufferView::SELECT|BufferView::FITCUR);
2074                 if (TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params))
2075                         TEXT(bv_)->CursorRight(bv_);
2076                 else
2077                         TEXT(bv_)->CursorLeft(bv_);
2078                 TEXT(bv_)->FinishUndo();
2079                 moveCursorUpdate(true);
2080                 owner_->showState();
2081                 break;
2082                 
2083         case LFUN_UPSEL:
2084                 update(TEXT(bv_),
2085                        BufferView::SELECT|BufferView::FITCUR);
2086                 TEXT(bv_)->CursorUp(bv_);
2087                 TEXT(bv_)->FinishUndo();
2088                 moveCursorUpdate(true);
2089                 owner_->showState();
2090                 break;
2091                 
2092         case LFUN_DOWNSEL:
2093                 update(TEXT(bv_),
2094                        BufferView::SELECT|BufferView::FITCUR);
2095                 TEXT(bv_)->CursorDown(bv_);
2096                 TEXT(bv_)->FinishUndo();
2097                 moveCursorUpdate(true);
2098                 owner_->showState();
2099                 break;
2100
2101         case LFUN_UP_PARAGRAPHSEL:
2102                 update(TEXT(bv_),
2103                        BufferView::SELECT|BufferView::FITCUR);
2104                 TEXT(bv_)->CursorUpParagraph(bv_);
2105                 TEXT(bv_)->FinishUndo();
2106                 moveCursorUpdate(true);
2107                 owner_->showState();
2108                 break;
2109                 
2110         case LFUN_DOWN_PARAGRAPHSEL:
2111                 update(TEXT(bv_),
2112                        BufferView::SELECT|BufferView::FITCUR);
2113                 TEXT(bv_)->CursorDownParagraph(bv_);
2114                 TEXT(bv_)->FinishUndo();
2115                 moveCursorUpdate(true);
2116                 owner_->showState();
2117                 break;
2118                 
2119         case LFUN_PRIORSEL:
2120                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2121                 cursorPrevious(TEXT(bv_));
2122                 TEXT(bv_)->FinishUndo();
2123                 moveCursorUpdate(true);
2124                 owner_->showState();
2125                 break;
2126                 
2127         case LFUN_NEXTSEL:
2128                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2129                 cursorNext(TEXT(bv_));
2130                 TEXT(bv_)->FinishUndo();
2131                 moveCursorUpdate(true);
2132                 owner_->showState();
2133                 break;
2134                 
2135         case LFUN_HOMESEL:
2136                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2137                 TEXT(bv_)->CursorHome(bv_);
2138                 TEXT(bv_)->FinishUndo();
2139                 moveCursorUpdate(true);
2140                 owner_->showState();
2141                 break;
2142                 
2143         case LFUN_ENDSEL:
2144                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2145                 TEXT(bv_)->CursorEnd(bv_);
2146                 TEXT(bv_)->FinishUndo();
2147                 moveCursorUpdate(true);
2148                 owner_->showState();
2149                 break;
2150                 
2151         case LFUN_WORDRIGHTSEL:
2152                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2153                 if (TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params))
2154                         TEXT(bv_)->CursorLeftOneWord(bv_);
2155                 else
2156                         TEXT(bv_)->CursorRightOneWord(bv_);
2157                 TEXT(bv_)->FinishUndo();
2158                 moveCursorUpdate(true);
2159                 owner_->showState();
2160                 break;
2161                 
2162         case LFUN_WORDLEFTSEL:
2163                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2164                 if (TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params))
2165                         TEXT(bv_)->CursorRightOneWord(bv_);
2166                 else
2167                         TEXT(bv_)->CursorLeftOneWord(bv_);
2168                 TEXT(bv_)->FinishUndo();
2169                 moveCursorUpdate(true);
2170                 owner_->showState();
2171                 break;
2172                 
2173         case LFUN_BEGINNINGBUFSEL:
2174                 if (TEXT(bv_)->inset_owner)
2175                         break;
2176                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2177                 TEXT(bv_)->CursorTop(bv_);
2178                 TEXT(bv_)->FinishUndo();
2179                 moveCursorUpdate(true);
2180                 owner_->showState();
2181                 break;
2182                 
2183         case LFUN_ENDBUFSEL:
2184                 if (TEXT(bv_)->inset_owner)
2185                         break;
2186                 update(TEXT(bv_),
2187                        BufferView::SELECT|BufferView::FITCUR);
2188                 TEXT(bv_)->CursorBottom(bv_);
2189                 TEXT(bv_)->FinishUndo();
2190                 moveCursorUpdate(true);
2191                 owner_->showState();
2192                 break;
2193
2194                 // --- text changing commands ------------------------
2195         case LFUN_BREAKLINE:
2196                 beforeChange(TEXT(bv_));
2197                 TEXT(bv_)->InsertChar(bv_, LyXParagraph::META_NEWLINE);
2198                 update(TEXT(bv_),
2199                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2200                 moveCursorUpdate(false);
2201                 break;
2202                 
2203         case LFUN_PROTECTEDSPACE:
2204         {
2205                 LyXLayout const & style =
2206                         textclasslist.Style(buffer_->params.textclass,
2207                                             TEXT(bv_)->cursor.par()->GetLayout());
2208
2209                 if (style.free_spacing) {
2210                         TEXT(bv_)->InsertChar(bv_, ' ');
2211                         update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2212                 } else {
2213                         bv_->protectedBlank(TEXT(bv_));
2214                 }
2215                 moveCursorUpdate(false);
2216         }
2217         break;
2218                 
2219         case LFUN_SETMARK:
2220                 if (TEXT(bv_)->mark_set) {
2221                         beforeChange(TEXT(bv_));
2222                         update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2223                         owner_->getLyXFunc()->setMessage(N_("Mark removed"));
2224                 } else {
2225                         beforeChange(TEXT(bv_));
2226                         TEXT(bv_)->mark_set = 1;
2227                         update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2228                         owner_->getLyXFunc()->setMessage(N_("Mark set"));
2229                 }
2230                 TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
2231                 break;
2232                 
2233         case LFUN_DELETE:
2234                 if (!TEXT(bv_)->selection) {
2235                         TEXT(bv_)->Delete(bv_);
2236                         TEXT(bv_)->sel_cursor = 
2237                                 TEXT(bv_)->cursor;
2238                         update(TEXT(bv_),
2239                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2240                         // It is possible to make it a lot faster still
2241                         // just comment out the line below...
2242                         showCursor();
2243                 } else {
2244                         bv_->cut();
2245                 }
2246                 moveCursorUpdate(false);
2247                 owner_->showState();
2248                 setState();
2249                 break;
2250
2251         case LFUN_DELETE_SKIP:
2252         {
2253                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
2254                 
2255                 LyXCursor cursor = TEXT(bv_)->cursor;
2256
2257                 if (!TEXT(bv_)->selection) {
2258 #ifndef NEW_INSETS
2259                         if (cursor.pos() == cursor.par()->Last()) {
2260 #else
2261                         if (cursor.pos() == cursor.par()->size()) {
2262 #endif
2263                                 TEXT(bv_)->CursorRight(bv_);
2264                                 cursor = TEXT(bv_)->cursor;
2265                                 if (cursor.pos() == 0
2266                                     && !(cursor.par()->params.spaceTop()
2267                                          == VSpace (VSpace::NONE))) {
2268                                         TEXT(bv_)->SetParagraph
2269                                                 (bv_,
2270                                                  cursor.par()->params.lineTop(),
2271                                                  cursor.par()->params.lineBottom(),
2272                                                  cursor.par()->params.pagebreakTop(), 
2273                                                  cursor.par()->params.pagebreakBottom(),
2274                                                  VSpace(VSpace::NONE), 
2275                                                  cursor.par()->params.spaceBottom(),
2276                                                  cursor.par()->params.align(), 
2277                                                  cursor.par()->params.labelWidthString(), 0);
2278                                         TEXT(bv_)->CursorLeft(bv_);
2279                                         update(TEXT(bv_), 
2280                                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2281                                 } else {
2282                                         TEXT(bv_)->CursorLeft(bv_);
2283                                         TEXT(bv_)->Delete(bv_);
2284                                         TEXT(bv_)->sel_cursor = 
2285                                                 TEXT(bv_)->cursor;
2286                                         update(TEXT(bv_),
2287                                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2288                                 }
2289                         } else {
2290                                 TEXT(bv_)->Delete(bv_);
2291                                 TEXT(bv_)->sel_cursor = 
2292                                         TEXT(bv_)->cursor;
2293                                 update(TEXT(bv_),
2294                                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2295                         }
2296                 } else {
2297                         bv_->cut();
2298                 }
2299         }
2300         break;
2301
2302         /* -------> Delete word forward. */
2303         case LFUN_DELETE_WORD_FORWARD:
2304                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2305                 TEXT(bv_)->DeleteWordForward(bv_);
2306                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2307                 moveCursorUpdate(false);
2308                 owner_->showState();
2309                 break;
2310
2311                 /* -------> Delete word backward. */
2312         case LFUN_DELETE_WORD_BACKWARD:
2313                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2314                 TEXT(bv_)->DeleteWordBackward(bv_);
2315                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2316                 moveCursorUpdate(false);
2317                 owner_->showState();
2318                 break;
2319                 
2320                 /* -------> Kill to end of line. */
2321         case LFUN_DELETE_LINE_FORWARD:
2322                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2323                 TEXT(bv_)->DeleteLineForward(bv_);
2324                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2325                 moveCursorUpdate(false);
2326                 break;
2327                 
2328                 /* -------> Set mark off. */
2329         case LFUN_MARK_OFF:
2330                 beforeChange(TEXT(bv_));
2331                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2332                 TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
2333                 owner_->getLyXFunc()->setMessage(N_("Mark off"));
2334                 break;
2335
2336                 /* -------> Set mark on. */
2337         case LFUN_MARK_ON:
2338                 beforeChange(TEXT(bv_));
2339                 TEXT(bv_)->mark_set = 1;
2340                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2341                 TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
2342                 owner_->getLyXFunc()->setMessage(N_("Mark on"));
2343                 break;
2344                 
2345         case LFUN_BACKSPACE:
2346         {
2347                 if (!TEXT(bv_)->selection) {
2348                         if (owner_->getIntl()->getTrans().backspace()) {
2349                                 TEXT(bv_)->Backspace(bv_);
2350                                 TEXT(bv_)->sel_cursor = 
2351                                         TEXT(bv_)->cursor;
2352                                 update(TEXT(bv_),
2353                                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2354                                 // It is possible to make it a lot faster still
2355                                 // just comment out the line below...
2356                                 showCursor();
2357                         }
2358                 } else {
2359                         bv_->cut();
2360                 }
2361                 owner_->showState();
2362                 setState();
2363         }
2364         break;
2365
2366         case LFUN_BACKSPACE_SKIP:
2367         {
2368                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
2369                 
2370                 LyXCursor cursor = TEXT(bv_)->cursor;
2371                 
2372                 if (!TEXT(bv_)->selection) {
2373                         if (cursor.pos() == 0 
2374                             && !(cursor.par()->params.spaceTop() 
2375                                  == VSpace (VSpace::NONE))) {
2376                                 TEXT(bv_)->SetParagraph 
2377                                         (bv_,
2378                                          cursor.par()->params.lineTop(),      
2379                                          cursor.par()->params.lineBottom(),
2380                                          cursor.par()->params.pagebreakTop(), 
2381                                          cursor.par()->params.pagebreakBottom(),
2382                                          VSpace(VSpace::NONE), cursor.par()->params.spaceBottom(),
2383                                          cursor.par()->params.align(), 
2384                                          cursor.par()->params.labelWidthString(), 0);
2385                                 update(TEXT(bv_),
2386                                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2387                         } else {
2388                                 TEXT(bv_)->Backspace(bv_);
2389                                 TEXT(bv_)->sel_cursor 
2390                                         = cursor;
2391                                 update(TEXT(bv_),
2392                                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2393                         }
2394                 } else
2395                         bv_->cut();
2396         }
2397         break;
2398
2399         case LFUN_BREAKPARAGRAPH:
2400         {
2401                 beforeChange(TEXT(bv_));
2402                 TEXT(bv_)->BreakParagraph(bv_, 0);
2403                 update(TEXT(bv_),
2404                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2405                 TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
2406                 setState();
2407                 owner_->showState();
2408                 break;
2409         }
2410
2411         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
2412         {
2413                 beforeChange(TEXT(bv_));
2414                 TEXT(bv_)->BreakParagraph(bv_, 1);
2415                 update(TEXT(bv_),
2416                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2417                 TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
2418                 setState();
2419                 owner_->showState();
2420                 break;
2421         }
2422         
2423         case LFUN_BREAKPARAGRAPH_SKIP:
2424         {
2425                 // When at the beginning of a paragraph, remove
2426                 // indentation and add a "defskip" at the top.
2427                 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
2428                 
2429                 LyXCursor cursor = TEXT(bv_)->cursor;
2430                 
2431                 beforeChange(TEXT(bv_));
2432                 if (cursor.pos() == 0) {
2433                         if (cursor.par()->params.spaceTop() == VSpace(VSpace::NONE)) {
2434                                 TEXT(bv_)->SetParagraph
2435                                         (bv_,
2436                                          cursor.par()->params.lineTop(),      
2437                                          cursor.par()->params.lineBottom(),
2438                                          cursor.par()->params.pagebreakTop(), 
2439                                          cursor.par()->params.pagebreakBottom(),
2440                                          VSpace(VSpace::DEFSKIP), cursor.par()->params.spaceBottom(),
2441                                          cursor.par()->params.align(), 
2442                                          cursor.par()->params.labelWidthString(), 1);
2443                                 //update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2444                         } 
2445                 }
2446                 else {
2447                         TEXT(bv_)->BreakParagraph(bv_, 0);
2448                         //update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2449                 }
2450
2451                 update(TEXT(bv_),
2452                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2453                 TEXT(bv_)->sel_cursor = cursor;
2454                 setState();
2455                 owner_->showState();
2456         }
2457         break;
2458
2459         case LFUN_PARAGRAPH_SPACING:
2460         {
2461                 LyXParagraph * par = TEXT(bv_)->cursor.par();
2462                 Spacing::Space cur_spacing = par->params.spacing().getSpace();
2463                 float cur_value = 1.0;
2464                 if (cur_spacing == Spacing::Other) {
2465                         cur_value = par->params.spacing().getValue();
2466                 }
2467                 
2468                 istringstream istr(argument.c_str());
2469
2470                 string tmp;
2471                 istr >> tmp;
2472                 Spacing::Space new_spacing = cur_spacing;
2473                 float new_value = cur_value;
2474                 if (tmp.empty()) {
2475                         lyxerr << "Missing argument to `paragraph-spacing'"
2476                                << endl;
2477                 } else if (tmp == "single") {
2478                         new_spacing = Spacing::Single;
2479                 } else if (tmp == "onehalf") {
2480                         new_spacing = Spacing::Onehalf;
2481                 } else if (tmp == "double") {
2482                         new_spacing = Spacing::Double;
2483                 } else if (tmp == "other") {
2484                         new_spacing = Spacing::Other;
2485                         float tmpval = 0.0;
2486                         istr >> tmpval;
2487                         lyxerr << "new_value = " << tmpval << endl;
2488                         if (tmpval != 0.0)
2489                                 new_value = tmpval;
2490                 } else if (tmp == "default") {
2491                         new_spacing = Spacing::Default;
2492                 } else {
2493                         lyxerr << _("Unknown spacing argument: ")
2494                                << argument << endl;
2495                 }
2496                 if (cur_spacing != new_spacing || cur_value != new_value) {
2497                         par->params.spacing(Spacing(new_spacing, new_value));
2498                         TEXT(bv_)->RedoParagraph(bv_);
2499                         update(TEXT(bv_),
2500                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2501                 }
2502         }
2503         break;
2504         
2505         case LFUN_QUOTE:
2506                 beforeChange(TEXT(bv_));
2507                 TEXT(bv_)->InsertChar(bv_, '\"');  // This " matches the single quote in the code
2508                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2509                 moveCursorUpdate(false);
2510                 break;
2511
2512         case LFUN_HTMLURL:
2513         case LFUN_URL:
2514         {
2515                 InsetCommandParams p;
2516                 if (action == LFUN_HTMLURL)
2517                         p.setCmdName("htmlurl");
2518                 else
2519                         p.setCmdName("url");
2520                 owner_->getDialogs()->createUrl( p.getAsString() );
2521         }
2522         break;
2523                     
2524         case LFUN_INSERT_URL:
2525         {
2526                 InsetCommandParams p;
2527                 p.setFromString( argument );
2528
2529                 InsetUrl * inset = new InsetUrl( p );
2530                 if (!bv_->insertInset(inset))
2531                         delete inset;
2532                 else
2533                         bv_->updateInset( inset, true );
2534         }
2535         break;
2536                     
2537         case LFUN_INSET_TEXT:
2538         {
2539                 InsetText * new_inset = new InsetText;
2540                 if (bv_->insertInset(new_inset))
2541                         new_inset->Edit(bv_, 0, 0, 0);
2542                 else
2543                         delete new_inset;
2544         }
2545         break;
2546         
2547         case LFUN_INSET_ERT:
2548         {
2549                 InsetERT * new_inset = new InsetERT;
2550                 if (bv_->insertInset(new_inset))
2551                         new_inset->Edit(bv_, 0, 0, 0);
2552                 else
2553                         delete new_inset;
2554         }
2555         break;
2556         
2557         case LFUN_INSET_EXTERNAL:
2558         {
2559                 InsetExternal * new_inset = new InsetExternal;
2560                 if (bv_->insertInset(new_inset))
2561                         new_inset->Edit(bv_, 0, 0, 0);
2562                 else
2563                         delete new_inset;
2564         }
2565         break;
2566         
2567         case LFUN_INSET_FOOTNOTE:
2568         {
2569                 InsetFoot * new_inset = new InsetFoot;
2570                 if (bv_->insertInset(new_inset))
2571                         new_inset->Edit(bv_, 0, 0, 0);
2572                 else
2573                         delete new_inset;
2574         }
2575         break;
2576
2577         case LFUN_INSET_MARGINAL:
2578         {
2579                 InsetMarginal * new_inset = new InsetMarginal;
2580                 if (bv_->insertInset(new_inset))
2581                         new_inset->Edit(bv_, 0, 0, 0);
2582                 else
2583                         delete new_inset;
2584         }
2585         break;
2586
2587         case LFUN_INSET_MINIPAGE:
2588         {
2589                 InsetMinipage * new_inset = new InsetMinipage;
2590                 if (bv_->insertInset(new_inset))
2591                         new_inset->Edit(bv_, 0, 0, 0);
2592                 else
2593                         delete new_inset;
2594         }
2595         break;
2596
2597         case LFUN_INSET_FLOAT:
2598         {
2599                 // check if the float type exist
2600                 if (floatList.typeExist(argument)) {
2601                         InsetFloat * new_inset = new InsetFloat(argument);
2602                         if (bv_->insertInset(new_inset))
2603                                 new_inset->Edit(bv_, 0, 0, 0);
2604                         else
2605                                 delete new_inset;
2606                 } else {
2607                         lyxerr << "Non-existant float type: "
2608                                << argument << endl;
2609                 }
2610                 
2611         }
2612         break;
2613
2614         case LFUN_INSET_LIST:
2615         {
2616                 InsetList * new_inset = new InsetList;
2617                 if (bv_->insertInset(new_inset))
2618                         new_inset->Edit(bv_, 0, 0, 0);
2619                 else
2620                         delete new_inset;
2621         }
2622         break;
2623
2624         case LFUN_INSET_THEOREM:
2625         {
2626                 InsetTheorem * new_inset = new InsetTheorem;
2627                 if (bv_->insertInset(new_inset))
2628                         new_inset->Edit(bv_, 0, 0, 0);
2629                 else
2630                         delete new_inset;
2631         }
2632         break;
2633
2634         case LFUN_INSET_CAPTION:
2635         {
2636                 // Do we have a locking inset...
2637                 if (bv_->theLockingInset()) {
2638                         lyxerr << "Locking inset code: "
2639                                << static_cast<int>(bv_->theLockingInset()->LyxCode());
2640                         InsetCaption * new_inset = new InsetCaption;
2641                         new_inset->setOwner(bv_->theLockingInset());
2642                         new_inset->SetAutoBreakRows(true);
2643                         new_inset->SetDrawFrame(0, InsetText::LOCKED);
2644                         new_inset->SetFrameColor(0, LColor::footnoteframe);
2645                         if (bv_->insertInset(new_inset))
2646                                 new_inset->Edit(bv_, 0, 0, 0);
2647                         else
2648                                 delete new_inset;
2649                 }
2650         }
2651         break;
2652         
2653         case LFUN_INSET_TABULAR:
2654         {
2655                 int r = 2, c = 2;
2656                 if (!argument.empty())
2657                         ::sscanf(argument.c_str(),"%d%d", &r, &c);
2658                 InsetTabular * new_inset =
2659                         new InsetTabular(*buffer_, r, c);
2660                 bool rtl =
2661                         TEXT(bv_)->real_current_font.isRightToLeft();
2662                 if (!bv_->open_new_inset(new_inset, rtl))
2663                         delete new_inset;
2664         }
2665         break;
2666
2667         // --- lyxserver commands ----------------------------
2668
2669         case LFUN_CHARATCURSOR:
2670         {
2671                 LyXParagraph::size_type pos = TEXT(bv_)->cursor.pos();
2672                 if (pos < TEXT(bv_)->cursor.par()->size())
2673                         owner_->getLyXFunc()->setMessage(
2674                                 tostr(TEXT(bv_)->cursor.par()->GetChar(pos)));
2675                 else
2676                         owner_->getLyXFunc()->setMessage("EOF");
2677         }
2678         break;
2679         
2680         case LFUN_GETXY:
2681                 owner_->getLyXFunc()->setMessage(tostr(TEXT(bv_)->cursor.x())
2682                                                  + ' '
2683                                                  + tostr(TEXT(bv_)->cursor.y()));
2684                 break;
2685                 
2686         case LFUN_SETXY:
2687         {
2688                 int x;
2689                 int y;
2690                 ::sscanf(argument.c_str(), " %d %d", &x, &y);
2691                 TEXT(bv_)->SetCursorFromCoordinates(bv_, x, y);
2692         }
2693         break;
2694         
2695         case LFUN_GETLAYOUT:
2696                 owner_->getLyXFunc()->setMessage(tostr(TEXT(bv_)->cursor.par()->layout));
2697                 break;
2698                         
2699         case LFUN_GETFONT:
2700         {
2701                 LyXFont & font = TEXT(bv_)->current_font;
2702                 if (font.shape() == LyXFont::ITALIC_SHAPE)
2703                         owner_->getLyXFunc()->setMessage("E");
2704                 else if (font.shape() == LyXFont::SMALLCAPS_SHAPE)
2705                         owner_->getLyXFunc()->setMessage("N");
2706                 else
2707                         owner_->getLyXFunc()->setMessage("0");
2708
2709         }
2710         break;
2711
2712         case LFUN_GETLATEX:
2713         {
2714                 LyXFont & font = TEXT(bv_)->current_font;
2715                 if (font.latex() == LyXFont::ON)
2716                         owner_->getLyXFunc()->setMessage("L");
2717                 else
2718                         owner_->getLyXFunc()->setMessage("0");
2719         }
2720         break;
2721
2722         // --- accented characters ---------------------------
2723                 
2724         case LFUN_UMLAUT:
2725         case LFUN_CIRCUMFLEX:
2726         case LFUN_GRAVE:
2727         case LFUN_ACUTE:
2728         case LFUN_TILDE:
2729         case LFUN_CEDILLA:
2730         case LFUN_MACRON:
2731         case LFUN_DOT:
2732         case LFUN_UNDERDOT:
2733         case LFUN_UNDERBAR:
2734         case LFUN_CARON:
2735         case LFUN_SPECIAL_CARON:
2736         case LFUN_BREVE:
2737         case LFUN_TIE:
2738         case LFUN_HUNG_UMLAUT:
2739         case LFUN_CIRCLE:
2740         case LFUN_OGONEK:
2741                 owner_->getLyXFunc()->handleKeyFunc(action);
2742                 break;
2743         
2744         // --- insert characters ----------------------------------------
2745         
2746         case LFUN_MATH_DELIM:     
2747         case LFUN_INSERT_MATRIX:
2748         {          
2749                 if (available()) { 
2750                         if (bv_->open_new_inset(new InsetFormula(false)))
2751                         {
2752                                 bv_->theLockingInset()
2753                                         ->LocalDispatch(bv_, action, argument);
2754                         }
2755                 }
2756         }          
2757         break;
2758                
2759         case LFUN_INSERT_MATH:
2760         {
2761                 if (!available())
2762                         break;
2763  
2764                 InsetFormula * f = new InsetFormula(true);
2765                 bv_->open_new_inset(f);
2766                 f->LocalDispatch(bv_, LFUN_INSERT_MATH, argument);
2767         }
2768         break;
2769         
2770         case LFUN_MATH_DISPLAY:
2771         {
2772                 if (available())
2773                         bv_->open_new_inset(new InsetFormula(true));
2774                 break;
2775         }
2776                     
2777         case LFUN_MATH_MACRO:
2778         {
2779                 if (available()) {
2780                         string s(argument);
2781                         if (s.empty())
2782                                 owner_->getLyXFunc()->setErrorMessage(N_("Missing argument"));
2783                         else {
2784                                 string const s1 = token(s, ' ', 1);
2785                                 int const na = s1.empty() ? 0 : lyx::atoi(s1);
2786                                 bv_->open_new_inset(new InsetFormulaMacro(token(s, ' ', 0), na));
2787                         }
2788                 }
2789         }
2790         break;
2791
2792         case LFUN_MATH_MODE:   // Open or create a math inset
2793         {               
2794                 if (available())
2795                         bv_->open_new_inset(new InsetFormula);
2796                 owner_->getLyXFunc()->setMessage(N_("Math editor mode"));
2797         }
2798         break;
2799           
2800         case LFUN_CITATION_INSERT:
2801         {
2802                 InsetCommandParams p;
2803                 p.setFromString( argument );
2804
2805                 InsetCitation * inset = new InsetCitation( p );
2806                 if (!bv_->insertInset(inset))
2807                         delete inset;
2808                 else
2809                         bv_->updateInset( inset, true );
2810         }
2811         break;
2812                     
2813         case LFUN_INSERT_BIBTEX:
2814         {   
2815                 // ale970405+lasgoutt970425
2816                 // The argument can be up to two tokens separated 
2817                 // by a space. The first one is the bibstyle.
2818                 string const db       = token(argument, ' ', 0);
2819                 string bibstyle = token(argument, ' ', 1);
2820                 if (bibstyle.empty())
2821                         bibstyle = "plain";
2822
2823                 InsetCommandParams p( "BibTeX", db, bibstyle );
2824                 InsetBibtex * inset = new InsetBibtex(p);
2825                 
2826                 if (bv_->insertInset(inset)) {
2827                         if (argument.empty())
2828                                 inset->Edit(bv_, 0, 0, 0);
2829                 } else
2830                         delete inset;
2831         }
2832         break;
2833                 
2834         // BibTeX data bases
2835         case LFUN_BIBDB_ADD:
2836         {
2837                 InsetBibtex * inset = 
2838                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2839                 if (inset) {
2840                         inset->addDatabase(argument);
2841                 }
2842         }
2843         break;
2844                     
2845         case LFUN_BIBDB_DEL:
2846         {
2847                 InsetBibtex * inset = 
2848                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2849                 if (inset) {
2850                         inset->delDatabase(argument);
2851                 }
2852         }
2853         break;
2854         
2855         case LFUN_BIBTEX_STYLE:
2856         {
2857                 InsetBibtex * inset = 
2858                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2859                 if (inset) {
2860                         inset->setOptions(argument);
2861                 }
2862         }
2863         break;
2864                 
2865         case LFUN_INDEX_CREATE:
2866         {
2867                 InsetCommandParams p( "index" );
2868                 
2869                 if (argument.empty()) {
2870                         // Get the word immediately preceding the cursor
2871                         LyXParagraph::size_type curpos = 
2872                                 TEXT(bv_)->cursor.pos() - 1;
2873
2874                         string curstring;
2875                         if (curpos >= 0 )
2876                                 curstring = TEXT(bv_)
2877                                         ->cursor.par()->GetWord(curpos);
2878
2879                         p.setContents( curstring );
2880                 } else {
2881                         p.setContents( argument );
2882                 }
2883
2884                 owner_->getDialogs()->createIndex( p.getAsString() );
2885         }
2886         break;
2887                     
2888         case LFUN_INDEX_INSERT:
2889         {
2890                 InsetCommandParams p;
2891                 p.setFromString(argument);
2892                 InsetIndex * inset = new InsetIndex(p);
2893
2894                 if (!bv_->insertInset(inset))
2895                         delete inset;
2896                 else
2897                         bv_->updateInset(inset, true);
2898         }
2899         break;
2900                     
2901         case LFUN_INDEX_INSERT_LAST:
2902         {
2903                 // Get word immediately preceding the cursor
2904                 LyXParagraph::size_type curpos = 
2905                         TEXT(bv_)->cursor.pos() - 1;
2906                 // Can't do that at the beginning of a paragraph
2907                 if (curpos < 0) break;
2908
2909                 string const curstring(TEXT(bv_)
2910                                        ->cursor.par()->GetWord(curpos));
2911
2912                 InsetCommandParams p("index", curstring);
2913                 InsetIndex * inset = new InsetIndex(p);
2914
2915                 if (!bv_->insertInset(inset))
2916                         delete inset;
2917                 else
2918                         bv_->updateInset(inset, true);
2919         }
2920         break;
2921
2922         case LFUN_INDEX_PRINT:
2923         {
2924                 InsetCommandParams p("printindex");
2925                 Inset * inset = new InsetPrintIndex(p);
2926                 if (!bv_->insertInset(inset, "Standard", true))
2927                         delete inset;
2928         }
2929         break;
2930
2931         case LFUN_PARENTINSERT:
2932         {
2933                 lyxerr << "arg " << argument << endl;
2934                 InsetCommandParams p( "lyxparent", argument );
2935                 Inset * inset = new InsetParent(p, *buffer_);
2936                 if (!bv_->insertInset(inset, "Standard", true))
2937                         delete inset;
2938         }
2939                  
2940         break;
2941
2942         case LFUN_CHILD_INSERT:
2943         {
2944                 InsetInclude::Params p;
2945                 p.cparams.setFromString(argument);
2946                 p.masterFilename_ = buffer_->fileName();
2947
2948                 InsetInclude * inset = new InsetInclude(p);
2949                 if (!bv_->insertInset(inset))
2950                         delete inset;
2951                 else {
2952                         bv_->updateInset(inset, true);
2953                         bv_->owner()->getDialogs()->showInclude(inset);
2954                 }
2955         }
2956         break; 
2957
2958         case LFUN_INSERT_NOTE:
2959                 bv_->insertNote();
2960                 break;
2961
2962         case LFUN_SELFINSERT:
2963         {
2964                 LyXFont const old_font(TEXT(bv_)->real_current_font);
2965                 for (string::size_type i = 0; i < argument.length(); ++i) {
2966                         TEXT(bv_)->InsertChar(bv_, argument[i]);
2967                         // This needs to be in the loop, or else we
2968                         // won't break lines correctly. (Asger)
2969                         update(TEXT(bv_),
2970                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2971                 }
2972                 TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
2973                 moveCursorUpdate(false);
2974
2975                 // real_current_font.number can change so we need to 
2976                 // update the minibuffer
2977                 if (old_font != TEXT(bv_)->real_current_font)
2978                         owner_->showState();
2979         }
2980         break;
2981
2982         case LFUN_DATE_INSERT:  // jdblair: date-insert cmd
2983         {
2984                 struct tm * now_tm;
2985                 
2986                 time_t now_time_t = time(NULL);
2987                 now_tm = localtime(&now_time_t);
2988                 setlocale(LC_TIME, "");
2989                 string arg;
2990                 if (!argument.empty())
2991                         arg = argument;
2992                 else 
2993                         arg = lyxrc.date_insert_format;
2994                 char datetmp[32];
2995                 int const datetmp_len =
2996                         ::strftime(datetmp, 32, arg.c_str(), now_tm);
2997                 for (int i = 0; i < datetmp_len; i++) {
2998                         TEXT(bv_)->InsertChar(bv_, datetmp[i]);
2999                         update(TEXT(bv_),
3000                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
3001                 }
3002
3003                 TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
3004                 moveCursorUpdate(false);
3005         }
3006         break;
3007
3008         case LFUN_UNKNOWN_ACTION:
3009         {
3010                 if (!buffer_) {
3011                         owner_->getLyXFunc()->setErrorMessage(N_("No document open"));
3012                         break;
3013                 }
3014
3015                 if (buffer_->isReadonly()) {
3016                         owner_->getLyXFunc()->setErrorMessage(N_("Document is read only"));
3017                         break;
3018                 }
3019                 if (!argument.empty()) {
3020                         /* Automatically delete the currently selected
3021                          * text and replace it with what is being
3022                          * typed in now. Depends on lyxrc settings
3023                          * "auto_region_delete", which defaults to
3024                          * true (on). */
3025                 
3026                         if (lyxrc.auto_region_delete) {
3027                                 if (TEXT(bv_)->selection){
3028                                         TEXT(bv_)->CutSelection(bv_, false);
3029                                         update(TEXT(bv_),
3030                                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
3031                                 }
3032                         }
3033                         
3034                         beforeChange(TEXT(bv_));
3035                         LyXFont const old_font(TEXT(bv_)->real_current_font);
3036                         for (string::size_type i = 0;
3037                              i < argument.length(); ++i) {
3038                                 if (greek_kb_flag) {
3039                                         if (!math_insert_greek(bv_, argument[i]))
3040                                                 owner_->getIntl()->getTrans().TranslateAndInsert(argument[i], TEXT(bv_));
3041                                 } else
3042                                         owner_->getIntl()->getTrans().TranslateAndInsert(argument[i], TEXT(bv_));
3043                         }
3044
3045                         update(TEXT(bv_),BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
3046
3047                         TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
3048                         moveCursorUpdate(false);
3049
3050                         // real_current_font.number can change so we need to
3051                         // update the minibuffer
3052                         if (old_font != TEXT(bv_)->real_current_font)
3053                                 owner_->showState();
3054                 } else {
3055                         // if we don't have an argument there was something
3056                         // strange going on so we just tell this to someone!
3057                         owner_->getLyXFunc()->setErrorMessage(N_("No argument given"));
3058                 }
3059                 break;
3060         }
3061         default:
3062                 return false;
3063         } // end of switch
3064
3065         return true;
3066 }