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