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