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