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();
167 lyxerr[Debug::INFO] << " No Buffer!" << endl;
168 owner_->updateMenubar();
169 owner_->updateToolbar();
173 // Also remove all remaining text's from the testcache.
174 // (there should not be any!) (if there is any it is a
176 if (lyxerr.debugging())
177 textcache.show(lyxerr, "buffer delete all");
180 // should update layoutchoice even if we don't have a buffer.
181 owner_->updateLayoutChoice();
182 owner_->getMiniBuffer()->Init();
183 owner_->updateWindowTitle();
184 // Similarly, buffer-dependent dialogs should be updated or hidden.
185 // This should go here because some dialogs (ToC) require bv_->text.
186 owner_->getDialogs()->updateBufferDependent();
190 void BufferView::Pimpl::resize(int xpos, int ypos, int width, int height)
192 workarea_->resize(xpos, ypos, width, height);
198 void BufferView::Pimpl::resize()
200 // This will resize the buffer. (Asger)
202 resizeCurrentBuffer();
206 void BufferView::Pimpl::redraw()
208 lyxerr[Debug::INFO] << "BufferView::redraw()" << endl;
213 bool BufferView::Pimpl::fitCursor(LyXText * text)
215 Assert(screen_); // it is a programming error to call fitCursor
216 // without a valid screen.
217 bool ret = screen_->FitCursor(text);
224 void BufferView::Pimpl::redoCurrentBuffer()
226 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
227 if (buffer_ && bv_->text) {
229 owner_->updateLayoutChoice();
234 int BufferView::Pimpl::resizeCurrentBuffer()
236 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
238 LyXParagraph * par = 0;
239 LyXParagraph * selstartpar = 0;
240 LyXParagraph * selendpar = 0;
249 owner_->getMiniBuffer()->Set(_("Formatting document..."));
252 par = bv_->text->cursor.par();
253 pos = bv_->text->cursor.pos();
254 selstartpar = bv_->text->sel_start_cursor.par();
255 selstartpos = bv_->text->sel_start_cursor.pos();
256 selendpar = bv_->text->sel_end_cursor.par();
257 selendpos = bv_->text->sel_end_cursor.pos();
258 selection = bv_->text->selection;
259 mark_set = bv_->text->mark_set;
261 bv_->text = new LyXText(bv_);
263 // See if we have a text in TextCache that fits
264 // the new buffer_ with the correct width.
265 bv_->text = textcache.findFit(buffer_, workarea_->workWidth());
267 if (lyxerr.debugging()) {
268 lyxerr << "Found a LyXText that fits:\n";
269 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea_->workWidth(), bv_->text)));
271 // Set the owner of the newly found text
272 // bv_->text->owner(bv_);
273 if (lyxerr.debugging())
274 textcache.show(lyxerr, "resizeCurrentBuffer");
276 bv_->text = new LyXText(bv_);
282 bv_->text->selection = true;
283 /* at this point just to avoid the Delete-Empty-Paragraph
284 * Mechanism when setting the cursor */
285 bv_->text->mark_set = mark_set;
287 bv_->text->SetCursor(bv_, selstartpar, selstartpos);
288 bv_->text->sel_cursor = bv_->text->cursor;
289 bv_->text->SetCursor(bv_, selendpar, selendpos);
290 bv_->text->SetSelection();
291 bv_->text->SetCursor(bv_, par, pos);
293 bv_->text->SetCursor(bv_, par, pos);
294 bv_->text->sel_cursor = bv_->text->cursor;
295 bv_->text->selection = false;
298 bv_->text->first = screen_->TopCursorVisible(bv_->text);
299 buffer_->resizeInsets(bv_);
300 // this will scroll the screen such that the cursor becomes visible
303 owner_->getMiniBuffer()->Init();
307 // Now if the title form still exist kill it
314 void BufferView::Pimpl::gotoError()
319 screen_->HideCursor();
321 update(BufferView::SELECT|BufferView::FITCUR);
324 if (!bv_->text->GotoNextError(bv_)) {
325 if (bv_->text->cursor.pos()
326 || bv_->text->cursor.par() != bv_->text->FirstParagraph()) {
327 tmp = bv_->text->cursor;
328 bv_->text->cursor.par(bv_->text->FirstParagraph());
329 bv_->text->cursor.pos(0);
330 if (!bv_->text->GotoNextError(bv_)) {
331 bv_->text->cursor = tmp;
332 owner_->getMiniBuffer()
333 ->Set(_("No more errors"));
337 owner_->getMiniBuffer()->Set(_("No more errors"));
341 update(BufferView::SELECT|BufferView::FITCUR);
342 bv_->text->sel_cursor = bv_->text->cursor;
346 void BufferView::Pimpl::updateScreen()
348 // Regenerate the screen.
350 screen_ = new LyXScreen(*workarea_);
354 void BufferView::Pimpl::updateScrollbar()
356 /* If the text is smaller than the working area, the scrollbar
357 * maximum must be the working area height. No scrolling will
361 workarea_->setScrollbar(0, 1.0);
365 static unsigned long max2 = 0;
366 static unsigned long height2 = 0;
368 unsigned long cbth = 0;
372 cbth = bv_->text->height;
373 cbsf = bv_->text->first;
376 // check if anything has changed.
378 height2 == workarea_->height() &&
379 current_scrollbar_value == cbsf)
382 height2 = workarea_->height();
383 current_scrollbar_value = cbsf;
385 if (cbth <= height2) { // text is smaller than screen
386 workarea_->setScrollbar(0, 1.0); // right?
390 long maximum_height = workarea_->height() * 3 / 4 + cbth;
394 double hfloat = workarea_->height();
395 double maxfloat = maximum_height;
397 float slider_size = 0.0;
398 int slider_value = value;
400 workarea_->setScrollbarBounds(0, bv_->text->height - workarea_->height());
401 double lineh = bv_->text->DefaultHeight();
402 workarea_->setScrollbarIncrements(lineh);
403 if (maxfloat > 0.0) {
404 if ((hfloat / maxfloat) * float(height2) < 3)
405 slider_size = 3.0/float(height2);
407 slider_size = hfloat / maxfloat;
409 slider_size = hfloat;
411 workarea_->setScrollbar(slider_value, slider_size / workarea_->height());
415 // Callback for scrollbar slider
416 void BufferView::Pimpl::scrollCB(double value)
418 extern bool cursor_follows_scrollbar;
420 if (buffer_ == 0) return;
422 current_scrollbar_value = long(value);
423 if (current_scrollbar_value < 0)
424 current_scrollbar_value = 0;
429 screen_->Draw(bv_->text, current_scrollbar_value);
431 if (cursor_follows_scrollbar) {
432 LyXText * vbt = bv_->text;
433 int height = vbt->DefaultHeight();
435 if (vbt->cursor.y() < (int)(bv_->text->first + height)) {
436 vbt->SetCursorFromCoordinates(bv_, 0,
439 } else if (vbt->cursor.y() >
440 (int)(bv_->text->first+workarea_->height()-height))
442 vbt->SetCursorFromCoordinates(bv_, 0,
444 workarea_->height() -
452 int BufferView::Pimpl::scrollUp(long time)
454 if (buffer_ == 0) return 0;
455 if (!screen_) return 0;
457 double value = workarea_->getScrollbarValue();
459 if (value == 0) return 0;
461 float add_value = (bv_->text->DefaultHeight()
462 + float(time) * float(time) * 0.125);
464 if (add_value > workarea_->height())
465 add_value = float(workarea_->height() -
466 bv_->text->DefaultHeight());
473 workarea_->setScrollbarValue(value);
475 bv_->scrollCB(value);
480 int BufferView::Pimpl::scrollDown(long time)
482 if (buffer_ == 0) return 0;
483 if (!screen_) return 0;
485 double value= workarea_->getScrollbarValue();
486 pair<float, float> p = workarea_->getScrollbarBounds();
487 double max = p.second;
489 if (value == max) return 0;
491 float add_value = (bv_->text->DefaultHeight()
492 + float(time) * float(time) * 0.125);
494 if (add_value > workarea_->height())
495 add_value = float(workarea_->height() -
496 bv_->text->DefaultHeight());
503 workarea_->setScrollbarValue(value);
505 bv_->scrollCB(value);
510 void BufferView::Pimpl::workAreaKeyPress(KeySym keysym, unsigned int state)
512 bv_->owner()->getLyXFunc()->processKeySym(keysym, state);
516 void BufferView::Pimpl::workAreaMotionNotify(int x, int y, unsigned int state)
518 // Only use motion with button 1
519 if (!(state & Button1MotionMask))
522 if (buffer_ == 0 || !screen_) return;
524 // Check for inset locking
525 if (bv_->theLockingInset()) {
526 LyXCursor cursor = bv_->text->cursor;
527 bv_->theLockingInset()->
528 InsetMotionNotify(bv_,
530 bv_->theLockingInset()->scroll(),
531 y - cursor.y() + bv_->text->first,
536 /* The selection possible is needed, that only motion events are
537 * used, where the bottom press event was on the drawing area too */
538 if (selection_possible) {
539 screen_->HideCursor();
541 bv_->text->SetCursorFromCoordinates(bv_, x, y + bv_->text->first);
543 if (!bv_->text->selection)
544 update(BufferView::UPDATE); // Maybe an empty line was deleted
546 bv_->text->SetSelection();
547 screen_->ToggleToggle(bv_->text);
548 fitCursor(bv_->text);
549 screen_->ShowCursor(bv_->text);
555 // Single-click on work area
556 void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
562 if (buffer_ == 0 || !screen_) return;
564 Inset * inset_hit = checkInsetHit(bv_->text, xpos, ypos, button);
566 // ok ok, this is a hack.
567 if (button == 4 || button == 5) {
570 scrollUp(lyxrc.wheel_jump); // default 100, set in lyxrc
573 scrollDown(lyxrc.wheel_jump);
578 if (bv_->theLockingInset()) {
579 // We are in inset locking mode
581 /* Check whether the inset was hit. If not reset mode,
582 otherwise give the event to the inset */
583 if (inset_hit == bv_->theLockingInset()) {
584 bv_->theLockingInset()->
585 InsetButtonPress(bv_,
590 bv_->unlockInset(bv_->theLockingInset());
595 selection_possible = true;
596 screen_->HideCursor();
598 int const screen_first = bv_->text->first;
600 // Middle button press pastes if we have a selection
601 bool paste_internally = false;
603 && bv_->text->selection) {
604 owner_->getLyXFunc()->Dispatch(LFUN_COPY);
605 paste_internally = true;
608 // Clear the selection
609 screen_->ToggleSelection(bv_->text);
610 bv_->text->ClearSelection();
611 bv_->text->FullRebreak(bv_);
612 screen_->Update(bv_->text);
615 // Single left click in math inset?
616 if ((inset_hit != 0) &&
617 (inset_hit->Editable()==Inset::HIGHLY_EDITABLE)) {
618 // Highly editable inset, like math
619 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
620 selection_possible = false;
621 owner_->updateLayoutChoice();
622 owner_->getMiniBuffer()->Set(inset->EditMessage());
623 inset->InsetButtonPress(bv_, xpos, ypos, button);
624 inset->Edit(bv_, xpos, ypos, button);
628 // Right click on a footnote flag opens float menu
630 selection_possible = false;
634 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
635 bv_->text->SetCursorFromCoordinates(bv_, xpos, ypos + screen_first);
636 bv_->text->FinishUndo();
637 bv_->text->sel_cursor = bv_->text->cursor;
638 bv_->text->cursor.x_fix(bv_->text->cursor.x());
640 owner_->updateLayoutChoice();
641 if (fitCursor(bv_->text)) {
642 selection_possible = false;
645 // Insert primary selection with middle mouse
646 // if there is a local selection in the current buffer,
649 if (paste_internally)
650 owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
652 owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
654 selection_possible = false;
660 void BufferView::Pimpl::doubleClick(int /*x*/, int /*y*/, unsigned int button)
663 if (buffer_ && !bv_->theLockingInset()) {
664 if (screen_ && button == 1) {
665 screen_->HideCursor();
666 screen_->ToggleSelection(bv_->text);
667 bv_->text->SelectWord(bv_);
668 screen_->ToggleSelection(bv_->text, false);
669 /* This will fit the cursor on the screen
671 update(BufferView::SELECT|BufferView::FITCUR);
677 void BufferView::Pimpl::tripleClick(int /*x*/, int /*y*/, unsigned int button)
680 if (buffer_ && screen_ && !bv_->theLockingInset() && (button == 1)) {
681 screen_->HideCursor();
682 screen_->ToggleSelection(bv_->text);
683 bv_->text->CursorHome(bv_);
684 bv_->text->sel_cursor = bv_->text->cursor;
685 bv_->text->CursorEnd(bv_);
686 bv_->text->SetSelection();
687 screen_->ToggleSelection(bv_->text, false);
688 /* This will fit the cursor on the screen
690 update(BufferView::SELECT|BufferView::FITCUR);
695 void BufferView::Pimpl::enterView()
697 if (active() && available()) {
698 SetXtermCursor(workarea_->getWin());
699 using_xterm_cursor = true;
704 void BufferView::Pimpl::leaveView()
706 if (using_xterm_cursor) {
707 XUndefineCursor(fl_display, workarea_->getWin());
708 using_xterm_cursor = false;
713 void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
716 if (buffer_ == 0 || screen_ == 0) return;
718 // If we hit an inset, we have the inset coordinates in these
719 // and inset_hit points to the inset. If we do not hit an
720 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
721 Inset * inset_hit = checkInsetHit(bv_->text, x, y, button);
723 if (bv_->theLockingInset()) {
724 // We are in inset locking mode.
726 /* LyX does a kind of work-area grabbing for insets.
727 Only a ButtonPress Event outside the inset will
728 force a InsetUnlock. */
729 bv_->theLockingInset()->
730 InsetButtonRelease(bv_, x, y, button);
734 selection_possible = false;
736 if (button >= 2) return;
741 // Did we hit an editable inset?
742 if (inset_hit != 0) {
743 // Inset like error, notes and figures
744 selection_possible = false;
746 // CHECK fix this proper in 0.13
748 // Following a ref shouldn't issue
749 // a push on the undo-stack
750 // anylonger, now that we have
751 // keybindings for following
752 // references and returning from
753 // references. IMHO though, it
754 // should be the inset's own business
755 // to push or not push on the undo
756 // stack. They don't *have* to
757 // alter the document...
759 // ...or maybe the SetCursorParUndo()
760 // below isn't necessary at all anylonger?
761 if (inset_hit->LyxCode() == Inset::REF_CODE) {
762 bv_->text->SetCursorParUndo(bv_->buffer());
765 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
766 if (inset_hit->Editable()==Inset::HIGHLY_EDITABLE) {
767 // Highly editable inset, like math
768 UpdatableInset *inset = (UpdatableInset *)inset_hit;
769 inset->InsetButtonRelease(bv_, x, y, button);
771 inset_hit->InsetButtonRelease(bv_, x, y, button);
772 inset_hit->Edit(bv_, x, y, button);
777 // check whether we want to open a float
781 if (bv_->text->cursor.pos() <
782 bv_->text->cursor.par()->Last()) {
783 c = bv_->text->cursor.par()->
784 GetChar(bv_->text->cursor.pos());
787 if (c == LyXParagraph::META_FOOTNOTE
788 || c == LyXParagraph::META_MARGIN
789 || c == LyXParagraph::META_FIG
790 || c == LyXParagraph::META_TAB
791 || c == LyXParagraph::META_WIDE_FIG
792 || c == LyXParagraph::META_WIDE_TAB
793 || c == LyXParagraph::META_ALGORITHM){
797 if (bv_->text->cursor.pos() - 1 >= 0) {
798 c = bv_->text->cursor.par()->
799 GetChar(bv_->text->cursor.pos() - 1);
801 if (c == LyXParagraph::META_FOOTNOTE
802 || c == LyXParagraph::META_MARGIN
803 || c == LyXParagraph::META_FIG
804 || c == LyXParagraph::META_TAB
805 || c == LyXParagraph::META_WIDE_FIG
806 || c == LyXParagraph::META_WIDE_TAB
807 || c == LyXParagraph::META_ALGORITHM){
808 // We are one step too far to the right
809 bv_->text->CursorLeft(bv_);
818 selection_possible = false;
824 // Do we want to close a float? (click on the float-label)
825 if (bv_->text->cursor.row()->par()->footnoteflag ==
826 LyXParagraph::OPEN_FOOTNOTE
827 && bv_->text->cursor.row()->previous() &&
828 bv_->text->cursor.row()->previous()->par()->
829 footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
830 LyXFont font(LyXFont::ALL_SANE);
831 font.setSize(LyXFont::SIZE_FOOTNOTE);
833 int box_x = 20; // LYX_PAPER_MARGIN;
834 box_x += lyxfont::width(" wide-tab ", font);
836 unsigned int screen_first = bv_->text->first;
839 && y + screen_first > bv_->text->cursor.y() -
840 bv_->text->cursor.row()->baseline()
841 && y + screen_first < bv_->text->cursor.y() -
842 bv_->text->cursor.row()->baseline()
843 + lyxfont::maxAscent(font) * 1.2 + lyxfont::maxDescent(font) * 1.2) {
845 selection_possible = false;
851 // Maybe we want to edit a bibitem ale970302
852 if (bv_->text->cursor.par()->bibkey && x < 20 +
853 bibitemMaxWidth(bv_, textclasslist.
855 params.textclass).defaultfont())) {
856 bv_->text->cursor.par()->bibkey->Edit(bv_, 0, 0, 0);
864 * Returns an inset if inset was hit. 0 otherwise.
865 * If hit, the coordinates are changed relative to the inset.
866 * Otherwise coordinates are not changed, and false is returned.
868 Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y,
869 unsigned int /* button */)
874 int y_tmp = y + text->first;
877 text->SetCursorFromCoordinates(bv_, cursor, x, y_tmp);
879 if (cursor.pos() < cursor.par()->Last()
880 && cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
881 && cursor.par()->GetInset(cursor.pos())
882 && cursor.par()->GetInset(cursor.pos())->Editable()) {
884 // Check whether the inset really was hit
885 Inset * tmpinset = cursor.par()->GetInset(cursor.pos());
886 LyXFont font = text->GetFont(bv_->buffer(),
887 cursor.par(), cursor.pos());
888 bool is_rtl = font.isVisibleRightToLeft();
893 start_x = cursor.x() - tmpinset->width(bv_, font) + tmpinset->scroll();
894 end_x = cursor.x() + tmpinset->scroll();
896 start_x = cursor.x() + tmpinset->scroll();
897 end_x = cursor.x() + tmpinset->width(bv_, font) + tmpinset->scroll();
900 if (x > start_x && x < end_x
901 && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
902 && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
903 text->SetCursor(bv_, cursor.par(),cursor.pos(),true);
905 // The origin of an inset is on the baseline
906 y = y_tmp - (text->cursor.y());
911 if ((cursor.pos() - 1 >= 0) &&
912 (cursor.par()->GetChar(cursor.pos()-1) == LyXParagraph::META_INSET) &&
913 (cursor.par()->GetInset(cursor.pos() - 1)) &&
914 (cursor.par()->GetInset(cursor.pos() - 1)->Editable())) {
915 Inset * tmpinset = cursor.par()->GetInset(cursor.pos()-1);
916 LyXFont font = text->GetFont(bv_->buffer(), cursor.par(),
918 bool is_rtl = font.isVisibleRightToLeft();
923 start_x = cursor.x() - tmpinset->width(bv_, font) +
925 end_x = cursor.x() + tmpinset->scroll();
927 start_x = cursor.x() + tmpinset->scroll();
928 end_x = cursor.x() + tmpinset->width(bv_, font) +
931 if (x > start_x && x < end_x
932 && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
933 && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
935 if (move_cursor && (tmpinset != bv_->theLockingInset()))
937 text->SetCursor(bv_, cursor.par(),cursor.pos()-1,true);
939 // The origin of an inset is on the baseline
940 y = y_tmp - (text->cursor.y());
948 void BufferView::Pimpl::workAreaExpose()
950 // this is a hack to ensure that we only call this through
951 // BufferView::redraw().
956 static int work_area_width = 0;
957 static unsigned int work_area_height = 0;
959 bool widthChange = workarea_->workWidth() != work_area_width;
960 bool heightChange = workarea_->height() != work_area_height;
962 // update from work area
963 work_area_width = workarea_->workWidth();
964 work_area_height = workarea_->height();
967 // All buffers need a resize
970 // Remove all texts from the textcache
971 // This is not _really_ what we want to do. What
972 // we really want to do is to delete in textcache
973 // that does not have a BufferView with matching
974 // width, but as long as we have only one BufferView
975 // deleting all gives the same result.
976 if (lyxerr.debugging())
977 textcache.show(lyxerr, "Expose delete all");
979 } else if (heightChange) {
980 // Rebuild image of current screen
982 // fitCursor() ensures we don't jump back
983 // to the start of the document on vertical
985 fitCursor(bv_->text);
987 // The main window size has changed, repaint most stuff
989 // ...including the minibuffer
990 owner_->getMiniBuffer()->Init();
992 } else if (screen_) screen_->Redraw(bv_->text);
994 // Grey box when we don't have a buffer
995 workarea_->greyOut();
998 // always make sure that the scrollbar is sane.
1000 owner_->updateLayoutChoice();
1005 void BufferView::Pimpl::update()
1007 if (screen_) screen_->Update(bv_->text);
1010 // Values used when calling update:
1012 // -2 - update, move sel_cursor if selection, fitcursor
1013 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
1014 // 0 - update, move sel_cursor if selection, fitcursor
1015 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
1016 // 3 - update, move sel_cursor if selection
1019 // a simple redraw of the parts that need refresh
1021 // move sel_cursor if selection -
1022 // the text's sel_cursor is moved if there is selection is progress
1025 // fitCursor() is called and the scrollbar updated
1028 // the buffer is marked dirty.
1037 // UPDATE_ONLY = UPDATE;
1038 // UPDATE_SELECT = UPDATE | SELECT;
1039 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
1040 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
1042 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
1043 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
1044 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1045 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1046 // update(3) -> update(1) -> update(1) -> update(SELECT)
1048 void BufferView::Pimpl::update(BufferView::UpdateCodes f)
1050 owner_->updateLayoutChoice();
1052 if (!bv_->text->selection && (f & SELECT)) {
1053 bv_->text->sel_cursor = bv_->text->cursor;
1056 bv_->text->FullRebreak(bv_);
1061 fitCursor(bv_->text);
1065 if (buffer_->isLyxClean()) {
1066 buffer_->markDirty();
1067 owner_->getMiniBuffer()->setTimer(4);
1069 buffer_->markDirty();
1075 // Callback for cursor timer
1076 void BufferView::Pimpl::cursorToggle()
1078 // Quite a nice place for asyncron Inset updating, isn't it?
1079 // Actually no! This is run even if no buffer exist... so (Lgb)
1081 goto set_timer_and_return;
1085 // On my quest to solve the gs render hangups I am now
1086 // disabling the SIGHUP completely, and will do a wait
1087 // now and then instead. If the guess that xforms somehow
1088 // destroys something is true, this is likely (hopefully)
1089 // to solve the problem...at least I hope so. Lgb
1091 // ...Ok this seems to work...at least it does not make things
1092 // worse so far. However I still see gs processes that hangs.
1093 // I would really like to know _why_ they are hanging. Anyway
1094 // the solution without the SIGCHLD handler seems to be easier
1097 // When attaching gdb to a a running gs that hangs it shows
1098 // that it is waiting for input(?) Is it possible for us to
1099 // provide that input somehow? Or figure what it is expecing
1102 // One solution is to, after some time, look if there are some
1103 // old gs processes still running and if there are: kill them
1106 // Another solution is to provide the user an option to rerender
1107 // a picture. This would, for the picture in question, check if
1108 // there is a gs running for it, if so kill it, and start a new
1109 // rendering process.
1111 // these comments posted to lyx@via
1114 int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1115 if (pid == -1) // error find out what is wrong
1116 ; // ignore it for now.
1118 sigchldhandler(pid, &status);
1121 updatelist.update(bv_);
1124 goto set_timer_and_return;
1127 if (!bv_->theLockingInset()) {
1128 screen_->CursorToggle(bv_->text);
1130 bv_->theLockingInset()->ToggleInsetCursor(bv_);
1133 set_timer_and_return:
1134 cursor_timeout.restart();
1139 void BufferView::Pimpl::cursorPrevious()
1141 if (!bv_->text->cursor.row()->previous()) return;
1143 int y = bv_->text->first;
1144 Row * cursorrow = bv_->text->cursor.row();
1145 bv_->text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y);
1146 bv_->text->FinishUndo();
1147 // This is to allow jumping over large insets
1148 if ((cursorrow == bv_->text->cursor.row()))
1149 bv_->text->CursorUp(bv_);
1151 if (bv_->text->cursor.row()->height() < workarea_->height())
1152 screen_->Draw(bv_->text,
1153 bv_->text->cursor.y()
1154 - bv_->text->cursor.row()->baseline()
1155 + bv_->text->cursor.row()->height()
1156 - workarea_->height() + 1 );
1161 void BufferView::Pimpl::cursorNext()
1163 if (!bv_->text->cursor.row()->next()) return;
1165 int y = bv_->text->first;
1166 bv_->text->GetRowNearY(y);
1167 Row * cursorrow = bv_->text->cursor.row();
1168 bv_->text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y
1169 + workarea_->height());
1170 bv_->text->FinishUndo();
1171 // This is to allow jumping over large insets
1172 if ((cursorrow == bv_->text->cursor.row()))
1173 bv_->text->CursorDown(bv_);
1175 if (bv_->text->cursor.row()->height() < workarea_->height())
1176 screen_->Draw(bv_->text, bv_->text->cursor.y()
1177 - bv_->text->cursor.row()->baseline());
1182 bool BufferView::Pimpl::available() const
1184 if (buffer_ && bv_->text) return true;
1189 void BufferView::Pimpl::beforeChange()
1192 bv_->text->ClearSelection();
1195 //owner_->update_timeout.stop();
1199 void BufferView::Pimpl::savePosition()
1201 backstack.push(buffer_->fileName(),
1202 bv_->text->cursor.x(),
1203 bv_->text->cursor.y());
1207 void BufferView::Pimpl::restorePosition()
1209 if (backstack.empty()) return;
1212 string fname = backstack.pop(&x, &y);
1216 if( fname != buffer_->fileName() ) {
1217 Buffer * b = bufferlist.exists(fname) ?
1218 bufferlist.getBuffer(fname) :
1219 bufferlist.loadLyXFile(fname); // don't ask, just load it
1220 if( b != 0 ) buffer(b);
1223 bv_->text->SetCursorFromCoordinates(bv_, x, y);
1224 update(BufferView::SELECT|BufferView::FITCUR);
1228 bool BufferView::Pimpl::NoSavedPositions()
1230 return backstack.empty();
1234 void BufferView::Pimpl::setState()
1236 if (!lyxrc.rtl_support)
1239 if (bv_->text->real_current_font.isRightToLeft() &&
1240 bv_->text->real_current_font.latex() != LyXFont::ON) {
1241 if (owner_->getIntl()->primarykeymap)
1242 owner_->getIntl()->KeyMapSec();
1244 if (!owner_->getIntl()->primarykeymap)
1245 owner_->getIntl()->KeyMapPrim();
1250 void BufferView::Pimpl::insetSleep()
1252 if (bv_->theLockingInset() && !bv_->inset_slept) {
1253 bv_->theLockingInset()->GetCursorPos(bv_, bv_->slx, bv_->sly);
1254 bv_->theLockingInset()->InsetUnlock(bv_);
1255 bv_->inset_slept = true;
1260 void BufferView::Pimpl::insetWakeup()
1262 if (bv_->theLockingInset() && bv_->inset_slept) {
1263 bv_->theLockingInset()->Edit(bv_, bv_->slx, bv_->sly, 0);
1264 bv_->inset_slept = false;
1269 void BufferView::Pimpl::insetUnlock()
1271 if (bv_->theLockingInset()) {
1272 if (!bv_->inset_slept)
1273 bv_->theLockingInset()->InsetUnlock(bv_);
1274 bv_->theLockingInset(0);
1275 bv_->text->FinishUndo();
1276 bv_->inset_slept = false;
1281 bool BufferView::Pimpl::focus() const
1283 return workarea_->hasFocus();
1287 void BufferView::Pimpl::focus(bool f)
1289 if (f) workarea_->setFocus();
1293 bool BufferView::Pimpl::active() const
1295 return workarea_->active();
1299 bool BufferView::Pimpl::belowMouse() const
1301 return workarea_->belowMouse();
1305 void BufferView::Pimpl::showCursor()
1308 screen_->ShowCursor(bv_->text);
1312 void BufferView::Pimpl::hideCursor()
1315 screen_->HideCursor();
1319 void BufferView::Pimpl::toggleSelection(bool b)
1322 screen_->ToggleSelection(bv_->text, b);
1326 void BufferView::Pimpl::toggleToggle()
1329 screen_->ToggleToggle(bv_->text);
1333 void BufferView::Pimpl::center()
1336 if (bv_->text->cursor.y() > (int)(workarea_->height() / 2)) {
1337 screen_->Draw(bv_->text, bv_->text->cursor.y() - workarea_->height() / 2);
1339 screen_->Draw(bv_->text, 0);
1341 update(BufferView::SELECT|BufferView::FITCUR);
1346 void BufferView::Pimpl::pasteClipboard(bool asPara)
1348 if (buffer_ == 0) return;
1350 screen_->HideCursor();
1351 bv_->beforeChange();
1353 string clip(workarea_->getClipboard());
1355 if (clip.empty()) return;
1358 bv_->text->InsertStringB(bv_, clip);
1360 bv_->text->InsertStringA(bv_, clip);
1362 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1366 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
1368 workarea_->putClipboard(stuff);