10 #include "BufferView_pimpl.h"
12 #include "lyxscreen.h"
16 #include "commandtags.h"
18 #include "minibuffer.h"
20 #include "bufferview_funcs.h"
21 #include "TextCache.h"
22 #include "bufferlist.h"
23 #include "insets/insetbib.h"
24 #include "lyx_gui_misc.h"
27 #include "support/LAssert.h"
28 #include "frontends/Dialogs.h"
30 #ifdef SIGC_CXX_NAMESPACES
39 /* the selection possible is needed, that only motion events are
40 * used, where the bottom press event was on the drawing area too */
41 bool selection_possible = false;
43 extern BufferList bufferlist;
44 extern char ascii_type;
46 extern "C" void TimerCB(FL_OBJECT *, long);
47 extern void sigchldhandler(pid_t pid, int * status);
48 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
54 XSync(fl_get_display(), 0);
59 void SetXtermCursor(Window win)
62 static bool cursor_undefined = true;
63 if (cursor_undefined){
64 cursor = XCreateFontCursor(fl_display, XC_xterm);
66 cursor_undefined = false;
68 XDefineCursor(fl_display, win, cursor);
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)
78 workarea_ = new WorkArea(bv_, xpos, ypos, width, height);
80 workarea_->workAreaExpose
81 .connect(slot(this, &BufferView::Pimpl::workAreaExpose));
82 workarea_->workAreaEnter
83 .connect(slot(this, &BufferView::Pimpl::enterView));
84 workarea_->workAreaLeave
85 .connect(slot(this, &BufferView::Pimpl::leaveView));
86 workarea_->workAreaButtonPress
87 .connect(slot(this, &BufferView::Pimpl::workAreaButtonPress));
88 workarea_->workAreaButtonRelease
90 &BufferView::Pimpl::workAreaButtonRelease));
91 workarea_->workAreaMotionNotify
92 .connect(slot(this, &BufferView::Pimpl::workAreaMotionNotify));
93 workarea_->workAreaDoubleClick
94 .connect(slot(this, &BufferView::Pimpl::doubleClick));
95 workarea_->workAreaTripleClick
96 .connect(slot(this, &BufferView::Pimpl::tripleClick));
97 workarea_->workAreaKeyPress
98 .connect(slot(this, &BufferView::Pimpl::workAreaKeyPress));
102 cursor_timeout.timeout.connect(slot(this,
103 &BufferView::Pimpl::cursorToggle));
104 current_scrollbar_value = 0;
105 cursor_timeout.start();
106 workarea_->setFocus();
107 using_xterm_cursor = false;
111 Painter & BufferView::Pimpl::painter()
113 return workarea_->getPainter();
117 void BufferView::Pimpl::buffer(Buffer * b)
119 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
123 buffer_->delUser(bv_);
125 // Put the old text into the TextCache, but
126 // only if the buffer is still loaded.
127 // Also set the owner of the test to 0
128 // bv_->text->owner(0);
129 textcache.add(buffer_, workarea_->workWidth(), bv_->text);
130 if (lyxerr.debugging())
131 textcache.show(lyxerr, "BufferView::buffer");
136 // Set current buffer
139 if (bufferlist.getState() == BufferList::CLOSING) return;
142 // screen is always deleted when the buffer is changed.
146 // If we are closing the buffer, use the first buffer as current
148 buffer_ = bufferlist.first();
152 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
153 buffer_->addUser(bv_);
154 // If we don't have a text object for this, we make one
155 if (bv_->text == 0) {
156 resizeCurrentBuffer();
161 bv_->text->first = screen_->TopCursorVisible(bv_->text);
162 owner_->updateMenubar();
163 owner_->updateToolbar();
165 owner_->getDialogs()->updateBufferDependent();
168 lyxerr[Debug::INFO] << " No Buffer!" << endl;
169 owner_->updateMenubar();
170 owner_->updateToolbar();
174 // Also remove all remaining text's from the testcache.
175 // (there should not be any!) (if there is any it is a
177 if (lyxerr.debugging())
178 textcache.show(lyxerr, "buffer delete all");
181 // should update layoutchoice even if we don't have a buffer.
182 owner_->updateLayoutChoice();
183 owner_->getMiniBuffer()->Init();
184 owner_->updateWindowTitle();
188 void BufferView::Pimpl::resize(int xpos, int ypos, int width, int height)
190 workarea_->resize(xpos, ypos, width, height);
196 void BufferView::Pimpl::resize()
198 // This will resize the buffer. (Asger)
200 resizeCurrentBuffer();
204 void BufferView::Pimpl::redraw()
206 lyxerr[Debug::INFO] << "BufferView::redraw()" << endl;
211 bool BufferView::Pimpl::fitCursor(LyXText * text)
213 Assert(screen_); // it is a programming error to call fitCursor
214 // without a valid screen.
215 bool ret = screen_->FitCursor(text);
222 void BufferView::Pimpl::redoCurrentBuffer()
224 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
225 if (buffer_ && bv_->text) {
227 owner_->updateLayoutChoice();
232 int BufferView::Pimpl::resizeCurrentBuffer()
234 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
236 LyXParagraph * par = 0;
237 LyXParagraph * selstartpar = 0;
238 LyXParagraph * selendpar = 0;
247 owner_->getMiniBuffer()->Set(_("Formatting document..."));
250 par = bv_->text->cursor.par();
251 pos = bv_->text->cursor.pos();
252 selstartpar = bv_->text->sel_start_cursor.par();
253 selstartpos = bv_->text->sel_start_cursor.pos();
254 selendpar = bv_->text->sel_end_cursor.par();
255 selendpos = bv_->text->sel_end_cursor.pos();
256 selection = bv_->text->selection;
257 mark_set = bv_->text->mark_set;
259 bv_->text = new LyXText(bv_);
261 // See if we have a text in TextCache that fits
262 // the new buffer_ with the correct width.
263 bv_->text = textcache.findFit(buffer_, workarea_->workWidth());
265 if (lyxerr.debugging()) {
266 lyxerr << "Found a LyXText that fits:\n";
267 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea_->workWidth(), bv_->text)));
269 // Set the owner of the newly found text
270 // bv_->text->owner(bv_);
271 if (lyxerr.debugging())
272 textcache.show(lyxerr, "resizeCurrentBuffer");
274 bv_->text = new LyXText(bv_);
280 bv_->text->selection = true;
281 /* at this point just to avoid the Delete-Empty-Paragraph
282 * Mechanism when setting the cursor */
283 bv_->text->mark_set = mark_set;
285 bv_->text->SetCursor(bv_, selstartpar, selstartpos);
286 bv_->text->sel_cursor = bv_->text->cursor;
287 bv_->text->SetCursor(bv_, selendpar, selendpos);
288 bv_->text->SetSelection();
289 bv_->text->SetCursor(bv_, par, pos);
291 bv_->text->SetCursor(bv_, par, pos);
292 bv_->text->sel_cursor = bv_->text->cursor;
293 bv_->text->selection = false;
296 bv_->text->first = screen_->TopCursorVisible(bv_->text);
297 buffer_->resizeInsets(bv_);
298 // this will scroll the screen such that the cursor becomes visible
301 owner_->getMiniBuffer()->Init();
305 // Now if the title form still exist kill it
312 void BufferView::Pimpl::gotoError()
317 screen_->HideCursor();
319 update(BufferView::SELECT|BufferView::FITCUR);
322 if (!bv_->text->GotoNextError(bv_)) {
323 if (bv_->text->cursor.pos()
324 || bv_->text->cursor.par() != bv_->text->FirstParagraph()) {
325 tmp = bv_->text->cursor;
326 bv_->text->cursor.par(bv_->text->FirstParagraph());
327 bv_->text->cursor.pos(0);
328 if (!bv_->text->GotoNextError(bv_)) {
329 bv_->text->cursor = tmp;
330 owner_->getMiniBuffer()
331 ->Set(_("No more errors"));
335 owner_->getMiniBuffer()->Set(_("No more errors"));
339 update(BufferView::SELECT|BufferView::FITCUR);
340 bv_->text->sel_cursor = bv_->text->cursor;
344 void BufferView::Pimpl::updateScreen()
346 // Regenerate the screen.
348 screen_ = new LyXScreen(*workarea_);
352 void BufferView::Pimpl::updateScrollbar()
354 /* If the text is smaller than the working area, the scrollbar
355 * maximum must be the working area height. No scrolling will
359 workarea_->setScrollbar(0, 1.0);
363 static unsigned long max2 = 0;
364 static unsigned long height2 = 0;
366 unsigned long cbth = 0;
370 cbth = bv_->text->height;
371 cbsf = bv_->text->first;
374 // check if anything has changed.
376 height2 == workarea_->height() &&
377 current_scrollbar_value == cbsf)
380 height2 = workarea_->height();
381 current_scrollbar_value = cbsf;
383 if (cbth <= height2) { // text is smaller than screen
384 workarea_->setScrollbar(0, 1.0); // right?
388 long maximum_height = workarea_->height() * 3 / 4 + cbth;
392 double hfloat = workarea_->height();
393 double maxfloat = maximum_height;
395 float slider_size = 0.0;
396 int slider_value = value;
398 workarea_->setScrollbarBounds(0, bv_->text->height - workarea_->height());
399 double lineh = bv_->text->DefaultHeight();
400 workarea_->setScrollbarIncrements(lineh);
401 if (maxfloat > 0.0) {
402 if ((hfloat / maxfloat) * float(height2) < 3)
403 slider_size = 3.0/float(height2);
405 slider_size = hfloat / maxfloat;
407 slider_size = hfloat;
409 workarea_->setScrollbar(slider_value, slider_size / workarea_->height());
413 // Callback for scrollbar slider
414 void BufferView::Pimpl::scrollCB(double value)
416 extern bool cursor_follows_scrollbar;
418 if (buffer_ == 0) return;
420 current_scrollbar_value = long(value);
421 if (current_scrollbar_value < 0)
422 current_scrollbar_value = 0;
427 screen_->Draw(bv_->text, current_scrollbar_value);
429 if (cursor_follows_scrollbar) {
430 LyXText * vbt = bv_->text;
431 unsigned int height = vbt->DefaultHeight();
433 if (vbt->cursor.y() < bv_->text->first + height) {
434 vbt->SetCursorFromCoordinates(bv_, 0,
437 } else if (vbt->cursor.y() >
438 bv_->text->first + workarea_->height() - height) {
439 vbt->SetCursorFromCoordinates(bv_, 0,
441 workarea_->height() -
449 int BufferView::Pimpl::scrollUp(long time)
451 if (buffer_ == 0) return 0;
452 if (!screen_) return 0;
454 double value = workarea_->getScrollbarValue();
456 if (value == 0) return 0;
458 float add_value = (bv_->text->DefaultHeight()
459 + float(time) * float(time) * 0.125);
461 if (add_value > workarea_->height())
462 add_value = float(workarea_->height() -
463 bv_->text->DefaultHeight());
470 workarea_->setScrollbarValue(value);
472 bv_->scrollCB(value);
477 int BufferView::Pimpl::scrollDown(long time)
479 if (buffer_ == 0) return 0;
480 if (!screen_) return 0;
482 double value= workarea_->getScrollbarValue();
483 pair<float, float> p = workarea_->getScrollbarBounds();
484 double max = p.second;
486 if (value == max) return 0;
488 float add_value = (bv_->text->DefaultHeight()
489 + float(time) * float(time) * 0.125);
491 if (add_value > workarea_->height())
492 add_value = float(workarea_->height() -
493 bv_->text->DefaultHeight());
500 workarea_->setScrollbarValue(value);
502 bv_->scrollCB(value);
507 void BufferView::Pimpl::workAreaKeyPress(KeySym keysym, unsigned int state)
509 bv_->owner()->getLyXFunc()->processKeySym(keysym, state);
513 void BufferView::Pimpl::workAreaMotionNotify(int x, int y, unsigned int state)
515 // Only use motion with button 1
516 if (!(state & Button1MotionMask))
519 if (buffer_ == 0 || !screen_) return;
521 // Check for inset locking
522 if (bv_->the_locking_inset) {
523 LyXCursor cursor = bv_->text->cursor;
524 bv_->the_locking_inset->
525 InsetMotionNotify(bv_,
527 bv_->the_locking_inset->scroll(),
528 y - cursor.y() + bv_->text->first,
533 /* The selection possible is needed, that only motion events are
534 * used, where the bottom press event was on the drawing area too */
535 if (selection_possible) {
536 screen_->HideCursor();
538 bv_->text->SetCursorFromCoordinates(bv_, x, y + bv_->text->first);
540 if (!bv_->text->selection)
541 update(BufferView::UPDATE); // Maybe an empty line was deleted
543 bv_->text->SetSelection();
544 screen_->ToggleToggle(bv_->text);
545 fitCursor(bv_->text);
546 screen_->ShowCursor(bv_->text);
552 // Single-click on work area
553 void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
559 if (buffer_ == 0 || !screen_) return;
561 Inset * inset_hit = checkInsetHit(bv_->text, xpos, ypos, button);
563 // ok ok, this is a hack.
564 if (button == 4 || button == 5) {
567 scrollUp(lyxrc.wheel_jump); // default 100, set in lyxrc
570 scrollDown(lyxrc.wheel_jump);
575 if (bv_->the_locking_inset) {
576 // We are in inset locking mode
578 /* Check whether the inset was hit. If not reset mode,
579 otherwise give the event to the inset */
580 if (inset_hit == bv_->the_locking_inset) {
581 bv_->the_locking_inset->
582 InsetButtonPress(bv_,
587 bv_->unlockInset(bv_->the_locking_inset);
592 selection_possible = true;
593 screen_->HideCursor();
595 int const screen_first = bv_->text->first;
597 // Middle button press pastes if we have a selection
598 bool paste_internally = false;
600 && bv_->text->selection) {
601 owner_->getLyXFunc()->Dispatch(LFUN_COPY);
602 paste_internally = true;
605 // Clear the selection
606 screen_->ToggleSelection(bv_->text);
607 bv_->text->ClearSelection();
608 bv_->text->FullRebreak(bv_);
609 screen_->Update(bv_->text);
612 // Single left click in math inset?
613 if ((inset_hit != 0) &&
614 (inset_hit->Editable()==Inset::HIGHLY_EDITABLE)) {
615 // Highly editable inset, like math
616 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
617 selection_possible = false;
618 owner_->updateLayoutChoice();
619 owner_->getMiniBuffer()->Set(inset->EditMessage());
620 inset->InsetButtonPress(bv_, xpos, ypos, button);
621 inset->Edit(bv_, xpos, ypos, button);
625 // Right click on a footnote flag opens float menu
627 selection_possible = false;
631 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
632 bv_->text->SetCursorFromCoordinates(bv_, xpos, ypos + screen_first);
633 bv_->text->FinishUndo();
634 bv_->text->sel_cursor = bv_->text->cursor;
635 bv_->text->cursor.x_fix(bv_->text->cursor.x());
637 owner_->updateLayoutChoice();
638 if (fitCursor(bv_->text)) {
639 selection_possible = false;
642 // Insert primary selection with middle mouse
643 // if there is a local selection in the current buffer,
646 if (paste_internally)
647 owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
649 owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
651 selection_possible = false;
657 void BufferView::Pimpl::doubleClick(int /*x*/, int /*y*/, unsigned int button)
660 if (buffer_ && !bv_->the_locking_inset) {
661 if (screen_ && button == 1) {
662 screen_->HideCursor();
663 screen_->ToggleSelection(bv_->text);
664 bv_->text->SelectWord(bv_);
665 screen_->ToggleSelection(bv_->text, false);
666 /* This will fit the cursor on the screen
668 update(BufferView::SELECT|BufferView::FITCUR);
674 void BufferView::Pimpl::tripleClick(int /*x*/, int /*y*/, unsigned int button)
677 if (buffer_ && screen_ && !bv_->the_locking_inset && (button == 1)) {
678 screen_->HideCursor();
679 screen_->ToggleSelection(bv_->text);
680 bv_->text->CursorHome(bv_);
681 bv_->text->sel_cursor = bv_->text->cursor;
682 bv_->text->CursorEnd(bv_);
683 bv_->text->SetSelection();
684 screen_->ToggleSelection(bv_->text, false);
685 /* This will fit the cursor on the screen
687 update(BufferView::SELECT|BufferView::FITCUR);
692 void BufferView::Pimpl::enterView()
694 if (active() && available()) {
695 SetXtermCursor(workarea_->getWin());
696 using_xterm_cursor = true;
701 void BufferView::Pimpl::leaveView()
703 if (using_xterm_cursor) {
704 XUndefineCursor(fl_display, workarea_->getWin());
705 using_xterm_cursor = false;
710 void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
713 if (buffer_ == 0 || screen_ == 0) return;
715 // If we hit an inset, we have the inset coordinates in these
716 // and inset_hit points to the inset. If we do not hit an
717 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
718 Inset * inset_hit = checkInsetHit(bv_->text, x, y, button);
720 if (bv_->the_locking_inset) {
721 // We are in inset locking mode.
723 /* LyX does a kind of work-area grabbing for insets.
724 Only a ButtonPress Event outside the inset will
725 force a InsetUnlock. */
726 bv_->the_locking_inset->
727 InsetButtonRelease(bv_, x, y, button);
731 selection_possible = false;
733 if (button >= 2) return;
738 // Did we hit an editable inset?
739 if (inset_hit != 0) {
740 // Inset like error, notes and figures
741 selection_possible = false;
743 // CHECK fix this proper in 0.13
745 // Following a ref shouldn't issue
746 // a push on the undo-stack
747 // anylonger, now that we have
748 // keybindings for following
749 // references and returning from
750 // references. IMHO though, it
751 // should be the inset's own business
752 // to push or not push on the undo
753 // stack. They don't *have* to
754 // alter the document...
756 // ...or maybe the SetCursorParUndo()
757 // below isn't necessary at all anylonger?
758 if (inset_hit->LyxCode() == Inset::REF_CODE) {
759 bv_->text->SetCursorParUndo(bv_->buffer());
762 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
763 if (inset_hit->Editable()==Inset::HIGHLY_EDITABLE) {
764 // Highly editable inset, like math
765 UpdatableInset *inset = (UpdatableInset *)inset_hit;
766 inset->InsetButtonRelease(bv_, x, y, button);
768 inset_hit->InsetButtonRelease(bv_, x, y, button);
769 inset_hit->Edit(bv_, x, y, button);
774 // check whether we want to open a float
778 if (bv_->text->cursor.pos() <
779 bv_->text->cursor.par()->Last()) {
780 c = bv_->text->cursor.par()->
781 GetChar(bv_->text->cursor.pos());
784 if (c == LyXParagraph::META_FOOTNOTE
785 || c == LyXParagraph::META_MARGIN
786 || c == LyXParagraph::META_FIG
787 || c == LyXParagraph::META_TAB
788 || c == LyXParagraph::META_WIDE_FIG
789 || c == LyXParagraph::META_WIDE_TAB
790 || c == LyXParagraph::META_ALGORITHM){
794 if (bv_->text->cursor.pos() - 1 >= 0) {
795 c = bv_->text->cursor.par()->
796 GetChar(bv_->text->cursor.pos() - 1);
798 if (c == LyXParagraph::META_FOOTNOTE
799 || c == LyXParagraph::META_MARGIN
800 || c == LyXParagraph::META_FIG
801 || c == LyXParagraph::META_TAB
802 || c == LyXParagraph::META_WIDE_FIG
803 || c == LyXParagraph::META_WIDE_TAB
804 || c == LyXParagraph::META_ALGORITHM){
805 // We are one step too far to the right
806 bv_->text->CursorLeft(bv_);
815 selection_possible = false;
821 // Do we want to close a float? (click on the float-label)
822 if (bv_->text->cursor.row()->par()->footnoteflag ==
823 LyXParagraph::OPEN_FOOTNOTE
824 && bv_->text->cursor.row()->previous() &&
825 bv_->text->cursor.row()->previous()->par()->
826 footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
827 LyXFont font(LyXFont::ALL_SANE);
828 font.setSize(LyXFont::SIZE_FOOTNOTE);
830 int box_x = 20; // LYX_PAPER_MARGIN;
831 box_x += lyxfont::width(" wide-tab ", font);
833 unsigned int screen_first = bv_->text->first;
836 && y + screen_first > bv_->text->cursor.y() -
837 bv_->text->cursor.row()->baseline()
838 && y + screen_first < bv_->text->cursor.y() -
839 bv_->text->cursor.row()->baseline()
840 + lyxfont::maxAscent(font) * 1.2 + lyxfont::maxDescent(font) * 1.2) {
842 selection_possible = false;
848 // Maybe we want to edit a bibitem ale970302
849 if (bv_->text->cursor.par()->bibkey && x < 20 +
850 bibitemMaxWidth(bv_, textclasslist.
852 params.textclass).defaultfont())) {
853 bv_->text->cursor.par()->bibkey->Edit(bv_, 0, 0, 0);
861 * Returns an inset if inset was hit. 0 otherwise.
862 * If hit, the coordinates are changed relative to the inset.
863 * Otherwise coordinates are not changed, and false is returned.
865 Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y,
866 unsigned int /* button */)
871 unsigned int y_tmp = y + text->first;
874 text->SetCursorFromCoordinates(bv_, cursor, x, y_tmp);
876 if (cursor.pos() < cursor.par()->Last()
877 && cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
878 && cursor.par()->GetInset(cursor.pos())
879 && cursor.par()->GetInset(cursor.pos())->Editable()) {
881 // Check whether the inset really was hit
882 Inset * tmpinset = cursor.par()->GetInset(cursor.pos());
883 LyXFont font = text->GetFont(bv_->buffer(),
884 cursor.par(), cursor.pos());
885 bool is_rtl = font.isVisibleRightToLeft();
889 start_x = cursor.x() - tmpinset->width(bv_, font) + tmpinset->scroll();
890 end_x = cursor.x() + tmpinset->scroll();
892 start_x = cursor.x() + tmpinset->scroll();
893 end_x = cursor.x() + tmpinset->width(bv_, font) + tmpinset->scroll();
896 if (x > start_x && x < end_x
897 && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
898 && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
899 text->SetCursor(bv_, cursor.par(),cursor.pos(),true);
901 // The origin of an inset is on the baseline
902 y = y_tmp - (text->cursor.y());
907 if ((cursor.pos() - 1 >= 0) &&
908 (cursor.par()->GetChar(cursor.pos()-1) == LyXParagraph::META_INSET) &&
909 (cursor.par()->GetInset(cursor.pos() - 1)) &&
910 (cursor.par()->GetInset(cursor.pos() - 1)->Editable())) {
911 Inset * tmpinset = cursor.par()->GetInset(cursor.pos()-1);
912 LyXFont font = text->GetFont(bv_->buffer(), cursor.par(),
914 bool is_rtl = font.isVisibleRightToLeft();
918 start_x = cursor.x() - tmpinset->width(bv_, font) +
920 end_x = cursor.x() + tmpinset->scroll();
922 start_x = cursor.x() + tmpinset->scroll();
923 end_x = cursor.x() + tmpinset->width(bv_, font) +
926 if (x > start_x && x < end_x
927 && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
928 && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
930 if (move_cursor && (tmpinset != bv_->the_locking_inset))
932 text->SetCursor(bv_, cursor.par(),cursor.pos()-1,true);
934 // The origin of an inset is on the baseline
935 y = y_tmp - (text->cursor.y());
943 void BufferView::Pimpl::workAreaExpose()
945 // this is a hack to ensure that we only call this through
946 // BufferView::redraw().
951 static int work_area_width = 0;
952 static unsigned int work_area_height = 0;
954 bool widthChange = workarea_->workWidth() != work_area_width;
955 bool heightChange = workarea_->height() != work_area_height;
957 // update from work area
958 work_area_width = workarea_->workWidth();
959 work_area_height = workarea_->height();
962 // All buffers need a resize
965 // Remove all texts from the textcache
966 // This is not _really_ what we want to do. What
967 // we really want to do is to delete in textcache
968 // that does not have a BufferView with matching
969 // width, but as long as we have only one BufferView
970 // deleting all gives the same result.
971 if (lyxerr.debugging())
972 textcache.show(lyxerr, "Expose delete all");
974 } else if (heightChange) {
975 // Rebuild image of current screen
977 // fitCursor() ensures we don't jump back
978 // to the start of the document on vertical
980 fitCursor(bv_->text);
982 // The main window size has changed, repaint most stuff
984 // ...including the minibuffer
985 owner_->getMiniBuffer()->Init();
987 } else if (screen_) screen_->Redraw(bv_->text);
989 // Grey box when we don't have a buffer
990 workarea_->greyOut();
993 // always make sure that the scrollbar is sane.
995 owner_->updateLayoutChoice();
1000 void BufferView::Pimpl::update()
1002 if (screen_) screen_->Update(bv_->text);
1005 // Values used when calling update:
1007 // -2 - update, move sel_cursor if selection, fitcursor
1008 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
1009 // 0 - update, move sel_cursor if selection, fitcursor
1010 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
1011 // 3 - update, move sel_cursor if selection
1014 // a simple redraw of the parts that need refresh
1016 // move sel_cursor if selection -
1017 // the text's sel_cursor is moved if there is selection is progress
1020 // fitCursor() is called and the scrollbar updated
1023 // the buffer is marked dirty.
1032 // UPDATE_ONLY = UPDATE;
1033 // UPDATE_SELECT = UPDATE | SELECT;
1034 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
1035 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
1037 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
1038 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
1039 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1040 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1041 // update(3) -> update(1) -> update(1) -> update(SELECT)
1043 void BufferView::Pimpl::update(BufferView::UpdateCodes f)
1045 owner_->updateLayoutChoice();
1047 if (!bv_->text->selection && (f & SELECT)) {
1048 bv_->text->sel_cursor = bv_->text->cursor;
1051 bv_->text->FullRebreak(bv_);
1056 fitCursor(bv_->text);
1060 if (buffer_->isLyxClean()) {
1061 buffer_->markDirty();
1062 owner_->getMiniBuffer()->setTimer(4);
1064 buffer_->markDirty();
1070 // Callback for cursor timer
1071 void BufferView::Pimpl::cursorToggle()
1073 // Quite a nice place for asyncron Inset updating, isn't it?
1074 // Actually no! This is run even if no buffer exist... so (Lgb)
1076 goto set_timer_and_return;
1080 // On my quest to solve the gs render hangups I am now
1081 // disabling the SIGHUP completely, and will do a wait
1082 // now and then instead. If the guess that xforms somehow
1083 // destroys something is true, this is likely (hopefully)
1084 // to solve the problem...at least I hope so. Lgb
1086 // ...Ok this seems to work...at least it does not make things
1087 // worse so far. However I still see gs processes that hangs.
1088 // I would really like to know _why_ they are hanging. Anyway
1089 // the solution without the SIGCHLD handler seems to be easier
1092 // When attaching gdb to a a running gs that hangs it shows
1093 // that it is waiting for input(?) Is it possible for us to
1094 // provide that input somehow? Or figure what it is expecing
1097 // One solution is to, after some time, look if there are some
1098 // old gs processes still running and if there are: kill them
1101 // Another solution is to provide the user an option to rerender
1102 // a picture. This would, for the picture in question, check if
1103 // there is a gs running for it, if so kill it, and start a new
1104 // rendering process.
1106 // these comments posted to lyx@via
1109 int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1110 if (pid == -1) // error find out what is wrong
1111 ; // ignore it for now.
1113 sigchldhandler(pid, &status);
1116 updatelist.update(bv_);
1119 goto set_timer_and_return;
1122 if (!bv_->the_locking_inset) {
1123 screen_->CursorToggle(bv_->text);
1125 bv_->the_locking_inset->
1126 ToggleInsetCursor(bv_);
1129 set_timer_and_return:
1130 cursor_timeout.restart();
1135 void BufferView::Pimpl::cursorPrevious()
1137 if (!bv_->text->cursor.row()->previous()) return;
1139 int y = bv_->text->first;
1140 Row * cursorrow = bv_->text->cursor.row();
1141 bv_->text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y);
1142 bv_->text->FinishUndo();
1143 // This is to allow jumping over large insets
1144 if ((cursorrow == bv_->text->cursor.row()))
1145 bv_->text->CursorUp(bv_);
1147 if (bv_->text->cursor.row()->height() < workarea_->height())
1148 screen_->Draw(bv_->text,
1149 bv_->text->cursor.y()
1150 - bv_->text->cursor.row()->baseline()
1151 + bv_->text->cursor.row()->height()
1152 - workarea_->height() + 1 );
1157 void BufferView::Pimpl::cursorNext()
1159 if (!bv_->text->cursor.row()->next()) return;
1161 int y = bv_->text->first;
1162 bv_->text->GetRowNearY(y);
1163 Row * cursorrow = bv_->text->cursor.row();
1164 bv_->text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y
1165 + workarea_->height());
1166 bv_->text->FinishUndo();
1167 // This is to allow jumping over large insets
1168 if ((cursorrow == bv_->text->cursor.row()))
1169 bv_->text->CursorDown(bv_);
1171 if (bv_->text->cursor.row()->height() < workarea_->height())
1172 screen_->Draw(bv_->text, bv_->text->cursor.y()
1173 - bv_->text->cursor.row()->baseline());
1178 bool BufferView::Pimpl::available() const
1180 if (buffer_ && bv_->text) return true;
1185 void BufferView::Pimpl::beforeChange()
1188 bv_->text->ClearSelection();
1191 //owner_->update_timeout.stop();
1195 void BufferView::Pimpl::savePosition()
1197 backstack.push(buffer_->fileName(),
1198 bv_->text->cursor.x(),
1199 bv_->text->cursor.y());
1203 void BufferView::Pimpl::restorePosition()
1205 if (backstack.empty()) return;
1208 string fname = backstack.pop(&x, &y);
1212 if( fname != buffer_->fileName() ) {
1213 Buffer * b = bufferlist.exists(fname) ?
1214 bufferlist.getBuffer(fname) :
1215 bufferlist.loadLyXFile(fname); // don't ask, just load it
1216 if( b != 0 ) buffer(b);
1219 bv_->text->SetCursorFromCoordinates(bv_, x, y);
1220 update(BufferView::SELECT|BufferView::FITCUR);
1224 bool BufferView::Pimpl::NoSavedPositions()
1226 return backstack.empty();
1230 void BufferView::Pimpl::setState()
1232 if (!lyxrc.rtl_support)
1235 if (bv_->text->real_current_font.isRightToLeft() &&
1236 bv_->text->real_current_font.latex() != LyXFont::ON) {
1237 if (owner_->getIntl()->primarykeymap)
1238 owner_->getIntl()->KeyMapSec();
1240 if (!owner_->getIntl()->primarykeymap)
1241 owner_->getIntl()->KeyMapPrim();
1246 void BufferView::Pimpl::insetSleep()
1248 if (bv_->the_locking_inset && !bv_->inset_slept) {
1249 bv_->the_locking_inset->GetCursorPos(bv_, bv_->slx, bv_->sly);
1250 bv_->the_locking_inset->InsetUnlock(bv_);
1251 bv_->inset_slept = true;
1256 void BufferView::Pimpl::insetWakeup()
1258 if (bv_->the_locking_inset && bv_->inset_slept) {
1259 bv_->the_locking_inset->Edit(bv_, bv_->slx, bv_->sly, 0);
1260 bv_->inset_slept = false;
1265 void BufferView::Pimpl::insetUnlock()
1267 if (bv_->the_locking_inset) {
1268 if (!bv_->inset_slept) bv_->the_locking_inset->InsetUnlock(bv_);
1269 bv_->the_locking_inset = 0;
1270 bv_->text->FinishUndo();
1271 bv_->inset_slept = false;
1276 bool BufferView::Pimpl::focus() const
1278 return workarea_->hasFocus();
1282 void BufferView::Pimpl::focus(bool f)
1284 if (f) workarea_->setFocus();
1288 bool BufferView::Pimpl::active() const
1290 return workarea_->active();
1294 bool BufferView::Pimpl::belowMouse() const
1296 return workarea_->belowMouse();
1300 void BufferView::Pimpl::showCursor()
1303 screen_->ShowCursor(bv_->text);
1307 void BufferView::Pimpl::hideCursor()
1310 screen_->HideCursor();
1314 void BufferView::Pimpl::toggleSelection(bool b)
1317 screen_->ToggleSelection(bv_->text, b);
1321 void BufferView::Pimpl::toggleToggle()
1324 screen_->ToggleToggle(bv_->text);
1328 void BufferView::Pimpl::center()
1331 if (bv_->text->cursor.y() > workarea_->height() / 2) {
1332 screen_->Draw(bv_->text, bv_->text->cursor.y() - workarea_->height() / 2);
1334 screen_->Draw(bv_->text, 0);
1336 update(BufferView::SELECT|BufferView::FITCUR);
1341 void BufferView::Pimpl::pasteClipboard(bool asPara)
1343 if (buffer_ == 0) return;
1345 screen_->HideCursor();
1346 bv_->beforeChange();
1348 string clip(workarea_->getClipboard());
1350 if (clip.empty()) return;
1353 bv_->text->InsertStringB(bv_, clip);
1355 bv_->text->InsertStringA(bv_, clip);
1357 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1361 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
1363 workarea_->putClipboard(stuff);