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