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