]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
718f11bd8fa7a0b2dd0b3e2eb8dc1280e0619ddc
[lyx.git] / src / BufferView.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *        
7  *           Copyright 1995 Matthias Ettrich
8  *           Copyright 1995-1999 The LyX Team.
9  *
10  * ====================================================== */
11
12 #include <config.h>
13
14 #include <cstdlib>
15 #include <csignal>
16
17 #include <unistd.h>
18 #include <sys/wait.h>
19
20 #include "support/lstrings.h"
21
22 #ifdef __GNUG__
23 #pragma implementation
24 #endif
25
26 #include "commandtags.h"
27 #include "BufferView.h"
28 #include "bufferlist.h"
29 #include "LyXView.h"
30 #include "lyxfunc.h"
31 #include "insets/lyxinset.h"
32 #include "minibuffer.h"
33 #include "lyxscreen.h"
34 #include "up.xpm"
35 #include "down.xpm"
36 #include "debug.h"
37 #include "lyxdraw.h"
38 #include "lyx_gui_misc.h"
39 #include "BackStack.h"
40 #include "lyxtext.h"
41 #include "lyx_cb.h"
42 #include "gettext.h"
43 #include "layout.h"
44
45 extern BufferList bufferlist;
46 void sigchldhandler(pid_t pid, int *status);
47
48 extern void SetXtermCursor(Window win);
49 extern bool input_prohibited;
50 extern bool selection_possible;
51 extern void BeforeChange();
52 extern char ascii_type;
53 extern int UnlockInset(UpdatableInset* inset);
54 extern void ToggleFloat();
55 extern void MenuPasteSelection(char at);
56 extern InsetUpdateStruct *InsetUpdateList;
57 extern void UpdateInsetUpdateList();
58 extern void FreeUpdateTimer();
59
60 // This is _very_ temporary
61 FL_OBJECT * figinset_canvas;
62
63 BufferView::BufferView(LyXView *o, int xpos, int ypos,
64                        int width, int height)
65         : owner_(o)
66 {
67         buffer_ = 0;
68 #ifdef MOVE_TEXT
69         text = 0;
70 #endif
71         screen = 0;
72         work_area = 0;
73         figinset_canvas = 0;
74         scrollbar = 0;
75         button_down = 0;
76         button_up = 0;
77         timer_cursor = 0;
78         current_scrollbar_value = 0;
79         create_view(xpos, ypos, width, height);
80         // Activate the timer for the cursor 
81         fl_set_timer(timer_cursor, 0.4);
82         fl_set_focus_object(owner_->getForm(), work_area);
83         work_area_focus = true;
84         lyx_focus = false;
85 }
86
87
88 BufferView::~BufferView()
89 {
90 #ifdef MOVE_TEXT
91         delete text;
92 #endif
93 }
94
95
96 void BufferView::buffer(Buffer *b)
97 {
98         lyxerr[Debug::INFO] << "Setting buffer in BufferView" << endl;
99         if (buffer_) {
100                 buffer_->InsetSleep();
101                 buffer_->delUser(this);
102 #ifdef MOVE_TEXT
103                 delete text;
104                 text = 0;
105 #endif
106         }
107
108         // Set current buffer
109         buffer_ = b;
110
111         if (bufferlist.getState() == BufferList::CLOSING) return;
112         
113         // Nuke old image
114         if (screen)
115                 delete screen;
116         screen = 0;
117
118         // If we are closing the buffer, use the first buffer as current
119         if (!buffer_) {
120                 buffer_ = bufferlist.first();
121         }
122
123         if (buffer_) {
124                 lyxerr[Debug::INFO] << "  Buffer addr: " << buffer_ << endl;
125                 buffer_->addUser(this);
126                 owner_->getMenus()->showMenus();
127                 // If we don't have a text object for this, we make one
128 #ifdef MOVE_TEXT
129                 if (text == 0)
130                         resizeCurrentBuffer();
131                 else {
132                         updateScreen();
133                         updateScrollbar();
134                 }
135 #else
136                 if (buffer_->text == 0)
137                         resizeCurrentBuffer();
138                 else {
139                         updateScreen();
140                         updateScrollbar();
141                 }
142 #endif
143                 screen->first = screen->TopCursorVisible();
144                 redraw();
145                 updateAllVisibleBufferRelatedPopups();
146                 buffer_->InsetWakeup();
147         } else {
148                 lyxerr[Debug::INFO] << "  No Buffer!" << endl;
149                 owner_->getMenus()->hideMenus();
150                 updateScrollbar();
151                 fl_redraw_object(work_area);
152         }
153         // should update layoutchoice even if we don't have a buffer.
154         owner_->updateLayoutChoice();
155         owner_->getMiniBuffer()->Init();
156         owner_->updateWindowTitle();
157 }
158
159
160 void BufferView::updateScreen()
161 {
162         // Regenerate the screen.
163         if (screen)
164                 delete screen;
165 #ifdef MOVE_TEXT
166         screen = new LyXScreen(FL_ObjWin(work_area),
167                                work_area->w,
168                                work_area->h,
169                                work_area->x,
170                                work_area->y,
171                                text);
172 #else
173         screen = new LyXScreen(FL_ObjWin(work_area),
174                                work_area->w,
175                                work_area->h,
176                                work_area->x,
177                                work_area->y,
178                                buffer_->text);
179 #endif
180 }
181
182
183 void BufferView::resize()
184 {
185         // This will resize the buffer. (Asger)
186         if (buffer_)
187                 resizeCurrentBuffer();
188 }
189
190
191 static bool lgb_hack = false;
192
193 void BufferView::redraw()
194 {
195         lyxerr[Debug::INFO] << "BufferView::redraw()" << endl;
196         lgb_hack = true;
197         fl_redraw_object(work_area);
198         fl_redraw_object(scrollbar);
199         fl_redraw_object(button_down);
200         fl_redraw_object(button_up);
201         lgb_hack = false;
202 }
203
204
205 void BufferView::fitCursor()
206 {
207         if (screen) screen->FitCursor();
208 }
209
210
211 void BufferView::update()
212 {
213         if (screen) screen->Update();
214 }
215
216
217 void BufferView::updateScrollbar()
218 {
219         /* If the text is smaller than the working area, the scrollbar
220          * maximum must be the working area height. No scrolling will 
221          * be possible */
222
223         if (!buffer_) {
224                 fl_set_slider_value(scrollbar, 0);
225                 fl_set_slider_size(scrollbar, scrollbar->h);
226                 return;
227         }
228         
229         static long max2 = 0;
230         static long height2 = 0;
231
232         long cbth = 0;
233         long cbsf = 0;
234
235 #ifdef MOVE_TEXT
236         if (text)
237                 cbth = text->height;
238 #else
239         if (buffer_->text)
240                 cbth = buffer_->text->height;
241 #endif
242         if (screen)
243                 cbsf = screen->first;
244
245         // check if anything has changed.
246         if (max2 == cbth &&
247             height2 == work_area->h &&
248             current_scrollbar_value == cbsf)
249                 return;       // no
250         
251         max2 = cbth;
252         height2 = work_area->h;
253         current_scrollbar_value = cbsf;
254
255         if (cbth <= height2) { // text is smaller than screen
256                 fl_set_slider_size(scrollbar, scrollbar->h);
257                 return;
258         }
259         
260         long maximum_height = work_area->h * 3/4 + cbth;
261         long value = cbsf;
262
263         // set the scrollbar
264         double hfloat = work_area->h;
265         double maxfloat = maximum_height;
266    
267         fl_set_slider_value(scrollbar, value);
268         fl_set_slider_bounds(scrollbar, 0,
269                              maximum_height - work_area->h);
270 #if FL_REVISION > 85
271 #ifdef MOVE_TEXT
272         double lineh = text->DefaultHeight();
273 #else
274         double lineh = buffer_->text->DefaultHeight();
275 #endif
276         fl_set_slider_increment(scrollbar, work_area->h-lineh, lineh);
277 #endif
278         if (maxfloat > 0){
279                 if ((hfloat / maxfloat) * float(height2) < 3)
280                         fl_set_slider_size(scrollbar,
281                                            3 / float(height2));
282                 else
283                         fl_set_slider_size(scrollbar,
284                                            hfloat / maxfloat);
285         } else
286                 fl_set_slider_size(scrollbar, hfloat);
287         fl_set_slider_precision(scrollbar, 0);
288 }
289
290
291 void BufferView::redoCurrentBuffer()
292 {
293         lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
294 #ifdef MOVE_TEXT
295         if (buffer_ && text) {
296                 resize();
297                 owner_->updateLayoutChoice();
298         }
299 #else
300         if (buffer_ && buffer_->text) {
301                 resize();
302                 owner_->updateLayoutChoice();
303         }
304 #endif
305 }
306
307
308 int BufferView::resizeCurrentBuffer()
309 {
310         lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
311         
312         LyXParagraph * par = 0;
313         LyXParagraph * selstartpar = 0;
314         LyXParagraph * selendpar = 0;
315         int pos = 0;
316         int selstartpos = 0;
317         int selendpos = 0;
318         int selection = 0;
319         int mark_set = 0;
320
321         ProhibitInput();
322
323         owner_->getMiniBuffer()->Set(_("Formatting document..."));   
324
325 #ifdef MOVE_TEXT
326         if (text) {
327                 par = text->cursor.par;
328                 pos = text->cursor.pos;
329                 selstartpar = text->sel_start_cursor.par;
330                 selstartpos = text->sel_start_cursor.pos;
331                 selendpar = text->sel_end_cursor.par;
332                 selendpos = text->sel_end_cursor.pos;
333                 selection = text->selection;
334                 mark_set = text->mark_set;
335                 delete text;
336         }
337         text = new LyXText(work_area->w, buffer_);
338 #else
339         if (buffer_->text) {
340                 par = buffer_->text->cursor.par;
341                 pos = buffer_->text->cursor.pos;
342                 selstartpar = buffer_->text->sel_start_cursor.par;
343                 selstartpos = buffer_->text->sel_start_cursor.pos;
344                 selendpar = buffer_->text->sel_end_cursor.par;
345                 selendpos = buffer_->text->sel_end_cursor.pos;
346                 selection = buffer_->text->selection;
347                 mark_set = buffer_->text->mark_set;
348                 delete buffer_->text;
349         }
350         buffer_->text = new LyXText(work_area->w, buffer_);
351 #endif
352
353         updateScreen();
354
355 #ifdef MOVE_TEXT
356         if (par) {
357                 text->selection = true;
358                 /* at this point just
359                  * to avoid the Delete-
360                  * Empty-Paragraph
361                  * Mechanism when
362                  * setting the cursor */
363                 text->mark_set = mark_set;
364                 if (selection) {
365                         text->SetCursor(selstartpar, selstartpos);
366                         text->sel_cursor = text->cursor;
367                         text->SetCursor(selendpar, selendpos);
368                         text->SetSelection();
369                         text->SetCursor(par, pos);
370                 } else {
371                        text->SetCursor(par, pos);
372                         text->sel_cursor = text->cursor;
373                         text->selection = false;
374                 }
375         }
376 #else
377         if (par) {
378                 buffer_->text->selection = true;
379                 /* at this point just
380                  * to avoid the Delete-
381                  * Empty-Paragraph
382                  * Mechanism when
383                  * setting the cursor */
384                 buffer_->text->mark_set = mark_set;
385                 if (selection) {
386                         buffer_->text->SetCursor(selstartpar, selstartpos);
387                         buffer_->text->sel_cursor = buffer_->text->cursor;
388                         buffer_->text->SetCursor(selendpar, selendpos);
389                         buffer_->text->SetSelection();
390                         buffer_->text->SetCursor(par, pos);
391                 } else {
392                         buffer_->text->SetCursor(par, pos);
393                         buffer_->text->sel_cursor = buffer_->text->cursor;
394                         buffer_->text->selection = false;
395                 }
396         }
397 #endif
398         screen->first = screen->TopCursorVisible(); /* this will scroll the
399                                                      * screen such that the
400                                                      * cursor becomes
401                                                      * visible */ 
402         updateScrollbar();
403         redraw();
404         owner_->getMiniBuffer()->Init();
405         AllowInput();
406
407         // Now if the title form still exist kill it
408         TimerCB(0, 0);
409
410         return 0;
411 }
412
413
414 void BufferView::gotoError()
415 {
416         if (!screen)
417                 return;
418    
419         screen->HideCursor();
420         BeforeChange();
421 #ifdef MOVE_TEXT
422         update(-2);
423 #else
424         buffer_->update(-2);
425 #endif
426         LyXCursor tmp;
427
428 #ifdef MOVE_TEXT
429         if (!text->GotoNextError()) {
430                 if (text->cursor.pos 
431                     || text->cursor.par != text->FirstParagraph()) {
432                         tmp = text->cursor;
433                         text->cursor.par = text->FirstParagraph();
434                         text->cursor.pos = 0;
435                         if (!text->GotoNextError()) {
436                                 text->cursor = tmp;
437                                 owner_->getMiniBuffer()->Set(_("No more errors"));
438                                 LyXBell();
439                         }
440                 } else {
441                         owner_->getMiniBuffer()->Set(_("No more errors"));
442                         LyXBell();
443                 }
444         }
445         update(0);
446         text->sel_cursor = text->cursor;
447 #else
448         if (!buffer_->text->GotoNextError()) {
449                 if (buffer_->text->cursor.pos 
450                     || buffer_->text->cursor.par != 
451                     buffer_->text->FirstParagraph()) {
452                         tmp = buffer_->text->cursor;
453                         buffer_->text->cursor.par = 
454                                 buffer_->text->FirstParagraph();
455                         buffer_->text->cursor.pos = 0;
456                         if (!buffer_->text->GotoNextError()) {
457                                 buffer_->text->cursor = tmp;
458                                 owner_->getMiniBuffer()->Set(_("No more errors"));
459                                 LyXBell();
460                         }
461                 } else {
462                         owner_->getMiniBuffer()->Set(_("No more errors"));
463                         LyXBell();
464                 }
465         }
466         buffer_->update(0);
467         buffer_->text->sel_cursor = 
468                 buffer_->text->cursor;
469 #endif
470 }
471
472
473 // Just a bunch of C wrappers around static members of BufferView
474 extern "C" void C_BufferView_UpCB(FL_OBJECT * ob, long buf) {
475         BufferView::UpCB(ob, buf);
476 }
477
478 extern "C" void C_BufferView_DownCB(FL_OBJECT * ob, long buf) {
479         BufferView::DownCB(ob, buf);
480 }
481
482 extern "C" void C_BufferView_ScrollCB(FL_OBJECT * ob, long buf) {
483         BufferView::ScrollCB(ob, buf);
484 }
485
486 extern "C" void C_BufferView_CursorToggleCB(FL_OBJECT * ob, long buf) {
487         BufferView::CursorToggleCB(ob, buf);
488 }
489
490 extern "C" int C_BufferView_work_area_handler(FL_OBJECT * ob, int event,
491                                               FL_Coord, FL_Coord, 
492                                               int key, void * xev) {
493         return BufferView::work_area_handler(ob, event, 0, 0, key, xev);
494 }
495
496
497 void BufferView::create_view(int xpos, int ypos, int width, int height)
498 {
499         FL_OBJECT * obj;
500         const int bw = abs(fl_get_border_width());
501
502         // a hack for the figinsets (Matthias)
503         // This one first, then it will probably be invisible. (Lgb)
504         ::figinset_canvas = figinset_canvas = obj = 
505                 fl_add_canvas(FL_NORMAL_CANVAS,
506                               xpos + 1,
507                               ypos + 1, 1, 1, "");
508         fl_set_object_boxtype(obj, FL_NO_BOX);
509         fl_set_object_resize(obj, FL_RESIZE_ALL);
510         fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
511
512         // a box
513         obj = fl_add_box(FL_BORDER_BOX, xpos, ypos,
514                          width - 15,
515                          height, "");
516         fl_set_object_resize(obj, FL_RESIZE_ALL);
517         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
518
519         // the free object
520         work_area = obj = fl_add_free(FL_INPUT_FREE,
521                                       xpos +bw, ypos+bw,
522                                       width-15-2*bw /* scrollbarwidth */,
523                                       height-2*bw, "",
524                                       C_BufferView_work_area_handler);
525         obj->wantkey = FL_KEY_TAB;
526         obj->u_vdata = this; /* This is how we pass the BufferView
527                                        to the work_area_handler. */
528         fl_set_object_boxtype(obj, FL_DOWN_BOX);
529         fl_set_object_resize(obj, FL_RESIZE_ALL);
530         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
531
532         //
533         // THE SCROLLBAR
534         //
535
536         // up - scrollbar button
537 #if FL_REVISION > 85
538         fl_set_border_width(-1);
539 #else
540         fl_set_border_width(-2); // to get visible feedback
541 #endif
542         button_up = obj = fl_add_pixmapbutton(FL_TOUCH_BUTTON,
543                                               width-15+4*bw,
544                                               ypos,
545                                               15, 15, "");
546         fl_set_object_boxtype(obj, FL_UP_BOX);
547         fl_set_object_color(obj, FL_MCOL, FL_BLUE);
548         fl_set_object_resize(obj, FL_RESIZE_ALL);
549         fl_set_object_gravity(obj, NorthEastGravity, NorthEastGravity);
550         fl_set_object_callback(obj, C_BufferView_UpCB, 0);
551         obj->u_vdata = this;
552         fl_set_pixmapbutton_data(obj, const_cast<char**>(up_xpm));
553
554 #if FL_REVISION > 85
555         // Remove the blue feedback rectangle
556         fl_set_pixmapbutton_focus_outline(obj, 0);
557 #endif  
558
559         // the scrollbar slider
560         fl_set_border_width(-bw);
561         scrollbar = obj = fl_add_slider(FL_VERT_SLIDER,
562                                         width-15+4*bw,
563                                         ypos + 15,
564                                         15, height-30, "");
565         fl_set_object_color(obj, FL_COL1, FL_MCOL);
566         fl_set_object_boxtype(obj, FL_UP_BOX);
567         fl_set_object_resize(obj, FL_RESIZE_ALL);
568         fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
569         fl_set_object_callback(obj, C_BufferView_ScrollCB, 0);
570         obj->u_vdata = this;
571         
572         // down - scrollbar button
573 #if FL_REVISION > 85
574         fl_set_border_width(-1);
575 #else
576         fl_set_border_width(-2); // to get visible feedback
577 #endif
578         button_down = obj = fl_add_pixmapbutton(FL_TOUCH_BUTTON,
579                                                       width-15+4*bw,
580                                                       ypos + height-15,
581                                                       15, 15, "");
582         fl_set_object_boxtype(obj, FL_UP_BOX);
583         fl_set_object_color(obj, FL_MCOL, FL_BLUE);
584         fl_set_object_resize(obj, FL_RESIZE_ALL);
585         fl_set_object_gravity(obj, SouthEastGravity, SouthEastGravity);
586         fl_set_object_callback(obj, C_BufferView_DownCB, 0);
587         obj->u_vdata = this;
588         fl_set_pixmapbutton_data(obj, const_cast<char**>(down_xpm));
589         fl_set_border_width(-bw);
590
591 #if FL_REVISION >85
592         // Remove the blue feedback rectangle
593         fl_set_pixmapbutton_focus_outline(obj, 0);
594 #endif  
595
596         //
597         // TIMERS
598         //
599         
600         // timer_cursor
601         timer_cursor = obj = fl_add_timer(FL_HIDDEN_TIMER,
602                                           0, 0, 0, 0, "Timer");
603         fl_set_object_callback(obj, C_BufferView_CursorToggleCB, 0);
604         obj->u_vdata = this;
605 }
606
607
608 // Callback for scrollbar up button
609 void BufferView::UpCB(FL_OBJECT * ob, long)
610 {
611         BufferView * view = static_cast<BufferView*>(ob->u_vdata);
612         
613         if (view->buffer_ == 0) return;
614
615         XEvent const * ev2;
616         static long time = 0;
617         ev2 = fl_last_event();
618         if (ev2->type == ButtonPress || ev2->type == ButtonRelease) 
619                 time = 0;
620         int button = fl_get_button_numb(ob);
621         switch (button) {
622         case 3:
623                 view->ScrollUpOnePage(time++); break;
624         case 2:
625                 view->ScrollDownOnePage(time++); break;
626         default:
627                 view->ScrollUp(time++); break;
628         }
629 }
630
631
632 static
633 void waitForX()
634 {
635 #if 0
636         static Window w = 0;
637         static Atom a = 0;
638         if (!a)
639                 a = XInternAtom(fl_display, "WAIT_FOR_X", False);
640         if (w == 0) {
641                 int mask;
642                 XSetWindowAttributes attr;
643                 mask = CWOverrideRedirect;
644                 attr.override_redirect = 1;
645                 w = XCreateWindow(fl_display, fl_root,
646                                   0, 0, 1, 1, 0, CopyFromParent,
647                                   InputOnly, CopyFromParent, mask, &attr);
648                 XSelectInput(fl_display, w, PropertyChangeMask);
649                 XMapWindow(fl_display, w);
650         }
651         static XEvent ev;
652         XChangeProperty(fl_display, w, a, a, 8,
653                         PropModeAppend, reinterpret_cast<unsigned char*>(""), 0);
654         XWindowEvent(fl_display, w, PropertyChangeMask, &ev);
655 #endif
656         XSync(fl_get_display(), 0);
657 }
658
659
660 // Callback for scrollbar slider
661 void BufferView::ScrollCB(FL_OBJECT * ob, long)
662 {
663         BufferView * view = static_cast<BufferView*>(ob->u_vdata);
664         extern bool cursor_follows_scrollbar;
665         
666         if (view->buffer_ == 0) return;
667
668         view->current_scrollbar_value = long(fl_get_slider_value(ob));
669         if (view->current_scrollbar_value < 0)
670                 view->current_scrollbar_value = 0;
671    
672         if (!view->screen)
673                 return;
674
675         view->screen->Draw(view->current_scrollbar_value);
676
677         if (cursor_follows_scrollbar) {
678 #ifdef MOVE_TEXT
679                 LyXText * vbt = view->text;
680 #else
681                 LyXText * vbt = view->buffer_->text;
682 #endif
683                 int height = vbt->DefaultHeight();
684                 
685                 if (vbt->cursor.y < view->screen->first + height) {
686                         vbt->SetCursorFromCoordinates(0,
687                                                       view->screen->first +
688                                                       height);
689                 }
690                 else if (vbt->cursor.y >
691                          view->screen->first + view->work_area->h - height) {
692                         vbt->SetCursorFromCoordinates(0,
693                                                       view->screen->first +
694                                                       view->work_area->h  -
695                                                       height);
696                 }
697         }
698         waitForX();
699 }
700
701
702 // Callback for scrollbar down button
703 void BufferView::DownCB(FL_OBJECT * ob, long)
704 {
705         BufferView * view = static_cast<BufferView*>(ob->u_vdata);
706
707         if (view->buffer_ == 0) return;
708         
709         XEvent const * ev2;
710         static long time = 0;
711         ev2 = fl_last_event();
712         if (ev2->type == ButtonPress || ev2->type == ButtonRelease) 
713                 time = 0;
714         int button = fl_get_button_numb(ob);
715         switch (button) {
716         case 2:
717                 view->ScrollUpOnePage(time++); break;
718         case 3:
719                 view->ScrollDownOnePage(time++); break;
720         default:
721                 view->ScrollDown(time++); break;
722         }
723 }
724
725
726 int BufferView::ScrollUp(long time)
727 {
728         if (buffer_ == 0) return 0;
729         if (!screen)
730                 return 0;
731    
732         double value= fl_get_slider_value(scrollbar);
733    
734         if (value == 0)
735                 return 0;
736
737 #ifdef MOVE_TEXT
738         float add_value =  (text->DefaultHeight()
739                             + float(time) * float(time) * 0.125);
740    
741         if (add_value > work_area->h)
742                 add_value = float(work_area->h -
743                                   text->DefaultHeight());
744 #else
745         float add_value =  (buffer_->text->DefaultHeight()
746                             + float(time) * float(time) * 0.125);
747    
748         if (add_value > work_area->h)
749                 add_value = float(work_area->h -
750                                   buffer_->text->DefaultHeight());
751 #endif
752    
753         value -= add_value;
754
755         if (value < 0)
756                 value = 0;
757    
758         fl_set_slider_value(scrollbar, value);
759    
760         ScrollCB(scrollbar, 0); 
761         return 0;
762 }
763
764
765 int BufferView::ScrollDown(long time)
766 {
767         if (buffer_ == 0) return 0;
768         if (!screen)
769                 return 0;
770    
771         double value= fl_get_slider_value(scrollbar);
772         double min, max;
773         fl_get_slider_bounds(scrollbar, &min, &max);
774
775         if (value == max)
776                 return 0;
777
778 #ifdef MOVE_TEXT
779         float add_value =  (text->DefaultHeight()
780                             + float(time) * float(time) * 0.125);
781    
782         if (add_value > work_area->h)
783                 add_value = float(work_area->h -
784                                   text->DefaultHeight());
785 #else
786         float add_value =  (buffer_->text->DefaultHeight()
787                             + float(time) * float(time) * 0.125);
788    
789         if (add_value > work_area->h)
790                 add_value = float(work_area->h -
791                                   buffer_->text->DefaultHeight());
792 #endif
793    
794         value += add_value;
795    
796         if (value > max)
797                 value = max;
798    
799         fl_set_slider_value(scrollbar, value);
800    
801         ScrollCB(scrollbar, 0); 
802         return 0;
803 }
804
805
806 void BufferView::ScrollUpOnePage(long /*time*/)
807 {
808         if (buffer_ == 0) return;
809         if (!screen)
810                 return;
811    
812         long y = screen->first;
813
814         if (!y) return;
815 #ifdef MOVE_TEXT
816         Row * row = text->GetRowNearY(y);
817 #else
818         Row * row = buffer_->text->GetRowNearY(y);
819 #endif
820         y = y - work_area->h + row->height;
821         
822         fl_set_slider_value(scrollbar, y);
823    
824         ScrollCB(scrollbar, 0); 
825 }
826
827
828 void BufferView::ScrollDownOnePage(long /*time*/)
829 {
830         if (buffer_ == 0) return;
831         if (!screen)
832                 return;
833    
834         double min, max;
835         fl_get_slider_bounds(scrollbar, &min, &max);
836         long y = screen->first;
837
838 #ifdef MOVE_TEXT
839         if (y > text->height - work_area->h)
840                 return;
841    
842         y += work_area->h;
843         text->GetRowNearY(y);
844 #else
845         if (y > buffer_->text->height - work_area->h)
846                 return;
847    
848         y += work_area->h;
849         buffer_->text->GetRowNearY(y);
850 #endif
851         fl_set_slider_value(scrollbar, y);
852    
853         ScrollCB(scrollbar, 0); 
854 }
855
856
857 int BufferView::work_area_handler(FL_OBJECT * ob, int event,
858                                   FL_Coord, FL_Coord ,
859                                   int /*key*/, void *xev)
860 {
861         static int x_old = -1;
862         static int y_old = -1;
863         static long scrollbar_value_old = -1;
864         
865         XEvent * ev = static_cast<XEvent*>(xev);
866         BufferView * view = static_cast<BufferView*>(ob->u_vdata);
867
868         // If we don't have a view yet; return
869         if (!view || quitting) return 0;
870
871         switch (event){   
872         case FL_DRAW:
873                 view->workAreaExpose(); 
874                 break;
875         case FL_PUSH:
876                 view->WorkAreaButtonPress(ob, 0, 0, 0, ev, 0);
877                 break; 
878         case FL_RELEASE:
879                 view->WorkAreaButtonRelease(ob, 0, 0, 0, ev, 0);
880                 break;
881         case FL_MOUSE:
882                 if (ev->xmotion.x != x_old || 
883                     ev->xmotion.y != y_old ||
884                     view->current_scrollbar_value != scrollbar_value_old) {
885                         x_old = ev->xmotion.x;
886                         y_old = ev->xmotion.y;
887                         scrollbar_value_old = view->current_scrollbar_value;
888                         view->WorkAreaMotionNotify(ob, 0, 0, 0, ev, 0);
889                 }
890                 break;
891         // Done by the raw callback:
892         //  case FL_KEYBOARD: WorkAreaKeyPress(ob, 0, 0, 0, ev, 0); break;
893         case FL_FOCUS:
894                 if (!view->owner_->getMiniBuffer()->shows_no_match)
895                         view->owner_->getMiniBuffer()->Init();
896                 view->owner_->getMiniBuffer()->shows_no_match = false;
897                 view->work_area_focus = true;
898                 fl_set_timer(view->timer_cursor, 0.4);
899                 break;
900         case FL_UNFOCUS:
901                 view->owner_->getMiniBuffer()->ExecCommand();
902                 view->work_area_focus = false;
903                 break;
904         case FL_ENTER:
905                 SetXtermCursor(view->owner_->getForm()->window);
906                 // reset the timer
907                 view->lyx_focus = true;
908                 fl_set_timer(view->timer_cursor, 0.4);
909                 break;
910         case FL_LEAVE: 
911                 if (!input_prohibited)
912                         XUndefineCursor(fl_display,
913                                         view->owner_->getForm()->window);
914                 view->lyx_focus = false; // This is not an absolute truth
915                 // but if it is not true, it will be changed within a blink
916                 // of an eye. ... Not good enough... use regulare timeperiod
917                 //fl_set_timer(view->timer_cursor, 0.01); // 0.1 sec blink
918                 fl_set_timer(view->timer_cursor, 0.4); // 0.4 sec blink
919                 break;
920         case FL_DBLCLICK: 
921                 // select a word 
922                 if (view->buffer_ && !view->buffer_->the_locking_inset) {
923                         if (view->screen && ev->xbutton.button == 1) {
924                                 view->screen->HideCursor();
925                                 view->screen->ToggleSelection();
926 #ifdef MOVE_TEXT
927                                 view->text->SelectWord();
928 #else
929                                 view->buffer_->text->SelectWord();
930 #endif
931                                 view->screen->ToggleSelection(false);
932                                 /* This will fit the cursor on the screen
933                                  * if necessary */
934 #ifdef MOVE_TEXT
935                                 view->update(0);
936 #else
937                                 view->buffer_->update(0);
938 #endif
939                         }
940                 }
941                 break;
942         case FL_TRPLCLICK:
943                 // select a line
944                 if (view->buffer_ && view->screen && ev->xbutton.button == 1) {
945                         view->screen->HideCursor(); 
946                         view->screen->ToggleSelection();
947 #ifdef MOVE_TEXT
948                         view->text->CursorHome();
949                         view->text->sel_cursor = view->text->cursor;
950                         view->text->CursorEnd();
951                         view->text->SetSelection();
952 #else
953                         view->buffer_->text->CursorHome();
954                         view->buffer_->text->sel_cursor = 
955                                 view->buffer_->text->cursor;
956                         view->buffer_->text->CursorEnd();
957                         view->buffer_->text->SetSelection();
958 #endif
959                         view->screen->ToggleSelection(false); 
960                         /* This will fit the cursor on the screen
961                          * if necessary */
962 #ifdef MOVE_TEXT
963                         view->update(0);
964 #else
965                         view->buffer_->update(0);
966 #endif
967                 }
968                 break;
969         case FL_OTHER:
970                 view->WorkAreaSelectionNotify(ob,
971                                               view->owner_->getForm()->window,
972                                               0, 0, ev, 0); 
973                 break;
974         }
975         return 1;
976 }
977
978 int BufferView::WorkAreaMotionNotify(FL_OBJECT *ob, Window,
979                                      int /*w*/, int /*h*/,
980                                      XEvent *ev, void */*d*/)
981 {
982
983         if (buffer_ == 0) return 0;
984         if (!screen) return 0;
985
986         // Check for inset locking
987         if (buffer_->the_locking_inset) {
988 #ifdef MOVE_TEXT
989                 LyXCursor cursor = text->cursor;
990 #else
991                 LyXCursor cursor = buffer_->text->cursor;
992 #endif
993                 buffer_->the_locking_inset->
994                         InsetMotionNotify(ev->xbutton.x - ob->x - cursor.x,
995                                           ev->xbutton.y - ob->y -
996                                           (cursor.y),
997                                           ev->xbutton.state);
998                 return 0;
999         }
1000    
1001         // Only use motion with button 1
1002         if (!ev->xmotion.state & Button1MotionMask)
1003                 return 0; 
1004    
1005         /* The selection possible is needed, that only motion events are 
1006          * used, where the bottom press event was on the drawing area too */
1007         if (selection_possible) {
1008                 screen->HideCursor();
1009
1010 #ifdef MOVE_TEXT
1011                 text->SetCursorFromCoordinates(ev->xbutton.x - ob->x,
1012                                                ev->xbutton.y - ob->y +
1013                                                screen->first);
1014       
1015                 if (!text->selection)
1016                         update(-3); // Maybe an empty line was deleted
1017       
1018                 text->SetSelection();
1019 #else
1020                 buffer_->text->
1021                         SetCursorFromCoordinates(ev->xbutton.x - ob->x,
1022                                                  ev->xbutton.y - ob->y +
1023                                                  screen->first);
1024       
1025                 if (!buffer_->text->selection)
1026                     buffer_->update(-3); // Maybe an empty line was deleted
1027       
1028                 buffer_->text->SetSelection();
1029 #endif
1030                 screen->ToggleToggle();
1031                 if (screen->FitCursor())
1032                         updateScrollbar(); 
1033                 screen->ShowCursor();
1034         }
1035         return 0;
1036 }
1037
1038
1039 extern int bibitemMaxWidth(LyXFont const &);
1040
1041 #ifdef MOVE_TEXT
1042 // Single-click on work area
1043 int BufferView::WorkAreaButtonPress(FL_OBJECT *ob, Window,
1044                         int /*w*/, int /*h*/, XEvent *ev, void */*d*/)
1045 {
1046         last_click_x = -1;
1047         last_click_y = -1;
1048
1049         if (buffer_ == 0) return 0;
1050         if (!screen) return 0;
1051
1052         int const x = ev->xbutton.x - ob->x;
1053         int const y = ev->xbutton.y - ob->y;
1054         // If we hit an inset, we have the inset coordinates in these
1055         // and inset_hit points to the inset.  If we do not hit an
1056         // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1057         int inset_x = x;
1058         int inset_y = y;
1059         Inset * inset_hit = checkInsetHit(inset_x, inset_y);
1060
1061         // ok ok, this is a hack.
1062         int button = ev->xbutton.button;
1063         if (button == 4 || button == 5) goto wheel;
1064
1065         {
1066                 
1067         if (buffer_->the_locking_inset) {
1068                 // We are in inset locking mode
1069                 
1070                 /* Check whether the inset was hit. If not reset mode,
1071                    otherwise give the event to the inset */
1072                 if (inset_hit != 0) {
1073                         buffer_->the_locking_inset->
1074                                 InsetButtonPress(inset_x, inset_y, button);
1075                         return 0;
1076                 } else {
1077                         UnlockInset(buffer_->the_locking_inset);
1078                 }
1079         }
1080         
1081         selection_possible = true;
1082         screen->HideCursor();
1083         
1084         // Right button mouse click on a table
1085         if (button == 3 &&
1086             (text->cursor.par->table ||
1087              text->MouseHitInTable(x, y + screen->first))) {
1088                 // Set the cursor to the press-position
1089                 text->SetCursorFromCoordinates(x, y + screen->first);
1090                 bool doit = true;
1091                 
1092                 // Only show the table popup if the hit is in the table, too
1093                 if (!text->HitInTable(text->cursor.row, x))
1094                         doit = false;
1095                 
1096                 // Hit above or below the table?
1097                 if (doit) {
1098                         if (!text->selection) {
1099                                 screen->ToggleSelection();
1100                                 text->ClearSelection();
1101                                 text->FullRebreak();
1102                                 screen->Update();
1103                                 updateScrollbar();
1104                         }
1105                         // Popup table popup when on a table.
1106                         // This is obviously temporary, since we should be
1107                         // able to 
1108                         // popup various context-sensitive-menus with the
1109                         // the right mouse. So this should be done more
1110                         // general in the future. Matthias.
1111                         selection_possible = false;
1112                         owner_->getLyXFunc()->Dispatch(LFUN_LAYOUT_TABLE,
1113                                                        "true");
1114                         return 0;
1115                 }
1116         }
1117         
1118         int screen_first = screen->first;
1119         
1120         // Middle button press pastes if we have a selection
1121         bool paste_internally = false;
1122         if (button == 2  // && !buffer_->the_locking_inset
1123             && text->selection) {
1124                 owner_->getLyXFunc()->Dispatch(LFUN_COPY);
1125                 paste_internally = true;
1126         }
1127         
1128         // Clear the selection
1129         screen->ToggleSelection();
1130         text->ClearSelection();
1131         text->FullRebreak();
1132         screen->Update();
1133         updateScrollbar();
1134                 
1135         // Single left click in math inset?
1136         if (inset_hit != 0 && inset_hit->Editable() == 2) {
1137                 // Highly editable inset, like math
1138                 selection_possible = false;
1139                 owner_->updateLayoutChoice();
1140                 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
1141                 inset_hit->Edit(inset_x, inset_y);
1142                 return 0;
1143         } 
1144
1145         // Right click on a footnote flag opens float menu
1146         if (button == 3) { 
1147                 selection_possible = false;
1148                 return 0;
1149         }
1150         
1151         text->SetCursorFromCoordinates(x, y + screen_first);
1152         text->FinishUndo();
1153         text->sel_cursor = text->cursor;
1154         text->cursor.x_fix = text->cursor.x;
1155         
1156         owner_->updateLayoutChoice();
1157         if (screen->FitCursor()){
1158                 updateScrollbar();
1159                 selection_possible = false;
1160         }
1161
1162         // Insert primary selection with middle mouse
1163         // if there is a local selection in the current buffer, insert this
1164         if (button == 2) { //  && !buffer_->the_locking_inset){
1165                 if (paste_internally)
1166                         owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
1167                 else
1168                         owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
1169                                                        "paragraph");
1170                 selection_possible = false;
1171                 return 0;
1172         }
1173         }
1174         goto out;
1175  wheel: {
1176         // I am not quite sure if this is the correct place to put this,
1177         // but it will not cause any harm.
1178         // Patch from Mark Huang (markman@mit.edu) to make LyX recognise
1179         // button 4 and 5. This enables LyX use use the scrollwhell on
1180         // certain mice for something useful. (Lgb)
1181         // Added wheel acceleration detection code. (Rvdk)
1182         static Time lastTime = 0;
1183         int diff = ev->xbutton.time - lastTime;
1184         int scroll = int(1.0 + (4.0/(abs(diff)+1.0))*200.0);
1185         switch (button) {
1186         case 4:
1187                 ScrollUp(scroll);
1188                 break;
1189         case 5:
1190                 ScrollDown(scroll);
1191                 break;
1192         }
1193         lastTime = ev->xbutton.time;
1194         return 0;
1195         }
1196  out:
1197         last_click_x = x;
1198         last_click_y = y;
1199         
1200         return 0;
1201 }
1202 #else
1203 // Single-click on work area
1204 int BufferView::WorkAreaButtonPress(FL_OBJECT *ob, Window,
1205                         int /*w*/, int /*h*/, XEvent *ev, void */*d*/)
1206 {
1207         last_click_x = -1;
1208         last_click_y = -1;
1209
1210         if (buffer_ == 0) return 0;
1211         if (!screen) return 0;
1212
1213         int const x = ev->xbutton.x - ob->x;
1214         int const y = ev->xbutton.y - ob->y;
1215         // If we hit an inset, we have the inset coordinates in these
1216         // and inset_hit points to the inset.  If we do not hit an
1217         // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1218         int inset_x = x;
1219         int inset_y = y;
1220         Inset * inset_hit = checkInsetHit(inset_x, inset_y);
1221
1222         // ok ok, this is a hack.
1223         int button = ev->xbutton.button;
1224         if (button == 4 || button == 5) goto wheel;
1225
1226         {
1227                 
1228         if (buffer_->the_locking_inset) {
1229                 // We are in inset locking mode
1230                 
1231                 /* Check whether the inset was hit. If not reset mode,
1232                    otherwise give the event to the inset */
1233                 if (inset_hit != 0) {
1234                         buffer_->the_locking_inset->
1235                                 InsetButtonPress(inset_x, inset_y, button);
1236                         return 0;
1237                 } else {
1238                         UnlockInset(buffer_->the_locking_inset);
1239                 }
1240         }
1241         
1242         selection_possible = true;
1243         screen->HideCursor();
1244         
1245         // Right button mouse click on a table
1246         if (button == 3 &&
1247             (buffer_->text->cursor.par->table ||
1248              buffer_->text->MouseHitInTable(x, y+screen->first))) {
1249                 // Set the cursor to the press-position
1250                 buffer_->text->SetCursorFromCoordinates(x, y + screen->first);
1251                 bool doit = true;
1252                 
1253                 // Only show the table popup if the hit is in the table, too
1254                 if (!buffer_->text->HitInTable(buffer_->text->cursor.row, x))
1255                         doit = false;
1256                 
1257                 // Hit above or below the table?
1258                 if (doit) {
1259                         if (!buffer_->text->selection) {
1260                                 screen->ToggleSelection();
1261                                 buffer_->text->ClearSelection();
1262                                 buffer_->text->FullRebreak();
1263                                 screen->Update();
1264                                 updateScrollbar();
1265                         }
1266                         // Popup table popup when on a table.
1267                         // This is obviously temporary, since we should be
1268                         // able to 
1269                         // popup various context-sensitive-menus with the
1270                         // the right mouse. So this should be done more
1271                         // general in the future. Matthias.
1272                         selection_possible = false;
1273                         owner_->getLyXFunc()->Dispatch(LFUN_LAYOUT_TABLE,
1274                                                        "true");
1275                         return 0;
1276                 }
1277         }
1278         
1279         int screen_first = screen->first;
1280         
1281         // Middle button press pastes if we have a selection
1282         bool paste_internally = false;
1283         if (button == 2  // && !buffer_->the_locking_inset
1284             && buffer_->text->selection) {
1285                 owner_->getLyXFunc()->Dispatch(LFUN_COPY);
1286                 paste_internally = true;
1287         }
1288         
1289         // Clear the selection
1290         screen->ToggleSelection();
1291         buffer_->text->ClearSelection();
1292         buffer_->text->FullRebreak();
1293         screen->Update();
1294         updateScrollbar();
1295                 
1296         // Single left click in math inset?
1297         if (inset_hit != 0 && inset_hit->Editable() == 2) {
1298                 // Highly editable inset, like math
1299                 selection_possible = false;
1300                 owner_->updateLayoutChoice();
1301                 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
1302                 inset_hit->Edit(inset_x, inset_y);
1303                 return 0;
1304         } 
1305
1306         // Right click on a footnote flag opens float menu
1307         if (button == 3) { 
1308                 selection_possible = false;
1309                 return 0;
1310         }
1311         
1312         buffer_->text->SetCursorFromCoordinates(x, y + screen_first);
1313         buffer_->text->FinishUndo();
1314         buffer_->text->sel_cursor = buffer_->text->cursor;
1315         buffer_->text->cursor.x_fix = buffer_->text->cursor.x;
1316         
1317         owner_->updateLayoutChoice();
1318         if (screen->FitCursor()){
1319                 updateScrollbar();
1320                 selection_possible = false;
1321         }
1322
1323         // Insert primary selection with middle mouse
1324         // if there is a local selection in the current buffer, insert this
1325         if (button == 2) { //  && !buffer_->the_locking_inset){
1326                 if (paste_internally)
1327                         owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
1328                 else
1329                         owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
1330                                                        "paragraph");
1331                 selection_possible = false;
1332                 return 0;
1333         }
1334         }
1335         goto out;
1336  wheel: {
1337         // I am not quite sure if this is the correct place to put this,
1338         // but it will not cause any harm.
1339         // Patch from Mark Huang (markman@mit.edu) to make LyX recognise
1340         // button 4 and 5. This enables LyX use use the scrollwhell on
1341         // certain mice for something useful. (Lgb)
1342         // Added wheel acceleration detection code. (Rvdk)
1343         static Time lastTime = 0;
1344         int diff = ev->xbutton.time - lastTime;
1345         int scroll = int(1.0 + (4.0/(abs(diff)+1.0))*200.0);
1346         switch (button) {
1347         case 4:
1348                 ScrollUp(scroll);
1349                 break;
1350         case 5:
1351                 ScrollDown(scroll);
1352                 break;
1353         }
1354         lastTime = ev->xbutton.time;
1355         return 0;
1356         }
1357  out:
1358         last_click_x = x;
1359         last_click_y = y;
1360         
1361         return 0;
1362 }
1363 #endif
1364
1365 #ifdef MOVE_TEXT
1366 int BufferView::WorkAreaButtonRelease(FL_OBJECT * ob, Window ,
1367                           int /*w*/, int /*h*/, XEvent * ev, void * /*d*/)
1368 {
1369         if (buffer_ == 0 || screen == 0) return 0;
1370
1371         int const x = ev->xbutton.x - ob->x;
1372         int const y = ev->xbutton.y - ob->y;
1373
1374         // If we hit an inset, we have the inset coordinates in these
1375         // and inset_hit points to the inset.  If we do not hit an
1376         // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1377         int inset_x = x;
1378         int inset_y = y;
1379         Inset * inset_hit = checkInsetHit(inset_x, inset_y);
1380
1381         if (buffer_->the_locking_inset) {
1382                 // We are in inset locking mode.
1383
1384                 /* LyX does a kind of work-area grabbing for insets.
1385                    Only a ButtonPress Event outside the inset will 
1386                    force a InsetUnlock. */
1387                 buffer_->the_locking_inset->
1388                         InsetButtonRelease(inset_x, inset_y, 
1389                                            ev->xbutton.button);
1390                 return 0;
1391         }
1392   
1393         selection_possible = false;
1394         if (text->cursor.par->table) {
1395                 int cell = text->
1396                         NumberOfCell(text->cursor.par,
1397                                      text->cursor.pos);
1398                 if (text->cursor.par->table->IsContRow(cell) &&
1399                     text->cursor.par->table->
1400                     CellHasContRow(text->cursor.par->table->
1401                                    GetCellAbove(cell))<0) {
1402                         text->CursorUp();
1403                 }
1404         }
1405         
1406         if (ev->xbutton.button >= 2)
1407                 return 0;
1408
1409         // Make sure that the press was not far from the release
1410         if ((abs(last_click_x - x) >= 5) ||
1411             (abs(last_click_y - y) >= 5)) {
1412                 return 0;
1413         }
1414
1415         // Did we hit an editable inset?
1416         if (inset_hit != 0) {
1417                 // Inset like error, notes and figures
1418                 selection_possible = false;
1419 #ifdef WITH_WARNINGS
1420 #warning fix this proper in 0.13
1421 #endif
1422                 // Following a ref shouldn't issue
1423                 // a push on the undo-stack
1424                 // anylonger, now that we have
1425                 // keybindings for following
1426                 // references and returning from
1427                 // references.  IMHO though, it
1428                 // should be the inset's own business
1429                 // to push or not push on the undo
1430                 // stack. They don't *have* to
1431                 // alter the document...
1432                 // (Joacim)
1433                 // ...or maybe the SetCursorParUndo()
1434                 // below isn't necessary at all anylonger?
1435                 if (inset_hit->LyxCode() == Inset::REF_CODE) {
1436                         text->SetCursorParUndo();
1437                 }
1438
1439                 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
1440                 inset_hit->Edit(inset_x, inset_y);
1441                 return 0;
1442         }
1443
1444         // check whether we want to open a float
1445         if (text) {
1446                 bool hit = false;
1447                 char c = ' ';
1448                 if (text->cursor.pos <
1449                     text->cursor.par->Last()) {
1450                         c = text->cursor.par->
1451                                 GetChar(text->cursor.pos);
1452                 }
1453                 if (c == LyXParagraph::META_FOOTNOTE
1454                     || c == LyXParagraph::META_MARGIN
1455                     || c == LyXParagraph::META_FIG
1456                     || c == LyXParagraph::META_TAB
1457                     || c == LyXParagraph::META_WIDE_FIG
1458                     || c == LyXParagraph::META_WIDE_TAB
1459                     || c == LyXParagraph::META_ALGORITHM){
1460                         hit = true;
1461                 } else if (text->cursor.pos - 1 >= 0) {
1462                         c = text->cursor.par->
1463                                 GetChar(text->cursor.pos - 1);
1464                         if (c == LyXParagraph::META_FOOTNOTE
1465                             || c == LyXParagraph::META_MARGIN
1466                             || c == LyXParagraph::META_FIG
1467                             || c == LyXParagraph::META_TAB
1468                             || c == LyXParagraph::META_WIDE_FIG 
1469                             || c == LyXParagraph::META_WIDE_TAB
1470                             || c == LyXParagraph::META_ALGORITHM){
1471                                 // We are one step too far to the right
1472                                 text->CursorLeft();
1473                                 hit = true;
1474                         }
1475                 }
1476                 if (hit == true) {
1477                         ToggleFloat();
1478                         selection_possible = false;
1479                         return 0;
1480                 }
1481         }
1482
1483         // Do we want to close a float? (click on the float-label)
1484         if (text->cursor.row->par->footnoteflag == 
1485             LyXParagraph::OPEN_FOOTNOTE
1486             && text->cursor.pos == 0
1487             && text->cursor.row->previous &&
1488             text->cursor.row->previous->par->
1489             footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
1490                 LyXFont font (LyXFont::ALL_SANE);
1491                 font.setSize(LyXFont::SIZE_SMALL);
1492
1493                 int box_x = 20; // LYX_PAPER_MARGIN;
1494                 box_x += font.textWidth("Mwide-figM", 10);
1495
1496                 int screen_first = screen->first;
1497
1498                 if (x < box_x
1499                     && y + screen_first > text->cursor.y -
1500                     text->cursor.row->baseline
1501                     && y + screen_first < text->cursor.y -
1502                     text->cursor.row->baseline
1503                     + font.maxAscent()*1.2 + font.maxDescent()*1.2) {
1504                         ToggleFloat();
1505                         selection_possible = false;
1506                         return 0;
1507                 }
1508         }
1509
1510         // Maybe we want to edit a bibitem ale970302
1511         if (text->cursor.par->bibkey && x < 20 + 
1512             bibitemMaxWidth(textclasslist.TextClass(buffer_->
1513                                         params.textclass).defaultfont())) {
1514                 text->cursor.par->bibkey->Edit(0, 0);
1515         }
1516
1517         return 0;
1518 }
1519 #else
1520 int BufferView::WorkAreaButtonRelease(FL_OBJECT *ob, Window ,
1521                           int /*w*/, int /*h*/, XEvent *ev, void */*d*/)
1522 {
1523         if (buffer_ == 0 || screen == 0) return 0;
1524
1525         int const x = ev->xbutton.x - ob->x;
1526         int const y = ev->xbutton.y - ob->y;
1527
1528         // If we hit an inset, we have the inset coordinates in these
1529         // and inset_hit points to the inset.  If we do not hit an
1530         // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1531         int inset_x = x;
1532         int inset_y = y;
1533         Inset * inset_hit = checkInsetHit(inset_x, inset_y);
1534
1535         if (buffer_->the_locking_inset) {
1536                 // We are in inset locking mode.
1537
1538                 /* LyX does a kind of work-area grabbing for insets.
1539                    Only a ButtonPress Event outside the inset will 
1540                    force a InsetUnlock. */
1541                 buffer_->the_locking_inset->
1542                         InsetButtonRelease(inset_x, inset_y, 
1543                                            ev->xbutton.button);
1544                 return 0;
1545         }
1546   
1547         selection_possible = false;
1548         if (buffer_->text->cursor.par->table) {
1549                 int cell = buffer_->text->
1550                         NumberOfCell(buffer_->text->cursor.par,
1551                                      buffer_->text->cursor.pos);
1552                 if (buffer_->text->cursor.par->table->IsContRow(cell) &&
1553                     buffer_->text->cursor.par->table->
1554                     CellHasContRow(buffer_->text->cursor.par->table->
1555                                    GetCellAbove(cell))<0) {
1556                         buffer_->text->CursorUp();
1557                 }
1558         }
1559         
1560         if (ev->xbutton.button >= 2)
1561                 return 0;
1562
1563         // Make sure that the press was not far from the release
1564         if ((abs(last_click_x - x) >= 5) ||
1565             (abs(last_click_y - y) >= 5)) {
1566                 return 0;
1567         }
1568
1569         // Did we hit an editable inset?
1570         if (inset_hit != 0) {
1571                 // Inset like error, notes and figures
1572                 selection_possible = false;
1573 #ifdef WITH_WARNINGS
1574 #warning fix this proper in 0.13
1575 #endif
1576                 // Following a ref shouldn't issue
1577                 // a push on the undo-stack
1578                 // anylonger, now that we have
1579                 // keybindings for following
1580                 // references and returning from
1581                 // references.  IMHO though, it
1582                 // should be the inset's own business
1583                 // to push or not push on the undo
1584                 // stack. They don't *have* to
1585                 // alter the document...
1586                 // (Joacim)
1587                 // ...or maybe the SetCursorParUndo()
1588                 // below isn't necessary at all anylonger?
1589                 if (inset_hit->LyxCode() == Inset::REF_CODE) {
1590                         buffer_->text->SetCursorParUndo();
1591                 }
1592
1593                 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
1594                 inset_hit->Edit(inset_x, inset_y);
1595                 return 0;
1596         }
1597
1598         // check whether we want to open a float
1599         if (buffer_->text) {
1600                 bool hit = false;
1601                 char c = ' ';
1602                 if (buffer_->text->cursor.pos <
1603                     buffer_->text->cursor.par->Last()) {
1604                         c = buffer_->text->cursor.par->
1605                                 GetChar(buffer_->text->cursor.pos);
1606                 }
1607                 if (c == LyXParagraph::META_FOOTNOTE
1608                     || c == LyXParagraph::META_MARGIN
1609                     || c == LyXParagraph::META_FIG
1610                     || c == LyXParagraph::META_TAB
1611                     || c == LyXParagraph::META_WIDE_FIG
1612                     || c == LyXParagraph::META_WIDE_TAB
1613                     || c == LyXParagraph::META_ALGORITHM){
1614                         hit = true;
1615                 } else if (buffer_->text->cursor.pos - 1 >= 0) {
1616                         c = buffer_->text->cursor.par->
1617                                 GetChar(buffer_->text->cursor.pos - 1);
1618                         if (c == LyXParagraph::META_FOOTNOTE
1619                             || c == LyXParagraph::META_MARGIN
1620                             || c == LyXParagraph::META_FIG
1621                             || c == LyXParagraph::META_TAB
1622                             || c == LyXParagraph::META_WIDE_FIG 
1623                             || c == LyXParagraph::META_WIDE_TAB
1624                             || c == LyXParagraph::META_ALGORITHM){
1625                                 // We are one step too far to the right
1626                                 buffer_->text->CursorLeft();
1627                                 hit = true;
1628                         }
1629                 }
1630                 if (hit == true) {
1631                         ToggleFloat();
1632                         selection_possible = false;
1633                         return 0;
1634                 }
1635         }
1636
1637         // Do we want to close a float? (click on the float-label)
1638         if (buffer_->text->cursor.row->par->footnoteflag == 
1639             LyXParagraph::OPEN_FOOTNOTE
1640             && buffer_->text->cursor.pos == 0
1641             && buffer_->text->cursor.row->previous &&
1642             buffer_->text->cursor.row->previous->par->
1643             footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
1644                 LyXFont font (LyXFont::ALL_SANE);
1645                 font.setSize(LyXFont::SIZE_SMALL);
1646
1647                 int box_x = 20; // LYX_PAPER_MARGIN;
1648                 box_x += font.textWidth("Mwide-figM", 10);
1649
1650                 int screen_first = screen->first;
1651
1652                 if (x < box_x
1653                     && y + screen_first > buffer_->text->cursor.y -
1654                     buffer_->text->cursor.row->baseline
1655                     && y + screen_first < buffer_->text->cursor.y -
1656                     buffer_->text->cursor.row->baseline
1657                     + font.maxAscent()*1.2 + font.maxDescent()*1.2) {
1658                         ToggleFloat();
1659                         selection_possible = false;
1660                         return 0;
1661                 }
1662         }
1663
1664         // Maybe we want to edit a bibitem ale970302
1665         if (buffer_->text->cursor.par->bibkey && x < 20 + 
1666             bibitemMaxWidth(textclasslist.TextClass(buffer_->
1667                                         params.textclass).defaultfont())) {
1668                 buffer_->text->cursor.par->bibkey->Edit(0, 0);
1669         }
1670
1671         return 0;
1672 }
1673 #endif
1674
1675 /* 
1676  * Returns an inset if inset was hit. 0 otherwise.
1677  * If hit, the coordinates are changed relative to the inset. 
1678  * Otherwise coordinates are not changed, and false is returned.
1679  */
1680 #ifdef MOVE_TEXT
1681 Inset * BufferView::checkInsetHit(int & x, int & y)
1682 {
1683         if (!getScreen())
1684                 return 0;
1685   
1686         int y_tmp = y + getScreen()->first;
1687   
1688         LyXCursor cursor = text->cursor;
1689         if (cursor.pos < cursor.par->Last() 
1690             && cursor.par->GetChar(cursor.pos) == LyXParagraph::META_INSET
1691             && cursor.par->GetInset(cursor.pos)
1692             && cursor.par->GetInset(cursor.pos)->Editable()) {
1693
1694                 // Check whether the inset really was hit
1695                 Inset * tmpinset = cursor.par->GetInset(cursor.pos);
1696                 LyXFont font = text->GetFont(cursor.par, cursor.pos);
1697                 if (x > cursor.x
1698                     && x < cursor.x + tmpinset->Width(font) 
1699                     && y_tmp > cursor.y - tmpinset->Ascent(font)
1700                     && y_tmp < cursor.y + tmpinset->Descent(font)) {
1701                         x = x - cursor.x;
1702                         // The origin of an inset is on the baseline
1703                         y = y_tmp - (cursor.y); 
1704                         return tmpinset;
1705                 }
1706         } else if (cursor.pos - 1 >= 0 
1707                    && cursor.par->GetChar(cursor.pos - 1) == LyXParagraph::META_INSET
1708                    && cursor.par->GetInset(cursor.pos - 1)
1709                    && cursor.par->GetInset(cursor.pos - 1)->Editable()) {
1710                 text->CursorLeft();
1711                 Inset * result = checkInsetHit(x, y);
1712                 if (result == 0) {
1713                         text->CursorRight();
1714                         return 0;
1715                 } else {
1716                         return result;
1717                 }
1718         }
1719         return 0;
1720 }
1721 #else
1722 Inset * BufferView::checkInsetHit(int & x, int & y)
1723 {
1724         if (!getScreen())
1725                 return 0;
1726   
1727         int y_tmp = y + getScreen()->first;
1728   
1729         LyXCursor cursor = buffer_->text->cursor;
1730         if (cursor.pos < cursor.par->Last() 
1731             && cursor.par->GetChar(cursor.pos) == LyXParagraph::META_INSET
1732             && cursor.par->GetInset(cursor.pos)
1733             && cursor.par->GetInset(cursor.pos)->Editable()) {
1734
1735                 // Check whether the inset really was hit
1736                 Inset * tmpinset = cursor.par->GetInset(cursor.pos);
1737                 LyXFont font = buffer_->text->GetFont(cursor.par, cursor.pos);
1738                 if (x > cursor.x
1739                     && x < cursor.x + tmpinset->Width(font) 
1740                     && y_tmp > cursor.y - tmpinset->Ascent(font)
1741                     && y_tmp < cursor.y + tmpinset->Descent(font)) {
1742                         x = x - cursor.x;
1743                         // The origin of an inset is on the baseline
1744                         y = y_tmp - (cursor.y); 
1745                         return tmpinset;
1746                 }
1747         } else if (cursor.pos - 1 >= 0 
1748                    && cursor.par->GetChar(cursor.pos - 1) == LyXParagraph::META_INSET
1749                    && cursor.par->GetInset(cursor.pos - 1)
1750                    && cursor.par->GetInset(cursor.pos - 1)->Editable()) {
1751                 buffer_->text->CursorLeft();
1752                 Inset * result = checkInsetHit(x, y);
1753                 if (result == 0) {
1754                         buffer_->text->CursorRight();
1755                         return 0;
1756                 } else {
1757                         return result;
1758                 }
1759         }
1760         return 0;
1761 }
1762 #endif
1763
1764 int BufferView::workAreaExpose()
1765 {
1766         if (!work_area || !work_area->form->visible) 
1767                 return 1;
1768
1769         // this is a hack to ensure that we only call this through
1770         // BufferView::redraw().
1771         if (!lgb_hack) {
1772                 redraw();
1773         }
1774         
1775         static int work_area_width = work_area->w;
1776         static int work_area_height = work_area->h;
1777
1778         bool widthChange = work_area->w != work_area_width;
1779         bool heightChange = work_area->h != work_area_height;
1780
1781         // update from work area
1782         work_area_width = work_area->w;
1783         work_area_height = work_area->h;
1784         if (buffer_ != 0) {
1785                 if (widthChange) {
1786                         // All buffers need a resize
1787                         bufferlist.resize();
1788                 } else if (heightChange) {
1789                         // Rebuild image of current screen
1790                         updateScreen();
1791                         // fitCursor() ensures we don't jump back
1792                         // to the start of the document on vertical
1793                         // resize
1794                         fitCursor();
1795
1796                         // The main window size has changed, repaint most stuff
1797                         redraw();
1798                         // ...including the minibuffer
1799                         owner_->getMiniBuffer()->Init();
1800
1801                 } else if (screen) screen->Redraw();
1802         } else {
1803                 // Grey box when we don't have a buffer
1804                 fl_winset(FL_ObjWin(work_area));
1805                 fl_rectangle(1, work_area->x, work_area->y,
1806                              work_area->w, work_area->h, FL_GRAY63);
1807         }
1808
1809         // always make sure that the scrollbar is sane.
1810         updateScrollbar();
1811         owner_->updateLayoutChoice();
1812         return 1;
1813 }
1814
1815
1816 // Callback for cursor timer
1817 void BufferView::CursorToggleCB(FL_OBJECT * ob, long)
1818 {
1819         BufferView *view = static_cast<BufferView*>(ob->u_vdata);
1820         
1821         /* quite a nice place for asyncron Inset updating, isn't it? */
1822         // actually no! This is run even if no buffer exist... so (Lgb)
1823         if (view && !view->buffer_) {
1824                 goto set_timer_and_return;
1825         }
1826
1827         // NOTE:
1828         // On my quest to solve the gs rendre hangups I am now
1829         // disabling the SIGHUP completely, and will do a wait
1830         // now and then instead. If the guess that xforms somehow
1831         // destroys something is true, this is likely (hopefully)
1832         // to solve the problem...at least I hope so. Lgb
1833
1834         // ...Ok this seems to work...at least it does not make things
1835         // worse so far. However I still see gs processes that hangs.
1836         // I would really like to know _why_ they are hanging. Anyway
1837         // the solution without the SIGCHLD handler seems to be easier
1838         // to debug.
1839
1840         // When attaching gdb to a a running gs that hangs it shows
1841         // that it is waiting for input(?) Is it possible for us to
1842         // provide that input somehow? Or figure what it is expecing
1843         // to read?
1844
1845         // One solution is to, after some time, look if there are some
1846         // old gs processes still running and if there are: kill them
1847         // and re render.
1848
1849         // Another solution is to provide the user an option to rerender
1850         // a picture. This would, for the picture in question, check if
1851         // there is a gs running for it, if so kill it, and start a new
1852         // rendering process.
1853
1854         // these comments posted to lyx@via
1855         {
1856         int status = 1;
1857         int pid = waitpid((pid_t)0, &status, WNOHANG);
1858         if (pid == -1) // error find out what is wrong
1859                 ; // ignore it for now.
1860         else if (pid > 0)
1861                 sigchldhandler(pid, &status);
1862         }
1863         if (InsetUpdateList) 
1864                 UpdateInsetUpdateList();
1865
1866         if (view && !view->screen){
1867                 goto set_timer_and_return;
1868         }
1869
1870         if (view->lyx_focus && view->work_area_focus) {
1871                 if (!view->buffer_->the_locking_inset){
1872                         view->screen->CursorToggle();
1873                 } else {
1874                         view->buffer_->the_locking_inset->
1875                                 ToggleInsetCursor();
1876                 }
1877                 goto set_timer_and_return;
1878         } else {
1879                 // Make sure that the cursor is visible.
1880                 if (!view->buffer_->the_locking_inset){
1881                         view->screen->ShowCursor();
1882                 } else {
1883                         if (!view->buffer_->the_locking_inset->isCursorVisible())
1884                                 view->buffer_->the_locking_inset->
1885                                         ToggleInsetCursor();
1886                 }
1887
1888                 // This is only run when work_area_focus or lyx_focus is false.
1889                 Window tmpwin;
1890                 int tmp;
1891                 XGetInputFocus(fl_display, &tmpwin, &tmp);
1892                 if (lyxerr.debugging()) {
1893                         lyxerr << "tmpwin: " << tmpwin
1894                                << "\nwindow: " << view->owner_->getForm()->window
1895                                << "\nwork_area_focus: " << view->work_area_focus
1896                                << "\nlyx_focus      : " << view->lyx_focus
1897                                << endl;
1898                 }
1899                 if (tmpwin != view->owner_->getForm()->window) {
1900                         view->lyx_focus = false;
1901                         goto skip_timer;
1902                 } else {
1903                         view->lyx_focus = true;
1904                         if (!view->work_area_focus)
1905                                 goto skip_timer;
1906                         else
1907                                 goto set_timer_and_return;
1908                 }
1909         }
1910
1911   set_timer_and_return:
1912         fl_set_timer(ob, 0.4);
1913   skip_timer:
1914         return;
1915 }
1916
1917
1918 int BufferView::WorkAreaSelectionNotify(FL_OBJECT *, Window win,
1919                             int /*w*/, int /*h*/, XEvent *event, void */*d*/)
1920 {
1921         if (buffer_ == 0) return 0;
1922         if (event->type != SelectionNotify)
1923                 return 0;
1924
1925         Atom tmpatom;
1926         unsigned long ul1;
1927         unsigned long ul2;
1928         unsigned char * uc = 0;
1929         int tmpint;
1930         screen->HideCursor();
1931         BeforeChange();
1932         if (event->xselection.type == XA_STRING
1933             && event->xselection.property) {
1934     
1935                 if (XGetWindowProperty(
1936                         fl_display            /* display */,
1937                         win /* w */,
1938                         event->xselection.property        /* property */,
1939                         0                /* long_offset */,
1940                         0                /* long_length */,
1941                         false                /* delete */,
1942                         XA_STRING                /* req_type */,
1943                         &tmpatom               /* actual_type_return */,
1944                         &tmpint                /* actual_format_return */,
1945                         &ul1      /* nitems_return */,
1946                         &ul2      /* bytes_after_return */,
1947                         &uc     /* prop_return */
1948                         ) != Success) {
1949                         return 0;
1950                 }
1951                 XFlush(fl_display);
1952
1953                 if (uc){
1954                         free(uc);
1955                         uc = 0;
1956                 }
1957
1958                 if (XGetWindowProperty(
1959                         fl_display           /* display */,
1960                         win              /* w */,
1961                         event->xselection.property           /* property */,
1962                         0                /* long_offset */,
1963                         ul2/4+1                /* long_length */,
1964                         True                /* delete */,
1965                         XA_STRING                /* req_type */,
1966                         &tmpatom               /* actual_type_return */,
1967                         &tmpint                /* actual_format_return */,
1968                         &ul1      /* nitems_return */,
1969                         &ul2      /* bytes_after_return */,
1970                         &uc     /* prop_return */
1971                         ) != Success) {
1972                         return 0;
1973                 }
1974                 XFlush(fl_display);
1975         
1976                 if (uc){
1977 #ifdef MOVE_TEXT
1978                         if (!ascii_type) {
1979                                 text->InsertStringA(reinterpret_cast<char*>(uc));
1980                         } else {
1981                                 text->InsertStringB(reinterpret_cast<char*>(uc));
1982                         }
1983 #else
1984                         if (!ascii_type) {
1985                                 buffer_->text->
1986                                         InsertStringA(reinterpret_cast<char*>(uc));
1987                         } else {
1988                                 buffer_->text->
1989                                         InsertStringB(reinterpret_cast<char*>(uc));
1990                         }
1991 #endif
1992                         free(uc);
1993                         uc = 0;
1994                 }
1995
1996 #ifdef MOVE_TEXT
1997                 update(1);
1998 #else
1999                 buffer_->update(1);
2000 #endif
2001         }
2002         return 0;
2003 }
2004
2005
2006 #ifdef MOVE_TEXT
2007 void BufferView::cursorPrevious()
2008 {
2009         if (!text->cursor.row->previous) return;
2010         
2011         long y = getScreen()->first;
2012         Row * cursorrow = text->cursor.row;
2013         text->SetCursorFromCoordinates(text->cursor.x_fix, y);
2014         text->FinishUndo();
2015         // this is to allow jumping over large insets
2016         if ((cursorrow == text->cursor.row))
2017                 text->CursorUp();
2018         
2019         if (text->cursor.row->height < work_area->h)
2020                 getScreen()->Draw(text->cursor.y
2021                                   - text->cursor.row->baseline
2022                                   + text->cursor.row->height
2023                                   - work_area->h +1 );
2024 }
2025
2026
2027 void BufferView::cursorNext()
2028 {
2029         if (!text->cursor.row->next) return;
2030         
2031         long y = getScreen()->first;
2032         text->GetRowNearY(y);
2033         Row * cursorrow = text->cursor.row;
2034         text->SetCursorFromCoordinates(text->cursor.x_fix, y + work_area->h);
2035         text->FinishUndo();
2036         /* this is to allow jumping over large insets */
2037         if ((cursorrow == text->cursor.row))
2038                 text->CursorDown();
2039         
2040         if (text->cursor.row->height < work_area->h)
2041                 getScreen()->Draw(text->cursor.y
2042                                   - text->cursor.row->baseline);
2043 }
2044 #else
2045 void BufferView::cursorPrevious()
2046 {
2047         if (!buffer()->text->cursor.row->previous) return;
2048         
2049         long y = getScreen()->first;
2050         Row * cursorrow = buffer()->text->cursor.row;
2051         buffer()->text->
2052           SetCursorFromCoordinates(buffer()->text->
2053                                    cursor.x_fix,
2054                                    y);
2055         buffer()->text->FinishUndo();
2056         // this is to allow jumping over large insets
2057         if ((cursorrow == buffer()->text->cursor.row))
2058           buffer()->text->CursorUp();
2059         
2060         if (buffer()->text->cursor.row->height < work_area->h)
2061           getScreen()->Draw(buffer()->text->cursor.y
2062                             - buffer()->text->cursor.row->baseline
2063                             + buffer()->text->cursor.row->height
2064                             - work_area->h +1 );
2065 }
2066
2067
2068 void BufferView::cursorNext()
2069 {
2070         if (!buffer()->text->cursor.row->next) return;
2071         
2072         long y = getScreen()->first;
2073         buffer()->text->GetRowNearY(y);
2074         Row * cursorrow = buffer()->text->cursor.row;
2075         buffer()->text->
2076                 SetCursorFromCoordinates(buffer()->text->
2077                                          cursor.x_fix, 
2078                                          y + work_area->h);
2079         buffer()->text->FinishUndo();
2080         /* this is to allow jumping over large insets */
2081         if ((cursorrow == buffer()->text->cursor.row))
2082           buffer()->text->CursorDown();
2083         
2084         if (buffer()->text->cursor.row->height < work_area->h)
2085           getScreen()->Draw(buffer()->text->cursor.y
2086                             - buffer()->text->cursor.row->baseline);
2087 }
2088 #endif
2089
2090 bool BufferView::available() const
2091 {
2092 #ifdef MOVE_TEXT
2093         if (buffer_ && text) return true;
2094 #else
2095         if (buffer_ && buffer_->text) return true;
2096 #endif
2097         return false;
2098 }
2099
2100
2101 void BufferView::savePosition()
2102 {
2103 #ifdef MOVE_TEXT
2104         backstack.push(buffer()->fileName(),
2105                         text->cursor.x,
2106                         text->cursor.y);
2107 #else
2108         backstack.push(buffer()->getFileName(),
2109                         buffer()->text->cursor.x,
2110                         buffer()->text->cursor.y);
2111 #endif
2112 }
2113
2114
2115 void BufferView::restorePosition()
2116 {
2117         if (backstack.empty()) return;
2118         
2119         int  x, y;
2120         string fname = backstack.pop(&x, &y);
2121         
2122         BeforeChange();
2123         Buffer * b = (bufferlist.exists(fname)) ? bufferlist.getBuffer(fname):
2124                 bufferlist.loadLyXFile(fname); // don't ask, just load it
2125         buffer(b);
2126 #ifdef MOVE_TEXT
2127         text->SetCursorFromCoordinates(x, y);
2128         update(0);
2129 #else
2130         buffer()->text->SetCursorFromCoordinates(x, y);
2131         buffer()->update(0);
2132 #endif
2133
2134
2135
2136 #ifdef MOVE_TEXT
2137 // candidate for move to BufferView
2138 void BufferView::update(signed char f)
2139 {
2140         owner()->updateLayoutChoice();
2141
2142         if (!text->selection && f > -3)
2143                 text->sel_cursor = text->cursor;
2144         
2145         FreeUpdateTimer();
2146         text->FullRebreak();
2147
2148         update();
2149
2150         if (f != 3 && f != -3) {
2151                 fitCursor();
2152                 updateScrollbar();
2153         }
2154
2155         if (f == 1 || f == -1) {
2156                 if (buffer()->isLyxClean()) {
2157                         buffer()->markDirty();
2158                         owner()->getMiniBuffer()->setTimer(4);
2159                 } else {
2160                         buffer()->markDirty();
2161                 }
2162         }
2163 }
2164 #endif