]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
fixes because of SUN CC warnings, bmtable now compiled with C compilator, countChar...
[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         text = 0;
69         screen = 0;
70         work_area = 0;
71         figinset_canvas = 0;
72         scrollbar = 0;
73         button_down = 0;
74         button_up = 0;
75         timer_cursor = 0;
76         current_scrollbar_value = 0;
77         create_view(xpos, ypos, width, height);
78         // Activate the timer for the cursor 
79         fl_set_timer(timer_cursor, 0.4);
80         fl_set_focus_object(owner_->getForm(), work_area);
81         work_area_focus = true;
82         lyx_focus = false;
83 }
84
85
86 BufferView::~BufferView()
87 {
88         delete text;
89 }
90
91
92 void BufferView::buffer(Buffer * b)
93 {
94         lyxerr[Debug::INFO] << "Setting buffer in BufferView" << endl;
95         if (buffer_) {
96                 buffer_->InsetSleep();
97                 buffer_->delUser(this);
98                 delete text;
99                 text = 0;
100         }
101
102         // Set current buffer
103         buffer_ = b;
104
105         if (bufferlist.getState() == BufferList::CLOSING) return;
106         
107         // Nuke old image
108         // screen is always deleted when the buffer is changed.
109         if (screen)
110                 delete screen;
111         screen = 0;
112
113         // If we are closing the buffer, use the first buffer as current
114         if (!buffer_) {
115                 buffer_ = bufferlist.first();
116         }
117
118         if (buffer_) {
119                 lyxerr[Debug::INFO] << "  Buffer addr: " << buffer_ << endl;
120                 buffer_->addUser(this);
121                 owner_->getMenus()->showMenus();
122                 // If we don't have a text object for this, we make one
123                 if (text == 0)
124                         resizeCurrentBuffer();
125                 else {
126                         updateScreen();
127                         updateScrollbar();
128                 }
129                 screen->first = screen->TopCursorVisible();
130                 redraw();
131                 updateAllVisibleBufferRelatedPopups();
132                 buffer_->InsetWakeup();
133         } else {
134                 lyxerr[Debug::INFO] << "  No Buffer!" << endl;
135                 owner_->getMenus()->hideMenus();
136                 updateScrollbar();
137                 fl_redraw_object(work_area);
138         }
139         // should update layoutchoice even if we don't have a buffer.
140         owner_->updateLayoutChoice();
141         owner_->getMiniBuffer()->Init();
142         owner_->updateWindowTitle();
143 }
144
145
146 void BufferView::updateScreen()
147 {
148         // Regenerate the screen.
149         if (screen)
150                 delete screen;
151         screen = new LyXScreen(FL_ObjWin(work_area),
152                                work_area->w,
153                                work_area->h,
154                                work_area->x,
155                                work_area->y,
156                                text);
157 }
158
159
160 void BufferView::resize()
161 {
162         // This will resize the buffer. (Asger)
163         if (buffer_)
164                 resizeCurrentBuffer();
165 }
166
167
168 static bool lgb_hack = false;
169
170 void BufferView::redraw()
171 {
172         lyxerr[Debug::INFO] << "BufferView::redraw()" << endl;
173         lgb_hack = true;
174         fl_redraw_object(work_area);
175         fl_redraw_object(scrollbar);
176         fl_redraw_object(button_down);
177         fl_redraw_object(button_up);
178         lgb_hack = false;
179 }
180
181
182 void BufferView::fitCursor()
183 {
184         if (screen) screen->FitCursor();
185 }
186
187
188 void BufferView::update()
189 {
190         if (screen) screen->Update();
191 }
192
193
194 void BufferView::updateScrollbar()
195 {
196         /* If the text is smaller than the working area, the scrollbar
197          * maximum must be the working area height. No scrolling will 
198          * be possible */
199
200         if (!buffer_) {
201                 fl_set_slider_value(scrollbar, 0);
202                 fl_set_slider_size(scrollbar, scrollbar->h);
203                 return;
204         }
205         
206         static long max2 = 0;
207         static long height2 = 0;
208
209         long cbth = 0;
210         long cbsf = 0;
211
212         if (text)
213                 cbth = text->height;
214         if (screen)
215                 cbsf = screen->first;
216
217         // check if anything has changed.
218         if (max2 == cbth &&
219             height2 == work_area->h &&
220             current_scrollbar_value == cbsf)
221                 return;       // no
222         
223         max2 = cbth;
224         height2 = work_area->h;
225         current_scrollbar_value = cbsf;
226
227         if (cbth <= height2) { // text is smaller than screen
228                 fl_set_slider_size(scrollbar, scrollbar->h);
229                 return;
230         }
231         
232         long maximum_height = work_area->h * 3 / 4 + cbth;
233         long value = cbsf;
234
235         // set the scrollbar
236         double hfloat = work_area->h;
237         double maxfloat = maximum_height;
238    
239         fl_set_slider_value(scrollbar, value);
240         fl_set_slider_bounds(scrollbar, 0,
241                              maximum_height - work_area->h);
242 #if FL_REVISION > 85
243         double lineh = text->DefaultHeight();
244         fl_set_slider_increment(scrollbar, work_area->h-lineh, lineh);
245 #endif
246         if (maxfloat > 0){
247                 if ((hfloat / maxfloat) * float(height2) < 3)
248                         fl_set_slider_size(scrollbar,
249                                            3 / float(height2));
250                 else
251                         fl_set_slider_size(scrollbar,
252                                            hfloat / maxfloat);
253         } else
254                 fl_set_slider_size(scrollbar, hfloat);
255         fl_set_slider_precision(scrollbar, 0);
256 }
257
258
259 void BufferView::redoCurrentBuffer()
260 {
261         lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
262         if (buffer_ && text) {
263                 resize();
264                 owner_->updateLayoutChoice();
265         }
266 }
267
268
269 int BufferView::resizeCurrentBuffer()
270 {
271         lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
272         
273         LyXParagraph * par = 0;
274         LyXParagraph * selstartpar = 0;
275         LyXParagraph * selendpar = 0;
276         int pos = 0;
277         int selstartpos = 0;
278         int selendpos = 0;
279         int selection = 0;
280         int mark_set = 0;
281
282         ProhibitInput();
283
284         owner_->getMiniBuffer()->Set(_("Formatting document..."));   
285
286         if (text) {
287                 par = text->cursor.par;
288                 pos = text->cursor.pos;
289                 selstartpar = text->sel_start_cursor.par;
290                 selstartpos = text->sel_start_cursor.pos;
291                 selendpar = text->sel_end_cursor.par;
292                 selendpos = text->sel_end_cursor.pos;
293                 selection = text->selection;
294                 mark_set = text->mark_set;
295                 delete text;
296         }
297         text = new LyXText(work_area->w, buffer_);
298
299         updateScreen();
300
301         if (par) {
302                 text->selection = true;
303                 /* at this point just to avoid the Delete-Empty-Paragraph
304                  * Mechanism when setting the cursor */
305                 text->mark_set = mark_set;
306                 if (selection) {
307                         text->SetCursor(selstartpar, selstartpos);
308                         text->sel_cursor = text->cursor;
309                         text->SetCursor(selendpar, selendpos);
310                         text->SetSelection();
311                         text->SetCursor(par, pos);
312                 } else {
313                         text->SetCursor(par, pos);
314                         text->sel_cursor = text->cursor;
315                         text->selection = false;
316                 }
317         }
318         screen->first = screen->TopCursorVisible(); /* this will scroll the
319                                                      * screen such that the
320                                                      * cursor becomes
321                                                      * visible */ 
322         updateScrollbar();
323         redraw();
324         owner_->getMiniBuffer()->Init();
325         AllowInput();
326
327         // Now if the title form still exist kill it
328         TimerCB(0, 0);
329
330         return 0;
331 }
332
333
334 void BufferView::gotoError()
335 {
336         if (!screen)
337                 return;
338    
339         screen->HideCursor();
340         BeforeChange();
341         update(-2);
342         LyXCursor tmp;
343
344         if (!text->GotoNextError()) {
345                 if (text->cursor.pos 
346                     || text->cursor.par != text->FirstParagraph()) {
347                         tmp = text->cursor;
348                         text->cursor.par = text->FirstParagraph();
349                         text->cursor.pos = 0;
350                         if (!text->GotoNextError()) {
351                                 text->cursor = tmp;
352                                 owner_->getMiniBuffer()
353                                         ->Set(_("No more errors"));
354                                 LyXBell();
355                         }
356                 } else {
357                         owner_->getMiniBuffer()->Set(_("No more errors"));
358                         LyXBell();
359                 }
360         }
361         update(0);
362         text->sel_cursor = text->cursor;
363 }
364
365
366 extern "C" {
367 // Just a bunch of C wrappers around static members of BufferView
368         void C_BufferView_UpCB(FL_OBJECT * ob, long buf)
369         {
370                 BufferView::UpCB(ob, buf);
371         }
372
373
374         void C_BufferView_DownCB(FL_OBJECT * ob, long buf)
375         {
376                 BufferView::DownCB(ob, buf);
377         }
378
379
380         void C_BufferView_ScrollCB(FL_OBJECT * ob, long buf)
381         {
382                 BufferView::ScrollCB(ob, buf);
383         }
384
385
386         void C_BufferView_CursorToggleCB(FL_OBJECT * ob, long buf)
387         {
388                 BufferView::CursorToggleCB(ob, buf);
389         }
390
391
392         int C_BufferView_work_area_handler(FL_OBJECT * ob, int event,
393                                            FL_Coord, FL_Coord, 
394                                            int key, void * xev)
395         {
396                 return BufferView::work_area_handler(ob, event,
397                                                      0, 0, key, xev);
398         }
399 }
400
401
402 void BufferView::create_view(int xpos, int ypos, int width, int height)
403 {
404         FL_OBJECT * obj;
405         int const bw = abs(fl_get_border_width());
406
407         // a hack for the figinsets (Matthias)
408         // This one first, then it will probably be invisible. (Lgb)
409         ::figinset_canvas = figinset_canvas = obj = 
410                   fl_add_canvas(FL_NORMAL_CANVAS,
411                                 xpos + 1,
412                                 ypos + 1, 1, 1, "");
413         fl_set_object_boxtype(obj, FL_NO_BOX);
414         fl_set_object_resize(obj, FL_RESIZE_ALL);
415         fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
416
417         // a box
418         obj = fl_add_box(FL_BORDER_BOX, xpos, ypos,
419                          width - 15,
420                          height, "");
421         fl_set_object_resize(obj, FL_RESIZE_ALL);
422         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
423
424         // the free object
425         work_area = obj = fl_add_free(FL_INPUT_FREE,
426                                       xpos + bw, ypos + bw,
427                                       width - 15 - 2 * bw /* scrollbarwidth */,
428                                       height - 2 * bw, "",
429                                       C_BufferView_work_area_handler);
430         obj->wantkey = FL_KEY_TAB;
431         obj->u_vdata = this; /* This is how we pass the BufferView
432                                 to the work_area_handler. */
433         fl_set_object_boxtype(obj, FL_DOWN_BOX);
434         fl_set_object_resize(obj, FL_RESIZE_ALL);
435         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
436
437         //
438         // THE SCROLLBAR
439         //
440
441         // up - scrollbar button
442 #if FL_REVISION > 85
443         fl_set_border_width(-1);
444 #else
445         fl_set_border_width(-2); // to get visible feedback
446 #endif
447         button_up = obj = fl_add_pixmapbutton(FL_TOUCH_BUTTON,
448                                               width - 15 + 4 * bw,
449                                               ypos,
450                                               15, 15, "");
451         fl_set_object_boxtype(obj, FL_UP_BOX);
452         fl_set_object_color(obj, FL_MCOL, FL_BLUE);
453         fl_set_object_resize(obj, FL_RESIZE_ALL);
454         fl_set_object_gravity(obj, NorthEastGravity, NorthEastGravity);
455         fl_set_object_callback(obj, C_BufferView_UpCB, 0);
456         obj->u_vdata = this;
457         fl_set_pixmapbutton_data(obj, const_cast<char**>(up_xpm));
458
459 #if FL_REVISION > 85
460         // Remove the blue feedback rectangle
461         fl_set_pixmapbutton_focus_outline(obj, 0);
462 #endif  
463
464         // the scrollbar slider
465         fl_set_border_width(-bw);
466         scrollbar = obj = fl_add_slider(FL_VERT_SLIDER,
467                                         width - 15 + 4 * bw,
468                                         ypos + 15,
469                                         15, height - 30, "");
470         fl_set_object_color(obj, FL_COL1, FL_MCOL);
471         fl_set_object_boxtype(obj, FL_UP_BOX);
472         fl_set_object_resize(obj, FL_RESIZE_ALL);
473         fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
474         fl_set_object_callback(obj, C_BufferView_ScrollCB, 0);
475         obj->u_vdata = this;
476         
477         // down - scrollbar button
478 #if FL_REVISION > 85
479         fl_set_border_width(-1);
480 #else
481         fl_set_border_width(-2); // to get visible feedback
482 #endif
483         button_down = obj = fl_add_pixmapbutton(FL_TOUCH_BUTTON,
484                                                 width - 15 + 4 * bw,
485                                                 ypos + height - 15,
486                                                 15, 15, "");
487         fl_set_object_boxtype(obj, FL_UP_BOX);
488         fl_set_object_color(obj, FL_MCOL, FL_BLUE);
489         fl_set_object_resize(obj, FL_RESIZE_ALL);
490         fl_set_object_gravity(obj, SouthEastGravity, SouthEastGravity);
491         fl_set_object_callback(obj, C_BufferView_DownCB, 0);
492         obj->u_vdata = this;
493         fl_set_pixmapbutton_data(obj, const_cast<char**>(down_xpm));
494         fl_set_border_width(-bw);
495
496 #if FL_REVISION > 85
497         // Remove the blue feedback rectangle
498         fl_set_pixmapbutton_focus_outline(obj, 0);
499 #endif  
500
501         //
502         // TIMERS
503         //
504         
505         // timer_cursor
506         timer_cursor = obj = fl_add_timer(FL_HIDDEN_TIMER,
507                                           0, 0, 0, 0, "Timer");
508         fl_set_object_callback(obj, C_BufferView_CursorToggleCB, 0);
509         obj->u_vdata = this;
510 }
511
512
513 // Callback for scrollbar up button
514 void BufferView::UpCB(FL_OBJECT * ob, long)
515 {
516         BufferView * view = static_cast<BufferView*>(ob->u_vdata);
517         
518         if (view->buffer_ == 0) return;
519
520         static long time = 0;
521         XEvent const * ev2 = fl_last_event();
522         if (ev2->type == ButtonPress || ev2->type == ButtonRelease) 
523                 time = 0;
524         int button = fl_get_button_numb(ob);
525         switch (button) {
526         case 3:
527                 view->ScrollUpOnePage(time++); break;
528         case 2:
529                 view->ScrollDownOnePage(time++); break;
530         default:
531                 view->ScrollUp(time++); break;
532         }
533 }
534
535
536 static
537 void waitForX()
538 {
539 #if 0
540         static Window w = 0;
541         static Atom a = 0;
542         if (!a)
543                 a = XInternAtom(fl_display, "WAIT_FOR_X", False);
544         if (w == 0) {
545                 int mask;
546                 XSetWindowAttributes attr;
547                 mask = CWOverrideRedirect;
548                 attr.override_redirect = 1;
549                 w = XCreateWindow(fl_display, fl_root,
550                                   0, 0, 1, 1, 0, CopyFromParent,
551                                   InputOnly, CopyFromParent, mask, &attr);
552                 XSelectInput(fl_display, w, PropertyChangeMask);
553                 XMapWindow(fl_display, w);
554         }
555         static XEvent ev;
556         XChangeProperty(fl_display, w, a, a, 8,
557                         PropModeAppend,
558                         reinterpret_cast<unsigned char*>(""), 0);
559         XWindowEvent(fl_display, w, PropertyChangeMask, &ev);
560 #endif
561         XSync(fl_get_display(), 0);
562 }
563
564
565 // Callback for scrollbar slider
566 void BufferView::ScrollCB(FL_OBJECT * ob, long)
567 {
568         BufferView * view = static_cast<BufferView*>(ob->u_vdata);
569         extern bool cursor_follows_scrollbar;
570         
571         if (view->buffer_ == 0) return;
572
573         view->current_scrollbar_value = long(fl_get_slider_value(ob));
574         if (view->current_scrollbar_value < 0)
575                 view->current_scrollbar_value = 0;
576    
577         if (!view->screen)
578                 return;
579
580         view->screen->Draw(view->current_scrollbar_value);
581
582         if (cursor_follows_scrollbar) {
583                 LyXText * vbt = view->text;
584                 int height = vbt->DefaultHeight();
585                 
586                 if (vbt->cursor.y < view->screen->first + height) {
587                         vbt->SetCursorFromCoordinates(0,
588                                                       view->screen->first +
589                                                       height);
590                 }
591                 else if (vbt->cursor.y >
592                          view->screen->first + view->work_area->h - height) {
593                         vbt->SetCursorFromCoordinates(0,
594                                                       view->screen->first +
595                                                       view->work_area->h  -
596                                                       height);
597                 }
598         }
599         waitForX();
600 }
601
602
603 // Callback for scrollbar down button
604 void BufferView::DownCB(FL_OBJECT * ob, long)
605 {
606         BufferView * view = static_cast<BufferView*>(ob->u_vdata);
607
608         if (view->buffer_ == 0) return;
609         
610         XEvent const * ev2;
611         static long time = 0;
612         ev2 = fl_last_event();
613         if (ev2->type == ButtonPress || ev2->type == ButtonRelease) 
614                 time = 0;
615         int button = fl_get_button_numb(ob);
616         switch (button) {
617         case 2:
618                 view->ScrollUpOnePage(time++); break;
619         case 3:
620                 view->ScrollDownOnePage(time++); break;
621         default:
622                 view->ScrollDown(time++); break;
623         }
624 }
625
626
627 int BufferView::ScrollUp(long time)
628 {
629         if (buffer_ == 0) return 0;
630         if (!screen) return 0;
631    
632         double value = fl_get_slider_value(scrollbar);
633    
634         if (value == 0) return 0;
635
636         float add_value =  (text->DefaultHeight()
637                             + float(time) * float(time) * 0.125);
638    
639         if (add_value > work_area->h)
640                 add_value = float(work_area->h -
641                                   text->DefaultHeight());
642    
643         value -= add_value;
644
645         if (value < 0)
646                 value = 0;
647    
648         fl_set_slider_value(scrollbar, value);
649    
650         ScrollCB(scrollbar, 0); 
651         return 0;
652 }
653
654
655 int BufferView::ScrollDown(long time)
656 {
657         if (buffer_ == 0) return 0;
658         if (!screen) return 0;
659    
660         double value= fl_get_slider_value(scrollbar);
661         double min, max;
662         fl_get_slider_bounds(scrollbar, &min, &max);
663
664         if (value == max) return 0;
665
666         float add_value =  (text->DefaultHeight()
667                             + float(time) * float(time) * 0.125);
668    
669         if (add_value > work_area->h)
670                 add_value = float(work_area->h -
671                                   text->DefaultHeight());
672    
673         value += add_value;
674    
675         if (value > max)
676                 value = max;
677    
678         fl_set_slider_value(scrollbar, value);
679    
680         ScrollCB(scrollbar, 0); 
681         return 0;
682 }
683
684
685 void BufferView::ScrollUpOnePage(long /*time*/)
686 {
687         if (buffer_ == 0) return;
688         if (!screen) return;
689    
690         long y = screen->first;
691
692         if (!y) return;
693
694         Row * row = text->GetRowNearY(y);
695
696         y = y - work_area->h + row->height;
697         
698         fl_set_slider_value(scrollbar, y);
699    
700         ScrollCB(scrollbar, 0); 
701 }
702
703
704 void BufferView::ScrollDownOnePage(long /*time*/)
705 {
706         if (buffer_ == 0) return;
707         if (!screen) return;
708    
709         double min, max;
710         fl_get_slider_bounds(scrollbar, &min, &max);
711         long y = screen->first;
712
713         if (y > text->height - work_area->h)
714                 return;
715    
716         y += work_area->h;
717         text->GetRowNearY(y);
718
719         fl_set_slider_value(scrollbar, y);
720    
721         ScrollCB(scrollbar, 0); 
722 }
723
724
725 int BufferView::work_area_handler(FL_OBJECT * ob, int event,
726                                   FL_Coord, FL_Coord ,
727                                   int /*key*/, void * xev)
728 {
729         static int x_old = -1;
730         static int y_old = -1;
731         static long scrollbar_value_old = -1;
732         
733         XEvent * ev = static_cast<XEvent*>(xev);
734         BufferView * view = static_cast<BufferView*>(ob->u_vdata);
735
736         // If we don't have a view yet; return
737         if (!view || quitting) return 0;
738
739         switch (event){   
740         case FL_DRAW:
741                 view->workAreaExpose(); 
742                 break;
743         case FL_PUSH:
744                 view->WorkAreaButtonPress(ob, 0, 0, 0, ev, 0);
745                 break; 
746         case FL_RELEASE:
747                 view->WorkAreaButtonRelease(ob, 0, 0, 0, ev, 0);
748                 break;
749         case FL_MOUSE:
750                 if (ev->xmotion.x != x_old || 
751                     ev->xmotion.y != y_old ||
752                     view->current_scrollbar_value != scrollbar_value_old) {
753                         x_old = ev->xmotion.x;
754                         y_old = ev->xmotion.y;
755                         scrollbar_value_old = view->current_scrollbar_value;
756                         view->WorkAreaMotionNotify(ob, 0, 0, 0, ev, 0);
757                 }
758                 break;
759                 // Done by the raw callback:
760                 //  case FL_KEYBOARD:
761                 //  WorkAreaKeyPress(ob, 0, 0, 0, ev, 0); break;
762         case FL_FOCUS:
763                 if (!view->owner_->getMiniBuffer()->shows_no_match)
764                         view->owner_->getMiniBuffer()->Init();
765                 view->owner_->getMiniBuffer()->shows_no_match = false;
766                 view->work_area_focus = true;
767                 fl_set_timer(view->timer_cursor, 0.4);
768                 break;
769         case FL_UNFOCUS:
770                 view->owner_->getMiniBuffer()->ExecCommand();
771                 view->work_area_focus = false;
772                 break;
773         case FL_ENTER:
774                 SetXtermCursor(view->owner_->getForm()->window);
775                 // reset the timer
776                 view->lyx_focus = true;
777                 fl_set_timer(view->timer_cursor, 0.4);
778                 break;
779         case FL_LEAVE: 
780                 if (!input_prohibited)
781                         XUndefineCursor(fl_display,
782                                         view->owner_->getForm()->window);
783                 view->lyx_focus = false; // This is not an absolute truth
784                 // but if it is not true, it will be changed within a blink
785                 // of an eye. ... Not good enough... use regulare timeperiod
786                 //fl_set_timer(view->timer_cursor, 0.01); // 0.1 sec blink
787                 fl_set_timer(view->timer_cursor, 0.4); // 0.4 sec blink
788                 break;
789         case FL_DBLCLICK: 
790                 // select a word 
791                 if (view->buffer_ && !view->buffer_->the_locking_inset) {
792                         if (view->screen && ev->xbutton.button == 1) {
793                                 view->screen->HideCursor();
794                                 view->screen->ToggleSelection();
795                                 view->text->SelectWord();
796                                 view->screen->ToggleSelection(false);
797                                 /* This will fit the cursor on the screen
798                                  * if necessary */
799                                 view->update(0);
800                         }
801                 }
802                 break;
803         case FL_TRPLCLICK:
804                 // select a line
805                 if (view->buffer_ && view->screen && ev->xbutton.button == 1) {
806                         view->screen->HideCursor(); 
807                         view->screen->ToggleSelection();
808                         view->text->CursorHome();
809                         view->text->sel_cursor = view->text->cursor;
810                         view->text->CursorEnd();
811                         view->text->SetSelection();
812                         view->screen->ToggleSelection(false); 
813                         /* This will fit the cursor on the screen
814                          * if necessary */
815                         view->update(0);
816                 }
817                 break;
818         case FL_OTHER:
819                 view->WorkAreaSelectionNotify(ob,
820                                               view->owner_->getForm()->window,
821                                               0, 0, ev, 0); 
822                 break;
823         }
824         return 1;
825 }
826
827 int BufferView::WorkAreaMotionNotify(FL_OBJECT * ob, Window,
828                                      int /*w*/, int /*h*/,
829                                      XEvent * ev, void * /*d*/)
830 {
831         if (buffer_ == 0) return 0;
832         if (!screen) return 0;
833
834         // Check for inset locking
835         if (buffer_->the_locking_inset) {
836                 LyXCursor cursor = text->cursor;
837                 buffer_->the_locking_inset->
838                         InsetMotionNotify(ev->xbutton.x - ob->x - cursor.x,
839                                           ev->xbutton.y - ob->y -
840                                           (cursor.y),
841                                           ev->xbutton.state);
842                 return 0;
843         }
844    
845         // Only use motion with button 1
846         if (!ev->xmotion.state & Button1MotionMask)
847                 return 0; 
848    
849         /* The selection possible is needed, that only motion events are 
850          * used, where the bottom press event was on the drawing area too */
851         if (selection_possible) {
852                 screen->HideCursor();
853
854                 text->SetCursorFromCoordinates(ev->xbutton.x - ob->x,
855                                                ev->xbutton.y - ob->y +
856                                                screen->first);
857       
858                 if (!text->selection)
859                         update(-3); // Maybe an empty line was deleted
860       
861                 text->SetSelection();
862                 screen->ToggleToggle();
863                 if (screen->FitCursor())
864                         updateScrollbar(); 
865                 screen->ShowCursor();
866         }
867         return 0;
868 }
869
870
871 extern int bibitemMaxWidth(LyXFont const &);
872
873 // Single-click on work area
874 int BufferView::WorkAreaButtonPress(FL_OBJECT * ob, Window,
875                                     int /*w*/, int /*h*/,
876                                     XEvent * ev, void */*d*/)
877 {
878         last_click_x = -1;
879         last_click_y = -1;
880
881         if (buffer_ == 0) return 0;
882         if (!screen) return 0;
883
884         int const x = ev->xbutton.x - ob->x;
885         int const y = ev->xbutton.y - ob->y;
886         // If we hit an inset, we have the inset coordinates in these
887         // and inset_hit points to the inset.  If we do not hit an
888         // inset, inset_hit is 0, and inset_x == x, inset_y == y.
889         int inset_x = x;
890         int inset_y = y;
891         Inset * inset_hit = checkInsetHit(inset_x, inset_y);
892
893         // ok ok, this is a hack.
894         int button = ev->xbutton.button;
895         if (button == 4 || button == 5) goto wheel;
896
897         {
898                 
899                 if (buffer_->the_locking_inset) {
900                         // We are in inset locking mode
901                 
902                         /* Check whether the inset was hit. If not reset mode,
903                            otherwise give the event to the inset */
904                         if (inset_hit != 0) {
905                                 buffer_->the_locking_inset->
906                                         InsetButtonPress(inset_x, inset_y,
907                                                          button);
908                                 return 0;
909                         } else {
910                                 UnlockInset(buffer_->the_locking_inset);
911                         }
912                 }
913         
914                 selection_possible = true;
915                 screen->HideCursor();
916         
917                 // Right button mouse click on a table
918                 if (button == 3 &&
919                     (text->cursor.par->table ||
920                      text->MouseHitInTable(x, y + screen->first))) {
921                         // Set the cursor to the press-position
922                         text->SetCursorFromCoordinates(x, y + screen->first);
923                         bool doit = true;
924                 
925                         // Only show the table popup if the hit is in
926                         // the table, too
927                         if (!text->HitInTable(text->cursor.row, x))
928                                 doit = false;
929                 
930                         // Hit above or below the table?
931                         if (doit) {
932                                 if (!text->selection) {
933                                         screen->ToggleSelection();
934                                         text->ClearSelection();
935                                         text->FullRebreak();
936                                         screen->Update();
937                                         updateScrollbar();
938                                 }
939                                 // Popup table popup when on a table.
940                                 // This is obviously temporary, since we
941                                 // should be able to popup various
942                                 // context-sensitive-menus with the
943                                 // the right mouse. So this should be done more
944                                 // general in the future. Matthias.
945                                 selection_possible = false;
946                                 owner_->getLyXFunc()
947                                         ->Dispatch(LFUN_LAYOUT_TABLE,
948                                                    "true");
949                                 return 0;
950                         }
951                 }
952         
953                 int screen_first = screen->first;
954         
955                 // Middle button press pastes if we have a selection
956                 bool paste_internally = false;
957                 if (button == 2  // && !buffer_->the_locking_inset
958                     && text->selection) {
959                         owner_->getLyXFunc()->Dispatch(LFUN_COPY);
960                         paste_internally = true;
961                 }
962         
963                 // Clear the selection
964                 screen->ToggleSelection();
965                 text->ClearSelection();
966                 text->FullRebreak();
967                 screen->Update();
968                 updateScrollbar();
969                 
970                 // Single left click in math inset?
971                 if (inset_hit != 0 && inset_hit->Editable() == 2) {
972                         // Highly editable inset, like math
973                         selection_possible = false;
974                         owner_->updateLayoutChoice();
975                         owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
976                         inset_hit->Edit(inset_x, inset_y);
977                         return 0;
978                 } 
979
980                 // Right click on a footnote flag opens float menu
981                 if (button == 3) { 
982                         selection_possible = false;
983                         return 0;
984                 }
985         
986                 text->SetCursorFromCoordinates(x, y + screen_first);
987                 text->FinishUndo();
988                 text->sel_cursor = text->cursor;
989                 text->cursor.x_fix = text->cursor.x;
990         
991                 owner_->updateLayoutChoice();
992                 if (screen->FitCursor()){
993                         updateScrollbar();
994                         selection_possible = false;
995                 }
996
997                 // Insert primary selection with middle mouse
998                 // if there is a local selection in the current buffer,
999                 // insert this
1000                 if (button == 2) { //  && !buffer_->the_locking_inset){
1001                         if (paste_internally)
1002                                 owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
1003                         else
1004                                 owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
1005                                                                "paragraph");
1006                         selection_possible = false;
1007                         return 0;
1008                 }
1009         }
1010         goto out;
1011   wheel:
1012         {
1013                 // I am not quite sure if this is the correct place to put
1014                 // this, but it will not cause any harm.
1015                 // Patch from Mark Huang (markman@mit.edu) to make LyX
1016                 // recognise button 4 and 5. This enables LyX use use
1017                 // the scrollwhell on certain mice for something useful. (Lgb)
1018                 // Added wheel acceleration detection code. (Rvdk)
1019                 static Time lastTime = 0;
1020                 int diff = ev->xbutton.time - lastTime;
1021                 int scroll = int(1.0 + (4.0 / (abs(diff) + 1.0)) * 200.0);
1022                 switch (button) {
1023                 case 4:
1024                         ScrollUp(scroll);
1025                         break;
1026                 case 5:
1027                         ScrollDown(scroll);
1028                         break;
1029                 }
1030                 lastTime = ev->xbutton.time;
1031                 return 0;
1032         }
1033   out:
1034         last_click_x = x;
1035         last_click_y = y;
1036         
1037         return 0;
1038 }
1039
1040
1041 int BufferView::WorkAreaButtonRelease(FL_OBJECT * ob, Window ,
1042                                       int /*w*/, int /*h*/,
1043                                       XEvent * ev, void * /*d*/)
1044 {
1045         if (buffer_ == 0 || screen == 0) return 0;
1046
1047         int const x = ev->xbutton.x - ob->x;
1048         int const y = ev->xbutton.y - ob->y;
1049
1050         // If we hit an inset, we have the inset coordinates in these
1051         // and inset_hit points to the inset.  If we do not hit an
1052         // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1053         int inset_x = x;
1054         int inset_y = y;
1055         Inset * inset_hit = checkInsetHit(inset_x, inset_y);
1056
1057         if (buffer_->the_locking_inset) {
1058                 // We are in inset locking mode.
1059
1060                 /* LyX does a kind of work-area grabbing for insets.
1061                    Only a ButtonPress Event outside the inset will 
1062                    force a InsetUnlock. */
1063                 buffer_->the_locking_inset->
1064                         InsetButtonRelease(inset_x, inset_y, 
1065                                            ev->xbutton.button);
1066                 return 0;
1067         }
1068   
1069         selection_possible = false;
1070         if (text->cursor.par->table) {
1071                 int cell = text->
1072                         NumberOfCell(text->cursor.par,
1073                                      text->cursor.pos);
1074                 if (text->cursor.par->table->IsContRow(cell) &&
1075                     text->cursor.par->table->
1076                     CellHasContRow(text->cursor.par->table->
1077                                    GetCellAbove(cell))<0) {
1078                         text->CursorUp();
1079                 }
1080         }
1081         
1082         if (ev->xbutton.button >= 2)
1083                 return 0;
1084
1085         // Make sure that the press was not far from the release
1086         if ((abs(last_click_x - x) >= 5) ||
1087             (abs(last_click_y - y) >= 5)) {
1088                 return 0;
1089         }
1090
1091         // Did we hit an editable inset?
1092         if (inset_hit != 0) {
1093                 // Inset like error, notes and figures
1094                 selection_possible = false;
1095 #ifdef WITH_WARNINGS
1096 #warning fix this proper in 0.13
1097 #endif
1098                 // Following a ref shouldn't issue
1099                 // a push on the undo-stack
1100                 // anylonger, now that we have
1101                 // keybindings for following
1102                 // references and returning from
1103                 // references.  IMHO though, it
1104                 // should be the inset's own business
1105                 // to push or not push on the undo
1106                 // stack. They don't *have* to
1107                 // alter the document...
1108                 // (Joacim)
1109                 // ...or maybe the SetCursorParUndo()
1110                 // below isn't necessary at all anylonger?
1111                 if (inset_hit->LyxCode() == Inset::REF_CODE) {
1112                         text->SetCursorParUndo();
1113                 }
1114
1115                 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
1116                 inset_hit->Edit(inset_x, inset_y);
1117                 return 0;
1118         }
1119
1120         // check whether we want to open a float
1121         if (text) {
1122                 bool hit = false;
1123                 char c = ' ';
1124                 if (text->cursor.pos <
1125                     text->cursor.par->Last()) {
1126                         c = text->cursor.par->
1127                                 GetChar(text->cursor.pos);
1128                 }
1129                 if (c == LyXParagraph::META_FOOTNOTE
1130                     || c == LyXParagraph::META_MARGIN
1131                     || c == LyXParagraph::META_FIG
1132                     || c == LyXParagraph::META_TAB
1133                     || c == LyXParagraph::META_WIDE_FIG
1134                     || c == LyXParagraph::META_WIDE_TAB
1135                     || c == LyXParagraph::META_ALGORITHM){
1136                         hit = true;
1137                 } else if (text->cursor.pos - 1 >= 0) {
1138                         c = text->cursor.par->
1139                                 GetChar(text->cursor.pos - 1);
1140                         if (c == LyXParagraph::META_FOOTNOTE
1141                             || c == LyXParagraph::META_MARGIN
1142                             || c == LyXParagraph::META_FIG
1143                             || c == LyXParagraph::META_TAB
1144                             || c == LyXParagraph::META_WIDE_FIG 
1145                             || c == LyXParagraph::META_WIDE_TAB
1146                             || c == LyXParagraph::META_ALGORITHM){
1147                                 // We are one step too far to the right
1148                                 text->CursorLeft();
1149                                 hit = true;
1150                         }
1151                 }
1152                 if (hit == true) {
1153                         ToggleFloat();
1154                         selection_possible = false;
1155                         return 0;
1156                 }
1157         }
1158
1159         // Do we want to close a float? (click on the float-label)
1160         if (text->cursor.row->par->footnoteflag == 
1161             LyXParagraph::OPEN_FOOTNOTE
1162             && text->cursor.pos == 0
1163             && text->cursor.row->previous &&
1164             text->cursor.row->previous->par->
1165             footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
1166                 LyXFont font (LyXFont::ALL_SANE);
1167                 font.setSize(LyXFont::SIZE_SMALL);
1168
1169                 int box_x = 20; // LYX_PAPER_MARGIN;
1170                 box_x += font.textWidth("Mwide-figM", 10);
1171
1172                 int screen_first = screen->first;
1173
1174                 if (x < box_x
1175                     && y + screen_first > text->cursor.y -
1176                     text->cursor.row->baseline
1177                     && y + screen_first < text->cursor.y -
1178                     text->cursor.row->baseline
1179                     + font.maxAscent() * 1.2 + font.maxDescent() * 1.2) {
1180                         ToggleFloat();
1181                         selection_possible = false;
1182                         return 0;
1183                 }
1184         }
1185
1186         // Maybe we want to edit a bibitem ale970302
1187         if (text->cursor.par->bibkey && x < 20 + 
1188             bibitemMaxWidth(textclasslist
1189                             .TextClass(buffer_->
1190                                        params.textclass).defaultfont())) {
1191                 text->cursor.par->bibkey->Edit(0, 0);
1192         }
1193
1194         return 0;
1195 }
1196
1197
1198 /* 
1199  * Returns an inset if inset was hit. 0 otherwise.
1200  * If hit, the coordinates are changed relative to the inset. 
1201  * Otherwise coordinates are not changed, and false is returned.
1202  */
1203 Inset * BufferView::checkInsetHit(int & x, int & y)
1204 {
1205         if (!getScreen())
1206                 return 0;
1207   
1208         int y_tmp = y + getScreen()->first;
1209   
1210         LyXCursor cursor = text->cursor;
1211         if (cursor.pos < cursor.par->Last() 
1212             && cursor.par->GetChar(cursor.pos) == LyXParagraph::META_INSET
1213             && cursor.par->GetInset(cursor.pos)
1214             && cursor.par->GetInset(cursor.pos)->Editable()) {
1215
1216                 // Check whether the inset really was hit
1217                 Inset * tmpinset = cursor.par->GetInset(cursor.pos);
1218                 LyXFont font = text->GetFont(cursor.par, cursor.pos);
1219                 if (x > cursor.x
1220                     && x < cursor.x + tmpinset->Width(font) 
1221                     && y_tmp > cursor.y - tmpinset->Ascent(font)
1222                     && y_tmp < cursor.y + tmpinset->Descent(font)) {
1223                         x = x - cursor.x;
1224                         // The origin of an inset is on the baseline
1225                         y = y_tmp - (cursor.y); 
1226                         return tmpinset;
1227                 }
1228         } else if (cursor.pos - 1 >= 0 
1229                    && cursor.par->GetChar(cursor.pos - 1) == LyXParagraph::META_INSET
1230                    && cursor.par->GetInset(cursor.pos - 1)
1231                    && cursor.par->GetInset(cursor.pos - 1)->Editable()) {
1232                 text->CursorLeft();
1233                 Inset * result = checkInsetHit(x, y);
1234                 if (result == 0) {
1235                         text->CursorRight();
1236                         return 0;
1237                 } else {
1238                         return result;
1239                 }
1240         }
1241         return 0;
1242 }
1243
1244
1245 int BufferView::workAreaExpose()
1246 {
1247         if (!work_area || !work_area->form->visible) 
1248                 return 1;
1249
1250         // this is a hack to ensure that we only call this through
1251         // BufferView::redraw().
1252         if (!lgb_hack) {
1253                 redraw();
1254         }
1255         
1256         static int work_area_width = work_area->w;
1257         static int work_area_height = work_area->h;
1258
1259         bool widthChange = work_area->w != work_area_width;
1260         bool heightChange = work_area->h != work_area_height;
1261
1262         // update from work area
1263         work_area_width = work_area->w;
1264         work_area_height = work_area->h;
1265         if (buffer_ != 0) {
1266                 if (widthChange) {
1267                         // All buffers need a resize
1268                         bufferlist.resize();
1269                 } else if (heightChange) {
1270                         // Rebuild image of current screen
1271                         updateScreen();
1272                         // fitCursor() ensures we don't jump back
1273                         // to the start of the document on vertical
1274                         // resize
1275                         fitCursor();
1276
1277                         // The main window size has changed, repaint most stuff
1278                         redraw();
1279                         // ...including the minibuffer
1280                         owner_->getMiniBuffer()->Init();
1281
1282                 } else if (screen) screen->Redraw();
1283         } else {
1284                 // Grey box when we don't have a buffer
1285                 fl_winset(FL_ObjWin(work_area));
1286                 fl_rectangle(1, work_area->x, work_area->y,
1287                              work_area->w, work_area->h, FL_GRAY63);
1288         }
1289
1290         // always make sure that the scrollbar is sane.
1291         updateScrollbar();
1292         owner_->updateLayoutChoice();
1293         return 1;
1294 }
1295
1296
1297 // Callback for cursor timer
1298 void BufferView::CursorToggleCB(FL_OBJECT * ob, long)
1299 {
1300         BufferView * view = static_cast<BufferView*>(ob->u_vdata);
1301         
1302         // Quite a nice place for asyncron Inset updating, isn't it?
1303         // Actually no! This is run even if no buffer exist... so (Lgb)
1304         if (view && !view->buffer_) {
1305                 goto set_timer_and_return;
1306         }
1307
1308         // NOTE:
1309         // On my quest to solve the gs render hangups I am now
1310         // disabling the SIGHUP completely, and will do a wait
1311         // now and then instead. If the guess that xforms somehow
1312         // destroys something is true, this is likely (hopefully)
1313         // to solve the problem...at least I hope so. Lgb
1314
1315         // ...Ok this seems to work...at least it does not make things
1316         // worse so far. However I still see gs processes that hangs.
1317         // I would really like to know _why_ they are hanging. Anyway
1318         // the solution without the SIGCHLD handler seems to be easier
1319         // to debug.
1320
1321         // When attaching gdb to a a running gs that hangs it shows
1322         // that it is waiting for input(?) Is it possible for us to
1323         // provide that input somehow? Or figure what it is expecing
1324         // to read?
1325
1326         // One solution is to, after some time, look if there are some
1327         // old gs processes still running and if there are: kill them
1328         // and re render.
1329
1330         // Another solution is to provide the user an option to rerender
1331         // a picture. This would, for the picture in question, check if
1332         // there is a gs running for it, if so kill it, and start a new
1333         // rendering process.
1334
1335         // these comments posted to lyx@via
1336         {
1337                 int status = 1;
1338                 int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1339                 if (pid == -1) // error find out what is wrong
1340                         ; // ignore it for now.
1341                 else if (pid > 0)
1342                         sigchldhandler(pid, &status);
1343         }
1344         if (InsetUpdateList) 
1345                 UpdateInsetUpdateList();
1346
1347         if (view && !view->screen){
1348                 goto set_timer_and_return;
1349         }
1350
1351         if (view->lyx_focus && view->work_area_focus) {
1352                 if (!view->buffer_->the_locking_inset) {
1353                         view->screen->CursorToggle();
1354                 } else {
1355                         view->buffer_->the_locking_inset->
1356                                 ToggleInsetCursor();
1357                 }
1358                 goto set_timer_and_return;
1359         } else {
1360                 // Make sure that the cursor is visible.
1361                 if (!view->buffer_->the_locking_inset) {
1362                         view->screen->ShowCursor();
1363                 } else {
1364                         if (!view->buffer_->the_locking_inset->isCursorVisible())
1365                                 view->buffer_->the_locking_inset->
1366                                         ToggleInsetCursor();
1367                 }
1368
1369                 // This is only run when work_area_focus or lyx_focus is false.
1370                 Window tmpwin;
1371                 int tmp;
1372                 XGetInputFocus(fl_display, &tmpwin, &tmp);
1373                 if (lyxerr.debugging(Debug::INFO)) {
1374                         lyxerr << "tmpwin: " << tmpwin
1375                                << "\nwindow: " << view->owner_->getForm()->window
1376                                << "\nwork_area_focus: " << view->work_area_focus
1377                                << "\nlyx_focus      : " << view->lyx_focus
1378                                << endl;
1379                 }
1380                 if (tmpwin != view->owner_->getForm()->window) {
1381                         view->lyx_focus = false;
1382                         goto skip_timer;
1383                 } else {
1384                         view->lyx_focus = true;
1385                         if (!view->work_area_focus)
1386                                 goto skip_timer;
1387                         else
1388                                 goto set_timer_and_return;
1389                 }
1390         }
1391
1392   set_timer_and_return:
1393         fl_set_timer(ob, 0.4);
1394   skip_timer:
1395         return;
1396 }
1397
1398
1399 int BufferView::WorkAreaSelectionNotify(FL_OBJECT *, Window win,
1400                                         int /*w*/, int /*h*/,
1401                                         XEvent * event, void */*d*/)
1402 {
1403         if (buffer_ == 0) return 0;
1404         if (event->type != SelectionNotify)
1405                 return 0;
1406
1407         Atom tmpatom;
1408         unsigned long ul1;
1409         unsigned long ul2;
1410         unsigned char * uc = 0;
1411         int tmpint;
1412         screen->HideCursor();
1413         BeforeChange();
1414         if (event->xselection.type == XA_STRING
1415             && event->xselection.property) {
1416     
1417                 if (XGetWindowProperty(
1418                         fl_display            /* display */,
1419                         win /* w */,
1420                         event->xselection.property        /* property */,
1421                         0                /* long_offset */,
1422                         0                /* long_length */,
1423                         false                /* delete */,
1424                         XA_STRING                /* req_type */,
1425                         &tmpatom               /* actual_type_return */,
1426                         &tmpint                /* actual_format_return */,
1427                         &ul1      /* nitems_return */,
1428                         &ul2      /* bytes_after_return */,
1429                         &uc     /* prop_return */
1430                         ) != Success) {
1431                         return 0;
1432                 }
1433                 XFlush(fl_display);
1434
1435                 if (uc) {
1436                         free(uc);
1437                         uc = 0;
1438                 }
1439
1440                 if (XGetWindowProperty(
1441                         fl_display           /* display */,
1442                         win              /* w */,
1443                         event->xselection.property           /* property */,
1444                         0                /* long_offset */,
1445                         ul2/4+1                /* long_length */,
1446                         True                /* delete */,
1447                         XA_STRING                /* req_type */,
1448                         &tmpatom               /* actual_type_return */,
1449                         &tmpint                /* actual_format_return */,
1450                         &ul1      /* nitems_return */,
1451                         &ul2      /* bytes_after_return */,
1452                         &uc     /* prop_return */
1453                         ) != Success) {
1454                         return 0;
1455                 }
1456                 XFlush(fl_display);
1457         
1458                 if (uc) {
1459                         if (!ascii_type) {
1460                                 text->InsertStringA(reinterpret_cast<char*>(uc));
1461                         } else {
1462                                 text->InsertStringB(reinterpret_cast<char*>(uc));
1463                         }
1464                         free(uc);
1465                         uc = 0;
1466                 }
1467
1468                 update(1);
1469         }
1470         return 0;
1471 }
1472
1473
1474 void BufferView::cursorPrevious()
1475 {
1476         if (!text->cursor.row->previous) return;
1477         
1478         long y = getScreen()->first;
1479         Row * cursorrow = text->cursor.row;
1480         text->SetCursorFromCoordinates(text->cursor.x_fix, y);
1481         text->FinishUndo();
1482         // This is to allow jumping over large insets
1483         if ((cursorrow == text->cursor.row))
1484                 text->CursorUp();
1485         
1486         if (text->cursor.row->height < work_area->h)
1487                 getScreen()->Draw(text->cursor.y
1488                                   - text->cursor.row->baseline
1489                                   + text->cursor.row->height
1490                                   - work_area->h +1 );
1491 }
1492
1493
1494 void BufferView::cursorNext()
1495 {
1496         if (!text->cursor.row->next) return;
1497         
1498         long y = getScreen()->first;
1499         text->GetRowNearY(y);
1500         Row * cursorrow = text->cursor.row;
1501         text->SetCursorFromCoordinates(text->cursor.x_fix, y + work_area->h);
1502         text->FinishUndo();
1503         // This is to allow jumping over large insets
1504         if ((cursorrow == text->cursor.row))
1505                 text->CursorDown();
1506         
1507         if (text->cursor.row->height < work_area->h)
1508                 getScreen()->Draw(text->cursor.y
1509                                   - text->cursor.row->baseline);
1510 }
1511
1512
1513 bool BufferView::available() const
1514 {
1515         if (buffer_ && text) return true;
1516         return false;
1517 }
1518
1519
1520 void BufferView::savePosition()
1521 {
1522         backstack.push(buffer()->fileName(),
1523                        text->cursor.x,
1524                        text->cursor.y);
1525 }
1526
1527
1528 void BufferView::restorePosition()
1529 {
1530         if (backstack.empty()) return;
1531         
1532         int  x, y;
1533         string fname = backstack.pop(&x, &y);
1534         
1535         BeforeChange();
1536         Buffer * b = bufferlist.exists(fname) ?
1537                 bufferlist.getBuffer(fname) :
1538                 bufferlist.loadLyXFile(fname); // don't ask, just load it
1539         buffer(b);
1540         text->SetCursorFromCoordinates(x, y);
1541         update(0);
1542
1543
1544
1545 void BufferView::update(signed char f)
1546 {
1547         owner()->updateLayoutChoice();
1548
1549         if (!text->selection && f > -3)
1550                 text->sel_cursor = text->cursor;
1551         
1552         FreeUpdateTimer();
1553         text->FullRebreak();
1554
1555         update();
1556
1557         if (f != 3 && f != -3) {
1558                 fitCursor();
1559                 updateScrollbar();
1560         }
1561
1562         if (f == 1 || f == -1) {
1563                 if (buffer()->isLyxClean()) {
1564                         buffer()->markDirty();
1565                         owner()->getMiniBuffer()->setTimer(4);
1566                 } else {
1567                         buffer()->markDirty();
1568                 }
1569         }
1570 }