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