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