]> git.lyx.org Git - features.git/blob - src/BufferView.C
remove bogus backslashed from iso-8859-1, try to fix insert and encodeString, fixes...
[features.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         const int 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
832         if (buffer_ == 0) return 0;
833         if (!screen) return 0;
834
835         // Check for inset locking
836         if (buffer_->the_locking_inset) {
837                 LyXCursor cursor = text->cursor;
838                 buffer_->the_locking_inset->
839                         InsetMotionNotify(ev->xbutton.x - ob->x - cursor.x,
840                                           ev->xbutton.y - ob->y -
841                                           (cursor.y),
842                                           ev->xbutton.state);
843                 return 0;
844         }
845    
846         // Only use motion with button 1
847         if (!ev->xmotion.state & Button1MotionMask)
848                 return 0; 
849    
850         /* The selection possible is needed, that only motion events are 
851          * used, where the bottom press event was on the drawing area too */
852         if (selection_possible) {
853                 screen->HideCursor();
854
855                 text->SetCursorFromCoordinates(ev->xbutton.x - ob->x,
856                                                ev->xbutton.y - ob->y +
857                                                screen->first);
858       
859                 if (!text->selection)
860                         update(-3); // Maybe an empty line was deleted
861       
862                 text->SetSelection();
863                 screen->ToggleToggle();
864                 if (screen->FitCursor())
865                         updateScrollbar(); 
866                 screen->ShowCursor();
867         }
868         return 0;
869 }
870
871
872 extern int bibitemMaxWidth(LyXFont const &);
873
874 // Single-click on work area
875 int BufferView::WorkAreaButtonPress(FL_OBJECT * ob, Window,
876                                     int /*w*/, int /*h*/,
877                                     XEvent * ev, void */*d*/)
878 {
879         last_click_x = -1;
880         last_click_y = -1;
881
882         if (buffer_ == 0) return 0;
883         if (!screen) return 0;
884
885         int const x = ev->xbutton.x - ob->x;
886         int const y = ev->xbutton.y - ob->y;
887         // If we hit an inset, we have the inset coordinates in these
888         // and inset_hit points to the inset.  If we do not hit an
889         // inset, inset_hit is 0, and inset_x == x, inset_y == y.
890         int inset_x = x;
891         int inset_y = y;
892         Inset * inset_hit = checkInsetHit(inset_x, inset_y);
893
894         // ok ok, this is a hack.
895         int button = ev->xbutton.button;
896         if (button == 4 || button == 5) goto wheel;
897
898         {
899                 
900                 if (buffer_->the_locking_inset) {
901                         // We are in inset locking mode
902                 
903                         /* Check whether the inset was hit. If not reset mode,
904                            otherwise give the event to the inset */
905                         if (inset_hit != 0) {
906                                 buffer_->the_locking_inset->
907                                         InsetButtonPress(inset_x, inset_y,
908                                                          button);
909                                 return 0;
910                         } else {
911                                 UnlockInset(buffer_->the_locking_inset);
912                         }
913                 }
914         
915                 selection_possible = true;
916                 screen->HideCursor();
917         
918                 // Right button mouse click on a table
919                 if (button == 3 &&
920                     (text->cursor.par->table ||
921                      text->MouseHitInTable(x, y + screen->first))) {
922                         // Set the cursor to the press-position
923                         text->SetCursorFromCoordinates(x, y + screen->first);
924                         bool doit = true;
925                 
926                         // Only show the table popup if the hit is in
927                         // the table, too
928                         if (!text->HitInTable(text->cursor.row, x))
929                                 doit = false;
930                 
931                         // Hit above or below the table?
932                         if (doit) {
933                                 if (!text->selection) {
934                                         screen->ToggleSelection();
935                                         text->ClearSelection();
936                                         text->FullRebreak();
937                                         screen->Update();
938                                         updateScrollbar();
939                                 }
940                                 // Popup table popup when on a table.
941                                 // This is obviously temporary, since we
942                                 // should be able to popup various
943                                 // context-sensitive-menus with the
944                                 // the right mouse. So this should be done more
945                                 // general in the future. Matthias.
946                                 selection_possible = false;
947                                 owner_->getLyXFunc()
948                                         ->Dispatch(LFUN_LAYOUT_TABLE,
949                                                    "true");
950                                 return 0;
951                         }
952                 }
953         
954                 int screen_first = screen->first;
955         
956                 // Middle button press pastes if we have a selection
957                 bool paste_internally = false;
958                 if (button == 2  // && !buffer_->the_locking_inset
959                     && text->selection) {
960                         owner_->getLyXFunc()->Dispatch(LFUN_COPY);
961                         paste_internally = true;
962                 }
963         
964                 // Clear the selection
965                 screen->ToggleSelection();
966                 text->ClearSelection();
967                 text->FullRebreak();
968                 screen->Update();
969                 updateScrollbar();
970                 
971                 // Single left click in math inset?
972                 if (inset_hit != 0 && inset_hit->Editable() == 2) {
973                         // Highly editable inset, like math
974                         selection_possible = false;
975                         owner_->updateLayoutChoice();
976                         owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
977                         inset_hit->Edit(inset_x, inset_y);
978                         return 0;
979                 } 
980
981                 // Right click on a footnote flag opens float menu
982                 if (button == 3) { 
983                         selection_possible = false;
984                         return 0;
985                 }
986         
987                 text->SetCursorFromCoordinates(x, y + screen_first);
988                 text->FinishUndo();
989                 text->sel_cursor = text->cursor;
990                 text->cursor.x_fix = text->cursor.x;
991         
992                 owner_->updateLayoutChoice();
993                 if (screen->FitCursor()){
994                         updateScrollbar();
995                         selection_possible = false;
996                 }
997
998                 // Insert primary selection with middle mouse
999                 // if there is a local selection in the current buffer,
1000                 // insert this
1001                 if (button == 2) { //  && !buffer_->the_locking_inset){
1002                         if (paste_internally)
1003                                 owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
1004                         else
1005                                 owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
1006                                                                "paragraph");
1007                         selection_possible = false;
1008                         return 0;
1009                 }
1010         }
1011         goto out;
1012   wheel:
1013         {
1014                 // I am not quite sure if this is the correct place to put
1015                 // this, but it will not cause any harm.
1016                 // Patch from Mark Huang (markman@mit.edu) to make LyX
1017                 // recognise button 4 and 5. This enables LyX use use
1018                 // the scrollwhell on certain mice for something useful. (Lgb)
1019                 // Added wheel acceleration detection code. (Rvdk)
1020                 static Time lastTime = 0;
1021                 int diff = ev->xbutton.time - lastTime;
1022                 int scroll = int(1.0 + (4.0 / (abs(diff) + 1.0)) * 200.0);
1023                 switch (button) {
1024                 case 4:
1025                         ScrollUp(scroll);
1026                         break;
1027                 case 5:
1028                         ScrollDown(scroll);
1029                         break;
1030                 }
1031                 lastTime = ev->xbutton.time;
1032                 return 0;
1033         }
1034   out:
1035         last_click_x = x;
1036         last_click_y = y;
1037         
1038         return 0;
1039 }
1040
1041
1042 int BufferView::WorkAreaButtonRelease(FL_OBJECT * ob, Window ,
1043                                       int /*w*/, int /*h*/,
1044                                       XEvent * ev, void * /*d*/)
1045 {
1046         if (buffer_ == 0 || screen == 0) return 0;
1047
1048         int const x = ev->xbutton.x - ob->x;
1049         int const y = ev->xbutton.y - ob->y;
1050
1051         // If we hit an inset, we have the inset coordinates in these
1052         // and inset_hit points to the inset.  If we do not hit an
1053         // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1054         int inset_x = x;
1055         int inset_y = y;
1056         Inset * inset_hit = checkInsetHit(inset_x, inset_y);
1057
1058         if (buffer_->the_locking_inset) {
1059                 // We are in inset locking mode.
1060
1061                 /* LyX does a kind of work-area grabbing for insets.
1062                    Only a ButtonPress Event outside the inset will 
1063                    force a InsetUnlock. */
1064                 buffer_->the_locking_inset->
1065                         InsetButtonRelease(inset_x, inset_y, 
1066                                            ev->xbutton.button);
1067                 return 0;
1068         }
1069   
1070         selection_possible = false;
1071         if (text->cursor.par->table) {
1072                 int cell = text->
1073                         NumberOfCell(text->cursor.par,
1074                                      text->cursor.pos);
1075                 if (text->cursor.par->table->IsContRow(cell) &&
1076                     text->cursor.par->table->
1077                     CellHasContRow(text->cursor.par->table->
1078                                    GetCellAbove(cell))<0) {
1079                         text->CursorUp();
1080                 }
1081         }
1082         
1083         if (ev->xbutton.button >= 2)
1084                 return 0;
1085
1086         // Make sure that the press was not far from the release
1087         if ((abs(last_click_x - x) >= 5) ||
1088             (abs(last_click_y - y) >= 5)) {
1089                 return 0;
1090         }
1091
1092         // Did we hit an editable inset?
1093         if (inset_hit != 0) {
1094                 // Inset like error, notes and figures
1095                 selection_possible = false;
1096 #ifdef WITH_WARNINGS
1097 #warning fix this proper in 0.13
1098 #endif
1099                 // Following a ref shouldn't issue
1100                 // a push on the undo-stack
1101                 // anylonger, now that we have
1102                 // keybindings for following
1103                 // references and returning from
1104                 // references.  IMHO though, it
1105                 // should be the inset's own business
1106                 // to push or not push on the undo
1107                 // stack. They don't *have* to
1108                 // alter the document...
1109                 // (Joacim)
1110                 // ...or maybe the SetCursorParUndo()
1111                 // below isn't necessary at all anylonger?
1112                 if (inset_hit->LyxCode() == Inset::REF_CODE) {
1113                         text->SetCursorParUndo();
1114                 }
1115
1116                 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
1117                 inset_hit->Edit(inset_x, inset_y);
1118                 return 0;
1119         }
1120
1121         // check whether we want to open a float
1122         if (text) {
1123                 bool hit = false;
1124                 char c = ' ';
1125                 if (text->cursor.pos <
1126                     text->cursor.par->Last()) {
1127                         c = text->cursor.par->
1128                                 GetChar(text->cursor.pos);
1129                 }
1130                 if (c == LyXParagraph::META_FOOTNOTE
1131                     || c == LyXParagraph::META_MARGIN
1132                     || c == LyXParagraph::META_FIG
1133                     || c == LyXParagraph::META_TAB
1134                     || c == LyXParagraph::META_WIDE_FIG
1135                     || c == LyXParagraph::META_WIDE_TAB
1136                     || c == LyXParagraph::META_ALGORITHM){
1137                         hit = true;
1138                 } else if (text->cursor.pos - 1 >= 0) {
1139                         c = text->cursor.par->
1140                                 GetChar(text->cursor.pos - 1);
1141                         if (c == LyXParagraph::META_FOOTNOTE
1142                             || c == LyXParagraph::META_MARGIN
1143                             || c == LyXParagraph::META_FIG
1144                             || c == LyXParagraph::META_TAB
1145                             || c == LyXParagraph::META_WIDE_FIG 
1146                             || c == LyXParagraph::META_WIDE_TAB
1147                             || c == LyXParagraph::META_ALGORITHM){
1148                                 // We are one step too far to the right
1149                                 text->CursorLeft();
1150                                 hit = true;
1151                         }
1152                 }
1153                 if (hit == true) {
1154                         ToggleFloat();
1155                         selection_possible = false;
1156                         return 0;
1157                 }
1158         }
1159
1160         // Do we want to close a float? (click on the float-label)
1161         if (text->cursor.row->par->footnoteflag == 
1162             LyXParagraph::OPEN_FOOTNOTE
1163             && text->cursor.pos == 0
1164             && text->cursor.row->previous &&
1165             text->cursor.row->previous->par->
1166             footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
1167                 LyXFont font (LyXFont::ALL_SANE);
1168                 font.setSize(LyXFont::SIZE_SMALL);
1169
1170                 int box_x = 20; // LYX_PAPER_MARGIN;
1171                 box_x += font.textWidth("Mwide-figM", 10);
1172
1173                 int screen_first = screen->first;
1174
1175                 if (x < box_x
1176                     && y + screen_first > text->cursor.y -
1177                     text->cursor.row->baseline
1178                     && y + screen_first < text->cursor.y -
1179                     text->cursor.row->baseline
1180                     + font.maxAscent() * 1.2 + font.maxDescent() * 1.2) {
1181                         ToggleFloat();
1182                         selection_possible = false;
1183                         return 0;
1184                 }
1185         }
1186
1187         // Maybe we want to edit a bibitem ale970302
1188         if (text->cursor.par->bibkey && x < 20 + 
1189             bibitemMaxWidth(textclasslist
1190                             .TextClass(buffer_->
1191                                        params.textclass).defaultfont())) {
1192                 text->cursor.par->bibkey->Edit(0, 0);
1193         }
1194
1195         return 0;
1196 }
1197
1198
1199 /* 
1200  * Returns an inset if inset was hit. 0 otherwise.
1201  * If hit, the coordinates are changed relative to the inset. 
1202  * Otherwise coordinates are not changed, and false is returned.
1203  */
1204 Inset * BufferView::checkInsetHit(int & x, int & y)
1205 {
1206         if (!getScreen())
1207                 return 0;
1208   
1209         int y_tmp = y + getScreen()->first;
1210   
1211         LyXCursor cursor = text->cursor;
1212         if (cursor.pos < cursor.par->Last() 
1213             && cursor.par->GetChar(cursor.pos) == LyXParagraph::META_INSET
1214             && cursor.par->GetInset(cursor.pos)
1215             && cursor.par->GetInset(cursor.pos)->Editable()) {
1216
1217                 // Check whether the inset really was hit
1218                 Inset * tmpinset = cursor.par->GetInset(cursor.pos);
1219                 LyXFont font = text->GetFont(cursor.par, cursor.pos);
1220                 if (x > cursor.x
1221                     && x < cursor.x + tmpinset->Width(font) 
1222                     && y_tmp > cursor.y - tmpinset->Ascent(font)
1223                     && y_tmp < cursor.y + tmpinset->Descent(font)) {
1224                         x = x - cursor.x;
1225                         // The origin of an inset is on the baseline
1226                         y = y_tmp - (cursor.y); 
1227                         return tmpinset;
1228                 }
1229         } else if (cursor.pos - 1 >= 0 
1230                    && cursor.par->GetChar(cursor.pos - 1) == LyXParagraph::META_INSET
1231                    && cursor.par->GetInset(cursor.pos - 1)
1232                    && cursor.par->GetInset(cursor.pos - 1)->Editable()) {
1233                 text->CursorLeft();
1234                 Inset * result = checkInsetHit(x, y);
1235                 if (result == 0) {
1236                         text->CursorRight();
1237                         return 0;
1238                 } else {
1239                         return result;
1240                 }
1241         }
1242         return 0;
1243 }
1244
1245
1246 int BufferView::workAreaExpose()
1247 {
1248         if (!work_area || !work_area->form->visible) 
1249                 return 1;
1250
1251         // this is a hack to ensure that we only call this through
1252         // BufferView::redraw().
1253         if (!lgb_hack) {
1254                 redraw();
1255         }
1256         
1257         static int work_area_width = work_area->w;
1258         static int work_area_height = work_area->h;
1259
1260         bool widthChange = work_area->w != work_area_width;
1261         bool heightChange = work_area->h != work_area_height;
1262
1263         // update from work area
1264         work_area_width = work_area->w;
1265         work_area_height = work_area->h;
1266         if (buffer_ != 0) {
1267                 if (widthChange) {
1268                         // All buffers need a resize
1269                         bufferlist.resize();
1270                 } else if (heightChange) {
1271                         // Rebuild image of current screen
1272                         updateScreen();
1273                         // fitCursor() ensures we don't jump back
1274                         // to the start of the document on vertical
1275                         // resize
1276                         fitCursor();
1277
1278                         // The main window size has changed, repaint most stuff
1279                         redraw();
1280                         // ...including the minibuffer
1281                         owner_->getMiniBuffer()->Init();
1282
1283                 } else if (screen) screen->Redraw();
1284         } else {
1285                 // Grey box when we don't have a buffer
1286                 fl_winset(FL_ObjWin(work_area));
1287                 fl_rectangle(1, work_area->x, work_area->y,
1288                              work_area->w, work_area->h, FL_GRAY63);
1289         }
1290
1291         // always make sure that the scrollbar is sane.
1292         updateScrollbar();
1293         owner_->updateLayoutChoice();
1294         return 1;
1295 }
1296
1297
1298 // Callback for cursor timer
1299 void BufferView::CursorToggleCB(FL_OBJECT * ob, long)
1300 {
1301         BufferView * view = static_cast<BufferView*>(ob->u_vdata);
1302         
1303         /* quite a nice place for asyncron Inset updating, isn't it? */
1304         // actually no! This is run even if no buffer exist... so (Lgb)
1305         if (view && !view->buffer_) {
1306                 goto set_timer_and_return;
1307         }
1308
1309         // NOTE:
1310         // On my quest to solve the gs rendre hangups I am now
1311         // disabling the SIGHUP completely, and will do a wait
1312         // now and then instead. If the guess that xforms somehow
1313         // destroys something is true, this is likely (hopefully)
1314         // to solve the problem...at least I hope so. Lgb
1315
1316         // ...Ok this seems to work...at least it does not make things
1317         // worse so far. However I still see gs processes that hangs.
1318         // I would really like to know _why_ they are hanging. Anyway
1319         // the solution without the SIGCHLD handler seems to be easier
1320         // to debug.
1321
1322         // When attaching gdb to a a running gs that hangs it shows
1323         // that it is waiting for input(?) Is it possible for us to
1324         // provide that input somehow? Or figure what it is expecing
1325         // to read?
1326
1327         // One solution is to, after some time, look if there are some
1328         // old gs processes still running and if there are: kill them
1329         // and re render.
1330
1331         // Another solution is to provide the user an option to rerender
1332         // a picture. This would, for the picture in question, check if
1333         // there is a gs running for it, if so kill it, and start a new
1334         // rendering process.
1335
1336         // these comments posted to lyx@via
1337         {
1338                 int status = 1;
1339                 int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1340                 if (pid == -1) // error find out what is wrong
1341                         ; // ignore it for now.
1342                 else if (pid > 0)
1343                         sigchldhandler(pid, &status);
1344         }
1345         if (InsetUpdateList) 
1346                 UpdateInsetUpdateList();
1347
1348         if (view && !view->screen){
1349                 goto set_timer_and_return;
1350         }
1351
1352         if (view->lyx_focus && view->work_area_focus) {
1353                 if (!view->buffer_->the_locking_inset){
1354                         view->screen->CursorToggle();
1355                 } else {
1356                         view->buffer_->the_locking_inset->
1357                                 ToggleInsetCursor();
1358                 }
1359                 goto set_timer_and_return;
1360         } else {
1361                 // Make sure that the cursor is visible.
1362                 if (!view->buffer_->the_locking_inset){
1363                         view->screen->ShowCursor();
1364                 } else {
1365                         if (!view->buffer_->the_locking_inset->isCursorVisible())
1366                                 view->buffer_->the_locking_inset->
1367                                         ToggleInsetCursor();
1368                 }
1369
1370                 // This is only run when work_area_focus or lyx_focus is false.
1371                 Window tmpwin;
1372                 int tmp;
1373                 XGetInputFocus(fl_display, &tmpwin, &tmp);
1374                 if (lyxerr.debugging(Debug::INFO)) {
1375                         lyxerr << "tmpwin: " << tmpwin
1376                                << "\nwindow: " << view->owner_->getForm()->window
1377                                << "\nwork_area_focus: " << view->work_area_focus
1378                                << "\nlyx_focus      : " << view->lyx_focus
1379                                << endl;
1380                 }
1381                 if (tmpwin != view->owner_->getForm()->window) {
1382                         view->lyx_focus = false;
1383                         goto skip_timer;
1384                 } else {
1385                         view->lyx_focus = true;
1386                         if (!view->work_area_focus)
1387                                 goto skip_timer;
1388                         else
1389                                 goto set_timer_and_return;
1390                 }
1391         }
1392
1393   set_timer_and_return:
1394         fl_set_timer(ob, 0.4);
1395   skip_timer:
1396         return;
1397 }
1398
1399
1400 int BufferView::WorkAreaSelectionNotify(FL_OBJECT *, Window win,
1401                                         int /*w*/, int /*h*/,
1402                                         XEvent * event, void */*d*/)
1403 {
1404         if (buffer_ == 0) return 0;
1405         if (event->type != SelectionNotify)
1406                 return 0;
1407
1408         Atom tmpatom;
1409         unsigned long ul1;
1410         unsigned long ul2;
1411         unsigned char * uc = 0;
1412         int tmpint;
1413         screen->HideCursor();
1414         BeforeChange();
1415         if (event->xselection.type == XA_STRING
1416             && event->xselection.property) {
1417     
1418                 if (XGetWindowProperty(
1419                         fl_display            /* display */,
1420                         win /* w */,
1421                         event->xselection.property        /* property */,
1422                         0                /* long_offset */,
1423                         0                /* long_length */,
1424                         false                /* delete */,
1425                         XA_STRING                /* req_type */,
1426                         &tmpatom               /* actual_type_return */,
1427                         &tmpint                /* actual_format_return */,
1428                         &ul1      /* nitems_return */,
1429                         &ul2      /* bytes_after_return */,
1430                         &uc     /* prop_return */
1431                         ) != Success) {
1432                         return 0;
1433                 }
1434                 XFlush(fl_display);
1435
1436                 if (uc){
1437                         free(uc);
1438                         uc = 0;
1439                 }
1440
1441                 if (XGetWindowProperty(
1442                         fl_display           /* display */,
1443                         win              /* w */,
1444                         event->xselection.property           /* property */,
1445                         0                /* long_offset */,
1446                         ul2/4+1                /* long_length */,
1447                         True                /* delete */,
1448                         XA_STRING                /* req_type */,
1449                         &tmpatom               /* actual_type_return */,
1450                         &tmpint                /* actual_format_return */,
1451                         &ul1      /* nitems_return */,
1452                         &ul2      /* bytes_after_return */,
1453                         &uc     /* prop_return */
1454                         ) != Success) {
1455                         return 0;
1456                 }
1457                 XFlush(fl_display);
1458         
1459                 if (uc) {
1460                         if (!ascii_type) {
1461                                 text->InsertStringA(reinterpret_cast<char*>(uc));
1462                         } else {
1463                                 text->InsertStringB(reinterpret_cast<char*>(uc));
1464                         }
1465                         free(uc);
1466                         uc = 0;
1467                 }
1468
1469                 update(1);
1470         }
1471         return 0;
1472 }
1473
1474
1475 void BufferView::cursorPrevious()
1476 {
1477         if (!text->cursor.row->previous) return;
1478         
1479         long y = getScreen()->first;
1480         Row * cursorrow = text->cursor.row;
1481         text->SetCursorFromCoordinates(text->cursor.x_fix, y);
1482         text->FinishUndo();
1483         // this is to allow jumping over large insets
1484         if ((cursorrow == text->cursor.row))
1485                 text->CursorUp();
1486         
1487         if (text->cursor.row->height < work_area->h)
1488                 getScreen()->Draw(text->cursor.y
1489                                   - text->cursor.row->baseline
1490                                   + text->cursor.row->height
1491                                   - work_area->h +1 );
1492 }
1493
1494
1495 void BufferView::cursorNext()
1496 {
1497         if (!text->cursor.row->next) return;
1498         
1499         long y = getScreen()->first;
1500         text->GetRowNearY(y);
1501         Row * cursorrow = text->cursor.row;
1502         text->SetCursorFromCoordinates(text->cursor.x_fix, y + work_area->h);
1503         text->FinishUndo();
1504         /* this is to allow jumping over large insets */
1505         if ((cursorrow == text->cursor.row))
1506                 text->CursorDown();
1507         
1508         if (text->cursor.row->height < work_area->h)
1509                 getScreen()->Draw(text->cursor.y
1510                                   - text->cursor.row->baseline);
1511 }
1512
1513
1514 bool BufferView::available() const
1515 {
1516         if (buffer_ && text) return true;
1517         return false;
1518 }
1519
1520
1521 void BufferView::savePosition()
1522 {
1523         backstack.push(buffer()->fileName(),
1524                        text->cursor.x,
1525                        text->cursor.y);
1526 }
1527
1528
1529 void BufferView::restorePosition()
1530 {
1531         if (backstack.empty()) return;
1532         
1533         int  x, y;
1534         string fname = backstack.pop(&x, &y);
1535         
1536         BeforeChange();
1537         Buffer * b = bufferlist.exists(fname) ?
1538                 bufferlist.getBuffer(fname) :
1539                 bufferlist.loadLyXFile(fname); // don't ask, just load it
1540         buffer(b);
1541         text->SetCursorFromCoordinates(x, y);
1542         update(0);
1543
1544
1545
1546 // candidate for move to BufferView
1547 void BufferView::update(signed char f)
1548 {
1549         owner()->updateLayoutChoice();
1550
1551         if (!text->selection && f > -3)
1552                 text->sel_cursor = text->cursor;
1553         
1554         FreeUpdateTimer();
1555         text->FullRebreak();
1556
1557         update();
1558
1559         if (f != 3 && f != -3) {
1560                 fitCursor();
1561                 updateScrollbar();
1562         }
1563
1564         if (f == 1 || f == -1) {
1565                 if (buffer()->isLyxClean()) {
1566                         buffer()->markDirty();
1567                         owner()->getMiniBuffer()->setTimer(4);
1568                 } else {
1569                         buffer()->markDirty();
1570                 }
1571         }
1572 }